diff --git a/.coveragerc b/.coveragerc index 1a87ab2da86..7438377e667 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,2 +1,24 @@ +[run] +omit = */setup.py +source = + acme + certbot + certbot-apache + certbot-dns-cloudflare + certbot-dns-digitalocean + certbot-dns-dnsimple + certbot-dns-dnsmadeeasy + certbot-dns-gehirn + certbot-dns-google + certbot-dns-linode + certbot-dns-luadns + certbot-dns-nsone + certbot-dns-ovh + certbot-dns-rfc2136 + certbot-dns-route53 + certbot-dns-sakuracloud + certbot-nginx + [report] omit = */setup.py +show_missing = True diff --git a/.dockerignore b/.dockerignore index b94bf796011..2ce8a820983 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,5 +8,4 @@ .git .tox venv -venv3 docs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000000..2fb012b1480 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +# https://editorconfig.org/ + +root = true + +[*] +insert_final_newline = true +trim_trailing_whitespace = true +end_of_line = lf + +[*.py] +indent_style = space +indent_size = 4 +charset = utf-8 +max_line_length = 100 + +[*.yaml] +indent_style = space +indent_size = 2 diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 00000000000..63f9ea15bd6 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @certbot/eff-devs diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000000..f02dc208677 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +custom: https://supporters.eff.org/donate/support-work-on-certbot diff --git a/.github/ISSUE_TEMPLATE/bug.yaml b/.github/ISSUE_TEMPLATE/bug.yaml new file mode 100644 index 00000000000..ab06f9f10b9 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug.yaml @@ -0,0 +1,69 @@ +name: Bug Report +description: File a bug report. +title: "[Bug]: " +type: Bug +body: + - type: markdown + attributes: + value: | + Thanks for taking the time to fill out this bug report! If you're + having trouble using Certbot and aren't sure you've found a bug, + please first try asking for help at https://community.letsencrypt.org/. + There is a much larger community there of people familiar with the + project who will be able to more quickly answer your questions. + - type: input + attributes: + label: OS + description: | + Describe your Operating System. Examples: Ubuntu 18.04, CentOS 8 Stream + placeholder: Ubuntu 24.04 + validations: + required: true + - type: input + attributes: + label: Installation method + description: | + How did you install Certbot? Examples: snap, pip, apt, yum + placeholder: snap + validations: + required: true + - type: input + attributes: + label: Certbot Version + description: | + If you're not sure, you can find this by running `certbot --version`. + placeholder: 1.0.0 + validations: + required: true + - type: textarea + id: what-happened + attributes: + label: What happened? + description: | + I ran this command and it produced this output. Example: + ``` + $ sudo certbot certonly -d adfsfasdf.asdfasdf --staging + Saving debug log to /var/log/letsencrypt/letsencrypt.log + Plugins selected: Authenticator nginx, Installer nginx + Requesting a certificate for example.com + An unexpected error occurred: + Invalid identifiers requested :: Cannot issue for "adfsfasdf.asdfasdf": Domain name does not end with a valid public suffix (TLD) + Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details. + ``` + placeholder: Tell us what you see! + validations: + required: true + - type: textarea + id: expected + attributes: + label: Expected behavior + description: Certbot's behavior differed from what I expected because. + placeholder: "What was expected?" + validations: + required: true + - type: textarea + id: logs + attributes: + label: Relevant log output + description: Here is a Certbot log showing the issue (if available). Logs are stored in `/var/log/letsencrypt` by default. Feel free to redact domains, e-mail and IP addresses as you see fit. + render: shell diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 00000000000..dc6a8263380 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,8 @@ +blank_issues_enabled: false +contact_links: + - name: Let's Encrypt Community Support + url: https://community.letsencrypt.org/ + about: If you're having trouble using Certbot and aren't sure you've found a bug or request for a new feature, please first try asking for help here. There is a much larger community there of people familiar with the project who will be able to more quickly answer your questions. + - name: Certbot Security Policy + url: https://github.com/certbot/certbot/security/advisories/new + about: Please report security vulnerabilities here. diff --git a/.github/ISSUE_TEMPLATE/feature.yaml b/.github/ISSUE_TEMPLATE/feature.yaml new file mode 100644 index 00000000000..2276a859833 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature.yaml @@ -0,0 +1,27 @@ +name: Feature Request +description: Suggest a new feature or improvement to Certbot +title: "[Feature Request]: " +type: Feature +body: + - type: textarea + id: problem + attributes: + label: What problem does this feature solve or what does it enhance? + description: Explain what this feature addresses, or the benefit it provides. + placeholder: For example, "Currently, users have to manually do X, which is time-consuming." + validations: + required: true + - type: textarea + id: solution + attributes: + label: Proposed Solution + description: Describe the solution you'd like to see implemented. + placeholder: For example, "Implement a new button that automatically does X." + validations: + required: true + - type: textarea + id: alternatives + attributes: + label: Alternatives Considered + description: Have you considered any alternative solutions? + placeholder: For example, "We considered Y, but Z is a better approach because..." diff --git a/.github/ISSUE_TEMPLATE/task.yaml b/.github/ISSUE_TEMPLATE/task.yaml new file mode 100644 index 00000000000..e9861de7f2f --- /dev/null +++ b/.github/ISSUE_TEMPLATE/task.yaml @@ -0,0 +1,15 @@ +name: Task +description: A codebase upkeep task such as managing deprecations and refactoring +title: "[Task]: " +type: Task +body: + - type: textarea + id: problem + attributes: + label: Task description + description: Describe the work that needs to happen, and why. + placeholder: > + For example, "In issue [link here], we noted that we cannot update [dependency] until + [something happens]. That thing has happened, so now we should update [dependency]." + validations: + required: true diff --git a/.github/codecov.yml b/.github/codecov.yml new file mode 100644 index 00000000000..23d2fbbe980 --- /dev/null +++ b/.github/codecov.yml @@ -0,0 +1,7 @@ +# This disables all reporting from codecov. Let's just set it up to collect +# data for now and then we can play with the settings here. +comment: false +coverage: + status: + project: off + patch: off diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 00000000000..fef6e0f6923 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,60 @@ + + +## AI Assistance Disclosure + + +_This PR is missing an AI assistance disclosure._ + + +## Certbot Team Priority Evidence + + + +_This PR is missing evidence of team priority agreement or alternative explanation._ + + +## Newsfragments Filename + + +_This PR is missing the newsfragments filename or non-distributed statement._ diff --git a/.github/workflows/assigned.yaml b/.github/workflows/assigned.yaml new file mode 100644 index 00000000000..e3b8a51a3c9 --- /dev/null +++ b/.github/workflows/assigned.yaml @@ -0,0 +1,25 @@ +name: Issue Assigned + +on: + issues: + types: [assigned] +permissions: {} # let's not use any permissions we don't need here +jobs: + send-mattermost-message: + runs-on: ubuntu-latest + steps: + # issue triggers in github actions can be dangerous like + # pull_request_target because they run with additional privileges in an + # environment containing values that can be controlled by an attacker. + # because of this, please take extra caution when modifying the steps taken + # by this workflow. for additional information, see + # https://github.com/certbot/certbot/pull/10490 + # + # we pin this action to a version tested and audited by certbot's + # maintainers for extra security. the full hash is used as doing so is + # recommended by zizmor + - uses: mattermost/action-mattermost-notify@ae31bb6f9e26a54336e79696f108a2c91cf55b4e + with: + MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_ASSIGN_WEBHOOK }} + TEXT: > + ${{ github.event.assignee.login }} assigned to "${{ github.event.issue.title }}": ${{ github.event.issue.html_url }} diff --git a/.github/workflows/create_changelog.yml b/.github/workflows/create_changelog.yml new file mode 100644 index 00000000000..efa447955d7 --- /dev/null +++ b/.github/workflows/create_changelog.yml @@ -0,0 +1,27 @@ +name: Create changelog +on: + workflow_call: +permissions: + contents: read + +jobs: + prepare: + name: Changelog + runs-on: ubuntu-latest + steps: + # If we change the output filename from `release_notes.md`, it should also be changed in .github/workflows/release.yml + - name: checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Prepare changelog + run: |- + CERTBOT_VERSION="$(cd certbot/src && python -c "import certbot; print(certbot.__version__)" && cd ~-)" + tools/extract_changelog.py "${CERTBOT_VERSION}" >> "${{ runner.temp }}/release_notes.md" + shell: bash + - name: Publish changelog + uses: actions/upload-artifact@v7.0.0 + with: + # If we change the artifact's name, it should also be changed in .github/workflows/release.yml + name: changelog + path: "${{ runner.temp }}/release_notes.md" diff --git a/.github/workflows/deploy_docker_images.yml b/.github/workflows/deploy_docker_images.yml new file mode 100644 index 00000000000..ff646bca372 --- /dev/null +++ b/.github/workflows/deploy_docker_images.yml @@ -0,0 +1,81 @@ +name: Deploy docker images +on: + workflow_call: + inputs: + dockerTag: + required: true + description: 'tag to assign docker images' + type: string + secrets: + DOCKERHUB_TOKEN: + required: true + +permissions: + contents: read + +env: + DOCKER_TAG: "${{ inputs.dockerTag }}" + +jobs: + # The credentials used in the following jobs are for the shared + # certbotbot account on Docker Hub. + # They are located under the certbot organization settings, + # under Secrets and Variables -> Actions. + # DOCKERHUB_USERNAME is saved as a variable. + # DOCKERHUB_TOKEN is a secret, and it is a PAT created by + # following the instructions at + # https://docs.docker.com/security/access-tokens/ + # with Read and Write permissions. The access token can be deleted + # on Docker Hub if these credentials need to be revoked. + # The password is a PAT following the advice given by + # https://github.com/docker/login-action?tab=readme-ov-file#docker-hub + publish_docker_by_arch: + name: Publish docker by arch + runs-on: + - 'ubuntu-24.04' + strategy: + fail-fast: false + matrix: + DOCKER_ARCH: + - arm32v6 + - arm64v8 + - amd64 + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Retrieve Docker images + uses: actions/download-artifact@v8.0.1 + with: + name: docker_${{ matrix.DOCKER_ARCH }} + path: "${{ github.workspace }}" + - name: Load Docker images + run: docker load --input ${{ github.workspace }}/images.tar + shell: bash + - name: Login to Docker Hub + uses: docker/login-action@v4.1.0 + with: + username: "${{ vars.DOCKERHUB_USERNAME }}" + password: "${{ secrets.DOCKERHUB_TOKEN }}" + - name: Deploy the Docker images by architecture + run: tools/docker/deploy_images.sh "$DOCKER_TAG" ${{ matrix.DOCKER_ARCH }} + shell: bash + publish_docker_multiarch: + name: Publish docker multiarch + needs: publish_docker_by_arch + runs-on: + - 'ubuntu-24.04' + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Login to Docker Hub + uses: docker/login-action@v4.1.0 + with: + username: "${{ vars.DOCKERHUB_USERNAME }}" + password: "${{ secrets.DOCKERHUB_TOKEN }}" + - name: Deploy the Docker multiarch manifests + run: tools/docker/deploy_manifests.sh "$DOCKER_TAG" all + shell: bash diff --git a/.github/workflows/deploy_snaps.yml b/.github/workflows/deploy_snaps.yml new file mode 100644 index 00000000000..e5493de5a59 --- /dev/null +++ b/.github/workflows/deploy_snaps.yml @@ -0,0 +1,86 @@ +name: Deploy snaps +on: + workflow_call: + inputs: + snapReleaseChannel: + description: 'snap channel to release to' + required: true + type: string + secrets: + SNAPCRAFTCFG: + required: true + +permissions: + contents: read + +env: + SNAP_RELEASE_CHANNEL: "${{ inputs.snapReleaseChannel }}" + +jobs: + # This job relies on credentials used to publish the Certbot snaps. This + # credential file was created by running: + # + # snapcraft logout + # snapcraft export-login --channels=beta,edge snapcraft.cfg + # (provide the shared snapcraft credentials when prompted) + # + # Then the contents of the file were added as a secret in Github + # with the name SNAPCRAFTCFG under the Secrets and Variables -> Actions + # section of the settings for the certbot organization. + # + # Revoking these credentials can be done by changing the password of the + # account used to generate the credentials. See + # https://forum.snapcraft.io/t/revoking-exported-credentials/19031 for more + # info. + publish_snap: + name: Publish snap + if: ${{ inputs.snapReleaseChannel == 'edge' || inputs.snapReleaseChannel == 'beta' }} + runs-on: + - 'ubuntu-24.04' + strategy: + fail-fast: false + matrix: + SNAP_ARCH: [amd64, armhf, arm64] + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Install dependencies + run: |- + sudo apt-get update + sudo apt-get install -y --no-install-recommends snapd + sudo snap install --classic snapcraft + shell: bash + - name: Retrieve Certbot snaps + if: ${{ matrix.SNAP_ARCH == 'armhf' }} + uses: actions/download-artifact@v8.0.1 + with: + name: snaps_${{ matrix.SNAP_ARCH }} + path: "${{ github.workspace }}/snap" + - name: Retrieve Certbot snaps + if: ${{ matrix.SNAP_ARCH != 'armhf' }} + uses: actions/download-artifact@v8.0.1 + with: + pattern: snap-*-${{ matrix.SNAP_ARCH }} + merge-multiple: true + path: "${{ github.workspace }}/snap" + - name: Display structure of downloaded files + run: ls -R "${{ github.workspace }}/snap" + - name: Publish to Snap store + run: |- + export SNAPCRAFT_STORE_CREDENTIALS="${{ secrets.SNAPCRAFTCFG }}" + for SNAP_FILE in snap/*.snap; do + tools/retry.sh eval snapcraft upload --release="${SNAP_RELEASE_CHANNEL}" "${SNAP_FILE}" + done + shell: bash + publish_snap_invalid: + # Fail instead of silently skipping snap release + name: Fail on invalid snapReleaseChannel + if: ${{ inputs.snapReleaseChannel != 'edge' && inputs.snapReleaseChannel != 'beta' }} + runs-on: + - 'ubuntu-latest' + steps: + - name: Fail + run: exit 1 + shell: bash diff --git a/.github/workflows/docker_packaging_jobs.yml b/.github/workflows/docker_packaging_jobs.yml new file mode 100644 index 00000000000..a2878097b02 --- /dev/null +++ b/.github/workflows/docker_packaging_jobs.yml @@ -0,0 +1,82 @@ +name: Docker packaging +on: + workflow_call: + inputs: + dockerTag: + description: 'tag to assign docker images' + type: string +permissions: + contents: read + +env: + DOCKER_TAG: ${{ inputs.dockerTag }} + +jobs: + docker_build: + name: Build ${{ matrix.DOCKER_ARCH }} + runs-on: + - ${{ matrix.run-on }} + strategy: + fail-fast: false + matrix: + include: + - DOCKER_ARCH: arm64v8 + run-on: ubuntu-24.04-arm + - DOCKER_ARCH: amd64 + run-on: ubuntu-24.04 + - DOCKER_ARCH: arm32v6 + run-on: ubuntu-24.04 + steps: + - name: checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Build the Docker images + # We don't filter for the Docker Hub organization to continue to allow + # easy testing of these scripts on forks. + run: tools/docker/build.sh "$DOCKER_TAG" ${{ matrix.DOCKER_ARCH }} + shell: bash + - name: Save the Docker images + run: |- + DOCKER_IMAGES=$(docker images --filter reference="*/certbot" --filter reference="*/dns-*" --format "{{.Repository}}") + docker save --output images.tar $DOCKER_IMAGES + shell: bash + # If the name of the tar file or artifact changes, the deploy stage will + # also need to be updated. + - name: Store Docker artifact + uses: actions/upload-artifact@v7.0.0 + with: + name: docker_${{ matrix.DOCKER_ARCH }} + path: "${{ github.workspace }}/images.tar" + docker_test: + name: Test ${{ matrix.DOCKER_ARCH }} + needs: + - docker_build + runs-on: + - ${{ matrix.run-on }} + strategy: + fail-fast: false + matrix: + include: + - DOCKER_ARCH: arm64v8 + run-on: ubuntu-24.04-arm + - DOCKER_ARCH: amd64 + run-on: ubuntu-24.04 + - DOCKER_ARCH: arm32v6 + run-on: ubuntu-24.04-arm + steps: + - name: checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Retrieve Docker images + uses: actions/download-artifact@v8.0.1 + with: + name: docker_${{ matrix.DOCKER_ARCH }} + path: "${{ github.workspace }}" + - name: Load Docker images + run: docker load --input ${{ github.workspace }}/images.tar + shell: bash + - name: Run integration tests for Docker images + run: tools/docker/test.sh "$DOCKER_TAG" ${{ matrix.DOCKER_ARCH }} + shell: bash diff --git a/.github/workflows/extended_tests_jobs.yml b/.github/workflows/extended_tests_jobs.yml new file mode 100644 index 00000000000..cbe364f7b47 --- /dev/null +++ b/.github/workflows/extended_tests_jobs.yml @@ -0,0 +1,58 @@ +name: Extended tests jobs +on: + workflow_call: + secrets: + AWS_TEST_FARM_PEM: + required: false + AWS_ACCESS_KEY_ID: + required: false + AWS_SECRET_ACCESS_KEY: + required: false +permissions: + contents: read + +jobs: + test: + name: '-' # Makes results viewing cleaner + strategy: + fail-fast: false + matrix: + PYTHON_VERSION: ['3.14'] + TOXENV: + - isolated-acme,isolated-certbot,isolated-apache,isolated-cloudflare,isolated-digitalocean,isolated-dnsimple,isolated-dnsmadeeasy,isolated-gehirn,isolated-google,isolated-linode,isolated-luadns,isolated-nsone,isolated-ovh,isolated-rfc2136,isolated-route53,isolated-sakuracloud,isolated-nginx + - nginx_compat + - modification + include: + - PYTHON_VERSION: '3.11' + TOXENV: py311 + - PYTHON_VERSION: '3.12' + TOXENV: py312 + - PYTHON_VERSION: '3.13' + TOXENV: py313 + - PYTHON_VERSION: '3.10' + TOXENV: integration-certbot-oldest + - PYTHON_VERSION: '3.10' + TOXENV: integration-nginx-oldest + - PYTHON_VERSION: '3.10' + TOXENV: integration + - PYTHON_VERSION: '3.11' + TOXENV: integration + - PYTHON_VERSION: '3.12' + TOXENV: integration + - PYTHON_VERSION: '3.13' + TOXENV: integration + # python 3.14 integration tests are not run here because they're run as + # part of the standard test suite + - PYTHON_VERSION: '3.12' + TOXENV: integration-dns-rfc2136 + - PYTHON_VERSION: '3.12' + TOXENV: test-farm-apache2 + uses: "./.github/workflows/tox_steps.yml" + with: + PYTHON_VERSION: "${{ matrix.PYTHON_VERSION }}" + TOXENV: "${{ matrix.TOXENV }}" + IMAGE_NAME: ubuntu-22.04 + secrets: + AWS_TEST_FARM_PEM: "${{ secrets.AWS_TEST_FARM_PEM }}" + AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}" + AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}" diff --git a/.github/workflows/full_test_suite.yml b/.github/workflows/full_test_suite.yml new file mode 100644 index 00000000000..a2e35d173d0 --- /dev/null +++ b/.github/workflows/full_test_suite.yml @@ -0,0 +1,39 @@ +# Advanced pipeline for running our full test suite on demand. +name: Full test suite +on: + push: + branches: + # When changing these triggers, please ensure the documentation under + # "Running tests in CI" is still correct. + - test-* + workflow_dispatch: +permissions: + contents: read + +jobs: + # These four jobs could be grouped in a separate workflow, but the github actions UI + # is much nicer if they are instead listed explicitly here. + standard_tests_jobs: + name: Standard tests + uses: "./.github/workflows/standard_tests_jobs.yml" + extended_tests_jobs: + name: Extended tests + uses: "./.github/workflows/extended_tests_jobs.yml" + secrets: + AWS_TEST_FARM_PEM: "${{ secrets.AWS_TEST_FARM_PEM }}" + AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}" + AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}" + docker_packaging_jobs: + name: Docker packaging + uses: "./.github/workflows/docker_packaging_jobs.yml" + with: + # We don't publish our Docker images in this pipeline, but when building them + # for testing, let's use the nightly tag. + dockerTag: nightly + snap_packaging_jobs: + name: Snap packaging + uses: "./.github/workflows/snap_packaging_jobs.yml" + with: + snapBuildTimeout: 5400 + secrets: + LAUNCHPAD_CREDENTIALS: "${{ secrets.LAUNCHPAD_CREDENTIALS }}" diff --git a/.github/workflows/merged.yaml b/.github/workflows/merged.yaml new file mode 100644 index 00000000000..838e02ff248 --- /dev/null +++ b/.github/workflows/merged.yaml @@ -0,0 +1,30 @@ +name: Merge Event + +on: + pull_request_target: + types: + - closed +permissions: {} # let's not use any permissions we don't need here +jobs: + if_merged: + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + steps: + # github actions workflows triggered by pull_request_target can be + # dangerous because they run with additional privileges in an environment + # containing values that can be controlled by an attacker. because of + # this, please take extra caution when modifying the steps taken by this + # workflow. for additional information, see + # https://github.com/certbot/certbot/pull/10490 + # + # we pin this action to a version tested and audited by certbot's + # maintainers for extra security. the full hash is used as doing so is + # recommended by zizmor + - uses: mattermost/action-mattermost-notify@ae31bb6f9e26a54336e79696f108a2c91cf55b4e + with: + MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_MERGE_WEBHOOK }} + TEXT: > + [${{ github.repository }}] | + [${{ github.event.pull_request.title }} + #${{ github.event.number }}](https://github.com/${{ github.repository }}/pull/${{ github.event.number }}) + was merged into ${{ github.event.pull_request.base.ref }} by ${{ github.actor }} diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml new file mode 100644 index 00000000000..a47d3b499be --- /dev/null +++ b/.github/workflows/nightly.yml @@ -0,0 +1,121 @@ +# Nightly pipeline running each day for main. +name: Nightly build +on: + schedule: + - cron: 30 4 * * * + workflow_dispatch: + +jobs: + # While many of these jobs could be grouped in a separate workflow, the github actions UI + # is much nicer if they are instead listed explicitly here. As a result, changes made here + # may need to be mirrored in .github/workflows/release.yml. + ########################### + #### testing jobs ### + ########################### + standard_tests_jobs: + name: Standard tests + permissions: + contents: read + uses: "./.github/workflows/standard_tests_jobs.yml" + extended_tests_jobs: + name: Extended tests + permissions: + contents: read + uses: "./.github/workflows/extended_tests_jobs.yml" + secrets: + AWS_TEST_FARM_PEM: "${{ secrets.AWS_TEST_FARM_PEM }}" + AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}" + AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}" + ########################### + #### packaging jobs ### + ########################### + docker_packaging_jobs: + name: Docker packaging + permissions: + contents: read + uses: "./.github/workflows/docker_packaging_jobs.yml" + with: + dockerTag: nightly + snap_packaging_jobs: + name: Snap packaging + permissions: + contents: read + uses: "./.github/workflows/snap_packaging_jobs.yml" + with: + snapBuildTimeout: 19800 + secrets: + LAUNCHPAD_CREDENTIALS: "${{ secrets.LAUNCHPAD_CREDENTIALS }}" + create_changelog: + name: Create changelog + permissions: + contents: read + uses: "./.github/workflows/create_changelog.yml" + ############################ + #### deploy jobs ### + ############################ + docker_deploy_jobs: + name: Deploy docker images + permissions: + contents: read + needs: + - standard_tests_jobs + - extended_tests_jobs + - docker_packaging_jobs + uses: "./.github/workflows/deploy_docker_images.yml" + secrets: + DOCKERHUB_TOKEN: "${{ secrets.DOCKERHUB_TOKEN }}" + with: + dockerTag: nightly + snap_deploy_jobs: + name: Deploy snaps + permissions: + contents: read + needs: + - standard_tests_jobs + - extended_tests_jobs + - snap_packaging_jobs + uses: "./.github/workflows/deploy_snaps.yml" + secrets: + SNAPCRAFTCFG: "${{ secrets.SNAPCRAFTCFG }}" + with: + snapReleaseChannel: edge + ############################ + #### rerun job ### + ############################ + re-run: + name: Re-run + needs: + - standard_tests_jobs + - extended_tests_jobs + - docker_packaging_jobs + - snap_packaging_jobs + - create_changelog + - docker_deploy_jobs + - snap_deploy_jobs + if: failure() && fromJSON(github.run_attempt) < 3 + permissions: + actions: write + checks: write + runs-on: ubuntu-latest + steps: + - env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ github.token }} + GH_DEBUG: api + run: gh workflow run rerun.yml -F run_id=${{ github.run_id }} + shell: bash + ########################### + #### notify ### + ########################### + notify: + name: Notify + needs: + - re-run + # Returns true when any previous step of a job fails. If you have a chain of dependent + # jobs, failure() returns true if any ancestor job fails. + if: failure() && (needs.re-run.result == 'skipped' || needs.re-run.result == 'failure') + uses: "./.github/workflows/notify_nightly.yml" + permissions: + actions: read + secrets: + MATTERMOST_PUBLIC_CERTBOT_CHANNEL_WEBHOOK: "${{ secrets.MATTERMOST_PUBLIC_CERTBOT_CHANNEL_WEBHOOK }}" diff --git a/.github/workflows/notify_nightly.yml b/.github/workflows/notify_nightly.yml new file mode 100644 index 00000000000..0d0d60590c1 --- /dev/null +++ b/.github/workflows/notify_nightly.yml @@ -0,0 +1,59 @@ +name: Notify nightly failure + +on: + workflow_call: + secrets: + MATTERMOST_PUBLIC_CERTBOT_CHANNEL_WEBHOOK: + required: true + +permissions: + actions: read + +jobs: + notify_mattermost: + name: Notify mattermost + runs-on: ubuntu-latest + steps: + - name: Calculate duration + shell: bash + id: duration + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + run: |- + START=$(gh run view "$GITHUB_RUN_ID" --json startedAt --jq ".startedAt") + START_SECONDS=$(date -d"$START" +%s) + NOW_SECONDS=$(date +%s) + DURATION=$(date -d@"$((NOW_SECONDS - START_SECONDS))" -u +%H:%M:%S) + echo "result = $DURATION" + echo "result=$DURATION" >> "$GITHUB_OUTPUT" + - name: Send to mattermost + uses: mattermost/action-mattermost-notify@ae31bb6f9e26a54336e79696f108a2c91cf55b4e + with: + MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_PUBLIC_CERTBOT_CHANNEL_WEBHOOK }} + PAYLOAD: |- + { + "attachments": [ + { + "color": "danger", + "fields": [ + null, + { + "title": "Duration", + "value": "${{ steps.duration.outputs.result }}", + "short": true + }, + { + "title": "Workflow", + "value": "nightly", + "short": true + } + ], + "pretext": "Run failed", + "mrkdwn_in": [ + "pretext" + ], + "fallback": "Run ${{ github.run_id }} failed https://github.com/certbot/actions/runs/${{ github.run_id }}" + } + ] + } diff --git a/.github/workflows/notify_release.yml b/.github/workflows/notify_release.yml new file mode 100644 index 00000000000..95b83d4803a --- /dev/null +++ b/.github/workflows/notify_release.yml @@ -0,0 +1,40 @@ +name: Notify release workflow finished + +on: + workflow_call: + inputs: + success: + type: string + secrets: + MATTERMOST_PUBLIC_CERTBOT_CHANNEL_WEBHOOK: + required: true + +permissions: + actions: read + contents: read + +jobs: + notify_mattermost: + name: Notify mattermost + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Generate message text + shell: bash + id: message + env: + SUCCESS: "${{ inputs.success }}" + run: |- + AUTHOR_NAME="$(git log -1 --pretty=format:'%an')" + MESSAGE=$(tools/release_message.py "${AUTHOR_NAME}" "${SUCCESS}") + echo "result<> $GITHUB_OUTPUT + echo "$MESSAGE" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + - name: Send to mattermost + uses: mattermost/action-mattermost-notify@ae31bb6f9e26a54336e79696f108a2c91cf55b4e + with: + MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_PUBLIC_CERTBOT_CHANNEL_WEBHOOK }} + TEXT: "${{ steps.message.outputs.result }}" diff --git a/.github/workflows/pr-test-suite.yml b/.github/workflows/pr-test-suite.yml new file mode 100644 index 00000000000..0003a2d198d --- /dev/null +++ b/.github/workflows/pr-test-suite.yml @@ -0,0 +1,27 @@ +# We run the test suite on commits to main to double check that everything is good after merging +name: PR test suite +on: + push: + branches: + - main + pull_request: + branches: + - main + - "*.x" + workflow_dispatch: +permissions: + contents: read +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +jobs: + standard_tests_jobs: + name: Standard tests + uses: "./.github/workflows/standard_tests_jobs.yml" + all_success: + name: PR test suite success + needs: standard_tests_jobs + runs-on: ubuntu-latest + steps: + - run: true diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000000..b8f1f8a4c7b --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,129 @@ +# Release pipeline to run our full test suite, build artifacts, and deploy them +# for GitHub release tags. +name: Release +run-name: Release Certbot ${{ github.ref_name }} +on: + push: + tags: + - v* + +permissions: + contents: read + +jobs: + # While many of these jobs could be grouped in a separate workflow, the github actions UI + # is much nicer if they are instead listed explicitly here. As a result, changes made here + # may need to be mirrored in .github/workflows/nightly.yml. + ########################### + #### testing jobs ### + ########################### + standard_tests_jobs: + name: Standard tests + uses: "./.github/workflows/standard_tests_jobs.yml" + extended_tests_jobs: + name: Extended tests + uses: "./.github/workflows/extended_tests_jobs.yml" + secrets: + AWS_TEST_FARM_PEM: "${{ secrets.AWS_TEST_FARM_PEM }}" + AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}" + AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}" + ########################### + #### packaging jobs ### + ########################### + docker_packaging_jobs: + name: Docker packaging + uses: "./.github/workflows/docker_packaging_jobs.yml" + with: + dockerTag: "${{ github.ref_name }}" + snap_packaging_jobs: + name: Snap packaging + uses: "./.github/workflows/snap_packaging_jobs.yml" + with: + snapBuildTimeout: 19800 + secrets: + LAUNCHPAD_CREDENTIALS: "${{ secrets.LAUNCHPAD_CREDENTIALS }}" + create_changelog: + name: Create changelog + uses: "./.github/workflows/create_changelog.yml" + ############################ + #### deploy jobs ### + ############################ + docker_deploy_jobs: + name: Deploy docker images + needs: + - standard_tests_jobs + - extended_tests_jobs + - docker_packaging_jobs + uses: "./.github/workflows/deploy_docker_images.yml" + secrets: + DOCKERHUB_TOKEN: "${{ secrets.DOCKERHUB_TOKEN }}" + with: + dockerTag: "${{ github.ref_name }}" + snap_deploy_jobs: + name: Deploy snaps + needs: + - standard_tests_jobs + - extended_tests_jobs + - snap_packaging_jobs + uses: "./.github/workflows/deploy_snaps.yml" + secrets: + SNAPCRAFTCFG: "${{ secrets.SNAPCRAFTCFG }}" + with: + snapReleaseChannel: beta + create_github_release: + name: Create GitHub release + needs: + - standard_tests_jobs + - extended_tests_jobs + - docker_packaging_jobs + - snap_packaging_jobs + - create_changelog + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Download changelog + uses: actions/download-artifact@v8.0.1 + with: + name: changelog + path: "${{ github.workspace }}" + - name: GitHub release + shell: bash + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + TAG: ${{ github.ref_name }} + run: |- + gh release create "$TAG" "${GITHUB_WORKSPACE}/packages/"{*.tar.gz,SHA256SUMS*} \ + --title "Certbot ${TAG#v}" \ + --notes-file "$GITHUB_WORKSPACE/release_notes.md" + + ########################### + #### notify ### + ########################### + notify_success: + name: Notify success + with: + success: 'True' + needs: ¬ify_needs + - docker_deploy_jobs + - snap_deploy_jobs + - create_github_release + uses: ¬ify_uses "./.github/workflows/notify_release.yml" + permissions: ¬ify_permissions + actions: read + contents: read + secrets: ¬ify_secrets + MATTERMOST_PUBLIC_CERTBOT_CHANNEL_WEBHOOK: "${{ secrets.MATTERMOST_PUBLIC_CERTBOT_CHANNEL_WEBHOOK }}" + notify_failure: + name: Notify failure + with: + success: 'False' + if: ${{ failure() }} + needs: *notify_needs + uses: *notify_uses + permissions: *notify_permissions + secrets: *notify_secrets diff --git a/.github/workflows/rerun.yml b/.github/workflows/rerun.yml new file mode 100644 index 00000000000..36bfdf7fcad --- /dev/null +++ b/.github/workflows/rerun.yml @@ -0,0 +1,27 @@ +# based on https://github.com/orgs/community/discussions/67654#discussioncomment-8038649 +name: Rerun +on: + workflow_dispatch: + inputs: + run_id: + required: true + description: github.run_id of workflow to rerun +permissions: + actions: write + checks: write + +env: + RUN_ID: "${{ inputs.run_id }}" + +jobs: + rerun: + runs-on: ubuntu-latest + steps: + - name: rerun "$RUN_ID" + env: + GH_REPO: ${{ github.repository }} + GH_TOKEN: ${{ github.token }} + GH_DEBUG: api + run: | + gh run watch "$RUN_ID" > /dev/null 2>&1 + gh run rerun "$RUN_ID" --failed diff --git a/.github/workflows/review_requested.yaml b/.github/workflows/review_requested.yaml new file mode 100644 index 00000000000..48a3e8a173a --- /dev/null +++ b/.github/workflows/review_requested.yaml @@ -0,0 +1,27 @@ +name: Review Requested + +on: + pull_request_target: + types: [review_requested] +permissions: {} +jobs: + send-mattermost-message: + # Don't notify for the interim step of certbot/eff-devs being assigned + if: ${{ github.event.requested_reviewer.login != ''}} + runs-on: ubuntu-latest + steps: + # github actions workflows triggered by pull_request_target can be + # dangerous because they run with additional privileges in an environment + # containing values that can be controlled by an attacker. because of + # this, please take extra caution when modifying the steps taken by this + # workflow. for additional information, see + # https://github.com/certbot/certbot/pull/10490 + # + # we pin this action to a version tested and audited by certbot's + # maintainers for extra security. the full hash is used as doing so is + # recommended by zizmor + - uses: mattermost/action-mattermost-notify@ae31bb6f9e26a54336e79696f108a2c91cf55b4e + with: + MATTERMOST_WEBHOOK_URL: ${{ secrets.MATTERMOST_ASSIGN_WEBHOOK }} + TEXT: > + Review requested from ${{ github.event.requested_reviewer.login }} for "${{ github.event.pull_request.title }}": ${{ github.event.pull_request.html_url }} diff --git a/.github/workflows/snap_packaging_jobs.yml b/.github/workflows/snap_packaging_jobs.yml new file mode 100644 index 00000000000..7bc4e331232 --- /dev/null +++ b/.github/workflows/snap_packaging_jobs.yml @@ -0,0 +1,248 @@ +name: Snap packaging +on: + workflow_call: + inputs: + snapBuildTimeout: + description: 'timeout for snap builds in seconds' + type: number + secrets: + LAUNCHPAD_CREDENTIALS: + required: true + +permissions: + contents: read + +env: + SNAP_BUILD_TIMEOUT: ${{ inputs.snapBuildTimeout }} + +jobs: + gha_build_snap: + name: Build certbot ${{ matrix.build-for }} + runs-on: ${{ matrix.build-on }} + strategy: + fail-fast: false + matrix: + include: + - build-for: arm64 + build-on: ubuntu-24.04-arm + - build-for: amd64 + build-on: ubuntu-24.04 + steps: + - name: checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - uses: snapcore/action-build@v1.3.0 + id: build + with: + snapcraft-args: "--build-for=${{ matrix.build-for }}" + - name: Store snap artifact + uses: actions/upload-artifact@v7.0.0 + with: + name: snap-certbot-${{ matrix.build-for }} + path: ${{ steps.build.outputs.snap }} + generate_dns_list_matrix: + name: List DNS package names + runs-on: ubuntu-latest + outputs: + dns-dirs: ${{ steps.set-dns-dirs.outputs.dns-dirs }} + steps: + - name: checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - id: set-dns-dirs + run: | + DNS_NAMES="$(echo certbot-dns-* | jq -R -s -c 'gsub("\n"; "") | split(" ")')" + echo "dns-dirs=$DNS_NAMES" >> "$GITHUB_OUTPUT" + gha_build_dns_snaps: + name: Build ${{ matrix.dns-dir }} ${{ matrix.build-for }} + needs: generate_dns_list_matrix + runs-on: ${{ matrix.build-on }} + strategy: + fail-fast: false + matrix: + dns-dir: ${{ fromJSON(needs.generate_dns_list_matrix.outputs.dns-dirs) }} + build-for: [arm64, amd64] + include: + - build-for: arm64 + build-on: ubuntu-24.04-arm + - build-for: amd64 + build-on: ubuntu-24.04 + steps: + - name: checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: generate dns snapcraft.yamls + run: | + tools/snap/generate_dnsplugins_all.sh # unnecessarily doing every file + - uses: snapcore/action-build@v1.3.0 + id: build + with: + snapcraft-args: "--build-for=${{ matrix.build-for }}" + path: ${{ matrix.dns-dir }} + - name: Store snap artifact + uses: actions/upload-artifact@v7.0.0 + with: + name: snap-${{ matrix.dns-dir }}-${{ matrix.build-for }} + path: ${{ steps.build.outputs.snap }} + launchpad_build_all: + name: Build armhf snaps + runs-on: + - ubuntu-24.04 + env: + SNAP_ARCH: "armhf" + steps: + - name: checkout + uses: actions/checkout@v6.0.2 + with: + fetch-depth: 0 # need a non-shallow clone for launchpad + persist-credentials: false + - name: Install dependencies + run: |- + sudo apt-get update + sudo apt-get install -y --no-install-recommends snapd + sudo snap install --classic snapcraft + - uses: actions/setup-python@v6.2.0 + with: + python-version: '3.12' + - name: Build snaps + env: + LAUNCHPAD_CREDS: "${{ secrets.LAUNCHPAD_CREDENTIALS }}" + run: |- + git config --global user.email "github-actions[bot]" + git config --global user.name "41898282+github-actions[bot]@users.noreply.github.com" + mkdir -p ~/.local/share/snapcraft/ + echo "$LAUNCHPAD_CREDS" > ~/.local/share/snapcraft/launchpad-credentials + python3 tools/snap/build_remote.py ALL --archs "$SNAP_ARCH" --timeout "$SNAP_BUILD_TIMEOUT" + - name: Flatten snaps layout + run: |- + mv *.snap ${{ runner.temp }} + mv certbot-dns-*/*.snap ${{ runner.temp }} + - name: Store snaps artifacts + uses: actions/upload-artifact@v7.0.0 + with: + name: snaps_${{ env.SNAP_ARCH }} + path: "${{ runner.temp }}/*.snap" + snap_run: + name: Test certbot ${{ matrix.arch-name }} + needs: + - gha_build_snap + - launchpad_build_all + runs-on: + - ${{ matrix.run-on }} + strategy: + fail-fast: false + matrix: + include: + - arch-name: arm64 + run-on: ubuntu-24.04-arm + - arch-name: amd64 + run-on: ubuntu-24.04 + - arch-name: armhf + run-on: ubuntu-24.04-arm + steps: + - name: checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - uses: actions/setup-python@v6.2.0 + with: + python-version: '3.12' + - name: Install armhf dependencies + if: ${{ matrix.arch-name == 'armhf' }} + run: |- + sudo dpkg --add-architecture armhf + sudo apt-get update + # apparmor will conflict with snapd:armhf dependency if not removed first + sudo apt-get remove -y apparmor + sudo apt-get install -y --no-install-recommends fuse3:armhf snapd:armhf nginx-light + - name: Install non-armhf depdencies + if: ${{ matrix.arch-name != 'armhf' }} + run: |- + sudo apt-get update + sudo apt-get install -y --no-install-recommends nginx-light snapd + - name: Setup venv + run: |- + python3 -m venv venv + venv/bin/python tools/pip_install.py -U tox + - name: Retrieve Certbot snaps armhf + if: ${{ matrix.arch-name == 'armhf' }} + uses: actions/download-artifact@v8.0.1 + with: + name: snaps_${{ matrix.arch-name }} + path: "${{ github.workspace }}/snap" + - name: Retrieve Certbot snaps non-armhf + if: ${{ matrix.arch-name != 'armhf' }} + uses: actions/download-artifact@v8.0.1 + with: + pattern: snap-certbot-${{ matrix.arch-name }} + path: "${{ github.workspace }}/snap" + - name: Install Certbot snap + run: |- + sudo snap install --dangerous --classic snap/certbot_*.snap + - name: Run tox + run: |- + venv/bin/python -m tox run -e integration-external,apacheconftest-external-with-pebble + snap_dns_run: + name: Test DNS ${{ matrix.arch-name }} + needs: + - gha_build_dns_snaps + - gha_build_snap + - launchpad_build_all + runs-on: + - ${{ matrix.run-on }} + strategy: + fail-fast: false + matrix: + include: + - arch-name: arm64 + run-on: ubuntu-24.04-arm + - arch-name: amd64 + run-on: ubuntu-24.04 + - arch-name: armhf + run-on: ubuntu-24.04-arm + steps: + - name: checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Install armhf dependencies + if: ${{ matrix.arch-name == 'armhf' }} + run: |- + sudo dpkg --add-architecture armhf + sudo apt-get update + # apparmor will conflict with snapd:armhf dependency if not removed first + sudo apt-get remove -y apparmor + sudo apt-get install -y --no-install-recommends fuse3:armhf snapd:armhf + - name: Install non-armhf depdencies + if: ${{ matrix.arch-name != 'armhf' }} + run: |- + sudo apt-get update + sudo apt-get install -y --no-install-recommends snapd + - uses: actions/setup-python@v6.2.0 + with: + python-version: '3.12' + - name: Retrieve Certbot snaps armhf + if: ${{ matrix.arch-name == 'armhf' }} + uses: actions/download-artifact@v8.0.1 + with: + name: snaps_${{ matrix.arch-name }} + path: "${{ github.workspace }}/snap" + - name: Retrieve Certbot snaps non-armhf + if: ${{ matrix.arch-name != 'armhf' }} + uses: actions/download-artifact@v8.0.1 + with: + pattern: snap-*-${{ matrix.arch-name }} + merge-multiple: true + path: "${{ github.workspace }}/snap" + - name: Display structure of downloaded files + run: ls -R "${{ github.workspace }}/snap" + - name: Prepare Certbot-CI + run: |- + python3 -m venv venv + venv/bin/python tools/pip_install.py -e certbot-ci + - name: Test DNS plugins snaps + run: |- + sudo -E venv/bin/pytest certbot-ci/src/snap_integration_tests/dns_tests --allow-persistent-changes --snap-folder ${{ github.workspace }}/snap --snap-arch ${{ matrix.arch-name }} diff --git a/.github/workflows/sphinx_steps.yml b/.github/workflows/sphinx_steps.yml new file mode 100644 index 00000000000..73b2b2166cb --- /dev/null +++ b/.github/workflows/sphinx_steps.yml @@ -0,0 +1,42 @@ +name: Sphinx steps +on: + workflow_call: +permissions: + contents: read + +jobs: + test_sphinx_builds: + name: build sphinx documentation + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + - name: Build Sphinx Documentation + run: |- + echo "::group::Install dependencies" + sudo apt-get update + sudo apt-get install -y --no-install-recommends libaugeas-dev + echo "::endgroup::" + FINAL_STATUS=0 + declare -a FAILED_BUILDS + echo "::group::Setup venv" + tools/venv.py + source venv/bin/activate + echo "::endgroup::" + for doc_path in */docs + do + echo "" + echo "::group::Building $doc_path" + if ! sphinx-build -W --keep-going -b html "$doc_path" "$doc_path/_build/html"; then + FINAL_STATUS=1 + FAILED_BUILDS[${#FAILED_BUILDS[@]}]="${doc_path%/docs}" + fi + echo "::endgroup::" + done + if [[ $FINAL_STATUS -ne 0 ]]; then + echo "::error::The following builds failed: ${FAILED_BUILDS[*]}" + exit 1 + fi + shell: bash diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 00000000000..76de140cc5f --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,51 @@ +name: Update Stale Issues +on: + schedule: + # Run 1:24AM every night + - cron: '24 1 * * *' + workflow_dispatch: +permissions: + issues: write +jobs: + stale: + runs-on: ubuntu-latest + steps: + - uses: actions/stale@v6 + with: + # Idle number of days before marking issues stale + days-before-issue-stale: 365 + + # Never mark PRs as stale + days-before-pr-stale: -1 + + # Idle number of days before closing stale issues + days-before-issue-close: 30 + + # Never close PRs + days-before-pr-close: -1 + + # Ignore issues with an assignee + exempt-all-issue-assignees: true + + # Label to use when marking as stale + stale-issue-label: stale-needs-update + + # Label to use when issue is automatically closed + close-issue-label: auto-closed + + stale-issue-message: > + We've made a lot of changes to Certbot since this issue was opened. If you + still have this issue with an up-to-date version of Certbot, can you please + add a comment letting us know? This helps us to better see what issues are + still affecting our users. If there is no activity in the next 30 days, this + issue will be automatically closed. + + close-issue-message: > + This issue has been closed due to lack of activity, but if you think it + should be reopened, please open a new issue with a link to this one and we'll + take a look. + + # Limit the number of actions per run. As of writing this, GitHub's + # rate limit is 1000 requests per hour so we're still a ways off. See + # https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#rate-limits-for-requests-from-github-actions. + operations-per-run: 180 diff --git a/.github/workflows/standard_tests_jobs.yml b/.github/workflows/standard_tests_jobs.yml new file mode 100644 index 00000000000..2e29a1d10e2 --- /dev/null +++ b/.github/workflows/standard_tests_jobs.yml @@ -0,0 +1,43 @@ +name: Standard tests jobs +on: + workflow_call: +permissions: + contents: read + +jobs: + test: + name: '-' # Makes results viewing cleaner + strategy: + fail-fast: false + matrix: + PYTHON_VERSION: ['3.14'] + IMAGE_NAME: ['ubuntu-22.04'] + TOXENV: + - 'cover' + - 'lint-posix' + - 'mypy' + - 'integration' + - 'apache_compat' + - 'apacheconftest-with-pebble' + - 'nginxroundtrip' + - 'validate-changelog' + include: + - IMAGE_NAME: macOS-15 + PYTHON_VERSION: '3.14' + TOXENV: cover + PIP_USE_PEP517: 'true' + - IMAGE_NAME: ubuntu-22.04 + PYTHON_VERSION: '3.10' + TOXENV: oldest + - IMAGE_NAME: ubuntu-22.04 + PYTHON_VERSION: '3.10' + TOXENV: py310 + uses: "./.github/workflows/tox_steps.yml" + with: + PYTHON_VERSION: "${{ matrix.PYTHON_VERSION }}" + PIP_USE_PEP517: "${{ matrix.PIP_USE_PEP517 }}" + TOXENV: "${{ matrix.TOXENV }}" + IMAGE_NAME: "${{ matrix.IMAGE_NAME }}" + test_sphinx_builds: + name: '-' # Makes results viewing cleaner + uses: "./.github/workflows/sphinx_steps.yml" diff --git a/.github/workflows/tox_steps.yml b/.github/workflows/tox_steps.yml new file mode 100644 index 00000000000..529f00f01dd --- /dev/null +++ b/.github/workflows/tox_steps.yml @@ -0,0 +1,87 @@ +name: Tox steps +on: + workflow_call: + inputs: + PYTHON_VERSION: + type: string + IMAGE_NAME: + type: string + TOXENV: + type: string + PIP_USE_PEP517: + type: string + secrets: + AWS_TEST_FARM_PEM: + required: false + AWS_ACCESS_KEY_ID: + required: false + AWS_SECRET_ACCESS_KEY: + required: false + +permissions: + contents: read + +jobs: + tox_all: + name: ${{ inputs.TOXENV }} ${{ inputs.IMAGE_NAME }} + runs-on: + - "${{ inputs.IMAGE_NAME }}" + env: + AWS_EC2_PEM_FILE: ${{ github.workspace }}/GHAKeyPair.pem + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + with: + persist-credentials: false + # We run brew update because we've seen attempts to install an older version + # of a package fail. See + # https://github.com/actions/virtual-environments/issues/3165. + # + # We untap homebrew/core and homebrew/cask and unset HOMEBREW_NO_INSTALL_FROM_API (which + # is maybe set by the CI macOS env) because GitHub has been having issues, making these jobs + # fail on git clones: https://github.com/orgs/Homebrew/discussions/4612. + - name: Install MacOS dependencies + if: runner.os == 'macOS' + run: |- + unset HOMEBREW_NO_INSTALL_FROM_API + brew untap homebrew/core homebrew/cask + brew update + brew install augeas + BREW_PREFIX="$(brew --prefix)" + CFLAGS="$CFLAGS -I$BREW_PREFIX/include -L$BREW_PREFIX/lib" + echo "CFLAGS=$CFLAGS" >> "$GITHUB_ENV" + shell: bash + - name: Install Linux dependencies + if: runner.os == 'Linux' + run: |- + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + libaugeas-dev \ + nginx-light + sudo systemctl stop nginx + sudo sysctl net.ipv4.ip_unprivileged_port_start=0 + shell: bash + - uses: actions/setup-python@v6.2.0 + with: + python-version: "${{ inputs.PYTHON_VERSION }}" + - name: Install runtime dependencies + run: |- + python3 tools/pip_install.py tox + shell: bash + - name: Create test farm pem file + if: contains(inputs.TOXENV, 'test-farm') + env: + PEM_CONTENTS: "${{ secrets.AWS_TEST_FARM_PEM }}" + run: |- + echo "${PEM_CONTENTS}" >> $AWS_EC2_PEM_FILE + shell: bash + - name: Run tox + env: + AWS_ACCESS_KEY_ID: "${{ secrets.AWS_ACCESS_KEY_ID }}" + AWS_SECRET_ACCESS_KEY: "${{ secrets.AWS_SECRET_ACCESS_KEY }}" + PIP_USE_PEP517: "${{ inputs.PIP_USE_PEP517 }}" + TOXENV: "${{ inputs.TOXENV }}" + run: |- + env + python3 -m tox run + shell: bash diff --git a/.gitignore b/.gitignore index 54545e88378..49fcff36936 100644 --- a/.gitignore +++ b/.gitignore @@ -4,16 +4,16 @@ build/ dist*/ /venv*/ -/kgs/ /.tox/ /releases*/ /log* letsencrypt.log certbot.log -letsencrypt-auto-source/letsencrypt-auto.sig.lzma.base64 +poetry.lock # coverage .coverage +.coverage.* /htmlcov/ /.vagrant @@ -26,15 +26,13 @@ tags \#*# .idea .ropeproject +.vscode +*.sublime-project +*.sublime-workspace # auth --cert-path --chain-path /*.pem -# letstest -tests/letstest/letest-*/ -tests/letstest/*.pem -tests/letstest/venv/ - .venv # pytest cache @@ -44,3 +42,33 @@ tests/letstest/venv/ # docker files .docker + +# certbot tests +.certbot_test_workspace +**/assets/pebble* +**/assets/challtestsrv* + +# snap files +.snapcraft +parts +prime +stage +*.snap +snap-constraints.txt +qemu-* +certbot-dns*/certbot-dns*_amd64*.txt +certbot-dns*/certbot-dns*_arm*.txt +/certbot_amd64*.txt +/certbot_arm*.txt +certbot-dns*/snap +snapcraft.cfg + +# pyenv files +.python-version + +# macOS files +.DS_Store + +# azure workflow migration +.env.local +tmp diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 00000000000..821e38573ef --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,7 @@ +[settings] +skip_glob=venv* +force_sort_within_sections=True +force_single_line=True +order_by_type=False +line_length=400 +src_paths=acme/src,acme/tests,certbot*/tests,certbot/src,certbot*/src/certbot* diff --git a/.pylintrc b/.pylintrc index 1d3f0ac4f0c..6689b9a4378 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,338 +1,577 @@ -[MASTER] +[MAIN] + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Load and enable all available extensions. Use --list-extensions to see a list +# all available extensions. +#enable-all-extensions= + +# In error mode, messages with a category besides ERROR or FATAL are +# suppressed, and no reports are done by default. Error mode is compatible with +# disabling specific errors. +#errors-only= + +# Always return a 0 (non-error) status code, even if lint errors are found. +# This is primarily useful in continuous integration scripts. +#exit-zero= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-allow-list= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. (This is an alternative name to extension-pkg-allow-list +# for backward compatibility.) +extension-pkg-whitelist=pywintypes,win32api,win32file,win32security + +# Return non-zero exit code if any of these messages/categories are detected, +# even if score is above --fail-under value. Syntax same as enable. Messages +# specified are enabled, while categories only check already-enabled messages. +fail-on= + +# Specify a score threshold under which the program will exit with error. +fail-under=10 + +# Interpret the stdin as a python script, whose filename needs to be passed as +# the module_or_package argument. +#from-stdin= + +# Files or directories to be skipped. They should be base names, not paths. +ignore=CVS -# use as many jobs as there are cores -jobs=0 +# Add files or directories matching the regular expressions patterns to the +# ignore-list. The regex matches against paths and can be in Posix or Windows +# format. Because '\' represents the directory delimiter on Windows systems, it +# can't be used as an escape character. +# CERTBOT COMMENT +# Changing this line back to the default of `ignore-paths=` is being tracked by +# https://github.com/certbot/certbot/issues/7908. +ignore-paths=.*/_internal/tests/ + +# Files or directories matching the regular expression patterns are skipped. +# The regex matches against base names, not paths. The default value ignores +# Emacs file locks +ignore-patterns=^\.# -# Specify a configuration file. -#rcfile= +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis). It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= # Python code to execute, usually for sys.path manipulation such as # pygtk.require(). -#init-hook= +# CERTBOT COMMENT +# This is needed for pylint to import linter_plugin.py since +# https://github.com/PyCQA/pylint/pull/3396. +init-hook="import pylint.config, os, sys; sys.path.append(os.path.dirname(next(pylint.config.find_default_config_files())))" + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use, and will cap the count on Windows to +# avoid hangs. +jobs=0 -# Profiled execution. -profile=no +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=100 -# Add files or directories to the blacklist. They should be base names, not -# paths. -ignore=CVS +# List of plugins (as comma separated values of python module names) to load, +# usually to register additional checkers. +load-plugins=linter_plugin # Pickle collected data for later comparisons. persistent=yes -# List of plugins (as comma separated values of python modules names) to load, -# usually to register additional checkers. -load-plugins=linter_plugin +# Minimum Python version to use for version dependent checks. Will default to +# the version used to run pylint. +py-version=3.10 +# Discover python modules and packages in the file system subtree. +recursive=no -[MESSAGES CONTROL] +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes -# Enable the message, report, category or checker with the given id(s). You can -# either give multiple identifier separated by comma (,) or put this option -# multiple time. See also the "--disable" option for examples. -#enable= +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no -# Disable the message, report, category or checker with the given id(s). You -# can either give multiple identifiers separated by comma (,) or put this -# option multiple times (only on the command line, not in the configuration -# file where it should appear only once).You can also use "--disable=all" to -# disable everything first and then reenable specific checks. For example, if -# you want to run only the similarities checker, you can use "--disable=all -# --enable=similarities". If you want to run only the classes checker, but have -# no Warning level messages displayed, use"--disable=all --enable=classes -# --disable=W" -disable=fixme,locally-disabled,locally-enabled,abstract-class-not-used,abstract-class-little-used,bad-continuation,too-few-public-methods,no-self-use,invalid-name,too-many-instance-attributes,cyclic-import,duplicate-code -# abstract-class-not-used cannot be disabled locally (at least in -# pylint 1.4.1), same for abstract-class-little-used +# In verbose mode, extra non-checker-related info will be displayed. +#verbose= -[REPORTS] +[BASIC] -# Set the output format. Available formats are text, parseable, colorized, msvs -# (visual studio) and html. You can also give a reporter class, eg -# mypackage.mymodule.MyReporterClass. -output-format=text +# Naming style matching correct argument names. +argument-naming-style=snake_case -# Put messages in a separate file for each module / package specified on the -# command line instead of printing them on stdout. Reports (if any) will be -# written in a file name "pylint_global.[txt|html]". -files-output=no +# Regular expression matching correct argument names. Overrides argument- +# naming-style. If left empty, argument names will be checked with the set +# naming style. +#argument-rgx= -# Tells whether to display a full report or only the messages -reports=yes +# Naming style matching correct attribute names. +attr-naming-style=snake_case -# Python expression which should return a note less than 10 (10 is the highest -# note). You have access to the variables errors warning, statement which -# respectively contain the number of errors / warnings messages and the total -# number of statements analyzed. This is used by the global evaluation report -# (RP0004). -evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. If left empty, attribute names will be checked with the set naming +# style. +#attr-rgx= -# Add a comment according to your evaluation note. This is used by the global -# evaluation report (RP0004). -comment=no +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata -# Template used to display messages. This is a python new-style format string -# used to format the message information. See doc for all details -#msg-template= +# Bad variable names regexes, separated by a comma. If names match any regex, +# they will always be refused +bad-names-rgxs= +# Naming style matching correct class attribute names. +class-attribute-naming-style=any -[BASIC] +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. If left empty, class attribute names will be checked +# with the set naming style. +#class-attribute-rgx= + +# Naming style matching correct class constant names. +class-const-naming-style=UPPER_CASE + +# Regular expression matching correct class constant names. Overrides class- +# const-naming-style. If left empty, class constant names will be checked with +# the set naming style. +#class-const-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. If left empty, class names will be checked with the set naming style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. If left empty, constant names will be checked with the set naming +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. If left empty, function names will be checked with the set +# naming style. +function-rgx=[a-z_][a-z0-9_]{2,40}$ + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _, + fd, + logger + +# Good variable names regexes, separated by a comma. If names match any regex, +# they will always be accepted +good-names-rgxs= + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any -# Required attributes for module, separated by a comma -required-attributes= +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. If left empty, inline iteration names will be checked +# with the set naming style. +#inlinevar-rgx= -# List of builtins function names that should not be used, separated by a comma -bad-functions=map,filter,apply,input,file +# Naming style matching correct method names. +method-naming-style=snake_case -# Good variable names which should always be accepted, separated by a comma -good-names=f,i,j,k,ex,Run,_,fd,logger +# Regular expression matching correct method names. Overrides method-naming- +# style. If left empty, method names will be checked with the set naming style. +method-rgx=[a-z_][a-z0-9_]{2,50}$ + +# Naming style matching correct module names. +module-naming-style=snake_case -# Bad variable names which should always be refused, separated by a comma -bad-names=foo,bar,baz,toto,tutu,tata +# Regular expression matching correct module names. Overrides module-naming- +# style. If left empty, module names will be checked with the set naming style. +#module-rgx= # Colon-delimited sets of names that determine each other's naming style when # the name regexes allow several styles. name-group= -# Include a hint for the correct naming format with invalid-name -include-naming-hint=no +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=(__.*__)|(test_[A-Za-z0-9_]*)|(_.*)|(.*Test$) -# Regular expression matching correct function names -function-rgx=[a-z_][a-z0-9_]{2,40}$ +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Regular expression matching correct type variable names. If left empty, type +# variable names will be checked with the set naming style. +#typevar-rgx= -# Naming hint for function names -function-name-hint=[a-z_][a-z0-9_]{2,40}$ +# Naming style matching correct variable names. +variable-naming-style=snake_case -# Regular expression matching correct variable names +# Regular expression matching correct variable names. Overrides variable- +# naming-style. If left empty, variable names will be checked with the set +# naming style. variable-rgx=[a-z_][a-z0-9_]{1,30}$ -# Naming hint for variable names -variable-name-hint=[a-z_][a-z0-9_]{2,30}$ -# Regular expression matching correct constant names -const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$ +[CLASSES] + +# Warn about protected attribute access inside special methods +check-protected-access-in-special-methods=no + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp, + __post_init__ -# Naming hint for constant names -const-name-hint=(([A-Z_][A-Z0-9_]*)|(__.*__))$ +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls -# Regular expression matching correct attribute names -attr-rgx=[a-z_][a-z0-9_]{2,30}$ +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=cls -# Naming hint for attribute names -attr-name-hint=[a-z_][a-z0-9_]{2,30}$ -# Regular expression matching correct argument names -argument-rgx=[a-z_][a-z0-9_]{2,30}$ +[EXCEPTIONS] -# Naming hint for argument names -argument-name-hint=[a-z_][a-z0-9_]{2,30}$ +# Exceptions that will emit a warning when caught. +overgeneral-exceptions=builtins.BaseException, + builtins.Exception -# Regular expression matching correct class attribute names -class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$ -# Naming hint for class attribute names -class-attribute-name-hint=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$ +[FORMAT] -# Regular expression matching correct inline iteration names -inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$ +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= -# Naming hint for inline iteration names -inlinevar-name-hint=[A-Za-z_][A-Za-z0-9_]*$ +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ -# Regular expression matching correct class names -class-rgx=[A-Z_][a-zA-Z0-9]+$ +# Number of spaces of indent required inside a hanging or continued line. +# git history told me that "This does something silly/broken..." +#indent-after-paren=4 -# Naming hint for class names -class-name-hint=[A-Z_][a-zA-Z0-9]+$ +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' -# Regular expression matching correct module names -module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ +# Maximum number of characters on a single line. +max-line-length=100 -# Naming hint for module names -module-name-hint=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ +# Maximum number of lines in a module. +max-module-lines=1250 -# Regular expression matching correct method names -method-rgx=[a-z_][a-z0-9_]{2,50}$ +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no -# Naming hint for method names -method-name-hint=[a-z_][a-z0-9_]{2,50}$ +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no -# Regular expression which should only match function or class names that do -# not require a docstring. -no-docstring-rgx=(__.*__)|(test_[A-Za-z0-9_]*)|(_.*)|(.*Test$) -# Minimum line length for functions/classes that require docstrings, shorter -# ones are exempt. -docstring-min-length=-1 +[IMPORTS] +# List of modules that can be imported at any level, not just the top level +# one. +allow-any-import-level= -[MISCELLANEOUS] +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no -# List of note tags to take in consideration, separated by a comma. -notes=FIXME,XXX,TODO +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules= + +# Output a graph (.gv or any supported image format) of external dependencies +# to the given file (report RP0402 must not be disabled). +ext-import-graph= + +# Output a graph (.gv or any supported image format) of all (i.e. internal and +# external) dependencies to the given file (report RP0402 must not be +# disabled). +import-graph= + +# Output a graph (.gv or any supported image format) of internal dependencies +# to the given file (report RP0402 must not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + +# Couples of modules and preferred modules, separated by a comma. +preferred-modules= [LOGGING] +# The type of string formatting that logging methods do. `old` means using % +# formatting, `new` is for `{}` formatting. +logging-format-style=old + # Logging modules to check that the string format arguments are in logging -# function parameter format +# function parameter format. logging-modules=logging,logger -[VARIABLES] +[MESSAGES CONTROL] -# Tells whether we should check for unused import in __init__ files. -init-import=no +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, CONTROL_FLOW, INFERENCE, INFERENCE_FAILURE, +# UNDEFINED. +confidence=HIGH, + CONTROL_FLOW, + INFERENCE, + INFERENCE_FAILURE, + UNDEFINED -# A regular expression matching the name of dummy variables (i.e. expectedly -# not used). -dummy-variables-rgx=(unused)?_.*|dummy +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then re-enable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +# CERTBOT COMMENT +# 1) Once certbot codebase is claimed to be compatible exclusively with Python 3, +# the useless-object-inheritance check can be enabled again, and code fixed accordingly. +# 2) Check unsubscriptable-object tends to create a lot of false positives. Let's disable it. +# See https://github.com/PyCQA/pylint/issues/1498. +# 3) Same as point 2 for no-value-for-parameter. +# See https://github.com/PyCQA/pylint/issues/2820. +# 4) raise-missing-from makes it an error to raise an exception from except +# block without using explicit exception chaining. While explicit exception +# chaining results in a slightly more informative traceback, I don't think +# it's beneficial enough for us to change all of our current instances and +# give Certbot developers errors about this when they're working on new code +# in the future. You can read more about exception chaining and this pylint +# check at +# https://blog.ram.rachum.com/post/621791438475296768/improving-python-exception-chaining-with. +# 5) wrong-import-order generates false positives and a pylint developer +# suggests that people using isort should disable this check at +# https://github.com/PyCQA/pylint/issues/3817#issuecomment-687892090. +# 6) unspecified-encoding generates errors when encoding is not specified in +# in a call to the built-in open function. This relates more to a design decision +# (unspecified encoding makes the open function use the default encoding of the system) +# than a clear flaw on which a check should be enforced. Anyway the project does +# not need to enforce encoding on files so we disable this check. +# 7) consider-using-f-string is "suggesting" to move to f-string when possible with an error. This +# clearly relates to code design and not to potential defects in the code, let's just ignore that. +disable=fixme,locally-disabled,invalid-name,cyclic-import,duplicate-code,design,import-outside-toplevel,useless-object-inheritance,unsubscriptable-object,no-value-for-parameter,no-else-return,no-else-raise,no-else-break,no-else-continue,raise-missing-from,wrong-import-order,unspecified-encoding,consider-using-f-string,raw-checker-failed,bad-inline-option,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,use-symbolic-message-instead -# List of additional names supposed to be defined in builtins. Remember that -# you should avoid to define new builtins when possible. -additional-builtins= +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member -[SIMILARITIES] +[METHOD_ARGS] -# Minimum lines number of a similarity. -min-similarity-lines=6 +# List of qualified names (i.e., library.method) which require a timeout +# parameter e.g. 'requests.api.get,requests.api.post' +timeout-methods=requests.api.delete,requests.api.get,requests.api.head,requests.api.options,requests.api.patch,requests.api.post,requests.api.put,requests.api.request -# Ignore comments when computing similarities. -ignore-comments=yes -# Ignore docstrings when computing similarities. -ignore-docstrings=yes +[MISCELLANEOUS] -# Ignore imports when computing similarities. -ignore-imports=yes +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO +# Regular expression of note tags to take in consideration. +notes-rgx= -[FORMAT] -# Maximum number of characters on a single line. -max-line-length=100 +[REFACTORING] -# Regexp for a line that is allowed to be longer than the limit. -ignore-long-lines=^\s*(# )??$ +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 -# Allow the body of an if to be on the same line as the test if there is no -# else. -single-line-if-stmt=no +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit,argparse.parse_error -# List of optional constructs for which whitespace checking is disabled -no-space-check=trailing-comma -# Maximum number of lines in a module -max-module-lines=1250 +[REPORTS] -# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 -# tab). -indent-string=' ' +# Python expression which should return a score less than or equal to 10. You +# have access to the variables 'fatal', 'error', 'warning', 'refactor', +# 'convention', and 'info' which contain the number of messages in each +# category, as well as 'statement' which is the total number of statements +# analyzed. This score is used by the global evaluation report (RP0004). +evaluation=max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)) -# Number of spaces of indent required inside a hanging or continued line. -# This does something silly/broken... -#indent-after-paren=4 +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +msg-template= +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +#output-format= -[TYPECHECK] +# Tells whether to display a full report or only the messages. +reports=no -# Tells whether missing members accessed in mixin class should be ignored. A -# mixin class is detected if its name ends with "mixin" (case insensitive). -ignore-mixin-members=yes +# Activate the evaluation score. +score=yes -# List of module names for which member attributes should not be checked -# (useful for modules/projects where namespaces are manipulated during runtime -# and thus existing member attributes cannot be deduced by static analysis -ignored-modules=pkg_resources,confargparse,argparse,six.moves,six.moves.urllib -# import errors ignored only in 1.4.4 -# https://bitbucket.org/logilab/pylint/commits/cd000904c9e2 -# List of classes names for which member attributes should not be checked -# (useful for classes with attributes dynamically set). -ignored-classes=SQLObject +[SIMILARITIES] -# When zope mode is activated, add a predefined set of Zope acquired attributes -# to generated-members. -zope=yes +# Comments are removed from the similarity computation +ignore-comments=yes -# List of members which are set dynamically and missed by pylint inference -# system, and so shouldn't trigger E0201 when accessed. Python regular -# expressions are accepted. -generated-members=REQUEST,acl_users,aq_parent +# Docstrings are removed from the similarity computation +ignore-docstrings=yes +# Imports are removed from the similarity computation +ignore-imports=yes -[IMPORTS] +# Signatures are removed from the similarity computation +ignore-signatures=yes -# Deprecated modules which should not be used, separated by a comma -deprecated-modules=regsub,TERMIOS,Bastion,rexec +# Minimum lines number of a similarity. +min-similarity-lines=6 -# Create a graph of every (i.e. internal and external) dependencies in the -# given file (report RP0402 must not be disabled) -import-graph= -# Create a graph of external dependencies in the given file (report RP0402 must -# not be disabled) -ext-import-graph= +[STRING] -# Create a graph of internal dependencies in the given file (report RP0402 must -# not be disabled) -int-import-graph= +# This flag controls whether inconsistent-quotes generates a warning when the +# character used as a quote delimiter is used inconsistently within a module. +check-quote-consistency=no +# This flag controls whether the implicit-str-concat should generate a warning +# on implicit string concatenation in sequences defined over several lines. +check-str-concat-over-line-jumps=no -[CLASSES] -# List of interface methods to ignore, separated by a comma. This is used for -# instance to not check methods defined in Zope's Interface base class. -ignore-iface-methods=isImplementedBy,deferred,extends,names,namesAndDescriptions,queryDescriptionFor,getBases,getDescriptionFor,getDoc,getName,getTaggedValue,getTaggedValueTags,isEqualOrExtendedBy,setTaggedValue,isImplementedByInstancesOf,adaptWith,is_implemented_by,implementedBy,providedBy +[TYPECHECK] -# List of method names used to declare (i.e. assign) instance attributes. -defining-attr-methods=__init__,__new__,setUp +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager -# List of valid names for the first argument in a class method. -valid-classmethod-first-arg=cls +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of symbolic message names to ignore for Mixin members. +ignored-checks-for-mixins=no-member, + not-async-context-manager, + not-context-manager, + attribute-defined-outside-init + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local,argparse.Namespace,Field,Header,JWS,closing -# List of valid names for the first argument in a metaclass class method. -valid-metaclass-classmethod-first-arg=mcs +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis +ignored-modules=confargparse,argparse +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes -[DESIGN] +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 -# Maximum number of arguments for function / method -max-args=6 +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 -# Argument names that match this expression will be ignored. Default to name -# with leading underscore -ignored-argument-names=_.* +# Regex pattern to define which classes are considered mixins. +mixin-class-rgx=.*[Mm]ixin -# Maximum number of locals for function / method body -max-locals=15 +# List of decorators that change the signature of a decorated function. +signature-mutators= -# Maximum number of return / yield for function / method body -max-returns=6 -# Maximum number of branch for function / method body -max-branches=12 +[VARIABLES] -# Maximum number of statements in function / method body -max-statements=50 +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= -# Maximum number of parents for a class (see R0901). -max-parents=12 +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes -# Maximum number of attributes for a class (see R0902). -max-attributes=7 +# List of names allowed to shadow builtins +allowed-redefined-builtins= -# Minimum number of public methods for a class (see R0903). -min-public-methods=2 +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb -# Maximum number of public methods for a class (see R0904). -max-public-methods=20 +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ +# Argument names that match this expression will be ignored. +ignored-argument-names=_.*|^ignored_|^unused_ -[EXCEPTIONS] +# Tells whether we should check for unused import in __init__ files. +init-import=no -# Exceptions that will emit a warning when being caught. Defaults to -# "Exception" -overgeneral-exceptions=Exception +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a35d43b6389..00000000000 --- a/.travis.yml +++ /dev/null @@ -1,110 +0,0 @@ -language: python - -cache: - directories: - - $HOME/.cache/pip - -before_install: - - '([ $TRAVIS_OS_NAME == linux ] && dpkg -s libaugeas0) || (brew update && brew install augeas python3 && brew upgrade python && brew link python)' - -before_script: - - 'if [ $TRAVIS_OS_NAME = osx ] ; then ulimit -n 1024 ; fi' - - export TOX_TESTENV_PASSENV=TRAVIS - -matrix: - include: - - python: "2.7" - env: BOULDER_INTEGRATION=v1 INTEGRATION_TEST=all TOXENV=py27_install - sudo: required - services: docker - - python: "2.7" - env: BOULDER_INTEGRATION=v2 INTEGRATION_TEST=all TOXENV=py27_install - sudo: required - services: docker - - python: "2.7" - env: TOXENV=py27-cover FYI="py27 tests + code coverage" - - sudo: required - env: TOXENV=nginx_compat - services: docker - before_install: - addons: - - python: "2.7" - env: TOXENV=lint - - python: "3.4" - env: TOXENV=mypy - - python: "3.5" - env: TOXENV=mypy - - python: "2.7" - env: TOXENV='py27-{acme,apache,certbot,dns,nginx,postfix}-oldest' - sudo: required - services: docker - - python: "3.4" - env: TOXENV=py34 - sudo: required - services: docker - - python: "3.7" - dist: xenial - env: TOXENV=py37 - sudo: required - services: docker - - sudo: required - env: TOXENV=apache_compat - services: docker - before_install: - addons: - - sudo: required - env: TOXENV=le_auto_trusty - services: docker - before_install: - addons: - - python: "2.7" - env: TOXENV=apacheconftest-with-pebble - sudo: required - services: docker - - python: "2.7" - env: TOXENV=nginxroundtrip - - -# Only build pushes to the master branch, PRs, and branches beginning with -# `test-` or of the form `digit(s).digit(s).x`. This reduces the number of -# simultaneous Travis runs, which speeds turnaround time on review since there -# is a cap of on the number of simultaneous runs. -branches: - only: - - master - - /^\d+\.\d+\.x$/ - - /^test-.*$/ - -# container-based infrastructure -sudo: false - -addons: - apt: - packages: # Keep in sync with letsencrypt-auto-source/pieces/bootstrappers/deb_common.sh and Boulder. - - python-dev - - python-virtualenv - - gcc - - libaugeas0 - - libssl-dev - - libffi-dev - - ca-certificates - # For certbot-nginx integration testing - - nginx-light - - openssl - -install: "travis_retry $(command -v pip || command -v pip3) install codecov tox" -script: - - travis_retry tox - - '[ -z "${BOULDER_INTEGRATION+x}" ] || (travis_retry tests/boulder-fetch.sh && tests/tox-boulder-integration.sh)' - -after_success: '[ "$TOXENV" == "py27-cover" ] && codecov' - -notifications: - email: false - irc: - channels: - - secure: "SGWZl3ownKx9xKVV2VnGt7DqkTmutJ89oJV9tjKhSs84kLijU6EYdPnllqISpfHMTxXflNZuxtGo0wTDYHXBuZL47w1O32W6nzuXdra5zC+i4sYQwYULUsyfOv9gJX8zWAULiK0Z3r0oho45U+FR5ZN6TPCidi8/eGU+EEPwaAw=" - on_cancel: never - on_success: never - on_failure: always - use_notice: true diff --git a/AUTHORS.md b/AUTHORS.md index 6ee739bc0c3..17fa3313459 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -1,10 +1,12 @@ Authors ======= +* [Aaron Gable](https://github.com/aarongable) * [Aaron Zirbes](https://github.com/aaronzirbes) * Aaron Zuehlke * Ada Lovelace * [Adam Woodbeck](https://github.com/awoodbeck) +* [Adrien Ferrand](https://github.com/adferrand) * [Aidin Gharibnavaz](https://github.com/aidin36) * [AJ ONeal](https://github.com/coolaj86) * [Alcaro](https://github.com/Alcaro) @@ -14,10 +16,17 @@ Authors * [Alex Gaynor](https://github.com/alex) * [Alex Halderman](https://github.com/jhalderm) * [Alex Jordan](https://github.com/strugee) +* [Alex Zorin](https://github.com/alexzorin) +* [Alexis Hancock](https://github.com/zoracon) +* [Amir Omidi](https://github.com/aaomidi) * [Amjad Mashaal](https://github.com/TheNavigat) +* [amplifi](https://github.com/amplifi) * [Andrew Murray](https://github.com/radarhere) +* [Andrzej Górski](https://github.com/andrzej3393) +* [Anna Glasgall](https://github.com/aglasgall) * [Anselm Levskaya](https://github.com/levskaya) * [Antoine Jacoutot](https://github.com/ajacoutot) +* [April King](https://github.com/april) * [asaph](https://github.com/asaph) * [Axel Beckert](https://github.com/xtaran) * [Bas](https://github.com/Mechazawa) @@ -32,7 +41,9 @@ Authors * [Blake Griffith](https://github.com/cowlicks) * [Brad Warren](https://github.com/bmw) * [Brandon Kraft](https://github.com/kraftbj) -* [Brandon Kreisel](https://github.com/kraftbj) +* [Brandon Kreisel](https://github.com/BKreisel) +* [Brian Heim](https://github.com/brianlheim) +* [Cameron Steel](https://github.com/Tugzrida) * [Ceesjan Luiten](https://github.com/quinox) * [Chad Whitacre](https://github.com/whit537) * [Chhatoi Pritam Baral](https://github.com/pritambaral) @@ -54,8 +65,11 @@ Authors * [DanCld](https://github.com/DanCld) * [Daniel Albers](https://github.com/AID) * [Daniel Aleksandersen](https://github.com/da2x) +* [Daniel Almasi](https://github.com/almasen) * [Daniel Convissor](https://github.com/convissor) +* [Daniel "Drex" Drexler](https://github.com/aeturnum) * [Daniel Huang](https://github.com/dhuang) +* [Daniel McMahon] (https://github.com/igloodan) * [Dave Guarino](https://github.com/daguar) * [David cz](https://github.com/dave-cz) * [David Dworken](https://github.com/ddworken) @@ -70,14 +84,18 @@ Authors * [Eric Engestrom](https://github.com/1ace) * [Eric Rescorla](https://github.com/ekr) * [Eric Wustrow](https://github.com/ewust) +* [Erik Morén](https://github.com/morre95) * [Erik Rose](https://github.com/erikrose) * [Eugene Kazakov](https://github.com/xgin) * [Fabian](https://github.com/faerbit) * [Faidon Liambotis](https://github.com/paravoid) * [Fan Jiang](https://github.com/tcz001) +* [Felix Lechner](https://github.com/lechner) * [Felix Schwarz](https://github.com/FelixSchwarz) * [Felix Yan](https://github.com/felixonmars) * [Filip Ochnik](https://github.com/filipochnik) +* [Florian Klink](https://github.com/flokli) +* [Francesco Colista](https://github.com/fcolista) * [Francois Marier](https://github.com/fmarier) * [Frank](https://github.com/Frankkkkk) * [Frederic BLANC](https://github.com/fblanc) @@ -96,14 +114,19 @@ Authors * [Harlan Lieberman-Berg](https://github.com/hlieberman) * [Henri Salo](https://github.com/fgeek) * [Henry Chen](https://github.com/henrychen95) +* [Hugo van Kemenade](https://github.com/hugovk) * [Ingolf Becker](https://github.com/watercrossing) +* [Ivan Nejgebauer](https://github.com/inejge) * [Jaap Eldering](https://github.com/eldering) * [Jacob Hoffman-Andrews](https://github.com/jsha) * [Jacob Sachs](https://github.com/jsachs) * [Jairo Llopis](https://github.com/Yajo) * [Jakub Warmuz](https://github.com/kuba) +* [James Balazs](https://github.com/jamesbalazs) * [James Kasten](https://github.com/jdkasten) * [Jason Grinblat](https://github.com/ptychomancer) +* [Jason Owen](https://github.com/jasonaowen) +* [Jawshua](https://github.com/jawshua) * [Jay Faulkner](https://github.com/jayofdoom) * [J.C. Jones](https://github.com/jcjones) * [Jeff Hodges](https://github.com/jmhodges) @@ -118,12 +141,16 @@ Authors * [John Reed](https://github.com/leerspace) * [Jonas Berlin](https://github.com/xkr47) * [Jonathan Herlin](https://github.com/Jonher937) +* [Jonathan Vanasco](https://github.com/jvanasco) * [Jon Walsh](https://github.com/code-tree) * [Joona Hoikkala](https://github.com/joohoi) +* [Josh McCullough](https://github.com/JoshMcCullough) * [Josh Soref](https://github.com/jsoref) * [Joubin Jabbari](https://github.com/joubin) * [Juho Juopperi](https://github.com/jkjuopperi) * [Kane York](https://github.com/riking) +* [Katsuyoshi Ozaki](https://github.com/moratori) +* [Kenichi Maehashi](https://github.com/kmaehashi) * [Kenneth Skovhede](https://github.com/kenkendk) * [Kevin Burke](https://github.com/kevinburke) * [Kevin London](https://github.com/kevinlondon) @@ -131,16 +158,20 @@ Authors * [LeCoyote](https://github.com/LeCoyote) * [Lee Watson](https://github.com/TheReverend403) * [Leo Famulari](https://github.com/lfam) +* [Leon G](https://github.com/LeonGr) * [lf](https://github.com/lf-) * [Liam Marshall](https://github.com/liamim) * [Lior Sabag](https://github.com/liorsbg) * [Lipis](https://github.com/lipis) * [lord63](https://github.com/lord63) +* [Lorenzo Fundaró](https://github.com/lfundaro) * [Luca Beltrame](https://github.com/lbeltrame) * [Luca Ebach](https://github.com/lucebac) * [Luca Olivetti](https://github.com/olivluca) * [Luke Rogers](https://github.com/lukeroge) +* [Lukhnos Liu](https://github.com/lukhnos) * [Maarten](https://github.com/mrtndwrd) +* [Mads Jensen](https://github.com/atombrella) * [Maikel Martens](https://github.com/krukas) * [Malte Janduda](https://github.com/MalteJ) * [Mantas MikulÄ—nas](https://github.com/grawity) @@ -156,11 +187,14 @@ Authors * [Mathieu Leduc-Hamel](https://github.com/mlhamel) * [Matt Bostock](https://github.com/mattbostock) * [Matthew Ames](https://github.com/SuperMatt) +* [Matthew W. Thomas](https://github.com/mwt) * [Michael Schumacher](https://github.com/schumaml) * [Michael Strache](https://github.com/Jarodiv) * [Michael Sverdlin](https://github.com/sveder) +* [Michael Watters](https://github.com/blackknight36) * [Michal Moravec](https://github.com/https://github.com/Majkl578) * [Michal Papis](https://github.com/mpapis) +* [Mickaël Schoentgen](https://github.com/BoboTiG) * [Minn Soe](https://github.com/MinnSoe) * [Min RK](https://github.com/minrk) * [Miquel Ruiz](https://github.com/miquelruiz) @@ -178,34 +212,44 @@ Authors * [osirisinferi](https://github.com/osirisinferi) * Patrick Figel * [Patrick Heppler](https://github.com/PatrickHeppler) +* [Paul Buonopane](https://github.com/Zenexer) * [Paul Feitzinger](https://github.com/pfeyz) +* [Paulo Dias](https://github.com/paulojmdias) * [Pavan Gupta](https://github.com/pavgup) * [Pavel Pavlov](https://github.com/ghost355) * [Peter Conrad](https://github.com/pconrad-fb) * [Peter Eckersley](https://github.com/pde) * [Peter Mosmans](https://github.com/PeterMosmans) +* [Phil Martin](https://github.com/frillip) * [Philippe Langlois](https://github.com/langloisjp) * [Philipp Spitzer](https://github.com/spitza) * [Piero Steinger](https://github.com/Jadaw1n) * [Pierre Jaury](https://github.com/kaiyou) * [Piotr Kasprzyk](https://github.com/kwadrat) * [Prayag Verma](https://github.com/pra85) +* [Preston Locke](https://github.com/Preston12321) +* [Q Misell][https://magicalcodewit.ch] +* [Rasesh Patel](https://github.com/raspat1) * [Reinaldo de Souza Jr](https://github.com/juniorz) * [Remi Rampin](https://github.com/remram44) * [Rémy HUBSCHER](https://github.com/Natim) * [Rémy Léone](https://github.com/sieben) * [Richard Barnes](https://github.com/r-barnes) +* [Richard Harman](https://github.com/warewolf) * [Richard Panek](https://github.com/kernelpanek) * [Robert Buchholz](https://github.com/rbu) +* [Robert Dailey](https://github.com/pahrohfit) * [Robert Habermann](https://github.com/frennkie) * [Robert Xiao](https://github.com/nneonneo) * [Roland Shoemaker](https://github.com/rolandshoemaker) * [Roy Wellington â…£](https://github.com/thanatos) * [rugk](https://github.com/rugk) +* [seeg](https://github.com/s33g) * [Sachi King](https://github.com/nakato) * [Sagi Kedmi](https://github.com/sagi) * [Sam Lanning](https://github.com/samlanning) * [sapics](https://github.com/sapics) +* [SATOH Fumiyasu](https://github.com/fumiyas) * [Scott Barr](https://github.com/scottjbarr) * [Scott Merrill](https://github.com/skpy) * [Sebastian Bögl](https://github.com/TheBoegl) @@ -222,8 +266,11 @@ Authors * [Spencer Bliven](https://github.com/sbliven) * [Stacey Sheldon](https://github.com/solidgoldbomb) * [Stavros Korokithakis](https://github.com/skorokithakis) +* [Ștefan Talpalaru](https://github.com/stefantalpalaru) * [Stefan Weil](https://github.com/stweil) * [Steve Desmond](https://github.com/stevedesmond-ca) +* [sydneyli](https://github.com/sydneyli) +* [taixx046](https://github.com/taixx046) * [Tan Jay Jun](https://github.com/jayjun) * [Tapple Gao](https://github.com/tapple) * [Telepenin Nikolay](https://github.com/telepenin) @@ -248,6 +295,7 @@ Authors * [Wilfried Teiken](https://github.com/wteiken) * [Willem Fibbe](https://github.com/fibbers) * [William Budington](https://github.com/Hainish) +* [Will Greenberg](https://github.com/wgreenberg) * [Will Newby](https://github.com/willnewby) * [Will Oller](https://github.com/willoller) * [Yan](https://github.com/diracdeltas) @@ -255,5 +303,8 @@ Authors * [Yomna](https://github.com/ynasser) * [Yoni Jah](https://github.com/yonjah) * [YourDaddyIsHere](https://github.com/YourDaddyIsHere) +* [Yuseong Cho](https://github.com/g6123) * [Zach Shepherd](https://github.com/zjs) * [陈三](https://github.com/chenxsan) +* [Shahar Naveh](https://github.com/ShaharNaveh) + diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 1861e38bf48..00000000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,1408 +0,0 @@ -# Certbot change log - -Certbot adheres to [Semantic Versioning](https://semver.org/). - -## 0.31.0 - master - -### Added - -* Avoid to process again challenges that are already validated - when a certificate is issued. - -### Changed - -* Lexicon-based DNS plugins are now fully compatible with Lexicon 3.x (support - on 2.x branch is maintained). - -### Fixed - -* Fixed accessing josepy contents through acme.jose when the full acme.jose - path is used. -* Clarify behavior for deleting certs as part of revocation. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -package with changes other than its version number was: - -* acme -* certbot -* certbot-dns-cloudxns -* certbot-dns-dnsimple -* certbot-dns-dnsmadeeasy -* certbot-dns-gehirn -* certbot-dns-linode -* certbot-dns-luadns -* certbot-dns-nsone -* certbot-dns-ovh -* certbot-dns-sakuracloud - -More details about these changes can be found on our GitHub repo. - -## 0.30.0 - 2019-01-02 - -### Added - -* Added the `update_account` subcommand for account management commands. - -### Changed - -* Copied account management functionality from the `register` subcommand - to the `update_account` subcommand. -* Marked usage `register --update-registration` for deprecation and - removal in a future release. - -### Fixed - -* Older modules in the josepy library can now be accessed through acme.jose - like it could in previous versions of acme. This is only done to preserve - backwards compatibility and support for doing this with new modules in josepy - will not be added. Users of the acme library should switch to using josepy - directly if they haven't done so already. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -package with changes other than its version number was: - -* acme - -More details about these changes can be found on our GitHub repo. - -## 0.29.1 - 2018-12-05 - -### Added - -* - -### Changed - -* - -### Fixed - -* The default work and log directories have been changed back to - /var/lib/letsencrypt and /var/log/letsencrypt respectively. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -package with changes other than its version number was: - -* certbot - -More details about these changes can be found on our GitHub repo. - -## 0.29.0 - 2018-12-05 - -### Added - -* Noninteractive renewals with `certbot renew` (those not started from a - terminal) now randomly sleep 1-480 seconds before beginning work in - order to spread out load spikes on the server side. -* Added External Account Binding support in cli and acme library. - Command line arguments --eab-kid and --eab-hmac-key added. - -### Changed - -* Private key permissioning changes: Renewal preserves existing group mode - & gid of previous private key material. Private keys for new - lineages (i.e. new certs, not renewed) default to 0o600. - -### Fixed - -* Update code and dependencies to clean up Resource and Deprecation Warnings. -* Only depend on imgconverter extension for Sphinx >= 1.6 - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -package with changes other than its version number was: - -* acme -* certbot -* certbot-apache -* certbot-dns-cloudflare -* certbot-dns-digitalocean -* certbot-dns-google -* certbot-nginx - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/62?closed=1 - -## 0.28.0 - 2018-11-7 - -### Added - -* `revoke` accepts `--cert-name`, and doesn't accept both `--cert-name` and `--cert-path`. -* Use the ACMEv2 newNonce endpoint when a new nonce is needed, and newNonce is available in the directory. - -### Changed - -* Removed documentation mentions of `#letsencrypt` IRC on Freenode. -* Write README to the base of (config-dir)/live directory -* `--manual` will explicitly warn users that earlier challenges should remain in place when setting up subsequent challenges. -* Warn when using deprecated acme.challenges.TLSSNI01 -* Log warning about TLS-SNI deprecation in Certbot -* Stop preferring TLS-SNI in the Apache, Nginx, and standalone plugins -* OVH DNS plugin now relies on Lexicon>=2.7.14 to support HTTP proxies -* Default time the Linode plugin waits for DNS changes to propogate is now 1200 seconds. - -### Fixed - -* Match Nginx parser update in allowing variable names to start with `${`. -* Fix ranking of vhosts in Nginx so that all port-matching vhosts come first -* Correct OVH integration tests on machines without internet access. -* Stop caching the results of ipv6_info in http01.py -* Test fix for Route53 plugin to prevent boto3 making outgoing connections. -* The grammar used by Augeas parser in Apache plugin was updated to fix various parsing errors. -* The CloudXNS, DNSimple, DNS Made Easy, Gehirn, Linode, LuaDNS, NS1, OVH, and - Sakura Cloud DNS plugins are now compatible with Lexicon 3.0+. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -package with changes other than its version number was: - -* acme -* certbot -* certbot-apache -* certbot-dns-cloudxns -* certbot-dns-dnsimple -* certbot-dns-dnsmadeeasy -* certbot-dns-gehirn -* certbot-dns-linode -* certbot-dns-luadns -* certbot-dns-nsone -* certbot-dns-ovh -* certbot-dns-route53 -* certbot-dns-sakuracloud -* certbot-nginx - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/59?closed=1 - -## 0.27.1 - 2018-09-06 - -### Fixed - -* Fixed parameter name in OpenSUSE overrides for default parameters in the - Apache plugin. Certbot on OpenSUSE works again. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -package with changes other than its version number was: - -* certbot-apache - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/60?closed=1 - -## 0.27.0 - 2018-09-05 - -### Added - -* The Apache plugin now accepts the parameter --apache-ctl which can be - used to configure the path to the Apache control script. - -### Changed - -* When using `acme.client.ClientV2` (or - `acme.client.BackwardsCompatibleClientV2` with an ACME server that supports a - newer version of the ACME protocol), an `acme.errors.ConflictError` will be - raised if you try to create an ACME account with a key that has already been - used. Previously, a JSON parsing error was raised in this scenario when using - the library with Let's Encrypt's ACMEv2 endpoint. - -### Fixed - -* When Apache is not installed, Certbot's Apache plugin no longer prints - messages about being unable to find apachectl to the terminal when the plugin - is not selected. -* If you're using the Apache plugin with the --apache-vhost-root flag set to a - directory containing a disabled virtual host for the domain you're requesting - a certificate for, the virtual host will now be temporarily enabled if - necessary to pass the HTTP challenge. -* The documentation for the Certbot package can now be built using Sphinx 1.6+. -* You can now call `query_registration` without having to first call - `new_account` on `acme.client.ClientV2` objects. -* The requirement of `setuptools>=1.0` has been removed from `certbot-dns-ovh`. -* Names in certbot-dns-sakuracloud's tests have been updated to refer to Sakura - Cloud rather than NS1 whose plugin certbot-dns-sakuracloud was based on. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -package with changes other than its version number was: - -* acme -* certbot -* certbot-apache -* certbot-dns-ovh -* certbot-dns-sakuracloud - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/57?closed=1 - -## 0.26.1 - 2018-07-17 - -### Fixed - -* Fix a bug that was triggered when users who had previously manually set `--server` to get ACMEv2 certs tried to renew ACMEv1 certs. - -Despite us having broken lockstep, we are continuing to release new versions of all Certbot components during releases for the time being, however, the only package with changes other than its version number was: - -* certbot - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/58?closed=1 - -## 0.26.0 - 2018-07-11 - -### Added - -* A new security enhancement which we're calling AutoHSTS has been added to - Certbot's Apache plugin. This enhancement configures your webserver to send a - HTTP Strict Transport Security header with a low max-age value that is slowly - increased over time. The max-age value is not increased to a large value - until you've successfully managed to renew your certificate. This enhancement - can be requested with the --auto-hsts flag. -* New official DNS plugins have been created for Gehirn Infrastracture Service, - Linode, OVH, and Sakura Cloud. These plugins can be found on our Docker Hub - page at https://hub.docker.com/u/certbot and on PyPI. -* The ability to reuse ACME accounts from Let's Encrypt's ACMEv1 endpoint on - Let's Encrypt's ACMEv2 endpoint has been added. -* Certbot and its components now support Python 3.7. -* Certbot's install subcommand now allows you to interactively choose which - certificate to install from the list of certificates managed by Certbot. -* Certbot now accepts the flag `--no-autorenew` which causes any obtained - certificates to not be automatically renewed when it approaches expiration. -* Support for parsing the TLS-ALPN-01 challenge has been added back to the acme - library. - -### Changed - -* Certbot's default ACME server has been changed to Let's Encrypt's ACMEv2 - endpoint. By default, this server will now be used for both new certificate - lineages and renewals. -* The Nginx plugin is no longer marked labeled as an "Alpha" version. -* The `prepare` method of Certbot's plugins is no longer called before running - "Updater" enhancements that are run on every invocation of `certbot renew`. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -packages with functional changes were: - -* acme -* certbot -* certbot-apache -* certbot-dns-gehirn -* certbot-dns-linode -* certbot-dns-ovh -* certbot-dns-sakuracloud -* certbot-nginx - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/55?closed=1 - -## 0.25.1 - 2018-06-13 - -### Fixed - -* TLS-ALPN-01 support has been removed from our acme library. Using our current - dependencies, we are unable to provide a correct implementation of this - challenge so we decided to remove it from the library until we can provide - proper support. -* Issues causing test failures when running the tests in the acme package with - pytest<3.0 has been resolved. -* certbot-nginx now correctly depends on acme>=0.25.0. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -packages with changes other than their version number were: - -* acme -* certbot-nginx - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/56?closed=1 - -## 0.25.0 - 2018-06-06 - -### Added - -* Support for the ready status type was added to acme. Without this change, - Certbot and acme users will begin encountering errors when using Let's - Encrypt's ACMEv2 API starting on June 19th for the staging environment and - July 5th for production. See - https://community.letsencrypt.org/t/acmev2-order-ready-status/62866 for more - information. -* Certbot now accepts the flag --reuse-key which will cause the same key to be - used in the certificate when the lineage is renewed rather than generating a - new key. -* You can now add multiple email addresses to your ACME account with Certbot by - providing a comma separated list of emails to the --email flag. -* Support for Let's Encrypt's upcoming TLS-ALPN-01 challenge was added to acme. - For more information, see - https://community.letsencrypt.org/t/tls-alpn-validation-method/63814/1. -* acme now supports specifying the source address to bind to when sending - outgoing connections. You still cannot specify this address using Certbot. -* If you run Certbot against Let's Encrypt's ACMEv2 staging server but don't - already have an account registered at that server URL, Certbot will - automatically reuse your staging account from Let's Encrypt's ACMEv1 endpoint - if it exists. -* Interfaces were added to Certbot allowing plugins to be called at additional - points. The `GenericUpdater` interface allows plugins to perform actions - every time `certbot renew` is run, regardless of whether any certificates are - due for renewal, and the `RenewDeployer` interface allows plugins to perform - actions when a certificate is renewed. See `certbot.interfaces` for more - information. - -### Changed - -* When running Certbot with --dry-run and you don't already have a staging - account, the created account does not contain an email address even if one - was provided to avoid expiration emails from Let's Encrypt's staging server. -* certbot-nginx does a better job of automatically detecting the location of - Nginx's configuration files when run on BSD based systems. -* acme now requires and uses pytest when running tests with setuptools with - `python setup.py test`. -* `certbot config_changes` no longer waits for user input before exiting. - -### Fixed - -* Misleading log output that caused users to think that Certbot's standalone - plugin failed to bind to a port when performing a challenge has been - corrected. -* An issue where certbot-nginx would fail to enable HSTS if the server block - already had an `add_header` directive has been resolved. -* certbot-nginx now does a better job detecting the server block to base the - configuration for TLS-SNI challenges on. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -packages with functional changes were: - -* acme -* certbot -* certbot-apache -* certbot-nginx - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/54?closed=1 - -## 0.24.0 - 2018-05-02 - -### Added - -* certbot now has an enhance subcommand which allows you to configure security - enhancements like HTTP to HTTPS redirects, OCSP stapling, and HSTS without - reinstalling a certificate. -* certbot-dns-rfc2136 now allows the user to specify the port to use to reach - the DNS server in its credentials file. -* acme now parses the wildcard field included in authorizations so it can be - used by users of the library. - -### Changed - -* certbot-dns-route53 used to wait for each DNS update to propagate before - sending the next one, but now it sends all updates before waiting which - speeds up issuance for multiple domains dramatically. -* Certbot's official Docker images are now based on Alpine Linux 3.7 rather - than 3.4 because 3.4 has reached its end-of-life. -* We've doubled the time Certbot will spend polling authorizations before - timing out. -* The level of the message logged when Certbot is being used with - non-standard paths warning that crontabs for renewal included in Certbot - packages from OS package managers may not work has been reduced. This stops - the message from being written to stderr every time `certbot renew` runs. - -### Fixed - -* certbot-auto now works with Python 3.6. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -packages with changes other than their version number were: - -* acme -* certbot -* certbot-apache -* certbot-dns-digitalocean (only style improvements to tests) -* certbot-dns-rfc2136 - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/52?closed=1 - -## 0.23.0 - 2018-04-04 - -### Added - -* Support for OpenResty was added to the Nginx plugin. - -### Changed - -* The timestamps in Certbot's logfiles now use the system's local time zone - rather than UTC. -* Certbot's DNS plugins that use Lexicon now rely on Lexicon>=2.2.1 to be able - to create and delete multiple TXT records on a single domain. -* certbot-dns-google's test suite now works without an internet connection. - -### Fixed - -* Removed a small window that if during which an error occurred, Certbot - wouldn't clean up performed challenges. -* The parameters `default` and `ipv6only` are now removed from `listen` - directives when creating a new server block in the Nginx plugin. -* `server_name` directives enclosed in quotation marks in Nginx are now properly - supported. -* Resolved an issue preventing the Apache plugin from starting Apache when it's - not currently running on RHEL and Gentoo based systems. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -packages with changes other than their version number were: - -* certbot -* certbot-apache -* certbot-dns-cloudxns -* certbot-dns-dnsimple -* certbot-dns-dnsmadeeasy -* certbot-dns-google -* certbot-dns-luadns -* certbot-dns-nsone -* certbot-dns-rfc2136 -* certbot-nginx - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/50?closed=1 - -## 0.22.2 - 2018-03-19 - -### Fixed - -* A type error introduced in 0.22.1 that would occur during challenge cleanup - when a Certbot plugin raises an exception while trying to complete the - challenge was fixed. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -packages with changes other than their version number were: - -* certbot - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/53?closed=1 - -## 0.22.1 - 2018-03-19 - -### Changed - -* The ACME server used with Certbot's --dry-run and --staging flags is now - Let's Encrypt's ACMEv2 staging server which allows people to also test ACMEv2 - features with these flags. - -### Fixed - -* The HTTP Content-Type header is now set to the correct value during - certificate revocation with new versions of the ACME protocol. -* When using Certbot with Let's Encrypt's ACMEv2 server, it would add a blank - line to the top of chain.pem and between the certificates in fullchain.pem - for each lineage. These blank lines have been removed. -* Resolved a bug that caused Certbot's --allow-subset-of-names flag not to - work. -* Fixed a regression in acme.client.Client that caused the class to not work - when it was initialized without a ClientNetwork which is done by some of the - other projects using our ACME library. - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -packages with changes other than their version number were: - -* acme -* certbot - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/51?closed=1 - -## 0.22.0 - 2018-03-07 - -### Added - -* Support for obtaining wildcard certificates and a newer version of the ACME - protocol such as the one implemented by Let's Encrypt's upcoming ACMEv2 - endpoint was added to Certbot and its ACME library. Certbot still works with - older ACME versions and will automatically change the version of the protocol - used based on the version the ACME CA implements. -* The Apache and Nginx plugins are now able to automatically install a wildcard - certificate to multiple virtual hosts that you select from your server - configuration. -* The `certbot install` command now accepts the `--cert-name` flag for - selecting a certificate. -* `acme.client.BackwardsCompatibleClientV2` was added to Certbot's ACME library - which automatically handles most of the differences between new and old ACME - versions. `acme.client.ClientV2` is also available for people who only want - to support one version of the protocol or want to handle the differences - between versions themselves. -* certbot-auto now supports the flag --install-only which has the script - install Certbot and its dependencies and exit without invoking Certbot. -* Support for issuing a single certificate for a wildcard and base domain was - added to our Google Cloud DNS plugin. To do this, we now require your API - credentials have additional permissions, however, your credentials will - already have these permissions unless you defined a custom role with fewer - permissions than the standard DNS administrator role provided by Google. - These permissions are also only needed for the case described above so it - will continue to work for existing users. For more information about the - permissions changes, see the documentation in the plugin. - -### Changed - -* We have broken lockstep between our ACME library, Certbot, and its plugins. - This means that the different components do not need to be the same version - to work together like they did previously. This makes packaging easier - because not every piece of Certbot needs to be repackaged to ship a change to - a subset of its components. -* Support for Python 2.6 and Python 3.3 has been removed from ACME, Certbot, - Certbot's plugins, and certbot-auto. If you are using certbot-auto on a RHEL - 6 based system, it will walk you through the process of installing Certbot - with Python 3 and refuse to upgrade to a newer version of Certbot until you - have done so. -* Certbot's components now work with older versions of setuptools to simplify - packaging for EPEL 7. - -### Fixed - -* Issues caused by Certbot's Nginx plugin adding multiple ipv6only directives - has been resolved. -* A problem where Certbot's Apache plugin would add redundant include - directives for the TLS configuration managed by Certbot has been fixed. -* Certbot's webroot plugin now properly deletes any directories it creates. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/48?closed=1 - -## 0.21.1 - 2018-01-25 - -### Fixed - -* When creating an HTTP to HTTPS redirect in Nginx, we now ensure the Host - header of the request is set to an expected value before redirecting users to - the domain found in the header. The previous way Certbot configured Nginx - redirects was a potential security issue which you can read more about at - https://community.letsencrypt.org/t/security-issue-with-redirects-added-by-certbots-nginx-plugin/51493. -* Fixed a problem where Certbot's Apache plugin could fail HTTP-01 challenges - if basic authentication is configured for the domain you request a - certificate for. -* certbot-auto --no-bootstrap now properly tries to use Python 3.4 on RHEL 6 - based systems rather than Python 2.6. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/49?closed=1 - -## 0.21.0 - 2018-01-17 - -### Added - -* Support for the HTTP-01 challenge type was added to our Apache and Nginx - plugins. For those not aware, Let's Encrypt disabled the TLS-SNI-01 challenge - type which was what was previously being used by our Apache and Nginx plugins - last week due to a security issue. For more information about Let's Encrypt's - change, click - [here](https://community.letsencrypt.org/t/2018-01-11-update-regarding-acme-tls-sni-and-shared-hosting-infrastructure/50188). - Our Apache and Nginx plugins will automatically switch to use HTTP-01 so no - changes need to be made to your Certbot configuration, however, you should - make sure your server is accessible on port 80 and isn't behind an external - proxy doing things like redirecting all traffic from HTTP to HTTPS. HTTP to - HTTPS redirects inside Apache and Nginx are fine. -* IPv6 support was added to the Nginx plugin. -* Support for automatically creating server blocks based on the default server - block was added to the Nginx plugin. -* The flags --delete-after-revoke and --no-delete-after-revoke were added - allowing users to control whether the revoke subcommand also deletes the - certificates it is revoking. - -### Changed - -* We deprecated support for Python 2.6 and Python 3.3 in Certbot and its ACME - library. Support for these versions of Python will be removed in the next - major release of Certbot. If you are using certbot-auto on a RHEL 6 based - system, it will guide you through the process of installing Python 3. -* We split our implementation of JOSE (Javascript Object Signing and - Encryption) out of our ACME library and into a separate package named josepy. - This package is available on [PyPI](https://pypi.python.org/pypi/josepy) and - on [GitHub](https://github.com/certbot/josepy). -* We updated the ciphersuites used in Apache to the new [values recommended by - Mozilla](https://wiki.mozilla.org/Security/Server_Side_TLS#Intermediate_compatibility_.28default.29). - The major change here is adding ChaCha20 to the list of supported - ciphersuites. - -### Fixed - -* An issue with our Apache plugin on Gentoo due to differences in their - apache2ctl command have been resolved. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/47?closed=1 - -## 0.20.0 - 2017-12-06 - -### Added - -* Certbot's ACME library now recognizes URL fields in challenge objects in - preparation for Let's Encrypt's new ACME endpoint. The value is still - accessible in our ACME library through the name "uri". - -### Changed - -* The Apache plugin now parses some distro specific Apache configuration files - on non-Debian systems allowing it to get a clearer picture on the running - configuration. Internally, these changes were structured so that external - contributors can easily write patches to make the plugin work in new Apache - configurations. -* Certbot better reports network failures by removing information about - connection retries from the error output. -* An unnecessary question when using Certbot's webroot plugin interactively has - been removed. - -### Fixed - -* Certbot's NGINX plugin no longer sometimes incorrectly reports that it was - unable to deploy a HTTP->HTTPS redirect when requesting Certbot to enable a - redirect for multiple domains. -* Problems where the Apache plugin was failing to find directives and - duplicating existing directives on openSUSE have been resolved. -* An issue running the test shipped with Certbot and some our DNS plugins with - older versions of mock have been resolved. -* On some systems, users reported strangely interleaved output depending on - when stdout and stderr were flushed. This problem was resolved by having - Certbot regularly flush these streams. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/44?closed=1 - -## 0.19.0 - 2017-10-04 - -### Added - -* Certbot now has renewal hook directories where executable files can be placed - for Certbot to run with the renew subcommand. Pre-hooks, deploy-hooks, and - post-hooks can be specified in the renewal-hooks/pre, renewal-hooks/deploy, - and renewal-hooks/post directories respectively in Certbot's configuration - directory (which is /etc/letsencrypt by default). Certbot will automatically - create these directories when it is run if they do not already exist. -* After revoking a certificate with the revoke subcommand, Certbot will offer - to delete the lineage associated with the certificate. When Certbot is run - with --non-interactive, it will automatically try to delete the associated - lineage. -* When using Certbot's Google Cloud DNS plugin on Google Compute Engine, you no - longer have to provide a credential file to Certbot if you have configured - sufficient permissions for the instance which Certbot can automatically - obtain using Google's metadata service. - -### Changed - -* When deleting certificates interactively using the delete subcommand, Certbot - will now allow you to select multiple lineages to be deleted at once. -* Certbot's Apache plugin no longer always parses Apache's sites-available on - Debian based systems and instead only parses virtual hosts included in your - Apache configuration. You can provide an additional directory for Certbot to - parse using the command line flag --apache-vhost-root. - -### Fixed - -* The plugins subcommand can now be run without root access. -* certbot-auto now includes a timeout when updating itself so it no longer - hangs indefinitely when it is unable to connect to the external server. -* An issue where Certbot's Apache plugin would sometimes fail to deploy a - certificate on Debian based systems if mod_ssl wasn't already enabled has - been resolved. -* A bug in our Docker image where the certificates subcommand could not report - if certificates maintained by Certbot had been revoked has been fixed. -* Certbot's RFC 2136 DNS plugin (for use with software like BIND) now properly - performs DNS challenges when the domain being verified contains a CNAME - record. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/43?closed=1 - -## 0.18.2 - 2017-09-20 - -### Fixed - -* An issue where Certbot's ACME module would raise an AttributeError trying to - create self-signed certificates when used with pyOpenSSL 17.3.0 has been - resolved. For Certbot users with this version of pyOpenSSL, this caused - Certbot to crash when performing a TLS SNI challenge or when the Nginx plugin - tried to create an SSL server block. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/46?closed=1 - -## 0.18.1 - 2017-09-08 - -### Fixed - -* If certbot-auto was running as an unprivileged user and it upgraded from - 0.17.0 to 0.18.0, it would crash with a permissions error and would need to - be run again to successfully complete the upgrade. This has been fixed and - certbot-auto should upgrade cleanly to 0.18.1. -* Certbot usually uses "certbot-auto" or "letsencrypt-auto" in error messages - and the User-Agent string instead of "certbot" when you are using one of - these wrapper scripts. Proper detection of this was broken with Certbot's new - installation path in /opt in 0.18.0 but this problem has been resolved. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/45?closed=1 - -## 0.18.0 - 2017-09-06 - -### Added - -* The Nginx plugin now configures Nginx to use 2048-bit Diffie-Hellman - parameters. Java 6 clients do not support Diffie-Hellman parameters larger - than 1024 bits, so if you need to support these clients you will need to - manually modify your Nginx configuration after using the Nginx installer. - -### Changed - -* certbot-auto now installs Certbot in directories under `/opt/eff.org`. If you - had an existing installation from certbot-auto, a symlink is created to the - new directory. You can configure certbot-auto to use a different path by - setting the environment variable VENV_PATH. -* The Nginx plugin can now be selected in Certbot's interactive output. -* Output verbosity of renewal failures when running with `--quiet` has been - reduced. -* The default revocation reason shown in Certbot help output now is a human - readable string instead of a numerical code. -* Plugin selection is now included in normal terminal output. - -### Fixed - -* A newer version of ConfigArgParse is now installed when using certbot-auto - causing values set to false in a Certbot INI configuration file to be handled - intuitively. Setting a boolean command line flag to false is equivalent to - not including it in the configuration file at all. -* New naming conventions preventing certbot-auto from installing OS - dependencies on Fedora 26 have been resolved. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/42?closed=1 - -## 0.17.0 - 2017-08-02 - -### Added - -* Support in our nginx plugin for modifying SSL server blocks that do - not contain certificate or key directives. -* A `--max-log-backups` flag to allow users to configure or even completely - disable Certbot's built in log rotation. -* A `--user-agent-comment` flag to allow people who build tools around Certbot - to differentiate their user agent string by adding a comment to its default - value. - -### Changed - -* Due to some awesome work by - [cryptography project](https://github.com/pyca/cryptography), compilation can - now be avoided on most systems when using certbot-auto. This eliminates many - problems people have had in the past such as running out of memory, having - invalid headers/libraries, and changes to the OS packages on their system - after compilation breaking Certbot. -* The `--renew-hook` flag has been hidden in favor of `--deploy-hook`. This new - flag works exactly the same way except it is always run when a certificate is - issued rather than just when it is renewed. -* We have started printing deprecation warnings in certbot-auto for - experimentally supported systems with OS packages available. -* A certificate lineage's name is included in error messages during renewal. - -### Fixed - -* Encoding errors that could occur when parsing error messages from the ACME - server containing Unicode have been resolved. -* certbot-auto no longer prints misleading messages about there being a newer - pip version available when installation fails. -* Certbot's ACME library now properly extracts domains from critical SAN - extensions. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.17.0+is%3Aclosed - -## 0.16.0 - 2017-07-05 - -### Added - -* A plugin for performing DNS challenges using dynamic DNS updates as defined - in RFC 2316. This plugin is packaged separately from Certbot and is available - at https://pypi.python.org/pypi/certbot-dns-rfc2136. It supports Python 2.6, - 2.7, and 3.3+. At this time, there isn't a good way to install this plugin - when using certbot-auto, but this should change in the near future. -* Plugins for performing DNS challenges for the providers - [DNS Made Easy](https://pypi.python.org/pypi/certbot-dns-dnsmadeeasy) and - [LuaDNS](https://pypi.python.org/pypi/certbot-dns-luadns). These plugins are - packaged separately from Certbot and support Python 2.7 and 3.3+. Currently, - there isn't a good way to install these plugins when using certbot-auto, - but that should change soon. -* Support for performing TLS-SNI-01 challenges when using the manual plugin. -* Automatic detection of Arch Linux in the Apache plugin providing better - default settings for the plugin. - -### Changed - -* The text of the interactive question about whether a redirect from HTTP to - HTTPS should be added by Certbot has been rewritten to better explain the - choices to the user. -* Simplified HTTP challenge instructions in the manual plugin. - -### Fixed - -* Problems performing a dry run when using the Nginx plugin have been fixed. -* Resolved an issue where certbot-dns-digitalocean's test suite would sometimes - fail when ran using Python 3. -* On some systems, previous versions of certbot-auto would error out with a - message about a missing hash for setuptools. This has been fixed. -* A bug where Certbot would sometimes not print a space at the end of an - interactive prompt has been resolved. -* Nonfatal tracebacks are no longer shown in rare cases where Certbot - encounters an exception trying to close its TCP connection with the ACME - server. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.16.0+is%3Aclosed - -## 0.15.0 - 2017-06-08 - -### Added - -* Plugins for performing DNS challenges for popular providers. Like the Apache - and Nginx plugins, these plugins are packaged separately and not included in - Certbot by default. So far, we have plugins for - [Amazon Route 53](https://pypi.python.org/pypi/certbot-dns-route53), - [Cloudflare](https://pypi.python.org/pypi/certbot-dns-cloudflare), - [DigitalOcean](https://pypi.python.org/pypi/certbot-dns-digitalocean), and - [Google Cloud](https://pypi.python.org/pypi/certbot-dns-google) which all - work on Python 2.6, 2.7, and 3.3+. Additionally, we have plugins for - [CloudXNS](https://pypi.python.org/pypi/certbot-dns-cloudxns), - [DNSimple](https://pypi.python.org/pypi/certbot-dns-dnsimple), - [NS1](https://pypi.python.org/pypi/certbot-dns-nsone) which work on Python - 2.7 and 3.3+ (and not 2.6). Currently, there isn't a good way to install - these plugins when using `certbot-auto`, but that should change soon. -* IPv6 support in the standalone plugin. When performing a challenge, the - standalone plugin automatically handles listening for IPv4/IPv6 traffic based - on the configuration of your system. -* A mechanism for keeping your Apache and Nginx SSL/TLS configuration up to - date. When the Apache or Nginx plugins are used, they place SSL/TLS - configuration options in the root of Certbot's config directory - (`/etc/letsencrypt` by default). Now when a new version of these plugins run - on your system, they will automatically update the file to the newest - version if it is unmodified. If you manually modified the file, Certbot will - display a warning giving you a path to the updated file which you can use as - a reference to manually update your modified copy. -* `--http-01-address` and `--tls-sni-01-address` flags for controlling the - address Certbot listens on when using the standalone plugin. -* The command `certbot certificates` that lists certificates managed by Certbot - now performs additional validity checks to notify you if your files have - become corrupted. - -### Changed - -* Messages custom hooks print to `stdout` are now displayed by Certbot when not - running in `--quiet` mode. -* `jwk` and `alg` fields in JWS objects have been moved into the protected - header causing Certbot to more closely follow the latest version of the ACME - spec. - -### Fixed - -* Permissions on renewal configuration files are now properly preserved when - they are updated. -* A bug causing Certbot to display strange defaults in its help output when - using Python <= 2.7.4 has been fixed. -* Certbot now properly handles mixed case domain names found in custom CSRs. -* A number of poorly worded prompts and error messages. - -### Removed - -* Support for OpenSSL 1.0.0 in `certbot-auto` has been removed as we now pin a - newer version of `cryptography` which dropped support for this version. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.15.0+is%3Aclosed - -## 0.14.2 - 2017-05-25 - -### Fixed - -* Certbot 0.14.0 included a bug where Certbot would create a temporary log file -(usually in /tmp) if the program exited during argument parsing. If a user -provided -h/--help/help, --version, or an invalid command line argument, -Certbot would create this temporary log file. This was especially bothersome to -certbot-auto users as certbot-auto runs `certbot --version` internally to see -if the script needs to upgrade causing it to create at least one of these files -on every run. This problem has been resolved. - -More details about this change can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.14.2+is%3Aclosed - -## 0.14.1 - 2017-05-16 - -### Fixed - -* Certbot now works with configargparse 0.12.0. -* Issues with the Apache plugin and Augeas 1.7+ have been resolved. -* A problem where the Nginx plugin would fail to install certificates on -systems that had the plugin's SSL/TLS options file from 7+ months ago has been -fixed. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.14.1+is%3Aclosed - -## 0.14.0 - 2017-05-04 - -### Added - -* Python 3.3+ support for all Certbot packages. `certbot-auto` still currently -only supports Python 2, but the `acme`, `certbot`, `certbot-apache`, and -`certbot-nginx` packages on PyPI now fully support Python 2.6, 2.7, and 3.3+. -* Certbot's Apache plugin now handles multiple virtual hosts per file. -* Lockfiles to prevent multiple versions of Certbot running simultaneously. - -### Changed - -* When converting an HTTP virtual host to HTTPS in Apache, Certbot only copies -the virtual host rather than the entire contents of the file it's contained -in. -* The Nginx plugin now includes SSL/TLS directives in a separate file located -in Certbot's configuration directory rather than copying the contents of the -file into every modified `server` block. - -### Fixed - -* Ensure logging is configured before parts of Certbot attempt to log any -messages. -* Support for the `--quiet` flag in `certbot-auto`. -* Reverted a change made in a previous release to make the `acme` and `certbot` -packages always depend on `argparse`. This dependency is conditional again on -the user's Python version. -* Small bugs in the Nginx plugin such as properly handling empty `server` -blocks and setting `server_names_hash_bucket_size` during challenges. - -As always, a more complete list of changes can be found on GitHub: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.14.0+is%3Aclosed - -## 0.13.0 - 2017-04-06 - -### Added - -* `--debug-challenges` now pauses Certbot after setting up challenges for debugging. -* The Nginx parser can now handle all valid directives in configuration files. -* Nginx ciphersuites have changed to Mozilla Intermediate. -* `certbot-auto --no-bootstrap` provides the option to not install OS dependencies. - -### Fixed - -* `--register-unsafely-without-email` now respects `--quiet`. -* Hyphenated renewal parameters are now saved in renewal config files. -* `--dry-run` no longer persists keys and csrs. -* Certbot no longer hangs when trying to start Nginx in Arch Linux. -* Apache rewrite rules no longer double-encode characters. - -A full list of changes is available on GitHub: -https://github.com/certbot/certbot/issues?q=is%3Aissue%20milestone%3A0.13.0%20is%3Aclosed%20 - -## 0.12.0 - 2017-03-02 - -### Added - -* Certbot now allows non-camelcase Apache VirtualHost names. -* Certbot now allows more log messages to be silenced. - -### Fixed - -* Fixed a regression around using `--cert-name` when getting new certificates - -More information about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue%20milestone%3A0.12.0 - -## 0.11.1 - 2017-02-01 - -### Fixed - -* Resolved a problem where Certbot would crash while parsing command line -arguments in some cases. -* Fixed a typo. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/pulls?q=is%3Apr%20milestone%3A0.11.1%20is%3Aclosed - -## 0.11.0 - 2017-02-01 - -### Added - -* When using the standalone plugin while running Certbot interactively -and a required port is bound by another process, Certbot will give you -the option to retry to grab the port rather than immediately exiting. -* You are now able to deactivate your account with the Let's Encrypt -server using the `unregister` subcommand. -* When revoking a certificate using the `revoke` subcommand, you now -have the option to provide the reason the certificate is being revoked -to Let's Encrypt with `--reason`. - -### Changed - -* Providing `--quiet` to `certbot-auto` now silences package manager output. - -### Removed - -* Removed the optional `dnspython` dependency in our `acme` package. -Now the library does not support client side verification of the DNS -challenge. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.11.0+is%3Aclosed - -## 0.10.2 - 2017-01-25 - -### Added - -* If Certbot receives a request with a `badNonce` error, it now -automatically retries the request. Since nonces from Let's Encrypt expire, -this helps people performing the DNS challenge with the `manual` plugin -who may have to wait an extended period of time for their DNS changes to -propagate. - -### Fixed - -* Certbot now saves the `--preferred-challenges` values for renewal. Previously -these values were discarded causing a different challenge type to be used when -renewing certs in some cases. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.10.2+is%3Aclosed - -## 0.10.1 - 2017-01-13 - -### Fixed - -* Resolve problems where when asking Certbot to update a certificate at -an existing path to include different domain names, the old names would -continue to be used. -* Fix issues successfully running our unit test suite on some systems. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.10.1+is%3Aclosed - -## 0.10.0 - 2017-01-11 - -## Added - -* Added the ability to customize and automatically complete DNS and HTTP -domain validation challenges with the manual plugin. The flags -`--manual-auth-hook` and `--manual-cleanup-hook` can now be provided -when using the manual plugin to execute commands provided by the user to -perform and clean up challenges provided by the CA. This is best used in -complicated setups where the DNS challenge must be used or Certbot's -existing plugins cannot be used to perform HTTP challenges. For more -information on how this works, see `certbot --help manual`. -* Added a `--cert-name` flag for specifying the name to use for the -certificate in Certbot's configuration directory. Using this flag in -combination with `-d/--domains`, a user can easily request a new -certificate with different domains and save it with the name provided by -`--cert-name`. Additionally, `--cert-name` can be used to select a -certificate with the `certonly` and `run` subcommands so a full list of -domains in the certificate does not have to be provided. -* Added subcommand `certificates` for listing the certificates managed by -Certbot and their properties. -* Added the `delete` subcommand for removing certificates managed by Certbot -from the configuration directory. -* Certbot now supports requesting internationalized domain names (IDNs). -* Hooks provided to Certbot are now saved to be reused during renewal. -If you run Certbot with `--pre-hook`, `--renew-hook`, or `--post-hook` -flags when obtaining a certificate, the provided commands will -automatically be saved and executed again when renewing the certificate. -A pre-hook and/or post-hook can also be given to the `certbot renew` -command either on the command line or in a [configuration -file](https://certbot.eff.org/docs/using.html#configuration-file) to run -an additional command before/after any certificate is renewed. Hooks -will only be run if a certificate is renewed. -* Support Busybox in certbot-auto. - -### Changed - -* Recategorized `-h/--help` output to improve documentation and -discoverability. - -### Removed - -* Removed the ncurses interface. This change solves problems people -were having on many systems, reduces the number of Certbot -dependencies, and simplifies our code. Certbot's only interface now is -the text interface which was available by providing `-t/--text` to -earlier versions of Certbot. - -### Fixed - -* Many small bug fixes. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.10.0is%3Aclosed - -## 0.9.3 - 2016-10-13 - -### Added - -* The Apache plugin uses information about your OS to help determine the -layout of your Apache configuration directory. We added a patch to -ensure this code behaves the same way when testing on different systems -as the tests were failing in some cases. - -### Changed - -* Certbot adopted more conservative behavior about reporting a needed port as -unavailable when using the standalone plugin. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/27?closed=1 - -## 0.9.2 - 2016-10-12 - -### Added - -* Certbot stopped requiring that all possibly required ports are available when -using the standalone plugin. It now only verifies that the ports are available -when they are necessary. - -### Fixed - -* Certbot now verifies that our optional dependencies version matches what is -required by Certbot. -* Certnot now properly copies the `ssl on;` directives as necessary when -performing domain validation in the Nginx plugin. -* Fixed problem where symlinks were becoming files when they were -packaged, causing errors during testing and OS packaging. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/26?closed=1 - -## 0.9.1 - 2016-10-06 - -### Fixed - -* Fixed a bug that was introduced in version 0.9.0 where the command -line flag -q/--quiet wasn't respected in some cases. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/milestone/25?closed=1 - -## 0.9.0 - 2016-10-05 - -### Added - -* Added an alpha version of the Nginx plugin. This plugin fully automates the -process of obtaining and installing certificates with Nginx. -Additionally, it is able to automatically configure security -enhancements such as an HTTP to HTTPS redirect and OCSP stapling. To use -this plugin, you must have the `certbot-nginx` package installed (which -is installed automatically when using `certbot-auto`) and provide -`--nginx` on the command line. This plugin is still in its early stages -so we recommend you use it with some caution and make sure you have a -backup of your Nginx configuration. -* Added support for the `DNS` challenge in the `acme` library and `DNS` in -Certbot's `manual` plugin. This allows you to create DNS records to -prove to Let's Encrypt you control the requested domain name. To use -this feature, include `--manual --preferred-challenges dns` on the -command line. -* Certbot now helps with enabling Extra Packages for Enterprise Linux (EPEL) on -CentOS 6 when using `certbot-auto`. To use `certbot-auto` on CentOS 6, -the EPEL repository has to be enabled. `certbot-auto` will now prompt -users asking them if they would like the script to enable this for them -automatically. This is done without prompting users when using -`letsencrypt-auto` or if `-n/--non-interactive/--noninteractive` is -included on the command line. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.9.0+is%3Aclosed - -## 0.8.1 - 2016-06-14 - -### Added - -* Certbot now preserves a certificate's common name when using `renew`. -* Certbot now saves webroot values for renewal when they are entered interactively. -* Certbot now gracefully reports that the Apache plugin isn't usable when Augeas is not installed. -* Added experimental support for Mageia has been added to `certbot-auto`. - -### Fixed - -* Fixed problems with an invalid user-agent string on OS X. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.8.1+ - -## 0.8.0 - 2016-06-02 - -### Added - -* Added the `register` subcommand which can be used to register an account -with the Let's Encrypt CA. -* You can now run `certbot register --update-registration` to -change the e-mail address associated with your registration. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.8.0+ - -## 0.7.0 - 2016-05-27 - -### Added - -* Added `--must-staple` to request certificates from Let's Encrypt -with the OCSP must staple extension. -* Certbot now automatically configures OSCP stapling for Apache. -* Certbot now allows requesting certificates for domains found in the common name -of a custom CSR. - -### Fixed - -* Fixed a number of miscellaneous bugs - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=milestone%3A0.7.0+is%3Aissue - -## 0.6.0 - 2016-05-12 - -### Added - -* Versioned the datetime dependency in setup.py. - -### Changed - -* Renamed the client from `letsencrypt` to `certbot`. - -### Fixed - -* Fixed a small json deserialization error. -* Certbot now preserves domain order in generated CSRs. -* Fixed some minor bugs. - -More details about these changes can be found on our GitHub repo: -https://github.com/certbot/certbot/issues?q=is%3Aissue%20milestone%3A0.6.0%20is%3Aclosed%20 - -## 0.5.0 - 2016-04-05 - -### Added - -* Added the ability to use the webroot plugin interactively. -* Added the flags --pre-hook, --post-hook, and --renew-hook which can be used with -the renew subcommand to register shell commands to run in response to -renewal events. Pre-hook commands will be run before any certs are -renewed, post-hook commands will be run after any certs are renewed, -and renew-hook commands will be run after each cert is renewed. If no -certs are due for renewal, no command is run. -* Added a -q/--quiet flag which silences all output except errors. -* Added an --allow-subset-of-domains flag which can be used with the renew -command to prevent renewal failures for a subset of the requested -domains from causing the client to exit. - -### Changed - -* Certbot now uses renewal configuration files. In /etc/letsencrypt/renewal -by default, these files can be used to control what parameters are -used when renewing a specific certificate. - -More details about these changes can be found on our GitHub repo: -https://github.com/letsencrypt/letsencrypt/issues?q=milestone%3A0.5.0+is%3Aissue - -## 0.4.2 - 2016-03-03 - -### Fixed - -* Resolved problems encountered when compiling letsencrypt -against the new OpenSSL release. -* Fixed problems encountered when using `letsencrypt renew` with configuration files -from the private beta. - -More details about these changes can be found on our GitHub repo: -https://github.com/letsencrypt/letsencrypt/issues?q=is%3Aissue+milestone%3A0.4.2 - -## 0.4.1 - 2016-02-29 - -### Fixed - -* Fixed Apache parsing errors encountered with some configurations. -* Fixed Werkzeug dependency problems encountered on some Red Hat systems. -* Fixed bootstrapping failures when using letsencrypt-auto with --no-self-upgrade. -* Fixed problems with parsing renewal config files from private beta. - -More details about these changes can be found on our GitHub repo: -https://github.com/letsencrypt/letsencrypt/issues?q=is:issue+milestone:0.4.1 - -## 0.4.0 - 2016-02-10 - -### Added - -* Added the verb/subcommand `renew` which can be used to renew your existing -certificates as they approach expiration. Running `letsencrypt renew` -will examine all existing certificate lineages and determine if any are -less than 30 days from expiration. If so, the client will use the -settings provided when you previously obtained the certificate to renew -it. The subcommand finishes by printing a summary of which renewals were -successful, failed, or not yet due. -* Added a `--dry-run` flag to help with testing configuration -without affecting production rate limits. Currently supported by the -`renew` and `certonly` subcommands, providing `--dry-run` on the command -line will obtain certificates from the staging server without saving the -resulting certificates to disk. -* Added major improvements to letsencrypt-auto. This script -has been rewritten to include full support for Python 2.6, the ability -for letsencrypt-auto to update itself, and improvements to the -stability, security, and performance of the script. -* Added support for Apache 2.2 to the Apache plugin. - -More details about these changes can be found on our GitHub repo: -https://github.com/letsencrypt/letsencrypt/issues?q=is%3Aissue+milestone%3A0.4.0 - -## 0.3.0 - 2016-01-27 - -### Added - -* Added a non-interactive mode which can be enabled by including `-n` or -`--non-interactive` on the command line. This can be used to guarantee -the client will not prompt when run automatically using cron/systemd. -* Added preparation for the new letsencrypt-auto script. Over the past -couple months, we've been working on increasing the reliability and -security of letsencrypt-auto. A number of changes landed in this -release to prepare for the new version of this script. - -More details about these changes can be found on our GitHub repo: -https://github.com/letsencrypt/letsencrypt/issues?q=is%3Aissue+milestone%3A0.3.0 - -## 0.2.0 - 2016-01-14 - -### Added - -* Added Apache plugin support for non-Debian based systems. Support has been -added for modern Red Hat based systems such as Fedora 23, Red Hat 7, -and CentOS 7 running Apache 2.4. In theory, this plugin should be -able to be configured to run on any Unix-like OS running Apache 2.4. -* Relaxed PyOpenSSL version requirements. This adds support for systems -with PyOpenSSL versions 0.13 or 0.14. -* Improved error messages from the client. - -### Fixed - -* Resolved issues with the Apache plugin enabling an HTTP to HTTPS -redirect on some systems. - -More details about these changes can be found on our GitHub repo: -https://github.com/letsencrypt/letsencrypt/issues?q=is%3Aissue+milestone%3A0.2.0 - -## 0.1.1 - 2015-12-15 - -### Added - -* Added a check that avoids attempting to issue for unqualified domain names like -"localhost". - -### Fixed - -* Fixed a confusing UI path that caused some users to repeatedly renew -their certs while experimenting with the client, in some cases hitting -issuance rate limits. -* Fixed numerous Apache configuration parser problems -* Fixed --webroot permission handling for non-root users - -More details about these changes can be found on our GitHub repo: -https://github.com/letsencrypt/letsencrypt/issues?q=milestone%3A0.1.1 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 120000 index 00000000000..ba7396f24e4 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1 @@ +certbot/CHANGELOG.md \ No newline at end of file diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000000..142b31c933b --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1 @@ +This project is governed by [EFF's Public Projects Code of Conduct](https://www.eff.org/pages/eppcode). \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d740b7d896e..c4ad86f6b53 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,5 +1,8 @@ @@ -19,17 +22,15 @@ to the Sphinx generated docs is provided below. Hi! Welcome to the Certbot project. We look forward to collaborating with you. -If you're reporting a bug in Certbot, please make sure to include: - - The version of Certbot you're running. - - The operating system you're running it on. - - The commands you ran. - - What you expected to happen, and - - What actually happened. +If you're reporting a bug in Certbot. Please open an issue: https://github.com/certbot/certbot/issues/new/choose. + +If you're having trouble using Certbot and aren't sure you've found a bug, please first try asking for help at https://community.letsencrypt.org/. There is a much larger community there of people familiar with the project who will be able to more quickly answer your questions. If you're a developer, we have some helpful information in our [Developer's Guide](https://certbot.eff.org/docs/contributing.html) to get you -started. In particular, we recommend you read these sections +started. In particular, we recommend you read these sections: + - [EFF's Public Projects Code of Conduct](https://www.eff.org/pages/eppcode) - [Finding issues to work on](https://certbot.eff.org/docs/contributing.html#find-issues-to-work-on) - [Coding style](https://certbot.eff.org/docs/contributing.html#coding-style) - [Submitting a pull request](https://certbot.eff.org/docs/contributing.html#submitting-a-pull-request) diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index d1296b30f68..00000000000 --- a/Dockerfile +++ /dev/null @@ -1,27 +0,0 @@ -FROM python:2-alpine3.7 - -ENTRYPOINT [ "certbot" ] -EXPOSE 80 443 -VOLUME /etc/letsencrypt /var/lib/letsencrypt -WORKDIR /opt/certbot - -COPY CHANGELOG.md README.rst setup.py src/ -COPY acme src/acme -COPY certbot src/certbot - -RUN apk add --no-cache --virtual .certbot-deps \ - libffi \ - libssl1.0 \ - openssl \ - ca-certificates \ - binutils -RUN apk add --no-cache --virtual .build-deps \ - gcc \ - linux-headers \ - openssl-dev \ - musl-dev \ - libffi-dev \ - && pip install --no-cache-dir \ - --editable /opt/certbot/src/acme \ - --editable /opt/certbot/src \ - && apk del .build-deps diff --git a/Dockerfile-dev b/Dockerfile-dev deleted file mode 100644 index 1ab56e08108..00000000000 --- a/Dockerfile-dev +++ /dev/null @@ -1,21 +0,0 @@ -# This Dockerfile builds an image for development. -FROM ubuntu:xenial - -# Note: this only exposes the port to other docker containers. -EXPOSE 80 443 - -WORKDIR /opt/certbot/src - -# TODO: Install Apache/Nginx for plugin development. -COPY . . -RUN apt-get update && \ - apt-get install apache2 git nginx-light -y && \ - letsencrypt-auto-source/letsencrypt-auto --os-packages-only && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* \ - /tmp/* \ - /var/tmp/* - -RUN VENV_NAME="../venv" python tools/venv.py - -ENV PATH /opt/certbot/venv/bin:$PATH diff --git a/Dockerfile-old b/Dockerfile-old deleted file mode 100644 index da48c7fc8eb..00000000000 --- a/Dockerfile-old +++ /dev/null @@ -1,75 +0,0 @@ -# https://github.com/letsencrypt/letsencrypt/pull/431#issuecomment-103659297 -# it is more likely developers will already have ubuntu:trusty rather -# than e.g. debian:jessie and image size differences are negligible -FROM ubuntu:trusty -MAINTAINER Jakub Warmuz -MAINTAINER William Budington - -# Note: this only exposes the port to other docker containers. You -# still have to bind to 443@host at runtime, as per the ACME spec. -EXPOSE 443 - -# TODO: make sure --config-dir and --work-dir cannot be changed -# through the CLI (certbot-docker wrapper that uses standalone -# authenticator and text mode only?) -VOLUME /etc/letsencrypt /var/lib/letsencrypt - -WORKDIR /opt/certbot - -# no need to mkdir anything: -# https://docs.docker.com/reference/builder/#copy -# If doesn't exist, it is created along with all missing -# directories in its path. - -ENV DEBIAN_FRONTEND=noninteractive - -COPY letsencrypt-auto-source/letsencrypt-auto /opt/certbot/src/letsencrypt-auto-source/letsencrypt-auto -RUN /opt/certbot/src/letsencrypt-auto-source/letsencrypt-auto --os-packages-only && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* \ - /tmp/* \ - /var/tmp/* - -# the above is not likely to change, so by putting it further up the -# Dockerfile we make sure we cache as much as possible - - -COPY setup.py README.rst CHANGELOG.md MANIFEST.in letsencrypt-auto-source/pieces/pipstrap.py /opt/certbot/src/ - -# all above files are necessary for setup.py and venv setup, however, -# package source code directory has to be copied separately to a -# subdirectory... -# https://docs.docker.com/reference/builder/#copy: "If is a -# directory, the entire contents of the directory are copied, -# including filesystem metadata. Note: The directory itself is not -# copied, just its contents." Order again matters, three files are far -# more likely to be cached than the whole project directory - -COPY certbot /opt/certbot/src/certbot/ -COPY acme /opt/certbot/src/acme/ -COPY certbot-apache /opt/certbot/src/certbot-apache/ -COPY certbot-nginx /opt/certbot/src/certbot-nginx/ - - -RUN virtualenv --no-site-packages -p python2 /opt/certbot/venv - -# PATH is set now so pipstrap upgrades the correct (v)env -ENV PATH /opt/certbot/venv/bin:$PATH -RUN /opt/certbot/venv/bin/python /opt/certbot/src/pipstrap.py && \ - /opt/certbot/venv/bin/pip install \ - -e /opt/certbot/src/acme \ - -e /opt/certbot/src \ - -e /opt/certbot/src/certbot-apache \ - -e /opt/certbot/src/certbot-nginx - -# install in editable mode (-e) to save space: it's not possible to -# "rm -rf /opt/certbot/src" (it's stays in the underlaying image); -# this might also help in debugging: you can "docker run --entrypoint -# bash" and investigate, apply patches, etc. - -# set up certbot/letsencrypt wrapper to warn people about Dockerfile changes -COPY tools/docker-warning.sh /opt/certbot/bin/certbot -RUN ln -s /opt/certbot/bin/certbot /opt/certbot/bin/letsencrypt -ENV PATH /opt/certbot/bin:$PATH - -ENTRYPOINT [ "certbot" ] diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md deleted file mode 100644 index 03917f8ca61..00000000000 --- a/ISSUE_TEMPLATE.md +++ /dev/null @@ -1,22 +0,0 @@ -If you're having trouble using Certbot and aren't sure you've found a bug or -request for a new feature, please first try asking for help at -https://community.letsencrypt.org/. There is a much larger community there of -people familiar with the project who will be able to more quickly answer your -questions. - -## My operating system is (include version): - - -## I installed Certbot with (certbot-auto, OS package manager, pip, etc): - - -## I ran this command and it produced this output: - - -## Certbot's behavior differed from what I expected because: - - -## Here is a Certbot log showing the issue (if available): -###### Logs are stored in `/var/log/letsencrypt` by default. Feel free to redact domains, e-mail and IP addresses as you see fit. - -## Here is the relevant nginx server block or Apache virtualhost for the domain I am configuring: diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 7f529c7a786..00000000000 --- a/MANIFEST.in +++ /dev/null @@ -1,9 +0,0 @@ -include README.rst -include CHANGELOG.md -include CONTRIBUTING.md -include LICENSE.txt -include linter_plugin.py -recursive-include docs * -recursive-include examples * -recursive-include certbot/tests/testdata * -include certbot/ssl-dhparams.pem diff --git a/README.rst b/README.rst deleted file mode 100644 index f555812689f..00000000000 --- a/README.rst +++ /dev/null @@ -1,157 +0,0 @@ -.. This file contains a series of comments that are used to include sections of this README in other files. Do not modify these comments unless you know what you are doing. tag:intro-begin - -Certbot is part of EFF’s effort to encrypt the entire Internet. Secure communication over the Web relies on HTTPS, which requires the use of a digital certificate that lets browsers verify the identity of web servers (e.g., is that really google.com?). Web servers obtain their certificates from trusted third parties called certificate authorities (CAs). Certbot is an easy-to-use client that fetches a certificate from Let’s Encrypt—an open certificate authority launched by the EFF, Mozilla, and others—and deploys it to a web server. - -Anyone who has gone through the trouble of setting up a secure website knows what a hassle getting and maintaining a certificate is. Certbot and Let’s Encrypt can automate away the pain and let you turn on and manage HTTPS with simple commands. Using Certbot and Let's Encrypt is free, so there’s no need to arrange payment. - -How you use Certbot depends on the configuration of your web server. The best way to get started is to use our `interactive guide `_. It generates instructions based on your configuration settings. In most cases, you’ll need `root or administrator access `_ to your web server to run Certbot. - -Certbot is meant to be run directly on your web server, not on your personal computer. If you’re using a hosted service and don’t have direct access to your web server, you might not be able to use Certbot. Check with your hosting provider for documentation about uploading certificates or using certificates issued by Let’s Encrypt. - -Certbot is a fully-featured, extensible client for the Let's -Encrypt CA (or any other CA that speaks the `ACME -`_ -protocol) that can automate the tasks of obtaining certificates and -configuring webservers to use them. This client runs on Unix-based operating -systems. - -To see the changes made to Certbot between versions please refer to our -`changelog `_. - -Until May 2016, Certbot was named simply ``letsencrypt`` or ``letsencrypt-auto``, -depending on install method. Instructions on the Internet, and some pieces of the -software, may still refer to this older name. - -Contributing ------------- - -If you'd like to contribute to this project please read `Developer Guide -`_. - -.. _installation: - -Installation ------------- - -The easiest way to install Certbot is by visiting `certbot.eff.org`_, where you can -find the correct installation instructions for many web server and OS combinations. -For more information, see `Get Certbot `_. - -.. _certbot.eff.org: https://certbot.eff.org/ - -How to run the client ---------------------- - -In many cases, you can just run ``certbot-auto`` or ``certbot``, and the -client will guide you through the process of obtaining and installing certs -interactively. - -For full command line help, you can type:: - - ./certbot-auto --help all - - -You can also tell it exactly what you want it to do from the command line. -For instance, if you want to obtain a cert for ``example.com``, -``www.example.com``, and ``other.example.net``, using the Apache plugin to both -obtain and install the certs, you could do this:: - - ./certbot-auto --apache -d example.com -d www.example.com -d other.example.net - -(The first time you run the command, it will make an account, and ask for an -email and agreement to the Let's Encrypt Subscriber Agreement; you can -automate those with ``--email`` and ``--agree-tos``) - -If you want to use a webserver that doesn't have full plugin support yet, you -can still use "standalone" or "webroot" plugins to obtain a certificate:: - - ./certbot-auto certonly --standalone --email admin@example.com -d example.com -d www.example.com -d other.example.net - - -Understanding the client in more depth --------------------------------------- - -To understand what the client is doing in detail, it's important to -understand the way it uses plugins. Please see the `explanation of -plugins `_ in -the User Guide. - -Links -===== - -.. Do not modify this comment unless you know what you're doing. tag:links-begin - -Documentation: https://certbot.eff.org/docs - -Software project: https://github.com/certbot/certbot - -Notes for developers: https://certbot.eff.org/docs/contributing.html - -Main Website: https://certbot.eff.org - -Let's Encrypt Website: https://letsencrypt.org - -Community: https://community.letsencrypt.org - -ACME spec: http://ietf-wg-acme.github.io/acme/ - -ACME working area in github: https://github.com/ietf-wg-acme/acme - -|build-status| |coverage| |docs| |container| - -.. |build-status| image:: https://travis-ci.com/certbot/certbot.svg?branch=master - :target: https://travis-ci.com/certbot/certbot - :alt: Travis CI status - -.. |coverage| image:: https://codecov.io/gh/certbot/certbot/branch/master/graph/badge.svg - :target: https://codecov.io/gh/certbot/certbot - :alt: Coverage status - -.. |docs| image:: https://readthedocs.org/projects/letsencrypt/badge/ - :target: https://readthedocs.org/projects/letsencrypt/ - :alt: Documentation status - -.. |container| image:: https://quay.io/repository/letsencrypt/letsencrypt/status - :target: https://quay.io/repository/letsencrypt/letsencrypt - :alt: Docker Repository on Quay.io - -.. Do not modify this comment unless you know what you're doing. tag:links-end - -System Requirements -=================== - -See https://certbot.eff.org/docs/install.html#system-requirements. - -.. Do not modify this comment unless you know what you're doing. tag:intro-end - -.. Do not modify this comment unless you know what you're doing. tag:features-begin - -Current Features -===================== - -* Supports multiple web servers: - - - apache/2.x - - nginx/0.8.48+ - - webroot (adds files to webroot directories in order to prove control of - domains and obtain certs) - - standalone (runs its own simple webserver to prove you control a domain) - - other server software via `third party plugins `_ - -* The private key is generated locally on your system. -* Can talk to the Let's Encrypt CA or optionally to other ACME - compliant services. -* Can get domain-validated (DV) certificates. -* Can revoke certificates. -* Adjustable RSA key bit-length (2048 (default), 4096, ...). -* Can optionally install a http -> https redirect, so your site effectively - runs https only (Apache only) -* Fully automated. -* Configuration changes are logged and can be reverted. -* Supports an interactive text UI, or can be driven entirely from the - command line. -* Free and Open Source Software, made with Python. - -.. Do not modify this comment unless you know what you're doing. tag:features-end - -For extensive documentation on using and contributing to Certbot, go to https://certbot.eff.org/docs. If you would like to contribute to the project or run the latest code from git, you should read our `developer guide `_. diff --git a/README.rst b/README.rst new file mode 120000 index 00000000000..645fd4c78f6 --- /dev/null +++ b/README.rst @@ -0,0 +1 @@ +certbot/README.rst \ No newline at end of file diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 00000000000..c494ecb5a92 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,16 @@ +# Security Policy + +## Supported Versions + +Explanation on supported versions [here](https://github.com/certbot/certbot/wiki/Architectural-Decision-Records#-update-to-certbots-version-policy-and-end-of-life-support-on-previous-major-versions) + +| Major Version | Support Level | +| ------- | ------------------ | +| >= 5.0 | Full Support | +| 4.x | Discretionary Backports | +| <=3.x | None | + + +## Reporting a Vulnerability + +Security vulnerabilities can be reported using GitHub's [private vulnerability reporting tool](https://github.com/certbot/certbot/security/advisories/new). diff --git a/acme/.readthedocs.yaml b/acme/.readthedocs.yaml new file mode 100644 index 00000000000..1373ba8f389 --- /dev/null +++ b/acme/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: acme/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/acme/MANIFEST.in b/acme/MANIFEST.in index 1619bef698b..baa6b1f0253 100644 --- a/acme/MANIFEST.in +++ b/acme/MANIFEST.in @@ -1,6 +1,8 @@ include LICENSE.txt include README.rst -include pytest.ini recursive-include docs * recursive-include examples * -recursive-include acme/testdata * +recursive-include src/acme/_internal/tests/testdata * +include src/acme/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/acme/acme/__init__.py b/acme/acme/__init__.py deleted file mode 100644 index bab5b40ce74..00000000000 --- a/acme/acme/__init__.py +++ /dev/null @@ -1,27 +0,0 @@ -"""ACME protocol implementation. - -This module is an implementation of the `ACME protocol`_. Latest -supported version: `draft-ietf-acme-01`_. - - -.. _`ACME protocol`: https://ietf-wg-acme.github.io/acme - -.. _`draft-ietf-acme-01`: - https://github.com/ietf-wg-acme/acme/tree/draft-ietf-acme-acme-01 - -""" -import sys - -# This code exists to keep backwards compatibility with people using acme.jose -# before it became the standalone josepy package. -# -# It is based on -# https://github.com/requests/requests/blob/1278ecdf71a312dc2268f3bfc0aabfab3c006dcf/requests/packages.py - -import josepy as jose - -for mod in list(sys.modules): - # This traversal is apparently necessary such that the identities are - # preserved (acme.jose.* is josepy.*) - if mod == 'josepy' or mod.startswith('josepy.'): - sys.modules['acme.' + mod.replace('josepy', 'jose', 1)] = sys.modules[mod] diff --git a/acme/acme/challenges.py b/acme/acme/challenges.py deleted file mode 100644 index a6576822848..00000000000 --- a/acme/acme/challenges.py +++ /dev/null @@ -1,611 +0,0 @@ -"""ACME Identifier Validation Challenges.""" -import abc -import functools -import hashlib -import logging -import socket -import warnings - -from cryptography.hazmat.primitives import hashes # type: ignore -import josepy as jose -import OpenSSL -import requests -import six - -from acme import errors -from acme import crypto_util -from acme import fields - -logger = logging.getLogger(__name__) - - -# pylint: disable=too-few-public-methods - - -class Challenge(jose.TypedJSONObjectWithFields): - # _fields_to_partial_json | pylint: disable=abstract-method - """ACME challenge.""" - TYPES = {} # type: dict - - @classmethod - def from_json(cls, jobj): - try: - return super(Challenge, cls).from_json(jobj) - except jose.UnrecognizedTypeError as error: - logger.debug(error) - return UnrecognizedChallenge.from_json(jobj) - - -class ChallengeResponse(jose.TypedJSONObjectWithFields): - # _fields_to_partial_json | pylint: disable=abstract-method - """ACME challenge response.""" - TYPES = {} # type: dict - resource_type = 'challenge' - resource = fields.Resource(resource_type) - - -class UnrecognizedChallenge(Challenge): - """Unrecognized challenge. - - ACME specification defines a generic framework for challenges and - defines some standard challenges that are implemented in this - module. However, other implementations (including peers) might - define additional challenge types, which should be ignored if - unrecognized. - - :ivar jobj: Original JSON decoded object. - - """ - - def __init__(self, jobj): - super(UnrecognizedChallenge, self).__init__() - object.__setattr__(self, "jobj", jobj) - - def to_partial_json(self): - # pylint: disable=no-member - return self.jobj - - @classmethod - def from_json(cls, jobj): - return cls(jobj) - - -class _TokenChallenge(Challenge): - """Challenge with token. - - :ivar bytes token: - - """ - TOKEN_SIZE = 128 / 8 # Based on the entropy value from the spec - """Minimum size of the :attr:`token` in bytes.""" - - # TODO: acme-spec doesn't specify token as base64-encoded value - token = jose.Field( - "token", encoder=jose.encode_b64jose, decoder=functools.partial( - jose.decode_b64jose, size=TOKEN_SIZE, minimum=True)) - - # XXX: rename to ~token_good_for_url - @property - def good_token(self): # XXX: @token.decoder - """Is `token` good? - - .. todo:: acme-spec wants "It MUST NOT contain any non-ASCII - characters", but it should also warrant that it doesn't - contain ".." or "/"... - - """ - # TODO: check that path combined with uri does not go above - # URI_ROOT_PATH! - return b'..' not in self.token and b'/' not in self.token - - -class KeyAuthorizationChallengeResponse(ChallengeResponse): - """Response to Challenges based on Key Authorization. - - :param unicode key_authorization: - - """ - key_authorization = jose.Field("keyAuthorization") - thumbprint_hash_function = hashes.SHA256 - - def verify(self, chall, account_public_key): - """Verify the key authorization. - - :param KeyAuthorization chall: Challenge that corresponds to - this response. - :param JWK account_public_key: - - :return: ``True`` iff verification of the key authorization was - successful. - :rtype: bool - - """ - parts = self.key_authorization.split('.') # pylint: disable=no-member - if len(parts) != 2: - logger.debug("Key authorization (%r) is not well formed", - self.key_authorization) - return False - - if parts[0] != chall.encode("token"): - logger.debug("Mismatching token in key authorization: " - "%r instead of %r", parts[0], chall.encode("token")) - return False - - thumbprint = jose.b64encode(account_public_key.thumbprint( - hash_function=self.thumbprint_hash_function)).decode() - if parts[1] != thumbprint: - logger.debug("Mismatching thumbprint in key authorization: " - "%r instead of %r", parts[0], thumbprint) - return False - - return True - - -@six.add_metaclass(abc.ABCMeta) -class KeyAuthorizationChallenge(_TokenChallenge): - # pylint: disable=abstract-class-little-used,too-many-ancestors - """Challenge based on Key Authorization. - - :param response_cls: Subclass of `KeyAuthorizationChallengeResponse` - that will be used to generate `response`. - :param str typ: type of the challenge - """ - typ = NotImplemented - response_cls = NotImplemented - thumbprint_hash_function = ( - KeyAuthorizationChallengeResponse.thumbprint_hash_function) - - def key_authorization(self, account_key): - """Generate Key Authorization. - - :param JWK account_key: - :rtype unicode: - - """ - return self.encode("token") + "." + jose.b64encode( - account_key.thumbprint( - hash_function=self.thumbprint_hash_function)).decode() - - def response(self, account_key): - """Generate response to the challenge. - - :param JWK account_key: - - :returns: Response (initialized `response_cls`) to the challenge. - :rtype: KeyAuthorizationChallengeResponse - - """ - return self.response_cls( - key_authorization=self.key_authorization(account_key)) - - @abc.abstractmethod - def validation(self, account_key, **kwargs): - """Generate validation for the challenge. - - Subclasses must implement this method, but they are likely to - return completely different data structures, depending on what's - necessary to complete the challenge. Interpretation of that - return value must be known to the caller. - - :param JWK account_key: - :returns: Challenge-specific validation. - - """ - raise NotImplementedError() # pragma: no cover - - def response_and_validation(self, account_key, *args, **kwargs): - """Generate response and validation. - - Convenience function that return results of `response` and - `validation`. - - :param JWK account_key: - :rtype: tuple - - """ - return (self.response(account_key), - self.validation(account_key, *args, **kwargs)) - - -@ChallengeResponse.register -class DNS01Response(KeyAuthorizationChallengeResponse): - """ACME dns-01 challenge response.""" - typ = "dns-01" - - def simple_verify(self, chall, domain, account_public_key): - """Simple verify. - - This method no longer checks DNS records and is a simple wrapper - around `KeyAuthorizationChallengeResponse.verify`. - - :param challenges.DNS01 chall: Corresponding challenge. - :param unicode domain: Domain name being verified. - :param JWK account_public_key: Public key for the key pair - being authorized. - - :return: ``True`` iff verification of the key authorization was - successful. - :rtype: bool - - """ - # pylint: disable=unused-argument - verified = self.verify(chall, account_public_key) - if not verified: - logger.debug("Verification of key authorization in response failed") - return verified - - -@Challenge.register # pylint: disable=too-many-ancestors -class DNS01(KeyAuthorizationChallenge): - """ACME dns-01 challenge.""" - response_cls = DNS01Response - typ = response_cls.typ - - LABEL = "_acme-challenge" - """Label clients prepend to the domain name being validated.""" - - def validation(self, account_key, **unused_kwargs): - """Generate validation. - - :param JWK account_key: - :rtype: unicode - - """ - return jose.b64encode(hashlib.sha256(self.key_authorization( - account_key).encode("utf-8")).digest()).decode() - - def validation_domain_name(self, name): - """Domain name for TXT validation record. - - :param unicode name: Domain name being validated. - - """ - return "{0}.{1}".format(self.LABEL, name) - - -@ChallengeResponse.register -class HTTP01Response(KeyAuthorizationChallengeResponse): - """ACME http-01 challenge response.""" - typ = "http-01" - - PORT = 80 - """Verification port as defined by the protocol. - - You can override it (e.g. for testing) by passing ``port`` to - `simple_verify`. - - """ - - WHITESPACE_CUTSET = "\n\r\t " - """Whitespace characters which should be ignored at the end of the body.""" - - def simple_verify(self, chall, domain, account_public_key, port=None): - """Simple verify. - - :param challenges.SimpleHTTP chall: Corresponding challenge. - :param unicode domain: Domain name being verified. - :param JWK account_public_key: Public key for the key pair - being authorized. - :param int port: Port used in the validation. - - :returns: ``True`` iff validation with the files currently served by the - HTTP server is successful. - :rtype: bool - - """ - if not self.verify(chall, account_public_key): - logger.debug("Verification of key authorization in response failed") - return False - - # TODO: ACME specification defines URI template that doesn't - # allow to use a custom port... Make sure port is not in the - # request URI, if it's standard. - if port is not None and port != self.PORT: - logger.warning( - "Using non-standard port for http-01 verification: %s", port) - domain += ":{0}".format(port) - - uri = chall.uri(domain) - logger.debug("Verifying %s at %s...", chall.typ, uri) - try: - http_response = requests.get(uri) - except requests.exceptions.RequestException as error: - logger.error("Unable to reach %s: %s", uri, error) - return False - logger.debug("Received %s: %s. Headers: %s", http_response, - http_response.text, http_response.headers) - - challenge_response = http_response.text.rstrip(self.WHITESPACE_CUTSET) - if self.key_authorization != challenge_response: - logger.debug("Key authorization from response (%r) doesn't match " - "HTTP response (%r)", self.key_authorization, - challenge_response) - return False - - return True - - -@Challenge.register # pylint: disable=too-many-ancestors -class HTTP01(KeyAuthorizationChallenge): - """ACME http-01 challenge.""" - response_cls = HTTP01Response - typ = response_cls.typ - - URI_ROOT_PATH = ".well-known/acme-challenge" - """URI root path for the server provisioned resource.""" - - @property - def path(self): - """Path (starting with '/') for provisioned resource. - - :rtype: string - - """ - return '/' + self.URI_ROOT_PATH + '/' + self.encode('token') - - def uri(self, domain): - """Create an URI to the provisioned resource. - - Forms an URI to the HTTPS server provisioned resource - (containing :attr:`~SimpleHTTP.token`). - - :param unicode domain: Domain name being verified. - :rtype: string - - """ - return "http://" + domain + self.path - - def validation(self, account_key, **unused_kwargs): - """Generate validation. - - :param JWK account_key: - :rtype: unicode - - """ - return self.key_authorization(account_key) - - -@ChallengeResponse.register -class TLSSNI01Response(KeyAuthorizationChallengeResponse): - """ACME tls-sni-01 challenge response.""" - typ = "tls-sni-01" - - DOMAIN_SUFFIX = b".acme.invalid" - """Domain name suffix.""" - - PORT = 443 - """Verification port as defined by the protocol. - - You can override it (e.g. for testing) by passing ``port`` to - `simple_verify`. - - """ - - @property - def z(self): # pylint: disable=invalid-name - """``z`` value used for verification. - - :rtype bytes: - - """ - return hashlib.sha256( - self.key_authorization.encode("utf-8")).hexdigest().lower().encode() - - @property - def z_domain(self): - """Domain name used for verification, generated from `z`. - - :rtype bytes: - - """ - return self.z[:32] + b'.' + self.z[32:] + self.DOMAIN_SUFFIX - - def gen_cert(self, key=None, bits=2048): - """Generate tls-sni-01 certificate. - - :param OpenSSL.crypto.PKey key: Optional private key used in - certificate generation. If not provided (``None``), then - fresh key will be generated. - :param int bits: Number of bits for newly generated key. - - :rtype: `tuple` of `OpenSSL.crypto.X509` and `OpenSSL.crypto.PKey` - - """ - if key is None: - key = OpenSSL.crypto.PKey() - key.generate_key(OpenSSL.crypto.TYPE_RSA, bits) - return crypto_util.gen_ss_cert(key, [ - # z_domain is too big to fit into CN, hence first dummy domain - 'dummy', self.z_domain.decode()], force_san=True), key - - def probe_cert(self, domain, **kwargs): - """Probe tls-sni-01 challenge certificate. - - :param unicode domain: - - """ - # TODO: domain is not necessary if host is provided - if "host" not in kwargs: - host = socket.gethostbyname(domain) - logger.debug('%s resolved to %s', domain, host) - kwargs["host"] = host - - kwargs.setdefault("port", self.PORT) - kwargs["name"] = self.z_domain - # TODO: try different methods? - # pylint: disable=protected-access - return crypto_util.probe_sni(**kwargs) - - def verify_cert(self, cert): - """Verify tls-sni-01 challenge certificate. - - :param OpensSSL.crypto.X509 cert: Challenge certificate. - - :returns: Whether the certificate was successfully verified. - :rtype: bool - - """ - # pylint: disable=protected-access - sans = crypto_util._pyopenssl_cert_or_req_san(cert) - logger.debug('Certificate %s. SANs: %s', cert.digest('sha256'), sans) - return self.z_domain.decode() in sans - - def simple_verify(self, chall, domain, account_public_key, - cert=None, **kwargs): - """Simple verify. - - Verify ``validation`` using ``account_public_key``, optionally - probe tls-sni-01 certificate and check using `verify_cert`. - - :param .challenges.TLSSNI01 chall: Corresponding challenge. - :param str domain: Domain name being validated. - :param JWK account_public_key: - :param OpenSSL.crypto.X509 cert: Optional certificate. If not - provided (``None``) certificate will be retrieved using - `probe_cert`. - :param int port: Port used to probe the certificate. - - - :returns: ``True`` iff client's control of the domain has been - verified. - :rtype: bool - - """ - if not self.verify(chall, account_public_key): - logger.debug("Verification of key authorization in response failed") - return False - - if cert is None: - try: - cert = self.probe_cert(domain=domain, **kwargs) - except errors.Error as error: - logger.debug(str(error), exc_info=True) - return False - - return self.verify_cert(cert) - - -@Challenge.register # pylint: disable=too-many-ancestors -class TLSSNI01(KeyAuthorizationChallenge): - """ACME tls-sni-01 challenge.""" - response_cls = TLSSNI01Response - typ = response_cls.typ - - # boulder#962, ietf-wg-acme#22 - #n = jose.Field("n", encoder=int, decoder=int) - - def __init__(self, *args, **kwargs): - warnings.warn("TLS-SNI-01 is deprecated, and will stop working soon.", - DeprecationWarning, stacklevel=2) - super(TLSSNI01, self).__init__(*args, **kwargs) - - def validation(self, account_key, **kwargs): - """Generate validation. - - :param JWK account_key: - :param OpenSSL.crypto.PKey cert_key: Optional private key used - in certificate generation. If not provided (``None``), then - fresh key will be generated. - - :rtype: `tuple` of `OpenSSL.crypto.X509` and `OpenSSL.crypto.PKey` - - """ - return self.response(account_key).gen_cert(key=kwargs.get('cert_key')) - - -@Challenge.register # pylint: disable=too-many-ancestors -class TLSALPN01(KeyAuthorizationChallenge): - """ACME tls-alpn-01 challenge. - - This class simply allows parsing the TLS-ALPN-01 challenge returned from - the CA. Full TLS-ALPN-01 support is not currently provided. - - """ - typ = "tls-alpn-01" - - def validation(self, account_key, **kwargs): - """Generate validation for the challenge.""" - raise NotImplementedError() - - -@Challenge.register # pylint: disable=too-many-ancestors -class DNS(_TokenChallenge): - """ACME "dns" challenge.""" - typ = "dns" - - LABEL = "_acme-challenge" - """Label clients prepend to the domain name being validated.""" - - def gen_validation(self, account_key, alg=jose.RS256, **kwargs): - """Generate validation. - - :param .JWK account_key: Private account key. - :param .JWA alg: - - :returns: This challenge wrapped in `.JWS` - :rtype: .JWS - - """ - return jose.JWS.sign( - payload=self.json_dumps(sort_keys=True).encode('utf-8'), - key=account_key, alg=alg, **kwargs) - - def check_validation(self, validation, account_public_key): - """Check validation. - - :param JWS validation: - :param JWK account_public_key: - :rtype: bool - - """ - if not validation.verify(key=account_public_key): - return False - try: - return self == self.json_loads( - validation.payload.decode('utf-8')) - except jose.DeserializationError as error: - logger.debug("Checking validation for DNS failed: %s", error) - return False - - def gen_response(self, account_key, **kwargs): - """Generate response. - - :param .JWK account_key: Private account key. - :param .JWA alg: - - :rtype: DNSResponse - - """ - return DNSResponse(validation=self.gen_validation( - account_key, **kwargs)) - - def validation_domain_name(self, name): - """Domain name for TXT validation record. - - :param unicode name: Domain name being validated. - - """ - return "{0}.{1}".format(self.LABEL, name) - - -@ChallengeResponse.register -class DNSResponse(ChallengeResponse): - """ACME "dns" challenge response. - - :param JWS validation: - - """ - typ = "dns" - - validation = jose.Field("validation", decoder=jose.JWS.from_json) - - def check_validation(self, chall, account_public_key): - """Check validation. - - :param challenges.DNS chall: - :param JWK account_public_key: - - :rtype: bool - - """ - return chall.check_validation(self.validation, account_public_key) diff --git a/acme/acme/challenges_test.py b/acme/acme/challenges_test.py deleted file mode 100644 index 9307eb95be4..00000000000 --- a/acme/acme/challenges_test.py +++ /dev/null @@ -1,539 +0,0 @@ -"""Tests for acme.challenges.""" -import unittest -import warnings - -import josepy as jose -import mock -import OpenSSL -import requests - -from six.moves.urllib import parse as urllib_parse # pylint: disable=import-error - -from acme import errors -from acme import test_util - -CERT = test_util.load_comparable_cert('cert.pem') -KEY = jose.JWKRSA(key=test_util.load_rsa_private_key('rsa512_key.pem')) - - -class ChallengeTest(unittest.TestCase): - - def test_from_json_unrecognized(self): - from acme.challenges import Challenge - from acme.challenges import UnrecognizedChallenge - chall = UnrecognizedChallenge({"type": "foo"}) - # pylint: disable=no-member - self.assertEqual(chall, Challenge.from_json(chall.jobj)) - - -class UnrecognizedChallengeTest(unittest.TestCase): - - def setUp(self): - from acme.challenges import UnrecognizedChallenge - self.jobj = {"type": "foo"} - self.chall = UnrecognizedChallenge(self.jobj) - - def test_to_partial_json(self): - self.assertEqual(self.jobj, self.chall.to_partial_json()) - - def test_from_json(self): - from acme.challenges import UnrecognizedChallenge - self.assertEqual( - self.chall, UnrecognizedChallenge.from_json(self.jobj)) - - -class KeyAuthorizationChallengeResponseTest(unittest.TestCase): - - def setUp(self): - def _encode(name): - assert name == "token" - return "foo" - self.chall = mock.Mock() - self.chall.encode.side_effect = _encode - - def test_verify_ok(self): - from acme.challenges import KeyAuthorizationChallengeResponse - response = KeyAuthorizationChallengeResponse( - key_authorization='foo.oKGqedy-b-acd5eoybm2f-NVFxvyOoET5CNy3xnv8WY') - self.assertTrue(response.verify(self.chall, KEY.public_key())) - - def test_verify_wrong_token(self): - from acme.challenges import KeyAuthorizationChallengeResponse - response = KeyAuthorizationChallengeResponse( - key_authorization='bar.oKGqedy-b-acd5eoybm2f-NVFxvyOoET5CNy3xnv8WY') - self.assertFalse(response.verify(self.chall, KEY.public_key())) - - def test_verify_wrong_thumbprint(self): - from acme.challenges import KeyAuthorizationChallengeResponse - response = KeyAuthorizationChallengeResponse( - key_authorization='foo.oKGqedy-b-acd5eoybm2f-NVFxv') - self.assertFalse(response.verify(self.chall, KEY.public_key())) - - def test_verify_wrong_form(self): - from acme.challenges import KeyAuthorizationChallengeResponse - response = KeyAuthorizationChallengeResponse( - key_authorization='.foo.oKGqedy-b-acd5eoybm2f-' - 'NVFxvyOoET5CNy3xnv8WY') - self.assertFalse(response.verify(self.chall, KEY.public_key())) - - -class DNS01ResponseTest(unittest.TestCase): - # pylint: disable=too-many-instance-attributes - - def setUp(self): - from acme.challenges import DNS01Response - self.msg = DNS01Response(key_authorization=u'foo') - self.jmsg = { - 'resource': 'challenge', - 'type': 'dns-01', - 'keyAuthorization': u'foo', - } - - from acme.challenges import DNS01 - self.chall = DNS01(token=(b'x' * 16)) - self.response = self.chall.response(KEY) - - def test_to_partial_json(self): - self.assertEqual(self.jmsg, self.msg.to_partial_json()) - - def test_from_json(self): - from acme.challenges import DNS01Response - self.assertEqual(self.msg, DNS01Response.from_json(self.jmsg)) - - def test_from_json_hashable(self): - from acme.challenges import DNS01Response - hash(DNS01Response.from_json(self.jmsg)) - - def test_simple_verify_failure(self): - key2 = jose.JWKRSA.load(test_util.load_vector('rsa256_key.pem')) - public_key = key2.public_key() - verified = self.response.simple_verify(self.chall, "local", public_key) - self.assertFalse(verified) - - def test_simple_verify_success(self): - public_key = KEY.public_key() - verified = self.response.simple_verify(self.chall, "local", public_key) - self.assertTrue(verified) - - -class DNS01Test(unittest.TestCase): - - def setUp(self): - from acme.challenges import DNS01 - self.msg = DNS01(token=jose.decode_b64jose( - 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ+PCt92wr+oA')) - self.jmsg = { - 'type': 'dns-01', - 'token': 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA', - } - - def test_validation_domain_name(self): - self.assertEqual('_acme-challenge.www.example.com', - self.msg.validation_domain_name('www.example.com')) - - def test_validation(self): - self.assertEqual( - "rAa7iIg4K2y63fvUhCfy8dP1Xl7wEhmQq0oChTcE3Zk", - self.msg.validation(KEY)) - - def test_to_partial_json(self): - self.assertEqual(self.jmsg, self.msg.to_partial_json()) - - def test_from_json(self): - from acme.challenges import DNS01 - self.assertEqual(self.msg, DNS01.from_json(self.jmsg)) - - def test_from_json_hashable(self): - from acme.challenges import DNS01 - hash(DNS01.from_json(self.jmsg)) - - -class HTTP01ResponseTest(unittest.TestCase): - # pylint: disable=too-many-instance-attributes - - def setUp(self): - from acme.challenges import HTTP01Response - self.msg = HTTP01Response(key_authorization=u'foo') - self.jmsg = { - 'resource': 'challenge', - 'type': 'http-01', - 'keyAuthorization': u'foo', - } - - from acme.challenges import HTTP01 - self.chall = HTTP01(token=(b'x' * 16)) - self.response = self.chall.response(KEY) - - def test_to_partial_json(self): - self.assertEqual(self.jmsg, self.msg.to_partial_json()) - - def test_from_json(self): - from acme.challenges import HTTP01Response - self.assertEqual( - self.msg, HTTP01Response.from_json(self.jmsg)) - - def test_from_json_hashable(self): - from acme.challenges import HTTP01Response - hash(HTTP01Response.from_json(self.jmsg)) - - def test_simple_verify_bad_key_authorization(self): - key2 = jose.JWKRSA.load(test_util.load_vector('rsa256_key.pem')) - self.response.simple_verify(self.chall, "local", key2.public_key()) - - @mock.patch("acme.challenges.requests.get") - def test_simple_verify_good_validation(self, mock_get): - validation = self.chall.validation(KEY) - mock_get.return_value = mock.MagicMock(text=validation) - self.assertTrue(self.response.simple_verify( - self.chall, "local", KEY.public_key())) - mock_get.assert_called_once_with(self.chall.uri("local")) - - @mock.patch("acme.challenges.requests.get") - def test_simple_verify_bad_validation(self, mock_get): - mock_get.return_value = mock.MagicMock(text="!") - self.assertFalse(self.response.simple_verify( - self.chall, "local", KEY.public_key())) - - @mock.patch("acme.challenges.requests.get") - def test_simple_verify_whitespace_validation(self, mock_get): - from acme.challenges import HTTP01Response - mock_get.return_value = mock.MagicMock( - text=(self.chall.validation(KEY) + - HTTP01Response.WHITESPACE_CUTSET)) - self.assertTrue(self.response.simple_verify( - self.chall, "local", KEY.public_key())) - mock_get.assert_called_once_with(self.chall.uri("local")) - - @mock.patch("acme.challenges.requests.get") - def test_simple_verify_connection_error(self, mock_get): - mock_get.side_effect = requests.exceptions.RequestException - self.assertFalse(self.response.simple_verify( - self.chall, "local", KEY.public_key())) - - @mock.patch("acme.challenges.requests.get") - def test_simple_verify_port(self, mock_get): - self.response.simple_verify( - self.chall, domain="local", - account_public_key=KEY.public_key(), port=8080) - self.assertEqual("local:8080", urllib_parse.urlparse( - mock_get.mock_calls[0][1][0]).netloc) - - -class HTTP01Test(unittest.TestCase): - - def setUp(self): - from acme.challenges import HTTP01 - self.msg = HTTP01( - token=jose.decode_b64jose( - 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ+PCt92wr+oA')) - self.jmsg = { - 'type': 'http-01', - 'token': 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA', - } - - def test_path(self): - self.assertEqual(self.msg.path, '/.well-known/acme-challenge/' - 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA') - - def test_uri(self): - self.assertEqual( - 'http://example.com/.well-known/acme-challenge/' - 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA', - self.msg.uri('example.com')) - - def test_to_partial_json(self): - self.assertEqual(self.jmsg, self.msg.to_partial_json()) - - def test_from_json(self): - from acme.challenges import HTTP01 - self.assertEqual(self.msg, HTTP01.from_json(self.jmsg)) - - def test_from_json_hashable(self): - from acme.challenges import HTTP01 - hash(HTTP01.from_json(self.jmsg)) - - def test_good_token(self): - self.assertTrue(self.msg.good_token) - self.assertFalse( - self.msg.update(token=b'..').good_token) - - -class TLSSNI01ResponseTest(unittest.TestCase): - # pylint: disable=too-many-instance-attributes - - def setUp(self): - from acme.challenges import TLSSNI01 - self.chall = TLSSNI01( - token=jose.b64decode(b'a82d5ff8ef740d12881f6d3c2277ab2e')) - - self.response = self.chall.response(KEY) - self.jmsg = { - 'resource': 'challenge', - 'type': 'tls-sni-01', - 'keyAuthorization': self.response.key_authorization, - } - - # pylint: disable=invalid-name - label1 = b'dc38d9c3fa1a4fdcc3a5501f2d38583f' - label2 = b'b7793728f084394f2a1afd459556bb5c' - self.z = label1 + label2 - self.z_domain = label1 + b'.' + label2 + b'.acme.invalid' - self.domain = 'foo.com' - - def test_z_and_domain(self): - self.assertEqual(self.z, self.response.z) - self.assertEqual(self.z_domain, self.response.z_domain) - - def test_to_partial_json(self): - self.assertEqual(self.jmsg, self.response.to_partial_json()) - - def test_from_json(self): - from acme.challenges import TLSSNI01Response - self.assertEqual(self.response, TLSSNI01Response.from_json(self.jmsg)) - - def test_from_json_hashable(self): - from acme.challenges import TLSSNI01Response - hash(TLSSNI01Response.from_json(self.jmsg)) - - @mock.patch('acme.challenges.socket.gethostbyname') - @mock.patch('acme.challenges.crypto_util.probe_sni') - def test_probe_cert(self, mock_probe_sni, mock_gethostbyname): - mock_gethostbyname.return_value = '127.0.0.1' - self.response.probe_cert('foo.com') - mock_gethostbyname.assert_called_once_with('foo.com') - mock_probe_sni.assert_called_once_with( - host='127.0.0.1', port=self.response.PORT, - name=self.z_domain) - - self.response.probe_cert('foo.com', host='8.8.8.8') - mock_probe_sni.assert_called_with( - host='8.8.8.8', port=mock.ANY, name=mock.ANY) - - self.response.probe_cert('foo.com', port=1234) - mock_probe_sni.assert_called_with( - host=mock.ANY, port=1234, name=mock.ANY) - - self.response.probe_cert('foo.com', bar='baz') - mock_probe_sni.assert_called_with( - host=mock.ANY, port=mock.ANY, name=mock.ANY, bar='baz') - - self.response.probe_cert('foo.com', name=b'xxx') - mock_probe_sni.assert_called_with( - host=mock.ANY, port=mock.ANY, - name=self.z_domain) - - def test_gen_verify_cert(self): - key1 = test_util.load_pyopenssl_private_key('rsa512_key.pem') - cert, key2 = self.response.gen_cert(key1) - self.assertEqual(key1, key2) - self.assertTrue(self.response.verify_cert(cert)) - - def test_gen_verify_cert_gen_key(self): - cert, key = self.response.gen_cert() - self.assertTrue(isinstance(key, OpenSSL.crypto.PKey)) - self.assertTrue(self.response.verify_cert(cert)) - - def test_verify_bad_cert(self): - self.assertFalse(self.response.verify_cert( - test_util.load_cert('cert.pem'))) - - def test_simple_verify_bad_key_authorization(self): - key2 = jose.JWKRSA.load(test_util.load_vector('rsa256_key.pem')) - self.response.simple_verify(self.chall, "local", key2.public_key()) - - @mock.patch('acme.challenges.TLSSNI01Response.verify_cert', autospec=True) - def test_simple_verify(self, mock_verify_cert): - mock_verify_cert.return_value = mock.sentinel.verification - self.assertEqual( - mock.sentinel.verification, self.response.simple_verify( - self.chall, self.domain, KEY.public_key(), - cert=mock.sentinel.cert)) - mock_verify_cert.assert_called_once_with( - self.response, mock.sentinel.cert) - - @mock.patch('acme.challenges.TLSSNI01Response.probe_cert') - def test_simple_verify_false_on_probe_error(self, mock_probe_cert): - mock_probe_cert.side_effect = errors.Error - self.assertFalse(self.response.simple_verify( - self.chall, self.domain, KEY.public_key())) - - -class TLSSNI01Test(unittest.TestCase): - - def setUp(self): - self.jmsg = { - 'type': 'tls-sni-01', - 'token': 'a82d5ff8ef740d12881f6d3c2277ab2e', - } - - def _msg(self): - from acme.challenges import TLSSNI01 - with warnings.catch_warnings(record=True) as warn: - warnings.simplefilter("always") - msg = TLSSNI01( - token=jose.b64decode('a82d5ff8ef740d12881f6d3c2277ab2e')) - assert warn is not None # using a raw assert for mypy - self.assertTrue(len(warn) == 1) - self.assertTrue(issubclass(warn[-1].category, DeprecationWarning)) - self.assertTrue('deprecated' in str(warn[-1].message)) - return msg - - def test_to_partial_json(self): - self.assertEqual(self.jmsg, self._msg().to_partial_json()) - - def test_from_json(self): - from acme.challenges import TLSSNI01 - self.assertEqual(self._msg(), TLSSNI01.from_json(self.jmsg)) - - def test_from_json_hashable(self): - from acme.challenges import TLSSNI01 - hash(TLSSNI01.from_json(self.jmsg)) - - def test_from_json_invalid_token_length(self): - from acme.challenges import TLSSNI01 - self.jmsg['token'] = jose.encode_b64jose(b'abcd') - self.assertRaises( - jose.DeserializationError, TLSSNI01.from_json, self.jmsg) - - @mock.patch('acme.challenges.TLSSNI01Response.gen_cert') - def test_validation(self, mock_gen_cert): - mock_gen_cert.return_value = ('cert', 'key') - self.assertEqual(('cert', 'key'), self._msg().validation( - KEY, cert_key=mock.sentinel.cert_key)) - mock_gen_cert.assert_called_once_with(key=mock.sentinel.cert_key) - - -class TLSALPN01Test(unittest.TestCase): - - def setUp(self): - from acme.challenges import TLSALPN01 - self.msg = TLSALPN01( - token=jose.b64decode('a82d5ff8ef740d12881f6d3c2277ab2e')) - self.jmsg = { - 'type': 'tls-alpn-01', - 'token': 'a82d5ff8ef740d12881f6d3c2277ab2e', - } - - def test_to_partial_json(self): - self.assertEqual(self.jmsg, self.msg.to_partial_json()) - - def test_from_json(self): - from acme.challenges import TLSALPN01 - self.assertEqual(self.msg, TLSALPN01.from_json(self.jmsg)) - - def test_from_json_hashable(self): - from acme.challenges import TLSALPN01 - hash(TLSALPN01.from_json(self.jmsg)) - - def test_from_json_invalid_token_length(self): - from acme.challenges import TLSALPN01 - self.jmsg['token'] = jose.encode_b64jose(b'abcd') - self.assertRaises( - jose.DeserializationError, TLSALPN01.from_json, self.jmsg) - - def test_validation(self): - self.assertRaises(NotImplementedError, self.msg.validation, KEY) - - -class DNSTest(unittest.TestCase): - - def setUp(self): - from acme.challenges import DNS - self.msg = DNS(token=jose.b64decode( - b'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA')) - self.jmsg = { - 'type': 'dns', - 'token': 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA', - } - - def test_to_partial_json(self): - self.assertEqual(self.jmsg, self.msg.to_partial_json()) - - def test_from_json(self): - from acme.challenges import DNS - self.assertEqual(self.msg, DNS.from_json(self.jmsg)) - - def test_from_json_hashable(self): - from acme.challenges import DNS - hash(DNS.from_json(self.jmsg)) - - def test_gen_check_validation(self): - self.assertTrue(self.msg.check_validation( - self.msg.gen_validation(KEY), KEY.public_key())) - - def test_gen_check_validation_wrong_key(self): - key2 = jose.JWKRSA.load(test_util.load_vector('rsa1024_key.pem')) - self.assertFalse(self.msg.check_validation( - self.msg.gen_validation(KEY), key2.public_key())) - - def test_check_validation_wrong_payload(self): - validations = tuple( - jose.JWS.sign(payload=payload, alg=jose.RS256, key=KEY) - for payload in (b'', b'{}') - ) - for validation in validations: - self.assertFalse(self.msg.check_validation( - validation, KEY.public_key())) - - def test_check_validation_wrong_fields(self): - bad_validation = jose.JWS.sign( - payload=self.msg.update( - token=b'x' * 20).json_dumps().encode('utf-8'), - alg=jose.RS256, key=KEY) - self.assertFalse(self.msg.check_validation( - bad_validation, KEY.public_key())) - - def test_gen_response(self): - with mock.patch('acme.challenges.DNS.gen_validation') as mock_gen: - mock_gen.return_value = mock.sentinel.validation - response = self.msg.gen_response(KEY) - from acme.challenges import DNSResponse - self.assertTrue(isinstance(response, DNSResponse)) - self.assertEqual(response.validation, mock.sentinel.validation) - - def test_validation_domain_name(self): - self.assertEqual( - '_acme-challenge.le.wtf', self.msg.validation_domain_name('le.wtf')) - - -class DNSResponseTest(unittest.TestCase): - - def setUp(self): - from acme.challenges import DNS - self.chall = DNS(token=jose.b64decode( - b"evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA")) - self.validation = jose.JWS.sign( - payload=self.chall.json_dumps(sort_keys=True).encode(), - key=KEY, alg=jose.RS256) - - from acme.challenges import DNSResponse - self.msg = DNSResponse(validation=self.validation) - self.jmsg_to = { - 'resource': 'challenge', - 'type': 'dns', - 'validation': self.validation, - } - self.jmsg_from = { - 'resource': 'challenge', - 'type': 'dns', - 'validation': self.validation.to_json(), - } - - def test_to_partial_json(self): - self.assertEqual(self.jmsg_to, self.msg.to_partial_json()) - - def test_from_json(self): - from acme.challenges import DNSResponse - self.assertEqual(self.msg, DNSResponse.from_json(self.jmsg_from)) - - def test_from_json_hashable(self): - from acme.challenges import DNSResponse - hash(DNSResponse.from_json(self.jmsg_from)) - - def test_check_validation(self): - self.assertTrue( - self.msg.check_validation(self.chall, KEY.public_key())) - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/acme/acme/client.py b/acme/acme/client.py deleted file mode 100644 index 41338e17e49..00000000000 --- a/acme/acme/client.py +++ /dev/null @@ -1,1204 +0,0 @@ -"""ACME client API.""" -import base64 -import collections -import datetime -from email.utils import parsedate_tz -import heapq -import logging -import time - -import six -from six.moves import http_client # pylint: disable=import-error -import josepy as jose -import OpenSSL -import re -from requests_toolbelt.adapters.source import SourceAddressAdapter -import requests -from requests.adapters import HTTPAdapter -import sys - -from acme import crypto_util -from acme import errors -from acme import jws -from acme import messages -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import Dict, List, Set, Text - - -logger = logging.getLogger(__name__) - -# Prior to Python 2.7.9 the stdlib SSL module did not allow a user to configure -# many important security related options. On these platforms we use PyOpenSSL -# for SSL, which does allow these options to be configured. -# https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning -if sys.version_info < (2, 7, 9): # pragma: no cover - try: - # pylint: disable=no-member - requests.packages.urllib3.contrib.pyopenssl.inject_into_urllib3() # type: ignore - except AttributeError: - import urllib3.contrib.pyopenssl # pylint: disable=import-error - urllib3.contrib.pyopenssl.inject_into_urllib3() - -DEFAULT_NETWORK_TIMEOUT = 45 - -DER_CONTENT_TYPE = 'application/pkix-cert' - - -class ClientBase(object): # pylint: disable=too-many-instance-attributes - """ACME client base object. - - :ivar messages.Directory directory: - :ivar .ClientNetwork net: Client network. - :ivar int acme_version: ACME protocol version. 1 or 2. - """ - def __init__(self, directory, net, acme_version): - """Initialize. - - :param .messages.Directory directory: Directory Resource - :param .ClientNetwork net: Client network. - :param int acme_version: ACME protocol version. 1 or 2. - """ - self.directory = directory - self.net = net - self.acme_version = acme_version - - @classmethod - def _regr_from_response(cls, response, uri=None, terms_of_service=None): - if 'terms-of-service' in response.links: - terms_of_service = response.links['terms-of-service']['url'] - - return messages.RegistrationResource( - body=messages.Registration.from_json(response.json()), - uri=response.headers.get('Location', uri), - terms_of_service=terms_of_service) - - def _send_recv_regr(self, regr, body): - response = self._post(regr.uri, body) - - # TODO: Boulder returns httplib.ACCEPTED - #assert response.status_code == httplib.OK - - # TODO: Boulder does not set Location or Link on update - # (c.f. acme-spec #94) - - return self._regr_from_response( - response, uri=regr.uri, - terms_of_service=regr.terms_of_service) - - def _post(self, *args, **kwargs): - """Wrapper around self.net.post that adds the acme_version. - - """ - kwargs.setdefault('acme_version', self.acme_version) - if hasattr(self.directory, 'newNonce'): - kwargs.setdefault('new_nonce_url', getattr(self.directory, 'newNonce')) - return self.net.post(*args, **kwargs) - - def update_registration(self, regr, update=None): - """Update registration. - - :param messages.RegistrationResource regr: Registration Resource. - :param messages.Registration update: Updated body of the - resource. If not provided, body will be taken from `regr`. - - :returns: Updated Registration Resource. - :rtype: `.RegistrationResource` - - """ - update = regr.body if update is None else update - body = messages.UpdateRegistration(**dict(update)) - updated_regr = self._send_recv_regr(regr, body=body) - self.net.account = updated_regr - return updated_regr - - def deactivate_registration(self, regr): - """Deactivate registration. - - :param messages.RegistrationResource regr: The Registration Resource - to be deactivated. - - :returns: The Registration resource that was deactivated. - :rtype: `.RegistrationResource` - - """ - return self.update_registration(regr, update={'status': 'deactivated'}) - - def query_registration(self, regr): - """Query server about registration. - - :param messages.RegistrationResource: Existing Registration - Resource. - - """ - return self._send_recv_regr(regr, messages.UpdateRegistration()) - - def _authzr_from_response(self, response, identifier=None, uri=None): - authzr = messages.AuthorizationResource( - body=messages.Authorization.from_json(response.json()), - uri=response.headers.get('Location', uri)) - if identifier is not None and authzr.body.identifier != identifier: - raise errors.UnexpectedUpdate(authzr) - return authzr - - def answer_challenge(self, challb, response): - """Answer challenge. - - :param challb: Challenge Resource body. - :type challb: `.ChallengeBody` - - :param response: Corresponding Challenge response - :type response: `.challenges.ChallengeResponse` - - :returns: Challenge Resource with updated body. - :rtype: `.ChallengeResource` - - :raises .UnexpectedUpdate: - - """ - response = self._post(challb.uri, response) - try: - authzr_uri = response.links['up']['url'] - except KeyError: - raise errors.ClientError('"up" Link header missing') - challr = messages.ChallengeResource( - authzr_uri=authzr_uri, - body=messages.ChallengeBody.from_json(response.json())) - # TODO: check that challr.uri == response.headers['Location']? - if challr.uri != challb.uri: - raise errors.UnexpectedUpdate(challr.uri) - return challr - - @classmethod - def retry_after(cls, response, default): - """Compute next `poll` time based on response ``Retry-After`` header. - - Handles integers and various datestring formats per - https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.37 - - :param requests.Response response: Response from `poll`. - :param int default: Default value (in seconds), used when - ``Retry-After`` header is not present or invalid. - - :returns: Time point when next `poll` should be performed. - :rtype: `datetime.datetime` - - """ - retry_after = response.headers.get('Retry-After', str(default)) - try: - seconds = int(retry_after) - except ValueError: - # The RFC 2822 parser handles all of RFC 2616's cases in modern - # environments (primarily HTTP 1.1+ but also py27+) - when = parsedate_tz(retry_after) - if when is not None: - try: - tz_secs = datetime.timedelta(when[-1] if when[-1] else 0) - return datetime.datetime(*when[:7]) - tz_secs - except (ValueError, OverflowError): - pass - seconds = default - - return datetime.datetime.now() + datetime.timedelta(seconds=seconds) - - def _revoke(self, cert, rsn, url): - """Revoke certificate. - - :param .ComparableX509 cert: `OpenSSL.crypto.X509` wrapped in - `.ComparableX509` - - :param int rsn: Reason code for certificate revocation. - - :param str url: ACME URL to post to - - :raises .ClientError: If revocation is unsuccessful. - - """ - response = self._post(url, - messages.Revocation( - certificate=cert, - reason=rsn)) - if response.status_code != http_client.OK: - raise errors.ClientError( - 'Successful revocation must return HTTP OK status') - - -class Client(ClientBase): - """ACME client for a v1 API. - - .. todo:: - Clean up raised error types hierarchy, document, and handle (wrap) - instances of `.DeserializationError` raised in `from_json()`. - - :ivar messages.Directory directory: - :ivar key: `josepy.JWK` (private) - :ivar alg: `josepy.JWASignature` - :ivar bool verify_ssl: Verify SSL certificates? - :ivar .ClientNetwork net: Client network. Useful for testing. If not - supplied, it will be initialized using `key`, `alg` and - `verify_ssl`. - - """ - - def __init__(self, directory, key, alg=jose.RS256, verify_ssl=True, - net=None): - """Initialize. - - :param directory: Directory Resource (`.messages.Directory`) or - URI from which the resource will be downloaded. - - """ - # pylint: disable=too-many-arguments - self.key = key - if net is None: - net = ClientNetwork(key, alg=alg, verify_ssl=verify_ssl) - - if isinstance(directory, six.string_types): - directory = messages.Directory.from_json( - net.get(directory).json()) - super(Client, self).__init__(directory=directory, - net=net, acme_version=1) - - def register(self, new_reg=None): - """Register. - - :param .NewRegistration new_reg: - - :returns: Registration Resource. - :rtype: `.RegistrationResource` - - """ - new_reg = messages.NewRegistration() if new_reg is None else new_reg - response = self._post(self.directory[new_reg], new_reg) - # TODO: handle errors - assert response.status_code == http_client.CREATED - - # "Instance of 'Field' has no key/contact member" bug: - # pylint: disable=no-member - return self._regr_from_response(response) - - def agree_to_tos(self, regr): - """Agree to the terms-of-service. - - Agree to the terms-of-service in a Registration Resource. - - :param regr: Registration Resource. - :type regr: `.RegistrationResource` - - :returns: Updated Registration Resource. - :rtype: `.RegistrationResource` - - """ - return self.update_registration( - regr.update(body=regr.body.update(agreement=regr.terms_of_service))) - - def request_challenges(self, identifier, new_authzr_uri=None): - """Request challenges. - - :param .messages.Identifier identifier: Identifier to be challenged. - :param str new_authzr_uri: Deprecated. Do not use. - - :returns: Authorization Resource. - :rtype: `.AuthorizationResource` - - :raises errors.WildcardUnsupportedError: if a wildcard is requested - - """ - if new_authzr_uri is not None: - logger.debug("request_challenges with new_authzr_uri deprecated.") - - if identifier.value.startswith("*"): - raise errors.WildcardUnsupportedError( - "Requesting an authorization for a wildcard name is" - " forbidden by this version of the ACME protocol.") - - new_authz = messages.NewAuthorization(identifier=identifier) - response = self._post(self.directory.new_authz, new_authz) - # TODO: handle errors - assert response.status_code == http_client.CREATED - return self._authzr_from_response(response, identifier) - - def request_domain_challenges(self, domain, new_authzr_uri=None): - """Request challenges for domain names. - - This is simply a convenience function that wraps around - `request_challenges`, but works with domain names instead of - generic identifiers. See ``request_challenges`` for more - documentation. - - :param str domain: Domain name to be challenged. - :param str new_authzr_uri: Deprecated. Do not use. - - :returns: Authorization Resource. - :rtype: `.AuthorizationResource` - - :raises errors.WildcardUnsupportedError: if a wildcard is requested - - """ - return self.request_challenges(messages.Identifier( - typ=messages.IDENTIFIER_FQDN, value=domain), new_authzr_uri) - - def request_issuance(self, csr, authzrs): - """Request issuance. - - :param csr: CSR - :type csr: `OpenSSL.crypto.X509Req` wrapped in `.ComparableX509` - - :param authzrs: `list` of `.AuthorizationResource` - - :returns: Issued certificate - :rtype: `.messages.CertificateResource` - - """ - assert authzrs, "Authorizations list is empty" - logger.debug("Requesting issuance...") - - # TODO: assert len(authzrs) == number of SANs - req = messages.CertificateRequest(csr=csr) - - content_type = DER_CONTENT_TYPE # TODO: add 'cert_type 'argument - response = self._post( - self.directory.new_cert, - req, - content_type=content_type, - headers={'Accept': content_type}) - - cert_chain_uri = response.links.get('up', {}).get('url') - - try: - uri = response.headers['Location'] - except KeyError: - raise errors.ClientError('"Location" Header missing') - - return messages.CertificateResource( - uri=uri, authzrs=authzrs, cert_chain_uri=cert_chain_uri, - body=jose.ComparableX509(OpenSSL.crypto.load_certificate( - OpenSSL.crypto.FILETYPE_ASN1, response.content))) - - def poll(self, authzr): - """Poll Authorization Resource for status. - - :param authzr: Authorization Resource - :type authzr: `.AuthorizationResource` - - :returns: Updated Authorization Resource and HTTP response. - - :rtype: (`.AuthorizationResource`, `requests.Response`) - - """ - response = self.net.get(authzr.uri) - updated_authzr = self._authzr_from_response( - response, authzr.body.identifier, authzr.uri) - return updated_authzr, response - - def poll_and_request_issuance( - self, csr, authzrs, mintime=5, max_attempts=10): - """Poll and request issuance. - - This function polls all provided Authorization Resource URIs - until all challenges are valid, respecting ``Retry-After`` HTTP - headers, and then calls `request_issuance`. - - :param .ComparableX509 csr: CSR (`OpenSSL.crypto.X509Req` - wrapped in `.ComparableX509`) - :param authzrs: `list` of `.AuthorizationResource` - :param int mintime: Minimum time before next attempt, used if - ``Retry-After`` is not present in the response. - :param int max_attempts: Maximum number of attempts (per - authorization) before `PollError` with non-empty ``waiting`` - is raised. - - :returns: ``(cert, updated_authzrs)`` `tuple` where ``cert`` is - the issued certificate (`.messages.CertificateResource`), - and ``updated_authzrs`` is a `tuple` consisting of updated - Authorization Resources (`.AuthorizationResource`) as - present in the responses from server, and in the same order - as the input ``authzrs``. - :rtype: `tuple` - - :raises PollError: in case of timeout or if some authorization - was marked by the CA as invalid - - """ - # pylint: disable=too-many-locals - assert max_attempts > 0 - attempts = collections.defaultdict(int) # type: Dict[messages.AuthorizationResource, int] - exhausted = set() - - # priority queue with datetime.datetime (based on Retry-After) as key, - # and original Authorization Resource as value - waiting = [ - (datetime.datetime.now(), index, authzr) - for index, authzr in enumerate(authzrs) - ] - heapq.heapify(waiting) - # mapping between original Authorization Resource and the most - # recently updated one - updated = dict((authzr, authzr) for authzr in authzrs) - - while waiting: - # find the smallest Retry-After, and sleep if necessary - when, index, authzr = heapq.heappop(waiting) - now = datetime.datetime.now() - if when > now: - seconds = (when - now).seconds - logger.debug('Sleeping for %d seconds', seconds) - time.sleep(seconds) - - # Note that we poll with the latest updated Authorization - # URI, which might have a different URI than initial one - updated_authzr, response = self.poll(updated[authzr]) - updated[authzr] = updated_authzr - - attempts[authzr] += 1 - # pylint: disable=no-member - if updated_authzr.body.status not in ( - messages.STATUS_VALID, messages.STATUS_INVALID): - if attempts[authzr] < max_attempts: - # push back to the priority queue, with updated retry_after - heapq.heappush(waiting, (self.retry_after( - response, default=mintime), index, authzr)) - else: - exhausted.add(authzr) - - if exhausted or any(authzr.body.status == messages.STATUS_INVALID - for authzr in six.itervalues(updated)): - raise errors.PollError(exhausted, updated) - - updated_authzrs = tuple(updated[authzr] for authzr in authzrs) - return self.request_issuance(csr, updated_authzrs), updated_authzrs - - def _get_cert(self, uri): - """Returns certificate from URI. - - :param str uri: URI of certificate - - :returns: tuple of the form - (response, :class:`josepy.util.ComparableX509`) - :rtype: tuple - - """ - content_type = DER_CONTENT_TYPE # TODO: make it a param - response = self.net.get(uri, headers={'Accept': content_type}, - content_type=content_type) - return response, jose.ComparableX509(OpenSSL.crypto.load_certificate( - OpenSSL.crypto.FILETYPE_ASN1, response.content)) - - def check_cert(self, certr): - """Check for new cert. - - :param certr: Certificate Resource - :type certr: `.CertificateResource` - - :returns: Updated Certificate Resource. - :rtype: `.CertificateResource` - - """ - # TODO: acme-spec 5.1 table action should be renamed to - # "refresh cert", and this method integrated with self.refresh - response, cert = self._get_cert(certr.uri) - if 'Location' not in response.headers: - raise errors.ClientError('Location header missing') - if response.headers['Location'] != certr.uri: - raise errors.UnexpectedUpdate(response.text) - return certr.update(body=cert) - - def refresh(self, certr): - """Refresh certificate. - - :param certr: Certificate Resource - :type certr: `.CertificateResource` - - :returns: Updated Certificate Resource. - :rtype: `.CertificateResource` - - """ - # TODO: If a client sends a refresh request and the server is - # not willing to refresh the certificate, the server MUST - # respond with status code 403 (Forbidden) - return self.check_cert(certr) - - def fetch_chain(self, certr, max_length=10): - """Fetch chain for certificate. - - :param .CertificateResource certr: Certificate Resource - :param int max_length: Maximum allowed length of the chain. - Note that each element in the certificate requires new - ``HTTP GET`` request, and the length of the chain is - controlled by the ACME CA. - - :raises errors.Error: if recursion exceeds `max_length` - - :returns: Certificate chain for the Certificate Resource. It is - a list ordered so that the first element is a signer of the - certificate from Certificate Resource. Will be empty if - ``cert_chain_uri`` is ``None``. - :rtype: `list` of `OpenSSL.crypto.X509` wrapped in `.ComparableX509` - - """ - chain = [] # type: List[jose.ComparableX509] - uri = certr.cert_chain_uri - while uri is not None and len(chain) < max_length: - response, cert = self._get_cert(uri) - uri = response.links.get('up', {}).get('url') - chain.append(cert) - if uri is not None: - raise errors.Error( - "Recursion limit reached. Didn't get {0}".format(uri)) - return chain - - def revoke(self, cert, rsn): - """Revoke certificate. - - :param .ComparableX509 cert: `OpenSSL.crypto.X509` wrapped in - `.ComparableX509` - - :param int rsn: Reason code for certificate revocation. - - :raises .ClientError: If revocation is unsuccessful. - - """ - return self._revoke(cert, rsn, self.directory[messages.Revocation]) - - -class ClientV2(ClientBase): - """ACME client for a v2 API. - - :ivar messages.Directory directory: - :ivar .ClientNetwork net: Client network. - """ - - def __init__(self, directory, net): - """Initialize. - - :param .messages.Directory directory: Directory Resource - :param .ClientNetwork net: Client network. - """ - super(ClientV2, self).__init__(directory=directory, - net=net, acme_version=2) - - def new_account(self, new_account): - """Register. - - :param .NewRegistration new_account: - - :raises .ConflictError: in case the account already exists - - :returns: Registration Resource. - :rtype: `.RegistrationResource` - """ - response = self._post(self.directory['newAccount'], new_account) - # if account already exists - if response.status_code == 200 and 'Location' in response.headers: - raise errors.ConflictError(response.headers.get('Location')) - # "Instance of 'Field' has no key/contact member" bug: - # pylint: disable=no-member - regr = self._regr_from_response(response) - self.net.account = regr - return regr - - def query_registration(self, regr): - """Query server about registration. - - :param messages.RegistrationResource: Existing Registration - Resource. - - """ - self.net.account = regr - updated_regr = super(ClientV2, self).query_registration(regr) - self.net.account = updated_regr - return updated_regr - - def update_registration(self, regr, update=None): - """Update registration. - - :param messages.RegistrationResource regr: Registration Resource. - :param messages.Registration update: Updated body of the - resource. If not provided, body will be taken from `regr`. - - :returns: Updated Registration Resource. - :rtype: `.RegistrationResource` - - """ - # https://github.com/certbot/certbot/issues/6155 - new_regr = self._get_v2_account(regr) - return super(ClientV2, self).update_registration(new_regr, update) - - def _get_v2_account(self, regr): - self.net.account = None - only_existing_reg = regr.body.update(only_return_existing=True) - response = self._post(self.directory['newAccount'], only_existing_reg) - updated_uri = response.headers['Location'] - new_regr = regr.update(uri=updated_uri) - self.net.account = new_regr - return new_regr - - def new_order(self, csr_pem): - """Request a new Order object from the server. - - :param str csr_pem: A CSR in PEM format. - - :returns: The newly created order. - :rtype: OrderResource - """ - csr = OpenSSL.crypto.load_certificate_request(OpenSSL.crypto.FILETYPE_PEM, csr_pem) - # pylint: disable=protected-access - dnsNames = crypto_util._pyopenssl_cert_or_req_all_names(csr) - - identifiers = [] - for name in dnsNames: - identifiers.append(messages.Identifier(typ=messages.IDENTIFIER_FQDN, - value=name)) - order = messages.NewOrder(identifiers=identifiers) - response = self._post(self.directory['newOrder'], order) - body = messages.Order.from_json(response.json()) - authorizations = [] - for url in body.authorizations: - authorizations.append(self._authzr_from_response(self._post_as_get(url), uri=url)) - return messages.OrderResource( - body=body, - uri=response.headers.get('Location'), - authorizations=authorizations, - csr_pem=csr_pem) - - def poll(self, authzr): - """Poll Authorization Resource for status. - - :param authzr: Authorization Resource - :type authzr: `.AuthorizationResource` - - :returns: Updated Authorization Resource and HTTP response. - - :rtype: (`.AuthorizationResource`, `requests.Response`) - - """ - response = self._post_as_get(authzr.uri) - updated_authzr = self._authzr_from_response( - response, authzr.body.identifier, authzr.uri) - return updated_authzr, response - - def poll_and_finalize(self, orderr, deadline=None): - """Poll authorizations and finalize the order. - - If no deadline is provided, this method will timeout after 90 - seconds. - - :param messages.OrderResource orderr: order to finalize - :param datetime.datetime deadline: when to stop polling and timeout - - :returns: finalized order - :rtype: messages.OrderResource - - """ - if deadline is None: - deadline = datetime.datetime.now() + datetime.timedelta(seconds=90) - orderr = self.poll_authorizations(orderr, deadline) - return self.finalize_order(orderr, deadline) - - def poll_authorizations(self, orderr, deadline): - """Poll Order Resource for status.""" - responses = [] - for url in orderr.body.authorizations: - while datetime.datetime.now() < deadline: - authzr = self._authzr_from_response(self._post_as_get(url), uri=url) - if authzr.body.status != messages.STATUS_PENDING: - responses.append(authzr) - break - time.sleep(1) - # If we didn't get a response for every authorization, we fell through - # the bottom of the loop due to hitting the deadline. - if len(responses) < len(orderr.body.authorizations): - raise errors.TimeoutError() - failed = [] - for authzr in responses: - if authzr.body.status != messages.STATUS_VALID: - for chall in authzr.body.challenges: - if chall.error != None: - failed.append(authzr) - if len(failed) > 0: - raise errors.ValidationError(failed) - return orderr.update(authorizations=responses) - - def finalize_order(self, orderr, deadline): - """Finalize an order and obtain a certificate. - - :param messages.OrderResource orderr: order to finalize - :param datetime.datetime deadline: when to stop polling and timeout - - :returns: finalized order - :rtype: messages.OrderResource - - """ - csr = OpenSSL.crypto.load_certificate_request( - OpenSSL.crypto.FILETYPE_PEM, orderr.csr_pem) - wrapped_csr = messages.CertificateRequest(csr=jose.ComparableX509(csr)) - self._post(orderr.body.finalize, wrapped_csr) - while datetime.datetime.now() < deadline: - time.sleep(1) - response = self._post_as_get(orderr.uri) - body = messages.Order.from_json(response.json()) - if body.error is not None: - raise errors.IssuanceError(body.error) - if body.certificate is not None: - certificate_response = self._post_as_get(body.certificate, - content_type=DER_CONTENT_TYPE).text - return orderr.update(body=body, fullchain_pem=certificate_response) - raise errors.TimeoutError() - - def revoke(self, cert, rsn): - """Revoke certificate. - - :param .ComparableX509 cert: `OpenSSL.crypto.X509` wrapped in - `.ComparableX509` - - :param int rsn: Reason code for certificate revocation. - - :raises .ClientError: If revocation is unsuccessful. - - """ - return self._revoke(cert, rsn, self.directory['revokeCert']) - - def external_account_required(self): - """Checks if ACME server requires External Account Binding authentication.""" - if hasattr(self.directory, 'meta') and self.directory.meta.external_account_required: - return True - else: - return False - - def _post_as_get(self, *args, **kwargs): - """ - Send GET request using the POST-as-GET protocol if needed. - The request will be first issued using POST-as-GET for ACME v2. If the ACME CA servers do - not support this yet and return an error, request will be retried using GET. - For ACME v1, only GET request will be tried, as POST-as-GET is not supported. - :param args: - :param kwargs: - :return: - """ - if self.acme_version >= 2: - # We add an empty payload for POST-as-GET requests - new_args = args[:1] + (None,) + args[1:] - try: - return self._post(*new_args, **kwargs) # pylint: disable=star-args - except messages.Error as error: - if error.code == 'malformed': - logger.debug('Error during a POST-as-GET request, ' - 'your ACME CA may not support it:\n%s', error) - logger.debug('Retrying request with GET.') - else: # pragma: no cover - raise - - # If POST-as-GET is not supported yet, we use a GET instead. - return self.net.get(*args, **kwargs) - - -class BackwardsCompatibleClientV2(object): - """ACME client wrapper that tends towards V2-style calls, but - supports V1 servers. - - .. note:: While this class handles the majority of the differences - between versions of the ACME protocol, if you need to support an - ACME server based on version 3 or older of the IETF ACME draft - that uses combinations in authorizations (or lack thereof) to - signal that the client needs to complete something other than - any single challenge in the authorization to make it valid, the - user of this class needs to understand and handle these - differences themselves. This does not apply to either of Let's - Encrypt's endpoints where successfully completing any challenge - in an authorization will make it valid. - - :ivar int acme_version: 1 or 2, corresponding to the Let's Encrypt endpoint - :ivar .ClientBase client: either Client or ClientV2 - """ - - def __init__(self, net, key, server): - directory = messages.Directory.from_json(net.get(server).json()) - self.acme_version = self._acme_version_from_directory(directory) - if self.acme_version == 1: - self.client = Client(directory, key=key, net=net) - else: - self.client = ClientV2(directory, net=net) - - def __getattr__(self, name): - return getattr(self.client, name) - - def new_account_and_tos(self, regr, check_tos_cb=None): - """Combined register and agree_tos for V1, new_account for V2 - - :param .NewRegistration regr: - :param callable check_tos_cb: callback that raises an error if - the check does not work - """ - def _assess_tos(tos): - if check_tos_cb is not None: - check_tos_cb(tos) - if self.acme_version == 1: - regr = self.client.register(regr) - if regr.terms_of_service is not None: - _assess_tos(regr.terms_of_service) - return self.client.agree_to_tos(regr) - return regr - else: - if "terms_of_service" in self.client.directory.meta: - _assess_tos(self.client.directory.meta.terms_of_service) - regr = regr.update(terms_of_service_agreed=True) - return self.client.new_account(regr) - - def new_order(self, csr_pem): - """Request a new Order object from the server. - - If using ACMEv1, returns a dummy OrderResource with only - the authorizations field filled in. - - :param str csr_pem: A CSR in PEM format. - - :returns: The newly created order. - :rtype: OrderResource - - :raises errors.WildcardUnsupportedError: if a wildcard domain is - requested but unsupported by the ACME version - - """ - if self.acme_version == 1: - csr = OpenSSL.crypto.load_certificate_request(OpenSSL.crypto.FILETYPE_PEM, csr_pem) - # pylint: disable=protected-access - dnsNames = crypto_util._pyopenssl_cert_or_req_all_names(csr) - authorizations = [] - for domain in dnsNames: - authorizations.append(self.client.request_domain_challenges(domain)) - return messages.OrderResource(authorizations=authorizations, csr_pem=csr_pem) - else: - return self.client.new_order(csr_pem) - - def finalize_order(self, orderr, deadline): - """Finalize an order and obtain a certificate. - - :param messages.OrderResource orderr: order to finalize - :param datetime.datetime deadline: when to stop polling and timeout - - :returns: finalized order - :rtype: messages.OrderResource - - """ - if self.acme_version == 1: - csr_pem = orderr.csr_pem - certr = self.client.request_issuance( - jose.ComparableX509( - OpenSSL.crypto.load_certificate_request(OpenSSL.crypto.FILETYPE_PEM, csr_pem)), - orderr.authorizations) - - chain = None - while datetime.datetime.now() < deadline: - try: - chain = self.client.fetch_chain(certr) - break - except errors.Error: - time.sleep(1) - - if chain is None: - raise errors.TimeoutError( - 'Failed to fetch chain. You should not deploy the generated ' - 'certificate, please rerun the command for a new one.') - - cert = OpenSSL.crypto.dump_certificate( - OpenSSL.crypto.FILETYPE_PEM, certr.body.wrapped).decode() - chain = crypto_util.dump_pyopenssl_chain(chain).decode() - - return orderr.update(fullchain_pem=(cert + chain)) - else: - return self.client.finalize_order(orderr, deadline) - - def revoke(self, cert, rsn): - """Revoke certificate. - - :param .ComparableX509 cert: `OpenSSL.crypto.X509` wrapped in - `.ComparableX509` - - :param int rsn: Reason code for certificate revocation. - - :raises .ClientError: If revocation is unsuccessful. - - """ - return self.client.revoke(cert, rsn) - - def _acme_version_from_directory(self, directory): - if hasattr(directory, 'newNonce'): - return 2 - else: - return 1 - - def external_account_required(self): - """Checks if the server requires an external account for ACMEv2 servers. - - Always return False for ACMEv1 servers, as it doesn't use External Account Binding.""" - if self.acme_version == 1: - return False - else: - return self.client.external_account_required() - - -class ClientNetwork(object): # pylint: disable=too-many-instance-attributes - """Wrapper around requests that signs POSTs for authentication. - - Also adds user agent, and handles Content-Type. - """ - JSON_CONTENT_TYPE = 'application/json' - JOSE_CONTENT_TYPE = 'application/jose+json' - JSON_ERROR_CONTENT_TYPE = 'application/problem+json' - REPLAY_NONCE_HEADER = 'Replay-Nonce' - - """Initialize. - - :param josepy.JWK key: Account private key - :param messages.RegistrationResource account: Account object. Required if you are - planning to use .post() with acme_version=2 for anything other than - creating a new account; may be set later after registering. - :param josepy.JWASignature alg: Algoritm to use in signing JWS. - :param bool verify_ssl: Whether to verify certificates on SSL connections. - :param str user_agent: String to send as User-Agent header. - :param float timeout: Timeout for requests. - :param source_address: Optional source address to bind to when making requests. - :type source_address: str or tuple(str, int) - """ - def __init__(self, key, account=None, alg=jose.RS256, verify_ssl=True, - user_agent='acme-python', timeout=DEFAULT_NETWORK_TIMEOUT, - source_address=None): - # pylint: disable=too-many-arguments - self.key = key - self.account = account - self.alg = alg - self.verify_ssl = verify_ssl - self._nonces = set() # type: Set[Text] - self.user_agent = user_agent - self.session = requests.Session() - self._default_timeout = timeout - adapter = HTTPAdapter() - - if source_address is not None: - adapter = SourceAddressAdapter(source_address) - - self.session.mount("http://", adapter) - self.session.mount("https://", adapter) - - def __del__(self): - # Try to close the session, but don't show exceptions to the - # user if the call to close() fails. See #4840. - try: - self.session.close() - except Exception: # pylint: disable=broad-except - pass - - def _wrap_in_jws(self, obj, nonce, url, acme_version): - """Wrap `JSONDeSerializable` object in JWS. - - .. todo:: Implement ``acmePath``. - - :param josepy.JSONDeSerializable obj: - :param str url: The URL to which this object will be POSTed - :param bytes nonce: - :rtype: `josepy.JWS` - - """ - jobj = obj.json_dumps(indent=2).encode() if obj else b'' - logger.debug('JWS payload:\n%s', jobj) - kwargs = { - "alg": self.alg, - "nonce": nonce - } - if acme_version == 2: - kwargs["url"] = url - # newAccount and revokeCert work without the kid - # newAccount must not have kid - if self.account is not None: - kwargs["kid"] = self.account["uri"] - kwargs["key"] = self.key - # pylint: disable=star-args - return jws.JWS.sign(jobj, **kwargs).json_dumps(indent=2) - - @classmethod - def _check_response(cls, response, content_type=None): - """Check response content and its type. - - .. note:: - Checking is not strict: wrong server response ``Content-Type`` - HTTP header is ignored if response is an expected JSON object - (c.f. Boulder #56). - - :param str content_type: Expected Content-Type response header. - If JSON is expected and not present in server response, this - function will raise an error. Otherwise, wrong Content-Type - is ignored, but logged. - - :raises .messages.Error: If server response body - carries HTTP Problem (draft-ietf-appsawg-http-problem-00). - :raises .ClientError: In case of other networking errors. - - """ - response_ct = response.headers.get('Content-Type') - try: - # TODO: response.json() is called twice, once here, and - # once in _get and _post clients - jobj = response.json() - except ValueError: - jobj = None - - if response.status_code == 409: - raise errors.ConflictError(response.headers.get('Location')) - - if not response.ok: - if jobj is not None: - if response_ct != cls.JSON_ERROR_CONTENT_TYPE: - logger.debug( - 'Ignoring wrong Content-Type (%r) for JSON Error', - response_ct) - try: - raise messages.Error.from_json(jobj) - except jose.DeserializationError as error: - # Couldn't deserialize JSON object - raise errors.ClientError((response, error)) - else: - # response is not JSON object - raise errors.ClientError(response) - else: - if jobj is not None and response_ct != cls.JSON_CONTENT_TYPE: - logger.debug( - 'Ignoring wrong Content-Type (%r) for JSON decodable ' - 'response', response_ct) - - if content_type == cls.JSON_CONTENT_TYPE and jobj is None: - raise errors.ClientError( - 'Unexpected response Content-Type: {0}'.format(response_ct)) - - return response - - def _send_request(self, method, url, *args, **kwargs): - # pylint: disable=too-many-locals - """Send HTTP request. - - Makes sure that `verify_ssl` is respected. Logs request and - response (with headers). For allowed parameters please see - `requests.request`. - - :param str method: method for the new `requests.Request` object - :param str url: URL for the new `requests.Request` object - - :raises requests.exceptions.RequestException: in case of any problems - - :returns: HTTP Response - :rtype: `requests.Response` - - - """ - if method == "POST": - logger.debug('Sending POST request to %s:\n%s', - url, kwargs['data']) - else: - logger.debug('Sending %s request to %s.', method, url) - kwargs['verify'] = self.verify_ssl - kwargs.setdefault('headers', {}) - kwargs['headers'].setdefault('User-Agent', self.user_agent) - kwargs.setdefault('timeout', self._default_timeout) - try: - response = self.session.request(method, url, *args, **kwargs) - except requests.exceptions.RequestException as e: - # pylint: disable=pointless-string-statement - """Requests response parsing - - The requests library emits exceptions with a lot of extra text. - We parse them with a regexp to raise a more readable exceptions. - - Example: - HTTPSConnectionPool(host='acme-v01.api.letsencrypt.org', - port=443): Max retries exceeded with url: /directory - (Caused by NewConnectionError(' - : Failed to establish a new connection: - [Errno 65] No route to host',))""" - - # pylint: disable=line-too-long - err_regex = r".*host='(\S*)'.*Max retries exceeded with url\: (\/\w*).*(\[Errno \d+\])([A-Za-z ]*)" - m = re.match(err_regex, str(e)) - if m is None: - raise # pragma: no cover - else: - host, path, _err_no, err_msg = m.groups() - raise ValueError("Requesting {0}{1}:{2}".format(host, path, err_msg)) - - # If content is DER, log the base64 of it instead of raw bytes, to keep - # binary data out of the logs. - if response.headers.get("Content-Type") == DER_CONTENT_TYPE: - debug_content = base64.b64encode(response.content) - else: - debug_content = response.content.decode("utf-8") - logger.debug('Received response:\nHTTP %d\n%s\n\n%s', - response.status_code, - "\n".join(["{0}: {1}".format(k, v) - for k, v in response.headers.items()]), - debug_content) - return response - - def head(self, *args, **kwargs): - """Send HEAD request without checking the response. - - Note, that `_check_response` is not called, as it is expected - that status code other than successfully 2xx will be returned, or - messages2.Error will be raised by the server. - - """ - return self._send_request('HEAD', *args, **kwargs) - - def get(self, url, content_type=JSON_CONTENT_TYPE, **kwargs): - """Send GET request and check response.""" - return self._check_response( - self._send_request('GET', url, **kwargs), content_type=content_type) - - def _add_nonce(self, response): - if self.REPLAY_NONCE_HEADER in response.headers: - nonce = response.headers[self.REPLAY_NONCE_HEADER] - try: - decoded_nonce = jws.Header._fields['nonce'].decode(nonce) - except jose.DeserializationError as error: - raise errors.BadNonce(nonce, error) - logger.debug('Storing nonce: %s', nonce) - self._nonces.add(decoded_nonce) - else: - raise errors.MissingNonce(response) - - def _get_nonce(self, url, new_nonce_url): - if not self._nonces: - logger.debug('Requesting fresh nonce') - if new_nonce_url is None: - response = self.head(url) - else: - # request a new nonce from the acme newNonce endpoint - response = self._check_response(self.head(new_nonce_url), content_type=None) - self._add_nonce(response) - return self._nonces.pop() - - def post(self, *args, **kwargs): - """POST object wrapped in `.JWS` and check response. - - If the server responded with a badNonce error, the request will - be retried once. - - """ - try: - return self._post_once(*args, **kwargs) - except messages.Error as error: - if error.code == 'badNonce': - logger.debug('Retrying request after error:\n%s', error) - return self._post_once(*args, **kwargs) - else: - raise - - def _post_once(self, url, obj, content_type=JOSE_CONTENT_TYPE, - acme_version=1, **kwargs): - try: - new_nonce_url = kwargs.pop('new_nonce_url') - except KeyError: - new_nonce_url = None - data = self._wrap_in_jws(obj, self._get_nonce(url, new_nonce_url), url, acme_version) - kwargs.setdefault('headers', {'Content-Type': content_type}) - response = self._send_request('POST', url, data=data, **kwargs) - response = self._check_response(response, content_type=content_type) - self._add_nonce(response) - return response diff --git a/acme/acme/client_test.py b/acme/acme/client_test.py deleted file mode 100644 index b3d0f192128..00000000000 --- a/acme/acme/client_test.py +++ /dev/null @@ -1,1306 +0,0 @@ -"""Tests for acme.client.""" -# pylint: disable=too-many-lines -import copy -import datetime -import json -import unittest - -from six.moves import http_client # pylint: disable=import-error - -import josepy as jose -import mock -import OpenSSL -import requests - -from acme import challenges -from acme import errors -from acme import jws as acme_jws -from acme import messages -from acme import messages_test -from acme import test_util -from acme.magic_typing import Dict # pylint: disable=unused-import, no-name-in-module - - -CERT_DER = test_util.load_vector('cert.der') -CERT_SAN_PEM = test_util.load_vector('cert-san.pem') -CSR_SAN_PEM = test_util.load_vector('csr-san.pem') -KEY = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem')) -KEY2 = jose.JWKRSA.load(test_util.load_vector('rsa256_key.pem')) - -DIRECTORY_V1 = messages.Directory({ - messages.NewRegistration: - 'https://www.letsencrypt-demo.org/acme/new-reg', - messages.Revocation: - 'https://www.letsencrypt-demo.org/acme/revoke-cert', - messages.NewAuthorization: - 'https://www.letsencrypt-demo.org/acme/new-authz', - messages.CertificateRequest: - 'https://www.letsencrypt-demo.org/acme/new-cert', -}) - -DIRECTORY_V2 = messages.Directory({ - 'newAccount': 'https://www.letsencrypt-demo.org/acme/new-account', - 'newNonce': 'https://www.letsencrypt-demo.org/acme/new-nonce', - 'newOrder': 'https://www.letsencrypt-demo.org/acme/new-order', - 'revokeCert': 'https://www.letsencrypt-demo.org/acme/revoke-cert', -}) - - -class ClientTestBase(unittest.TestCase): - """Base for tests in acme.client.""" - - def setUp(self): - self.response = mock.MagicMock( - ok=True, status_code=http_client.OK, headers={}, links={}) - self.net = mock.MagicMock() - self.net.post.return_value = self.response - self.net.get.return_value = self.response - - self.identifier = messages.Identifier( - typ=messages.IDENTIFIER_FQDN, value='example.com') - - # Registration - self.contact = ('mailto:cert-admin@example.com', 'tel:+12025551212') - reg = messages.Registration( - contact=self.contact, key=KEY.public_key()) - the_arg = dict(reg) # type: Dict - self.new_reg = messages.NewRegistration(**the_arg) # pylint: disable=star-args - self.regr = messages.RegistrationResource( - body=reg, uri='https://www.letsencrypt-demo.org/acme/reg/1') - - # Authorization - authzr_uri = 'https://www.letsencrypt-demo.org/acme/authz/1' - challb = messages.ChallengeBody( - uri=(authzr_uri + '/1'), status=messages.STATUS_VALID, - chall=challenges.DNS(token=jose.b64decode( - 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA'))) - self.challr = messages.ChallengeResource( - body=challb, authzr_uri=authzr_uri) - self.authz = messages.Authorization( - identifier=messages.Identifier( - typ=messages.IDENTIFIER_FQDN, value='example.com'), - challenges=(challb,), combinations=None) - self.authzr = messages.AuthorizationResource( - body=self.authz, uri=authzr_uri) - - # Reason code for revocation - self.rsn = 1 - - -class BackwardsCompatibleClientV2Test(ClientTestBase): - """Tests for acme.client.BackwardsCompatibleClientV2.""" - - def setUp(self): - super(BackwardsCompatibleClientV2Test, self).setUp() - # contains a loaded cert - self.certr = messages.CertificateResource( - body=messages_test.CERT) - - loaded = OpenSSL.crypto.load_certificate( - OpenSSL.crypto.FILETYPE_PEM, CERT_SAN_PEM) - wrapped = jose.ComparableX509(loaded) - self.chain = [wrapped, wrapped] - - self.cert_pem = OpenSSL.crypto.dump_certificate( - OpenSSL.crypto.FILETYPE_PEM, messages_test.CERT.wrapped).decode() - - single_chain = OpenSSL.crypto.dump_certificate( - OpenSSL.crypto.FILETYPE_PEM, loaded).decode() - self.chain_pem = single_chain + single_chain - - self.fullchain_pem = self.cert_pem + self.chain_pem - - self.orderr = messages.OrderResource( - csr_pem=CSR_SAN_PEM) - - def _init(self): - uri = 'http://www.letsencrypt-demo.org/directory' - from acme.client import BackwardsCompatibleClientV2 - return BackwardsCompatibleClientV2(net=self.net, - key=KEY, server=uri) - - def test_init_downloads_directory(self): - uri = 'http://www.letsencrypt-demo.org/directory' - from acme.client import BackwardsCompatibleClientV2 - BackwardsCompatibleClientV2(net=self.net, - key=KEY, server=uri) - self.net.get.assert_called_once_with(uri) - - def test_init_acme_version(self): - self.response.json.return_value = DIRECTORY_V1.to_json() - client = self._init() - self.assertEqual(client.acme_version, 1) - - self.response.json.return_value = DIRECTORY_V2.to_json() - client = self._init() - self.assertEqual(client.acme_version, 2) - - def test_query_registration_client_v2(self): - self.response.json.return_value = DIRECTORY_V2.to_json() - client = self._init() - self.response.json.return_value = self.regr.body.to_json() - self.assertEqual(self.regr, client.query_registration(self.regr)) - - def test_forwarding(self): - self.response.json.return_value = DIRECTORY_V1.to_json() - client = self._init() - self.assertEqual(client.directory, client.client.directory) - self.assertEqual(client.key, KEY) - self.assertEqual(client.deactivate_registration, client.client.deactivate_registration) - self.assertRaises(AttributeError, client.__getattr__, 'nonexistent') - self.assertRaises(AttributeError, client.__getattr__, 'new_account_and_tos') - self.assertRaises(AttributeError, client.__getattr__, 'new_account') - - def test_new_account_and_tos(self): - # v2 no tos - self.response.json.return_value = DIRECTORY_V2.to_json() - with mock.patch('acme.client.ClientV2') as mock_client: - client = self._init() - client.new_account_and_tos(self.new_reg) - mock_client().new_account.assert_called_with(self.new_reg) - - # v2 tos good - with mock.patch('acme.client.ClientV2') as mock_client: - mock_client().directory.meta.__contains__.return_value = True - client = self._init() - client.new_account_and_tos(self.new_reg, lambda x: True) - mock_client().new_account.assert_called_with( - self.new_reg.update(terms_of_service_agreed=True)) - - # v2 tos bad - with mock.patch('acme.client.ClientV2') as mock_client: - mock_client().directory.meta.__contains__.return_value = True - client = self._init() - def _tos_cb(tos): - raise errors.Error - self.assertRaises(errors.Error, client.new_account_and_tos, - self.new_reg, _tos_cb) - mock_client().new_account.assert_not_called() - - # v1 yes tos - self.response.json.return_value = DIRECTORY_V1.to_json() - with mock.patch('acme.client.Client') as mock_client: - regr = mock.MagicMock(terms_of_service="TOS") - mock_client().register.return_value = regr - client = self._init() - client.new_account_and_tos(self.new_reg) - mock_client().register.assert_called_once_with(self.new_reg) - mock_client().agree_to_tos.assert_called_once_with(regr) - - # v1 no tos - with mock.patch('acme.client.Client') as mock_client: - regr = mock.MagicMock(terms_of_service=None) - mock_client().register.return_value = regr - client = self._init() - client.new_account_and_tos(self.new_reg) - mock_client().register.assert_called_once_with(self.new_reg) - mock_client().agree_to_tos.assert_not_called() - - @mock.patch('OpenSSL.crypto.load_certificate_request') - @mock.patch('acme.crypto_util._pyopenssl_cert_or_req_all_names') - def test_new_order_v1(self, mock__pyopenssl_cert_or_req_all_names, - unused_mock_load_certificate_request): - self.response.json.return_value = DIRECTORY_V1.to_json() - mock__pyopenssl_cert_or_req_all_names.return_value = ['example.com', 'www.example.com'] - mock_csr_pem = mock.MagicMock() - with mock.patch('acme.client.Client') as mock_client: - mock_client().request_domain_challenges.return_value = mock.sentinel.auth - client = self._init() - orderr = client.new_order(mock_csr_pem) - self.assertEqual(orderr.authorizations, [mock.sentinel.auth, mock.sentinel.auth]) - - def test_new_order_v2(self): - self.response.json.return_value = DIRECTORY_V2.to_json() - mock_csr_pem = mock.MagicMock() - with mock.patch('acme.client.ClientV2') as mock_client: - client = self._init() - client.new_order(mock_csr_pem) - mock_client().new_order.assert_called_once_with(mock_csr_pem) - - @mock.patch('acme.client.Client') - def test_finalize_order_v1_success(self, mock_client): - self.response.json.return_value = DIRECTORY_V1.to_json() - - mock_client().request_issuance.return_value = self.certr - mock_client().fetch_chain.return_value = self.chain - - deadline = datetime.datetime(9999, 9, 9) - client = self._init() - result = client.finalize_order(self.orderr, deadline) - self.assertEqual(result.fullchain_pem, self.fullchain_pem) - mock_client().fetch_chain.assert_called_once_with(self.certr) - - @mock.patch('acme.client.Client') - def test_finalize_order_v1_fetch_chain_error(self, mock_client): - self.response.json.return_value = DIRECTORY_V1.to_json() - - mock_client().request_issuance.return_value = self.certr - mock_client().fetch_chain.return_value = self.chain - mock_client().fetch_chain.side_effect = [errors.Error, self.chain] - - deadline = datetime.datetime(9999, 9, 9) - client = self._init() - result = client.finalize_order(self.orderr, deadline) - self.assertEqual(result.fullchain_pem, self.fullchain_pem) - self.assertEqual(mock_client().fetch_chain.call_count, 2) - - @mock.patch('acme.client.Client') - def test_finalize_order_v1_timeout(self, mock_client): - self.response.json.return_value = DIRECTORY_V1.to_json() - - mock_client().request_issuance.return_value = self.certr - - deadline = deadline = datetime.datetime.now() - datetime.timedelta(seconds=60) - client = self._init() - self.assertRaises(errors.TimeoutError, client.finalize_order, - self.orderr, deadline) - - def test_finalize_order_v2(self): - self.response.json.return_value = DIRECTORY_V2.to_json() - mock_orderr = mock.MagicMock() - mock_deadline = mock.MagicMock() - with mock.patch('acme.client.ClientV2') as mock_client: - client = self._init() - client.finalize_order(mock_orderr, mock_deadline) - mock_client().finalize_order.assert_called_once_with(mock_orderr, mock_deadline) - - def test_revoke(self): - self.response.json.return_value = DIRECTORY_V1.to_json() - with mock.patch('acme.client.Client') as mock_client: - client = self._init() - client.revoke(messages_test.CERT, self.rsn) - mock_client().revoke.assert_called_once_with(messages_test.CERT, self.rsn) - - self.response.json.return_value = DIRECTORY_V2.to_json() - with mock.patch('acme.client.ClientV2') as mock_client: - client = self._init() - client.revoke(messages_test.CERT, self.rsn) - mock_client().revoke.assert_called_once_with(messages_test.CERT, self.rsn) - - def test_update_registration(self): - self.response.json.return_value = DIRECTORY_V1.to_json() - with mock.patch('acme.client.Client') as mock_client: - client = self._init() - client.update_registration(mock.sentinel.regr, None) - mock_client().update_registration.assert_called_once_with(mock.sentinel.regr, None) - - # newNonce present means it will pick acme_version 2 - def test_external_account_required_true(self): - self.response.json.return_value = messages.Directory({ - 'newNonce': 'http://letsencrypt-test.com/acme/new-nonce', - 'meta': messages.Directory.Meta(external_account_required=True), - }).to_json() - - client = self._init() - - self.assertTrue(client.external_account_required()) - - # newNonce present means it will pick acme_version 2 - def test_external_account_required_false(self): - self.response.json.return_value = messages.Directory({ - 'newNonce': 'http://letsencrypt-test.com/acme/new-nonce', - 'meta': messages.Directory.Meta(external_account_required=False), - }).to_json() - - client = self._init() - - self.assertFalse(client.external_account_required()) - - def test_external_account_required_false_v1(self): - self.response.json.return_value = messages.Directory({ - 'meta': messages.Directory.Meta(external_account_required=False), - }).to_json() - - client = self._init() - - self.assertFalse(client.external_account_required()) - - -class ClientTest(ClientTestBase): - """Tests for acme.client.Client.""" - # pylint: disable=too-many-instance-attributes,too-many-public-methods - - def setUp(self): - super(ClientTest, self).setUp() - - self.directory = DIRECTORY_V1 - - # Registration - self.regr = self.regr.update( - terms_of_service='https://www.letsencrypt-demo.org/tos') - - # Request issuance - self.certr = messages.CertificateResource( - body=messages_test.CERT, authzrs=(self.authzr,), - uri='https://www.letsencrypt-demo.org/acme/cert/1', - cert_chain_uri='https://www.letsencrypt-demo.org/ca') - - from acme.client import Client - self.client = Client( - directory=self.directory, key=KEY, alg=jose.RS256, net=self.net) - - def test_init_downloads_directory(self): - uri = 'http://www.letsencrypt-demo.org/directory' - from acme.client import Client - self.client = Client( - directory=uri, key=KEY, alg=jose.RS256, net=self.net) - self.net.get.assert_called_once_with(uri) - - @mock.patch('acme.client.ClientNetwork') - def test_init_without_net(self, mock_net): - mock_net.return_value = mock.sentinel.net - alg = jose.RS256 - from acme.client import Client - self.client = Client( - directory=self.directory, key=KEY, alg=alg) - mock_net.called_once_with(KEY, alg=alg, verify_ssl=True) - self.assertEqual(self.client.net, mock.sentinel.net) - - def test_register(self): - # "Instance of 'Field' has no to_json/update member" bug: - # pylint: disable=no-member - self.response.status_code = http_client.CREATED - self.response.json.return_value = self.regr.body.to_json() - self.response.headers['Location'] = self.regr.uri - self.response.links.update({ - 'terms-of-service': {'url': self.regr.terms_of_service}, - }) - - self.assertEqual(self.regr, self.client.register(self.new_reg)) - # TODO: test POST call arguments - - def test_update_registration(self): - # "Instance of 'Field' has no to_json/update member" bug: - # pylint: disable=no-member - self.response.headers['Location'] = self.regr.uri - self.response.json.return_value = self.regr.body.to_json() - self.assertEqual(self.regr, self.client.update_registration(self.regr)) - # TODO: test POST call arguments - - # TODO: split here and separate test - self.response.json.return_value = self.regr.body.update( - contact=()).to_json() - - def test_deactivate_account(self): - self.response.headers['Location'] = self.regr.uri - self.response.json.return_value = self.regr.body.to_json() - self.assertEqual(self.regr, - self.client.deactivate_registration(self.regr)) - - def test_query_registration(self): - self.response.json.return_value = self.regr.body.to_json() - self.assertEqual(self.regr, self.client.query_registration(self.regr)) - - def test_agree_to_tos(self): - self.client.update_registration = mock.Mock() - self.client.agree_to_tos(self.regr) - regr = self.client.update_registration.call_args[0][0] - self.assertEqual(self.regr.terms_of_service, regr.body.agreement) - - def _prepare_response_for_request_challenges(self): - self.response.status_code = http_client.CREATED - self.response.headers['Location'] = self.authzr.uri - self.response.json.return_value = self.authz.to_json() - - def test_request_challenges(self): - self._prepare_response_for_request_challenges() - self.client.request_challenges(self.identifier) - self.net.post.assert_called_once_with( - self.directory.new_authz, - messages.NewAuthorization(identifier=self.identifier), - acme_version=1) - - def test_request_challenges_deprecated_arg(self): - self._prepare_response_for_request_challenges() - self.client.request_challenges(self.identifier, new_authzr_uri="hi") - self.net.post.assert_called_once_with( - self.directory.new_authz, - messages.NewAuthorization(identifier=self.identifier), - acme_version=1) - - def test_request_challenges_custom_uri(self): - self._prepare_response_for_request_challenges() - self.client.request_challenges(self.identifier) - self.net.post.assert_called_once_with( - 'https://www.letsencrypt-demo.org/acme/new-authz', mock.ANY, - acme_version=1) - - def test_request_challenges_unexpected_update(self): - self._prepare_response_for_request_challenges() - self.response.json.return_value = self.authz.update( - identifier=self.identifier.update(value='foo')).to_json() - self.assertRaises( - errors.UnexpectedUpdate, self.client.request_challenges, - self.identifier) - - def test_request_challenges_wildcard(self): - wildcard_identifier = messages.Identifier( - typ=messages.IDENTIFIER_FQDN, value='*.example.org') - self.assertRaises( - errors.WildcardUnsupportedError, self.client.request_challenges, - wildcard_identifier) - - def test_request_domain_challenges(self): - self.client.request_challenges = mock.MagicMock() - self.assertEqual( - self.client.request_challenges(self.identifier), - self.client.request_domain_challenges('example.com')) - - def test_answer_challenge(self): - self.response.links['up'] = {'url': self.challr.authzr_uri} - self.response.json.return_value = self.challr.body.to_json() - - chall_response = challenges.DNSResponse(validation=None) - - self.client.answer_challenge(self.challr.body, chall_response) - - # TODO: split here and separate test - self.assertRaises(errors.UnexpectedUpdate, self.client.answer_challenge, - self.challr.body.update(uri='foo'), chall_response) - - def test_answer_challenge_missing_next(self): - self.assertRaises( - errors.ClientError, self.client.answer_challenge, - self.challr.body, challenges.DNSResponse(validation=None)) - - def test_retry_after_date(self): - self.response.headers['Retry-After'] = 'Fri, 31 Dec 1999 23:59:59 GMT' - self.assertEqual( - datetime.datetime(1999, 12, 31, 23, 59, 59), - self.client.retry_after(response=self.response, default=10)) - - @mock.patch('acme.client.datetime') - def test_retry_after_invalid(self, dt_mock): - dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27) - dt_mock.timedelta = datetime.timedelta - - self.response.headers['Retry-After'] = 'foooo' - self.assertEqual( - datetime.datetime(2015, 3, 27, 0, 0, 10), - self.client.retry_after(response=self.response, default=10)) - - @mock.patch('acme.client.datetime') - def test_retry_after_overflow(self, dt_mock): - dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27) - dt_mock.timedelta = datetime.timedelta - dt_mock.datetime.side_effect = datetime.datetime - - self.response.headers['Retry-After'] = "Tue, 116 Feb 2016 11:50:00 MST" - self.assertEqual( - datetime.datetime(2015, 3, 27, 0, 0, 10), - self.client.retry_after(response=self.response, default=10)) - - @mock.patch('acme.client.datetime') - def test_retry_after_seconds(self, dt_mock): - dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27) - dt_mock.timedelta = datetime.timedelta - - self.response.headers['Retry-After'] = '50' - self.assertEqual( - datetime.datetime(2015, 3, 27, 0, 0, 50), - self.client.retry_after(response=self.response, default=10)) - - @mock.patch('acme.client.datetime') - def test_retry_after_missing(self, dt_mock): - dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27) - dt_mock.timedelta = datetime.timedelta - - self.assertEqual( - datetime.datetime(2015, 3, 27, 0, 0, 10), - self.client.retry_after(response=self.response, default=10)) - - def test_poll(self): - self.response.json.return_value = self.authzr.body.to_json() - self.assertEqual((self.authzr, self.response), - self.client.poll(self.authzr)) - - # TODO: split here and separate test - self.response.json.return_value = self.authz.update( - identifier=self.identifier.update(value='foo')).to_json() - self.assertRaises( - errors.UnexpectedUpdate, self.client.poll, self.authzr) - - def test_request_issuance(self): - self.response.content = CERT_DER - self.response.headers['Location'] = self.certr.uri - self.response.links['up'] = {'url': self.certr.cert_chain_uri} - self.assertEqual(self.certr, self.client.request_issuance( - messages_test.CSR, (self.authzr,))) - # TODO: check POST args - - def test_request_issuance_missing_up(self): - self.response.content = CERT_DER - self.response.headers['Location'] = self.certr.uri - self.assertEqual( - self.certr.update(cert_chain_uri=None), - self.client.request_issuance(messages_test.CSR, (self.authzr,))) - - def test_request_issuance_missing_location(self): - self.assertRaises( - errors.ClientError, self.client.request_issuance, - messages_test.CSR, (self.authzr,)) - - @mock.patch('acme.client.datetime') - @mock.patch('acme.client.time') - def test_poll_and_request_issuance(self, time_mock, dt_mock): - # clock.dt | pylint: disable=no-member - clock = mock.MagicMock(dt=datetime.datetime(2015, 3, 27)) - - def sleep(seconds): - """increment clock""" - clock.dt += datetime.timedelta(seconds=seconds) - time_mock.sleep.side_effect = sleep - - def now(): - """return current clock value""" - return clock.dt - dt_mock.datetime.now.side_effect = now - dt_mock.timedelta = datetime.timedelta - - def poll(authzr): # pylint: disable=missing-docstring - # record poll start time based on the current clock value - authzr.times.append(clock.dt) - - # suppose it takes 2 seconds for server to produce the - # result, increment clock - clock.dt += datetime.timedelta(seconds=2) - - if len(authzr.retries) == 1: # no more retries - done = mock.MagicMock(uri=authzr.uri, times=authzr.times) - done.body.status = authzr.retries[0] - return done, [] - - # response (2nd result tuple element) is reduced to only - # Retry-After header contents represented as integer - # seconds; authzr.retries is a list of Retry-After - # headers, head(retries) is peeled of as a current - # Retry-After header, and tail(retries) is persisted for - # later poll() calls - return (mock.MagicMock(retries=authzr.retries[1:], - uri=authzr.uri + '.', times=authzr.times), - authzr.retries[0]) - self.client.poll = mock.MagicMock(side_effect=poll) - - mintime = 7 - - def retry_after(response, default): - # pylint: disable=missing-docstring - # check that poll_and_request_issuance correctly passes mintime - self.assertEqual(default, mintime) - return clock.dt + datetime.timedelta(seconds=response) - self.client.retry_after = mock.MagicMock(side_effect=retry_after) - - def request_issuance(csr, authzrs): # pylint: disable=missing-docstring - return csr, authzrs - self.client.request_issuance = mock.MagicMock( - side_effect=request_issuance) - - csr = mock.MagicMock() - authzrs = ( - mock.MagicMock(uri='a', times=[], retries=( - 8, 20, 30, messages.STATUS_VALID)), - mock.MagicMock(uri='b', times=[], retries=( - 5, messages.STATUS_VALID)), - ) - - cert, updated_authzrs = self.client.poll_and_request_issuance( - csr, authzrs, mintime=mintime, - # make sure that max_attempts is per-authorization, rather - # than global - max_attempts=max(len(authzrs[0].retries), len(authzrs[1].retries))) - self.assertTrue(cert[0] is csr) - self.assertTrue(cert[1] is updated_authzrs) - self.assertEqual(updated_authzrs[0].uri, 'a...') - self.assertEqual(updated_authzrs[1].uri, 'b.') - self.assertEqual(updated_authzrs[0].times, [ - datetime.datetime(2015, 3, 27), - # a is scheduled for 10, but b is polling [9..11), so it - # will be picked up as soon as b is finished, without - # additional sleeping - datetime.datetime(2015, 3, 27, 0, 0, 11), - datetime.datetime(2015, 3, 27, 0, 0, 33), - datetime.datetime(2015, 3, 27, 0, 1, 5), - ]) - self.assertEqual(updated_authzrs[1].times, [ - datetime.datetime(2015, 3, 27, 0, 0, 2), - datetime.datetime(2015, 3, 27, 0, 0, 9), - ]) - self.assertEqual(clock.dt, datetime.datetime(2015, 3, 27, 0, 1, 7)) - - # CA sets invalid | TODO: move to a separate test - invalid_authzr = mock.MagicMock( - times=[], retries=[messages.STATUS_INVALID]) - self.assertRaises( - errors.PollError, self.client.poll_and_request_issuance, - csr, authzrs=(invalid_authzr,), mintime=mintime) - - # exceeded max_attempts | TODO: move to a separate test - self.assertRaises( - errors.PollError, self.client.poll_and_request_issuance, - csr, authzrs, mintime=mintime, max_attempts=2) - - def test_check_cert(self): - self.response.headers['Location'] = self.certr.uri - self.response.content = CERT_DER - self.assertEqual(self.certr.update(body=messages_test.CERT), - self.client.check_cert(self.certr)) - - # TODO: split here and separate test - self.response.headers['Location'] = 'foo' - self.assertRaises( - errors.UnexpectedUpdate, self.client.check_cert, self.certr) - - def test_check_cert_missing_location(self): - self.response.content = CERT_DER - self.assertRaises( - errors.ClientError, self.client.check_cert, self.certr) - - def test_refresh(self): - self.client.check_cert = mock.MagicMock() - self.assertEqual( - self.client.check_cert(self.certr), self.client.refresh(self.certr)) - - def test_fetch_chain_no_up_link(self): - self.assertEqual([], self.client.fetch_chain(self.certr.update( - cert_chain_uri=None))) - - def test_fetch_chain_single(self): - # pylint: disable=protected-access - self.client._get_cert = mock.MagicMock() - self.client._get_cert.return_value = ( - mock.MagicMock(links={}), "certificate") - self.assertEqual([self.client._get_cert(self.certr.cert_chain_uri)[1]], - self.client.fetch_chain(self.certr)) - - def test_fetch_chain_max(self): - # pylint: disable=protected-access - up_response = mock.MagicMock(links={'up': {'url': 'http://cert'}}) - noup_response = mock.MagicMock(links={}) - self.client._get_cert = mock.MagicMock() - self.client._get_cert.side_effect = [ - (up_response, "cert")] * 9 + [(noup_response, "last_cert")] - chain = self.client.fetch_chain(self.certr, max_length=10) - self.assertEqual(chain, ["cert"] * 9 + ["last_cert"]) - - def test_fetch_chain_too_many(self): # recursive - # pylint: disable=protected-access - response = mock.MagicMock(links={'up': {'url': 'http://cert'}}) - self.client._get_cert = mock.MagicMock() - self.client._get_cert.return_value = (response, "certificate") - self.assertRaises(errors.Error, self.client.fetch_chain, self.certr) - - def test_revoke(self): - self.client.revoke(self.certr.body, self.rsn) - self.net.post.assert_called_once_with( - self.directory[messages.Revocation], mock.ANY, acme_version=1) - - def test_revocation_payload(self): - obj = messages.Revocation(certificate=self.certr.body, reason=self.rsn) - self.assertTrue('reason' in obj.to_partial_json().keys()) - self.assertEqual(self.rsn, obj.to_partial_json()['reason']) - - def test_revoke_bad_status_raises_error(self): - self.response.status_code = http_client.METHOD_NOT_ALLOWED - self.assertRaises( - errors.ClientError, - self.client.revoke, - self.certr, - self.rsn) - - -class ClientV2Test(ClientTestBase): - """Tests for acme.client.ClientV2.""" - - def setUp(self): - super(ClientV2Test, self).setUp() - - self.directory = DIRECTORY_V2 - - from acme.client import ClientV2 - self.client = ClientV2(self.directory, self.net) - - self.new_reg = self.new_reg.update(terms_of_service_agreed=True) - - self.authzr_uri2 = 'https://www.letsencrypt-demo.org/acme/authz/2' - self.authz2 = self.authz.update(identifier=messages.Identifier( - typ=messages.IDENTIFIER_FQDN, value='www.example.com'), - status=messages.STATUS_PENDING) - self.authzr2 = messages.AuthorizationResource( - body=self.authz2, uri=self.authzr_uri2) - - self.order = messages.Order( - identifiers=(self.authz.identifier, self.authz2.identifier), - status=messages.STATUS_PENDING, - authorizations=(self.authzr.uri, self.authzr_uri2), - finalize='https://www.letsencrypt-demo.org/acme/acct/1/order/1/finalize') - self.orderr = messages.OrderResource( - body=self.order, - uri='https://www.letsencrypt-demo.org/acme/acct/1/order/1', - authorizations=[self.authzr, self.authzr2], csr_pem=CSR_SAN_PEM) - - def test_new_account(self): - self.response.status_code = http_client.CREATED - self.response.json.return_value = self.regr.body.to_json() - self.response.headers['Location'] = self.regr.uri - - self.assertEqual(self.regr, self.client.new_account(self.new_reg)) - - def test_new_account_conflict(self): - self.response.status_code = http_client.OK - self.response.headers['Location'] = self.regr.uri - self.assertRaises(errors.ConflictError, self.client.new_account, self.new_reg) - - def test_new_order(self): - order_response = copy.deepcopy(self.response) - order_response.status_code = http_client.CREATED - order_response.json.return_value = self.order.to_json() - order_response.headers['Location'] = self.orderr.uri - self.net.post.return_value = order_response - - authz_response = copy.deepcopy(self.response) - authz_response.json.return_value = self.authz.to_json() - authz_response.headers['Location'] = self.authzr.uri - authz_response2 = self.response - authz_response2.json.return_value = self.authz2.to_json() - authz_response2.headers['Location'] = self.authzr2.uri - - with mock.patch('acme.client.ClientV2._post_as_get') as mock_post_as_get: - mock_post_as_get.side_effect = (authz_response, authz_response2) - self.assertEqual(self.client.new_order(CSR_SAN_PEM), self.orderr) - - @mock.patch('acme.client.datetime') - def test_poll_and_finalize(self, mock_datetime): - mock_datetime.datetime.now.return_value = datetime.datetime(2018, 2, 15) - mock_datetime.timedelta = datetime.timedelta - expected_deadline = mock_datetime.datetime.now() + datetime.timedelta(seconds=90) - - self.client.poll_authorizations = mock.Mock(return_value=self.orderr) - self.client.finalize_order = mock.Mock(return_value=self.orderr) - - self.assertEqual(self.client.poll_and_finalize(self.orderr), self.orderr) - self.client.poll_authorizations.assert_called_once_with(self.orderr, expected_deadline) - self.client.finalize_order.assert_called_once_with(self.orderr, expected_deadline) - - @mock.patch('acme.client.datetime') - def test_poll_authorizations_timeout(self, mock_datetime): - now_side_effect = [datetime.datetime(2018, 2, 15), - datetime.datetime(2018, 2, 16), - datetime.datetime(2018, 2, 17)] - mock_datetime.datetime.now.side_effect = now_side_effect - self.response.json.side_effect = [ - self.authz.to_json(), self.authz2.to_json(), self.authz2.to_json()] - - self.assertRaises( - errors.TimeoutError, self.client.poll_authorizations, self.orderr, now_side_effect[1]) - - def test_poll_authorizations_failure(self): - deadline = datetime.datetime(9999, 9, 9) - challb = self.challr.body.update(status=messages.STATUS_INVALID, - error=messages.Error.with_code('unauthorized')) - authz = self.authz.update(status=messages.STATUS_INVALID, challenges=(challb,)) - self.response.json.return_value = authz.to_json() - - self.assertRaises( - errors.ValidationError, self.client.poll_authorizations, self.orderr, deadline) - - def test_poll_authorizations_success(self): - deadline = datetime.datetime(9999, 9, 9) - updated_authz2 = self.authz2.update(status=messages.STATUS_VALID) - updated_authzr2 = messages.AuthorizationResource( - body=updated_authz2, uri=self.authzr_uri2) - updated_orderr = self.orderr.update(authorizations=[self.authzr, updated_authzr2]) - - self.response.json.side_effect = ( - self.authz.to_json(), self.authz2.to_json(), updated_authz2.to_json()) - self.assertEqual(self.client.poll_authorizations(self.orderr, deadline), updated_orderr) - - def test_finalize_order_success(self): - updated_order = self.order.update( - certificate='https://www.letsencrypt-demo.org/acme/cert/') - updated_orderr = self.orderr.update(body=updated_order, fullchain_pem=CERT_SAN_PEM) - - self.response.json.return_value = updated_order.to_json() - self.response.text = CERT_SAN_PEM - - deadline = datetime.datetime(9999, 9, 9) - self.assertEqual(self.client.finalize_order(self.orderr, deadline), updated_orderr) - - def test_finalize_order_error(self): - updated_order = self.order.update(error=messages.Error.with_code('unauthorized')) - self.response.json.return_value = updated_order.to_json() - - deadline = datetime.datetime(9999, 9, 9) - self.assertRaises(errors.IssuanceError, self.client.finalize_order, self.orderr, deadline) - - def test_finalize_order_timeout(self): - deadline = datetime.datetime.now() - datetime.timedelta(seconds=60) - self.assertRaises(errors.TimeoutError, self.client.finalize_order, self.orderr, deadline) - - def test_revoke(self): - self.client.revoke(messages_test.CERT, self.rsn) - self.net.post.assert_called_once_with( - self.directory["revokeCert"], mock.ANY, acme_version=2, - new_nonce_url=DIRECTORY_V2['newNonce']) - - def test_update_registration(self): - # "Instance of 'Field' has no to_json/update member" bug: - # pylint: disable=no-member - self.response.headers['Location'] = self.regr.uri - self.response.json.return_value = self.regr.body.to_json() - self.assertEqual(self.regr, self.client.update_registration(self.regr)) - self.assertNotEqual(self.client.net.account, None) - self.assertEqual(self.client.net.post.call_count, 2) - self.assertTrue(DIRECTORY_V2.newAccount in self.net.post.call_args_list[0][0]) - - self.response.json.return_value = self.regr.body.update( - contact=()).to_json() - - def test_external_account_required_true(self): - self.client.directory = messages.Directory({ - 'meta': messages.Directory.Meta(external_account_required=True) - }) - - self.assertTrue(self.client.external_account_required()) - - def test_external_account_required_false(self): - self.client.directory = messages.Directory({ - 'meta': messages.Directory.Meta(external_account_required=False) - }) - - self.assertFalse(self.client.external_account_required()) - - def test_external_account_required_default(self): - self.assertFalse(self.client.external_account_required()) - - def test_post_as_get(self): - with mock.patch('acme.client.ClientV2._authzr_from_response') as mock_client: - mock_client.return_value = self.authzr2 - - self.client.poll(self.authzr2) # pylint: disable=protected-access - - self.client.net.post.assert_called_once_with( - self.authzr2.uri, None, acme_version=2, - new_nonce_url='https://www.letsencrypt-demo.org/acme/new-nonce') - self.client.net.get.assert_not_called() - - class FakeError(messages.Error): # pylint: disable=too-many-ancestors - """Fake error to reproduce a malformed request ACME error""" - def __init__(self): # pylint: disable=super-init-not-called - pass - @property - def code(self): - return 'malformed' - self.client.net.post.side_effect = FakeError() - - self.client.poll(self.authzr2) # pylint: disable=protected-access - - self.client.net.get.assert_called_once_with(self.authzr2.uri) - - -class MockJSONDeSerializable(jose.JSONDeSerializable): - # pylint: disable=missing-docstring - def __init__(self, value): - self.value = value - - def to_partial_json(self): - return {'foo': self.value} - - @classmethod - def from_json(cls, value): - pass # pragma: no cover - - -class ClientNetworkTest(unittest.TestCase): - """Tests for acme.client.ClientNetwork.""" - # pylint: disable=too-many-public-methods - - def setUp(self): - self.verify_ssl = mock.MagicMock() - self.wrap_in_jws = mock.MagicMock(return_value=mock.sentinel.wrapped) - - from acme.client import ClientNetwork - self.net = ClientNetwork( - key=KEY, alg=jose.RS256, verify_ssl=self.verify_ssl, - user_agent='acme-python-test') - - self.response = mock.MagicMock(ok=True, status_code=http_client.OK) - self.response.headers = {} - self.response.links = {} - - def test_init(self): - self.assertTrue(self.net.verify_ssl is self.verify_ssl) - - def test_wrap_in_jws(self): - # pylint: disable=protected-access - jws_dump = self.net._wrap_in_jws( - MockJSONDeSerializable('foo'), nonce=b'Tg', url="url", - acme_version=1) - jws = acme_jws.JWS.json_loads(jws_dump) - self.assertEqual(json.loads(jws.payload.decode()), {'foo': 'foo'}) - self.assertEqual(jws.signature.combined.nonce, b'Tg') - - def test_wrap_in_jws_v2(self): - self.net.account = {'uri': 'acct-uri'} - # pylint: disable=protected-access - jws_dump = self.net._wrap_in_jws( - MockJSONDeSerializable('foo'), nonce=b'Tg', url="url", - acme_version=2) - jws = acme_jws.JWS.json_loads(jws_dump) - self.assertEqual(json.loads(jws.payload.decode()), {'foo': 'foo'}) - self.assertEqual(jws.signature.combined.nonce, b'Tg') - self.assertEqual(jws.signature.combined.kid, u'acct-uri') - self.assertEqual(jws.signature.combined.url, u'url') - - def test_check_response_not_ok_jobj_no_error(self): - self.response.ok = False - self.response.json.return_value = {} - with mock.patch('acme.client.messages.Error.from_json') as from_json: - from_json.side_effect = jose.DeserializationError - # pylint: disable=protected-access - self.assertRaises( - errors.ClientError, self.net._check_response, self.response) - - def test_check_response_not_ok_jobj_error(self): - self.response.ok = False - self.response.json.return_value = messages.Error( - detail='foo', typ='serverInternal', title='some title').to_json() - # pylint: disable=protected-access - self.assertRaises( - messages.Error, self.net._check_response, self.response) - - def test_check_response_not_ok_no_jobj(self): - self.response.ok = False - self.response.json.side_effect = ValueError - # pylint: disable=protected-access - self.assertRaises( - errors.ClientError, self.net._check_response, self.response) - - def test_check_response_ok_no_jobj_ct_required(self): - self.response.json.side_effect = ValueError - for response_ct in [self.net.JSON_CONTENT_TYPE, 'foo']: - self.response.headers['Content-Type'] = response_ct - # pylint: disable=protected-access - self.assertRaises( - errors.ClientError, self.net._check_response, self.response, - content_type=self.net.JSON_CONTENT_TYPE) - - def test_check_response_ok_no_jobj_no_ct(self): - self.response.json.side_effect = ValueError - for response_ct in [self.net.JSON_CONTENT_TYPE, 'foo']: - self.response.headers['Content-Type'] = response_ct - # pylint: disable=protected-access,no-value-for-parameter - self.assertEqual( - self.response, self.net._check_response(self.response)) - - def test_check_response_conflict(self): - self.response.ok = False - self.response.status_code = 409 - # pylint: disable=protected-access - self.assertRaises(errors.ConflictError, self.net._check_response, self.response) - - def test_check_response_jobj(self): - self.response.json.return_value = {} - for response_ct in [self.net.JSON_CONTENT_TYPE, 'foo']: - self.response.headers['Content-Type'] = response_ct - # pylint: disable=protected-access,no-value-for-parameter - self.assertEqual( - self.response, self.net._check_response(self.response)) - - def test_send_request(self): - self.net.session = mock.MagicMock() - self.net.session.request.return_value = self.response - # pylint: disable=protected-access - self.assertEqual(self.response, self.net._send_request( - 'HEAD', 'http://example.com/', 'foo', bar='baz')) - self.net.session.request.assert_called_once_with( - 'HEAD', 'http://example.com/', 'foo', - headers=mock.ANY, verify=mock.ANY, timeout=mock.ANY, bar='baz') - - @mock.patch('acme.client.logger') - def test_send_request_get_der(self, mock_logger): - self.net.session = mock.MagicMock() - self.net.session.request.return_value = mock.MagicMock( - ok=True, status_code=http_client.OK, - headers={"Content-Type": "application/pkix-cert"}, - content=b"hi") - # pylint: disable=protected-access - self.net._send_request('HEAD', 'http://example.com/', 'foo', - timeout=mock.ANY, bar='baz') - mock_logger.debug.assert_called_with( - 'Received response:\nHTTP %d\n%s\n\n%s', 200, - 'Content-Type: application/pkix-cert', b'aGk=') - - def test_send_request_post(self): - self.net.session = mock.MagicMock() - self.net.session.request.return_value = self.response - # pylint: disable=protected-access - self.assertEqual(self.response, self.net._send_request( - 'POST', 'http://example.com/', 'foo', data='qux', bar='baz')) - self.net.session.request.assert_called_once_with( - 'POST', 'http://example.com/', 'foo', - headers=mock.ANY, verify=mock.ANY, timeout=mock.ANY, data='qux', bar='baz') - - def test_send_request_verify_ssl(self): - # pylint: disable=protected-access - for verify in True, False: - self.net.session = mock.MagicMock() - self.net.session.request.return_value = self.response - self.net.verify_ssl = verify - # pylint: disable=protected-access - self.assertEqual( - self.response, - self.net._send_request('GET', 'http://example.com/')) - self.net.session.request.assert_called_once_with( - 'GET', 'http://example.com/', verify=verify, - timeout=mock.ANY, headers=mock.ANY) - - def test_send_request_user_agent(self): - self.net.session = mock.MagicMock() - # pylint: disable=protected-access - self.net._send_request('GET', 'http://example.com/', - headers={'bar': 'baz'}) - self.net.session.request.assert_called_once_with( - 'GET', 'http://example.com/', verify=mock.ANY, - timeout=mock.ANY, - headers={'User-Agent': 'acme-python-test', 'bar': 'baz'}) - - self.net._send_request('GET', 'http://example.com/', - headers={'User-Agent': 'foo2'}) - self.net.session.request.assert_called_with( - 'GET', 'http://example.com/', - verify=mock.ANY, timeout=mock.ANY, headers={'User-Agent': 'foo2'}) - - def test_send_request_timeout(self): - self.net.session = mock.MagicMock() - # pylint: disable=protected-access - self.net._send_request('GET', 'http://example.com/', - headers={'bar': 'baz'}) - self.net.session.request.assert_called_once_with( - mock.ANY, mock.ANY, verify=mock.ANY, headers=mock.ANY, - timeout=45) - - def test_del(self, close_exception=None): - sess = mock.MagicMock() - - if close_exception is not None: - sess.close.side_effect = close_exception - - self.net.session = sess - del self.net - sess.close.assert_called_once_with() - - def test_del_error(self): - self.test_del(ReferenceError) - - @mock.patch('acme.client.requests') - def test_requests_error_passthrough(self, mock_requests): - mock_requests.exceptions = requests.exceptions - mock_requests.request.side_effect = requests.exceptions.RequestException - # pylint: disable=protected-access - self.assertRaises(requests.exceptions.RequestException, - self.net._send_request, 'GET', 'uri') - - def test_urllib_error(self): - # Using a connection error to test a properly formatted error message - try: - # pylint: disable=protected-access - self.net._send_request('GET', "http://localhost:19123/nonexistent.txt") - - # Value Error Generated Exceptions - except ValueError as y: - self.assertEqual("Requesting localhost/nonexistent: " - "Connection refused", str(y)) - - # Requests Library Exceptions - except requests.exceptions.ConnectionError as z: #pragma: no cover - self.assertTrue("'Connection aborted.'" in str(z) or "[WinError 10061]" in str(z)) - - -class ClientNetworkWithMockedResponseTest(unittest.TestCase): - """Tests for acme.client.ClientNetwork which mock out response.""" - # pylint: disable=too-many-instance-attributes - - def setUp(self): - from acme.client import ClientNetwork - self.net = ClientNetwork(key=None, alg=None) - - self.response = mock.MagicMock(ok=True, status_code=http_client.OK) - self.response.headers = {} - self.response.links = {} - self.response.checked = False - self.acmev1_nonce_response = mock.MagicMock(ok=False, - status_code=http_client.METHOD_NOT_ALLOWED) - self.acmev1_nonce_response.headers = {} - self.obj = mock.MagicMock() - self.wrapped_obj = mock.MagicMock() - self.content_type = mock.sentinel.content_type - - self.all_nonces = [ - jose.b64encode(b'Nonce'), - jose.b64encode(b'Nonce2'), jose.b64encode(b'Nonce3')] - self.available_nonces = self.all_nonces[:] - - def send_request(*args, **kwargs): - # pylint: disable=unused-argument,missing-docstring - self.assertFalse("new_nonce_url" in kwargs) - method = args[0] - uri = args[1] - if method == 'HEAD' and uri != "new_nonce_uri": - response = self.acmev1_nonce_response - else: - response = self.response - - if self.available_nonces: - response.headers = { - self.net.REPLAY_NONCE_HEADER: - self.available_nonces.pop().decode()} - else: - response.headers = {} - return response - - # pylint: disable=protected-access - self.net._send_request = self.send_request = mock.MagicMock( - side_effect=send_request) - self.net._check_response = self.check_response - self.net._wrap_in_jws = mock.MagicMock(return_value=self.wrapped_obj) - - def check_response(self, response, content_type): - # pylint: disable=missing-docstring - self.assertEqual(self.response, response) - self.assertEqual(self.content_type, content_type) - self.assertTrue(self.response.ok) - self.response.checked = True - return self.response - - def test_head(self): - self.assertEqual(self.acmev1_nonce_response, self.net.head( - 'http://example.com/', 'foo', bar='baz')) - self.send_request.assert_called_once_with( - 'HEAD', 'http://example.com/', 'foo', bar='baz') - - def test_head_v2(self): - self.assertEqual(self.response, self.net.head( - 'new_nonce_uri', 'foo', bar='baz')) - self.send_request.assert_called_once_with( - 'HEAD', 'new_nonce_uri', 'foo', bar='baz') - - def test_get(self): - self.assertEqual(self.response, self.net.get( - 'http://example.com/', content_type=self.content_type, bar='baz')) - self.assertTrue(self.response.checked) - self.send_request.assert_called_once_with( - 'GET', 'http://example.com/', bar='baz') - - def test_post_no_content_type(self): - self.content_type = self.net.JOSE_CONTENT_TYPE - self.assertEqual(self.response, self.net.post('uri', self.obj)) - self.assertTrue(self.response.checked) - - def test_post(self): - # pylint: disable=protected-access - self.assertEqual(self.response, self.net.post( - 'uri', self.obj, content_type=self.content_type)) - self.assertTrue(self.response.checked) - self.net._wrap_in_jws.assert_called_once_with( - self.obj, jose.b64decode(self.all_nonces.pop()), "uri", 1) - - self.available_nonces = [] - self.assertRaises(errors.MissingNonce, self.net.post, - 'uri', self.obj, content_type=self.content_type) - self.net._wrap_in_jws.assert_called_with( - self.obj, jose.b64decode(self.all_nonces.pop()), "uri", 1) - - def test_post_wrong_initial_nonce(self): # HEAD - self.available_nonces = [b'f', jose.b64encode(b'good')] - self.assertRaises(errors.BadNonce, self.net.post, 'uri', - self.obj, content_type=self.content_type) - - def test_post_wrong_post_response_nonce(self): - self.available_nonces = [jose.b64encode(b'good'), b'f'] - self.assertRaises(errors.BadNonce, self.net.post, 'uri', - self.obj, content_type=self.content_type) - - def test_post_failed_retry(self): - check_response = mock.MagicMock() - check_response.side_effect = messages.Error.with_code('badNonce') - - # pylint: disable=protected-access - self.net._check_response = check_response - self.assertRaises(messages.Error, self.net.post, 'uri', - self.obj, content_type=self.content_type) - - def test_post_not_retried(self): - check_response = mock.MagicMock() - check_response.side_effect = [messages.Error.with_code('malformed'), - self.response] - - # pylint: disable=protected-access - self.net._check_response = check_response - self.assertRaises(messages.Error, self.net.post, 'uri', - self.obj, content_type=self.content_type) - - def test_post_successful_retry(self): - post_once = mock.MagicMock() - post_once.side_effect = [messages.Error.with_code('badNonce'), - self.response] - - # pylint: disable=protected-access - self.assertEqual(self.response, self.net.post( - 'uri', self.obj, content_type=self.content_type)) - - def test_head_get_post_error_passthrough(self): - self.send_request.side_effect = requests.exceptions.RequestException - for method in self.net.head, self.net.get: - self.assertRaises( - requests.exceptions.RequestException, method, 'GET', 'uri') - self.assertRaises(requests.exceptions.RequestException, - self.net.post, 'uri', obj=self.obj) - - def test_post_bad_nonce_head(self): - # pylint: disable=protected-access - # regression test for https://github.com/certbot/certbot/issues/6092 - bad_response = mock.MagicMock(ok=False, status_code=http_client.SERVICE_UNAVAILABLE) - self.net._send_request = mock.MagicMock() - self.net._send_request.return_value = bad_response - self.content_type = None - check_response = mock.MagicMock() - self.net._check_response = check_response - self.assertRaises(errors.ClientError, self.net.post, 'uri', - self.obj, content_type=self.content_type, acme_version=2, - new_nonce_url='new_nonce_uri') - self.assertEqual(check_response.call_count, 1) - - def test_new_nonce_uri_removed(self): - self.content_type = None - self.net.post('uri', self.obj, content_type=None, - acme_version=2, new_nonce_url='new_nonce_uri') - - -class ClientNetworkSourceAddressBindingTest(unittest.TestCase): - """Tests that if ClientNetwork has a source IP set manually, the underlying library has - used the provided source address.""" - - def setUp(self): - self.source_address = "8.8.8.8" - - def test_source_address_set(self): - from acme.client import ClientNetwork - net = ClientNetwork(key=None, alg=None, source_address=self.source_address) - for adapter in net.session.adapters.values(): - self.assertTrue(self.source_address in adapter.source_address) - - def test_behavior_assumption(self): - """This is a test that guardrails the HTTPAdapter behavior so that if the default for - a Session() changes, the assumptions here aren't violated silently.""" - from acme.client import ClientNetwork - # Source address not specified, so the default adapter type should be bound -- this - # test should fail if the default adapter type is changed by requests - net = ClientNetwork(key=None, alg=None) - session = requests.Session() - for scheme in session.adapters.keys(): - client_network_adapter = net.session.adapters.get(scheme) - default_adapter = session.adapters.get(scheme) - self.assertEqual(client_network_adapter.__class__, default_adapter.__class__) - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/acme/acme/crypto_util.py b/acme/acme/crypto_util.py deleted file mode 100644 index c88cab9431d..00000000000 --- a/acme/acme/crypto_util.py +++ /dev/null @@ -1,312 +0,0 @@ -"""Crypto utilities.""" -import binascii -import contextlib -import logging -import os -import re -import socket - -from OpenSSL import crypto -from OpenSSL import SSL # type: ignore # https://github.com/python/typeshed/issues/2052 -import josepy as jose - -from acme import errors -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import Callable, Union, Tuple, Optional -# pylint: enable=unused-import, no-name-in-module - - -logger = logging.getLogger(__name__) - -# TLSSNI01 certificate serving and probing is not affected by SSL -# vulnerabilities: prober needs to check certificate for expected -# contents anyway. Working SNI is the only thing that's necessary for -# the challenge and thus scoping down SSL/TLS method (version) would -# cause interoperability issues: TLSv1_METHOD is only compatible with -# TLSv1_METHOD, while SSLv23_METHOD is compatible with all other -# methods, including TLSv2_METHOD (read more at -# https://www.openssl.org/docs/ssl/SSLv23_method.html). _serve_sni -# should be changed to use "set_options" to disable SSLv2 and SSLv3, -# in case it's used for things other than probing/serving! -_DEFAULT_TLSSNI01_SSL_METHOD = SSL.SSLv23_METHOD # type: ignore - - -class SSLSocket(object): # pylint: disable=too-few-public-methods - """SSL wrapper for sockets. - - :ivar socket sock: Original wrapped socket. - :ivar dict certs: Mapping from domain names (`bytes`) to - `OpenSSL.crypto.X509`. - :ivar method: See `OpenSSL.SSL.Context` for allowed values. - - """ - def __init__(self, sock, certs, method=_DEFAULT_TLSSNI01_SSL_METHOD): - self.sock = sock - self.certs = certs - self.method = method - - def __getattr__(self, name): - return getattr(self.sock, name) - - def _pick_certificate_cb(self, connection): - """SNI certificate callback. - - This method will set a new OpenSSL context object for this - connection when an incoming connection provides an SNI name - (in order to serve the appropriate certificate, if any). - - :param connection: The TLS connection object on which the SNI - extension was received. - :type connection: :class:`OpenSSL.Connection` - - """ - server_name = connection.get_servername() - try: - key, cert = self.certs[server_name] - except KeyError: - logger.debug("Server name (%s) not recognized, dropping SSL", - server_name) - return - new_context = SSL.Context(self.method) - new_context.set_options(SSL.OP_NO_SSLv2) - new_context.set_options(SSL.OP_NO_SSLv3) - new_context.use_privatekey(key) - new_context.use_certificate(cert) - connection.set_context(new_context) - - class FakeConnection(object): - """Fake OpenSSL.SSL.Connection.""" - - # pylint: disable=too-few-public-methods,missing-docstring - - def __init__(self, connection): - self._wrapped = connection - - def __getattr__(self, name): - return getattr(self._wrapped, name) - - def shutdown(self, *unused_args): - # OpenSSL.SSL.Connection.shutdown doesn't accept any args - return self._wrapped.shutdown() - - def accept(self): # pylint: disable=missing-docstring - sock, addr = self.sock.accept() - - context = SSL.Context(self.method) - context.set_options(SSL.OP_NO_SSLv2) - context.set_options(SSL.OP_NO_SSLv3) - context.set_tlsext_servername_callback(self._pick_certificate_cb) - - ssl_sock = self.FakeConnection(SSL.Connection(context, sock)) - ssl_sock.set_accept_state() - - logger.debug("Performing handshake with %s", addr) - try: - ssl_sock.do_handshake() - except SSL.Error as error: - # _pick_certificate_cb might have returned without - # creating SSL context (wrong server name) - raise socket.error(error) - - return ssl_sock, addr - - -def probe_sni(name, host, port=443, timeout=300, - method=_DEFAULT_TLSSNI01_SSL_METHOD, source_address=('', 0)): - """Probe SNI server for SSL certificate. - - :param bytes name: Byte string to send as the server name in the - client hello message. - :param bytes host: Host to connect to. - :param int port: Port to connect to. - :param int timeout: Timeout in seconds. - :param method: See `OpenSSL.SSL.Context` for allowed values. - :param tuple source_address: Enables multi-path probing (selection - of source interface). See `socket.creation_connection` for more - info. Available only in Python 2.7+. - - :raises acme.errors.Error: In case of any problems. - - :returns: SSL certificate presented by the server. - :rtype: OpenSSL.crypto.X509 - - """ - context = SSL.Context(method) - context.set_timeout(timeout) - - socket_kwargs = {'source_address': source_address} - - try: - # pylint: disable=star-args - logger.debug( - "Attempting to connect to %s:%d%s.", host, port, - " from {0}:{1}".format( - source_address[0], - source_address[1] - ) if socket_kwargs else "" - ) - socket_tuple = (host, port) # type: Tuple[str, int] - sock = socket.create_connection(socket_tuple, **socket_kwargs) # type: ignore - except socket.error as error: - raise errors.Error(error) - - with contextlib.closing(sock) as client: - client_ssl = SSL.Connection(context, client) - client_ssl.set_connect_state() - client_ssl.set_tlsext_host_name(name) # pyOpenSSL>=0.13 - try: - client_ssl.do_handshake() - client_ssl.shutdown() - except SSL.Error as error: - raise errors.Error(error) - return client_ssl.get_peer_certificate() - -def make_csr(private_key_pem, domains, must_staple=False): - """Generate a CSR containing a list of domains as subjectAltNames. - - :param buffer private_key_pem: Private key, in PEM PKCS#8 format. - :param list domains: List of DNS names to include in subjectAltNames of CSR. - :param bool must_staple: Whether to include the TLS Feature extension (aka - OCSP Must Staple: https://tools.ietf.org/html/rfc7633). - :returns: buffer PEM-encoded Certificate Signing Request. - """ - private_key = crypto.load_privatekey( - crypto.FILETYPE_PEM, private_key_pem) - csr = crypto.X509Req() - extensions = [ - crypto.X509Extension( - b'subjectAltName', - critical=False, - value=', '.join('DNS:' + d for d in domains).encode('ascii') - ), - ] - if must_staple: - extensions.append(crypto.X509Extension( - b"1.3.6.1.5.5.7.1.24", - critical=False, - value=b"DER:30:03:02:01:05")) - csr.add_extensions(extensions) - csr.set_pubkey(private_key) - csr.set_version(2) - csr.sign(private_key, 'sha256') - return crypto.dump_certificate_request( - crypto.FILETYPE_PEM, csr) - -def _pyopenssl_cert_or_req_all_names(loaded_cert_or_req): - common_name = loaded_cert_or_req.get_subject().CN - sans = _pyopenssl_cert_or_req_san(loaded_cert_or_req) - - if common_name is None: - return sans - else: - return [common_name] + [d for d in sans if d != common_name] - -def _pyopenssl_cert_or_req_san(cert_or_req): - """Get Subject Alternative Names from certificate or CSR using pyOpenSSL. - - .. todo:: Implement directly in PyOpenSSL! - - .. note:: Although this is `acme` internal API, it is used by - `letsencrypt`. - - :param cert_or_req: Certificate or CSR. - :type cert_or_req: `OpenSSL.crypto.X509` or `OpenSSL.crypto.X509Req`. - - :returns: A list of Subject Alternative Names. - :rtype: `list` of `unicode` - - """ - # This function finds SANs by dumping the certificate/CSR to text and - # searching for "X509v3 Subject Alternative Name" in the text. This method - # is used to support PyOpenSSL version 0.13 where the - # `_subjectAltNameString` and `get_extensions` methods are not available - # for CSRs. - - # constants based on PyOpenSSL certificate/CSR text dump - part_separator = ":" - parts_separator = ", " - prefix = "DNS" + part_separator - - if isinstance(cert_or_req, crypto.X509): - # pylint: disable=line-too-long - func = crypto.dump_certificate # type: Union[Callable[[int, crypto.X509Req], bytes], Callable[[int, crypto.X509], bytes]] - else: - func = crypto.dump_certificate_request - text = func(crypto.FILETYPE_TEXT, cert_or_req).decode("utf-8") - # WARNING: this function does not support multiple SANs extensions. - # Multiple X509v3 extensions of the same type is disallowed by RFC 5280. - match = re.search(r"X509v3 Subject Alternative Name:(?: critical)?\s*(.*)", text) - # WARNING: this function assumes that no SAN can include - # parts_separator, hence the split! - sans_parts = [] if match is None else match.group(1).split(parts_separator) - - return [part.split(part_separator)[1] - for part in sans_parts if part.startswith(prefix)] - - -def gen_ss_cert(key, domains, not_before=None, - validity=(7 * 24 * 60 * 60), force_san=True): - """Generate new self-signed certificate. - - :type domains: `list` of `unicode` - :param OpenSSL.crypto.PKey key: - :param bool force_san: - - If more than one domain is provided, all of the domains are put into - ``subjectAltName`` X.509 extension and first domain is set as the - subject CN. If only one domain is provided no ``subjectAltName`` - extension is used, unless `force_san` is ``True``. - - """ - assert domains, "Must provide one or more hostnames for the cert." - cert = crypto.X509() - cert.set_serial_number(int(binascii.hexlify(os.urandom(16)), 16)) - cert.set_version(2) - - extensions = [ - crypto.X509Extension( - b"basicConstraints", True, b"CA:TRUE, pathlen:0"), - ] - - cert.get_subject().CN = domains[0] - # TODO: what to put into cert.get_subject()? - cert.set_issuer(cert.get_subject()) - - if force_san or len(domains) > 1: - extensions.append(crypto.X509Extension( - b"subjectAltName", - critical=False, - value=b", ".join(b"DNS:" + d.encode() for d in domains) - )) - - cert.add_extensions(extensions) - - cert.gmtime_adj_notBefore(0 if not_before is None else not_before) - cert.gmtime_adj_notAfter(validity) - - cert.set_pubkey(key) - cert.sign(key, "sha256") - return cert - -def dump_pyopenssl_chain(chain, filetype=crypto.FILETYPE_PEM): - """Dump certificate chain into a bundle. - - :param list chain: List of `OpenSSL.crypto.X509` (or wrapped in - :class:`josepy.util.ComparableX509`). - - :returns: certificate chain bundle - :rtype: bytes - - """ - # XXX: returns empty string when no chain is available, which - # shuts up RenewableCert, but might not be the best solution... - - def _dump_cert(cert): - if isinstance(cert, jose.ComparableX509): - # pylint: disable=protected-access - cert = cert.wrapped - return crypto.dump_certificate(filetype, cert) - - # assumes that OpenSSL.crypto.dump_certificate includes ending - # newline character - return b"".join(_dump_cert(cert) for cert in chain) diff --git a/acme/acme/crypto_util_test.py b/acme/acme/crypto_util_test.py deleted file mode 100644 index 44b245bbed4..00000000000 --- a/acme/acme/crypto_util_test.py +++ /dev/null @@ -1,268 +0,0 @@ -"""Tests for acme.crypto_util.""" -import itertools -import socket -import threading -import time -import unittest - -import six -from six.moves import socketserver #type: ignore # pylint: disable=import-error - -import josepy as jose -import OpenSSL - -from acme import errors -from acme import test_util -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module - - -class SSLSocketAndProbeSNITest(unittest.TestCase): - """Tests for acme.crypto_util.SSLSocket/probe_sni.""" - - - def setUp(self): - self.cert = test_util.load_comparable_cert('rsa2048_cert.pem') - key = test_util.load_pyopenssl_private_key('rsa2048_key.pem') - # pylint: disable=protected-access - certs = {b'foo': (key, self.cert.wrapped)} - - from acme.crypto_util import SSLSocket - - class _TestServer(socketserver.TCPServer): - - # pylint: disable=too-few-public-methods - # six.moves.* | pylint: disable=attribute-defined-outside-init,no-init - - def server_bind(self): # pylint: disable=missing-docstring - self.socket = SSLSocket(socket.socket(), certs=certs) - socketserver.TCPServer.server_bind(self) - - self.server = _TestServer(('', 0), socketserver.BaseRequestHandler) - self.port = self.server.socket.getsockname()[1] - self.server_thread = threading.Thread( - # pylint: disable=no-member - target=self.server.handle_request) - - def tearDown(self): - if self.server_thread.is_alive(): - # The thread may have already terminated. - self.server_thread.join() # pragma: no cover - - def _probe(self, name): - from acme.crypto_util import probe_sni - return jose.ComparableX509(probe_sni( - name, host='127.0.0.1', port=self.port)) - - def _start_server(self): - self.server_thread.start() - time.sleep(1) # TODO: avoid race conditions in other way - - def test_probe_ok(self): - self._start_server() - self.assertEqual(self.cert, self._probe(b'foo')) - - def test_probe_not_recognized_name(self): - self._start_server() - self.assertRaises(errors.Error, self._probe, b'bar') - - def test_probe_connection_error(self): - # pylint has a hard time with six - self.server.server_close() # pylint: disable=no-member - original_timeout = socket.getdefaulttimeout() - try: - socket.setdefaulttimeout(1) - self.assertRaises(errors.Error, self._probe, b'bar') - finally: - socket.setdefaulttimeout(original_timeout) - - -class PyOpenSSLCertOrReqAllNamesTest(unittest.TestCase): - """Test for acme.crypto_util._pyopenssl_cert_or_req_all_names.""" - - @classmethod - def _call(cls, loader, name): - # pylint: disable=protected-access - from acme.crypto_util import _pyopenssl_cert_or_req_all_names - return _pyopenssl_cert_or_req_all_names(loader(name)) - - def _call_cert(self, name): - return self._call(test_util.load_cert, name) - - def test_cert_one_san_no_common(self): - self.assertEqual(self._call_cert('cert-nocn.der'), - ['no-common-name.badssl.com']) - - def test_cert_no_sans_yes_common(self): - self.assertEqual(self._call_cert('cert.pem'), ['example.com']) - - def test_cert_two_sans_yes_common(self): - self.assertEqual(self._call_cert('cert-san.pem'), - ['example.com', 'www.example.com']) - - -class PyOpenSSLCertOrReqSANTest(unittest.TestCase): - """Test for acme.crypto_util._pyopenssl_cert_or_req_san.""" - - - @classmethod - def _call(cls, loader, name): - # pylint: disable=protected-access - from acme.crypto_util import _pyopenssl_cert_or_req_san - return _pyopenssl_cert_or_req_san(loader(name)) - - @classmethod - def _get_idn_names(cls): - """Returns expected names from '{cert,csr}-idnsans.pem'.""" - chars = [six.unichr(i) for i in itertools.chain(range(0x3c3, 0x400), - range(0x641, 0x6fc), - range(0x1820, 0x1877))] - return [''.join(chars[i: i + 45]) + '.invalid' - for i in range(0, len(chars), 45)] - - def _call_cert(self, name): - return self._call(test_util.load_cert, name) - - def _call_csr(self, name): - return self._call(test_util.load_csr, name) - - def test_cert_no_sans(self): - self.assertEqual(self._call_cert('cert.pem'), []) - - def test_cert_two_sans(self): - self.assertEqual(self._call_cert('cert-san.pem'), - ['example.com', 'www.example.com']) - - def test_cert_hundred_sans(self): - self.assertEqual(self._call_cert('cert-100sans.pem'), - ['example{0}.com'.format(i) for i in range(1, 101)]) - - def test_cert_idn_sans(self): - self.assertEqual(self._call_cert('cert-idnsans.pem'), - self._get_idn_names()) - - def test_csr_no_sans(self): - self.assertEqual(self._call_csr('csr-nosans.pem'), []) - - def test_csr_one_san(self): - self.assertEqual(self._call_csr('csr.pem'), ['example.com']) - - def test_csr_two_sans(self): - self.assertEqual(self._call_csr('csr-san.pem'), - ['example.com', 'www.example.com']) - - def test_csr_six_sans(self): - self.assertEqual(self._call_csr('csr-6sans.pem'), - ['example.com', 'example.org', 'example.net', - 'example.info', 'subdomain.example.com', - 'other.subdomain.example.com']) - - def test_csr_hundred_sans(self): - self.assertEqual(self._call_csr('csr-100sans.pem'), - ['example{0}.com'.format(i) for i in range(1, 101)]) - - def test_csr_idn_sans(self): - self.assertEqual(self._call_csr('csr-idnsans.pem'), - self._get_idn_names()) - - def test_critical_san(self): - self.assertEqual(self._call_cert('critical-san.pem'), - ['chicago-cubs.venafi.example', 'cubs.venafi.example']) - - - -class RandomSnTest(unittest.TestCase): - """Test for random certificate serial numbers.""" - - - def setUp(self): - self.cert_count = 5 - self.serial_num = [] # type: List[int] - self.key = OpenSSL.crypto.PKey() - self.key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048) - - def test_sn_collisions(self): - from acme.crypto_util import gen_ss_cert - - for _ in range(self.cert_count): - cert = gen_ss_cert(self.key, ['dummy'], force_san=True) - self.serial_num.append(cert.get_serial_number()) - self.assertTrue(len(set(self.serial_num)) > 1) - -class MakeCSRTest(unittest.TestCase): - """Test for standalone functions.""" - - @classmethod - def _call_with_key(cls, *args, **kwargs): - privkey = OpenSSL.crypto.PKey() - privkey.generate_key(OpenSSL.crypto.TYPE_RSA, 2048) - privkey_pem = OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, privkey) - from acme.crypto_util import make_csr - return make_csr(privkey_pem, *args, **kwargs) - - def test_make_csr(self): - csr_pem = self._call_with_key(["a.example", "b.example"]) - self.assertTrue(b'--BEGIN CERTIFICATE REQUEST--' in csr_pem) - self.assertTrue(b'--END CERTIFICATE REQUEST--' in csr_pem) - csr = OpenSSL.crypto.load_certificate_request( - OpenSSL.crypto.FILETYPE_PEM, csr_pem) - # In pyopenssl 0.13 (used with TOXENV=py27-oldest), csr objects don't - # have a get_extensions() method, so we skip this test if the method - # isn't available. - if hasattr(csr, 'get_extensions'): - self.assertEqual(len(csr.get_extensions()), 1) - self.assertEqual(csr.get_extensions()[0].get_data(), - OpenSSL.crypto.X509Extension( - b'subjectAltName', - critical=False, - value=b'DNS:a.example, DNS:b.example', - ).get_data(), - ) - - def test_make_csr_must_staple(self): - csr_pem = self._call_with_key(["a.example"], must_staple=True) - csr = OpenSSL.crypto.load_certificate_request( - OpenSSL.crypto.FILETYPE_PEM, csr_pem) - - # In pyopenssl 0.13 (used with TOXENV=py27-oldest), csr objects don't - # have a get_extensions() method, so we skip this test if the method - # isn't available. - if hasattr(csr, 'get_extensions'): - self.assertEqual(len(csr.get_extensions()), 2) - # NOTE: Ideally we would filter by the TLS Feature OID, but - # OpenSSL.crypto.X509Extension doesn't give us the extension's raw OID, - # and the shortname field is just "UNDEF" - must_staple_exts = [e for e in csr.get_extensions() - if e.get_data() == b"0\x03\x02\x01\x05"] - self.assertEqual(len(must_staple_exts), 1, - "Expected exactly one Must Staple extension") - - -class DumpPyopensslChainTest(unittest.TestCase): - """Test for dump_pyopenssl_chain.""" - - @classmethod - def _call(cls, loaded): - # pylint: disable=protected-access - from acme.crypto_util import dump_pyopenssl_chain - return dump_pyopenssl_chain(loaded) - - def test_dump_pyopenssl_chain(self): - names = ['cert.pem', 'cert-san.pem', 'cert-idnsans.pem'] - loaded = [test_util.load_cert(name) for name in names] - length = sum( - len(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)) - for cert in loaded) - self.assertEqual(len(self._call(loaded)), length) - - def test_dump_pyopenssl_chain_wrapped(self): - names = ['cert.pem', 'cert-san.pem', 'cert-idnsans.pem'] - loaded = [test_util.load_cert(name) for name in names] - wrap_func = jose.ComparableX509 - wrapped = [wrap_func(cert) for cert in loaded] - dump_func = OpenSSL.crypto.dump_certificate - length = sum(len(dump_func(OpenSSL.crypto.FILETYPE_PEM, cert)) for cert in loaded) - self.assertEqual(len(self._call(wrapped)), length) - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/acme/acme/errors.py b/acme/acme/errors.py deleted file mode 100644 index 3a0f8c59671..00000000000 --- a/acme/acme/errors.py +++ /dev/null @@ -1,122 +0,0 @@ -"""ACME errors.""" -from josepy import errors as jose_errors - - -class Error(Exception): - """Generic ACME error.""" - - -class DependencyError(Error): - """Dependency error""" - - -class SchemaValidationError(jose_errors.DeserializationError): - """JSON schema ACME object validation error.""" - - -class ClientError(Error): - """Network error.""" - - -class UnexpectedUpdate(ClientError): - """Unexpected update error.""" - - -class NonceError(ClientError): - """Server response nonce error.""" - - -class BadNonce(NonceError): - """Bad nonce error.""" - def __init__(self, nonce, error, *args, **kwargs): - super(BadNonce, self).__init__(*args, **kwargs) - self.nonce = nonce - self.error = error - - def __str__(self): - return 'Invalid nonce ({0!r}): {1}'.format(self.nonce, self.error) - - -class MissingNonce(NonceError): - """Missing nonce error. - - According to the specification an "ACME server MUST include an - Replay-Nonce header field in each successful response to a POST it - provides to a client (...)". - - :ivar requests.Response response: HTTP Response - - """ - def __init__(self, response, *args, **kwargs): - super(MissingNonce, self).__init__(*args, **kwargs) - self.response = response - - def __str__(self): - return ('Server {0} response did not include a replay ' - 'nonce, headers: {1} (This may be a service outage)'.format( - self.response.request.method, self.response.headers)) - - -class PollError(ClientError): - """Generic error when polling for authorization fails. - - This might be caused by either timeout (`exhausted` will be non-empty) - or by some authorization being invalid. - - :ivar exhausted: Set of `.AuthorizationResource` that didn't finish - within max allowed attempts. - :ivar updated: Mapping from original `.AuthorizationResource` - to the most recently updated one - - """ - def __init__(self, exhausted, updated): - self.exhausted = exhausted - self.updated = updated - super(PollError, self).__init__() - - @property - def timeout(self): - """Was the error caused by timeout?""" - return bool(self.exhausted) - - def __repr__(self): - return '{0}(exhausted={1!r}, updated={2!r})'.format( - self.__class__.__name__, self.exhausted, self.updated) - -class ValidationError(Error): - """Error for authorization failures. Contains a list of authorization - resources, each of which is invalid and should have an error field. - """ - def __init__(self, failed_authzrs): - self.failed_authzrs = failed_authzrs - super(ValidationError, self).__init__() - -class TimeoutError(Error): - """Error for when polling an authorization or an order times out.""" - -class IssuanceError(Error): - """Error sent by the server after requesting issuance of a certificate.""" - - def __init__(self, error): - """Initialize. - - :param messages.Error error: The error provided by the server. - """ - self.error = error - super(IssuanceError, self).__init__() - -class ConflictError(ClientError): - """Error for when the server returns a 409 (Conflict) HTTP status. - - In the version of ACME implemented by Boulder, this is used to find an - account if you only have the private key, but don't know the account URL. - - Also used in V2 of the ACME client for the same purpose. - """ - def __init__(self, location): - self.location = location - super(ConflictError, self).__init__() - - -class WildcardUnsupportedError(Error): - """Error for when a wildcard is requested but is unsupported by ACME CA.""" diff --git a/acme/acme/errors_test.py b/acme/acme/errors_test.py deleted file mode 100644 index 1e5f3d47927..00000000000 --- a/acme/acme/errors_test.py +++ /dev/null @@ -1,53 +0,0 @@ -"""Tests for acme.errors.""" -import unittest - -import mock - - -class BadNonceTest(unittest.TestCase): - """Tests for acme.errors.BadNonce.""" - - def setUp(self): - from acme.errors import BadNonce - self.error = BadNonce(nonce="xxx", error="error") - - def test_str(self): - self.assertEqual("Invalid nonce ('xxx'): error", str(self.error)) - - -class MissingNonceTest(unittest.TestCase): - """Tests for acme.errors.MissingNonce.""" - - def setUp(self): - from acme.errors import MissingNonce - self.response = mock.MagicMock(headers={}) - self.response.request.method = 'FOO' - self.error = MissingNonce(self.response) - - def test_str(self): - self.assertTrue("FOO" in str(self.error)) - self.assertTrue("{}" in str(self.error)) - - -class PollErrorTest(unittest.TestCase): - """Tests for acme.errors.PollError.""" - - def setUp(self): - from acme.errors import PollError - self.timeout = PollError( - exhausted=set([mock.sentinel.AR]), - updated={}) - self.invalid = PollError(exhausted=set(), updated={ - mock.sentinel.AR: mock.sentinel.AR2}) - - def test_timeout(self): - self.assertTrue(self.timeout.timeout) - self.assertFalse(self.invalid.timeout) - - def test_repr(self): - self.assertEqual('PollError(exhausted=%s, updated={sentinel.AR: ' - 'sentinel.AR2})' % repr(set()), repr(self.invalid)) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/acme/acme/fields.py b/acme/acme/fields.py deleted file mode 100644 index d7ec78403ed..00000000000 --- a/acme/acme/fields.py +++ /dev/null @@ -1,65 +0,0 @@ -"""ACME JSON fields.""" -import logging - -import josepy as jose -import pyrfc3339 - - -logger = logging.getLogger(__name__) - - -class Fixed(jose.Field): - """Fixed field.""" - - def __init__(self, json_name, value): - self.value = value - super(Fixed, self).__init__( - json_name=json_name, default=value, omitempty=False) - - def decode(self, value): - if value != self.value: - raise jose.DeserializationError('Expected {0!r}'.format(self.value)) - return self.value - - def encode(self, value): - if value != self.value: - logger.warning( - 'Overriding fixed field (%s) with %r', self.json_name, value) - return value - - -class RFC3339Field(jose.Field): - """RFC3339 field encoder/decoder. - - Handles decoding/encoding between RFC3339 strings and aware (not - naive) `datetime.datetime` objects - (e.g. ``datetime.datetime.now(pytz.utc)``). - - """ - - @classmethod - def default_encoder(cls, value): - return pyrfc3339.generate(value) - - @classmethod - def default_decoder(cls, value): - try: - return pyrfc3339.parse(value) - except ValueError as error: - raise jose.DeserializationError(error) - - -class Resource(jose.Field): - """Resource MITM field.""" - - def __init__(self, resource_type, *args, **kwargs): - self.resource_type = resource_type - super(Resource, self).__init__( - 'resource', default=resource_type, *args, **kwargs) - - def decode(self, value): - if value != self.resource_type: - raise jose.DeserializationError( - 'Wrong resource type: {0} instead of {1}'.format( - value, self.resource_type)) - return value diff --git a/acme/acme/fields_test.py b/acme/acme/fields_test.py deleted file mode 100644 index 69dde8b89bc..00000000000 --- a/acme/acme/fields_test.py +++ /dev/null @@ -1,72 +0,0 @@ -"""Tests for acme.fields.""" -import datetime -import unittest - -import josepy as jose -import pytz - - -class FixedTest(unittest.TestCase): - """Tests for acme.fields.Fixed.""" - - def setUp(self): - from acme.fields import Fixed - self.field = Fixed('name', 'x') - - def test_decode(self): - self.assertEqual('x', self.field.decode('x')) - - def test_decode_bad(self): - self.assertRaises(jose.DeserializationError, self.field.decode, 'y') - - def test_encode(self): - self.assertEqual('x', self.field.encode('x')) - - def test_encode_override(self): - self.assertEqual('y', self.field.encode('y')) - - -class RFC3339FieldTest(unittest.TestCase): - """Tests for acme.fields.RFC3339Field.""" - - def setUp(self): - self.decoded = datetime.datetime(2015, 3, 27, tzinfo=pytz.utc) - self.encoded = '2015-03-27T00:00:00Z' - - def test_default_encoder(self): - from acme.fields import RFC3339Field - self.assertEqual( - self.encoded, RFC3339Field.default_encoder(self.decoded)) - - def test_default_encoder_naive_fails(self): - from acme.fields import RFC3339Field - self.assertRaises( - ValueError, RFC3339Field.default_encoder, datetime.datetime.now()) - - def test_default_decoder(self): - from acme.fields import RFC3339Field - self.assertEqual( - self.decoded, RFC3339Field.default_decoder(self.encoded)) - - def test_default_decoder_raises_deserialization_error(self): - from acme.fields import RFC3339Field - self.assertRaises( - jose.DeserializationError, RFC3339Field.default_decoder, '') - - -class ResourceTest(unittest.TestCase): - """Tests for acme.fields.Resource.""" - - def setUp(self): - from acme.fields import Resource - self.field = Resource('x') - - def test_decode_good(self): - self.assertEqual('x', self.field.decode('x')) - - def test_decode_wrong(self): - self.assertRaises(jose.DeserializationError, self.field.decode, 'y') - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/acme/acme/jose_test.py b/acme/acme/jose_test.py deleted file mode 100644 index 340624a4f81..00000000000 --- a/acme/acme/jose_test.py +++ /dev/null @@ -1,53 +0,0 @@ -"""Tests for acme.jose shim.""" -import importlib -import unittest - -class JoseTest(unittest.TestCase): - """Tests for acme.jose shim.""" - - def _test_it(self, submodule, attribute): - if submodule: - acme_jose_path = 'acme.jose.' + submodule - josepy_path = 'josepy.' + submodule - else: - acme_jose_path = 'acme.jose' - josepy_path = 'josepy' - acme_jose_mod = importlib.import_module(acme_jose_path) - josepy_mod = importlib.import_module(josepy_path) - - self.assertIs(acme_jose_mod, josepy_mod) - self.assertIs(getattr(acme_jose_mod, attribute), getattr(josepy_mod, attribute)) - - # We use the imports below with eval, but pylint doesn't - # understand that. - # pylint: disable=eval-used,unused-variable - import acme - import josepy - acme_jose_mod = eval(acme_jose_path) - josepy_mod = eval(josepy_path) - self.assertIs(acme_jose_mod, josepy_mod) - self.assertIs(getattr(acme_jose_mod, attribute), getattr(josepy_mod, attribute)) - - def test_top_level(self): - self._test_it('', 'RS512') - - def test_submodules(self): - # This test ensures that the modules in josepy that were - # available at the time it was moved into its own package are - # available under acme.jose. Backwards compatibility with new - # modules or testing code is not maintained. - mods_and_attrs = [('b64', 'b64decode',), - ('errors', 'Error',), - ('interfaces', 'JSONDeSerializable',), - ('json_util', 'Field',), - ('jwa', 'HS256',), - ('jwk', 'JWK',), - ('jws', 'JWS',), - ('util', 'ImmutableMap',),] - - for mod, attr in mods_and_attrs: - self._test_it(mod, attr) - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/acme/acme/jws.py b/acme/acme/jws.py deleted file mode 100644 index c92d226d477..00000000000 --- a/acme/acme/jws.py +++ /dev/null @@ -1,54 +0,0 @@ -"""ACME-specific JWS. - -The JWS implementation in josepy only implements the base JOSE standard. In -order to support the new header fields defined in ACME, this module defines some -ACME-specific classes that layer on top of josepy. -""" -import josepy as jose - - -class Header(jose.Header): - """ACME-specific JOSE Header. Implements nonce, kid, and url. - """ - nonce = jose.Field('nonce', omitempty=True, encoder=jose.encode_b64jose) - kid = jose.Field('kid', omitempty=True) - url = jose.Field('url', omitempty=True) - - @nonce.decoder - def nonce(value): # pylint: disable=missing-docstring,no-self-argument - try: - return jose.decode_b64jose(value) - except jose.DeserializationError as error: - # TODO: custom error - raise jose.DeserializationError("Invalid nonce: {0}".format(error)) - - -class Signature(jose.Signature): - """ACME-specific Signature. Uses ACME-specific Header for customer fields.""" - __slots__ = jose.Signature._orig_slots # pylint: disable=no-member - - # TODO: decoder/encoder should accept cls? Otherwise, subclassing - # JSONObjectWithFields is tricky... - header_cls = Header - header = jose.Field( - 'header', omitempty=True, default=header_cls(), - decoder=header_cls.from_json) - - # TODO: decoder should check that nonce is in the protected header - - -class JWS(jose.JWS): - """ACME-specific JWS. Includes none, url, and kid in protected header.""" - signature_cls = Signature - __slots__ = jose.JWS._orig_slots # pylint: disable=no-member - - @classmethod - # pylint: disable=arguments-differ,too-many-arguments - def sign(cls, payload, key, alg, nonce, url=None, kid=None): - # Per ACME spec, jwk and kid are mutually exclusive, so only include a - # jwk field if kid is not provided. - include_jwk = kid is None - return super(JWS, cls).sign(payload, key=key, alg=alg, - protect=frozenset(['nonce', 'url', 'kid', 'jwk', 'alg']), - nonce=nonce, url=url, kid=kid, - include_jwk=include_jwk) diff --git a/acme/acme/jws_test.py b/acme/acme/jws_test.py deleted file mode 100644 index aa3ccb700d9..00000000000 --- a/acme/acme/jws_test.py +++ /dev/null @@ -1,67 +0,0 @@ -"""Tests for acme.jws.""" -import unittest - -import josepy as jose - -from acme import test_util - - -KEY = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem')) - - -class HeaderTest(unittest.TestCase): - """Tests for acme.jws.Header.""" - - good_nonce = jose.encode_b64jose(b'foo') - wrong_nonce = u'F' - # Following just makes sure wrong_nonce is wrong - try: - jose.b64decode(wrong_nonce) - except (ValueError, TypeError): - assert True - else: - assert False # pragma: no cover - - def test_nonce_decoder(self): - from acme.jws import Header - nonce_field = Header._fields['nonce'] - - self.assertRaises( - jose.DeserializationError, nonce_field.decode, self.wrong_nonce) - self.assertEqual(b'foo', nonce_field.decode(self.good_nonce)) - - -class JWSTest(unittest.TestCase): - """Tests for acme.jws.JWS.""" - - def setUp(self): - self.privkey = KEY - self.pubkey = self.privkey.public_key() - self.nonce = jose.b64encode(b'Nonce') - self.url = 'hi' - self.kid = 'baaaaa' - - def test_kid_serialize(self): - from acme.jws import JWS - jws = JWS.sign(payload=b'foo', key=self.privkey, - alg=jose.RS256, nonce=self.nonce, - url=self.url, kid=self.kid) - self.assertEqual(jws.signature.combined.nonce, self.nonce) - self.assertEqual(jws.signature.combined.url, self.url) - self.assertEqual(jws.signature.combined.kid, self.kid) - self.assertEqual(jws.signature.combined.jwk, None) - # TODO: check that nonce is in protected header - - self.assertEqual(jws, JWS.from_json(jws.to_json())) - - def test_jwk_serialize(self): - from acme.jws import JWS - jws = JWS.sign(payload=b'foo', key=self.privkey, - alg=jose.RS256, nonce=self.nonce, - url=self.url) - self.assertEqual(jws.signature.combined.kid, None) - self.assertEqual(jws.signature.combined.jwk, self.pubkey) - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/acme/acme/magic_typing.py b/acme/acme/magic_typing.py deleted file mode 100644 index 471b8dfa95c..00000000000 --- a/acme/acme/magic_typing.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Shim class to not have to depend on typing module in prod.""" -import sys - -class TypingClass(object): - """Ignore import errors by getting anything""" - def __getattr__(self, name): - return None - -try: - # mypy doesn't respect modifying sys.modules - from typing import * # pylint: disable=wildcard-import, unused-wildcard-import - # pylint: disable=unused-import - from typing import Collection, IO # type: ignore - # pylint: enable=unused-import -except ImportError: - sys.modules[__name__] = TypingClass() diff --git a/acme/acme/magic_typing_test.py b/acme/acme/magic_typing_test.py deleted file mode 100644 index 23dfe33676a..00000000000 --- a/acme/acme/magic_typing_test.py +++ /dev/null @@ -1,41 +0,0 @@ -"""Tests for acme.magic_typing.""" -import sys -import unittest - -import mock - - -class MagicTypingTest(unittest.TestCase): - """Tests for acme.magic_typing.""" - def test_import_success(self): - try: - import typing as temp_typing - except ImportError: # pragma: no cover - temp_typing = None # pragma: no cover - typing_class_mock = mock.MagicMock() - text_mock = mock.MagicMock() - typing_class_mock.Text = text_mock - sys.modules['typing'] = typing_class_mock - if 'acme.magic_typing' in sys.modules: - del sys.modules['acme.magic_typing'] # pragma: no cover - from acme.magic_typing import Text # pylint: disable=no-name-in-module - self.assertEqual(Text, text_mock) - del sys.modules['acme.magic_typing'] - sys.modules['typing'] = temp_typing - - def test_import_failure(self): - try: - import typing as temp_typing - except ImportError: # pragma: no cover - temp_typing = None # pragma: no cover - sys.modules['typing'] = None - if 'acme.magic_typing' in sys.modules: - del sys.modules['acme.magic_typing'] # pragma: no cover - from acme.magic_typing import Text # pylint: disable=no-name-in-module - self.assertTrue(Text is None) - del sys.modules['acme.magic_typing'] - sys.modules['typing'] = temp_typing - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/acme/acme/messages.py b/acme/acme/messages.py deleted file mode 100644 index 4400a6c318a..00000000000 --- a/acme/acme/messages.py +++ /dev/null @@ -1,582 +0,0 @@ -"""ACME protocol messages.""" -import collections -import six -import json - -import josepy as jose - -from acme import challenges -from acme import errors -from acme import fields -from acme import util -from acme import jws - -OLD_ERROR_PREFIX = "urn:acme:error:" -ERROR_PREFIX = "urn:ietf:params:acme:error:" - -ERROR_CODES = { - 'badCSR': 'The CSR is unacceptable (e.g., due to a short key)', - 'badNonce': 'The client sent an unacceptable anti-replay nonce', - 'connection': ('The server could not connect to the client to verify the' - ' domain'), - 'dnssec': 'The server could not validate a DNSSEC signed domain', - # deprecate invalidEmail - 'invalidEmail': 'The provided email for a registration was invalid', - 'invalidContact': 'The provided contact URI was invalid', - 'malformed': 'The request message was malformed', - 'rateLimited': 'There were too many requests of a given type', - 'serverInternal': 'The server experienced an internal error', - 'tls': 'The server experienced a TLS error during domain verification', - 'unauthorized': 'The client lacks sufficient authorization', - 'unknownHost': 'The server could not resolve a domain name', - 'externalAccountRequired': 'The server requires external account binding', -} - -ERROR_TYPE_DESCRIPTIONS = dict( - (ERROR_PREFIX + name, desc) for name, desc in ERROR_CODES.items()) - -ERROR_TYPE_DESCRIPTIONS.update(dict( # add errors with old prefix, deprecate me - (OLD_ERROR_PREFIX + name, desc) for name, desc in ERROR_CODES.items())) - - -def is_acme_error(err): - """Check if argument is an ACME error.""" - if isinstance(err, Error) and (err.typ is not None): - return (ERROR_PREFIX in err.typ) or (OLD_ERROR_PREFIX in err.typ) - else: - return False - - -@six.python_2_unicode_compatible -class Error(jose.JSONObjectWithFields, errors.Error): - """ACME error. - - https://tools.ietf.org/html/draft-ietf-appsawg-http-problem-00 - - :ivar unicode typ: - :ivar unicode title: - :ivar unicode detail: - - """ - typ = jose.Field('type', omitempty=True, default='about:blank') - title = jose.Field('title', omitempty=True) - detail = jose.Field('detail', omitempty=True) - - @classmethod - def with_code(cls, code, **kwargs): - """Create an Error instance with an ACME Error code. - - :unicode code: An ACME error code, like 'dnssec'. - :kwargs: kwargs to pass to Error. - - """ - if code not in ERROR_CODES: - raise ValueError("The supplied code: %s is not a known ACME error" - " code" % code) - typ = ERROR_PREFIX + code - return cls(typ=typ, **kwargs) - - @property - def description(self): - """Hardcoded error description based on its type. - - :returns: Description if standard ACME error or ``None``. - :rtype: unicode - - """ - return ERROR_TYPE_DESCRIPTIONS.get(self.typ) - - @property - def code(self): - """ACME error code. - - Basically self.typ without the ERROR_PREFIX. - - :returns: error code if standard ACME code or ``None``. - :rtype: unicode - - """ - code = str(self.typ).split(':')[-1] - if code in ERROR_CODES: - return code - - def __str__(self): - return b' :: '.join( - part.encode('ascii', 'backslashreplace') for part in - (self.typ, self.description, self.detail, self.title) - if part is not None).decode() - - -class _Constant(jose.JSONDeSerializable, collections.Hashable): # type: ignore - """ACME constant.""" - __slots__ = ('name',) - POSSIBLE_NAMES = NotImplemented - - def __init__(self, name): - self.POSSIBLE_NAMES[name] = self - self.name = name - - def to_partial_json(self): - return self.name - - @classmethod - def from_json(cls, value): - if value not in cls.POSSIBLE_NAMES: - raise jose.DeserializationError( - '{0} not recognized'.format(cls.__name__)) - return cls.POSSIBLE_NAMES[value] - - def __repr__(self): - return '{0}({1})'.format(self.__class__.__name__, self.name) - - def __eq__(self, other): - return isinstance(other, type(self)) and other.name == self.name - - def __hash__(self): - return hash((self.__class__, self.name)) - - def __ne__(self, other): - return not self == other - - -class Status(_Constant): - """ACME "status" field.""" - POSSIBLE_NAMES = {} # type: dict -STATUS_UNKNOWN = Status('unknown') -STATUS_PENDING = Status('pending') -STATUS_PROCESSING = Status('processing') -STATUS_VALID = Status('valid') -STATUS_INVALID = Status('invalid') -STATUS_REVOKED = Status('revoked') -STATUS_READY = Status('ready') - - -class IdentifierType(_Constant): - """ACME identifier type.""" - POSSIBLE_NAMES = {} # type: dict -IDENTIFIER_FQDN = IdentifierType('dns') # IdentifierDNS in Boulder - - -class Identifier(jose.JSONObjectWithFields): - """ACME identifier. - - :ivar IdentifierType typ: - :ivar unicode value: - - """ - typ = jose.Field('type', decoder=IdentifierType.from_json) - value = jose.Field('value') - - -class Directory(jose.JSONDeSerializable): - """Directory.""" - - _REGISTERED_TYPES = {} # type: dict - - class Meta(jose.JSONObjectWithFields): - """Directory Meta.""" - _terms_of_service = jose.Field('terms-of-service', omitempty=True) - _terms_of_service_v2 = jose.Field('termsOfService', omitempty=True) - website = jose.Field('website', omitempty=True) - caa_identities = jose.Field('caaIdentities', omitempty=True) - external_account_required = jose.Field('externalAccountRequired', omitempty=True) - - def __init__(self, **kwargs): - kwargs = dict((self._internal_name(k), v) for k, v in kwargs.items()) - # pylint: disable=star-args - super(Directory.Meta, self).__init__(**kwargs) - - @property - def terms_of_service(self): - """URL for the CA TOS""" - return self._terms_of_service or self._terms_of_service_v2 - - def __iter__(self): - # When iterating over fields, use the external name 'terms_of_service' instead of - # the internal '_terms_of_service'. - for name in super(Directory.Meta, self).__iter__(): - yield name[1:] if name == '_terms_of_service' else name - - def _internal_name(self, name): - return '_' + name if name == 'terms_of_service' else name - - - @classmethod - def _canon_key(cls, key): - return getattr(key, 'resource_type', key) - - @classmethod - def register(cls, resource_body_cls): - """Register resource.""" - resource_type = resource_body_cls.resource_type - assert resource_type not in cls._REGISTERED_TYPES - cls._REGISTERED_TYPES[resource_type] = resource_body_cls - return resource_body_cls - - def __init__(self, jobj): - canon_jobj = util.map_keys(jobj, self._canon_key) - # TODO: check that everything is an absolute URL; acme-spec is - # not clear on that - self._jobj = canon_jobj - - def __getattr__(self, name): - try: - return self[name.replace('_', '-')] - except KeyError as error: - raise AttributeError(str(error) + ': ' + name) - - def __getitem__(self, name): - try: - return self._jobj[self._canon_key(name)] - except KeyError: - raise KeyError('Directory field not found') - - def to_partial_json(self): - return self._jobj - - @classmethod - def from_json(cls, jobj): - jobj['meta'] = cls.Meta.from_json(jobj.pop('meta', {})) - return cls(jobj) - - -class Resource(jose.JSONObjectWithFields): - """ACME Resource. - - :ivar acme.messages.ResourceBody body: Resource body. - - """ - body = jose.Field('body') - - -class ResourceWithURI(Resource): - """ACME Resource with URI. - - :ivar unicode uri: Location of the resource. - - """ - uri = jose.Field('uri') # no ChallengeResource.uri - - -class ResourceBody(jose.JSONObjectWithFields): - """ACME Resource Body.""" - - -class ExternalAccountBinding(object): - """ACME External Account Binding""" - - @classmethod - def from_data(cls, account_public_key, kid, hmac_key, directory): - """Create External Account Binding Resource from contact details, kid and hmac.""" - - key_json = json.dumps(account_public_key.to_partial_json()).encode() - decoded_hmac_key = jose.b64.b64decode(hmac_key) - url = directory["newAccount"] - - eab = jws.JWS.sign(key_json, jose.jwk.JWKOct(key=decoded_hmac_key), - jose.jwa.HS256, None, - url, kid) - - return eab.to_partial_json() - - -class Registration(ResourceBody): - """Registration Resource Body. - - :ivar josepy.jwk.JWK key: Public key. - :ivar tuple contact: Contact information following ACME spec, - `tuple` of `unicode`. - :ivar unicode agreement: - - """ - # on new-reg key server ignores 'key' and populates it based on - # JWS.signature.combined.jwk - key = jose.Field('key', omitempty=True, decoder=jose.JWK.from_json) - contact = jose.Field('contact', omitempty=True, default=()) - agreement = jose.Field('agreement', omitempty=True) - status = jose.Field('status', omitempty=True) - terms_of_service_agreed = jose.Field('termsOfServiceAgreed', omitempty=True) - only_return_existing = jose.Field('onlyReturnExisting', omitempty=True) - external_account_binding = jose.Field('externalAccountBinding', omitempty=True) - - phone_prefix = 'tel:' - email_prefix = 'mailto:' - - @classmethod - def from_data(cls, phone=None, email=None, external_account_binding=None, **kwargs): - """Create registration resource from contact details.""" - details = list(kwargs.pop('contact', ())) - if phone is not None: - details.append(cls.phone_prefix + phone) - if email is not None: - details.extend([cls.email_prefix + mail for mail in email.split(',')]) - kwargs['contact'] = tuple(details) - - if external_account_binding: - kwargs['external_account_binding'] = external_account_binding - - return cls(**kwargs) - - def _filter_contact(self, prefix): - return tuple( - detail[len(prefix):] for detail in self.contact - if detail.startswith(prefix)) - - @property - def phones(self): - """All phones found in the ``contact`` field.""" - return self._filter_contact(self.phone_prefix) - - @property - def emails(self): - """All emails found in the ``contact`` field.""" - return self._filter_contact(self.email_prefix) - - -@Directory.register -class NewRegistration(Registration): - """New registration.""" - resource_type = 'new-reg' - resource = fields.Resource(resource_type) - - -class UpdateRegistration(Registration): - """Update registration.""" - resource_type = 'reg' - resource = fields.Resource(resource_type) - - -class RegistrationResource(ResourceWithURI): - """Registration Resource. - - :ivar acme.messages.Registration body: - :ivar unicode new_authzr_uri: Deprecated. Do not use. - :ivar unicode terms_of_service: URL for the CA TOS. - - """ - body = jose.Field('body', decoder=Registration.from_json) - new_authzr_uri = jose.Field('new_authzr_uri', omitempty=True) - terms_of_service = jose.Field('terms_of_service', omitempty=True) - - -class ChallengeBody(ResourceBody): - """Challenge Resource Body. - - .. todo:: - Confusingly, this has a similar name to `.challenges.Challenge`, - as well as `.achallenges.AnnotatedChallenge`. Please use names - such as ``challb`` to distinguish instances of this class from - ``achall``. - - :ivar acme.challenges.Challenge: Wrapped challenge. - Conveniently, all challenge fields are proxied, i.e. you can - call ``challb.x`` to get ``challb.chall.x`` contents. - :ivar acme.messages.Status status: - :ivar datetime.datetime validated: - :ivar messages.Error error: - - """ - __slots__ = ('chall',) - # ACMEv1 has a "uri" field in challenges. ACMEv2 has a "url" field. This - # challenge object supports either one, but should be accessed through the - # name "uri". In Client.answer_challenge, whichever one is set will be - # used. - _uri = jose.Field('uri', omitempty=True, default=None) - _url = jose.Field('url', omitempty=True, default=None) - status = jose.Field('status', decoder=Status.from_json, - omitempty=True, default=STATUS_PENDING) - validated = fields.RFC3339Field('validated', omitempty=True) - error = jose.Field('error', decoder=Error.from_json, - omitempty=True, default=None) - - def __init__(self, **kwargs): - kwargs = dict((self._internal_name(k), v) for k, v in kwargs.items()) - # pylint: disable=star-args - super(ChallengeBody, self).__init__(**kwargs) - - def encode(self, name): - return super(ChallengeBody, self).encode(self._internal_name(name)) - - def to_partial_json(self): - jobj = super(ChallengeBody, self).to_partial_json() - jobj.update(self.chall.to_partial_json()) - return jobj - - @classmethod - def fields_from_json(cls, jobj): - jobj_fields = super(ChallengeBody, cls).fields_from_json(jobj) - jobj_fields['chall'] = challenges.Challenge.from_json(jobj) - return jobj_fields - - @property - def uri(self): - """The URL of this challenge.""" - return self._url or self._uri - - def __getattr__(self, name): - return getattr(self.chall, name) - - def __iter__(self): - # When iterating over fields, use the external name 'uri' instead of - # the internal '_uri'. - for name in super(ChallengeBody, self).__iter__(): - yield name[1:] if name == '_uri' else name - - def _internal_name(self, name): - return '_' + name if name == 'uri' else name - - -class ChallengeResource(Resource): - """Challenge Resource. - - :ivar acme.messages.ChallengeBody body: - :ivar unicode authzr_uri: URI found in the 'up' ``Link`` header. - - """ - body = jose.Field('body', decoder=ChallengeBody.from_json) - authzr_uri = jose.Field('authzr_uri') - - @property - def uri(self): - """The URL of the challenge body.""" - # pylint: disable=function-redefined,no-member - return self.body.uri - - -class Authorization(ResourceBody): - """Authorization Resource Body. - - :ivar acme.messages.Identifier identifier: - :ivar list challenges: `list` of `.ChallengeBody` - :ivar tuple combinations: Challenge combinations (`tuple` of `tuple` - of `int`, as opposed to `list` of `list` from the spec). - :ivar acme.messages.Status status: - :ivar datetime.datetime expires: - - """ - identifier = jose.Field('identifier', decoder=Identifier.from_json) - challenges = jose.Field('challenges', omitempty=True) - combinations = jose.Field('combinations', omitempty=True) - - status = jose.Field('status', omitempty=True, decoder=Status.from_json) - # TODO: 'expires' is allowed for Authorization Resources in - # general, but for Key Authorization '[t]he "expires" field MUST - # be absent'... then acme-spec gives example with 'expires' - # present... That's confusing! - expires = fields.RFC3339Field('expires', omitempty=True) - wildcard = jose.Field('wildcard', omitempty=True) - - @challenges.decoder - def challenges(value): # pylint: disable=missing-docstring,no-self-argument - return tuple(ChallengeBody.from_json(chall) for chall in value) - - @property - def resolved_combinations(self): - """Combinations with challenges instead of indices.""" - return tuple(tuple(self.challenges[idx] for idx in combo) - for combo in self.combinations) - - -@Directory.register -class NewAuthorization(Authorization): - """New authorization.""" - resource_type = 'new-authz' - resource = fields.Resource(resource_type) - - -class AuthorizationResource(ResourceWithURI): - """Authorization Resource. - - :ivar acme.messages.Authorization body: - :ivar unicode new_cert_uri: Deprecated. Do not use. - - """ - body = jose.Field('body', decoder=Authorization.from_json) - new_cert_uri = jose.Field('new_cert_uri', omitempty=True) - - -@Directory.register -class CertificateRequest(jose.JSONObjectWithFields): - """ACME new-cert request. - - :ivar josepy.util.ComparableX509 csr: - `OpenSSL.crypto.X509Req` wrapped in `.ComparableX509` - - """ - resource_type = 'new-cert' - resource = fields.Resource(resource_type) - csr = jose.Field('csr', decoder=jose.decode_csr, encoder=jose.encode_csr) - - -class CertificateResource(ResourceWithURI): - """Certificate Resource. - - :ivar josepy.util.ComparableX509 body: - `OpenSSL.crypto.X509` wrapped in `.ComparableX509` - :ivar unicode cert_chain_uri: URI found in the 'up' ``Link`` header - :ivar tuple authzrs: `tuple` of `AuthorizationResource`. - - """ - cert_chain_uri = jose.Field('cert_chain_uri') - authzrs = jose.Field('authzrs') - - -@Directory.register -class Revocation(jose.JSONObjectWithFields): - """Revocation message. - - :ivar .ComparableX509 certificate: `OpenSSL.crypto.X509` wrapped in - `.ComparableX509` - - """ - resource_type = 'revoke-cert' - resource = fields.Resource(resource_type) - certificate = jose.Field( - 'certificate', decoder=jose.decode_cert, encoder=jose.encode_cert) - reason = jose.Field('reason') - - -class Order(ResourceBody): - """Order Resource Body. - - :ivar list of .Identifier: List of identifiers for the certificate. - :ivar acme.messages.Status status: - :ivar list of str authorizations: URLs of authorizations. - :ivar str certificate: URL to download certificate as a fullchain PEM. - :ivar str finalize: URL to POST to to request issuance once all - authorizations have "valid" status. - :ivar datetime.datetime expires: When the order expires. - :ivar .Error error: Any error that occurred during finalization, if applicable. - """ - identifiers = jose.Field('identifiers', omitempty=True) - status = jose.Field('status', decoder=Status.from_json, - omitempty=True) - authorizations = jose.Field('authorizations', omitempty=True) - certificate = jose.Field('certificate', omitempty=True) - finalize = jose.Field('finalize', omitempty=True) - expires = fields.RFC3339Field('expires', omitempty=True) - error = jose.Field('error', omitempty=True, decoder=Error.from_json) - - @identifiers.decoder - def identifiers(value): # pylint: disable=missing-docstring,no-self-argument - return tuple(Identifier.from_json(identifier) for identifier in value) - -class OrderResource(ResourceWithURI): - """Order Resource. - - :ivar acme.messages.Order body: - :ivar str csr_pem: The CSR this Order will be finalized with. - :ivar list of acme.messages.AuthorizationResource authorizations: - Fully-fetched AuthorizationResource objects. - :ivar str fullchain_pem: The fetched contents of the certificate URL - produced once the order was finalized, if it's present. - """ - body = jose.Field('body', decoder=Order.from_json) - csr_pem = jose.Field('csr_pem', omitempty=True) - authorizations = jose.Field('authorizations') - fullchain_pem = jose.Field('fullchain_pem', omitempty=True) - -@Directory.register -class NewOrder(Order): - """New order.""" - resource_type = 'new-order' diff --git a/acme/acme/messages_test.py b/acme/acme/messages_test.py deleted file mode 100644 index 7efaaa1a3a2..00000000000 --- a/acme/acme/messages_test.py +++ /dev/null @@ -1,476 +0,0 @@ -"""Tests for acme.messages.""" -import unittest - -import josepy as jose -import mock - -from acme import challenges -from acme import test_util -from acme.magic_typing import Dict # pylint: disable=unused-import, no-name-in-module - - -CERT = test_util.load_comparable_cert('cert.der') -CSR = test_util.load_comparable_csr('csr.der') -KEY = test_util.load_rsa_private_key('rsa512_key.pem') - - -class ErrorTest(unittest.TestCase): - """Tests for acme.messages.Error.""" - - def setUp(self): - from acme.messages import Error, ERROR_PREFIX - self.error = Error( - detail='foo', typ=ERROR_PREFIX + 'malformed', title='title') - self.jobj = { - 'detail': 'foo', - 'title': 'some title', - 'type': ERROR_PREFIX + 'malformed', - } - self.error_custom = Error(typ='custom', detail='bar') - self.empty_error = Error() - self.jobj_custom = {'type': 'custom', 'detail': 'bar'} - - def test_default_typ(self): - from acme.messages import Error - self.assertEqual(Error().typ, 'about:blank') - - def test_from_json_empty(self): - from acme.messages import Error - self.assertEqual(Error(), Error.from_json('{}')) - - def test_from_json_hashable(self): - from acme.messages import Error - hash(Error.from_json(self.error.to_json())) - - def test_description(self): - self.assertEqual( - 'The request message was malformed', self.error.description) - self.assertTrue(self.error_custom.description is None) - - def test_code(self): - from acme.messages import Error - self.assertEqual('malformed', self.error.code) - self.assertEqual(None, self.error_custom.code) - self.assertEqual(None, Error().code) - - def test_is_acme_error(self): - from acme.messages import is_acme_error - self.assertTrue(is_acme_error(self.error)) - self.assertFalse(is_acme_error(self.error_custom)) - self.assertFalse(is_acme_error(self.empty_error)) - self.assertFalse(is_acme_error("must pet all the {dogs|rabbits}")) - - def test_unicode_error(self): - from acme.messages import Error, ERROR_PREFIX, is_acme_error - arabic_error = Error( - detail=u'\u0639\u062f\u0627\u0644\u0629', typ=ERROR_PREFIX + 'malformed', - title='title') - self.assertTrue(is_acme_error(arabic_error)) - - def test_with_code(self): - from acme.messages import Error, is_acme_error - self.assertTrue(is_acme_error(Error.with_code('badCSR'))) - self.assertRaises(ValueError, Error.with_code, 'not an ACME error code') - - def test_str(self): - self.assertEqual( - str(self.error), - u"{0.typ} :: {0.description} :: {0.detail} :: {0.title}" - .format(self.error)) - - -class ConstantTest(unittest.TestCase): - """Tests for acme.messages._Constant.""" - - def setUp(self): - from acme.messages import _Constant - - class MockConstant(_Constant): # pylint: disable=missing-docstring - POSSIBLE_NAMES = {} # type: Dict - - self.MockConstant = MockConstant # pylint: disable=invalid-name - self.const_a = MockConstant('a') - self.const_b = MockConstant('b') - - def test_to_partial_json(self): - self.assertEqual('a', self.const_a.to_partial_json()) - self.assertEqual('b', self.const_b.to_partial_json()) - - def test_from_json(self): - self.assertEqual(self.const_a, self.MockConstant.from_json('a')) - self.assertRaises( - jose.DeserializationError, self.MockConstant.from_json, 'c') - - def test_from_json_hashable(self): - hash(self.MockConstant.from_json('a')) - - def test_repr(self): - self.assertEqual('MockConstant(a)', repr(self.const_a)) - self.assertEqual('MockConstant(b)', repr(self.const_b)) - - def test_equality(self): - const_a_prime = self.MockConstant('a') - self.assertFalse(self.const_a == self.const_b) - self.assertTrue(self.const_a == const_a_prime) - - self.assertTrue(self.const_a != self.const_b) - self.assertFalse(self.const_a != const_a_prime) - - -class DirectoryTest(unittest.TestCase): - """Tests for acme.messages.Directory.""" - - def setUp(self): - from acme.messages import Directory - self.dir = Directory({ - 'new-reg': 'reg', - mock.MagicMock(resource_type='new-cert'): 'cert', - 'meta': Directory.Meta( - terms_of_service='https://example.com/acme/terms', - website='https://www.example.com/', - caa_identities=['example.com'], - ), - }) - - def test_init_wrong_key_value_success(self): # pylint: disable=no-self-use - from acme.messages import Directory - Directory({'foo': 'bar'}) - - def test_getitem(self): - self.assertEqual('reg', self.dir['new-reg']) - from acme.messages import NewRegistration - self.assertEqual('reg', self.dir[NewRegistration]) - self.assertEqual('reg', self.dir[NewRegistration()]) - - def test_getitem_fails_with_key_error(self): - self.assertRaises(KeyError, self.dir.__getitem__, 'foo') - - def test_getattr(self): - self.assertEqual('reg', self.dir.new_reg) - - def test_getattr_fails_with_attribute_error(self): - self.assertRaises(AttributeError, self.dir.__getattr__, 'foo') - - def test_to_json(self): - self.assertEqual(self.dir.to_json(), { - 'new-reg': 'reg', - 'new-cert': 'cert', - 'meta': { - 'terms-of-service': 'https://example.com/acme/terms', - 'website': 'https://www.example.com/', - 'caaIdentities': ['example.com'], - }, - }) - - def test_from_json_deserialization_unknown_key_success(self): # pylint: disable=no-self-use - from acme.messages import Directory - Directory.from_json({'foo': 'bar'}) - - def test_iter_meta(self): - result = False - for k in self.dir.meta: - if k == 'terms_of_service': - result = self.dir.meta[k] == 'https://example.com/acme/terms' - self.assertTrue(result) - - -class ExternalAccountBindingTest(unittest.TestCase): - def setUp(self): - from acme.messages import Directory - self.key = jose.jwk.JWKRSA(key=KEY.public_key()) - self.kid = "kid-for-testing" - self.hmac_key = "hmac-key-for-testing" - self.dir = Directory({ - 'newAccount': 'http://url/acme/new-account', - }) - - def test_from_data(self): - from acme.messages import ExternalAccountBinding - eab = ExternalAccountBinding.from_data(self.key, self.kid, self.hmac_key, self.dir) - - self.assertEqual(len(eab), 3) - self.assertEqual(sorted(eab.keys()), sorted(['protected', 'payload', 'signature'])) - - -class RegistrationTest(unittest.TestCase): - """Tests for acme.messages.Registration.""" - - def setUp(self): - key = jose.jwk.JWKRSA(key=KEY.public_key()) - contact = ( - 'mailto:admin@foo.com', - 'tel:1234', - ) - agreement = 'https://letsencrypt.org/terms' - - from acme.messages import Registration - self.reg = Registration(key=key, contact=contact, agreement=agreement) - self.reg_none = Registration() - - self.jobj_to = { - 'contact': contact, - 'agreement': agreement, - 'key': key, - } - self.jobj_from = self.jobj_to.copy() - self.jobj_from['key'] = key.to_json() - - def test_from_data(self): - from acme.messages import Registration - reg = Registration.from_data(phone='1234', email='admin@foo.com') - self.assertEqual(reg.contact, ( - 'tel:1234', - 'mailto:admin@foo.com', - )) - - def test_new_registration_from_data_with_eab(self): - from acme.messages import NewRegistration, ExternalAccountBinding, Directory - key = jose.jwk.JWKRSA(key=KEY.public_key()) - kid = "kid-for-testing" - hmac_key = "hmac-key-for-testing" - directory = Directory({ - 'newAccount': 'http://url/acme/new-account', - }) - eab = ExternalAccountBinding.from_data(key, kid, hmac_key, directory) - reg = NewRegistration.from_data(email='admin@foo.com', external_account_binding=eab) - self.assertEqual(reg.contact, ( - 'mailto:admin@foo.com', - )) - self.assertEqual(sorted(reg.external_account_binding.keys()), - sorted(['protected', 'payload', 'signature'])) - - def test_phones(self): - self.assertEqual(('1234',), self.reg.phones) - - def test_emails(self): - self.assertEqual(('admin@foo.com',), self.reg.emails) - - def test_to_partial_json(self): - self.assertEqual(self.jobj_to, self.reg.to_partial_json()) - - def test_from_json(self): - from acme.messages import Registration - self.assertEqual(self.reg, Registration.from_json(self.jobj_from)) - - def test_from_json_hashable(self): - from acme.messages import Registration - hash(Registration.from_json(self.jobj_from)) - - -class UpdateRegistrationTest(unittest.TestCase): - """Tests for acme.messages.UpdateRegistration.""" - - def test_empty(self): - from acme.messages import UpdateRegistration - jstring = '{"resource": "reg"}' - self.assertEqual(jstring, UpdateRegistration().json_dumps()) - self.assertEqual( - UpdateRegistration(), UpdateRegistration.json_loads(jstring)) - - -class RegistrationResourceTest(unittest.TestCase): - """Tests for acme.messages.RegistrationResource.""" - - def setUp(self): - from acme.messages import RegistrationResource - self.regr = RegistrationResource( - body=mock.sentinel.body, uri=mock.sentinel.uri, - terms_of_service=mock.sentinel.terms_of_service) - - def test_to_partial_json(self): - self.assertEqual(self.regr.to_json(), { - 'body': mock.sentinel.body, - 'uri': mock.sentinel.uri, - 'terms_of_service': mock.sentinel.terms_of_service, - }) - - -class ChallengeResourceTest(unittest.TestCase): - """Tests for acme.messages.ChallengeResource.""" - - def test_uri(self): - from acme.messages import ChallengeResource - self.assertEqual('http://challb', ChallengeResource(body=mock.MagicMock( - uri='http://challb'), authzr_uri='http://authz').uri) - - -class ChallengeBodyTest(unittest.TestCase): - """Tests for acme.messages.ChallengeBody.""" - - def setUp(self): - self.chall = challenges.DNS(token=jose.b64decode( - 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA')) - - from acme.messages import ChallengeBody - from acme.messages import Error - from acme.messages import STATUS_INVALID - self.status = STATUS_INVALID - error = Error(typ='urn:ietf:params:acme:error:serverInternal', - detail='Unable to communicate with DNS server') - self.challb = ChallengeBody( - uri='http://challb', chall=self.chall, status=self.status, - error=error) - - self.jobj_to = { - 'uri': 'http://challb', - 'status': self.status, - 'type': 'dns', - 'token': 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA', - 'error': error, - } - self.jobj_from = self.jobj_to.copy() - self.jobj_from['status'] = 'invalid' - self.jobj_from['error'] = { - 'type': 'urn:ietf:params:acme:error:serverInternal', - 'detail': 'Unable to communicate with DNS server', - } - - def test_encode(self): - self.assertEqual(self.challb.encode('uri'), self.challb.uri) - - def test_to_partial_json(self): - self.assertEqual(self.jobj_to, self.challb.to_partial_json()) - - def test_from_json(self): - from acme.messages import ChallengeBody - self.assertEqual(self.challb, ChallengeBody.from_json(self.jobj_from)) - - def test_from_json_hashable(self): - from acme.messages import ChallengeBody - hash(ChallengeBody.from_json(self.jobj_from)) - - def test_proxy(self): - self.assertEqual(jose.b64decode( - 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA'), self.challb.token) - - -class AuthorizationTest(unittest.TestCase): - """Tests for acme.messages.Authorization.""" - - def setUp(self): - from acme.messages import ChallengeBody - from acme.messages import STATUS_VALID - - self.challbs = ( - ChallengeBody( - uri='http://challb1', status=STATUS_VALID, - chall=challenges.HTTP01(token=b'IlirfxKKXAsHtmzK29Pj8A')), - ChallengeBody(uri='http://challb2', status=STATUS_VALID, - chall=challenges.DNS( - token=b'DGyRejmCefe7v4NfDGDKfA')), - ) - combinations = ((0,), (1,)) - - from acme.messages import Authorization - from acme.messages import Identifier - from acme.messages import IDENTIFIER_FQDN - identifier = Identifier(typ=IDENTIFIER_FQDN, value='example.com') - self.authz = Authorization( - identifier=identifier, combinations=combinations, - challenges=self.challbs) - - self.jobj_from = { - 'identifier': identifier.to_json(), - 'challenges': [challb.to_json() for challb in self.challbs], - 'combinations': combinations, - } - - def test_from_json(self): - from acme.messages import Authorization - Authorization.from_json(self.jobj_from) - - def test_from_json_hashable(self): - from acme.messages import Authorization - hash(Authorization.from_json(self.jobj_from)) - - def test_resolved_combinations(self): - self.assertEqual(self.authz.resolved_combinations, ( - (self.challbs[0],), - (self.challbs[1],), - )) - - -class AuthorizationResourceTest(unittest.TestCase): - """Tests for acme.messages.AuthorizationResource.""" - - def test_json_de_serializable(self): - from acme.messages import AuthorizationResource - authzr = AuthorizationResource( - uri=mock.sentinel.uri, - body=mock.sentinel.body) - self.assertTrue(isinstance(authzr, jose.JSONDeSerializable)) - - -class CertificateRequestTest(unittest.TestCase): - """Tests for acme.messages.CertificateRequest.""" - - def setUp(self): - from acme.messages import CertificateRequest - self.req = CertificateRequest(csr=CSR) - - def test_json_de_serializable(self): - self.assertTrue(isinstance(self.req, jose.JSONDeSerializable)) - from acme.messages import CertificateRequest - self.assertEqual( - self.req, CertificateRequest.from_json(self.req.to_json())) - - -class CertificateResourceTest(unittest.TestCase): - """Tests for acme.messages.CertificateResourceTest.""" - - def setUp(self): - from acme.messages import CertificateResource - self.certr = CertificateResource( - body=CERT, uri=mock.sentinel.uri, authzrs=(), - cert_chain_uri=mock.sentinel.cert_chain_uri) - - def test_json_de_serializable(self): - self.assertTrue(isinstance(self.certr, jose.JSONDeSerializable)) - from acme.messages import CertificateResource - self.assertEqual( - self.certr, CertificateResource.from_json(self.certr.to_json())) - - -class RevocationTest(unittest.TestCase): - """Tests for acme.messages.RevocationTest.""" - - def setUp(self): - from acme.messages import Revocation - self.rev = Revocation(certificate=CERT) - - def test_from_json_hashable(self): - from acme.messages import Revocation - hash(Revocation.from_json(self.rev.to_json())) - - -class OrderResourceTest(unittest.TestCase): - """Tests for acme.messages.OrderResource.""" - - def setUp(self): - from acme.messages import OrderResource - self.regr = OrderResource( - body=mock.sentinel.body, uri=mock.sentinel.uri) - - def test_to_partial_json(self): - self.assertEqual(self.regr.to_json(), { - 'body': mock.sentinel.body, - 'uri': mock.sentinel.uri, - 'authorizations': None, - }) - -class NewOrderTest(unittest.TestCase): - """Tests for acme.messages.NewOrder.""" - - def setUp(self): - from acme.messages import NewOrder - self.reg = NewOrder( - identifiers=mock.sentinel.identifiers) - - def test_to_partial_json(self): - self.assertEqual(self.reg.to_json(), { - 'identifiers': mock.sentinel.identifiers, - }) - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/acme/acme/standalone.py b/acme/acme/standalone.py deleted file mode 100644 index ff91599335e..00000000000 --- a/acme/acme/standalone.py +++ /dev/null @@ -1,300 +0,0 @@ -"""Support for standalone client challenge solvers. """ -import argparse -import collections -import functools -import logging -import os -import socket -import sys -import threading - -from six.moves import BaseHTTPServer # type: ignore # pylint: disable=import-error -from six.moves import http_client # pylint: disable=import-error -from six.moves import socketserver # type: ignore # pylint: disable=import-error - -import OpenSSL - -from acme import challenges -from acme import crypto_util -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module - - -logger = logging.getLogger(__name__) - -# six.moves.* | pylint: disable=no-member,attribute-defined-outside-init -# pylint: disable=too-few-public-methods,no-init - - -class TLSServer(socketserver.TCPServer): - """Generic TLS Server.""" - - def __init__(self, *args, **kwargs): - self.ipv6 = kwargs.pop("ipv6", False) - if self.ipv6: - self.address_family = socket.AF_INET6 - else: - self.address_family = socket.AF_INET - self.certs = kwargs.pop("certs", {}) - self.method = kwargs.pop( - # pylint: disable=protected-access - "method", crypto_util._DEFAULT_TLSSNI01_SSL_METHOD) - self.allow_reuse_address = kwargs.pop("allow_reuse_address", True) - socketserver.TCPServer.__init__(self, *args, **kwargs) - - def _wrap_sock(self): - self.socket = crypto_util.SSLSocket( - self.socket, certs=self.certs, method=self.method) - - def server_bind(self): # pylint: disable=missing-docstring - self._wrap_sock() - return socketserver.TCPServer.server_bind(self) - - -class ACMEServerMixin: # pylint: disable=old-style-class - """ACME server common settings mixin.""" - # TODO: c.f. #858 - server_version = "ACME client standalone challenge solver" - allow_reuse_address = True - - -class BaseDualNetworkedServers(object): - """Base class for a pair of IPv6 and IPv4 servers that tries to do everything - it's asked for both servers, but where failures in one server don't - affect the other. - - If two servers are instantiated, they will serve on the same port. - """ - - def __init__(self, ServerClass, server_address, *remaining_args, **kwargs): - port = server_address[1] - self.threads = [] # type: List[threading.Thread] - self.servers = [] # type: List[ACMEServerMixin] - - # Must try True first. - # Ubuntu, for example, will fail to bind to IPv4 if we've already bound - # to IPv6. But that's ok, since it will accept IPv4 connections on the IPv6 - # socket. On the other hand, FreeBSD will successfully bind to IPv4 on the - # same port, which means that server will accept the IPv4 connections. - # If Python is compiled without IPv6, we'll error out but (probably) successfully - # create the IPv4 server. - for ip_version in [True, False]: - try: - kwargs["ipv6"] = ip_version - new_address = (server_address[0],) + (port,) + server_address[2:] - new_args = (new_address,) + remaining_args - server = ServerClass(*new_args, **kwargs) # pylint: disable=star-args - logger.debug( - "Successfully bound to %s:%s using %s", new_address[0], - new_address[1], "IPv6" if ip_version else "IPv4") - except socket.error: - if self.servers: - # Already bound using IPv6. - logger.debug( - "Certbot wasn't able to bind to %s:%s using %s, this " + - "is often expected due to the dual stack nature of " + - "IPv6 socket implementations.", - new_address[0], new_address[1], - "IPv6" if ip_version else "IPv4") - else: - logger.debug( - "Failed to bind to %s:%s using %s", new_address[0], - new_address[1], "IPv6" if ip_version else "IPv4") - else: - self.servers.append(server) - # If two servers are set up and port 0 was passed in, ensure we always - # bind to the same port for both servers. - port = server.socket.getsockname()[1] - if len(self.servers) == 0: - raise socket.error("Could not bind to IPv4 or IPv6.") - - def serve_forever(self): - """Wraps socketserver.TCPServer.serve_forever""" - for server in self.servers: - thread = threading.Thread( - # pylint: disable=no-member - target=server.serve_forever) - thread.start() - self.threads.append(thread) - - def getsocknames(self): - """Wraps socketserver.TCPServer.socket.getsockname""" - return [server.socket.getsockname() for server in self.servers] - - def shutdown_and_server_close(self): - """Wraps socketserver.TCPServer.shutdown, socketserver.TCPServer.server_close, and - threading.Thread.join""" - for server in self.servers: - server.shutdown() - server.server_close() - for thread in self.threads: - thread.join() - self.threads = [] - - -class TLSSNI01Server(TLSServer, ACMEServerMixin): - """TLSSNI01 Server.""" - - def __init__(self, server_address, certs, ipv6=False): - TLSServer.__init__( - self, server_address, BaseRequestHandlerWithLogging, certs=certs, ipv6=ipv6) - - -class TLSSNI01DualNetworkedServers(BaseDualNetworkedServers): - """TLSSNI01Server Wrapper. Tries everything for both. Failures for one don't - affect the other.""" - - def __init__(self, *args, **kwargs): - BaseDualNetworkedServers.__init__(self, TLSSNI01Server, *args, **kwargs) - - -class BaseRequestHandlerWithLogging(socketserver.BaseRequestHandler): - """BaseRequestHandler with logging.""" - - def log_message(self, format, *args): # pylint: disable=redefined-builtin - """Log arbitrary message.""" - logger.debug("%s - - %s", self.client_address[0], format % args) - - def handle(self): - """Handle request.""" - self.log_message("Incoming request") - socketserver.BaseRequestHandler.handle(self) - - -class HTTPServer(BaseHTTPServer.HTTPServer): - """Generic HTTP Server.""" - - def __init__(self, *args, **kwargs): - self.ipv6 = kwargs.pop("ipv6", False) - if self.ipv6: - self.address_family = socket.AF_INET6 - else: - self.address_family = socket.AF_INET - BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs) - - -class HTTP01Server(HTTPServer, ACMEServerMixin): - """HTTP01 Server.""" - - def __init__(self, server_address, resources, ipv6=False): - HTTPServer.__init__( - self, server_address, HTTP01RequestHandler.partial_init( - simple_http_resources=resources), ipv6=ipv6) - - -class HTTP01DualNetworkedServers(BaseDualNetworkedServers): - """HTTP01Server Wrapper. Tries everything for both. Failures for one don't - affect the other.""" - - def __init__(self, *args, **kwargs): - BaseDualNetworkedServers.__init__(self, HTTP01Server, *args, **kwargs) - - -class HTTP01RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): - """HTTP01 challenge handler. - - Adheres to the stdlib's `socketserver.BaseRequestHandler` interface. - - :ivar set simple_http_resources: A set of `HTTP01Resource` - objects. TODO: better name? - - """ - HTTP01Resource = collections.namedtuple( - "HTTP01Resource", "chall response validation") - - def __init__(self, *args, **kwargs): - self.simple_http_resources = kwargs.pop("simple_http_resources", set()) - BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args, **kwargs) - - def log_message(self, format, *args): # pylint: disable=redefined-builtin - """Log arbitrary message.""" - logger.debug("%s - - %s", self.client_address[0], format % args) - - def handle(self): - """Handle request.""" - self.log_message("Incoming request") - BaseHTTPServer.BaseHTTPRequestHandler.handle(self) - - def do_GET(self): # pylint: disable=invalid-name,missing-docstring - if self.path == "/": - self.handle_index() - elif self.path.startswith("/" + challenges.HTTP01.URI_ROOT_PATH): - self.handle_simple_http_resource() - else: - self.handle_404() - - def handle_index(self): - """Handle index page.""" - self.send_response(200) - self.send_header("Content-Type", "text/html") - self.end_headers() - self.wfile.write(self.server.server_version.encode()) - - def handle_404(self): - """Handler 404 Not Found errors.""" - self.send_response(http_client.NOT_FOUND, message="Not Found") - self.send_header("Content-type", "text/html") - self.end_headers() - self.wfile.write(b"404") - - def handle_simple_http_resource(self): - """Handle HTTP01 provisioned resources.""" - for resource in self.simple_http_resources: - if resource.chall.path == self.path: - self.log_message("Serving HTTP01 with token %r", - resource.chall.encode("token")) - self.send_response(http_client.OK) - self.end_headers() - self.wfile.write(resource.validation.encode()) - return - else: # pylint: disable=useless-else-on-loop - self.log_message("No resources to serve") - self.log_message("%s does not correspond to any resource. ignoring", - self.path) - - @classmethod - def partial_init(cls, simple_http_resources): - """Partially initialize this handler. - - This is useful because `socketserver.BaseServer` takes - uninitialized handler and initializes it with the current - request. - - """ - return functools.partial( - cls, simple_http_resources=simple_http_resources) - - -def simple_tls_sni_01_server(cli_args, forever=True): - """Run simple standalone TLSSNI01 server.""" - logging.basicConfig(level=logging.DEBUG) - - parser = argparse.ArgumentParser() - parser.add_argument( - "-p", "--port", default=0, help="Port to serve at. By default " - "picks random free port.") - args = parser.parse_args(cli_args[1:]) - - certs = {} - - _, hosts, _ = next(os.walk('.')) # type: ignore # https://github.com/python/mypy/issues/465 - for host in hosts: - with open(os.path.join(host, "cert.pem")) as cert_file: - cert_contents = cert_file.read() - with open(os.path.join(host, "key.pem")) as key_file: - key_contents = key_file.read() - certs[host.encode()] = ( - OpenSSL.crypto.load_privatekey( - OpenSSL.crypto.FILETYPE_PEM, key_contents), - OpenSSL.crypto.load_certificate( - OpenSSL.crypto.FILETYPE_PEM, cert_contents)) - - server = TLSSNI01Server(('', int(args.port)), certs=certs) - logger.info("Serving at https://%s:%s...", *server.socket.getsockname()[:2]) - if forever: # pragma: no cover - server.serve_forever() - else: - server.handle_request() - - -if __name__ == "__main__": - sys.exit(simple_tls_sni_01_server(sys.argv)) # pragma: no cover diff --git a/acme/acme/standalone_test.py b/acme/acme/standalone_test.py deleted file mode 100644 index ee527782ab6..00000000000 --- a/acme/acme/standalone_test.py +++ /dev/null @@ -1,298 +0,0 @@ -"""Tests for acme.standalone.""" -import os -import shutil -import socket -import threading -import tempfile -import unittest - -from six.moves import http_client # pylint: disable=import-error -from six.moves import queue # pylint: disable=import-error -from six.moves import socketserver # type: ignore # pylint: disable=import-error - -import josepy as jose -import mock -import requests - -from acme import challenges -from acme import crypto_util -from acme import test_util -from acme.magic_typing import Set # pylint: disable=unused-import, no-name-in-module - - -class TLSServerTest(unittest.TestCase): - """Tests for acme.standalone.TLSServer.""" - - - def test_bind(self): # pylint: disable=no-self-use - from acme.standalone import TLSServer - server = TLSServer( - ('', 0), socketserver.BaseRequestHandler, bind_and_activate=True) - server.server_close() # pylint: disable=no-member - - def test_ipv6(self): - if socket.has_ipv6: - from acme.standalone import TLSServer - server = TLSServer( - ('', 0), socketserver.BaseRequestHandler, bind_and_activate=True, ipv6=True) - server.server_close() # pylint: disable=no-member - - -class TLSSNI01ServerTest(unittest.TestCase): - """Test for acme.standalone.TLSSNI01Server.""" - - - def setUp(self): - self.certs = {b'localhost': ( - test_util.load_pyopenssl_private_key('rsa2048_key.pem'), - test_util.load_cert('rsa2048_cert.pem'), - )} - from acme.standalone import TLSSNI01Server - self.server = TLSSNI01Server(('localhost', 0), certs=self.certs) - # pylint: disable=no-member - self.thread = threading.Thread(target=self.server.serve_forever) - self.thread.start() - - def tearDown(self): - self.server.shutdown() # pylint: disable=no-member - self.thread.join() - - def test_it(self): - host, port = self.server.socket.getsockname()[:2] - cert = crypto_util.probe_sni( - b'localhost', host=host, port=port, timeout=1) - self.assertEqual(jose.ComparableX509(cert), - jose.ComparableX509(self.certs[b'localhost'][1])) - - -class HTTP01ServerTest(unittest.TestCase): - """Tests for acme.standalone.HTTP01Server.""" - - - def setUp(self): - self.account_key = jose.JWK.load( - test_util.load_vector('rsa1024_key.pem')) - self.resources = set() # type: Set - - from acme.standalone import HTTP01Server - self.server = HTTP01Server(('', 0), resources=self.resources) - - # pylint: disable=no-member - self.port = self.server.socket.getsockname()[1] - self.thread = threading.Thread(target=self.server.serve_forever) - self.thread.start() - - def tearDown(self): - self.server.shutdown() # pylint: disable=no-member - self.thread.join() - - def test_index(self): - response = requests.get( - 'http://localhost:{0}'.format(self.port), verify=False) - self.assertEqual( - response.text, 'ACME client standalone challenge solver') - self.assertTrue(response.ok) - - def test_404(self): - response = requests.get( - 'http://localhost:{0}/foo'.format(self.port), verify=False) - self.assertEqual(response.status_code, http_client.NOT_FOUND) - - def _test_http01(self, add): - chall = challenges.HTTP01(token=(b'x' * 16)) - response, validation = chall.response_and_validation(self.account_key) - - from acme.standalone import HTTP01RequestHandler - resource = HTTP01RequestHandler.HTTP01Resource( - chall=chall, response=response, validation=validation) - if add: - self.resources.add(resource) - return resource.response.simple_verify( - resource.chall, 'localhost', self.account_key.public_key(), - port=self.port) - - def test_http01_found(self): - self.assertTrue(self._test_http01(add=True)) - - def test_http01_not_found(self): - self.assertFalse(self._test_http01(add=False)) - - -class BaseDualNetworkedServersTest(unittest.TestCase): - """Test for acme.standalone.BaseDualNetworkedServers.""" - - - class SingleProtocolServer(socketserver.TCPServer): - """Server that only serves on a single protocol. FreeBSD has this behavior for AF_INET6.""" - def __init__(self, *args, **kwargs): - ipv6 = kwargs.pop("ipv6", False) - if ipv6: - self.address_family = socket.AF_INET6 - kwargs["bind_and_activate"] = False - else: - self.address_family = socket.AF_INET - socketserver.TCPServer.__init__(self, *args, **kwargs) - if ipv6: - # NB: On Windows, socket.IPPROTO_IPV6 constant may be missing. - # We use the corresponding value (41) instead. - level = getattr(socket, "IPPROTO_IPV6", 41) - # pylint: disable=no-member - self.socket.setsockopt(level, socket.IPV6_V6ONLY, 1) - try: - self.server_bind() - self.server_activate() - except: - self.server_close() - raise - - @mock.patch("socket.socket.bind") - def test_fail_to_bind(self, mock_bind): - mock_bind.side_effect = socket.error - from acme.standalone import BaseDualNetworkedServers - self.assertRaises(socket.error, BaseDualNetworkedServers, - BaseDualNetworkedServersTest.SingleProtocolServer, - ('', 0), - socketserver.BaseRequestHandler) - - def test_ports_equal(self): - from acme.standalone import BaseDualNetworkedServers - servers = BaseDualNetworkedServers( - BaseDualNetworkedServersTest.SingleProtocolServer, - ('', 0), - socketserver.BaseRequestHandler) - socknames = servers.getsocknames() - prev_port = None - # assert ports are equal - for sockname in socknames: - port = sockname[1] - if prev_port: - self.assertEqual(prev_port, port) - prev_port = port - - -class TLSSNI01DualNetworkedServersTest(unittest.TestCase): - """Test for acme.standalone.TLSSNI01DualNetworkedServers.""" - - - def setUp(self): - self.certs = {b'localhost': ( - test_util.load_pyopenssl_private_key('rsa2048_key.pem'), - test_util.load_cert('rsa2048_cert.pem'), - )} - from acme.standalone import TLSSNI01DualNetworkedServers - self.servers = TLSSNI01DualNetworkedServers(('localhost', 0), certs=self.certs) - self.servers.serve_forever() - - def tearDown(self): - self.servers.shutdown_and_server_close() - - def test_connect(self): - socknames = self.servers.getsocknames() - # connect to all addresses - for sockname in socknames: - host, port = sockname[:2] - cert = crypto_util.probe_sni( - b'localhost', host=host, port=port, timeout=1) - self.assertEqual(jose.ComparableX509(cert), - jose.ComparableX509(self.certs[b'localhost'][1])) - - -class HTTP01DualNetworkedServersTest(unittest.TestCase): - """Tests for acme.standalone.HTTP01DualNetworkedServers.""" - - - def setUp(self): - self.account_key = jose.JWK.load( - test_util.load_vector('rsa1024_key.pem')) - self.resources = set() # type: Set - - from acme.standalone import HTTP01DualNetworkedServers - self.servers = HTTP01DualNetworkedServers(('', 0), resources=self.resources) - - # pylint: disable=no-member - self.port = self.servers.getsocknames()[0][1] - self.servers.serve_forever() - - def tearDown(self): - self.servers.shutdown_and_server_close() - - def test_index(self): - response = requests.get( - 'http://localhost:{0}'.format(self.port), verify=False) - self.assertEqual( - response.text, 'ACME client standalone challenge solver') - self.assertTrue(response.ok) - - def test_404(self): - response = requests.get( - 'http://localhost:{0}/foo'.format(self.port), verify=False) - self.assertEqual(response.status_code, http_client.NOT_FOUND) - - def _test_http01(self, add): - chall = challenges.HTTP01(token=(b'x' * 16)) - response, validation = chall.response_and_validation(self.account_key) - - from acme.standalone import HTTP01RequestHandler - resource = HTTP01RequestHandler.HTTP01Resource( - chall=chall, response=response, validation=validation) - if add: - self.resources.add(resource) - return resource.response.simple_verify( - resource.chall, 'localhost', self.account_key.public_key(), - port=self.port) - - def test_http01_found(self): - self.assertTrue(self._test_http01(add=True)) - - def test_http01_not_found(self): - self.assertFalse(self._test_http01(add=False)) - - -@test_util.broken_on_windows -class TestSimpleTLSSNI01Server(unittest.TestCase): - """Tests for acme.standalone.simple_tls_sni_01_server.""" - - - def setUp(self): - # mirror ../examples/standalone - self.test_cwd = tempfile.mkdtemp() - localhost_dir = os.path.join(self.test_cwd, 'localhost') - os.makedirs(localhost_dir) - shutil.copy(test_util.vector_path('rsa2048_cert.pem'), - os.path.join(localhost_dir, 'cert.pem')) - shutil.copy(test_util.vector_path('rsa2048_key.pem'), - os.path.join(localhost_dir, 'key.pem')) - - from acme.standalone import simple_tls_sni_01_server - self.thread = threading.Thread( - target=simple_tls_sni_01_server, kwargs={ - 'cli_args': ('filename',), - 'forever': False, - }, - ) - self.old_cwd = os.getcwd() - os.chdir(self.test_cwd) - - def tearDown(self): - os.chdir(self.old_cwd) - self.thread.join() - shutil.rmtree(self.test_cwd) - - @mock.patch('acme.standalone.logger') - def test_it(self, mock_logger): - # Use a Queue because mock objects aren't thread safe. - q = queue.Queue() # type: queue.Queue[int] - # Add port number to the queue. - mock_logger.info.side_effect = lambda *args: q.put(args[-1]) - self.thread.start() - - # After the timeout, an exception is raised if the queue is empty. - port = q.get(timeout=5) - cert = crypto_util.probe_sni(b'localhost', b'0.0.0.0', port) - self.assertEqual(jose.ComparableX509(cert), - test_util.load_comparable_cert( - 'rsa2048_cert.pem')) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/acme/acme/test_util.py b/acme/acme/test_util.py deleted file mode 100644 index f9761470025..00000000000 --- a/acme/acme/test_util.py +++ /dev/null @@ -1,105 +0,0 @@ -"""Test utilities. - -.. warning:: This module is not part of the public API. - -""" -import os -import sys -import pkg_resources -import unittest - -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives import serialization -import josepy as jose -from OpenSSL import crypto - - -def vector_path(*names): - """Path to a test vector.""" - return pkg_resources.resource_filename( - __name__, os.path.join('testdata', *names)) - - -def load_vector(*names): - """Load contents of a test vector.""" - # luckily, resource_string opens file in binary mode - return pkg_resources.resource_string( - __name__, os.path.join('testdata', *names)) - - -def _guess_loader(filename, loader_pem, loader_der): - _, ext = os.path.splitext(filename) - if ext.lower() == '.pem': - return loader_pem - elif ext.lower() == '.der': - return loader_der - else: # pragma: no cover - raise ValueError("Loader could not be recognized based on extension") - - -def load_cert(*names): - """Load certificate.""" - loader = _guess_loader( - names[-1], crypto.FILETYPE_PEM, crypto.FILETYPE_ASN1) - return crypto.load_certificate(loader, load_vector(*names)) - - -def load_comparable_cert(*names): - """Load ComparableX509 cert.""" - return jose.ComparableX509(load_cert(*names)) - - -def load_csr(*names): - """Load certificate request.""" - loader = _guess_loader( - names[-1], crypto.FILETYPE_PEM, crypto.FILETYPE_ASN1) - return crypto.load_certificate_request(loader, load_vector(*names)) - - -def load_comparable_csr(*names): - """Load ComparableX509 certificate request.""" - return jose.ComparableX509(load_csr(*names)) - - -def load_rsa_private_key(*names): - """Load RSA private key.""" - loader = _guess_loader(names[-1], serialization.load_pem_private_key, - serialization.load_der_private_key) - return jose.ComparableRSAKey(loader( - load_vector(*names), password=None, backend=default_backend())) - - -def load_pyopenssl_private_key(*names): - """Load pyOpenSSL private key.""" - loader = _guess_loader( - names[-1], crypto.FILETYPE_PEM, crypto.FILETYPE_ASN1) - return crypto.load_privatekey(loader, load_vector(*names)) - - -def skip_unless(condition, reason): # pragma: no cover - """Skip tests unless a condition holds. - - This implements the basic functionality of unittest.skipUnless - which is only available on Python 2.7+. - - :param bool condition: If ``False``, the test will be skipped - :param str reason: the reason for skipping the test - - :rtype: callable - :returns: decorator that hides tests unless condition is ``True`` - - """ - if hasattr(unittest, "skipUnless"): - return unittest.skipUnless(condition, reason) - elif condition: - return lambda cls: cls - else: - return lambda cls: None - -def broken_on_windows(function): - """Decorator to skip temporarily a broken test on Windows.""" - reason = 'Test is broken and ignored on windows but should be fixed.' - return unittest.skipIf( - sys.platform == 'win32' - and os.environ.get('SKIP_BROKEN_TESTS_ON_WINDOWS', 'true') == 'true', - reason)(function) diff --git a/acme/acme/testdata/README b/acme/acme/testdata/README deleted file mode 100644 index dfe3f5405fe..00000000000 --- a/acme/acme/testdata/README +++ /dev/null @@ -1,15 +0,0 @@ -In order for acme.test_util._guess_loader to work properly, make sure -to use appropriate extension for vector filenames: .pem for PEM and -.der for DER. - -The following command has been used to generate test keys: - - for x in 256 512 1024 2048; do openssl genrsa -out rsa${k}_key.pem $k; done - -and for the CSR: - - openssl req -key rsa2048_key.pem -new -subj '/CN=example.com' -outform DER > csr.der - -and for the certificate: - - openssl req -key rsa2047_key.pem -new -subj '/CN=example.com' -x509 -outform DER > cert.der diff --git a/acme/acme/util.py b/acme/acme/util.py deleted file mode 100644 index 1fff89a9e6a..00000000000 --- a/acme/acme/util.py +++ /dev/null @@ -1,7 +0,0 @@ -"""ACME utilities.""" -import six - - -def map_keys(dikt, func): - """Map dictionary keys.""" - return dict((func(key), value) for key, value in six.iteritems(dikt)) diff --git a/acme/acme/util_test.py b/acme/acme/util_test.py deleted file mode 100644 index 00aa8b02d95..00000000000 --- a/acme/acme/util_test.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Tests for acme.util.""" -import unittest - - -class MapKeysTest(unittest.TestCase): - """Tests for acme.util.map_keys.""" - - def test_it(self): - from acme.util import map_keys - self.assertEqual({'a': 'b', 'c': 'd'}, - map_keys({'a': 'b', 'c': 'd'}, lambda key: key)) - self.assertEqual({2: 2, 4: 4}, map_keys({1: 2, 3: 4}, lambda x: x + 1)) - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/acme/docs/Makefile b/acme/docs/Makefile index 79de9c0a3aa..aaf5f157286 100644 --- a/acme/docs/Makefile +++ b/acme/docs/Makefile @@ -9,7 +9,7 @@ BUILDDIR = _build # User-friendly check for sphinx-build ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) -$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from https://www.sphinx-doc.org/) endif # Internal variables. diff --git a/acme/docs/api/crypto_util.rst b/acme/docs/api/crypto_util.rst new file mode 100644 index 00000000000..9c516d317eb --- /dev/null +++ b/acme/docs/api/crypto_util.rst @@ -0,0 +1,5 @@ +Crypto_util +----------- + +.. automodule:: acme.crypto_util + :members: diff --git a/acme/docs/api/jws.rst b/acme/docs/api/jws.rst new file mode 100644 index 00000000000..158b0bab577 --- /dev/null +++ b/acme/docs/api/jws.rst @@ -0,0 +1,5 @@ +JWS +--- + +.. automodule:: acme.jws + :members: diff --git a/acme/docs/api/util.rst b/acme/docs/api/util.rst new file mode 100644 index 00000000000..40b628985a5 --- /dev/null +++ b/acme/docs/api/util.rst @@ -0,0 +1,5 @@ +Util +---- + +.. automodule:: acme.util + :members: diff --git a/acme/docs/conf.py b/acme/docs/conf.py index e70651648e5..b94294bd0aa 100644 --- a/acme/docs/conf.py +++ b/acme/docs/conf.py @@ -12,10 +12,8 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys import os -import shlex - +import sys here = os.path.abspath(os.path.dirname(__file__)) @@ -39,10 +37,11 @@ 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.viewcode', + 'sphinx_rtd_theme', ] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -60,7 +59,7 @@ # General information about the project. project = u'acme-python' -copyright = u'2015-2015, Let\'s Encrypt Project' +copyright = u'2015, Let\'s Encrypt Project' author = u'Let\'s Encrypt Project' # The version info for the project you're documenting, acts as replacement for @@ -87,7 +86,9 @@ # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. -exclude_patterns = ['_build'] +exclude_patterns = [ + '_build', +] # The reST default role (used for this markup: `text`) to use for all # documents. @@ -114,7 +115,7 @@ #keep_warnings = False # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -122,14 +123,7 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the diff --git a/acme/docs/index.rst b/acme/docs/index.rst index a200808daf4..a94c02e5ca5 100644 --- a/acme/docs/index.rst +++ b/acme/docs/index.rst @@ -16,13 +16,6 @@ Contents: .. automodule:: acme :members: - -Example client: - -.. include:: ../examples/example_client.py - :code: python - - Indices and tables ================== diff --git a/acme/docs/jws-help.txt b/acme/docs/jws-help.txt index 34cf5ce2356..bfd16dff4d7 100644 --- a/acme/docs/jws-help.txt +++ b/acme/docs/jws-help.txt @@ -3,6 +3,6 @@ usage: jws [-h] [--compact] {sign,verify} ... positional arguments: {sign,verify} -optional arguments: +options: -h, --help show this help message and exit --compact diff --git a/acme/docs/make.bat b/acme/docs/make.bat index 7811859777f..a61f4379895 100644 --- a/acme/docs/make.bat +++ b/acme/docs/make.bat @@ -65,7 +65,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/acme/docs/man/jws.rst b/acme/docs/man/jws.rst index d7ff8f40547..7e83328b25e 100644 --- a/acme/docs/man/jws.rst +++ b/acme/docs/man/jws.rst @@ -1 +1,3 @@ +:orphan: + .. literalinclude:: ../jws-help.txt diff --git a/acme/examples/example_client.py b/acme/examples/example_client.py deleted file mode 100644 index abeedc0829f..00000000000 --- a/acme/examples/example_client.py +++ /dev/null @@ -1,47 +0,0 @@ -"""Example script showing how to use acme client API.""" -import logging -import os -import pkg_resources - -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives.asymmetric import rsa -import josepy as jose -import OpenSSL - -from acme import client -from acme import messages - - -logging.basicConfig(level=logging.DEBUG) - - -DIRECTORY_URL = 'https://acme-staging.api.letsencrypt.org/directory' -BITS = 2048 # minimum for Boulder -DOMAIN = 'example1.com' # example.com is ignored by Boulder - -# generate_private_key requires cryptography>=0.5 -key = jose.JWKRSA(key=rsa.generate_private_key( - public_exponent=65537, - key_size=BITS, - backend=default_backend())) -acme = client.Client(DIRECTORY_URL, key) - -regr = acme.register() -logging.info('Auto-accepting TOS: %s', regr.terms_of_service) -acme.agree_to_tos(regr) -logging.debug(regr) - -authzr = acme.request_challenges( - identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value=DOMAIN)) -logging.debug(authzr) - -authzr, authzr_response = acme.poll(authzr) - -csr = OpenSSL.crypto.load_certificate_request( - OpenSSL.crypto.FILETYPE_ASN1, pkg_resources.resource_string( - 'acme', os.path.join('testdata', 'csr.der'))) -try: - acme.request_issuance(jose.util.ComparableX509(csr), (authzr,)) -except messages.Error as error: - print ("This script is doomed to fail as no authorization " - "challenges are ever solved. Error from server: {0}".format(error)) diff --git a/acme/examples/http01_example.py b/acme/examples/http01_example.py new file mode 100644 index 00000000000..d1ff8a4ef7b --- /dev/null +++ b/acme/examples/http01_example.py @@ -0,0 +1,237 @@ +"""Example ACME-V2 API for HTTP-01 challenge. + +Brief: + +This a complete usage example of the python-acme API. + +Limitations of this example: + - Works for only one Domain name + - Performs only HTTP-01 challenge + - Uses ACME-v2 + +Workflow: + (Account creation) + - Create account key + - Register account and accept TOS + (Certificate actions) + - Select HTTP-01 within offered challenges by the CA server + - Set up http challenge resource + - Set up standalone web server + - Create domain private key and CSR + - Issue certificate + - Renew certificate + - Revoke certificate + (Account update actions) + - Change contact information + - Deactivate Account +""" +from contextlib import contextmanager + +from cryptography import x509 +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import rsa +import josepy as jose + +from acme import challenges +from acme import client +from acme import crypto_util +from acme import errors +from acme import messages +from acme import standalone + +# Constants: + +# This is the staging point for ACME-V2 within Let's Encrypt. +DIRECTORY_URL = 'https://acme-staging-v02.api.letsencrypt.org/directory' + +USER_AGENT = 'python-acme-example' + +# Account key size +ACC_KEY_BITS = 2048 + +# Certificate private key size +CERT_PKEY_BITS = 2048 + +# Domain name for the certificate. +DOMAIN = 'client.example.com' + +# If you are running Boulder locally, it is possible to configure any port +# number to execute the challenge, but real CA servers will always use port +# 80, as described in the ACME specification. +PORT = 80 + + +# Useful methods and classes: + + +def new_csr_comp(domain_name, pkey_pem=None): + """Create certificate signing request.""" + if pkey_pem is None: + # Create private key. + pkey = rsa.generate_private_key(public_exponent=65537, key_size=CERT_PKEY_BITS) + pkey_pem = pkey.private_bytes(encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.PKCS8, + encryption_algorithm=serialization.NoEncryption()) + + csr_pem = crypto_util.make_csr(pkey_pem, [domain_name]) + return pkey_pem, csr_pem + + +def select_http01_chall(orderr): + """Extract authorization resource from within order resource.""" + # Authorization Resource: authz. + # This object holds the offered challenges by the server and their status. + authz_list = orderr.authorizations + + for authz in authz_list: + # Choosing challenge. + # authz.body.challenges is a set of ChallengeBody objects. + for i in authz.body.challenges: + # Find the supported challenge. + if isinstance(i.chall, challenges.HTTP01): + return i + + raise Exception('HTTP-01 challenge was not offered by the CA server.') + + +@contextmanager +def challenge_server(http_01_resources): + """Manage standalone server set up and shutdown.""" + + # Setting up a fake server that binds at PORT and any address. + address = ('', PORT) + try: + servers = standalone.HTTP01DualNetworkedServers(address, + http_01_resources) + # Start client standalone web server. + servers.serve_forever() + yield servers + finally: + # Shutdown client web server and unbind from PORT + servers.shutdown_and_server_close() + + +def perform_http01(client_acme, challb, orderr): + """Set up standalone webserver and perform HTTP-01 challenge.""" + + response, validation = challb.response_and_validation(client_acme.net.key) + + resource = standalone.HTTP01RequestHandler.HTTP01Resource( + chall=challb.chall, response=response, validation=validation) + + with challenge_server({resource}): + # Let the CA server know that we are ready for the challenge. + client_acme.answer_challenge(challb, response) + + # Wait for challenge status and then issue a certificate. + # It is possible to set a deadline time. + finalized_orderr = client_acme.poll_and_finalize(orderr) + + return finalized_orderr.fullchain_pem + + +# Main examples: + + +def example_http(): + """This example executes the whole process of fulfilling a HTTP-01 + challenge for one specific domain. + + The workflow consists of: + (Account creation) + - Create account key + - Register account and accept TOS + (Certificate actions) + - Select HTTP-01 within offered challenges by the CA server + - Set up http challenge resource + - Set up standalone web server + - Create domain private key and CSR + - Issue certificate + - Renew certificate + - Revoke certificate + (Account update actions) + - Change contact information + - Deactivate Account + + """ + # Create account key + + acc_key = jose.JWKRSA( + key=rsa.generate_private_key(public_exponent=65537, + key_size=ACC_KEY_BITS, + backend=default_backend())) + + # Register account and accept TOS + + net = client.ClientNetwork(acc_key, user_agent=USER_AGENT) + directory = client.ClientV2.get_directory(DIRECTORY_URL, net) + client_acme = client.ClientV2(directory, net=net) + + # Terms of Service URL is in client_acme.directory.meta.terms_of_service + # Registration Resource: regr + regr = client_acme.new_account( + messages.NewRegistration.from_data(terms_of_service_agreed=True)) + + # Create domain private key and CSR + pkey_pem, csr_pem = new_csr_comp(DOMAIN) + + # Issue certificate + + orderr = client_acme.new_order(csr_pem) + + # Select HTTP-01 within offered challenges by the CA server + challb = select_http01_chall(orderr) + + # The certificate is ready to be used in the variable "fullchain_pem". + fullchain_pem = perform_http01(client_acme, challb, orderr) + + # Renew certificate + + _, csr_pem = new_csr_comp(DOMAIN, pkey_pem) + + orderr = client_acme.new_order(csr_pem) + + challb = select_http01_chall(orderr) + + # Performing challenge + fullchain_pem = perform_http01(client_acme, challb, orderr) + + # Revoke certificate + + fullchain_com = x509.load_pem_x509_certificate(fullchain_pem.encode()) + + try: + client_acme.revoke(fullchain_com, 0) # revocation reason = 0 + except errors.ConflictError: + # Certificate already revoked. + pass + + # Query registration status. + client_acme.net.account = regr + try: + regr = client_acme.query_registration(regr) + except errors.Error as err: + if err.typ == messages.ERROR_PREFIX + 'unauthorized': + # Status is deactivated. + pass + raise + + # Change contact information + + email = 'newfake@example.com' + regr = client_acme.update_registration( + regr.update( + body=regr.body.update( + contact=('mailto:' + email,) + ) + ) + ) + + # Deactivate account/registration + + regr = client_acme.deactivate_registration(regr) + + +if __name__ == "__main__": + example_http() diff --git a/acme/examples/standalone/README b/acme/examples/standalone/README deleted file mode 100644 index 8bb0fad979d..00000000000 --- a/acme/examples/standalone/README +++ /dev/null @@ -1,2 +0,0 @@ -python -m acme.standalone -p 1234 -curl -k https://localhost:1234 diff --git a/acme/examples/standalone/localhost/cert.pem b/acme/examples/standalone/localhost/cert.pem deleted file mode 120000 index 1cca87af590..00000000000 --- a/acme/examples/standalone/localhost/cert.pem +++ /dev/null @@ -1 +0,0 @@ -../../../acme/testdata/rsa2048_cert.pem \ No newline at end of file diff --git a/acme/examples/standalone/localhost/key.pem b/acme/examples/standalone/localhost/key.pem deleted file mode 120000 index ee3dd28473f..00000000000 --- a/acme/examples/standalone/localhost/key.pem +++ /dev/null @@ -1 +0,0 @@ -../../../acme/testdata/rsa2048_key.pem \ No newline at end of file diff --git a/acme/pyproject.toml b/acme/pyproject.toml new file mode 100644 index 00000000000..d75a2e70588 --- /dev/null +++ b/acme/pyproject.toml @@ -0,0 +1,56 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "acme" +dynamic = ["version"] +description = "ACME protocol implementation in Python" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", +] +dependencies = [ + "cryptography>=47.0.0", + "josepy>=2.0.0", + # PyOpenSSL>=25.0.0 is just needed to satisfy mypy right now so this dependency can probably be + # relaxed to >=24.0.0 if needed. + "PyOpenSSL>=25.0.0", + "pyrfc3339", + "requests>=2.25.1", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", + "pytest-xdist", + "typing-extensions", +] + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/acme/pytest.ini b/acme/pytest.ini deleted file mode 100644 index 0c07ceac76f..00000000000 --- a/acme/pytest.ini +++ /dev/null @@ -1,2 +0,0 @@ -[pytest] -norecursedirs = .* build dist CVS _darcs {arch} *.egg diff --git a/acme/readthedocs.org.requirements.txt b/acme/readthedocs.org.requirements.txt deleted file mode 100644 index 65e6c7cf3c8..00000000000 --- a/acme/readthedocs.org.requirements.txt +++ /dev/null @@ -1,10 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme[docs] diff --git a/acme/setup.cfg b/acme/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/acme/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/acme/setup.py b/acme/setup.py index eac3974fad4..11fc858b9b0 100644 --- a/acme/setup.py +++ b/acme/setup.py @@ -1,86 +1,7 @@ from setuptools import setup -from setuptools import find_packages -from setuptools.command.test import test as TestCommand -import sys -version = '0.31.0.dev0' - -# Please update tox.ini when modifying dependency version requirements -install_requires = [ - # load_pem_private/public_key (>=0.6) - # rsa_recover_prime_factors (>=0.8) - 'cryptography>=1.2.3', - # formerly known as acme.jose: - 'josepy>=1.0.0', - # Connection.set_tlsext_host_name (>=0.13) - 'mock', - 'PyOpenSSL>=0.13.1', - 'pyrfc3339', - 'pytz', - 'requests[security]>=2.6.0', # security extras added in 2.4.1 - 'requests-toolbelt>=0.3.0', - 'setuptools', - 'six>=1.9.0', # needed for python_2_unicode_compatible -] - -dev_extras = [ - 'pytest', - 'pytest-xdist', - 'tox', -] - -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] - -class PyTest(TestCommand): - user_options = [] - - def initialize_options(self): - TestCommand.initialize_options(self) - self.pytest_args = '' - - def run_tests(self): - import shlex - # import here, cause outside the eggs aren't loaded - import pytest - errno = pytest.main(shlex.split(self.pytest_args)) - sys.exit(errno) +version = '5.8.0.dev0' setup( - name='acme', version=version, - description='ACME protocol implementation in Python', - url='https://github.com/letsencrypt/letsencrypt', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 5 - Production/Stable', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: Apache Software License', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - ], - - packages=find_packages(), - include_package_data=True, - install_requires=install_requires, - extras_require={ - 'dev': dev_extras, - 'docs': docs_extras, - }, - tests_require=["pytest"], - test_suite='acme', - cmdclass={"test": PyTest}, ) diff --git a/acme/src/acme/__init__.py b/acme/src/acme/__init__.py new file mode 100644 index 00000000000..405a45252f1 --- /dev/null +++ b/acme/src/acme/__init__.py @@ -0,0 +1,21 @@ +"""ACME protocol implementation. + +This module is an implementation of the `ACME protocol`_. + +.. _`ACME protocol`: https://datatracker.ietf.org/doc/html/rfc8555 + +""" +import sys + +# This code exists to keep backwards compatibility with people using acme.jose +# before it became the standalone josepy package. +# +# It is based on +# https://github.com/requests/requests/blob/1278ecdf71a312dc2268f3bfc0aabfab3c006dcf/requests/packages.py +import josepy as jose # noqa: F401 + +for mod in list(sys.modules): + # This traversal is apparently necessary such that the identities are + # preserved (acme.jose.* is josepy.*) + if mod == 'josepy' or mod.startswith('josepy.'): + sys.modules['acme.' + mod.replace('josepy', 'jose', 1)] = sys.modules[mod] diff --git a/acme/src/acme/_internal/__init__.py b/acme/src/acme/_internal/__init__.py new file mode 100644 index 00000000000..bc855fd7b85 --- /dev/null +++ b/acme/src/acme/_internal/__init__.py @@ -0,0 +1 @@ +"""acme's internal implementation""" diff --git a/acme/src/acme/_internal/tests/__init__.py b/acme/src/acme/_internal/tests/__init__.py new file mode 100644 index 00000000000..9e17591c46b --- /dev/null +++ b/acme/src/acme/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""acme tests""" diff --git a/acme/src/acme/_internal/tests/challenges_test.py b/acme/src/acme/_internal/tests/challenges_test.py new file mode 100644 index 00000000000..cd604328756 --- /dev/null +++ b/acme/src/acme/_internal/tests/challenges_test.py @@ -0,0 +1,399 @@ +"""Tests for acme.challenges.""" +import sys +from typing import TYPE_CHECKING +import unittest +from unittest import mock +import urllib.parse as urllib_parse + +import josepy as jose +from josepy.jwk import JWKEC +import pytest +import requests + +from acme._internal.tests import test_util + +CERT = test_util.load_cert('cert.pem') +KEY = jose.JWKRSA(key=test_util.load_rsa_private_key('rsa512_key.pem')) + + +class ChallengeTest(unittest.TestCase): + + def test_from_json_unrecognized(self): + from acme.challenges import Challenge + from acme.challenges import UnrecognizedChallenge + chall = UnrecognizedChallenge({"type": "foo"}) + assert chall == Challenge.from_json(chall.jobj) + + +class UnrecognizedChallengeTest(unittest.TestCase): + + def setUp(self): + from acme.challenges import UnrecognizedChallenge + self.jobj = {"type": "foo"} + self.chall = UnrecognizedChallenge(self.jobj) + + def test_to_partial_json(self): + assert self.jobj == self.chall.to_partial_json() + + def test_from_json(self): + from acme.challenges import UnrecognizedChallenge + assert self.chall == UnrecognizedChallenge.from_json(self.jobj) + + +class KeyAuthorizationChallengeResponseTest(unittest.TestCase): + + def setUp(self): + def _encode(name): + assert name == "token" + return "foo" + self.chall = mock.Mock() + self.chall.encode.side_effect = _encode + + def test_verify_ok(self): + from acme.challenges import KeyAuthorizationChallengeResponse + response = KeyAuthorizationChallengeResponse( + key_authorization='foo.oKGqedy-b-acd5eoybm2f-NVFxvyOoET5CNy3xnv8WY') + assert response.verify(self.chall, KEY.public_key()) + + def test_verify_wrong_token(self): + from acme.challenges import KeyAuthorizationChallengeResponse + response = KeyAuthorizationChallengeResponse( + key_authorization='bar.oKGqedy-b-acd5eoybm2f-NVFxvyOoET5CNy3xnv8WY') + assert not response.verify(self.chall, KEY.public_key()) + + def test_verify_wrong_thumbprint(self): + from acme.challenges import KeyAuthorizationChallengeResponse + response = KeyAuthorizationChallengeResponse( + key_authorization='foo.oKGqedy-b-acd5eoybm2f-NVFxv') + assert not response.verify(self.chall, KEY.public_key()) + + def test_verify_wrong_form(self): + from acme.challenges import KeyAuthorizationChallengeResponse + response = KeyAuthorizationChallengeResponse( + key_authorization='.foo.oKGqedy-b-acd5eoybm2f-' + 'NVFxvyOoET5CNy3xnv8WY') + assert not response.verify(self.chall, KEY.public_key()) + + +class DNS01ResponseTest(unittest.TestCase): + + def setUp(self): + from acme.challenges import DNS01Response + self.msg = DNS01Response(key_authorization=u'foo') + self.jmsg = { + 'resource': 'challenge', + 'type': 'dns-01', + 'keyAuthorization': u'foo', + } + + from acme.challenges import DNS01 + self.chall = DNS01(token=(b'x' * 16)) + self.response = self.chall.response(KEY) + + def test_to_partial_json(self): + assert {} == self.msg.to_partial_json() + + def test_from_json(self): + from acme.challenges import DNS01Response + assert self.msg == DNS01Response.from_json(self.jmsg) + + def test_from_json_hashable(self): + from acme.challenges import DNS01Response + hash(DNS01Response.from_json(self.jmsg)) + + def test_simple_verify_failure(self): + key2 = jose.JWKRSA.load(test_util.load_vector('rsa256_key.pem')) + public_key = key2.public_key() + verified = self.response.simple_verify(self.chall, "local", public_key) + assert not verified + + def test_simple_verify_success(self): + public_key = KEY.public_key() + verified = self.response.simple_verify(self.chall, "local", public_key) + assert verified + + +class DNS01Test(unittest.TestCase): + + def setUp(self): + from acme.challenges import DNS01 + self.msg = DNS01(token=jose.decode_b64jose( + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ+PCt92wr+oA')) + self.jmsg = { + 'type': 'dns-01', + 'token': 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA', + } + + def test_validation_domain_name(self): + assert '_acme-challenge.www.example.com' == \ + self.msg.validation_domain_name('www.example.com') + + def test_validation(self): + assert "rAa7iIg4K2y63fvUhCfy8dP1Xl7wEhmQq0oChTcE3Zk" == \ + self.msg.validation(KEY) + + def test_to_partial_json(self): + assert self.jmsg == self.msg.to_partial_json() + + def test_from_json(self): + from acme.challenges import DNS01 + assert self.msg == DNS01.from_json(self.jmsg) + + def test_from_json_hashable(self): + from acme.challenges import DNS01 + hash(DNS01.from_json(self.jmsg)) + + +class HTTP01ResponseTest(unittest.TestCase): + + def setUp(self): + from acme.challenges import HTTP01Response + self.msg = HTTP01Response(key_authorization=u'foo') + self.jmsg = { + 'resource': 'challenge', + 'type': 'http-01', + 'keyAuthorization': u'foo', + } + + from acme.challenges import HTTP01 + self.chall = HTTP01(token=(b'x' * 16)) + self.response = self.chall.response(KEY) + + def test_to_partial_json(self): + assert {} == self.msg.to_partial_json() + + def test_from_json(self): + from acme.challenges import HTTP01Response + assert self.msg == HTTP01Response.from_json(self.jmsg) + + def test_from_json_hashable(self): + from acme.challenges import HTTP01Response + hash(HTTP01Response.from_json(self.jmsg)) + + def test_simple_verify_bad_key_authorization(self): + key2 = jose.JWKRSA.load(test_util.load_vector('rsa256_key.pem')) + self.response.simple_verify(self.chall, "local", key2.public_key()) + + @mock.patch("acme.challenges.requests.get") + def test_simple_verify_good_validation(self, mock_get): + validation = self.chall.validation(KEY) + mock_get.return_value = mock.MagicMock(text=validation) + assert self.response.simple_verify( + self.chall, "local", KEY.public_key()) + mock_get.assert_called_once_with(self.chall.uri("local"), verify=False, + timeout=mock.ANY) + + @mock.patch("acme.challenges.requests.get") + def test_simple_verify_bad_validation(self, mock_get): + mock_get.return_value = mock.MagicMock(text="!") + assert not self.response.simple_verify( + self.chall, "local", KEY.public_key()) + + @mock.patch("acme.challenges.requests.get") + def test_simple_verify_whitespace_validation(self, mock_get): + from acme.challenges import HTTP01Response + mock_get.return_value = mock.MagicMock( + text=(self.chall.validation(KEY) + + HTTP01Response.WHITESPACE_CUTSET)) + assert self.response.simple_verify( + self.chall, "local", KEY.public_key()) + mock_get.assert_called_once_with(self.chall.uri("local"), verify=False, + timeout=mock.ANY) + + @mock.patch("acme.challenges.requests.get") + def test_simple_verify_connection_error(self, mock_get): + mock_get.side_effect = requests.exceptions.RequestException + assert not self.response.simple_verify( + self.chall, "local", KEY.public_key()) + + @mock.patch("acme.challenges.requests.get") + def test_simple_verify_port(self, mock_get): + self.response.simple_verify( + self.chall, domain="local", + account_public_key=KEY.public_key(), port=8080) + assert "local:8080" == urllib_parse.urlparse( + mock_get.mock_calls[0][1][0]).netloc + + @mock.patch("acme.challenges.requests.get") + def test_simple_verify_timeout(self, mock_get): + self.response.simple_verify(self.chall, "local", KEY.public_key()) + mock_get.assert_called_once_with(self.chall.uri("local"), verify=False, + timeout=30) + mock_get.reset_mock() + self.response.simple_verify(self.chall, "local", KEY.public_key(), timeout=1234) + mock_get.assert_called_once_with(self.chall.uri("local"), verify=False, + timeout=1234) + + +class HTTP01Test(unittest.TestCase): + + def setUp(self): + from acme.challenges import HTTP01 + self.msg = HTTP01( + token=jose.decode_b64jose( + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ+PCt92wr+oA')) + self.jmsg = { + 'type': 'http-01', + 'token': 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA', + } + + def test_path(self): + assert self.msg.path == '/.well-known/acme-challenge/' \ + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA' + + def test_uri(self): + assert 'http://example.com/.well-known/acme-challenge/' \ + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA' == \ + self.msg.uri('example.com') + assert 'http://1.2.3.4/.well-known/acme-challenge/' \ + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA' == \ + self.msg.uri('1.2.3.4') + assert 'http://[::1]/.well-known/acme-challenge/' \ + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA' == \ + self.msg.uri('::1') + + def test_to_partial_json(self): + assert self.jmsg == self.msg.to_partial_json() + + def test_from_json(self): + from acme.challenges import HTTP01 + assert self.msg == HTTP01.from_json(self.jmsg) + + def test_from_json_hashable(self): + from acme.challenges import HTTP01 + hash(HTTP01.from_json(self.jmsg)) + + def test_good_token(self): + assert self.msg.good_token + assert not self.msg.update(token=b'..').good_token + + +class TestDNS: + + if TYPE_CHECKING: + from acme.challenges import DNS + + @pytest.fixture + def jmsg(self) -> dict: + jmsg = { + 'type': 'dns', + 'token': 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA', + } + return jmsg + + @pytest.fixture + def msg(self) -> 'DNS': + from acme.challenges import DNS + msg = DNS(token=jose.b64decode( + b'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA')) + return msg + + def test_to_partial_json(self, msg: 'DNS', jmsg: dict): + assert jmsg == msg.to_partial_json() + + def test_from_json(self, msg: 'DNS', jmsg: dict): + from acme.challenges import DNS + assert msg == DNS.from_json(jmsg) + + def test_from_json_hashable(self, jmsg: dict): + from acme.challenges import DNS + hash(DNS.from_json(jmsg)) + + # Using fixtures in parametrize is an open issue + # https://github.com/pytest-dev/pytest/issues/349 + @pytest.mark.parametrize("key, alg", [ + (KEY, jose.RS256), + (JWKEC(key=test_util.load_ecdsa_private_key('ec_secp384r1_key.pem')), jose.ES384)]) + def test_gen_check_validation(self, key, alg, msg: 'DNS'): + assert msg.check_validation( + msg.gen_validation(key, alg=alg), key.public_key()) + + def test_gen_check_validation_wrong_key(self, msg: 'DNS'): + key2 = jose.JWKRSA.load(test_util.load_vector('rsa1024_key.pem')) + assert not msg.check_validation( + msg.gen_validation(KEY), key2.public_key()) + + def test_check_validation_wrong_payload(self, msg: 'DNS'): + validations = tuple( + jose.JWS.sign(payload=payload, alg=jose.RS256, key=KEY) + for payload in (b'', b'{}') + ) + for validation in validations: + assert not msg.check_validation( + validation, KEY.public_key()) + + def test_check_validation_wrong_fields(self, msg: 'DNS'): + bad_validation = jose.JWS.sign( + payload=msg.update( + token=b'x' * 20).json_dumps().encode('utf-8'), + alg=jose.RS256, key=KEY) + assert not msg.check_validation(bad_validation, KEY.public_key()) + + def test_gen_response(self, msg: 'DNS'): + with mock.patch('acme.challenges.DNS.gen_validation') as mock_gen: + mock_gen.return_value = mock.sentinel.validation + response = msg.gen_response(KEY) + from acme.challenges import DNSResponse + assert isinstance(response, DNSResponse) + assert response.validation == mock.sentinel.validation + + def test_validation_domain_name(self, msg: 'DNS'): + assert '_acme-challenge.le.wtf' == msg.validation_domain_name('le.wtf') + + def test_validation_domain_name_ecdsa(self, msg: 'DNS'): + ec_key_secp384r1 = JWKEC(key=test_util.load_ecdsa_private_key('ec_secp384r1_key.pem')) + assert msg.check_validation( + msg.gen_validation(ec_key_secp384r1, alg=jose.ES384), + ec_key_secp384r1.public_key()) is True + + +class DNSResponseTest(unittest.TestCase): + + def setUp(self): + from acme.challenges import DNS + self.chall = DNS(token=jose.b64decode( + b"evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA")) + self.validation = jose.JWS.sign( + payload=self.chall.json_dumps(sort_keys=True).encode(), + key=KEY, alg=jose.RS256) + + from acme.challenges import DNSResponse + self.msg = DNSResponse(validation=self.validation) + self.jmsg_to = { + 'validation': self.validation, + } + self.jmsg_from = { + 'resource': 'challenge', + 'type': 'dns', + 'validation': self.validation.to_json(), + } + + def test_to_partial_json(self): + assert self.jmsg_to == self.msg.to_partial_json() + + def test_from_json(self): + from acme.challenges import DNSResponse + assert self.msg == DNSResponse.from_json(self.jmsg_from) + + def test_from_json_hashable(self): + from acme.challenges import DNSResponse + hash(DNSResponse.from_json(self.jmsg_from)) + + def test_check_validation(self): + assert self.msg.check_validation(self.chall, KEY.public_key()) + + +class JWSPayloadRFC8555Compliant(unittest.TestCase): + """Test for RFC8555 compliance of JWS generated from resources/challenges""" + def test_challenge_payload(self): + from acme.challenges import HTTP01Response + + challenge_body = HTTP01Response() + + jobj = challenge_body.json_dumps(indent=2).encode() + # RFC8555 states that challenge responses must have an empty payload. + assert jobj == b'{}' + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/_internal/tests/client_test.py b/acme/src/acme/_internal/tests/client_test.py new file mode 100644 index 00000000000..0ba861c23a9 --- /dev/null +++ b/acme/src/acme/_internal/tests/client_test.py @@ -0,0 +1,1067 @@ +"""Tests for acme.client.""" +# pylint: disable=too-many-lines +import copy +import datetime +import http.client as http_client +import json +import sys +import unittest +from unittest import mock + +from cryptography import x509 + +import josepy as jose +import pytest +import requests + +from acme import challenges +from acme import errors +from acme import jws as acme_jws +from acme import messages +from acme._internal.tests import messages_test +from acme._internal.tests import test_util +from acme.client import ClientNetwork +from acme.client import ClientV2 + +CERT_SAN_PEM = test_util.load_vector('cert-san.pem') +CSR_MIXED_PEM = test_util.load_vector('csr-mixed.pem') +CSR_NO_SANS_PEM = test_util.load_vector('csr-nosans.pem') +KEY = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem')) + +DIRECTORY_V2 = messages.Directory({ + 'newAccount': 'https://www.letsencrypt-demo.org/acme/new-account', + 'newNonce': 'https://www.letsencrypt-demo.org/acme/new-nonce', + 'newOrder': 'https://www.letsencrypt-demo.org/acme/new-order', + 'revokeCert': 'https://www.letsencrypt-demo.org/acme/revoke-cert', + 'meta': messages.Directory.Meta(), +}) + + +class ClientV2Test(unittest.TestCase): + """Tests for acme.client.ClientV2.""" + + def setUp(self): + self.response = mock.MagicMock( + ok=True, status_code=http_client.OK, headers={}, links={}) + self.net = mock.MagicMock() + self.net.post.return_value = self.response + self.net.get.return_value = self.response + + self.identifier = messages.Identifier( + typ=messages.IDENTIFIER_FQDN, value='example.com') + + # Registration + self.contact = ('mailto:cert-admin@example.com', 'tel:+12025551212') + reg = messages.Registration( + contact=self.contact, key=KEY.public_key()) + the_arg: dict = dict(reg) + self.new_reg = messages.NewRegistration(**the_arg) + self.regr = messages.RegistrationResource( + body=reg, uri='https://www.letsencrypt-demo.org/acme/reg/1') + + # Authorization + authzr_uri = 'https://www.letsencrypt-demo.org/acme/authz/1' + challb = messages.ChallengeBody( + uri=(authzr_uri + '/1'), status=messages.STATUS_VALID, + chall=challenges.DNS(token=jose.b64decode( + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA'))) + self.challr = messages.ChallengeResource( + body=challb, authzr_uri=authzr_uri) + self.authz = messages.Authorization( + identifier=messages.Identifier( + typ=messages.IDENTIFIER_FQDN, value='example.com'), + challenges=(challb,)) + self.authzr = messages.AuthorizationResource( + body=self.authz, uri=authzr_uri) + + # Reason code for revocation + self.rsn = 1 + + self.directory = DIRECTORY_V2 + + self.client = ClientV2(self.directory, self.net) + + self.new_reg = self.new_reg.update(terms_of_service_agreed=True) + + self.authzr_uri2 = 'https://www.letsencrypt-demo.org/acme/authz/2' + self.authz2 = self.authz.update(identifier=messages.Identifier( + typ=messages.IDENTIFIER_FQDN, value='www.example.com'), + status=messages.STATUS_PENDING) + self.authzr2 = messages.AuthorizationResource( + body=self.authz2, uri=self.authzr_uri2) + + self.order = messages.Order( + identifiers=(self.authz.identifier, self.authz2.identifier), + status=messages.STATUS_PENDING, + authorizations=(self.authzr.uri, self.authzr_uri2), + finalize='https://www.letsencrypt-demo.org/acme/acct/1/order/1/finalize') + self.orderr = messages.OrderResource( + body=self.order, + uri='https://www.letsencrypt-demo.org/acme/acct/1/order/1', + authorizations=[self.authzr, self.authzr2], csr_pem=CSR_MIXED_PEM) + self.orderr2 = messages.OrderResource( + body=self.order, + uri='https://www.letsencrypt-demo.org/acme/acct/1/order/1', + authorizations=[self.authzr, self.authzr2], csr_pem=CSR_NO_SANS_PEM) + + def test_new_account(self): + self.response.status_code = http_client.CREATED + self.response.json.return_value = self.regr.body.to_json() + self.response.headers['Location'] = self.regr.uri + + assert self.regr == self.client.new_account(self.new_reg) + + def test_new_account_tos_link(self): + self.response.status_code = http_client.CREATED + self.response.json.return_value = self.regr.body.to_json() + self.response.headers['Location'] = self.regr.uri + self.response.links.update({ + 'terms-of-service': {'url': 'https://www.letsencrypt-demo.org/tos'}, + }) + + assert self.client.new_account(self.new_reg).terms_of_service == \ + 'https://www.letsencrypt-demo.org/tos' + + + def test_new_account_conflict(self): + self.response.status_code = http_client.OK + self.response.headers['Location'] = self.regr.uri + with pytest.raises(errors.ConflictError): + self.client.new_account(self.new_reg) + + def test_deactivate_account(self): + deactivated_regr = self.regr.update( + body=self.regr.body.update(status='deactivated')) + self.response.json.return_value = deactivated_regr.body.to_json() + self.response.status_code = http_client.OK + self.response.headers['Location'] = self.regr.uri + assert self.client.deactivate_registration(self.regr) == deactivated_regr + + def test_deactivate_authorization(self): + deactivated_authz = self.authzr.update( + body=self.authzr.body.update(status=messages.STATUS_DEACTIVATED)) + self.response.json.return_value = deactivated_authz.body.to_json() + authzr = self.client.deactivate_authorization(self.authzr) + assert deactivated_authz.body == authzr.body + assert self.client.net.post.call_count == 1 + assert self.authzr.uri in self.net.post.call_args_list[0][0] + + def test_new_order(self): + order_response = copy.deepcopy(self.response) + order_response.status_code = http_client.CREATED + order_response.json.return_value = self.order.to_json() + order_response.headers['Location'] = self.orderr.uri + self.net.post.return_value = order_response + + authz_response = copy.deepcopy(self.response) + authz_response.json.return_value = self.authz.to_json() + authz_response.headers['Location'] = self.authzr.uri + authz_response2 = self.response + authz_response2.json.return_value = self.authz2.to_json() + authz_response2.headers['Location'] = self.authzr2.uri + + with mock.patch('acme.client.ClientV2._post_as_get') as mock_post_as_get: + mock_post_as_get.side_effect = (authz_response, authz_response2) + assert self.client.new_order(CSR_MIXED_PEM) == self.orderr + + with mock.patch('acme.client.ClientV2._post_as_get') as mock_post_as_get: + mock_post_as_get.side_effect = (authz_response, authz_response2) + assert self.client.new_order(CSR_NO_SANS_PEM) == self.orderr2 + + def test_answer_challege(self): + self.response.links['up'] = {'url': self.challr.authzr_uri} + self.response.json.return_value = self.challr.body.to_json() + chall_response = challenges.DNSResponse(validation=None) + self.client.answer_challenge(self.challr.body, chall_response) + + with pytest.raises(errors.UnexpectedUpdate): + self.client.answer_challenge(self.challr.body.update(uri='foo'), chall_response) + + def test_answer_challenge_missing_next(self): + with pytest.raises(errors.ClientError): + self.client.answer_challenge(self.challr.body, challenges.DNSResponse(validation=None)) + + @mock.patch('acme.client.datetime') + def test_poll_and_finalize(self, mock_datetime): + mock_datetime.datetime.now.return_value = datetime.datetime(2018, 2, 15) + mock_datetime.timedelta = datetime.timedelta + expected_deadline = mock_datetime.datetime.now() + datetime.timedelta(seconds=90) + + self.client.poll_authorizations = mock.Mock(return_value=self.orderr) + self.client.finalize_order = mock.Mock(return_value=self.orderr) + + assert self.client.poll_and_finalize(self.orderr) == self.orderr + self.client.poll_authorizations.assert_called_once_with(self.orderr, expected_deadline) + self.client.finalize_order.assert_called_once_with(self.orderr, expected_deadline) + + @mock.patch('acme.client.datetime') + def test_poll_authorizations_timeout(self, mock_datetime): + now_side_effect = [datetime.datetime(2018, 2, 15), + datetime.datetime(2018, 2, 16), + datetime.datetime(2018, 2, 17)] + mock_datetime.datetime.now.side_effect = now_side_effect + self.response.json.side_effect = [ + self.authz.to_json(), self.authz2.to_json(), self.authz2.to_json()] + + with pytest.raises(errors.TimeoutError): + self.client.poll_authorizations(self.orderr, now_side_effect[1]) + + def test_poll_authorizations_failure(self): + deadline = datetime.datetime(9999, 9, 9) + challb = self.challr.body.update(status=messages.STATUS_INVALID, + error=messages.Error.with_code('unauthorized')) + authz = self.authz.update(status=messages.STATUS_INVALID, challenges=(challb,)) + self.response.json.return_value = authz.to_json() + + with pytest.raises(errors.ValidationError): + self.client.poll_authorizations(self.orderr, deadline) + + def test_poll_authorizations_success(self): + deadline = datetime.datetime(9999, 9, 9) + updated_authz2 = self.authz2.update(status=messages.STATUS_VALID) + updated_authzr2 = messages.AuthorizationResource( + body=updated_authz2, uri=self.authzr_uri2) + updated_orderr = self.orderr.update(authorizations=[self.authzr, updated_authzr2]) + + self.response.json.side_effect = ( + self.authz.to_json(), self.authz2.to_json(), updated_authz2.to_json()) + assert self.client.poll_authorizations(self.orderr, deadline) == updated_orderr + + def test_poll_unexpected_update(self): + updated_authz = self.authz.update(identifier=self.identifier.update(value='foo')) + self.response.json.return_value = updated_authz.to_json() + with pytest.raises(errors.UnexpectedUpdate): + self.client.poll(self.authzr) + + def test_finalize_order_success(self): + updated_order = self.order.update( + certificate='https://www.letsencrypt-demo.org/acme/cert/', + status=messages.STATUS_VALID) + updated_orderr = self.orderr.update(body=updated_order, fullchain_pem=CERT_SAN_PEM) + + self.response.json.return_value = updated_order.to_json() + self.response.text = CERT_SAN_PEM + + deadline = datetime.datetime(9999, 9, 9) + assert self.client.finalize_order(self.orderr, deadline) == updated_orderr + + def test_finalize_order_error(self): + updated_order = self.order.update( + error=messages.Error.with_code('unauthorized'), + status=messages.STATUS_INVALID) + self.response.json.return_value = updated_order.to_json() + + deadline = datetime.datetime(9999, 9, 9) + with pytest.raises(errors.IssuanceError): + self.client.finalize_order(self.orderr, deadline) + + @mock.patch('acme.client.ClientV2.begin_finalization') + def test_finalize_order_ready(self, mock_begin): + # https://github.com/certbot/certbot/issues/9766 + updated_order_ready = self.order.update(status=messages.STATUS_READY) + + updated_order_valid = self.order.update( + certificate='https://www.letsencrypt-demo.org/acme/cert/', + status=messages.STATUS_VALID) + updated_orderr = self.orderr.update(body=updated_order_valid, fullchain_pem=CERT_SAN_PEM) + + self.response.text = CERT_SAN_PEM + + self.response.json.side_effect = [updated_order_ready.to_json(), + updated_order_valid.to_json()] + + deadline = datetime.datetime(9999, 9, 9) + assert self.client.finalize_order(self.orderr, deadline) == updated_orderr + assert self.response.json.call_count == 2 + assert mock_begin.call_count == 2 + + def test_finalize_order_invalid_status(self): + # https://github.com/certbot/certbot/issues/9296 + order = self.order.update(error=None, status=messages.STATUS_INVALID) + self.response.json.return_value = order.to_json() + with pytest.raises(errors.Error, match="The certificate order failed"): + self.client.finalize_order(self.orderr, datetime.datetime(9999, 9, 9)) + + @mock.patch('acme.client.time.sleep') + @mock.patch('acme.client.datetime') + def test_finalize_order_orderNotReady(self, dt_mock, mock_sleep): + # https://github.com/certbot/certbot/issues/9766 + updated_order_processing = self.order.update(status=messages.STATUS_PROCESSING) + updated_order_ready = self.order.update(status=messages.STATUS_READY) + + updated_order_valid = self.order.update( + certificate='https://www.letsencrypt-demo.org/acme/cert/', + status=messages.STATUS_VALID) + self.orderr.update(body=updated_order_valid, fullchain_pem=CERT_SAN_PEM) + + self.response.text = CERT_SAN_PEM + + self.response.json.side_effect = [updated_order_processing.to_json(), + updated_order_ready.to_json(), + updated_order_valid.to_json()] + + dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27) + dt_mock.timedelta = datetime.timedelta + self.response.headers['Retry-After'] = '50' + + post = mock.MagicMock() + post.side_effect = [messages.Error.with_code('orderNotReady'), # first begin_finalization + # sleep 1 + self.response, # first poll_finalization poll --> returns processing + # retry-after sleep here + self.response, # second poll_finalization poll --> returns ready + mock.MagicMock(), # second begin_finalization + # sleep 1 + self.response, # third poll_finalization poll --> returns valid + self.response # fetch cert + ] + self.net.post = post + + self.client.finalize_order(self.orderr, datetime.datetime(9999, 9, 9)) + assert self.net.post.call_count == 6 + assert mock_sleep.call_args_list == [((1,),), ((50,),), ((1,),)] + + def test_finalize_order_otherErrorCode(self): + post = mock.MagicMock() + post.side_effect = [messages.Error.with_code('serverInternal')] + self.net.post = post + + with pytest.raises(messages.Error): + self.client.finalize_order(self.orderr, datetime.datetime(9999, 9, 9)) + + def test_finalize_order_timeout(self): + deadline = datetime.datetime.now() - datetime.timedelta(seconds=60) + with pytest.raises(errors.TimeoutError): + self.client.finalize_order(self.orderr, deadline) + + def test_finalize_order_alt_chains(self): + updated_order = self.order.update( + certificate='https://www.letsencrypt-demo.org/acme/cert/', + status=messages.STATUS_VALID + ) + updated_orderr = self.orderr.update(body=updated_order, + fullchain_pem=CERT_SAN_PEM, + alternative_fullchains_pem=[CERT_SAN_PEM, + CERT_SAN_PEM]) + self.response.json.return_value = updated_order.to_json() + self.response.text = CERT_SAN_PEM + self.response.headers['Link'] =';rel="alternate", ' + \ + ';rel="index", ' + \ + ';title="foo";rel="alternate"' + + deadline = datetime.datetime(9999, 9, 9) + resp = self.client.finalize_order(self.orderr, deadline, fetch_alternative_chains=True) + self.net.post.assert_any_call('https://example.com/acme/cert/1', + mock.ANY, new_nonce_url=mock.ANY) + self.net.post.assert_any_call('https://example.com/acme/cert/2', + mock.ANY, new_nonce_url=mock.ANY) + assert resp == updated_orderr + + del self.response.headers['Link'] + resp = self.client.finalize_order(self.orderr, deadline, fetch_alternative_chains=True) + assert resp == updated_orderr.update(alternative_fullchains_pem=[]) + + def test_revoke(self): + self.client.revoke(messages_test.CERT, self.rsn) + self.net.post.assert_called_once_with( + self.directory["revokeCert"], mock.ANY, new_nonce_url=DIRECTORY_V2['newNonce']) + + def test_revoke_bad_status_raises_error(self): + self.response.status_code = http_client.METHOD_NOT_ALLOWED + with pytest.raises(errors.ClientError): + self.client.revoke(messages_test.CERT, + self.rsn) + + def test_update_registration(self): + # "Instance of 'Field' has no to_json/update member" bug: + self.response.headers['Location'] = self.regr.uri + self.response.json.return_value = self.regr.body.to_json() + assert self.regr == self.client.update_registration(self.regr) + assert self.client.net.account is not None + assert self.client.net.post.call_count == 2 + assert DIRECTORY_V2.newAccount in self.net.post.call_args_list[0][0] + + self.response.json.return_value = self.regr.body.update( + contact=()).to_json() + + def test_external_account_required_true(self): + self.client.directory = messages.Directory({ + 'meta': messages.Directory.Meta(external_account_required=True) + }) + + assert self.client.external_account_required() + + def test_external_account_required_false(self): + self.client.directory = messages.Directory({ + 'meta': messages.Directory.Meta(external_account_required=False) + }) + + assert not self.client.external_account_required() + + def test_external_account_required_default(self): + assert not self.client.external_account_required() + + def test_query_registration_client(self): + self.response.json.return_value = self.regr.body.to_json() + self.response.headers['Location'] = 'https://www.letsencrypt-demo.org/acme/reg/1' + assert self.regr == self.client.query_registration(self.regr) + + def test_post_as_get(self): + with mock.patch('acme.client.ClientV2._authzr_from_response') as mock_client: + mock_client.return_value = self.authzr2 + + self.client.poll(self.authzr2) + + self.client.net.post.assert_called_once_with( + self.authzr2.uri, None, + new_nonce_url='https://www.letsencrypt-demo.org/acme/new-nonce') + self.client.net.get.assert_not_called() + + def test_retry_after_date(self): + self.response.headers['Retry-After'] = 'Fri, 31 Dec 1999 23:59:59 GMT' + assert datetime.datetime(1999, 12, 31, 23, 59, 59) == \ + self.client.retry_after(response=self.response, default=10) + + @mock.patch('acme.client.datetime') + def test_retry_after_invalid(self, dt_mock): + dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27) + dt_mock.timedelta = datetime.timedelta + + self.response.headers['Retry-After'] = 'foooo' + assert datetime.datetime(2015, 3, 27, 0, 0, 10) == \ + self.client.retry_after(response=self.response, default=10) + + @mock.patch('acme.client.datetime') + def test_retry_after_overflow(self, dt_mock): + dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27) + dt_mock.timedelta = datetime.timedelta + dt_mock.datetime.side_effect = datetime.datetime + + self.response.headers['Retry-After'] = "Tue, 116 Feb 2016 11:50:00 MST" + assert datetime.datetime(2015, 3, 27, 0, 0, 10) == \ + self.client.retry_after(response=self.response, default=10) + + @mock.patch('acme.client.datetime') + def test_retry_after_seconds(self, dt_mock): + dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27) + dt_mock.timedelta = datetime.timedelta + + self.response.headers['Retry-After'] = '50' + assert datetime.datetime(2015, 3, 27, 0, 0, 50) == \ + self.client.retry_after(response=self.response, default=10) + + @mock.patch('acme.client.datetime') + def test_retry_after_missing(self, dt_mock): + dt_mock.datetime.now.return_value = datetime.datetime(2015, 3, 27) + dt_mock.timedelta = datetime.timedelta + + assert datetime.datetime(2015, 3, 27, 0, 0, 10) == \ + self.client.retry_after(response=self.response, default=10) + + def test_get_directory(self): + self.response.json.return_value = DIRECTORY_V2.to_json() + assert DIRECTORY_V2.to_partial_json() == \ + ClientV2.get_directory('https://example.com/dir', self.net).to_partial_json() + + @mock.patch('acme.client.datetime') + def test_renewal_time_expired_cert(self, dt_mock): + utc_now = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) + dt_mock.datetime.now.return_value = utc_now + + cert_pem = make_cert_for_renewal( + not_before=datetime.datetime(2025, 3, 12, 00, 00, 00), + not_after=datetime.datetime(2025, 3, 20, 00, 00, 00), + ) + cert = x509.load_pem_x509_certificate(cert_pem) + + t, _ = self.client.renewal_time(cert_pem) + assert t == cert.not_valid_after_utc + + @mock.patch('acme.client.datetime') + def test_renewal_time_no_renewal_info(self, dt_mock): + utc_now = datetime.datetime(2025, 3, 15, tzinfo=datetime.timezone.utc) + dt_mock.datetime.now.return_value = utc_now + # A directory with no 'renewalInfo' should result in None. + self.client.directory = messages.Directory({}) + cert_pem = make_cert_for_renewal( + not_before=datetime.datetime(2025, 3, 12, 00, 00, 00), + not_after=datetime.datetime(2025, 3, 20, 00, 00, 00), + ) + t, _ = self.client.renewal_time(cert_pem) + assert t is None + + cert_pem = make_cert_for_renewal( + not_before=datetime.datetime(2025, 3, 12, 00, 00, 00), + not_after=datetime.datetime(2025, 3, 30, 00, 00, 00), + ) + t, _ = self.client.renewal_time(cert_pem) + assert t is None + + @mock.patch('acme.client.datetime') + def test_renewal_time_with_renewal_info(self, dt_mock): + from cryptography import x509 + from acme.client import _renewal_info_path_component + utc_now = datetime.datetime(2025, 3, 15, tzinfo=datetime.timezone.utc) + dt_mock.datetime.now.return_value = utc_now + dt_mock.timedelta = datetime.timedelta + + cert_pem = make_cert_for_renewal( + not_before=datetime.datetime(2025, 3, 12, 00, 00, 00), + not_after=datetime.datetime(2025, 3, 20, 00, 00, 00), + ) + + self.client.directory = messages.Directory({ + 'renewalInfo': 'https://www.letsencrypt-demo.org/acme/renewal-info', + }) + + self.response.json.return_value = { + "suggestedWindow": { + "start": "2025-03-14T01:01:01Z", + "end": "2025-03-14T01:01:01Z", + }, + "message": "Keep those certs fresh" + } + t, _ = self.client.renewal_time(cert_pem) + cert_parsed = x509.load_pem_x509_certificate(cert_pem) + ari_path_component = _renewal_info_path_component(cert_parsed) + self.net.get.assert_called_once_with("https://www.letsencrypt-demo.org/acme/renewal-info/" + + ari_path_component, + content_type='application/json') + assert t == datetime.datetime(2025, 3, 14, 1, 1, 1, tzinfo=datetime.timezone.utc) + + self.net.reset_mock() + + self.response.json.return_value = { + "suggestedWindow": { + "start": "2025-03-16T01:01:01Z", + "end": "2025-03-17T01:01:01Z", + }, + "message": "Keep those certs fresh" + } + t, _ = self.client.renewal_time(cert_pem) + self.net.get.assert_called_once_with("https://www.letsencrypt-demo.org/acme/renewal-info/" + + ari_path_component, + content_type='application/json') + assert t >= datetime.datetime(2025, 3, 16, 1, 1, 1, tzinfo=datetime.timezone.utc) + assert t <= datetime.datetime(2025, 3, 17, 1, 1, 1, tzinfo=datetime.timezone.utc) + + @mock.patch('acme.client.datetime') + def test_renewal_time_renewal_info_errors(self, dt_mock): + def now(tzinfo=None): + return datetime.datetime(2025, 3, 15, tzinfo=tzinfo) + dt_mock.datetime.now.side_effect = now + dt_mock.timedelta = datetime.timedelta + dt_mock.timezone = datetime.timezone + + self.client.directory = messages.Directory({ + 'renewalInfo': 'https://www.letsencrypt-demo.org/acme/renewal-info', + }) + # Failure to fetch the 'renewalInfo' URL should raise an ARIError with the exception raised + # by self.net.get as the __cause__ and a Retry-After 6 hours in the future + expected_cause = requests.exceptions.RequestException + expected_retry_after = now() + datetime.timedelta(seconds=6 * 60 * 60) + self.net.get.side_effect = expected_cause + + cert_pem = make_cert_for_renewal( + not_before=datetime.datetime(2025, 3, 12, 00, 00, 00), + not_after=datetime.datetime(2025, 3, 20, 00, 00, 00), + ) + with pytest.raises(errors.ARIError) as exception_info: + self.client.renewal_time(cert_pem) + assert isinstance(exception_info.value.__cause__, expected_cause) + assert exception_info.value.retry_after == expected_retry_after + + cert_pem = make_cert_for_renewal( + not_before=datetime.datetime(2025, 3, 12, 00, 00, 00), + not_after=datetime.datetime(2025, 3, 30, 00, 00, 00), + ) + with pytest.raises(errors.ARIError) as exception_info: + self.client.renewal_time(cert_pem) + assert isinstance(exception_info.value.__cause__, expected_cause) + assert exception_info.value.retry_after == expected_retry_after + + @mock.patch('acme.client.datetime') + def test_renewal_time_returns_retry_after(self, dt_mock): + def now(tzinfo=None): + return datetime.datetime(2025, 3, 15, tzinfo=tzinfo) + dt_mock.datetime.now.side_effect = now + dt_mock.timedelta = datetime.timedelta + dt_mock.timezone = datetime.timezone + + self.client.directory = messages.Directory({ + 'renewalInfo': 'https://www.letsencrypt-demo.org/acme/renewal-info', + }) + cert_pem = make_cert_for_renewal( + not_before=datetime.datetime(2025, 3, 12, 00, 00, 00), + not_after=datetime.datetime(2025, 3, 20, 00, 00, 00), + ) + self.response.json.return_value = { + "suggestedWindow": { + "start": "2025-03-14T01:01:01Z", + "end": "2025-03-14T01:01:01Z", + }, + "message": "Keep those certs fresh" + } + + # With no explicit Retry-After in header, default to six hours + _, retry_after = self.client.renewal_time(cert_pem) + assert retry_after == datetime.datetime(2025, 3, 15, 6, 0, 0) + + # With an explicit Retry-After in header, use that + self.response.headers['Retry-After'] = '100' + _, retry_after = self.client.renewal_time(cert_pem) + assert retry_after == datetime.datetime(2025, 3, 15, 00, 1, 40) + +def test_renewal_info_path_component(): + from cryptography import x509 + from acme.client import _renewal_info_path_component + + cert = x509.load_pem_x509_certificate(test_util.load_vector('rsa2048_cert.pem')) + + assert _renewal_info_path_component(cert) == "fL5sRirC8VS5AtOQh9DfoAzYNCI.ALVG_VbBb5U7" + + # From https://www.ietf.org/archive/id/draft-ietf-acme-ari-08.html appendix A. + ARI_TEST_CERT = b""" +-----BEGIN CERTIFICATE----- +MIIBQzCB66ADAgECAgUAh2VDITAKBggqhkjOPQQDAjAVMRMwEQYDVQQDEwpFeGFt +cGxlIENBMCIYDzAwMDEwMTAxMDAwMDAwWhgPMDAwMTAxMDEwMDAwMDBaMBYxFDAS +BgNVBAMTC2V4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEeBZu +7cbpAYNXZLbbh8rNIzuOoqOOtmxA1v7cRm//AwyMwWxyHz4zfwmBhcSrf47NUAFf +qzLQ2PPQxdTXREYEnKMjMCEwHwYDVR0jBBgwFoAUaYhba4dGQEHhs3uEe6CuLN4B +yNQwCgYIKoZIzj0EAwIDRwAwRAIge09+S5TZAlw5tgtiVvuERV6cT4mfutXIlwTb ++FYN/8oCIClDsqBklhB9KAelFiYt9+6FDj3z4KGVelYM5MdsO3pK +-----END CERTIFICATE----- +""" + + cert = x509.load_pem_x509_certificate(ARI_TEST_CERT) + assert _renewal_info_path_component(cert) == "aYhba4dGQEHhs3uEe6CuLN4ByNQ.AIdlQyE" + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover + +def make_cert_for_renewal(not_before, not_after) -> bytes: + """ + Return a PEM-encoded, self-signed certificate with the given dates. + """ + from cryptography import x509 + from cryptography.hazmat.primitives.asymmetric import ec + from cryptography.hazmat.primitives import serialization, hashes + # AKID and serial are the inputs to constructing the renewalInfo URL + akid = x509.AuthorityKeyIdentifier(b"1234", None, None) + serial = 56789 + key = ec.generate_private_key(ec.SECP256R1()) + cert = x509.CertificateBuilder( + issuer_name=x509.Name([x509.NameAttribute(x509.oid.NameOID.COMMON_NAME, "Some Issuer")]), + subject_name=x509.Name([]), + public_key=key.public_key(), + serial_number=serial, + not_valid_before=not_before, + not_valid_after=not_after, + ).add_extension( + x509.SubjectAlternativeName([x509.DNSName('example.com')]), + critical=False, + ).add_extension( + akid, + critical=False, + ).sign( + private_key=key, + algorithm=hashes.SHA256(), + ) + return cert.public_bytes(serialization.Encoding.PEM) + +class MockJSONDeSerializable(jose.JSONDeSerializable): + # pylint: disable=missing-docstring + def __init__(self, value): + self.value = value + + def to_partial_json(self): + return {'foo': self.value} + + @classmethod + def from_json(cls, jobj): + pass # pragma: no cover + + +class ClientNetworkTest(unittest.TestCase): + """Tests for acme.client.ClientNetwork.""" + + def setUp(self): + self.verify_ssl = mock.MagicMock() + self.wrap_in_jws = mock.MagicMock(return_value=mock.sentinel.wrapped) + + self.net = ClientNetwork( + key=KEY, alg=jose.RS256, verify_ssl=self.verify_ssl, + user_agent='acme-python-test') + + self.response = mock.MagicMock(ok=True, status_code=http_client.OK) + self.response.headers = {} + self.response.links = {} + + def test_init(self): + assert self.net.verify_ssl is self.verify_ssl + + def test_wrap_in_jws(self): + # pylint: disable=protected-access + jws_dump = self.net._wrap_in_jws( + MockJSONDeSerializable('foo'), nonce=b'Tg', url="url") + jws = acme_jws.JWS.json_loads(jws_dump) + assert json.loads(jws.payload.decode()) == {'foo': 'foo'} + assert jws.signature.combined.nonce == b'Tg' + + def test_wrap_in_jws_v2(self): + self.net.account = {'uri': 'acct-uri'} + # pylint: disable=protected-access + jws_dump = self.net._wrap_in_jws( + MockJSONDeSerializable('foo'), nonce=b'Tg', url="url") + jws = acme_jws.JWS.json_loads(jws_dump) + assert json.loads(jws.payload.decode()) == {'foo': 'foo'} + assert jws.signature.combined.nonce == b'Tg' + assert jws.signature.combined.kid == u'acct-uri' + assert jws.signature.combined.url == u'url' + + def test_check_response_not_ok_jobj_no_error(self): + self.response.ok = False + self.response.json.return_value = {} + with mock.patch('acme.client.messages.Error.from_json') as from_json: + from_json.side_effect = jose.DeserializationError + # pylint: disable=protected-access + with pytest.raises(errors.ClientError): + self.net._check_response(self.response) + + def test_check_response_not_ok_jobj_error(self): + self.response.ok = False + self.response.json.return_value = messages.Error.with_code( + 'serverInternal', detail='foo', title='some title').to_json() + # pylint: disable=protected-access + with pytest.raises(messages.Error): + self.net._check_response(self.response) + + def test_check_response_not_ok_no_jobj(self): + self.response.ok = False + self.response.json.side_effect = ValueError + # pylint: disable=protected-access + with pytest.raises(errors.ClientError): + self.net._check_response(self.response) + + def test_check_response_ok_no_jobj_ct_required(self): + self.response.json.side_effect = ValueError + for response_ct in [self.net.JSON_CONTENT_TYPE, 'foo']: + self.response.headers['Content-Type'] = response_ct + # pylint: disable=protected-access + with pytest.raises(errors.ClientError): + self.net._check_response(self.response, + content_type=self.net.JSON_CONTENT_TYPE) + + def test_check_response_ok_no_jobj_no_ct(self): + self.response.json.side_effect = ValueError + for response_ct in [self.net.JSON_CONTENT_TYPE, 'foo']: + self.response.headers['Content-Type'] = response_ct + # pylint: disable=protected-access + assert self.response == self.net._check_response(self.response) + + @mock.patch('acme.client.logger') + def test_check_response_ok_ct_with_charset(self, mock_logger): + self.response.json.return_value = {} + self.response.headers['Content-Type'] = 'application/json; charset=utf-8' + # pylint: disable=protected-access + assert self.response == self.net._check_response( + self.response, content_type='application/json') + try: + mock_logger.debug.assert_called_with( + 'Ignoring wrong Content-Type (%r) for JSON decodable response', + 'application/json; charset=utf-8' + ) + except AssertionError: + return + raise AssertionError('Expected Content-Type warning ' #pragma: no cover + 'to not have been logged') + + @mock.patch('acme.client.logger') + def test_check_response_ok_bad_ct(self, mock_logger): + self.response.json.return_value = {} + self.response.headers['Content-Type'] = 'text/plain' + # pylint: disable=protected-access + assert self.response == self.net._check_response( + self.response, content_type='application/json') + mock_logger.debug.assert_called_with( + 'Ignoring wrong Content-Type (%r) for JSON decodable response', + 'text/plain' + ) + + def test_check_response_conflict(self): + self.response.ok = False + self.response.status_code = 409 + # pylint: disable=protected-access + with pytest.raises(errors.ConflictError): + self.net._check_response(self.response) + + def test_check_response_jobj(self): + self.response.json.return_value = {} + for response_ct in [self.net.JSON_CONTENT_TYPE, 'foo']: + self.response.headers['Content-Type'] = response_ct + # pylint: disable=protected-access + assert self.response == self.net._check_response(self.response) + + def test_send_request(self): + self.net.session = mock.MagicMock() + self.net.session.request.return_value = self.response + # pylint: disable=protected-access + assert self.response == self.net._send_request( + 'HEAD', 'http://example.com/', 'foo', bar='baz') + self.net.session.request.assert_called_once_with( + 'HEAD', 'http://example.com/', 'foo', + headers=mock.ANY, verify=mock.ANY, timeout=mock.ANY, bar='baz') + + @mock.patch('acme.client.logger') + def test_send_request_get_der(self, mock_logger): + self.net.session = mock.MagicMock() + self.net.session.request.return_value = mock.MagicMock( + ok=True, status_code=http_client.OK, + content=b"hi") + # pylint: disable=protected-access + self.net._send_request('HEAD', 'http://example.com/', 'foo', + timeout=mock.ANY, bar='baz', headers={'Accept': 'application/pkix-cert'}) + mock_logger.debug.assert_called_with( + 'Received response:\nHTTP %d\n%s\n\n%s', 200, + '', b'aGk=') + + def test_send_request_post(self): + self.net.session = mock.MagicMock() + self.net.session.request.return_value = self.response + # pylint: disable=protected-access + assert self.response == self.net._send_request( + 'POST', 'http://example.com/', 'foo', data='qux', bar='baz') + self.net.session.request.assert_called_once_with( + 'POST', 'http://example.com/', 'foo', + headers=mock.ANY, verify=mock.ANY, timeout=mock.ANY, data='qux', bar='baz') + + def test_send_request_verify_ssl(self): + # pylint: disable=protected-access + for verify in True, False: + self.net.session = mock.MagicMock() + self.net.session.request.return_value = self.response + self.net.verify_ssl = verify + # pylint: disable=protected-access + assert self.response == \ + self.net._send_request('GET', 'http://example.com/') + self.net.session.request.assert_called_once_with( + 'GET', 'http://example.com/', verify=verify, + timeout=mock.ANY, headers=mock.ANY) + + def test_send_request_user_agent(self): + self.net.session = mock.MagicMock() + # pylint: disable=protected-access + self.net._send_request('GET', 'http://example.com/', + headers={'bar': 'baz'}) + self.net.session.request.assert_called_once_with( + 'GET', 'http://example.com/', verify=mock.ANY, + timeout=mock.ANY, + headers={'User-Agent': 'acme-python-test', 'bar': 'baz'}) + + self.net._send_request('GET', 'http://example.com/', + headers={'User-Agent': 'foo2'}) + self.net.session.request.assert_called_with( + 'GET', 'http://example.com/', + verify=mock.ANY, timeout=mock.ANY, headers={'User-Agent': 'foo2'}) + + def test_send_request_timeout(self): + self.net.session = mock.MagicMock() + # pylint: disable=protected-access + self.net._send_request('GET', 'http://example.com/', + headers={'bar': 'baz'}) + self.net.session.request.assert_called_once_with( + mock.ANY, mock.ANY, verify=mock.ANY, headers=mock.ANY, + timeout=45) + + def test_del(self, close_exception=None): + sess = mock.MagicMock() + + if close_exception is not None: + sess.close.side_effect = close_exception + + self.net.session = sess + del self.net + sess.close.assert_called_once_with() + + def test_del_error(self): + self.test_del(ReferenceError) + + @mock.patch('acme.client.requests') + def test_requests_error_passthrough(self, mock_requests): + mock_requests.exceptions = requests.exceptions + mock_requests.request.side_effect = requests.exceptions.RequestException + # pylint: disable=protected-access + with pytest.raises(requests.exceptions.RequestException): + self.net._send_request('GET', 'uri') + + +class ClientNetworkWithMockedResponseTest(unittest.TestCase): + """Tests for acme.client.ClientNetwork which mock out response.""" + + def setUp(self): + self.net = ClientNetwork(key='fake', alg=None) + + self.response = mock.MagicMock(ok=True, status_code=http_client.OK) + self.response.headers = {} + self.response.links = {} + self.response.checked = False + self.acmev1_nonce_response = mock.MagicMock( + ok=False, status_code=http_client.METHOD_NOT_ALLOWED) + self.acmev1_nonce_response.headers = {} + self.obj = mock.MagicMock() + self.wrapped_obj = mock.MagicMock() + self.content_type = mock.sentinel.content_type + + self.all_nonces = [ + jose.b64encode(b'Nonce'), + jose.b64encode(b'Nonce2'), jose.b64encode(b'Nonce3')] + self.available_nonces = self.all_nonces[:] + + def send_request(*args, **kwargs): + # pylint: disable=unused-argument,missing-docstring + assert "new_nonce_url" not in kwargs + method = args[0] + uri = args[1] + if method == 'HEAD' and uri != "new_nonce_uri": + response = self.acmev1_nonce_response + else: + response = self.response + + if self.available_nonces: + response.headers = { + self.net.REPLAY_NONCE_HEADER: + self.available_nonces.pop().decode()} + else: + response.headers = {} + return response + + # pylint: disable=protected-access + self.net._send_request = self.send_request = mock.MagicMock( + side_effect=send_request) + self.net._check_response = self.check_response + self.net._wrap_in_jws = mock.MagicMock(return_value=self.wrapped_obj) + + def check_response(self, response, content_type): + # pylint: disable=missing-docstring + assert self.response == response + assert self.content_type == content_type + assert self.response.ok + self.response.checked = True + return self.response + + def test_head(self): + assert self.acmev1_nonce_response == self.net.head( + 'http://example.com/', 'foo', bar='baz') + self.send_request.assert_called_once_with( + 'HEAD', 'http://example.com/', 'foo', bar='baz') + + def test_head_v2(self): + assert self.response == self.net.head( + 'new_nonce_uri', 'foo', bar='baz') + self.send_request.assert_called_once_with( + 'HEAD', 'new_nonce_uri', 'foo', bar='baz') + + def test_get(self): + assert self.response == self.net.get( + 'http://example.com/', content_type=self.content_type, bar='baz') + assert self.response.checked + self.send_request.assert_called_once_with( + 'GET', 'http://example.com/', bar='baz') + + def test_post_no_content_type(self): + self.content_type = self.net.JOSE_CONTENT_TYPE + assert self.response == self.net.post('uri', self.obj) + assert self.response.checked + + def test_post(self): + assert self.response == self.net.post( + 'uri', self.obj, content_type=self.content_type) + assert self.response.checked + self.net._wrap_in_jws.assert_called_once_with( + self.obj, jose.b64decode(self.all_nonces.pop()), "uri") + + self.available_nonces = [] + with pytest.raises(errors.MissingNonce): + self.net.post('uri', self.obj, content_type=self.content_type) + self.net._wrap_in_jws.assert_called_with( + self.obj, jose.b64decode(self.all_nonces.pop()), "uri") + + def test_post_wrong_initial_nonce(self): # HEAD + self.available_nonces = [b'f', jose.b64encode(b'good')] + with pytest.raises(errors.BadNonce): + self.net.post('uri', + self.obj, content_type=self.content_type) + + def test_post_wrong_post_response_nonce(self): + self.available_nonces = [jose.b64encode(b'good'), b'f'] + with pytest.raises(errors.BadNonce): + self.net.post('uri', + self.obj, content_type=self.content_type) + + def test_post_failed_retry(self): + check_response = mock.MagicMock() + check_response.side_effect = messages.Error.with_code('badNonce') + + self.net._check_response = check_response + with pytest.raises(messages.Error): + self.net.post('uri', + self.obj, content_type=self.content_type) + + def test_post_not_retried(self): + check_response = mock.MagicMock() + check_response.side_effect = [messages.Error.with_code('malformed'), + self.response] + + self.net._check_response = check_response + with pytest.raises(messages.Error): + self.net.post('uri', + self.obj, content_type=self.content_type) + + def test_post_successful_retry(self): + post_once = mock.MagicMock() + post_once.side_effect = [messages.Error.with_code('badNonce'), + self.response] + + assert self.response == self.net.post( + 'uri', self.obj, content_type=self.content_type) + + def test_head_get_post_error_passthrough(self): + self.send_request.side_effect = requests.exceptions.RequestException + for method in self.net.head, self.net.get: + with pytest.raises(requests.exceptions.RequestException): + method('GET', 'uri') + with pytest.raises(requests.exceptions.RequestException): + self.net.post('uri', obj=self.obj) + + def test_post_bad_nonce_head(self): + # pylint: disable=protected-access + # regression test for https://github.com/certbot/certbot/issues/6092 + bad_response = mock.MagicMock(ok=False, status_code=http_client.SERVICE_UNAVAILABLE) + self.net._send_request = mock.MagicMock() + self.net._send_request.return_value = bad_response + self.content_type = None + check_response = mock.MagicMock() + self.net._check_response = check_response + with pytest.raises(errors.ClientError): + self.net.post('uri', + self.obj, content_type=self.content_type, + new_nonce_url='new_nonce_uri') + assert check_response.call_count == 1 + + def test_new_nonce_uri_removed(self): + self.content_type = None + self.net.post('uri', self.obj, content_type=None, new_nonce_url='new_nonce_uri') + + def test_no_key_error(self): + "A ClientNetwork with no key should error on POST but succeed on GET" + self.net = ClientNetwork() + self.net._send_request = mock.MagicMock() + self.net._send_request.return_value = self.response + with pytest.raises(errors.Error): + self.net.post('uri', "body") + assert self.response == self.net.get( + 'uri', content_type=self.content_type, bar='baz') + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/_internal/tests/conftest.py b/acme/src/acme/_internal/tests/conftest.py new file mode 100644 index 00000000000..6e929364d5b --- /dev/null +++ b/acme/src/acme/_internal/tests/conftest.py @@ -0,0 +1,16 @@ +from unittest import mock + +import pytest + +# This avoids a bug on mac where getfqdn errors after a long timeout. +# See https://bugs.python.org/issue35164 +# and discussion at https://github.com/certbot/certbot/pull/10408 +@pytest.fixture(autouse=True) +def mock_getfqdn(): + with mock.patch("socket.getfqdn", return_value="server_name") as mocked: + yield mocked + +@pytest.fixture(autouse=True) +def mock_sleep(): + with mock.patch("time.sleep") as mocked: + yield mocked diff --git a/acme/src/acme/_internal/tests/crypto_util_test.py b/acme/src/acme/_internal/tests/crypto_util_test.py new file mode 100644 index 00000000000..ff96497c517 --- /dev/null +++ b/acme/src/acme/_internal/tests/crypto_util_test.py @@ -0,0 +1,277 @@ +"""Tests for acme.crypto_util.""" +import ipaddress +import itertools +import sys +import unittest +from unittest import mock +import warnings + +import pytest +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import rsa, x25519 + +from acme._internal.tests import test_util + + +class FormatTest(unittest.TestCase): + def test_to_cryptography_encoding(self): + with pytest.warns(DeprecationWarning, match='Format is deprecated'): + from acme.crypto_util import Format + assert Format.DER.to_cryptography_encoding() == serialization.Encoding.DER + assert Format.PEM.to_cryptography_encoding() == serialization.Encoding.PEM + + +class MiscTests(unittest.TestCase): + + def test_dump_cryptography_chain(self): + from acme.crypto_util import dump_cryptography_chain + + cert1 = test_util.load_cert('rsa2048_cert.pem') + cert2 = test_util.load_cert('rsa4096_cert.pem') + + chain = [cert1, cert2] + dumped = dump_cryptography_chain(chain) + + # default is PEM encoding Encoding.PEM + assert isinstance(dumped, bytes) + + def test_dump_cryptography_chain_wrong_encoding(self): + from acme.crypto_util import dump_cryptography_chain + + with pytest.raises(ValueError): + dump_cryptography_chain(mock.Mock(), serialization.Encoding.SMIME) + + +class CryptographyCertOrReqSANTest(unittest.TestCase): + """Test for acme.crypto_util._cryptography_cert_or_req_san.""" + + @classmethod + def _call(cls, loader, name): + # pylint: disable=protected-access + from acme.crypto_util import _cryptography_cert_or_req_san + return _cryptography_cert_or_req_san(loader(name)) + + @classmethod + def _get_idn_names(cls): + """Returns expected names from '{cert,csr}-idnsans.pem'.""" + chars = [chr(i) for i in itertools.chain(range(0x3c3, 0x400), + range(0x641, 0x6fc), + range(0x1820, 0x1877))] + return [''.join(chars[i: i + 45]) + '.invalid' + for i in range(0, len(chars), 45)] + + def _call_cert(self, name): + return self._call(test_util.load_cert, name) + + def _call_csr(self, name): + return self._call(test_util.load_csr, name) + + def test_cert_no_sans(self): + assert self._call_cert('cert.pem') == [] + + def test_cert_two_sans(self): + assert self._call_cert('cert-san.pem') == \ + ['example.com', 'www.example.com'] + + def test_cert_hundred_sans(self): + assert self._call_cert('cert-100sans.pem') == \ + ['example{0}.com'.format(i) for i in range(1, 101)] + + def test_cert_idn_sans(self): + assert self._call_cert('cert-idnsans.pem') == \ + self._get_idn_names() + + def test_csr_no_sans(self): + assert self._call_csr('csr-nosans.pem') == [] + + def test_csr_one_san(self): + assert self._call_csr('csr.pem') == ['example.com'] + + def test_csr_two_sans(self): + assert self._call_csr('csr-san.pem') == \ + ['example.com', 'www.example.com'] + + def test_csr_six_sans(self): + assert self._call_csr('csr-6sans.pem') == \ + ['example.com', 'example.org', 'example.net', + 'example.info', 'subdomain.example.com', + 'other.subdomain.example.com'] + + def test_csr_hundred_sans(self): + assert self._call_csr('csr-100sans.pem') == \ + ['example{0}.com'.format(i) for i in range(1, 101)] + + def test_csr_idn_sans(self): + assert self._call_csr('csr-idnsans.pem') == \ + self._get_idn_names() + + def test_critical_san(self): + assert self._call_cert('critical-san.pem') == \ + ['chicago-cubs.venafi.example', 'cubs.venafi.example'] + + +class GenMakeSelfSignedCertTest(unittest.TestCase): + """Test for make_self_signed_cert.""" + + def setUp(self): + self.cert_count = 5 + self.serial_num: list[int] = [] + self.privkey = rsa.generate_private_key(public_exponent=65537, key_size=2048) + + @classmethod + def _call(cls, *args, **kwargs): + from acme.crypto_util import make_self_signed_cert + with pytest.warns(DeprecationWarning, match='make_self_signed_cert is deprecated'): + return make_self_signed_cert(*args, **kwargs) + + def test_sn_collisions(self): + for _ in range(self.cert_count): + cert = self._call(self.privkey, ['dummy'], force_san=True, + ips=[ipaddress.ip_address("10.10.10.10")]) + self.serial_num.append(cert.serial_number) + assert len(set(self.serial_num)) >= self.cert_count + + def test_no_ips(self): + self._call(self.privkey, ['dummy']) + + @mock.patch("acme.crypto_util._now") + def test_expiry_times(self, mock_now): + from datetime import datetime, timedelta, timezone + not_before = 1736200830 + validity = 100 + + not_before_dt = datetime.fromtimestamp(not_before) + validity_td = timedelta(validity) + not_after_dt = not_before_dt + validity_td + cert = self._call( + self.privkey, + ['dummy'], + not_before=not_before_dt, + validity=validity_td, + ) + # TODO: This should be `not_valid_before_utc` once we raise the minimum + # cryptography version. + # https://github.com/certbot/certbot/issues/10105 + with warnings.catch_warnings(): + warnings.filterwarnings( + 'ignore', + message='Properties that return.*datetime object' + ) + self.assertEqual(cert.not_valid_before, not_before_dt) + self.assertEqual(cert.not_valid_after, not_after_dt) + + now = not_before + 1 + now_dt = datetime.fromtimestamp(now) + mock_now.return_value = now_dt.replace(tzinfo=timezone.utc) + valid_after_now_dt = now_dt + validity_td + cert = self._call( + self.privkey, + ['dummy'], + validity=validity_td, + ) + with warnings.catch_warnings(): + warnings.filterwarnings( + 'ignore', + message='Properties that return.*datetime object' + ) + self.assertEqual(cert.not_valid_before, now_dt) + self.assertEqual(cert.not_valid_after, valid_after_now_dt) + + def test_no_name(self): + with pytest.raises(AssertionError): + self._call(self.privkey, ips=[ipaddress.ip_address("1.1.1.1")]) + self._call(self.privkey) + + def test_extensions(self): + extension_type = x509.TLSFeature([x509.TLSFeatureType.status_request]) + extension = x509.Extension( + x509.TLSFeature.oid, + False, + extension_type + ) + cert = self._call( + self.privkey, + ips=[ipaddress.ip_address("1.1.1.1")], + extensions=[extension] + ) + self.assertIn(extension, cert.extensions) + + +class MakeCSRTest(unittest.TestCase): + """Test for standalone functions.""" + + @classmethod + def _call_with_key(cls, *args, **kwargs): + privkey = rsa.generate_private_key(public_exponent=65537, key_size=2048) + privkey_pem = privkey.private_bytes( + serialization.Encoding.PEM, + serialization.PrivateFormat.PKCS8, + serialization.NoEncryption(), + ) + from acme.crypto_util import make_csr + + return make_csr(privkey_pem, *args, **kwargs) + + def test_make_csr(self): + csr_pem = self._call_with_key(["a.example", "b.example"]) + assert b"--BEGIN CERTIFICATE REQUEST--" in csr_pem + assert b"--END CERTIFICATE REQUEST--" in csr_pem + csr = x509.load_pem_x509_csr(csr_pem) + + assert len(csr.extensions) == 1 + assert list( + csr.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + ) == [ + x509.DNSName("a.example"), + x509.DNSName("b.example"), + ] + + def test_make_csr_ip(self): + csr_pem = self._call_with_key( + ["a.example"], + False, + [ipaddress.ip_address("127.0.0.1"), ipaddress.ip_address("::1")], + ) + assert b"--BEGIN CERTIFICATE REQUEST--" in csr_pem + assert b"--END CERTIFICATE REQUEST--" in csr_pem + + csr = x509.load_pem_x509_csr(csr_pem) + + assert len(csr.extensions) == 1 + assert list( + csr.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + ) == [ + x509.DNSName("a.example"), + x509.IPAddress(ipaddress.ip_address("127.0.0.1")), + x509.IPAddress(ipaddress.ip_address("::1")), + ] + + def test_make_csr_must_staple(self): + csr_pem = self._call_with_key(["a.example"], must_staple=True) + csr = x509.load_pem_x509_csr(csr_pem) + + assert len(csr.extensions) == 2 + assert list(csr.extensions.get_extension_for_class(x509.TLSFeature).value) == [ + x509.TLSFeatureType.status_request + ] + + def test_make_csr_without_hostname(self): + with pytest.raises(ValueError): + self._call_with_key() + + def test_make_csr_invalid_key_type(self): + privkey = x25519.X25519PrivateKey.generate() + privkey_pem = privkey.private_bytes( + serialization.Encoding.PEM, + serialization.PrivateFormat.PKCS8, + serialization.NoEncryption(), + ) + from acme.crypto_util import make_csr + + with pytest.raises(ValueError): + make_csr(privkey_pem, ["a.example"]) + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/_internal/tests/errors_test.py b/acme/src/acme/_internal/tests/errors_test.py new file mode 100644 index 00000000000..531f16a2fa7 --- /dev/null +++ b/acme/src/acme/_internal/tests/errors_test.py @@ -0,0 +1,86 @@ +"""Tests for acme.errors.""" +import sys +import unittest +from unittest import mock + +import pytest + + +class BadNonceTest(unittest.TestCase): + """Tests for acme.errors.BadNonce.""" + + def setUp(self): + from acme.errors import BadNonce + self.error = BadNonce(nonce="xxx", error="error") + + def test_str(self): + assert "Invalid nonce ('xxx'): error" == str(self.error) + + +class MissingNonceTest(unittest.TestCase): + """Tests for acme.errors.MissingNonce.""" + + def setUp(self): + from acme.errors import MissingNonce + self.response = mock.MagicMock(headers={}) + self.response.request.method = 'FOO' + self.error = MissingNonce(self.response) + + def test_str(self): + assert "FOO" in str(self.error) + assert "{}" in str(self.error) + + +class PollErrorTest(unittest.TestCase): + """Tests for acme.errors.PollError.""" + + def setUp(self): + from acme.errors import PollError + self.timeout = PollError( + exhausted={mock.sentinel.AR}, + updated={}) + self.invalid = PollError(exhausted=set(), updated={ + mock.sentinel.AR: mock.sentinel.AR2}) + + def test_timeout(self): + assert self.timeout.timeout + assert not self.invalid.timeout + + def test_repr(self): + assert 'PollError(exhausted=%s, updated={sentinel.AR: ' \ + 'sentinel.AR2})' % repr(set()) == repr(self.invalid) + + +class ValidationErrorTest(unittest.TestCase): + """Tests for acme.errors.ValidationError""" + + def setUp(self): + from acme.errors import ValidationError + from acme.challenges import DNS01 + from acme.messages import Error + from acme.messages import Authorization + from acme.messages import AuthorizationResource + from acme.messages import IDENTIFIER_FQDN + from acme.messages import ChallengeBody + from acme.messages import Identifier + self.challenge_error = Error(typ='custom', detail='bar') + failed_authzr = AuthorizationResource( + body=Authorization( + identifier=Identifier(typ=IDENTIFIER_FQDN, value="example.com"), + challenges=[ChallengeBody( + chall=DNS01(), + error=self.challenge_error, + )] + ) + ) + self.error = ValidationError([failed_authzr]) + + def test_repr(self): + err_message = str(self.error) + assert 'Authorization for example.com failed' in err_message + assert 'Challenge dns-01 failed' in err_message + assert str(self.challenge_error) in err_message + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/_internal/tests/fields_test.py b/acme/src/acme/_internal/tests/fields_test.py new file mode 100644 index 00000000000..49d82725ca4 --- /dev/null +++ b/acme/src/acme/_internal/tests/fields_test.py @@ -0,0 +1,58 @@ +"""Tests for acme.fields.""" +import datetime +import sys +import unittest + +import josepy as jose +import pytest + + +class FixedTest(unittest.TestCase): + """Tests for acme.fields.Fixed.""" + + def setUp(self): + from acme.fields import fixed + self.field = fixed('name', 'x') + + def test_decode(self): + assert 'x' == self.field.decode('x') + + def test_decode_bad(self): + with pytest.raises(jose.DeserializationError): + self.field.decode('y') + + def test_encode(self): + assert 'x' == self.field.encode('x') + + def test_encode_override(self): + assert 'y' == self.field.encode('y') + + +class RFC3339FieldTest(unittest.TestCase): + """Tests for acme.fields.RFC3339Field.""" + + def setUp(self): + self.decoded = datetime.datetime(2015, 3, 27, tzinfo=datetime.timezone.utc) + self.encoded = '2015-03-27T00:00:00Z' + + def test_default_encoder(self): + from acme.fields import RFC3339Field + assert self.encoded == RFC3339Field.default_encoder(self.decoded) + + def test_default_encoder_naive_fails(self): + from acme.fields import RFC3339Field + with pytest.raises(ValueError): + RFC3339Field.default_encoder(datetime.datetime.now()) + + def test_default_decoder(self): + from acme.fields import RFC3339Field + assert self.decoded == RFC3339Field.default_decoder(self.encoded) + + def test_default_decoder_raises_deserialization_error(self): + from acme.fields import RFC3339Field + with pytest.raises(jose.DeserializationError): + RFC3339Field.default_decoder('') + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/_internal/tests/jose_test.py b/acme/src/acme/_internal/tests/jose_test.py new file mode 100644 index 00000000000..3da1ece0ac2 --- /dev/null +++ b/acme/src/acme/_internal/tests/jose_test.py @@ -0,0 +1,53 @@ +"""Tests for acme.jose shim.""" +import importlib +import sys + +import pytest + + +def _test_it(submodule, attribute): + if submodule: + acme_jose_path = 'acme.jose.' + submodule + josepy_path = 'josepy.' + submodule + else: + acme_jose_path = 'acme.jose' + josepy_path = 'josepy' + acme_jose_mod = importlib.import_module(acme_jose_path) + josepy_mod = importlib.import_module(josepy_path) + + assert acme_jose_mod is josepy_mod + assert getattr(acme_jose_mod, attribute) is getattr(josepy_mod, attribute) + + # We use the imports below with eval, but pylint doesn't + # understand that. + import josepy # pylint: disable=unused-import # noqa: F401 + + import acme # pylint: disable=unused-import # noqa: F401 + acme_jose_mod = eval(acme_jose_path) # pylint: disable=eval-used + josepy_mod = eval(josepy_path) # pylint: disable=eval-used + assert acme_jose_mod is josepy_mod + assert getattr(acme_jose_mod, attribute) is getattr(josepy_mod, attribute) + +def test_top_level(): + _test_it('', 'RS512') + +def test_submodules(): + # This test ensures that the modules in josepy that were + # available at the time it was moved into its own package are + # available under acme.jose. Backwards compatibility with new + # modules or testing code is not maintained. + mods_and_attrs = [('b64', 'b64decode',), + ('errors', 'Error',), + ('interfaces', 'JSONDeSerializable',), + ('json_util', 'Field',), + ('jwa', 'HS256',), + ('jwk', 'JWK',), + ('jws', 'JWS',), + ('util', 'ImmutableMap',),] + + for mod, attr in mods_and_attrs: + _test_it(mod, attr) + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/_internal/tests/jws_test.py b/acme/src/acme/_internal/tests/jws_test.py new file mode 100644 index 00000000000..222552cf489 --- /dev/null +++ b/acme/src/acme/_internal/tests/jws_test.py @@ -0,0 +1,68 @@ +"""Tests for acme.jws.""" +import sys +import unittest + +import josepy as jose +import pytest + +from acme._internal.tests import test_util + +KEY = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem')) + + +class HeaderTest(unittest.TestCase): + """Tests for acme.jws.Header.""" + + good_nonce = jose.encode_b64jose(b'foo') + wrong_nonce = u'F' + # Following just makes sure wrong_nonce is wrong + try: + jose.b64decode(wrong_nonce) + except (ValueError, TypeError): + assert True + else: + pytest.fail("Exception from jose.b64decode wasn't raised") # pragma: no cover + + def test_nonce_decoder(self): + from acme.jws import Header + nonce_field = Header._fields['nonce'] + + with pytest.raises(jose.DeserializationError): + nonce_field.decode(self.wrong_nonce) + assert b'foo' == nonce_field.decode(self.good_nonce) + + +class JWSTest(unittest.TestCase): + """Tests for acme.jws.JWS.""" + + def setUp(self): + self.privkey = KEY + self.pubkey = self.privkey.public_key() + self.nonce = jose.b64encode(b'Nonce') + self.url = 'hi' + self.kid = 'baaaaa' + + def test_kid_serialize(self): + from acme.jws import JWS + jws = JWS.sign(payload=b'foo', key=self.privkey, + alg=jose.RS256, nonce=self.nonce, + url=self.url, kid=self.kid) + assert jws.signature.combined.nonce == self.nonce + assert jws.signature.combined.url == self.url + assert jws.signature.combined.kid == self.kid + assert jws.signature.combined.jwk is None + # TODO: check that nonce is in protected header + + assert jws == JWS.from_json(jws.to_json()) + + def test_jwk_serialize(self): + from acme.jws import JWS + jws = JWS.sign(payload=b'foo', key=self.privkey, + alg=jose.RS256, nonce=self.nonce, + url=self.url) + assert jws.signature.combined.kid is None + assert jws.signature.combined.jwk == self.pubkey + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/_internal/tests/messages_test.py b/acme/src/acme/_internal/tests/messages_test.py new file mode 100644 index 00000000000..aad75ad016e --- /dev/null +++ b/acme/src/acme/_internal/tests/messages_test.py @@ -0,0 +1,597 @@ +"""Tests for acme.messages.""" +import sys +import json +import unittest +from unittest import mock + +import josepy as jose +import pytest + +from acme import challenges +from acme._internal.tests import test_util + +CERT = test_util.load_cert('cert.der') +CSR = test_util.load_csr('csr.der') +KEY = test_util.load_rsa_private_key('rsa512_key.pem') + + +class ErrorTest(unittest.TestCase): + """Tests for acme.messages.Error.""" + + def setUp(self): + from acme.messages import Error + from acme.messages import ERROR_PREFIX + from acme.messages import Identifier + from acme.messages import IDENTIFIER_FQDN + self.error = Error.with_code('malformed', detail='foo', title='title') + self.jobj = { + 'detail': 'foo', + 'title': 'some title', + 'type': ERROR_PREFIX + 'malformed', + } + self.error_custom = Error(typ='custom', detail='bar') + self.identifier = Identifier(typ=IDENTIFIER_FQDN, value='example.com') + self.subproblem = Error.with_code('caa', detail='bar', title='title', identifier=self.identifier) + self.error_with_subproblems = Error.with_code('malformed', detail='foo', title='title', subproblems=[self.subproblem]) + self.empty_error = Error() + + def test_default_typ(self): + from acme.messages import Error + assert Error().typ == 'about:blank' + + def test_from_json_empty(self): + from acme.messages import Error + assert Error() == Error.from_json('{}') + + def test_from_json_hashable(self): + from acme.messages import Error + hash(Error.from_json(self.error.to_json())) + + def test_from_json_with_subproblems(self): + from acme.messages import Error + + parsed_error = Error.from_json(self.error_with_subproblems.to_json()) + + assert 1 == len(parsed_error.subproblems) + assert self.subproblem == parsed_error.subproblems[0] + + def test_description(self): + assert 'The request message was malformed' == self.error.description + assert self.error_custom.description is None + + def test_code(self): + from acme.messages import Error + assert 'malformed' == self.error.code + assert self.error_custom.code is None + assert Error().code is None + + def test_is_acme_error(self): + from acme.messages import Error + from acme.messages import is_acme_error + assert is_acme_error(self.error) + assert not is_acme_error(self.error_custom) + assert not is_acme_error(Error()) + assert not is_acme_error(self.empty_error) + assert not is_acme_error("must pet all the {dogs|rabbits}") + + def test_unicode_error(self): + from acme.messages import Error + from acme.messages import is_acme_error + arabic_error = Error.with_code( + 'malformed', detail=u'\u0639\u062f\u0627\u0644\u0629', title='title') + assert is_acme_error(arabic_error) + + def test_with_code(self): + from acme.messages import Error + from acme.messages import is_acme_error + assert is_acme_error(Error.with_code('badCSR')) + with pytest.raises(ValueError): + Error.with_code('not an ACME error code') + + def test_str(self): + assert str(self.error) == \ + u"{0.typ} :: {0.description} :: {0.detail} :: {0.title}" \ + .format(self.error) + assert str(self.error_with_subproblems) == \ + (u"{0.typ} :: {0.description} :: {0.detail} :: {0.title}\n"+ + u"Problem for {1.identifier.value}: {1.typ} :: {1.description} :: {1.detail} :: {1.title}").format( + self.error_with_subproblems, self.subproblem) + + # this test is based on a minimal reproduction of a contextmanager/immutable + # exception related error: https://github.com/python/cpython/issues/99856 + def test_setting_traceback(self): + assert self.error_custom.__traceback__ is None + + try: + 1/0 + except ZeroDivisionError as e: + self.error_custom.__traceback__ = e.__traceback__ + + assert self.error_custom.__traceback__ is not None + + +class ConstantTest(unittest.TestCase): + """Tests for acme.messages._Constant.""" + + def setUp(self): + from acme.messages import _Constant + + class MockConstant(_Constant): # pylint: disable=missing-docstring + POSSIBLE_NAMES: dict = {} + + self.MockConstant = MockConstant # pylint: disable=invalid-name + self.const_a = MockConstant('a') + self.const_b = MockConstant('b') + + def test_to_partial_json(self): + assert 'a' == self.const_a.to_partial_json() + assert 'b' == self.const_b.to_partial_json() + + def test_from_json(self): + assert self.const_a == self.MockConstant.from_json('a') + with pytest.raises(jose.DeserializationError): + self.MockConstant.from_json('c') + + def test_from_json_hashable(self): + hash(self.MockConstant.from_json('a')) + + def test_repr(self): + assert 'MockConstant(a)' == repr(self.const_a) + assert 'MockConstant(b)' == repr(self.const_b) + + def test_equality(self): + const_a_prime = self.MockConstant('a') + assert self.const_a != self.const_b + assert self.const_a == const_a_prime + + assert self.const_a != self.const_b + assert self.const_a == const_a_prime + + +class DirectoryTest(unittest.TestCase): + """Tests for acme.messages.Directory.""" + + def setUp(self): + from acme.messages import Directory + self.dir = Directory({ + 'newReg': 'reg', + 'newCert': 'cert', + 'meta': Directory.Meta( + terms_of_service='https://example.com/acme/terms', + website='https://www.example.com/', + caa_identities=['example.com'], + profiles={ + "example": "some profile", + "other example": "a different profile" + } + ), + }) + + def test_init_wrong_key_value_success(self): # pylint: disable=no-self-use + from acme.messages import Directory + Directory({'foo': 'bar'}) + + def test_getitem(self): + assert 'reg' == self.dir['newReg'] + + def test_getitem_fails_with_key_error(self): + with pytest.raises(KeyError): + self.dir.__getitem__('foo') + + def test_getattr(self): + assert 'reg' == self.dir.newReg + + def test_getattr_fails_with_attribute_error(self): + with pytest.raises(AttributeError): + self.dir.__getattr__('foo') + + def test_to_json(self): + assert self.dir.to_json() == { + 'newReg': 'reg', + 'newCert': 'cert', + 'meta': { + 'termsOfService': 'https://example.com/acme/terms', + 'website': 'https://www.example.com/', + 'caaIdentities': ['example.com'], + 'profiles': { + 'example': 'some profile', + 'other example': 'a different profile' + } + }, + } + + def test_from_json_deserialization_unknown_key_success(self): # pylint: disable=no-self-use + from acme.messages import Directory + Directory.from_json({'foo': 'bar'}) + + def test_iter_meta(self): + result = False + for k in self.dir.meta: + if k == 'terms_of_service': + result = self.dir.meta[k] == 'https://example.com/acme/terms' + assert result + + +class ExternalAccountBindingTest(unittest.TestCase): + def setUp(self): + from acme.messages import Directory + self.key = jose.jwk.JWKRSA(key=KEY.public_key()) + self.kid = "kid-for-testing" + self.hmac_key = "hmac-key-for-testing" + self.hmac_alg = "HS256" + self.dir = Directory({ + 'newAccount': 'http://url/acme/new-account', + }) + + def test_from_data(self): + from acme.messages import ExternalAccountBinding + eab = ExternalAccountBinding.from_data(self.key, self.kid, self.hmac_key, self.dir, self.hmac_alg) + + assert len(eab) == 3 + assert sorted(eab.keys()) == sorted(['protected', 'payload', 'signature']) + + def test_from_data_invalid_hmac_alg(self): + from acme.messages import ExternalAccountBinding + invalid_alg = "HS9999" + with pytest.raises(ValueError) as info: + ExternalAccountBinding.from_data(self.key, self.kid, self.hmac_key, self.dir, invalid_alg) + + assert "Invalid value for hmac_alg" in str(info.value) + + def test_from_data_default_hmac_alg(self): + from acme.messages import ExternalAccountBinding + eab_default = ExternalAccountBinding.from_data(self.key, self.kid, self.hmac_key, self.dir) + + assert len(eab_default) == 3 + assert sorted(eab_default.keys()) == sorted(['protected', 'payload', 'signature']) + + eab_explicit = ExternalAccountBinding.from_data( + self.key, self.kid, self.hmac_key, self.dir, "HS256" + ) + + assert eab_default == eab_explicit + + protected_default = json.loads( + jose.b64.b64decode(eab_default['protected']).decode() + ) + assert protected_default['alg'] == 'HS256' + +class RegistrationTest(unittest.TestCase): + """Tests for acme.messages.Registration.""" + + def setUp(self): + key = jose.jwk.JWKRSA(key=KEY.public_key()) + contact = ( + 'mailto:admin@foo.com', + 'tel:1234', + ) + agreement = 'https://letsencrypt.org/terms' + + from acme.messages import Registration + self.reg = Registration(key=key, contact=contact, agreement=agreement) + self.reg_none = Registration() + + self.jobj_to = { + 'contact': contact, + 'agreement': agreement, + 'key': key, + } + self.jobj_from = self.jobj_to.copy() + self.jobj_from['key'] = key.to_json() + + def test_from_data(self): + from acme.messages import Registration + reg = Registration.from_data(phone='1234', email='admin@foo.com') + assert reg.contact == ( + 'tel:1234', + 'mailto:admin@foo.com', + ) + + def test_new_registration_from_data_with_eab(self): + from acme.messages import Directory + from acme.messages import ExternalAccountBinding + from acme.messages import NewRegistration + key = jose.jwk.JWKRSA(key=KEY.public_key()) + kid = "kid-for-testing" + hmac_key = "hmac-key-for-testing" + hmac_alg = "HS256" + directory = Directory({ + 'newAccount': 'http://url/acme/new-account', + }) + eab = ExternalAccountBinding.from_data(key, kid, hmac_key, directory, hmac_alg) + reg = NewRegistration.from_data(email='admin@foo.com', external_account_binding=eab) + assert reg.contact == ( + 'mailto:admin@foo.com', + ) + assert sorted(reg.external_account_binding.keys()) == \ + sorted(['protected', 'payload', 'signature']) + + def test_phones(self): + assert ('1234',) == self.reg.phones + + def test_emails(self): + assert ('admin@foo.com',) == self.reg.emails + + def test_to_partial_json(self): + assert self.jobj_to == self.reg.to_partial_json() + + def test_from_json(self): + from acme.messages import Registration + assert self.reg == Registration.from_json(self.jobj_from) + + def test_from_json_hashable(self): + from acme.messages import Registration + hash(Registration.from_json(self.jobj_from)) + + def test_default_not_transmitted(self): + from acme.messages import NewRegistration + empty_new_reg = NewRegistration() + new_reg_with_contact = NewRegistration(contact=()) + + assert empty_new_reg.contact == () + assert new_reg_with_contact.contact == () + + assert 'contact' not in empty_new_reg.to_partial_json() + assert 'contact' not in empty_new_reg.fields_to_partial_json() + assert 'contact' in new_reg_with_contact.to_partial_json() + assert 'contact' in new_reg_with_contact.fields_to_partial_json() + + +class UpdateRegistrationTest(unittest.TestCase): + """Tests for acme.messages.UpdateRegistration.""" + + def test_empty(self): + from acme.messages import UpdateRegistration + jstring = '{"resource": "reg"}' + assert '{}' == UpdateRegistration().json_dumps() + assert UpdateRegistration() == UpdateRegistration.json_loads(jstring) + + +class RegistrationResourceTest(unittest.TestCase): + """Tests for acme.messages.RegistrationResource.""" + + def setUp(self): + from acme.messages import RegistrationResource + self.regr = RegistrationResource( + body=mock.sentinel.body, uri=mock.sentinel.uri, + terms_of_service=mock.sentinel.terms_of_service) + + def test_to_partial_json(self): + assert self.regr.to_json() == { + 'body': mock.sentinel.body, + 'uri': mock.sentinel.uri, + 'terms_of_service': mock.sentinel.terms_of_service, + } + + +class ChallengeResourceTest(unittest.TestCase): + """Tests for acme.messages.ChallengeResource.""" + + def test_uri(self): + from acme.messages import ChallengeResource + assert 'http://challb' == ChallengeResource(body=mock.MagicMock( + uri='http://challb'), authzr_uri='http://authz').uri + + +class ChallengeBodyTest(unittest.TestCase): + """Tests for acme.messages.ChallengeBody.""" + + def setUp(self): + self.chall = challenges.DNS(token=jose.b64decode( + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA')) + + from acme.messages import ChallengeBody + from acme.messages import Error + from acme.messages import STATUS_INVALID + self.status = STATUS_INVALID + error = Error.with_code('serverInternal', detail='Unable to communicate with DNS server') + self.challb = ChallengeBody( + uri='http://challb', chall=self.chall, status=self.status, + error=error) + + self.jobj_to = { + 'url': 'http://challb', + 'status': self.status, + 'type': 'dns', + 'token': 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA', + 'error': error, + } + self.jobj_from = self.jobj_to.copy() + self.jobj_from['status'] = 'invalid' + self.jobj_from['error'] = { + 'type': 'urn:ietf:params:acme:error:serverInternal', + 'detail': 'Unable to communicate with DNS server', + } + + def test_encode(self): + assert self.challb.encode('uri') == self.challb.uri + + def test_to_partial_json(self): + assert self.jobj_to == self.challb.to_partial_json() + + def test_from_json(self): + from acme.messages import ChallengeBody + assert self.challb == ChallengeBody.from_json(self.jobj_from) + + def test_from_json_hashable(self): + from acme.messages import ChallengeBody + hash(ChallengeBody.from_json(self.jobj_from)) + + def test_proxy(self): + assert jose.b64decode( + 'evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ-PCt92wr-oA') == self.challb.token + + +class AuthorizationTest(unittest.TestCase): + """Tests for acme.messages.Authorization.""" + + def setUp(self): + from acme.messages import ChallengeBody + from acme.messages import STATUS_VALID + + self.challbs = ( + ChallengeBody( + uri='http://challb1', status=STATUS_VALID, + chall=challenges.HTTP01(token=b'IlirfxKKXAsHtmzK29Pj8A')), + ChallengeBody(uri='http://challb2', status=STATUS_VALID, + chall=challenges.DNS( + token=b'DGyRejmCefe7v4NfDGDKfA')), + ) + + from acme.messages import Authorization + from acme.messages import Identifier + from acme.messages import IDENTIFIER_FQDN + identifier = Identifier(typ=IDENTIFIER_FQDN, value='example.com') + self.authz = Authorization( + identifier=identifier, challenges=self.challbs) + + self.jobj_from = { + 'identifier': identifier.to_json(), + 'challenges': [challb.to_json() for challb in self.challbs], + } + + def test_from_json(self): + from acme.messages import Authorization + Authorization.from_json(self.jobj_from) + + def test_from_json_hashable(self): + from acme.messages import Authorization + hash(Authorization.from_json(self.jobj_from)) + + +class AuthorizationResourceTest(unittest.TestCase): + """Tests for acme.messages.AuthorizationResource.""" + + def test_json_de_serializable(self): + from acme.messages import AuthorizationResource + authzr = AuthorizationResource( + uri=mock.sentinel.uri, + body=mock.sentinel.body) + assert isinstance(authzr, jose.JSONDeSerializable) + + +class CertificateRequestTest(unittest.TestCase): + """Tests for acme.messages.CertificateRequest.""" + + def setUp(self): + from acme.messages import CertificateRequest + self.req = CertificateRequest(csr=CSR) + + def test_json_de_serializable(self): + assert isinstance(self.req, jose.JSONDeSerializable) + from acme.messages import CertificateRequest + assert self.req == CertificateRequest.from_json(self.req.to_json()) + + +class CertificateResourceTest(unittest.TestCase): + """Tests for acme.messages.CertificateResourceTest.""" + + def setUp(self): + from acme.messages import CertificateResource + self.certr = CertificateResource( + body=CERT, uri=mock.sentinel.uri, authzrs=(), + cert_chain_uri=mock.sentinel.cert_chain_uri) + + def test_json_de_serializable(self): + assert isinstance(self.certr, jose.JSONDeSerializable) + from acme.messages import CertificateResource + assert self.certr == CertificateResource.from_json(self.certr.to_json()) + + +class RevocationTest(unittest.TestCase): + """Tests for acme.messages.RevocationTest.""" + + def setUp(self): + from acme.messages import Revocation + self.rev = Revocation(certificate=CERT) + + def test_from_json_hashable(self): + from acme.messages import Revocation + hash(Revocation.from_json(self.rev.to_json())) + + +class OrderResourceTest(unittest.TestCase): + """Tests for acme.messages.OrderResource.""" + + def setUp(self): + from acme.messages import OrderResource + self.regr = OrderResource( + body=mock.sentinel.body, uri=mock.sentinel.uri) + + def test_to_partial_json(self): + assert self.regr.to_json() == { + 'body': mock.sentinel.body, + 'uri': mock.sentinel.uri, + 'authorizations': None, + } + + def test_json_de_serializable(self): + from acme.messages import ChallengeBody + from acme.messages import STATUS_PENDING + challbs = ( + ChallengeBody( + uri='http://challb1', status=STATUS_PENDING, + chall=challenges.HTTP01(token=b'IlirfxKKXAsHtmzK29Pj8A')), + ChallengeBody(uri='http://challb2', status=STATUS_PENDING, + chall=challenges.DNS( + token=b'DGyRejmCefe7v4NfDGDKfA')), + ) + + from acme.messages import Authorization + from acme.messages import AuthorizationResource + from acme.messages import Identifier + from acme.messages import IDENTIFIER_FQDN + identifier = Identifier(typ=IDENTIFIER_FQDN, value='example.com') + authz = AuthorizationResource(uri="http://authz1", + body=Authorization( + identifier=identifier, + challenges=challbs)) + from acme.messages import Order + body = Order(identifiers=(identifier,), status=STATUS_PENDING, + authorizations=tuple(challb.uri for challb in challbs)) + from acme.messages import OrderResource + orderr = OrderResource(uri="http://order1", body=body, + csr_pem=b'test blob', + authorizations=(authz,)) + self.assertEqual(orderr, + OrderResource.from_json(orderr.to_json())) + +class NewOrderTest(unittest.TestCase): + """Tests for acme.messages.NewOrder.""" + + def setUp(self): + from acme.messages import NewOrder + self.order = NewOrder( + identifiers=mock.sentinel.identifiers) + + def test_to_partial_json(self): + assert self.order.to_json() == { + 'identifiers': mock.sentinel.identifiers, + } + + def test_default_profile_empty(self): + assert self.order.profile is None + + def test_non_empty_profile(self): + from acme.messages import NewOrder + order = NewOrder(identifiers=mock.sentinel.identifiers, profile='example') + assert order.to_json() == { + 'identifiers': mock.sentinel.identifiers, + 'profile': 'example', + } + + +class JWSPayloadRFC8555Compliant(unittest.TestCase): + """Test for RFC8555 compliance of JWS generated from resources/challenges""" + def test_message_payload(self): + from acme.messages import NewAuthorization + + new_order = NewAuthorization() + + jobj = new_order.json_dumps(indent=2).encode() + # RFC8555 states that JWS bodies must not have a resource field. + assert jobj == b'{}' + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/_internal/tests/standalone_test.py b/acme/src/acme/_internal/tests/standalone_test.py new file mode 100644 index 00000000000..466e00ee9bd --- /dev/null +++ b/acme/src/acme/_internal/tests/standalone_test.py @@ -0,0 +1,199 @@ +"""Tests for acme.standalone.""" +import http.client as http_client +import socket +import socketserver +import sys +import threading +import unittest +from unittest import mock + +import josepy as jose +import pytest +import requests + +from acme import challenges +from acme._internal.tests import test_util + + +class HTTP01ServerTest(unittest.TestCase): + """Tests for acme.standalone.HTTP01Server.""" + + + def setUp(self): + self.account_key = jose.JWK.load( + test_util.load_vector('rsa1024_key.pem')) + self.resources: set = set() + + from acme.standalone import HTTP01Server + self.server = HTTP01Server(('', 0), resources=self.resources) + + self.port = self.server.socket.getsockname()[1] + self.thread = threading.Thread(target=self.server.serve_forever) + self.thread.start() + + def tearDown(self): + self.server.shutdown() + self.thread.join() + self.server.server_close() + + def test_index(self): + response = requests.get( + 'http://localhost:{0}'.format(self.port), verify=False) + assert response.text == 'ACME client standalone challenge solver' + assert response.ok + + def test_404(self): + response = requests.get( + 'http://localhost:{0}/foo'.format(self.port), verify=False) + assert response.status_code == http_client.NOT_FOUND + + def _test_http01(self, add): + chall = challenges.HTTP01(token=(b'x' * 16)) + response, validation = chall.response_and_validation(self.account_key) + + from acme.standalone import HTTP01RequestHandler + resource = HTTP01RequestHandler.HTTP01Resource( + chall=chall, response=response, validation=validation) + if add: + self.resources.add(resource) + return resource.response.simple_verify( + resource.chall, 'localhost', self.account_key.public_key(), + port=self.port) + + def test_http01_found(self): + assert self._test_http01(add=True) + + def test_http01_not_found(self): + assert not self._test_http01(add=False) + + def test_timely_shutdown(self): + from acme.standalone import HTTP01Server + with HTTP01Server(('', 0), resources=set(), timeout=0.05) as server: + server_thread = threading.Thread(target=server.serve_forever) + server_thread.start() + + with socket.socket() as client: + client.connect(('localhost', server.socket.getsockname()[1])) + + stop_thread = threading.Thread(target=server.shutdown) + stop_thread.start() + server_thread.join(5.) + + is_hung = server_thread.is_alive() + try: + client.shutdown(socket.SHUT_RDWR) + except: # pragma: no cover, pylint: disable=bare-except + # may raise error because socket could already be closed + pass + + assert not is_hung, 'Server shutdown should not be hung' + + +class BaseDualNetworkedServersTest(unittest.TestCase): + """Test for acme.standalone.BaseDualNetworkedServers.""" + + class SingleProtocolServer(socketserver.TCPServer): + """Server that only serves on a single protocol. FreeBSD has this behavior for AF_INET6.""" + def __init__(self, *args, **kwargs): + ipv6 = kwargs.pop("ipv6", False) + if ipv6: + self.address_family = socket.AF_INET6 + kwargs["bind_and_activate"] = False + else: + self.address_family = socket.AF_INET + super().__init__(*args, **kwargs) + if ipv6: + # NB: On Windows, socket.IPPROTO_IPV6 constant may be missing. + # We use the corresponding value (41) instead. + level = getattr(socket, "IPPROTO_IPV6", 41) + self.socket.setsockopt(level, socket.IPV6_V6ONLY, 1) + try: + self.server_bind() + self.server_activate() + except: + self.server_close() + raise + + @mock.patch("socket.socket.bind") + def test_fail_to_bind(self, mock_bind): + from errno import EADDRINUSE + + from acme.standalone import BaseDualNetworkedServers + + mock_bind.side_effect = OSError(EADDRINUSE, "Fake addr in use error") + + with pytest.raises(socket.error) as exc_info: + BaseDualNetworkedServers( + BaseDualNetworkedServersTest.SingleProtocolServer, + ('', 0), socketserver.BaseRequestHandler) + + assert exc_info.value.errno == EADDRINUSE + + def test_ports_equal(self): + from acme.standalone import BaseDualNetworkedServers + servers = BaseDualNetworkedServers( + BaseDualNetworkedServersTest.SingleProtocolServer, + ('', 0), + socketserver.BaseRequestHandler) + socknames = servers.getsocknames() + prev_port = None + # assert ports are equal + for sockname in socknames: + port = sockname[1] + if prev_port: + assert prev_port == port + prev_port = port + for server in servers.servers: + server.server_close() + + +class HTTP01DualNetworkedServersTest(unittest.TestCase): + """Tests for acme.standalone.HTTP01DualNetworkedServers.""" + + def setUp(self): + self.account_key = jose.JWK.load( + test_util.load_vector('rsa1024_key.pem')) + self.resources: set = set() + + from acme.standalone import HTTP01DualNetworkedServers + self.servers = HTTP01DualNetworkedServers(('', 0), resources=self.resources) + + self.port = self.servers.getsocknames()[0][1] + self.servers.serve_forever() + + def tearDown(self): + self.servers.shutdown_and_server_close() + + def test_index(self): + response = requests.get( + 'http://localhost:{0}'.format(self.port), verify=False) + assert response.text == 'ACME client standalone challenge solver' + assert response.ok + + def test_404(self): + response = requests.get( + 'http://localhost:{0}/foo'.format(self.port), verify=False) + assert response.status_code == http_client.NOT_FOUND + + def _test_http01(self, add): + chall = challenges.HTTP01(token=(b'x' * 16)) + response, validation = chall.response_and_validation(self.account_key) + + from acme.standalone import HTTP01RequestHandler + resource = HTTP01RequestHandler.HTTP01Resource( + chall=chall, response=response, validation=validation) + if add: + self.resources.add(resource) + return resource.response.simple_verify( + resource.chall, 'localhost', self.account_key.public_key(), + port=self.port) + + def test_http01_found(self): + assert self._test_http01(add=True) + + def test_http01_not_found(self): + assert not self._test_http01(add=False) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/_internal/tests/test_util.py b/acme/src/acme/_internal/tests/test_util.py new file mode 100644 index 00000000000..dfb7b7b8922 --- /dev/null +++ b/acme/src/acme/_internal/tests/test_util.py @@ -0,0 +1,61 @@ +"""Test utilities. + +.. warning:: This module is not part of the public API. + +""" +import importlib.resources +import os +from typing import Callable + +from cryptography import x509 +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization +import josepy as jose +from josepy.util import ComparableECKey + + +def load_vector(*names): + """Load contents of a test vector.""" + # luckily, resource_string opens file in binary mode + vector_ref = importlib.resources.files(__package__).joinpath('testdata', *names) + return vector_ref.read_bytes() + + +def _guess_loader(filename: str, loader_pem: Callable, loader_der: Callable) -> Callable: + _, ext = os.path.splitext(filename) + if ext.lower() == ".pem": + return loader_pem + elif ext.lower() == ".der": + return loader_der + else: # pragma: no cover + raise ValueError("Loader could not be recognized based on extension") + + +def load_cert(*names: str) -> x509.Certificate: + """Load certificate.""" + loader = _guess_loader( + names[-1], x509.load_pem_x509_certificate, x509.load_der_x509_certificate + ) + return loader(load_vector(*names)) + + +def load_csr(*names: str) -> x509.CertificateSigningRequest: + """Load certificate request.""" + loader = _guess_loader(names[-1], x509.load_pem_x509_csr, x509.load_der_x509_csr) + return loader(load_vector(*names)) + + +def load_rsa_private_key(*names): + """Load RSA private key.""" + loader = _guess_loader(names[-1], serialization.load_pem_private_key, + serialization.load_der_private_key) + return jose.ComparableRSAKey(loader( + load_vector(*names), password=None, backend=default_backend())) + + +def load_ecdsa_private_key(*names): + """Load ECDSA private key.""" + loader = _guess_loader(names[-1], serialization.load_pem_private_key, + serialization.load_der_private_key) + return ComparableECKey(loader( + load_vector(*names), password=None, backend=default_backend())) diff --git a/acme/src/acme/_internal/tests/testdata/README b/acme/src/acme/_internal/tests/testdata/README new file mode 100644 index 00000000000..5a3ca4663f9 --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/README @@ -0,0 +1,21 @@ +In order for acme.test_util._guess_loader to work properly, make sure +to use appropriate extension for vector filenames: .pem for PEM and +.der for DER. + +The following command has been used to generate test keys: + + for k in 256 512 1024 2048 4096; do openssl genrsa -out rsa${k}_key.pem $k; done + +and for the CSR: + + openssl req -key rsa2048_key.pem -new -subj '/CN=example.com' -outform DER > csr.der + +and for the certificates: + + openssl req -key rsa2048_key.pem -new -subj '/CN=example.com' -x509 -outform DER > cert.der + openssl req -key rsa2048_key.pem -new -subj '/CN=example.com' -x509 > rsa2048_cert.pem + openssl req -key rsa1024_key.pem -new -subj '/CN=example.com' -x509 > rsa1024_cert.pem + +and for the elliptic key curves: + + openssl genpkey -algorithm EC -out ec_secp384r1.pem -pkeyopt ec_paramgen_curve:P-384 -pkeyopt ec_param_enc:named_curve diff --git a/acme/acme/testdata/cert-100sans.pem b/acme/src/acme/_internal/tests/testdata/cert-100sans.pem similarity index 100% rename from acme/acme/testdata/cert-100sans.pem rename to acme/src/acme/_internal/tests/testdata/cert-100sans.pem diff --git a/acme/acme/testdata/cert-idnsans.pem b/acme/src/acme/_internal/tests/testdata/cert-idnsans.pem similarity index 100% rename from acme/acme/testdata/cert-idnsans.pem rename to acme/src/acme/_internal/tests/testdata/cert-idnsans.pem diff --git a/acme/src/acme/_internal/tests/testdata/cert-ipsans.pem b/acme/src/acme/_internal/tests/testdata/cert-ipsans.pem new file mode 100644 index 00000000000..bb197b38bac --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/cert-ipsans.pem @@ -0,0 +1,21 @@ +-----BEGIN CERTIFICATE----- +MIIDizCCAnOgAwIBAgIIPNBLQXwhoUkwDQYJKoZIhvcNAQELBQAwKDEmMCQGA1UE +AxMdUGViYmxlIEludGVybWVkaWF0ZSBDQSAxNzNiMjYwHhcNMjAwNTI5MTkxODA5 +WhcNMjUwNTI5MTkxODA5WjAWMRQwEgYDVQQDEwsxOTIuMC4yLjE0NTCCASIwDQYJ +KoZIhvcNAQEBBQADggEPADCCAQoCggEBALyChb+NDA26GF1AfC0nzEdfOTchKw0h +q41xEjonvg5UXgZf/aH/ntvugIkYP0MaFifNAjebOVVsemEVEtyWcUKTfBHKZGbZ +ukTDwFIjfTccCfo6U/B2H7ZLzJIywl8DcUw9DypadeQBm8PS0VVR2ncy73dvaqym +crhAwlASyXU0mhLqRDMMxfg5Bn/FWpcsIcDpLmPn8Q/FvdRc2t5ryBNw/aWOlwqT +Oy16nbfLj2T0zG1A3aPuD+eT/JFUe/o3K7R+FAx7wt+RziQO46wLVVF1SueZUrIU +zqN04Gl8Kt1WM2SniZ0gq/rORUNcPtT0NAEsEslTQfA+Trq6j2peqyMCAwEAAaOB +yjCBxzAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUF +BwMCMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFHj1mwZzP//nMIH2i58NRUl/arHn +MB8GA1UdIwQYMBaAFF5DVAKabvIUvKFHGouscA2Qdpe6MDEGCCsGAQUFBwEBBCUw +IzAhBggrBgEFBQcwAYYVaHR0cDovLzEyNy4wLjAuMTo0MDAyMBUGA1UdEQQOMAyH +BMAAApGHBMsAcQEwDQYJKoZIhvcNAQELBQADggEBAHjSgDg76/UCIMSYddyhj18r +LdNKjA7p8ovnErSkebFT4lIZ9f3Sma9moNr0w64M33NamuFyHe/KTdk90mvoW8Uu +26aDekiRIeeMakzbAtDKn67tt2tbedKIYRATcSYVwsV46uZKbM621dZKIjjxOWpo +IY6rZYrku8LYhoXJXOqRduV3cTRVuTm5bBa9FfVNtt6N1T5JOtKKDEhuSaF4RSug +PDy3hQIiHrVvhPfVrXU3j6owz/8UCS5549inES9ONTFrvM9o0H1R/MsmGNXR5hF5 +iJqHKC7n8LZujhVnoFIpHu2Dsiefbfr+yRYJS4I+ezy6Nq/Ok8rc8zp0eoX+uyY= +-----END CERTIFICATE----- diff --git a/acme/src/acme/_internal/tests/testdata/cert-ipv6sans.pem b/acme/src/acme/_internal/tests/testdata/cert-ipv6sans.pem new file mode 100644 index 00000000000..4c2b51156a3 --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/cert-ipv6sans.pem @@ -0,0 +1,22 @@ +-----BEGIN CERTIFICATE----- +MIIDmzCCAoOgAwIBAgIIFdxeZP+v2rgwDQYJKoZIhvcNAQELBQAwKDEmMCQGA1UE +AxMdUGViYmxlIEludGVybWVkaWF0ZSBDQSA0M2M5NTcwHhcNMjAwNTMwMDQwNzMw +WhcNMjUwNTMwMDQwNzMwWjAOMQwwCgYDVQQDEwM6OjEwggEiMA0GCSqGSIb3DQEB +AQUAA4IBDwAwggEKAoIBAQC7VidVduJvqKtrSH0fw6PjE0cqL4Kfzo7klexWUkHG +KVAa0fRVZFZ462jxKOt417V2U4WJQ6WHHO9PJ+3gW62d/MhCw8FRtUQS4nYFjqB6 +32+RFU21VRN7cWoQEqSwnEPbh/v/zv/KS5JhQ+swWUo79AOLm1kjnZWCKtcqh1Lc +Ug5Tkpot6luoxTKp52MkchvXDpj0q2B/XpLJ8/pw5cqjv7mH12EDOK2HXllA+WwX +ZpstcEhaA4FqtaHOW/OHnwTX5MUbINXE5YYHVEDR6moVM31/W/3pe9NDUMTDE7Si +lVQnZbXM9NYbzZqlh+WhemDWwnIfGI6rtsfNEiirVEOlAgMBAAGjgeIwgd8wDgYD +VR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAMBgNV +HRMBAf8EAjAAMB0GA1UdDgQWBBS8DL+MZfDIy6AKky69Tgry2Vxq5DAfBgNVHSME +GDAWgBRAsFqVenRRKgB1YPzWKzb9bzZ/ozAxBggrBgEFBQcBAQQlMCMwIQYIKwYB +BQUHMAGGFWh0dHA6Ly8xMjcuMC4wLjE6NDAwMjAtBgNVHREEJjAkhxAAAAAAAAAA +AAAAAAAAAAABhxCjvjLzIG7HXQlWDO6YWF7FMA0GCSqGSIb3DQEBCwUAA4IBAQBY +M9UTZ3uaKMQ+He9kWR3p9jh6hTSD0FNi79ZdfkG0lgSzhhduhN7OhzQH2ihUUfa6 +rtKTw74fGbszhizCd9UB8YPKlm3si1Xbg6ZUQlA1RtoQo7RUGEa6ZbR68PKGm9Go +hTTFIl/JS8jzxBR8jywZdyqtprUx+nnNUDiNk0hJtFLhw7OJH0AHlAUNqHsfD08m +HXRdaV6q14HXU5g31slBat9H4D6tCU/2uqBURwW0wVdnqh4QeRfAeqiatJS9EmSF +ctbc7n894Idy2Xce7NFoIy5cht3m6Rd42o/LmBsJopBmQcDPZT70/XzRtc2qE0cS +CzBIGQHUJ6BfmBjrCQnp +-----END CERTIFICATE----- diff --git a/acme/acme/testdata/cert-nocn.der b/acme/src/acme/_internal/tests/testdata/cert-nocn.der similarity index 100% rename from acme/acme/testdata/cert-nocn.der rename to acme/src/acme/_internal/tests/testdata/cert-nocn.der diff --git a/acme/acme/testdata/cert-san.pem b/acme/src/acme/_internal/tests/testdata/cert-san.pem similarity index 100% rename from acme/acme/testdata/cert-san.pem rename to acme/src/acme/_internal/tests/testdata/cert-san.pem diff --git a/acme/acme/testdata/cert.der b/acme/src/acme/_internal/tests/testdata/cert.der similarity index 100% rename from acme/acme/testdata/cert.der rename to acme/src/acme/_internal/tests/testdata/cert.der diff --git a/acme/acme/testdata/cert.pem b/acme/src/acme/_internal/tests/testdata/cert.pem similarity index 100% rename from acme/acme/testdata/cert.pem rename to acme/src/acme/_internal/tests/testdata/cert.pem diff --git a/acme/acme/testdata/critical-san.pem b/acme/src/acme/_internal/tests/testdata/critical-san.pem similarity index 100% rename from acme/acme/testdata/critical-san.pem rename to acme/src/acme/_internal/tests/testdata/critical-san.pem diff --git a/acme/acme/testdata/csr-100sans.pem b/acme/src/acme/_internal/tests/testdata/csr-100sans.pem similarity index 100% rename from acme/acme/testdata/csr-100sans.pem rename to acme/src/acme/_internal/tests/testdata/csr-100sans.pem diff --git a/acme/acme/testdata/csr-6sans.pem b/acme/src/acme/_internal/tests/testdata/csr-6sans.pem similarity index 100% rename from acme/acme/testdata/csr-6sans.pem rename to acme/src/acme/_internal/tests/testdata/csr-6sans.pem diff --git a/acme/acme/testdata/csr-idnsans.pem b/acme/src/acme/_internal/tests/testdata/csr-idnsans.pem similarity index 100% rename from acme/acme/testdata/csr-idnsans.pem rename to acme/src/acme/_internal/tests/testdata/csr-idnsans.pem diff --git a/acme/src/acme/_internal/tests/testdata/csr-ipsans.pem b/acme/src/acme/_internal/tests/testdata/csr-ipsans.pem new file mode 100644 index 00000000000..07b3b133002 --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/csr-ipsans.pem @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIICbTCCAVUCAQIwADCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKT/ +CE7Y5EYBvI4p7Frt763upIKHDHO/R5/TWMjG8Jm9qTMui8sbMgyh2Yh+lR/j/5Xd +tQrhgC6wx10MrW2+3JtYS88HP1p6si8zU1dbK34n3NyyklR2RivW0R7dXgnYNy7t +5YcDYLCrbRMIPINV/uHrmzIHWYUDNcZVdAfIM2AHfKYuV6Mepcn///5GR+l4GcAh +Nkf9CW8OdAIuKdbyLCxVr0mUW/vJz1b12uxPsgUdax9sjXgZdT4pfMXADsFd1NeF +atpsXU073inqtHru+2F9ijHTQ75TC+u/rr6eYl3BnBntac0gp/ADtDBii7/Q1JOO +Bhq7xJNqqxIEdiyM7zcCAwEAAaAoMCYGCSqGSIb3DQEJDjEZMBcwFQYDVR0RBA4w +DIcEwAACkYcEywBxATANBgkqhkiG9w0BAQsFAAOCAQEADG5g3zdbSCaXpZhWHkzE +Mek3f442TUE1pB+ITRpthmM4N3zZWETYmbLCIAO624uMrRnbCCMvAoLs/L/9ETg/ +XMMFtonQC8u9i9tV8B1ceBh8lpIfa+8b9TMWH3bqnrbWQ+YIl+Yd0gXiCZWJ9vK4 +eM1Gddu/2bR6s/k4h/XAWRgEexqk57EHr1z0N+T9OoX939n3mVcNI+u9kfd5VJ0z +VyA3R8WR6T6KlEl5P5pcWe5Kuyhi7xMmLVImXqBtvKq4O1AMfM+gQr/yn9aE8IRq +khP7JrMBLUIub1c/qu2TfvnynNPSM/ZcOX+6PHdHmRkR3nI0Ndpv7Ntv31FTplAm +Dw== +-----END CERTIFICATE REQUEST----- diff --git a/acme/src/acme/_internal/tests/testdata/csr-ipv6sans.pem b/acme/src/acme/_internal/tests/testdata/csr-ipv6sans.pem new file mode 100644 index 00000000000..3f0ba1600d5 --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/csr-ipv6sans.pem @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIIChTCCAW0CAQIwADCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAOIc +UAppcqJfTkSqqOFqGt1v7lIJZPOcF4bcKI3d5cHAGbOuVxbC7uMaDuObwYLzoiED +qnvs1NaEq2phO6KsgGESB7IE2LUjJivO7OnSZjNRpL5si/9egvBiNCn/50lULaWG +gLEuyMfk3awZy2mVAymy7Grhbx069A4TH8TqsHuq2RpKyuDL27e/jUt6yYecb3pu +hWMiWy3segif4tI46pkOW0/I6DpxyYD2OqOvzxm/voS9RMqE2+7YJA327H7bEi3N +lJZEZ1zy7clZ9ga5fBQaetzbg2RyxTrZ7F919NQXSFoXgxb10Eg64wIpz0L3ooCm +GEHehsZZexa3J5ccIvMCAwEAAaBAMD4GCSqGSIb3DQEJDjExMC8wLQYDVR0RBCYw +JIcQAAAAAAAAAAAAAAAAAAAAAYcQo74y8yBux10JVgzumFhexTANBgkqhkiG9w0B +AQsFAAOCAQEALvwVn0A/JPTCiNzcozHFnp5M23C9PXCplWc5u4k34d4XXzpSeFDz +fL4gy7NpYIueme2K2ppw2j3PNQUdR6vQ5a75sriegWYrosL+7Q6Joh51ZyEUZQoD +mNl4M4S4oX85EaChR6NFGBywTfjFarYi32XBTbFE7rK8N8KM+DQkNdwL1MXqaHWz +F1obQKpNXlLedbCBOteV5Eg4zG3565zu/Gw/NhwzzV3mQmgxUcd1sMJxAfHQz4Vl +ImLL+xMcR03nDsH2bgtDbK2tJm7WszSxA9tC+Xp2lRewxrnQloRWPYDz177WGQ5Q +SoGDzTTtA6uWZxG8h7CkNLOGvA8LtU2rNA== +-----END CERTIFICATE REQUEST----- diff --git a/acme/src/acme/_internal/tests/testdata/csr-mixed.pem b/acme/src/acme/_internal/tests/testdata/csr-mixed.pem new file mode 100644 index 00000000000..2e7d6eda812 --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/csr-mixed.pem @@ -0,0 +1,16 @@ +-----BEGIN CERTIFICATE REQUEST----- +MIICdjCCAV4CAQAwADCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANoV +T1pdvRUUBOqvm7M2ebLEHV7higUH7qAGUZEkfP6W4YriYVY+IHrH1svNPSa+oPTK +7weDNmT11ehWnGyECIM9z2r2Hi9yVV0ycxh4hWQ4Nt8BAKZwCwaXpyWm7Gj6m2Ez +pSN5Dd67g5YAQBrUUh1+RRbFi9c0Ls/6ZOExMvfg8kqt4c2sXCgH1IFnxvvOjBYo +p7xh0x3L1Akyax0tw8qgQp/z5mkupmVDNJYPFmbzFPMNyDR61ed6QUTDg7P4UAuF +kejLLzFvz5YaO7vC+huaTuPhInAhpzqpr4yU97KIjos2/83Itu/Cv8U1RAeEeRTk +h0WjUfltoem/5f8bIdsCAwEAAaAxMC8GCSqGSIb3DQEJDjEiMCAwHgYDVR0RBBcw +FYINYS5leGVtcGxlLmNvbYcEwAACbzANBgkqhkiG9w0BAQsFAAOCAQEAQ7n/hYen +5INHlcslHPYCQ/BAbX6Ou+Y8hUu8puWNVpE2OM95L2C87jbWwTmCRnkFBwtyoNqo +j3DXVW2RYv8y/exq7V6Y5LtpHTgwfugINJ3XlcVzA4Vnf1xqOxv3kwejkq74RuXn +xd5N28srgiFqb0e4tOAWVI8Tw27bgBqjoXl0QDFPZpctqUia5bcDJ9WzNSM7VaO1 +CBNGHBRz+zL8sqoqJA4HV58tjcgzl+1RtGM+iUHxXpnH+aCNKWIUINrAzIm4Sm00 +93RJjhb1kdNR0BC7ikWVbAWaVviHdvATK/RfpmhWDqfEaNgBpvT91GnkhpzctSFD +ro0yCUUXXrIr0w== +-----END CERTIFICATE REQUEST----- diff --git a/acme/acme/testdata/csr-nosans.pem b/acme/src/acme/_internal/tests/testdata/csr-nosans.pem similarity index 100% rename from acme/acme/testdata/csr-nosans.pem rename to acme/src/acme/_internal/tests/testdata/csr-nosans.pem diff --git a/acme/acme/testdata/csr-san.pem b/acme/src/acme/_internal/tests/testdata/csr-san.pem similarity index 100% rename from acme/acme/testdata/csr-san.pem rename to acme/src/acme/_internal/tests/testdata/csr-san.pem diff --git a/acme/acme/testdata/csr.der b/acme/src/acme/_internal/tests/testdata/csr.der similarity index 100% rename from acme/acme/testdata/csr.der rename to acme/src/acme/_internal/tests/testdata/csr.der diff --git a/acme/acme/testdata/csr.pem b/acme/src/acme/_internal/tests/testdata/csr.pem similarity index 100% rename from acme/acme/testdata/csr.pem rename to acme/src/acme/_internal/tests/testdata/csr.pem diff --git a/acme/acme/testdata/dsa512_key.pem b/acme/src/acme/_internal/tests/testdata/dsa512_key.pem similarity index 100% rename from acme/acme/testdata/dsa512_key.pem rename to acme/src/acme/_internal/tests/testdata/dsa512_key.pem diff --git a/acme/src/acme/_internal/tests/testdata/ec_secp384r1_key.pem b/acme/src/acme/_internal/tests/testdata/ec_secp384r1_key.pem new file mode 100644 index 00000000000..4bc4e68e52b --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/ec_secp384r1_key.pem @@ -0,0 +1,6 @@ +-----BEGIN PRIVATE KEY----- +MIG2AgEAMBAGByqGSM49AgEGBSuBBAAiBIGeMIGbAgEBBDArTn0pbFk3xHfKeXte +xJgS4JVdJQ8mqvezhaNpULZPnwb+mcKLlrj6f5SRM52yREGhZANiAAQcrMoPMVqV +rHnDGGz5HUKLNmXfChlNgsrwsruawXF+M283CA6eckAjTXNyiC/ounWmvtoKsZG0 +2UQOfQUNSCANId/r986yRGc03W6RJSkcRp86qBYjNsLgbZpber/3+M4= +-----END PRIVATE KEY----- diff --git a/acme/src/acme/_internal/tests/testdata/rsa1024_cert.pem b/acme/src/acme/_internal/tests/testdata/rsa1024_cert.pem new file mode 100644 index 00000000000..1b791218195 --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/rsa1024_cert.pem @@ -0,0 +1,13 @@ +-----BEGIN CERTIFICATE----- +MIIB/TCCAWagAwIBAgIJAOyRIBs3QT8QMA0GCSqGSIb3DQEBCwUAMBYxFDASBgNV +BAMMC2V4YW1wbGUuY29tMB4XDTE4MDQyMzEwMzE0NFoXDTE4MDUyMzEwMzE0NFow +FjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ +AoGBAJqJ87R8aVwByONxgQA9hwgvQd/QqI1r1UInXhEF2VnEtZGtUWLi100IpIqr +Mq4qusDwNZ3g8cUPtSkvJGs89djoajMDIJP7lQUEKUYnYrI0q755Tr/DgLWSk7iW +l5ezym0VzWUD0/xXUz8yRbNMTjTac80rS5SZk2ja2wWkYlRJAgMBAAGjUzBRMB0G +A1UdDgQWBBSsaX0IVZ4XXwdeffVAbG7gnxSYjTAfBgNVHSMEGDAWgBSsaX0IVZ4X +XwdeffVAbG7gnxSYjTAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4GB +ADe7SVmvGH2nkwVfONk8TauRUDkePN1CJZKFb2zW1uO9ANJ2v5Arm/OQp0BG/xnI +Djw/aLTNVESF89oe15dkrUErtcaF413MC1Ld5lTCaJLHLGqDKY69e02YwRuxW7jY +qarpt7k7aR5FbcfO5r4V/FK/Gvp4Dmoky8uap7SJIW6x +-----END CERTIFICATE----- diff --git a/acme/acme/testdata/rsa1024_key.pem b/acme/src/acme/_internal/tests/testdata/rsa1024_key.pem similarity index 100% rename from acme/acme/testdata/rsa1024_key.pem rename to acme/src/acme/_internal/tests/testdata/rsa1024_key.pem diff --git a/acme/acme/testdata/rsa2048_cert.pem b/acme/src/acme/_internal/tests/testdata/rsa2048_cert.pem similarity index 100% rename from acme/acme/testdata/rsa2048_cert.pem rename to acme/src/acme/_internal/tests/testdata/rsa2048_cert.pem diff --git a/acme/acme/testdata/rsa2048_key.pem b/acme/src/acme/_internal/tests/testdata/rsa2048_key.pem similarity index 100% rename from acme/acme/testdata/rsa2048_key.pem rename to acme/src/acme/_internal/tests/testdata/rsa2048_key.pem diff --git a/acme/acme/testdata/rsa256_key.pem b/acme/src/acme/_internal/tests/testdata/rsa256_key.pem similarity index 100% rename from acme/acme/testdata/rsa256_key.pem rename to acme/src/acme/_internal/tests/testdata/rsa256_key.pem diff --git a/acme/src/acme/_internal/tests/testdata/rsa4096_cert.pem b/acme/src/acme/_internal/tests/testdata/rsa4096_cert.pem new file mode 100644 index 00000000000..d8f6a98961e --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/rsa4096_cert.pem @@ -0,0 +1,30 @@ +-----BEGIN CERTIFICATE----- +MIIFDTCCAvWgAwIBAgIUImqDrP53V69vFROsjP/gL0YtoA4wDQYJKoZIhvcNAQEL +BQAwFjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wHhcNMjAwNTI3MjMyNDE0WhcNMjAw +NjI2MjMyNDE0WjAWMRQwEgYDVQQDDAtleGFtcGxlLmNvbTCCAiIwDQYJKoZIhvcN +AQEBBQADggIPADCCAgoCggIBANY9LKLk9Dxn0MUMQFHwBoTN4ehDSWBws2KcytpF +mc8m9Mfk1wmb4fQSKYtK3wIFMfIyo9HQu0nKqMkkUw52o3ZXyOv+oWwF5qNy2BKu +lh5OMSkaZ0o13zoPpW42e+IUnyxvg70+0urD+sUue4cyTHh/nBIUjrM/05ZJ/ac8 +HR0RK3H41YoqBjq69JjMZczZZhbNFit3s6p0R1TbVAgc3ckqbtX5BDyQMQQCP4Ed +m4DgbAFVqdcPUCC5W3F3fmuQiPKHiADzONZnXpy6lUvLDWqcd6loKp+nKHM6OkXX +8hmD7pE1PYMQo4hqOfhBR2IgMjAShwd5qUFjl1m2oo0Qm3PFXOk6i2ZQdS6AA/yd +B5/mX0RnM2oIdFZPb6UZFSmtEgs9sTzn+hMUyNSZQRE54px1ur1xws2R+vbsCyM5 ++KoFVxDjVjU9TlZx3GvDvnqz/tbHjji6l8VHZYOBMBUXbKHu2U6pJFZ5Zp7k68/z +a3Fb9Pjtn3iRkXEyC0N5kLgqO4QTlExnxebV8aMvQpWd/qefnMn9qPYIZPEXSQAR +mEBIahkcACb60s+acG0WFFluwBPtBqEr8Q67XlSF0Ibf4iBiRzpPobhlWta1nrFg +4IWHMSoZ0PE75bhIGBEkhrpcXQCAxXmAfxfjKDH7jdJ1fRdnZ/9+OzwYGVX5GH/l +0QDtAgMBAAGjUzBRMB0GA1UdDgQWBBQh3xiz/o1nEU2ySylZ9gxCXvIPGzAfBgNV +HSMEGDAWgBQh3xiz/o1nEU2ySylZ9gxCXvIPGzAPBgNVHRMBAf8EBTADAQH/MA0G +CSqGSIb3DQEBCwUAA4ICAQAELoXz31oR9pdAwidlv9ZBOKiC7KBWy8VMqXNVkfTn +bVRxAUex7zleLFIOkWnqadsMesU9sIwrbLzBcZ8Q/vBY+z2xOPdXcgcAoAmdKWoq +YBQNiqng9r54sqlzB/77QZCf5fdktESe7NTxhCifgx5SAWq7IUQs/lm3tnMUSAfE +5ctuN6M+w8K54y3WDprcfMHpnc3ZHeSPhVQApHM0h/bDvXq0bRS7kmq27Hb153Qm +nH3TwYB5pPSWW38NbUc+s/a7mItO7S8ly8yGbA0j9c/IbN5lM+OCdk06asz3+c8E +uo8nuCBoYO5+6AqC2N7WJ3Tdr/pFA8jTbd6VNVlgCWTIR8ZosL5Fgkfv+4fUBrHt +zdVUqMUzvga5rvZnwnJ5Qfu/drHeAAo9MTNFQNe2QgDlYfWBh5GweolgmFSwrpkY +v/5wLtIyv/ASHKswybbqMIlpttcLTXjx5yuh8swttT6Wh+FQqqQ32KSRB3StiwyK +oH0ZhrwYHiFYNlPxecGX6XUta6rFtTlEdkBGSnXzgiTzL2l+Nc0as0V5B9RninZG +qJ+VOChSQ0OFvg1riSXv7tMvbLdGQnxwTRL3t6BMS8I4LA2m3ZfWUcuXT783ODTH +16f1Q1AgXd2csstTWO9cv+N/0fpX31nqrm6+CrGduSr2u4HjYYnlLIUhmdTvK3fX +Fg== +-----END CERTIFICATE----- diff --git a/acme/src/acme/_internal/tests/testdata/rsa4096_key.pem b/acme/src/acme/_internal/tests/testdata/rsa4096_key.pem new file mode 100644 index 00000000000..b16e1fc9b17 --- /dev/null +++ b/acme/src/acme/_internal/tests/testdata/rsa4096_key.pem @@ -0,0 +1,51 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIJKgIBAAKCAgEA1j0souT0PGfQxQxAUfAGhM3h6ENJYHCzYpzK2kWZzyb0x+TX +CZvh9BIpi0rfAgUx8jKj0dC7ScqoySRTDnajdlfI6/6hbAXmo3LYEq6WHk4xKRpn +SjXfOg+lbjZ74hSfLG+DvT7S6sP6xS57hzJMeH+cEhSOsz/Tlkn9pzwdHRErcfjV +iioGOrr0mMxlzNlmFs0WK3ezqnRHVNtUCBzdySpu1fkEPJAxBAI/gR2bgOBsAVWp +1w9QILlbcXd+a5CI8oeIAPM41mdenLqVS8sNapx3qWgqn6coczo6RdfyGYPukTU9 +gxCjiGo5+EFHYiAyMBKHB3mpQWOXWbaijRCbc8Vc6TqLZlB1LoAD/J0Hn+ZfRGcz +agh0Vk9vpRkVKa0SCz2xPOf6ExTI1JlBETninHW6vXHCzZH69uwLIzn4qgVXEONW +NT1OVnHca8O+erP+1seOOLqXxUdlg4EwFRdsoe7ZTqkkVnlmnuTrz/NrcVv0+O2f +eJGRcTILQ3mQuCo7hBOUTGfF5tXxoy9ClZ3+p5+cyf2o9ghk8RdJABGYQEhqGRwA +JvrSz5pwbRYUWW7AE+0GoSvxDrteVIXQht/iIGJHOk+huGVa1rWesWDghYcxKhnQ +8TvluEgYESSGulxdAIDFeYB/F+MoMfuN0nV9F2dn/347PBgZVfkYf+XRAO0CAwEA +AQKCAgEA0hZdTkQtCYtYm9LexDsXeWYX8VcCfrMmBj7xYcg9A3oVMmzDPuYBVwH0 +gWbjd6y2hOaJ5TfGYZ99kvmvBRDsTSHaoyopC7BhssjtAKz6Ay/0X3VH8usPQ3WS +aZi+NT65tK6KRqtz08ppgLGLa1G00bl5x/Um1rpxeACI4FU/y4BJ1VMJvJpnT3KE +Z86Qyagqx5NH+UpCApZSWPFX3zjHePzGgcfXErjniCHYOnpZQrFQ2KIzkfSvQ9fg +x01ByKOM2CB2C1B33TCzBAioXRH6zyAu7A59NeCK9ywTduhDvie1a+oEryFC7IQW +4s7I/H3MGX4hsf/pLXlHMy+5CZJOjRaC2h+pypfbbcuiXu6Sn64kHNpiI7SxI5DI +MIRjyG7MdUcrzq0Rt8ogwwpbCoRqrl/w3bhxtqmeZaEZtyxbjlm7reK2YkIFDgyz +JMqiJK5ZAi+9L/8c0xhjjAQQ0sIzrjmjA8U+6YnWL9jU5qXTVnBB8XQucyeeZGgk +yRHyMur71qOXN8z3UEva7MHkDTUBlj8DgTz6sEjqCipaWl0CXfDNa4IhHIXD5qiF +wplhq7OeS0v6EGG/UFa3Q/lFntxtrayxJX7uvvSccGzjPKXTjpWUELLi/FdnIsum +eXT3RgIEYozj4BibDXaBLfHTCVzxOr7AAEvKM9XWSUgLA0paSWECggEBAO9ZBeE1 +GWzd1ejTTkcxBC9AK2rNsYG8PdNqiof/iTbuJWNeRqpG+KB/0CNIpjZ2X5xZd0tM +FDpHTFehlP26Roxuq50iRAFc+SN5KoiO0A3JuJAidreIgRTia1saUUrypHqWrYEA +VZVj2AI8Pyg3s1OkR2frFskY7hXBVb/pJNDP/m9xTXXIYiIXYkHYe+4RIJCnAxRv +q5YHKaX+0Ull9YCZJCxmwvcHat8sgu8qkiwUMEM6QSNEkrEbdnWYBABvC1AR6sws +7MP1h9+j22n4Zc/3D6kpFZEL9Erx8nNyhbOZ6q2Tdnf6YKVVjZdyVa8VyNnR0ROl +3BjkFaHb/bg4e4kCggEBAOUk8ZJS3qBeGCOjug384zbHGcnhUBYtYJiOz+RXBtP+ +PRksbFtTkgk1sHuSGO8YRddU4Qv7Av1xL8o+DEsLBSD0YQ7pmLrR/LK+iDQ5N63O +Fve9uJH0ybxAOkiua7G24+lTsVUP//KWToL4Wh5zbHBBjL5D2Z9zoeVbcE87xhva +lImMVr4Ex252DqNP9wkZxBjudFyJ/C/TnXrjPcgwhxWTC7sLQMhE5p+490G7c4hX +PywkIKrANbu37KDiAvVS+dC66ZgpL/NUDkeloAmGNO08LGzbV6YKchlvDyWU/AvW +0hYjbL0FUq7K/wp1G9fumolB+fbI25K9c13X93STzUUCggEBAJDsNFUyk5yJjbYW +C/WrRj9d+WwH9Az77+uNPSgvn+O0usq6EMuVgYGdImfa21lqv2Wp/kOHY1AOT7lX +yyD+oyzw7dSNJOQ2aVwDR6+72Vof5DLRy1RBwPbmSd61xrc8yD658YCEtU1pUSe5 +VvyBDYH9nIbdn8RP5gkiMUusXXBaIFNWJXLFzDWcNxBrhk6V7EPp/EFphFmpKJyr ++AkbRVWCZJbF+hMdWKadCwLJogwyhS6PnVU/dhrq6AU38GRa2Fy5HJRYN1xH1Oej +DX3Su8L6c28Xw0k6FcczTHx+wVoIPkKvYTIwVkiFzt/+iMckx6KsGo5tBSHFKRwC +WlQrTxECggEBALjUruLnY1oZ7AC7bTUhOimSOfQEgTQSUCtebsRxijlvhtsKYTDd +XRt+qidStjgN7S/+8DRYuZWzOeg5WnMhpXZqiOudcyume922IGl3ibjxVsdoyjs5 +J4xohlrgDlBgBMDNWGoTqNGFejjcmNydH+gAh8VlN2INxJYbxqCyx17qVgwJHmLR +uggYxD/pHYvCs9GkbknCp5/wYsOgDtKuihfV741lS1D/esN1UEQ+LrfYIEW7snno +5q7Pcdhn1hkKYCWEzy2Ec4Aj2gzixQ9JqOF/OxpnZvCw1k47rg0TeqcWFYnz8x8Y +7xO8/DH0OoxXk2GJzVXJuItJs4gLzzfCjL0CggEAJFHfC9jisdy7CoWiOpNCSF1B +S0/CWDz77cZdlWkpTdaXGGp1MA/UKUFPIH8sOHfvpKS660+X4G/1ZBHmFb4P5kFF +Qy8UyUMKtSOEdZS6KFlRlfSCAMd5aSTmCvq4OSjYEpMRwUhU/iEJNkn9Z1Soehe0 +U3dxJ8KiT1071geO6rRquSHoSJs6Y0WQKriYYQJOhh4Axs3PQihER2eyh+WGk8YJ +02m0mMsjntqnXtdc6IcdKaHp9ko+OpM9QZLsvt19fxBcrXj/i21uUXrzuNtKfO6M +JqGhsOrO2dh8lMhvodENvgKA0DmYDC9N7ogo7bxTNSedcjBF46FhJoqii8m70Q== +-----END RSA PRIVATE KEY----- diff --git a/acme/acme/testdata/rsa512_key.pem b/acme/src/acme/_internal/tests/testdata/rsa512_key.pem similarity index 100% rename from acme/acme/testdata/rsa512_key.pem rename to acme/src/acme/_internal/tests/testdata/rsa512_key.pem diff --git a/acme/src/acme/_internal/tests/util_test.py b/acme/src/acme/_internal/tests/util_test.py new file mode 100644 index 00000000000..4bda9933bb4 --- /dev/null +++ b/acme/src/acme/_internal/tests/util_test.py @@ -0,0 +1,15 @@ +"""Tests for acme.util.""" +import sys + +import pytest + + +def test_it(): + from acme.util import map_keys + assert {'a': 'b', 'c': 'd'} == \ + map_keys({'a': 'b', 'c': 'd'}, lambda key: key) + assert {2: 2, 4: 4} == map_keys({1: 2, 3: 4}, lambda x: x + 1) + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/acme/src/acme/challenges.py b/acme/src/acme/challenges.py new file mode 100644 index 00000000000..6b979e906dd --- /dev/null +++ b/acme/src/acme/challenges.py @@ -0,0 +1,478 @@ +"""ACME Identifier Validation Challenges.""" +import abc +import functools +import hashlib +import ipaddress +import logging +from typing import Any +from typing import cast +from typing import Mapping +from typing import Optional +from typing import TypeVar +from typing import Union + +from cryptography.hazmat.primitives import hashes +import josepy as jose +import requests + +logger = logging.getLogger(__name__) + +GenericChallenge = TypeVar('GenericChallenge', bound='Challenge') + + +class Challenge(jose.TypedJSONObjectWithFields): + # _fields_to_partial_json + """ACME challenge.""" + TYPES: dict[str, type['Challenge']] = {} + + @classmethod + def from_json(cls: type[GenericChallenge], + jobj: Mapping[str, Any]) -> Union[GenericChallenge, 'UnrecognizedChallenge']: + try: + return cast(GenericChallenge, super().from_json(jobj)) + except jose.UnrecognizedTypeError as error: + logger.debug(error) + return UnrecognizedChallenge.from_json(jobj) + + +class ChallengeResponse(jose.TypedJSONObjectWithFields): + # _fields_to_partial_json + """ACME challenge response.""" + TYPES: dict[str, type['ChallengeResponse']] = {} + + def to_partial_json(self) -> dict[str, Any]: + # Removes the `type` field which is inserted by TypedJSONObjectWithFields.to_partial_json. + # This field breaks RFC8555 compliance. + jobj = super().to_partial_json() + jobj.pop(self.type_field_name, None) + return jobj + + +class UnrecognizedChallenge(Challenge): + """Unrecognized challenge. + + ACME specification defines a generic framework for challenges and + defines some standard challenges that are implemented in this + module. However, other implementations (including peers) might + define additional challenge types, which should be ignored if + unrecognized. + + :ivar jobj: Original JSON decoded object. + + """ + jobj: dict[str, Any] + + def __init__(self, jobj: Mapping[str, Any]) -> None: + super().__init__() + object.__setattr__(self, "jobj", jobj) + + def to_partial_json(self) -> dict[str, Any]: + return self.jobj # pylint: disable=no-member + + @classmethod + def from_json(cls, jobj: Mapping[str, Any]) -> 'UnrecognizedChallenge': + return cls(jobj) + + +class _TokenChallenge(Challenge): + """Challenge with token. + + :ivar bytes token: + + """ + TOKEN_SIZE = 128 // 8 # Based on the entropy value from the spec + """Minimum size of the :attr:`token` in bytes.""" + + # TODO: acme-spec doesn't specify token as base64-encoded value + token: bytes = jose.field( + "token", encoder=jose.encode_b64jose, decoder=functools.partial( + jose.decode_b64jose, size=TOKEN_SIZE, minimum=True)) + + # XXX: rename to ~token_good_for_url + @property + def good_token(self) -> bool: # XXX: @token.decoder + """Is `token` good? + + .. todo:: acme-spec wants "It MUST NOT contain any non-ASCII + characters", but it should also warrant that it doesn't + contain ".." or "/"... + + """ + # TODO: check that path combined with uri does not go above + # URI_ROOT_PATH! + # pylint: disable=unsupported-membership-test + return b'..' not in self.token and b'/' not in self.token + + +class KeyAuthorizationChallengeResponse(ChallengeResponse): + """Response to Challenges based on Key Authorization. + + :param str key_authorization: + + """ + key_authorization: str = jose.field("keyAuthorization") + thumbprint_hash_function = hashes.SHA256 + + def verify(self, chall: 'KeyAuthorizationChallenge', account_public_key: jose.JWK) -> bool: + """Verify the key authorization. + + :param KeyAuthorization chall: Challenge that corresponds to + this response. + :param JWK account_public_key: + + :return: ``True`` iff verification of the key authorization was + successful. + :rtype: bool + + """ + parts = self.key_authorization.split('.') # pylint: disable=no-member + if len(parts) != 2: + logger.debug("Key authorization (%r) is not well formed", + self.key_authorization) + return False + + if parts[0] != chall.encode("token"): + logger.debug("Mismatching token in key authorization: " + "%r instead of %r", parts[0], chall.encode("token")) + return False + + thumbprint = jose.b64encode(account_public_key.thumbprint( + hash_function=self.thumbprint_hash_function)).decode() + if parts[1] != thumbprint: + logger.debug("Mismatching thumbprint in key authorization: " + "%r instead of %r", parts[0], thumbprint) + return False + + return True + + def to_partial_json(self) -> dict[str, Any]: + jobj = super().to_partial_json() + jobj.pop('keyAuthorization', None) + return jobj + + +# TODO: Make this method a generic of K (bound=KeyAuthorizationChallenge), response_cls of type +# Type[K] and use it in response/response_and_validation return types once Python 3.6 support is +# dropped (do not support generic ABC classes, see https://github.com/python/typing/issues/449). +class KeyAuthorizationChallenge(_TokenChallenge, metaclass=abc.ABCMeta): + """Challenge based on Key Authorization. + + :param response_cls: Subclass of `KeyAuthorizationChallengeResponse` + that will be used to generate ``response``. + :param str typ: type of the challenge + """ + typ: str = NotImplemented + response_cls: type[KeyAuthorizationChallengeResponse] = NotImplemented + thumbprint_hash_function = ( + KeyAuthorizationChallengeResponse.thumbprint_hash_function) + + def key_authorization(self, account_key: jose.JWK) -> str: + """Generate Key Authorization. + + :param JWK account_key: + :rtype str: + + """ + return self.encode("token") + "." + jose.b64encode( + account_key.thumbprint( + hash_function=self.thumbprint_hash_function)).decode() + + def response(self, account_key: jose.JWK) -> KeyAuthorizationChallengeResponse: + """Generate response to the challenge. + + :param JWK account_key: + + :returns: Response (initialized `response_cls`) to the challenge. + :rtype: KeyAuthorizationChallengeResponse + + """ + return self.response_cls( # pylint: disable=not-callable + key_authorization=self.key_authorization(account_key)) + + @abc.abstractmethod + def validation(self, account_key: jose.JWK, **kwargs: Any) -> Any: + """Generate validation for the challenge. + + Subclasses must implement this method, but they are likely to + return completely different data structures, depending on what's + necessary to complete the challenge. Interpretation of that + return value must be known to the caller. + + :param JWK account_key: + :returns: Challenge-specific validation. + + """ + raise NotImplementedError() # pragma: no cover + + def response_and_validation(self, account_key: jose.JWK, *args: Any, **kwargs: Any + ) -> tuple[KeyAuthorizationChallengeResponse, Any]: + """Generate response and validation. + + Convenience function that return results of `response` and + `validation`. + + :param JWK account_key: + :rtype: tuple + + """ + return (self.response(account_key), + self.validation(account_key, *args, **kwargs)) + + +@ChallengeResponse.register +class DNS01Response(KeyAuthorizationChallengeResponse): + """ACME dns-01 challenge response.""" + typ = "dns-01" + + def simple_verify(self, chall: 'DNS01', domain: str, account_public_key: jose.JWK) -> bool: # pylint: disable=unused-argument + """Simple verify. + + This method no longer checks DNS records and is a simple wrapper + around `KeyAuthorizationChallengeResponse.verify`. + + :param challenges.DNS01 chall: Corresponding challenge. + :param str domain: Domain name being verified. + :param JWK account_public_key: Public key for the key pair + being authorized. + + :return: ``True`` iff verification of the key authorization was + successful. + :rtype: bool + + """ + verified = self.verify(chall, account_public_key) + if not verified: + logger.debug("Verification of key authorization in response failed") + return verified + + +@Challenge.register +class DNS01(KeyAuthorizationChallenge): + """ACME dns-01 challenge.""" + response_cls = DNS01Response + typ = response_cls.typ + + LABEL = "_acme-challenge" + """Label clients prepend to the domain name being validated.""" + + def validation(self, account_key: jose.JWK, **unused_kwargs: Any) -> str: + """Generate validation. + + :param JWK account_key: + :rtype: str + + """ + return jose.b64encode(hashlib.sha256(self.key_authorization( + account_key).encode("utf-8")).digest()).decode() + + def validation_domain_name(self, name: str) -> str: + """Domain name for TXT validation record. + + :param str name: Domain name being validated. + :rtype: str + + """ + return f"{self.LABEL}.{name}" + + +@ChallengeResponse.register +class HTTP01Response(KeyAuthorizationChallengeResponse): + """ACME http-01 challenge response.""" + typ = "http-01" + + PORT = 80 + """Verification port as defined by the protocol. + + You can override it (e.g. for testing) by passing ``port`` to + `simple_verify`. + + """ + + WHITESPACE_CUTSET = "\n\r\t " + """Whitespace characters which should be ignored at the end of the body.""" + + def simple_verify(self, chall: 'HTTP01', domain: str, account_public_key: jose.JWK, + port: Optional[int] = None, timeout: int = 30) -> bool: + """Simple verify. + + :param challenges.SimpleHTTP chall: Corresponding challenge. + :param str domain: Domain name being verified. + :param JWK account_public_key: Public key for the key pair + being authorized. + :param int port: Port used in the validation. + :param int timeout: Timeout in seconds. + + :returns: ``True`` iff validation with the files currently served by the + HTTP server is successful. + :rtype: bool + + """ + if not self.verify(chall, account_public_key): + logger.debug("Verification of key authorization in response failed") + return False + + # TODO: ACME specification defines URI template that doesn't + # allow to use a custom port... Make sure port is not in the + # request URI, if it's standard. + if port is not None and port != self.PORT: + logger.warning( + "Using non-standard port for http-01 verification: %s", port) + domain += ":{0}".format(port) + + uri = chall.uri(domain) + logger.debug("Verifying %s at %s...", chall.typ, uri) + try: + http_response = requests.get(uri, verify=False, timeout=timeout) + except requests.exceptions.RequestException as error: + logger.error("Unable to reach %s: %s", uri, error) + return False + # By default, http_response.text will try to guess the encoding to use + # when decoding the response to Python unicode strings. This guesswork + # is error prone. RFC 8555 specifies that HTTP-01 responses should be + # key authorizations with possible trailing whitespace. Since key + # authorizations must be composed entirely of the base64url alphabet + # plus ".", we tell requests that the response should be ASCII. See + # https://datatracker.ietf.org/doc/html/rfc8555#section-8.3 for more + # info. + http_response.encoding = "ascii" + logger.debug("Received %s: %s. Headers: %s", http_response, + http_response.text, http_response.headers) + + challenge_response = http_response.text.rstrip(self.WHITESPACE_CUTSET) + if self.key_authorization != challenge_response: + logger.debug("Key authorization from response (%r) doesn't match " + "HTTP response (%r)", self.key_authorization, + challenge_response) + return False + + return True + + +@Challenge.register +class HTTP01(KeyAuthorizationChallenge): + """ACME http-01 challenge.""" + response_cls = HTTP01Response + typ = response_cls.typ + + URI_ROOT_PATH = ".well-known/acme-challenge" + """URI root path for the server provisioned resource.""" + + @property + def path(self) -> str: + """Path (starting with '/') for provisioned resource. + + :rtype: str + + """ + return '/' + self.URI_ROOT_PATH + '/' + self.encode('token') + + def uri(self, identifier: str) -> str: + """Create an URI to the provisioned resource. + + Forms an URI to the HTTPS server provisioned resource + (containing :attr:`~SimpleHTTP.token`). + + :param str identifier: Domain name or IP address being verified. + :rtype: str + + """ + try: + # https://datatracker.ietf.org/doc/html/rfc2732#section-2 + # IPv6 addresses in URLs should be enclosed in brackets. + ipaddress.IPv6Address(identifier) + identifier = "[" + identifier + "]" + except ipaddress.AddressValueError: + pass + return "http://" + identifier + self.path + + def validation(self, account_key: jose.JWK, **unused_kwargs: Any) -> str: + """Generate validation. + + :param JWK account_key: + :rtype: str + + """ + return self.key_authorization(account_key) + + +@Challenge.register +class DNS(_TokenChallenge): + """ACME "dns" challenge.""" + typ = "dns" + + LABEL = "_acme-challenge" + """Label clients prepend to the domain name being validated.""" + + def gen_validation(self, account_key: jose.JWK, alg: jose.JWASignature = jose.RS256, + **kwargs: Any) -> jose.JWS: + """Generate validation. + + :param .JWK account_key: Private account key. + :param .JWA alg: + + :returns: This challenge wrapped in `.JWS` + :rtype: .JWS + + """ + return jose.JWS.sign( + payload=self.json_dumps(sort_keys=True).encode('utf-8'), + key=account_key, alg=alg, **kwargs) + + def check_validation(self, validation: jose.JWS, account_public_key: jose.JWK) -> bool: + """Check validation. + + :param JWS validation: + :param JWK account_public_key: + :rtype: bool + + """ + if not validation.verify(key=account_public_key): + return False + try: + return self == self.json_loads( + validation.payload.decode('utf-8')) + except jose.DeserializationError as error: + logger.debug("Checking validation for DNS failed: %s", error) + return False + + def gen_response(self, account_key: jose.JWK, **kwargs: Any) -> 'DNSResponse': + """Generate response. + + :param .JWK account_key: Private account key. + :param .JWA alg: + + :rtype: DNSResponse + + """ + return DNSResponse(validation=self.gen_validation(account_key, **kwargs)) + + def validation_domain_name(self, name: str) -> str: + """Domain name for TXT validation record. + + :param str name: Domain name being validated. + + """ + return "{0}.{1}".format(self.LABEL, name) + + +@ChallengeResponse.register +class DNSResponse(ChallengeResponse): + """ACME "dns" challenge response. + + :param JWS validation: + + """ + typ = "dns" + + validation: jose.JWS = jose.field("validation", decoder=jose.JWS.from_json) + + def check_validation(self, chall: 'DNS', account_public_key: jose.JWK) -> bool: + """Check validation. + + :param challenges.DNS chall: + :param JWK account_public_key: + + :rtype: bool + + """ + return chall.check_validation(self.validation, account_public_key) diff --git a/acme/src/acme/client.py b/acme/src/acme/client.py new file mode 100644 index 00000000000..671e1332ebe --- /dev/null +++ b/acme/src/acme/client.py @@ -0,0 +1,848 @@ +"""ACME client API.""" +import base64 +import datetime +from email.utils import parsedate_tz +import http.client as http_client +import logging +import math +import random +import time +from typing import Any +from typing import cast +from typing import Mapping +from typing import Optional +from typing import Union + +from cryptography import x509 + +import josepy as jose +import requests +from requests.adapters import HTTPAdapter +from requests.utils import parse_header_links + +from acme import challenges +from acme import crypto_util +from acme import errors +from acme import jws +from acme import messages + +logger = logging.getLogger(__name__) + +DEFAULT_NETWORK_TIMEOUT = 45 + + +class ClientV2: + """ACME client for a v2 API. + + :ivar messages.Directory directory: + :ivar .ClientNetwork net: Client network. + """ + + def __init__(self, directory: messages.Directory, net: 'ClientNetwork') -> None: + """Initialize. + + :param .messages.Directory directory: Directory Resource + :param .ClientNetwork net: Client network. + """ + self.directory = directory + self.net = net + + def new_account(self, new_account: messages.NewRegistration) -> messages.RegistrationResource: + """Register. + + :param .NewRegistration new_account: + + :raises .ConflictError: in case the account already exists + + :returns: Registration Resource. + :rtype: `.RegistrationResource` + """ + response = self._post(self.directory['newAccount'], new_account) + # if account already exists + if response.status_code == 200 and 'Location' in response.headers: + raise errors.ConflictError(response.headers['Location']) + # "Instance of 'Field' has no key/contact member" bug: + regr = self._regr_from_response(response) + self.net.account = regr + return regr + + def query_registration(self, regr: messages.RegistrationResource + ) -> messages.RegistrationResource: + """Query server about registration. + + :param messages.RegistrationResource regr: Existing Registration + Resource. + + """ + self.net.account = self._get_v2_account(regr, True) + + return self.net.account + + def update_registration(self, regr: messages.RegistrationResource, + update: Optional[messages.Registration] = None + ) -> messages.RegistrationResource: + """Update registration. + + :param messages.RegistrationResource regr: Registration Resource. + :param messages.Registration update: Updated body of the + resource. If not provided, body will be taken from `regr`. + + :returns: Updated Registration Resource. + :rtype: `.RegistrationResource` + + """ + # https://github.com/certbot/certbot/issues/6155 + regr = self._get_v2_account(regr) + + update = regr.body if update is None else update + body = messages.UpdateRegistration(**dict(update)) + updated_regr = self._send_recv_regr(regr, body=body) + self.net.account = updated_regr + return updated_regr + + def _get_v2_account(self, regr: messages.RegistrationResource, update_body: bool = False + ) -> messages.RegistrationResource: + self.net.account = None + only_existing_reg = regr.body.update(only_return_existing=True) + response = self._post(self.directory['newAccount'], only_existing_reg) + updated_uri = response.headers['Location'] + new_regr = regr.update(body=messages.Registration.from_json(response.json()) + if update_body else regr.body, + uri=updated_uri) + self.net.account = new_regr + return new_regr + + def new_order(self, csr_pem: bytes, profile: Optional[str] = None) -> messages.OrderResource: + """Request a new Order object from the server. + + :param bytes csr_pem: A CSR in PEM format. + + :returns: The newly created order. + :rtype: OrderResource + """ + csr = x509.load_pem_x509_csr(csr_pem) + dns_names, ip_addrs = crypto_util.get_identifiers_from_x509(csr.subject, csr.extensions) + identifiers = [] + for name in dns_names: + identifiers.append(messages.Identifier(typ=messages.IDENTIFIER_FQDN, + value=name)) + for ip in ip_addrs: + identifiers.append(messages.Identifier(typ=messages.IDENTIFIER_IP, + value=str(ip))) + if profile is None: + profile = "" + order = messages.NewOrder(identifiers=identifiers, profile=profile) + response = self._post(self.directory['newOrder'], order) + body = messages.Order.from_json(response.json()) + authorizations = [] + # pylint has trouble understanding our josepy based objects which use + # things like custom metaclass logic. body.authorizations should be a + # list of strings containing URLs so let's disable this check here. + for url in body.authorizations: # pylint: disable=not-an-iterable + authorizations.append(self._authzr_from_response(self._post_as_get(url), uri=url)) + return messages.OrderResource( + body=body, + uri=response.headers.get('Location'), + authorizations=authorizations, + csr_pem=csr_pem) + + def poll(self, authzr: messages.AuthorizationResource + ) -> tuple[messages.AuthorizationResource, requests.Response]: + """Poll Authorization Resource for status. + + :param authzr: Authorization Resource + :type authzr: `.AuthorizationResource` + + :returns: Updated Authorization Resource and HTTP response. + + :rtype: (`.AuthorizationResource`, `requests.Response`) + + """ + response = self._post_as_get(authzr.uri) + updated_authzr = self._authzr_from_response( + response, authzr.body.identifier, authzr.uri) + return updated_authzr, response + + def poll_and_finalize(self, orderr: messages.OrderResource, + deadline: Optional[datetime.datetime] = None) -> messages.OrderResource: + """Poll authorizations and finalize the order. + + If no deadline is provided, this method will timeout after 90 + seconds. + + :param messages.OrderResource orderr: order to finalize + :param datetime.datetime deadline: when to stop polling and timeout + + :returns: finalized order + :rtype: messages.OrderResource + + """ + if deadline is None: + deadline = datetime.datetime.now() + datetime.timedelta(seconds=90) + orderr = self.poll_authorizations(orderr, deadline) + return self.finalize_order(orderr, deadline) + + def poll_authorizations(self, orderr: messages.OrderResource, deadline: datetime.datetime + ) -> messages.OrderResource: + """Poll Order Resource for status.""" + responses = [] + for url in orderr.body.authorizations: + while datetime.datetime.now() < deadline: + authzr = self._authzr_from_response(self._post_as_get(url), uri=url) + if authzr.body.status != messages.STATUS_PENDING: # pylint: disable=no-member + responses.append(authzr) + break + time.sleep(1) + # If we didn't get a response for every authorization, we fell through + # the bottom of the loop due to hitting the deadline. + if len(responses) < len(orderr.body.authorizations): + raise errors.TimeoutError() + failed = [] + for authzr in responses: + if authzr.body.status != messages.STATUS_VALID: + for chall in authzr.body.challenges: + if chall.error is not None: + failed.append(authzr) + if failed: + raise errors.ValidationError(failed) + return orderr.update(authorizations=responses) + + def begin_finalization(self, orderr: messages.OrderResource + ) -> messages.OrderResource: + """Start the process of finalizing an order. + + :param messages.OrderResource orderr: order to finalize + :param datetime.datetime deadline: when to stop polling and timeout + + :returns: updated order + :rtype: messages.OrderResource + + :raises .messages.Error: If server indicates order is not yet in ready state, + it will return a 403 (Forbidden) error with a problem document/error code of type + "orderNotReady" + + """ + csr = x509.load_pem_x509_csr(orderr.csr_pem) + wrapped_csr = messages.CertificateRequest(csr=csr) + res = self._post(orderr.body.finalize, wrapped_csr) + orderr = orderr.update(body=messages.Order.from_json(res.json())) + return orderr + + def poll_finalization(self, orderr: messages.OrderResource, + deadline: datetime.datetime, + fetch_alternative_chains: bool = False + ) -> messages.OrderResource: + """ + Poll an order that has been finalized for its status. + If it becomes valid, obtain the certificate. + + If a finalization request previously returned `orderNotReady`, + poll until ready, send a new finalization request, and continue + polling until valid as above. + + :returns: finalized order (with certificate) + :rtype: messages.OrderResource + """ + sleep_seconds: float = 1 + while datetime.datetime.now() < deadline: + if sleep_seconds > 0: + time.sleep(sleep_seconds) + response = self._post_as_get(orderr.uri) + body = messages.Order.from_json(response.json()) + if body.status == messages.STATUS_INVALID: + # "invalid": The certificate will not be issued. Consider this + # order process abandoned. + if body.error is not None: + raise errors.IssuanceError(body.error) + raise errors.Error( + "The certificate order failed. No further information was provided " + "by the server.") + elif body.status == messages.STATUS_READY: + # "ready": The server agrees that the requirements have been + # fulfilled, and is awaiting finalization. Submit a finalization + # request. + self.begin_finalization(orderr) + sleep_seconds = 1 + elif body.status == messages.STATUS_VALID and body.certificate is not None: + # "valid": The server has issued the certificate and provisioned its + # URL to the "certificate" field of the order. Download the + # certificate. + certificate_response = self._post_as_get(body.certificate) + orderr = orderr.update(body=body, fullchain_pem=certificate_response.text) + if fetch_alternative_chains: + alt_chains_urls = self._get_links(certificate_response, 'alternate') + alt_chains = [self._post_as_get(url).text for url in alt_chains_urls] + orderr = orderr.update(alternative_fullchains_pem=alt_chains) + return orderr + elif body.status == messages.STATUS_PROCESSING: + # "processing": The certificate is being issued. Send a POST-as-GET request after + # the time given in the Retry-After header field of the response, if any. + retry_after = self.retry_after(response, 1) + # Whatever Retry-After the ACME server requests, the polling must not take + # longer than the overall deadline + retry_after = min(retry_after, deadline) + sleep_seconds = (retry_after - datetime.datetime.now()).total_seconds() + raise errors.TimeoutError() + + def finalize_order(self, orderr: messages.OrderResource, deadline: datetime.datetime, + fetch_alternative_chains: bool = False) -> messages.OrderResource: + """Finalize an order and obtain a certificate. + + :param messages.OrderResource orderr: order to finalize + :param datetime.datetime deadline: when to stop polling and timeout + :param bool fetch_alternative_chains: whether to also fetch alternative + certificate chains + + :returns: finalized order + :rtype: messages.OrderResource + + """ + try: + self.begin_finalization(orderr) + except messages.Error as e: + if e.code != 'orderNotReady': + raise e + return self.poll_finalization(orderr, deadline, fetch_alternative_chains) + + def renewal_time(self, cert_pem: bytes + ) -> tuple[Optional[datetime.datetime], datetime.datetime]: + """Return an appropriate time to attempt renewal of the certificate, + and the next time to ask the ACME server for renewal info. + + If the certificate has already expired, renewal info isn't checked. + Instead, the certificate's notAfter time is returned and the certificate + should be immediately renewed. + + If the ACME directory has a "renewalInfo" field, the response will be + based on a fetch of the renewal info resource for the certificate + (https://www.ietf.org/archive/id/draft-ietf-acme-ari-08.html). + + If there is no "renewalInfo" field, this function will return a tuple of + None, and the next time to ask the ACME server for renewal info. + + This function may make other network calls in the future (e.g., OCSP + or CRL). + + :param bytes cert_pem: cert as pem file + + :returns: Tuple of time to attempt renewal, next time to ask for renewal info + + :raises errors.ARIError: If an error occurs fetching ARI from the + server. Explicit exception chaining is used so the original error + can be accessed through the __cause__ attribute on the ARIError if + desired. + + """ + now = datetime.datetime.now() + # https://www.ietf.org/archive/id/draft-ietf-acme-ari-08.html#section-4.3.3 + default_retry_after = datetime.timedelta(seconds=6 * 60 * 60) + + cert = x509.load_pem_x509_certificate(cert_pem) + + # from https://www.ietf.org/archive/id/draft-ietf-acme-ari-08.html#section-4.3, "Clients + # MUST NOT check a certificate's RenewalInfo after the certificate has expired." + # + # we call datetime.datetime.now here with the UTC argument to create a timezone aware + # datetime object that can be compared with the UTC notAfter from cryptography + if cert.not_valid_after_utc < datetime.datetime.now(datetime.timezone.utc): + return cert.not_valid_after_utc, now + default_retry_after + + try: + renewal_info_base_url = self.directory['renewalInfo'] + except KeyError: + return None, now + default_retry_after + + ari_url = renewal_info_base_url + '/' + _renewal_info_path_component(cert) + try: + resp = self.net.get(ari_url, content_type='application/json') + except Exception as e: # pylint: disable=broad-except + error_msg = f'failed to fetch renewal_info URL {ari_url}' + raise errors.ARIError(error_msg, now + default_retry_after) from e + renewal_info: messages.RenewalInfo = messages.RenewalInfo.from_json(resp.json()) + + start = renewal_info.suggested_window.start # pylint: disable=no-member + end = renewal_info.suggested_window.end # pylint: disable=no-member + + delta_seconds = (end - start).total_seconds() + random_seconds = random.uniform(0, delta_seconds) + random_time = start + datetime.timedelta(seconds=random_seconds) + + retry_after = self.retry_after(resp, default_retry_after.seconds) + return random_time, retry_after + + + def revoke(self, cert: x509.Certificate, rsn: int) -> None: + """Revoke certificate. + + :param x509.Certificate cert: `x509.Certificate` + + :param int rsn: Reason code for certificate revocation. + + :raises .ClientError: If revocation is unsuccessful. + + """ + self._revoke(cert, rsn, self.directory['revokeCert']) + + def external_account_required(self) -> bool: + """Checks if ACME server requires External Account Binding authentication.""" + return hasattr(self.directory, 'meta') and \ + hasattr(self.directory.meta, 'external_account_required') and \ + self.directory.meta.external_account_required + + def _post_as_get(self, *args: Any, **kwargs: Any) -> requests.Response: + """ + Send GET request using the POST-as-GET protocol. + :param args: + :param kwargs: + :return: + """ + new_args = args[:1] + (None,) + args[1:] + return self._post(*new_args, **kwargs) + + def _get_links(self, response: requests.Response, relation_type: str) -> list[str]: + """ + Retrieves all Link URIs of relation_type from the response. + :param requests.Response response: The requests HTTP response. + :param str relation_type: The relation type to filter by. + """ + # Can't use response.links directly because it drops multiple links + # of the same relation type, which is possible in RFC8555 responses. + if 'Link' not in response.headers: + return [] + links = parse_header_links(response.headers['Link']) + return [link['url'] for link in links + if 'rel' in link and 'url' in link and link['rel'] == relation_type] + + @classmethod + def get_directory(cls, url: str, net: 'ClientNetwork') -> messages.Directory: + """ + Retrieves the ACME directory (RFC 8555 section 7.1.1) from the ACME server. + :param str url: the URL where the ACME directory is available + :param ClientNetwork net: the ClientNetwork to use to make the request + + :returns: the ACME directory object + :rtype: messages.Directory + """ + return messages.Directory.from_json(net.get(url).json()) + + @classmethod + def _regr_from_response(cls, response: requests.Response, uri: Optional[str] = None, + terms_of_service: Optional[str] = None + ) -> messages.RegistrationResource: + if 'terms-of-service' in response.links: + terms_of_service = response.links['terms-of-service']['url'] + + return messages.RegistrationResource( + body=messages.Registration.from_json(response.json()), + uri=response.headers.get('Location', uri), + terms_of_service=terms_of_service) + + def _send_recv_regr(self, regr: messages.RegistrationResource, + body: messages.Registration) -> messages.RegistrationResource: + response = self._post(regr.uri, body) + + # TODO: Boulder returns httplib.ACCEPTED + #assert response.status_code == httplib.OK + + # TODO: Boulder does not set Location or Link on update + # (c.f. acme-spec #94) + + return self._regr_from_response( + response, uri=regr.uri, + terms_of_service=regr.terms_of_service) + + def _post(self, *args: Any, **kwargs: Any) -> requests.Response: + """Wrapper around self.net.post that adds the newNonce URL. + + This is used to retry the request in case of a badNonce error. + + """ + kwargs.setdefault('new_nonce_url', getattr(self.directory, 'newNonce')) + return self.net.post(*args, **kwargs) + + def deactivate_registration(self, regr: messages.RegistrationResource + ) -> messages.RegistrationResource: + """Deactivate registration. + + :param messages.RegistrationResource regr: The Registration Resource + to be deactivated. + + :returns: The Registration resource that was deactivated. + :rtype: `.RegistrationResource` + + """ + return self.update_registration(regr, messages.Registration.from_json( + {"status": "deactivated", "contact": None})) + + def deactivate_authorization(self, + authzr: messages.AuthorizationResource + ) -> messages.AuthorizationResource: + """Deactivate authorization. + + :param messages.AuthorizationResource authzr: The Authorization resource + to be deactivated. + + :returns: The Authorization resource that was deactivated. + :rtype: `.AuthorizationResource` + + """ + body = messages.UpdateAuthorization(status='deactivated') + response = self._post(authzr.uri, body) + return self._authzr_from_response(response, + authzr.body.identifier, authzr.uri) + + def _authzr_from_response(self, response: requests.Response, + identifier: Optional[messages.Identifier] = None, + uri: Optional[str] = None) -> messages.AuthorizationResource: + authzr = messages.AuthorizationResource( + body=messages.Authorization.from_json(response.json()), + uri=response.headers.get('Location', uri)) + if identifier is not None and authzr.body.identifier != identifier: # pylint: disable=no-member + raise errors.UnexpectedUpdate(authzr) + return authzr + + def answer_challenge(self, challb: messages.ChallengeBody, + response: challenges.ChallengeResponse) -> messages.ChallengeResource: + """Answer challenge. + + :param challb: Challenge Resource body. + :type challb: `.ChallengeBody` + + :param response: Corresponding Challenge response + :type response: `.challenges.ChallengeResponse` + + :returns: Challenge Resource with updated body. + :rtype: `.ChallengeResource` + + :raises .UnexpectedUpdate: + + """ + resp = self._post(challb.uri, response) + try: + authzr_uri = resp.links['up']['url'] + except KeyError: + raise errors.ClientError('"up" Link header missing') + challr = messages.ChallengeResource( + authzr_uri=authzr_uri, + body=messages.ChallengeBody.from_json(resp.json())) + # TODO: check that challr.uri == resp.headers['Location']? + if challr.uri != challb.uri: + raise errors.UnexpectedUpdate(challr.uri) + return challr + + @classmethod + def retry_after(cls, response: requests.Response, default: int) -> datetime.datetime: + """Compute next `poll` time based on response ``Retry-After`` header. + + Handles integers and various datestring formats per + https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.37 + + :param requests.Response response: Response from `poll`. + :param int default: Default value (in seconds), used when + ``Retry-After`` header is not present or invalid. + + :returns: Time point when next `poll` should be performed. + :rtype: `datetime.datetime` + + """ + retry_after = response.headers.get('Retry-After', str(default)) + try: + seconds = int(retry_after) + except ValueError: + # The RFC 2822 parser handles all of RFC 2616's cases in modern + # environments (primarily HTTP 1.1+ but also py27+) + when = parsedate_tz(retry_after) + if when is not None: + try: + tz_secs = datetime.timedelta(when[-1] if when[-1] is not None else 0) + return datetime.datetime(*when[:7]) - tz_secs + except (ValueError, OverflowError): + pass + seconds = default + + return datetime.datetime.now() + datetime.timedelta(seconds=seconds) + + def _revoke(self, cert: x509.Certificate, rsn: int, url: str) -> None: + """Revoke certificate. + + :param .x509.Certificate cert: `x509.Certificate` + + :param int rsn: Reason code for certificate revocation. + + :param str url: ACME URL to post to + + :raises .ClientError: If revocation is unsuccessful. + + """ + response = self._post(url, + messages.Revocation( + certificate=cert, + reason=rsn)) + if response.status_code != http_client.OK: + raise errors.ClientError( + 'Successful revocation must return HTTP OK status') + + +class ClientNetwork: + """Wrapper around requests that signs POSTs for authentication. + + Also adds user agent, and handles Content-Type. + """ + JSON_CONTENT_TYPE = 'application/json' + JOSE_CONTENT_TYPE = 'application/jose+json' + JSON_ERROR_CONTENT_TYPE = 'application/problem+json' + REPLAY_NONCE_HEADER = 'Replay-Nonce' + + """Initialize. + + :param josepy.JWK key: Account private key. Required to use .post(). + :param messages.RegistrationResource account: Account object. Required if you are + planning to use .post() for anything other than creating a new account; + may be set later after registering. + :param josepy.JWASignature alg: Algorithm to use in signing JWS. + :param bool verify_ssl: Whether to verify certificates on SSL connections. + :param str user_agent: String to send as User-Agent header. + :param int timeout: Timeout for requests. + """ + def __init__(self, key: Optional[jose.JWK] = None, + account: Optional[messages.RegistrationResource] = None, + alg: jose.JWASignature = jose.RS256, verify_ssl: bool = True, + user_agent: str = 'acme-python', timeout: int = DEFAULT_NETWORK_TIMEOUT) -> None: + self.key = key + self.account = account + self.alg = alg + self.verify_ssl = verify_ssl + self._nonces: set[str] = set() + self.user_agent = user_agent + self.session = requests.Session() + self._default_timeout = timeout + adapter = HTTPAdapter() + + self.session.mount("http://", adapter) + self.session.mount("https://", adapter) + + def __del__(self) -> None: + # Try to close the session, but don't show exceptions to the + # user if the call to close() fails. See #4840. + try: + self.session.close() + except Exception: # pylint: disable=broad-except + pass + + def _wrap_in_jws(self, obj: jose.JSONDeSerializable, nonce: str, url: str) -> str: + """Wrap `JSONDeSerializable` object in JWS. + + .. todo:: Implement ``acmePath``. + + :param josepy.JSONDeSerializable obj: + :param str url: The URL to which this object will be POSTed + :param str nonce: + :rtype: str + + """ + jobj = obj.json_dumps(indent=2).encode() if obj else b'' + logger.debug('JWS payload:\n%s', jobj) + assert self.key + kwargs = { + "alg": self.alg, + "nonce": nonce, + "url": url, + "key": self.key + } + # newAccount and revokeCert work without the kid + # newAccount must not have kid + if self.account is not None: + kwargs["kid"] = self.account["uri"] + return jws.JWS.sign(jobj, **cast(Mapping[str, Any], kwargs)).json_dumps(indent=2) + + @classmethod + def _check_response(cls, response: requests.Response, + content_type: Optional[str] = None) -> requests.Response: + """Check response content and its type. + + .. note:: + Checking is not strict: wrong server response ``Content-Type`` + HTTP header is ignored if response is an expected JSON object + (c.f. Boulder #56). + + :param str content_type: Expected Content-Type response header. + If JSON is expected and not present in server response, this + function will raise an error. Otherwise, wrong Content-Type + is ignored, but logged. + + :raises .messages.Error: If server response body + carries HTTP Problem (https://datatracker.ietf.org/doc/html/rfc7807). + :raises .ClientError: In case of other networking errors. + + """ + response_ct = response.headers.get('Content-Type') + # Strip parameters from the media-type (rfc2616#section-3.7) + if response_ct: + response_ct = response_ct.split(';')[0].strip() + try: + # TODO: response.json() is called twice, once here, and + # once in _get and _post clients + jobj = response.json() + except ValueError: + jobj = None + + if response.status_code == 409: + raise errors.ConflictError(response.headers.get('Location', 'UNKNOWN-LOCATION')) + + if not response.ok: + if jobj is not None: + if response_ct != cls.JSON_ERROR_CONTENT_TYPE: + logger.debug( + 'Ignoring wrong Content-Type (%r) for JSON Error', + response_ct) + try: + raise messages.Error.from_json(jobj) + except jose.DeserializationError as error: + # Couldn't deserialize JSON object + raise errors.ClientError((response, error)) + else: + # response is not JSON object + raise errors.ClientError(response) + else: + if jobj is not None and response_ct != cls.JSON_CONTENT_TYPE: + logger.debug( + 'Ignoring wrong Content-Type (%r) for JSON decodable ' + 'response', response_ct) + + if content_type == cls.JSON_CONTENT_TYPE and jobj is None: + raise errors.ClientError(f'Unexpected response Content-Type: {response_ct}') + + return response + + def _send_request(self, method: str, url: str, *args: Any, **kwargs: Any) -> requests.Response: + """Send HTTP request. + + Makes sure that `verify_ssl` is respected. Logs request and + response (with headers). For allowed parameters please see + `requests.request`. + + :param str method: method for the new `requests.Request` object + :param str url: URL for the new `requests.Request` object + + :raises requests.exceptions.RequestException: in case of any problems + + :returns: HTTP Response + :rtype: `requests.Response` + + + """ + if method == "POST": + logger.debug('Sending POST request to %s:\n%s', + url, kwargs['data']) + else: + logger.debug('Sending %s request to %s.', method, url) + kwargs['verify'] = self.verify_ssl + kwargs.setdefault('headers', {}) + kwargs['headers'].setdefault('User-Agent', self.user_agent) + kwargs.setdefault('timeout', self._default_timeout) + response = self.session.request(method, url, *args, **kwargs) + + # If an Accept header was sent in the request, the response may not be + # UTF-8 encoded. In this case, we don't set response.encoding and log + # the base64 response instead of raw bytes to keep binary data out of the logs. + debug_content: Union[bytes, str] + if "Accept" in kwargs["headers"]: + debug_content = base64.b64encode(response.content) + else: + # We set response.encoding so response.text knows the response is + # UTF-8 encoded instead of trying to guess the encoding that was + # used which is error prone. This setting affects all future + # accesses of .text made on the returned response object as well. + response.encoding = "utf-8" + debug_content = response.text + logger.debug('Received response:\nHTTP %d\n%s\n\n%s', + response.status_code, + "\n".join("{0}: {1}".format(k, v) + for k, v in response.headers.items()), + debug_content) + return response + + def head(self, *args: Any, **kwargs: Any) -> requests.Response: + """Send HEAD request without checking the response. + + Note, that `_check_response` is not called, as it is expected + that status code other than successfully 2xx will be returned, or + messages2.Error will be raised by the server. + + """ + return self._send_request('HEAD', *args, **kwargs) + + def get(self, url: str, content_type: str = JSON_CONTENT_TYPE, + **kwargs: Any) -> requests.Response: + """Send GET request and check response.""" + return self._check_response( + self._send_request('GET', url, **kwargs), content_type=content_type) + + def _add_nonce(self, response: requests.Response) -> None: + if self.REPLAY_NONCE_HEADER in response.headers: + nonce = response.headers[self.REPLAY_NONCE_HEADER] + try: + decoded_nonce = jws.Header._fields['nonce'].decode(nonce) + except jose.DeserializationError as error: + raise errors.BadNonce(nonce, error) + logger.debug('Storing nonce: %s', nonce) + self._nonces.add(decoded_nonce) + else: + raise errors.MissingNonce(response) + + def _get_nonce(self, url: str, new_nonce_url: str) -> str: + if not self._nonces: + logger.debug('Requesting fresh nonce') + if new_nonce_url is None: + response = self.head(url) + else: + # request a new nonce from the acme newNonce endpoint + response = self._check_response(self.head(new_nonce_url), content_type=None) + self._add_nonce(response) + return self._nonces.pop() + + def post(self, *args: Any, **kwargs: Any) -> requests.Response: + """POST object wrapped in `.JWS` and check response. + + If the server responded with a badNonce error, the request will + be retried once. + + """ + try: + return self._post_once(*args, **kwargs) + except messages.Error as error: + if error.code == 'badNonce': + logger.debug('Retrying request after error:\n%s', error) + return self._post_once(*args, **kwargs) + raise + + def _post_once(self, url: str, obj: jose.JSONDeSerializable, + content_type: str = JOSE_CONTENT_TYPE, **kwargs: Any) -> requests.Response: + new_nonce_url = kwargs.pop('new_nonce_url', None) + if not self.key: + raise errors.Error("acme.ClientNetwork with no private key can't POST.") + data = self._wrap_in_jws(obj, self._get_nonce(url, new_nonce_url), url) + kwargs.setdefault('headers', {'Content-Type': content_type}) + response = self._send_request('POST', url, data=data, **kwargs) + response = self._check_response(response, content_type=content_type) + self._add_nonce(response) + return response + +def _renewal_info_path_component(cert: x509.Certificate) -> str: + akid_ext = cert.extensions.get_extension_for_oid(x509.ExtensionOID.AUTHORITY_KEY_IDENTIFIER) + key_identifier = akid_ext.value.key_identifier # type: ignore[attr-defined] + + akid_encoded = base64.urlsafe_b64encode(key_identifier).decode('ascii').replace("=", "") + + # We add one to the reported bit_length so there is room for the sign bit. + # https://docs.python.org/3/library/stdtypes.html#int.bit_length + # "Return the number of bits necessary to represent an integer in binary, excluding + # the sign and leading zeros" + serial = cert.serial_number + encoded_serial_len = math.ceil((serial.bit_length()+1)/8) + # Serials are encoded as ASN.1 INTEGERS, which means big endian and signed (two's complement). + # https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/#integer-encoding + serial_bytes = serial.to_bytes(encoded_serial_len, byteorder='big', signed=True) + serial_encoded = base64.urlsafe_b64encode(serial_bytes).decode('ascii').replace("=", "") + + return f"{akid_encoded}.{serial_encoded}" diff --git a/acme/src/acme/crypto_util.py b/acme/src/acme/crypto_util.py new file mode 100644 index 00000000000..a58badda93c --- /dev/null +++ b/acme/src/acme/crypto_util.py @@ -0,0 +1,331 @@ +"""Crypto utilities.""" +import enum +from datetime import datetime, timedelta, timezone +import ipaddress +import logging +from types import ModuleType +import typing +from typing import Any +from typing import Optional +from typing import Union +import warnings +import sys + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import dsa, rsa, ec, ed25519, ed448, types +from cryptography.hazmat.primitives.serialization import Encoding +from OpenSSL import crypto + +logger = logging.getLogger(__name__) + + +# https://github.com/pyca/cryptography/blob/1eab7a67dcc34b568e3c0df64e6222a2ac74b1ee/src/cryptography/utils.py#L66-L113 +class _ClientDeprecationModule(ModuleType): + """ + Internal class delegating to a module, and displaying warnings when attributes + related to deprecated attributes in the acme.client module. + """ + def __init__(self, module: ModuleType) -> None: + super().__init__(module.__name__) + self.__dict__['_module'] = module + + def __getattr__(self, attr: str) -> Any: + if attr == 'Format': + warnings.warn("acme.crypto_util.Format is deprecated and will be removed in " + "the next major release.", DeprecationWarning) + return getattr(self._module, attr) + + def __setattr__(self, attr: str, value: Any) -> None: # pragma: no cover + setattr(self._module, attr, value) + + def __delattr__(self, attr: str) -> None: # pragma: no cover + delattr(self._module, attr) + + def __dir__(self) -> list[str]: # pragma: no cover + return ['_module'] + dir(self._module) + + +# Patching ourselves to warn about deprecation and planned removal of some elements in the module. +sys.modules[__name__] = _ClientDeprecationModule(sys.modules[__name__]) + + +class Format(enum.IntEnum): + """File format to be used when parsing or serializing X.509 structures. Deprecated. + + Backwards compatible with the `FILETYPE_ASN1` and `FILETYPE_PEM` constants + from pyOpenSSL. + """ + DER = crypto.FILETYPE_ASN1 + PEM = crypto.FILETYPE_PEM + + def to_cryptography_encoding(self) -> Encoding: + """Converts the Format to the corresponding cryptography `Encoding`. + """ + if self == Format.DER: + return Encoding.DER + else: + return Encoding.PEM + + +# Annoyingly, due to a mypy bug, we can't use Union[] types in +# isinstance expressions without causing false mypy errors. So we have to +# recreate the type collection as a tuple here. And no, typing.get_args doesn't +# work due to another mypy bug. +# +# mypy issues: +# * https://github.com/python/mypy/issues/17680 +# * https://github.com/python/mypy/issues/15106 +CertificateIssuerPrivateKeyTypesTpl = ( + dsa.DSAPrivateKey, + rsa.RSAPrivateKey, + ec.EllipticCurvePrivateKey, + ed25519.Ed25519PrivateKey, + ed448.Ed448PrivateKey, +) + + +def make_csr( + private_key_pem: bytes, + domains: Optional[Union[set[str], list[str]]] = None, + must_staple: bool = False, + ipaddrs: Optional[list[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]] = None, +) -> bytes: + """Generate a CSR containing domains or IPs as subjectAltNames. + + Parameters are ordered this way for backwards compatibility when called using positional + arguments. + + :param buffer private_key_pem: Private key, in PEM PKCS#8 format. + :param list domains: List of DNS names to include in subjectAltNames of CSR. + :param bool must_staple: Whether to include the TLS Feature extension (aka + OCSP Must Staple: https://tools.ietf.org/html/rfc7633). + :param list ipaddrs: List of IPaddress(type ipaddress.IPv4Address or ipaddress.IPv6Address) + names to include in subbjectAltNames of CSR. + + :returns: buffer PEM-encoded Certificate Signing Request. + + """ + private_key = serialization.load_pem_private_key(private_key_pem, password=None) + if not isinstance(private_key, CertificateIssuerPrivateKeyTypesTpl): + raise ValueError(f"Invalid private key type: {type(private_key)}") + if domains is None: + domains = [] + if ipaddrs is None: + ipaddrs = [] + if len(domains) + len(ipaddrs) == 0: + raise ValueError( + "At least one of domains or ipaddrs parameter need to be not empty" + ) + + builder = ( + x509.CertificateSigningRequestBuilder() + .subject_name(x509.Name([])) + .add_extension( + x509.SubjectAlternativeName( + [x509.DNSName(d) for d in domains] + + [x509.IPAddress(i) for i in ipaddrs] + ), + critical=False, + ) + ) + if must_staple: + builder = builder.add_extension( + # "status_request" is the feature commonly known as OCSP + # Must-Staple + x509.TLSFeature([x509.TLSFeatureType.status_request]), + critical=False, + ) + + csr = builder.sign(private_key, hashes.SHA256()) + return csr.public_bytes(Encoding.PEM) + + +def get_names_from_subject_and_extensions( + subject: x509.Name, exts: x509.Extensions +) -> list[str]: + """Gets all DNS SANs as well as the first Common Name from subject. + + :param subject: Name of the x509 object, which may include Common Name + :type subject: `cryptography.x509.Name` + :param exts: Extensions of the x509 object, which may include SANs + :type exts: `cryptography.x509.Extensions` + + :returns: List of DNS Subject Alternative Names and first Common Name + :rtype: `list` of `str` + """ + dns_names, _ = get_identifiers_from_x509(subject, exts) + return dns_names + + +def get_identifiers_from_x509( + subject: x509.Name, exts: x509.Extensions +) -> tuple[list[str], list[str]]: + """Gets all DNS and/or IP address SANs as well as the first Common Name from subject. + + The CN will be first in the list of DNS names, if present. + + :param subject: Name of the x509 object, which may include Common Name + :type subject: `cryptography.x509.Name` + :param exts: Extensions of the x509 object, which may include SANs + :type exts: `cryptography.x509.Extensions` + + :returns: Tuple containing DNS names and IP addresses. + """ + # We know these are always `str` because `bytes` is only possible for + # other OIDs. + cns = [ + typing.cast(str, c.value) + for c in subject.get_attributes_for_oid(x509.NameOID.COMMON_NAME) + ] + try: + san_ext = exts.get_extension_for_class(x509.SubjectAlternativeName) + except x509.ExtensionNotFound: + dns_names = [] + ip_addresses = [] + else: + dns_names = san_ext.value.get_values_for_type(x509.DNSName) + ip_addresses = [str(ip) for ip in san_ext.value.get_values_for_type(x509.IPAddress)] + + if not cns: + return dns_names, ip_addresses + else: + # We only include the first CN, if there are multiple. This matches + # the behavior of the previous implementation using pyOpenSSL. + return [cns[0]] + [d for d in dns_names if d != cns[0]], ip_addresses + + +def _cryptography_cert_or_req_san( + cert_or_req: Union[x509.Certificate, x509.CertificateSigningRequest], +) -> list[str]: + """Get Subject Alternative Names from certificate or CSR using cryptography. + + .. note:: Although this is `acme` internal API, it is used by + `letsencrypt`. + + :param cert_or_req: Certificate or CSR. + :type cert_or_req: `x509.Certificate` or `x509.CertificateSigningRequest`. + + :returns: A list of Subject Alternative Names that is DNS. + :rtype: `list` of `str` + + Deprecated + .. deprecated: 3.2.1 + """ + # ???: is this translation needed? + exts = cert_or_req.extensions + try: + san_ext = exts.get_extension_for_class(x509.SubjectAlternativeName) + except x509.ExtensionNotFound: + return [] + + return san_ext.value.get_values_for_type(x509.DNSName) + + +# Helper function that can be mocked in unit tests +def _now() -> datetime: + return datetime.now(tz=timezone.utc) + + +def make_self_signed_cert(private_key: types.CertificateIssuerPrivateKeyTypes, + domains: Optional[list[str]] = None, + not_before: Optional[datetime] = None, + validity: Optional[timedelta] = None, force_san: bool = True, + extensions: Optional[list[x509.Extension]] = None, + ips: Optional[list[Union[ipaddress.IPv4Address, + ipaddress.IPv6Address]]] = None + ) -> x509.Certificate: + """Generate new self-signed certificate. + :param buffer private_key_pem: Private key, in PEM PKCS#8 format. + :type domains: `list` of `str` + :param int not_before: A datetime after which the cert is valid. If no + timezone is specified, UTC is assumed + :type not_before: `datetime.datetime` + :param validity: Duration for which the cert will be valid. Defaults to 1 + week + :type validity: `datetime.timedelta` + :param buffer private_key_pem: One of + `cryptography.hazmat.primitives.asymmetric.types.CertificateIssuerPrivateKeyTypes` + :param bool force_san: + :param extensions: List of additional extensions to include in the cert. + :type extensions: `list` of `x509.Extension[x509.ExtensionType]` + :type ips: `list` of (`ipaddress.IPv4Address` or `ipaddress.IPv6Address`) + If more than one domain is provided, all of the domains are put into + ``subjectAltName`` X.509 extension and first domain is set as the + subject CN. If only one domain is provided no ``subjectAltName`` + extension is used, unless `force_san` is ``True``. + """ + warnings.warn("make_self_signed_cert is deprecated and will be removed in " + "an upcoming release", DeprecationWarning) + assert domains or ips, "Must provide one or more hostnames or IPs for the cert." + + builder = x509.CertificateBuilder() + builder = builder.serial_number(x509.random_serial_number()) + + if extensions is not None: + for ext in extensions: + builder = builder.add_extension(ext.value, ext.critical) + if domains is None: + domains = [] + if ips is None: + ips = [] + builder = builder.add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + + name_attrs = [] + if len(domains) > 0: + name_attrs.append(x509.NameAttribute( + x509.OID_COMMON_NAME, + domains[0] + )) + + builder = builder.subject_name(x509.Name(name_attrs)) + builder = builder.issuer_name(x509.Name(name_attrs)) + + sanlist: list[x509.GeneralName] = [] + for address in domains: + sanlist.append(x509.DNSName(address)) + for ip in ips: + sanlist.append(x509.IPAddress(ip)) + if force_san or len(domains) > 1 or len(ips) > 0: + builder = builder.add_extension( + x509.SubjectAlternativeName(sanlist), + critical=False + ) + + if not_before is None: + not_before = _now() + if validity is None: + validity = timedelta(seconds=7 * 24 * 60 * 60) + builder = builder.not_valid_before(not_before) + builder = builder.not_valid_after(not_before + validity) + + public_key = private_key.public_key() + builder = builder.public_key(public_key) + return builder.sign(private_key, hashes.SHA256()) + + +def dump_cryptography_chain( + chain: list[x509.Certificate], + encoding: Encoding = Encoding.PEM, +) -> bytes: + """Dump certificate chain into a bundle. + + :param list chain: List of `cryptography.x509.Certificate`. + + :returns: certificate chain bundle + :rtype: bytes + + Deprecated + .. deprecated: 3.2.1 + """ + # XXX: returns empty string when no chain is available, which + # shuts up RenewableCert, but might not be the best solution... + if encoding not in (Encoding.PEM, Encoding.DER): + raise ValueError("Encoding must be PEM or DER") + + def _dump_cert(cert: x509.Certificate) -> bytes: + return cert.public_bytes(encoding) + + # assumes that x509.Certificate.public_bytes includes ending + # newline character + return b"".join(_dump_cert(cert) for cert in chain) diff --git a/acme/src/acme/errors.py b/acme/src/acme/errors.py new file mode 100644 index 00000000000..0e9c09cfb23 --- /dev/null +++ b/acme/src/acme/errors.py @@ -0,0 +1,157 @@ +"""ACME errors.""" +import datetime +import typing +from typing import Any +from typing import Mapping + +from josepy import errors as jose_errors +import requests + +# We import acme.messages only during type check to avoid circular dependencies. Type references +# to acme.message.* must be quoted to be lazily initialized and avoid compilation errors. +if typing.TYPE_CHECKING: + from acme import messages # pragma: no cover + + +class Error(Exception): + """Generic ACME error.""" + + +class DependencyError(Error): + """Dependency error""" + + +class SchemaValidationError(jose_errors.DeserializationError): + """JSON schema ACME object validation error.""" + + +class ClientError(Error): + """Network error.""" + + +class UnexpectedUpdate(ClientError): + """Unexpected update error.""" + + +class NonceError(ClientError): + """Server response nonce error.""" + + +class BadNonce(NonceError): + """Bad nonce error.""" + def __init__(self, nonce: str, error: Exception, *args: Any) -> None: + super().__init__(*args) + self.nonce = nonce + self.error = error + + def __str__(self) -> str: + return 'Invalid nonce ({0!r}): {1}'.format(self.nonce, self.error) + + +class MissingNonce(NonceError): + """Missing nonce error. + + According to the specification an "ACME server MUST include an + Replay-Nonce header field in each successful response to a POST it + provides to a client (...)". + + :ivar requests.Response ~.response: HTTP Response + + """ + def __init__(self, response: requests.Response, *args: Any) -> None: + super().__init__(*args) + self.response = response + + def __str__(self) -> str: + return ('Server {0} response did not include a replay ' + 'nonce, headers: {1} (This may be a service outage)'.format( + self.response.request.method, self.response.headers)) + + +class PollError(ClientError): + """Generic error when polling for authorization fails. + + This might be caused by either timeout (`exhausted` will be non-empty) + or by some authorization being invalid. + + :ivar exhausted: Set of `.AuthorizationResource` that didn't finish + within max allowed attempts. + :ivar updated: Mapping from original `.AuthorizationResource` + to the most recently updated one + + """ + def __init__(self, exhausted: set['messages.AuthorizationResource'], + updated: Mapping['messages.AuthorizationResource', + 'messages.AuthorizationResource'] + ) -> None: + self.exhausted = exhausted + self.updated = updated + super().__init__() + + @property + def timeout(self) -> bool: + """Was the error caused by timeout?""" + return bool(self.exhausted) + + def __repr__(self) -> str: + return '{0}(exhausted={1!r}, updated={2!r})'.format( + self.__class__.__name__, self.exhausted, self.updated) + + +class ValidationError(Error): + """Error for authorization failures. Contains a list of authorization + resources, each of which is invalid and should have an error field. + """ + def __init__(self, failed_authzrs: list['messages.AuthorizationResource']) -> None: + self.failed_authzrs = failed_authzrs + super().__init__() + + def __str__(self) -> str: + msg = [] + for authzr in self.failed_authzrs: + msg.append(f'Authorization for {authzr.body.identifier.value} ' \ + 'failed due to one or more failed challenges:') + for challenge in authzr.body.challenges: + msg.append(f' Challenge {challenge.chall.typ} failed ' \ + f'with error {str(challenge.error)}') + return '\n'.join(msg) + + +class TimeoutError(Error): # pylint: disable=redefined-builtin + """Error for when polling an authorization or an order times out.""" + + +class IssuanceError(Error): + """Error sent by the server after requesting issuance of a certificate.""" + + def __init__(self, error: 'messages.Error') -> None: + """Initialize. + + :param messages.Error error: The error provided by the server. + """ + self.error = error + super().__init__() + + +class ConflictError(ClientError): + """Error for when the server returns a 409 (Conflict) HTTP status. + + In the version of ACME implemented by Boulder, this is used to find an + account if you only have the private key, but don't know the account URL. + + Also used in V2 of the ACME client for the same purpose. + """ + def __init__(self, location: str) -> None: + self.location = location + super().__init__() + + +class WildcardUnsupportedError(Error): + """Error for when a wildcard is requested but is unsupported by ACME CA.""" + + +class ARIError(ClientError): + """An error occurred during an ARI request and we want to suggest a Retry-After time.""" + def __init__(self, message: str, retry_after: datetime.datetime) -> None: + super().__init__(message, retry_after) + self.retry_after = retry_after diff --git a/acme/src/acme/fields.py b/acme/src/acme/fields.py new file mode 100644 index 00000000000..b6d76a52c0f --- /dev/null +++ b/acme/src/acme/fields.py @@ -0,0 +1,60 @@ +"""ACME JSON fields.""" +import datetime +import logging +from typing import Any + +import josepy as jose +import pyrfc3339 + +logger = logging.getLogger(__name__) + + +class Fixed(jose.Field): + """Fixed field.""" + + def __init__(self, json_name: str, value: Any) -> None: + self.value = value + super().__init__( + json_name=json_name, default=value, omitempty=False) + + def decode(self, value: Any) -> Any: + if value != self.value: + raise jose.DeserializationError('Expected {0!r}'.format(self.value)) + return self.value + + def encode(self, value: Any) -> Any: + if value != self.value: + logger.warning( + 'Overriding fixed field (%s) with %r', self.json_name, value) + return value + + +class RFC3339Field(jose.Field): + """RFC3339 field encoder/decoder. + + Handles decoding/encoding between RFC3339 strings and aware (not + naive) `datetime.datetime` objects + (e.g. ``datetime.datetime.now(datetime.timezone.utc)``). + + """ + + @classmethod + def default_encoder(cls, value: datetime.datetime) -> str: + return pyrfc3339.generate(value) + + @classmethod + def default_decoder(cls, value: str) -> datetime.datetime: + try: + return pyrfc3339.parse(value) + except ValueError as error: + raise jose.DeserializationError(error) + + +def fixed(json_name: str, value: Any) -> Any: + """Generates a type-friendly Fixed field.""" + return Fixed(json_name, value) + + +def rfc3339(json_name: str, omitempty: bool = False) -> Any: + """Generates a type-friendly RFC3339 field.""" + return RFC3339Field(json_name, omitempty=omitempty) diff --git a/acme/src/acme/jws.py b/acme/src/acme/jws.py new file mode 100644 index 00000000000..4b6a2c0ea1e --- /dev/null +++ b/acme/src/acme/jws.py @@ -0,0 +1,59 @@ +"""ACME-specific JWS. + +The JWS implementation in josepy only implements the base JOSE standard. In +order to support the new header fields defined in ACME, this module defines some +ACME-specific classes that layer on top of josepy. +""" +from typing import Optional + +import josepy as jose + + +class Header(jose.Header): + """ACME-specific JOSE Header. Implements nonce, kid, and url. + """ + nonce: Optional[bytes] = jose.field('nonce', omitempty=True, encoder=jose.encode_b64jose) + kid: Optional[str] = jose.field('kid', omitempty=True) + url: Optional[str] = jose.field('url', omitempty=True) + + # Mypy does not understand the josepy magic happening here, and falsely claims + # that nonce is redefined. Let's ignore the type check here. + @nonce.decoder # type: ignore[no-redef,union-attr] + def nonce(value: str) -> bytes: # type: ignore[misc] # pylint: disable=no-self-argument,missing-function-docstring + try: + return jose.decode_b64jose(value) + except jose.DeserializationError as error: + # TODO: custom error + raise jose.DeserializationError("Invalid nonce: {0}".format(error)) + + +class Signature(jose.Signature): + """ACME-specific Signature. Uses ACME-specific Header for customer fields.""" + __slots__ = jose.Signature._orig_slots # pylint: disable=protected-access,no-member + + # TODO: decoder/encoder should accept cls? Otherwise, subclassing + # JSONObjectWithFields is tricky... + header_cls = Header + header: Header = jose.field( + 'header', omitempty=True, default=header_cls(), + decoder=header_cls.from_json) + + # TODO: decoder should check that nonce is in the protected header + + +class JWS(jose.JWS): + """ACME-specific JWS. Includes none, url, and kid in protected header.""" + signature_cls = Signature + __slots__ = jose.JWS._orig_slots # pylint: disable=protected-access + + @classmethod + # type: ignore[override] # pylint: disable=arguments-differ + def sign(cls, payload: bytes, key: jose.JWK, alg: jose.JWASignature, nonce: Optional[bytes], + url: Optional[str] = None, kid: Optional[str] = None) -> jose.JWS: + # Per ACME spec, jwk and kid are mutually exclusive, so only include a + # jwk field if kid is not provided. + include_jwk = kid is None + return super().sign(payload, key=key, alg=alg, + protect=frozenset(['nonce', 'url', 'kid', 'jwk', 'alg']), + nonce=nonce, url=url, kid=kid, + include_jwk=include_jwk) diff --git a/acme/src/acme/messages.py b/acme/src/acme/messages.py new file mode 100644 index 00000000000..e56297e77ae --- /dev/null +++ b/acme/src/acme/messages.py @@ -0,0 +1,709 @@ +"""ACME protocol messages.""" +from collections.abc import Hashable +import datetime +import json +from typing import Any +from typing import Iterator +from typing import Mapping +from typing import MutableMapping +from typing import Optional +from typing import TypeVar + +from cryptography import x509 + +import josepy as jose + +from acme import challenges +from acme import errors +from acme import fields +from acme import jws +from acme import util + +ERROR_PREFIX = "urn:ietf:params:acme:error:" + +ERROR_CODES = { + 'accountDoesNotExist': 'The request specified an account that does not exist', + 'alreadyRevoked': 'The request specified a certificate to be revoked that has' \ + ' already been revoked', + 'badCSR': 'The CSR is unacceptable (e.g., due to a short key)', + 'badNonce': 'The client sent an unacceptable anti-replay nonce', + 'badPublicKey': 'The JWS was signed by a public key the server does not support', + 'badRevocationReason': 'The revocation reason provided is not allowed by the server', + 'badSignatureAlgorithm': 'The JWS was signed with an algorithm the server does not support', + 'caa': 'Certification Authority Authorization (CAA) records forbid the CA from issuing' \ + ' a certificate', + 'compound': 'Specific error conditions are indicated in the "subproblems" array', + 'connection': ('The server could not connect to the client to verify the' + ' domain'), + 'dns': 'There was a problem with a DNS query during identifier validation', + 'dnssec': 'The server could not validate a DNSSEC signed domain', + 'incorrectResponse': 'Response received didn\'t match the challenge\'s requirements', + # deprecate invalidEmail + 'invalidEmail': 'The provided email for a registration was invalid', + 'invalidContact': 'The provided contact URI was invalid', + 'malformed': 'The request message was malformed', + 'rejectedIdentifier': 'The server will not issue certificates for the identifier', + 'orderNotReady': 'The request attempted to finalize an order that is not ready to be finalized', + 'rateLimited': 'There were too many requests of a given type', + 'serverInternal': 'The server experienced an internal error', + 'tls': 'The server experienced a TLS error during domain verification', + 'unauthorized': 'The client lacks sufficient authorization', + 'unsupportedContact': 'A contact URL for an account used an unsupported protocol scheme', + 'unknownHost': 'The server could not resolve a domain name', + 'unsupportedIdentifier': 'An identifier is of an unsupported type', + 'externalAccountRequired': 'The server requires external account binding', +} + +ERROR_TYPE_DESCRIPTIONS = {**{ + ERROR_PREFIX + name: desc for name, desc in ERROR_CODES.items() +}} + + +def is_acme_error(err: BaseException) -> bool: + """Check if argument is an ACME error.""" + if isinstance(err, Error) and (err.typ is not None): + return ERROR_PREFIX in err.typ + return False + + +class _Constant(jose.JSONDeSerializable, Hashable): + """ACME constant.""" + __slots__ = ('name',) + POSSIBLE_NAMES: dict[str, '_Constant'] = NotImplemented + + def __init__(self, name: str) -> None: + super().__init__() + self.POSSIBLE_NAMES[name] = self # pylint: disable=unsupported-assignment-operation + self.name = name + + def to_partial_json(self) -> str: + return self.name + + @classmethod + def from_json(cls, jobj: str) -> '_Constant': + if jobj not in cls.POSSIBLE_NAMES: # pylint: disable=unsupported-membership-test + raise jose.DeserializationError(f'{cls.__name__} not recognized') + return cls.POSSIBLE_NAMES[jobj] + + def __repr__(self) -> str: + return f'{self.__class__.__name__}({self.name})' + + def __eq__(self, other: Any) -> bool: + return isinstance(other, type(self)) and other.name == self.name + + def __hash__(self) -> int: + return hash((self.__class__, self.name)) + + +class IdentifierType(_Constant): + """ACME identifier type.""" + POSSIBLE_NAMES: dict[str, _Constant] = {} + + +IDENTIFIER_FQDN = IdentifierType('dns') # IdentifierDNS in Boulder +IDENTIFIER_IP = IdentifierType('ip') # IdentifierIP in pebble - not in Boulder yet + + +class Identifier(jose.JSONObjectWithFields): + """ACME identifier. + + :ivar IdentifierType typ: + :ivar str value: + + """ + typ: IdentifierType = jose.field('type', decoder=IdentifierType.from_json) + value: str = jose.field('value') + + +class Error(jose.JSONObjectWithFields, errors.Error): + """ACME error. + + https://datatracker.ietf.org/doc/html/rfc7807 + + Note: Although Error inherits from JSONObjectWithFields, which is immutable, + we add mutability for Error to comply with the Python exception API. + + :ivar str typ: + :ivar str title: + :ivar str detail: + :ivar Identifier identifier: + :ivar tuple subproblems: An array of ACME Errors which may be present when the CA + returns multiple errors related to the same request, `tuple` of `Error`. + + """ + typ: str = jose.field('type', omitempty=True, default='about:blank') + title: str = jose.field('title', omitempty=True) + detail: str = jose.field('detail', omitempty=True) + identifier: Optional['Identifier'] = jose.field( + 'identifier', decoder=Identifier.from_json, omitempty=True) + subproblems: Optional[tuple['Error', ...]] = jose.field('subproblems', omitempty=True) + + # Mypy does not understand the josepy magic happening here, and falsely claims + # that subproblems is redefined. Let's ignore the type check here. + @subproblems.decoder # type: ignore + def subproblems(value: list[dict[str, Any]]) -> tuple['Error', ...]: # pylint: disable=no-self-argument,missing-function-docstring + return tuple(Error.from_json(subproblem) for subproblem in value) + + @classmethod + def with_code(cls, code: str, **kwargs: Any) -> 'Error': + """Create an Error instance with an ACME Error code. + + :str code: An ACME error code, like 'dnssec'. + :kwargs: kwargs to pass to Error. + + """ + if code not in ERROR_CODES: + raise ValueError("The supplied code: %s is not a known ACME error" + " code" % code) + typ = ERROR_PREFIX + code + # Mypy will not understand that the Error constructor accepts a named argument + # "typ" because of josepy magic. Let's ignore the type check here. + return cls(typ=typ, **kwargs) + + @property + def description(self) -> Optional[str]: + """Hardcoded error description based on its type. + + :returns: Description if standard ACME error or ``None``. + :rtype: str + + """ + return ERROR_TYPE_DESCRIPTIONS.get(self.typ) + + @property + def code(self) -> Optional[str]: + """ACME error code. + + Basically self.typ without the ERROR_PREFIX. + + :returns: error code if standard ACME code or ``None``. + :rtype: str + + """ + code = str(self.typ).rsplit(':', maxsplit=1)[-1] + if code in ERROR_CODES: + return code + return None + + # Hack to allow mutability on Errors (see GH #9539) + def __setattr__(self, name: str, value: Any) -> None: + return object.__setattr__(self, name, value) + + def __str__(self) -> str: + result = b' :: '.join( + part.encode('ascii', 'backslashreplace') for part in + (self.typ, self.description, self.detail, self.title) + if part is not None).decode() + if self.identifier: + result = f'Problem for {self.identifier.value}: ' + result # pylint: disable=no-member + if self.subproblems and len(self.subproblems) > 0: + for subproblem in self.subproblems: + result += f'\n{subproblem}' + return result + + +class Status(_Constant): + """ACME "status" field.""" + POSSIBLE_NAMES: dict[str, _Constant] = {} + + +STATUS_UNKNOWN = Status('unknown') +STATUS_PENDING = Status('pending') +STATUS_PROCESSING = Status('processing') +STATUS_VALID = Status('valid') +STATUS_INVALID = Status('invalid') +STATUS_REVOKED = Status('revoked') +STATUS_READY = Status('ready') +STATUS_DEACTIVATED = Status('deactivated') + + +class Directory(jose.JSONDeSerializable): + """Directory. + + Directory resources must be accessed by the exact field name in RFC8555 (section 9.7.5). + """ + + class Meta(jose.JSONObjectWithFields): + """Directory Meta.""" + _terms_of_service: str = jose.field('termsOfService', omitempty=True) + website: str = jose.field('website', omitempty=True) + caa_identities: list[str] = jose.field('caaIdentities', omitempty=True) + external_account_required: bool = jose.field('externalAccountRequired', omitempty=True) + profiles: dict[str, str] = jose.field('profiles', omitempty=True) + + def __init__(self, **kwargs: Any) -> None: + kwargs = {self._internal_name(k): v for k, v in kwargs.items()} + super().__init__(**kwargs) + + @property + def terms_of_service(self) -> str: + """URL for the CA TOS""" + return self._terms_of_service + + def __iter__(self) -> Iterator[str]: + # When iterating over fields, use the external name 'terms_of_service' instead of + # the internal '_terms_of_service'. + for name in super().__iter__(): + yield name[1:] if name == '_terms_of_service' else name + + def _internal_name(self, name: str) -> str: + return '_' + name if name == 'terms_of_service' else name + + def __init__(self, jobj: Mapping[str, Any]) -> None: + self._jobj = jobj + + def __getattr__(self, name: str) -> Any: + try: + return self[name] + except KeyError as error: + raise AttributeError(str(error)) + + def __getitem__(self, name: str) -> Any: + try: + return self._jobj[name] + except KeyError: + raise KeyError(f'Directory field "{name}" not found') + + def to_partial_json(self) -> dict[str, Any]: + return util.map_keys(self._jobj, lambda k: k) + + @classmethod + def from_json(cls, jobj: MutableMapping[str, Any]) -> 'Directory': + jobj['meta'] = cls.Meta.from_json(jobj.pop('meta', {})) + return cls(jobj) + + +class Resource(jose.JSONObjectWithFields): + """ACME Resource. + + :ivar acme.messages.ResourceBody body: Resource body. + + """ + body: "ResourceBody" = jose.field('body') + + +class ResourceWithURI(Resource): + """ACME Resource with URI. + + :ivar str uri: Location of the resource. + + """ + uri: str = jose.field('uri') # no ChallengeResource.uri + + +class ResourceBody(jose.JSONObjectWithFields): + """ACME Resource Body.""" + + +class ExternalAccountBinding: + """ACME External Account Binding""" + + @classmethod + def from_data(cls, account_public_key: jose.JWK, kid: str, hmac_key: str, + directory: Directory, hmac_alg: str = "HS256") -> dict[str, Any]: + """Create External Account Binding Resource from contact details, kid and hmac.""" + + key_json = json.dumps(account_public_key.to_partial_json()).encode() + decoded_hmac_key = jose.b64.b64decode(hmac_key) + url = directory["newAccount"] + + hmac_alg_map = { + "HS256": jose.jwa.HS256, + "HS384": jose.jwa.HS384, + "HS512": jose.jwa.HS512, + } + alg = hmac_alg_map.get(hmac_alg) + if alg is None: + supported = ", ".join(hmac_alg_map.keys()) + raise ValueError(f"Invalid value for hmac_alg: {hmac_alg}. " + f"Expected one of: {supported}.") + + eab = jws.JWS.sign(key_json, jose.jwk.JWKOct(key=decoded_hmac_key), + alg, None, + url, kid) + + return eab.to_partial_json() + + +GenericRegistration = TypeVar('GenericRegistration', bound='Registration') + + +class Registration(ResourceBody): + """Registration Resource Body. + + :ivar jose.JWK key: Public key. + :ivar tuple contact: Contact information following ACME spec, + `tuple` of `str`. + :ivar str agreement: + + """ + # on new-reg key server ignores 'key' and populates it based on + # JWS.signature.combined.jwk + key: jose.JWK = jose.field('key', omitempty=True, decoder=jose.JWK.from_json) + # Contact field implements special behavior to allow messages that clear existing + # contacts while not expecting the `contact` field when loading from json. + # This is implemented in the constructor and *_json methods. + contact: tuple[str, ...] = jose.field('contact', omitempty=True, default=()) + agreement: str = jose.field('agreement', omitempty=True) + status: Status = jose.field('status', omitempty=True) + terms_of_service_agreed: bool = jose.field('termsOfServiceAgreed', omitempty=True) + only_return_existing: bool = jose.field('onlyReturnExisting', omitempty=True) + external_account_binding: dict[str, Any] = jose.field('externalAccountBinding', + omitempty=True) + + phone_prefix = 'tel:' + email_prefix = 'mailto:' + + @classmethod + def from_data(cls: type[GenericRegistration], phone: Optional[str] = None, + email: Optional[str] = None, + external_account_binding: Optional[dict[str, Any]] = None, + **kwargs: Any) -> GenericRegistration: + """ + Create registration resource from contact details. + + The `contact` keyword being passed to a Registration object is meaningful, so + this function represents empty iterables in its kwargs by passing on an empty + `tuple`. + """ + + # Note if `contact` was in kwargs. + contact_provided = 'contact' in kwargs + + # Pop `contact` from kwargs and add formatted email or phone numbers + details = list(kwargs.pop('contact', ())) + if phone is not None: + details.append(cls.phone_prefix + phone) + if email is not None: + details.extend([cls.email_prefix + mail for mail in email.split(',')]) + + # Insert formatted contact information back into kwargs + # or insert an empty tuple if `contact` provided. + if details or contact_provided: + kwargs['contact'] = tuple(details) + + if external_account_binding: + kwargs['external_account_binding'] = external_account_binding + + return cls(**kwargs) + + def __init__(self, **kwargs: Any) -> None: + """Note if the user provides a value for the `contact` member.""" + if 'contact' in kwargs and kwargs['contact'] is not None: + # Avoid the __setattr__ used by jose.TypedJSONObjectWithFields + object.__setattr__(self, '_add_contact', True) + super().__init__(**kwargs) + + def _filter_contact(self, prefix: str) -> tuple[str, ...]: + return tuple( + detail[len(prefix):] for detail in self.contact # pylint: disable=not-an-iterable + if detail.startswith(prefix)) + + def _add_contact_if_appropriate(self, jobj: dict[str, Any]) -> dict[str, Any]: + """ + The `contact` member of Registration objects should not be required when + de-serializing (as it would be if the Fields' `omitempty` flag were `False`), but + it should be included in serializations if it was provided. + + :param jobj: Dictionary containing this Registrations' data + :type jobj: dict + + :returns: Dictionary containing Registrations data to transmit to the server + :rtype: dict + """ + if getattr(self, '_add_contact', False): + jobj['contact'] = self.encode('contact') + + return jobj + + def to_partial_json(self) -> dict[str, Any]: + """Modify josepy.JSONDeserializable.to_partial_json()""" + jobj = super().to_partial_json() + return self._add_contact_if_appropriate(jobj) + + def fields_to_partial_json(self) -> dict[str, Any]: + """Modify josepy.JSONObjectWithFields.fields_to_partial_json()""" + jobj = super().fields_to_partial_json() + return self._add_contact_if_appropriate(jobj) + + @property + def phones(self) -> tuple[str, ...]: + """All phones found in the ``contact`` field.""" + return self._filter_contact(self.phone_prefix) + + @property + def emails(self) -> tuple[str, ...]: + """All emails found in the ``contact`` field.""" + return self._filter_contact(self.email_prefix) + + +class NewRegistration(Registration): + """New registration.""" + + +class UpdateRegistration(Registration): + """Update registration.""" + + +class RegistrationResource(ResourceWithURI): + """Registration Resource. + + :ivar acme.messages.Registration body: + :ivar str new_authzr_uri: Deprecated. Do not use. + :ivar str terms_of_service: URL for the CA TOS. + + """ + body: Registration = jose.field('body', decoder=Registration.from_json) + new_authzr_uri: str = jose.field('new_authzr_uri', omitempty=True) + terms_of_service: str = jose.field('terms_of_service', omitempty=True) + + +class ChallengeBody(ResourceBody): + """Challenge Resource Body. + + .. todo:: + Confusingly, this has a similar name to `.challenges.Challenge`, + as well as `.achallenges.AnnotatedChallenge`. Please use names + such as ``challb`` to distinguish instances of this class from + ``achall``. + + :ivar acme.challenges.Challenge: Wrapped challenge. + Conveniently, all challenge fields are proxied, i.e. you can + call ``challb.x`` to get ``challb.chall.x`` contents. + :ivar acme.messages.Status status: + :ivar datetime.datetime validated: + :ivar messages.Error error: + + """ + __slots__ = ('chall',) + # ACMEv1 has a "uri" field in challenges. ACMEv2 has a "url" field. This + # challenge object supports either one, but should be accessed through the + # name "uri". In Client.answer_challenge, whichever one is set will be + # used. + _url: str = jose.field('url', omitempty=True, default=None) + status: Status = jose.field('status', decoder=Status.from_json, + omitempty=True, default=STATUS_PENDING) + validated: datetime.datetime = fields.rfc3339('validated', omitempty=True) + error: Error = jose.field('error', decoder=Error.from_json, + omitempty=True, default=None) + + def __init__(self, **kwargs: Any) -> None: + kwargs = {self._internal_name(k): v for k, v in kwargs.items()} + super().__init__(**kwargs) + + def encode(self, name: str) -> Any: + return super().encode(self._internal_name(name)) + + def to_partial_json(self) -> dict[str, Any]: + jobj = super().to_partial_json() + jobj.update(self.chall.to_partial_json()) + return jobj + + @classmethod + def fields_from_json(cls, jobj: Mapping[str, Any]) -> dict[str, Any]: + jobj_fields = super().fields_from_json(jobj) + jobj_fields['chall'] = challenges.Challenge.from_json(jobj) + return jobj_fields + + @property + def uri(self) -> str: + """The URL of this challenge.""" + return self._url + + def __getattr__(self, name: str) -> Any: + return getattr(self.chall, name) + + def __iter__(self) -> Iterator[str]: + # When iterating over fields, use the external name 'uri' instead of + # the internal '_uri'. + for name in super().__iter__(): + yield 'uri' if name == '_url' else name + + def _internal_name(self, name: str) -> str: + return '_url' if name == 'uri' else name + + +class ChallengeResource(Resource): + """Challenge Resource. + + :ivar acme.messages.ChallengeBody body: + :ivar str authzr_uri: URI found in the 'up' ``Link`` header. + + """ + body: ChallengeBody = jose.field('body', decoder=ChallengeBody.from_json) + authzr_uri: str = jose.field('authzr_uri') + + @property + def uri(self) -> str: + """The URL of the challenge body.""" + return self.body.uri # pylint: disable=no-member + + +class Authorization(ResourceBody): + """Authorization Resource Body. + + :ivar acme.messages.Identifier identifier: + :ivar list challenges: `list` of `.ChallengeBody` + :ivar acme.messages.Status status: + :ivar datetime.datetime expires: + + """ + identifier: Identifier = jose.field('identifier', decoder=Identifier.from_json, omitempty=True) + challenges: list[ChallengeBody] = jose.field('challenges', omitempty=True) + + status: Status = jose.field('status', omitempty=True, decoder=Status.from_json) + # TODO: 'expires' is allowed for Authorization Resources in + # general, but for Key Authorization '[t]he "expires" field MUST + # be absent'... then acme-spec gives example with 'expires' + # present... That's confusing! + expires: datetime.datetime = fields.rfc3339('expires', omitempty=True) + wildcard: bool = jose.field('wildcard', omitempty=True) + + # Mypy does not understand the josepy magic happening here, and falsely claims + # that challenge is redefined. Let's ignore the type check here. + @challenges.decoder # type: ignore + def challenges(value: list[dict[str, Any]]) -> tuple[ChallengeBody, ...]: # pylint: disable=no-self-argument,missing-function-docstring + return tuple(ChallengeBody.from_json(chall) for chall in value) + + +class NewAuthorization(Authorization): + """New authorization.""" + + +class UpdateAuthorization(Authorization): + """Update authorization.""" + + +class AuthorizationResource(ResourceWithURI): + """Authorization Resource. + + :ivar acme.messages.Authorization body: + :ivar str new_cert_uri: Deprecated. Do not use. + + """ + body: Authorization = jose.field('body', decoder=Authorization.from_json) + new_cert_uri: str = jose.field('new_cert_uri', omitempty=True) + + +class CertificateRequest(jose.JSONObjectWithFields): + """ACME newOrder request. + + :ivar x509.CertificateSigningRequest csr: `x509.CertificateSigningRequest` + + """ + csr: x509.CertificateSigningRequest = jose.field( + 'csr', decoder=jose.decode_csr, encoder=jose.encode_csr) + + +class CertificateResource(ResourceWithURI): + """Certificate Resource. + + :ivar x509.Certificate body: `x509.Certificate` + :ivar str cert_chain_uri: URI found in the 'up' ``Link`` header + :ivar tuple authzrs: `tuple` of `AuthorizationResource`. + + """ + cert_chain_uri: str = jose.field('cert_chain_uri') + authzrs: tuple[AuthorizationResource, ...] = jose.field('authzrs') + + +class Revocation(jose.JSONObjectWithFields): + """Revocation message. + + :ivar x509.Certificate certificate: `x509.Certificate` + + """ + certificate: x509.Certificate = jose.field( + 'certificate', decoder=jose.decode_cert, encoder=jose.encode_cert) + reason: int = jose.field('reason') + + +class Order(ResourceBody): + """Order Resource Body. + + :ivar profile: The profile to request. + :vartype profile: str + :ivar identifiers: List of identifiers for the certificate. + :vartype identifiers: `list` of `.Identifier` + :ivar acme.messages.Status status: + :ivar authorizations: URLs of authorizations. + :vartype authorizations: `list` of `str` + :ivar str certificate: URL to download certificate as a fullchain PEM. + :ivar str finalize: URL to POST to to request issuance once all + authorizations have "valid" status. + :ivar datetime.datetime expires: When the order expires. + :ivar ~.Error error: Any error that occurred during finalization, if applicable. + """ + # https://datatracker.ietf.org/doc/draft-aaron-acme-profiles/ + profile: str = jose.field('profile', omitempty=True) + identifiers: list[Identifier] = jose.field('identifiers', omitempty=True) + status: Status = jose.field('status', decoder=Status.from_json, omitempty=True) + authorizations: list[str] = jose.field('authorizations', omitempty=True) + certificate: str = jose.field('certificate', omitempty=True) + finalize: str = jose.field('finalize', omitempty=True) + expires: datetime.datetime = fields.rfc3339('expires', omitempty=True) + error: Error = jose.field('error', omitempty=True, decoder=Error.from_json) + + # Mypy does not understand the josepy magic happening here, and falsely claims + # that identifiers is redefined. Let's ignore the type check here. + @identifiers.decoder # type: ignore + def identifiers(value: list[dict[str, Any]]) -> tuple[Identifier, ...]: # pylint: disable=no-self-argument,missing-function-docstring + return tuple(Identifier.from_json(identifier) for identifier in value) + + +class OrderResource(ResourceWithURI): + """Order Resource. + + :ivar acme.messages.Order body: + :ivar bytes csr_pem: The CSR this Order will be finalized with. + :ivar authorizations: Fully-fetched AuthorizationResource objects. + :vartype authorizations: `list` of `acme.messages.AuthorizationResource` + :ivar str fullchain_pem: The fetched contents of the certificate URL + produced once the order was finalized, if it's present. + :ivar alternative_fullchains_pem: The fetched contents of alternative certificate + chain URLs produced once the order was finalized, if present and requested during + finalization. + :vartype alternative_fullchains_pem: `list` of `str` + """ + body: Order = jose.field('body', decoder=Order.from_json) + csr_pem: bytes = jose.field('csr_pem', omitempty=True, + # This looks backwards, but it's not - + # we want the deserialized value to be + # `bytes`, but anything we put into + # JSON needs to be `str`, so we encode + # to decode and decode to + # encode. Otherwise we end up with an + # array of ints on serialization + decoder=lambda s: s.encode("utf-8"), + encoder=lambda b: b.decode("utf-8")) + + authorizations: list[AuthorizationResource] = jose.field('authorizations') + fullchain_pem: str = jose.field('fullchain_pem', omitempty=True) + alternative_fullchains_pem: list[str] = jose.field('alternative_fullchains_pem', + omitempty=True) + + # Mypy does not understand the josepy magic happening here, and falsely claims + # that authorizations is redefined. Let's ignore the type check here. + @authorizations.decoder # type: ignore + def authorizations(value: list[dict[str, Any]]) -> tuple[AuthorizationResource, ...]: # pylint: disable=no-self-argument,missing-function-docstring + return tuple(AuthorizationResource.from_json(authz) for authz in value) + + +class NewOrder(Order): + """New order.""" + + +class RenewalInfo(ResourceBody): + """Renewal Info Resource Body. + :ivar acme.messages.SuggestedWindow window: The suggested renewal window. + """ + class SuggestedWindow(jose.JSONObjectWithFields): + """Suggested Renewal Window, sub-resource of Renewal Info Resource. + :ivar datetime.datetime start: Beginning of suggested renewal window + :ivar datetime.datetime end: End of suggested renewal window (inclusive) + """ + start: datetime.datetime = fields.rfc3339('start', omitempty=True) + end: datetime.datetime = fields.rfc3339('end', omitempty=True) + + suggested_window: SuggestedWindow = jose.field('suggestedWindow', + decoder=SuggestedWindow.from_json) diff --git a/certbot-apache/docs/_static/.gitignore b/acme/src/acme/py.typed similarity index 100% rename from certbot-apache/docs/_static/.gitignore rename to acme/src/acme/py.typed diff --git a/acme/src/acme/standalone.py b/acme/src/acme/standalone.py new file mode 100644 index 00000000000..b807b763ac7 --- /dev/null +++ b/acme/src/acme/standalone.py @@ -0,0 +1,230 @@ +"""Support for standalone client challenge solvers. """ +from __future__ import annotations + +import collections +import functools +import http.client as http_client +import http.server as BaseHTTPServer +import logging +import socket +import socketserver +import threading +from typing import Any +from typing import Optional + +from acme import challenges + +logger = logging.getLogger(__name__) + + +class ACMEServerMixin: + """ACME server common settings mixin.""" + # TODO: c.f. #858 + server_version = "ACME client standalone challenge solver" + allow_reuse_address = True + + +class BaseDualNetworkedServers: + """Base class for a pair of IPv6 and IPv4 servers that tries to do everything + it's asked for both servers, but where failures in one server don't + affect the other. + + If two servers are instantiated, they will serve on the same port. + """ + + def __init__(self, ServerClass: type[socketserver.TCPServer], server_address: tuple[str, int], + *remaining_args: Any, **kwargs: Any) -> None: + port = server_address[1] + self.threads: list[threading.Thread] = [] + self.servers: list[socketserver.BaseServer] = [] + + # Preserve socket error for re-raising, if no servers can be started + last_socket_err: Optional[socket.error] = None + + # Must try True first. + # Ubuntu, for example, will fail to bind to IPv4 if we've already bound + # to IPv6. But that's ok, since it will accept IPv4 connections on the IPv6 + # socket. On the other hand, FreeBSD will successfully bind to IPv4 on the + # same port, which means that server will accept the IPv4 connections. + # If Python is compiled without IPv6, we'll error out but (probably) successfully + # create the IPv4 server. + for ip_version in [True, False]: + try: + kwargs["ipv6"] = ip_version + new_address = (server_address[0],) + (port,) + server_address[2:] + new_args = (new_address,) + remaining_args + server = ServerClass(*new_args, **kwargs) + logger.debug( + "Successfully bound to %s:%s using %s", new_address[0], + new_address[1], "IPv6" if ip_version else "IPv4") + except OSError as e: + last_socket_err = e + if self.servers: + # Already bound using IPv6. + logger.debug( + "Certbot wasn't able to bind to %s:%s using %s, this " + "is often expected due to the dual stack nature of " + "IPv6 socket implementations.", + new_address[0], new_address[1], + "IPv6" if ip_version else "IPv4") + else: + logger.debug( + "Failed to bind to %s:%s using %s", new_address[0], + new_address[1], "IPv6" if ip_version else "IPv4") + else: + self.servers.append(server) + # If two servers are set up and port 0 was passed in, ensure we always + # bind to the same port for both servers. + port = server.socket.getsockname()[1] + if not self.servers: + if last_socket_err: + raise last_socket_err + else: # pragma: no cover + raise OSError("Could not bind to IPv4 or IPv6.") + + def serve_forever(self) -> None: + """Wraps socketserver.TCPServer.serve_forever""" + for server in self.servers: + thread = threading.Thread( + target=server.serve_forever) + thread.start() + self.threads.append(thread) + + def getsocknames(self) -> list[tuple[str, int]]: + """Wraps socketserver.TCPServer.socket.getsockname""" + return [server.socket.getsockname() for server in self.servers] + + def shutdown_and_server_close(self) -> None: + """Wraps socketserver.TCPServer.shutdown, socketserver.TCPServer.server_close, and + threading.Thread.join""" + for server in self.servers: + server.shutdown() + server.server_close() + for thread in self.threads: + thread.join() + self.threads = [] + + +class HTTPServer(BaseHTTPServer.HTTPServer): + """Generic HTTP Server.""" + + def __init__(self, *args: Any, **kwargs: Any) -> None: + self.ipv6 = kwargs.pop("ipv6", False) + if self.ipv6: + self.address_family = socket.AF_INET6 + else: + self.address_family = socket.AF_INET + super().__init__(*args, **kwargs) + + +class HTTP01Server(HTTPServer, ACMEServerMixin): + """HTTP01 Server.""" + + def __init__(self, server_address: tuple[str, int], + resources: set[HTTP01RequestHandler.HTTP01Resource], + ipv6: bool = False, timeout: int = 30) -> None: + super().__init__( + server_address, HTTP01RequestHandler.partial_init( + simple_http_resources=resources, timeout=timeout), ipv6=ipv6) + + +class HTTP01DualNetworkedServers(BaseDualNetworkedServers): + """HTTP01Server Wrapper. Tries everything for both. Failures for one don't + affect the other.""" + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(HTTP01Server, *args, **kwargs) + + +class HTTP01RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): + """HTTP01 challenge handler. + + Adheres to the stdlib's `socketserver.BaseRequestHandler` interface. + + :ivar set simple_http_resources: A set of `HTTP01Resource` + objects. TODO: better name? + + """ + HTTP01Resource = collections.namedtuple( + "HTTP01Resource", "chall response validation") + + def __init__(self, *args: Any, **kwargs: Any) -> None: + self.simple_http_resources = kwargs.pop("simple_http_resources", set()) + self._timeout = kwargs.pop('timeout', 30) + super().__init__(*args, **kwargs) + self.server: HTTP01Server + + # In parent class BaseHTTPRequestHandler, 'timeout' is a class-level property but we + # need to define its value during the initialization phase in HTTP01RequestHandler. + # However MyPy does not appreciate that we dynamically shadow a class-level property + # with an instance-level property (eg. self.timeout = ... in __init__()). So to make + # everyone happy, we statically redefine 'timeout' as a method property, and set the + # timeout value in a new internal instance-level property _timeout. + @property + def timeout(self) -> int: # type: ignore[override] + """ + The default timeout this server should apply to requests. + :return: timeout to apply + :rtype: int + """ + return self._timeout + + def log_message(self, format: str, *args: Any) -> None: # pylint: disable=redefined-builtin + """Log arbitrary message.""" + logger.debug("%s - - %s", self.client_address[0], format % args) + + def handle(self) -> None: + """Handle request.""" + self.log_message("Incoming request") + BaseHTTPServer.BaseHTTPRequestHandler.handle(self) + + def do_GET(self) -> None: # pylint: disable=invalid-name,missing-function-docstring + if self.path == "/": + self.handle_index() + elif self.path.startswith("/" + challenges.HTTP01.URI_ROOT_PATH): + self.handle_simple_http_resource() + else: + self.handle_404() + + def handle_index(self) -> None: + """Handle index page.""" + self.send_response(200) + self.send_header("Content-Type", "text/html") + self.end_headers() + self.wfile.write(self.server.server_version.encode()) + + def handle_404(self) -> None: + """Handler 404 Not Found errors.""" + self.send_response(http_client.NOT_FOUND, message="Not Found") + self.send_header("Content-type", "text/html") + self.end_headers() + self.wfile.write(b"404") + + def handle_simple_http_resource(self) -> None: + """Handle HTTP01 provisioned resources.""" + for resource in self.simple_http_resources: + if resource.chall.path == self.path: + self.log_message("Serving HTTP01 with token %r", + resource.chall.encode("token")) + self.send_response(http_client.OK) + self.end_headers() + self.wfile.write(resource.validation.encode()) + return + else: # pylint: disable=useless-else-on-loop + self.log_message("No resources to serve") + self.log_message("%s does not correspond to any resource. ignoring", + self.path) + + @classmethod + def partial_init(cls, simple_http_resources: set[HTTP01RequestHandler.HTTP01Resource], + timeout: int) -> functools.partial[HTTP01RequestHandler]: + """Partially initialize this handler. + + This is useful because `socketserver.BaseServer` takes + uninitialized handler and initializes it with the current + request. + + """ + return functools.partial( + cls, simple_http_resources=simple_http_resources, + timeout=timeout) diff --git a/acme/src/acme/util.py b/acme/src/acme/util.py new file mode 100644 index 00000000000..d38533c4414 --- /dev/null +++ b/acme/src/acme/util.py @@ -0,0 +1,9 @@ +"""ACME utilities.""" +from typing import Any +from typing import Callable +from typing import Mapping + + +def map_keys(dikt: Mapping[Any, Any], func: Callable[[Any], Any]) -> dict[Any, Any]: + """Map dictionary keys.""" + return {func(key): value for key, value in dikt.items()} diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 2b6b82747a6..00000000000 --- a/appveyor.yml +++ /dev/null @@ -1,32 +0,0 @@ -image: Visual Studio 2015 - -environment: - matrix: - - TOXENV: py35 - - TOXENV: py37-cover - -branches: - only: - - master - - /^\d+\.\d+\.x$/ # Version branches like X.X.X - - /^test-.*$/ - -install: - # Use Python 3.7 by default - - "SET PATH=C:\\Python37;C:\\Python37\\Scripts;%PATH%" - # Check env - - "python --version" - # Upgrade pip to avoid warnings - - "python -m pip install --upgrade pip" - # Ready to install tox and coverage - - "pip install tox codecov" - -build: off - -test_script: - - set TOX_TESTENV_PASSENV=APPVEYOR - # Test env is set by TOXENV env variable - - tox - -on_success: - - if exist .coverage codecov diff --git a/certbot-apache/MANIFEST.in b/certbot-apache/MANIFEST.in index 3e594a9531a..0cbb1083da0 100644 --- a/certbot-apache/MANIFEST.in +++ b/certbot-apache/MANIFEST.in @@ -1,7 +1,5 @@ include LICENSE.txt include README.rst -recursive-include docs * -recursive-include certbot_apache/tests/testdata * -include certbot_apache/centos-options-ssl-apache.conf -include certbot_apache/options-ssl-apache.conf -recursive-include certbot_apache/augeas_lens *.aug +include src/certbot_apache/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-apache/certbot_apache/apache_util.py b/certbot-apache/certbot_apache/apache_util.py deleted file mode 100644 index 62342004fc8..00000000000 --- a/certbot-apache/certbot_apache/apache_util.py +++ /dev/null @@ -1,106 +0,0 @@ -""" Utility functions for certbot-apache plugin """ -import binascii -import os - -from certbot import util - -def get_mod_deps(mod_name): - """Get known module dependencies. - - .. note:: This does not need to be accurate in order for the client to - run. This simply keeps things clean if the user decides to revert - changes. - .. warning:: If all deps are not included, it may cause incorrect parsing - behavior, due to enable_mod's shortcut for updating the parser's - currently defined modules (`.ApacheParser.add_mod`) - This would only present a major problem in extremely atypical - configs that use ifmod for the missing deps. - - """ - deps = { - "ssl": ["setenvif", "mime"] - } - return deps.get(mod_name, []) - - -def get_file_path(vhost_path): - """Get file path from augeas_vhost_path. - - Takes in Augeas path and returns the file name - - :param str vhost_path: Augeas virtual host path - - :returns: filename of vhost - :rtype: str - - """ - if not vhost_path or not vhost_path.startswith("/files/"): - return None - - return _split_aug_path(vhost_path)[0] - - -def get_internal_aug_path(vhost_path): - """Get the Augeas path for a vhost with the file path removed. - - :param str vhost_path: Augeas virtual host path - - :returns: Augeas path to vhost relative to the containing file - :rtype: str - - """ - return _split_aug_path(vhost_path)[1] - - -def _split_aug_path(vhost_path): - """Splits an Augeas path into a file path and an internal path. - - After removing "/files", this function splits vhost_path into the - file path and the remaining Augeas path. - - :param str vhost_path: Augeas virtual host path - - :returns: file path and internal Augeas path - :rtype: `tuple` of `str` - - """ - # Strip off /files - file_path = vhost_path[6:] - internal_path = [] - - # Remove components from the end of file_path until it becomes valid - while not os.path.exists(file_path): - file_path, _, internal_path_part = file_path.rpartition("/") - internal_path.append(internal_path_part) - - return file_path, "/".join(reversed(internal_path)) - - -def parse_define_file(filepath, varname): - """ Parses Defines from a variable in configuration file - - :param str filepath: Path of file to parse - :param str varname: Name of the variable - - :returns: Dict of Define:Value pairs - :rtype: `dict` - - """ - return_vars = {} - # Get list of words in the variable - a_opts = util.get_var_from_file(varname, filepath).split() - for i, v in enumerate(a_opts): - # Handle Define statements and make sure it has an argument - if v == "-D" and len(a_opts) >= i+2: - var_parts = a_opts[i+1].partition("=") - return_vars[var_parts[0]] = var_parts[2] - elif len(v) > 2 and v.startswith("-D"): - # Found var with no whitespace separator - var_parts = v[2:].partition("=") - return_vars[var_parts[0]] = var_parts[2] - return return_vars - - -def unique_id(): - """ Returns an unique id to be used as a VirtualHost identifier""" - return binascii.hexlify(os.urandom(16)).decode("utf-8") diff --git a/certbot-apache/certbot_apache/augeas_configurator.py b/certbot-apache/certbot_apache/augeas_configurator.py deleted file mode 100644 index a32c65c4100..00000000000 --- a/certbot-apache/certbot_apache/augeas_configurator.py +++ /dev/null @@ -1,207 +0,0 @@ -"""Class of Augeas Configurators.""" -import logging - - -from certbot import errors -from certbot.plugins import common - -from certbot_apache import constants - -logger = logging.getLogger(__name__) - - -class AugeasConfigurator(common.Installer): - """Base Augeas Configurator class. - - :ivar config: Configuration. - :type config: :class:`~certbot.interfaces.IConfig` - - :ivar aug: Augeas object - :type aug: :class:`augeas.Augeas` - - :ivar str save_notes: Human-readable configuration change notes - :ivar reverter: saves and reverts checkpoints - :type reverter: :class:`certbot.reverter.Reverter` - - """ - def __init__(self, *args, **kwargs): - super(AugeasConfigurator, self).__init__(*args, **kwargs) - - # Placeholder for augeas - self.aug = None - - self.save_notes = "" - - - def init_augeas(self): - """ Initialize the actual Augeas instance """ - import augeas - self.aug = augeas.Augeas( - # specify a directory to load our preferred lens from - loadpath=constants.AUGEAS_LENS_DIR, - # Do not save backup (we do it ourselves), do not load - # anything by default - flags=(augeas.Augeas.NONE | - augeas.Augeas.NO_MODL_AUTOLOAD | - augeas.Augeas.ENABLE_SPAN)) - # See if any temporary changes need to be recovered - # This needs to occur before VirtualHost objects are setup... - # because this will change the underlying configuration and potential - # vhosts - self.recovery_routine() - - def check_parsing_errors(self, lens): - """Verify Augeas can parse all of the lens files. - - :param str lens: lens to check for errors - - :raises .errors.PluginError: If there has been an error in parsing with - the specified lens. - - """ - error_files = self.aug.match("/augeas//error") - - for path in error_files: - # Check to see if it was an error resulting from the use of - # the httpd lens - lens_path = self.aug.get(path + "/lens") - # As aug.get may return null - if lens_path and lens in lens_path: - msg = ( - "There has been an error in parsing the file {0} on line {1}: " - "{2}".format( - # Strip off /augeas/files and /error - path[13:len(path) - 6], - self.aug.get(path + "/line"), - self.aug.get(path + "/message"))) - raise errors.PluginError(msg) - - def ensure_augeas_state(self): - """Makes sure that all Augeas dom changes are written to files to avoid - loss of configuration directives when doing additional augeas parsing, - causing a possible augeas.load() resulting dom reset - """ - - if self.unsaved_files(): - self.save_notes += "(autosave)" - self.save() - - def unsaved_files(self): - """Lists files that have modified Augeas DOM but the changes have not - been written to the filesystem yet, used by `self.save()` and - ApacheConfigurator to check the file state. - - :raises .errors.PluginError: If there was an error in Augeas, in - an attempt to save the configuration, or an error creating a - checkpoint - - :returns: `set` of unsaved files - """ - save_state = self.aug.get("/augeas/save") - self.aug.set("/augeas/save", "noop") - # Existing Errors - ex_errs = self.aug.match("/augeas//error") - try: - # This is a noop save - self.aug.save() - except (RuntimeError, IOError): - self._log_save_errors(ex_errs) - # Erase Save Notes - self.save_notes = "" - raise errors.PluginError( - "Error saving files, check logs for more info.") - - # Return the original save method - self.aug.set("/augeas/save", save_state) - - # Retrieve list of modified files - # Note: Noop saves can cause the file to be listed twice, I used a - # set to remove this possibility. This is a known augeas 0.10 error. - save_paths = self.aug.match("/augeas/events/saved") - - save_files = set() - if save_paths: - for path in save_paths: - save_files.add(self.aug.get(path)[6:]) - return save_files - - def save(self, title=None, temporary=False): - """Saves all changes to the configuration files. - - This function first checks for save errors, if none are found, - all configuration changes made will be saved. According to the - function parameters. If an exception is raised, a new checkpoint - was not created. - - :param str title: The title of the save. If a title is given, the - configuration will be saved as a new checkpoint and put in a - timestamped directory. - - :param bool temporary: Indicates whether the changes made will - be quickly reversed in the future (ie. challenges) - - """ - save_files = self.unsaved_files() - if save_files: - self.add_to_checkpoint(save_files, - self.save_notes, temporary=temporary) - - self.save_notes = "" - self.aug.save() - - # Force reload if files were modified - # This is needed to recalculate augeas directive span - if save_files: - for sf in save_files: - self.aug.remove("/files/"+sf) - self.aug.load() - if title and not temporary: - self.finalize_checkpoint(title) - - def _log_save_errors(self, ex_errs): - """Log errors due to bad Augeas save. - - :param list ex_errs: Existing errors before save - - """ - # Check for the root of save problems - new_errs = self.aug.match("/augeas//error") - # logger.error("During Save - %s", mod_conf) - logger.error("Unable to save files: %s. Attempted Save Notes: %s", - ", ".join(err[13:len(err) - 6] for err in new_errs - # Only new errors caused by recent save - if err not in ex_errs), self.save_notes) - - # Wrapper functions for Reverter class - def recovery_routine(self): - """Revert all previously modified files. - - Reverts all modified files that have not been saved as a checkpoint - - :raises .errors.PluginError: If unable to recover the configuration - - """ - super(AugeasConfigurator, self).recovery_routine() - # Need to reload configuration after these changes take effect - self.aug.load() - - def revert_challenge_config(self): - """Used to cleanup challenge configurations. - - :raises .errors.PluginError: If unable to revert the challenge config. - - """ - self.revert_temporary_config() - self.aug.load() - - def rollback_checkpoints(self, rollback=1): - """Rollback saved checkpoints. - - :param int rollback: Number of checkpoints to revert - - :raises .errors.PluginError: If there is a problem with the input or - the function is unable to correctly revert the configuration - - """ - super(AugeasConfigurator, self).rollback_checkpoints(rollback) - self.aug.load() diff --git a/certbot-apache/certbot_apache/centos-options-ssl-apache.conf b/certbot-apache/certbot_apache/centos-options-ssl-apache.conf deleted file mode 100644 index 56c946a4e21..00000000000 --- a/certbot-apache/certbot_apache/centos-options-ssl-apache.conf +++ /dev/null @@ -1,25 +0,0 @@ -# This file contains important security parameters. If you modify this file -# manually, Certbot will be unable to automatically provide future security -# updates. Instead, Certbot will print and log an error message with a path to -# the up-to-date file that you will need to refer to when manually updating -# this file. - -SSLEngine on - -# Intermediate configuration, tweak to your needs -SSLProtocol all -SSLv2 -SSLv3 -SSLCipherSuite ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS -SSLHonorCipherOrder on - -SSLOptions +StrictRequire - -# Add vhost name to log entries: -LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined -LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common - -#CustomLog /var/log/apache2/access.log vhost_combined -#LogLevel warn -#ErrorLog /var/log/apache2/error.log - -# Always ensure Cookies have "Secure" set (JAH 2012/1) -#Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4" diff --git a/certbot-apache/certbot_apache/configurator.py b/certbot-apache/certbot_apache/configurator.py deleted file mode 100644 index 16de3a3d86d..00000000000 --- a/certbot-apache/certbot_apache/configurator.py +++ /dev/null @@ -1,2498 +0,0 @@ -"""Apache Configuration based off of Augeas Configurator.""" -# pylint: disable=too-many-lines -import copy -import fnmatch -import logging -import os -import pkg_resources -import re -import six -import socket -import time - -import zope.component -import zope.interface - -from acme import challenges -from acme.magic_typing import Any, DefaultDict, Dict, List, Set, Union # pylint: disable=unused-import, no-name-in-module - -from certbot import errors -from certbot import interfaces -from certbot import util - -from certbot.achallenges import KeyAuthorizationAnnotatedChallenge # pylint: disable=unused-import -from certbot.plugins import common -from certbot.plugins.util import path_surgery -from certbot.plugins.enhancements import AutoHSTSEnhancement - -from certbot_apache import apache_util -from certbot_apache import augeas_configurator -from certbot_apache import constants -from certbot_apache import display_ops -from certbot_apache import http_01 -from certbot_apache import obj -from certbot_apache import parser -from certbot_apache import tls_sni_01 - -from collections import defaultdict - -logger = logging.getLogger(__name__) - - -# TODO: Augeas sections ie. , beginning and closing -# tags need to be the same case, otherwise Augeas doesn't recognize them. -# This is not able to be completely remedied by regular expressions because -# Augeas views as an error. This will just -# require another check_parsing_errors() after all files are included... -# (after a find_directive search is executed currently). It can be a one -# time check however because all of LE's transactions will ensure -# only properly formed sections are added. - -# Note: This protocol works for filenames with spaces in it, the sites are -# properly set up and directives are changed appropriately, but Apache won't -# recognize names in sites-enabled that have spaces. These are not added to the -# Apache configuration. It may be wise to warn the user if they are trying -# to use vhost filenames that contain spaces and offer to change ' ' to '_' - -# Note: FILEPATHS and changes to files are transactional. They are copied -# over before the updates are made to the existing files. NEW_FILES is -# transactional due to the use of register_file_creation() - - -# TODO: Verify permissions on configuration root... it is easier than -# checking permissions on each of the relative directories and less error -# prone. -# TODO: Write a server protocol finder. Listen or -# Protocol . This can verify partial setups are correct -# TODO: Add directives to sites-enabled... not sites-available. -# sites-available doesn't allow immediate find_dir search even with save() -# and load() - -@zope.interface.implementer(interfaces.IAuthenticator, interfaces.IInstaller) -@zope.interface.provider(interfaces.IPluginFactory) -class ApacheConfigurator(augeas_configurator.AugeasConfigurator): - # pylint: disable=too-many-instance-attributes,too-many-public-methods - """Apache configurator. - - State of Configurator: This code has been been tested and built for Ubuntu - 14.04 Apache 2.4 and it works for Ubuntu 12.04 Apache 2.2 - - :ivar config: Configuration. - :type config: :class:`~certbot.interfaces.IConfig` - - :ivar parser: Handles low level parsing - :type parser: :class:`~certbot_apache.parser` - - :ivar tup version: version of Apache - :ivar list vhosts: All vhosts found in the configuration - (:class:`list` of :class:`~certbot_apache.obj.VirtualHost`) - - :ivar dict assoc: Mapping between domains and vhosts - - """ - - description = "Apache Web Server plugin" - - OS_DEFAULTS = dict( - server_root="/etc/apache2", - vhost_root="/etc/apache2/sites-available", - vhost_files="*", - logs_root="/var/log/apache2", - ctl="apache2ctl", - version_cmd=['apache2ctl', '-v'], - restart_cmd=['apache2ctl', 'graceful'], - conftest_cmd=['apache2ctl', 'configtest'], - enmod=None, - dismod=None, - le_vhost_ext="-le-ssl.conf", - handle_modules=False, - handle_sites=False, - challenge_location="/etc/apache2", - MOD_SSL_CONF_SRC=pkg_resources.resource_filename( - "certbot_apache", "options-ssl-apache.conf") - ) - - def option(self, key): - """Get a value from options""" - return self.options.get(key) - - def _prepare_options(self): - """ - Set the values possibly changed by command line parameters to - OS_DEFAULTS constant dictionary - """ - opts = ["enmod", "dismod", "le_vhost_ext", "server_root", "vhost_root", - "logs_root", "challenge_location", "handle_modules", "handle_sites", - "ctl"] - for o in opts: - # Config options use dashes instead of underscores - if self.conf(o.replace("_", "-")) is not None: - self.options[o] = self.conf(o.replace("_", "-")) - else: - self.options[o] = self.OS_DEFAULTS[o] - - # Special cases - self.options["version_cmd"][0] = self.option("ctl") - self.options["restart_cmd"][0] = self.option("ctl") - self.options["conftest_cmd"][0] = self.option("ctl") - - @classmethod - def add_parser_arguments(cls, add): - # When adding, modifying or deleting command line arguments, be sure to - # include the changes in the list used in method _prepare_options() to - # ensure consistent behavior. - add("enmod", default=cls.OS_DEFAULTS["enmod"], - help="Path to the Apache 'a2enmod' binary") - add("dismod", default=cls.OS_DEFAULTS["dismod"], - help="Path to the Apache 'a2dismod' binary") - add("le-vhost-ext", default=cls.OS_DEFAULTS["le_vhost_ext"], - help="SSL vhost configuration extension") - add("server-root", default=cls.OS_DEFAULTS["server_root"], - help="Apache server root directory") - add("vhost-root", default=None, - help="Apache server VirtualHost configuration root") - add("logs-root", default=cls.OS_DEFAULTS["logs_root"], - help="Apache server logs directory") - add("challenge-location", - default=cls.OS_DEFAULTS["challenge_location"], - help="Directory path for challenge configuration") - add("handle-modules", default=cls.OS_DEFAULTS["handle_modules"], - help="Let installer handle enabling required modules for you " + - "(Only Ubuntu/Debian currently)") - add("handle-sites", default=cls.OS_DEFAULTS["handle_sites"], - help="Let installer handle enabling sites for you " + - "(Only Ubuntu/Debian currently)") - add("ctl", default=cls.OS_DEFAULTS["ctl"], - help="Full path to Apache control script") - util.add_deprecated_argument( - add, argument_name="init-script", nargs=1) - - def __init__(self, *args, **kwargs): - """Initialize an Apache Configurator. - - :param tup version: version of Apache as a tuple (2, 4, 7) - (used mostly for unittesting) - - """ - version = kwargs.pop("version", None) - super(ApacheConfigurator, self).__init__(*args, **kwargs) - - # Add name_server association dict - self.assoc = dict() # type: Dict[str, obj.VirtualHost] - # Outstanding challenges - self._chall_out = set() # type: Set[KeyAuthorizationAnnotatedChallenge] - # List of vhosts configured per wildcard domain on this run. - # used by deploy_cert() and enhance() - self._wildcard_vhosts = dict() # type: Dict[str, List[obj.VirtualHost]] - # Maps enhancements to vhosts we've enabled the enhancement for - self._enhanced_vhosts = defaultdict(set) # type: DefaultDict[str, Set[obj.VirtualHost]] - # Temporary state for AutoHSTS enhancement - self._autohsts = {} # type: Dict[str, Dict[str, Union[int, float]]] - - # These will be set in the prepare function - self._prepared = False - self.parser = None - self.version = version - self.vhosts = None - self.options = copy.deepcopy(self.OS_DEFAULTS) - self._enhance_func = {"redirect": self._enable_redirect, - "ensure-http-header": self._set_http_header, - "staple-ocsp": self._enable_ocsp_stapling} - - @property - def mod_ssl_conf(self): - """Full absolute path to SSL configuration file.""" - return os.path.join(self.config.config_dir, - constants.MOD_SSL_CONF_DEST) - - @property - def updated_mod_ssl_conf_digest(self): - """Full absolute path to digest of updated SSL configuration file.""" - return os.path.join(self.config.config_dir, constants.UPDATED_MOD_SSL_CONF_DIGEST) - - - def prepare(self): - """Prepare the authenticator/installer. - - :raises .errors.NoInstallationError: If Apache configs cannot be found - :raises .errors.MisconfigurationError: If Apache is misconfigured - :raises .errors.NotSupportedError: If Apache version is not supported - :raises .errors.PluginError: If there is any other error - - """ - # Perform the actual Augeas initialization to be able to react - try: - self.init_augeas() - except ImportError: - raise errors.NoInstallationError("Problem in Augeas installation") - - self._prepare_options() - - # Verify Apache is installed - self._verify_exe_availability(self.option("ctl")) - - # Make sure configuration is valid - self.config_test() - - # Set Version - if self.version is None: - self.version = self.get_version() - logger.debug('Apache version is %s', - '.'.join(str(i) for i in self.version)) - if self.version < (2, 2): - raise errors.NotSupportedError( - "Apache Version %s not supported.", str(self.version)) - - if not self._check_aug_version(): - raise errors.NotSupportedError( - "Apache plugin support requires libaugeas0 and augeas-lenses " - "version 1.2.0 or higher, please make sure you have you have " - "those installed.") - - self.parser = self.get_parser() - - # Check for errors in parsing files with Augeas - self.check_parsing_errors("httpd.aug") - - # Get all of the available vhosts - self.vhosts = self.get_virtual_hosts() - - self.install_ssl_options_conf(self.mod_ssl_conf, - self.updated_mod_ssl_conf_digest) - - # Prevent two Apache plugins from modifying a config at once - try: - util.lock_dir_until_exit(self.option("server_root")) - except (OSError, errors.LockError): - logger.debug("Encountered error:", exc_info=True) - raise errors.PluginError( - "Unable to lock %s", self.option("server_root")) - self._prepared = True - - def _verify_exe_availability(self, exe): - """Checks availability of Apache executable""" - if not util.exe_exists(exe): - if not path_surgery(exe): - raise errors.NoInstallationError( - 'Cannot find Apache executable {0}'.format(exe)) - - def _check_aug_version(self): - """ Checks that we have recent enough version of libaugeas. - If augeas version is recent enough, it will support case insensitive - regexp matching""" - - self.aug.set("/test/path/testing/arg", "aRgUMeNT") - try: - matches = self.aug.match( - "/test//*[self::arg=~regexp('argument', 'i')]") - except RuntimeError: - self.aug.remove("/test/path") - return False - self.aug.remove("/test/path") - return matches - - def get_parser(self): - """Initializes the ApacheParser""" - # If user provided vhost_root value in command line, use it - return parser.ApacheParser( - self.aug, self.option("server_root"), self.conf("vhost-root"), - self.version, configurator=self) - - def _wildcard_domain(self, domain): - """ - Checks if domain is a wildcard domain - - :param str domain: Domain to check - - :returns: If the domain is wildcard domain - :rtype: bool - """ - if isinstance(domain, six.text_type): - wildcard_marker = u"*." - else: - wildcard_marker = b"*." - return domain.startswith(wildcard_marker) - - def deploy_cert(self, domain, cert_path, key_path, - chain_path=None, fullchain_path=None): - """Deploys certificate to specified virtual host. - - Currently tries to find the last directives to deploy the certificate - in the VHost associated with the given domain. If it can't find the - directives, it searches the "included" confs. The function verifies - that it has located the three directives and finally modifies them - to point to the correct destination. After the certificate is - installed, the VirtualHost is enabled if it isn't already. - - .. todo:: Might be nice to remove chain directive if none exists - This shouldn't happen within certbot though - - :raises errors.PluginError: When unable to deploy certificate due to - a lack of directives - - """ - vhosts = self.choose_vhosts(domain) - for vhost in vhosts: - self._deploy_cert(vhost, cert_path, key_path, chain_path, fullchain_path) - - def choose_vhosts(self, domain, create_if_no_ssl=True): - """ - Finds VirtualHosts that can be used with the provided domain - - :param str domain: Domain name to match VirtualHosts to - :param bool create_if_no_ssl: If found VirtualHost doesn't have a HTTPS - counterpart, should one get created - - :returns: List of VirtualHosts or None - :rtype: `list` of :class:`~certbot_apache.obj.VirtualHost` - """ - - if self._wildcard_domain(domain): - if domain in self._wildcard_vhosts: - # Vhosts for a wildcard domain were already selected - return self._wildcard_vhosts[domain] - # Ask user which VHosts to support. - # Returned objects are guaranteed to be ssl vhosts - return self._choose_vhosts_wildcard(domain, create_if_no_ssl) - else: - return [self.choose_vhost(domain, create_if_no_ssl)] - - def _vhosts_for_wildcard(self, domain): - """ - Get VHost objects for every VirtualHost that the user wants to handle - with the wildcard certificate. - """ - - # Collect all vhosts that match the name - matched = set() - for vhost in self.vhosts: - for name in vhost.get_names(): - if self._in_wildcard_scope(name, domain): - matched.add(vhost) - - return list(matched) - - def _in_wildcard_scope(self, name, domain): - """ - Helper method for _vhosts_for_wildcard() that makes sure that the domain - is in the scope of wildcard domain. - - eg. in scope: domain = *.wild.card, name = 1.wild.card - not in scope: domain = *.wild.card, name = 1.2.wild.card - """ - if len(name.split(".")) == len(domain.split(".")): - return fnmatch.fnmatch(name, domain) - - - def _choose_vhosts_wildcard(self, domain, create_ssl=True): - """Prompts user to choose vhosts to install a wildcard certificate for""" - - # Get all vhosts that are covered by the wildcard domain - vhosts = self._vhosts_for_wildcard(domain) - - # Go through the vhosts, making sure that we cover all the names - # present, but preferring the SSL vhosts - filtered_vhosts = dict() - for vhost in vhosts: - for name in vhost.get_names(): - if vhost.ssl: - # Always prefer SSL vhosts - filtered_vhosts[name] = vhost - elif name not in filtered_vhosts and create_ssl: - # Add if not in list previously - filtered_vhosts[name] = vhost - - # Only unique VHost objects - dialog_input = set([vhost for vhost in filtered_vhosts.values()]) - - # Ask the user which of names to enable, expect list of names back - dialog_output = display_ops.select_vhost_multiple(list(dialog_input)) - - if not dialog_output: - logger.error( - "No vhost exists with servername or alias for domain %s. " - "No vhost was selected. Please specify ServerName or ServerAlias " - "in the Apache config.", - domain) - raise errors.PluginError("No vhost selected") - - # Make sure we create SSL vhosts for the ones that are HTTP only - # if requested. - return_vhosts = list() - for vhost in dialog_output: - if not vhost.ssl: - return_vhosts.append(self.make_vhost_ssl(vhost)) - else: - return_vhosts.append(vhost) - - self._wildcard_vhosts[domain] = return_vhosts - return return_vhosts - - - def _deploy_cert(self, vhost, cert_path, key_path, chain_path, fullchain_path): - """ - Helper function for deploy_cert() that handles the actual deployment - this exists because we might want to do multiple deployments per - domain originally passed for deploy_cert(). This is especially true - with wildcard certificates - """ - - - # This is done first so that ssl module is enabled and cert_path, - # cert_key... can all be parsed appropriately - self.prepare_server_https("443") - - # Add directives and remove duplicates - self._add_dummy_ssl_directives(vhost.path) - self._clean_vhost(vhost) - - path = {"cert_path": self.parser.find_dir("SSLCertificateFile", - None, vhost.path), - "cert_key": self.parser.find_dir("SSLCertificateKeyFile", - None, vhost.path)} - - # Only include if a certificate chain is specified - if chain_path is not None: - path["chain_path"] = self.parser.find_dir( - "SSLCertificateChainFile", None, vhost.path) - - # Handle errors when certificate/key directives cannot be found - if not path["cert_path"]: - logger.warning( - "Cannot find an SSLCertificateFile directive in %s. " - "VirtualHost was not modified", vhost.path) - raise errors.PluginError( - "Unable to find an SSLCertificateFile directive") - elif not path["cert_key"]: - logger.warning( - "Cannot find an SSLCertificateKeyFile directive for " - "certificate in %s. VirtualHost was not modified", vhost.path) - raise errors.PluginError( - "Unable to find an SSLCertificateKeyFile directive for " - "certificate") - - logger.info("Deploying Certificate to VirtualHost %s", vhost.filep) - - if self.version < (2, 4, 8) or (chain_path and not fullchain_path): - # install SSLCertificateFile, SSLCertificateKeyFile, - # and SSLCertificateChainFile directives - set_cert_path = cert_path - self.aug.set(path["cert_path"][-1], cert_path) - self.aug.set(path["cert_key"][-1], key_path) - if chain_path is not None: - self.parser.add_dir(vhost.path, - "SSLCertificateChainFile", chain_path) - else: - raise errors.PluginError("--chain-path is required for your " - "version of Apache") - else: - if not fullchain_path: - raise errors.PluginError("Please provide the --fullchain-path " - "option pointing to your full chain file") - set_cert_path = fullchain_path - self.aug.set(path["cert_path"][-1], fullchain_path) - self.aug.set(path["cert_key"][-1], key_path) - - # Enable the new vhost if needed - if not vhost.enabled: - self.enable_site(vhost) - - # Save notes about the transaction that took place - self.save_notes += ("Changed vhost at %s with addresses of %s\n" - "\tSSLCertificateFile %s\n" - "\tSSLCertificateKeyFile %s\n" % - (vhost.filep, - ", ".join(str(addr) for addr in vhost.addrs), - set_cert_path, key_path)) - if chain_path is not None: - self.save_notes += "\tSSLCertificateChainFile %s\n" % chain_path - - def choose_vhost(self, target_name, create_if_no_ssl=True): - """Chooses a virtual host based on the given domain name. - - If there is no clear virtual host to be selected, the user is prompted - with all available choices. - - The returned vhost is guaranteed to have TLS enabled unless - create_if_no_ssl is set to False, in which case there is no such guarantee - and the result is not cached. - - :param str target_name: domain name - :param bool create_if_no_ssl: If found VirtualHost doesn't have a HTTPS - counterpart, should one get created - - :returns: vhost associated with name - :rtype: :class:`~certbot_apache.obj.VirtualHost` - - :raises .errors.PluginError: If no vhost is available or chosen - - """ - # Allows for domain names to be associated with a virtual host - if target_name in self.assoc: - return self.assoc[target_name] - - # Try to find a reasonable vhost - vhost = self._find_best_vhost(target_name) - if vhost is not None: - if not create_if_no_ssl: - return vhost - if not vhost.ssl: - vhost = self.make_vhost_ssl(vhost) - - self._add_servername_alias(target_name, vhost) - self.assoc[target_name] = vhost - return vhost - - # Negate create_if_no_ssl value to indicate if we want a SSL vhost - # to get created if a non-ssl vhost is selected. - return self._choose_vhost_from_list(target_name, temp=not create_if_no_ssl) - - def _choose_vhost_from_list(self, target_name, temp=False): - # Select a vhost from a list - vhost = display_ops.select_vhost(target_name, self.vhosts) - if vhost is None: - logger.error( - "No vhost exists with servername or alias of %s. " - "No vhost was selected. Please specify ServerName or ServerAlias " - "in the Apache config.", - target_name) - raise errors.PluginError("No vhost selected") - elif temp: - return vhost - elif not vhost.ssl: - addrs = self._get_proposed_addrs(vhost, "443") - # TODO: Conflicts is too conservative - if not any(vhost.enabled and vhost.conflicts(addrs) for - vhost in self.vhosts): - vhost = self.make_vhost_ssl(vhost) - else: - logger.error( - "The selected vhost would conflict with other HTTPS " - "VirtualHosts within Apache. Please select another " - "vhost or add ServerNames to your configuration.") - raise errors.PluginError( - "VirtualHost not able to be selected.") - - self._add_servername_alias(target_name, vhost) - self.assoc[target_name] = vhost - return vhost - - def included_in_wildcard(self, names, target_name): - """Is target_name covered by a wildcard? - - :param names: server aliases - :type names: `collections.Iterable` of `str` - :param str target_name: name to compare with wildcards - - :returns: True if target_name is covered by a wildcard, - otherwise, False - :rtype: bool - - """ - # use lowercase strings because fnmatch can be case sensitive - target_name = target_name.lower() - for name in names: - name = name.lower() - # fnmatch treats "[seq]" specially and [ or ] characters aren't - # valid in Apache but Apache doesn't error out if they are present - if "[" not in name and fnmatch.fnmatch(target_name, name): - return True - return False - - def find_best_http_vhost(self, target, filter_defaults, port="80"): - """Returns non-HTTPS vhost objects found from the Apache config - - :param str target: Domain name of the desired VirtualHost - :param bool filter_defaults: whether _default_ vhosts should be - included if it is the best match - :param str port: port number the vhost should be listening on - - :returns: VirtualHost object that's the best match for target name - :rtype: `obj.VirtualHost` or None - """ - filtered_vhosts = [] - for vhost in self.vhosts: - if any(a.is_wildcard() or a.get_port() == port for a in vhost.addrs) and not vhost.ssl: - filtered_vhosts.append(vhost) - return self._find_best_vhost(target, filtered_vhosts, filter_defaults) - - def _find_best_vhost(self, target_name, vhosts=None, filter_defaults=True): - """Finds the best vhost for a target_name. - - This does not upgrade a vhost to HTTPS... it only finds the most - appropriate vhost for the given target_name. - - :param str target_name: domain handled by the desired vhost - :param vhosts: vhosts to consider - :type vhosts: `collections.Iterable` of :class:`~certbot_apache.obj.VirtualHost` - :param bool filter_defaults: whether a vhost with a _default_ - addr is acceptable - - :returns: VHost or None - - """ - # Points 6 - Servername SSL - # Points 5 - Wildcard SSL - # Points 4 - Address name with SSL - # Points 3 - Servername no SSL - # Points 2 - Wildcard no SSL - # Points 1 - Address name with no SSL - best_candidate = None - best_points = 0 - - if vhosts is None: - vhosts = self.vhosts - - for vhost in vhosts: - if vhost.modmacro is True: - continue - names = vhost.get_names() - if target_name in names: - points = 3 - elif self.included_in_wildcard(names, target_name): - points = 2 - elif any(addr.get_addr() == target_name for addr in vhost.addrs): - points = 1 - else: - # No points given if names can't be found. - # This gets hit but doesn't register - continue # pragma: no cover - - if vhost.ssl: - points += 3 - - if points > best_points: - best_points = points - best_candidate = vhost - - # No winners here... is there only one reasonable vhost? - if best_candidate is None: - if filter_defaults: - vhosts = self._non_default_vhosts(vhosts) - # remove mod_macro hosts from reasonable vhosts - reasonable_vhosts = [vh for vh - in vhosts if vh.modmacro is False] - if len(reasonable_vhosts) == 1: - best_candidate = reasonable_vhosts[0] - - return best_candidate - - def _non_default_vhosts(self, vhosts): - """Return all non _default_ only vhosts.""" - return [vh for vh in vhosts if not all( - addr.get_addr() == "_default_" for addr in vh.addrs - )] - - def get_all_names(self): - """Returns all names found in the Apache Configuration. - - :returns: All ServerNames, ServerAliases, and reverse DNS entries for - virtual host addresses - :rtype: set - - """ - all_names = set() # type: Set[str] - - vhost_macro = [] - - for vhost in self.vhosts: - all_names.update(vhost.get_names()) - if vhost.modmacro: - vhost_macro.append(vhost.filep) - - for addr in vhost.addrs: - if common.hostname_regex.match(addr.get_addr()): - all_names.add(addr.get_addr()) - else: - name = self.get_name_from_ip(addr) - if name: - all_names.add(name) - - if len(vhost_macro) > 0: - zope.component.getUtility(interfaces.IDisplay).notification( - "Apache mod_macro seems to be in use in file(s):\n{0}" - "\n\nUnfortunately mod_macro is not yet supported".format( - "\n ".join(vhost_macro)), force_interactive=True) - - return util.get_filtered_names(all_names) - - def get_name_from_ip(self, addr): # pylint: disable=no-self-use - """Returns a reverse dns name if available. - - :param addr: IP Address - :type addr: ~.common.Addr - - :returns: name or empty string if name cannot be determined - :rtype: str - - """ - # If it isn't a private IP, do a reverse DNS lookup - if not common.private_ips_regex.match(addr.get_addr()): - try: - socket.inet_aton(addr.get_addr()) - return socket.gethostbyaddr(addr.get_addr())[0] - except (socket.error, socket.herror, socket.timeout): - pass - - return "" - - def _get_vhost_names(self, path): - """Helper method for getting the ServerName and - ServerAlias values from vhost in path - - :param path: Path to read ServerName and ServerAliases from - - :returns: Tuple including ServerName and `list` of ServerAlias strings - """ - - servername_match = self.parser.find_dir( - "ServerName", None, start=path, exclude=False) - serveralias_match = self.parser.find_dir( - "ServerAlias", None, start=path, exclude=False) - - serveraliases = [] - for alias in serveralias_match: - serveralias = self.parser.get_arg(alias) - serveraliases.append(serveralias) - - servername = None - if servername_match: - # Get last ServerName as each overwrites the previous - servername = self.parser.get_arg(servername_match[-1]) - - return (servername, serveraliases) - - def _add_servernames(self, host): - """Helper function for get_virtual_hosts(). - - :param host: In progress vhost whose names will be added - :type host: :class:`~certbot_apache.obj.VirtualHost` - - """ - - servername, serveraliases = self._get_vhost_names(host.path) - - for alias in serveraliases: - if not host.modmacro: - host.aliases.add(alias) - - if not host.modmacro: - host.name = servername - - def _create_vhost(self, path): - """Used by get_virtual_hosts to create vhost objects - - :param str path: Augeas path to virtual host - - :returns: newly created vhost - :rtype: :class:`~certbot_apache.obj.VirtualHost` - - """ - addrs = set() - try: - args = self.aug.match(path + "/arg") - except RuntimeError: - logger.warning("Encountered a problem while parsing file: %s, skipping", path) - return None - for arg in args: - addrs.add(obj.Addr.fromstring(self.parser.get_arg(arg))) - is_ssl = False - - if self.parser.find_dir("SSLEngine", "on", start=path, exclude=False): - is_ssl = True - - # "SSLEngine on" might be set outside of - # Treat vhosts with port 443 as ssl vhosts - for addr in addrs: - if addr.get_port() == "443": - is_ssl = True - - filename = apache_util.get_file_path( - self.aug.get("/augeas/files%s/path" % apache_util.get_file_path(path))) - if filename is None: - return None - - macro = False - if "/macro/" in path.lower(): - macro = True - - vhost_enabled = self.parser.parsed_in_original(filename) - - vhost = obj.VirtualHost(filename, path, addrs, is_ssl, - vhost_enabled, modmacro=macro) - self._add_servernames(vhost) - return vhost - - def get_virtual_hosts(self): - """Returns list of virtual hosts found in the Apache configuration. - - :returns: List of :class:`~certbot_apache.obj.VirtualHost` - objects found in configuration - :rtype: list - - """ - # Search base config, and all included paths for VirtualHosts - file_paths = {} # type: Dict[str, str] - internal_paths = defaultdict(set) # type: DefaultDict[str, Set[str]] - vhs = [] - # Make a list of parser paths because the parser_paths - # dictionary may be modified during the loop. - for vhost_path in list(self.parser.parser_paths): - paths = self.aug.match( - ("/files%s//*[label()=~regexp('%s')]" % - (vhost_path, parser.case_i("VirtualHost")))) - paths = [path for path in paths if - "virtualhost" in os.path.basename(path).lower()] - for path in paths: - new_vhost = self._create_vhost(path) - if not new_vhost: - continue - internal_path = apache_util.get_internal_aug_path(new_vhost.path) - realpath = os.path.realpath(new_vhost.filep) - if realpath not in file_paths: - file_paths[realpath] = new_vhost.filep - internal_paths[realpath].add(internal_path) - vhs.append(new_vhost) - elif (realpath == new_vhost.filep and - realpath != file_paths[realpath]): - # Prefer "real" vhost paths instead of symlinked ones - # ex: sites-enabled/vh.conf -> sites-available/vh.conf - - # remove old (most likely) symlinked one - new_vhs = [] - for v in vhs: - if v.filep == file_paths[realpath]: - internal_paths[realpath].remove( - apache_util.get_internal_aug_path(v.path)) - else: - new_vhs.append(v) - vhs = new_vhs - - file_paths[realpath] = realpath - internal_paths[realpath].add(internal_path) - vhs.append(new_vhost) - elif internal_path not in internal_paths[realpath]: - internal_paths[realpath].add(internal_path) - vhs.append(new_vhost) - return vhs - - def is_name_vhost(self, target_addr): - """Returns if vhost is a name based vhost - - NameVirtualHost was deprecated in Apache 2.4 as all VirtualHosts are - now NameVirtualHosts. If version is earlier than 2.4, check if addr - has a NameVirtualHost directive in the Apache config - - :param certbot_apache.obj.Addr target_addr: vhost address - - :returns: Success - :rtype: bool - - """ - # Mixed and matched wildcard NameVirtualHost with VirtualHost - # behavior is undefined. Make sure that an exact match exists - - # search for NameVirtualHost directive for ip_addr - # note ip_addr can be FQDN although Apache does not recommend it - return (self.version >= (2, 4) or - self.parser.find_dir("NameVirtualHost", str(target_addr))) - - def add_name_vhost(self, addr): - """Adds NameVirtualHost directive for given address. - - :param addr: Address that will be added as NameVirtualHost directive - :type addr: :class:`~certbot_apache.obj.Addr` - - """ - - loc = parser.get_aug_path(self.parser.loc["name"]) - if addr.get_port() == "443": - path = self.parser.add_dir_to_ifmodssl( - loc, "NameVirtualHost", [str(addr)]) - else: - path = self.parser.add_dir(loc, "NameVirtualHost", [str(addr)]) - - msg = ("Setting %s to be NameBasedVirtualHost\n" - "\tDirective added to %s\n" % (addr, path)) - logger.debug(msg) - self.save_notes += msg - - def prepare_server_https(self, port, temp=False): - """Prepare the server for HTTPS. - - Make sure that the ssl_module is loaded and that the server - is appropriately listening on port. - - :param str port: Port to listen on - - """ - - self.prepare_https_modules(temp) - self.ensure_listen(port, https=True) - - def ensure_listen(self, port, https=False): - """Make sure that Apache is listening on the port. Checks if the - Listen statement for the port already exists, and adds it to the - configuration if necessary. - - :param str port: Port number to check and add Listen for if not in - place already - :param bool https: If the port will be used for HTTPS - - """ - - # If HTTPS requested for nonstandard port, add service definition - if https and port != "443": - port_service = "%s %s" % (port, "https") - else: - port_service = port - - # Check for Listen - # Note: This could be made to also look for ip:443 combo - listens = [self.parser.get_arg(x).split()[0] for - x in self.parser.find_dir("Listen")] - - # Listen already in place - if self._has_port_already(listens, port): - return - - listen_dirs = set(listens) - - if not listens: - listen_dirs.add(port_service) - - for listen in listens: - # For any listen statement, check if the machine also listens on - # the given port. If not, add such a listen statement. - if len(listen.split(":")) == 1: - # Its listening to all interfaces - if port not in listen_dirs and port_service not in listen_dirs: - listen_dirs.add(port_service) - else: - # The Listen statement specifies an ip - _, ip = listen[::-1].split(":", 1) - ip = ip[::-1] - if "%s:%s" % (ip, port_service) not in listen_dirs and ( - "%s:%s" % (ip, port_service) not in listen_dirs): - listen_dirs.add("%s:%s" % (ip, port_service)) - if https: - self._add_listens_https(listen_dirs, listens, port) - else: - self._add_listens_http(listen_dirs, listens, port) - - def _add_listens_http(self, listens, listens_orig, port): - """Helper method for ensure_listen to figure out which new - listen statements need adding for listening HTTP on port - - :param set listens: Set of all needed Listen statements - :param list listens_orig: List of existing listen statements - :param string port: Port number we're adding - """ - - new_listens = listens.difference(listens_orig) - - if port in new_listens: - # We have wildcard, skip the rest - self.parser.add_dir(parser.get_aug_path(self.parser.loc["listen"]), - "Listen", port) - self.save_notes += "Added Listen %s directive to %s\n" % ( - port, self.parser.loc["listen"]) - else: - for listen in new_listens: - self.parser.add_dir(parser.get_aug_path( - self.parser.loc["listen"]), "Listen", listen.split(" ")) - self.save_notes += ("Added Listen %s directive to " - "%s\n") % (listen, - self.parser.loc["listen"]) - - def _add_listens_https(self, listens, listens_orig, port): - """Helper method for ensure_listen to figure out which new - listen statements need adding for listening HTTPS on port - - :param set listens: Set of all needed Listen statements - :param list listens_orig: List of existing listen statements - :param string port: Port number we're adding - """ - - # Add service definition for non-standard ports - if port != "443": - port_service = "%s %s" % (port, "https") - else: - port_service = port - - new_listens = listens.difference(listens_orig) - - if port in new_listens or port_service in new_listens: - # We have wildcard, skip the rest - self.parser.add_dir_to_ifmodssl( - parser.get_aug_path(self.parser.loc["listen"]), - "Listen", port_service.split(" ")) - self.save_notes += "Added Listen %s directive to %s\n" % ( - port_service, self.parser.loc["listen"]) - else: - for listen in new_listens: - self.parser.add_dir_to_ifmodssl( - parser.get_aug_path(self.parser.loc["listen"]), - "Listen", listen.split(" ")) - self.save_notes += ("Added Listen %s directive to " - "%s\n") % (listen, - self.parser.loc["listen"]) - - def _has_port_already(self, listens, port): - """Helper method for prepare_server_https to find out if user - already has an active Listen statement for the port we need - - :param list listens: List of listen variables - :param string port: Port in question - """ - - if port in listens: - return True - # Check if Apache is already listening on a specific IP - for listen in listens: - if len(listen.split(":")) > 1: - # Ugly but takes care of protocol def, eg: 1.1.1.1:443 https - if listen.split(":")[-1].split(" ")[0] == port: - return True - - def prepare_https_modules(self, temp): - """Helper method for prepare_server_https, taking care of enabling - needed modules - - :param boolean temp: If the change is temporary - """ - - if self.option("handle_modules"): - if self.version >= (2, 4) and ("socache_shmcb_module" not in - self.parser.modules): - self.enable_mod("socache_shmcb", temp=temp) - if "ssl_module" not in self.parser.modules: - self.enable_mod("ssl", temp=temp) - - def make_addrs_sni_ready(self, addrs): - """Checks to see if the server is ready for SNI challenges. - - :param addrs: Addresses to check SNI compatibility - :type addrs: :class:`~certbot_apache.obj.Addr` - - """ - # Version 2.4 and later are automatically SNI ready. - if self.version >= (2, 4): - return - - for addr in addrs: - if not self.is_name_vhost(addr): - logger.debug("Setting VirtualHost at %s to be a name " - "based virtual host", addr) - self.add_name_vhost(addr) - - def make_vhost_ssl(self, nonssl_vhost): # pylint: disable=too-many-locals - """Makes an ssl_vhost version of a nonssl_vhost. - - Duplicates vhost and adds default ssl options - New vhost will reside as (nonssl_vhost.path) + - ``self.option("le_vhost_ext")`` - - .. note:: This function saves the configuration - - :param nonssl_vhost: Valid VH that doesn't have SSLEngine on - :type nonssl_vhost: :class:`~certbot_apache.obj.VirtualHost` - - :returns: SSL vhost - :rtype: :class:`~certbot_apache.obj.VirtualHost` - - :raises .errors.PluginError: If more than one virtual host is in - the file or if plugin is unable to write/read vhost files. - - """ - avail_fp = nonssl_vhost.filep - ssl_fp = self._get_ssl_vhost_path(avail_fp) - - orig_matches = self.aug.match("/files%s//* [label()=~regexp('%s')]" % - (self._escape(ssl_fp), - parser.case_i("VirtualHost"))) - - self._copy_create_ssl_vhost_skeleton(nonssl_vhost, ssl_fp) - - # Reload augeas to take into account the new vhost - self.aug.load() - # Get Vhost augeas path for new vhost - new_matches = self.aug.match("/files%s//* [label()=~regexp('%s')]" % - (self._escape(ssl_fp), - parser.case_i("VirtualHost"))) - - vh_p = self._get_new_vh_path(orig_matches, new_matches) - - if not vh_p: - # The vhost was not found on the currently parsed paths - # Make Augeas aware of the new vhost - self.parser.parse_file(ssl_fp) - # Try to search again - new_matches = self.aug.match( - "/files%s//* [label()=~regexp('%s')]" % - (self._escape(ssl_fp), - parser.case_i("VirtualHost"))) - vh_p = self._get_new_vh_path(orig_matches, new_matches) - if not vh_p: - raise errors.PluginError( - "Could not reverse map the HTTPS VirtualHost to the original") - - - # Update Addresses - self._update_ssl_vhosts_addrs(vh_p) - - # Log actions and create save notes - logger.info("Created an SSL vhost at %s", ssl_fp) - self.save_notes += "Created ssl vhost at %s\n" % ssl_fp - self.save() - - # We know the length is one because of the assertion above - # Create the Vhost object - ssl_vhost = self._create_vhost(vh_p) - ssl_vhost.ancestor = nonssl_vhost - - self.vhosts.append(ssl_vhost) - - # NOTE: Searches through Augeas seem to ruin changes to directives - # The configuration must also be saved before being searched - # for the new directives; For these reasons... this is tacked - # on after fully creating the new vhost - - # Now check if addresses need to be added as NameBasedVhost addrs - # This is for compliance with versions of Apache < 2.4 - self._add_name_vhost_if_necessary(ssl_vhost) - - return ssl_vhost - - def _get_new_vh_path(self, orig_matches, new_matches): - """ Helper method for make_vhost_ssl for matching augeas paths. Returns - VirtualHost path from new_matches that's not present in orig_matches. - - Paths are normalized, because augeas leaves indices out for paths - with only single directive with a similar key """ - - orig_matches = [i.replace("[1]", "") for i in orig_matches] - for match in new_matches: - if match.replace("[1]", "") not in orig_matches: - # Return the unmodified path - return match - return None - - def _get_ssl_vhost_path(self, non_ssl_vh_fp): - """ Get a file path for SSL vhost, uses user defined path as priority, - but if the value is invalid or not defined, will fall back to non-ssl - vhost filepath. - - :param str non_ssl_vh_fp: Filepath of non-SSL vhost - - :returns: Filepath for SSL vhost - :rtype: str - """ - - if self.conf("vhost-root") and os.path.exists(self.conf("vhost-root")): - fp = os.path.join(os.path.realpath(self.option("vhost_root")), - os.path.basename(non_ssl_vh_fp)) - else: - # Use non-ssl filepath - fp = os.path.realpath(non_ssl_vh_fp) - - if fp.endswith(".conf"): - return fp[:-(len(".conf"))] + self.option("le_vhost_ext") - else: - return fp + self.option("le_vhost_ext") - - def _sift_rewrite_rule(self, line): - """Decides whether a line should be copied to a SSL vhost. - - A canonical example of when sifting a line is required: - When the http vhost contains a RewriteRule that unconditionally - redirects any request to the https version of the same site. - e.g: - RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent] - Copying the above line to the ssl vhost would cause a - redirection loop. - - :param str line: a line extracted from the http vhost. - - :returns: True - don't copy line from http vhost to SSL vhost. - :rtype: bool - - """ - if not line.lower().lstrip().startswith("rewriterule"): - return False - - # According to: http://httpd.apache.org/docs/2.4/rewrite/flags.html - # The syntax of a RewriteRule is: - # RewriteRule pattern target [Flag1,Flag2,Flag3] - # i.e. target is required, so it must exist. - target = line.split()[2].strip() - - # target may be surrounded with quotes - if target[0] in ("'", '"') and target[0] == target[-1]: - target = target[1:-1] - - # Sift line if it redirects the request to a HTTPS site - return target.startswith("https://") - - def _copy_create_ssl_vhost_skeleton(self, vhost, ssl_fp): - """Copies over existing Vhost with IfModule mod_ssl.c> skeleton. - - :param obj.VirtualHost vhost: Original VirtualHost object - :param str ssl_fp: Full path where the new ssl_vhost will reside. - - A new file is created on the filesystem. - - """ - # First register the creation so that it is properly removed if - # configuration is rolled back - if os.path.exists(ssl_fp): - notes = "Appended new VirtualHost directive to file %s" % ssl_fp - files = set() - files.add(ssl_fp) - self.reverter.add_to_checkpoint(files, notes) - else: - self.reverter.register_file_creation(False, ssl_fp) - sift = False - - try: - orig_contents = self._get_vhost_block(vhost) - ssl_vh_contents, sift = self._sift_rewrite_rules(orig_contents) - - with open(ssl_fp, "a") as new_file: - new_file.write("\n") - new_file.write("\n".join(ssl_vh_contents)) - # The content does not include the closing tag, so add it - new_file.write("\n") - new_file.write("\n") - # Add new file to augeas paths if we're supposed to handle - # activation (it's not included as default) - if not self.parser.parsed_in_current(ssl_fp): - self.parser.parse_file(ssl_fp) - except IOError: - logger.critical("Error writing/reading to file in make_vhost_ssl", exc_info=True) - raise errors.PluginError("Unable to write/read in make_vhost_ssl") - - if sift: - reporter = zope.component.getUtility(interfaces.IReporter) - reporter.add_message( - "Some rewrite rules copied from {0} were disabled in the " - "vhost for your HTTPS site located at {1} because they have " - "the potential to create redirection loops.".format( - vhost.filep, ssl_fp), reporter.MEDIUM_PRIORITY) - self.aug.set("/augeas/files%s/mtime" % (self._escape(ssl_fp)), "0") - self.aug.set("/augeas/files%s/mtime" % (self._escape(vhost.filep)), "0") - - def _sift_rewrite_rules(self, contents): - """ Helper function for _copy_create_ssl_vhost_skeleton to prepare the - new HTTPS VirtualHost contents. Currently disabling the rewrites """ - - result = [] - sift = False - contents = iter(contents) - - comment = ("# Some rewrite rules in this file were " - "disabled on your HTTPS site,\n" - "# because they have the potential to create " - "redirection loops.\n") - - for line in contents: - A = line.lower().lstrip().startswith("rewritecond") - B = line.lower().lstrip().startswith("rewriterule") - - if not (A or B): - result.append(line) - continue - - # A RewriteRule that doesn't need filtering - if B and not self._sift_rewrite_rule(line): - result.append(line) - continue - - # A RewriteRule that does need filtering - if B and self._sift_rewrite_rule(line): - if not sift: - result.append(comment) - sift = True - result.append("# " + line) - continue - - # We save RewriteCond(s) and their corresponding - # RewriteRule in 'chunk'. - # We then decide whether we comment out the entire - # chunk based on its RewriteRule. - chunk = [] - if A: - chunk.append(line) - line = next(contents) - - # RewriteCond(s) must be followed by one RewriteRule - while not line.lower().lstrip().startswith("rewriterule"): - chunk.append(line) - line = next(contents) - - # Now, current line must start with a RewriteRule - chunk.append(line) - - if self._sift_rewrite_rule(line): - if not sift: - result.append(comment) - sift = True - - result.append('\n'.join( - ['# ' + l for l in chunk])) - continue - else: - result.append('\n'.join(chunk)) - continue - return result, sift - - def _get_vhost_block(self, vhost): - """ Helper method to get VirtualHost contents from the original file. - This is done with help of augeas span, which returns the span start and - end positions - - :returns: `list` of VirtualHost block content lines without closing tag - """ - - try: - span_val = self.aug.span(vhost.path) - except ValueError: - logger.critical("Error while reading the VirtualHost %s from " - "file %s", vhost.name, vhost.filep, exc_info=True) - raise errors.PluginError("Unable to read VirtualHost from file") - span_filep = span_val[0] - span_start = span_val[5] - span_end = span_val[6] - with open(span_filep, 'r') as fh: - fh.seek(span_start) - vh_contents = fh.read(span_end-span_start).split("\n") - self._remove_closing_vhost_tag(vh_contents) - return vh_contents - - def _remove_closing_vhost_tag(self, vh_contents): - """Removes the closing VirtualHost tag if it exists. - - This method modifies vh_contents directly to remove the closing - tag. If the closing vhost tag is found, everything on the line - after it is also removed. Whether or not this tag is included - in the result of span depends on the Augeas version. - - :param list vh_contents: VirtualHost block contents to check - - """ - for offset, line in enumerate(reversed(vh_contents)): - if line: - line_index = line.lower().find("") - if line_index != -1: - content_index = len(vh_contents) - offset - 1 - vh_contents[content_index] = line[:line_index] - break - - def _update_ssl_vhosts_addrs(self, vh_path): - ssl_addrs = set() - ssl_addr_p = self.aug.match(vh_path + "/arg") - - for addr in ssl_addr_p: - old_addr = obj.Addr.fromstring( - str(self.parser.get_arg(addr))) - ssl_addr = old_addr.get_addr_obj("443") - self.aug.set(addr, str(ssl_addr)) - ssl_addrs.add(ssl_addr) - - return ssl_addrs - - def _clean_vhost(self, vhost): - # remove duplicated or conflicting ssl directives - self._deduplicate_directives(vhost.path, - ["SSLCertificateFile", - "SSLCertificateKeyFile"]) - # remove all problematic directives - self._remove_directives(vhost.path, ["SSLCertificateChainFile"]) - - def _deduplicate_directives(self, vh_path, directives): - for directive in directives: - while len(self.parser.find_dir(directive, None, - vh_path, False)) > 1: - directive_path = self.parser.find_dir(directive, None, - vh_path, False) - self.aug.remove(re.sub(r"/\w*$", "", directive_path[0])) - - def _remove_directives(self, vh_path, directives): - for directive in directives: - while len(self.parser.find_dir(directive, None, - vh_path, False)) > 0: - directive_path = self.parser.find_dir(directive, None, - vh_path, False) - self.aug.remove(re.sub(r"/\w*$", "", directive_path[0])) - - def _add_dummy_ssl_directives(self, vh_path): - self.parser.add_dir(vh_path, "SSLCertificateFile", - "insert_cert_file_path") - self.parser.add_dir(vh_path, "SSLCertificateKeyFile", - "insert_key_file_path") - # Only include the TLS configuration if not already included - existing_inc = self.parser.find_dir("Include", self.mod_ssl_conf, vh_path) - if not existing_inc: - self.parser.add_dir(vh_path, "Include", self.mod_ssl_conf) - - def _add_servername_alias(self, target_name, vhost): - vh_path = vhost.path - sname, saliases = self._get_vhost_names(vh_path) - if target_name == sname or target_name in saliases: - return - if self._has_matching_wildcard(vh_path, target_name): - return - if not self.parser.find_dir("ServerName", None, - start=vh_path, exclude=False): - self.parser.add_dir(vh_path, "ServerName", target_name) - else: - self.parser.add_dir(vh_path, "ServerAlias", target_name) - self._add_servernames(vhost) - - def _has_matching_wildcard(self, vh_path, target_name): - """Is target_name already included in a wildcard in the vhost? - - :param str vh_path: Augeas path to the vhost - :param str target_name: name to compare with wildcards - - :returns: True if there is a wildcard covering target_name in - the vhost in vhost_path, otherwise, False - :rtype: bool - - """ - matches = self.parser.find_dir( - "ServerAlias", start=vh_path, exclude=False) - aliases = (self.aug.get(match) for match in matches) - return self.included_in_wildcard(aliases, target_name) - - def _add_name_vhost_if_necessary(self, vhost): - """Add NameVirtualHost Directives if necessary for new vhost. - - NameVirtualHosts was a directive in Apache < 2.4 - https://httpd.apache.org/docs/2.2/mod/core.html#namevirtualhost - - :param vhost: New virtual host that was recently created. - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - """ - need_to_save = False - - # See if the exact address appears in any other vhost - # Remember 1.1.1.1:* == 1.1.1.1 -> hence any() - for addr in vhost.addrs: - # In Apache 2.2, when a NameVirtualHost directive is not - # set, "*" and "_default_" will conflict when sharing a port - addrs = set((addr,)) - if addr.get_addr() in ("*", "_default_"): - addrs.update(obj.Addr((a, addr.get_port(),)) - for a in ("*", "_default_")) - - for test_vh in self.vhosts: - if (vhost.filep != test_vh.filep and - any(test_addr in addrs for - test_addr in test_vh.addrs) and - not self.is_name_vhost(addr)): - self.add_name_vhost(addr) - logger.info("Enabling NameVirtualHosts on %s", addr) - need_to_save = True - break - - if need_to_save: - self.save() - - def find_vhost_by_id(self, id_str): - """ - Searches through VirtualHosts and tries to match the id in a comment - - :param str id_str: Id string for matching - - :returns: The matched VirtualHost or None - :rtype: :class:`~certbot_apache.obj.VirtualHost` or None - - :raises .errors.PluginError: If no VirtualHost is found - """ - - for vh in self.vhosts: - if self._find_vhost_id(vh) == id_str: - return vh - msg = "No VirtualHost with ID {} was found.".format(id_str) - logger.warning(msg) - raise errors.PluginError(msg) - - def _find_vhost_id(self, vhost): - """Tries to find the unique ID from the VirtualHost comments. This is - used for keeping track of VirtualHost directive over time. - - :param vhost: Virtual host to add the id - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - :returns: The unique ID or None - :rtype: str or None - """ - - # Strip the {} off from the format string - search_comment = constants.MANAGED_COMMENT_ID.format("") - - id_comment = self.parser.find_comments(search_comment, vhost.path) - if id_comment: - # Use the first value, multiple ones shouldn't exist - comment = self.parser.get_arg(id_comment[0]) - return comment.split(" ")[-1] - return None - - def add_vhost_id(self, vhost): - """Adds an unique ID to the VirtualHost as a comment for mapping back - to it on later invocations, as the config file order might have changed. - If ID already exists, returns that instead. - - :param vhost: Virtual host to add or find the id - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - :returns: The unique ID for vhost - :rtype: str or None - """ - - vh_id = self._find_vhost_id(vhost) - if vh_id: - return vh_id - - id_string = apache_util.unique_id() - comment = constants.MANAGED_COMMENT_ID.format(id_string) - self.parser.add_comment(vhost.path, comment) - return id_string - - def _escape(self, fp): - fp = fp.replace(",", "\\,") - fp = fp.replace("[", "\\[") - fp = fp.replace("]", "\\]") - fp = fp.replace("|", "\\|") - fp = fp.replace("=", "\\=") - fp = fp.replace("(", "\\(") - fp = fp.replace(")", "\\)") - fp = fp.replace("!", "\\!") - return fp - - ###################################################################### - # Enhancements - ###################################################################### - def supported_enhancements(self): # pylint: disable=no-self-use - """Returns currently supported enhancements.""" - return ["redirect", "ensure-http-header", "staple-ocsp"] - - def enhance(self, domain, enhancement, options=None): - """Enhance configuration. - - :param str domain: domain to enhance - :param str enhancement: enhancement type defined in - :const:`~certbot.constants.ENHANCEMENTS` - :param options: options for the enhancement - See :const:`~certbot.constants.ENHANCEMENTS` - documentation for appropriate parameter. - - :raises .errors.PluginError: If Enhancement is not supported, or if - there is any other problem with the enhancement. - - """ - try: - func = self._enhance_func[enhancement] - except KeyError: - raise errors.PluginError( - "Unsupported enhancement: {0}".format(enhancement)) - - matched_vhosts = self.choose_vhosts(domain, create_if_no_ssl=False) - # We should be handling only SSL vhosts for enhancements - vhosts = [vhost for vhost in matched_vhosts if vhost.ssl] - - if not vhosts: - msg_tmpl = ("Certbot was not able to find SSL VirtualHost for a " - "domain {0} for enabling enhancement \"{1}\". The requested " - "enhancement was not configured.") - msg_enhancement = enhancement - if options: - msg_enhancement += ": " + options - msg = msg_tmpl.format(domain, msg_enhancement) - logger.warning(msg) - raise errors.PluginError(msg) - try: - for vhost in vhosts: - func(vhost, options) - except errors.PluginError: - logger.warning("Failed %s for %s", enhancement, domain) - raise - - def _autohsts_increase(self, vhost, id_str, nextstep): - """Increase the AutoHSTS max-age value - - :param vhost: Virtual host object to modify - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - :param str id_str: The unique ID string of VirtualHost - - :param int nextstep: Next AutoHSTS max-age value index - - """ - nextstep_value = constants.AUTOHSTS_STEPS[nextstep] - self._autohsts_write(vhost, nextstep_value) - self._autohsts[id_str] = {"laststep": nextstep, "timestamp": time.time()} - - def _autohsts_write(self, vhost, nextstep_value): - """ - Write the new HSTS max-age value to the VirtualHost file - """ - - hsts_dirpath = None - header_path = self.parser.find_dir("Header", None, vhost.path) - if header_path: - pat = '(?:[ "]|^)(strict-transport-security)(?:[ "]|$)' - for match in header_path: - if re.search(pat, self.aug.get(match).lower()): - hsts_dirpath = match - if not hsts_dirpath: - err_msg = ("Certbot was unable to find the existing HSTS header " - "from the VirtualHost at path {0}.").format(vhost.filep) - raise errors.PluginError(err_msg) - - # Prepare the HSTS header value - hsts_maxage = "\"max-age={0}\"".format(nextstep_value) - - # Update the header - # Our match statement was for string strict-transport-security, but - # we need to update the value instead. The next index is for the value - hsts_dirpath = hsts_dirpath.replace("arg[3]", "arg[4]") - self.aug.set(hsts_dirpath, hsts_maxage) - note_msg = ("Increasing HSTS max-age value to {0} for VirtualHost " - "in {1}\n".format(nextstep_value, vhost.filep)) - logger.debug(note_msg) - self.save_notes += note_msg - self.save(note_msg) - - def _autohsts_fetch_state(self): - """ - Populates the AutoHSTS state from the pluginstorage - """ - try: - self._autohsts = self.storage.fetch("autohsts") - except KeyError: - self._autohsts = dict() - - def _autohsts_save_state(self): - """ - Saves the state of AutoHSTS object to pluginstorage - """ - self.storage.put("autohsts", self._autohsts) - self.storage.save() - - def _autohsts_vhost_in_lineage(self, vhost, lineage): - """ - Searches AutoHSTS managed VirtualHosts that belong to the lineage. - Matches the private key path. - """ - - return bool( - self.parser.find_dir("SSLCertificateKeyFile", - lineage.key_path, vhost.path)) - - def _enable_ocsp_stapling(self, ssl_vhost, unused_options): - """Enables OCSP Stapling - - In OCSP, each client (e.g. browser) would have to query the - OCSP Responder to validate that the site certificate was not revoked. - - Enabling OCSP Stapling, would allow the web-server to query the OCSP - Responder, and staple its response to the offered certificate during - TLS. i.e. clients would not have to query the OCSP responder. - - OCSP Stapling enablement on Apache implicitly depends on - SSLCertificateChainFile being set by other code. - - .. note:: This function saves the configuration - - :param ssl_vhost: Destination of traffic, an ssl enabled vhost - :type ssl_vhost: :class:`~certbot_apache.obj.VirtualHost` - - :param unused_options: Not currently used - :type unused_options: Not Available - - :returns: Success, general_vhost (HTTP vhost) - :rtype: (bool, :class:`~certbot_apache.obj.VirtualHost`) - - """ - min_apache_ver = (2, 3, 3) - if self.get_version() < min_apache_ver: - raise errors.PluginError( - "Unable to set OCSP directives.\n" - "Apache version is below 2.3.3.") - - if "socache_shmcb_module" not in self.parser.modules: - self.enable_mod("socache_shmcb") - - # Check if there's an existing SSLUseStapling directive on. - use_stapling_aug_path = self.parser.find_dir("SSLUseStapling", - "on", start=ssl_vhost.path) - if not use_stapling_aug_path: - self.parser.add_dir(ssl_vhost.path, "SSLUseStapling", "on") - - ssl_vhost_aug_path = self._escape(parser.get_aug_path(ssl_vhost.filep)) - - # Check if there's an existing SSLStaplingCache directive. - stapling_cache_aug_path = self.parser.find_dir('SSLStaplingCache', - None, ssl_vhost_aug_path) - - # We'll simply delete the directive, so that we'll have a - # consistent OCSP cache path. - if stapling_cache_aug_path: - self.aug.remove( - re.sub(r"/\w*$", "", stapling_cache_aug_path[0])) - - self.parser.add_dir_to_ifmodssl(ssl_vhost_aug_path, - "SSLStaplingCache", - ["shmcb:/var/run/apache2/stapling_cache(128000)"]) - - msg = "OCSP Stapling was enabled on SSL Vhost: %s.\n"%( - ssl_vhost.filep) - self.save_notes += msg - self.save() - logger.info(msg) - - def _set_http_header(self, ssl_vhost, header_substring): - """Enables header that is identified by header_substring on ssl_vhost. - - If the header identified by header_substring is not already set, - a new Header directive is placed in ssl_vhost's configuration with - arguments from: constants.HTTP_HEADER[header_substring] - - .. note:: This function saves the configuration - - :param ssl_vhost: Destination of traffic, an ssl enabled vhost - :type ssl_vhost: :class:`~certbot_apache.obj.VirtualHost` - - :param header_substring: string that uniquely identifies a header. - e.g: Strict-Transport-Security, Upgrade-Insecure-Requests. - :type str - - :returns: Success, general_vhost (HTTP vhost) - :rtype: (bool, :class:`~certbot_apache.obj.VirtualHost`) - - :raises .errors.PluginError: If no viable HTTP host can be created or - set with header header_substring. - - """ - if "headers_module" not in self.parser.modules: - self.enable_mod("headers") - - # Check if selected header is already set - self._verify_no_matching_http_header(ssl_vhost, header_substring) - - # Add directives to server - self.parser.add_dir(ssl_vhost.path, "Header", - constants.HEADER_ARGS[header_substring]) - - self.save_notes += ("Adding %s header to ssl vhost in %s\n" % - (header_substring, ssl_vhost.filep)) - - self.save() - logger.info("Adding %s header to ssl vhost in %s", header_substring, - ssl_vhost.filep) - - def _verify_no_matching_http_header(self, ssl_vhost, header_substring): - """Checks to see if an there is an existing Header directive that - contains the string header_substring. - - :param ssl_vhost: vhost to check - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - :param header_substring: string that uniquely identifies a header. - e.g: Strict-Transport-Security, Upgrade-Insecure-Requests. - :type str - - :returns: boolean - :rtype: (bool) - - :raises errors.PluginEnhancementAlreadyPresent When header - header_substring exists - - """ - header_path = self.parser.find_dir("Header", None, - start=ssl_vhost.path) - if header_path: - # "Existing Header directive for virtualhost" - pat = '(?:[ "]|^)(%s)(?:[ "]|$)' % (header_substring.lower()) - for match in header_path: - if re.search(pat, self.aug.get(match).lower()): - raise errors.PluginEnhancementAlreadyPresent( - "Existing %s header" % (header_substring)) - - def _enable_redirect(self, ssl_vhost, unused_options): - """Redirect all equivalent HTTP traffic to ssl_vhost. - - .. todo:: This enhancement should be rewritten and will - unfortunately require lots of debugging by hand. - - Adds Redirect directive to the port 80 equivalent of ssl_vhost - First the function attempts to find the vhost with equivalent - ip addresses that serves on non-ssl ports - The function then adds the directive - - .. note:: This function saves the configuration - - :param ssl_vhost: Destination of traffic, an ssl enabled vhost - :type ssl_vhost: :class:`~certbot_apache.obj.VirtualHost` - - :param unused_options: Not currently used - :type unused_options: Not Available - - :raises .errors.PluginError: If no viable HTTP host can be created or - used for the redirect. - - """ - if "rewrite_module" not in self.parser.modules: - self.enable_mod("rewrite") - general_vh = self._get_http_vhost(ssl_vhost) - - if general_vh is None: - # Add virtual_server with redirect - logger.debug("Did not find http version of ssl virtual host " - "attempting to create") - redirect_addrs = self._get_proposed_addrs(ssl_vhost) - for vhost in self.vhosts: - if vhost.enabled and vhost.conflicts(redirect_addrs): - raise errors.PluginError( - "Unable to find corresponding HTTP vhost; " - "Unable to create one as intended addresses conflict; " - "Current configuration does not support automated " - "redirection") - self._create_redirect_vhost(ssl_vhost) - else: - if general_vh in self._enhanced_vhosts["redirect"]: - logger.debug("Already enabled redirect for this vhost") - return - - # Check if Certbot redirection already exists - self._verify_no_certbot_redirect(general_vh) - - # Note: if code flow gets here it means we didn't find the exact - # certbot RewriteRule config for redirection. Finding - # another RewriteRule is likely to be fine in most or all cases, - # but redirect loops are possible in very obscure cases; see #1620 - # for reasoning. - if self._is_rewrite_exists(general_vh): - logger.warning("Added an HTTP->HTTPS rewrite in addition to " - "other RewriteRules; you may wish to check for " - "overall consistency.") - - # Add directives to server - # Note: These are not immediately searchable in sites-enabled - # even with save() and load() - if not self._is_rewrite_engine_on(general_vh): - self.parser.add_dir(general_vh.path, "RewriteEngine", "on") - - names = ssl_vhost.get_names() - for idx, name in enumerate(names): - args = ["%{SERVER_NAME}", "={0}".format(name), "[OR]"] - if idx == len(names) - 1: - args.pop() - self.parser.add_dir(general_vh.path, "RewriteCond", args) - - self._set_https_redirection_rewrite_rule(general_vh) - - self.save_notes += ("Redirecting host in %s to ssl vhost in %s\n" % - (general_vh.filep, ssl_vhost.filep)) - self.save() - - self._enhanced_vhosts["redirect"].add(general_vh) - logger.info("Redirecting vhost in %s to ssl vhost in %s", - general_vh.filep, ssl_vhost.filep) - - def _set_https_redirection_rewrite_rule(self, vhost): - if self.get_version() >= (2, 3, 9): - self.parser.add_dir(vhost.path, "RewriteRule", - constants.REWRITE_HTTPS_ARGS_WITH_END) - else: - self.parser.add_dir(vhost.path, "RewriteRule", - constants.REWRITE_HTTPS_ARGS) - - - def _verify_no_certbot_redirect(self, vhost): - """Checks to see if a redirect was already installed by certbot. - - Checks to see if virtualhost already contains a rewrite rule that is - identical to Certbot's redirection rewrite rule. - - For graceful transition to new rewrite rules for HTTPS redireciton we - delete certbot's old rewrite rules and set the new one instead. - - :param vhost: vhost to check - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - :raises errors.PluginEnhancementAlreadyPresent: When the exact - certbot redirection WriteRule exists in virtual host. - """ - rewrite_path = self.parser.find_dir( - "RewriteRule", None, start=vhost.path) - - # There can be other RewriteRule directive lines in vhost config. - # rewrite_args_dict keys are directive ids and the corresponding value - # for each is a list of arguments to that directive. - rewrite_args_dict = defaultdict(list) # type: DefaultDict[str, List[str]] - pat = r'(.*directive\[\d+\]).*' - for match in rewrite_path: - m = re.match(pat, match) - if m: - dir_path = m.group(1) - rewrite_args_dict[dir_path].append(match) - - if rewrite_args_dict: - redirect_args = [constants.REWRITE_HTTPS_ARGS, - constants.REWRITE_HTTPS_ARGS_WITH_END] - - for dir_path, args_paths in rewrite_args_dict.items(): - arg_vals = [self.aug.get(x) for x in args_paths] - - # Search for past redirection rule, delete it, set the new one - if arg_vals in constants.OLD_REWRITE_HTTPS_ARGS: - self.aug.remove(dir_path) - self._set_https_redirection_rewrite_rule(vhost) - self.save() - raise errors.PluginEnhancementAlreadyPresent( - "Certbot has already enabled redirection") - - if arg_vals in redirect_args: - raise errors.PluginEnhancementAlreadyPresent( - "Certbot has already enabled redirection") - - def _is_rewrite_exists(self, vhost): - """Checks if there exists a RewriteRule directive in vhost - - :param vhost: vhost to check - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - :returns: True if a RewriteRule directive exists. - :rtype: bool - - """ - rewrite_path = self.parser.find_dir( - "RewriteRule", None, start=vhost.path) - return bool(rewrite_path) - - def _is_rewrite_engine_on(self, vhost): - """Checks if a RewriteEngine directive is on - - :param vhost: vhost to check - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - """ - rewrite_engine_path_list = self.parser.find_dir("RewriteEngine", "on", - start=vhost.path) - if rewrite_engine_path_list: - for re_path in rewrite_engine_path_list: - # A RewriteEngine directive may also be included in per - # directory .htaccess files. We only care about the VirtualHost. - if 'virtualhost' in re_path.lower(): - return self.parser.get_arg(re_path) - return False - - def _create_redirect_vhost(self, ssl_vhost): - """Creates an http_vhost specifically to redirect for the ssl_vhost. - - :param ssl_vhost: ssl vhost - :type ssl_vhost: :class:`~certbot_apache.obj.VirtualHost` - - :returns: tuple of the form - (`success`, :class:`~certbot_apache.obj.VirtualHost`) - :rtype: tuple - - """ - text = self._get_redirect_config_str(ssl_vhost) - - redirect_filepath = self._write_out_redirect(ssl_vhost, text) - - self.aug.load() - # Make a new vhost data structure and add it to the lists - new_vhost = self._create_vhost(parser.get_aug_path(self._escape(redirect_filepath))) - self.vhosts.append(new_vhost) - self._enhanced_vhosts["redirect"].add(new_vhost) - - # Finally create documentation for the change - self.save_notes += ("Created a port 80 vhost, %s, for redirection to " - "ssl vhost %s\n" % - (new_vhost.filep, ssl_vhost.filep)) - - def _get_redirect_config_str(self, ssl_vhost): - # get servernames and serveraliases - serveralias = "" - servername = "" - - if ssl_vhost.name is not None: - servername = "ServerName " + ssl_vhost.name - if ssl_vhost.aliases: - serveralias = "ServerAlias " + " ".join(ssl_vhost.aliases) - - rewrite_rule_args = [] # type: List[str] - if self.get_version() >= (2, 3, 9): - rewrite_rule_args = constants.REWRITE_HTTPS_ARGS_WITH_END - else: - rewrite_rule_args = constants.REWRITE_HTTPS_ARGS - - return ("\n" - "%s \n" - "%s \n" - "ServerSignature Off\n" - "\n" - "RewriteEngine On\n" - "RewriteRule %s\n" - "\n" - "ErrorLog %s/redirect.error.log\n" - "LogLevel warn\n" - "\n" - % (" ".join(str(addr) for - addr in self._get_proposed_addrs(ssl_vhost)), - servername, serveralias, - " ".join(rewrite_rule_args), - self.option("logs_root"))) - - def _write_out_redirect(self, ssl_vhost, text): - # This is the default name - redirect_filename = "le-redirect.conf" - - # See if a more appropriate name can be applied - if ssl_vhost.name is not None: - # make sure servername doesn't exceed filename length restriction - if len(ssl_vhost.name) < (255 - (len(redirect_filename) + 1)): - redirect_filename = "le-redirect-%s.conf" % ssl_vhost.name - - redirect_filepath = os.path.join(self.option("vhost_root"), - redirect_filename) - - # Register the new file that will be created - # Note: always register the creation before writing to ensure file will - # be removed in case of unexpected program exit - self.reverter.register_file_creation(False, redirect_filepath) - - # Write out file - with open(redirect_filepath, "w") as redirect_file: - redirect_file.write(text) - - # Add new include to configuration if it doesn't exist yet - if not self.parser.parsed_in_current(redirect_filepath): - self.parser.parse_file(redirect_filepath) - - logger.info("Created redirect file: %s", redirect_filename) - - return redirect_filepath - - def _get_http_vhost(self, ssl_vhost): - """Find appropriate HTTP vhost for ssl_vhost.""" - # First candidate vhosts filter - if ssl_vhost.ancestor: - return ssl_vhost.ancestor - candidate_http_vhs = [ - vhost for vhost in self.vhosts if not vhost.ssl - ] - - # Second filter - check addresses - for http_vh in candidate_http_vhs: - if http_vh.same_server(ssl_vhost): - return http_vh - # Third filter - if none with same names, return generic - for http_vh in candidate_http_vhs: - if http_vh.same_server(ssl_vhost, generic=True): - return http_vh - - return None - - def _get_proposed_addrs(self, vhost, port="80"): - """Return all addrs of vhost with the port replaced with the specified. - - :param obj.VirtualHost ssl_vhost: Original Vhost - :param str port: Desired port for new addresses - - :returns: `set` of :class:`~obj.Addr` - - """ - redirects = set() - for addr in vhost.addrs: - redirects.add(addr.get_addr_obj(port)) - - return redirects - - def enable_site(self, vhost): - """Enables an available site, Apache reload required. - - .. note:: Does not make sure that the site correctly works or that all - modules are enabled appropriately. - .. note:: The distribution specific override replaces functionality - of this method where available. - - :param vhost: vhost to enable - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - :raises .errors.NotSupportedError: If filesystem layout is not - supported. - - """ - if vhost.enabled: - return - - if not self.parser.parsed_in_original(vhost.filep): - # Add direct include to root conf - logger.info("Enabling site %s by adding Include to root configuration", - vhost.filep) - self.save_notes += "Enabled site %s\n" % vhost.filep - self.parser.add_include(self.parser.loc["default"], vhost.filep) - vhost.enabled = True - return - - def enable_mod(self, mod_name, temp=False): # pylint: disable=unused-argument - """Enables module in Apache. - - Both enables and reloads Apache so module is active. - - :param str mod_name: Name of the module to enable. (e.g. 'ssl') - :param bool temp: Whether or not this is a temporary action. - - .. note:: The distribution specific override replaces functionality - of this method where available. - - :raises .errors.MisconfigurationError: We cannot enable modules in - generic fashion. - - """ - mod_message = ("Apache needs to have module \"{0}\" active for the " + - "requested installation options. Unfortunately Certbot is unable " + - "to install or enable it for you. Please install the module, and " + - "run Certbot again.") - raise errors.MisconfigurationError(mod_message.format(mod_name)) - - def restart(self): - """Runs a config test and reloads the Apache server. - - :raises .errors.MisconfigurationError: If either the config test - or reload fails. - - """ - self.config_test() - self._reload() - - def _reload(self): - """Reloads the Apache server. - - :raises .errors.MisconfigurationError: If reload fails - - """ - error = "" - try: - util.run_script(self.option("restart_cmd")) - except errors.SubprocessError as err: - logger.info("Unable to restart apache using %s", - self.option("restart_cmd")) - alt_restart = self.option("restart_cmd_alt") - if alt_restart: - logger.debug("Trying alternative restart command: %s", - alt_restart) - # There is an alternative restart command available - # This usually is "restart" verb while original is "graceful" - try: - util.run_script(self.option( - "restart_cmd_alt")) - return - except errors.SubprocessError as secerr: - error = str(secerr) - else: - error = str(err) - raise errors.MisconfigurationError(error) - - def config_test(self): # pylint: disable=no-self-use - """Check the configuration of Apache for errors. - - :raises .errors.MisconfigurationError: If config_test fails - - """ - try: - util.run_script(self.option("conftest_cmd")) - except errors.SubprocessError as err: - raise errors.MisconfigurationError(str(err)) - - def get_version(self): - """Return version of Apache Server. - - Version is returned as tuple. (ie. 2.4.7 = (2, 4, 7)) - - :returns: version - :rtype: tuple - - :raises .PluginError: if unable to find Apache version - - """ - try: - stdout, _ = util.run_script(self.option("version_cmd")) - except errors.SubprocessError: - raise errors.PluginError( - "Unable to run %s -v" % - self.option("version_cmd")) - - regex = re.compile(r"Apache/([0-9\.]*)", re.IGNORECASE) - matches = regex.findall(stdout) - - if len(matches) != 1: - raise errors.PluginError("Unable to find Apache version") - - return tuple([int(i) for i in matches[0].split(".")]) - - def more_info(self): - """Human-readable string to help understand the module""" - return ( - "Configures Apache to authenticate and install HTTPS.{0}" - "Server root: {root}{0}" - "Version: {version}".format( - os.linesep, root=self.parser.loc["root"], - version=".".join(str(i) for i in self.version)) - ) - - ########################################################################### - # Challenges Section - ########################################################################### - def get_chall_pref(self, unused_domain): # pylint: disable=no-self-use - """Return list of challenge preferences.""" - return [challenges.HTTP01, challenges.TLSSNI01] - - def perform(self, achalls): - """Perform the configuration related challenge. - - This function currently assumes all challenges will be fulfilled. - If this turns out not to be the case in the future. Cleanup and - outstanding challenges will have to be designed better. - - """ - self._chall_out.update(achalls) - responses = [None] * len(achalls) - http_doer = http_01.ApacheHttp01(self) - sni_doer = tls_sni_01.ApacheTlsSni01(self) - - for i, achall in enumerate(achalls): - # Currently also have chall_doer hold associated index of the - # challenge. This helps to put all of the responses back together - # when they are all complete. - if isinstance(achall.chall, challenges.HTTP01): - http_doer.add_chall(achall, i) - else: # tls-sni-01 - sni_doer.add_chall(achall, i) - - http_response = http_doer.perform() - sni_response = sni_doer.perform() - if http_response or sni_response: - # Must reload in order to activate the challenges. - # Handled here because we may be able to load up other challenge - # types - self.restart() - - # TODO: Remove this dirty hack. We need to determine a reliable way - # of identifying when the new configuration is being used. - time.sleep(3) - - self._update_responses(responses, http_response, http_doer) - self._update_responses(responses, sni_response, sni_doer) - - return responses - - def _update_responses(self, responses, chall_response, chall_doer): - # Go through all of the challenges and assign them to the proper - # place in the responses return value. All responses must be in the - # same order as the original challenges. - for i, resp in enumerate(chall_response): - responses[chall_doer.indices[i]] = resp - - def cleanup(self, achalls): - """Revert all challenges.""" - self._chall_out.difference_update(achalls) - - # If all of the challenges have been finished, clean up everything - if not self._chall_out: - self.revert_challenge_config() - self.restart() - self.parser.reset_modules() - - def install_ssl_options_conf(self, options_ssl, options_ssl_digest): - """Copy Certbot's SSL options file into the system's config dir if required.""" - - # XXX if we ever try to enforce a local privilege boundary (eg, running - # certbot for unprivileged users via setuid), this function will need - # to be modified. - return common.install_version_controlled_file(options_ssl, options_ssl_digest, - self.option("MOD_SSL_CONF_SRC"), constants.ALL_SSL_OPTIONS_HASHES) - - def enable_autohsts(self, _unused_lineage, domains): - """ - Enable the AutoHSTS enhancement for defined domains - - :param _unused_lineage: Certificate lineage object, unused - :type _unused_lineage: certbot.storage.RenewableCert - - :param domains: List of domains in certificate to enhance - :type domains: str - """ - - self._autohsts_fetch_state() - _enhanced_vhosts = [] - for d in domains: - matched_vhosts = self.choose_vhosts(d, create_if_no_ssl=False) - # We should be handling only SSL vhosts for AutoHSTS - vhosts = [vhost for vhost in matched_vhosts if vhost.ssl] - - if not vhosts: - msg_tmpl = ("Certbot was not able to find SSL VirtualHost for a " - "domain {0} for enabling AutoHSTS enhancement.") - msg = msg_tmpl.format(d) - logger.warning(msg) - raise errors.PluginError(msg) - for vh in vhosts: - try: - self._enable_autohsts_domain(vh) - _enhanced_vhosts.append(vh) - except errors.PluginEnhancementAlreadyPresent: - if vh in _enhanced_vhosts: - continue - msg = ("VirtualHost for domain {0} in file {1} has a " + - "String-Transport-Security header present, exiting.") - raise errors.PluginEnhancementAlreadyPresent( - msg.format(d, vh.filep)) - if _enhanced_vhosts: - note_msg = "Enabling AutoHSTS" - self.save(note_msg) - logger.info(note_msg) - self.restart() - - # Save the current state to pluginstorage - self._autohsts_save_state() - - def _enable_autohsts_domain(self, ssl_vhost): - """Do the initial AutoHSTS deployment to a vhost - - :param ssl_vhost: The VirtualHost object to deploy the AutoHSTS - :type ssl_vhost: :class:`~certbot_apache.obj.VirtualHost` or None - - :raises errors.PluginEnhancementAlreadyPresent: When already enhanced - - """ - # This raises the exception - self._verify_no_matching_http_header(ssl_vhost, - "Strict-Transport-Security") - - if "headers_module" not in self.parser.modules: - self.enable_mod("headers") - # Prepare the HSTS header value - hsts_header = constants.HEADER_ARGS["Strict-Transport-Security"][:-1] - initial_maxage = constants.AUTOHSTS_STEPS[0] - hsts_header.append("\"max-age={0}\"".format(initial_maxage)) - - # Add ID to the VirtualHost for mapping back to it later - uniq_id = self.add_vhost_id(ssl_vhost) - self.save_notes += "Adding unique ID {0} to VirtualHost in {1}\n".format( - uniq_id, ssl_vhost.filep) - # Add the actual HSTS header - self.parser.add_dir(ssl_vhost.path, "Header", hsts_header) - note_msg = ("Adding gradually increasing HSTS header with initial value " - "of {0} to VirtualHost in {1}\n".format( - initial_maxage, ssl_vhost.filep)) - self.save_notes += note_msg - - # Save the current state to pluginstorage - self._autohsts[uniq_id] = {"laststep": 0, "timestamp": time.time()} - - def update_autohsts(self, _unused_domain): - """ - Increase the AutoHSTS values of VirtualHosts that the user has enabled - this enhancement for. - - :param _unused_domain: Not currently used - :type _unused_domain: Not Available - - """ - self._autohsts_fetch_state() - if not self._autohsts: - # No AutoHSTS enabled for any domain - return - curtime = time.time() - save_and_restart = False - for id_str, config in list(self._autohsts.items()): - if config["timestamp"] + constants.AUTOHSTS_FREQ > curtime: - # Skip if last increase was < AUTOHSTS_FREQ ago - continue - nextstep = config["laststep"] + 1 - if nextstep < len(constants.AUTOHSTS_STEPS): - # If installer hasn't been prepared yet, do it now - if not self._prepared: - self.prepare() - # Have not reached the max value yet - try: - vhost = self.find_vhost_by_id(id_str) - except errors.PluginError: - msg = ("Could not find VirtualHost with ID {0}, disabling " - "AutoHSTS for this VirtualHost").format(id_str) - logger.warning(msg) - # Remove the orphaned AutoHSTS entry from pluginstorage - self._autohsts.pop(id_str) - continue - self._autohsts_increase(vhost, id_str, nextstep) - msg = ("Increasing HSTS max-age value for VirtualHost with id " - "{0}").format(id_str) - self.save_notes += msg - save_and_restart = True - - if save_and_restart: - self.save("Increased HSTS max-age values") - self.restart() - - self._autohsts_save_state() - - def deploy_autohsts(self, lineage): - """ - Checks if autohsts vhost has reached maximum auto-increased value - and changes the HSTS max-age to a high value. - - :param lineage: Certificate lineage object - :type lineage: certbot.storage.RenewableCert - """ - self._autohsts_fetch_state() - if not self._autohsts: - # No autohsts enabled for any vhost - return - - vhosts = [] - affected_ids = [] - # Copy, as we are removing from the dict inside the loop - for id_str, config in list(self._autohsts.items()): - if config["laststep"]+1 >= len(constants.AUTOHSTS_STEPS): - # max value reached, try to make permanent - try: - vhost = self.find_vhost_by_id(id_str) - except errors.PluginError: - msg = ("VirtualHost with id {} was not found, unable to " - "make HSTS max-age permanent.").format(id_str) - logger.warning(msg) - self._autohsts.pop(id_str) - continue - if self._autohsts_vhost_in_lineage(vhost, lineage): - vhosts.append(vhost) - affected_ids.append(id_str) - - save_and_restart = False - for vhost in vhosts: - self._autohsts_write(vhost, constants.AUTOHSTS_PERMANENT) - msg = ("Strict-Transport-Security max-age value for " - "VirtualHost in {0} was made permanent.").format(vhost.filep) - logger.debug(msg) - self.save_notes += msg+"\n" - save_and_restart = True - - if save_and_restart: - self.save("Made HSTS max-age permanent") - self.restart() - - for id_str in affected_ids: - self._autohsts.pop(id_str) - - # Update AutoHSTS storage (We potentially removed vhosts from managed) - self._autohsts_save_state() - - -AutoHSTSEnhancement.register(ApacheConfigurator) # pylint: disable=no-member diff --git a/certbot-apache/certbot_apache/constants.py b/certbot-apache/certbot_apache/constants.py deleted file mode 100644 index 23a7b7afd03..00000000000 --- a/certbot-apache/certbot_apache/constants.py +++ /dev/null @@ -1,63 +0,0 @@ -"""Apache plugin constants.""" -import pkg_resources - - -MOD_SSL_CONF_DEST = "options-ssl-apache.conf" -"""Name of the mod_ssl config file as saved in `IConfig.config_dir`.""" - - -UPDATED_MOD_SSL_CONF_DIGEST = ".updated-options-ssl-apache-conf-digest.txt" -"""Name of the hash of the updated or informed mod_ssl_conf as saved in `IConfig.config_dir`.""" - -ALL_SSL_OPTIONS_HASHES = [ - '2086bca02db48daf93468332543c60ac6acdb6f0b58c7bfdf578a5d47092f82a', - '4844d36c9a0f587172d9fa10f4f1c9518e3bcfa1947379f155e16a70a728c21a', - '5a922826719981c0a234b1fbcd495f3213e49d2519e845ea0748ba513044b65b', - '4066b90268c03c9ba0201068eaa39abbc02acf9558bb45a788b630eb85dadf27', - 'f175e2e7c673bd88d0aff8220735f385f916142c44aa83b09f1df88dd4767a88', - 'cfdd7c18d2025836ea3307399f509cfb1ebf2612c87dd600a65da2a8e2f2797b', - '80720bd171ccdc2e6b917ded340defae66919e4624962396b992b7218a561791', - 'c0c022ea6b8a51ecc8f1003d0a04af6c3f2bc1c3ce506b3c2dfc1f11ef931082', -] -"""SHA256 hashes of the contents of previous versions of all versions of MOD_SSL_CONF_SRC""" - -AUGEAS_LENS_DIR = pkg_resources.resource_filename( - "certbot_apache", "augeas_lens") -"""Path to the Augeas lens directory""" - -REWRITE_HTTPS_ARGS = [ - "^", "https://%{SERVER_NAME}%{REQUEST_URI}", "[L,NE,R=permanent]"] -"""Apache version<2.3.9 rewrite rule arguments used for redirections to -https vhost""" - -REWRITE_HTTPS_ARGS_WITH_END = [ - "^", "https://%{SERVER_NAME}%{REQUEST_URI}", "[END,NE,R=permanent]"] -"""Apache version >= 2.3.9 rewrite rule arguments used for redirections to - https vhost""" - -OLD_REWRITE_HTTPS_ARGS = [ - ["^", "https://%{SERVER_NAME}%{REQUEST_URI}", "[L,QSA,R=permanent]"], - ["^", "https://%{SERVER_NAME}%{REQUEST_URI}", "[END,QSA,R=permanent]"]] - -HSTS_ARGS = ["always", "set", "Strict-Transport-Security", - "\"max-age=31536000\""] -"""Apache header arguments for HSTS""" - -UIR_ARGS = ["always", "set", "Content-Security-Policy", - "upgrade-insecure-requests"] - -HEADER_ARGS = {"Strict-Transport-Security": HSTS_ARGS, - "Upgrade-Insecure-Requests": UIR_ARGS} - -AUTOHSTS_STEPS = [60, 300, 900, 3600, 21600, 43200, 86400] -"""AutoHSTS increase steps: 1min, 5min, 15min, 1h, 6h, 12h, 24h""" - -AUTOHSTS_PERMANENT = 31536000 -"""Value for the last max-age of HSTS""" - -AUTOHSTS_FREQ = 172800 -"""Minimum time since last increase to perform a new one: 48h""" - -MANAGED_COMMENT = "DO NOT REMOVE - Managed by Certbot" -MANAGED_COMMENT_ID = MANAGED_COMMENT+", VirtualHost id: {0}" -"""Managed by Certbot comments and the VirtualHost identification template""" diff --git a/certbot-apache/certbot_apache/display_ops.py b/certbot-apache/certbot_apache/display_ops.py deleted file mode 100644 index db1c3cca4b8..00000000000 --- a/certbot-apache/certbot_apache/display_ops.py +++ /dev/null @@ -1,128 +0,0 @@ -"""Contains UI methods for Apache operations.""" -import logging -import os - -import zope.component - -from certbot import errors -from certbot import interfaces - -import certbot.display.util as display_util - - -logger = logging.getLogger(__name__) - - -def select_vhost_multiple(vhosts): - """Select multiple Vhosts to install the certificate for - - :param vhosts: Available Apache VirtualHosts - :type vhosts: :class:`list` of type `~obj.Vhost` - - :returns: List of VirtualHosts - :rtype: :class:`list`of type `~obj.Vhost` - """ - if not vhosts: - return list() - tags_list = [vhost.display_repr()+"\n" for vhost in vhosts] - # Remove the extra newline from the last entry - if len(tags_list): - tags_list[-1] = tags_list[-1][:-1] - code, names = zope.component.getUtility(interfaces.IDisplay).checklist( - "Which VirtualHosts would you like to install the wildcard certificate for?", - tags=tags_list, force_interactive=True) - if code == display_util.OK: - return_vhosts = _reversemap_vhosts(names, vhosts) - return return_vhosts - return [] - -def _reversemap_vhosts(names, vhosts): - """Helper function for select_vhost_multiple for mapping string - representations back to actual vhost objects""" - return_vhosts = list() - - for selection in names: - for vhost in vhosts: - if vhost.display_repr().strip() == selection.strip(): - return_vhosts.append(vhost) - return return_vhosts - -def select_vhost(domain, vhosts): - """Select an appropriate Apache Vhost. - - :param vhosts: Available Apache VirtualHosts - :type vhosts: :class:`list` of type `~obj.Vhost` - - :returns: VirtualHost or `None` - :rtype: `~obj.Vhost` or `None` - - """ - if not vhosts: - return None - code, tag = _vhost_menu(domain, vhosts) - if code == display_util.OK: - return vhosts[tag] - else: - return None - -def _vhost_menu(domain, vhosts): - """Select an appropriate Apache Vhost. - - :param vhosts: Available Apache Virtual Hosts - :type vhosts: :class:`list` of type `~obj.Vhost` - - :returns: Display tuple - ('code', tag') - :rtype: `tuple` - - """ - # Free characters in the line of display text (9 is for ' | ' formatting) - free_chars = display_util.WIDTH - len("HTTPS") - len("Enabled") - 9 - - if free_chars < 2: - logger.debug("Display size is too small for " - "certbot_apache.display_ops._vhost_menu()") - # This runs the edge off the screen, but it doesn't cause an "error" - filename_size = 1 - disp_name_size = 1 - else: - # Filename is a bit more important and probably longer with 000-* - filename_size = int(free_chars * .6) - disp_name_size = free_chars - filename_size - - choices = [] - for vhost in vhosts: - if len(vhost.get_names()) == 1: - disp_name = next(iter(vhost.get_names())) - elif len(vhost.get_names()) == 0: - disp_name = "" - else: - disp_name = "Multiple Names" - - choices.append( - "{fn:{fn_size}s} | {name:{name_size}s} | {https:5s} | " - "{active:7s}".format( - fn=os.path.basename(vhost.filep)[:filename_size], - name=disp_name[:disp_name_size], - https="HTTPS" if vhost.ssl else "", - active="Enabled" if vhost.enabled else "", - fn_size=filename_size, - name_size=disp_name_size) - ) - - try: - code, tag = zope.component.getUtility(interfaces.IDisplay).menu( - "We were unable to find a vhost with a ServerName " - "or Address of {0}.{1}Which virtual host would you " - "like to choose?".format(domain, os.linesep), - choices, force_interactive=True) - except errors.MissingCommandlineFlag: - msg = ( - "Encountered vhost ambiguity when trying to find a vhost for " - "{0} but was unable to ask for user " - "guidance in non-interactive mode. Certbot may need " - "vhosts to be explicitly labelled with ServerName or " - "ServerAlias directives.".format(domain)) - logger.warning(msg) - raise errors.MissingCommandlineFlag(msg) - - return code, tag diff --git a/certbot-apache/certbot_apache/entrypoint.py b/certbot-apache/certbot_apache/entrypoint.py deleted file mode 100644 index 6f144350711..00000000000 --- a/certbot-apache/certbot_apache/entrypoint.py +++ /dev/null @@ -1,48 +0,0 @@ -""" Entry point for Apache Plugin """ -from certbot import util - -from certbot_apache import configurator -from certbot_apache import override_arch -from certbot_apache import override_darwin -from certbot_apache import override_debian -from certbot_apache import override_centos -from certbot_apache import override_gentoo -from certbot_apache import override_suse - -OVERRIDE_CLASSES = { - "arch": override_arch.ArchConfigurator, - "darwin": override_darwin.DarwinConfigurator, - "debian": override_debian.DebianConfigurator, - "ubuntu": override_debian.DebianConfigurator, - "centos": override_centos.CentOSConfigurator, - "centos linux": override_centos.CentOSConfigurator, - "fedora": override_centos.CentOSConfigurator, - "ol": override_centos.CentOSConfigurator, - "red hat enterprise linux server": override_centos.CentOSConfigurator, - "rhel": override_centos.CentOSConfigurator, - "amazon": override_centos.CentOSConfigurator, - "gentoo": override_gentoo.GentooConfigurator, - "gentoo base system": override_gentoo.GentooConfigurator, - "opensuse": override_suse.OpenSUSEConfigurator, - "suse": override_suse.OpenSUSEConfigurator, -} - -def get_configurator(): - """ Get correct configurator class based on the OS fingerprint """ - os_info = util.get_os_info() - override_class = None - try: - override_class = OVERRIDE_CLASSES[os_info[0].lower()] - except KeyError: - # OS not found in the list - os_like = util.get_systemd_os_like() - if os_like: - for os_name in os_like: - if os_name in OVERRIDE_CLASSES.keys(): - override_class = OVERRIDE_CLASSES[os_name] - if not override_class: - # No override class found, return the generic configurator - override_class = configurator.ApacheConfigurator - return override_class - -ENTRYPOINT = get_configurator() diff --git a/certbot-apache/certbot_apache/http_01.py b/certbot-apache/certbot_apache/http_01.py deleted file mode 100644 index 22598bacaa0..00000000000 --- a/certbot-apache/certbot_apache/http_01.py +++ /dev/null @@ -1,181 +0,0 @@ -"""A class that performs HTTP-01 challenges for Apache""" -import logging -import os - -from acme.magic_typing import Set # pylint: disable=unused-import, no-name-in-module -from certbot import errors -from certbot.plugins import common -from certbot_apache.obj import VirtualHost # pylint: disable=unused-import -from certbot_apache.parser import get_aug_path - -logger = logging.getLogger(__name__) - -class ApacheHttp01(common.TLSSNI01): - """Class that performs HTTP-01 challenges within the Apache configurator.""" - - CONFIG_TEMPLATE22_PRE = """\ - RewriteEngine on - RewriteRule ^/\\.well-known/acme-challenge/([A-Za-z0-9-_=]+)$ {0}/$1 [L] - - """ - CONFIG_TEMPLATE22_POST = """\ - - Order Allow,Deny - Allow from all - - - Order Allow,Deny - Allow from all - - """ - - CONFIG_TEMPLATE24_PRE = """\ - RewriteEngine on - RewriteRule ^/\\.well-known/acme-challenge/([A-Za-z0-9-_=]+)$ {0}/$1 [END] - """ - CONFIG_TEMPLATE24_POST = """\ - - Require all granted - - - Require all granted - - """ - - def __init__(self, *args, **kwargs): - super(ApacheHttp01, self).__init__(*args, **kwargs) - self.challenge_conf_pre = os.path.join( - self.configurator.conf("challenge-location"), - "le_http_01_challenge_pre.conf") - self.challenge_conf_post = os.path.join( - self.configurator.conf("challenge-location"), - "le_http_01_challenge_post.conf") - self.challenge_dir = os.path.join( - self.configurator.config.work_dir, - "http_challenges") - self.moded_vhosts = set() # type: Set[VirtualHost] - - def perform(self): - """Perform all HTTP-01 challenges.""" - if not self.achalls: - return [] - # Save any changes to the configuration as a precaution - # About to make temporary changes to the config - self.configurator.save("Changes before challenge setup", True) - - self.configurator.ensure_listen(str( - self.configurator.config.http01_port)) - self.prepare_http01_modules() - - responses = self._set_up_challenges() - - self._mod_config() - # Save reversible changes - self.configurator.save("HTTP Challenge", True) - - return responses - - def prepare_http01_modules(self): - """Make sure that we have the needed modules available for http01""" - - if self.configurator.conf("handle-modules"): - needed_modules = ["rewrite"] - if self.configurator.version < (2, 4): - needed_modules.append("authz_host") - else: - needed_modules.append("authz_core") - for mod in needed_modules: - if mod + "_module" not in self.configurator.parser.modules: - self.configurator.enable_mod(mod, temp=True) - - def _mod_config(self): - for chall in self.achalls: - vh = self.configurator.find_best_http_vhost( - chall.domain, filter_defaults=False, - port=str(self.configurator.config.http01_port)) - if vh: - self._set_up_include_directives(vh) - else: - for vh in self._relevant_vhosts(): - self._set_up_include_directives(vh) - - self.configurator.reverter.register_file_creation( - True, self.challenge_conf_pre) - self.configurator.reverter.register_file_creation( - True, self.challenge_conf_post) - - if self.configurator.version < (2, 4): - config_template_pre = self.CONFIG_TEMPLATE22_PRE - config_template_post = self.CONFIG_TEMPLATE22_POST - else: - config_template_pre = self.CONFIG_TEMPLATE24_PRE - config_template_post = self.CONFIG_TEMPLATE24_POST - - config_text_pre = config_template_pre.format(self.challenge_dir) - config_text_post = config_template_post.format(self.challenge_dir) - - logger.debug("writing a pre config file with text:\n %s", config_text_pre) - with open(self.challenge_conf_pre, "w") as new_conf: - new_conf.write(config_text_pre) - logger.debug("writing a post config file with text:\n %s", config_text_post) - with open(self.challenge_conf_post, "w") as new_conf: - new_conf.write(config_text_post) - - def _relevant_vhosts(self): - http01_port = str(self.configurator.config.http01_port) - relevant_vhosts = [] - for vhost in self.configurator.vhosts: - if any(a.is_wildcard() or a.get_port() == http01_port for a in vhost.addrs): - if not vhost.ssl: - relevant_vhosts.append(vhost) - if not relevant_vhosts: - raise errors.PluginError( - "Unable to find a virtual host listening on port {0} which is" - " currently needed for Certbot to prove to the CA that you" - " control your domain. Please add a virtual host for port" - " {0}.".format(http01_port)) - - return relevant_vhosts - - def _set_up_challenges(self): - if not os.path.isdir(self.challenge_dir): - os.makedirs(self.challenge_dir) - os.chmod(self.challenge_dir, 0o755) - - responses = [] - for achall in self.achalls: - responses.append(self._set_up_challenge(achall)) - - return responses - - def _set_up_challenge(self, achall): - response, validation = achall.response_and_validation() - - name = os.path.join(self.challenge_dir, achall.chall.encode("token")) - - self.configurator.reverter.register_file_creation(True, name) - with open(name, 'wb') as f: - f.write(validation.encode()) - os.chmod(name, 0o644) - - return response - - def _set_up_include_directives(self, vhost): - """Includes override configuration to the beginning and to the end of - VirtualHost. Note that this include isn't added to Augeas search tree""" - - if vhost not in self.moded_vhosts: - logger.debug( - "Adding a temporary challenge validation Include for name: %s " + - "in: %s", vhost.name, vhost.filep) - self.configurator.parser.add_dir_beginning( - vhost.path, "Include", self.challenge_conf_pre) - self.configurator.parser.add_dir( - vhost.path, "Include", self.challenge_conf_post) - - if not vhost.enabled: - self.configurator.parser.add_dir( - get_aug_path(self.configurator.parser.loc["default"]), - "Include", vhost.filep) - - self.moded_vhosts.add(vhost) diff --git a/certbot-apache/certbot_apache/obj.py b/certbot-apache/certbot_apache/obj.py deleted file mode 100644 index 290979f2792..00000000000 --- a/certbot-apache/certbot_apache/obj.py +++ /dev/null @@ -1,271 +0,0 @@ -"""Module contains classes used by the Apache Configurator.""" -import re - -from acme.magic_typing import Set # pylint: disable=unused-import, no-name-in-module -from certbot.plugins import common - - -class Addr(common.Addr): - """Represents an Apache address.""" - - def __eq__(self, other): - """This is defined as equivalent within Apache. - - ip_addr:* == ip_addr - - """ - if isinstance(other, self.__class__): - return ((self.tup == other.tup) or - (self.tup[0] == other.tup[0] and - self.is_wildcard() and other.is_wildcard())) - return False - - def __ne__(self, other): - return not self.__eq__(other) - - def __repr__(self): - return "certbot_apache.obj.Addr(" + repr(self.tup) + ")" - - def __hash__(self): - # Python 3 requires explicit overridden for __hash__ if __eq__ or - # __cmp__ is overridden. See https://bugs.python.org/issue2235 - return super(Addr, self).__hash__() - - def _addr_less_specific(self, addr): - """Returns if addr.get_addr() is more specific than self.get_addr().""" - # pylint: disable=protected-access - return addr._rank_specific_addr() > self._rank_specific_addr() - - def _rank_specific_addr(self): - """Returns numerical rank for get_addr() - - :returns: 2 - FQ, 1 - wildcard, 0 - _default_ - :rtype: int - - """ - if self.get_addr() == "_default_": - return 0 - elif self.get_addr() == "*": - return 1 - else: - return 2 - - def conflicts(self, addr): - r"""Returns if address could conflict with correct function of self. - - Could addr take away service provided by self within Apache? - - .. note::IP Address is more important than wildcard. - Connection from 127.0.0.1:80 with choices of *:80 and 127.0.0.1:* - chooses 127.0.0.1:\* - - .. todo:: Handle domain name addrs... - - Examples: - - ========================================= ===== - ``127.0.0.1:\*.conflicts(127.0.0.1:443)`` True - ``127.0.0.1:443.conflicts(127.0.0.1:\*)`` False - ``\*:443.conflicts(\*:80)`` False - ``_default_:443.conflicts(\*:443)`` True - ========================================= ===== - - """ - if self._addr_less_specific(addr): - return True - elif self.get_addr() == addr.get_addr(): - if self.is_wildcard() or self.get_port() == addr.get_port(): - return True - return False - - def is_wildcard(self): - """Returns if address has a wildcard port.""" - return self.tup[1] == "*" or not self.tup[1] - - def get_sni_addr(self, port): - """Returns the least specific address that resolves on the port. - - Examples: - - - ``1.2.3.4:443`` -> ``1.2.3.4:`` - - ``1.2.3.4:*`` -> ``1.2.3.4:*`` - - :param str port: Desired port - - """ - if self.is_wildcard(): - return self - - return self.get_addr_obj(port) - - -class VirtualHost(object): # pylint: disable=too-few-public-methods - """Represents an Apache Virtualhost. - - :ivar str filep: file path of VH - :ivar str path: Augeas path to virtual host - :ivar set addrs: Virtual Host addresses (:class:`set` of - :class:`common.Addr`) - :ivar str name: ServerName of VHost - :ivar list aliases: Server aliases of vhost - (:class:`list` of :class:`str`) - - :ivar bool ssl: SSLEngine on in vhost - :ivar bool enabled: Virtual host is enabled - :ivar bool modmacro: VirtualHost is using mod_macro - :ivar VirtualHost ancestor: A non-SSL VirtualHost this is based on - - https://httpd.apache.org/docs/2.4/vhosts/details.html - - .. todo:: Any vhost that includes the magic _default_ wildcard is given the - same ServerName as the main server. - - """ - # ?: is used for not returning enclosed characters - strip_name = re.compile(r"^(?:.+://)?([^ :$]*)") - - def __init__(self, filep, path, addrs, ssl, enabled, name=None, - aliases=None, modmacro=False, ancestor=None): - - # pylint: disable=too-many-arguments - """Initialize a VH.""" - self.filep = filep - self.path = path - self.addrs = addrs - self.name = name - self.aliases = aliases if aliases is not None else set() - self.ssl = ssl - self.enabled = enabled - self.modmacro = modmacro - self.ancestor = ancestor - - def get_names(self): - """Return a set of all names.""" - all_names = set() # type: Set[str] - all_names.update(self.aliases) - # Strip out any scheme:// and field from servername - if self.name is not None: - all_names.add(VirtualHost.strip_name.findall(self.name)[0]) - - return all_names - - def __str__(self): - return ( - "File: {filename}\n" - "Vhost path: {vhpath}\n" - "Addresses: {addrs}\n" - "Name: {name}\n" - "Aliases: {aliases}\n" - "TLS Enabled: {tls}\n" - "Site Enabled: {active}\n" - "mod_macro Vhost: {modmacro}".format( - filename=self.filep, - vhpath=self.path, - addrs=", ".join(str(addr) for addr in self.addrs), - name=self.name if self.name is not None else "", - aliases=", ".join(name for name in self.aliases), - tls="Yes" if self.ssl else "No", - active="Yes" if self.enabled else "No", - modmacro="Yes" if self.modmacro else "No")) - - def display_repr(self): - """Return a representation of VHost to be used in dialog""" - return ( - "File: {filename}\n" - "Addresses: {addrs}\n" - "Names: {names}\n" - "HTTPS: {https}\n".format( - filename=self.filep, - addrs=", ".join(str(addr) for addr in self.addrs), - names=", ".join(self.get_names()), - https="Yes" if self.ssl else "No")) - - - def __eq__(self, other): - if isinstance(other, self.__class__): - return (self.filep == other.filep and self.path == other.path and - self.addrs == other.addrs and - self.get_names() == other.get_names() and - self.ssl == other.ssl and - self.enabled == other.enabled and - self.modmacro == other.modmacro) - - return False - - def __ne__(self, other): - return not self.__eq__(other) - - def __hash__(self): - return hash((self.filep, self.path, - tuple(self.addrs), tuple(self.get_names()), - self.ssl, self.enabled, self.modmacro)) - - def conflicts(self, addrs): - """See if vhost conflicts with any of the addrs. - - This determines whether or not these addresses would/could overwrite - the vhost addresses. - - :param addrs: Iterable Addresses - :type addrs: Iterable :class:~obj.Addr - - :returns: If addresses conflicts with vhost - :rtype: bool - - """ - for pot_addr in addrs: - for addr in self.addrs: - if addr.conflicts(pot_addr): - return True - return False - - def same_server(self, vhost, generic=False): - """Determines if the vhost is the same 'server'. - - Used in redirection - indicates whether or not the two virtual hosts - serve on the exact same IP combinations, but different ports. - The generic flag indicates that that we're trying to match to a - default or generic vhost - - .. todo:: Handle _default_ - - """ - - if not generic: - if vhost.get_names() != self.get_names(): - return False - - # If equal and set is not empty... assume same server - if self.name is not None or self.aliases: - return True - # If we're looking for a generic vhost, - # don't return one with a ServerName - elif self.name: - return False - - # Both sets of names are empty. - - # Make conservative educated guess... this is very restrictive - # Consider adding more safety checks. - if len(vhost.addrs) != len(self.addrs): - return False - - # already_found acts to keep everything very conservative. - # Don't allow multiple ip:ports in same set. - already_found = set() # type: Set[str] - - for addr in vhost.addrs: - for local_addr in self.addrs: - if (local_addr.get_addr() == addr.get_addr() and - local_addr != addr and - local_addr.get_addr() not in already_found): - - # This intends to make sure we aren't double counting... - # e.g. 127.0.0.1:* - We require same number of addrs - # currently - already_found.add(local_addr.get_addr()) - break - else: - return False - - return True diff --git a/certbot-apache/certbot_apache/options-ssl-apache.conf b/certbot-apache/certbot_apache/options-ssl-apache.conf deleted file mode 100644 index 8113ee81e5a..00000000000 --- a/certbot-apache/certbot_apache/options-ssl-apache.conf +++ /dev/null @@ -1,26 +0,0 @@ -# This file contains important security parameters. If you modify this file -# manually, Certbot will be unable to automatically provide future security -# updates. Instead, Certbot will print and log an error message with a path to -# the up-to-date file that you will need to refer to when manually updating -# this file. - -SSLEngine on - -# Intermediate configuration, tweak to your needs -SSLProtocol all -SSLv2 -SSLv3 -SSLCipherSuite ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS -SSLHonorCipherOrder on -SSLCompression off - -SSLOptions +StrictRequire - -# Add vhost name to log entries: -LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined -LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common - -#CustomLog /var/log/apache2/access.log vhost_combined -#LogLevel warn -#ErrorLog /var/log/apache2/error.log - -# Always ensure Cookies have "Secure" set (JAH 2012/1) -#Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4" diff --git a/certbot-apache/certbot_apache/override_arch.py b/certbot-apache/certbot_apache/override_arch.py deleted file mode 100644 index c5620e9f956..00000000000 --- a/certbot-apache/certbot_apache/override_arch.py +++ /dev/null @@ -1,31 +0,0 @@ -""" Distribution specific override class for Arch Linux """ -import pkg_resources - -import zope.interface - -from certbot import interfaces - -from certbot_apache import configurator - -@zope.interface.provider(interfaces.IPluginFactory) -class ArchConfigurator(configurator.ApacheConfigurator): - """Arch Linux specific ApacheConfigurator override class""" - - OS_DEFAULTS = dict( - server_root="/etc/httpd", - vhost_root="/etc/httpd/conf", - vhost_files="*.conf", - logs_root="/var/log/httpd", - ctl="apachectl", - version_cmd=['apachectl', '-v'], - restart_cmd=['apachectl', 'graceful'], - conftest_cmd=['apachectl', 'configtest'], - enmod=None, - dismod=None, - le_vhost_ext="-le-ssl.conf", - handle_modules=False, - handle_sites=False, - challenge_location="/etc/httpd/conf", - MOD_SSL_CONF_SRC=pkg_resources.resource_filename( - "certbot_apache", "options-ssl-apache.conf") - ) diff --git a/certbot-apache/certbot_apache/override_centos.py b/certbot-apache/certbot_apache/override_centos.py deleted file mode 100644 index a4f1b84ecbd..00000000000 --- a/certbot-apache/certbot_apache/override_centos.py +++ /dev/null @@ -1,68 +0,0 @@ -""" Distribution specific override class for CentOS family (RHEL, Fedora) """ -import pkg_resources - -import zope.interface - -from certbot import interfaces - -from certbot_apache import apache_util -from certbot_apache import configurator -from certbot_apache import parser - -@zope.interface.provider(interfaces.IPluginFactory) -class CentOSConfigurator(configurator.ApacheConfigurator): - """CentOS specific ApacheConfigurator override class""" - - OS_DEFAULTS = dict( - server_root="/etc/httpd", - vhost_root="/etc/httpd/conf.d", - vhost_files="*.conf", - logs_root="/var/log/httpd", - ctl="apachectl", - version_cmd=['apachectl', '-v'], - restart_cmd=['apachectl', 'graceful'], - restart_cmd_alt=['apachectl', 'restart'], - conftest_cmd=['apachectl', 'configtest'], - enmod=None, - dismod=None, - le_vhost_ext="-le-ssl.conf", - handle_modules=False, - handle_sites=False, - challenge_location="/etc/httpd/conf.d", - MOD_SSL_CONF_SRC=pkg_resources.resource_filename( - "certbot_apache", "centos-options-ssl-apache.conf") - ) - - def _prepare_options(self): - """ - Override the options dictionary initialization in order to support - alternative restart cmd used in CentOS. - """ - super(CentOSConfigurator, self)._prepare_options() - self.options["restart_cmd_alt"][0] = self.option("ctl") - - def get_parser(self): - """Initializes the ApacheParser""" - return CentOSParser( - self.aug, self.option("server_root"), self.option("vhost_root"), - self.version, configurator=self) - - -class CentOSParser(parser.ApacheParser): - """CentOS specific ApacheParser override class""" - def __init__(self, *args, **kwargs): - # CentOS specific configuration file for Apache - self.sysconfig_filep = "/etc/sysconfig/httpd" - super(CentOSParser, self).__init__(*args, **kwargs) - - def update_runtime_variables(self): - """ Override for update_runtime_variables for custom parsing """ - # Opportunistic, works if SELinux not enforced - super(CentOSParser, self).update_runtime_variables() - self.parse_sysconfig_var() - - def parse_sysconfig_var(self): - """ Parses Apache CLI options from CentOS configuration file """ - defines = apache_util.parse_define_file(self.sysconfig_filep, "OPTIONS") - for k in defines.keys(): - self.variables[k] = defines[k] diff --git a/certbot-apache/certbot_apache/override_darwin.py b/certbot-apache/certbot_apache/override_darwin.py deleted file mode 100644 index 4e2a6acac06..00000000000 --- a/certbot-apache/certbot_apache/override_darwin.py +++ /dev/null @@ -1,31 +0,0 @@ -""" Distribution specific override class for macOS """ -import pkg_resources - -import zope.interface - -from certbot import interfaces - -from certbot_apache import configurator - -@zope.interface.provider(interfaces.IPluginFactory) -class DarwinConfigurator(configurator.ApacheConfigurator): - """macOS specific ApacheConfigurator override class""" - - OS_DEFAULTS = dict( - server_root="/etc/apache2", - vhost_root="/etc/apache2/other", - vhost_files="*.conf", - logs_root="/var/log/apache2", - ctl="apachectl", - version_cmd=['apachectl', '-v'], - restart_cmd=['apachectl', 'graceful'], - conftest_cmd=['apachectl', 'configtest'], - enmod=None, - dismod=None, - le_vhost_ext="-le-ssl.conf", - handle_modules=False, - handle_sites=False, - challenge_location="/etc/apache2/other", - MOD_SSL_CONF_SRC=pkg_resources.resource_filename( - "certbot_apache", "options-ssl-apache.conf") - ) diff --git a/certbot-apache/certbot_apache/override_debian.py b/certbot-apache/certbot_apache/override_debian.py deleted file mode 100644 index 0caa619d256..00000000000 --- a/certbot-apache/certbot_apache/override_debian.py +++ /dev/null @@ -1,144 +0,0 @@ -""" Distribution specific override class for Debian family (Ubuntu/Debian) """ -import logging -import os -import pkg_resources - -import zope.interface - -from certbot import errors -from certbot import interfaces -from certbot import util - -from certbot_apache import apache_util -from certbot_apache import configurator - -logger = logging.getLogger(__name__) - -@zope.interface.provider(interfaces.IPluginFactory) -class DebianConfigurator(configurator.ApacheConfigurator): - """Debian specific ApacheConfigurator override class""" - - OS_DEFAULTS = dict( - server_root="/etc/apache2", - vhost_root="/etc/apache2/sites-available", - vhost_files="*", - logs_root="/var/log/apache2", - ctl="apache2ctl", - version_cmd=['apache2ctl', '-v'], - restart_cmd=['apache2ctl', 'graceful'], - conftest_cmd=['apache2ctl', 'configtest'], - enmod="a2enmod", - dismod="a2dismod", - le_vhost_ext="-le-ssl.conf", - handle_modules=True, - handle_sites=True, - challenge_location="/etc/apache2", - MOD_SSL_CONF_SRC=pkg_resources.resource_filename( - "certbot_apache", "options-ssl-apache.conf") - ) - - def enable_site(self, vhost): - """Enables an available site, Apache reload required. - - .. note:: Does not make sure that the site correctly works or that all - modules are enabled appropriately. - - :param vhost: vhost to enable - :type vhost: :class:`~certbot_apache.obj.VirtualHost` - - :raises .errors.NotSupportedError: If filesystem layout is not - supported. - - """ - if vhost.enabled: - return - - enabled_path = ("%s/sites-enabled/%s" % - (self.parser.root, - os.path.basename(vhost.filep))) - if not os.path.isdir(os.path.dirname(enabled_path)): - # For some reason, sites-enabled / sites-available do not exist - # Call the parent method - return super(DebianConfigurator, self).enable_site(vhost) - self.reverter.register_file_creation(False, enabled_path) - try: - os.symlink(vhost.filep, enabled_path) - except OSError as err: - if os.path.islink(enabled_path) and os.path.realpath( - enabled_path) == vhost.filep: - # Already in shape - vhost.enabled = True - return - else: - logger.warning( - "Could not symlink %s to %s, got error: %s", enabled_path, - vhost.filep, err.strerror) - errstring = ("Encountered error while trying to enable a " + - "newly created VirtualHost located at {0} by " + - "linking to it from {1}") - raise errors.NotSupportedError(errstring.format(vhost.filep, - enabled_path)) - vhost.enabled = True - logger.info("Enabling available site: %s", vhost.filep) - self.save_notes += "Enabled site %s\n" % vhost.filep - - def enable_mod(self, mod_name, temp=False): - # pylint: disable=unused-argument - """Enables module in Apache. - - Both enables and reloads Apache so module is active. - - :param str mod_name: Name of the module to enable. (e.g. 'ssl') - :param bool temp: Whether or not this is a temporary action. - - :raises .errors.NotSupportedError: If the filesystem layout is not - supported. - :raises .errors.MisconfigurationError: If a2enmod or a2dismod cannot be - run. - - """ - avail_path = os.path.join(self.parser.root, "mods-available") - enabled_path = os.path.join(self.parser.root, "mods-enabled") - if not os.path.isdir(avail_path) or not os.path.isdir(enabled_path): - raise errors.NotSupportedError( - "Unsupported directory layout. You may try to enable mod %s " - "and try again." % mod_name) - - deps = apache_util.get_mod_deps(mod_name) - - # Enable all dependencies - for dep in deps: - if (dep + "_module") not in self.parser.modules: - self._enable_mod_debian(dep, temp) - self.parser.add_mod(dep) - note = "Enabled dependency of %s module - %s" % (mod_name, dep) - if not temp: - self.save_notes += note + os.linesep - logger.debug(note) - - # Enable actual module - self._enable_mod_debian(mod_name, temp) - self.parser.add_mod(mod_name) - - if not temp: - self.save_notes += "Enabled %s module in Apache\n" % mod_name - logger.info("Enabled Apache %s module", mod_name) - - # Modules can enable additional config files. Variables may be defined - # within these new configuration sections. - # Reload is not necessary as DUMP_RUN_CFG uses latest config. - self.parser.update_runtime_variables() - - def _enable_mod_debian(self, mod_name, temp): - """Assumes mods-available, mods-enabled layout.""" - # Generate reversal command. - # Try to be safe here... check that we can probably reverse before - # applying enmod command - if not util.exe_exists(self.option("dismod")): - raise errors.MisconfigurationError( - "Unable to find a2dismod, please make sure a2enmod and " - "a2dismod are configured correctly for certbot.") - - self.reverter.register_undo_command( - temp, [self.option("dismod"), "-f", mod_name]) - util.run_script([self.option("enmod"), mod_name]) diff --git a/certbot-apache/certbot_apache/override_gentoo.py b/certbot-apache/certbot_apache/override_gentoo.py deleted file mode 100644 index 556e3225e5e..00000000000 --- a/certbot-apache/certbot_apache/override_gentoo.py +++ /dev/null @@ -1,75 +0,0 @@ -""" Distribution specific override class for Gentoo Linux """ -import pkg_resources - -import zope.interface - -from certbot import interfaces - -from certbot_apache import apache_util -from certbot_apache import configurator -from certbot_apache import parser - -@zope.interface.provider(interfaces.IPluginFactory) -class GentooConfigurator(configurator.ApacheConfigurator): - """Gentoo specific ApacheConfigurator override class""" - - OS_DEFAULTS = dict( - server_root="/etc/apache2", - vhost_root="/etc/apache2/vhosts.d", - vhost_files="*.conf", - logs_root="/var/log/apache2", - ctl="apache2ctl", - version_cmd=['apache2ctl', '-v'], - restart_cmd=['apache2ctl', 'graceful'], - restart_cmd_alt=['apache2ctl', 'restart'], - conftest_cmd=['apache2ctl', 'configtest'], - enmod=None, - dismod=None, - le_vhost_ext="-le-ssl.conf", - handle_modules=False, - handle_sites=False, - challenge_location="/etc/apache2/vhosts.d", - MOD_SSL_CONF_SRC=pkg_resources.resource_filename( - "certbot_apache", "options-ssl-apache.conf") - ) - - def _prepare_options(self): - """ - Override the options dictionary initialization in order to support - alternative restart cmd used in Gentoo. - """ - super(GentooConfigurator, self)._prepare_options() - self.options["restart_cmd_alt"][0] = self.option("ctl") - - def get_parser(self): - """Initializes the ApacheParser""" - return GentooParser( - self.aug, self.option("server_root"), self.option("vhost_root"), - self.version, configurator=self) - - -class GentooParser(parser.ApacheParser): - """Gentoo specific ApacheParser override class""" - def __init__(self, *args, **kwargs): - # Gentoo specific configuration file for Apache2 - self.apacheconfig_filep = "/etc/conf.d/apache2" - super(GentooParser, self).__init__(*args, **kwargs) - - def update_runtime_variables(self): - """ Override for update_runtime_variables for custom parsing """ - self.parse_sysconfig_var() - self.update_modules() - - def parse_sysconfig_var(self): - """ Parses Apache CLI options from Gentoo configuration file """ - defines = apache_util.parse_define_file(self.apacheconfig_filep, - "APACHE2_OPTS") - for k in defines.keys(): - self.variables[k] = defines[k] - - def update_modules(self): - """Get loaded modules from httpd process, and add them to DOM""" - mod_cmd = [self.configurator.option("ctl"), "modules"] - matches = self.parse_from_subprocess(mod_cmd, r"(.*)_module") - for mod in matches: - self.add_mod(mod.strip()) diff --git a/certbot-apache/certbot_apache/override_suse.py b/certbot-apache/certbot_apache/override_suse.py deleted file mode 100644 index 3d0043afe5f..00000000000 --- a/certbot-apache/certbot_apache/override_suse.py +++ /dev/null @@ -1,31 +0,0 @@ -""" Distribution specific override class for OpenSUSE """ -import pkg_resources - -import zope.interface - -from certbot import interfaces - -from certbot_apache import configurator - -@zope.interface.provider(interfaces.IPluginFactory) -class OpenSUSEConfigurator(configurator.ApacheConfigurator): - """OpenSUSE specific ApacheConfigurator override class""" - - OS_DEFAULTS = dict( - server_root="/etc/apache2", - vhost_root="/etc/apache2/vhosts.d", - vhost_files="*.conf", - logs_root="/var/log/apache2", - ctl="apache2ctl", - version_cmd=['apache2ctl', '-v'], - restart_cmd=['apache2ctl', 'graceful'], - conftest_cmd=['apache2ctl', 'configtest'], - enmod="a2enmod", - dismod="a2dismod", - le_vhost_ext="-le-ssl.conf", - handle_modules=False, - handle_sites=False, - challenge_location="/etc/apache2/vhosts.d", - MOD_SSL_CONF_SRC=pkg_resources.resource_filename( - "certbot_apache", "options-ssl-apache.conf") - ) diff --git a/certbot-apache/certbot_apache/parser.py b/certbot-apache/certbot_apache/parser.py deleted file mode 100644 index 148f052d0cc..00000000000 --- a/certbot-apache/certbot_apache/parser.py +++ /dev/null @@ -1,822 +0,0 @@ -"""ApacheParser is a member object of the ApacheConfigurator class.""" -import copy -import fnmatch -import logging -import os -import re -import subprocess -import sys - -import six - -from acme.magic_typing import Dict, List, Set # pylint: disable=unused-import, no-name-in-module -from certbot import errors - -logger = logging.getLogger(__name__) - - -class ApacheParser(object): - # pylint: disable=too-many-public-methods - """Class handles the fine details of parsing the Apache Configuration. - - .. todo:: Make parsing general... remove sites-available etc... - - :ivar str root: Normalized absolute path to the server root - directory. Without trailing slash. - :ivar set modules: All module names that are currently enabled. - :ivar dict loc: Location to place directives, root - configuration origin, - default - user config file, name - NameVirtualHost, - - """ - arg_var_interpreter = re.compile(r"\$\{[^ \}]*}") - fnmatch_chars = set(["*", "?", "\\", "[", "]"]) - - def __init__(self, aug, root, vhostroot=None, version=(2, 4), - configurator=None): - # Note: Order is important here. - - # Needed for calling save() with reverter functionality that resides in - # AugeasConfigurator superclass of ApacheConfigurator. This resolves - # issues with aug.load() after adding new files / defines to parse tree - self.configurator = configurator - - self.modules = set() # type: Set[str] - self.parser_paths = {} # type: Dict[str, List[str]] - self.variables = {} # type: Dict[str, str] - - self.aug = aug - # Find configuration root and make sure augeas can parse it. - self.root = os.path.abspath(root) - self.loc = {"root": self._find_config_root()} - self.parse_file(self.loc["root"]) - - if version >= (2, 4): - # Look up variables from httpd and add to DOM if not already parsed - self.update_runtime_variables() - - # This problem has been fixed in Augeas 1.0 - self.standardize_excl() - - # Parse LoadModule directives from configuration files - self.parse_modules() - - # Set up rest of locations - self.loc.update(self._set_locations()) - - # list of the active include paths, before modifications - self.existing_paths = copy.deepcopy(self.parser_paths) - - # Must also attempt to parse additional virtual host root - if vhostroot: - self.parse_file(os.path.abspath(vhostroot) + "/" + - self.configurator.option("vhost_files")) - - # check to see if there were unparsed define statements - if version < (2, 4): - if self.find_dir("Define", exclude=False): - raise errors.PluginError("Error parsing runtime variables") - - def add_include(self, main_config, inc_path): - """Add Include for a new configuration file if one does not exist - - :param str main_config: file path to main Apache config file - :param str inc_path: path of file to include - - """ - if len(self.find_dir(case_i("Include"), inc_path)) == 0: - logger.debug("Adding Include %s to %s", - inc_path, get_aug_path(main_config)) - self.add_dir( - get_aug_path(main_config), - "Include", inc_path) - - # Add new path to parser paths - new_dir = os.path.dirname(inc_path) - new_file = os.path.basename(inc_path) - if new_dir in self.existing_paths.keys(): - # Add to existing path - self.existing_paths[new_dir].append(new_file) - else: - # Create a new path - self.existing_paths[new_dir] = [new_file] - - def add_mod(self, mod_name): - """Shortcut for updating parser modules.""" - if mod_name + "_module" not in self.modules: - self.modules.add(mod_name + "_module") - if "mod_" + mod_name + ".c" not in self.modules: - self.modules.add("mod_" + mod_name + ".c") - - def reset_modules(self): - """Reset the loaded modules list. This is called from cleanup to clear - temporarily loaded modules.""" - self.modules = set() - self.update_modules() - self.parse_modules() - - def parse_modules(self): - """Iterates on the configuration until no new modules are loaded. - - ..todo:: This should be attempted to be done with a binary to avoid - the iteration issue. Else... parse and enable mods at same time. - - """ - mods = set() # type: Set[str] - matches = self.find_dir("LoadModule") - iterator = iter(matches) - # Make sure prev_size != cur_size for do: while: iteration - prev_size = -1 - - while len(mods) != prev_size: - prev_size = len(mods) - - for match_name, match_filename in six.moves.zip( - iterator, iterator): - mod_name = self.get_arg(match_name) - mod_filename = self.get_arg(match_filename) - if mod_name and mod_filename: - mods.add(mod_name) - mods.add(os.path.basename(mod_filename)[:-2] + "c") - else: - logger.debug("Could not read LoadModule directive from " + - "Augeas path: {0}".format(match_name[6:])) - self.modules.update(mods) - - def update_runtime_variables(self): - """Update Includes, Defines and Includes from httpd config dump data""" - self.update_defines() - self.update_includes() - self.update_modules() - - def update_defines(self): - """Get Defines from httpd process""" - - variables = dict() - define_cmd = [self.configurator.option("ctl"), "-t", "-D", - "DUMP_RUN_CFG"] - matches = self.parse_from_subprocess(define_cmd, r"Define: ([^ \n]*)") - try: - matches.remove("DUMP_RUN_CFG") - except ValueError: - return - - for match in matches: - if match.count("=") > 1: - logger.error("Unexpected number of equal signs in " - "runtime config dump.") - raise errors.PluginError( - "Error parsing Apache runtime variables") - parts = match.partition("=") - variables[parts[0]] = parts[2] - - self.variables = variables - - def update_includes(self): - """Get includes from httpd process, and add them to DOM if needed""" - - # Find_dir iterates over configuration for Include and IncludeOptional - # directives to make sure we see the full include tree present in the - # configuration files - _ = self.find_dir("Include") - - inc_cmd = [self.configurator.option("ctl"), "-t", "-D", - "DUMP_INCLUDES"] - matches = self.parse_from_subprocess(inc_cmd, r"\(.*\) (.*)") - if matches: - for i in matches: - if not self.parsed_in_current(i): - self.parse_file(i) - - def update_modules(self): - """Get loaded modules from httpd process, and add them to DOM""" - - mod_cmd = [self.configurator.option("ctl"), "-t", "-D", - "DUMP_MODULES"] - matches = self.parse_from_subprocess(mod_cmd, r"(.*)_module") - for mod in matches: - self.add_mod(mod.strip()) - - def parse_from_subprocess(self, command, regexp): - """Get values from stdout of subprocess command - - :param list command: Command to run - :param str regexp: Regexp for parsing - - :returns: list parsed from command output - :rtype: list - - """ - stdout = self._get_runtime_cfg(command) - return re.compile(regexp).findall(stdout) - - def _get_runtime_cfg(self, command): # pylint: disable=no-self-use - """Get runtime configuration info. - :param command: Command to run - - :returns: stdout from command - - """ - try: - proc = subprocess.Popen( - command, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True) - stdout, stderr = proc.communicate() - - except (OSError, ValueError): - logger.error( - "Error running command %s for runtime parameters!%s", - command, os.linesep) - raise errors.MisconfigurationError( - "Error accessing loaded Apache parameters: %s", - command) - # Small errors that do not impede - if proc.returncode != 0: - logger.warning("Error in checking parameter list: %s", stderr) - raise errors.MisconfigurationError( - "Apache is unable to check whether or not the module is " - "loaded because Apache is misconfigured.") - - return stdout - - def filter_args_num(self, matches, args): # pylint: disable=no-self-use - """Filter out directives with specific number of arguments. - - This function makes the assumption that all related arguments are given - in order. Thus /files/apache/directive[5]/arg[2] must come immediately - after /files/apache/directive[5]/arg[1]. Runs in 1 linear pass. - - :param string matches: Matches of all directives with arg nodes - :param int args: Number of args you would like to filter - - :returns: List of directives that contain # of arguments. - (arg is stripped off) - - """ - filtered = [] - if args == 1: - for i in range(len(matches)): - if matches[i].endswith("/arg"): - filtered.append(matches[i][:-4]) - else: - for i in range(len(matches)): - if matches[i].endswith("/arg[%d]" % args): - # Make sure we don't cause an IndexError (end of list) - # Check to make sure arg + 1 doesn't exist - if (i == (len(matches) - 1) or - not matches[i + 1].endswith("/arg[%d]" % - (args + 1))): - filtered.append(matches[i][:-len("/arg[%d]" % args)]) - - return filtered - - def add_dir_to_ifmodssl(self, aug_conf_path, directive, args): - """Adds directive and value to IfMod ssl block. - - Adds given directive and value along configuration path within - an IfMod mod_ssl.c block. If the IfMod block does not exist in - the file, it is created. - - :param str aug_conf_path: Desired Augeas config path to add directive - :param str directive: Directive you would like to add, e.g. Listen - :param args: Values of the directive; str "443" or list of str - :type args: list - - """ - # TODO: Add error checking code... does the path given even exist? - # Does it throw exceptions? - if_mod_path = self._get_ifmod(aug_conf_path, "mod_ssl.c") - # IfModule can have only one valid argument, so append after - self.aug.insert(if_mod_path + "arg", "directive", False) - nvh_path = if_mod_path + "directive[1]" - self.aug.set(nvh_path, directive) - if len(args) == 1: - self.aug.set(nvh_path + "/arg", args[0]) - else: - for i, arg in enumerate(args): - self.aug.set("%s/arg[%d]" % (nvh_path, i + 1), arg) - - def _get_ifmod(self, aug_conf_path, mod): - """Returns the path to and creates one if it doesn't exist. - - :param str aug_conf_path: Augeas configuration path - :param str mod: module ie. mod_ssl.c - - """ - if_mods = self.aug.match(("%s/IfModule/*[self::arg='%s']" % - (aug_conf_path, mod))) - if len(if_mods) == 0: - self.aug.set("%s/IfModule[last() + 1]" % aug_conf_path, "") - self.aug.set("%s/IfModule[last()]/arg" % aug_conf_path, mod) - if_mods = self.aug.match(("%s/IfModule/*[self::arg='%s']" % - (aug_conf_path, mod))) - # Strip off "arg" at end of first ifmod path - return if_mods[0][:len(if_mods[0]) - 3] - - def add_dir(self, aug_conf_path, directive, args): - """Appends directive to the end fo the file given by aug_conf_path. - - .. note:: Not added to AugeasConfigurator because it may depend - on the lens - - :param str aug_conf_path: Augeas configuration path to add directive - :param str directive: Directive to add - :param args: Value of the directive. ie. Listen 443, 443 is arg - :type args: list or str - - """ - self.aug.set(aug_conf_path + "/directive[last() + 1]", directive) - if isinstance(args, list): - for i, value in enumerate(args, 1): - self.aug.set( - "%s/directive[last()]/arg[%d]" % (aug_conf_path, i), value) - else: - self.aug.set(aug_conf_path + "/directive[last()]/arg", args) - - def add_dir_beginning(self, aug_conf_path, dirname, args): - """Adds the directive to the beginning of defined aug_conf_path. - - :param str aug_conf_path: Augeas configuration path to add directive - :param str dirname: Directive to add - :param args: Value of the directive. ie. Listen 443, 443 is arg - :type args: list or str - """ - first_dir = aug_conf_path + "/directive[1]" - self.aug.insert(first_dir, "directive", True) - self.aug.set(first_dir, dirname) - if isinstance(args, list): - for i, value in enumerate(args, 1): - self.aug.set(first_dir + "/arg[%d]" % (i), value) - else: - self.aug.set(first_dir + "/arg", args) - - def add_comment(self, aug_conf_path, comment): - """Adds the comment to the augeas path - - :param str aug_conf_path: Augeas configuration path to add directive - :param str comment: Comment content - - """ - self.aug.set(aug_conf_path + "/#comment[last() + 1]", comment) - - def find_comments(self, arg, start=None): - """Finds a comment with specified content from the provided DOM path - - :param str arg: Comment content to search - :param str start: Beginning Augeas path to begin looking - - :returns: List of augeas paths containing the comment content - :rtype: list - - """ - if not start: - start = get_aug_path(self.root) - - comments = self.aug.match("%s//*[label() = '#comment']" % start) - - results = [] - for comment in comments: - c_content = self.aug.get(comment) - if c_content and arg in c_content: - results.append(comment) - return results - - def find_dir(self, directive, arg=None, start=None, exclude=True): - """Finds directive in the configuration. - - Recursively searches through config files to find directives - Directives should be in the form of a case insensitive regex currently - - .. todo:: arg should probably be a list - .. todo:: arg search currently only supports direct matching. It does - not handle the case of variables or quoted arguments. This should - be adapted to use a generic search for the directive and then do a - case-insensitive self.get_arg filter - - Note: Augeas is inherently case sensitive while Apache is case - insensitive. Augeas 1.0 allows case insensitive regexes like - regexp(/Listen/, "i"), however the version currently supported - by Ubuntu 0.10 does not. Thus I have included my own case insensitive - transformation by calling case_i() on everything to maintain - compatibility. - - :param str directive: Directive to look for - :param arg: Specific value directive must have, None if all should - be considered - :type arg: str or None - - :param str start: Beginning Augeas path to begin looking - :param bool exclude: Whether or not to exclude directives based on - variables and enabled modules - - """ - # Cannot place member variable in the definition of the function so... - if not start: - start = get_aug_path(self.loc["root"]) - - # No regexp code - # if arg is None: - # matches = self.aug.match(start + - # "//*[self::directive='" + directive + "']/arg") - # else: - # matches = self.aug.match(start + - # "//*[self::directive='" + directive + - # "']/* [self::arg='" + arg + "']") - - # includes = self.aug.match(start + - # "//* [self::directive='Include']/* [label()='arg']") - - regex = "(%s)|(%s)|(%s)" % (case_i(directive), - case_i("Include"), - case_i("IncludeOptional")) - matches = self.aug.match( - "%s//*[self::directive=~regexp('%s')]" % (start, regex)) - - if exclude: - matches = self._exclude_dirs(matches) - - if arg is None: - arg_suffix = "/arg" - else: - arg_suffix = "/*[self::arg=~regexp('%s')]" % case_i(arg) - - ordered_matches = [] # type: List[str] - - # TODO: Wildcards should be included in alphabetical order - # https://httpd.apache.org/docs/2.4/mod/core.html#include - for match in matches: - dir_ = self.aug.get(match).lower() - if dir_ == "include" or dir_ == "includeoptional": - ordered_matches.extend(self.find_dir( - directive, arg, - self._get_include_path(self.get_arg(match + "/arg")), - exclude)) - # This additionally allows Include - if dir_ == directive.lower(): - ordered_matches.extend(self.aug.match(match + arg_suffix)) - - return ordered_matches - - def get_arg(self, match): - """Uses augeas.get to get argument value and interprets result. - - This also converts all variables and parameters appropriately. - - """ - value = self.aug.get(match) - - # No need to strip quotes for variables, as apache2ctl already does - # this, but we do need to strip quotes for all normal arguments. - - # Note: normal argument may be a quoted variable - # e.g. strip now, not later - if not value: - return None - else: - value = value.strip("'\"") - - variables = ApacheParser.arg_var_interpreter.findall(value) - - for var in variables: - # Strip off ${ and } - try: - value = value.replace(var, self.variables[var[2:-1]]) - except KeyError: - raise errors.PluginError("Error Parsing variable: %s" % var) - - return value - - def _exclude_dirs(self, matches): - """Exclude directives that are not loaded into the configuration.""" - filters = [("ifmodule", self.modules), ("ifdefine", self.variables)] - - valid_matches = [] - - for match in matches: - for filter_ in filters: - if not self._pass_filter(match, filter_): - break - else: - valid_matches.append(match) - return valid_matches - - def _pass_filter(self, match, filter_): - """Determine if directive passes a filter. - - :param str match: Augeas path - :param list filter: list of tuples of form - [("lowercase if directive", set of relevant parameters)] - - """ - match_l = match.lower() - last_match_idx = match_l.find(filter_[0]) - - while last_match_idx != -1: - # Check args - end_of_if = match_l.find("/", last_match_idx) - # This should be aug.get (vars are not used e.g. parser.aug_get) - expression = self.aug.get(match[:end_of_if] + "/arg") - - if expression.startswith("!"): - # Strip off "!" - if expression[1:] in filter_[1]: - return False - else: - if expression not in filter_[1]: - return False - - last_match_idx = match_l.find(filter_[0], end_of_if) - - return True - - def _get_include_path(self, arg): - """Converts an Apache Include directive into Augeas path. - - Converts an Apache Include directive argument into an Augeas - searchable path - - .. todo:: convert to use os.path.join() - - :param str arg: Argument of Include directive - - :returns: Augeas path string - :rtype: str - - """ - # Check to make sure only expected characters are used <- maybe remove - # validChars = re.compile("[a-zA-Z0-9.*?_-/]*") - # matchObj = validChars.match(arg) - # if matchObj.group() != arg: - # logger.error("Error: Invalid regexp characters in %s", arg) - # return [] - - # Remove beginning and ending quotes - arg = arg.strip("'\"") - - # Standardize the include argument based on server root - if not arg.startswith("/"): - # Normpath will condense ../ - arg = os.path.normpath(os.path.join(self.root, arg)) - else: - arg = os.path.normpath(arg) - - # Attempts to add a transform to the file if one does not already exist - if os.path.isdir(arg): - self.parse_file(os.path.join(arg, "*")) - else: - self.parse_file(arg) - - # Argument represents an fnmatch regular expression, convert it - # Split up the path and convert each into an Augeas accepted regex - # then reassemble - split_arg = arg.split("/") - for idx, split in enumerate(split_arg): - if any(char in ApacheParser.fnmatch_chars for char in split): - # Turn it into a augeas regex - # TODO: Can this instead be an augeas glob instead of regex - split_arg[idx] = ("* [label()=~regexp('%s')]" % - self.fnmatch_to_re(split)) - # Reassemble the argument - # Note: This also normalizes the argument /serverroot/ -> /serverroot - arg = "/".join(split_arg) - - return get_aug_path(arg) - - def fnmatch_to_re(self, clean_fn_match): # pylint: disable=no-self-use - """Method converts Apache's basic fnmatch to regular expression. - - Assumption - Configs are assumed to be well-formed and only writable by - privileged users. - - https://apr.apache.org/docs/apr/2.0/apr__fnmatch_8h_source.html - http://apache2.sourcearchive.com/documentation/2.2.16-6/apr__fnmatch_8h_source.html - - :param str clean_fn_match: Apache style filename match, like globs - - :returns: regex suitable for augeas - :rtype: str - - """ - if sys.version_info < (3, 6): - # This strips off final /Z(?ms) - return fnmatch.translate(clean_fn_match)[:-7] - else: # pragma: no cover - # Since Python 3.6, it returns a different pattern like (?s:.*\.load)\Z - return fnmatch.translate(clean_fn_match)[4:-3] - - def parse_file(self, filepath): - """Parse file with Augeas - - Checks to see if file_path is parsed by Augeas - If filepath isn't parsed, the file is added and Augeas is reloaded - - :param str filepath: Apache config file path - - """ - use_new, remove_old = self._check_path_actions(filepath) - # Ensure that we have the latest Augeas DOM state on disk before - # calling aug.load() which reloads the state from disk - if self.configurator: - self.configurator.ensure_augeas_state() - # Test if augeas included file for Httpd.lens - # Note: This works for augeas globs, ie. *.conf - if use_new: - inc_test = self.aug.match( - "/augeas/load/Httpd['%s' =~ glob(incl)]" % filepath) - if not inc_test: - # Load up files - # This doesn't seem to work on TravisCI - # self.aug.add_transform("Httpd.lns", [filepath]) - if remove_old: - self._remove_httpd_transform(filepath) - self._add_httpd_transform(filepath) - self.aug.load() - - def parsed_in_current(self, filep): - """Checks if the file path is parsed by current Augeas parser config - ie. returns True if the file is found on a path that's found in live - Augeas configuration. - - :param str filep: Path to match - - :returns: True if file is parsed in existing configuration tree - :rtype: bool - """ - return self._parsed_by_parser_paths(filep, self.parser_paths) - - def parsed_in_original(self, filep): - """Checks if the file path is parsed by existing Apache config. - ie. returns True if the file is found on a path that matches Include or - IncludeOptional statement in the Apache configuration. - - :param str filep: Path to match - - :returns: True if file is parsed in existing configuration tree - :rtype: bool - """ - return self._parsed_by_parser_paths(filep, self.existing_paths) - - def _parsed_by_parser_paths(self, filep, paths): - """Helper function that searches through provided paths and returns - True if file path is found in the set""" - for directory in paths.keys(): - for filename in paths[directory]: - if fnmatch.fnmatch(filep, os.path.join(directory, filename)): - return True - return False - - def _check_path_actions(self, filepath): - """Determine actions to take with a new augeas path - - This helper function will return a tuple that defines - if we should try to append the new filepath to augeas - parser paths, and / or remove the old one with more - narrow matching. - - :param str filepath: filepath to check the actions for - - """ - - try: - new_file_match = os.path.basename(filepath) - existing_matches = self.parser_paths[os.path.dirname(filepath)] - if "*" in existing_matches: - use_new = False - else: - use_new = True - if new_file_match == "*": - remove_old = True - else: - remove_old = False - except KeyError: - use_new = True - remove_old = False - return use_new, remove_old - - def _remove_httpd_transform(self, filepath): - """Remove path from Augeas transform - - :param str filepath: filepath to remove - """ - - remove_basenames = self.parser_paths[os.path.dirname(filepath)] - remove_dirname = os.path.dirname(filepath) - for name in remove_basenames: - remove_path = remove_dirname + "/" + name - remove_inc = self.aug.match( - "/augeas/load/Httpd/incl [. ='%s']" % remove_path) - self.aug.remove(remove_inc[0]) - self.parser_paths.pop(remove_dirname) - - def _add_httpd_transform(self, incl): - """Add a transform to Augeas. - - This function will correctly add a transform to augeas - The existing augeas.add_transform in python doesn't seem to work for - Travis CI as it loads in libaugeas.so.0.10.0 - - :param str incl: filepath to include for transform - - """ - last_include = self.aug.match("/augeas/load/Httpd/incl [last()]") - if last_include: - # Insert a new node immediately after the last incl - self.aug.insert(last_include[0], "incl", False) - self.aug.set("/augeas/load/Httpd/incl[last()]", incl) - # On first use... must load lens and add file to incl - else: - # Augeas uses base 1 indexing... insert at beginning... - self.aug.set("/augeas/load/Httpd/lens", "Httpd.lns") - self.aug.set("/augeas/load/Httpd/incl", incl) - # Add included path to paths dictionary - try: - self.parser_paths[os.path.dirname(incl)].append( - os.path.basename(incl)) - except KeyError: - self.parser_paths[os.path.dirname(incl)] = [ - os.path.basename(incl)] - - def standardize_excl(self): - """Standardize the excl arguments for the Httpd lens in Augeas. - - Note: Hack! - Standardize the excl arguments for the Httpd lens in Augeas - Servers sometimes give incorrect defaults - Note: This problem should be fixed in Augeas 1.0. Unfortunately, - Augeas 0.10 appears to be the most popular version currently. - - """ - # attempt to protect against augeas error in 0.10.0 - ubuntu - # *.augsave -> /*.augsave upon augeas.load() - # Try to avoid bad httpd files - # There has to be a better way... but after a day and a half of testing - # I had no luck - # This is a hack... work around... submit to augeas if still not fixed - - excl = ["*.augnew", "*.augsave", "*.dpkg-dist", "*.dpkg-bak", - "*.dpkg-new", "*.dpkg-old", "*.rpmsave", "*.rpmnew", - "*~", - self.root + "/*.augsave", - self.root + "/*~", - self.root + "/*/*augsave", - self.root + "/*/*~", - self.root + "/*/*/*.augsave", - self.root + "/*/*/*~"] - - for i, excluded in enumerate(excl, 1): - self.aug.set("/augeas/load/Httpd/excl[%d]" % i, excluded) - - self.aug.load() - - def _set_locations(self): - """Set default location for directives. - - Locations are given as file_paths - .. todo:: Make sure that files are included - - """ - default = self.loc["root"] - - temp = os.path.join(self.root, "ports.conf") - if os.path.isfile(temp): - listen = temp - name = temp - else: - listen = default - name = default - - return {"default": default, "listen": listen, "name": name} - - def _find_config_root(self): - """Find the Apache Configuration Root file.""" - location = ["apache2.conf", "httpd.conf", "conf/httpd.conf"] - for name in location: - if os.path.isfile(os.path.join(self.root, name)): - return os.path.join(self.root, name) - raise errors.NoInstallationError("Could not find configuration root") - - -def case_i(string): - """Returns case insensitive regex. - - Returns a sloppy, but necessary version of a case insensitive regex. - Any string should be able to be submitted and the string is - escaped and then made case insensitive. - May be replaced by a more proper /i once augeas 1.0 is widely - supported. - - :param str string: string to make case i regex - - """ - return "".join(["[" + c.upper() + c.lower() + "]" - if c.isalpha() else c for c in re.escape(string)]) - - -def get_aug_path(file_path): - """Return augeas path for full filepath. - - :param str file_path: Full filepath - - """ - return "/files%s" % file_path diff --git a/certbot-apache/certbot_apache/tests/__init__.py b/certbot-apache/certbot_apache/tests/__init__.py deleted file mode 100644 index 7e7d39fa436..00000000000 --- a/certbot-apache/certbot_apache/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Certbot Apache Tests""" diff --git a/certbot-apache/certbot_apache/tests/augeas_configurator_test.py b/certbot-apache/certbot_apache/tests/augeas_configurator_test.py deleted file mode 100644 index c121ecdf3d9..00000000000 --- a/certbot-apache/certbot_apache/tests/augeas_configurator_test.py +++ /dev/null @@ -1,116 +0,0 @@ -"""Test for certbot_apache.augeas_configurator.""" -import os -import shutil -import unittest - -import mock - -from certbot import errors - -from certbot_apache.tests import util - - -class AugeasConfiguratorTest(util.ApacheTest): - """Test for Augeas Configurator base class.""" - - - def setUp(self): # pylint: disable=arguments-differ - super(AugeasConfiguratorTest, self).setUp() - - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, self.work_dir) - - self.vh_truth = util.get_vh_truth( - self.temp_dir, "debian_apache_2_4/multiple_vhosts") - - def tearDown(self): - shutil.rmtree(self.config_dir) - shutil.rmtree(self.work_dir) - shutil.rmtree(self.temp_dir) - - def test_bad_parse(self): - # pylint: disable=protected-access - self.config.parser.parse_file(os.path.join( - self.config.parser.root, "conf-available", "bad_conf_file.conf")) - self.assertRaises( - errors.PluginError, self.config.check_parsing_errors, "httpd.aug") - - def test_bad_save(self): - mock_save = mock.Mock() - mock_save.side_effect = IOError - self.config.aug.save = mock_save - - self.assertRaises(errors.PluginError, self.config.save) - - def test_bad_save_checkpoint(self): - self.config.reverter.add_to_checkpoint = mock.Mock( - side_effect=errors.ReverterError) - self.config.parser.add_dir( - self.vh_truth[0].path, "Test", "bad_save_ckpt") - self.assertRaises(errors.PluginError, self.config.save) - - def test_bad_save_finalize_checkpoint(self): - self.config.reverter.finalize_checkpoint = mock.Mock( - side_effect=errors.ReverterError) - self.config.parser.add_dir( - self.vh_truth[0].path, "Test", "bad_save_ckpt") - self.assertRaises(errors.PluginError, self.config.save, "Title") - - def test_finalize_save(self): - mock_finalize = mock.Mock() - self.config.reverter = mock_finalize - self.config.save("Example Title") - - self.assertTrue(mock_finalize.is_called) - - def test_recovery_routine(self): - mock_load = mock.Mock() - self.config.aug.load = mock_load - - self.config.recovery_routine() - self.assertEqual(mock_load.call_count, 1) - - def test_recovery_routine_error(self): - self.config.reverter.recovery_routine = mock.Mock( - side_effect=errors.ReverterError) - - self.assertRaises( - errors.PluginError, self.config.recovery_routine) - - def test_revert_challenge_config(self): - mock_load = mock.Mock() - self.config.aug.load = mock_load - - self.config.revert_challenge_config() - self.assertEqual(mock_load.call_count, 1) - - def test_revert_challenge_config_error(self): - self.config.reverter.revert_temporary_config = mock.Mock( - side_effect=errors.ReverterError) - - self.assertRaises( - errors.PluginError, self.config.revert_challenge_config) - - def test_rollback_checkpoints(self): - mock_load = mock.Mock() - self.config.aug.load = mock_load - - self.config.rollback_checkpoints() - self.assertEqual(mock_load.call_count, 1) - - def test_rollback_error(self): - self.config.reverter.rollback_checkpoints = mock.Mock( - side_effect=errors.ReverterError) - self.assertRaises(errors.PluginError, self.config.rollback_checkpoints) - - def test_view_config_changes(self): - self.config.view_config_changes() - - def test_view_config_changes_error(self): - self.config.reverter.view_config_changes = mock.Mock( - side_effect=errors.ReverterError) - self.assertRaises(errors.PluginError, self.config.view_config_changes) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/autohsts_test.py b/certbot-apache/certbot_apache/tests/autohsts_test.py deleted file mode 100644 index bf92a13ff87..00000000000 --- a/certbot-apache/certbot_apache/tests/autohsts_test.py +++ /dev/null @@ -1,187 +0,0 @@ -# pylint: disable=too-many-public-methods,too-many-lines -"""Test for certbot_apache.configurator AutoHSTS functionality""" -import re -import unittest -import mock -# six is used in mock.patch() -import six # pylint: disable=unused-import - -from certbot import errors -from certbot_apache import constants -from certbot_apache.tests import util - - -class AutoHSTSTest(util.ApacheTest): - """Tests for AutoHSTS feature""" - # pylint: disable=protected-access - - def setUp(self): # pylint: disable=arguments-differ - super(AutoHSTSTest, self).setUp() - - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, self.work_dir) - self.config.parser.modules.add("headers_module") - self.config.parser.modules.add("mod_headers.c") - self.config.parser.modules.add("ssl_module") - self.config.parser.modules.add("mod_ssl.c") - - self.vh_truth = util.get_vh_truth( - self.temp_dir, "debian_apache_2_4/multiple_vhosts") - - def get_autohsts_value(self, vh_path): - """ Get value from Strict-Transport-Security header """ - header_path = self.config.parser.find_dir("Header", None, vh_path) - if header_path: - pat = '(?:[ "]|^)(strict-transport-security)(?:[ "]|$)' - for head in header_path: - if re.search(pat, self.config.parser.aug.get(head).lower()): - return self.config.parser.aug.get(head.replace("arg[3]", - "arg[4]")) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - @mock.patch("certbot_apache.configurator.ApacheConfigurator.enable_mod") - def test_autohsts_enable_headers_mod(self, mock_enable, _restart): - self.config.parser.modules.discard("headers_module") - self.config.parser.modules.discard("mod_header.c") - self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) - self.assertTrue(mock_enable.called) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - def test_autohsts_deploy_already_exists(self, _restart): - self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) - self.assertRaises(errors.PluginEnhancementAlreadyPresent, - self.config.enable_autohsts, - mock.MagicMock(), ["ocspvhost.com"]) - - @mock.patch("certbot_apache.constants.AUTOHSTS_FREQ", 0) - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - @mock.patch("certbot_apache.configurator.ApacheConfigurator.prepare") - def test_autohsts_increase(self, mock_prepare, _mock_restart): - self.config._prepared = False - maxage = "\"max-age={0}\"" - initial_val = maxage.format(constants.AUTOHSTS_STEPS[0]) - inc_val = maxage.format(constants.AUTOHSTS_STEPS[1]) - - self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) - # Verify initial value - self.assertEqual(self.get_autohsts_value(self.vh_truth[7].path), - initial_val) - # Increase - self.config.update_autohsts(mock.MagicMock()) - # Verify increased value - self.assertEqual(self.get_autohsts_value(self.vh_truth[7].path), - inc_val) - self.assertTrue(mock_prepare.called) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - @mock.patch("certbot_apache.configurator.ApacheConfigurator._autohsts_increase") - def test_autohsts_increase_noop(self, mock_increase, _restart): - maxage = "\"max-age={0}\"" - initial_val = maxage.format(constants.AUTOHSTS_STEPS[0]) - self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) - # Verify initial value - self.assertEqual(self.get_autohsts_value(self.vh_truth[7].path), - initial_val) - - self.config.update_autohsts(mock.MagicMock()) - # Freq not patched, so value shouldn't increase - self.assertFalse(mock_increase.called) - - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - @mock.patch("certbot_apache.constants.AUTOHSTS_FREQ", 0) - def test_autohsts_increase_no_header(self, _restart): - self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) - # Remove the header - dir_locs = self.config.parser.find_dir("Header", None, - self.vh_truth[7].path) - dir_loc = "/".join(dir_locs[0].split("/")[:-1]) - self.config.parser.aug.remove(dir_loc) - self.assertRaises(errors.PluginError, - self.config.update_autohsts, - mock.MagicMock()) - - @mock.patch("certbot_apache.constants.AUTOHSTS_FREQ", 0) - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - def test_autohsts_increase_and_make_permanent(self, _mock_restart): - maxage = "\"max-age={0}\"" - max_val = maxage.format(constants.AUTOHSTS_PERMANENT) - mock_lineage = mock.MagicMock() - mock_lineage.key_path = "/etc/apache2/ssl/key-certbot_15.pem" - self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) - for i in range(len(constants.AUTOHSTS_STEPS)-1): - # Ensure that value is not made permanent prematurely - self.config.deploy_autohsts(mock_lineage) - self.assertNotEqual(self.get_autohsts_value(self.vh_truth[7].path), - max_val) - self.config.update_autohsts(mock.MagicMock()) - # Value should match pre-permanent increment step - cur_val = maxage.format(constants.AUTOHSTS_STEPS[i+1]) - self.assertEqual(self.get_autohsts_value(self.vh_truth[7].path), - cur_val) - # Ensure that the value is raised to max - self.assertEqual(self.get_autohsts_value(self.vh_truth[7].path), - maxage.format(constants.AUTOHSTS_STEPS[-1])) - # Make permanent - self.config.deploy_autohsts(mock_lineage) - self.assertEqual(self.get_autohsts_value(self.vh_truth[7].path), - max_val) - - def test_autohsts_update_noop(self): - with mock.patch("time.time") as mock_time: - # Time mock is used to make sure that the execution does not - # continue when no autohsts entries exist in pluginstorage - self.config.update_autohsts(mock.MagicMock()) - self.assertFalse(mock_time.called) - - def test_autohsts_make_permanent_noop(self): - self.config.storage.put = mock.MagicMock() - self.config.deploy_autohsts(mock.MagicMock()) - # Make sure that the execution does not continue when no entries in store - self.assertFalse(self.config.storage.put.called) - - @mock.patch("certbot_apache.display_ops.select_vhost") - def test_autohsts_no_ssl_vhost(self, mock_select): - mock_select.return_value = self.vh_truth[0] - with mock.patch("certbot_apache.configurator.logger.warning") as mock_log: - self.assertRaises(errors.PluginError, - self.config.enable_autohsts, - mock.MagicMock(), "invalid.example.com") - self.assertTrue( - "Certbot was not able to find SSL" in mock_log.call_args[0][0]) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - @mock.patch("certbot_apache.configurator.ApacheConfigurator.add_vhost_id") - def test_autohsts_dont_enhance_twice(self, mock_id, _restart): - mock_id.return_value = "1234567" - self.config.enable_autohsts(mock.MagicMock(), - ["ocspvhost.com", "ocspvhost.com"]) - self.assertEqual(mock_id.call_count, 1) - - def test_autohsts_remove_orphaned(self): - # pylint: disable=protected-access - self.config._autohsts_fetch_state() - self.config._autohsts["orphan_id"] = {"laststep": 0, "timestamp": 0} - - self.config._autohsts_save_state() - self.config.update_autohsts(mock.MagicMock()) - self.assertFalse("orphan_id" in self.config._autohsts) - # Make sure it's removed from the pluginstorage file as well - self.config._autohsts = None - self.config._autohsts_fetch_state() - self.assertFalse(self.config._autohsts) - - def test_autohsts_make_permanent_vhost_not_found(self): - # pylint: disable=protected-access - self.config._autohsts_fetch_state() - self.config._autohsts["orphan_id"] = {"laststep": 999, "timestamp": 0} - self.config._autohsts_save_state() - with mock.patch("certbot_apache.configurator.logger.warning") as mock_log: - self.config.deploy_autohsts(mock.MagicMock()) - self.assertTrue(mock_log.called) - self.assertTrue( - "VirtualHost with id orphan_id was not" in mock_log.call_args[0][0]) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/centos_test.py b/certbot-apache/certbot_apache/tests/centos_test.py deleted file mode 100644 index a27916c322f..00000000000 --- a/certbot-apache/certbot_apache/tests/centos_test.py +++ /dev/null @@ -1,141 +0,0 @@ -"""Test for certbot_apache.configurator for Centos overrides""" -import os -import unittest - -import mock - -from certbot import errors - -from certbot_apache import obj -from certbot_apache import override_centos -from certbot_apache.tests import util - -def get_vh_truth(temp_dir, config_name): - """Return the ground truth for the specified directory.""" - prefix = os.path.join( - temp_dir, config_name, "httpd/conf.d") - - aug_pre = "/files" + prefix - vh_truth = [ - obj.VirtualHost( - os.path.join(prefix, "centos.example.com.conf"), - os.path.join(aug_pre, "centos.example.com.conf/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), - False, True, "centos.example.com"), - obj.VirtualHost( - os.path.join(prefix, "ssl.conf"), - os.path.join(aug_pre, "ssl.conf/VirtualHost"), - set([obj.Addr.fromstring("_default_:443")]), - True, True, None) - ] - return vh_truth - -class MultipleVhostsTestCentOS(util.ApacheTest): - """Multiple vhost tests for CentOS / RHEL family of distros""" - - _multiprocess_can_split_ = True - - def setUp(self): # pylint: disable=arguments-differ - test_dir = "centos7_apache/apache" - config_root = "centos7_apache/apache/httpd" - vhost_root = "centos7_apache/apache/httpd/conf.d" - super(MultipleVhostsTestCentOS, self).setUp(test_dir=test_dir, - config_root=config_root, - vhost_root=vhost_root) - - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, self.work_dir, - os_info="centos") - self.vh_truth = get_vh_truth( - self.temp_dir, "centos7_apache/apache") - - def test_get_parser(self): - self.assertTrue(isinstance(self.config.parser, - override_centos.CentOSParser)) - - @mock.patch("certbot_apache.parser.ApacheParser._get_runtime_cfg") - def test_opportunistic_httpd_runtime_parsing(self, mock_get): - define_val = ( - 'Define: TEST1\n' - 'Define: TEST2\n' - 'Define: DUMP_RUN_CFG\n' - ) - mod_val = ( - 'Loaded Modules:\n' - ' mock_module (static)\n' - ' another_module (static)\n' - ) - def mock_get_cfg(command): - """Mock httpd process stdout""" - if command == ['apachectl', '-t', '-D', 'DUMP_RUN_CFG']: - return define_val - elif command == ['apachectl', '-t', '-D', 'DUMP_MODULES']: - return mod_val - return "" - mock_get.side_effect = mock_get_cfg - self.config.parser.modules = set() - self.config.parser.variables = {} - - with mock.patch("certbot.util.get_os_info") as mock_osi: - # Make sure we have the have the CentOS httpd constants - mock_osi.return_value = ("centos", "7") - self.config.parser.update_runtime_variables() - - self.assertEqual(mock_get.call_count, 3) - self.assertEqual(len(self.config.parser.modules), 4) - self.assertEqual(len(self.config.parser.variables), 2) - self.assertTrue("TEST2" in self.config.parser.variables.keys()) - self.assertTrue("mod_another.c" in self.config.parser.modules) - - def test_get_virtual_hosts(self): - """Make sure all vhosts are being properly found.""" - vhs = self.config.get_virtual_hosts() - self.assertEqual(len(vhs), 2) - found = 0 - - for vhost in vhs: - for centos_truth in self.vh_truth: - if vhost == centos_truth: - found += 1 - break - else: - raise Exception("Missed: %s" % vhost) # pragma: no cover - self.assertEqual(found, 2) - - @mock.patch("certbot_apache.parser.ApacheParser._get_runtime_cfg") - def test_get_sysconfig_vars(self, mock_cfg): - """Make sure we read the sysconfig OPTIONS variable correctly""" - # Return nothing for the process calls - mock_cfg.return_value = "" - self.config.parser.sysconfig_filep = os.path.realpath( - os.path.join(self.config.parser.root, "../sysconfig/httpd")) - self.config.parser.variables = {} - - with mock.patch("certbot.util.get_os_info") as mock_osi: - # Make sure we have the have the CentOS httpd constants - mock_osi.return_value = ("centos", "7") - self.config.parser.update_runtime_variables() - - self.assertTrue("mock_define" in self.config.parser.variables.keys()) - self.assertTrue("mock_define_too" in self.config.parser.variables.keys()) - self.assertTrue("mock_value" in self.config.parser.variables.keys()) - self.assertEqual("TRUE", self.config.parser.variables["mock_value"]) - self.assertTrue("MOCK_NOSEP" in self.config.parser.variables.keys()) - self.assertEqual("NOSEP_VAL", self.config.parser.variables["NOSEP_TWO"]) - - @mock.patch("certbot_apache.configurator.util.run_script") - def test_alt_restart_works(self, mock_run_script): - mock_run_script.side_effect = [None, errors.SubprocessError, None] - self.config.restart() - self.assertEqual(mock_run_script.call_count, 3) - - @mock.patch("certbot_apache.configurator.util.run_script") - def test_alt_restart_errors(self, mock_run_script): - mock_run_script.side_effect = [None, - errors.SubprocessError, - errors.SubprocessError] - self.assertRaises(errors.MisconfigurationError, self.config.restart) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/complex_parsing_test.py b/certbot-apache/certbot_apache/tests/complex_parsing_test.py deleted file mode 100644 index a296fb0eb37..00000000000 --- a/certbot-apache/certbot_apache/tests/complex_parsing_test.py +++ /dev/null @@ -1,129 +0,0 @@ -"""Tests for certbot_apache.parser.""" -import os -import shutil -import unittest - -from certbot import errors - -from certbot_apache.tests import util - - -class ComplexParserTest(util.ParserTest): - """Apache Parser Test.""" - - def setUp(self): # pylint: disable=arguments-differ - super(ComplexParserTest, self).setUp( - "complex_parsing", "complex_parsing") - - self.setup_variables() - # This needs to happen after due to setup_variables not being run - # until after - self.parser.parse_modules() # pylint: disable=protected-access - - def tearDown(self): - shutil.rmtree(self.temp_dir) - shutil.rmtree(self.config_dir) - shutil.rmtree(self.work_dir) - - def setup_variables(self): - """Set up variables for parser.""" - self.parser.variables.update( - { - "COMPLEX": "", - "tls_port": "1234", - "fnmatch_filename": "test_fnmatch.conf", - "tls_port_str": "1234" - } - ) - - def test_filter_args_num(self): - """Note: This may also fail do to Include conf-enabled/ syntax.""" - matches = self.parser.find_dir("TestArgsDirective") - - self.assertEqual(len(self.parser.filter_args_num(matches, 1)), 3) - self.assertEqual(len(self.parser.filter_args_num(matches, 2)), 2) - self.assertEqual(len(self.parser.filter_args_num(matches, 3)), 1) - - def test_basic_variable_parsing(self): - matches = self.parser.find_dir("TestVariablePort") - - self.assertEqual(len(matches), 1) - self.assertEqual(self.parser.get_arg(matches[0]), "1234") - - def test_basic_variable_parsing_quotes(self): - matches = self.parser.find_dir("TestVariablePortStr") - - self.assertEqual(len(matches), 1) - self.assertEqual(self.parser.get_arg(matches[0]), "1234") - - def test_invalid_variable_parsing(self): - del self.parser.variables["tls_port"] - - matches = self.parser.find_dir("TestVariablePort") - self.assertRaises( - errors.PluginError, self.parser.get_arg, matches[0]) - - def test_basic_ifdefine(self): - self.assertEqual(len(self.parser.find_dir("VAR_DIRECTIVE")), 2) - self.assertEqual(len(self.parser.find_dir("INVALID_VAR_DIRECTIVE")), 0) - - def test_basic_ifmodule(self): - self.assertEqual(len(self.parser.find_dir("MOD_DIRECTIVE")), 2) - self.assertEqual( - len(self.parser.find_dir("INVALID_MOD_DIRECTIVE")), 0) - - def test_nested(self): - self.assertEqual(len(self.parser.find_dir("NESTED_DIRECTIVE")), 3) - self.assertEqual( - len(self.parser.find_dir("INVALID_NESTED_DIRECTIVE")), 0) - - def test_load_modules(self): - """If only first is found, there is bad variable parsing.""" - self.assertTrue("status_module" in self.parser.modules) - self.assertTrue("mod_status.c" in self.parser.modules) - - # This is in an IfDefine - self.assertTrue("ssl_module" in self.parser.modules) - self.assertTrue("mod_ssl.c" in self.parser.modules) - - def verify_fnmatch(self, arg, hit=True): - """Test if Include was correctly parsed.""" - from certbot_apache import parser - self.parser.add_dir(parser.get_aug_path(self.parser.loc["default"]), - "Include", [arg]) - if hit: - self.assertTrue(self.parser.find_dir("FNMATCH_DIRECTIVE")) - else: - self.assertFalse(self.parser.find_dir("FNMATCH_DIRECTIVE")) - - # NOTE: Only run one test per function otherwise you will have - # inf recursion - def test_include(self): - self.verify_fnmatch("test_fnmatch.?onf") - - def test_include_complex(self): - self.verify_fnmatch("../complex_parsing/[te][te]st_*.?onf") - - def test_include_fullpath(self): - self.verify_fnmatch(os.path.join(self.config_path, - "test_fnmatch.conf")) - - def test_include_fullpath_trailing_slash(self): - self.verify_fnmatch(self.config_path + "//") - - def test_include_single_quotes(self): - self.verify_fnmatch("'" + self.config_path + "'") - - def test_include_double_quotes(self): - self.verify_fnmatch('"' + self.config_path + '"') - - def test_include_variable(self): - self.verify_fnmatch("../complex_parsing/${fnmatch_filename}") - - def test_include_missing(self): - # This should miss - self.verify_fnmatch("test_*.onf", False) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/configurator_test.py b/certbot-apache/certbot_apache/tests/configurator_test.py deleted file mode 100644 index a77dbf63753..00000000000 --- a/certbot-apache/certbot_apache/tests/configurator_test.py +++ /dev/null @@ -1,1776 +0,0 @@ -# pylint: disable=too-many-public-methods,too-many-lines -"""Test for certbot_apache.configurator.""" -import os -import shutil -import socket -import tempfile -import unittest - -import mock -# six is used in mock.patch() -import six # pylint: disable=unused-import - -from acme import challenges - -from certbot import achallenges -from certbot import crypto_util -from certbot import errors - -from certbot.tests import acme_util -from certbot.tests import util as certbot_util - -from certbot_apache import apache_util -from certbot_apache import constants -from certbot_apache import parser -from certbot_apache import obj - -from certbot_apache.tests import util - - -class MultipleVhostsTest(util.ApacheTest): - """Test two standard well-configured HTTP vhosts.""" - - - def setUp(self): # pylint: disable=arguments-differ - super(MultipleVhostsTest, self).setUp() - - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, self.work_dir) - self.config = self.mock_deploy_cert(self.config) - self.vh_truth = util.get_vh_truth( - self.temp_dir, "debian_apache_2_4/multiple_vhosts") - - def mock_deploy_cert(self, config): - """A test for a mock deploy cert""" - config.real_deploy_cert = self.config.deploy_cert - - def mocked_deploy_cert(*args, **kwargs): - """a helper to mock a deployed cert""" - g_mod = "certbot_apache.configurator.ApacheConfigurator.enable_mod" - with mock.patch(g_mod): - config.real_deploy_cert(*args, **kwargs) - self.config.deploy_cert = mocked_deploy_cert - return self.config - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.init_augeas") - @mock.patch("certbot_apache.configurator.path_surgery") - def test_prepare_no_install(self, mock_surgery, _init_augeas): - silly_path = {"PATH": "/tmp/nothingness2342"} - mock_surgery.return_value = False - with mock.patch.dict('os.environ', silly_path): - self.assertRaises(errors.NoInstallationError, self.config.prepare) - self.assertEqual(mock_surgery.call_count, 1) - - @mock.patch("certbot_apache.augeas_configurator.AugeasConfigurator.init_augeas") - def test_prepare_no_augeas(self, mock_init_augeas): - """ Test augeas initialization ImportError """ - def side_effect_error(): - """ Side effect error for the test """ - raise ImportError - mock_init_augeas.side_effect = side_effect_error - self.assertRaises( - errors.NoInstallationError, self.config.prepare) - - @mock.patch("certbot_apache.parser.ApacheParser") - @mock.patch("certbot_apache.configurator.util.exe_exists") - def test_prepare_version(self, mock_exe_exists, _): - mock_exe_exists.return_value = True - self.config.version = None - self.config.config_test = mock.Mock() - self.config.get_version = mock.Mock(return_value=(1, 1)) - - self.assertRaises( - errors.NotSupportedError, self.config.prepare) - - @mock.patch("certbot_apache.parser.ApacheParser") - @mock.patch("certbot_apache.configurator.util.exe_exists") - def test_prepare_old_aug(self, mock_exe_exists, _): - mock_exe_exists.return_value = True - self.config.config_test = mock.Mock() - # pylint: disable=protected-access - self.config._check_aug_version = mock.Mock(return_value=False) - self.assertRaises( - errors.NotSupportedError, self.config.prepare) - - def test_prepare_locked(self): - server_root = self.config.conf("server-root") - self.config.config_test = mock.Mock() - os.remove(os.path.join(server_root, ".certbot.lock")) - certbot_util.lock_and_call(self._test_prepare_locked, server_root) - - @mock.patch("certbot_apache.parser.ApacheParser") - @mock.patch("certbot_apache.configurator.util.exe_exists") - def _test_prepare_locked(self, unused_parser, unused_exe_exists): - try: - self.config.prepare() - except errors.PluginError as err: - err_msg = str(err) - self.assertTrue("lock" in err_msg) - self.assertTrue(self.config.conf("server-root") in err_msg) - else: # pragma: no cover - self.fail("Exception wasn't raised!") - - def test_add_parser_arguments(self): # pylint: disable=no-self-use - from certbot_apache.configurator import ApacheConfigurator - # Weak test.. - ApacheConfigurator.add_parser_arguments(mock.MagicMock()) - - def test_add_parser_arguments_all_configurators(self): # pylint: disable=no-self-use - from certbot_apache.entrypoint import OVERRIDE_CLASSES - for cls in OVERRIDE_CLASSES.values(): - cls.add_parser_arguments(mock.MagicMock()) - - def test_all_configurators_defaults_defined(self): - from certbot_apache.entrypoint import OVERRIDE_CLASSES - from certbot_apache.configurator import ApacheConfigurator - parameters = set(ApacheConfigurator.OS_DEFAULTS.keys()) - for cls in OVERRIDE_CLASSES.values(): - self.assertTrue(parameters.issubset(set(cls.OS_DEFAULTS.keys()))) - - def test_constant(self): - self.assertTrue("debian_apache_2_4/multiple_vhosts/apache" in - self.config.option("server_root")) - self.assertEqual(self.config.option("nonexistent"), None) - - @certbot_util.patch_get_utility() - def test_get_all_names(self, mock_getutility): - mock_utility = mock_getutility() - mock_utility.notification = mock.MagicMock(return_value=True) - names = self.config.get_all_names() - self.assertEqual(names, set( - ["certbot.demo", "ocspvhost.com", "encryption-example.demo", - "nonsym.link", "vhost.in.rootconf", "www.certbot.demo"] - )) - - @certbot_util.patch_get_utility() - @mock.patch("certbot_apache.configurator.socket.gethostbyaddr") - def test_get_all_names_addrs(self, mock_gethost, mock_getutility): - mock_gethost.side_effect = [("google.com", "", ""), socket.error] - mock_utility = mock_getutility() - mock_utility.notification.return_value = True - vhost = obj.VirtualHost( - "fp", "ap", - set([obj.Addr(("8.8.8.8", "443")), - obj.Addr(("zombo.com",)), - obj.Addr(("192.168.1.2"))]), - True, False) - - self.config.vhosts.append(vhost) - - names = self.config.get_all_names() - # Names get filtered, only 5 are returned - self.assertEqual(len(names), 8) - self.assertTrue("zombo.com" in names) - self.assertTrue("google.com" in names) - self.assertTrue("certbot.demo" in names) - - def test_get_bad_path(self): - self.assertEqual(apache_util.get_file_path(None), None) - self.assertEqual(apache_util.get_file_path("nonexistent"), None) - self.assertEqual(self.config._create_vhost("nonexistent"), None) # pylint: disable=protected-access - - def test_get_aug_internal_path(self): - from certbot_apache.apache_util import get_internal_aug_path - internal_paths = [ - "Virtualhost", "IfModule/VirtualHost", "VirtualHost", "VirtualHost", - "Macro/VirtualHost", "IfModule/VirtualHost", "VirtualHost", - "IfModule/VirtualHost"] - - for i, internal_path in enumerate(internal_paths): - self.assertEqual( - get_internal_aug_path(self.vh_truth[i].path), internal_path) - - def test_bad_servername_alias(self): - ssl_vh1 = obj.VirtualHost( - "fp1", "ap1", set([obj.Addr(("*", "443"))]), - True, False) - # pylint: disable=protected-access - self.config._add_servernames(ssl_vh1) - self.assertTrue( - self.config._add_servername_alias("oy_vey", ssl_vh1) is None) - - def test_add_servernames_alias(self): - self.config.parser.add_dir( - self.vh_truth[2].path, "ServerAlias", ["*.le.co"]) - # pylint: disable=protected-access - self.config._add_servernames(self.vh_truth[2]) - self.assertEqual( - self.vh_truth[2].get_names(), set(["*.le.co", "ip-172-30-0-17"])) - - def test_get_virtual_hosts(self): - """Make sure all vhosts are being properly found.""" - vhs = self.config.get_virtual_hosts() - self.assertEqual(len(vhs), 10) - found = 0 - - for vhost in vhs: - for truth in self.vh_truth: - if vhost == truth: - found += 1 - break - else: - raise Exception("Missed: %s" % vhost) # pragma: no cover - - self.assertEqual(found, 10) - - # Handle case of non-debian layout get_virtual_hosts - with mock.patch( - "certbot_apache.configurator.ApacheConfigurator.conf" - ) as mock_conf: - mock_conf.return_value = False - vhs = self.config.get_virtual_hosts() - self.assertEqual(len(vhs), 10) - - @mock.patch("certbot_apache.display_ops.select_vhost") - def test_choose_vhost_none_avail(self, mock_select): - mock_select.return_value = None - self.assertRaises( - errors.PluginError, self.config.choose_vhost, "none.com") - - @mock.patch("certbot_apache.display_ops.select_vhost") - def test_choose_vhost_select_vhost_ssl(self, mock_select): - mock_select.return_value = self.vh_truth[1] - self.assertEqual( - self.vh_truth[1], self.config.choose_vhost("none.com")) - - @mock.patch("certbot_apache.display_ops.select_vhost") - @mock.patch("certbot_apache.obj.VirtualHost.conflicts") - def test_choose_vhost_select_vhost_non_ssl(self, mock_conf, mock_select): - mock_select.return_value = self.vh_truth[0] - mock_conf.return_value = False - chosen_vhost = self.config.choose_vhost("none.com") - self.vh_truth[0].aliases.add("none.com") - self.assertEqual( - self.vh_truth[0].get_names(), chosen_vhost.get_names()) - - # Make sure we go from HTTP -> HTTPS - self.assertFalse(self.vh_truth[0].ssl) - self.assertTrue(chosen_vhost.ssl) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator._find_best_vhost") - @mock.patch("certbot_apache.parser.ApacheParser.add_dir") - def test_choose_vhost_and_servername_addition(self, mock_add, mock_find): - ret_vh = self.vh_truth[8] - ret_vh.enabled = False - mock_find.return_value = self.vh_truth[8] - self.config.choose_vhost("whatever.com") - self.assertTrue(mock_add.called) - - @mock.patch("certbot_apache.display_ops.select_vhost") - def test_choose_vhost_select_vhost_with_temp(self, mock_select): - mock_select.return_value = self.vh_truth[0] - chosen_vhost = self.config.choose_vhost("none.com", create_if_no_ssl=False) - self.assertEqual(self.vh_truth[0], chosen_vhost) - - @mock.patch("certbot_apache.display_ops.select_vhost") - def test_choose_vhost_select_vhost_conflicting_non_ssl(self, mock_select): - mock_select.return_value = self.vh_truth[3] - conflicting_vhost = obj.VirtualHost( - "path", "aug_path", set([obj.Addr.fromstring("*:443")]), - True, True) - self.config.vhosts.append(conflicting_vhost) - - self.assertRaises( - errors.PluginError, self.config.choose_vhost, "none.com") - - def test_find_best_http_vhost_default(self): - vh = obj.VirtualHost( - "fp", "ap", set([obj.Addr.fromstring("_default_:80")]), False, True) - self.config.vhosts = [vh] - self.assertEqual(self.config.find_best_http_vhost("foo.bar", False), vh) - - def test_find_best_http_vhost_port(self): - port = "8080" - vh = obj.VirtualHost( - "fp", "ap", set([obj.Addr.fromstring("*:" + port)]), - False, True, "encryption-example.demo") - self.config.vhosts.append(vh) - self.assertEqual(self.config.find_best_http_vhost("foo.bar", False, port), vh) - - def test_findbest_continues_on_short_domain(self): - # pylint: disable=protected-access - chosen_vhost = self.config._find_best_vhost("purple.com") - self.assertEqual(None, chosen_vhost) - - def test_findbest_continues_on_long_domain(self): - # pylint: disable=protected-access - chosen_vhost = self.config._find_best_vhost("green.red.purple.com") - self.assertEqual(None, chosen_vhost) - - def test_find_best_vhost(self): - # pylint: disable=protected-access - self.assertEqual( - self.vh_truth[3], self.config._find_best_vhost("certbot.demo")) - self.assertEqual( - self.vh_truth[0], - self.config._find_best_vhost("encryption-example.demo")) - self.assertEqual( - self.config._find_best_vhost("does-not-exist.com"), None) - - def test_find_best_vhost_variety(self): - # pylint: disable=protected-access - ssl_vh = obj.VirtualHost( - "fp", "ap", set([obj.Addr(("*", "443")), - obj.Addr(("zombo.com",))]), - True, False) - self.config.vhosts.append(ssl_vh) - self.assertEqual(self.config._find_best_vhost("zombo.com"), ssl_vh) - - def test_find_best_vhost_default(self): - # pylint: disable=protected-access - # Assume only the two default vhosts. - self.config.vhosts = [ - vh for vh in self.config.vhosts - if vh.name not in ["certbot.demo", "nonsym.link", - "encryption-example.demo", - "ocspvhost.com", "vhost.in.rootconf"] - and "*.blue.purple.com" not in vh.aliases - ] - self.assertEqual( - self.config._find_best_vhost("encryption-example.demo"), - self.vh_truth[2]) - - def test_non_default_vhosts(self): - # pylint: disable=protected-access - vhosts = self.config._non_default_vhosts(self.config.vhosts) - self.assertEqual(len(vhosts), 8) - - def test_deploy_cert_enable_new_vhost(self): - # Create - ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[0]) - self.config.parser.modules.add("ssl_module") - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("socache_shmcb_module") - - self.assertFalse(ssl_vhost.enabled) - self.config.deploy_cert( - "encryption-example.demo", "example/cert.pem", "example/key.pem", - "example/cert_chain.pem", "example/fullchain.pem") - self.assertTrue(ssl_vhost.enabled) - - def test_no_duplicate_include(self): - def mock_find_dir(directive, argument, _): - """Mock method for parser.find_dir""" - if directive == "Include" and argument.endswith("options-ssl-apache.conf"): - return ["/path/to/whatever"] - - mock_add = mock.MagicMock() - self.config.parser.add_dir = mock_add - self.config._add_dummy_ssl_directives(self.vh_truth[0]) # pylint: disable=protected-access - tried_to_add = False - for a in mock_add.call_args_list: - if a[0][1] == "Include" and a[0][2] == self.config.mod_ssl_conf: - tried_to_add = True - # Include should be added, find_dir is not patched, and returns falsy - self.assertTrue(tried_to_add) - - self.config.parser.find_dir = mock_find_dir - mock_add.reset_mock() - self.config._add_dummy_ssl_directives(self.vh_truth[0]) # pylint: disable=protected-access - for a in mock_add.call_args_list: - if a[0][1] == "Include" and a[0][2] == self.config.mod_ssl_conf: - self.fail("Include shouldn't be added, as patched find_dir 'finds' existing one") \ - # pragma: no cover - - def test_deploy_cert(self): - self.config.parser.modules.add("ssl_module") - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("socache_shmcb_module") - # Patch _add_dummy_ssl_directives to make sure we write them correctly - # pylint: disable=protected-access - orig_add_dummy = self.config._add_dummy_ssl_directives - def mock_add_dummy_ssl(vhostpath): - """Mock method for _add_dummy_ssl_directives""" - def find_args(path, directive): - """Return list of arguments in requested directive at path""" - f_args = [] - dirs = self.config.parser.find_dir(directive, None, - path) - for d in dirs: - f_args.append(self.config.parser.get_arg(d)) - return f_args - # Verify that the dummy directives do not exist - self.assertFalse( - "insert_cert_file_path" in find_args(vhostpath, - "SSLCertificateFile")) - self.assertFalse( - "insert_key_file_path" in find_args(vhostpath, - "SSLCertificateKeyFile")) - orig_add_dummy(vhostpath) - # Verify that the dummy directives exist - self.assertTrue( - "insert_cert_file_path" in find_args(vhostpath, - "SSLCertificateFile")) - self.assertTrue( - "insert_key_file_path" in find_args(vhostpath, - "SSLCertificateKeyFile")) - # pylint: disable=protected-access - self.config._add_dummy_ssl_directives = mock_add_dummy_ssl - - # Get the default 443 vhost - self.config.assoc["random.demo"] = self.vh_truth[1] - self.config.deploy_cert( - "random.demo", - "example/cert.pem", "example/key.pem", "example/cert_chain.pem") - self.config.save() - - # Verify ssl_module was enabled. - self.assertTrue(self.vh_truth[1].enabled) - self.assertTrue("ssl_module" in self.config.parser.modules) - - loc_cert = self.config.parser.find_dir( - "sslcertificatefile", "example/cert.pem", self.vh_truth[1].path) - loc_key = self.config.parser.find_dir( - "sslcertificateKeyfile", "example/key.pem", self.vh_truth[1].path) - loc_chain = self.config.parser.find_dir( - "SSLCertificateChainFile", "example/cert_chain.pem", - self.vh_truth[1].path) - - # Verify one directive was found in the correct file - self.assertEqual(len(loc_cert), 1) - self.assertEqual( - apache_util.get_file_path(loc_cert[0]), - self.vh_truth[1].filep) - - self.assertEqual(len(loc_key), 1) - self.assertEqual( - apache_util.get_file_path(loc_key[0]), - self.vh_truth[1].filep) - - self.assertEqual(len(loc_chain), 1) - self.assertEqual( - apache_util.get_file_path(loc_chain[0]), - self.vh_truth[1].filep) - - # One more time for chain directive setting - self.config.deploy_cert( - "random.demo", - "two/cert.pem", "two/key.pem", "two/cert_chain.pem") - self.assertTrue(self.config.parser.find_dir( - "SSLCertificateChainFile", "two/cert_chain.pem", - self.vh_truth[1].path)) - - def test_deploy_cert_invalid_vhost(self): - """For test cases where the `ApacheConfigurator` class' `_deploy_cert` - method is called with an invalid vhost parameter. Currently this tests - that a PluginError is appropriately raised when important directives - are missing in an SSL module.""" - self.config.parser.modules.add("ssl_module") - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("socache_shmcb_module") - - def side_effect(*args): - """Mocks case where an SSLCertificateFile directive can be found - but an SSLCertificateKeyFile directive is missing.""" - if "SSLCertificateFile" in args: - return ["example/cert.pem"] - else: - return [] - - mock_find_dir = mock.MagicMock(return_value=[]) - mock_find_dir.side_effect = side_effect - - self.config.parser.find_dir = mock_find_dir - - # Get the default 443 vhost - self.config.assoc["random.demo"] = self.vh_truth[1] - - self.assertRaises( - errors.PluginError, self.config.deploy_cert, "random.demo", - "example/cert.pem", "example/key.pem", "example/cert_chain.pem") - - # Remove side_effect to mock case where both SSLCertificateFile - # and SSLCertificateKeyFile directives are missing - self.config.parser.find_dir.side_effect = None - self.assertRaises( - errors.PluginError, self.config.deploy_cert, "random.demo", - "example/cert.pem", "example/key.pem", "example/cert_chain.pem") - - def test_is_name_vhost(self): - addr = obj.Addr.fromstring("*:80") - self.assertTrue(self.config.is_name_vhost(addr)) - self.config.version = (2, 2) - self.assertFalse(self.config.is_name_vhost(addr)) - - def test_add_name_vhost(self): - self.config.add_name_vhost(obj.Addr.fromstring("*:443")) - self.config.add_name_vhost(obj.Addr.fromstring("*:80")) - self.assertTrue(self.config.parser.find_dir( - "NameVirtualHost", "*:443", exclude=False)) - self.assertTrue(self.config.parser.find_dir( - "NameVirtualHost", "*:80")) - - def test_add_listen_80(self): - mock_find = mock.Mock() - mock_add_dir = mock.Mock() - mock_find.return_value = [] - self.config.parser.find_dir = mock_find - self.config.parser.add_dir = mock_add_dir - self.config.ensure_listen("80") - self.assertTrue(mock_add_dir.called) - self.assertTrue(mock_find.called) - self.assertEqual(mock_add_dir.call_args[0][1], "Listen") - self.assertEqual(mock_add_dir.call_args[0][2], "80") - - def test_add_listen_80_named(self): - mock_find = mock.Mock() - mock_find.return_value = ["test1", "test2", "test3"] - mock_get = mock.Mock() - mock_get.side_effect = ["1.2.3.4:80", "[::1]:80", "1.1.1.1:443"] - mock_add_dir = mock.Mock() - - self.config.parser.find_dir = mock_find - self.config.parser.get_arg = mock_get - self.config.parser.add_dir = mock_add_dir - - self.config.ensure_listen("80") - self.assertEqual(mock_add_dir.call_count, 0) - - # Reset return lists and inputs - mock_add_dir.reset_mock() - mock_get.side_effect = ["1.2.3.4:80", "[::1]:80", "1.1.1.1:443"] - - # Test - self.config.ensure_listen("8080") - self.assertEqual(mock_add_dir.call_count, 3) - self.assertTrue(mock_add_dir.called) - self.assertEqual(mock_add_dir.call_args[0][1], "Listen") - call_found = False - for mock_call in mock_add_dir.mock_calls: - if mock_call[1][2] == ['1.2.3.4:8080']: - call_found = True - self.assertTrue(call_found) - - def test_prepare_server_https(self): - mock_enable = mock.Mock() - self.config.enable_mod = mock_enable - - mock_find = mock.Mock() - mock_add_dir = mock.Mock() - mock_find.return_value = [] - - # This will test the Add listen - self.config.parser.find_dir = mock_find - self.config.parser.add_dir_to_ifmodssl = mock_add_dir - self.config.prepare_server_https("443") - # Changing the order these modules are enabled breaks the reverter - self.assertEqual(mock_enable.call_args_list[0][0][0], "socache_shmcb") - self.assertEqual(mock_enable.call_args[0][0], "ssl") - self.assertEqual(mock_enable.call_args[1], {"temp": False}) - - self.config.prepare_server_https("8080", temp=True) - # Changing the order these modules are enabled breaks the reverter - self.assertEqual(mock_enable.call_args_list[2][0][0], "socache_shmcb") - self.assertEqual(mock_enable.call_args[0][0], "ssl") - # Enable mod is temporary - self.assertEqual(mock_enable.call_args[1], {"temp": True}) - - self.assertEqual(mock_add_dir.call_count, 2) - - def test_prepare_server_https_named_listen(self): - mock_find = mock.Mock() - mock_find.return_value = ["test1", "test2", "test3"] - mock_get = mock.Mock() - mock_get.side_effect = ["1.2.3.4:80", "[::1]:80", "1.1.1.1:443"] - mock_add_dir = mock.Mock() - mock_enable = mock.Mock() - - self.config.parser.find_dir = mock_find - self.config.parser.get_arg = mock_get - self.config.parser.add_dir_to_ifmodssl = mock_add_dir - self.config.enable_mod = mock_enable - - # Test Listen statements with specific ip listeed - self.config.prepare_server_https("443") - # Should be 0 as one interface already listens to 443 - self.assertEqual(mock_add_dir.call_count, 0) - - # Reset return lists and inputs - mock_add_dir.reset_mock() - mock_get.side_effect = ["1.2.3.4:80", "[::1]:80", "1.1.1.1:443"] - - # Test - self.config.prepare_server_https("8080", temp=True) - self.assertEqual(mock_add_dir.call_count, 3) - call_args_list = [mock_add_dir.call_args_list[i][0][2] for i in range(3)] - self.assertEqual( - sorted(call_args_list), - sorted([["1.2.3.4:8080", "https"], - ["[::1]:8080", "https"], - ["1.1.1.1:8080", "https"]])) - - # mock_get.side_effect = ["1.2.3.4:80", "[::1]:80"] - # mock_find.return_value = ["test1", "test2", "test3"] - # self.config.parser.get_arg = mock_get - # self.config.prepare_server_https("8080", temp=True) - # self.assertEqual(self.listens, 0) - - def test_prepare_server_https_needed_listen(self): - mock_find = mock.Mock() - mock_find.return_value = ["test1", "test2"] - mock_get = mock.Mock() - mock_get.side_effect = ["1.2.3.4:8080", "80"] - mock_add_dir = mock.Mock() - mock_enable = mock.Mock() - - self.config.parser.find_dir = mock_find - self.config.parser.get_arg = mock_get - self.config.parser.add_dir_to_ifmodssl = mock_add_dir - self.config.enable_mod = mock_enable - - self.config.prepare_server_https("443") - self.assertEqual(mock_add_dir.call_count, 1) - - def test_prepare_server_https_mixed_listen(self): - - mock_find = mock.Mock() - mock_find.return_value = ["test1", "test2"] - mock_get = mock.Mock() - mock_get.side_effect = ["1.2.3.4:8080", "443"] - mock_add_dir = mock.Mock() - mock_enable = mock.Mock() - - self.config.parser.find_dir = mock_find - self.config.parser.get_arg = mock_get - self.config.parser.add_dir_to_ifmodssl = mock_add_dir - self.config.enable_mod = mock_enable - - # Test Listen statements with specific ip listeed - self.config.prepare_server_https("443") - # Should only be 2 here, as the third interface - # already listens to the correct port - self.assertEqual(mock_add_dir.call_count, 0) - - def test_make_vhost_ssl_with_mock_span(self): - # span excludes the closing tag in older versions - # of Augeas - return_value = [self.vh_truth[0].filep, 1, 12, 0, 0, 0, 1142] - with mock.patch.object(self.config.aug, 'span') as mock_span: - mock_span.return_value = return_value - self.test_make_vhost_ssl() - - def test_make_vhost_ssl_with_mock_span2(self): - # span includes the closing tag in newer versions - # of Augeas - return_value = [self.vh_truth[0].filep, 1, 12, 0, 0, 0, 1157] - with mock.patch.object(self.config.aug, 'span') as mock_span: - mock_span.return_value = return_value - self.test_make_vhost_ssl() - - def test_make_vhost_ssl_nonsymlink(self): - ssl_vhost_slink = self.config.make_vhost_ssl(self.vh_truth[8]) - self.assertTrue(ssl_vhost_slink.ssl) - self.assertTrue(ssl_vhost_slink.enabled) - self.assertEqual(ssl_vhost_slink.name, "nonsym.link") - - def test_make_vhost_ssl_nonexistent_vhost_path(self): - ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[1]) - self.assertEqual(os.path.dirname(ssl_vhost.filep), - os.path.dirname(os.path.realpath( - self.vh_truth[1].filep))) - - def test_make_vhost_ssl(self): - ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[0]) - - self.assertEqual( - ssl_vhost.filep, - os.path.join(self.config_path, "sites-available", - "encryption-example-le-ssl.conf")) - - self.assertEqual(ssl_vhost.path, - "/files" + ssl_vhost.filep + "/IfModule/Virtualhost") - self.assertEqual(len(ssl_vhost.addrs), 1) - self.assertEqual(set([obj.Addr.fromstring("*:443")]), ssl_vhost.addrs) - self.assertEqual(ssl_vhost.name, "encryption-example.demo") - self.assertTrue(ssl_vhost.ssl) - self.assertFalse(ssl_vhost.enabled) - - self.assertEqual(self.config.is_name_vhost(self.vh_truth[0]), - self.config.is_name_vhost(ssl_vhost)) - - self.assertEqual(len(self.config.vhosts), 11) - - def test_clean_vhost_ssl(self): - # pylint: disable=protected-access - for directive in ["SSLCertificateFile", "SSLCertificateKeyFile", - "SSLCertificateChainFile", "SSLCACertificatePath"]: - for _ in range(10): - self.config.parser.add_dir(self.vh_truth[1].path, - directive, ["bogus"]) - self.config.save() - - self.config._clean_vhost(self.vh_truth[1]) - self.config.save() - - loc_cert = self.config.parser.find_dir( - 'SSLCertificateFile', None, self.vh_truth[1].path, False) - loc_key = self.config.parser.find_dir( - 'SSLCertificateKeyFile', None, self.vh_truth[1].path, False) - loc_chain = self.config.parser.find_dir( - 'SSLCertificateChainFile', None, self.vh_truth[1].path, False) - loc_cacert = self.config.parser.find_dir( - 'SSLCACertificatePath', None, self.vh_truth[1].path, False) - - self.assertEqual(len(loc_cert), 1) - self.assertEqual(len(loc_key), 1) - - self.assertEqual(len(loc_chain), 0) - - self.assertEqual(len(loc_cacert), 10) - - def test_deduplicate_directives(self): - # pylint: disable=protected-access - DIRECTIVE = "Foo" - for _ in range(10): - self.config.parser.add_dir(self.vh_truth[1].path, - DIRECTIVE, ["bar"]) - self.config.save() - - self.config._deduplicate_directives(self.vh_truth[1].path, [DIRECTIVE]) - self.config.save() - - self.assertEqual( - len(self.config.parser.find_dir( - DIRECTIVE, None, self.vh_truth[1].path, False)), 1) - - def test_remove_directives(self): - # pylint: disable=protected-access - DIRECTIVES = ["Foo", "Bar"] - for directive in DIRECTIVES: - for _ in range(10): - self.config.parser.add_dir(self.vh_truth[2].path, - directive, ["baz"]) - self.config.save() - - self.config._remove_directives(self.vh_truth[2].path, DIRECTIVES) - self.config.save() - - for directive in DIRECTIVES: - self.assertEqual( - len(self.config.parser.find_dir( - directive, None, self.vh_truth[2].path, False)), 0) - - def test_make_vhost_ssl_bad_write(self): - mock_open = mock.mock_open() - # This calls open - self.config.reverter.register_file_creation = mock.Mock() - mock_open.side_effect = IOError - with mock.patch("six.moves.builtins.open", mock_open): - self.assertRaises( - errors.PluginError, - self.config.make_vhost_ssl, self.vh_truth[0]) - - def test_get_ssl_vhost_path(self): - # pylint: disable=protected-access - self.assertTrue( - self.config._get_ssl_vhost_path("example_path").endswith(".conf")) - - def test_add_name_vhost_if_necessary(self): - # pylint: disable=protected-access - self.config.add_name_vhost = mock.Mock() - self.config.version = (2, 2) - self.config._add_name_vhost_if_necessary(self.vh_truth[0]) - self.assertTrue(self.config.add_name_vhost.called) - - new_addrs = set() - for addr in self.vh_truth[0].addrs: - new_addrs.add(obj.Addr(("_default_", addr.get_port(),))) - - self.vh_truth[0].addrs = new_addrs - self.config._add_name_vhost_if_necessary(self.vh_truth[0]) - self.assertEqual(self.config.add_name_vhost.call_count, 2) - - @mock.patch("certbot_apache.configurator.http_01.ApacheHttp01.perform") - @mock.patch("certbot_apache.configurator.tls_sni_01.ApacheTlsSni01.perform") - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - def test_perform(self, mock_restart, mock_tls_perform, mock_http_perform): - # Only tests functionality specific to configurator.perform - # Note: As more challenges are offered this will have to be expanded - account_key, achalls = self.get_key_and_achalls() - - all_expected = [] - http_expected = [] - tls_expected = [] - for achall in achalls: - response = achall.response(account_key) - if isinstance(achall.chall, challenges.HTTP01): - http_expected.append(response) - else: - tls_expected.append(response) - all_expected.append(response) - - mock_http_perform.return_value = http_expected - mock_tls_perform.return_value = tls_expected - - responses = self.config.perform(achalls) - - self.assertEqual(mock_http_perform.call_count, 1) - self.assertEqual(mock_tls_perform.call_count, 1) - self.assertEqual(responses, all_expected) - - self.assertEqual(mock_restart.call_count, 1) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - @mock.patch("certbot_apache.parser.ApacheParser._get_runtime_cfg") - def test_cleanup(self, mock_cfg, mock_restart): - mock_cfg.return_value = "" - _, achalls = self.get_key_and_achalls() - - for achall in achalls: - self.config._chall_out.add(achall) # pylint: disable=protected-access - - for i, achall in enumerate(achalls): - self.config.cleanup([achall]) - if i == len(achalls) - 1: - self.assertTrue(mock_restart.called) - else: - self.assertFalse(mock_restart.called) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.restart") - @mock.patch("certbot_apache.parser.ApacheParser._get_runtime_cfg") - def test_cleanup_no_errors(self, mock_cfg, mock_restart): - mock_cfg.return_value = "" - _, achalls = self.get_key_and_achalls() - self.config.http_doer = mock.MagicMock() - - for achall in achalls: - self.config._chall_out.add(achall) # pylint: disable=protected-access - - self.config.cleanup([achalls[-1]]) - self.assertFalse(mock_restart.called) - - self.config.cleanup(achalls) - self.assertTrue(mock_restart.called) - - @mock.patch("certbot.util.run_script") - def test_get_version(self, mock_script): - mock_script.return_value = ( - "Server Version: Apache/2.4.2 (Debian)", "") - self.assertEqual(self.config.get_version(), (2, 4, 2)) - - mock_script.return_value = ( - "Server Version: Apache/2 (Linux)", "") - self.assertEqual(self.config.get_version(), (2,)) - - mock_script.return_value = ( - "Server Version: Apache (Debian)", "") - self.assertRaises(errors.PluginError, self.config.get_version) - - mock_script.return_value = ( - "Server Version: Apache/2.3{0} Apache/2.4.7".format( - os.linesep), "") - self.assertRaises(errors.PluginError, self.config.get_version) - - mock_script.side_effect = errors.SubprocessError("Can't find program") - self.assertRaises(errors.PluginError, self.config.get_version) - - @mock.patch("certbot_apache.configurator.util.run_script") - def test_restart(self, _): - self.config.restart() - - @mock.patch("certbot_apache.configurator.util.run_script") - def test_restart_bad_process(self, mock_run_script): - mock_run_script.side_effect = [None, errors.SubprocessError] - - self.assertRaises(errors.MisconfigurationError, self.config.restart) - - @mock.patch("certbot.util.run_script") - def test_config_test(self, _): - self.config.config_test() - - @mock.patch("certbot.util.run_script") - def test_config_test_bad_process(self, mock_run_script): - mock_run_script.side_effect = errors.SubprocessError - - self.assertRaises(errors.MisconfigurationError, - self.config.config_test) - - def test_more_info(self): - self.assertTrue(self.config.more_info()) - - def test_get_chall_pref(self): - self.assertTrue(isinstance(self.config.get_chall_pref(""), list)) - - def test_install_ssl_options_conf(self): - path = os.path.join(self.work_dir, "test_it") - other_path = os.path.join(self.work_dir, "other_test_it") - self.config.install_ssl_options_conf(path, other_path) - self.assertTrue(os.path.isfile(path)) - self.assertTrue(os.path.isfile(other_path)) - - # TEST ENHANCEMENTS - def test_supported_enhancements(self): - self.assertTrue(isinstance(self.config.supported_enhancements(), list)) - - def test_find_http_vhost_without_ancestor(self): - # pylint: disable=protected-access - vhost = self.vh_truth[0] - vhost.ssl = True - vhost.ancestor = None - res = self.config._get_http_vhost(vhost) - self.assertEqual(self.vh_truth[0].name, res.name) - self.assertEqual(self.vh_truth[0].aliases, res.aliases) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator._get_http_vhost") - @mock.patch("certbot_apache.display_ops.select_vhost") - @mock.patch("certbot.util.exe_exists") - def test_enhance_unknown_vhost(self, mock_exe, mock_sel_vhost, mock_get): - self.config.parser.modules.add("rewrite_module") - mock_exe.return_value = True - ssl_vh1 = obj.VirtualHost( - "fp1", "ap1", set([obj.Addr(("*", "443"))]), - True, False) - ssl_vh1.name = "satoshi.com" - self.config.vhosts.append(ssl_vh1) - mock_sel_vhost.return_value = None - mock_get.return_value = None - - self.assertRaises( - errors.PluginError, - self.config.enhance, "satoshi.com", "redirect") - - def test_enhance_unknown_enhancement(self): - self.assertRaises( - errors.PluginError, - self.config.enhance, "certbot.demo", "unknown_enhancement") - - def test_enhance_no_ssl_vhost(self): - with mock.patch("certbot_apache.configurator.logger.warning") as mock_log: - self.assertRaises(errors.PluginError, self.config.enhance, - "certbot.demo", "redirect") - # Check that correct logger.warning was printed - self.assertTrue("not able to find" in mock_log.call_args[0][0]) - self.assertTrue("\"redirect\"" in mock_log.call_args[0][0]) - - mock_log.reset_mock() - - self.assertRaises(errors.PluginError, self.config.enhance, - "certbot.demo", "ensure-http-header", "Test") - # Check that correct logger.warning was printed - self.assertTrue("not able to find" in mock_log.call_args[0][0]) - self.assertTrue("Test" in mock_log.call_args[0][0]) - - @mock.patch("certbot.util.exe_exists") - def test_ocsp_stapling(self, mock_exe): - self.config.parser.update_runtime_variables = mock.Mock() - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("socache_shmcb_module") - self.config.get_version = mock.Mock(return_value=(2, 4, 7)) - mock_exe.return_value = True - - # This will create an ssl vhost for certbot.demo - self.config.choose_vhost("certbot.demo") - self.config.enhance("certbot.demo", "staple-ocsp") - - # Get the ssl vhost for certbot.demo - ssl_vhost = self.config.assoc["certbot.demo"] - - ssl_use_stapling_aug_path = self.config.parser.find_dir( - "SSLUseStapling", "on", ssl_vhost.path) - - self.assertEqual(len(ssl_use_stapling_aug_path), 1) - - ssl_vhost_aug_path = parser.get_aug_path(ssl_vhost.filep) - stapling_cache_aug_path = self.config.parser.find_dir('SSLStaplingCache', - "shmcb:/var/run/apache2/stapling_cache(128000)", - ssl_vhost_aug_path) - - self.assertEqual(len(stapling_cache_aug_path), 1) - - @mock.patch("certbot.util.exe_exists") - def test_ocsp_stapling_twice(self, mock_exe): - self.config.parser.update_runtime_variables = mock.Mock() - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("socache_shmcb_module") - self.config.get_version = mock.Mock(return_value=(2, 4, 7)) - mock_exe.return_value = True - - # Checking the case with already enabled ocsp stapling configuration - self.config.choose_vhost("ocspvhost.com") - self.config.enhance("ocspvhost.com", "staple-ocsp") - - # Get the ssl vhost for letsencrypt.demo - ssl_vhost = self.config.assoc["ocspvhost.com"] - - ssl_use_stapling_aug_path = self.config.parser.find_dir( - "SSLUseStapling", "on", ssl_vhost.path) - - self.assertEqual(len(ssl_use_stapling_aug_path), 1) - ssl_vhost_aug_path = parser.get_aug_path(ssl_vhost.filep) - stapling_cache_aug_path = self.config.parser.find_dir('SSLStaplingCache', - "shmcb:/var/run/apache2/stapling_cache(128000)", - ssl_vhost_aug_path) - - self.assertEqual(len(stapling_cache_aug_path), 1) - - - @mock.patch("certbot.util.exe_exists") - def test_ocsp_unsupported_apache_version(self, mock_exe): - mock_exe.return_value = True - self.config.parser.update_runtime_variables = mock.Mock() - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("socache_shmcb_module") - self.config.get_version = mock.Mock(return_value=(2, 2, 0)) - self.config.choose_vhost("certbot.demo") - - self.assertRaises(errors.PluginError, - self.config.enhance, "certbot.demo", "staple-ocsp") - - - def test_get_http_vhost_third_filter(self): - ssl_vh = obj.VirtualHost( - "fp", "ap", set([obj.Addr(("*", "443"))]), - True, False) - ssl_vh.name = "satoshi.com" - self.config.vhosts.append(ssl_vh) - - # pylint: disable=protected-access - http_vh = self.config._get_http_vhost(ssl_vh) - self.assertTrue(http_vh.ssl == False) - - @mock.patch("certbot.util.run_script") - @mock.patch("certbot.util.exe_exists") - def test_http_header_hsts(self, mock_exe, _): - self.config.parser.update_runtime_variables = mock.Mock() - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("headers_module") - mock_exe.return_value = True - - # This will create an ssl vhost for certbot.demo - self.config.choose_vhost("certbot.demo") - self.config.enhance("certbot.demo", "ensure-http-header", - "Strict-Transport-Security") - - # Get the ssl vhost for certbot.demo - ssl_vhost = self.config.assoc["certbot.demo"] - - # These are not immediately available in find_dir even with save() and - # load(). They must be found in sites-available - hsts_header = self.config.parser.find_dir( - "Header", None, ssl_vhost.path) - - # four args to HSTS header - self.assertEqual(len(hsts_header), 4) - - def test_http_header_hsts_twice(self): - self.config.parser.modules.add("mod_ssl.c") - # skip the enable mod - self.config.parser.modules.add("headers_module") - - # This will create an ssl vhost for encryption-example.demo - self.config.choose_vhost("encryption-example.demo") - self.config.enhance("encryption-example.demo", "ensure-http-header", - "Strict-Transport-Security") - - self.assertRaises( - errors.PluginEnhancementAlreadyPresent, - self.config.enhance, "encryption-example.demo", - "ensure-http-header", "Strict-Transport-Security") - - @mock.patch("certbot.util.run_script") - @mock.patch("certbot.util.exe_exists") - def test_http_header_uir(self, mock_exe, _): - self.config.parser.update_runtime_variables = mock.Mock() - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("headers_module") - - mock_exe.return_value = True - - # This will create an ssl vhost for certbot.demo - self.config.choose_vhost("certbot.demo") - self.config.enhance("certbot.demo", "ensure-http-header", - "Upgrade-Insecure-Requests") - - self.assertTrue("headers_module" in self.config.parser.modules) - - # Get the ssl vhost for certbot.demo - ssl_vhost = self.config.assoc["certbot.demo"] - - # These are not immediately available in find_dir even with save() and - # load(). They must be found in sites-available - uir_header = self.config.parser.find_dir( - "Header", None, ssl_vhost.path) - - # four args to HSTS header - self.assertEqual(len(uir_header), 4) - - def test_http_header_uir_twice(self): - self.config.parser.modules.add("mod_ssl.c") - # skip the enable mod - self.config.parser.modules.add("headers_module") - - # This will create an ssl vhost for encryption-example.demo - self.config.choose_vhost("encryption-example.demo") - self.config.enhance("encryption-example.demo", "ensure-http-header", - "Upgrade-Insecure-Requests") - - self.assertRaises( - errors.PluginEnhancementAlreadyPresent, - self.config.enhance, "encryption-example.demo", - "ensure-http-header", "Upgrade-Insecure-Requests") - - @mock.patch("certbot.util.run_script") - @mock.patch("certbot.util.exe_exists") - def test_redirect_well_formed_http(self, mock_exe, _): - self.config.parser.modules.add("rewrite_module") - self.config.parser.update_runtime_variables = mock.Mock() - mock_exe.return_value = True - self.config.get_version = mock.Mock(return_value=(2, 2)) - - # This will create an ssl vhost for certbot.demo - self.config.choose_vhost("certbot.demo") - self.config.enhance("certbot.demo", "redirect") - - # These are not immediately available in find_dir even with save() and - # load(). They must be found in sites-available - rw_engine = self.config.parser.find_dir( - "RewriteEngine", "on", self.vh_truth[3].path) - rw_rule = self.config.parser.find_dir( - "RewriteRule", None, self.vh_truth[3].path) - - self.assertEqual(len(rw_engine), 1) - # three args to rw_rule - self.assertEqual(len(rw_rule), 3) - - # [:-3] to remove the vhost index number - self.assertTrue(rw_engine[0].startswith(self.vh_truth[3].path[:-3])) - self.assertTrue(rw_rule[0].startswith(self.vh_truth[3].path[:-3])) - - def test_rewrite_rule_exists(self): - # Skip the enable mod - self.config.parser.modules.add("rewrite_module") - self.config.get_version = mock.Mock(return_value=(2, 3, 9)) - self.config.parser.add_dir( - self.vh_truth[3].path, "RewriteRule", ["Unknown"]) - # pylint: disable=protected-access - self.assertTrue(self.config._is_rewrite_exists(self.vh_truth[3])) - - def test_rewrite_engine_exists(self): - # Skip the enable mod - self.config.parser.modules.add("rewrite_module") - self.config.get_version = mock.Mock(return_value=(2, 3, 9)) - self.config.parser.add_dir( - self.vh_truth[3].path, "RewriteEngine", "on") - # pylint: disable=protected-access - self.assertTrue(self.config._is_rewrite_engine_on(self.vh_truth[3])) - - @mock.patch("certbot.util.run_script") - @mock.patch("certbot.util.exe_exists") - def test_redirect_with_existing_rewrite(self, mock_exe, _): - self.config.parser.modules.add("rewrite_module") - self.config.parser.update_runtime_variables = mock.Mock() - mock_exe.return_value = True - self.config.get_version = mock.Mock(return_value=(2, 2, 0)) - - # Create a preexisting rewrite rule - self.config.parser.add_dir( - self.vh_truth[3].path, "RewriteRule", ["UnknownPattern", - "UnknownTarget"]) - self.config.save() - - # This will create an ssl vhost for certbot.demo - self.config.choose_vhost("certbot.demo") - self.config.enhance("certbot.demo", "redirect") - - # These are not immediately available in find_dir even with save() and - # load(). They must be found in sites-available - rw_engine = self.config.parser.find_dir( - "RewriteEngine", "on", self.vh_truth[3].path) - rw_rule = self.config.parser.find_dir( - "RewriteRule", None, self.vh_truth[3].path) - - self.assertEqual(len(rw_engine), 1) - # three args to rw_rule + 1 arg for the pre existing rewrite - self.assertEqual(len(rw_rule), 5) - # [:-3] to remove the vhost index number - self.assertTrue(rw_engine[0].startswith(self.vh_truth[3].path[:-3])) - self.assertTrue(rw_rule[0].startswith(self.vh_truth[3].path[:-3])) - - self.assertTrue("rewrite_module" in self.config.parser.modules) - - @mock.patch("certbot.util.run_script") - @mock.patch("certbot.util.exe_exists") - def test_redirect_with_old_https_redirection(self, mock_exe, _): - self.config.parser.modules.add("rewrite_module") - self.config.parser.update_runtime_variables = mock.Mock() - mock_exe.return_value = True - self.config.get_version = mock.Mock(return_value=(2, 2, 0)) - - ssl_vhost = self.config.choose_vhost("certbot.demo") - - # pylint: disable=protected-access - http_vhost = self.config._get_http_vhost(ssl_vhost) - - # Create an old (previously suppoorted) https redirectoin rewrite rule - self.config.parser.add_dir( - http_vhost.path, "RewriteRule", - ["^", - "https://%{SERVER_NAME}%{REQUEST_URI}", - "[L,QSA,R=permanent]"]) - - self.config.save() - - try: - self.config.enhance("certbot.demo", "redirect") - except errors.PluginEnhancementAlreadyPresent: - args_paths = self.config.parser.find_dir( - "RewriteRule", None, http_vhost.path, False) - arg_vals = [self.config.aug.get(x) for x in args_paths] - self.assertEqual(arg_vals, constants.REWRITE_HTTPS_ARGS) - - - def test_redirect_with_conflict(self): - self.config.parser.modules.add("rewrite_module") - ssl_vh = obj.VirtualHost( - "fp", "ap", set([obj.Addr(("*", "443")), - obj.Addr(("zombo.com",))]), - True, False) - # No names ^ this guy should conflict. - - # pylint: disable=protected-access - self.assertRaises( - errors.PluginError, self.config._enable_redirect, ssl_vh, "") - - def test_redirect_two_domains_one_vhost(self): - # Skip the enable mod - self.config.parser.modules.add("rewrite_module") - self.config.get_version = mock.Mock(return_value=(2, 3, 9)) - - # Creates ssl vhost for the domain - self.config.choose_vhost("red.blue.purple.com") - - self.config.enhance("red.blue.purple.com", "redirect") - verify_no_redirect = ("certbot_apache.configurator." - "ApacheConfigurator._verify_no_certbot_redirect") - with mock.patch(verify_no_redirect) as mock_verify: - self.config.enhance("green.blue.purple.com", "redirect") - self.assertFalse(mock_verify.called) - - def test_redirect_from_previous_run(self): - # Skip the enable mod - self.config.parser.modules.add("rewrite_module") - self.config.get_version = mock.Mock(return_value=(2, 3, 9)) - self.config.choose_vhost("red.blue.purple.com") - self.config.enhance("red.blue.purple.com", "redirect") - # Clear state about enabling redirect on this run - # pylint: disable=protected-access - self.config._enhanced_vhosts["redirect"].clear() - - self.assertRaises( - errors.PluginEnhancementAlreadyPresent, - self.config.enhance, "green.blue.purple.com", "redirect") - - def test_create_own_redirect(self): - self.config.parser.modules.add("rewrite_module") - self.config.get_version = mock.Mock(return_value=(2, 3, 9)) - # For full testing... give names... - self.vh_truth[1].name = "default.com" - self.vh_truth[1].aliases = set(["yes.default.com"]) - - # pylint: disable=protected-access - self.config._enable_redirect(self.vh_truth[1], "") - self.assertEqual(len(self.config.vhosts), 11) - - def test_create_own_redirect_for_old_apache_version(self): - self.config.parser.modules.add("rewrite_module") - self.config.get_version = mock.Mock(return_value=(2, 2)) - # For full testing... give names... - self.vh_truth[1].name = "default.com" - self.vh_truth[1].aliases = set(["yes.default.com"]) - - # pylint: disable=protected-access - self.config._enable_redirect(self.vh_truth[1], "") - self.assertEqual(len(self.config.vhosts), 11) - - def test_sift_rewrite_rule(self): - # pylint: disable=protected-access - small_quoted_target = "RewriteRule ^ \"http://\"" - self.assertFalse(self.config._sift_rewrite_rule(small_quoted_target)) - - https_target = "RewriteRule ^ https://satoshi" - self.assertTrue(self.config._sift_rewrite_rule(https_target)) - - normal_target = "RewriteRule ^/(.*) http://www.a.com:1234/$1 [L,R]" - self.assertFalse(self.config._sift_rewrite_rule(normal_target)) - - not_rewriterule = "NotRewriteRule ^ ..." - self.assertFalse(self.config._sift_rewrite_rule(not_rewriterule)) - - def get_key_and_achalls(self): - """Return testing achallenges.""" - account_key = self.rsa512jwk - achall1 = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.TLSSNI01( - token=b"jIq_Xy1mXGN37tb4L6Xj_es58fW571ZNyXekdZzhh7Q"), - "pending"), - domain="encryption-example.demo", account_key=account_key) - achall2 = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.TLSSNI01( - token=b"uqnaPzxtrndteOqtrXb0Asl5gOJfWAnnx6QJyvcmlDU"), - "pending"), - domain="certbot.demo", account_key=account_key) - achall3 = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.HTTP01(token=(b'x' * 16)), "pending"), - domain="example.org", account_key=account_key) - - return account_key, (achall1, achall2, achall3) - - def test_make_addrs_sni_ready(self): - self.config.version = (2, 2) - self.config.make_addrs_sni_ready( - set([obj.Addr.fromstring("*:443"), obj.Addr.fromstring("*:80")])) - self.assertTrue(self.config.parser.find_dir( - "NameVirtualHost", "*:80", exclude=False)) - self.assertTrue(self.config.parser.find_dir( - "NameVirtualHost", "*:443", exclude=False)) - - def test_aug_version(self): - mock_match = mock.Mock(return_value=["something"]) - self.config.aug.match = mock_match - # pylint: disable=protected-access - self.assertEqual(self.config._check_aug_version(), - ["something"]) - self.config.aug.match.side_effect = RuntimeError - self.assertFalse(self.config._check_aug_version()) - - def test_enable_site_nondebian(self): - inc_path = "/path/to/wherever" - vhost = self.vh_truth[0] - vhost.enabled = False - vhost.filep = inc_path - self.assertFalse(self.config.parser.find_dir("Include", inc_path)) - self.assertFalse( - os.path.dirname(inc_path) in self.config.parser.existing_paths) - self.config.enable_site(vhost) - self.assertTrue(self.config.parser.find_dir("Include", inc_path)) - self.assertTrue( - os.path.dirname(inc_path) in self.config.parser.existing_paths) - self.assertTrue( - os.path.basename(inc_path) in self.config.parser.existing_paths[ - os.path.dirname(inc_path)]) - - def test_deploy_cert_not_parsed_path(self): - # Make sure that we add include to root config for vhosts when - # handle-sites is false - self.config.parser.modules.add("ssl_module") - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("socache_shmcb_module") - tmp_path = os.path.realpath(tempfile.mkdtemp("vhostroot")) - os.chmod(tmp_path, 0o755) - mock_p = "certbot_apache.configurator.ApacheConfigurator._get_ssl_vhost_path" - mock_a = "certbot_apache.parser.ApacheParser.add_include" - - with mock.patch(mock_p) as mock_path: - mock_path.return_value = os.path.join(tmp_path, "whatever.conf") - with mock.patch(mock_a) as mock_add: - self.config.deploy_cert( - "encryption-example.demo", - "example/cert.pem", "example/key.pem", - "example/cert_chain.pem") - # Test that we actually called add_include - self.assertTrue(mock_add.called) - shutil.rmtree(tmp_path) - - @mock.patch("certbot_apache.parser.ApacheParser.parsed_in_original") - def test_choose_vhost_and_servername_addition_parsed(self, mock_parsed): - ret_vh = self.vh_truth[8] - ret_vh.enabled = True - self.config.enable_site(ret_vh) - # Make sure that we return early - self.assertFalse(mock_parsed.called) - - def test_enable_mod_unsupported(self): - self.assertRaises(errors.MisconfigurationError, - self.config.enable_mod, - "whatever") - - def test_wildcard_domain(self): - # pylint: disable=protected-access - cases = {u"*.example.org": True, b"*.x.example.org": True, - u"a.example.org": False, b"a.x.example.org": False} - for key in cases.keys(): - self.assertEqual(self.config._wildcard_domain(key), cases[key]) - - def test_choose_vhosts_wildcard(self): - # pylint: disable=protected-access - mock_path = "certbot_apache.display_ops.select_vhost_multiple" - with mock.patch(mock_path) as mock_select_vhs: - mock_select_vhs.return_value = [self.vh_truth[3]] - vhs = self.config._choose_vhosts_wildcard("*.certbot.demo", - create_ssl=True) - # Check that the dialog was called with one vh: certbot.demo - self.assertEqual(mock_select_vhs.call_args[0][0][0], self.vh_truth[3]) - self.assertEqual(len(mock_select_vhs.call_args_list), 1) - - # And the actual returned values - self.assertEqual(len(vhs), 1) - self.assertTrue(vhs[0].name == "certbot.demo") - self.assertTrue(vhs[0].ssl) - - self.assertFalse(vhs[0] == self.vh_truth[3]) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.make_vhost_ssl") - def test_choose_vhosts_wildcard_no_ssl(self, mock_makessl): - # pylint: disable=protected-access - mock_path = "certbot_apache.display_ops.select_vhost_multiple" - with mock.patch(mock_path) as mock_select_vhs: - mock_select_vhs.return_value = [self.vh_truth[1]] - vhs = self.config._choose_vhosts_wildcard("*.certbot.demo", - create_ssl=False) - self.assertFalse(mock_makessl.called) - self.assertEqual(vhs[0], self.vh_truth[1]) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator._vhosts_for_wildcard") - @mock.patch("certbot_apache.configurator.ApacheConfigurator.make_vhost_ssl") - def test_choose_vhosts_wildcard_already_ssl(self, mock_makessl, mock_vh_for_w): - # pylint: disable=protected-access - # Already SSL vhost - mock_vh_for_w.return_value = [self.vh_truth[7]] - mock_path = "certbot_apache.display_ops.select_vhost_multiple" - with mock.patch(mock_path) as mock_select_vhs: - mock_select_vhs.return_value = [self.vh_truth[7]] - vhs = self.config._choose_vhosts_wildcard("whatever", - create_ssl=True) - self.assertEqual(mock_select_vhs.call_args[0][0][0], self.vh_truth[7]) - self.assertEqual(len(mock_select_vhs.call_args_list), 1) - # Ensure that make_vhost_ssl was not called, vhost.ssl == true - self.assertFalse(mock_makessl.called) - - # And the actual returned values - self.assertEqual(len(vhs), 1) - self.assertTrue(vhs[0].ssl) - self.assertEqual(vhs[0], self.vh_truth[7]) - - - def test_deploy_cert_wildcard(self): - # pylint: disable=protected-access - mock_choose_vhosts = mock.MagicMock() - mock_choose_vhosts.return_value = [self.vh_truth[7]] - self.config._choose_vhosts_wildcard = mock_choose_vhosts - mock_d = "certbot_apache.configurator.ApacheConfigurator._deploy_cert" - with mock.patch(mock_d) as mock_dep: - self.config.deploy_cert("*.wildcard.example.org", "/tmp/path", - "/tmp/path", "/tmp/path", "/tmp/path") - self.assertTrue(mock_dep.called) - self.assertEqual(len(mock_dep.call_args_list), 1) - self.assertEqual(self.vh_truth[7], mock_dep.call_args_list[0][0][0]) - - @mock.patch("certbot_apache.display_ops.select_vhost_multiple") - def test_deploy_cert_wildcard_no_vhosts(self, mock_dialog): - # pylint: disable=protected-access - mock_dialog.return_value = [] - self.assertRaises(errors.PluginError, - self.config.deploy_cert, - "*.wild.cat", "/tmp/path", "/tmp/path", - "/tmp/path", "/tmp/path") - - @mock.patch("certbot_apache.configurator.ApacheConfigurator._choose_vhosts_wildcard") - def test_enhance_wildcard_after_install(self, mock_choose): - # pylint: disable=protected-access - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("headers_module") - self.vh_truth[3].ssl = True - self.config._wildcard_vhosts["*.certbot.demo"] = [self.vh_truth[3]] - self.config.enhance("*.certbot.demo", "ensure-http-header", - "Upgrade-Insecure-Requests") - self.assertFalse(mock_choose.called) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator._choose_vhosts_wildcard") - def test_enhance_wildcard_no_install(self, mock_choose): - self.vh_truth[3].ssl = True - mock_choose.return_value = [self.vh_truth[3]] - self.config.parser.modules.add("mod_ssl.c") - self.config.parser.modules.add("headers_module") - self.config.enhance("*.certbot.demo", "ensure-http-header", - "Upgrade-Insecure-Requests") - self.assertTrue(mock_choose.called) - - def test_add_vhost_id(self): - for vh in [self.vh_truth[0], self.vh_truth[1], self.vh_truth[2]]: - vh_id = self.config.add_vhost_id(vh) - self.assertEqual(vh, self.config.find_vhost_by_id(vh_id)) - - def test_find_vhost_by_id_404(self): - self.assertRaises(errors.PluginError, - self.config.find_vhost_by_id, - "nonexistent") - - def test_add_vhost_id_already_exists(self): - first_id = self.config.add_vhost_id(self.vh_truth[0]) - second_id = self.config.add_vhost_id(self.vh_truth[0]) - self.assertEqual(first_id, second_id) - - -class AugeasVhostsTest(util.ApacheTest): - """Test vhosts with illegal names dependent on augeas version.""" - # pylint: disable=protected-access - - def setUp(self): # pylint: disable=arguments-differ - td = "debian_apache_2_4/augeas_vhosts" - cr = "debian_apache_2_4/augeas_vhosts/apache2" - vr = "debian_apache_2_4/augeas_vhosts/apache2/sites-available" - super(AugeasVhostsTest, self).setUp(test_dir=td, - config_root=cr, - vhost_root=vr) - - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, - self.work_dir) - - def test_choosevhost_with_illegal_name(self): - self.config.aug = mock.MagicMock() - self.config.aug.match.side_effect = RuntimeError - path = "debian_apache_2_4/augeas_vhosts/apache2/sites-available/old,default.conf" - chosen_vhost = self.config._create_vhost(path) - self.assertEqual(None, chosen_vhost) - - def test_choosevhost_works(self): - path = "debian_apache_2_4/augeas_vhosts/apache2/sites-available/old,default.conf" - chosen_vhost = self.config._create_vhost(path) - self.assertTrue(chosen_vhost == None or chosen_vhost.path == path) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator._create_vhost") - def test_get_vhost_continue(self, mock_vhost): - mock_vhost.return_value = None - vhs = self.config.get_virtual_hosts() - self.assertEqual([], vhs) - - def test_choose_vhost_with_matching_wildcard(self): - names = ( - "an.example.net", "another.example.net", "an.other.example.net") - for name in names: - self.assertFalse(name in self.config.choose_vhost(name).aliases) - - @mock.patch("certbot_apache.obj.VirtualHost.conflicts") - def test_choose_vhost_without_matching_wildcard(self, mock_conflicts): - mock_conflicts.return_value = False - mock_path = "certbot_apache.display_ops.select_vhost" - with mock.patch(mock_path, lambda _, vhosts: vhosts[0]): - for name in ("a.example.net", "other.example.net"): - self.assertTrue(name in self.config.choose_vhost(name).aliases) - - @mock.patch("certbot_apache.obj.VirtualHost.conflicts") - def test_choose_vhost_wildcard_not_found(self, mock_conflicts): - mock_conflicts.return_value = False - mock_path = "certbot_apache.display_ops.select_vhost" - names = ( - "abc.example.net", "not.there.tld", "aa.wildcard.tld" - ) - with mock.patch(mock_path) as mock_select: - mock_select.return_value = self.config.vhosts[0] - for name in names: - orig_cc = mock_select.call_count - self.config.choose_vhost(name) - self.assertEqual(mock_select.call_count - orig_cc, 1) - - def test_choose_vhost_wildcard_found(self): - mock_path = "certbot_apache.display_ops.select_vhost" - names = ( - "ab.example.net", "a.wildcard.tld", "yetanother.example.net" - ) - with mock.patch(mock_path) as mock_select: - mock_select.return_value = self.config.vhosts[0] - for name in names: - self.config.choose_vhost(name) - self.assertEqual(mock_select.call_count, 0) - - def test_augeas_span_error(self): - broken_vhost = self.config.vhosts[0] - broken_vhost.path = broken_vhost.path + "/nonexistent" - self.assertRaises(errors.PluginError, self.config.make_vhost_ssl, - broken_vhost) - -class MultiVhostsTest(util.ApacheTest): - """Test configuration with multiple virtualhosts in a single file.""" - # pylint: disable=protected-access - - def setUp(self): # pylint: disable=arguments-differ - td = "debian_apache_2_4/multi_vhosts" - cr = "debian_apache_2_4/multi_vhosts/apache2" - vr = "debian_apache_2_4/multi_vhosts/apache2/sites-available" - super(MultiVhostsTest, self).setUp(test_dir=td, - config_root=cr, - vhost_root=vr) - - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, - self.config_dir, self.work_dir, conf_vhost_path=self.vhost_path) - self.vh_truth = util.get_vh_truth( - self.temp_dir, "debian_apache_2_4/multi_vhosts") - - def test_make_vhost_ssl(self): - ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[1]) - - self.assertEqual( - ssl_vhost.filep, - os.path.join(self.config_path, "sites-available", - "default-le-ssl.conf")) - - self.assertEqual(ssl_vhost.path, - "/files" + ssl_vhost.filep + "/IfModule/VirtualHost") - self.assertEqual(len(ssl_vhost.addrs), 1) - self.assertEqual(set([obj.Addr.fromstring("*:443")]), ssl_vhost.addrs) - self.assertEqual(ssl_vhost.name, "banana.vomit.com") - self.assertTrue(ssl_vhost.ssl) - self.assertFalse(ssl_vhost.enabled) - - - self.assertEqual(self.config.is_name_vhost(self.vh_truth[1]), - self.config.is_name_vhost(ssl_vhost)) - - mock_path = "certbot_apache.configurator.ApacheConfigurator._get_new_vh_path" - with mock.patch(mock_path) as mock_getpath: - mock_getpath.return_value = None - self.assertRaises(errors.PluginError, self.config.make_vhost_ssl, - self.vh_truth[1]) - - def test_get_new_path(self): - with_index_1 = ["/path[1]/section[1]"] - without_index = ["/path/section"] - with_index_2 = ["/path[2]/section[2]"] - self.assertEqual(self.config._get_new_vh_path(without_index, - with_index_1), - None) - self.assertEqual(self.config._get_new_vh_path(without_index, - with_index_2), - with_index_2[0]) - - both = with_index_1 + with_index_2 - self.assertEqual(self.config._get_new_vh_path(without_index, both), - with_index_2[0]) - - @certbot_util.patch_get_utility() - def test_make_vhost_ssl_with_existing_rewrite_rule(self, mock_get_utility): - self.config.parser.modules.add("rewrite_module") - - ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[4]) - - self.assertTrue(self.config.parser.find_dir( - "RewriteEngine", "on", ssl_vhost.path, False)) - - with open(ssl_vhost.filep) as the_file: - conf_text = the_file.read() - commented_rewrite_rule = ("# RewriteRule \"^/secrets/(.+)\" " - "\"https://new.example.com/docs/$1\" [R,L]") - uncommented_rewrite_rule = ("RewriteRule \"^/docs/(.+)\" " - "\"http://new.example.com/docs/$1\" [R,L]") - self.assertTrue(commented_rewrite_rule in conf_text) - self.assertTrue(uncommented_rewrite_rule in conf_text) - mock_get_utility().add_message.assert_called_once_with(mock.ANY, - mock.ANY) - - @certbot_util.patch_get_utility() - def test_make_vhost_ssl_with_existing_rewrite_conds(self, mock_get_utility): - self.config.parser.modules.add("rewrite_module") - - ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[3]) - - with open(ssl_vhost.filep) as the_file: - conf_lines = the_file.readlines() - conf_line_set = [l.strip() for l in conf_lines] - not_commented_cond1 = ("RewriteCond " - "%{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f") - not_commented_rewrite_rule = ("RewriteRule " - "^(.*)$ b://u%{REQUEST_URI} [P,NE,L]") - - commented_cond1 = "# RewriteCond %{HTTPS} !=on" - commented_cond2 = "# RewriteCond %{HTTPS} !^$" - commented_rewrite_rule = ("# RewriteRule ^ " - "https://%{SERVER_NAME}%{REQUEST_URI} " - "[L,NE,R=permanent]") - - self.assertTrue(not_commented_cond1 in conf_line_set) - self.assertTrue(not_commented_rewrite_rule in conf_line_set) - - self.assertTrue(commented_cond1 in conf_line_set) - self.assertTrue(commented_cond2 in conf_line_set) - self.assertTrue(commented_rewrite_rule in conf_line_set) - mock_get_utility().add_message.assert_called_once_with(mock.ANY, - mock.ANY) - - -class InstallSslOptionsConfTest(util.ApacheTest): - """Test that the options-ssl-nginx.conf file is installed and updated properly.""" - - def setUp(self): # pylint: disable=arguments-differ - super(InstallSslOptionsConfTest, self).setUp() - - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, self.work_dir) - - def _call(self): - self.config.install_ssl_options_conf(self.config.mod_ssl_conf, - self.config.updated_mod_ssl_conf_digest) - - def _current_ssl_options_hash(self): - return crypto_util.sha256sum(self.config.option("MOD_SSL_CONF_SRC")) - - def _assert_current_file(self): - self.assertTrue(os.path.isfile(self.config.mod_ssl_conf)) - self.assertEqual(crypto_util.sha256sum(self.config.mod_ssl_conf), - self._current_ssl_options_hash()) - - def test_no_file(self): - # prepare should have placed a file there - self._assert_current_file() - os.remove(self.config.mod_ssl_conf) - self.assertFalse(os.path.isfile(self.config.mod_ssl_conf)) - self._call() - self._assert_current_file() - - def test_current_file(self): - self._assert_current_file() - self._call() - self._assert_current_file() - - def test_prev_file_updates_to_current(self): - from certbot_apache.constants import ALL_SSL_OPTIONS_HASHES - ALL_SSL_OPTIONS_HASHES.insert(0, "test_hash_does_not_match") - with mock.patch('certbot.crypto_util.sha256sum') as mock_sha256: - mock_sha256.return_value = ALL_SSL_OPTIONS_HASHES[0] - self._call() - self._assert_current_file() - - def test_manually_modified_current_file_does_not_update(self): - with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf: - mod_ssl_conf.write("a new line for the wrong hash\n") - with mock.patch("certbot.plugins.common.logger") as mock_logger: - self._call() - self.assertFalse(mock_logger.warning.called) - self.assertTrue(os.path.isfile(self.config.mod_ssl_conf)) - self.assertEqual(crypto_util.sha256sum( - self.config.option("MOD_SSL_CONF_SRC")), - self._current_ssl_options_hash()) - self.assertNotEqual(crypto_util.sha256sum(self.config.mod_ssl_conf), - self._current_ssl_options_hash()) - - def test_manually_modified_past_file_warns(self): - with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf: - mod_ssl_conf.write("a new line for the wrong hash\n") - with open(self.config.updated_mod_ssl_conf_digest, "w") as f: - f.write("hashofanoldversion") - with mock.patch("certbot.plugins.common.logger") as mock_logger: - self._call() - self.assertEqual(mock_logger.warning.call_args[0][0], - "%s has been manually modified; updated file " - "saved to %s. We recommend updating %s for security purposes.") - self.assertEqual(crypto_util.sha256sum( - self.config.option("MOD_SSL_CONF_SRC")), - self._current_ssl_options_hash()) - # only print warning once - with mock.patch("certbot.plugins.common.logger") as mock_logger: - self._call() - self.assertFalse(mock_logger.warning.called) - - def test_current_file_hash_in_all_hashes(self): - from certbot_apache.constants import ALL_SSL_OPTIONS_HASHES - self.assertTrue(self._current_ssl_options_hash() in ALL_SSL_OPTIONS_HASHES, - "Constants.ALL_SSL_OPTIONS_HASHES must be appended" - " with the sha256 hash of self.config.mod_ssl_conf when it is updated.") - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/debian_test.py b/certbot-apache/certbot_apache/tests/debian_test.py deleted file mode 100644 index bb1d642781f..00000000000 --- a/certbot-apache/certbot_apache/tests/debian_test.py +++ /dev/null @@ -1,213 +0,0 @@ -"""Test for certbot_apache.configurator for Debian overrides""" -import os -import shutil -import unittest - -import mock - -from certbot import errors - -from certbot_apache import apache_util -from certbot_apache import obj -from certbot_apache.tests import util - - -class MultipleVhostsTestDebian(util.ApacheTest): - """Multiple vhost tests for Debian family of distros""" - - _multiprocess_can_split_ = True - - def setUp(self): # pylint: disable=arguments-differ - super(MultipleVhostsTestDebian, self).setUp() - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, self.work_dir, - os_info="debian") - self.config = self.mock_deploy_cert(self.config) - self.vh_truth = util.get_vh_truth(self.temp_dir, - "debian_apache_2_4/multiple_vhosts") - - def mock_deploy_cert(self, config): - """A test for a mock deploy cert""" - config.real_deploy_cert = self.config.deploy_cert - - def mocked_deploy_cert(*args, **kwargs): - """a helper to mock a deployed cert""" - g_mod = "certbot_apache.configurator.ApacheConfigurator.enable_mod" - d_mod = "certbot_apache.override_debian.DebianConfigurator.enable_mod" - with mock.patch(g_mod): - with mock.patch(d_mod): - config.real_deploy_cert(*args, **kwargs) - self.config.deploy_cert = mocked_deploy_cert - return self.config - - def test_enable_mod_unsupported_dirs(self): - shutil.rmtree(os.path.join(self.config.parser.root, "mods-enabled")) - self.assertRaises( - errors.NotSupportedError, self.config.enable_mod, "ssl") - - @mock.patch("certbot.util.run_script") - @mock.patch("certbot.util.exe_exists") - @mock.patch("certbot_apache.parser.subprocess.Popen") - def test_enable_mod(self, mock_popen, mock_exe_exists, mock_run_script): - mock_popen().communicate.return_value = ("Define: DUMP_RUN_CFG", "") - mock_popen().returncode = 0 - mock_exe_exists.return_value = True - - self.config.enable_mod("ssl") - self.assertTrue("ssl_module" in self.config.parser.modules) - self.assertTrue("mod_ssl.c" in self.config.parser.modules) - - self.assertTrue(mock_run_script.called) - - def test_deploy_cert_enable_new_vhost(self): - # Create - ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[0]) - self.config.parser.modules.add("ssl_module") - self.config.parser.modules.add("mod_ssl.c") - self.assertFalse(ssl_vhost.enabled) - self.config.deploy_cert( - "encryption-example.demo", "example/cert.pem", "example/key.pem", - "example/cert_chain.pem", "example/fullchain.pem") - self.assertTrue(ssl_vhost.enabled) - # Make sure that we don't error out if symlink already exists - ssl_vhost.enabled = False - self.assertFalse(ssl_vhost.enabled) - self.config.deploy_cert( - "encryption-example.demo", "example/cert.pem", "example/key.pem", - "example/cert_chain.pem", "example/fullchain.pem") - self.assertTrue(ssl_vhost.enabled) - - def test_enable_site_failure(self): - self.config.parser.root = "/tmp/nonexistent" - with mock.patch("os.path.isdir") as mock_dir: - mock_dir.return_value = True - with mock.patch("os.path.islink") as mock_link: - mock_link.return_value = False - self.assertRaises( - errors.NotSupportedError, - self.config.enable_site, - obj.VirtualHost("asdf", "afsaf", set(), False, False)) - - def test_deploy_cert_newssl(self): - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, - self.work_dir, version=(2, 4, 16)) - self.config = self.mock_deploy_cert(self.config) - self.config.parser.modules.add("ssl_module") - self.config.parser.modules.add("mod_ssl.c") - - # Get the default 443 vhost - self.config.assoc["random.demo"] = self.vh_truth[1] - self.config.deploy_cert( - "random.demo", "example/cert.pem", "example/key.pem", - "example/cert_chain.pem", "example/fullchain.pem") - self.config.save() - - # Verify ssl_module was enabled. - self.assertTrue(self.vh_truth[1].enabled) - self.assertTrue("ssl_module" in self.config.parser.modules) - - loc_cert = self.config.parser.find_dir( - "sslcertificatefile", "example/fullchain.pem", - self.vh_truth[1].path) - loc_key = self.config.parser.find_dir( - "sslcertificateKeyfile", "example/key.pem", self.vh_truth[1].path) - - # Verify one directive was found in the correct file - self.assertEqual(len(loc_cert), 1) - self.assertEqual( - apache_util.get_file_path(loc_cert[0]), - self.vh_truth[1].filep) - - self.assertEqual(len(loc_key), 1) - self.assertEqual( - apache_util.get_file_path(loc_key[0]), - self.vh_truth[1].filep) - - def test_deploy_cert_newssl_no_fullchain(self): - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, - self.work_dir, version=(2, 4, 16)) - self.config = self.mock_deploy_cert(self.config) - self.config.parser.modules.add("ssl_module") - self.config.parser.modules.add("mod_ssl.c") - - # Get the default 443 vhost - self.config.assoc["random.demo"] = self.vh_truth[1] - self.assertRaises(errors.PluginError, - lambda: self.config.deploy_cert( - "random.demo", "example/cert.pem", - "example/key.pem")) - - def test_deploy_cert_old_apache_no_chain(self): - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, - self.work_dir, version=(2, 4, 7)) - self.config = self.mock_deploy_cert(self.config) - self.config.parser.modules.add("ssl_module") - self.config.parser.modules.add("mod_ssl.c") - - # Get the default 443 vhost - self.config.assoc["random.demo"] = self.vh_truth[1] - self.assertRaises(errors.PluginError, - lambda: self.config.deploy_cert( - "random.demo", "example/cert.pem", - "example/key.pem")) - - @mock.patch("certbot.util.run_script") - @mock.patch("certbot.util.exe_exists") - def test_ocsp_stapling_enable_mod(self, mock_exe, _): - self.config.parser.update_runtime_variables = mock.Mock() - self.config.parser.modules.add("mod_ssl.c") - self.config.get_version = mock.Mock(return_value=(2, 4, 7)) - mock_exe.return_value = True - # This will create an ssl vhost for certbot.demo - self.config.choose_vhost("certbot.demo") - self.config.enhance("certbot.demo", "staple-ocsp") - self.assertTrue("socache_shmcb_module" in self.config.parser.modules) - - @mock.patch("certbot.util.run_script") - @mock.patch("certbot.util.exe_exists") - def test_ensure_http_header_enable_mod(self, mock_exe, _): - self.config.parser.update_runtime_variables = mock.Mock() - self.config.parser.modules.add("mod_ssl.c") - mock_exe.return_value = True - - # This will create an ssl vhost for certbot.demo - self.config.choose_vhost("certbot.demo") - self.config.enhance("certbot.demo", "ensure-http-header", - "Strict-Transport-Security") - self.assertTrue("headers_module" in self.config.parser.modules) - - @mock.patch("certbot.util.run_script") - @mock.patch("certbot.util.exe_exists") - def test_redirect_enable_mod(self, mock_exe, _): - self.config.parser.update_runtime_variables = mock.Mock() - mock_exe.return_value = True - self.config.get_version = mock.Mock(return_value=(2, 2)) - # This will create an ssl vhost for certbot.demo - self.config.choose_vhost("certbot.demo") - self.config.enhance("certbot.demo", "redirect") - self.assertTrue("rewrite_module" in self.config.parser.modules) - - def test_enable_site_already_enabled(self): - self.assertTrue(self.vh_truth[1].enabled) - self.config.enable_site(self.vh_truth[1]) - - def test_enable_site_call_parent(self): - with mock.patch( - "certbot_apache.configurator.ApacheConfigurator.enable_site") as e_s: - self.config.parser.root = "/tmp/nonexistent" - vh = self.vh_truth[0] - vh.enabled = False - self.config.enable_site(vh) - self.assertTrue(e_s.called) - - @mock.patch("certbot.util.exe_exists") - def test_enable_mod_no_disable(self, mock_exe_exists): - mock_exe_exists.return_value = False - self.assertRaises( - errors.MisconfigurationError, self.config.enable_mod, "ssl") - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/display_ops_test.py b/certbot-apache/certbot_apache/tests/display_ops_test.py deleted file mode 100644 index df5cdbac0a2..00000000000 --- a/certbot-apache/certbot_apache/tests/display_ops_test.py +++ /dev/null @@ -1,108 +0,0 @@ -"""Test certbot_apache.display_ops.""" -import unittest - -import mock - -from certbot import errors - -from certbot.display import util as display_util - -from certbot.tests import util as certbot_util - -from certbot_apache import obj - -from certbot_apache.display_ops import select_vhost_multiple -from certbot_apache.tests import util - - -class SelectVhostMultiTest(unittest.TestCase): - """Tests for certbot_apache.display_ops.select_vhost_multiple.""" - - def setUp(self): - self.base_dir = "/example_path" - self.vhosts = util.get_vh_truth( - self.base_dir, "debian_apache_2_4/multiple_vhosts") - - def test_select_no_input(self): - self.assertFalse(select_vhost_multiple([])) - - @certbot_util.patch_get_utility() - def test_select_correct(self, mock_util): - mock_util().checklist.return_value = ( - display_util.OK, [self.vhosts[3].display_repr(), - self.vhosts[2].display_repr()]) - vhs = select_vhost_multiple([self.vhosts[3], - self.vhosts[2], - self.vhosts[1]]) - self.assertTrue(self.vhosts[2] in vhs) - self.assertTrue(self.vhosts[3] in vhs) - self.assertFalse(self.vhosts[1] in vhs) - - @certbot_util.patch_get_utility() - def test_select_cancel(self, mock_util): - mock_util().checklist.return_value = (display_util.CANCEL, "whatever") - vhs = select_vhost_multiple([self.vhosts[2], self.vhosts[3]]) - self.assertFalse(vhs) - -class SelectVhostTest(unittest.TestCase): - """Tests for certbot_apache.display_ops.select_vhost.""" - - def setUp(self): - self.base_dir = "/example_path" - self.vhosts = util.get_vh_truth( - self.base_dir, "debian_apache_2_4/multiple_vhosts") - - @classmethod - def _call(cls, vhosts): - from certbot_apache.display_ops import select_vhost - return select_vhost("example.com", vhosts) - - @certbot_util.patch_get_utility() - def test_successful_choice(self, mock_util): - mock_util().menu.return_value = (display_util.OK, 3) - self.assertEqual(self.vhosts[3], self._call(self.vhosts)) - - @certbot_util.patch_get_utility() - def test_noninteractive(self, mock_util): - mock_util().menu.side_effect = errors.MissingCommandlineFlag("no vhost default") - try: - self._call(self.vhosts) - except errors.MissingCommandlineFlag as e: - self.assertTrue("vhost ambiguity" in str(e)) - - @certbot_util.patch_get_utility() - def test_more_info_cancel(self, mock_util): - mock_util().menu.side_effect = [ - (display_util.CANCEL, -1), - ] - - self.assertEqual(None, self._call(self.vhosts)) - - def test_no_vhosts(self): - self.assertEqual(self._call([]), None) - - @mock.patch("certbot_apache.display_ops.display_util") - @certbot_util.patch_get_utility() - @mock.patch("certbot_apache.display_ops.logger") - def test_small_display(self, mock_logger, mock_util, mock_display_util): - mock_display_util.WIDTH = 20 - mock_util().menu.return_value = (display_util.OK, 0) - self._call(self.vhosts) - - self.assertEqual(mock_logger.debug.call_count, 1) - - @certbot_util.patch_get_utility() - def test_multiple_names(self, mock_util): - mock_util().menu.return_value = (display_util.OK, 5) - - self.vhosts.append( - obj.VirtualHost( - "path", "aug_path", set([obj.Addr.fromstring("*:80")]), - False, False, - "wildcard.com", set(["*.wildcard.com"]))) - - self.assertEqual(self.vhosts[5], self._call(self.vhosts)) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/entrypoint_test.py b/certbot-apache/certbot_apache/tests/entrypoint_test.py deleted file mode 100644 index c04611465d9..00000000000 --- a/certbot-apache/certbot_apache/tests/entrypoint_test.py +++ /dev/null @@ -1,41 +0,0 @@ -"""Test for certbot_apache.entrypoint for override class resolution""" -import unittest - -import mock - -from certbot_apache import configurator -from certbot_apache import entrypoint - -class EntryPointTest(unittest.TestCase): - """Entrypoint tests""" - - _multiprocess_can_split_ = True - - def test_get_configurator(self): - - with mock.patch("certbot.util.get_os_info") as mock_info: - for distro in entrypoint.OVERRIDE_CLASSES.keys(): - mock_info.return_value = (distro, "whatever") - self.assertEqual(entrypoint.get_configurator(), - entrypoint.OVERRIDE_CLASSES[distro]) - - def test_nonexistent_like(self): - with mock.patch("certbot.util.get_os_info") as mock_info: - mock_info.return_value = ("nonexistent", "irrelevant") - with mock.patch("certbot.util.get_systemd_os_like") as mock_like: - for like in entrypoint.OVERRIDE_CLASSES.keys(): - mock_like.return_value = [like] - self.assertEqual(entrypoint.get_configurator(), - entrypoint.OVERRIDE_CLASSES[like]) - - def test_nonexistent_generic(self): - with mock.patch("certbot.util.get_os_info") as mock_info: - mock_info.return_value = ("nonexistent", "irrelevant") - with mock.patch("certbot.util.get_systemd_os_like") as mock_like: - mock_like.return_value = ["unknonwn"] - self.assertEqual(entrypoint.get_configurator(), - configurator.ApacheConfigurator) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/gentoo_test.py b/certbot-apache/certbot_apache/tests/gentoo_test.py deleted file mode 100644 index f09d742a4ea..00000000000 --- a/certbot-apache/certbot_apache/tests/gentoo_test.py +++ /dev/null @@ -1,135 +0,0 @@ -"""Test for certbot_apache.configurator for Gentoo overrides""" -import os -import unittest - -import mock - -from certbot import errors - -from certbot_apache import override_gentoo -from certbot_apache import obj -from certbot_apache.tests import util - -def get_vh_truth(temp_dir, config_name): - """Return the ground truth for the specified directory.""" - prefix = os.path.join( - temp_dir, config_name, "apache2/vhosts.d") - - aug_pre = "/files" + prefix - vh_truth = [ - obj.VirtualHost( - os.path.join(prefix, "gentoo.example.com.conf"), - os.path.join(aug_pre, "gentoo.example.com.conf/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), - False, True, "gentoo.example.com"), - obj.VirtualHost( - os.path.join(prefix, "00_default_vhost.conf"), - os.path.join(aug_pre, "00_default_vhost.conf/IfDefine/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), - False, True, "localhost"), - obj.VirtualHost( - os.path.join(prefix, "00_default_ssl_vhost.conf"), - os.path.join(aug_pre, - "00_default_ssl_vhost.conf" + - "/IfDefine/IfDefine/IfModule/VirtualHost"), - set([obj.Addr.fromstring("_default_:443")]), - True, True, "localhost") - ] - return vh_truth - -class MultipleVhostsTestGentoo(util.ApacheTest): - """Multiple vhost tests for non-debian distro""" - - _multiprocess_can_split_ = True - - def setUp(self): # pylint: disable=arguments-differ - test_dir = "gentoo_apache/apache" - config_root = "gentoo_apache/apache/apache2" - vhost_root = "gentoo_apache/apache/apache2/vhosts.d" - super(MultipleVhostsTestGentoo, self).setUp(test_dir=test_dir, - config_root=config_root, - vhost_root=vhost_root) - - with mock.patch("certbot_apache.override_gentoo.GentooParser.update_runtime_variables"): - self.config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, self.work_dir, - os_info="gentoo") - self.vh_truth = get_vh_truth( - self.temp_dir, "gentoo_apache/apache") - - def test_get_parser(self): - self.assertTrue(isinstance(self.config.parser, - override_gentoo.GentooParser)) - - def test_get_virtual_hosts(self): - """Make sure all vhosts are being properly found.""" - vhs = self.config.get_virtual_hosts() - self.assertEqual(len(vhs), 3) - found = 0 - - for vhost in vhs: - for gentoo_truth in self.vh_truth: - if vhost == gentoo_truth: - found += 1 - break - else: - raise Exception("Missed: %s" % vhost) # pragma: no cover - self.assertEqual(found, 3) - - def test_get_sysconfig_vars(self): - """Make sure we read the Gentoo APACHE2_OPTS variable correctly""" - defines = ['DEFAULT_VHOST', 'INFO', - 'SSL', 'SSL_DEFAULT_VHOST', 'LANGUAGE'] - self.config.parser.apacheconfig_filep = os.path.realpath( - os.path.join(self.config.parser.root, "../conf.d/apache2")) - self.config.parser.variables = {} - with mock.patch("certbot_apache.override_gentoo.GentooParser.update_modules"): - self.config.parser.update_runtime_variables() - for define in defines: - self.assertTrue(define in self.config.parser.variables.keys()) - - @mock.patch("certbot_apache.parser.ApacheParser.parse_from_subprocess") - def test_no_binary_configdump(self, mock_subprocess): - """Make sure we don't call binary dumps other than modules from Apache - as this is not supported in Gentoo currently""" - - with mock.patch("certbot_apache.override_gentoo.GentooParser.update_modules"): - self.config.parser.update_runtime_variables() - self.config.parser.reset_modules() - self.assertFalse(mock_subprocess.called) - - self.config.parser.update_runtime_variables() - self.config.parser.reset_modules() - self.assertTrue(mock_subprocess.called) - - @mock.patch("certbot_apache.parser.ApacheParser._get_runtime_cfg") - def test_opportunistic_httpd_runtime_parsing(self, mock_get): - mod_val = ( - 'Loaded Modules:\n' - ' mock_module (static)\n' - ' another_module (static)\n' - ) - def mock_get_cfg(command): - """Mock httpd process stdout""" - if command == ['apache2ctl', 'modules']: - return mod_val - mock_get.side_effect = mock_get_cfg - self.config.parser.modules = set() - - with mock.patch("certbot.util.get_os_info") as mock_osi: - # Make sure we have the have the Gentoo httpd constants - mock_osi.return_value = ("gentoo", "123") - self.config.parser.update_runtime_variables() - - self.assertEqual(mock_get.call_count, 1) - self.assertEqual(len(self.config.parser.modules), 4) - self.assertTrue("mod_another.c" in self.config.parser.modules) - - @mock.patch("certbot_apache.configurator.util.run_script") - def test_alt_restart_works(self, mock_run_script): - mock_run_script.side_effect = [None, errors.SubprocessError, None] - self.config.restart() - self.assertEqual(mock_run_script.call_count, 3) - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/http_01_test.py b/certbot-apache/certbot_apache/tests/http_01_test.py deleted file mode 100644 index 9c729b08ce0..00000000000 --- a/certbot-apache/certbot_apache/tests/http_01_test.py +++ /dev/null @@ -1,222 +0,0 @@ -"""Test for certbot_apache.http_01.""" -import mock -import os -import unittest - -from acme import challenges -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module - -from certbot import achallenges -from certbot import errors - -from certbot.tests import acme_util -from certbot_apache.parser import get_aug_path -from certbot_apache.tests import util - - -NUM_ACHALLS = 3 - - -class ApacheHttp01Test(util.ApacheTest): - """Test for certbot_apache.http_01.ApacheHttp01.""" - - def setUp(self, *args, **kwargs): - super(ApacheHttp01Test, self).setUp(*args, **kwargs) - - self.account_key = self.rsa512jwk - self.achalls = [] # type: List[achallenges.KeyAuthorizationAnnotatedChallenge] - vh_truth = util.get_vh_truth( - self.temp_dir, "debian_apache_2_4/multiple_vhosts") - # Takes the vhosts for encryption-example.demo, certbot.demo, and - # vhost.in.rootconf - self.vhosts = [vh_truth[0], vh_truth[3], vh_truth[10]] - - for i in range(NUM_ACHALLS): - self.achalls.append( - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.HTTP01(token=((chr(ord('a') + i).encode() * 16))), - "pending"), - domain=self.vhosts[i].name, account_key=self.account_key)) - - modules = ["rewrite", "authz_core", "authz_host"] - for mod in modules: - self.config.parser.modules.add("mod_{0}.c".format(mod)) - self.config.parser.modules.add(mod + "_module") - - from certbot_apache.http_01 import ApacheHttp01 - self.http = ApacheHttp01(self.config) - - def test_empty_perform(self): - self.assertFalse(self.http.perform()) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.enable_mod") - def test_enable_modules_apache_2_2(self, mock_enmod): - self.config.version = (2, 2) - self.config.parser.modules.remove("authz_host_module") - self.config.parser.modules.remove("mod_authz_host.c") - - enmod_calls = self.common_enable_modules_test(mock_enmod) - self.assertEqual(enmod_calls[0][0][0], "authz_host") - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.enable_mod") - def test_enable_modules_apache_2_4(self, mock_enmod): - self.config.parser.modules.remove("authz_core_module") - self.config.parser.modules.remove("mod_authz_core.c") - - enmod_calls = self.common_enable_modules_test(mock_enmod) - self.assertEqual(enmod_calls[0][0][0], "authz_core") - - def common_enable_modules_test(self, mock_enmod): - """Tests enabling mod_rewrite and other modules.""" - self.config.parser.modules.remove("rewrite_module") - self.config.parser.modules.remove("mod_rewrite.c") - - self.http.prepare_http01_modules() - - self.assertTrue(mock_enmod.called) - calls = mock_enmod.call_args_list - other_calls = [] - for call in calls: - if "rewrite" != call[0][0]: - other_calls.append(call) - - # If these lists are equal, we never enabled mod_rewrite - self.assertNotEqual(calls, other_calls) - return other_calls - - def test_same_vhost(self): - vhost = next(v for v in self.config.vhosts if v.name == "certbot.demo") - achalls = [ - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.HTTP01(token=((b'a' * 16))), - "pending"), - domain=vhost.name, account_key=self.account_key), - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.HTTP01(token=((b'b' * 16))), - "pending"), - domain=next(iter(vhost.aliases)), account_key=self.account_key) - ] - self.common_perform_test(achalls, [vhost]) - - def test_anonymous_vhost(self): - vhosts = [v for v in self.config.vhosts if not v.ssl] - achalls = [ - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.HTTP01(token=((b'a' * 16))), - "pending"), - domain="something.nonexistent", account_key=self.account_key)] - self.common_perform_test(achalls, vhosts) - - def test_no_vhost(self): - for achall in self.achalls: - self.http.add_chall(achall) - self.config.config.http01_port = 12345 - self.assertRaises(errors.PluginError, self.http.perform) - - def test_perform_1_achall_apache_2_2(self): - self.combinations_perform_test(num_achalls=1, minor_version=2) - - def test_perform_1_achall_apache_2_4(self): - self.combinations_perform_test(num_achalls=1, minor_version=4) - - def test_perform_2_achall_apache_2_2(self): - self.combinations_perform_test(num_achalls=2, minor_version=2) - - def test_perform_2_achall_apache_2_4(self): - self.combinations_perform_test(num_achalls=2, minor_version=4) - - def test_perform_3_achall_apache_2_2(self): - self.combinations_perform_test(num_achalls=3, minor_version=2) - - def test_perform_3_achall_apache_2_4(self): - self.combinations_perform_test(num_achalls=3, minor_version=4) - - def test_activate_disabled_vhost(self): - vhosts = [v for v in self.config.vhosts if v.name == "certbot.demo"] - achalls = [ - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.HTTP01(token=((b'a' * 16))), - "pending"), - domain="certbot.demo", account_key=self.account_key)] - vhosts[0].enabled = False - self.common_perform_test(achalls, vhosts) - matches = self.config.parser.find_dir( - "Include", vhosts[0].filep, - get_aug_path(self.config.parser.loc["default"])) - self.assertEqual(len(matches), 1) - - def combinations_perform_test(self, num_achalls, minor_version): - """Test perform with the given achall count and Apache version.""" - achalls = self.achalls[:num_achalls] - vhosts = self.vhosts[:num_achalls] - self.config.version = (2, minor_version) - self.common_perform_test(achalls, vhosts) - - def common_perform_test(self, achalls, vhosts): - """Tests perform with the given achalls.""" - challenge_dir = self.http.challenge_dir - self.assertFalse(os.path.exists(challenge_dir)) - for achall in achalls: - self.http.add_chall(achall) - - expected_response = [ - achall.response(self.account_key) for achall in achalls] - self.assertEqual(self.http.perform(), expected_response) - - self.assertTrue(os.path.isdir(self.http.challenge_dir)) - self._has_min_permissions(self.http.challenge_dir, 0o755) - self._test_challenge_conf() - - for achall in achalls: - self._test_challenge_file(achall) - - for vhost in vhosts: - if not vhost.ssl: - matches = self.config.parser.find_dir("Include", - self.http.challenge_conf_pre, - vhost.path) - self.assertEqual(len(matches), 1) - matches = self.config.parser.find_dir("Include", - self.http.challenge_conf_post, - vhost.path) - self.assertEqual(len(matches), 1) - - self.assertTrue(os.path.exists(challenge_dir)) - - def _test_challenge_conf(self): - with open(self.http.challenge_conf_pre) as f: - pre_conf_contents = f.read() - - with open(self.http.challenge_conf_post) as f: - post_conf_contents = f.read() - - self.assertTrue("RewriteEngine on" in pre_conf_contents) - self.assertTrue("RewriteRule" in pre_conf_contents) - - self.assertTrue(self.http.challenge_dir in post_conf_contents) - if self.config.version < (2, 4): - self.assertTrue("Allow from all" in post_conf_contents) - else: - self.assertTrue("Require all granted" in post_conf_contents) - - def _test_challenge_file(self, achall): - name = os.path.join(self.http.challenge_dir, achall.chall.encode("token")) - validation = achall.validation(self.account_key) - - self._has_min_permissions(name, 0o644) - with open(name, 'rb') as f: - self.assertEqual(f.read(), validation.encode()) - - def _has_min_permissions(self, path, min_mode): - """Tests the given file has at least the permissions in mode.""" - st_mode = os.stat(path).st_mode - self.assertEqual(st_mode, st_mode | min_mode) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/obj_test.py b/certbot-apache/certbot_apache/tests/obj_test.py deleted file mode 100644 index 10dba18bc98..00000000000 --- a/certbot-apache/certbot_apache/tests/obj_test.py +++ /dev/null @@ -1,140 +0,0 @@ -"""Tests for certbot_apache.obj.""" -import unittest - - -class VirtualHostTest(unittest.TestCase): - """Test the VirtualHost class.""" - - def setUp(self): - from certbot_apache.obj import Addr - from certbot_apache.obj import VirtualHost - - self.addr1 = Addr.fromstring("127.0.0.1") - self.addr2 = Addr.fromstring("127.0.0.1:443") - self.addr_default = Addr.fromstring("_default_:443") - - self.vhost1 = VirtualHost( - "filep", "vh_path", set([self.addr1]), False, False, "localhost") - - self.vhost1b = VirtualHost( - "filep", "vh_path", set([self.addr1]), False, False, "localhost") - - self.vhost2 = VirtualHost( - "fp", "vhp", set([self.addr2]), False, False, "localhost") - - def test_repr(self): - self.assertEqual(repr(self.addr2), "certbot_apache.obj.Addr(('127.0.0.1', '443'))") - - def test_eq(self): - self.assertTrue(self.vhost1b == self.vhost1) - self.assertFalse(self.vhost1 == self.vhost2) - self.assertEqual(str(self.vhost1b), str(self.vhost1)) - self.assertFalse(self.vhost1b == 1234) - - def test_ne(self): - self.assertTrue(self.vhost1 != self.vhost2) - self.assertFalse(self.vhost1 != self.vhost1b) - - def test_conflicts(self): - from certbot_apache.obj import Addr - from certbot_apache.obj import VirtualHost - - complex_vh = VirtualHost( - "fp", "vhp", - set([Addr.fromstring("*:443"), Addr.fromstring("1.2.3.4:443")]), - False, False) - self.assertTrue(complex_vh.conflicts([self.addr1])) - self.assertTrue(complex_vh.conflicts([self.addr2])) - self.assertFalse(complex_vh.conflicts([self.addr_default])) - - self.assertTrue(self.vhost1.conflicts([self.addr2])) - self.assertFalse(self.vhost1.conflicts([self.addr_default])) - - self.assertFalse(self.vhost2.conflicts([self.addr1, - self.addr_default])) - - def test_same_server(self): - from certbot_apache.obj import VirtualHost - no_name1 = VirtualHost( - "fp", "vhp", set([self.addr1]), False, False, None) - no_name2 = VirtualHost( - "fp", "vhp", set([self.addr2]), False, False, None) - no_name3 = VirtualHost( - "fp", "vhp", set([self.addr_default]), - False, False, None) - no_name4 = VirtualHost( - "fp", "vhp", set([self.addr2, self.addr_default]), - False, False, None) - - self.assertTrue(self.vhost1.same_server(self.vhost2)) - self.assertTrue(no_name1.same_server(no_name2)) - - self.assertFalse(self.vhost1.same_server(no_name1)) - self.assertFalse(no_name1.same_server(no_name3)) - self.assertFalse(no_name1.same_server(no_name4)) - - -class AddrTest(unittest.TestCase): - """Test obj.Addr.""" - def setUp(self): - from certbot_apache.obj import Addr - self.addr = Addr.fromstring("*:443") - - self.addr1 = Addr.fromstring("127.0.0.1") - self.addr2 = Addr.fromstring("127.0.0.1:*") - - self.addr_defined = Addr.fromstring("127.0.0.1:443") - self.addr_default = Addr.fromstring("_default_:443") - - def test_wildcard(self): - self.assertFalse(self.addr.is_wildcard()) - self.assertTrue(self.addr1.is_wildcard()) - self.assertTrue(self.addr2.is_wildcard()) - - def test_get_sni_addr(self): - from certbot_apache.obj import Addr - self.assertEqual( - self.addr.get_sni_addr("443"), Addr.fromstring("*:443")) - self.assertEqual( - self.addr.get_sni_addr("225"), Addr.fromstring("*:225")) - self.assertEqual( - self.addr1.get_sni_addr("443"), Addr.fromstring("127.0.0.1")) - - def test_conflicts(self): - # Note: Defined IP is more important than defined port in match - self.assertTrue(self.addr.conflicts(self.addr1)) - self.assertTrue(self.addr.conflicts(self.addr2)) - self.assertTrue(self.addr.conflicts(self.addr_defined)) - self.assertFalse(self.addr.conflicts(self.addr_default)) - - self.assertFalse(self.addr1.conflicts(self.addr)) - self.assertTrue(self.addr1.conflicts(self.addr_defined)) - self.assertFalse(self.addr1.conflicts(self.addr_default)) - - self.assertFalse(self.addr_defined.conflicts(self.addr1)) - self.assertFalse(self.addr_defined.conflicts(self.addr2)) - self.assertFalse(self.addr_defined.conflicts(self.addr)) - self.assertFalse(self.addr_defined.conflicts(self.addr_default)) - - self.assertTrue(self.addr_default.conflicts(self.addr)) - self.assertTrue(self.addr_default.conflicts(self.addr1)) - self.assertTrue(self.addr_default.conflicts(self.addr_defined)) - - # Self test - self.assertTrue(self.addr.conflicts(self.addr)) - self.assertTrue(self.addr1.conflicts(self.addr1)) - # This is a tricky one... - self.assertTrue(self.addr1.conflicts(self.addr2)) - - def test_equal(self): - self.assertTrue(self.addr1 == self.addr2) - self.assertFalse(self.addr == self.addr1) - self.assertFalse(self.addr == 123) - - def test_not_equal(self): - self.assertFalse(self.addr1 != self.addr2) - self.assertTrue(self.addr != self.addr1) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/parser_test.py b/certbot-apache/certbot_apache/tests/parser_test.py deleted file mode 100644 index a089ec471b9..00000000000 --- a/certbot-apache/certbot_apache/tests/parser_test.py +++ /dev/null @@ -1,365 +0,0 @@ -"""Tests for certbot_apache.parser.""" -import os -import shutil -import unittest - -import augeas -import mock - -from certbot import errors - -from certbot_apache.tests import util - - -class BasicParserTest(util.ParserTest): - """Apache Parser Test.""" - - def setUp(self): # pylint: disable=arguments-differ - super(BasicParserTest, self).setUp() - - def tearDown(self): - shutil.rmtree(self.temp_dir) - shutil.rmtree(self.config_dir) - shutil.rmtree(self.work_dir) - - def test_find_config_root_no_root(self): - # pylint: disable=protected-access - os.remove(self.parser.loc["root"]) - self.assertRaises( - errors.NoInstallationError, self.parser._find_config_root) - - def test_parse_file(self): - """Test parse_file. - - certbot.conf is chosen as the test file as it will not be - included during the normal course of execution. - - """ - file_path = os.path.join( - self.config_path, "not-parsed-by-default", "certbot.conf") - - self.parser.parse_file(file_path) # pylint: disable=protected-access - - # search for the httpd incl - matches = self.parser.aug.match( - "/augeas/load/Httpd/incl [. ='%s']" % file_path) - - self.assertTrue(matches) - - def test_find_dir(self): - test = self.parser.find_dir("Listen", "80") - # This will only look in enabled hosts - test2 = self.parser.find_dir("documentroot") - - self.assertEqual(len(test), 1) - self.assertEqual(len(test2), 7) - - def test_add_dir(self): - aug_default = "/files" + self.parser.loc["default"] - self.parser.add_dir(aug_default, "AddDirective", "test") - - self.assertTrue( - self.parser.find_dir("AddDirective", "test", aug_default)) - - self.parser.add_dir(aug_default, "AddList", ["1", "2", "3", "4"]) - matches = self.parser.find_dir("AddList", None, aug_default) - for i, match in enumerate(matches): - self.assertEqual(self.parser.aug.get(match), str(i + 1)) - - def test_add_dir_beginning(self): - aug_default = "/files" + self.parser.loc["default"] - self.parser.add_dir_beginning(aug_default, - "AddDirectiveBeginning", - "testBegin") - - self.assertTrue( - self.parser.find_dir("AddDirectiveBeginning", "testBegin", aug_default)) - - self.assertEqual( - self.parser.aug.get(aug_default+"/directive[1]"), - "AddDirectiveBeginning") - self.parser.add_dir_beginning(aug_default, "AddList", ["1", "2", "3", "4"]) - matches = self.parser.find_dir("AddList", None, aug_default) - for i, match in enumerate(matches): - self.assertEqual(self.parser.aug.get(match), str(i + 1)) - - def test_empty_arg(self): - self.assertEqual(None, - self.parser.get_arg("/files/whatever/nonexistent")) - - def test_add_dir_to_ifmodssl(self): - """test add_dir_to_ifmodssl. - - Path must be valid before attempting to add to augeas - - """ - from certbot_apache.parser import get_aug_path - # This makes sure that find_dir will work - self.parser.modules.add("mod_ssl.c") - - self.parser.add_dir_to_ifmodssl( - get_aug_path(self.parser.loc["default"]), - "FakeDirective", ["123"]) - - matches = self.parser.find_dir("FakeDirective", "123") - - self.assertEqual(len(matches), 1) - self.assertTrue("IfModule" in matches[0]) - - def test_add_dir_to_ifmodssl_multiple(self): - from certbot_apache.parser import get_aug_path - # This makes sure that find_dir will work - self.parser.modules.add("mod_ssl.c") - - self.parser.add_dir_to_ifmodssl( - get_aug_path(self.parser.loc["default"]), - "FakeDirective", ["123", "456", "789"]) - - matches = self.parser.find_dir("FakeDirective") - - self.assertEqual(len(matches), 3) - self.assertTrue("IfModule" in matches[0]) - - def test_get_aug_path(self): - from certbot_apache.parser import get_aug_path - self.assertEqual("/files/etc/apache", get_aug_path("/etc/apache")) - - def test_set_locations(self): - with mock.patch("certbot_apache.parser.os.path") as mock_path: - - mock_path.isfile.side_effect = [False, False] - - # pylint: disable=protected-access - results = self.parser._set_locations() - - self.assertEqual(results["default"], results["listen"]) - self.assertEqual(results["default"], results["name"]) - - @mock.patch("certbot_apache.parser.ApacheParser.find_dir") - @mock.patch("certbot_apache.parser.ApacheParser.get_arg") - def test_parse_modules_bad_syntax(self, mock_arg, mock_find): - mock_find.return_value = ["1", "2", "3", "4", "5", "6", "7", "8"] - mock_arg.return_value = None - with mock.patch("certbot_apache.parser.logger") as mock_logger: - self.parser.parse_modules() - # Make sure that we got None return value and logged the file - self.assertTrue(mock_logger.debug.called) - - @mock.patch("certbot_apache.parser.ApacheParser.find_dir") - @mock.patch("certbot_apache.parser.ApacheParser._get_runtime_cfg") - def test_update_runtime_variables(self, mock_cfg, _): - define_val = ( - 'ServerRoot: "/etc/apache2"\n' - 'Main DocumentRoot: "/var/www"\n' - 'Main ErrorLog: "/var/log/apache2/error.log"\n' - 'Mutex ssl-stapling: using_defaults\n' - 'Mutex ssl-cache: using_defaults\n' - 'Mutex default: dir="/var/lock/apache2" mechanism=fcntl\n' - 'Mutex watchdog-callback: using_defaults\n' - 'PidFile: "/var/run/apache2/apache2.pid"\n' - 'Define: TEST\n' - 'Define: DUMP_RUN_CFG\n' - 'Define: U_MICH\n' - 'Define: TLS=443\n' - 'Define: example_path=Documents/path\n' - 'User: name="www-data" id=33 not_used\n' - 'Group: name="www-data" id=33 not_used\n' - ) - inc_val = ( - 'Included configuration files:\n' - ' (*) /etc/apache2/apache2.conf\n' - ' (146) /etc/apache2/mods-enabled/access_compat.load\n' - ' (146) /etc/apache2/mods-enabled/alias.load\n' - ' (146) /etc/apache2/mods-enabled/auth_basic.load\n' - ' (146) /etc/apache2/mods-enabled/authn_core.load\n' - ' (146) /etc/apache2/mods-enabled/authn_file.load\n' - ' (146) /etc/apache2/mods-enabled/authz_core.load\n' - ' (146) /etc/apache2/mods-enabled/authz_host.load\n' - ' (146) /etc/apache2/mods-enabled/authz_user.load\n' - ' (146) /etc/apache2/mods-enabled/autoindex.load\n' - ' (146) /etc/apache2/mods-enabled/deflate.load\n' - ' (146) /etc/apache2/mods-enabled/dir.load\n' - ' (146) /etc/apache2/mods-enabled/env.load\n' - ' (146) /etc/apache2/mods-enabled/filter.load\n' - ' (146) /etc/apache2/mods-enabled/mime.load\n' - ' (146) /etc/apache2/mods-enabled/mpm_event.load\n' - ' (146) /etc/apache2/mods-enabled/negotiation.load\n' - ' (146) /etc/apache2/mods-enabled/reqtimeout.load\n' - ' (146) /etc/apache2/mods-enabled/setenvif.load\n' - ' (146) /etc/apache2/mods-enabled/socache_shmcb.load\n' - ' (146) /etc/apache2/mods-enabled/ssl.load\n' - ' (146) /etc/apache2/mods-enabled/status.load\n' - ' (147) /etc/apache2/mods-enabled/alias.conf\n' - ' (147) /etc/apache2/mods-enabled/autoindex.conf\n' - ' (147) /etc/apache2/mods-enabled/deflate.conf\n' - ) - mod_val = ( - 'Loaded Modules:\n' - ' core_module (static)\n' - ' so_module (static)\n' - ' watchdog_module (static)\n' - ' http_module (static)\n' - ' log_config_module (static)\n' - ' logio_module (static)\n' - ' version_module (static)\n' - ' unixd_module (static)\n' - ' access_compat_module (shared)\n' - ' alias_module (shared)\n' - ' auth_basic_module (shared)\n' - ' authn_core_module (shared)\n' - ' authn_file_module (shared)\n' - ' authz_core_module (shared)\n' - ' authz_host_module (shared)\n' - ' authz_user_module (shared)\n' - ' autoindex_module (shared)\n' - ' deflate_module (shared)\n' - ' dir_module (shared)\n' - ' env_module (shared)\n' - ' filter_module (shared)\n' - ' mime_module (shared)\n' - ' mpm_event_module (shared)\n' - ' negotiation_module (shared)\n' - ' reqtimeout_module (shared)\n' - ' setenvif_module (shared)\n' - ' socache_shmcb_module (shared)\n' - ' ssl_module (shared)\n' - ' status_module (shared)\n' - ) - - def mock_get_vars(cmd): - """Mock command output""" - if cmd[-1] == "DUMP_RUN_CFG": - return define_val - elif cmd[-1] == "DUMP_INCLUDES": - return inc_val - elif cmd[-1] == "DUMP_MODULES": - return mod_val - - mock_cfg.side_effect = mock_get_vars - - expected_vars = {"TEST": "", "U_MICH": "", "TLS": "443", - "example_path": "Documents/path"} - - self.parser.modules = set() - with mock.patch( - "certbot_apache.parser.ApacheParser.parse_file") as mock_parse: - self.parser.update_runtime_variables() - self.assertEqual(self.parser.variables, expected_vars) - self.assertEqual(len(self.parser.modules), 58) - # None of the includes in inc_val should be in parsed paths. - # Make sure we tried to include them all. - self.assertEqual(mock_parse.call_count, 25) - - @mock.patch("certbot_apache.parser.ApacheParser.find_dir") - @mock.patch("certbot_apache.parser.ApacheParser._get_runtime_cfg") - def test_update_runtime_variables_alt_values(self, mock_cfg, _): - inc_val = ( - 'Included configuration files:\n' - ' (*) {0}\n' - ' (146) /etc/apache2/mods-enabled/access_compat.load\n' - ' (146) {1}/mods-enabled/alias.load\n' - ).format(self.parser.loc["root"], - os.path.dirname(self.parser.loc["root"])) - - mock_cfg.return_value = inc_val - self.parser.modules = set() - - with mock.patch( - "certbot_apache.parser.ApacheParser.parse_file") as mock_parse: - self.parser.update_runtime_variables() - # No matching modules should have been found - self.assertEqual(len(self.parser.modules), 0) - # Only one of the three includes do not exist in already parsed - # path derived from root configuration Include statements - self.assertEqual(mock_parse.call_count, 1) - - @mock.patch("certbot_apache.parser.ApacheParser._get_runtime_cfg") - def test_update_runtime_vars_bad_output(self, mock_cfg): - mock_cfg.return_value = "Define: TLS=443=24" - self.parser.update_runtime_variables() - - mock_cfg.return_value = "Define: DUMP_RUN_CFG\nDefine: TLS=443=24" - self.assertRaises( - errors.PluginError, self.parser.update_runtime_variables) - - @mock.patch("certbot_apache.configurator.ApacheConfigurator.option") - @mock.patch("certbot_apache.parser.subprocess.Popen") - def test_update_runtime_vars_bad_ctl(self, mock_popen, mock_opt): - mock_popen.side_effect = OSError - mock_opt.return_value = "nonexistent" - self.assertRaises( - errors.MisconfigurationError, - self.parser.update_runtime_variables) - - @mock.patch("certbot_apache.parser.subprocess.Popen") - def test_update_runtime_vars_bad_exit(self, mock_popen): - mock_popen().communicate.return_value = ("", "") - mock_popen.returncode = -1 - self.assertRaises( - errors.MisconfigurationError, - self.parser.update_runtime_variables) - - def test_add_comment(self): - from certbot_apache.parser import get_aug_path - self.parser.add_comment(get_aug_path(self.parser.loc["name"]), "123456") - comm = self.parser.find_comments("123456") - self.assertEqual(len(comm), 1) - self.assertTrue(self.parser.loc["name"] in comm[0]) - - -class ParserInitTest(util.ApacheTest): - def setUp(self): # pylint: disable=arguments-differ - super(ParserInitTest, self).setUp() - self.aug = augeas.Augeas( - flags=augeas.Augeas.NONE | augeas.Augeas.NO_MODL_AUTOLOAD) - - def tearDown(self): - shutil.rmtree(self.temp_dir) - shutil.rmtree(self.config_dir) - shutil.rmtree(self.work_dir) - - @mock.patch("certbot_apache.parser.ApacheParser._get_runtime_cfg") - def test_unparseable(self, mock_cfg): - from certbot_apache.parser import ApacheParser - mock_cfg.return_value = ('Define: TEST') - self.assertRaises( - errors.PluginError, - ApacheParser, self.aug, os.path.relpath(self.config_path), - "/dummy/vhostpath", version=(2, 2, 22), configurator=self.config) - - def test_root_normalized(self): - from certbot_apache.parser import ApacheParser - - with mock.patch("certbot_apache.parser.ApacheParser." - "update_runtime_variables"): - path = os.path.join( - self.temp_dir, - "debian_apache_2_4/////multiple_vhosts/../multiple_vhosts/apache2") - - parser = ApacheParser(self.aug, path, - "/dummy/vhostpath", configurator=self.config) - - self.assertEqual(parser.root, self.config_path) - - def test_root_absolute(self): - from certbot_apache.parser import ApacheParser - with mock.patch("certbot_apache.parser.ApacheParser." - "update_runtime_variables"): - parser = ApacheParser( - self.aug, os.path.relpath(self.config_path), - "/dummy/vhostpath", configurator=self.config) - - self.assertEqual(parser.root, self.config_path) - - def test_root_no_trailing_slash(self): - from certbot_apache.parser import ApacheParser - with mock.patch("certbot_apache.parser.ApacheParser." - "update_runtime_variables"): - parser = ApacheParser( - self.aug, self.config_path + os.path.sep, - "/dummy/vhostpath", configurator=self.config) - self.assertEqual(parser.root, self.config_path) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/ssl.conf b/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/ssl.conf deleted file mode 100644 index 6e2502e9a10..00000000000 --- a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/ssl.conf +++ /dev/null @@ -1,211 +0,0 @@ -# -# When we also provide SSL we have to listen to the -# the HTTPS port in addition. -# -Listen 443 https - -## -## SSL Global Context -## -## All SSL configuration in this context applies both to -## the main server and all SSL-enabled virtual hosts. -## - -# Pass Phrase Dialog: -# Configure the pass phrase gathering process. -# The filtering dialog program (`builtin' is a internal -# terminal dialog) has to provide the pass phrase on stdout. -SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog - -# Inter-Process Session Cache: -# Configure the SSL Session Cache: First the mechanism -# to use and second the expiring timeout (in seconds). -SSLSessionCache shmcb:/run/httpd/sslcache(512000) -SSLSessionCacheTimeout 300 - -# Pseudo Random Number Generator (PRNG): -# Configure one or more sources to seed the PRNG of the -# SSL library. The seed data should be of good random quality. -# WARNING! On some platforms /dev/random blocks if not enough entropy -# is available. This means you then cannot use the /dev/random device -# because it would lead to very long connection times (as long as -# it requires to make more entropy available). But usually those -# platforms additionally provide a /dev/urandom device which doesn't -# block. So, if available, use this one instead. Read the mod_ssl User -# Manual for more details. -SSLRandomSeed startup file:/dev/urandom 256 -SSLRandomSeed connect builtin -#SSLRandomSeed startup file:/dev/random 512 -#SSLRandomSeed connect file:/dev/random 512 -#SSLRandomSeed connect file:/dev/urandom 512 - -# -# Use "SSLCryptoDevice" to enable any supported hardware -# accelerators. Use "openssl engine -v" to list supported -# engine names. NOTE: If you enable an accelerator and the -# server does not start, consult the error logs and ensure -# your accelerator is functioning properly. -# -SSLCryptoDevice builtin -#SSLCryptoDevice ubsec - -## -## SSL Virtual Host Context -## - - - -# General setup for the virtual host, inherited from global configuration -#DocumentRoot "/var/www/html" -#ServerName www.example.com:443 - -# Use separate log files for the SSL virtual host; note that LogLevel -# is not inherited from httpd.conf. -ErrorLog logs/ssl_error_log -TransferLog logs/ssl_access_log -LogLevel warn - -# SSL Engine Switch: -# Enable/Disable SSL for this virtual host. -SSLEngine on - -# SSL Protocol support: -# List the enable protocol levels with which clients will be able to -# connect. Disable SSLv2 access by default: -SSLProtocol all -SSLv2 - -# SSL Cipher Suite: -# List the ciphers that the client is permitted to negotiate. -# See the mod_ssl documentation for a complete list. -SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!SEED:!IDEA - -# Speed-optimized SSL Cipher configuration: -# If speed is your main concern (on busy HTTPS servers e.g.), -# you might want to force clients to specific, performance -# optimized ciphers. In this case, prepend those ciphers -# to the SSLCipherSuite list, and enable SSLHonorCipherOrder. -# Caveat: by giving precedence to RC4-SHA and AES128-SHA -# (as in the example below), most connections will no longer -# have perfect forward secrecy - if the server's key is -# compromised, captures of past or future traffic must be -# considered compromised, too. -#SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5 -#SSLHonorCipherOrder on - -# Server Certificate: -# Point SSLCertificateFile at a PEM encoded certificate. If -# the certificate is encrypted, then you will be prompted for a -# pass phrase. Note that a kill -HUP will prompt again. A new -# certificate can be generated using the genkey(1) command. - -# Server Private Key: -# If the key is not combined with the certificate, use this -# directive to point at the key file. Keep in mind that if -# you've both a RSA and a DSA private key you can configure -# both in parallel (to also allow the use of DSA ciphers, etc.) - -# Server Certificate Chain: -# Point SSLCertificateChainFile at a file containing the -# concatenation of PEM encoded CA certificates which form the -# certificate chain for the server certificate. Alternatively -# the referenced file can be the same as SSLCertificateFile -# when the CA certificates are directly appended to the server -# certificate for convinience. -#SSLCertificateChainFile /etc/pki/tls/certs/server-chain.crt - -# Certificate Authority (CA): -# Set the CA certificate verification path where to find CA -# certificates for client authentication or alternatively one -# huge file containing all of them (file must be PEM encoded) -#SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt - -# Client Authentication (Type): -# Client certificate verification type and depth. Types are -# none, optional, require and optional_no_ca. Depth is a -# number which specifies how deeply to verify the certificate -# issuer chain before deciding the certificate is not valid. -#SSLVerifyClient require -#SSLVerifyDepth 10 - -# Access Control: -# With SSLRequire you can do per-directory access control based -# on arbitrary complex boolean expressions containing server -# variable checks and other lookup directives. The syntax is a -# mixture between C and Perl. See the mod_ssl documentation -# for more details. -# -#SSLRequire ( %{SSL_CIPHER} !~ m/^(EXP|NULL)/ \ -# and %{SSL_CLIENT_S_DN_O} eq "Snake Oil, Ltd." \ -# and %{SSL_CLIENT_S_DN_OU} in {"Staff", "CA", "Dev"} \ -# and %{TIME_WDAY} >= 1 and %{TIME_WDAY} <= 5 \ -# and %{TIME_HOUR} >= 8 and %{TIME_HOUR} <= 20 ) \ -# or %{REMOTE_ADDR} =~ m/^192\.76\.162\.[0-9]+$/ -# - -# SSL Engine Options: -# Set various options for the SSL engine. -# o FakeBasicAuth: -# Translate the client X.509 into a Basic Authorisation. This means that -# the standard Auth/DBMAuth methods can be used for access control. The -# user name is the `one line' version of the client's X.509 certificate. -# Note that no password is obtained from the user. Every entry in the user -# file needs this password: `xxj31ZMTZzkVA'. -# o ExportCertData: -# This exports two additional environment variables: SSL_CLIENT_CERT and -# SSL_SERVER_CERT. These contain the PEM-encoded certificates of the -# server (always existing) and the client (only existing when client -# authentication is used). This can be used to import the certificates -# into CGI scripts. -# o StdEnvVars: -# This exports the standard SSL/TLS related `SSL_*' environment variables. -# Per default this exportation is switched off for performance reasons, -# because the extraction step is an expensive operation and is usually -# useless for serving static content. So one usually enables the -# exportation for CGI and SSI requests only. -# o StrictRequire: -# This denies access when "SSLRequireSSL" or "SSLRequire" applied even -# under a "Satisfy any" situation, i.e. when it applies access is denied -# and no other module can change it. -# o OptRenegotiate: -# This enables optimized SSL connection renegotiation handling when SSL -# directives are used in per-directory context. -#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire - - SSLOptions +StdEnvVars - - - SSLOptions +StdEnvVars - - -# SSL Protocol Adjustments: -# The safe and default but still SSL/TLS standard compliant shutdown -# approach is that mod_ssl sends the close notify alert but doesn't wait for -# the close notify alert from client. When you need a different shutdown -# approach you can use one of the following variables: -# o ssl-unclean-shutdown: -# This forces an unclean shutdown when the connection is closed, i.e. no -# SSL close notify alert is send or allowed to received. This violates -# the SSL/TLS standard but is needed for some brain-dead browsers. Use -# this when you receive I/O errors because of the standard approach where -# mod_ssl sends the close notify alert. -# o ssl-accurate-shutdown: -# This forces an accurate shutdown when the connection is closed, i.e. a -# SSL close notify alert is send and mod_ssl waits for the close notify -# alert of the client. This is 100% SSL/TLS standard compliant, but in -# practice often causes hanging connections with brain-dead browsers. Use -# this only for browsers where you know that their SSL implementation -# works correctly. -# Notice: Most problems of broken clients are also related to the HTTP -# keep-alive facility, so you usually additionally want to disable -# keep-alive for those clients, too. Use variable "nokeepalive" for this. -# Similarly, one has to force some clients to use HTTP/1.0 to workaround -# their broken HTTP/1.1 implementation. Use variables "downgrade-1.0" and -# "force-response-1.0" for this. -BrowserMatch "MSIE [2-5]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0 - -# Per-Server Logging: -# The home of a custom SSL log file. Use this when you want a -# compact non-error SSL logfile on a virtual host basis. -CustomLog logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" - - diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.conf b/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.conf deleted file mode 100644 index e9fcf4f9bbf..00000000000 --- a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.conf +++ /dev/null @@ -1,89 +0,0 @@ - - - # Pseudo Random Number Generator (PRNG): - # Configure one or more sources to seed the PRNG of the SSL library. - # The seed data should be of good random quality. - # WARNING! On some platforms /dev/random blocks if not enough entropy - # is available. This means you then cannot use the /dev/random device - # because it would lead to very long connection times (as long as - # it requires to make more entropy available). But usually those - # platforms additionally provide a /dev/urandom device which doesn't - # block. So, if available, use this one instead. Read the mod_ssl User - # Manual for more details. - # - SSLRandomSeed startup builtin - SSLRandomSeed startup file:/dev/urandom 512 - SSLRandomSeed connect builtin - SSLRandomSeed connect file:/dev/urandom 512 - - ## - ## SSL Global Context - ## - ## All SSL configuration in this context applies both to - ## the main server and all SSL-enabled virtual hosts. - ## - - # - # Some MIME-types for downloading Certificates and CRLs - # - AddType application/x-x509-ca-cert .crt - AddType application/x-pkcs7-crl .crl - - # Pass Phrase Dialog: - # Configure the pass phrase gathering process. - # The filtering dialog program (`builtin' is a internal - # terminal dialog) has to provide the pass phrase on stdout. - SSLPassPhraseDialog exec:/usr/share/apache2/ask-for-passphrase - - # Inter-Process Session Cache: - # Configure the SSL Session Cache: First the mechanism - # to use and second the expiring timeout (in seconds). - # (The mechanism dbm has known memory leaks and should not be used). - #SSLSessionCache dbm:${APACHE_RUN_DIR}/ssl_scache - SSLSessionCache shmcb:${APACHE_RUN_DIR}/ssl_scache(512000) - SSLSessionCacheTimeout 300 - - # Semaphore: - # Configure the path to the mutual exclusion semaphore the - # SSL engine uses internally for inter-process synchronization. - # (Disabled by default, the global Mutex directive consolidates by default - # this) - #Mutex file:${APACHE_LOCK_DIR}/ssl_mutex ssl-cache - - - # SSL Cipher Suite: - # List the ciphers that the client is permitted to negotiate. See the - # ciphers(1) man page from the openssl package for list of all available - # options. - # Enable only secure ciphers: - SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5 - - # Speed-optimized SSL Cipher configuration: - # If speed is your main concern (on busy HTTPS servers e.g.), - # you might want to force clients to specific, performance - # optimized ciphers. In this case, prepend those ciphers - # to the SSLCipherSuite list, and enable SSLHonorCipherOrder. - # Caveat: by giving precedence to RC4-SHA and AES128-SHA - # (as in the example below), most connections will no longer - # have perfect forward secrecy - if the server's key is - # compromised, captures of past or future traffic must be - # considered compromised, too. - #SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5 - #SSLHonorCipherOrder on - - # The protocols to enable. - # Available values: all, SSLv3, TLSv1, TLSv1.1, TLSv1.2 - # SSL v2 is no longer supported - SSLProtocol all - - # Allow insecure renegotiation with clients which do not yet support the - # secure renegotiation protocol. Default: Off - #SSLInsecureRenegotiation on - - # Whether to forbid non-SNI clients to access name based virtual hosts. - # Default: Off - #SSLStrictSNIVHostCheck On - - - -# vim: syntax=apache ts=4 sw=4 sts=4 sr noet diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/old,default.conf b/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/old,default.conf deleted file mode 120000 index 6782fe9e5c4..00000000000 --- a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/old,default.conf +++ /dev/null @@ -1 +0,0 @@ -../sites-available/old,default.conf \ No newline at end of file diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.conf b/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.conf deleted file mode 100644 index e9fcf4f9bbf..00000000000 --- a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.conf +++ /dev/null @@ -1,89 +0,0 @@ - - - # Pseudo Random Number Generator (PRNG): - # Configure one or more sources to seed the PRNG of the SSL library. - # The seed data should be of good random quality. - # WARNING! On some platforms /dev/random blocks if not enough entropy - # is available. This means you then cannot use the /dev/random device - # because it would lead to very long connection times (as long as - # it requires to make more entropy available). But usually those - # platforms additionally provide a /dev/urandom device which doesn't - # block. So, if available, use this one instead. Read the mod_ssl User - # Manual for more details. - # - SSLRandomSeed startup builtin - SSLRandomSeed startup file:/dev/urandom 512 - SSLRandomSeed connect builtin - SSLRandomSeed connect file:/dev/urandom 512 - - ## - ## SSL Global Context - ## - ## All SSL configuration in this context applies both to - ## the main server and all SSL-enabled virtual hosts. - ## - - # - # Some MIME-types for downloading Certificates and CRLs - # - AddType application/x-x509-ca-cert .crt - AddType application/x-pkcs7-crl .crl - - # Pass Phrase Dialog: - # Configure the pass phrase gathering process. - # The filtering dialog program (`builtin' is a internal - # terminal dialog) has to provide the pass phrase on stdout. - SSLPassPhraseDialog exec:/usr/share/apache2/ask-for-passphrase - - # Inter-Process Session Cache: - # Configure the SSL Session Cache: First the mechanism - # to use and second the expiring timeout (in seconds). - # (The mechanism dbm has known memory leaks and should not be used). - #SSLSessionCache dbm:${APACHE_RUN_DIR}/ssl_scache - SSLSessionCache shmcb:${APACHE_RUN_DIR}/ssl_scache(512000) - SSLSessionCacheTimeout 300 - - # Semaphore: - # Configure the path to the mutual exclusion semaphore the - # SSL engine uses internally for inter-process synchronization. - # (Disabled by default, the global Mutex directive consolidates by default - # this) - #Mutex file:${APACHE_LOCK_DIR}/ssl_mutex ssl-cache - - - # SSL Cipher Suite: - # List the ciphers that the client is permitted to negotiate. See the - # ciphers(1) man page from the openssl package for list of all available - # options. - # Enable only secure ciphers: - SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5 - - # Speed-optimized SSL Cipher configuration: - # If speed is your main concern (on busy HTTPS servers e.g.), - # you might want to force clients to specific, performance - # optimized ciphers. In this case, prepend those ciphers - # to the SSLCipherSuite list, and enable SSLHonorCipherOrder. - # Caveat: by giving precedence to RC4-SHA and AES128-SHA - # (as in the example below), most connections will no longer - # have perfect forward secrecy - if the server's key is - # compromised, captures of past or future traffic must be - # considered compromised, too. - #SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5 - #SSLHonorCipherOrder on - - # The protocols to enable. - # Available values: all, SSLv3, TLSv1, TLSv1.1, TLSv1.2 - # SSL v2 is no longer supported - SSLProtocol all - - # Allow insecure renegotiation with clients which do not yet support the - # secure renegotiation protocol. Default: Off - #SSLInsecureRenegotiation on - - # Whether to forbid non-SNI clients to access name based virtual hosts. - # Default: Off - #SSLStrictSNIVHostCheck On - - - -# vim: syntax=apache ts=4 sw=4 sts=4 sr noet diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.conf b/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.conf deleted file mode 100644 index e9fcf4f9bbf..00000000000 --- a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.conf +++ /dev/null @@ -1,89 +0,0 @@ - - - # Pseudo Random Number Generator (PRNG): - # Configure one or more sources to seed the PRNG of the SSL library. - # The seed data should be of good random quality. - # WARNING! On some platforms /dev/random blocks if not enough entropy - # is available. This means you then cannot use the /dev/random device - # because it would lead to very long connection times (as long as - # it requires to make more entropy available). But usually those - # platforms additionally provide a /dev/urandom device which doesn't - # block. So, if available, use this one instead. Read the mod_ssl User - # Manual for more details. - # - SSLRandomSeed startup builtin - SSLRandomSeed startup file:/dev/urandom 512 - SSLRandomSeed connect builtin - SSLRandomSeed connect file:/dev/urandom 512 - - ## - ## SSL Global Context - ## - ## All SSL configuration in this context applies both to - ## the main server and all SSL-enabled virtual hosts. - ## - - # - # Some MIME-types for downloading Certificates and CRLs - # - AddType application/x-x509-ca-cert .crt - AddType application/x-pkcs7-crl .crl - - # Pass Phrase Dialog: - # Configure the pass phrase gathering process. - # The filtering dialog program (`builtin' is a internal - # terminal dialog) has to provide the pass phrase on stdout. - SSLPassPhraseDialog exec:/usr/share/apache2/ask-for-passphrase - - # Inter-Process Session Cache: - # Configure the SSL Session Cache: First the mechanism - # to use and second the expiring timeout (in seconds). - # (The mechanism dbm has known memory leaks and should not be used). - #SSLSessionCache dbm:${APACHE_RUN_DIR}/ssl_scache - SSLSessionCache shmcb:${APACHE_RUN_DIR}/ssl_scache(512000) - SSLSessionCacheTimeout 300 - - # Semaphore: - # Configure the path to the mutual exclusion semaphore the - # SSL engine uses internally for inter-process synchronization. - # (Disabled by default, the global Mutex directive consolidates by default - # this) - #Mutex file:${APACHE_LOCK_DIR}/ssl_mutex ssl-cache - - - # SSL Cipher Suite: - # List the ciphers that the client is permitted to negotiate. See the - # ciphers(1) man page from the openssl package for list of all available - # options. - # Enable only secure ciphers: - SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5 - - # Speed-optimized SSL Cipher configuration: - # If speed is your main concern (on busy HTTPS servers e.g.), - # you might want to force clients to specific, performance - # optimized ciphers. In this case, prepend those ciphers - # to the SSLCipherSuite list, and enable SSLHonorCipherOrder. - # Caveat: by giving precedence to RC4-SHA and AES128-SHA - # (as in the example below), most connections will no longer - # have perfect forward secrecy - if the server's key is - # compromised, captures of past or future traffic must be - # considered compromised, too. - #SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5 - #SSLHonorCipherOrder on - - # The protocols to enable. - # Available values: all, SSLv3, TLSv1, TLSv1.1, TLSv1.2 - # SSL v2 is no longer supported - SSLProtocol all - - # Allow insecure renegotiation with clients which do not yet support the - # secure renegotiation protocol. Default: Off - #SSLInsecureRenegotiation on - - # Whether to forbid non-SNI clients to access name based virtual hosts. - # Default: Off - #SSLStrictSNIVHostCheck On - - - -# vim: syntax=apache ts=4 sw=4 sts=4 sr noet diff --git a/certbot-apache/certbot_apache/tests/tls_sni_01_test.py b/certbot-apache/certbot_apache/tests/tls_sni_01_test.py deleted file mode 100644 index 8cea97f04d0..00000000000 --- a/certbot-apache/certbot_apache/tests/tls_sni_01_test.py +++ /dev/null @@ -1,151 +0,0 @@ -"""Test for certbot_apache.tls_sni_01.""" -import shutil -import unittest - -import mock - -from certbot import errors -from certbot.plugins import common_test - -from certbot_apache import obj -from certbot_apache.tests import util - -from six.moves import xrange # pylint: disable=redefined-builtin, import-error - - -class TlsSniPerformTest(util.ApacheTest): - """Test the ApacheTlsSni01 challenge.""" - - auth_key = common_test.AUTH_KEY - achalls = common_test.ACHALLS - - def setUp(self): # pylint: disable=arguments-differ - super(TlsSniPerformTest, self).setUp() - - config = util.get_apache_configurator( - self.config_path, self.vhost_path, self.config_dir, - self.work_dir) - config.config.tls_sni_01_port = 443 - - from certbot_apache import tls_sni_01 - self.sni = tls_sni_01.ApacheTlsSni01(config) - - def tearDown(self): - shutil.rmtree(self.temp_dir) - shutil.rmtree(self.config_dir) - shutil.rmtree(self.work_dir) - - def test_perform0(self): - resp = self.sni.perform() - self.assertEqual(len(resp), 0) - - @mock.patch("certbot.util.exe_exists") - @mock.patch("certbot.util.run_script") - def test_perform1(self, _, mock_exists): - self.sni.configurator.parser.modules.add("socache_shmcb_module") - self.sni.configurator.parser.modules.add("ssl_module") - - mock_exists.return_value = True - self.sni.configurator.parser.update_runtime_variables = mock.Mock() - - achall = self.achalls[0] - self.sni.add_chall(achall) - response = self.achalls[0].response(self.auth_key) - mock_setup_cert = mock.MagicMock(return_value=response) - # pylint: disable=protected-access - self.sni._setup_challenge_cert = mock_setup_cert - - responses = self.sni.perform() - mock_setup_cert.assert_called_once_with(achall) - - # Check to make sure challenge config path is included in apache config - self.assertEqual( - len(self.sni.configurator.parser.find_dir( - "Include", self.sni.challenge_conf)), 1) - self.assertEqual(len(responses), 1) - self.assertEqual(responses[0], response) - - def test_perform2(self): - # Avoid load module - self.sni.configurator.parser.modules.add("ssl_module") - self.sni.configurator.parser.modules.add("socache_shmcb_module") - acme_responses = [] - for achall in self.achalls: - self.sni.add_chall(achall) - acme_responses.append(achall.response(self.auth_key)) - - mock_setup_cert = mock.MagicMock(side_effect=acme_responses) - # pylint: disable=protected-access - self.sni._setup_challenge_cert = mock_setup_cert - - with mock.patch( - "certbot_apache.override_debian.DebianConfigurator.enable_mod"): - sni_responses = self.sni.perform() - - self.assertEqual(mock_setup_cert.call_count, 2) - - # Make sure calls made to mocked function were correct - self.assertEqual( - mock_setup_cert.call_args_list[0], mock.call(self.achalls[0])) - self.assertEqual( - mock_setup_cert.call_args_list[1], mock.call(self.achalls[1])) - - self.assertEqual( - len(self.sni.configurator.parser.find_dir( - "Include", self.sni.challenge_conf)), - 1) - self.assertEqual(len(sni_responses), 2) - for i in xrange(2): - self.assertEqual(sni_responses[i], acme_responses[i]) - - def test_mod_config(self): - z_domains = [] - for achall in self.achalls: - self.sni.add_chall(achall) - z_domain = achall.response(self.auth_key).z_domain - z_domains.append(set([z_domain.decode('ascii')])) - - self.sni._mod_config() # pylint: disable=protected-access - self.sni.configurator.save() - - self.sni.configurator.parser.find_dir( - "Include", self.sni.challenge_conf) - vh_match = self.sni.configurator.aug.match( - "/files" + self.sni.challenge_conf + "//VirtualHost") - - vhs = [] - for match in vh_match: - # pylint: disable=protected-access - vhs.append(self.sni.configurator._create_vhost(match)) - self.assertEqual(len(vhs), 2) - for vhost in vhs: - self.assertEqual(vhost.addrs, set([obj.Addr.fromstring("*:443")])) - names = vhost.get_names() - self.assertTrue(names in z_domains) - - def test_get_addrs_default(self): - self.sni.configurator.choose_vhost = mock.Mock( - return_value=obj.VirtualHost( - "path", "aug_path", - set([obj.Addr.fromstring("_default_:443")]), - False, False) - ) - - # pylint: disable=protected-access - self.assertEqual( - set([obj.Addr.fromstring("*:443")]), - self.sni._get_addrs(self.achalls[0])) - - def test_get_addrs_no_vhost_found(self): - self.sni.configurator.choose_vhost = mock.Mock( - side_effect=errors.MissingCommandlineFlag( - "Failed to run Apache plugin non-interactively")) - - # pylint: disable=protected-access - self.assertEqual( - set([obj.Addr.fromstring("*:443")]), - self.sni._get_addrs(self.achalls[0])) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/util.py b/certbot-apache/certbot_apache/tests/util.py deleted file mode 100644 index 9329ccb2005..00000000000 --- a/certbot-apache/certbot_apache/tests/util.py +++ /dev/null @@ -1,232 +0,0 @@ -"""Common utilities for certbot_apache.""" -import os -import shutil -import sys -import unittest - -import augeas -import josepy as jose -import mock -import zope.component - -from certbot.display import util as display_util - -from certbot.plugins import common - -from certbot.tests import util as test_util - -from certbot_apache import configurator -from certbot_apache import entrypoint -from certbot_apache import obj - - -class ApacheTest(unittest.TestCase): # pylint: disable=too-few-public-methods - - def setUp(self, test_dir="debian_apache_2_4/multiple_vhosts", - config_root="debian_apache_2_4/multiple_vhosts/apache2", - vhost_root="debian_apache_2_4/multiple_vhosts/apache2/sites-available"): - # pylint: disable=arguments-differ - super(ApacheTest, self).setUp() - - self.temp_dir, self.config_dir, self.work_dir = common.dir_setup( - test_dir=test_dir, - pkg="certbot_apache.tests") - - self.config_path = os.path.join(self.temp_dir, config_root) - self.vhost_path = os.path.join(self.temp_dir, vhost_root) - - self.rsa512jwk = jose.JWKRSA.load(test_util.load_vector( - "rsa512_key.pem")) - - self.config = get_apache_configurator(self.config_path, vhost_root, - self.config_dir, self.work_dir) - - # Make sure all vhosts in sites-enabled are symlinks (Python packaging - # does not preserve symlinks) - sites_enabled = os.path.join(self.config_path, "sites-enabled") - if not os.path.exists(sites_enabled): - return - - for vhost_basename in os.listdir(sites_enabled): - # Keep the one non-symlink test vhost in place - if vhost_basename == "non-symlink.conf": - continue - vhost = os.path.join(sites_enabled, vhost_basename) - if not os.path.islink(vhost): # pragma: no cover - os.remove(vhost) - target = os.path.join( - os.path.pardir, "sites-available", vhost_basename) - os.symlink(target, vhost) - - def tearDown(self): - shutil.rmtree(self.temp_dir) - shutil.rmtree(self.config_dir) - shutil.rmtree(self.work_dir) - - -class ParserTest(ApacheTest): - - def setUp(self, test_dir="debian_apache_2_4/multiple_vhosts", - config_root="debian_apache_2_4/multiple_vhosts/apache2", - vhost_root="debian_apache_2_4/multiple_vhosts/apache2/sites-available"): - super(ParserTest, self).setUp(test_dir, config_root, vhost_root) - - zope.component.provideUtility(display_util.FileDisplay(sys.stdout, - False)) - - from certbot_apache.parser import ApacheParser - self.aug = augeas.Augeas( - flags=augeas.Augeas.NONE | augeas.Augeas.NO_MODL_AUTOLOAD) - with mock.patch("certbot_apache.parser.ApacheParser." - "update_runtime_variables"): - self.parser = ApacheParser( - self.aug, self.config_path, self.vhost_path, - configurator=self.config) - - -def get_apache_configurator( # pylint: disable=too-many-arguments, too-many-locals - config_path, vhost_path, - config_dir, work_dir, version=(2, 4, 7), - os_info="generic", - conf_vhost_path=None): - """Create an Apache Configurator with the specified options. - - :param conf: Function that returns binary paths. self.conf in Configurator - - """ - backups = os.path.join(work_dir, "backups") - mock_le_config = mock.MagicMock( - apache_server_root=config_path, - apache_vhost_root=None, - apache_le_vhost_ext="-le-ssl.conf", - apache_challenge_location=config_path, - apache_enmod=None, - backup_dir=backups, - config_dir=config_dir, - http01_port=80, - temp_checkpoint_dir=os.path.join(work_dir, "temp_checkpoints"), - in_progress_dir=os.path.join(backups, "IN_PROGRESS"), - work_dir=work_dir) - - with mock.patch("certbot_apache.configurator.util.run_script"): - with mock.patch("certbot_apache.configurator.util." - "exe_exists") as mock_exe_exists: - mock_exe_exists.return_value = True - with mock.patch("certbot_apache.parser.ApacheParser." - "update_runtime_variables"): - try: - config_class = entrypoint.OVERRIDE_CLASSES[os_info] - except KeyError: - config_class = configurator.ApacheConfigurator - config = config_class(config=mock_le_config, name="apache", - version=version) - if not conf_vhost_path: - config_class.OS_DEFAULTS["vhost_root"] = vhost_path - else: - # Custom virtualhost path was requested - config.config.apache_vhost_root = conf_vhost_path - config.config.apache_ctl = config_class.OS_DEFAULTS["ctl"] - config.prepare() - return config - - -def get_vh_truth(temp_dir, config_name): - """Return the ground truth for the specified directory.""" - if config_name == "debian_apache_2_4/multiple_vhosts": - prefix = os.path.join( - temp_dir, config_name, "apache2/sites-enabled") - - aug_pre = "/files" + prefix - vh_truth = [ - obj.VirtualHost( - os.path.join(prefix, "encryption-example.conf"), - os.path.join(aug_pre, "encryption-example.conf/Virtualhost"), - set([obj.Addr.fromstring("*:80")]), - False, True, "encryption-example.demo"), - obj.VirtualHost( - os.path.join(prefix, "default-ssl.conf"), - os.path.join(aug_pre, - "default-ssl.conf/IfModule/VirtualHost"), - set([obj.Addr.fromstring("_default_:443")]), True, True), - obj.VirtualHost( - os.path.join(prefix, "000-default.conf"), - os.path.join(aug_pre, "000-default.conf/VirtualHost"), - set([obj.Addr.fromstring("*:80"), - obj.Addr.fromstring("[::]:80")]), - False, True, "ip-172-30-0-17"), - obj.VirtualHost( - os.path.join(prefix, "certbot.conf"), - os.path.join(aug_pre, "certbot.conf/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), False, True, - "certbot.demo", aliases=["www.certbot.demo"]), - obj.VirtualHost( - os.path.join(prefix, "mod_macro-example.conf"), - os.path.join(aug_pre, - "mod_macro-example.conf/Macro/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), False, True, - modmacro=True), - obj.VirtualHost( - os.path.join(prefix, "default-ssl-port-only.conf"), - os.path.join(aug_pre, ("default-ssl-port-only.conf/" - "IfModule/VirtualHost")), - set([obj.Addr.fromstring("_default_:443")]), True, True), - obj.VirtualHost( - os.path.join(prefix, "wildcard.conf"), - os.path.join(aug_pre, "wildcard.conf/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), False, True, - "ip-172-30-0-17", aliases=["*.blue.purple.com"]), - obj.VirtualHost( - os.path.join(prefix, "ocsp-ssl.conf"), - os.path.join(aug_pre, "ocsp-ssl.conf/IfModule/VirtualHost"), - set([obj.Addr.fromstring("10.2.3.4:443")]), True, True, - "ocspvhost.com"), - obj.VirtualHost( - os.path.join(prefix, "non-symlink.conf"), - os.path.join(aug_pre, "non-symlink.conf/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), False, True, - "nonsym.link"), - obj.VirtualHost( - os.path.join(prefix, "default-ssl-port-only.conf"), - os.path.join(aug_pre, - "default-ssl-port-only.conf/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), True, True, ""), - obj.VirtualHost( - os.path.join(temp_dir, config_name, - "apache2/apache2.conf"), - "/files" + os.path.join(temp_dir, config_name, - "apache2/apache2.conf/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), False, True, - "vhost.in.rootconf")] - return vh_truth - if config_name == "debian_apache_2_4/multi_vhosts": - prefix = os.path.join( - temp_dir, config_name, "apache2/sites-available") - aug_pre = "/files" + prefix - vh_truth = [ - obj.VirtualHost( - os.path.join(prefix, "default.conf"), - os.path.join(aug_pre, "default.conf/VirtualHost[1]"), - set([obj.Addr.fromstring("*:80")]), - False, True, "ip-172-30-0-17"), - obj.VirtualHost( - os.path.join(prefix, "default.conf"), - os.path.join(aug_pre, "default.conf/VirtualHost[2]"), - set([obj.Addr.fromstring("*:80")]), - False, True, "banana.vomit.com"), - obj.VirtualHost( - os.path.join(prefix, "multi-vhost.conf"), - os.path.join(aug_pre, "multi-vhost.conf/VirtualHost[1]"), - set([obj.Addr.fromstring("*:80")]), - False, True, "1.multi.vhost.tld"), - obj.VirtualHost( - os.path.join(prefix, "multi-vhost.conf"), - os.path.join(aug_pre, "multi-vhost.conf/IfModule/VirtualHost"), - set([obj.Addr.fromstring("*:80")]), - False, True, "2.multi.vhost.tld"), - obj.VirtualHost( - os.path.join(prefix, "multi-vhost.conf"), - os.path.join(aug_pre, "multi-vhost.conf/VirtualHost[2]"), - set([obj.Addr.fromstring("*:80")]), - False, True, "3.multi.vhost.tld")] - return vh_truth - return None # pragma: no cover diff --git a/certbot-apache/certbot_apache/tls_sni_01.py b/certbot-apache/certbot_apache/tls_sni_01.py deleted file mode 100644 index 65230cdcb3e..00000000000 --- a/certbot-apache/certbot_apache/tls_sni_01.py +++ /dev/null @@ -1,174 +0,0 @@ -"""A class that performs TLS-SNI-01 challenges for Apache""" - -import os -import logging - -from acme.magic_typing import Set # pylint: disable=unused-import, no-name-in-module -from certbot.plugins import common -from certbot.errors import PluginError, MissingCommandlineFlag - -from certbot_apache import obj - -logger = logging.getLogger(__name__) - - -class ApacheTlsSni01(common.TLSSNI01): - """Class that performs TLS-SNI-01 challenges within the Apache configurator - - :ivar configurator: ApacheConfigurator object - :type configurator: :class:`~apache.configurator.ApacheConfigurator` - - :ivar list achalls: Annotated TLS-SNI-01 - (`.KeyAuthorizationAnnotatedChallenge`) challenges. - - :param list indices: Meant to hold indices of challenges in a - larger array. ApacheTlsSni01 is capable of solving many challenges - at once which causes an indexing issue within ApacheConfigurator - who must return all responses in order. Imagine ApacheConfigurator - maintaining state about where all of the http-01 Challenges, - TLS-SNI-01 Challenges belong in the response array. This is an - optional utility. - - :param str challenge_conf: location of the challenge config file - - """ - - VHOST_TEMPLATE = """\ - - ServerName {server_name} - UseCanonicalName on - SSLStrictSNIVHostCheck on - - LimitRequestBody 1048576 - - Include {ssl_options_conf_path} - SSLCertificateFile {cert_path} - SSLCertificateKeyFile {key_path} - - DocumentRoot {document_root} - - -""" - - def __init__(self, *args, **kwargs): - super(ApacheTlsSni01, self).__init__(*args, **kwargs) - - self.challenge_conf = os.path.join( - self.configurator.conf("challenge-location"), - "le_tls_sni_01_cert_challenge.conf") - - def perform(self): - """Perform a TLS-SNI-01 challenge.""" - if not self.achalls: - return [] - # Save any changes to the configuration as a precaution - # About to make temporary changes to the config - self.configurator.save("Changes before challenge setup", True) - - # Prepare the server for HTTPS - self.configurator.prepare_server_https( - str(self.configurator.config.tls_sni_01_port), True) - - responses = [] - - # Create all of the challenge certs - for achall in self.achalls: - responses.append(self._setup_challenge_cert(achall)) - - # Setup the configuration - addrs = self._mod_config() - self.configurator.save("Don't lose mod_config changes", True) - self.configurator.make_addrs_sni_ready(addrs) - - # Save reversible changes - self.configurator.save("SNI Challenge", True) - - return responses - - def _mod_config(self): - """Modifies Apache config files to include challenge vhosts. - - Result: Apache config includes virtual servers for issued challs - - :returns: All TLS-SNI-01 addresses used - :rtype: set - - """ - addrs = set() # type: Set[obj.Addr] - config_text = "\n" - - for achall in self.achalls: - achall_addrs = self._get_addrs(achall) - addrs.update(achall_addrs) - - config_text += self._get_config_text(achall, achall_addrs) - - config_text += "\n" - - self.configurator.parser.add_include( - self.configurator.parser.loc["default"], self.challenge_conf) - self.configurator.reverter.register_file_creation( - True, self.challenge_conf) - - logger.debug("writing a config file with text:\n %s", config_text) - with open(self.challenge_conf, "w") as new_conf: - new_conf.write(config_text) - - return addrs - - def _get_addrs(self, achall): - """Return the Apache addresses needed for TLS-SNI-01.""" - # TODO: Checkout _default_ rules. - addrs = set() - default_addr = obj.Addr(("*", str( - self.configurator.config.tls_sni_01_port))) - - try: - vhost = self.configurator.choose_vhost(achall.domain, - create_if_no_ssl=False) - except (PluginError, MissingCommandlineFlag): - # We couldn't find the virtualhost for this domain, possibly - # because it's a new vhost that's not configured yet - # (GH #677). See also GH #2600. - logger.warning("Falling back to default vhost %s...", default_addr) - addrs.add(default_addr) - return addrs - - for addr in vhost.addrs: - if "_default_" == addr.get_addr(): - addrs.add(default_addr) - else: - addrs.add( - addr.get_sni_addr( - self.configurator.config.tls_sni_01_port)) - - return addrs - - def _get_config_text(self, achall, ip_addrs): - """Chocolate virtual server configuration text - - :param .KeyAuthorizationAnnotatedChallenge achall: Annotated - TLS-SNI-01 challenge. - - :param list ip_addrs: addresses of challenged domain - :class:`list` of type `~.obj.Addr` - - :returns: virtual host configuration text - :rtype: str - - """ - ips = " ".join(str(i) for i in ip_addrs) - document_root = os.path.join( - self.configurator.config.work_dir, "tls_sni_01_page/") - # TODO: Python docs is not clear how multiline string literal - # newlines are parsed on different platforms. At least on - # Linux (Debian sid), when source file uses CRLF, Python still - # parses it as "\n"... c.f.: - # https://docs.python.org/2.7/reference/lexical_analysis.html - return self.VHOST_TEMPLATE.format( - vhost=ips, - server_name=achall.response(achall.account_key).z_domain.decode('ascii'), - ssl_options_conf_path=self.configurator.mod_ssl_conf, - cert_path=self.get_cert_path(achall), - key_path=self.get_key_path(achall), - document_root=document_root).replace("\n", os.linesep) diff --git a/certbot-apache/docs/Makefile b/certbot-apache/docs/Makefile deleted file mode 100644 index 0e611ececbb..00000000000 --- a/certbot-apache/docs/Makefile +++ /dev/null @@ -1,192 +0,0 @@ -# Makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -PAPER = -BUILDDIR = _build - -# User-friendly check for sphinx-build -ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) -$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) -endif - -# Internal variables. -PAPEROPT_a4 = -D latex_paper_size=a4 -PAPEROPT_letter = -D latex_paper_size=letter -ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . -# the i18n builder cannot share the environment and doctrees with the others -I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . - -.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext - -help: - @echo "Please use \`make ' where is one of" - @echo " html to make standalone HTML files" - @echo " dirhtml to make HTML files named index.html in directories" - @echo " singlehtml to make a single large HTML file" - @echo " pickle to make pickle files" - @echo " json to make JSON files" - @echo " htmlhelp to make HTML files and a HTML help project" - @echo " qthelp to make HTML files and a qthelp project" - @echo " applehelp to make an Apple Help Book" - @echo " devhelp to make HTML files and a Devhelp project" - @echo " epub to make an epub" - @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" - @echo " latexpdf to make LaTeX files and run them through pdflatex" - @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" - @echo " text to make text files" - @echo " man to make manual pages" - @echo " texinfo to make Texinfo files" - @echo " info to make Texinfo files and run them through makeinfo" - @echo " gettext to make PO message catalogs" - @echo " changes to make an overview of all changed/added/deprecated items" - @echo " xml to make Docutils-native XML files" - @echo " pseudoxml to make pseudoxml-XML files for display purposes" - @echo " linkcheck to check all external links for integrity" - @echo " doctest to run all doctests embedded in the documentation (if enabled)" - @echo " coverage to run coverage check of the documentation (if enabled)" - -clean: - rm -rf $(BUILDDIR)/* - -html: - $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." - -dirhtml: - $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." - -singlehtml: - $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml - @echo - @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." - -pickle: - $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle - @echo - @echo "Build finished; now you can process the pickle files." - -json: - $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json - @echo - @echo "Build finished; now you can process the JSON files." - -htmlhelp: - $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp - @echo - @echo "Build finished; now you can run HTML Help Workshop with the" \ - ".hhp project file in $(BUILDDIR)/htmlhelp." - -qthelp: - $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp - @echo - @echo "Build finished; now you can run "qcollectiongenerator" with the" \ - ".qhcp project file in $(BUILDDIR)/qthelp, like this:" - @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/certbot-apache.qhcp" - @echo "To view the help file:" - @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/certbot-apache.qhc" - -applehelp: - $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp - @echo - @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." - @echo "N.B. You won't be able to view it unless you put it in" \ - "~/Library/Documentation/Help or install it in your application" \ - "bundle." - -devhelp: - $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp - @echo - @echo "Build finished." - @echo "To view the help file:" - @echo "# mkdir -p $$HOME/.local/share/devhelp/certbot-apache" - @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/certbot-apache" - @echo "# devhelp" - -epub: - $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub - @echo - @echo "Build finished. The epub file is in $(BUILDDIR)/epub." - -latex: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo - @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." - @echo "Run \`make' in that directory to run these through (pdf)latex" \ - "(use \`make latexpdf' here to do that automatically)." - -latexpdf: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through pdflatex..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -latexpdfja: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through platex and dvipdfmx..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -text: - $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text - @echo - @echo "Build finished. The text files are in $(BUILDDIR)/text." - -man: - $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man - @echo - @echo "Build finished. The manual pages are in $(BUILDDIR)/man." - -texinfo: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo - @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." - @echo "Run \`make' in that directory to run these through makeinfo" \ - "(use \`make info' here to do that automatically)." - -info: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo "Running Texinfo files through makeinfo..." - make -C $(BUILDDIR)/texinfo info - @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." - -gettext: - $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale - @echo - @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." - -changes: - $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes - @echo - @echo "The overview file is in $(BUILDDIR)/changes." - -linkcheck: - $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck - @echo - @echo "Link check complete; look for any errors in the above output " \ - "or in $(BUILDDIR)/linkcheck/output.txt." - -doctest: - $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest - @echo "Testing of doctests in the sources finished, look at the " \ - "results in $(BUILDDIR)/doctest/output.txt." - -coverage: - $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage - @echo "Testing of coverage in the sources finished, look at the " \ - "results in $(BUILDDIR)/coverage/python.txt." - -xml: - $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml - @echo - @echo "Build finished. The XML files are in $(BUILDDIR)/xml." - -pseudoxml: - $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml - @echo - @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/certbot-apache/docs/api.rst b/certbot-apache/docs/api.rst deleted file mode 100644 index 8668ec5d8ad..00000000000 --- a/certbot-apache/docs/api.rst +++ /dev/null @@ -1,8 +0,0 @@ -================= -API Documentation -================= - -.. toctree:: - :glob: - - api/** diff --git a/certbot-apache/docs/api/augeas_configurator.rst b/certbot-apache/docs/api/augeas_configurator.rst deleted file mode 100644 index b47ffbc6b62..00000000000 --- a/certbot-apache/docs/api/augeas_configurator.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_apache.augeas_configurator` ---------------------------------------------- - -.. automodule:: certbot_apache.augeas_configurator - :members: diff --git a/certbot-apache/docs/api/configurator.rst b/certbot-apache/docs/api/configurator.rst deleted file mode 100644 index 8ec266d1ae6..00000000000 --- a/certbot-apache/docs/api/configurator.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_apache.configurator` --------------------------------------- - -.. automodule:: certbot_apache.configurator - :members: diff --git a/certbot-apache/docs/api/display_ops.rst b/certbot-apache/docs/api/display_ops.rst deleted file mode 100644 index 26d3ed3dc3f..00000000000 --- a/certbot-apache/docs/api/display_ops.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_apache.display_ops` -------------------------------------- - -.. automodule:: certbot_apache.display_ops - :members: diff --git a/certbot-apache/docs/api/obj.rst b/certbot-apache/docs/api/obj.rst deleted file mode 100644 index 82e58df3ffd..00000000000 --- a/certbot-apache/docs/api/obj.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_apache.obj` ------------------------------ - -.. automodule:: certbot_apache.obj - :members: diff --git a/certbot-apache/docs/api/parser.rst b/certbot-apache/docs/api/parser.rst deleted file mode 100644 index 3427735be1f..00000000000 --- a/certbot-apache/docs/api/parser.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_apache.parser` --------------------------------- - -.. automodule:: certbot_apache.parser - :members: diff --git a/certbot-apache/docs/api/tls_sni_01.rst b/certbot-apache/docs/api/tls_sni_01.rst deleted file mode 100644 index 3ecd0a3655d..00000000000 --- a/certbot-apache/docs/api/tls_sni_01.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_apache.tls_sni_01` ------------------------------------- - -.. automodule:: certbot_apache.tls_sni_01 - :members: diff --git a/certbot-apache/docs/conf.py b/certbot-apache/docs/conf.py deleted file mode 100644 index d2fe15581a3..00000000000 --- a/certbot-apache/docs/conf.py +++ /dev/null @@ -1,318 +0,0 @@ -# -*- coding: utf-8 -*- -# -# certbot-apache documentation build configuration file, created by -# sphinx-quickstart on Sun Oct 18 13:39:26 2015. -# -# This file is execfile()d with the current directory set to its -# containing dir. -# -# Note that not all possible configuration values are present in this -# autogenerated file. -# -# All configuration values have a default; values that are commented out -# serve to show the default. - -import sys -import os -import shlex - -import mock - - -# http://docs.readthedocs.org/en/latest/faq.html#i-get-import-errors-on-libraries-that-depend-on-c-modules -# c.f. #262 -sys.modules.update( - (mod_name, mock.MagicMock()) for mod_name in ['augeas']) - -here = os.path.abspath(os.path.dirname(__file__)) - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath(os.path.join(here, '..'))) - -# -- General configuration ------------------------------------------------ - -# If your documentation needs a minimal Sphinx version, state it here. -needs_sphinx = '1.0' - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.viewcode', -] - -autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# The suffix(es) of source filenames. -# You can specify multiple suffix as a list of string: -# source_suffix = ['.rst', '.md'] -source_suffix = '.rst' - -# The encoding of source files. -#source_encoding = 'utf-8-sig' - -# The master toctree document. -master_doc = 'index' - -# General information about the project. -project = u'certbot-apache' -copyright = u'2014-2015, Let\'s Encrypt Project' -author = u'Certbot Project' - -# The version info for the project you're documenting, acts as replacement for -# |version| and |release|, also used in various other places throughout the -# built documents. -# -# The short X.Y version. -version = '0' -# The full version, including alpha/beta/rc tags. -release = '0' - -# The language for content autogenerated by Sphinx. Refer to documentation -# for a list of supported languages. -# -# This is also used if you do content translation via gettext catalogs. -# Usually you set "language" from the command line for these cases. -language = 'en' - -# There are two options for replacing |today|: either, you set today to some -# non-false value, then it is used: -#today = '' -# Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -exclude_patterns = ['_build'] - -# The reST default role (used for this markup: `text`) to use for all -# documents. -default_role = 'py:obj' - -# If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True - -# If true, the current module name will be prepended to all description -# unit titles (such as .. function::). -#add_module_names = True - -# If true, sectionauthor and moduleauthor directives will be shown in the -# output. They are ignored by default. -#show_authors = False - -# The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' - -# A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] - -# If true, keep warnings as "system message" paragraphs in the built documents. -#keep_warnings = False - -# If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True - - -# -- Options for HTML output ---------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. - -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it - -# Theme options are theme-specific and customize the look and feel of a theme -# further. For a list of options available for each theme, see the -# documentation. -#html_theme_options = {} - -# Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] - -# The name for this set of Sphinx documents. If None, it defaults to -# " v documentation". -#html_title = None - -# A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None - -# The name of an image file (relative to this directory) to place at the top -# of the sidebar. -#html_logo = None - -# The name of an image file (within the static path) to use as favicon of the -# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 -# pixels large. -#html_favicon = None - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] - -# Add any extra paths that contain custom files (such as robots.txt or -# .htaccess) here, relative to this directory. These files are copied -# directly to the root of the documentation. -#html_extra_path = [] - -# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, -# using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' - -# If true, SmartyPants will be used to convert quotes and dashes to -# typographically correct entities. -#html_use_smartypants = True - -# Custom sidebar templates, maps document names to template names. -#html_sidebars = {} - -# Additional templates that should be rendered to pages, maps page names to -# template names. -#html_additional_pages = {} - -# If false, no module index is generated. -#html_domain_indices = True - -# If false, no index is generated. -#html_use_index = True - -# If true, the index is split into individual pages for each letter. -#html_split_index = False - -# If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True - -# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True - -# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True - -# If true, an OpenSearch description file will be output, and all pages will -# contain a tag referring to it. The value of this option must be the -# base URL from which the finished HTML is served. -#html_use_opensearch = '' - -# This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None - -# Language to be used for generating the HTML full-text search index. -# Sphinx supports the following languages: -# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' -# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' -#html_search_language = 'en' - -# A dictionary with options for the search language support, empty by default. -# Now only 'ja' uses this config value -#html_search_options = {'type': 'default'} - -# The name of a javascript file (relative to the configuration directory) that -# implements a search results scorer. If empty, the default will be used. -#html_search_scorer = 'scorer.js' - -# Output file base name for HTML help builder. -htmlhelp_basename = 'certbot-apachedoc' - -# -- Options for LaTeX output --------------------------------------------- - -latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - #'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - #'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - #'preamble': '', - - # Latex figure (float) alignment - #'figure_align': 'htbp', -} - -# Grouping the document tree into LaTeX files. List of tuples -# (source start file, target name, title, -# author, documentclass [howto, manual, or own class]). -latex_documents = [ - (master_doc, 'certbot-apache.tex', u'certbot-apache Documentation', - u'Certbot Project', 'manual'), -] - -# The name of an image file (relative to this directory) to place at the top of -# the title page. -#latex_logo = None - -# For "manual" documents, if this is true, then toplevel headings are parts, -# not chapters. -#latex_use_parts = False - -# If true, show page references after internal links. -#latex_show_pagerefs = False - -# If true, show URL addresses after external links. -#latex_show_urls = False - -# Documents to append as an appendix to all manuals. -#latex_appendices = [] - -# If false, no module index is generated. -#latex_domain_indices = True - - -# -- Options for manual page output --------------------------------------- - -# One entry per manual page. List of tuples -# (source start file, name, description, authors, manual section). -man_pages = [ - (master_doc, 'certbot-apache', u'certbot-apache Documentation', - [author], 1) -] - -# If true, show URL addresses after external links. -#man_show_urls = False - - -# -- Options for Texinfo output ------------------------------------------- - -# Grouping the document tree into Texinfo files. List of tuples -# (source start file, target name, title, author, -# dir menu entry, description, category) -texinfo_documents = [ - (master_doc, 'certbot-apache', u'certbot-apache Documentation', - author, 'certbot-apache', 'One line description of project.', - 'Miscellaneous'), -] - -# Documents to append as an appendix to all manuals. -#texinfo_appendices = [] - -# If false, no module index is generated. -#texinfo_domain_indices = True - -# How to display URL addresses: 'footnote', 'no', or 'inline'. -#texinfo_show_urls = 'footnote' - -# If true, do not generate a @detailmenu in the "Top" node's menu. -#texinfo_no_detailmenu = False - - -intersphinx_mapping = { - 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), -} diff --git a/certbot-apache/docs/index.rst b/certbot-apache/docs/index.rst deleted file mode 100644 index bfe4d245ccb..00000000000 --- a/certbot-apache/docs/index.rst +++ /dev/null @@ -1,31 +0,0 @@ -.. certbot-apache documentation master file, created by - sphinx-quickstart on Sun Oct 18 13:39:26 2015. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -Welcome to certbot-apache's documentation! -============================================== - -Contents: - -.. toctree:: - :maxdepth: 2 - - -.. toctree:: - :maxdepth: 1 - - api - - -.. automodule:: certbot_apache - :members: - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` - diff --git a/certbot-apache/docs/make.bat b/certbot-apache/docs/make.bat deleted file mode 100644 index 3a7818940c5..00000000000 --- a/certbot-apache/docs/make.bat +++ /dev/null @@ -1,263 +0,0 @@ -@ECHO OFF - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set BUILDDIR=_build -set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . -set I18NSPHINXOPTS=%SPHINXOPTS% . -if NOT "%PAPER%" == "" ( - set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% - set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% -) - -if "%1" == "" goto help - -if "%1" == "help" ( - :help - echo.Please use `make ^` where ^ is one of - echo. html to make standalone HTML files - echo. dirhtml to make HTML files named index.html in directories - echo. singlehtml to make a single large HTML file - echo. pickle to make pickle files - echo. json to make JSON files - echo. htmlhelp to make HTML files and a HTML help project - echo. qthelp to make HTML files and a qthelp project - echo. devhelp to make HTML files and a Devhelp project - echo. epub to make an epub - echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter - echo. text to make text files - echo. man to make manual pages - echo. texinfo to make Texinfo files - echo. gettext to make PO message catalogs - echo. changes to make an overview over all changed/added/deprecated items - echo. xml to make Docutils-native XML files - echo. pseudoxml to make pseudoxml-XML files for display purposes - echo. linkcheck to check all external links for integrity - echo. doctest to run all doctests embedded in the documentation if enabled - echo. coverage to run coverage check of the documentation if enabled - goto end -) - -if "%1" == "clean" ( - for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i - del /q /s %BUILDDIR%\* - goto end -) - - -REM Check if sphinx-build is available and fallback to Python version if any -%SPHINXBUILD% 2> nul -if errorlevel 9009 goto sphinx_python -goto sphinx_ok - -:sphinx_python - -set SPHINXBUILD=python -m sphinx.__init__ -%SPHINXBUILD% 2> nul -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ - exit /b 1 -) - -:sphinx_ok - - -if "%1" == "html" ( - %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/html. - goto end -) - -if "%1" == "dirhtml" ( - %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. - goto end -) - -if "%1" == "singlehtml" ( - %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. - goto end -) - -if "%1" == "pickle" ( - %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the pickle files. - goto end -) - -if "%1" == "json" ( - %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the JSON files. - goto end -) - -if "%1" == "htmlhelp" ( - %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run HTML Help Workshop with the ^ -.hhp project file in %BUILDDIR%/htmlhelp. - goto end -) - -if "%1" == "qthelp" ( - %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run "qcollectiongenerator" with the ^ -.qhcp project file in %BUILDDIR%/qthelp, like this: - echo.^> qcollectiongenerator %BUILDDIR%\qthelp\certbot-apache.qhcp - echo.To view the help file: - echo.^> assistant -collectionFile %BUILDDIR%\qthelp\certbot-apache.ghc - goto end -) - -if "%1" == "devhelp" ( - %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. - goto end -) - -if "%1" == "epub" ( - %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The epub file is in %BUILDDIR%/epub. - goto end -) - -if "%1" == "latex" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdf" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdfja" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf-ja - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "text" ( - %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The text files are in %BUILDDIR%/text. - goto end -) - -if "%1" == "man" ( - %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The manual pages are in %BUILDDIR%/man. - goto end -) - -if "%1" == "texinfo" ( - %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. - goto end -) - -if "%1" == "gettext" ( - %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The message catalogs are in %BUILDDIR%/locale. - goto end -) - -if "%1" == "changes" ( - %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes - if errorlevel 1 exit /b 1 - echo. - echo.The overview file is in %BUILDDIR%/changes. - goto end -) - -if "%1" == "linkcheck" ( - %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck - if errorlevel 1 exit /b 1 - echo. - echo.Link check complete; look for any errors in the above output ^ -or in %BUILDDIR%/linkcheck/output.txt. - goto end -) - -if "%1" == "doctest" ( - %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest - if errorlevel 1 exit /b 1 - echo. - echo.Testing of doctests in the sources finished, look at the ^ -results in %BUILDDIR%/doctest/output.txt. - goto end -) - -if "%1" == "coverage" ( - %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage - if errorlevel 1 exit /b 1 - echo. - echo.Testing of coverage in the sources finished, look at the ^ -results in %BUILDDIR%/coverage/python.txt. - goto end -) - -if "%1" == "xml" ( - %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The XML files are in %BUILDDIR%/xml. - goto end -) - -if "%1" == "pseudoxml" ( - %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. - goto end -) - -:end diff --git a/certbot-apache/local-oldest-requirements.txt b/certbot-apache/local-oldest-requirements.txt deleted file mode 100644 index fd8869f7ce8..00000000000 --- a/certbot-apache/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -acme[dev]==0.25.0 -certbot[dev]==0.26.0 diff --git a/certbot-apache/pyproject.toml b/certbot-apache/pyproject.toml new file mode 100644 index 00000000000..1500400d772 --- /dev/null +++ b/certbot-apache/pyproject.toml @@ -0,0 +1,45 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-apache" +dynamic = ["version", "dependencies"] +description = "Apache plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.entry-points."certbot.plugins"] +apache = "certbot_apache._internal.entrypoint:ENTRYPOINT" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-apache/readthedocs.org.requirements.txt b/certbot-apache/readthedocs.org.requirements.txt deleted file mode 100644 index fe30ab1dc93..00000000000 --- a/certbot-apache/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-apache[docs] diff --git a/certbot-apache/setup.cfg b/certbot-apache/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-apache/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-apache/setup.py b/certbot-apache/setup.py index 14d6cacb669..ce89349b768 100644 --- a/certbot-apache/setup.py +++ b/certbot-apache/setup.py @@ -1,67 +1,15 @@ from setuptools import setup -from setuptools import find_packages +version = '5.8.0.dev0' -version = '0.31.0.dev0' - -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.25.0', - 'certbot>=0.26.0', - 'mock', - 'python-augeas', - 'setuptools', - 'zope.component', - 'zope.interface', -] - -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', + # We specify the minimum certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'certbot[apache]>={version}', ] setup( - name='certbot-apache', version=version, - description="Apache plugin for Certbot", - url='https://github.com/letsencrypt/letsencrypt', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 5 - Production/Stable', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'apache = certbot_apache.entrypoint:ENTRYPOINT', - ], - }, - test_suite='certbot_apache', ) diff --git a/certbot-apache/certbot_apache/__init__.py b/certbot-apache/src/certbot_apache/__init__.py similarity index 100% rename from certbot-apache/certbot_apache/__init__.py rename to certbot-apache/src/certbot_apache/__init__.py diff --git a/certbot-apache/src/certbot_apache/_internal/entrypoint.py b/certbot-apache/src/certbot_apache/_internal/entrypoint.py new file mode 100644 index 00000000000..44c08874f18 --- /dev/null +++ b/certbot-apache/src/certbot_apache/_internal/entrypoint.py @@ -0,0 +1,6 @@ +""" Entry point for Apache Plugin """ + +import certbot.plugins.apache + + +ENTRYPOINT = certbot.plugins.apache.ENTRYPOINT diff --git a/certbot-apache/docs/_templates/.gitignore b/certbot-apache/src/certbot_apache/py.typed similarity index 100% rename from certbot-apache/docs/_templates/.gitignore rename to certbot-apache/src/certbot_apache/py.typed diff --git a/certbot-auto b/certbot-auto deleted file mode 100755 index be2c3679b70..00000000000 --- a/certbot-auto +++ /dev/null @@ -1,1702 +0,0 @@ -#!/bin/sh -# -# Download and run the latest release version of the Certbot client. -# -# NOTE: THIS SCRIPT IS AUTO-GENERATED AND SELF-UPDATING -# -# IF YOU WANT TO EDIT IT LOCALLY, *ALWAYS* RUN YOUR COPY WITH THE -# "--no-self-upgrade" FLAG -# -# IF YOU WANT TO SEND PULL REQUESTS, THE REAL SOURCE FOR THIS FILE IS -# letsencrypt-auto-source/letsencrypt-auto.template AND -# letsencrypt-auto-source/pieces/bootstrappers/* - -set -e # Work even if somebody does "sh thisscript.sh". - -# Note: you can set XDG_DATA_HOME or VENV_PATH before running this script, -# if you want to change where the virtual environment will be installed - -# HOME might not be defined when being run through something like systemd -if [ -z "$HOME" ]; then - HOME=~root -fi -if [ -z "$XDG_DATA_HOME" ]; then - XDG_DATA_HOME=~/.local/share -fi -if [ -z "$VENV_PATH" ]; then - # We export these values so they are preserved properly if this script is - # rerun with sudo/su where $HOME/$XDG_DATA_HOME may have a different value. - export OLD_VENV_PATH="$XDG_DATA_HOME/letsencrypt" - export VENV_PATH="/opt/eff.org/certbot/venv" -fi -VENV_BIN="$VENV_PATH/bin" -BOOTSTRAP_VERSION_PATH="$VENV_PATH/certbot-auto-bootstrap-version.txt" -LE_AUTO_VERSION="0.30.0" -BASENAME=$(basename $0) -USAGE="Usage: $BASENAME [OPTIONS] -A self-updating wrapper script for the Certbot ACME client. When run, updates -to both this script and certbot will be downloaded and installed. After -ensuring you have the latest versions installed, certbot will be invoked with -all arguments you have provided. - -Help for certbot itself cannot be provided until it is installed. - - --debug attempt experimental installation - -h, --help print this help - -n, --non-interactive, --noninteractive run without asking for user input - --no-bootstrap do not install OS dependencies - --no-self-upgrade do not download updates - --os-packages-only install OS dependencies and exit - --install-only install certbot, upgrade if needed, and exit - -v, --verbose provide more output - -q, --quiet provide only update/error output; - implies --non-interactive - -All arguments are accepted and forwarded to the Certbot client when run." -export CERTBOT_AUTO="$0" - -for arg in "$@" ; do - case "$arg" in - --debug) - DEBUG=1;; - --os-packages-only) - OS_PACKAGES_ONLY=1;; - --install-only) - INSTALL_ONLY=1;; - --no-self-upgrade) - # Do not upgrade this script (also prevents client upgrades, because each - # copy of the script pins a hash of the python client) - NO_SELF_UPGRADE=1;; - --no-bootstrap) - NO_BOOTSTRAP=1;; - --help) - HELP=1;; - --noninteractive|--non-interactive) - NONINTERACTIVE=1;; - --quiet) - QUIET=1;; - renew) - ASSUME_YES=1;; - --verbose) - VERBOSE=1;; - -[!-]*) - OPTIND=1 - while getopts ":hnvq" short_arg $arg; do - case "$short_arg" in - h) - HELP=1;; - n) - NONINTERACTIVE=1;; - q) - QUIET=1;; - v) - VERBOSE=1;; - esac - done;; - esac -done - -if [ $BASENAME = "letsencrypt-auto" ]; then - # letsencrypt-auto does not respect --help or --yes for backwards compatibility - NONINTERACTIVE=1 - HELP=0 -fi - -# Set ASSUME_YES to 1 if QUIET or NONINTERACTIVE -if [ "$QUIET" = 1 -o "$NONINTERACTIVE" = 1 ]; then - ASSUME_YES=1 -fi - -say() { - if [ "$QUIET" != 1 ]; then - echo "$@" - fi -} - -error() { - echo "$@" -} - -# Support for busybox and others where there is no "command", -# but "which" instead -if command -v command > /dev/null 2>&1 ; then - export EXISTS="command -v" -elif which which > /dev/null 2>&1 ; then - export EXISTS="which" -else - error "Cannot find command nor which... please install one!" - exit 1 -fi - -# Certbot itself needs root access for almost all modes of operation. -# certbot-auto needs root access to bootstrap OS dependencies and install -# Certbot at a protected path so it can be safely run as root. To accomplish -# this, this script will attempt to run itself as root if it doesn't have the -# necessary privileges by using `sudo` or falling back to `su` if it is not -# available. The mechanism used to obtain root access can be set explicitly by -# setting the environment variable LE_AUTO_SUDO to 'sudo', 'su', 'su_sudo', -# 'SuSudo', or '' as used below. - -# Because the parameters in `su -c` has to be a string, -# we need to properly escape it. -SuSudo() { - args="" - # This `while` loop iterates over all parameters given to this function. - # For each parameter, all `'` will be replace by `'"'"'`, and the escaped string - # will be wrapped in a pair of `'`, then appended to `$args` string - # For example, `echo "It's only 1\$\!"` will be escaped to: - # 'echo' 'It'"'"'s only 1$!' - # │ │└┼┘│ - # │ │ │ └── `'s only 1$!'` the literal string - # │ │ └── `\"'\"` is a single quote (as a string) - # │ └── `'It'`, to be concatenated with the strings following it - # └── `echo` wrapped in a pair of `'`, it's totally fine for the shell command itself - while [ $# -ne 0 ]; do - args="$args'$(printf "%s" "$1" | sed -e "s/'/'\"'\"'/g")' " - shift - done - su root -c "$args" -} - -# Sets the environment variable SUDO to be the name of the program or function -# to call to get root access. If this script already has root privleges, SUDO -# is set to an empty string. The value in SUDO should be run with the command -# to called with root privileges as arguments. -SetRootAuthMechanism() { - SUDO="" - if [ -n "${LE_AUTO_SUDO+x}" ]; then - case "$LE_AUTO_SUDO" in - SuSudo|su_sudo|su) - SUDO=SuSudo - ;; - sudo) - SUDO="sudo -E" - ;; - '') ;; # Nothing to do for plain root method. - *) - error "Error: unknown root authorization mechanism '$LE_AUTO_SUDO'." - exit 1 - esac - say "Using preset root authorization mechanism '$LE_AUTO_SUDO'." - else - if test "`id -u`" -ne "0" ; then - if $EXISTS sudo 1>/dev/null 2>&1; then - SUDO="sudo -E" - else - say \"sudo\" is not available, will use \"su\" for installation steps... - SUDO=SuSudo - fi - fi - fi -} - -if [ "$1" = "--cb-auto-has-root" ]; then - shift 1 -else - SetRootAuthMechanism - if [ -n "$SUDO" ]; then - say "Requesting to rerun $0 with root privileges..." - $SUDO "$0" --cb-auto-has-root "$@" - exit 0 - fi -fi - -# Runs this script again with the given arguments. --cb-auto-has-root is added -# to the command line arguments to ensure we don't try to acquire root a -# second time. After the script is rerun, we exit the current script. -RerunWithArgs() { - "$0" --cb-auto-has-root "$@" - exit 0 -} - -BootstrapMessage() { - # Arguments: Platform name - say "Bootstrapping dependencies for $1... (you can skip this with --no-bootstrap)" -} - -ExperimentalBootstrap() { - # Arguments: Platform name, bootstrap function name - if [ "$DEBUG" = 1 ]; then - if [ "$2" != "" ]; then - BootstrapMessage $1 - $2 - fi - else - error "FATAL: $1 support is very experimental at present..." - error "if you would like to work on improving it, please ensure you have backups" - error "and then run this script again with the --debug flag!" - error "Alternatively, you can install OS dependencies yourself and run this script" - error "again with --no-bootstrap." - exit 1 - fi -} - -DeprecationBootstrap() { - # Arguments: Platform name, bootstrap function name - if [ "$DEBUG" = 1 ]; then - if [ "$2" != "" ]; then - BootstrapMessage $1 - $2 - fi - else - error "WARNING: certbot-auto support for this $1 is DEPRECATED!" - error "Please visit certbot.eff.org to learn how to download a version of" - error "Certbot that is packaged for your system. While an existing version" - error "of certbot-auto may work currently, we have stopped supporting updating" - error "system packages for your system. Please switch to a packaged version" - error "as soon as possible." - exit 1 - fi -} - -MIN_PYTHON_VERSION="2.7" -MIN_PYVER=$(echo "$MIN_PYTHON_VERSION" | sed 's/\.//') -# Sets LE_PYTHON to Python version string and PYVER to the first two -# digits of the python version -DeterminePythonVersion() { - # Arguments: "NOCRASH" if we shouldn't crash if we don't find a good python - # - # If no Python is found, PYVER is set to 0. - if [ "$USE_PYTHON_3" = 1 ]; then - for LE_PYTHON in "$LE_PYTHON" python3; do - # Break (while keeping the LE_PYTHON value) if found. - $EXISTS "$LE_PYTHON" > /dev/null && break - done - else - for LE_PYTHON in "$LE_PYTHON" python2.7 python27 python2 python; do - # Break (while keeping the LE_PYTHON value) if found. - $EXISTS "$LE_PYTHON" > /dev/null && break - done - fi - if [ "$?" != "0" ]; then - if [ "$1" != "NOCRASH" ]; then - error "Cannot find any Pythons; please install one!" - exit 1 - else - PYVER=0 - return 0 - fi - fi - - PYVER=`"$LE_PYTHON" -V 2>&1 | cut -d" " -f 2 | cut -d. -f1,2 | sed 's/\.//'` - if [ "$PYVER" -lt "$MIN_PYVER" ]; then - if [ "$1" != "NOCRASH" ]; then - error "You have an ancient version of Python entombed in your operating system..." - error "This isn't going to work; you'll need at least version $MIN_PYTHON_VERSION." - exit 1 - fi - fi -} - -# If new packages are installed by BootstrapDebCommon below, this version -# number must be increased. -BOOTSTRAP_DEB_COMMON_VERSION=1 - -BootstrapDebCommon() { - # Current version tested with: - # - # - Ubuntu - # - 14.04 (x64) - # - 15.04 (x64) - # - Debian - # - 7.9 "wheezy" (x64) - # - sid (2015-10-21) (x64) - - # Past versions tested with: - # - # - Debian 8.0 "jessie" (x64) - # - Raspbian 7.8 (armhf) - - # Believed not to work: - # - # - Debian 6.0.10 "squeeze" (x64) - - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='-qq' - fi - - apt-get $QUIET_FLAG update || error apt-get update hit problems but continuing anyway... - - # virtualenv binary can be found in different packages depending on - # distro version (#346) - - virtualenv= - # virtual env is known to apt and is installable - if apt-cache show virtualenv > /dev/null 2>&1 ; then - if ! LC_ALL=C apt-cache --quiet=0 show virtualenv 2>&1 | grep -q 'No packages found'; then - virtualenv="virtualenv" - fi - fi - - if apt-cache show python-virtualenv > /dev/null 2>&1; then - virtualenv="$virtualenv python-virtualenv" - fi - - augeas_pkg="libaugeas0 augeas-lenses" - AUGVERSION=`LC_ALL=C apt-cache show --no-all-versions libaugeas0 | grep ^Version: | cut -d" " -f2` - - if [ "$ASSUME_YES" = 1 ]; then - YES_FLAG="-y" - fi - - AddBackportRepo() { - # ARGS: - BACKPORT_NAME="$1" - BACKPORT_SOURCELINE="$2" - say "To use the Apache Certbot plugin, augeas needs to be installed from $BACKPORT_NAME." - if ! grep -v -e ' *#' /etc/apt/sources.list | grep -q "$BACKPORT_NAME" ; then - # This can theoretically error if sources.list.d is empty, but in that case we don't care. - if ! grep -v -e ' *#' /etc/apt/sources.list.d/* 2>/dev/null | grep -q "$BACKPORT_NAME"; then - if [ "$ASSUME_YES" = 1 ]; then - /bin/echo -n "Installing augeas from $BACKPORT_NAME in 3 seconds..." - sleep 1s - /bin/echo -ne "\e[0K\rInstalling augeas from $BACKPORT_NAME in 2 seconds..." - sleep 1s - /bin/echo -e "\e[0K\rInstalling augeas from $BACKPORT_NAME in 1 second ..." - sleep 1s - add_backports=1 - else - read -p "Would you like to enable the $BACKPORT_NAME repository [Y/n]? " response - case $response in - [yY][eE][sS]|[yY]|"") - add_backports=1;; - *) - add_backports=0;; - esac - fi - if [ "$add_backports" = 1 ]; then - sh -c "echo $BACKPORT_SOURCELINE >> /etc/apt/sources.list.d/$BACKPORT_NAME.list" - apt-get $QUIET_FLAG update - fi - fi - fi - if [ "$add_backports" != 0 ]; then - apt-get install $QUIET_FLAG $YES_FLAG --no-install-recommends -t "$BACKPORT_NAME" $augeas_pkg - augeas_pkg= - fi - } - - - if dpkg --compare-versions 1.0 gt "$AUGVERSION" ; then - if lsb_release -a | grep -q wheezy ; then - AddBackportRepo wheezy-backports "deb http://http.debian.net/debian wheezy-backports main" - elif lsb_release -a | grep -q precise ; then - # XXX add ARM case - AddBackportRepo precise-backports "deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse" - else - echo "No libaugeas0 version is available that's new enough to run the" - echo "Certbot apache plugin..." - fi - # XXX add a case for ubuntu PPAs - fi - - apt-get install $QUIET_FLAG $YES_FLAG --no-install-recommends \ - python \ - python-dev \ - $virtualenv \ - gcc \ - $augeas_pkg \ - libssl-dev \ - openssl \ - libffi-dev \ - ca-certificates \ - - - if ! $EXISTS virtualenv > /dev/null ; then - error Failed to install a working \"virtualenv\" command, exiting - exit 1 - fi -} - -# If new packages are installed by BootstrapRpmCommonBase below, version -# numbers in rpm_common.sh and rpm_python3.sh must be increased. - -# Sets TOOL to the name of the package manager -# Sets appropriate values for YES_FLAG and QUIET_FLAG based on $ASSUME_YES and $QUIET_FLAG. -# Enables EPEL if applicable and possible. -InitializeRPMCommonBase() { - if type dnf 2>/dev/null - then - TOOL=dnf - elif type yum 2>/dev/null - then - TOOL=yum - - else - error "Neither yum nor dnf found. Aborting bootstrap!" - exit 1 - fi - - if [ "$ASSUME_YES" = 1 ]; then - YES_FLAG="-y" - fi - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='--quiet' - fi - - if ! $TOOL list *virtualenv >/dev/null 2>&1; then - echo "To use Certbot, packages from the EPEL repository need to be installed." - if ! $TOOL list epel-release >/dev/null 2>&1; then - error "Enable the EPEL repository and try running Certbot again." - exit 1 - fi - if [ "$ASSUME_YES" = 1 ]; then - /bin/echo -n "Enabling the EPEL repository in 3 seconds..." - sleep 1s - /bin/echo -ne "\e[0K\rEnabling the EPEL repository in 2 seconds..." - sleep 1s - /bin/echo -e "\e[0K\rEnabling the EPEL repository in 1 second..." - sleep 1s - fi - if ! $TOOL install $YES_FLAG $QUIET_FLAG epel-release; then - error "Could not enable EPEL. Aborting bootstrap!" - exit 1 - fi - fi -} - -BootstrapRpmCommonBase() { - # Arguments: whitespace-delimited python packages to install - - InitializeRPMCommonBase # This call is superfluous in practice - - pkgs=" - gcc - augeas-libs - openssl - openssl-devel - libffi-devel - redhat-rpm-config - ca-certificates - " - - # Add the python packages - pkgs="$pkgs - $1 - " - - if $TOOL list installed "httpd" >/dev/null 2>&1; then - pkgs="$pkgs - mod_ssl - " - fi - - if ! $TOOL install $YES_FLAG $QUIET_FLAG $pkgs; then - error "Could not install OS dependencies. Aborting bootstrap!" - exit 1 - fi -} - -# If new packages are installed by BootstrapRpmCommon below, this version -# number must be increased. -BOOTSTRAP_RPM_COMMON_VERSION=1 - -BootstrapRpmCommon() { - # Tested with: - # - Fedora 20, 21, 22, 23 (x64) - # - Centos 7 (x64: on DigitalOcean droplet) - # - CentOS 7 Minimal install in a Hyper-V VM - # - CentOS 6 - - InitializeRPMCommonBase - - # Most RPM distros use the "python" or "python-" naming convention. Let's try that first. - if $TOOL list python >/dev/null 2>&1; then - python_pkgs="$python - python-devel - python-virtualenv - python-tools - python-pip - " - # Fedora 26 starts to use the prefix python2 for python2 based packages. - # this elseif is theoretically for any Fedora over version 26: - elif $TOOL list python2 >/dev/null 2>&1; then - python_pkgs="$python2 - python2-libs - python2-setuptools - python2-devel - python2-virtualenv - python2-tools - python2-pip - " - # Some distros and older versions of current distros use a "python27" - # instead of the "python" or "python-" naming convention. - else - python_pkgs="$python27 - python27-devel - python27-virtualenv - python27-tools - python27-pip - " - fi - - BootstrapRpmCommonBase "$python_pkgs" -} - -# If new packages are installed by BootstrapRpmPython3 below, this version -# number must be increased. -BOOTSTRAP_RPM_PYTHON3_VERSION=1 - -BootstrapRpmPython3() { - # Tested with: - # - CentOS 6 - - InitializeRPMCommonBase - - # EPEL uses python34 - if $TOOL list python34 >/dev/null 2>&1; then - python_pkgs="python34 - python34-devel - python34-tools - " - else - error "No supported Python package available to install. Aborting bootstrap!" - exit 1 - fi - - BootstrapRpmCommonBase "$python_pkgs" -} - -# If new packages are installed by BootstrapSuseCommon below, this version -# number must be increased. -BOOTSTRAP_SUSE_COMMON_VERSION=1 - -BootstrapSuseCommon() { - # SLE12 don't have python-virtualenv - - if [ "$ASSUME_YES" = 1 ]; then - zypper_flags="-nq" - install_flags="-l" - fi - - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='-qq' - fi - - zypper $QUIET_FLAG $zypper_flags in $install_flags \ - python \ - python-devel \ - python-virtualenv \ - gcc \ - augeas-lenses \ - libopenssl-devel \ - libffi-devel \ - ca-certificates -} - -# If new packages are installed by BootstrapArchCommon below, this version -# number must be increased. -BOOTSTRAP_ARCH_COMMON_VERSION=1 - -BootstrapArchCommon() { - # Tested with: - # - ArchLinux (x86_64) - # - # "python-virtualenv" is Python3, but "python2-virtualenv" provides - # only "virtualenv2" binary, not "virtualenv". - - deps=" - python2 - python-virtualenv - gcc - augeas - openssl - libffi - ca-certificates - pkg-config - " - - # pacman -T exits with 127 if there are missing dependencies - missing=$(pacman -T $deps) || true - - if [ "$ASSUME_YES" = 1 ]; then - noconfirm="--noconfirm" - fi - - if [ "$missing" ]; then - if [ "$QUIET" = 1 ]; then - pacman -S --needed $missing $noconfirm > /dev/null - else - pacman -S --needed $missing $noconfirm - fi - fi -} - -# If new packages are installed by BootstrapGentooCommon below, this version -# number must be increased. -BOOTSTRAP_GENTOO_COMMON_VERSION=1 - -BootstrapGentooCommon() { - PACKAGES=" - dev-lang/python:2.7 - dev-python/virtualenv - app-admin/augeas - dev-libs/openssl - dev-libs/libffi - app-misc/ca-certificates - virtual/pkgconfig" - - ASK_OPTION="--ask" - if [ "$ASSUME_YES" = 1 ]; then - ASK_OPTION="" - fi - - case "$PACKAGE_MANAGER" in - (paludis) - cave resolve --preserve-world --keep-targets if-possible $PACKAGES -x - ;; - (pkgcore) - pmerge --noreplace --oneshot $ASK_OPTION $PACKAGES - ;; - (portage|*) - emerge --noreplace --oneshot $ASK_OPTION $PACKAGES - ;; - esac -} - -# If new packages are installed by BootstrapFreeBsd below, this version number -# must be increased. -BOOTSTRAP_FREEBSD_VERSION=1 - -BootstrapFreeBsd() { - if [ "$QUIET" = 1 ]; then - QUIET_FLAG="--quiet" - fi - - pkg install -Ay $QUIET_FLAG \ - python \ - py27-virtualenv \ - augeas \ - libffi -} - -# If new packages are installed by BootstrapMac below, this version number must -# be increased. -BOOTSTRAP_MAC_VERSION=1 - -BootstrapMac() { - if hash brew 2>/dev/null; then - say "Using Homebrew to install dependencies..." - pkgman=brew - pkgcmd="brew install" - elif hash port 2>/dev/null; then - say "Using MacPorts to install dependencies..." - pkgman=port - pkgcmd="port install" - else - say "No Homebrew/MacPorts; installing Homebrew..." - ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - pkgman=brew - pkgcmd="brew install" - fi - - $pkgcmd augeas - if [ "$(which python)" = "/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python" \ - -o "$(which python)" = "/usr/bin/python" ]; then - # We want to avoid using the system Python because it requires root to use pip. - # python.org, MacPorts or HomeBrew Python installations should all be OK. - say "Installing python..." - $pkgcmd python - fi - - # Workaround for _dlopen not finding augeas on macOS - if [ "$pkgman" = "port" ] && ! [ -e "/usr/local/lib/libaugeas.dylib" ] && [ -e "/opt/local/lib/libaugeas.dylib" ]; then - say "Applying augeas workaround" - mkdir -p /usr/local/lib/ - ln -s /opt/local/lib/libaugeas.dylib /usr/local/lib/ - fi - - if ! hash pip 2>/dev/null; then - say "pip not installed" - say "Installing pip..." - curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | python - fi - - if ! hash virtualenv 2>/dev/null; then - say "virtualenv not installed." - say "Installing with pip..." - pip install virtualenv - fi -} - -# If new packages are installed by BootstrapSmartOS below, this version number -# must be increased. -BOOTSTRAP_SMARTOS_VERSION=1 - -BootstrapSmartOS() { - pkgin update - pkgin -y install 'gcc49' 'py27-augeas' 'py27-virtualenv' -} - -# If new packages are installed by BootstrapMageiaCommon below, this version -# number must be increased. -BOOTSTRAP_MAGEIA_COMMON_VERSION=1 - -BootstrapMageiaCommon() { - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='--quiet' - fi - - if ! urpmi --force $QUIET_FLAG \ - python \ - libpython-devel \ - python-virtualenv - then - error "Could not install Python dependencies. Aborting bootstrap!" - exit 1 - fi - - if ! urpmi --force $QUIET_FLAG \ - git \ - gcc \ - python-augeas \ - libopenssl-devel \ - libffi-devel \ - rootcerts - then - error "Could not install additional dependencies. Aborting bootstrap!" - exit 1 - fi -} - - -# Set Bootstrap to the function that installs OS dependencies on this system -# and BOOTSTRAP_VERSION to the unique identifier for the current version of -# that function. If Bootstrap is set to a function that doesn't install any -# packages BOOTSTRAP_VERSION is not set. -if [ -f /etc/debian_version ]; then - Bootstrap() { - BootstrapMessage "Debian-based OSes" - BootstrapDebCommon - } - BOOTSTRAP_VERSION="BootstrapDebCommon $BOOTSTRAP_DEB_COMMON_VERSION" -elif [ -f /etc/mageia-release ]; then - # Mageia has both /etc/mageia-release and /etc/redhat-release - Bootstrap() { - ExperimentalBootstrap "Mageia" BootstrapMageiaCommon - } - BOOTSTRAP_VERSION="BootstrapMageiaCommon $BOOTSTRAP_MAGEIA_COMMON_VERSION" -elif [ -f /etc/redhat-release ]; then - # Run DeterminePythonVersion to decide on the basis of available Python versions - # whether to use 2.x or 3.x on RedHat-like systems. - # Then, revert LE_PYTHON to its previous state. - prev_le_python="$LE_PYTHON" - unset LE_PYTHON - DeterminePythonVersion "NOCRASH" - if [ "$PYVER" -eq 26 ]; then - Bootstrap() { - BootstrapMessage "RedHat-based OSes that will use Python3" - BootstrapRpmPython3 - } - USE_PYTHON_3=1 - BOOTSTRAP_VERSION="BootstrapRpmPython3 $BOOTSTRAP_RPM_PYTHON3_VERSION" - else - Bootstrap() { - BootstrapMessage "RedHat-based OSes" - BootstrapRpmCommon - } - BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION" - fi - LE_PYTHON="$prev_le_python" -elif [ -f /etc/os-release ] && `grep -q openSUSE /etc/os-release` ; then - Bootstrap() { - BootstrapMessage "openSUSE-based OSes" - BootstrapSuseCommon - } - BOOTSTRAP_VERSION="BootstrapSuseCommon $BOOTSTRAP_SUSE_COMMON_VERSION" -elif [ -f /etc/arch-release ]; then - Bootstrap() { - if [ "$DEBUG" = 1 ]; then - BootstrapMessage "Archlinux" - BootstrapArchCommon - else - error "Please use pacman to install letsencrypt packages:" - error "# pacman -S certbot certbot-apache" - error - error "If you would like to use the virtualenv way, please run the script again with the" - error "--debug flag." - exit 1 - fi - } - BOOTSTRAP_VERSION="BootstrapArchCommon $BOOTSTRAP_ARCH_COMMON_VERSION" -elif [ -f /etc/manjaro-release ]; then - Bootstrap() { - ExperimentalBootstrap "Manjaro Linux" BootstrapArchCommon - } - BOOTSTRAP_VERSION="BootstrapArchCommon $BOOTSTRAP_ARCH_COMMON_VERSION" -elif [ -f /etc/gentoo-release ]; then - Bootstrap() { - DeprecationBootstrap "Gentoo" BootstrapGentooCommon - } - BOOTSTRAP_VERSION="BootstrapGentooCommon $BOOTSTRAP_GENTOO_COMMON_VERSION" -elif uname | grep -iq FreeBSD ; then - Bootstrap() { - DeprecationBootstrap "FreeBSD" BootstrapFreeBsd - } - BOOTSTRAP_VERSION="BootstrapFreeBsd $BOOTSTRAP_FREEBSD_VERSION" -elif uname | grep -iq Darwin ; then - Bootstrap() { - DeprecationBootstrap "macOS" BootstrapMac - } - BOOTSTRAP_VERSION="BootstrapMac $BOOTSTRAP_MAC_VERSION" -elif [ -f /etc/issue ] && grep -iq "Amazon Linux" /etc/issue ; then - Bootstrap() { - ExperimentalBootstrap "Amazon Linux" BootstrapRpmCommon - } - BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION" -elif [ -f /etc/product ] && grep -q "Joyent Instance" /etc/product ; then - Bootstrap() { - ExperimentalBootstrap "Joyent SmartOS Zone" BootstrapSmartOS - } - BOOTSTRAP_VERSION="BootstrapSmartOS $BOOTSTRAP_SMARTOS_VERSION" -else - Bootstrap() { - error "Sorry, I don't know how to bootstrap Certbot on your operating system!" - error - error "You will need to install OS dependencies, configure virtualenv, and run pip install manually." - error "Please see https://letsencrypt.readthedocs.org/en/latest/contributing.html#prerequisites" - error "for more info." - exit 1 - } -fi - -# We handle this case after determining the normal bootstrap version to allow -# variables like USE_PYTHON_3 to be properly set. As described above, if the -# Bootstrap function doesn't install any packages, BOOTSTRAP_VERSION should not -# be set so we unset it here. -if [ "$NO_BOOTSTRAP" = 1 ]; then - Bootstrap() { - : - } - unset BOOTSTRAP_VERSION -fi - -# Sets PREV_BOOTSTRAP_VERSION to the identifier for the bootstrap script used -# to install OS dependencies on this system. PREV_BOOTSTRAP_VERSION isn't set -# if it is unknown how OS dependencies were installed on this system. -SetPrevBootstrapVersion() { - if [ -f $BOOTSTRAP_VERSION_PATH ]; then - PREV_BOOTSTRAP_VERSION=$(cat "$BOOTSTRAP_VERSION_PATH") - # The list below only contains bootstrap version strings that existed before - # we started writing them to disk. - # - # DO NOT MODIFY THIS LIST UNLESS YOU KNOW WHAT YOU'RE DOING! - elif grep -Fqx "$BOOTSTRAP_VERSION" << "UNLIKELY_EOF" -BootstrapDebCommon 1 -BootstrapMageiaCommon 1 -BootstrapRpmCommon 1 -BootstrapSuseCommon 1 -BootstrapArchCommon 1 -BootstrapGentooCommon 1 -BootstrapFreeBsd 1 -BootstrapMac 1 -BootstrapSmartOS 1 -UNLIKELY_EOF - then - # If there's no bootstrap version saved to disk, but the currently selected - # bootstrap script is from before we started saving the version number, - # return the currently selected version to prevent us from rebootstrapping - # unnecessarily. - PREV_BOOTSTRAP_VERSION="$BOOTSTRAP_VERSION" - fi -} - -TempDir() { - mktemp -d 2>/dev/null || mktemp -d -t 'le' # Linux || macOS -} - -# Returns 0 if a letsencrypt installation exists at $OLD_VENV_PATH, otherwise, -# returns a non-zero number. -OldVenvExists() { - [ -n "$OLD_VENV_PATH" -a -f "$OLD_VENV_PATH/bin/letsencrypt" ] -} - -# Given python path, version 1 and version 2, check if version 1 is outdated compared to version 2. -# An unofficial version provided as version 1 (eg. 0.28.0.dev0) will be treated -# specifically by printing "UNOFFICIAL". Otherwise, print "OUTDATED" if version 1 -# is outdated, and "UP_TO_DATE" if not. -# This function relies only on installed python environment (2.x or 3.x) by certbot-auto. -CompareVersions() { - "$1" - "$2" "$3" << "UNLIKELY_EOF" -import sys -from distutils.version import StrictVersion - -try: - current = StrictVersion(sys.argv[1]) -except ValueError: - sys.stdout.write('UNOFFICIAL') - sys.exit() - -try: - remote = StrictVersion(sys.argv[2]) -except ValueError: - sys.stdout.write('UP_TO_DATE') - sys.exit() - -if current < remote: - sys.stdout.write('OUTDATED') -else: - sys.stdout.write('UP_TO_DATE') -UNLIKELY_EOF -} - -if [ "$1" = "--le-auto-phase2" ]; then - # Phase 2: Create venv, install LE, and run. - - shift 1 # the --le-auto-phase2 arg - SetPrevBootstrapVersion - - if [ -z "$PHASE_1_VERSION" -a "$USE_PYTHON_3" = 1 ]; then - unset LE_PYTHON - fi - - INSTALLED_VERSION="none" - if [ -d "$VENV_PATH" ] || OldVenvExists; then - # If the selected Bootstrap function isn't a noop and it differs from the - # previously used version - if [ -n "$BOOTSTRAP_VERSION" -a "$BOOTSTRAP_VERSION" != "$PREV_BOOTSTRAP_VERSION" ]; then - # if non-interactive mode or stdin and stdout are connected to a terminal - if [ \( "$NONINTERACTIVE" = 1 \) -o \( \( -t 0 \) -a \( -t 1 \) \) ]; then - if [ -d "$VENV_PATH" ]; then - rm -rf "$VENV_PATH" - fi - # In the case the old venv was just a symlink to the new one, - # OldVenvExists is now false because we deleted the venv at VENV_PATH. - if OldVenvExists; then - rm -rf "$OLD_VENV_PATH" - ln -s "$VENV_PATH" "$OLD_VENV_PATH" - fi - RerunWithArgs "$@" - else - error "Skipping upgrade because new OS dependencies may need to be installed." - error - error "To upgrade to a newer version, please run this script again manually so you can" - error "approve changes or with --non-interactive on the command line to automatically" - error "install any required packages." - # Set INSTALLED_VERSION to be the same so we don't update the venv - INSTALLED_VERSION="$LE_AUTO_VERSION" - # Continue to use OLD_VENV_PATH if the new venv doesn't exist - if [ ! -d "$VENV_PATH" ]; then - VENV_BIN="$OLD_VENV_PATH/bin" - fi - fi - elif [ -f "$VENV_BIN/letsencrypt" ]; then - # --version output ran through grep due to python-cryptography DeprecationWarnings - # grep for both certbot and letsencrypt until certbot and shim packages have been released - INSTALLED_VERSION=$("$VENV_BIN/letsencrypt" --version 2>&1 | grep "^certbot\|^letsencrypt" | cut -d " " -f 2) - if [ -z "$INSTALLED_VERSION" ]; then - error "Error: couldn't get currently installed version for $VENV_BIN/letsencrypt: " 1>&2 - "$VENV_BIN/letsencrypt" --version - exit 1 - fi - fi - fi - - if [ "$LE_AUTO_VERSION" != "$INSTALLED_VERSION" ]; then - say "Creating virtual environment..." - DeterminePythonVersion - rm -rf "$VENV_PATH" - if [ "$PYVER" -le 27 ]; then - if [ "$VERBOSE" = 1 ]; then - virtualenv --no-site-packages --python "$LE_PYTHON" "$VENV_PATH" - else - virtualenv --no-site-packages --python "$LE_PYTHON" "$VENV_PATH" > /dev/null - fi - else - if [ "$VERBOSE" = 1 ]; then - "$LE_PYTHON" -m venv "$VENV_PATH" - else - "$LE_PYTHON" -m venv "$VENV_PATH" > /dev/null - fi - fi - - if [ -n "$BOOTSTRAP_VERSION" ]; then - echo "$BOOTSTRAP_VERSION" > "$BOOTSTRAP_VERSION_PATH" - elif [ -n "$PREV_BOOTSTRAP_VERSION" ]; then - echo "$PREV_BOOTSTRAP_VERSION" > "$BOOTSTRAP_VERSION_PATH" - fi - - say "Installing Python packages..." - TEMP_DIR=$(TempDir) - trap 'rm -rf "$TEMP_DIR"' EXIT - # There is no $ interpolation due to quotes on starting heredoc delimiter. - # ------------------------------------------------------------------------- - cat << "UNLIKELY_EOF" > "$TEMP_DIR/letsencrypt-auto-requirements.txt" -# This is the flattened list of packages certbot-auto installs. To generate -# this, do -# `pip install --no-cache-dir -e acme -e . -e certbot-apache -e certbot-nginx`, -# and then use `hashin` or a more secure method to gather the hashes. - -# Hashin example: -# pip install hashin -# hashin -r dependency-requirements.txt cryptography==1.5.2 -# sets the new certbot-auto pinned version of cryptography to 1.5.2 - -argparse==1.4.0 \ - --hash=sha256:c31647edb69fd3d465a847ea3157d37bed1f95f19760b11a47aa91c04b666314 \ - --hash=sha256:62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4 - -# This comes before cffi because cffi will otherwise install an unchecked -# version via setup_requires. -pycparser==2.14 \ - --hash=sha256:7959b4a74abdc27b312fed1c21e6caf9309ce0b29ea86b591fd2e99ecdf27f73 \ - --no-binary pycparser - -asn1crypto==0.22.0 \ - --hash=sha256:d232509fefcfcdb9a331f37e9c9dc20441019ad927c7d2176cf18ed5da0ba097 \ - --hash=sha256:cbbadd640d3165ab24b06ef25d1dca09a3441611ac15f6a6b452474fdf0aed1a -cffi==1.11.5 \ - --hash=sha256:1b0493c091a1898f1136e3f4f991a784437fac3673780ff9de3bcf46c80b6b50 \ - --hash=sha256:87f37fe5130574ff76c17cab61e7d2538a16f843bb7bca8ebbc4b12de3078596 \ - --hash=sha256:1553d1e99f035ace1c0544050622b7bc963374a00c467edafac50ad7bd276aef \ - --hash=sha256:151b7eefd035c56b2b2e1eb9963c90c6302dc15fbd8c1c0a83a163ff2c7d7743 \ - --hash=sha256:edabd457cd23a02965166026fd9bfd196f4324fe6032e866d0f3bd0301cd486f \ - --hash=sha256:ba5e697569f84b13640c9e193170e89c13c6244c24400fc57e88724ef610cd31 \ - --hash=sha256:79f9b6f7c46ae1f8ded75f68cf8ad50e5729ed4d590c74840471fc2823457d04 \ - --hash=sha256:b0f7d4a3df8f06cf49f9f121bead236e328074de6449866515cea4907bbc63d6 \ - --hash=sha256:4c91af6e967c2015729d3e69c2e51d92f9898c330d6a851bf8f121236f3defd3 \ - --hash=sha256:7a33145e04d44ce95bcd71e522b478d282ad0eafaf34fe1ec5bbd73e662f22b6 \ - --hash=sha256:95d5251e4b5ca00061f9d9f3d6fe537247e145a8524ae9fd30a2f8fbce993b5b \ - --hash=sha256:b75110fb114fa366b29a027d0c9be3709579602ae111ff61674d28c93606acca \ - --hash=sha256:ae5e35a2c189d397b91034642cb0eab0e346f776ec2eb44a49a459e6615d6e2e \ - --hash=sha256:fdf1c1dc5bafc32bc5d08b054f94d659422b05aba244d6be4ddc1c72d9aa70fb \ - --hash=sha256:9d1d3e63a4afdc29bd76ce6aa9d58c771cd1599fbba8cf5057e7860b203710dd \ - --hash=sha256:be2a9b390f77fd7676d80bc3cdc4f8edb940d8c198ed2d8c0be1319018c778e1 \ - --hash=sha256:ed01918d545a38998bfa5902c7c00e0fee90e957ce036a4000a88e3fe2264917 \ - --hash=sha256:857959354ae3a6fa3da6651b966d13b0a8bed6bbc87a0de7b38a549db1d2a359 \ - --hash=sha256:2ba8a45822b7aee805ab49abfe7eec16b90587f7f26df20c71dd89e45a97076f \ - --hash=sha256:a36c5c154f9d42ec176e6e620cb0dd275744aa1d804786a71ac37dc3661a5e95 \ - --hash=sha256:e55e22ac0a30023426564b1059b035973ec82186ddddbac867078435801c7801 \ - --hash=sha256:3eb6434197633b7748cea30bf0ba9f66727cdce45117a712b29a443943733257 \ - --hash=sha256:ecbb7b01409e9b782df5ded849c178a0aa7c906cf8c5a67368047daab282b184 \ - --hash=sha256:770f3782b31f50b68627e22f91cb182c48c47c02eb405fd689472aa7b7aa16dc \ - --hash=sha256:d5d8555d9bfc3f02385c1c37e9f998e2011f0db4f90e250e5bc0c0a85a813085 \ - --hash=sha256:3c85641778460581c42924384f5e68076d724ceac0f267d66c757f7535069c93 \ - --hash=sha256:ca1bd81f40adc59011f58159e4aa6445fc585a32bb8ac9badf7a2c1aa23822f2 \ - --hash=sha256:3bb6bd7266598f318063e584378b8e27c67de998a43362e8fce664c54ee52d30 \ - --hash=sha256:a6a5cb8809091ec9ac03edde9304b3ad82ad4466333432b16d78ef40e0cce0d5 \ - --hash=sha256:57b2533356cb2d8fac1555815929f7f5f14d68ac77b085d2326b571310f34f6e \ - --hash=sha256:495c5c2d43bf6cebe0178eb3e88f9c4aa48d8934aa6e3cddb865c058da76756b \ - --hash=sha256:e90f17980e6ab0f3c2f3730e56d1fe9bcba1891eeea58966e89d352492cc74f4 -ConfigArgParse==0.12.0 \ - --hash=sha256:28cd7d67669651f2a4518367838c49539457504584a139709b2b8f6c208ef339 \ - --no-binary ConfigArgParse -configobj==5.0.6 \ - --hash=sha256:a2f5650770e1c87fb335af19a9b7eb73fc05ccf22144eb68db7d00cd2bcb0902 \ - --no-binary configobj -cryptography==2.2.2 \ - --hash=sha256:3f3b65d5a16e6b52fba63dc860b62ca9832f51f1a2ae5083c78b6840275f12dd \ - --hash=sha256:5251e7de0de66810833606439ca65c9b9e45da62196b0c88bfadf27740aac09f \ - --hash=sha256:551a3abfe0c8c6833df4192a63371aa2ff43afd8f570ed345d31f251d78e7e04 \ - --hash=sha256:5cb990056b7cadcca26813311187ad751ea644712022a3976443691168781b6f \ - --hash=sha256:60bda7f12ecb828358be53095fc9c6edda7de8f1ef571f96c00b2363643fa3cd \ - --hash=sha256:64b5c67acc9a7c83fbb4b69166f3105a0ab722d27934fac2cb26456718eec2ba \ - --hash=sha256:6fef51ec447fe9f8351894024e94736862900d3a9aa2961528e602eb65c92bdb \ - --hash=sha256:77d0ad229d47a6e0272d00f6bf8ac06ce14715a9fd02c9a97f5a2869aab3ccb2 \ - --hash=sha256:808fe471b1a6b777f026f7dc7bd9a4959da4bfab64972f2bbe91e22527c1c037 \ - --hash=sha256:9b62fb4d18529c84b961efd9187fecbb48e89aa1a0f9f4161c61b7fc42a101bd \ - --hash=sha256:9e5bed45ec6b4f828866ac6a6bedf08388ffcfa68abe9e94b34bb40977aba531 \ - --hash=sha256:9fc295bf69130a342e7a19a39d7bbeb15c0bcaabc7382ec33ef3b2b7d18d2f63 \ - --hash=sha256:abd070b5849ed64e6d349199bef955ee0ad99aefbad792f0c587f8effa681a5e \ - --hash=sha256:ba6a774749b6e510cffc2fb98535f717e0e5fd91c7c99a61d223293df79ab351 \ - --hash=sha256:c332118647f084c983c6a3e1dba0f3bcb051f69d12baccac68db8d62d177eb8a \ - --hash=sha256:d6f46e862ee36df81e6342c2177ba84e70f722d9dc9c6c394f9f1f434c4a5563 \ - --hash=sha256:db6013746f73bf8edd9c3d1d3f94db635b9422f503db3fc5ef105233d4c011ab \ - --hash=sha256:f57008eaff597c69cf692c3518f6d4800f0309253bb138b526a37fe9ef0c7471 \ - --hash=sha256:f6c821ac253c19f2ad4c8691633ae1d1a17f120d5b01ea1d256d7b602bc59887 -enum34==1.1.2 ; python_version < '3.4' \ - --hash=sha256:2475d7fcddf5951e92ff546972758802de5260bf409319a9f1934e6bbc8b1dc7 \ - --hash=sha256:35907defb0f992b75ab7788f65fedc1cf20ffa22688e0e6f6f12afc06b3ea501 -funcsigs==1.0.2 \ - --hash=sha256:330cc27ccbf7f1e992e69fef78261dc7c6569012cf397db8d3de0234e6c937ca \ - --hash=sha256:a7bb0f2cf3a3fd1ab2732cb49eba4252c2af4240442415b4abce3b87022a8f50 -idna==2.5 \ - --hash=sha256:cc19709fd6d0cbfed39ea875d29ba6d4e22c0cebc510a76d6302a28385e8bb70 \ - --hash=sha256:3cb5ce08046c4e3a560fc02f138d0ac63e00f8ce5901a56b32ec8b7994082aab -ipaddress==1.0.16 \ - --hash=sha256:935712800ce4760701d89ad677666cd52691fd2f6f0b340c8b4239a3c17988a5 \ - --hash=sha256:5a3182b322a706525c46282ca6f064d27a02cffbd449f9f47416f1dc96aa71b0 -josepy==1.1.0 \ - --hash=sha256:1309a25aac3caeff5239729c58ff9b583f7d022ffdb1553406ddfc8e5b52b76e \ - --hash=sha256:fb5c62c77d26e04df29cb5ecd01b9ce69b6fcc9e521eb1ca193b7faa2afa7086 -linecache2==1.0.0 \ - --hash=sha256:e78be9c0a0dfcbac712fe04fbf92b96cddae80b1b842f24248214c8496f006ef \ - --hash=sha256:4b26ff4e7110db76eeb6f5a7b64a82623839d595c2038eeda662f2a2db78e97c -# Using an older version of mock here prevents regressions of #5276. -mock==1.3.0 \ - --hash=sha256:3f573a18be94de886d1191f27c168427ef693e8dcfcecf95b170577b2eb69cbb \ - --hash=sha256:1e247dbecc6ce057299eb7ee019ad68314bb93152e81d9a6110d35f4d5eca0f6 -ordereddict==1.1 \ - --hash=sha256:1c35b4ac206cef2d24816c89f89cf289dd3d38cf7c449bb3fab7bf6d43f01b1f \ - --no-binary ordereddict -packaging==16.8 \ - --hash=sha256:99276dc6e3a7851f32027a68f1095cd3f77c148091b092ea867a351811cfe388 \ - --hash=sha256:5d50835fdf0a7edf0b55e311b7c887786504efea1177abd7e69329a8e5ea619e -parsedatetime==2.1 \ - --hash=sha256:ce9d422165cf6e963905cd5f74f274ebf7cc98c941916169178ef93f0e557838 \ - --hash=sha256:17c578775520c99131634e09cfca5a05ea9e1bd2a05cd06967ebece10df7af2d -pbr==1.8.1 \ - --hash=sha256:46c8db75ae75a056bd1cc07fa21734fe2e603d11a07833ecc1eeb74c35c72e0c \ - --hash=sha256:e2127626a91e6c885db89668976db31020f0af2da728924b56480fc7ccf09649 -pyOpenSSL==16.2.0 \ - --hash=sha256:26ca380ddf272f7556e48064bbcd5bd71f83dfc144f3583501c7ddbd9434ee17 \ - --hash=sha256:7779a3bbb74e79db234af6a08775568c6769b5821faecf6e2f4143edb227516e -pyparsing==2.1.8 \ - --hash=sha256:2f0f5ceb14eccd5aef809d6382e87df22ca1da583c79f6db01675ce7d7f49c18 \ - --hash=sha256:03a4869b9f3493807ee1f1cb405e6d576a1a2ca4d81a982677c0c1ad6177c56b \ - --hash=sha256:ab09aee814c0241ff0c503cff30018219fe1fc14501d89f406f4664a0ec9fbcd \ - --hash=sha256:6e9a7f052f8e26bcf749e4033e3115b6dc7e3c85aafcb794b9a88c9d9ef13c97 \ - --hash=sha256:9f463a6bcc4eeb6c08f1ed84439b17818e2085937c0dee0d7674ac127c67c12b \ - --hash=sha256:3626b4d81cfb300dad57f52f2f791caaf7b06c09b368c0aa7b868e53a5775424 \ - --hash=sha256:367b90cc877b46af56d4580cd0ae278062903f02b8204ab631f5a2c0f50adfd0 \ - --hash=sha256:9f1ea360086cd68681e7f4ca8f1f38df47bf81942a0d76a9673c2d23eff35b13 -pyRFC3339==1.0 \ - --hash=sha256:eea31835c56e2096af4363a5745a784878a61d043e247d3a6d6a0a32a9741f56 \ - --hash=sha256:8dfbc6c458b8daba1c0f3620a8c78008b323a268b27b7359e92a4ae41325f535 -python-augeas==0.5.0 \ - --hash=sha256:67d59d66cdba8d624e0389b87b2a83a176f21f16a87553b50f5703b23f29bac2 \ - --no-binary python-augeas -pytz==2015.7 \ - --hash=sha256:3abe6a6d3fc2fbbe4c60144211f45da2edbe3182a6f6511af6bbba0598b1f992 \ - --hash=sha256:939ef9c1e1224d980405689a97ffcf7828c56d1517b31d73464356c1f2b7769e \ - --hash=sha256:ead4aefa7007249e05e51b01095719d5a8dd95760089f5730aac5698b1932918 \ - --hash=sha256:3cca0df08bd0ed98432390494ce3ded003f5e661aa460be7a734bffe35983605 \ - --hash=sha256:3ede470d3d17ba3c07638dfa0d10452bc1b6e5ad326127a65ba77e6aaeb11bec \ - --hash=sha256:68c47964f7186eec306b13629627722b9079cd4447ed9e5ecaecd4eac84ca734 \ - --hash=sha256:dd5d3991950aae40a6c81de1578942e73d629808cefc51d12cd157980e6cfc18 \ - --hash=sha256:a77c52062c07eb7c7b30545dbc73e32995b7e117eea750317b5cb5c7a4618f14 \ - --hash=sha256:81af9aec4bc960a9a0127c488f18772dae4634689233f06f65443e7b11ebeb51 \ - --hash=sha256:e079b1dadc5c06246cc1bb6fe1b23a50b1d1173f2edd5104efd40bb73a28f406 \ - --hash=sha256:fbd26746772c24cb93c8b97cbdad5cb9e46c86bbdb1b9d8a743ee00e2fb1fc5d \ - --hash=sha256:99266ef30a37e43932deec2b7ca73e83c8dbc3b9ff703ec73eca6b1dae6befea \ - --hash=sha256:8b6ce1c993909783bc96e0b4f34ea223bff7a4df2c90bdb9c4e0f1ac928689e3 -requests==2.20.0 \ - --hash=sha256:99dcfdaaeb17caf6e526f32b6a7b780461512ab3f1d992187801694cba42770c \ - --hash=sha256:a84b8c9ab6239b578f22d1c21d51b696dcfe004032bb80ea832398d6909d7279 -six==1.10.0 \ - --hash=sha256:0ff78c403d9bccf5a425a6d31a12aa6b47f1c21ca4dc2573a7e2f32a97335eb1 \ - --hash=sha256:105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a -traceback2==1.4.0 \ - --hash=sha256:8253cebec4b19094d67cc5ed5af99bf1dba1285292226e98a31929f87a5d6b23 \ - --hash=sha256:05acc67a09980c2ecfedd3423f7ae0104839eccb55fc645773e1caa0951c3030 -unittest2==1.1.0 \ - --hash=sha256:13f77d0875db6d9b435e1d4f41e74ad4cc2eb6e1d5c824996092b3430f088bb8 \ - --hash=sha256:22882a0e418c284e1f718a822b3b022944d53d2d908e1690b319a9d3eb2c0579 -zope.component==4.2.2 \ - --hash=sha256:282c112b55dd8e3c869a3571f86767c150ab1284a9ace2bdec226c592acaf81a \ - --no-binary zope.component -zope.event==4.1.0 \ - --hash=sha256:dc7a59a2fd91730d3793131a5d261b29e93ec4e2a97f1bc487ce8defee2fe786 \ - --no-binary zope.event -zope.interface==4.1.3 \ - --hash=sha256:f07b631f7a601cd8cbd3332d54f43142c7088a83299f859356f08d1d4d4259b3 \ - --hash=sha256:de5cca083b9439d8002fb76bbe6b4998c5a5a721fab25b84298967f002df4c94 \ - --hash=sha256:6788416f7ea7f5b8a97be94825377aa25e8bdc73463e07baaf9858b29e737077 \ - --hash=sha256:6f3230f7254518201e5a3708cbb2de98c848304f06e3ded8bfb39e5825cba2e1 \ - --hash=sha256:5fa575a5240f04200c3088427d0d4b7b737f6e9018818a51d8d0f927a6a2517a \ - --hash=sha256:522194ad6a545735edd75c8a83f48d65d1af064e432a7d320d64f56bafc12e99 \ - --hash=sha256:e8c7b2d40943f71c99148c97f66caa7f5134147f57423f8db5b4825099ce9a09 \ - --hash=sha256:279024f0208601c3caa907c53876e37ad88625f7eaf1cb3842dbe360b2287017 \ - --hash=sha256:2e221a9eec7ccc58889a278ea13dcfed5ef939d80b07819a9a8b3cb1c681484f \ - --hash=sha256:69118965410ec86d44dc6b9017ee3ddbd582e0c0abeef62b3a19dbf6c8ad132b \ - --hash=sha256:d04df8686ec864d0cade8cf199f7f83aecd416109a20834d568f8310ded12dea \ - --hash=sha256:e75a947e15ee97e7e71e02ea302feb2fc62d3a2bb4668bf9dfbed43a506ac7e7 \ - --hash=sha256:4e45d22fb883222a5ab9f282a116fec5ee2e8d1a568ccff6a2d75bbd0eb6bcfc \ - --hash=sha256:bce9339bb3c7a55e0803b63d21c5839e8e479bc85c4adf42ae415b72f94facb2 \ - --hash=sha256:928138365245a0e8869a5999fbcc2a45475a0a6ed52a494d60dbdc540335fedd \ - --hash=sha256:0d841ba1bb840eea0e6489dc5ecafa6125554971f53b5acb87764441e61bceba \ - --hash=sha256:b09c8c1d47b3531c400e0195697f1414a63221de6ef478598a4f1460f7d9a392 -requests-toolbelt==0.8.0 \ - --hash=sha256:42c9c170abc2cacb78b8ab23ac957945c7716249206f90874651971a4acff237 \ - --hash=sha256:f6a531936c6fa4c6cfce1b9c10d5c4f498d16528d2a54a22ca00011205a187b5 -chardet==3.0.2 \ - --hash=sha256:4f7832e7c583348a9eddd927ee8514b3bf717c061f57b21dbe7697211454d9bb \ - --hash=sha256:6ebf56457934fdce01fb5ada5582762a84eed94cad43ed877964aebbdd8174c0 -urllib3==1.24.1 \ - --hash=sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39 \ - --hash=sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22 -certifi==2017.4.17 \ - --hash=sha256:f4318671072f030a33c7ca6acaef720ddd50ff124d1388e50c1bda4cbd6d7010 \ - --hash=sha256:f7527ebf7461582ce95f7a9e03dd141ce810d40590834f4ec20cddd54234c10a - -# Contains the requirements for the letsencrypt package. -# -# Since the letsencrypt package depends on certbot and using pip with hashes -# requires that all installed packages have hashes listed, this allows -# dependency-requirements.txt to be used without requiring a hash for a -# (potentially unreleased) Certbot package. - -letsencrypt==0.7.0 \ - --hash=sha256:105a5fb107e45bcd0722eb89696986dcf5f08a86a321d6aef25a0c7c63375ade \ - --hash=sha256:c36e532c486a7e92155ee09da54b436a3c420813ec1c590b98f635d924720de9 - -certbot==0.30.0 \ - --hash=sha256:b3468e128e74d2295598f6d3fbf9d0edfb67fe5abaca3b985a9e858395bd027f \ - --hash=sha256:d631fe6c75700ce9b2fdae194ff8b53c7518545d87dd451a1704f7572dcd49e8 -acme==0.30.0 \ - --hash=sha256:eed9389f802ebf4988c9e43c28ad3d5c2734237371d78e97450a1d61189a15aa \ - --hash=sha256:984b6d00bec73dcfa616636a760e80ca14bd246fb908710a656547f542f09445 -certbot-apache==0.30.0 \ - --hash=sha256:d38c70fc6930db298ea992a3145362eebdce460d3d2651f86a8f2f43d838c6d0 \ - --hash=sha256:1d4bc207d53a3e5d37e5d9ebd05f26089aa21d1fbf384113ed9d1829b4d1e9bf -certbot-nginx==0.30.0 \ - --hash=sha256:6163c7d0080f59b4ebe510afcc6af2d2eebf15469275c3835866690db4d465d6 \ - --hash=sha256:e39a3f3d77cd4c653949cf066fb2211039fd2032665697c27b6e8501c7c2dd92 - -UNLIKELY_EOF - # ------------------------------------------------------------------------- - cat << "UNLIKELY_EOF" > "$TEMP_DIR/pipstrap.py" -#!/usr/bin/env python -"""A small script that can act as a trust root for installing pip >=8 -Embed this in your project, and your VCS checkout is all you have to trust. In -a post-peep era, this lets you claw your way to a hash-checking version of pip, -with which you can install the rest of your dependencies safely. All it assumes -is Python 2.6 or better and *some* version of pip already installed. If -anything goes wrong, it will exit with a non-zero status code. -""" -# This is here so embedded copies are MIT-compliant: -# Copyright (c) 2016 Erik Rose -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -from __future__ import print_function -from distutils.version import StrictVersion -from hashlib import sha256 -from os import environ -from os.path import join -from pipes import quote -from shutil import rmtree -try: - from subprocess import check_output -except ImportError: - from subprocess import CalledProcessError, PIPE, Popen - - def check_output(*popenargs, **kwargs): - if 'stdout' in kwargs: - raise ValueError('stdout argument not allowed, it will be ' - 'overridden.') - process = Popen(stdout=PIPE, *popenargs, **kwargs) - output, unused_err = process.communicate() - retcode = process.poll() - if retcode: - cmd = kwargs.get("args") - if cmd is None: - cmd = popenargs[0] - raise CalledProcessError(retcode, cmd) - return output -from sys import exit, version_info -from tempfile import mkdtemp -try: - from urllib2 import build_opener, HTTPHandler, HTTPSHandler -except ImportError: - from urllib.request import build_opener, HTTPHandler, HTTPSHandler -try: - from urlparse import urlparse -except ImportError: - from urllib.parse import urlparse # 3.4 - - -__version__ = 1, 5, 1 -PIP_VERSION = '9.0.1' -DEFAULT_INDEX_BASE = 'https://pypi.python.org' - - -# wheel has a conditional dependency on argparse: -maybe_argparse = ( - [('18/dd/e617cfc3f6210ae183374cd9f6a26b20514bbb5a792af97949c5aacddf0f/' - 'argparse-1.4.0.tar.gz', - '62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4')] - if version_info < (2, 7, 0) else []) - - -PACKAGES = maybe_argparse + [ - # Pip has no dependencies, as it vendors everything: - ('11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/' - 'pip-{0}.tar.gz'.format(PIP_VERSION), - '09f243e1a7b461f654c26a725fa373211bb7ff17a9300058b205c61658ca940d'), - # This version of setuptools has only optional dependencies: - ('59/88/2f3990916931a5de6fa9706d6d75eb32ee8b78627bb2abaab7ed9e6d0622/' - 'setuptools-29.0.1.tar.gz', - 'b539118819a4857378398891fa5366e090690e46b3e41421a1e07d6e9fd8feb0'), - ('c9/1d/bd19e691fd4cfe908c76c429fe6e4436c9e83583c4414b54f6c85471954a/' - 'wheel-0.29.0.tar.gz', - '1ebb8ad7e26b448e9caa4773d2357849bf80ff9e313964bcaf79cbf0201a1648') -] - - -class HashError(Exception): - def __str__(self): - url, path, actual, expected = self.args - return ('{url} did not match the expected hash {expected}. Instead, ' - 'it was {actual}. The file (left at {path}) may have been ' - 'tampered with.'.format(**locals())) - - -def hashed_download(url, temp, digest): - """Download ``url`` to ``temp``, make sure it has the SHA-256 ``digest``, - and return its path.""" - # Based on pip 1.4.1's URLOpener but with cert verification removed. Python - # >=2.7.9 verifies HTTPS certs itself, and, in any case, the cert - # authenticity has only privacy (not arbitrary code execution) - # implications, since we're checking hashes. - def opener(using_https=True): - opener = build_opener(HTTPSHandler()) - if using_https: - # Strip out HTTPHandler to prevent MITM spoof: - for handler in opener.handlers: - if isinstance(handler, HTTPHandler): - opener.handlers.remove(handler) - return opener - - def read_chunks(response, chunk_size): - while True: - chunk = response.read(chunk_size) - if not chunk: - break - yield chunk - - parsed_url = urlparse(url) - response = opener(using_https=parsed_url.scheme == 'https').open(url) - path = join(temp, parsed_url.path.split('/')[-1]) - actual_hash = sha256() - with open(path, 'wb') as file: - for chunk in read_chunks(response, 4096): - file.write(chunk) - actual_hash.update(chunk) - - actual_digest = actual_hash.hexdigest() - if actual_digest != digest: - raise HashError(url, path, actual_digest, digest) - return path - - -def get_index_base(): - """Return the URL to the dir containing the "packages" folder. - Try to wring something out of PIP_INDEX_URL, if set. Hack "/simple" off the - end if it's there; that is likely to give us the right dir. - """ - env_var = environ.get('PIP_INDEX_URL', '').rstrip('/') - if env_var: - SIMPLE = '/simple' - if env_var.endswith(SIMPLE): - return env_var[:-len(SIMPLE)] - else: - return env_var - else: - return DEFAULT_INDEX_BASE - - -def main(): - pip_version = StrictVersion(check_output(['pip', '--version']) - .decode('utf-8').split()[1]) - min_pip_version = StrictVersion(PIP_VERSION) - if pip_version >= min_pip_version: - return 0 - has_pip_cache = pip_version >= StrictVersion('6.0') - index_base = get_index_base() - temp = mkdtemp(prefix='pipstrap-') - try: - downloads = [hashed_download(index_base + '/packages/' + path, - temp, - digest) - for path, digest in PACKAGES] - check_output('pip install --no-index --no-deps -U ' + - # Disable cache since we're not using it and it otherwise - # sometimes throws permission warnings: - ('--no-cache-dir ' if has_pip_cache else '') + - ' '.join(quote(d) for d in downloads), - shell=True) - except HashError as exc: - print(exc) - except Exception: - rmtree(temp) - raise - else: - rmtree(temp) - return 0 - return 1 - - -if __name__ == '__main__': - exit(main()) - -UNLIKELY_EOF - # ------------------------------------------------------------------------- - # Set PATH so pipstrap upgrades the right (v)env: - PATH="$VENV_BIN:$PATH" "$VENV_BIN/python" "$TEMP_DIR/pipstrap.py" - set +e - if [ "$VERBOSE" = 1 ]; then - "$VENV_BIN/pip" install --disable-pip-version-check --no-cache-dir --require-hashes -r "$TEMP_DIR/letsencrypt-auto-requirements.txt" - else - PIP_OUT=`"$VENV_BIN/pip" install --disable-pip-version-check --no-cache-dir --require-hashes -r "$TEMP_DIR/letsencrypt-auto-requirements.txt" 2>&1` - fi - PIP_STATUS=$? - set -e - if [ "$PIP_STATUS" != 0 ]; then - # Report error. (Otherwise, be quiet.) - error "Had a problem while installing Python packages." - if [ "$VERBOSE" != 1 ]; then - error - error "pip prints the following errors: " - error "=====================================================" - error "$PIP_OUT" - error "=====================================================" - error - error "Certbot has problem setting up the virtual environment." - - if `echo $PIP_OUT | grep -q Killed` || `echo $PIP_OUT | grep -q "allocate memory"` ; then - error - error "Based on your pip output, the problem can likely be fixed by " - error "increasing the available memory." - else - error - error "We were not be able to guess the right solution from your pip " - error "output." - fi - - error - error "Consult https://certbot.eff.org/docs/install.html#problems-with-python-virtual-environment" - error "for possible solutions." - error "You may also find some support resources at https://certbot.eff.org/support/ ." - fi - rm -rf "$VENV_PATH" - exit 1 - fi - - if [ -d "$OLD_VENV_PATH" -a ! -L "$OLD_VENV_PATH" ]; then - rm -rf "$OLD_VENV_PATH" - ln -s "$VENV_PATH" "$OLD_VENV_PATH" - fi - - say "Installation succeeded." - fi - - if [ "$INSTALL_ONLY" = 1 ]; then - say "Certbot is installed." - exit 0 - fi - - "$VENV_BIN/letsencrypt" "$@" - -else - # Phase 1: Upgrade certbot-auto if necessary, then self-invoke. - # - # Each phase checks the version of only the thing it is responsible for - # upgrading. Phase 1 checks the version of the latest release of - # certbot-auto (which is always the same as that of the certbot - # package). Phase 2 checks the version of the locally installed certbot. - export PHASE_1_VERSION="$LE_AUTO_VERSION" - - if [ ! -f "$VENV_BIN/letsencrypt" ]; then - if ! OldVenvExists; then - if [ "$HELP" = 1 ]; then - echo "$USAGE" - exit 0 - fi - # If it looks like we've never bootstrapped before, bootstrap: - Bootstrap - fi - fi - if [ "$OS_PACKAGES_ONLY" = 1 ]; then - say "OS packages installed." - exit 0 - fi - - if [ "$NO_SELF_UPGRADE" != 1 ]; then - TEMP_DIR=$(TempDir) - trap 'rm -rf "$TEMP_DIR"' EXIT - # --------------------------------------------------------------------------- - cat << "UNLIKELY_EOF" > "$TEMP_DIR/fetch.py" -"""Do downloading and JSON parsing without additional dependencies. :: - - # Print latest released version of LE to stdout: - python fetch.py --latest-version - - # Download letsencrypt-auto script from git tag v1.2.3 into the folder I'm - # in, and make sure its signature verifies: - python fetch.py --le-auto-script v1.2.3 - -On failure, return non-zero. - -""" - -from __future__ import print_function, unicode_literals - -from distutils.version import LooseVersion -from json import loads -from os import devnull, environ -from os.path import dirname, join -import re -import ssl -from subprocess import check_call, CalledProcessError -from sys import argv, exit -try: - from urllib2 import build_opener, HTTPHandler, HTTPSHandler - from urllib2 import HTTPError, URLError -except ImportError: - from urllib.request import build_opener, HTTPHandler, HTTPSHandler - from urllib.error import HTTPError, URLError - -PUBLIC_KEY = environ.get('LE_AUTO_PUBLIC_KEY', """-----BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6MR8W/galdxnpGqBsYbq -OzQb2eyW15YFjDDEMI0ZOzt8f504obNs920lDnpPD2/KqgsfjOgw2K7xWDJIj/18 -xUvWPk3LDkrnokNiRkA3KOx3W6fHycKL+zID7zy+xZYBuh2fLyQtWV1VGQ45iNRp -9+Zo7rH86cdfgkdnWTlNSHyTLW9NbXvyv/E12bppPcEvgCTAQXgnDVJ0/sqmeiij -n9tTFh03aM+R2V/21h8aTraAS24qiPCz6gkmYGC8yr6mglcnNoYbsLNYZ69zF1XH -cXPduCPdPdfLlzVlKK1/U7hkA28eG3BIAMh6uJYBRJTpiGgaGdPd7YekUB8S6cy+ -CQIDAQAB ------END PUBLIC KEY----- -""") - -class ExpectedError(Exception): - """A novice-readable exception that also carries the original exception for - debugging""" - - -class HttpsGetter(object): - def __init__(self): - """Build an HTTPS opener.""" - # Based on pip 1.4.1's URLOpener - # This verifies certs on only Python >=2.7.9, and when NO_CERT_VERIFY isn't set. - if environ.get('NO_CERT_VERIFY') == '1' and hasattr(ssl, 'SSLContext'): - self._opener = build_opener(HTTPSHandler(context=cert_none_context())) - else: - self._opener = build_opener(HTTPSHandler()) - # Strip out HTTPHandler to prevent MITM spoof: - for handler in self._opener.handlers: - if isinstance(handler, HTTPHandler): - self._opener.handlers.remove(handler) - - def get(self, url): - """Return the document contents pointed to by an HTTPS URL. - - If something goes wrong (404, timeout, etc.), raise ExpectedError. - - """ - try: - # socket module docs say default timeout is None: that is, no - # timeout - return self._opener.open(url, timeout=30).read() - except (HTTPError, IOError) as exc: - raise ExpectedError("Couldn't download %s." % url, exc) - - -def write(contents, dir, filename): - """Write something to a file in a certain directory.""" - with open(join(dir, filename), 'wb') as file: - file.write(contents) - - -def latest_stable_version(get): - """Return the latest stable release of letsencrypt.""" - metadata = loads(get( - environ.get('LE_AUTO_JSON_URL', - 'https://pypi.python.org/pypi/certbot/json')).decode('UTF-8')) - # metadata['info']['version'] actually returns the latest of any kind of - # release release, contrary to https://wiki.python.org/moin/PyPIJSON. - # The regex is a sufficient regex for picking out prereleases for most - # packages, LE included. - return str(max(LooseVersion(r) for r - in metadata['releases'].keys() - if re.match('^[0-9.]+$', r))) - - -def verified_new_le_auto(get, tag, temp_dir): - """Return the path to a verified, up-to-date letsencrypt-auto script. - - If the download's signature does not verify or something else goes wrong - with the verification process, raise ExpectedError. - - """ - le_auto_dir = environ.get( - 'LE_AUTO_DIR_TEMPLATE', - 'https://raw.githubusercontent.com/certbot/certbot/%s/' - 'letsencrypt-auto-source/') % tag - write(get(le_auto_dir + 'letsencrypt-auto'), temp_dir, 'letsencrypt-auto') - write(get(le_auto_dir + 'letsencrypt-auto.sig'), temp_dir, 'letsencrypt-auto.sig') - write(PUBLIC_KEY.encode('UTF-8'), temp_dir, 'public_key.pem') - try: - with open(devnull, 'w') as dev_null: - check_call(['openssl', 'dgst', '-sha256', '-verify', - join(temp_dir, 'public_key.pem'), - '-signature', - join(temp_dir, 'letsencrypt-auto.sig'), - join(temp_dir, 'letsencrypt-auto')], - stdout=dev_null, - stderr=dev_null) - except CalledProcessError as exc: - raise ExpectedError("Couldn't verify signature of downloaded " - "certbot-auto.", exc) - - -def cert_none_context(): - """Create a SSLContext object to not check hostname.""" - # PROTOCOL_TLS isn't available before 2.7.13 but this code is for 2.7.9+, so use this. - context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) - context.verify_mode = ssl.CERT_NONE - return context - - -def main(): - get = HttpsGetter().get - flag = argv[1] - try: - if flag == '--latest-version': - print(latest_stable_version(get)) - elif flag == '--le-auto-script': - tag = argv[2] - verified_new_le_auto(get, tag, dirname(argv[0])) - except ExpectedError as exc: - print(exc.args[0], exc.args[1]) - return 1 - else: - return 0 - - -if __name__ == '__main__': - exit(main()) - -UNLIKELY_EOF - # --------------------------------------------------------------------------- - DeterminePythonVersion "NOCRASH" - if [ "$PYVER" -lt "$MIN_PYVER" ]; then - error "WARNING: couldn't find Python $MIN_PYTHON_VERSION+ to check for updates." - elif ! REMOTE_VERSION=`"$LE_PYTHON" "$TEMP_DIR/fetch.py" --latest-version` ; then - error "WARNING: unable to check for updates." - fi - - LE_VERSION_STATE=`CompareVersions "$LE_PYTHON" "$LE_AUTO_VERSION" "$REMOTE_VERSION"` - if [ "$LE_VERSION_STATE" = "UNOFFICIAL" ]; then - say "Unofficial certbot-auto version detected, self-upgrade is disabled: $LE_AUTO_VERSION" - elif [ "$LE_VERSION_STATE" = "OUTDATED" ]; then - say "Upgrading certbot-auto $LE_AUTO_VERSION to $REMOTE_VERSION..." - - # Now we drop into Python so we don't have to install even more - # dependencies (curl, etc.), for better flow control, and for the option of - # future Windows compatibility. - "$LE_PYTHON" "$TEMP_DIR/fetch.py" --le-auto-script "v$REMOTE_VERSION" - - # Install new copy of certbot-auto. - # TODO: Deal with quotes in pathnames. - say "Replacing certbot-auto..." - # Clone permissions with cp. chmod and chown don't have a --reference - # option on macOS or BSD, and stat -c on Linux is stat -f on macOS and BSD: - cp -p "$0" "$TEMP_DIR/letsencrypt-auto.permission-clone" - cp "$TEMP_DIR/letsencrypt-auto" "$TEMP_DIR/letsencrypt-auto.permission-clone" - # Using mv rather than cp leaves the old file descriptor pointing to the - # original copy so the shell can continue to read it unmolested. mv across - # filesystems is non-atomic, doing `rm dest, cp src dest, rm src`, but the - # cp is unlikely to fail if the rm doesn't. - mv -f "$TEMP_DIR/letsencrypt-auto.permission-clone" "$0" - fi # A newer version is available. - fi # Self-upgrading is allowed. - - RerunWithArgs --le-auto-phase2 "$@" -fi diff --git a/certbot-ci/MANIFEST.in b/certbot-ci/MANIFEST.in new file mode 100644 index 00000000000..55e2107b049 --- /dev/null +++ b/certbot-ci/MANIFEST.in @@ -0,0 +1,3 @@ +recursive-include src/certbot_integration_tests/assets * +include src/certbot_integration_tests/py.typed +include src/snap_integration_tests/py.typed diff --git a/certbot-ci/pyproject.toml b/certbot-ci/pyproject.toml new file mode 100644 index 00000000000..acae26fc681 --- /dev/null +++ b/certbot-ci/pyproject.toml @@ -0,0 +1,57 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-ci" +dynamic = ["version"] +description = "Certbot continuous integration framework" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", +] +dependencies = [ + "coverage", + "cryptography", + "pytest", + "pytest-cov", + # This version is needed for "worker" attributes we currently use like + # "workerinput". See https://github.com/pytest-dev/pytest-xdist/pull/268. + "pytest-xdist>=1.22.1", + "python-dateutil>=2.9.0", # https://github.com/dateutil/dateutil/issues/1314 + # This dependency needs to be added using environment markers to avoid its + # installation on Linux. + "pywin32>=300 ; sys_platform == \"win32\"", + "pyyaml", + # requests unvendored its dependencies in version 2.16.0 and this code relies on that for + # calling `urllib3.disable_warnings`. + "requests>=2.25.1", + "types-python-dateutil", +] + +[project.scripts] +certbot_test = "certbot_integration_tests.utils.certbot_call:main" +run_acme_server = "certbot_integration_tests.utils.acme_server:main" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-ci/setup.py b/certbot-ci/setup.py new file mode 100644 index 00000000000..11fc858b9b0 --- /dev/null +++ b/certbot-ci/setup.py @@ -0,0 +1,7 @@ +from setuptools import setup + +version = '5.8.0.dev0' + +setup( + version=version, +) diff --git a/certbot-ci/src/certbot_integration_tests/.coveragerc b/certbot-ci/src/certbot_integration_tests/.coveragerc new file mode 100644 index 00000000000..90de5023e93 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/.coveragerc @@ -0,0 +1,11 @@ +[run] +# Avoid false warnings because certbot packages are not installed in the thread that executes +# the coverage: indeed, certbot is launched as a CLI from a subprocess. +disable_warnings = module-not-imported,no-data-collected +omit = **/*_test.py,**/tests/*,**/dns_common*,**/certbot/_internal/plugins/nginx/parser_obj.py +patch = subprocess +parallel = True + +[report] +# Exclude unit tests in coverage during integration tests. +omit = **/*_test.py,**/tests/*,**/dns_common*,**/certbot/_internal/plugins/nginx/parser_obj.py diff --git a/certbot-ci/src/certbot_integration_tests/__init__.py b/certbot-ci/src/certbot_integration_tests/__init__.py new file mode 100644 index 00000000000..434a85a23dc --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/__init__.py @@ -0,0 +1 @@ +"""Package certbot_integration_test is for tests that require a live acme ca server instance""" diff --git a/certbot-ci/src/certbot_integration_tests/assets/bind-config/conf/named.conf b/certbot-ci/src/certbot_integration_tests/assets/bind-config/conf/named.conf new file mode 100644 index 00000000000..672a447d337 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/bind-config/conf/named.conf @@ -0,0 +1,60 @@ +options { + directory "/var/cache/bind"; + + // Running inside Docker. Bind address on Docker host is 127.0.0.1. + listen-on { any; }; + listen-on-v6 { any; }; + + // We are allowing BIND to service recursive queries, but only in an extremely limimited sense + // where it is entirely disconnected from public DNS: + // - Iterative queries are disabled. Only forwarding to a non-existent forwarder. + // - The only recursive answers we can get (that will not be a SERVFAIL) will come from the + // RPZ "mock-recursion" zone. Effectively this means we are mocking out the entirety of + // public DNS. + allow-recursion { any; }; // BIND will only answer using RPZ if recursion is enabled + forwarders { 192.0.2.254; }; // Nobody is listening, this is TEST-NET-1 + forward only; // Do NOT perform iterative queries from the root zone + dnssec-validation no; // Do not bother fetching the root DNSKEY set (performance) + response-policy { // All recursive queries will be served from here. + zone "mock-recursion" + log yes; + } recursive-only no // Allow RPZs to affect authoritative zones too. + qname-wait-recurse no // No real recursion. + nsip-wait-recurse no; // No real recursion. + + allow-transfer { none; }; + allow-update { none; }; +}; + +key "default-key." { + algorithm hmac-sha512; + secret "91CgOwzihr0nAVEHKFXJPQCbuBBbBI19Ks5VAweUXgbF40NWTD83naeg3c5y2MPdEiFRXnRLJxL6M+AfHCGLNw=="; +}; + +zone "mock-recursion" { + type primary; + file "/var/lib/bind/rpz.mock-recursion"; + allow-query { + none; + }; +}; + +zone "example.com." { + type primary; + file "/var/lib/bind/db.example.com"; + journal "/var/cache/bind/db.example.com.jnl"; + + update-policy { + grant default-key zonesub TXT; + }; +}; + +zone "sub.example.com." { + type primary; + file "/var/lib/bind/db.sub.example.com"; + journal "/var/cache/bind/db.sub.example.com.jnl"; + + update-policy { + grant default-key zonesub TXT; + }; +}; diff --git a/certbot-ci/src/certbot_integration_tests/assets/bind-config/rfc2136-credentials-default.ini.tpl b/certbot-ci/src/certbot_integration_tests/assets/bind-config/rfc2136-credentials-default.ini.tpl new file mode 100644 index 00000000000..8aa7cc3cbdd --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/bind-config/rfc2136-credentials-default.ini.tpl @@ -0,0 +1,10 @@ +# Target DNS server +dns_rfc2136_server = {server_address} +# Target DNS port +dns_rfc2136_port = {server_port} +# TSIG key name +dns_rfc2136_name = default-key. +# TSIG key secret +dns_rfc2136_secret = 91CgOwzihr0nAVEHKFXJPQCbuBBbBI19Ks5VAweUXgbF40NWTD83naeg3c5y2MPdEiFRXnRLJxL6M+AfHCGLNw== +# TSIG key algorithm +dns_rfc2136_algorithm = HMAC-SHA512 diff --git a/certbot-ci/src/certbot_integration_tests/assets/bind-config/zones/db.example.com b/certbot-ci/src/certbot_integration_tests/assets/bind-config/zones/db.example.com new file mode 100644 index 00000000000..2573470b56e --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/bind-config/zones/db.example.com @@ -0,0 +1,11 @@ +$ORIGIN example.com. +$TTL 3600 +example.com. IN SOA ns1.example.com. admin.example.com. ( 2020091025 7200 3600 1209600 3600 ) + +example.com. IN NS ns1 +example.com. IN NS ns2 + +ns1 IN A 192.0.2.2 +ns2 IN A 192.0.2.3 + +@ IN A 192.0.2.1 diff --git a/certbot-ci/src/certbot_integration_tests/assets/bind-config/zones/db.sub.example.com b/certbot-ci/src/certbot_integration_tests/assets/bind-config/zones/db.sub.example.com new file mode 100644 index 00000000000..0379003b79e --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/bind-config/zones/db.sub.example.com @@ -0,0 +1,9 @@ +$ORIGIN sub.example.com. +$TTL 3600 +sub.example.com. IN SOA ns1.example.com. admin.example.com. ( 2020091025 7200 3600 1209600 3600 ) + +sub.example.com. IN NS ns1 +sub.example.com. IN NS ns2 + +ns1 IN A 192.0.2.2 +ns2 IN A 192.0.2.3 diff --git a/certbot-ci/src/certbot_integration_tests/assets/bind-config/zones/rpz.mock-recursion b/certbot-ci/src/certbot_integration_tests/assets/bind-config/zones/rpz.mock-recursion new file mode 100644 index 00000000000..589689d37da --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/bind-config/zones/rpz.mock-recursion @@ -0,0 +1,6 @@ +$TTL 3600 + +@ SOA ns1.example.test. dummy.example.test. 1 12h 15m 3w 2h + NS ns1.example.test. + +_acme-challenge.aliased.example IN CNAME _acme-challenge.example.com. diff --git a/certbot-ci/src/certbot_integration_tests/assets/cert.pem b/certbot-ci/src/certbot_integration_tests/assets/cert.pem new file mode 100644 index 00000000000..5aae58f2598 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/cert.pem @@ -0,0 +1,32 @@ +-----BEGIN CERTIFICATE----- +MIIFlTCCA32gAwIBAgIUR3wbM8qFE68f8NxfciHhUjR1GeUwDQYJKoZIhvcNAQEL +BQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM +GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDESMBAGA1UEAwwJbmdpbngud3RmMCAX +DTE5MDQxODIwMDUwM1oYDzIyOTMwMTMwMjAwNTAzWjBZMQswCQYDVQQGEwJBVTET +MBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQ +dHkgTHRkMRIwEAYDVQQDDAluZ2lueC53dGYwggIiMA0GCSqGSIb3DQEBAQUAA4IC +DwAwggIKAoICAQC/W+yxYE0PWJOS4df71Yx596fDjW03I9JZuu9kfP7mneMgy+OC +HyRm0TEhl6FPUp9tD9YeEHloUZNjHEOg/qrnbEOspv3Ha3RFinzrzkMwbzEPR3Xf +0go+aVsWelDhapFl8fccw4tWwijVZQquhBsWOUnPenS3Txe96kEv2NNJlJ0qFUa+ +rOTruzRzOzlbgKv5WRb4+BxxWonHLkAQ5IT87GBlsCerVIyPD+BnZveZGl6e9oMH +ZlZvUT6aWRnzFWjAnQGiJpVIw7l9r4EW0jq1z7wqb37FrqrFbtWrOfUZVE7AlqXH +aKIR82/xwkcZfFk3sCAM0IcZc8B2SDLi4zNZtDivW6qQgTC/3z5yf1hnJ+j00dtE +X5qYlgXRaM2raOn31lxcerk5pjgagQ7Zj+v3YZS0QnenrgyXJcdnXLDj+cIARzx4 +QHtoO0nyP0RJqxvwX/H98513JTkeqFBc/Bx11UWYsUv20Qoo9IAuz0VDARu6rquu +k9anv56yvxo77qZ8r80l3z8eMyDA+UjuSD2p1Za09RAHfva7o8rMUqULHNQ4pfFH +JIUozHoinAg/9lBC/W80fcbILks+Sdi6E9WQ0n8PLl7oFLx9prEDCycKuC0z76J/ +Shb6R6sWr1YtzUFUc5EH2g9pMriaqT8uGO4CMOeRemXahrdT/H+Xg5m4TQIDAQAB +o1MwUTAdBgNVHQ4EFgQU46gJeu9ZOfTQ6c4vfbWbSLUpEMowHwYDVR0jBBgwFoAU +46gJeu9ZOfTQ6c4vfbWbSLUpEMowDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0B +AQsFAAOCAgEAcnfkXDUTsEGs0MleegkGbRCVy72a3U7tv1KVTLB8qLPc3tpPJJoT +D4PbOuw9+yIE+HetZTZooOpaZoorLQdiwAEjlQ44RVuXSHSARQ8KW9ZZeiWN/Qvl +Ip4xJ/cHxcKTFKSc/99o8M+kmPKEXF9SUMfKPc5jXarNxCsnA3VriYqJ+CnYEox2 +duNUEe3A9Y2d8ZxjmscBqlcXpk1kFwsCRT5UYVoUYwyjYznLkO5A+GJ0ZnMyRMQp +obUiB34hUrNgyOaBvizk+pNh9EV4rEBPRQwhy4vDMco4AjQcwLWQAQ9G4GSt/E+Q +62XdVDa6CAuOvBCudDPki7kEqNLbj1tMY1K/gsbgb6TYA/xTOVulAnqm4OEZ2svJ +0Jqw3BzMfRTaxsNU6jxm8WehVL15GjoJUzfs7Te+l7Vm/QNc1Dv2pmEhVfBibwMa +YxUZ8ClQtQ1lsOpne97Og0p/Cm93kKELNBLTjzXtpXGGPPYisAyNwe0Hadq8SiOd +pXeNwXa5vHOXHv8xBENzBvFJ3TRN2GmMlHBp/eOfVUx/huNSpcnh2gO3fn5EbMj7 +43IaR133JW5yWbneYAMJOEAMdEB5EthRmEDtLVA7kLqLc/ywFTQ4VbS2b+PsOr5O +501nzt0OTMMEz+UafvGXj5OPJBhe26RtnYXzVwwLfto/F5udM5zglWo= +-----END CERTIFICATE----- diff --git a/certbot-ci/src/certbot_integration_tests/assets/hook.py b/certbot-ci/src/certbot_integration_tests/assets/hook.py new file mode 100755 index 00000000000..1b04b2998b3 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/hook.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +"""A Certbot hook for probing.""" +import os +import sys + +hook_script_type = os.path.basename(os.path.dirname(sys.argv[1])) +if hook_script_type == 'deploy' and ('RENEWED_DOMAINS' not in os.environ + or 'RENEWED_LINEAGE' not in os.environ): + sys.stderr.write('Environment variables not properly set!\n') + sys.exit(1) + +print(hook_script_type) diff --git a/certbot-ci/src/certbot_integration_tests/assets/key.pem b/certbot-ci/src/certbot_integration_tests/assets/key.pem new file mode 100644 index 00000000000..1f768f079fb --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/key.pem @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQC/W+yxYE0PWJOS +4df71Yx596fDjW03I9JZuu9kfP7mneMgy+OCHyRm0TEhl6FPUp9tD9YeEHloUZNj +HEOg/qrnbEOspv3Ha3RFinzrzkMwbzEPR3Xf0go+aVsWelDhapFl8fccw4tWwijV +ZQquhBsWOUnPenS3Txe96kEv2NNJlJ0qFUa+rOTruzRzOzlbgKv5WRb4+BxxWonH +LkAQ5IT87GBlsCerVIyPD+BnZveZGl6e9oMHZlZvUT6aWRnzFWjAnQGiJpVIw7l9 +r4EW0jq1z7wqb37FrqrFbtWrOfUZVE7AlqXHaKIR82/xwkcZfFk3sCAM0IcZc8B2 +SDLi4zNZtDivW6qQgTC/3z5yf1hnJ+j00dtEX5qYlgXRaM2raOn31lxcerk5pjga +gQ7Zj+v3YZS0QnenrgyXJcdnXLDj+cIARzx4QHtoO0nyP0RJqxvwX/H98513JTke +qFBc/Bx11UWYsUv20Qoo9IAuz0VDARu6rquuk9anv56yvxo77qZ8r80l3z8eMyDA ++UjuSD2p1Za09RAHfva7o8rMUqULHNQ4pfFHJIUozHoinAg/9lBC/W80fcbILks+ +Sdi6E9WQ0n8PLl7oFLx9prEDCycKuC0z76J/Shb6R6sWr1YtzUFUc5EH2g9pMria +qT8uGO4CMOeRemXahrdT/H+Xg5m4TQIDAQABAoICAAGGL+pxw+tdXz+KQPgmiUnn +aRSrqbUIugIw9Pst67HWjBqUxSkiKl4PSH7mAEjrdY2e1KvEodLs42mkrf04ShAx +0pArfFX8Sx7KrZgLOonGOPPQM+YmfCJnIGybaM2C1cmkFb3K6O81+LFKbr1ZHAYf +SrE2XnufS6cdmItTBMvPPTk6lieqpOAjy5UnYZuS+Muxo/czsrZMbFCD08rOpyiE +kXf94TMCJ2R0UetA7LPxe9N0TzLd485bLU55azV+dCkklwC9oe7EcFPJ9BNEdWdB +UlRcMvxMGdwct+L3QTaEb2QlTwi5kqDl+XxJeduAHA3Pf1Haz1iqjVvj01PvT1di +Cs0+ZeFBsa+BfiGDe9ONwuSQljda1CuI+vDv5bGUExulOSG1dHJ7RK9PBaXFaR/b +/9tRBwAw1Erm7s1JIkjda5Oc46jFb3HzDaZYB1n5hUmEIrYM8HhUOGITyVT3hxDO +AWlaV3aveQ0MmMXLptVXDgbjPGbWDGMLD9d5vUE9R7IyOLeXOmjthYlCH2rj378M +r2PkgX2tD0A/yoEZ8XCFdtBWSVajLdL0/gkm7sKosMABBy3yrSCxbHeq5TFuTAXA +hOdypX4NOZkA6WJU+hn3GkQyIScLqSrvGRA9kzHGoEWVZDKkB9DXg+dmTARZDWXD +mCnHkJo6+FcbhUpXniuZAoIBAQDmE94vvdstB+HEtXxN1uNDY7H8gPc/BUonU6a9 +G5YOIbjByCfEDcXF8AUWekc6lc8DNG3ydx0dnb2ZAkxmdlsaD8GLqHGILzlSsOwR +sez8nR4+4n9vYMfx9Qal8Ren5xEP9Z9sJcNqbKVGta1WFtQzrgYbpVXXf/Luv0xS +YoVK8KaEACciD6XX4wmajrAXPPQgThvqQtXuTn/AxWsUDg1DK0tw1VRUuOJuJwpw +f6qocM9AyqUNvdeVyjFx8Slag34ZI7fmxPtHX/e6opTg3zVXab1Ow8AMICOHMRL6 +m5/+wnWa9xMoKI4kYfk/QFqeTccnLDlwi6kQM8WRfbwr9AyPAoIBAQDU60wrX6Lm +0vIfngv1/4j/w+AGAwjvxiuJ7Q7LwQ2fGsZGOIfMK/lpBxCn543kGbQT+KQKNOjO ++EywObftnJ6Y2+om2NoLkCnCiptsfr5WlN8pxtIPQu2iu5xXA67WpQv4Nc4769PM +wJGVW3pmPKi6H0QjjqYAZd1NAXdN9Au14zZVh3KBWoz82kTHWKSL6Ld1UClG728W +k/moyCFFMMGTXX/LVliQzDVLM6L5jbAOaG317qAuxZIqFJ9NLwHFW9uH/i1S6Qfp ++lOmOfVYKu1O/qh1DUBQfuJkR1XIn2ifZEjxOsxeTmWu1LXpyoZy526JRu49pk8Z +DdEu+w7hsdNjAoIBAD1YWsub8Y6GJXpPcX9HpnzXXiOXN1VEUcs+kJyneFD4SMzS +U1gA3BS0tIaTv94tB28xUYdunwLAhkb/x+Mh95RxUwert+m5va0Ao1DsgeWw9tmJ +hrTptyYaUNV5/Pa1s2Tv9rvdLcd4hHDgDAGCQL4uzk4cvVCiOuHRe8YTorqig6N6 +bvSz+2IelPbyyJzJkcXzTZoei+/oWkPJ340PWhXou0qwdrXIPgdkvXHVeGlE+t2p +qmyJi6vSp3Bb/sy1dq+5SFVtfBpBykmnA88ZdJ2EAge4RcJ150MqoIbVa8l/i9/v +tNnmRlAJF233+LFwx4L4VbBebIt3YlwyjDOj9J0CggEAIknKOGnsV/O8ni7bikAe +leG7X/x5IfPt6wZMDbAHO4oaSBCufcjPH4TNv9xgU014XIb8E9C1dS8zWmXRIujH ++aHgsWTWqGoM75FWukAm8taCob2s8lw63KwN301uiI6HwO8ZSTkPILgaOc1DhtdZ +7K9AT+GXBhVhcBc+WUVl5WKzy05GuGIWtlmIHfo+dXGCqdfA7fV9FEu8NtwTz4qs +gcja3aoIFTltk7C7HCkfIxLaMnK9RQr4IOK1TL63MEs8rUfXkLSKW7m+YtSOmCZB +lSkZg9AgfVYRq0h5nhddx91kicSISN+jLGaA7Sd6Q2LVwDG2CCOSNVyuRTyVBu+W +NQKCAQAWN6vB6oToNIoBLdOThm0HD07cNHcrnBjtaKsYsQDgqbr2m8LRCRzNRML4 +jG0IAOWpuCiEGsgUPxywiI1Ufvyq7ZSNT1QQNzCR47NM3Ve6S2abrQkMIk9VJ+za +CB9c1BH92GokoRxqswb/BiMttG2EIP8L8/pSRYEcVnaaxAkf9QOhEwj4LJPGX0mS +t7kWIUVHPdFJ67F25dYr3mUHgyV+QJupQICkkkgY3nBOU1fS42vAugaxqH0wAP3T +53FlpY3NuE7+kYC3FjfcBer99F1pOac3X9jxhk26w9dr2/QNA33xhDXHKYvoLUCG +RPQylahJByU7IrtQzSCf/RE7q4v0 +-----END PRIVATE KEY----- diff --git a/tests/letstest/testdata/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/meta.json b/certbot-ci/src/certbot_integration_tests/assets/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/meta.json similarity index 100% rename from tests/letstest/testdata/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/meta.json rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/meta.json diff --git a/tests/letstest/testdata/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/private_key.json b/certbot-ci/src/certbot_integration_tests/assets/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/private_key.json similarity index 100% rename from tests/letstest/testdata/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/private_key.json rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/private_key.json diff --git a/tests/letstest/testdata/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/regr.json b/certbot-ci/src/certbot_integration_tests/assets/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/regr.json similarity index 100% rename from tests/letstest/testdata/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/regr.json rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/accounts/acme-staging.api.letsencrypt.org/directory/48d6b9e8d767eccf7e4d877d6ffa81e3/regr.json diff --git a/tests/letstest/testdata/sample-config/archive/a.encryption-example.com/cert1.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/a.encryption-example.com/cert1.pem similarity index 100% rename from tests/letstest/testdata/sample-config/archive/a.encryption-example.com/cert1.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/a.encryption-example.com/cert1.pem diff --git a/tests/letstest/testdata/sample-config/archive/a.encryption-example.com/chain1.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/a.encryption-example.com/chain1.pem similarity index 100% rename from tests/letstest/testdata/sample-config/archive/a.encryption-example.com/chain1.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/a.encryption-example.com/chain1.pem diff --git a/tests/letstest/testdata/sample-config/archive/a.encryption-example.com/fullchain1.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/a.encryption-example.com/fullchain1.pem similarity index 100% rename from tests/letstest/testdata/sample-config/archive/a.encryption-example.com/fullchain1.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/a.encryption-example.com/fullchain1.pem diff --git a/tests/letstest/testdata/sample-config/archive/a.encryption-example.com/privkey1.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/a.encryption-example.com/privkey1.pem similarity index 100% rename from tests/letstest/testdata/sample-config/archive/a.encryption-example.com/privkey1.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/a.encryption-example.com/privkey1.pem diff --git a/tests/letstest/testdata/sample-config/archive/b.encryption-example.com/cert1.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/b.encryption-example.com/cert1.pem similarity index 100% rename from tests/letstest/testdata/sample-config/archive/b.encryption-example.com/cert1.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/b.encryption-example.com/cert1.pem diff --git a/tests/letstest/testdata/sample-config/archive/b.encryption-example.com/chain1.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/b.encryption-example.com/chain1.pem similarity index 100% rename from tests/letstest/testdata/sample-config/archive/b.encryption-example.com/chain1.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/b.encryption-example.com/chain1.pem diff --git a/tests/letstest/testdata/sample-config/archive/b.encryption-example.com/fullchain1.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/b.encryption-example.com/fullchain1.pem similarity index 100% rename from tests/letstest/testdata/sample-config/archive/b.encryption-example.com/fullchain1.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/b.encryption-example.com/fullchain1.pem diff --git a/tests/letstest/testdata/sample-config/archive/b.encryption-example.com/privkey1.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/b.encryption-example.com/privkey1.pem similarity index 100% rename from tests/letstest/testdata/sample-config/archive/b.encryption-example.com/privkey1.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/b.encryption-example.com/privkey1.pem diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/README b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/README new file mode 100644 index 00000000000..5050078ff85 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/README @@ -0,0 +1,14 @@ +This directory contains your keys and certificates. + +`privkey.pem` : the private key for your certificate. +`fullchain.pem`: the certificate file used in most server software. +`chain.pem` : used for OCSP stapling in Nginx >=1.3.7. +`cert.pem` : will break many server configurations, and should not be used + without reading further documentation (see link below). + +WARNING: DO NOT MOVE OR RENAME THESE FILES! + Certbot expects these files to remain in this location in order + to function properly! + +We recommend not moving these files. For more information, see the Certbot +User Guide at https://certbot.eff.org/docs/using.html#where-are-my-certificates. diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/cert.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/cert.pem new file mode 100644 index 00000000000..b07af2bf75c --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/cert.pem @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC2zCCAcOgAwIBAgIIBvrEnbPRYu8wDQYJKoZIhvcNAQELBQAwKDEmMCQGA1UE +AxMdUGViYmxlIEludGVybWVkaWF0ZSBDQSAxMjZjNGIwHhcNMjAxMDEyMjEwNzQw +WhcNMjUxMDEyMjEwNzQwWjAjMSEwHwYDVQQDExhjLmVuY3J5cHRpb24tZXhhbXBs +ZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARjMhuW0ENPPC33PjB5XsYU +CRw640kPQENIDatcTJaENZIZdqKd6rI6jc+lpbmXot7Zi52clJlSJS+V6oDAt2Lh +o4HYMIHVMA4GA1UdDwEB/wQEAwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYB +BQUHAwIwDAYDVR0TAQH/BAIwADAdBgNVHQ4EFgQUj7Kd3ENqxlPf8B2bIGhsjydX +mPswHwYDVR0jBBgwFoAUEiGxlkRsi+VvcogH5dVD3h1laAcwMQYIKwYBBQUHAQEE +JTAjMCEGCCsGAQUFBzABhhVodHRwOi8vMTI3LjAuMC4xOjQwMDIwIwYDVR0RBBww +GoIYYy5lbmNyeXB0aW9uLWV4YW1wbGUuY29tMA0GCSqGSIb3DQEBCwUAA4IBAQCl +k0JXsa8y7fg41WWMDhw60bPW77O0FtOmTcnhdI5daYNemQVk+Q5EMaBLQ/oGjgXd +9QXFzXH1PL904YEnSLt+iTpXn++7rQSNzQsdYqw0neWk4f5pEBiN+WORpb6mwobV +ifMtBOkNEHvrJ2Pkci9U1lLwtKD/DSew6QtJU5DSkmH1XdGuMJiubygEIvELtvgq +cP9S368ZvPmPGmKaJQXBiuaR8MTjY/Bkr79aXQMjKbf+mpn7h0POCcePk1DY/rm6 +Da+X16lf0hHyQhSUa7Vgyim6rK1/hlw+Z00i+sQCKD9Ih7kXuuGqfSDC33cfO8Tj +o/MXO8lcxkrem5zU5QWP +-----END CERTIFICATE----- diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/chain.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/chain.pem new file mode 100644 index 00000000000..64a805c0fd1 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/chain.pem @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDUDCCAjigAwIBAgIIbi787yVrcMAwDQYJKoZIhvcNAQELBQAwIDEeMBwGA1UE +AxMVUGViYmxlIFJvb3QgQ0EgMGM1MjI1MCAXDTIwMTAxMjIwMjI0NloYDzIwNTAx +MDEyMjEyMjQ2WjAoMSYwJAYDVQQDEx1QZWJibGUgSW50ZXJtZWRpYXRlIENBIDEy +NmM0YjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALGeVk1BMJraeqRq +mJ2+hgso8VOAv2s2CVxUJjIVcn7f2adE8NyTsSQ1brlsnKCUYUw7yLTQH0izLQRB +qKVIDFkUqo5/FuTJ2QlfA2EwBL8J7s/7L7vj3L0DiVpwgxPSyFEwdl/Y5y7ofsX5 +CIhCFcaMAmTIuKLiSfCJjGwkbEMuolm+lO8Mikxxc/JtDVUC479ugU7PU9O09bMH +nm+sD6Bgd+KMoPkCCCoeShJS9X3Ziq9HGc7Z6nhM/zirFARt2XkonEdAZ8br01zY +MRiY9txhlWQ7mUkOtzOSoEuYJNoUbvMUf0+tNzto26WRyF7dJmh7lTBsYrvAwUTx +PzNyst0CAwEAAaOBgzCBgDAOBgNVHQ8BAf8EBAMCAoQwHQYDVR0lBBYwFAYIKwYB +BQUHAwEGCCsGAQUFBwMCMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFBIhsZZE +bIvlb3KIB+XVQ94dZWgHMB8GA1UdIwQYMBaAFOaKTaXg37vKgRt7d79YOjAoAtJT +MA0GCSqGSIb3DQEBCwUAA4IBAQAU2mZii7PH2pkw2lNM0QqPbcW/UYyvFoUeM8Aq +uCtsI2s+oxCJTqzfLsA0N8NY4nHLQ5wAlNJfJekngni8hbmJTKU4JFTMe7kLQO8P +fJbk0pTzhhHVQw7CVwB6Pwq3u2m/JV+d6xDIDc+AVkuEl19ZJU0rTWyooClfFLZV +EdZmEiUtA3PGlxoYwYhoGHYlhFxsoFONhCsBEdN7k7FKtFGVxN7oc5SKmKp0YZTW +fcrEtrdNThATO4ymhCC2zh33NI/MT1O74fpaAc2k6LcTl57MKiLfTYX4LTL6v9JG +9tlNqjFVRRmzEbtXTPcCb+w9g1VqoOGok7mGXYLTYtShCuvE +-----END CERTIFICATE----- diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/fullchain.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/fullchain.pem new file mode 100644 index 00000000000..1ba80ba4e9e --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/fullchain.pem @@ -0,0 +1,38 @@ +-----BEGIN CERTIFICATE----- +MIIC2zCCAcOgAwIBAgIILlmGtZhUFEwwDQYJKoZIhvcNAQELBQAwKDEmMCQGA1UE +AxMdUGViYmxlIEludGVybWVkaWF0ZSBDQSAxMjZjNGIwHhcNMjAxMDEyMjA1MDM0 +WhcNMjUxMDEyMjA1MDM0WjAjMSEwHwYDVQQDExhjLmVuY3J5cHRpb24tZXhhbXBs +ZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARHEzR8JPWrEmpmgM+F2bk5 +9mT0u6CjzmJG0QpbaqprLiG5NGpW84VQ5TFCrmC4KxYfigCfMhfHRNfFYvNUK3V/ +o4HYMIHVMA4GA1UdDwEB/wQEAwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYB +BQUHAwIwDAYDVR0TAQH/BAIwADAdBgNVHQ4EFgQU1CsVL+bPnzaxxQ5jUENmQJIO +lKwwHwYDVR0jBBgwFoAUEiGxlkRsi+VvcogH5dVD3h1laAcwMQYIKwYBBQUHAQEE +JTAjMCEGCCsGAQUFBzABhhVodHRwOi8vMTI3LjAuMC4xOjQwMDIwIwYDVR0RBBww +GoIYYy5lbmNyeXB0aW9uLWV4YW1wbGUuY29tMA0GCSqGSIb3DQEBCwUAA4IBAQBn +2D8loC7pfk28JYpFLr5lmFKJWWmtLGlpsWDj61fVjtTfGKLziJz+MM6il4Y3hIz5 +58qiFK0ue0M63dIBJ33N+XxSEXon4Q0gy/zRWfH9jtPJ3FwfjkU/RT9PAUClYi0G +ptNWnTmgQkNzousbcAtRNXuuShH3856vhUnwkX+xM+cbIDi1JVmFjcGrEEQJ0rUF +mv2ZTyfbWbUs3v4rReETi2NVzr1Ql6J+ByNcMvHODzFy3t0L6yelAw2ca1I+c9HU ++Z0tnp/ykR7eXNuVLivok8UBf5OC413lh8ZO5g+Bgzh/LdtkUuavg1MYtEX0H6mX +9U7y3nVI8WEbPGf+HDeu +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIDUDCCAjigAwIBAgIIbi787yVrcMAwDQYJKoZIhvcNAQELBQAwIDEeMBwGA1UE +AxMVUGViYmxlIFJvb3QgQ0EgMGM1MjI1MCAXDTIwMTAxMjIwMjI0NloYDzIwNTAx +MDEyMjEyMjQ2WjAoMSYwJAYDVQQDEx1QZWJibGUgSW50ZXJtZWRpYXRlIENBIDEy +NmM0YjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALGeVk1BMJraeqRq +mJ2+hgso8VOAv2s2CVxUJjIVcn7f2adE8NyTsSQ1brlsnKCUYUw7yLTQH0izLQRB +qKVIDFkUqo5/FuTJ2QlfA2EwBL8J7s/7L7vj3L0DiVpwgxPSyFEwdl/Y5y7ofsX5 +CIhCFcaMAmTIuKLiSfCJjGwkbEMuolm+lO8Mikxxc/JtDVUC479ugU7PU9O09bMH +nm+sD6Bgd+KMoPkCCCoeShJS9X3Ziq9HGc7Z6nhM/zirFARt2XkonEdAZ8br01zY +MRiY9txhlWQ7mUkOtzOSoEuYJNoUbvMUf0+tNzto26WRyF7dJmh7lTBsYrvAwUTx +PzNyst0CAwEAAaOBgzCBgDAOBgNVHQ8BAf8EBAMCAoQwHQYDVR0lBBYwFAYIKwYB +BQUHAwEGCCsGAQUFBwMCMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFBIhsZZE +bIvlb3KIB+XVQ94dZWgHMB8GA1UdIwQYMBaAFOaKTaXg37vKgRt7d79YOjAoAtJT +MA0GCSqGSIb3DQEBCwUAA4IBAQAU2mZii7PH2pkw2lNM0QqPbcW/UYyvFoUeM8Aq +uCtsI2s+oxCJTqzfLsA0N8NY4nHLQ5wAlNJfJekngni8hbmJTKU4JFTMe7kLQO8P +fJbk0pTzhhHVQw7CVwB6Pwq3u2m/JV+d6xDIDc+AVkuEl19ZJU0rTWyooClfFLZV +EdZmEiUtA3PGlxoYwYhoGHYlhFxsoFONhCsBEdN7k7FKtFGVxN7oc5SKmKp0YZTW +fcrEtrdNThATO4ymhCC2zh33NI/MT1O74fpaAc2k6LcTl57MKiLfTYX4LTL6v9JG +9tlNqjFVRRmzEbtXTPcCb+w9g1VqoOGok7mGXYLTYtShCuvE +-----END CERTIFICATE----- diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/privkey.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/privkey.pem new file mode 100644 index 00000000000..6843b83d6e4 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/archive/c.encryption-example.com/privkey.pem @@ -0,0 +1,5 @@ +-----BEGIN PRIVATE KEY----- +MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgNgefv2dad4U1VYEi +0WkdHuqywi5QXAe30OwNTTGjhbihRANCAARHEzR8JPWrEmpmgM+F2bk59mT0u6Cj +zmJG0QpbaqprLiG5NGpW84VQ5TFCrmC4KxYfigCfMhfHRNfFYvNUK3V/ +-----END PRIVATE KEY----- diff --git a/tests/letstest/testdata/sample-config/csr/0000_csr-certbot.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/csr/0000_csr-certbot.pem similarity index 100% rename from tests/letstest/testdata/sample-config/csr/0000_csr-certbot.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/csr/0000_csr-certbot.pem diff --git a/tests/letstest/testdata/sample-config/csr/0001_csr-certbot.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/csr/0001_csr-certbot.pem similarity index 100% rename from tests/letstest/testdata/sample-config/csr/0001_csr-certbot.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/csr/0001_csr-certbot.pem diff --git a/tests/letstest/testdata/sample-config/csr/0002_csr-certbot.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/csr/0002_csr-certbot.pem similarity index 100% rename from tests/letstest/testdata/sample-config/csr/0002_csr-certbot.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/csr/0002_csr-certbot.pem diff --git a/tests/letstest/testdata/sample-config/csr/0003_csr-certbot.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/csr/0003_csr-certbot.pem similarity index 100% rename from tests/letstest/testdata/sample-config/csr/0003_csr-certbot.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/csr/0003_csr-certbot.pem diff --git a/tests/letstest/testdata/sample-config/keys/0000_key-certbot.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/keys/0000_key-certbot.pem similarity index 100% rename from tests/letstest/testdata/sample-config/keys/0000_key-certbot.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/keys/0000_key-certbot.pem diff --git a/tests/letstest/testdata/sample-config/keys/0001_key-certbot.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/keys/0001_key-certbot.pem similarity index 100% rename from tests/letstest/testdata/sample-config/keys/0001_key-certbot.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/keys/0001_key-certbot.pem diff --git a/tests/letstest/testdata/sample-config/keys/0002_key-certbot.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/keys/0002_key-certbot.pem similarity index 100% rename from tests/letstest/testdata/sample-config/keys/0002_key-certbot.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/keys/0002_key-certbot.pem diff --git a/tests/letstest/testdata/sample-config/keys/0003_key-certbot.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/keys/0003_key-certbot.pem similarity index 100% rename from tests/letstest/testdata/sample-config/keys/0003_key-certbot.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/keys/0003_key-certbot.pem diff --git a/tests/letstest/testdata/sample-config/live/a.encryption-example.com/README b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/README similarity index 100% rename from tests/letstest/testdata/sample-config/live/a.encryption-example.com/README rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/README diff --git a/tests/letstest/testdata/sample-config/live/a.encryption-example.com/cert.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/cert.pem similarity index 100% rename from tests/letstest/testdata/sample-config/live/a.encryption-example.com/cert.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/cert.pem diff --git a/tests/letstest/testdata/sample-config/live/a.encryption-example.com/chain.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/chain.pem similarity index 100% rename from tests/letstest/testdata/sample-config/live/a.encryption-example.com/chain.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/chain.pem diff --git a/tests/letstest/testdata/sample-config/live/a.encryption-example.com/fullchain.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/fullchain.pem similarity index 100% rename from tests/letstest/testdata/sample-config/live/a.encryption-example.com/fullchain.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/fullchain.pem diff --git a/tests/letstest/testdata/sample-config/live/a.encryption-example.com/privkey.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/privkey.pem similarity index 100% rename from tests/letstest/testdata/sample-config/live/a.encryption-example.com/privkey.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/a.encryption-example.com/privkey.pem diff --git a/tests/letstest/testdata/sample-config/live/b.encryption-example.com/README b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/README similarity index 100% rename from tests/letstest/testdata/sample-config/live/b.encryption-example.com/README rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/README diff --git a/tests/letstest/testdata/sample-config/live/b.encryption-example.com/cert.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/cert.pem similarity index 100% rename from tests/letstest/testdata/sample-config/live/b.encryption-example.com/cert.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/cert.pem diff --git a/tests/letstest/testdata/sample-config/live/b.encryption-example.com/chain.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/chain.pem similarity index 100% rename from tests/letstest/testdata/sample-config/live/b.encryption-example.com/chain.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/chain.pem diff --git a/tests/letstest/testdata/sample-config/live/b.encryption-example.com/fullchain.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/fullchain.pem similarity index 100% rename from tests/letstest/testdata/sample-config/live/b.encryption-example.com/fullchain.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/fullchain.pem diff --git a/tests/letstest/testdata/sample-config/live/b.encryption-example.com/privkey.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/privkey.pem similarity index 100% rename from tests/letstest/testdata/sample-config/live/b.encryption-example.com/privkey.pem rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/live/b.encryption-example.com/privkey.pem diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/README b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/README new file mode 100644 index 00000000000..5050078ff85 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/README @@ -0,0 +1,14 @@ +This directory contains your keys and certificates. + +`privkey.pem` : the private key for your certificate. +`fullchain.pem`: the certificate file used in most server software. +`chain.pem` : used for OCSP stapling in Nginx >=1.3.7. +`cert.pem` : will break many server configurations, and should not be used + without reading further documentation (see link below). + +WARNING: DO NOT MOVE OR RENAME THESE FILES! + Certbot expects these files to remain in this location in order + to function properly! + +We recommend not moving these files. For more information, see the Certbot +User Guide at https://certbot.eff.org/docs/using.html#where-are-my-certificates. diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/cert.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/cert.pem new file mode 120000 index 00000000000..23e9f36efbb --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/cert.pem @@ -0,0 +1 @@ +../../archive/c.encryption-example.com/cert.pem \ No newline at end of file diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/chain.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/chain.pem new file mode 120000 index 00000000000..3ce63c2200e --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/chain.pem @@ -0,0 +1 @@ +../../archive/c.encryption-example.com/chain.pem \ No newline at end of file diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/fullchain.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/fullchain.pem new file mode 120000 index 00000000000..5f86022af9b --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/fullchain.pem @@ -0,0 +1 @@ +../../archive/c.encryption-example.com/fullchain.pem \ No newline at end of file diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/privkey.pem b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/privkey.pem new file mode 120000 index 00000000000..4a8866fdc8b --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/live/c.encryption-example.com/privkey.pem @@ -0,0 +1 @@ +../../archive/c.encryption-example.com/privkey.pem \ No newline at end of file diff --git a/tests/letstest/testdata/sample-config/options-ssl-apache.conf b/certbot-ci/src/certbot_integration_tests/assets/sample-config/options-ssl-apache.conf similarity index 100% rename from tests/letstest/testdata/sample-config/options-ssl-apache.conf rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/options-ssl-apache.conf diff --git a/tests/letstest/testdata/sample-config/renewal/a.encryption-example.com.conf b/certbot-ci/src/certbot_integration_tests/assets/sample-config/renewal/a.encryption-example.com.conf similarity index 100% rename from tests/letstest/testdata/sample-config/renewal/a.encryption-example.com.conf rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/renewal/a.encryption-example.com.conf diff --git a/tests/letstest/testdata/sample-config/renewal/b.encryption-example.com.conf b/certbot-ci/src/certbot_integration_tests/assets/sample-config/renewal/b.encryption-example.com.conf similarity index 100% rename from tests/letstest/testdata/sample-config/renewal/b.encryption-example.com.conf rename to certbot-ci/src/certbot_integration_tests/assets/sample-config/renewal/b.encryption-example.com.conf diff --git a/certbot-ci/src/certbot_integration_tests/assets/sample-config/renewal/c.encryption-example.com.conf b/certbot-ci/src/certbot_integration_tests/assets/sample-config/renewal/c.encryption-example.com.conf new file mode 100644 index 00000000000..0c65891e461 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/assets/sample-config/renewal/c.encryption-example.com.conf @@ -0,0 +1,17 @@ +# renew_before_expiry = 30 days +version = 1.10.0.dev0 +archive_dir = sample-config/archive/c.encryption-example.com +cert = sample-config/live/c.encryption-example.com/cert.pem +privkey = sample-config/live/c.encryption-example.com/privkey.pem +chain = sample-config/live/c.encryption-example.com/chain.pem +fullchain = sample-config/live/c.encryption-example.com/fullchain.pem + +# Options used in the renewal process +[renewalparams] +authenticator = apache +installer = apache +account = 48d6b9e8d767eccf7e4d877d6ffa81e3 +key_type = ecdsa +config_dir = sample-config-ec +elliptic_curve = secp256r1 +manual_public_ip_logging_ok = True diff --git a/certbot-ci/src/certbot_integration_tests/certbot_tests/__init__.py b/certbot-ci/src/certbot_integration_tests/certbot_tests/__init__.py new file mode 100644 index 00000000000..819cb3e7823 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/certbot_tests/__init__.py @@ -0,0 +1,6 @@ +# pylint: disable=missing-module-docstring +import pytest + +# Custom assertions defined in the following package need to be registered to be properly +# displayed in a pytest report when they are failing. +pytest.register_assert_rewrite('certbot_integration_tests.certbot_tests.assertions') diff --git a/certbot-ci/src/certbot_integration_tests/certbot_tests/assertions.py b/certbot-ci/src/certbot_integration_tests/certbot_tests/assertions.py new file mode 100644 index 00000000000..07246216269 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/certbot_tests/assertions.py @@ -0,0 +1,205 @@ +"""This module contains advanced assertions for the certbot integration tests.""" +import os +from typing import Optional + +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurve +from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePrivateKey +from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey +from cryptography.hazmat.primitives.serialization import load_pem_private_key + +try: + import grp + POSIX_MODE = True +except ImportError: + import ntsecuritycon + import win32security + POSIX_MODE = False + +EVERYBODY_SID = 'S-1-1-0' +SYSTEM_SID = 'S-1-5-18' +ADMINS_SID = 'S-1-5-32-544' + + +def assert_elliptic_key(key_path: str, curve: type[EllipticCurve]) -> None: + """ + Asserts that the key at the given path is an EC key using the given curve. + :param key_path: path to key + :param EllipticCurve curve: name of the expected elliptic curve + """ + with open(key_path, 'rb') as file: + privkey1 = file.read() + + key = load_pem_private_key(data=privkey1, password=None, backend=default_backend()) + + assert isinstance(key, EllipticCurvePrivateKey), f"should be an EC key but was {type(key)}" + assert isinstance(key.curve, curve), f"should have curve {curve} but was {key.curve}" + + +def assert_rsa_key(key_path: str, key_size: Optional[int] = None) -> None: + """ + Asserts that the key at the given path is an RSA key. + :param str key_path: path to key + :param int key_size: if provided, assert that the RSA key is of this size + """ + with open(key_path, 'rb') as file: + privkey1 = file.read() + + key = load_pem_private_key(data=privkey1, password=None, backend=default_backend()) + assert isinstance(key, RSAPrivateKey) + if key_size: + assert key_size == key.key_size + + +def assert_hook_execution(probe_path: str, probe_content: str) -> None: + """ + Assert that a certbot hook has been executed + :param str probe_path: path to the file that received the hook output + :param str probe_content: content expected when the hook is executed + """ + encoding = 'utf-8' if POSIX_MODE else 'utf-16' + with open(probe_path, 'rt', encoding=encoding) as file: + data = file.read() + + lines = [line.strip() for line in data.splitlines()] + assert probe_content in lines + + +def assert_saved_lineage_option(config_dir: str, lineage: str, + option: str, value: Optional[str] = None) -> None: + """ + Assert that the option of a lineage has been saved. + :param str config_dir: location of the certbot configuration + :param str lineage: lineage domain name + :param str option: the option key + :param value: if desired, the expected option value + """ + with open(os.path.join(config_dir, 'renewal', f'{lineage}.conf')) as file_h: + assert f"{option} = {value if value else ''}" in file_h.read() + + +def assert_saved_deploy_hook(config_dir: str, lineage: str) -> None: + """ + Assert that the deploy hook configuration of a lineage has been saved. + :param str config_dir: location of the certbot configuration + :param str lineage: lineage domain name + """ + assert_saved_lineage_option(config_dir, lineage, 'renew_hook') + + +def assert_cert_count_for_lineage(config_dir: str, lineage: str, count: int) -> None: + """ + Assert the number of certificates generated for a lineage. + :param str config_dir: location of the certbot configuration + :param str lineage: lineage domain name + :param int count: number of expected certificates + """ + archive_dir = os.path.join(config_dir, 'archive') + lineage_dir = os.path.join(archive_dir, lineage) + certs = [file for file in os.listdir(lineage_dir) if file.startswith('cert')] + assert len(certs) == count + + +def assert_equals_group_permissions(file1: str, file2: str) -> None: + """ + Assert that two files have the same permissions for group owner. + :param str file1: first file path to compare + :param str file2: second file path to compare + """ + # On Windows there is no group, so this assertion does nothing on this platform + if POSIX_MODE: + mode_file1 = os.stat(file1).st_mode & 0o070 + mode_file2 = os.stat(file2).st_mode & 0o070 + + assert mode_file1 == mode_file2 + + +def assert_equals_world_read_permissions(file1: str, file2: str) -> None: + """ + Assert that two files have the same read permissions for everyone. + :param str file1: first file path to compare + :param str file2: second file path to compare + """ + if POSIX_MODE: + mode_file1 = os.stat(file1).st_mode & 0o004 + mode_file2 = os.stat(file2).st_mode & 0o004 + else: + everybody = win32security.ConvertStringSidToSid(EVERYBODY_SID) # pylint: disable=used-before-assignment + + security1 = win32security.GetFileSecurity(file1, win32security.DACL_SECURITY_INFORMATION) + dacl1 = security1.GetSecurityDescriptorDacl() + + mode_file1 = dacl1.GetEffectiveRightsFromAcl({ + 'TrusteeForm': win32security.TRUSTEE_IS_SID, + 'TrusteeType': win32security.TRUSTEE_IS_USER, + 'Identifier': everybody, + }) + mode_file1 = mode_file1 & ntsecuritycon.FILE_GENERIC_READ # pylint: disable=used-before-assignment + + security2 = win32security.GetFileSecurity(file2, win32security.DACL_SECURITY_INFORMATION) + dacl2 = security2.GetSecurityDescriptorDacl() + + mode_file2 = dacl2.GetEffectiveRightsFromAcl({ + 'TrusteeForm': win32security.TRUSTEE_IS_SID, + 'TrusteeType': win32security.TRUSTEE_IS_USER, + 'Identifier': everybody, + }) + mode_file2 = mode_file2 & ntsecuritycon.FILE_GENERIC_READ + + assert mode_file1 == mode_file2 + + +def assert_equals_group_owner(file1: str, file2: str) -> None: + """ + Assert that two files have the same group owner. + :param str file1: first file path to compare + :param str file2: second file path to compare + """ + # On Windows there is no group, so this assertion does nothing on this platform + if POSIX_MODE: + group_owner_file1 = grp.getgrgid(os.stat(file1).st_gid)[0] + group_owner_file2 = grp.getgrgid(os.stat(file2).st_gid)[0] + + assert group_owner_file1 == group_owner_file2 + + +def assert_world_no_permissions(file: str) -> None: + """ + Assert that the given file is not world-readable. + :param str file: path of the file to check + """ + if POSIX_MODE: + mode_file_all = os.stat(file).st_mode & 0o007 + assert mode_file_all == 0 + else: + security = win32security.GetFileSecurity(file, win32security.DACL_SECURITY_INFORMATION) + dacl = security.GetSecurityDescriptorDacl() + mode = dacl.GetEffectiveRightsFromAcl({ + 'TrusteeForm': win32security.TRUSTEE_IS_SID, + 'TrusteeType': win32security.TRUSTEE_IS_USER, + 'Identifier': win32security.ConvertStringSidToSid(EVERYBODY_SID), + }) + + assert not mode + + +def assert_world_read_permissions(file: str) -> None: + """ + Assert that the given file is world-readable, but not world-writable or world-executable. + :param str file: path of the file to check + """ + if POSIX_MODE: + mode_file_all = os.stat(file).st_mode & 0o007 + assert mode_file_all == 4 + else: + security = win32security.GetFileSecurity(file, win32security.DACL_SECURITY_INFORMATION) + dacl = security.GetSecurityDescriptorDacl() + mode = dacl.GetEffectiveRightsFromAcl({ + 'TrusteeForm': win32security.TRUSTEE_IS_SID, + 'TrusteeType': win32security.TRUSTEE_IS_USER, + 'Identifier': win32security.ConvertStringSidToSid(EVERYBODY_SID), + }) + + assert not mode & ntsecuritycon.FILE_GENERIC_WRITE + assert not mode & ntsecuritycon.FILE_GENERIC_EXECUTE + assert mode & ntsecuritycon.FILE_GENERIC_READ == ntsecuritycon.FILE_GENERIC_READ diff --git a/certbot-ci/src/certbot_integration_tests/certbot_tests/context.py b/certbot-ci/src/certbot_integration_tests/certbot_tests/context.py new file mode 100644 index 00000000000..4547ac713f7 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/certbot_tests/context.py @@ -0,0 +1,95 @@ +"""Module to handle the context of integration tests.""" +import os +import shutil +import sys +import tempfile +from typing import Iterable + +import pytest + +from certbot_integration_tests.utils import certbot_call + + +class IntegrationTestsContext: + """General fixture describing a certbot integration tests context""" + def __init__(self, request: pytest.FixtureRequest) -> None: + self.request = request + + if hasattr(request.config, 'workerinput'): # Worker node + self.worker_id = request.config.workerinput['workerid'] + acme_xdist = request.config.workerinput['acme_xdist'] + else: # Primary node + self.worker_id = 'primary' + acme_xdist = request.config.acme_xdist # type: ignore[attr-defined] + + self.directory_url = acme_xdist['directory_url'] + self.https_port = acme_xdist['https_port'][self.worker_id] + self.local_ip = str(acme_xdist['local_ip'][self.worker_id]) + self.http_01_port = acme_xdist['http_port'][self.worker_id] + self.other_port = acme_xdist['other_port'][self.worker_id] + # Challtestsrv REST API, that exposes entrypoints to register new DNS entries, + # is listening on challtestsrv_url. + self.challtestsrv_url = acme_xdist['challtestsrv_url'] + + self.workspace = tempfile.mkdtemp() + self.config_dir = os.path.join(self.workspace, 'conf') + + probe = tempfile.mkstemp(dir=self.workspace) + os.close(probe[0]) + self.hook_probe = probe[1] + + self.manual_dns_auth_hook = ( + '{0} -c "import os; import requests; import json; ' + "assert not os.environ.get('CERTBOT_DOMAIN').startswith('fail'); " + "data = {{'host':'_acme-challenge.{{0}}.'.format(os.environ.get('CERTBOT_DOMAIN'))," + "'value':os.environ.get('CERTBOT_VALIDATION')}}; " + "request = requests.post('{1}/set-txt', data=json.dumps(data)); " + "request.raise_for_status(); " + '"' + ).format(sys.executable, self.challtestsrv_url) + self.manual_dns_auth_hook_allow_fail = ( + '{0} -c "import os; import requests; import json; ' + "data = {{'host':'_acme-challenge.{{0}}.'.format(os.environ.get('CERTBOT_DOMAIN'))," + "'value':os.environ.get('CERTBOT_VALIDATION')}}; " + "request = requests.post('{1}/set-txt', data=json.dumps(data)); " + "request.raise_for_status(); " + '"' + ).format(sys.executable, self.challtestsrv_url) + self.manual_dns_cleanup_hook = ( + '{0} -c "import os; import requests; import json; ' + "data = {{'host':'_acme-challenge.{{0}}.'.format(os.environ.get('CERTBOT_DOMAIN'))}}; " + "request = requests.post('{1}/clear-txt', data=json.dumps(data)); " + "request.raise_for_status(); " + '"' + ).format(sys.executable, self.challtestsrv_url) + + def cleanup(self) -> None: + """Cleanup the integration test context.""" + shutil.rmtree(self.workspace) + + def certbot(self, args: Iterable[str], force_renew: bool = True) -> tuple[str, str]: + """ + Execute certbot with given args, not renewing certificates by default. + :param args: args to pass to certbot + :param bool force_renew: set to False to not renew by default + :return: stdout and stderr from certbot execution + :rtype: Tuple of `str` + """ + command = ['--authenticator', 'standalone', '--installer', 'null'] + command.extend(args) + return certbot_call.certbot_test( + command, self.directory_url, self.http_01_port, self.https_port, + self.config_dir, self.workspace, force_renew=force_renew) + + def get_domain(self, subdomain: str = 'le') -> str: + """ + Generate a certificate domain name suitable for distributed certbot integration tests. + This is a requirement to let the distribution know how to redirect the challenge check + from the ACME server to the relevant pytest-xdist worker. This resolution is done by + appending the pytest worker id to the subdomain, using this pattern: + {subdomain}.{worker_id}.wtf + :param str subdomain: the subdomain to use in the generated domain (default 'le') + :return: the well-formed domain suitable for redirection on + :rtype: str + """ + return '{0}.{1}.wtf'.format(subdomain, self.worker_id) diff --git a/certbot-ci/src/certbot_integration_tests/certbot_tests/test_main.py b/certbot-ci/src/certbot_integration_tests/certbot_tests/test_main.py new file mode 100644 index 00000000000..afd902f5c9d --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/certbot_tests/test_main.py @@ -0,0 +1,1145 @@ +"""Module executing integration tests against certbot core.""" +import json +import os +from os.path import exists +from os.path import join +import re +import shutil +import subprocess +import sys +from typing import Generator + +from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurve +from cryptography.hazmat.primitives.asymmetric.ec import SECP256R1 +from cryptography.hazmat.primitives.asymmetric.ec import SECP384R1 +from cryptography.hazmat.primitives.asymmetric.ec import SECP521R1 +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography import x509 +from cryptography.x509 import ExtensionNotFound +from cryptography.x509 import NameOID +from cryptography.x509 import TLSFeatureType +import pytest + +from certbot_integration_tests.certbot_tests.assertions import assert_cert_count_for_lineage +from certbot_integration_tests.certbot_tests.assertions import assert_elliptic_key +from certbot_integration_tests.certbot_tests.assertions import assert_equals_group_owner +from certbot_integration_tests.certbot_tests.assertions import assert_equals_group_permissions +from certbot_integration_tests.certbot_tests.assertions import assert_equals_world_read_permissions +from certbot_integration_tests.certbot_tests.assertions import assert_hook_execution +from certbot_integration_tests.certbot_tests.assertions import assert_rsa_key +from certbot_integration_tests.certbot_tests.assertions import assert_saved_lineage_option +from certbot_integration_tests.certbot_tests.assertions import assert_saved_deploy_hook +from certbot_integration_tests.certbot_tests.assertions import assert_world_no_permissions +from certbot_integration_tests.certbot_tests.assertions import assert_world_read_permissions +from certbot_integration_tests.certbot_tests.assertions import EVERYBODY_SID +from certbot_integration_tests.certbot_tests.context import IntegrationTestsContext +from certbot_integration_tests.utils import misc, constants + + +@pytest.fixture(name='context') +def test_context(request: pytest.FixtureRequest) -> Generator[IntegrationTestsContext, None, None]: + """Fixture providing the integration test context.""" + # Fixture request is a built-in pytest fixture describing current test request. + integration_test_context = IntegrationTestsContext(request) + try: + yield integration_test_context + finally: + integration_test_context.cleanup() + + +def test_basic_commands(context: IntegrationTestsContext) -> None: + """Test simple commands on Certbot CLI.""" + # TMPDIR env variable is set to workspace for the certbot subprocess. + # So tempdir module will create any temporary files/dirs in workspace, + # and its content can be tested to check correct certbot cleanup. + initial_count_tmpfiles = len(os.listdir(context.workspace)) + + context.certbot(['--help']) + context.certbot(['--help', 'all']) + context.certbot(['--version']) + + with pytest.raises(subprocess.CalledProcessError): + context.certbot(['--csr']) + + new_count_tmpfiles = len(os.listdir(context.workspace)) + assert initial_count_tmpfiles == new_count_tmpfiles + + +def test_hook_dirs_creation(context: IntegrationTestsContext) -> None: + """Test all hooks directory are created during Certbot startup.""" + context.certbot(['register']) + + for hook_dir in misc.list_renewal_hooks_dirs(context.config_dir): + assert os.path.isdir(hook_dir) + + +def test_registration_override(context: IntegrationTestsContext) -> None: + """Test correct register/unregister, and registration override.""" + context.certbot(['register']) + context.certbot(['unregister']) + context.certbot(['register', '--email', 'ex1@domain.org,ex2@domain.org']) + + context.certbot(['update_account', '--email', 'example@domain.org']) + stdout1, _ = context.certbot(['show_account']) + context.certbot(['update_account', '--email', 'ex1@domain.org,ex2@domain.org']) + stdout2, _ = context.certbot(['show_account']) + + assert 'example@domain.org' in stdout1, "New email should be present" + assert 'example@domain.org' not in stdout2, "Old email should not be present" + assert 'ex1@domain.org, ex2@domain.org' in stdout2, "New emails should be present" + + +def test_prepare_plugins(context: IntegrationTestsContext) -> None: + """Test that plugins are correctly instantiated and displayed.""" + stdout, _ = context.certbot(['plugins', '--init', '--prepare']) + + assert 'webroot' in stdout + + +def test_http_01(context: IntegrationTestsContext) -> None: + """Test the HTTP-01 challenge using standalone plugin.""" + certname = context.get_domain('le2') + context.certbot([ + '--domains', certname, '--preferred-challenges', 'http-01', 'run', + '--cert-name', certname, + '--pre-hook', misc.echo('wtf_pre', context.hook_probe), + '--post-hook', misc.echo('wtf_post', context.hook_probe), + '--deploy-hook', misc.echo('deploy', context.hook_probe), + ]) + + assert_hook_execution(context.hook_probe, 'deploy') + assert_saved_deploy_hook(context.config_dir, certname) + assert_saved_lineage_option(context.config_dir, certname, 'key_type', 'ecdsa') + + +@pytest.mark.skipif(sys.platform == 'darwin', + reason='macOS has one IPv4 loopback address by default') +def test_ipv4_address_standalone(context: IntegrationTestsContext) -> None: + """Test the HTTP-01 challenge with an IPv4 address using standalone authenticator. + + While Pebble will offer both HTTP-01 and TLS-ALPN-01 challenges, we will + only select HTTP-01 because TLS-ALPN-01 is not supported by the standalone + authenticator. + + This test relies on proxy.py being able to forward requests for, e.g. `127.0.0.2`, + (`local_ip`) to this test runner. That works on Linux because 127.0.0.0/8 all routes + to localhost. However, on macOS by default only 127.0.0.1 is routed, so this test is + skipped. If you want to run it, configure additional loopback addresses: + for n in $(seq 2 127) ; do sudo ifconfig lo0 alias "127.0.0.${n}" up ; done. + """ + context.certbot([ + 'certonly', '--ip-address', context.local_ip, '--standalone', + ]) + assert_cert_count_for_lineage(context.config_dir, context.local_ip, 1) + + +def test_ipv6_address_standalone(context: IntegrationTestsContext) -> None: + """Test the HTTP-01 challenge with an IPv6 address using standalone authenticator. + + This test relies on some tricks. Pebble is configured to do validations on port 5002. + Since multiple integration tests want to handle validation requests, and may run + concurrently, proxy.py handles HTTP traffic for port 5002 and sends it to the appropriate + integration test runner. However, it binds that port for IPv4 only. That is, + GracefulTCPServer doesn't specify `address_family = AF_INET6` when subclassing + socketserver.TCPServer. + + For IPv4 integration tests (above), we simply assign each integration test runner a unique + IP address under 127.0.0.0/8, and the proxy knows how to route those IP addresses. Pebble's + validation connects to the proxy because all of 127.0.0.0/8 is defined to be loopback. + + However, under IPv6 there is exactly one loopback address, so we can't use the same trick. + Instead, we ensure that this is the only test that cares about IPv6, and bind [::1]:5002 + for IPv6 (via `--http-01-address`). When Pebble reaches out to validate `::1`, it reaches + this test runner rather than the proxy. + + Note: This works because this is the only test case that binds [::1]:5002. If additional + IPv6 tests are added they could conflict. In that case we might try using the + @pytest.mark.xdist_group annotation along with --dist loadgroup. + https://pytest-xdist.readthedocs.io/en/stable/distribution.html + """ + context.certbot([ + 'certonly', '--ip-address', '::1', '--standalone', + '--http-01-address', '::1', + '--http-01-port', str(constants.DEFAULT_HTTP_01_PORT), + ]) + assert_cert_count_for_lineage(context.config_dir, '::1', 1) + + +def test_manual_http_auth(context: IntegrationTestsContext) -> None: + """Test the HTTP-01 challenge using manual plugin.""" + with misc.create_http_server(context.http_01_port) as webroot,\ + misc.manual_http_hooks(webroot) as scripts: + + certname = context.get_domain() + context.certbot([ + 'certonly', '-a', 'manual', '-d', certname, + '--cert-name', certname, + '--manual-auth-hook', scripts[0], + '--manual-cleanup-hook', scripts[1], + '--pre-hook', misc.echo('wtf_pre', context.hook_probe), + '--post-hook', misc.echo('wtf_post', context.hook_probe), + '--deploy-hook', misc.echo('deploy', context.hook_probe), + ]) + + assert_hook_execution(context.hook_probe, 'deploy') + assert_saved_deploy_hook(context.config_dir, certname) + + +def test_manual_dns_auth(context: IntegrationTestsContext) -> None: + """Test the DNS-01 challenge using manual plugin.""" + certname = context.get_domain('dns') + context.certbot([ + '-a', 'manual', '-d', certname, '--preferred-challenges', 'dns', + 'run', '--cert-name', certname, + '--manual-auth-hook', context.manual_dns_auth_hook, + '--manual-cleanup-hook', context.manual_dns_cleanup_hook, + '--pre-hook', misc.echo('wtf_pre', context.hook_probe), + '--post-hook', misc.echo('wtf_post', context.hook_probe), + '--deploy-hook', misc.echo('deploy', context.hook_probe), + ]) + + assert_hook_execution(context.hook_probe, 'deploy') + assert_saved_deploy_hook(context.config_dir, certname) + + context.certbot(['renew', '--cert-name', certname, '--authenticator', 'manual']) + + assert_cert_count_for_lineage(context.config_dir, certname, 2) + + +def test_certonly(context: IntegrationTestsContext) -> None: + """Test the certonly verb on certbot.""" + context.certbot(['certonly', '--cert-name', 'newname', '-d', context.get_domain('newname')]) + + assert_cert_count_for_lineage(context.config_dir, 'newname', 1) + + +def test_certonly_webroot(context: IntegrationTestsContext) -> None: + """Test the certonly verb with webroot plugin""" + with misc.create_http_server(context.http_01_port) as webroot: + certname = context.get_domain('webroot') + context.certbot(['certonly', '-a', 'webroot', '--webroot-path', webroot, '-d', certname]) + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + +@pytest.mark.skipif(sys.platform == 'darwin', + reason='macOS has one IPv4 loopback address by default') +def test_certonly_webroot_ipv4(context: IntegrationTestsContext) -> None: + """Test the HTTP-01 challenge with an IPv4 address using webroot authenticator. + + This test relies on proxy.py being able to forward requests for, e.g. `127.0.0.2`, + (`local_ip`) to this test runner. That works on Linux because 127.0.0.0/8 all routes + to localhost. However, on macOS by default only 127.0.0.1 is routed, so this test is + skipped. If you want to run it, configure additional loopback addresses: + for n in $(seq 2 127) ; do sudo ifconfig lo0 alias "127.0.0.${n}" up ; done. + """ + with misc.create_http_server(context.http_01_port) as webroot: + context.certbot(['certonly', '-a', 'webroot', '--webroot-path', webroot, + '--ip-address', context.local_ip]) + + assert_cert_count_for_lineage(context.config_dir, context.local_ip, 1) + + +def test_auth_and_install_with_csr(context: IntegrationTestsContext) -> None: + """Test certificate issuance and install using an existing CSR.""" + certname = context.get_domain('le3') + key_path = join(context.workspace, 'key.pem') + csr_path = join(context.workspace, 'csr.der') + + misc.generate_csr([certname], key_path, csr_path) + + cert_path = join(context.workspace, 'csr', 'cert.pem') + chain_path = join(context.workspace, 'csr', 'chain.pem') + + context.certbot([ + 'auth', '--csr', csr_path, + '--cert-path', cert_path, + '--chain-path', chain_path + ]) + + print(misc.read_certificate(cert_path)) + print(misc.read_certificate(chain_path)) + + context.certbot([ + '--domains', certname, 'install', + '--cert-path', cert_path, + '--key-path', key_path + ]) + + +def test_renew_files_permissions(context: IntegrationTestsContext) -> None: + """Test proper certificate file permissions upon renewal""" + certname = context.get_domain('renew') + context.certbot(['-d', certname]) + + privkey1 = join(context.config_dir, 'archive', certname, 'privkey1.pem') + privkey2 = join(context.config_dir, 'archive', certname, 'privkey2.pem') + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + assert_world_no_permissions(privkey1) + + context.certbot(['renew']) + + assert_cert_count_for_lineage(context.config_dir, certname, 2) + assert_world_no_permissions(privkey2) + assert_equals_group_owner(privkey1, privkey2) + assert_equals_world_read_permissions(privkey1, privkey2) + assert_equals_group_permissions(privkey1, privkey2) + + +def test_renew_with_hook_scripts(context: IntegrationTestsContext) -> None: + """Test certificate renewal with script hooks.""" + certname = context.get_domain('renew') + context.certbot(['-d', certname]) + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + misc.generate_test_file_hooks(context.config_dir, context.hook_probe) + context.certbot(['renew']) + + assert_cert_count_for_lineage(context.config_dir, certname, 2) + assert_hook_execution(context.hook_probe, 'deploy') + + +def test_renew_files_propagate_permissions(context: IntegrationTestsContext) -> None: + """Test proper certificate renewal with custom permissions propagated on private key.""" + certname = context.get_domain('renew') + context.certbot(['-d', certname]) + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + privkey1 = join(context.config_dir, 'archive', certname, 'privkey1.pem') + privkey2 = join(context.config_dir, 'archive', certname, 'privkey2.pem') + + if os.name != 'nt': + os.chmod(privkey1, 0o444) + else: + import ntsecuritycon # pylint: disable=import-error + import win32security # pylint: disable=import-error + + # Get the current DACL of the private key + security = win32security.GetFileSecurity(privkey1, win32security.DACL_SECURITY_INFORMATION) + dacl = security.GetSecurityDescriptorDacl() + # Create a read permission for Everybody group + everybody = win32security.ConvertStringSidToSid(EVERYBODY_SID) + dacl.AddAccessAllowedAce( + win32security.ACL_REVISION, ntsecuritycon.FILE_GENERIC_READ, everybody + ) + # Apply the updated DACL to the private key + security.SetSecurityDescriptorDacl(1, dacl, 0) + win32security.SetFileSecurity(privkey1, win32security.DACL_SECURITY_INFORMATION, security) + + context.certbot(['renew']) + + assert_cert_count_for_lineage(context.config_dir, certname, 2) + if os.name != 'nt': + # On Linux, read world permissions + all group permissions + # will be copied from the previous private key + assert_world_read_permissions(privkey2) + assert_equals_world_read_permissions(privkey1, privkey2) + assert_equals_group_permissions(privkey1, privkey2) + else: + # On Windows, world will never have any permissions, and + # group permission is irrelevant for this platform + assert_world_no_permissions(privkey2) + + +def test_graceful_renew_it_is_not_time(context: IntegrationTestsContext) -> None: + """Test graceful renew is not done when it is not due time.""" + certname = context.get_domain('renew') + context.certbot(['-d', certname]) + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + context.certbot(['renew', '--deploy-hook', misc.echo('deploy', context.hook_probe)], + force_renew=False) + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + with pytest.raises(AssertionError): + assert_hook_execution(context.hook_probe, 'deploy') + + +def test_graceful_renew_it_is_time(context: IntegrationTestsContext) -> None: + """Test graceful renew is done when it is due time.""" + certname = context.get_domain('renew') + context.certbot(['-d', certname]) + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + with open(join(context.config_dir, 'renewal', '{0}.conf'.format(certname)), 'r') as file: + lines = file.readlines() + lines.insert(4, 'renew_before_expiry = 100 years{0}'.format(os.linesep)) + with open(join(context.config_dir, 'renewal', '{0}.conf'.format(certname)), 'w') as file: + file.writelines(lines) + + context.certbot(['renew', '--deploy-hook', misc.echo('deploy', context.hook_probe)], + force_renew=False) + + assert_cert_count_for_lineage(context.config_dir, certname, 2) + assert_hook_execution(context.hook_probe, 'deploy') + + +def test_renew_when_ari_says_its_time(context: IntegrationTestsContext) -> None: + """Test graceful renew is done when it is due time.""" + certname = context.get_domain('renew') + context.certbot(['-d', certname]) + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + # Tell Pebble to make ARI look urgent + with open(join(context.config_dir, 'live', certname, 'cert.pem'), 'r') as c: + certificate_pem = c.read() + + misc.set_ari_response(certificate_pem, json.dumps({ + 'suggestedWindow': { + 'start': '2020-01-01T00:00:00Z', + 'end': '2020-01-01T00:00:00Z' + } + })) + + # For the renew call only, avoid passing `--server` to the `certbot` command, so + # we fall back on the hardcoded default of `https://acme-v02.api.letsencrypt.org`. + # No requests should be made to that URL because the lineage has a baked-in Pebble + # URL in its config from the issuance earlier in this test case. If there's a bug + # an ARI _is_ called against that URL it will fail because Let's Encrypt doesn't + # know about certificates issued by Pebble. + context.directory_url = None + context.certbot(['renew', '--deploy-hook', misc.echo('deploy', context.hook_probe)], + force_renew=False) + + assert_cert_count_for_lineage(context.config_dir, certname, 2) + assert_hook_execution(context.hook_probe, 'deploy') + + +def test_renew_with_changed_private_key_complexity(context: IntegrationTestsContext) -> None: + """Test proper renew with updated private key complexity.""" + certname = context.get_domain('renew') + context.certbot(['-d', certname, '--key-type', 'rsa', '--rsa-key-size', '4096']) + + key1 = join(context.config_dir, 'archive', certname, 'privkey1.pem') + assert_rsa_key(key1, 4096) + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + context.certbot(['renew']) + + assert_cert_count_for_lineage(context.config_dir, certname, 2) + key2 = join(context.config_dir, 'archive', certname, 'privkey2.pem') + assert_rsa_key(key2, 4096) + + context.certbot(['renew', '--rsa-key-size', '2048']) + + key3 = join(context.config_dir, 'archive', certname, 'privkey3.pem') + assert_rsa_key(key3, 2048) + +def test_certonly_non_default_key_size_kept(context: IntegrationTestsContext) -> None: + """Test that certonly keeps key type but uses default key size when unspecified.""" + + # create rsa 4096 key cert + certname = context.get_domain('renew') + context.certbot([ + 'certonly', + '--cert-name', certname, + '--key-type', 'rsa', '--rsa-key-size', '4096', + '--force-renewal', '-d', certname, + ]) + key1 = join(context.config_dir, "archive", certname, 'privkey1.pem') + assert_rsa_key(key1, 4096) + assert_cert_count_for_lineage(context.config_dir, certname, 1) + assert_saved_lineage_option(context.config_dir, certname, 'key_type', 'rsa') + + # When running non-interactively, if --key-type is unspecified but the default value differs + # to the lineage key type, Certbot should keep the lineage key type. The key size will still + # change to the default value, in order to stay consistent with the behavior of certonly. + context.certbot(['certonly', '--force-renewal', '-d', certname]) + key2 = join(context.config_dir, 'archive', certname, 'privkey2.pem') + assert_rsa_key(key2, 2048) + + +def test_renew_ignoring_directory_hooks(context: IntegrationTestsContext) -> None: + """Test hooks are ignored during renewal with relevant CLI flag.""" + certname = context.get_domain('renew') + context.certbot(['-d', certname]) + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + misc.generate_test_file_hooks(context.config_dir, context.hook_probe) + context.certbot(['renew', '--no-directory-hooks']) + + assert_cert_count_for_lineage(context.config_dir, certname, 2) + with pytest.raises(AssertionError): + assert_hook_execution(context.hook_probe, 'deploy') + + +def test_renew_empty_hook_scripts(context: IntegrationTestsContext) -> None: + """Test proper renew with empty hook scripts.""" + certname = context.get_domain('renew') + context.certbot(['-d', certname]) + + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + misc.generate_test_file_hooks(context.config_dir, context.hook_probe) + for hook_dir in misc.list_renewal_hooks_dirs(context.config_dir): + shutil.rmtree(hook_dir) + os.makedirs(join(hook_dir, 'dir')) + with open(join(hook_dir, 'file'), 'w'): + pass + context.certbot(['renew']) + + assert_cert_count_for_lineage(context.config_dir, certname, 2) + + +def test_renew_hook_override(context: IntegrationTestsContext) -> None: + """Test correct hook override on renew.""" + certname = context.get_domain('override') + context.certbot([ + 'certonly', '-d', certname, + '--preferred-challenges', 'http-01', + '--pre-hook', misc.echo('pre', context.hook_probe), + '--post-hook', misc.echo('post', context.hook_probe), + '--deploy-hook', misc.echo('deploy', context.hook_probe), + ]) + + assert_hook_execution(context.hook_probe, 'pre') + assert_hook_execution(context.hook_probe, 'post') + assert_hook_execution(context.hook_probe, 'deploy') + + # Now we override all previous hooks during next renew. + with open(context.hook_probe, 'w'): + pass + context.certbot([ + 'renew', '--cert-name', certname, + '--pre-hook', misc.echo('pre_override', context.hook_probe), + '--post-hook', misc.echo('post_override', context.hook_probe), + '--deploy-hook', misc.echo('deploy_override', context.hook_probe), + ]) + + assert_hook_execution(context.hook_probe, 'pre_override') + assert_hook_execution(context.hook_probe, 'post_override') + assert_hook_execution(context.hook_probe, 'deploy_override') + with pytest.raises(AssertionError): + assert_hook_execution(context.hook_probe, 'pre') + with pytest.raises(AssertionError): + assert_hook_execution(context.hook_probe, 'post') + with pytest.raises(AssertionError): + assert_hook_execution(context.hook_probe, 'deploy') + + # Expect that this renew will reuse new hooks registered in the previous renew. + with open(context.hook_probe, 'w'): + pass + context.certbot(['renew', '--cert-name', certname]) + + assert_hook_execution(context.hook_probe, 'pre_override') + assert_hook_execution(context.hook_probe, 'post_override') + assert_hook_execution(context.hook_probe, 'deploy_override') + + +def test_renew_hook_env_vars(context: IntegrationTestsContext) -> None: + fail_domain = context.get_domain('fail-env') + context.certbot([ + 'certonly', '-d', fail_domain, + '--preferred-challenges', 'http-01' + ]) + + context.certbot([ + 'renew', + '--post-hook', f'printenv RENEWED_DOMAINS >> {context.hook_probe}' + ]) + + assert_hook_execution(context.hook_probe, fail_domain) + + # Clear probe + with open(context.hook_probe, 'w'): + pass + + # now renew using manual dns, which will fail on renew + # manual_dns_auth_hook from misc is designed to fail if the domain contains 'fail-*'. + with pytest.raises(subprocess.CalledProcessError): + context.certbot([ + 'renew', '--cert-name', fail_domain, + '--preferred-challenges', 'dns', + '--manual-auth-hook', context.manual_dns_auth_hook, + '--manual-cleanup-hook', context.manual_dns_cleanup_hook, + '-a', 'manual', + '--post-hook', f'printenv FAILED_DOMAINS >> {context.hook_probe}', + '--dry-run', # use dry run here to deactivate previous authz, or this will pass + ]) + + assert_hook_execution(context.hook_probe, fail_domain) + + +def test_invalid_domain_with_dns_challenge(context: IntegrationTestsContext) -> None: + """Test certificate issuance failure with DNS-01 challenge.""" + # Manual dns auth hooks from misc are designed to fail if the domain contains 'fail-*'. + domains = ','.join([context.get_domain('dns1'), context.get_domain('fail-dns1')]) + context.certbot([ + '-a', 'manual', '-d', domains, + '--allow-subset-of-names', + '--preferred-challenges', 'dns', + '--manual-auth-hook', context.manual_dns_auth_hook, + '--manual-cleanup-hook', context.manual_dns_cleanup_hook + ]) + + stdout, _ = context.certbot(['certificates']) + + assert context.get_domain('fail-dns1') not in stdout + + +def test_reuse_key(context: IntegrationTestsContext) -> None: + """Test various scenarios where a key is reused.""" + certname = context.get_domain('reusekey') + context.certbot(['--domains', certname, '--reuse-key']) + context.certbot(['renew', '--cert-name', certname]) + + with open(join(context.config_dir, 'archive/{0}/privkey1.pem').format(certname), 'r') as file: + privkey1 = file.read() + with open(join(context.config_dir, 'archive/{0}/cert1.pem').format(certname), 'r') as file: + cert1 = file.read() + with open(join(context.config_dir, 'archive/{0}/privkey2.pem').format(certname), 'r') as file: + privkey2 = file.read() + with open(join(context.config_dir, 'archive/{0}/cert2.pem').format(certname), 'r') as file: + cert2 = file.read() + assert privkey1 == privkey2 + + context.certbot(['--cert-name', certname, '--domains', certname, '--force-renewal']) + + with open(join(context.config_dir, 'archive/{0}/privkey3.pem').format(certname), 'r') as file: + privkey3 = file.read() + with open(join(context.config_dir, 'archive/{0}/cert3.pem').format(certname), 'r') as file: + cert3 = file.read() + assert privkey2 != privkey3 + + context.certbot(['--cert-name', certname, '--domains', certname, + '--reuse-key','--force-renewal']) + with open(join(context.config_dir, 'archive/{0}/privkey4.pem').format(certname), 'r') as file: + privkey4 = file.read() + context.certbot(['renew', '--cert-name', certname, '--no-reuse-key', '--force-renewal']) + with open(join(context.config_dir, 'archive/{0}/privkey5.pem').format(certname), 'r') as file: + privkey5 = file.read() + context.certbot(['renew', '--cert-name', certname, '--force-renewal']) + with open(join(context.config_dir, 'archive/{0}/privkey6.pem').format(certname), 'r') as file: + privkey6 = file.read() + + assert privkey3 == privkey4 + assert privkey4 != privkey5 + assert privkey5 != privkey6 + + assert len({cert1, cert2, cert3}) == 3 + + +def test_reuse_key_allow_subset_of_names(context: IntegrationTestsContext) -> None: + """Test that key is reused when allow_subset_of_names is set.""" + certname = context.get_domain('dns1') + domains = ','.join([certname, context.get_domain('fail-dns1')]) + context.certbot([ + '-a', 'manual', '-d', domains, + '--allow-subset-of-names', + '--preferred-challenges', 'dns', + '--manual-auth-hook', context.manual_dns_auth_hook_allow_fail, + '--manual-cleanup-hook', context.manual_dns_cleanup_hook, + '--reuse-key' + ]) + + stdout, _ = context.certbot(['certificates']) + assert context.get_domain('fail-dns1') in stdout + certname = context.get_domain('dns1') + + # manual_dns_auth_hook from misc is designed to fail if the domain contains 'fail-*'. + context.certbot([ + 'reconfigure', + '--cert-name', certname, + '--manual-auth-hook', context.manual_dns_auth_hook, + '-a', 'manual' # needed to override --standalone passed automatically + ]) + + context.certbot(['renew', '--cert-name', certname, '--force-renewal', '-a', 'manual']) + stdout, _ = context.certbot(['certificates']) + assert context.get_domain('fail-dns1') not in stdout + + with open(join(context.config_dir, 'archive/{0}/privkey1.pem').format(certname), 'r') as file: + privkey1 = file.read() + with open(join(context.config_dir, 'archive/{0}/privkey2.pem').format(certname), 'r') as file: + privkey2 = file.read() + assert privkey1 == privkey2 + + +def test_new_key(context: IntegrationTestsContext) -> None: + """Tests --new-key and its interactions with --reuse-key""" + def private_key(generation: int) -> tuple[str, str]: + pk_path = join(context.config_dir, f'archive/{certname}/privkey{generation}.pem') + with open(pk_path, 'r') as file: + return file.read(), pk_path + + certname = context.get_domain('newkey') + + context.certbot(['--domains', certname, '--reuse-key', + '--key-type', 'ecdsa', '--elliptic-curve', 'secp384r1']) + privkey1, _ = private_key(1) + + # renew: --new-key should replace the key, but keep reuse_key and the key type + params + context.certbot(['renew', '--cert-name', certname, '--new-key']) + privkey2, privkey2_path = private_key(2) + assert privkey1 != privkey2 + assert_saved_lineage_option(context.config_dir, certname, 'reuse_key', 'True') + assert_elliptic_key(privkey2_path, SECP384R1) + + # certonly: it should replace the key but the elliptic curve will change + context.certbot(['certonly', '-d', certname, '--reuse-key', '--new-key']) + privkey3, privkey3_path = private_key(3) + assert privkey2 != privkey3 + assert_saved_lineage_option(context.config_dir, certname, 'reuse_key', 'True') + assert_elliptic_key(privkey3_path, SECP256R1) + + # certonly: it should be possible to change the key type and keep reuse_key + context.certbot(['certonly', '-d', certname, '--reuse-key', '--new-key', '--key-type', 'rsa', + '--rsa-key-size', '4096', '--cert-name', certname]) + privkey4, privkey4_path = private_key(4) + assert privkey3 != privkey4 + assert_saved_lineage_option(context.config_dir, certname, 'reuse_key', 'True') + assert_rsa_key(privkey4_path, 4096) + + # certonly: it should not be possible to change a key parameter without --new-key + with pytest.raises(subprocess.CalledProcessError) as error: + context.certbot(['certonly', '-d', certname, '--key-type', 'rsa', '--reuse-key', + '--rsa-key-size', '2048']) + assert 'Unable to change the --rsa-key-size' in error.value.stderr + + # certonly: not specifying --key-type should keep the existing key type (non-interactively). + context.certbot(['certonly', '-d', certname, '--no-reuse-key']) + privkey5, privkey5_path = private_key(5) + assert_rsa_key(privkey5_path, 2048) + assert privkey4 != privkey5 + + +def test_incorrect_key_type(context: IntegrationTestsContext) -> None: + with pytest.raises(subprocess.CalledProcessError): + context.certbot(['--key-type="failwhale"']) + + +def test_ecdsa(context: IntegrationTestsContext) -> None: + """Test issuance for ECDSA CSR based request (legacy supported mode).""" + key_path = join(context.workspace, 'privkey-p384.pem') + csr_path = join(context.workspace, 'csr-p384.der') + cert_path = join(context.workspace, 'cert-p384.pem') + chain_path = join(context.workspace, 'chain-p384.pem') + + misc.generate_csr( + [context.get_domain('ecdsa')], + key_path, csr_path, + key_type=misc.ECDSA_KEY_TYPE + ) + context.certbot([ + 'auth', '--csr', csr_path, '--cert-path', cert_path, + '--chain-path', chain_path, + ]) + + certificate = misc.read_certificate(cert_path) + # Check that the certificate uses SECP384R1 curve + public_key = certificate.public_key() + assert isinstance(public_key, ec.EllipticCurvePublicKey) + assert isinstance(public_key.curve, ec.SECP384R1) + + +def test_default_key_type(context: IntegrationTestsContext) -> None: + """Test default key type is ECDSA""" + certname = context.get_domain('renew') + context.certbot([ + 'certonly', + '--cert-name', certname, '-d', certname + ]) + filename = join(context.config_dir, 'archive/{0}/privkey1.pem').format(certname) + assert_elliptic_key(filename, SECP256R1) + + +def test_default_rsa_size(context: IntegrationTestsContext) -> None: + """test that the RSA key size used when not specifying any is 2048""" + certname = context.get_domain('renew') + context.certbot([ + '--key-type', 'rsa', '--cert-name', certname, '-d', certname + ]) + key1 = join(context.config_dir, 'archive/{0}/privkey1.pem'.format(certname)) + assert_rsa_key(key1, 2048) + + +@pytest.mark.parametrize('curve,curve_cls', [ + # Curve name, Curve class, ACME servers to skip + ('secp256r1', SECP256R1), + ('secp384r1', SECP384R1), + ('secp521r1', SECP521R1)] +) +def test_ecdsa_curves(context: IntegrationTestsContext, curve: str, + curve_cls: type[EllipticCurve]) -> None: + """Test issuance for each supported ECDSA curve""" + domain = context.get_domain('curve') + context.certbot([ + 'certonly', + '--key-type', 'ecdsa', '--elliptic-curve', curve, + '--force-renewal', '-d', domain, + ]) + key = join(context.config_dir, "live", domain, 'privkey.pem') + assert_elliptic_key(key, curve_cls) + + +def test_renew_with_ec_keys(context: IntegrationTestsContext) -> None: + """Test proper renew with updated private key complexity.""" + # create ecdsa 256 key cert + certname = context.get_domain('renew') + context.certbot([ + 'certonly', + '--cert-name', certname, + '--key-type', 'ecdsa', '--elliptic-curve', 'secp256r1', + '--force-renewal', '-d', certname, + ]) + key1 = join(context.config_dir, "archive", certname, 'privkey1.pem') + assert 200 < os.stat(key1).st_size < 250 # ec keys of 256 bits are ~225 bytes + assert_elliptic_key(key1, SECP256R1) + assert_cert_count_for_lineage(context.config_dir, certname, 1) + assert_saved_lineage_option(context.config_dir, certname, 'key_type', 'ecdsa') + + # renew using 384 ecdsa key instead + context.certbot(['renew', '--elliptic-curve', 'secp384r1']) + assert_cert_count_for_lineage(context.config_dir, certname, 2) + key2 = join(context.config_dir, 'archive', certname, 'privkey2.pem') + assert 280 < os.stat(key2).st_size < 320 # ec keys of 384 bits are ~310 bytes + assert_elliptic_key(key2, SECP384R1) + + # When running non-interactively, if --key-type is unspecified, Certbot should keep the + # lineage key type. The curve will still change to the default value, in order to stay + # consistent with the behavior of certonly. + context.certbot(['certonly', '--force-renewal', '-d', certname]) + key3 = join(context.config_dir, 'archive', certname, 'privkey3.pem') + assert 200 < os.stat(key3).st_size < 250 # ec keys of 256 bits are ~225 bytes + assert_elliptic_key(key3, SECP256R1) + + # When running non-interactively, specifying a different --key-type requires user confirmation + # with both --key-type and --cert-name. + with pytest.raises(subprocess.CalledProcessError) as error: + context.certbot(['certonly', '--force-renewal', '-d', certname, + '--key-type', 'rsa']) + assert 'Please provide both --cert-name and --key-type' in error.value.stderr + + context.certbot(['certonly', '--force-renewal', '-d', certname, + '--key-type', 'rsa', '--cert-name', certname]) + key4 = join(context.config_dir, 'archive', certname, 'privkey4.pem') + assert_rsa_key(key4) + + # We expect that the previous behavior of requiring both --cert-name and + # --key-type to be set to not apply to the renew subcommand. + context.certbot(['renew', '--force-renewal', '--key-type', 'ecdsa']) + key5 = join(context.config_dir, 'archive', certname, 'privkey5.pem') + assert 200 < os.stat(key5).st_size < 250 # ec keys of 256 bits are ~225 bytes + assert_elliptic_key(key5, SECP256R1) + + +def test_ocsp_must_staple(context: IntegrationTestsContext) -> None: + """Test that OCSP Must-Staple is correctly set in the generated certificate.""" + certname = context.get_domain('must-staple') + context.certbot(['auth', '--must-staple', '--domains', certname]) + + certificate = misc.read_certificate(join(context.config_dir, + 'live/{0}/cert.pem').format(certname)) + + try: + tls_feature_ext = certificate.extensions.get_extension_for_class(x509.TLSFeature) + assert TLSFeatureType.status_request in list(tls_feature_ext.value) + except ExtensionNotFound: + assert False, "OCSP Must-Staple requires TLS Feature extension" + + +def test_revoke_simple(context: IntegrationTestsContext) -> None: + """Test various scenarios that revokes a certificate.""" + # Default action after revoke is to delete the certificate. + certname = context.get_domain() + cert_path = join(context.config_dir, 'live', certname, 'cert.pem') + context.certbot(['-d', certname]) + context.certbot(['revoke', '--cert-path', cert_path, '--delete-after-revoke']) + + assert not exists(cert_path) + + # Check default deletion is overridden. + certname = context.get_domain('le1') + cert_path = join(context.config_dir, 'live', certname, 'cert.pem') + context.certbot(['-d', certname]) + context.certbot(['revoke', '--cert-path', cert_path, '--no-delete-after-revoke']) + + assert exists(cert_path) + + context.certbot(['delete', '--cert-name', certname]) + + assert not exists(join(context.config_dir, 'archive', certname)) + assert not exists(join(context.config_dir, 'live', certname)) + assert not exists(join(context.config_dir, 'renewal', '{0}.conf'.format(certname))) + + certname = context.get_domain('le2') + key_path = join(context.config_dir, 'live', certname, 'privkey.pem') + cert_path = join(context.config_dir, 'live', certname, 'cert.pem') + context.certbot(['-d', certname]) + context.certbot(['revoke', '--cert-path', cert_path, '--key-path', key_path]) + + +def test_revoke_and_unregister(context: IntegrationTestsContext) -> None: + """Test revoke with a reason then unregister.""" + cert1 = context.get_domain('le1') + cert2 = context.get_domain('le2') + cert3 = context.get_domain('le3') + + cert_path1 = join(context.config_dir, 'live', cert1, 'cert.pem') + key_path2 = join(context.config_dir, 'live', cert2, 'privkey.pem') + cert_path2 = join(context.config_dir, 'live', cert2, 'cert.pem') + + context.certbot(['-d', cert1]) + context.certbot(['-d', cert2]) + context.certbot(['-d', cert3]) + + context.certbot(['revoke', '--cert-path', cert_path1, + '--reason', 'cessationOfOperation']) + context.certbot(['revoke', '--cert-path', cert_path2, '--key-path', key_path2, + '--reason', 'keyCompromise']) + + context.certbot(['unregister']) + + stdout, _ = context.certbot(['certificates']) + + assert cert1 not in stdout + assert cert2 not in stdout + assert cert3 in stdout + + +@pytest.mark.parametrize('curve,curve_cls', [ + ('secp256r1', SECP256R1), + ('secp384r1', SECP384R1), + ('secp521r1', SECP521R1)] +) +def test_revoke_ecdsa_cert_key( + context: IntegrationTestsContext, curve: str, curve_cls: type[EllipticCurve]) -> None: + """Test revoking a certificate """ + cert: str = context.get_domain('curve') + context.certbot([ + 'certonly', + '--key-type', 'ecdsa', '--elliptic-curve', curve, + '-d', cert, + ]) + key = join(context.config_dir, "live", cert, 'privkey.pem') + cert_path = join(context.config_dir, "live", cert, 'cert.pem') + assert_elliptic_key(key, curve_cls) + context.certbot([ + 'revoke', '--cert-path', cert_path, '--key-path', key, + '--no-delete-after-revoke', + ]) + stdout, _ = context.certbot(['certificates']) + assert stdout.count('INVALID: REVOKED') == 1, 'Expected {0} to be REVOKED'.format(cert) + + +@pytest.mark.parametrize('curve,curve_cls', [ + ('secp256r1', SECP256R1), + ('secp384r1', SECP384R1), + ('secp521r1', SECP521R1)] +) +def test_revoke_ecdsa_cert_key_delete( + context: IntegrationTestsContext, curve: str, curve_cls: type[EllipticCurve]) -> None: + """Test revoke and deletion for each supported curve type""" + cert: str = context.get_domain('curve') + context.certbot([ + 'certonly', + '--key-type', 'ecdsa', '--elliptic-curve', curve, + '-d', cert, + ]) + key = join(context.config_dir, "live", cert, 'privkey.pem') + cert_path = join(context.config_dir, "live", cert, 'cert.pem') + assert_elliptic_key(key, curve_cls) + context.certbot([ + 'revoke', '--cert-path', cert_path, '--key-path', key, + '--delete-after-revoke', + ]) + assert not exists(cert_path) + + +def test_revoke_mutual_exclusive_flags(context: IntegrationTestsContext) -> None: + """Test --cert-path and --cert-name cannot be used during revoke.""" + cert = context.get_domain('le1') + context.certbot(['-d', cert]) + with pytest.raises(subprocess.CalledProcessError) as error: + context.certbot([ + 'revoke', '--cert-name', cert, + '--cert-path', join(context.config_dir, 'live', cert, 'fullchain.pem') + ]) + assert 'Exactly one of --cert-path or --cert-name must be specified' in error.value.stderr + + +def test_revoke_multiple_lineages(context: IntegrationTestsContext) -> None: + """Test revoke does not delete certs if multiple lineages share the same dir.""" + cert1 = context.get_domain('le1') + context.certbot(['-d', cert1]) + + assert os.path.isfile(join(context.config_dir, 'renewal', '{0}.conf'.format(cert1))) + + cert2 = context.get_domain('le2') + context.certbot(['-d', cert2]) + + # Copy over renewal configuration of cert1 into renewal configuration of cert2. + with open(join(context.config_dir, 'renewal', '{0}.conf'.format(cert2)), 'r') as file: + data = file.read() + + data = re.sub( + 'archive_dir = .*\n', + 'archive_dir = {0}\n'.format( + join(context.config_dir, 'archive', cert1).replace('\\', '\\\\') + ), data + ) + + with open(join(context.config_dir, 'renewal', '{0}.conf'.format(cert2)), 'w') as file: + file.write(data) + + context.certbot([ + 'revoke', '--cert-path', join(context.config_dir, 'live', cert1, 'cert.pem') + ]) + + with open(join(context.workspace, 'logs', 'letsencrypt.log'), 'r') as f: + assert 'Not deleting revoked certificates due to overlapping archive dirs' in f.read() + + +def test_reconfigure(context: IntegrationTestsContext) -> None: + """Test the reconfigure verb""" + certname = context.get_domain() + context.certbot(['-d', certname]) + conf_path = join(context.config_dir, 'renewal', '{}.conf'.format(certname)) + + with misc.create_http_server(context.http_01_port) as webroot: + context.certbot(['reconfigure', '--cert-name', certname, + '-a', 'webroot', '--webroot-path', webroot]) + with open(conf_path, 'r') as f: + file_contents = f.read() + # Check changed value + assert 'authenticator = webroot' in file_contents, \ + 'Expected authenticator to be changed to webroot in renewal config' + # Check added value + assert f'webroot_path = {webroot}' in file_contents, \ + 'Expected new webroot path to be added to renewal config' + + +def test_wildcard_certificates(context: IntegrationTestsContext) -> None: + """Test wildcard certificate issuance.""" + certname = context.get_domain('wild') + + context.certbot([ + '-a', 'manual', '-d', '*.{0},{0}'.format(certname), + '--preferred-challenge', 'dns', + '--manual-auth-hook', context.manual_dns_auth_hook, + '--manual-cleanup-hook', context.manual_dns_cleanup_hook + ]) + + assert exists(join(context.config_dir, 'live', certname, 'fullchain.pem')) + + +def test_ocsp_status_stale(context: IntegrationTestsContext) -> None: + """Test retrieval of OCSP statuses for staled config""" + sample_data_path = misc.load_sample_data_path(context.workspace) + stdout, _ = context.certbot(['certificates', '--config-dir', sample_data_path]) + + assert stdout.count('TEST_CERT') == 2, ('Did not find two test certs as expected ({0})' + .format(stdout.count('TEST_CERT'))) + assert stdout.count('EXPIRED') == 2, ('Did not find two expired certs as expected ({0})' + .format(stdout.count('EXPIRED'))) + + +def test_ocsp_status_live(context: IntegrationTestsContext) -> None: + """Test retrieval of OCSP statuses for live config""" + cert = context.get_domain('ocsp-check') + + # OSCP 1: Check live certificate OCSP status (VALID) + context.certbot(['--domains', cert]) + stdout, _ = context.certbot(['certificates']) + + assert stdout.count('VALID') == 1, 'Expected {0} to be VALID'.format(cert) + assert stdout.count('EXPIRED') == 0, 'Did not expect {0} to be EXPIRED'.format(cert) + + # OSCP 2: Check live certificate OCSP status (REVOKED) + context.certbot(['revoke', '--cert-name', cert, '--no-delete-after-revoke']) + stdout, _ = context.certbot(['certificates']) + + assert stdout.count('INVALID') == 1, 'Expected {0} to be INVALID'.format(cert) + assert stdout.count('REVOKED') == 1, 'Expected {0} to be REVOKED'.format(cert) + + +def test_ocsp_renew(context: IntegrationTestsContext) -> None: + """Test that revoked certificates are renewed.""" + # Obtain a certificate + certname = context.get_domain('ocsp-renew') + context.certbot(['--domains', certname]) + + # Test that "certbot renew" does not renew the certificate + assert_cert_count_for_lineage(context.config_dir, certname, 1) + context.certbot(['renew'], force_renew=False) + assert_cert_count_for_lineage(context.config_dir, certname, 1) + + # Revoke the certificate and test that it does renew the certificate + context.certbot(['revoke', '--cert-name', certname, '--no-delete-after-revoke']) + context.certbot(['renew'], force_renew=False) + assert_cert_count_for_lineage(context.config_dir, certname, 2) + + +def test_dry_run_deactivate_authzs(context: IntegrationTestsContext) -> None: + """Test that Certbot deactivates authorizations when performing a dry run""" + + name = context.get_domain('dry-run-authz-deactivation') + args = ['certonly', '--cert-name', name, '-d', name, '--dry-run'] + log_line = 'Recreating order after authz deactivation' + + # First order will not need deactivation + context.certbot(args) + with open(join(context.workspace, 'logs', 'letsencrypt.log'), 'r') as f: + assert log_line not in f.read(), 'First order should not have had any authz reuse' + + # Second order will require deactivation + context.certbot(args) + with open(join(context.workspace, 'logs', 'letsencrypt.log'), 'r') as f: + assert log_line in f.read(), 'Second order should have been recreated due to authz reuse' + + +def test_preferred_chain(context: IntegrationTestsContext) -> None: + """Test that --preferred-chain results in the correct chain.pem being produced""" + try: + issuers = misc.get_acme_issuers() + except NotImplementedError: + pytest.skip('This ACME server does not support alternative issuers.') + + names = [str(i.issuer.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value) \ + for i in issuers] + + domain = context.get_domain('preferred-chain') + cert_path = join(context.config_dir, 'live', domain, 'chain.pem') + conf_path = join(context.config_dir, 'renewal', '{}.conf'.format(domain)) + + for (requested, expected) in [(n, n) for n in names] + [('nonexistent', names[0])]: + args = ['certonly', '--cert-name', domain, '-d', domain, + '--preferred-chain', requested, '--force-renewal'] + context.certbot(args) + + certificate = misc.read_certificate(cert_path) + issuer_cn = certificate.issuer.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value + assert expected == issuer_cn, \ + f'Expected chain issuer to be {expected} when preferring {requested}' + + with open(conf_path, 'r') as f: + assert f'preferred_chain = {requested}' in f.read(), \ + 'Expected preferred_chain to be set in renewal config' + + +def test_ancient_rsa_key_type_preserved(context: IntegrationTestsContext) -> None: + certname = context.get_domain('newname') + context.certbot(['certonly', '-d', certname, '--key-type', 'rsa']) + assert_saved_lineage_option(context.config_dir, certname, 'key_type', 'rsa') + + # Remove `key_type = rsa` from the renewal config to emulate a None: + super().__init__(request) + + self.nginx_root = os.path.join(self.workspace, 'nginx') + os.mkdir(self.nginx_root) + + self.webroot = os.path.join(self.nginx_root, 'webroot') + os.mkdir(self.webroot) + with open(os.path.join(self.webroot, 'index.html'), 'w') as file_handler: + file_handler.write('Hello World!') + + self.nginx_config_path = os.path.join(self.nginx_root, 'nginx.conf') + self.nginx_config: str + + default_server = request.param['default_server'] + self.process = self._start_nginx(default_server) + + def cleanup(self) -> None: + self._stop_nginx() + super().cleanup() + + def certbot_test_nginx(self, args: Iterable[str]) -> tuple[str, str]: + """ + Main command to execute certbot using the nginx plugin. + :param list args: list of arguments to pass to nginx + :param bool force_renew: set to False to not renew by default + """ + command = ['--authenticator', 'nginx', '--installer', 'nginx', + '--nginx-server-root', self.nginx_root] + command.extend(args) + return certbot_call.certbot_test( + command, self.directory_url, self.http_01_port, self.https_port, + self.config_dir, self.workspace, force_renew=True) + + def _start_nginx(self, default_server: bool) -> subprocess.Popen[bytes]: + self.nginx_config = config.construct_nginx_config( + self.nginx_root, self.webroot, self.http_01_port, self.https_port, + self.other_port, default_server, wtf_prefix=self.worker_id) + with open(self.nginx_config_path, 'w') as file: + file.write(self.nginx_config) + + # pylint: disable=consider-using-with + process = subprocess.Popen(['nginx', '-c', self.nginx_config_path, '-g', 'daemon off;']) + + assert process.poll() is None + misc.check_until_timeout('http://localhost:{0}'.format(self.http_01_port)) + return process + + def _stop_nginx(self) -> None: + assert self.process.poll() is None + self.process.terminate() + self.process.wait(constants.MAX_SUBPROCESS_WAIT) diff --git a/certbot-ci/src/certbot_integration_tests/nginx_tests/nginx_config.py b/certbot-ci/src/certbot_integration_tests/nginx_tests/nginx_config.py new file mode 100644 index 00000000000..291414c4e10 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/nginx_tests/nginx_config.py @@ -0,0 +1,140 @@ +# -*- coding: utf-8 -*- +"""General purpose nginx test configuration generator.""" +import atexit +import importlib.resources +from contextlib import ExitStack +from typing import Optional + + +def construct_nginx_config(nginx_root: str, nginx_webroot: str, http_port: int, https_port: int, + other_port: int, default_server: bool, key_path: Optional[str] = None, + cert_path: Optional[str] = None, wtf_prefix: str = 'le') -> str: + """ + This method returns a full nginx configuration suitable for integration tests. + :param str nginx_root: nginx root configuration path + :param str nginx_webroot: nginx webroot path + :param int http_port: HTTP port to listen on + :param int https_port: HTTPS port to listen on + :param int other_port: other HTTP port to listen on + :param bool default_server: True to set a default server in nginx config, False otherwise + :param str key_path: the path to a SSL key + :param str cert_path: the path to a SSL certificate + :param str wtf_prefix: the prefix to use in all domains handled by this nginx config + :return: a string containing the full nginx configuration + :rtype: str + """ + if not key_path: + file_manager = ExitStack() + atexit.register(file_manager.close) + ref = (importlib.resources.files('certbot_integration_tests').joinpath('assets') + .joinpath('key.pem')) + key_path = str(file_manager.enter_context(importlib.resources.as_file(ref))) + + if not cert_path: + file_manager = ExitStack() + atexit.register(file_manager.close) + ref = (importlib.resources.files('certbot_integration_tests').joinpath('assets') + .joinpath('cert.pem')) + cert_path = str(file_manager.enter_context(importlib.resources.as_file(ref))) + + return '''\ +# This error log will be written regardless of server scope error_log +# definitions, so we have to set this here in the main scope. +# +# Even doing this, Nginx will still try to create the default error file, and +# log a non-fatal error when it fails. After that things will work, however. +error_log {nginx_root}/error.log; + +# The pidfile will be written to /var/run unless this is set. +pid {nginx_root}/nginx.pid; + +worker_processes 1; + +events {{ + worker_connections 1024; +}} + +# “This comment contains valid Unicodeâ€. + +http {{ + # Set an array of temp, cache and log file options that will otherwise default to + # restricted locations accessible only to root. + client_body_temp_path {nginx_root}/client_body; + fastcgi_temp_path {nginx_root}/fastcgi_temp; + proxy_temp_path {nginx_root}/proxy_temp; + #scgi_temp_path {nginx_root}/scgi_temp; + #uwsgi_temp_path {nginx_root}/uwsgi_temp; + access_log {nginx_root}/error.log; + + # This should be turned off in a Virtualbox VM, as it can cause some + # interesting issues with data corruption in delivered files. + sendfile off; + + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + + #include /etc/nginx/mime.types; + index index.html index.htm index.php; + + log_format main '$remote_addr - $remote_user [$time_local] $status ' + '"$request" $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + default_type application/octet-stream; + + server {{ + # IPv4. + listen {http_port} {default_server}; + # IPv6. + listen [::]:{http_port} {default_server}; + server_name nginx.{wtf_prefix}.wtf nginx2.{wtf_prefix}.wtf; + + root {nginx_webroot}; + + location / {{ + # First attempt to serve request as file, then as directory, then fall + # back to index.html. + try_files $uri $uri/ /index.html; + }} + }} + + server {{ + listen {http_port}; + listen [::]:{http_port}; + server_name nginx3.{wtf_prefix}.wtf; + + root {nginx_webroot}; + + location /.well-known/ {{ + return 404; + }} + + return 301 https://$host$request_uri; + }} + + server {{ + listen {other_port}; + listen [::]:{other_port}; + server_name nginx4.{wtf_prefix}.wtf nginx5.{wtf_prefix}.wtf; + }} + + server {{ + listen {http_port}; + listen [::]:{http_port}; + listen {https_port} ssl; + listen [::]:{https_port} ssl; + if ($scheme != "https") {{ + return 301 https://$host$request_uri; + }} + server_name nginx6.{wtf_prefix}.wtf nginx7.{wtf_prefix}.wtf; + + ssl_certificate {cert_path}; + ssl_certificate_key {key_path}; + }} +}} +'''.format(nginx_root=nginx_root, nginx_webroot=nginx_webroot, + http_port=http_port, https_port=https_port, other_port=other_port, + default_server='default_server' if default_server else '', wtf_prefix=wtf_prefix, + key_path=key_path, cert_path=cert_path) diff --git a/certbot-ci/src/certbot_integration_tests/nginx_tests/test_main.py b/certbot-ci/src/certbot_integration_tests/nginx_tests/test_main.py new file mode 100644 index 00000000000..099d79989b0 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/nginx_tests/test_main.py @@ -0,0 +1,59 @@ +"""Module executing integration tests against certbot with nginx plugin.""" +import os +import ssl +from typing import Generator + +import pytest + +from certbot_integration_tests.nginx_tests.context import IntegrationTestsContext + + +@pytest.fixture(name='context') +def test_context(request: pytest.FixtureRequest) -> Generator[IntegrationTestsContext, None, None]: + # Fixture request is a built-in pytest fixture describing current test request. + integration_test_context = IntegrationTestsContext(request) + try: + yield integration_test_context + finally: + integration_test_context.cleanup() + + +@pytest.mark.parametrize('certname_pattern, params, context', [ + ('nginx.{0}.wtf', ['run'], {'default_server': True}), + ('nginx2.{0}.wtf', ['--preferred-challenges', 'http'], {'default_server': True}), + # Overlapping location block and server-block-level return 301 + ('nginx3.{0}.wtf', ['--preferred-challenges', 'http'], {'default_server': True}), + # No matching server block; default_server exists + ('nginx4.{0}.wtf', ['--preferred-challenges', 'http'], {'default_server': True}), + # No matching server block; default_server does not exist + ('nginx5.{0}.wtf', ['--preferred-challenges', 'http'], {'default_server': False}), + # Multiple domains, mix of matching and not + ('nginx6.{0}.wtf,nginx7.{0}.wtf', [ + '--preferred-challenges', 'http' + ], {'default_server': False}), +], indirect=['context']) +def test_certificate_deployment(certname_pattern: str, params: list[str], + context: IntegrationTestsContext) -> None: + """ + Test various scenarios to deploy a certificate to nginx using certbot. + """ + domains = certname_pattern.format(context.worker_id) + command = ['--domains', domains] + command.extend(params) + context.certbot_test_nginx(command) + + lineage = domains.split(',')[0] + server_cert = ssl.get_server_certificate(('localhost', context.https_port)) + with open(os.path.join( + context.workspace, 'conf/live/{0}/cert.pem'.format(lineage)), 'r' + ) as file: + certbot_cert = file.read() + + assert server_cert == certbot_cert + + context.certbot_test_nginx(['rollback', '--checkpoints', '1']) + + with open(context.nginx_config_path, 'r') as file_h: + current_nginx_config = file_h.read() + + assert context.nginx_config == current_nginx_config diff --git a/certbot-compatibility-test/docs/_templates/.gitignore b/certbot-ci/src/certbot_integration_tests/py.typed similarity index 100% rename from certbot-compatibility-test/docs/_templates/.gitignore rename to certbot-ci/src/certbot_integration_tests/py.typed diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/mime.types b/certbot-ci/src/certbot_integration_tests/rfc2136_tests/__init__.py similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/mime.types rename to certbot-ci/src/certbot_integration_tests/rfc2136_tests/__init__.py diff --git a/certbot-ci/src/certbot_integration_tests/rfc2136_tests/context.py b/certbot-ci/src/certbot_integration_tests/rfc2136_tests/context.py new file mode 100644 index 00000000000..12cb65cc73a --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/rfc2136_tests/context.py @@ -0,0 +1,64 @@ +"""Module to handle the context of RFC2136 integration tests.""" +from contextlib import contextmanager +import importlib.resources +import tempfile +from typing import Generator +from typing import Iterable + +import pytest + +from certbot_integration_tests.certbot_tests import context as certbot_context +from certbot_integration_tests.utils import certbot_call + + +class IntegrationTestsContext(certbot_context.IntegrationTestsContext): + """Integration test context for certbot-dns-rfc2136""" + def __init__(self, request: pytest.FixtureRequest) -> None: + super().__init__(request) + + self.request = request + + if hasattr(request.config, 'workerinput'): # Worker node + self._dns_xdist = request.config.workerinput['dns_xdist'] + else: # Primary node + self._dns_xdist = request.config.dns_xdist # type: ignore[attr-defined] + + def certbot_test_rfc2136(self, args: Iterable[str]) -> tuple[str, str]: + """ + Main command to execute certbot using the RFC2136 DNS authenticator. + :param list args: list of arguments to pass to Certbot + """ + command = ['--authenticator', 'dns-rfc2136', '--dns-rfc2136-propagation-seconds', '2'] + command.extend(args) + return certbot_call.certbot_test( + command, self.directory_url, self.http_01_port, self.https_port, + self.config_dir, self.workspace, force_renew=True) + + @contextmanager + def rfc2136_credentials(self, label: str = 'default') -> Generator[str, None, None]: + """ + Produces the contents of a certbot-dns-rfc2136 credentials file. + :param str label: which RFC2136 credential to use + :yields: Path to credentials file + :rtype: str + """ + src_ref_file = (importlib.resources.files('certbot_integration_tests').joinpath('assets') + .joinpath('bind-config').joinpath(f'rfc2136-credentials-{label}.ini.tpl')) + with importlib.resources.as_file(src_ref_file) as src_file: + with open(src_file, 'r') as f: + contents = f.read().format( + server_address=self._dns_xdist['address'], + server_port=self._dns_xdist['port'] + ) + + with tempfile.NamedTemporaryFile('w+', prefix='rfc2136-creds-{}'.format(label), + suffix='.ini', dir=self.workspace) as fp: + fp.write(contents) + fp.flush() + yield fp.name + + def skip_if_no_bind9_server(self) -> None: + """Skips the test if there was no RFC2136-capable DNS server configured + in the test environment""" + if not self._dns_xdist: + pytest.skip('No RFC2136-capable DNS server is configured') diff --git a/certbot-ci/src/certbot_integration_tests/rfc2136_tests/test_main.py b/certbot-ci/src/certbot_integration_tests/rfc2136_tests/test_main.py new file mode 100644 index 00000000000..9466934a8f0 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/rfc2136_tests/test_main.py @@ -0,0 +1,28 @@ +"""Module executing integration tests against Certbot with the RFC2136 DNS authenticator.""" +from typing import Generator + +import pytest + +from certbot_integration_tests.rfc2136_tests.context import IntegrationTestsContext + + +@pytest.fixture(name="context") +def test_context(request: pytest.FixtureRequest) -> Generator[IntegrationTestsContext, None, None]: + # pylint: disable=missing-function-docstring + # Fixture request is a built-in pytest fixture describing current test request. + integration_test_context = IntegrationTestsContext(request) + try: + yield integration_test_context + finally: + integration_test_context.cleanup() + + +@pytest.mark.parametrize('domain', [('example.com'), ('sub.example.com')]) +def test_get_certificate(domain: str, context: IntegrationTestsContext) -> None: + context.skip_if_no_bind9_server() + + with context.rfc2136_credentials() as creds: + context.certbot_test_rfc2136([ + 'certonly', '--dns-rfc2136-credentials', creds, + '-d', domain, '-d', '*.{}'.format(domain) + ]) diff --git a/certbot-nginx/docs/_static/.gitignore b/certbot-ci/src/certbot_integration_tests/utils/__init__.py similarity index 100% rename from certbot-nginx/docs/_static/.gitignore rename to certbot-ci/src/certbot_integration_tests/utils/__init__.py diff --git a/certbot-ci/src/certbot_integration_tests/utils/acme_server.py b/certbot-ci/src/certbot_integration_tests/utils/acme_server.py new file mode 100755 index 00000000000..ee824c00b99 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/utils/acme_server.py @@ -0,0 +1,229 @@ +#!/usr/bin/env python +"""Module to setup an ACME CA server environment able to run multiple tests in parallel""" + +import argparse +import errno +import json +import os +import shutil +import subprocess +import sys +import tempfile +import time +from types import TracebackType +from typing import Any +from typing import cast +from typing import Mapping +from typing import Optional + +# pylint: disable=wildcard-import,unused-wildcard-import +from certbot_integration_tests.utils import misc +from certbot_integration_tests.utils import pebble_artifacts +from certbot_integration_tests.utils import pebble_ocsp_server +from certbot_integration_tests.utils import proxy +from certbot_integration_tests.utils.constants import DEFAULT_HTTP_01_PORT +from certbot_integration_tests.utils.constants import CHALLTESTSRV_PORT +from certbot_integration_tests.utils.constants import PEBBLE_DIRECTORY_URL +from certbot_integration_tests.utils.constants import PEBBLE_CHALLTESTSRV_URL +from certbot_integration_tests.utils.constants import PEBBLE_ALTERNATE_ROOTS +from certbot_integration_tests.utils.constants import MAX_SUBPROCESS_WAIT + + +class ACMEServer: + """ + ACMEServer configures and handles the lifecycle of an ACME CA server and an HTTP reverse proxy + instance, to allow parallel execution of integration tests against the unique http-01 port + expected by the ACME CA server. + Typically all pytest integration tests will be executed in this context. + ACMEServer gives access the acme_xdist parameter, listing the ports and directory url to use + for each pytest node. It exposes also start and stop methods in order to start the stack, and + stop it with proper resources cleanup. + ACMEServer is also a context manager, and so can be used to ensure ACME server is + started/stopped upon context enter/exit. + """ + def __init__(self, nodes: list[str], http_proxy: bool = True, + stdout: bool = False, dns_server: Optional[str] = None, + http_01_port: Optional[int] = None) -> None: + """ + Create an ACMEServer instance. + :param list nodes: list of node names that will be setup by pytest xdist + :param bool http_proxy: if False do not start the HTTP proxy + :param bool stdout: if True stream all subprocesses stdout to standard stdout + :param str dns_server: if set, Pebble will use it to resolve domains + :param int http_01_port: port to use for http-01 validation; currently + only supported for pebble without an HTTP proxy + """ + self._construct_acme_xdist(nodes) + + self._proxy = http_proxy + self._workspace = tempfile.mkdtemp() + self._processes: list[subprocess.Popen[bytes]] = [] + self._stdout = sys.stdout if stdout else open(os.devnull, 'w') # pylint: disable=consider-using-with + self._dns_server = dns_server + self._http_01_port = DEFAULT_HTTP_01_PORT + if http_01_port: + if self._proxy: + raise ValueError('Setting http_01_port is not currently supported when ' + 'using the HTTP proxy') + self._http_01_port = http_01_port + + def start(self) -> None: + """Start the test stack""" + try: + if self._proxy: + self._prepare_http_proxy() + self._prepare_pebble_server() + except BaseException as e: + self.stop() + raise e + + def stop(self) -> None: + """Stop the test stack, and clean its resources""" + print('=> Tear down the test infrastructure...') + try: + for process in self._processes: + try: + process.terminate() + except OSError as e: + # Process may be not started yet, so no PID and terminate fails. + # Then the process never started, and the situation is acceptable. + if e.errno != errno.ESRCH: + raise + for process in self._processes: + process.wait(MAX_SUBPROCESS_WAIT) + finally: + if os.path.exists(self._workspace): + shutil.rmtree(self._workspace) + if self._stdout != sys.stdout: + self._stdout.close() + print('=> Test infrastructure stopped and cleaned up.') + + def __enter__(self) -> dict[str, Any]: + self.start() + return self.acme_xdist + + def __exit__(self, exc_type: Optional[type[BaseException]], exc: Optional[BaseException], + traceback: Optional[TracebackType]) -> None: + self.stop() + + def _construct_acme_xdist(self, nodes: list[str]) -> None: + """Generate and return the acme_xdist dict""" + acme_xdist: dict[str, Any] = {} + + # Directory and ACME port are set implicitly in the docker-compose.yml + # files of Pebble. + acme_xdist['directory_url'] = PEBBLE_DIRECTORY_URL + acme_xdist['challtestsrv_url'] = PEBBLE_CHALLTESTSRV_URL + acme_xdist['http_port'] = dict(zip(nodes, range(5200, 5200 + len(nodes)))) + # When testing the standalone plugin with IP address certificates, we need a way for + # the proxy to map incoming requests to workers. Since on Linux all 127.* addresses are + # loopback, we give each worker a 127.0.0.n address. The proxy will route all requests for + # that IP address to the http_port assigned for that worker. + acme_xdist['local_ip'] = {} + for i, node in enumerate(nodes): + acme_xdist['local_ip'][node] = f"127.0.0.{i+2}" + acme_xdist['https_port'] = dict(zip(nodes, range(5100, 5100 + len(nodes)))) + acme_xdist['other_port'] = dict(zip(nodes, range(5300, 5300 + len(nodes)))) + + self.acme_xdist = acme_xdist + + def _prepare_pebble_server(self) -> None: + """Configure and launch the Pebble server""" + print('=> Starting pebble instance deployment...') + pebble_artifacts_rv = pebble_artifacts.fetch(self._workspace, self._http_01_port) + pebble_path, challtestsrv_path, pebble_config_path = pebble_artifacts_rv + + # Configure Pebble at full speed (PEBBLE_VA_NOSLEEP=1) and not randomly refusing valid + # nonce (PEBBLE_WFE_NONCEREJECT=0) to have a stable test environment. + environ = os.environ.copy() + environ['PEBBLE_VA_NOSLEEP'] = '1' + environ['PEBBLE_WFE_NONCEREJECT'] = '0' + environ['PEBBLE_AUTHZREUSE'] = '100' + environ['PEBBLE_ALTERNATE_ROOTS'] = str(PEBBLE_ALTERNATE_ROOTS) + + if self._dns_server: + dns_server = self._dns_server + else: + dns_server = '127.0.0.1:8053' + self._launch_process( + [challtestsrv_path, '-management', ':{0}'.format(CHALLTESTSRV_PORT), + '-defaultIPv6', '""', '-defaultIPv4', '127.0.0.1', '-http01', '""', + '-tlsalpn01', '""', '-https01', '""']) + + self._launch_process( + [pebble_path, '-config', pebble_config_path, '-dnsserver', dns_server, '-strict'], + env=environ) + + self._launch_process([sys.executable, pebble_ocsp_server.__file__]) + + # Wait for the ACME CA server to be up. + print('=> Waiting for pebble instance to respond...') + misc.check_until_timeout(self.acme_xdist['directory_url']) + + print('=> Finished pebble instance deployment.') + + def _prepare_http_proxy(self) -> None: + """Configure and launch an HTTP proxy""" + print(f'=> Configuring the HTTP proxy on port {self._http_01_port}...') + http_port_map = cast(dict[str, int], self.acme_xdist['http_port']) + mapping = {r'.+\.{0}\.wtf'.format(node): 'http://127.0.0.1:{0}'.format(port) + for node, port in http_port_map.items()} + + # Each worker gets a specific loopback IP address. All traffic to that IP address gets + # routed to that worker's http_port. + local_ip_map = cast(dict[str, str], self.acme_xdist['local_ip']) + for node, local_ip in local_ip_map.items(): + mapping[r'{0}:.+'.format(local_ip)] = \ + 'http://{0}:{1}'.format(local_ip, http_port_map[node]) + + command = [sys.executable, proxy.__file__, str(self._http_01_port), json.dumps(mapping)] + self._launch_process(command) + print('=> Finished configuring the HTTP proxy.') + + def _launch_process(self, command: list[str], cwd: str = os.getcwd(), + env: Optional[Mapping[str, str]] = None, + force_stderr: bool = False) -> subprocess.Popen[bytes]: + """Launch silently a subprocess OS command""" + if not env: + env = os.environ + stdout = sys.stderr if force_stderr else self._stdout + # pylint: disable=consider-using-with + process = subprocess.Popen( + command, stdout=stdout, stderr=subprocess.STDOUT, cwd=cwd, env=env + ) + self._processes.append(process) + return process + + +def main() -> None: + # pylint: disable=missing-function-docstring + parser = argparse.ArgumentParser( + description='CLI tool to start a local instance of Pebble CA server.') + parser.add_argument('--dns-server', '-d', + help='specify the DNS server as `IP:PORT` to use by ' + 'Pebble; if not specified, a local mock DNS server will be used to ' + 'resolve domains to localhost.') + parser.add_argument('--http-01-port', type=int, default=DEFAULT_HTTP_01_PORT, + help='specify the port to use for http-01 validation; ' + 'this is currently only supported for Pebble.') + args = parser.parse_args() + + acme_server = ACMEServer( + [], http_proxy=False, stdout=True, dns_server=args.dns_server, + http_01_port=args.http_01_port, + ) + + try: + with acme_server as acme_xdist: + print('--> Instance of {0} is running, directory URL is {0}' + .format(acme_xdist['directory_url'])) + print('--> Press CTRL+C to stop the ACME server.') + + while True: + time.sleep(3600) + except KeyboardInterrupt: + pass + + +if __name__ == '__main__': + main() diff --git a/certbot-ci/src/certbot_integration_tests/utils/certbot_call.py b/certbot-ci/src/certbot_integration_tests/utils/certbot_call.py new file mode 100755 index 00000000000..a879dd3eff1 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/utils/certbot_call.py @@ -0,0 +1,178 @@ +#!/usr/bin/env python +"""Module to call certbot in test mode""" + +import configparser +import os +import subprocess +import sys +from typing import Optional + +import certbot_integration_tests +from certbot_integration_tests.utils.constants import DEFAULT_HTTP_01_PORT +from certbot_integration_tests.utils.constants import HTTPS_PORT +from certbot_integration_tests.utils.constants import PEBBLE_DIRECTORY_URL + + +def certbot_test(certbot_args: list[str], directory_url: Optional[str], http_01_port: int, + https_port: int, config_dir: str, workspace: str, + force_renew: bool = True) -> tuple[str, str]: + """ + Invoke the certbot executable available in PATH in a test context for the given args. + The test context consists in running certbot in debug mode, with various flags suitable + for tests (eg. no ssl check, customizable ACME challenge ports and config directory ...). + This command captures both stdout and stderr and returns it to the caller. + :param list certbot_args: the arguments to pass to the certbot executable + :param str directory_url: URL of the ACME directory server to use + :param int http_01_port: port for the HTTP-01 challenges + :param int https_port: port Nginx expects will serve HTTPS + :param str config_dir: certbot configuration directory to use + :param str workspace: certbot current directory to use + :param bool force_renew: set False to not force renew existing certificates (default: True) + :return: stdout and stderr as strings + :rtype: `tuple` of `str` + """ + command, env = _prepare_args_env(certbot_args, directory_url, http_01_port, https_port, + config_dir, workspace, force_renew) + + proc = subprocess.run(command, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, check=False, universal_newlines=True, + cwd=workspace, env=env) + print('--> Certbot log output was:') + print(proc.stderr) + proc.check_returncode() + return proc.stdout, proc.stderr + + +def _prepare_environ(workspace: str) -> dict[str, str]: + # pylint: disable=missing-function-docstring + + new_environ = os.environ.copy() + new_environ['TMPDIR'] = workspace + + # So, pytest is nice, and a little too nice for our usage. + # In order to help user to call seamlessly any piece of python code without requiring to + # install it as a full-fledged Python package for instance, it may inject the path + # to the test files into the PYTHONPATH. This allows the python interpreter to import + # as modules any python file available at this path. + # See https://docs.pytest.org/en/3.2.5/pythonpath.html for the explanation and description. + # However this behavior is not good in integration tests, in particular the nginx oldest ones. + # Indeed during these kind of tests certbot is installed as a transitive dependency to + # certbot-nginx. Here is the trick: this certbot version is not necessarily the same as + # the certbot codebase lying in current working directory. For instance in oldest tests + # certbot==0.36.0 may be installed while the codebase corresponds to certbot==0.37.0.dev0. + # Then during a pytest run, PYTHONPATH contains the path to the Certbot codebase, so invoking + # certbot will import the modules from the codebase (0.37.0.dev0), not from the + # required/installed version (0.36.0). + # This will lead to funny and totally incomprehensible errors. To avoid that, we ensure that + # if PYTHONPATH is set, it does not contain the path to the root of the codebase. + if new_environ.get('PYTHONPATH'): + # certbot_integration_tests.__file__ is: + # '/path/to/certbot/certbot-ci/src/certbot_integration_tests/__init__.pyc' + # ... and we want '/path/to/certbot' + certbot_root = os.path.dirname(os.path.dirname( + os.path.dirname(certbot_integration_tests.__file__)) + ) + python_paths = [ + path for path in new_environ['PYTHONPATH'].split(':') + if path != certbot_root + ] + new_environ['PYTHONPATH'] = ':'.join(python_paths) + + # Pytest finds its ini file by doing the following: + # Determine the common ancestor directory for the specified args that are recognised as paths + # that exist in the file system. If no such paths are found, the common ancestor directory is + # set to the current working directory. + # + # Look for pytest.toml, .pytest.toml, pytest.ini, .pytest.ini, pyproject.toml, tox.ini, and + # setup.cfg files in the ancestor directory and upwards. If one is matched, it becomes the + # configfile and its directory becomes the rootdir. + # source: https://docs.pytest.org/en/stable/reference/customize.html#finding-the-rootdir + # + # Certbot's is located in its root directory. Let's just walk up the tree from this file. + # In case we can't find it, we still do want to error, so add a default. It's fine to have + # error in there twice. Also, ignore the unverified HTTPS request specifically for this call. + warning_filters: list[str]= [ + 'error', + "ignore:Unverified HTTPS request is being made to host 'localhost'", + ] + base_path: str = os.path.realpath(__file__) + while base_path != os.sep: + ini_loc: str = os.path.join(base_path, 'pytest.ini') + if os.path.exists(ini_loc): + ini_config: configparser.ConfigParser = configparser.ConfigParser() + ini_config.read(ini_loc) + if ini_config is not None: + ini_filters = ini_config.get('pytest', 'filterwarnings', fallback='') + warning_filters.extend(ini_filters.split('\n')) + break + else: + base_path = os.path.dirname(base_path) + + new_environ['PYTHONWARNINGS'] = ','.join(warning_filters) + + return new_environ + + +def _prepare_args_env(certbot_args: list[str], directory_url: Optional[str], http_01_port: int, + https_port: int, config_dir: str, workspace: str, + force_renew: bool) -> tuple[list[str], dict[str, str]]: + + new_environ = _prepare_environ(workspace) + additional_args = ['--no-random-sleep-on-renew'] + if force_renew: + additional_args.append('--renew-by-default') + + if directory_url: + additional_args.extend(['--server', directory_url]) + + + command = [ + 'certbot', + '--no-verify-ssl', + '--http-01-port', str(http_01_port), + '--https-port', str(https_port), + '--config-dir', config_dir, + '--work-dir', os.path.join(workspace, 'work'), + '--logs-dir', os.path.join(workspace, 'logs'), + '--non-interactive', + '--no-redirect', + '--agree-tos', + '--register-unsafely-without-email', + '--debug', + '-vv' + ] + + command.extend(certbot_args) + command.extend(additional_args) + + print('--> Invoke command:\n=====\n{0}\n====='.format(subprocess.list2cmdline(command))) + + return command, new_environ + + +def main() -> None: + # pylint: disable=missing-function-docstring + args = sys.argv[1:] + + # Default config is pebble + directory_url = os.environ.get('SERVER', PEBBLE_DIRECTORY_URL) + http_01_port = int(os.environ.get('HTTP_01_PORT', DEFAULT_HTTP_01_PORT)) + https_port = int(os.environ.get('HTTPS_PORT', HTTPS_PORT)) + + # Execution of certbot in a self-contained workspace + workspace = os.environ.get('WORKSPACE', os.path.join(os.getcwd(), '.certbot_test_workspace')) + if not os.path.exists(workspace): + print('--> Creating a workspace for certbot_test: {0}'.format(workspace)) + os.mkdir(workspace) + else: + print('--> Using an existing workspace for certbot_test: {0}'.format(workspace)) + config_dir = os.path.join(workspace, 'conf') + + # Invoke certbot in test mode, without capturing output so users see directly the outcome. + command, env = _prepare_args_env(args, directory_url, http_01_port, https_port, + config_dir, workspace, False) + subprocess.check_call(command, universal_newlines=True, cwd=workspace, env=env) + + +if __name__ == '__main__': + main() diff --git a/certbot-ci/src/certbot_integration_tests/utils/constants.py b/certbot-ci/src/certbot_integration_tests/utils/constants.py new file mode 100644 index 00000000000..a9786181111 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/utils/constants.py @@ -0,0 +1,10 @@ +"""Some useful constants to use throughout certbot-ci integration tests""" +DEFAULT_HTTP_01_PORT = 5002 +HTTPS_PORT = 5001 +CHALLTESTSRV_PORT = 8055 +PEBBLE_DIRECTORY_URL = 'https://localhost:14000/dir' +PEBBLE_MANAGEMENT_URL = 'https://localhost:15000' +PEBBLE_CHALLTESTSRV_URL = f'http://localhost:{CHALLTESTSRV_PORT}' +MOCK_OCSP_SERVER_PORT = 4002 +PEBBLE_ALTERNATE_ROOTS = 2 +MAX_SUBPROCESS_WAIT = 120 diff --git a/certbot-ci/src/certbot_integration_tests/utils/dns_server.py b/certbot-ci/src/certbot_integration_tests/utils/dns_server.py new file mode 100644 index 00000000000..657ce6bd6c5 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/utils/dns_server.py @@ -0,0 +1,161 @@ +#!/usr/bin/env python +"""Module to setup an RFC2136-capable DNS server""" +import importlib.resources +import os +import os.path +import shutil +import socket +import subprocess +import sys +import tempfile +import time +from types import TracebackType +from typing import Any +from typing import Optional + +from certbot_integration_tests.utils import constants + +BIND_DOCKER_IMAGE = "internetsystemsconsortium/bind9:9.20" +BIND_BIND_ADDRESS = ("127.0.0.1", 45953) + +# A TCP DNS message which is a query for '. CH A' transaction ID 0xcb37. This is used +# by _wait_until_ready to check that BIND is responding without depending on dnspython. +BIND_TEST_QUERY = bytearray.fromhex("0011cb37000000010000000000000000010003") + + +class DNSServer: + """ + DNSServer configures and handles the lifetime of an RFC2136-capable server. + DNServer provides access to the dns_xdist parameter, listing the address and port + to use for each pytest node. + + At this time, DNSServer should only be used with a single node, but may be expanded in + future to support parallelization (https://github.com/certbot/certbot/issues/8455). + """ + + def __init__(self, unused_nodes: list[str], show_output: bool = False) -> None: + """ + Create an DNSServer instance. + :param list nodes: list of node names that will be setup by pytest xdist + :param bool show_output: if True, print the output of the DNS server + """ + + self.bind_root = tempfile.mkdtemp() + + self.process: Optional[subprocess.Popen[bytes]] = None + + self.dns_xdist = {"address": BIND_BIND_ADDRESS[0], "port": BIND_BIND_ADDRESS[1]} + + # Unfortunately the BIND9 image forces everything to stderr with -g and we can't + # modify the verbosity. + # pylint: disable=consider-using-with + self._output = sys.stderr if show_output else open(os.devnull, "w") + + def start(self) -> None: + """Start the DNS server""" + try: + self._configure_bind() + self._start_bind() + except: + self.stop() + raise + + def stop(self) -> None: + """Stop the DNS server, and clean its resources""" + if self.process: + try: + self.process.terminate() + self.process.wait(constants.MAX_SUBPROCESS_WAIT) + except BaseException as e: # pylint: disable=broad-except + print("BIND9 did not stop cleanly: {}".format(e), file=sys.stderr) + + shutil.rmtree(self.bind_root, ignore_errors=True) + + if self._output != sys.stderr: + self._output.close() + + def _configure_bind(self) -> None: + """Configure the BIND9 server based on the prebaked configuration""" + ref = importlib.resources.files("certbot_integration_tests") / "assets" / "bind-config" + with importlib.resources.as_file(ref) as path: + for directory in ("conf", "zones"): + shutil.copytree( + os.path.join(path, directory), os.path.join(self.bind_root, directory) + ) + + def _start_bind(self) -> None: + """Launch the BIND9 server as a Docker container""" + addr_str = "{}:{}".format(BIND_BIND_ADDRESS[0], BIND_BIND_ADDRESS[1]) + # pylint: disable=consider-using-with + self.process = subprocess.Popen( + [ + "docker", + "run", + "--rm", + "-p", + "{}:53/udp".format(addr_str), + "-p", + "{}:53/tcp".format(addr_str), + "-v", + "{}/conf:/etc/bind".format(self.bind_root), + "-v", + "{}/zones:/var/lib/bind".format(self.bind_root), + BIND_DOCKER_IMAGE, + ], + stdout=self._output, + stderr=self._output, + ) + + if self.process.poll(): + raise ValueError("BIND9 server stopped unexpectedly") + + try: + self._wait_until_ready() + except: + # The container might be running even if we think it isn't + self.stop() + raise + + def _wait_until_ready(self, attempts: int = 30) -> None: + """ + Polls the DNS server over TCP until it gets a response, or until + it runs out of attempts and raises a ValueError. + The DNS response message must match the txn_id of the DNS query message, + but otherwise the contents are ignored. + :param int attempts: The number of attempts to make. + """ + if not self.process: + raise ValueError("DNS server has not been started. Please run start() first.") + + for _ in range(attempts): + if self.process.poll(): + raise ValueError("BIND9 server stopped unexpectedly") + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(5.0) + try: + sock.connect(BIND_BIND_ADDRESS) + sock.sendall(BIND_TEST_QUERY) + buf = sock.recv(1024) + # We should receive a DNS message with the same tx_id + if buf and len(buf) > 4 and buf[2:4] == BIND_TEST_QUERY[2:4]: + return + # If we got a response but it wasn't the one we wanted, wait a little + time.sleep(1) + except: # pylint: disable=bare-except + # If there was a network error, wait a little + time.sleep(1) + finally: + sock.close() + + raise ValueError( + "Gave up waiting for DNS server {} to respond".format(BIND_BIND_ADDRESS) + ) + + def __start__(self) -> dict[str, Any]: + self.start() + return self.dns_xdist + + def __exit__(self, exc_type: Optional[type[BaseException]], exc: Optional[BaseException], + traceback: Optional[TracebackType]) -> None: + self.stop() diff --git a/certbot-ci/src/certbot_integration_tests/utils/misc.py b/certbot-ci/src/certbot_integration_tests/utils/misc.py new file mode 100644 index 00000000000..e65078c62e0 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/utils/misc.py @@ -0,0 +1,337 @@ +""" +Misc module contains stateless functions that could be used during pytest execution, +or outside during setup/teardown of the integration tests environment. +""" +import atexit +import contextlib +import errno +import functools +import http.server as SimpleHTTPServer +import importlib.resources +import json +import os +import re +import shutil +import socket +import socketserver +import stat +import sys +import tempfile +import threading +import time +from typing import Generator +from typing import Iterable +from typing import Optional +from typing import Union + +from cryptography import x509 +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.hazmat.primitives.asymmetric import rsa +from cryptography.hazmat.primitives.serialization import Encoding +from cryptography.hazmat.primitives.serialization import NoEncryption +from cryptography.hazmat.primitives.serialization import PrivateFormat +from cryptography.x509 import Certificate +from cryptography.x509 import load_pem_x509_certificate + +import requests + +from certbot_integration_tests.utils.constants import PEBBLE_ALTERNATE_ROOTS +from certbot_integration_tests.utils.constants import PEBBLE_MANAGEMENT_URL + +RSA_KEY_TYPE = 'rsa' +ECDSA_KEY_TYPE = 'ecdsa' + + +def _suppress_x509_verification_warnings() -> None: + import urllib3 + urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + + +def check_until_timeout(url: str, attempts: int = 30) -> None: + """ + Wait and block until given url responds with status 200, or raise an exception + after the specified number of attempts. + :param str url: the URL to test + :param int attempts: the number of times to try to connect to the URL + :raise ValueError: exception raised if unable to reach the URL + """ + _suppress_x509_verification_warnings() + for _ in range(attempts): + time.sleep(1) + try: + if requests.get(url, verify=False, timeout=10).status_code == 200: + return + except requests.exceptions.RequestException: + pass + + raise ValueError('Error, url did not respond after {0} attempts: {1}'.format(attempts, url)) + + +class GracefulTCPServer(socketserver.TCPServer): + """ + This subclass of TCPServer allows graceful reuse of an address that has + just been released by another instance of TCPServer. + """ + allow_reuse_address = True + # AF_INET is the default, but make it explicit. We don't want to conflict with + # test_ipv6_address_standalone in the integration tests, which listens on [::1]:5002 + # (that is, AF_INET6). + address_family = socket.AF_INET + + +@contextlib.contextmanager +def create_http_server(port: int) -> Generator[str, None, None]: + """ + Setup and start an HTTP server for the given TCP port. + This server stays active for the lifetime of the context, and is automatically + stopped with context exit, while its temporary webroot is deleted. + :param int port: the TCP port to use + :return str: the temporary webroot attached to this server + """ + with tempfile.TemporaryDirectory() as webroot: + # Setting the directory argument of SimpleHTTPRequestHandler causes + # files to be served from that directory. + handler = functools.partial(SimpleHTTPServer.SimpleHTTPRequestHandler, directory=webroot) + server = GracefulTCPServer(('', port), handler) + thread = threading.Thread(target=server.serve_forever) + thread.start() + try: + check_until_timeout('http://localhost:{0}/'.format(port)) + yield webroot + finally: + server.shutdown() + thread.join() + server.server_close() + + +def list_renewal_hooks_dirs(config_dir: str) -> list[str]: + """ + Find and return paths of all hook directories for the given certbot config directory + :param str config_dir: path to the certbot config directory + :return str[]: list of path to the standard hooks directory for this certbot instance + """ + renewal_hooks_root = os.path.join(config_dir, 'renewal-hooks') + return [os.path.join(renewal_hooks_root, item) for item in ['pre', 'deploy', 'post']] + + +def generate_test_file_hooks(config_dir: str, hook_probe: str) -> None: + """ + Create a suite of certbot hook scripts and put them in the relevant hook directory + for the given certbot configuration directory. These scripts, when executed, will write + specific verbs in the given hook_probe file to allow asserting they have effectively + been executed. The deploy hook also checks that the renewal environment variables are set. + :param str config_dir: current certbot config directory + :param str hook_probe: path to the hook probe to test hook scripts execution + """ + file_manager = contextlib.ExitStack() + atexit.register(file_manager.close) + hook_path_ref = (importlib.resources.files('certbot_integration_tests').joinpath('assets') + .joinpath('hook.py')) + hook_path = str(file_manager.enter_context(importlib.resources.as_file(hook_path_ref))) + + for hook_dir in list_renewal_hooks_dirs(config_dir): + # We want an equivalent of bash `chmod -p $HOOK_DIR, that does not fail if one folder of + # the hierarchy already exists. It is not the case of os.makedirs. Python 3 has an + # optional parameter `exists_ok` to not fail on existing dir, but Python 2.7 does not. + # So we pass through a try except pass for it. To be removed with dropped support on py27. + try: + os.makedirs(hook_dir) + except OSError as error: + if error.errno != errno.EEXIST: + raise + + if os.name != 'nt': + entrypoint_script_path = os.path.join(hook_dir, 'entrypoint.sh') + entrypoint_script = '''\ +#!/usr/bin/env bash +set -e +"{0}" "{1}" "{2}" >> "{3}" +'''.format(sys.executable, hook_path, entrypoint_script_path, hook_probe) + else: + entrypoint_script_path = os.path.join(hook_dir, 'entrypoint.ps1') + entrypoint_script = '''\ +& "{0}" "{1}" "{2}" >> "{3}" + '''.format(sys.executable, hook_path, entrypoint_script_path, hook_probe) + + with open(entrypoint_script_path, 'w') as file_h: + file_h.write(entrypoint_script) + + os.chmod(entrypoint_script_path, os.stat(entrypoint_script_path).st_mode | stat.S_IEXEC) + + +@contextlib.contextmanager +def manual_http_hooks(http_server_root: str) -> Generator[tuple[str, str], None, None]: + """ + Generate suitable http-01 hooks command for test purpose in the given HTTP + server webroot directory. These hooks command use temporary python scripts + that are deleted upon context exit. + :param str http_server_root: path to the HTTP server configured to serve http-01 challenges + :return (str, str): a tuple containing the authentication hook and cleanup hook commands + """ + tempdir = tempfile.mkdtemp() + try: + auth_script_path = os.path.join(tempdir, 'auth.py') + with open(auth_script_path, 'w') as file_h: + file_h.write('''\ +#!/usr/bin/env python +import os +challenge_dir = os.path.join('{0}', '.well-known', 'acme-challenge') +os.makedirs(challenge_dir) +challenge_file = os.path.join(challenge_dir, os.environ.get('CERTBOT_TOKEN')) +with open(challenge_file, 'w') as file_h: + file_h.write(os.environ.get('CERTBOT_VALIDATION')) +'''.format(http_server_root.replace('\\', '\\\\'))) + os.chmod(auth_script_path, 0o755) + + cleanup_script_path = os.path.join(tempdir, 'cleanup.py') + with open(cleanup_script_path, 'w') as file_h: + file_h.write('''\ +#!/usr/bin/env python +import os +import shutil +well_known = os.path.join('{0}', '.well-known') +shutil.rmtree(well_known) +'''.format(http_server_root.replace('\\', '\\\\'))) + os.chmod(cleanup_script_path, 0o755) + + yield ('{0} {1}'.format(sys.executable, auth_script_path), + '{0} {1}'.format(sys.executable, cleanup_script_path)) + finally: + shutil.rmtree(tempdir) + + +def generate_csr( + domains: Iterable[str], key_path: str, csr_path: str, key_type: str = RSA_KEY_TYPE +) -> None: + """ + Generate a private key, and a CSR for the given domains using this key. + :param domains: the domain names to include in the CSR + :type domains: `list` of `str` + :param str key_path: path to the private key that will be generated + :param str csr_path: path to the CSR that will be generated + :param str key_type: type of the key (misc.RSA_KEY_TYPE or misc.ECDSA_KEY_TYPE) + """ + key: Union[rsa.RSAPrivateKey, ec.EllipticCurvePrivateKey] + if key_type == RSA_KEY_TYPE: + key = rsa.generate_private_key(public_exponent=65537, key_size=2048) + elif key_type == ECDSA_KEY_TYPE: + key = ec.generate_private_key(ec.SECP384R1()) + else: + raise ValueError("Invalid key type: {0}".format(key_type)) + + with open(key_path, "wb") as file_h: + file_h.write( + key.private_bytes( + Encoding.PEM, PrivateFormat.TraditionalOpenSSL, NoEncryption() + ) + ) + + csr = ( + x509.CertificateSigningRequestBuilder() + .subject_name(x509.Name([])) + .add_extension( + x509.SubjectAlternativeName([x509.DNSName(d) for d in domains]), + critical=False, + ) + .sign(key, hashes.SHA256()) + ) + + with open(csr_path, "wb") as file_h: + file_h.write(csr.public_bytes(Encoding.DER)) + + +def read_certificate(cert_path: str) -> Certificate: + """ + Load the certificate from the provided path and return the certificate object. + :param str cert_path: the path to the certificate + :returns: a cryptography.x509.Certificate object + """ + with open(cert_path, "rb") as file: + data = file.read() + + return x509.load_pem_x509_certificate(data) + + +def load_sample_data_path(workspace: str) -> str: + """ + Load the certbot configuration example designed to make OCSP tests, and return its path + :param str workspace: current test workspace directory path + :returns: the path to the loaded sample data directory + :rtype: str + """ + original_ref = (importlib.resources.files('certbot_integration_tests').joinpath('assets') + .joinpath('sample-config')) + with importlib.resources.as_file(original_ref) as original: + copied = os.path.join(workspace, 'sample-config') + shutil.copytree(original, copied, symlinks=True) + + if os.name == 'nt': + # Fix the symlinks on Windows if GIT is not configured to create them upon checkout + for lineage in [ + 'a.encryption-example.com', + 'b.encryption-example.com', + 'c.encryption-example.com', + ]: + current_live = os.path.join(copied, 'live', lineage) + for name in os.listdir(current_live): + if name != 'README': + current_file = os.path.join(current_live, name) + if not os.path.islink(current_file): + with open(current_file) as file_h: + src = file_h.read() + os.unlink(current_file) + os.symlink(os.path.join(current_live, src), current_file) + + return copied + + +def echo(keyword: str, path: Optional[str] = None) -> str: + """ + Generate a platform independent executable command + that echoes the given keyword into the given file. + :param keyword: the keyword to echo (must be a single keyword) + :param path: path to the file were keyword is echoed + :return: the executable command + """ + if not re.match(r'^\w+$', keyword): + raise ValueError('Error, keyword `{0}` is not a single keyword.' + .format(keyword)) + return '{0} -c "print(\'{1}\')"{2}'.format( + os.path.basename(sys.executable), keyword, ' >> "{0}"'.format(path) if path else '') + + +def get_acme_issuers() -> list[Certificate]: + """Gets the list of one or more issuer certificates from the ACME server used by the + context. + :param context: the testing context. + :return: the `list of x509.Certificate` representing the list of issuers. + """ + _suppress_x509_verification_warnings() + + issuers = [] + for i in range(PEBBLE_ALTERNATE_ROOTS + 1): + request = requests.get(PEBBLE_MANAGEMENT_URL + '/intermediates/{}'.format(i), + verify=False, + timeout=10) + issuers.append(load_pem_x509_certificate(request.content, default_backend())) + + return issuers + +def set_ari_response(certificate_pem: str, response_json: str) -> None: + """POST to an endpoint on the Pebble server setting the ARI response + for the given certificate.""" + set_renewal_info_body = json.dumps( + { + 'certificate': certificate_pem, + 'ariResponse': response_json, + }) + + _suppress_x509_verification_warnings() + url = PEBBLE_MANAGEMENT_URL + '/set-renewal-info/' + print(f'sending to {url}: {set_renewal_info_body}') + resp = requests.post(url, verify=False, timeout=10, data=set_renewal_info_body) + if resp.status_code != 200: + print(f'setting renewal info: {resp.status_code} {resp.text}') + assert resp.status_code == 200 diff --git a/certbot-ci/src/certbot_integration_tests/utils/pebble_artifacts.py b/certbot-ci/src/certbot_integration_tests/utils/pebble_artifacts.py new file mode 100644 index 00000000000..de99568470e --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/utils/pebble_artifacts.py @@ -0,0 +1,92 @@ +# pylint: disable=missing-module-docstring +import atexit +import importlib.resources +import io +import json +import os +import platform +import stat +import zipfile +from contextlib import ExitStack +from typing import Optional + +import requests + +from certbot_integration_tests.utils.constants import DEFAULT_HTTP_01_PORT +from certbot_integration_tests.utils.constants import MOCK_OCSP_SERVER_PORT + +PEBBLE_VERSION = 'v2.8.0' + + +def fetch(workspace: str, http_01_port: int = DEFAULT_HTTP_01_PORT) -> tuple[str, str, str]: + # pylint: disable=missing-function-docstring + file_manager = ExitStack() + atexit.register(file_manager.close) + pebble_path_ref = importlib.resources.files('certbot_integration_tests') / 'assets' + assets_path = str(file_manager.enter_context(importlib.resources.as_file(pebble_path_ref))) + + pebble_path = _fetch_asset('pebble', assets_path) + challtestsrv_path = _fetch_asset('pebble-challtestsrv', assets_path) + pebble_config_path = _build_pebble_config(workspace, http_01_port, assets_path) + + return pebble_path, challtestsrv_path, pebble_config_path + + +def _fetch_asset(asset: str, assets_path: str) -> str: + base_url = 'https://github.com/letsencrypt/pebble/releases/download' + os_type, architecture = _get_validated_os_and_architecture() + asset_path = os.path.join(assets_path, f'{asset}_{PEBBLE_VERSION}_{os_type}_{architecture}') + if not os.path.exists(asset_path): + asset_url = f'{base_url}/{PEBBLE_VERSION}/{asset}-{os_type}-{architecture}.zip' + response = requests.get(asset_url, timeout=30) + response.raise_for_status() + asset_data = _unzip_asset(response.content, asset) + if asset_data is None: + raise ValueError(f"zipfile {asset_url} didn't contain file {asset}") + with open(asset_path, 'wb') as file_h: + file_h.write(asset_data) + os.chmod(asset_path, os.stat(asset_path).st_mode | stat.S_IEXEC) + + return asset_path + + +def _get_validated_os_and_architecture() -> tuple[str, str]: + os_type = platform.system().lower() + if os_type not in ('darwin', 'linux'): + raise ValueError(f'this code has not been tested on {os_type} systems') + + architecture = platform.machine() + if architecture in ('amd64', 'x86_64'): + architecture = 'amd64' + elif architecture in ('aarch64' 'arm64'): + architecture = 'arm64' + else: + raise ValueError(f'this code has not been tested on {architecture} systems') + + return os_type, architecture + + +def _unzip_asset(zipped_data: bytes, asset_name: str) -> Optional[bytes]: + with zipfile.ZipFile(io.BytesIO(zipped_data)) as zip_file: + for entry in zip_file.filelist: + if not entry.is_dir() and entry.filename.endswith(asset_name): + return zip_file.read(entry) + return None + + +def _build_pebble_config(workspace: str, http_01_port: int, assets_path: str) -> str: + config_path = os.path.join(workspace, 'pebble-config.json') + with open(config_path, 'w') as file_h: + file_h.write(json.dumps({ + 'pebble': { + 'listenAddress': '0.0.0.0:14000', + 'managementListenAddress': '0.0.0.0:15000', + 'certificate': os.path.join(assets_path, 'cert.pem'), + 'privateKey': os.path.join(assets_path, 'key.pem'), + 'httpPort': http_01_port, + 'tlsPort': 5001, + 'ocspResponderURL': 'http://127.0.0.1:{0}'.format(MOCK_OCSP_SERVER_PORT), + }, + })) + + return config_path diff --git a/certbot-ci/src/certbot_integration_tests/utils/pebble_ocsp_server.py b/certbot-ci/src/certbot_integration_tests/utils/pebble_ocsp_server.py new file mode 100755 index 00000000000..83dc96e1954 --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/utils/pebble_ocsp_server.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +""" +This runnable module interfaces itself with the Pebble management interface in order +to serve a mock OCSP responder during integration tests against Pebble. +""" +import datetime +import http.server as BaseHTTPServer +import re +from typing import cast +from typing import Union + +from cryptography import x509 +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePrivateKey +from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey +from cryptography.x509 import ocsp +from dateutil import parser +import requests + +from certbot_integration_tests.utils.constants import MOCK_OCSP_SERVER_PORT +from certbot_integration_tests.utils.constants import PEBBLE_MANAGEMENT_URL +from certbot_integration_tests.utils.misc import GracefulTCPServer + + +class _ProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler): + # pylint: disable=missing-function-docstring + def do_POST(self) -> None: + request = requests.get(PEBBLE_MANAGEMENT_URL + '/intermediate-keys/0', + verify=False, timeout=10) + issuer_key = cast( + Union[RSAPrivateKey, EllipticCurvePrivateKey], + serialization.load_pem_private_key(request.content, None, default_backend())) + + request = requests.get(PEBBLE_MANAGEMENT_URL + '/intermediates/0', + verify=False, timeout=10) + issuer_cert = x509.load_pem_x509_certificate(request.content, default_backend()) + + raw_content_len = self.headers.get('Content-Length') + assert isinstance(raw_content_len, str) + content_len = int(raw_content_len) + + ocsp_request = ocsp.load_der_ocsp_request(self.rfile.read(content_len)) + response = requests.get('{0}/cert-status-by-serial/{1}'.format( + PEBBLE_MANAGEMENT_URL, str(hex(ocsp_request.serial_number)).replace('0x', '')), + verify=False, timeout=10 + ) + + if not response.ok: + ocsp_response = ocsp.OCSPResponseBuilder.build_unsuccessful( + ocsp.OCSPResponseStatus.UNAUTHORIZED + ) + else: + data = response.json() + + now = datetime.datetime.now(datetime.timezone.utc) + cert = x509.load_pem_x509_certificate(data['Certificate'].encode(), default_backend()) + if data['Status'] != 'Revoked': + ocsp_status = ocsp.OCSPCertStatus.GOOD + revocation_time = None + revocation_reason = None + else: + ocsp_status = ocsp.OCSPCertStatus.REVOKED + revocation_reason = x509.ReasonFlags.unspecified + # "... +0000 UTC" => "+0000" + revoked_at = re.sub(r'( \+\d{4}).*$', r'\1', data['RevokedAt']) + revocation_time = parser.parse(revoked_at) + + ocsp_response = ocsp.OCSPResponseBuilder().add_response( + cert=cert, issuer=issuer_cert, algorithm=hashes.SHA1(), + cert_status=ocsp_status, + this_update=now, next_update=now + datetime.timedelta(hours=1), + revocation_time=revocation_time, revocation_reason=revocation_reason + ).responder_id( + ocsp.OCSPResponderEncoding.NAME, issuer_cert + ).sign(issuer_key, hashes.SHA256()) + + self.send_response(200) + self.end_headers() + self.wfile.write(ocsp_response.public_bytes(serialization.Encoding.DER)) + + +if __name__ == '__main__': + try: + GracefulTCPServer(('', MOCK_OCSP_SERVER_PORT), _ProxyHandler).serve_forever() + except KeyboardInterrupt: + pass diff --git a/certbot-ci/src/certbot_integration_tests/utils/proxy.py b/certbot-ci/src/certbot_integration_tests/utils/proxy.py new file mode 100644 index 00000000000..a583938c4db --- /dev/null +++ b/certbot-ci/src/certbot_integration_tests/utils/proxy.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# pylint: disable=missing-module-docstring +# +# Note: If you're having trouble with this module, there was some discussion +# about how it could be ripped out entirely at +# https://github.com/certbot/certbot/pull/10495#discussion_r2699618989 that you +# may want to read. + +import http.server as BaseHTTPServer +import json +import re +import sys +from typing import Mapping + +import requests + +from certbot_integration_tests.utils.misc import GracefulTCPServer + +def _create_proxy(mapping: Mapping[str, str]) -> type[BaseHTTPServer.BaseHTTPRequestHandler]: + # pylint: disable=missing-function-docstring + class ProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler): + # pylint: disable=missing-class-docstring + def do_GET(self) -> None: + headers = {key.lower(): value for key, value in self.headers.items()} + host = headers['host'] + for pattern, backend in mapping.items(): + if re.match(pattern, host): + response = requests.get(backend + self.path, headers=headers, timeout=10) + + self.send_response(response.status_code) + for key, value in response.headers.items(): + self.send_header(key, value) + self.end_headers() + self.wfile.write(response.content) + return + + # We should never hit this if the tests are written correctly, but if we do, this may + # be helpful debugging output. + print(f"proxy.py: do_GET for {host}: No backend") + self.send_response(200, "No backend") + self.end_headers() + self.wfile.write(bytes(f"No backend found for {host}\n", 'utf-8')) + + return ProxyHandler + + +if __name__ == '__main__': + http_port = int(sys.argv[1]) + port_mapping = json.loads(sys.argv[2]) + httpd = GracefulTCPServer(('', http_port), _create_proxy(port_mapping)) + try: + httpd.serve_forever() + except KeyboardInterrupt: + pass diff --git a/certbot-nginx/docs/_templates/.gitignore b/certbot-ci/src/snap_integration_tests/__init__.py similarity index 100% rename from certbot-nginx/docs/_templates/.gitignore rename to certbot-ci/src/snap_integration_tests/__init__.py diff --git a/certbot-ci/src/snap_integration_tests/conftest.py b/certbot-ci/src/snap_integration_tests/conftest.py new file mode 100644 index 00000000000..26314ab0ed8 --- /dev/null +++ b/certbot-ci/src/snap_integration_tests/conftest.py @@ -0,0 +1,48 @@ +# type: ignore +""" +General conftest for pytest execution of all integration tests lying +in the snap_installer_integration tests package. +As stated by pytest documentation, conftest module is used to set on +for a directory a specific configuration using built-in pytest hooks. + +See https://docs.pytest.org/en/latest/reference.html#hook-reference +""" +import glob +import os + + +def pytest_addoption(parser): + """ + Standard pytest hook to add options to the pytest parser. + :param parser: current pytest parser that will be used on the CLI + """ + parser.addoption('--snap-folder', required=True, + help='set the folder path where snaps to test are located') + parser.addoption('--snap-arch', default='amd64', + help='set the architecture do test (default: amd64)') + parser.addoption('--allow-persistent-changes', action='store_true', + help='needs to be set, and confirm that the test will make persistent ' + 'changes on this machine') + + +def pytest_configure(config): + """ + Standard pytest hook used to add a configuration logic for each node of a pytest run. + :param config: the current pytest configuration + """ + if not config.option.allow_persistent_changes: + raise RuntimeError('This integration test would install the Certbot snap on your machine. ' + 'Please run it again with the `--allow-persistent-changes` flag set ' + 'to acknowledge.') + + +def pytest_generate_tests(metafunc): + """ + Generate (multiple) parametrized calls to a test function. + """ + if "dns_snap_path" in metafunc.fixturenames: + snap_arch = metafunc.config.getoption('snap_arch') + snap_folder = metafunc.config.getoption('snap_folder') + snap_dns_path_list = glob.glob(os.path.join(snap_folder, + 'certbot-dns-*_{0}.snap'.format(snap_arch))) + metafunc.parametrize("dns_snap_path", snap_dns_path_list) diff --git a/docs/_static/.gitignore b/certbot-ci/src/snap_integration_tests/dns_tests/__init__.py similarity index 100% rename from docs/_static/.gitignore rename to certbot-ci/src/snap_integration_tests/dns_tests/__init__.py diff --git a/certbot-ci/src/snap_integration_tests/dns_tests/test_main.py b/certbot-ci/src/snap_integration_tests/dns_tests/test_main.py new file mode 100644 index 00000000000..8dc6c4b80cf --- /dev/null +++ b/certbot-ci/src/snap_integration_tests/dns_tests/test_main.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +"""Module executing integration tests against certbot snap.""" +import glob +import os +import re +import subprocess +from typing import Generator + +import pytest + + +@pytest.fixture(autouse=True, scope="module") +def install_certbot_snap(request: pytest.FixtureRequest) -> Generator[None, None, None]: + """Fixture ensuring the certbot snap is installed before each test.""" + with pytest.raises(Exception): + subprocess.check_call(['certbot', '--version']) + try: + snap_folder = request.config.getoption("snap_folder") + snap_arch = request.config.getoption("snap_arch") + snap_path = glob.glob(os.path.join(snap_folder, 'certbot_*_{0}.snap'.format(snap_arch)))[0] + subprocess.check_call(['snap', 'install', '--classic', '--dangerous', snap_path]) + subprocess.check_call(['certbot', '--version']) + yield + finally: + subprocess.call(['snap', 'remove', 'certbot']) + + +def test_dns_plugin_install(dns_snap_path: str) -> None: + """ + Test that each DNS plugin Certbot snap can be installed + and is usable with the Certbot snap. + """ + match = re.match(r'^certbot-(dns-\w+)_.*\.snap$', os.path.basename(dns_snap_path)) + assert match + plugin_name = match.group(1) + snap_name = 'certbot-{0}'.format(plugin_name) + assert plugin_name not in subprocess.check_output(['certbot', 'plugins', '--prepare'], + universal_newlines=True) + + try: + subprocess.check_call(['snap', 'install', '--dangerous', dns_snap_path]) + subprocess.check_call(['snap', 'set', 'certbot', 'trust-plugin-with-root=ok']) + subprocess.check_call(['snap', 'connect', 'certbot:plugin', snap_name]) + + assert plugin_name in subprocess.check_output(['certbot', 'plugins', '--prepare'], + universal_newlines=True) + subprocess.check_call(['snap', 'connect', snap_name + ':certbot-metadata', + 'certbot:certbot-metadata']) + subprocess.check_call(['snap', 'install', '--dangerous', dns_snap_path]) + finally: + subprocess.call(['snap', 'remove', plugin_name]) diff --git a/letshelp-certbot/docs/_static/.gitignore b/certbot-ci/src/snap_integration_tests/py.typed similarity index 100% rename from letshelp-certbot/docs/_static/.gitignore rename to certbot-ci/src/snap_integration_tests/py.typed diff --git a/certbot-compatibility-test/Dockerfile b/certbot-compatibility-test/Dockerfile index cbbdf719351..b9310fe89e1 100644 --- a/certbot-compatibility-test/Dockerfile +++ b/certbot-compatibility-test/Dockerfile @@ -1,51 +1,23 @@ -FROM debian:jessie -MAINTAINER Brad Warren +FROM docker.io/python:3.13-bookworm +LABEL org.opencontainers.image.authors="certbot-dev@eff.org" -# no need to mkdir anything: -# https://docs.docker.com/reference/builder/#copy -# If doesn't exist, it is created along with all missing -# directories in its path. +# This does not include the dependencies needed to build cryptography. See +# https://cryptography.io/en/latest/installation/#building-cryptography-on-linux +RUN apt-get update && \ + apt install python3-venv libaugeas-dev -y -# TODO: Install non-default Python versions for tox. -# TODO: Install Apache/Nginx for plugin development. -COPY letsencrypt-auto-source /opt/certbot/src/letsencrypt-auto-source -RUN /opt/certbot/src/letsencrypt-auto-source/letsencrypt-auto --os-packages-only +WORKDIR /opt/certbot/src -# the above is not likely to change, so by putting it further up the -# Dockerfile we make sure we cache as much as possible +# We copy all contents of the build directory to allow us to easily use +# things like tools/venv.py which expects all of our packages to be available. +COPY . . -COPY setup.py README.rst CHANGELOG.md MANIFEST.in linter_plugin.py tox.cover.py tox.ini .pylintrc /opt/certbot/src/ - -# all above files are necessary for setup.py, however, package source -# code directory has to be copied separately to a subdirectory... -# https://docs.docker.com/reference/builder/#copy: "If is a -# directory, the entire contents of the directory are copied, -# including filesystem metadata. Note: The directory itself is not -# copied, just its contents." Order again matters, three files are far -# more likely to be cached than the whole project directory - -COPY certbot /opt/certbot/src/certbot/ -COPY acme /opt/certbot/src/acme/ -COPY certbot-apache /opt/certbot/src/certbot-apache/ -COPY certbot-nginx /opt/certbot/src/certbot-nginx/ -COPY certbot-compatibility-test /opt/certbot/src/certbot-compatibility-test/ -COPY tools /opt/certbot/src/tools - -RUN virtualenv --no-site-packages -p python2 /opt/certbot/venv && \ - /opt/certbot/venv/bin/pip install -U setuptools && \ - /opt/certbot/venv/bin/pip install -U pip -ENV PATH /opt/certbot/venv/bin:$PATH -RUN /opt/certbot/venv/bin/python \ - /opt/certbot/src/tools/pip_install_editable.py \ - /opt/certbot/src/acme \ - /opt/certbot/src \ - /opt/certbot/src/certbot-apache \ - /opt/certbot/src/certbot-nginx \ - /opt/certbot/src/certbot-compatibility-test +RUN tools/venv.py +ENV PATH=/opt/certbot/src/venv/bin:$PATH # install in editable mode (-e) to save space: it's not possible to -# "rm -rf /opt/certbot/src" (it's stays in the underlaying image); +# "rm -rf /opt/certbot/src" (it's stays in the underlying image); # this might also help in debugging: you can "docker run --entrypoint # bash" and investigate, apply patches, etc. -WORKDIR /opt/certbot/src/certbot-compatibility-test/certbot_compatibility_test/testdata +WORKDIR /opt/certbot/src/certbot-compatibility-test/src/certbot_compatibility_test/testdata diff --git a/certbot-compatibility-test/Dockerfile-apache b/certbot-compatibility-test/Dockerfile-apache index 5c04959669e..cd42e1fdc5d 100644 --- a/certbot-compatibility-test/Dockerfile-apache +++ b/certbot-compatibility-test/Dockerfile-apache @@ -1,5 +1,5 @@ FROM certbot-compatibility-test -MAINTAINER Brad Warren +LABEL org.opencontainers.image.authors="certbot-dev@eff.org" RUN apt-get install apache2 -y diff --git a/certbot-compatibility-test/Dockerfile-nginx b/certbot-compatibility-test/Dockerfile-nginx index 4ade0306545..979609f8a76 100644 --- a/certbot-compatibility-test/Dockerfile-nginx +++ b/certbot-compatibility-test/Dockerfile-nginx @@ -1,5 +1,5 @@ FROM certbot-compatibility-test -MAINTAINER Brad Warren +LABEL org.opencontainers.image.authors="certbot-dev@eff.org" RUN apt-get install nginx -y diff --git a/certbot-compatibility-test/MANIFEST.in b/certbot-compatibility-test/MANIFEST.in index 11762538ad7..911da598e08 100644 --- a/certbot-compatibility-test/MANIFEST.in +++ b/certbot-compatibility-test/MANIFEST.in @@ -1,7 +1,6 @@ include LICENSE.txt include README.rst -recursive-include docs * -include certbot_compatibility_test/configurators/apache/a2enmod.sh -include certbot_compatibility_test/configurators/apache/a2dismod.sh -include certbot_compatibility_test/configurators/apache/Dockerfile -recursive-include certbot_compatibility_test/testdata * +include src/certbot_compatibility_test/configurators/apache/a2enmod.sh +include src/certbot_compatibility_test/configurators/apache/a2dismod.sh +include src/certbot_compatibility_test/configurators/apache/Dockerfile +recursive-include src/certbot_compatibility_test/testdata * diff --git a/certbot-compatibility-test/certbot_compatibility_test/configurators/apache/common.py b/certbot-compatibility-test/certbot_compatibility_test/configurators/apache/common.py deleted file mode 100644 index 82195264b3b..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/configurators/apache/common.py +++ /dev/null @@ -1,110 +0,0 @@ -"""Provides a common base for Apache proxies""" -import os -import shutil -import subprocess - -import mock -import zope.interface - -from certbot import configuration -from certbot import errors as le_errors -from certbot import util as certbot_util -from certbot_apache import entrypoint -from certbot_compatibility_test import errors -from certbot_compatibility_test import interfaces -from certbot_compatibility_test import util -from certbot_compatibility_test.configurators import common as configurators_common - - -@zope.interface.implementer(interfaces.IConfiguratorProxy) -class Proxy(configurators_common.Proxy): - # pylint: disable=too-many-instance-attributes - """A common base for Apache test configurators""" - - def __init__(self, args): - """Initializes the plugin with the given command line args""" - super(Proxy, self).__init__(args) - self.le_config.apache_le_vhost_ext = "-le-ssl.conf" - - self.modules = self.server_root = self.test_conf = self.version = None - patch = mock.patch( - "certbot_apache.configurator.display_ops.select_vhost") - mock_display = patch.start() - mock_display.side_effect = le_errors.PluginError( - "Unable to determine vhost") - - def load_config(self): - """Loads the next configuration for the plugin to test""" - config = super(Proxy, self).load_config() - self._all_names, self._test_names = _get_names(config) - - server_root = _get_server_root(config) - shutil.rmtree("/etc/apache2") - shutil.copytree(server_root, "/etc/apache2", symlinks=True) - - self._prepare_configurator() - - try: - subprocess.check_call("apachectl -k restart".split()) - except errors.Error: - raise errors.Error( - "Apache failed to load {0} before tests started".format( - config)) - - return config - - def _prepare_configurator(self): - """Prepares the Apache plugin for testing""" - for k in entrypoint.ENTRYPOINT.OS_DEFAULTS.keys(): - setattr(self.le_config, "apache_" + k, - entrypoint.ENTRYPOINT.OS_DEFAULTS[k]) - - self._configurator = entrypoint.ENTRYPOINT( - config=configuration.NamespaceConfig(self.le_config), - name="apache") - self._configurator.prepare() - - def cleanup_from_tests(self): - """Performs any necessary cleanup from running plugin tests""" - super(Proxy, self).cleanup_from_tests() - mock.patch.stopall() - - -def _get_server_root(config): - """Returns the server root directory in config""" - subdirs = [ - name for name in os.listdir(config) - if os.path.isdir(os.path.join(config, name))] - - if len(subdirs) != 1: - errors.Error("Malformed configuration directory {0}".format(config)) - - return os.path.join(config, subdirs[0].rstrip()) - - -def _get_names(config): - """Returns all and testable domain names in config""" - all_names = set() - non_ip_names = set() - with open(os.path.join(config, "vhosts")) as f: - for line in f: - # If parsing a specific vhost - if line[0].isspace(): - words = line.split() - if words[0] == "alias": - all_names.add(words[1]) - non_ip_names.add(words[1]) - # If for port 80 and not IP vhost - elif words[1] == "80" and not util.IP_REGEX.match(words[3]): - all_names.add(words[3]) - non_ip_names.add(words[3]) - elif "NameVirtualHost" not in line: - words = line.split() - if (words[0].endswith("*") or words[0].endswith("80") and - not util.IP_REGEX.match(words[1]) and - words[1].find(".") != -1): - all_names.add(words[1]) - return ( - certbot_util.get_filtered_names(all_names), - certbot_util.get_filtered_names(non_ip_names) - ) diff --git a/certbot-compatibility-test/certbot_compatibility_test/configurators/common.py b/certbot-compatibility-test/certbot_compatibility_test/configurators/common.py deleted file mode 100644 index 2a800c1c2cf..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/configurators/common.py +++ /dev/null @@ -1,100 +0,0 @@ -"""Provides a common base for configurator proxies""" -import logging -import os -import shutil -import tempfile - -from certbot import constants -from certbot_compatibility_test import errors -from certbot_compatibility_test import util - - -logger = logging.getLogger(__name__) - - -class Proxy(object): - # pylint: disable=too-many-instance-attributes - """A common base for compatibility test configurators""" - - @classmethod - def add_parser_arguments(cls, parser): - """Adds command line arguments needed by the plugin""" - - def __init__(self, args): - """Initializes the plugin with the given command line args""" - self._temp_dir = tempfile.mkdtemp() - self.le_config = util.create_le_config(self._temp_dir) - config_dir = util.extract_configs(args.configs, self._temp_dir) - self._configs = [ - os.path.join(config_dir, config) - for config in os.listdir(config_dir)] - - self.args = args - self.http_port = 80 - self.https_port = 443 - self._configurator = self._all_names = self._test_names = None - - def __getattr__(self, name): - """Wraps the configurator methods""" - if self._configurator is None: - raise AttributeError() - - method = getattr(self._configurator, name, None) - if callable(method): - return method - else: - raise AttributeError() - - def has_more_configs(self): - """Returns true if there are more configs to test""" - return bool(self._configs) - - def cleanup_from_tests(self): - """Performs any necessary cleanup from running plugin tests""" - - def load_config(self): - """Returns the next config directory to be tested""" - shutil.rmtree(self.le_config.work_dir, ignore_errors=True) - backup = os.path.join(self.le_config.work_dir, constants.BACKUP_DIR) - os.makedirs(backup) - return self._configs.pop() - - def copy_certs_and_keys(self, cert_path, key_path, chain_path=None): - """Copies certs and keys into the temporary directory""" - cert_and_key_dir = os.path.join(self._temp_dir, "certs_and_keys") - if not os.path.isdir(cert_and_key_dir): - os.mkdir(cert_and_key_dir) - - cert = os.path.join(cert_and_key_dir, "cert") - shutil.copy(cert_path, cert) - key = os.path.join(cert_and_key_dir, "key") - shutil.copy(key_path, key) - if chain_path: - chain = os.path.join(cert_and_key_dir, "chain") - shutil.copy(chain_path, chain) - else: - chain = None - - return cert, key, chain - - def get_all_names_answer(self): - """Returns the set of domain names that the plugin should find""" - if self._all_names: - return self._all_names - else: - raise errors.Error("No configuration file loaded") - - def get_testable_domain_names(self): - """Returns the set of domain names that can be tested against""" - if self._test_names: - return self._test_names - else: - return {"example.com"} - - def deploy_cert(self, domain, cert_path, key_path, chain_path=None, - fullchain_path=None): - """Installs cert""" - cert_path, key_path, chain_path = self.copy_certs_and_keys( - cert_path, key_path, chain_path) - self._configurator.deploy_cert( - domain, cert_path, key_path, chain_path, fullchain_path) diff --git a/certbot-compatibility-test/certbot_compatibility_test/configurators/nginx/common.py b/certbot-compatibility-test/certbot_compatibility_test/configurators/nginx/common.py deleted file mode 100644 index ed5cf750ed4..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/configurators/nginx/common.py +++ /dev/null @@ -1,86 +0,0 @@ -"""Provides a common base for Nginx proxies""" -import os -import shutil -import subprocess - -import zope.interface - -from certbot import configuration -from certbot_nginx import configurator -from certbot_nginx import constants -from certbot_compatibility_test import errors -from certbot_compatibility_test import interfaces -from certbot_compatibility_test import util -from certbot_compatibility_test.configurators import common as configurators_common - - -@zope.interface.implementer(interfaces.IConfiguratorProxy) -class Proxy(configurators_common.Proxy): - # pylint: disable=too-many-instance-attributes - """A common base for Nginx test configurators""" - - def __init__(self, args): - """Initializes the plugin with the given command line args""" - super(Proxy, self).__init__(args) - - def load_config(self): - """Loads the next configuration for the plugin to test""" - config = super(Proxy, self).load_config() - self._all_names, self._test_names = _get_names(config) - - server_root = _get_server_root(config) - - # XXX: Deleting all of this is kind of scary unless the test - # instances really each have a complete configuration! - shutil.rmtree("/etc/nginx") - shutil.copytree(server_root, "/etc/nginx", symlinks=True) - - self._prepare_configurator() - - try: - subprocess.check_call("service nginx reload".split()) - except errors.Error: - raise errors.Error( - "Nginx failed to load {0} before tests started".format( - config)) - - return config - - def _prepare_configurator(self): - """Prepares the Nginx plugin for testing""" - for k in constants.CLI_DEFAULTS.keys(): - setattr(self.le_config, "nginx_" + k, constants.os_constant(k)) - - conf = configuration.NamespaceConfig(self.le_config) - zope.component.provideUtility(conf) - self._configurator = configurator.NginxConfigurator( - config=conf, name="nginx") - self._configurator.prepare() - - -def _get_server_root(config): - """Returns the server root directory in config""" - subdirs = [ - name for name in os.listdir(config) - if os.path.isdir(os.path.join(config, name))] - - if len(subdirs) != 1: - raise errors.Error("Malformed configuration directory {0}".format(config)) - - return os.path.join(config, subdirs[0].rstrip()) - - -def _get_names(config): - """Returns all and testable domain names in config""" - all_names = set() - for root, _dirs, files in os.walk(config): - for this_file in files: - for line in open(os.path.join(root, this_file)): - if line.strip().startswith("server_name"): - names = line.partition("server_name")[2].rpartition(";")[0] - for n in names.split(): - # Filter out wildcards in both all_names and test_names - if not n.startswith("*."): - all_names.add(n) - non_ip_names = set(n for n in all_names if not util.IP_REGEX.match(n)) - return all_names, non_ip_names diff --git a/certbot-compatibility-test/certbot_compatibility_test/interfaces.py b/certbot-compatibility-test/certbot_compatibility_test/interfaces.py deleted file mode 100644 index 7d3daee0950..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/interfaces.py +++ /dev/null @@ -1,52 +0,0 @@ -"""Certbot compatibility test interfaces""" -import zope.interface - -import certbot.interfaces - -# pylint: disable=no-self-argument,no-method-argument - - -class IPluginProxy(zope.interface.Interface): - """Wraps a Certbot plugin""" - http_port = zope.interface.Attribute( - "The port to connect to on localhost for HTTP traffic") - - https_port = zope.interface.Attribute( - "The port to connect to on localhost for HTTPS traffic") - - def add_parser_arguments(cls, parser): - """Adds command line arguments needed by the parser""" - - def __init__(args): - """Initializes the plugin with the given command line args""" - - def cleanup_from_tests(): # type: ignore - """Performs any necessary cleanup from running plugin tests. - - This is guaranteed to be called before the program exits. - - """ - - def has_more_configs(): # type: ignore - """Returns True if there are more configs to test""" - - def load_config(): # type: ignore - """Loads the next config and returns its name""" - - def get_testable_domain_names(): # type: ignore - """Returns the domain names that can be used in testing""" - - -class IAuthenticatorProxy(IPluginProxy, certbot.interfaces.IAuthenticator): - """Wraps a Certbot authenticator""" - - -class IInstallerProxy(IPluginProxy, certbot.interfaces.IInstaller): - """Wraps a Certbot installer""" - - def get_all_names_answer(): # type: ignore - """Returns all names that should be found by the installer""" - - -class IConfiguratorProxy(IAuthenticatorProxy, IInstallerProxy): - """Wraps a Certbot configurator""" diff --git a/certbot-compatibility-test/certbot_compatibility_test/test_driver.py b/certbot-compatibility-test/certbot_compatibility_test/test_driver.py deleted file mode 100644 index 9eea95e674c..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/test_driver.py +++ /dev/null @@ -1,373 +0,0 @@ -"""Tests Certbot plugins against different server configurations.""" -import argparse -import filecmp -import logging -import os -import shutil -import tempfile -import time -import sys - -import OpenSSL - -from six.moves import xrange # pylint: disable=import-error,redefined-builtin - -from acme import challenges -from acme import crypto_util -from acme import messages -from acme.magic_typing import List, Tuple # pylint: disable=unused-import, no-name-in-module -from certbot import achallenges -from certbot import errors as le_errors -from certbot.tests import acme_util - -from certbot_compatibility_test import errors -from certbot_compatibility_test import util -from certbot_compatibility_test import validator - -from certbot_compatibility_test.configurators.apache import common as a_common -from certbot_compatibility_test.configurators.nginx import common as n_common - - -DESCRIPTION = """ -Tests Certbot plugins against different server configurations. It is -assumed that Docker is already installed. If no test type is specified, all -tests that the plugin supports are performed. - -""" - -PLUGINS = {"apache": a_common.Proxy, "nginx": n_common.Proxy} - - -logger = logging.getLogger(__name__) - - -def test_authenticator(plugin, config, temp_dir): - """Tests authenticator, returning True if the tests are successful""" - backup = _create_backup(config, temp_dir) - - achalls = _create_achalls(plugin) - if not achalls: - logger.error("The plugin and this program support no common " - "challenge types") - return False - - try: - responses = plugin.perform(achalls) - except le_errors.Error: - logger.error("Performing challenges on %s caused an error:", config, exc_info=True) - return False - - success = True - for i in xrange(len(responses)): - if not responses[i]: - logger.error( - "Plugin failed to complete %s for %s in %s", - type(achalls[i]), achalls[i].domain, config) - success = False - elif isinstance(responses[i], challenges.TLSSNI01Response): - verified = responses[i].simple_verify(achalls[i].chall, - achalls[i].domain, - util.JWK.public_key(), - host="127.0.0.1", - port=plugin.https_port) - if verified: - logger.info( - "tls-sni-01 verification for %s succeeded", achalls[i].domain) - else: - logger.error( - "**** tls-sni-01 verification for %s in %s failed", - achalls[i].domain, config) - success = False - - if success: - try: - plugin.cleanup(achalls) - except le_errors.Error: - logger.error("Challenge cleanup for %s caused an error:", config, exc_info=True) - success = False - - if _dirs_are_unequal(config, backup): - logger.error("Challenge cleanup failed for %s", config) - return False - else: - logger.info("Challenge cleanup succeeded") - - return success - - -def _create_achalls(plugin): - """Returns a list of annotated challenges to test on plugin""" - achalls = list() - names = plugin.get_testable_domain_names() - for domain in names: - prefs = plugin.get_chall_pref(domain) - for chall_type in prefs: - if chall_type == challenges.TLSSNI01: - chall = challenges.TLSSNI01( - token=os.urandom(challenges.TLSSNI01.TOKEN_SIZE)) - challb = acme_util.chall_to_challb( - chall, messages.STATUS_PENDING) - achall = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=challb, domain=domain, account_key=util.JWK) - achalls.append(achall) - - return achalls - - -def test_installer(args, plugin, config, temp_dir): - """Tests plugin as an installer""" - backup = _create_backup(config, temp_dir) - - names_match = plugin.get_all_names() == plugin.get_all_names_answer() - if names_match: - logger.info("get_all_names test succeeded") - else: - logger.error("**** get_all_names test failed for config %s", config) - - domains = list(plugin.get_testable_domain_names()) - success = test_deploy_cert(plugin, temp_dir, domains) - - if success and args.enhance: - success = test_enhancements(plugin, domains) - - good_rollback = test_rollback(plugin, config, backup) - return names_match and success and good_rollback - - -def test_deploy_cert(plugin, temp_dir, domains): - """Tests deploy_cert returning True if the tests are successful""" - cert = crypto_util.gen_ss_cert(util.KEY, domains) - cert_path = os.path.join(temp_dir, "cert.pem") - with open(cert_path, "w") as f: - f.write(OpenSSL.crypto.dump_certificate( - OpenSSL.crypto.FILETYPE_PEM, cert)) - - for domain in domains: - try: - plugin.deploy_cert(domain, cert_path, util.KEY_PATH, cert_path, cert_path) - plugin.save() # Needed by the Apache plugin - except le_errors.Error: - logger.error("**** Plugin failed to deploy certificate for %s:", domain, exc_info=True) - return False - - if not _save_and_restart(plugin, "deployed"): - return False - - success = True - time.sleep(3) - for domain in domains: - verified = validator.Validator().certificate( - cert, domain, "127.0.0.1", plugin.https_port) - if not verified: - logger.error("**** Could not verify certificate for domain %s", domain) - success = False - - if success: - logger.info("HTTPS validation succeeded") - - return success - - -def test_enhancements(plugin, domains): - """Tests supported enhancements returning True if successful""" - supported = plugin.supported_enhancements() - - if "redirect" not in supported: - logger.error("The plugin and this program support no common " - "enhancements") - return False - - domains_and_info = [(domain, []) for domain in domains] # type: List[Tuple[str, List[bool]]] - - for domain, info in domains_and_info: - try: - previous_redirect = validator.Validator().any_redirect( - "localhost", plugin.http_port, headers={"Host": domain}) - info.append(previous_redirect) - plugin.enhance(domain, "redirect") - plugin.save() # Needed by the Apache plugin - except le_errors.PluginError as error: - # Don't immediately fail because a redirect may already be enabled - logger.warning("*** Plugin failed to enable redirect for %s:", domain) - logger.warning("%s", error) - except le_errors.Error: - logger.error("*** An error occurred while enabling redirect for %s:", - domain, exc_info=True) - - if not _save_and_restart(plugin, "enhanced"): - return False - - success = True - for domain, info in domains_and_info: - previous_redirect = info[0] - if not previous_redirect: - verified = validator.Validator().redirect( - "localhost", plugin.http_port, headers={"Host": domain}) - if not verified: - logger.error("*** Improper redirect for domain %s", domain) - success = False - - if success: - logger.info("Enhancements test succeeded") - - return success - - -def _save_and_restart(plugin, title=None): - """Saves and restart the plugin, returning True if no errors occurred""" - try: - plugin.save(title) - plugin.restart() - return True - except le_errors.Error: - logger.error("*** Plugin failed to save and restart server:", exc_info=True) - return False - - -def test_rollback(plugin, config, backup): - """Tests the rollback checkpoints function""" - try: - plugin.rollback_checkpoints(1337) - except le_errors.Error: - logger.error("*** Plugin raised an exception during rollback:", exc_info=True) - return False - - if _dirs_are_unequal(config, backup): - logger.error("*** Rollback failed for config `%s`", config) - return False - else: - logger.info("Rollback succeeded") - return True - - -def _create_backup(config, temp_dir): - """Creates a backup of config in temp_dir""" - backup = os.path.join(temp_dir, "backup") - shutil.rmtree(backup, ignore_errors=True) - shutil.copytree(config, backup, symlinks=True) - - return backup - - -def _dirs_are_unequal(dir1, dir2): - """Returns True if dir1 and dir2 are unequal""" - dircmps = [filecmp.dircmp(dir1, dir2)] - while len(dircmps): - dircmp = dircmps.pop() - if dircmp.left_only or dircmp.right_only: - logger.error("The following files and directories are only " - "present in one directory") - if dircmp.left_only: - logger.error(str(dircmp.left_only)) - else: - logger.error(str(dircmp.right_only)) - return True - elif dircmp.common_funny or dircmp.funny_files: - logger.error("The following files and directories could not be " - "compared:") - if dircmp.common_funny: - logger.error(str(dircmp.common_funny)) - else: - logger.error(str(dircmp.funny_files)) - return True - elif dircmp.diff_files: - logger.error("The following files differ:") - logger.error(str(dircmp.diff_files)) - return True - - for subdir in dircmp.subdirs.itervalues(): - dircmps.append(subdir) - - return False - - -def get_args(): - """Returns parsed command line arguments.""" - parser = argparse.ArgumentParser( - description=DESCRIPTION, - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - group = parser.add_argument_group("general") - group.add_argument( - "-c", "--configs", default="configs.tar.gz", - help="a directory or tarball containing server configurations") - group.add_argument( - "-p", "--plugin", default="apache", help="the plugin to be tested") - group.add_argument( - "-v", "--verbose", dest="verbose_count", action="count", - default=0, help="you know how to use this") - group.add_argument( - "-a", "--auth", action="store_true", - help="tests the challenges the plugin supports") - group.add_argument( - "-i", "--install", action="store_true", - help="tests the plugin as an installer") - group.add_argument( - "-e", "--enhance", action="store_true", help="tests the enhancements " - "the plugin supports (implicitly includes installer tests)") - - for plugin in PLUGINS.itervalues(): - plugin.add_parser_arguments(parser) - - args = parser.parse_args() - if args.enhance: - args.install = True - elif not (args.auth or args.install): - args.auth = args.install = args.enhance = True - - return args - - -def setup_logging(args): - """Prepares logging for the program""" - handler = logging.StreamHandler() - - root_logger = logging.getLogger() - root_logger.setLevel(logging.ERROR - args.verbose_count * 10) - root_logger.addHandler(handler) - - -def main(): - """Main test script execution.""" - args = get_args() - setup_logging(args) - - if args.plugin not in PLUGINS: - raise errors.Error("Unknown plugin {0}".format(args.plugin)) - - temp_dir = tempfile.mkdtemp() - plugin = PLUGINS[args.plugin](args) - try: - overall_success = True - while plugin.has_more_configs(): - success = True - - try: - config = plugin.load_config() - logger.info("Loaded configuration: %s", config) - if args.auth: - success = test_authenticator(plugin, config, temp_dir) - if success and args.install: - success = test_installer(args, plugin, config, temp_dir) - except errors.Error: - logger.error("Tests on %s raised:", config, exc_info=True) - success = False - - if success: - logger.info("All tests on %s succeeded", config) - else: - overall_success = False - logger.error("Tests on %s failed", config) - finally: - plugin.cleanup_from_tests() - - if overall_success: - logger.warning("All compatibility tests succeeded") - sys.exit(0) - else: - logger.warning("One or more compatibility tests failed") - sys.exit(1) - - -if __name__ == "__main__": - main() diff --git a/certbot-compatibility-test/certbot_compatibility_test/testdata/empty_cert.pem b/certbot-compatibility-test/certbot_compatibility_test/testdata/empty_cert.pem deleted file mode 100644 index 4ea812a8767..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/testdata/empty_cert.pem +++ /dev/null @@ -1,13 +0,0 @@ ------BEGIN CERTIFICATE----- -MIICATCCAWoCCQCvMbKu4FHZ6zANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB -VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 -cyBQdHkgTHRkMB4XDTE1MDcyMzIzMjc1MFoXDTE2MDcyMjIzMjc1MFowRTELMAkG -A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 -IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAws3o -y46PMLM9Gr68pbex0MhdPr7Cq4rRe9BBpnOuHFdF35Ak0aPrzFwVzLlGOir94U11 -e5JYJDWJi+4FwLBRkOAfanjJ5GJ9BnEHSOdbtO+sv9uhbt+7iYOOUOngKSiJyUrM -i1THAE+B1CenxZ1KHRQCke708zkK8jVuxLeIAOMCAwEAATANBgkqhkiG9w0BAQsF -AAOBgQCC3LUP3MHk+IBmwHHZAZCX+6p4lop9SP6y6rDpWgnqEEeb9oFleHi2Rvzq -7gxl6nS5AsaSzfAygJ3zWKTwVAZyU4GOQ8QTK+nHk3+LO1X4cDbUlQfm5+YuwKDa -4LFKeovmrK6BiMLIc1J+MxUjLfCeVHYSdkZULTVXue0zif0BUA== ------END CERTIFICATE----- diff --git a/certbot-compatibility-test/certbot_compatibility_test/testdata/nginx.tar.gz b/certbot-compatibility-test/certbot_compatibility_test/testdata/nginx.tar.gz deleted file mode 100644 index 2f06add1707..00000000000 Binary files a/certbot-compatibility-test/certbot_compatibility_test/testdata/nginx.tar.gz and /dev/null differ diff --git a/certbot-compatibility-test/certbot_compatibility_test/testdata/rsa1024_key.pem b/certbot-compatibility-test/certbot_compatibility_test/testdata/rsa1024_key.pem deleted file mode 100644 index 8f82146badb..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/testdata/rsa1024_key.pem +++ /dev/null @@ -1,15 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIICXAIBAAKBgQCsREbM+UcfsgDy2w56AVGyxsO0HVsbEZHHoEzv7qksIwFgRYMp -rowwIxD450RQQqjvw9IoXlMVmr1t5szn5KXn9JRO9T5KNCCy3VPx75WBcp6kzd9Q -2HS1OEOtpilNnDkZ+TJfdgFWPUBYj2o4Md1hPmcvagiIJY5U6speka2bjwIDAQAB -AoGANCMZ9pF/mDUsmlP4Rq69hkkoFAxKdZ/UqkF256so4mXZ1cRUFTpxzWPfkCWW -hGAYdzCiG3uo08IYkPmojIqkN1dk5Hcq5eQAmshaPkQHQCHjmPjjcNvgjIXQoGUf -TpDU2hbY4UAlJlj4ZLh+jGP5Zq8/WrNi8RsI3v9Nagfp/FECQQDgi2q8p1gX0TNh -d1aEKmSXkR3bxkyFk6oS+pBrAG3+yX27ZayN6Rx6DOs/FcBsOu7fX3PYBziDeEWe -Lkf1P743AkEAxGYT/LY3puglSz4iJZZzWmRCrVOg41yhfQ+F1BRX43/2vtoU5GyM -2lUn1vQ2e/rfmnAvfJxc90GeZCIHB1ihaQJBALH8UMLxMtbOMJgVbDKfF9U8ZhqK -+KT5A1q/2jG2yXmoZU1hroFeQgBMtTvwFfK0VBwjIUQflSBA+Y4EyW0Q9ckCQGvd -jHitM1+N/H2YwHRYbz5j9mLvnVuCEod3MQ9LpQGj1Eb5y6OxIqL/RgQ+2HW7UXem -yc3sqvp5pZ5lOesE+JECQETPI64gqxlTIs3nErNMpMynUuTWpaElOcIJTT6icLzB -Xix67kKXjROO5D58GEYkM0Yi5k7YdUPoQBW7MoIrSIA= ------END RSA PRIVATE KEY----- diff --git a/certbot-compatibility-test/certbot_compatibility_test/testdata/rsa1024_key2.pem b/certbot-compatibility-test/certbot_compatibility_test/testdata/rsa1024_key2.pem deleted file mode 100644 index 03f77d9036e..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/testdata/rsa1024_key2.pem +++ /dev/null @@ -1,15 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIICXAIBAAKBgQDCzejLjo8wsz0avrylt7HQyF0+vsKritF70EGmc64cV0XfkCTR -o+vMXBXMuUY6Kv3hTXV7klgkNYmL7gXAsFGQ4B9qeMnkYn0GcQdI51u076y/26Fu -37uJg45Q6eApKInJSsyLVMcAT4HUJ6fFnUodFAKR7vTzOQryNW7Et4gA4wIDAQAB -AoGAKiAU40/krwdTg2ETslJS5W8ums7tkeLnAfs69x+02vQUbA/jpmHoL70KCcdW -5GU/mWUCrsIqxUm+gL/sBosaV/TF256qUBt2qQCZTN8MbDaNSYiiMnucOfbWdIqx -Zgls6GUoXQvPic9RUoFSlgfSjo5ezz6el5ihvRMp+wbk24ECQQD3oz4hN029DSZo -Y3+flmBn77gA0BMUvLa6hmt9b3xT5U/ToCLfbmUvpx7zV1g5era2y9qt/o3UtAbW -1zCVETgzAkEAyWHv/+RnSXp8/D4YwTVWyeWi862uNBPkuLGP/0zASdwBfBK3uBls -+VumfSCtp0kt2AXXmScg1fkHdeAVT6AkkQJBAJb2XRnCrRFiwtdAULzo3zx9Vp6o -OfmaUYrEByMgo5pBYLiSFrA+jFDQgH238YCY3mnxPA517+CLHuA5rtQw+yECQCfm -gL/pyFE1tLfhsdPuNpDwL9YqLl7hJis1+zrxQRQhRCYKK16NoxrQ/u7B38ZKaIvp -tGsC5q2elszTJkXNjBECQCVE9QCVx056vHVdPWM8z3GAeV3sJQ01HLLjebTEEz6G -jH54gk+YYPp4kjCvVUykbnB58BY2n88GQt5Jj5eLuMo= ------END RSA PRIVATE KEY----- diff --git a/certbot-compatibility-test/certbot_compatibility_test/util.py b/certbot-compatibility-test/certbot_compatibility_test/util.py deleted file mode 100644 index 6051bbc2eaa..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/util.py +++ /dev/null @@ -1,53 +0,0 @@ -"""Utility functions for Certbot plugin tests.""" -import argparse -import copy -import os -import re -import shutil -import tarfile - -import josepy as jose - -from acme import test_util -from certbot import constants - -from certbot_compatibility_test import errors - - -_KEY_BASE = "rsa1024_key.pem" -KEY_PATH = test_util.vector_path(_KEY_BASE) -KEY = test_util.load_pyopenssl_private_key(_KEY_BASE) -JWK = jose.JWKRSA(key=test_util.load_rsa_private_key(_KEY_BASE)) -IP_REGEX = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") - - -def create_le_config(parent_dir): - """Sets up LE dirs in parent_dir and returns the config dict""" - config = copy.deepcopy(constants.CLI_DEFAULTS) - - le_dir = os.path.join(parent_dir, "certbot") - os.mkdir(le_dir) - for dir_name in ("config", "logs", "work"): - full_path = os.path.join(le_dir, dir_name) - os.mkdir(full_path) - full_name = dir_name + "_dir" - config[full_name] = full_path - - config["domains"] = None - - return argparse.Namespace(**config) # pylint: disable=star-args - - -def extract_configs(configs, parent_dir): - """Extracts configs to a new dir under parent_dir and returns it""" - config_dir = os.path.join(parent_dir, "configs") - - if os.path.isdir(configs): - shutil.copytree(configs, config_dir, symlinks=True) - elif tarfile.is_tarfile(configs): - with tarfile.open(configs, "r") as tar: - tar.extractall(config_dir) - else: - raise errors.Error("Unknown configurations file type") - - return config_dir diff --git a/certbot-compatibility-test/certbot_compatibility_test/validator.py b/certbot-compatibility-test/certbot_compatibility_test/validator.py deleted file mode 100644 index fd2f95702ea..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/validator.py +++ /dev/null @@ -1,103 +0,0 @@ -"""Validators to determine the current webserver configuration""" -import logging -import socket -import requests -import zope.interface - -import six -from six.moves import xrange # pylint: disable=import-error,redefined-builtin - -from acme import crypto_util -from acme import errors as acme_errors -from certbot import interfaces - - -logger = logging.getLogger(__name__) - - -@zope.interface.implementer(interfaces.IValidator) -class Validator(object): - # pylint: disable=no-self-use - """Collection of functions to test a live webserver's configuration""" - - def certificate(self, cert, name, alt_host=None, port=443): - """Verifies the certificate presented at name is cert""" - if alt_host is None: - host = socket.gethostbyname(name) - elif isinstance(alt_host, six.binary_type): - host = alt_host - else: - host = alt_host.encode() - name = name if isinstance(name, six.binary_type) else name.encode() - - try: - presented_cert = crypto_util.probe_sni(name, host, port) - except acme_errors.Error as error: - logger.exception(str(error)) - return False - - return presented_cert.digest("sha256") == cert.digest("sha256") - - def redirect(self, name, port=80, headers=None): - """Test whether webserver redirects to secure connection.""" - url = "http://{0}:{1}".format(name, port) - if headers: - response = requests.get(url, headers=headers, allow_redirects=False) - else: - response = requests.get(url, allow_redirects=False) - - redirect_location = response.headers.get("location", "") - # We're checking that the redirect we added behaves correctly. - # It's okay for some server configuration to redirect to an - # http URL, as long as it's on some other domain. - if not redirect_location.startswith("https://"): - return False - - if response.status_code != 301: - logger.error("Server did not redirect with permanent code") - return False - - return True - - def any_redirect(self, name, port=80, headers=None): - """Test whether webserver redirects.""" - url = "http://{0}:{1}".format(name, port) - if headers: - response = requests.get(url, headers=headers, allow_redirects=False) - else: - response = requests.get(url, allow_redirects=False) - - return response.status_code in xrange(300, 309) - - def hsts(self, name): - """Test for HTTP Strict Transport Security header""" - headers = requests.get("https://" + name).headers - hsts_header = headers.get("strict-transport-security") - - if not hsts_header: - return False - - # Split directives following RFC6797, section 6.1 - directives = [d.split("=") for d in hsts_header.split(";")] - max_age = [d for d in directives if d[0] == "max-age"] - - if not max_age: - logger.error("Server responded with invalid HSTS header field") - return False - - try: - max_age_value = int(max_age[0][1]) - except ValueError: - logger.error("Server responded with invalid HSTS header field") - return False - - # Test whether HSTS does not expire for at least two weeks. - if max_age_value <= (2 * 7 * 24 * 3600): - logger.error("HSTS should not expire in less than two weeks") - return False - - return True - - def ocsp_stapling(self, name): - """Verify ocsp stapling for domain.""" - raise NotImplementedError() diff --git a/certbot-compatibility-test/certbot_compatibility_test/validator_test.py b/certbot-compatibility-test/certbot_compatibility_test/validator_test.py deleted file mode 100644 index d0552a756ce..00000000000 --- a/certbot-compatibility-test/certbot_compatibility_test/validator_test.py +++ /dev/null @@ -1,125 +0,0 @@ -"""Tests for certbot_compatibility_test.validator.""" -import requests -import unittest - -import mock -import OpenSSL - -from acme import errors as acme_errors -from certbot_compatibility_test import validator - - -class ValidatorTest(unittest.TestCase): - def setUp(self): - self.validator = validator.Validator() - - @mock.patch( - "certbot_compatibility_test.validator.crypto_util.probe_sni") - def test_certificate_success(self, mock_probe_sni): - cert = OpenSSL.crypto.X509() - mock_probe_sni.return_value = cert - self.assertTrue(self.validator.certificate( - cert, "test.com", "127.0.0.1")) - - @mock.patch( - "certbot_compatibility_test.validator.crypto_util.probe_sni") - def test_certificate_error(self, mock_probe_sni): - cert = OpenSSL.crypto.X509() - mock_probe_sni.side_effect = [acme_errors.Error] - self.assertFalse(self.validator.certificate( - cert, "test.com", "127.0.0.1")) - - @mock.patch( - "certbot_compatibility_test.validator.crypto_util.probe_sni") - def test_certificate_failure(self, mock_probe_sni): - cert = OpenSSL.crypto.X509() - cert.set_serial_number(1337) - mock_probe_sni.return_value = OpenSSL.crypto.X509() - self.assertFalse(self.validator.certificate( - cert, "test.com", "127.0.0.1")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_succesful_redirect(self, mock_get_request): - mock_get_request.return_value = create_response( - 301, {"location": "https://test.com"}) - self.assertTrue(self.validator.redirect("test.com")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_redirect_with_headers(self, mock_get_request): - mock_get_request.return_value = create_response( - 301, {"location": "https://test.com"}) - self.assertTrue(self.validator.redirect( - "test.com", headers={"Host": "test.com"})) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_redirect_missing_location(self, mock_get_request): - mock_get_request.return_value = create_response(301) - self.assertFalse(self.validator.redirect("test.com")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_redirect_wrong_status_code(self, mock_get_request): - mock_get_request.return_value = create_response( - 201, {"location": "https://test.com"}) - self.assertFalse(self.validator.redirect("test.com")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_redirect_wrong_redirect_code(self, mock_get_request): - mock_get_request.return_value = create_response( - 303, {"location": "https://test.com"}) - self.assertFalse(self.validator.redirect("test.com")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_hsts_empty(self, mock_get_request): - mock_get_request.return_value = create_response( - headers={"strict-transport-security": ""}) - self.assertFalse(self.validator.hsts("test.com")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_hsts_malformed(self, mock_get_request): - mock_get_request.return_value = create_response( - headers={"strict-transport-security": "sdfal"}) - self.assertFalse(self.validator.hsts("test.com")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_hsts_bad_max_age(self, mock_get_request): - mock_get_request.return_value = create_response( - headers={"strict-transport-security": "max-age=not-an-int"}) - self.assertFalse(self.validator.hsts("test.com")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_hsts_expire(self, mock_get_request): - mock_get_request.return_value = create_response( - headers={"strict-transport-security": "max-age=3600"}) - self.assertFalse(self.validator.hsts("test.com")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_hsts(self, mock_get_request): - mock_get_request.return_value = create_response( - headers={"strict-transport-security": "max-age=31536000"}) - self.assertTrue(self.validator.hsts("test.com")) - - @mock.patch("certbot_compatibility_test.validator.requests.get") - def test_hsts_include_subdomains(self, mock_get_request): - mock_get_request.return_value = create_response( - headers={"strict-transport-security": - "max-age=31536000;includeSubDomains"}) - self.assertTrue(self.validator.hsts("test.com")) - - def test_ocsp_stapling(self): - self.assertRaises( - NotImplementedError, self.validator.ocsp_stapling, "test.com") - - -def create_response(status_code=200, headers=None): - """Creates a requests.Response object for testing""" - response = requests.Response() - response.status_code = status_code - - if headers: - response.headers = headers - - return response - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/certbot-compatibility-test/docs/.gitignore b/certbot-compatibility-test/docs/.gitignore deleted file mode 100644 index ba65b13af5e..00000000000 --- a/certbot-compatibility-test/docs/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/_build/ diff --git a/certbot-compatibility-test/docs/Makefile b/certbot-compatibility-test/docs/Makefile deleted file mode 100644 index 0c9cf40aae7..00000000000 --- a/certbot-compatibility-test/docs/Makefile +++ /dev/null @@ -1,192 +0,0 @@ -# Makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -PAPER = -BUILDDIR = _build - -# User-friendly check for sphinx-build -ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) -$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) -endif - -# Internal variables. -PAPEROPT_a4 = -D latex_paper_size=a4 -PAPEROPT_letter = -D latex_paper_size=letter -ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . -# the i18n builder cannot share the environment and doctrees with the others -I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . - -.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext - -help: - @echo "Please use \`make ' where is one of" - @echo " html to make standalone HTML files" - @echo " dirhtml to make HTML files named index.html in directories" - @echo " singlehtml to make a single large HTML file" - @echo " pickle to make pickle files" - @echo " json to make JSON files" - @echo " htmlhelp to make HTML files and a HTML help project" - @echo " qthelp to make HTML files and a qthelp project" - @echo " applehelp to make an Apple Help Book" - @echo " devhelp to make HTML files and a Devhelp project" - @echo " epub to make an epub" - @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" - @echo " latexpdf to make LaTeX files and run them through pdflatex" - @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" - @echo " text to make text files" - @echo " man to make manual pages" - @echo " texinfo to make Texinfo files" - @echo " info to make Texinfo files and run them through makeinfo" - @echo " gettext to make PO message catalogs" - @echo " changes to make an overview of all changed/added/deprecated items" - @echo " xml to make Docutils-native XML files" - @echo " pseudoxml to make pseudoxml-XML files for display purposes" - @echo " linkcheck to check all external links for integrity" - @echo " doctest to run all doctests embedded in the documentation (if enabled)" - @echo " coverage to run coverage check of the documentation (if enabled)" - -clean: - rm -rf $(BUILDDIR)/* - -html: - $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." - -dirhtml: - $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." - -singlehtml: - $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml - @echo - @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." - -pickle: - $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle - @echo - @echo "Build finished; now you can process the pickle files." - -json: - $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json - @echo - @echo "Build finished; now you can process the JSON files." - -htmlhelp: - $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp - @echo - @echo "Build finished; now you can run HTML Help Workshop with the" \ - ".hhp project file in $(BUILDDIR)/htmlhelp." - -qthelp: - $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp - @echo - @echo "Build finished; now you can run "qcollectiongenerator" with the" \ - ".qhcp project file in $(BUILDDIR)/qthelp, like this:" - @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/certbot-compatibility-test.qhcp" - @echo "To view the help file:" - @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/certbot-compatibility-test.qhc" - -applehelp: - $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp - @echo - @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." - @echo "N.B. You won't be able to view it unless you put it in" \ - "~/Library/Documentation/Help or install it in your application" \ - "bundle." - -devhelp: - $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp - @echo - @echo "Build finished." - @echo "To view the help file:" - @echo "# mkdir -p $$HOME/.local/share/devhelp/certbot-compatibility-test" - @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/certbot-compatibility-test" - @echo "# devhelp" - -epub: - $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub - @echo - @echo "Build finished. The epub file is in $(BUILDDIR)/epub." - -latex: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo - @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." - @echo "Run \`make' in that directory to run these through (pdf)latex" \ - "(use \`make latexpdf' here to do that automatically)." - -latexpdf: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through pdflatex..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -latexpdfja: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through platex and dvipdfmx..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -text: - $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text - @echo - @echo "Build finished. The text files are in $(BUILDDIR)/text." - -man: - $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man - @echo - @echo "Build finished. The manual pages are in $(BUILDDIR)/man." - -texinfo: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo - @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." - @echo "Run \`make' in that directory to run these through makeinfo" \ - "(use \`make info' here to do that automatically)." - -info: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo "Running Texinfo files through makeinfo..." - make -C $(BUILDDIR)/texinfo info - @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." - -gettext: - $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale - @echo - @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." - -changes: - $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes - @echo - @echo "The overview file is in $(BUILDDIR)/changes." - -linkcheck: - $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck - @echo - @echo "Link check complete; look for any errors in the above output " \ - "or in $(BUILDDIR)/linkcheck/output.txt." - -doctest: - $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest - @echo "Testing of doctests in the sources finished, look at the " \ - "results in $(BUILDDIR)/doctest/output.txt." - -coverage: - $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage - @echo "Testing of coverage in the sources finished, look at the " \ - "results in $(BUILDDIR)/coverage/python.txt." - -xml: - $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml - @echo - @echo "Build finished. The XML files are in $(BUILDDIR)/xml." - -pseudoxml: - $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml - @echo - @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/certbot-compatibility-test/docs/api.rst b/certbot-compatibility-test/docs/api.rst deleted file mode 100644 index 8668ec5d8ad..00000000000 --- a/certbot-compatibility-test/docs/api.rst +++ /dev/null @@ -1,8 +0,0 @@ -================= -API Documentation -================= - -.. toctree:: - :glob: - - api/** diff --git a/certbot-compatibility-test/docs/api/index.rst b/certbot-compatibility-test/docs/api/index.rst deleted file mode 100644 index fea92d2e5f3..00000000000 --- a/certbot-compatibility-test/docs/api/index.rst +++ /dev/null @@ -1,53 +0,0 @@ -:mod:`certbot_compatibility_test` -------------------------------------- - -.. automodule:: certbot_compatibility_test - :members: - -:mod:`certbot_compatibility_test.errors` -============================================ - -.. automodule:: certbot_compatibility_test.errors - :members: - -:mod:`certbot_compatibility_test.interfaces` -================================================ - -.. automodule:: certbot_compatibility_test.interfaces - :members: - -:mod:`certbot_compatibility_test.test_driver` -================================================= - -.. automodule:: certbot_compatibility_test.test_driver - :members: - -:mod:`certbot_compatibility_test.util` -========================================== - -.. automodule:: certbot_compatibility_test.util - :members: - -:mod:`certbot_compatibility_test.configurators` -=================================================== - -.. automodule:: certbot_compatibility_test.configurators - :members: - -:mod:`certbot_compatibility_test.configurators.apache` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -.. automodule:: certbot_compatibility_test.configurators.apache - :members: - -:mod:`certbot_compatibility_test.configurators.apache.apache24` -------------------------------------------------------------------- - -.. automodule:: certbot_compatibility_test.configurators.apache.apache24 - :members: - -:mod:`certbot_compatibility_test.configurators.apache.common` -------------------------------------------------------------------- - -.. automodule:: certbot_compatibility_test.configurators.apache.common - :members: diff --git a/certbot-compatibility-test/docs/conf.py b/certbot-compatibility-test/docs/conf.py deleted file mode 100644 index f89f4b368e8..00000000000 --- a/certbot-compatibility-test/docs/conf.py +++ /dev/null @@ -1,319 +0,0 @@ -# -*- coding: utf-8 -*- -# -# certbot-compatibility-test documentation build configuration file, created by -# sphinx-quickstart on Sun Oct 18 13:40:53 2015. -# -# This file is execfile()d with the current directory set to its -# containing dir. -# -# Note that not all possible configuration values are present in this -# autogenerated file. -# -# All configuration values have a default; values that are commented out -# serve to show the default. - -import sys -import os -import shlex - - -here = os.path.abspath(os.path.dirname(__file__)) - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath(os.path.join(here, '..'))) - -# -- General configuration ------------------------------------------------ - -# If your documentation needs a minimal Sphinx version, state it here. -needs_sphinx = '1.0' - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.viewcode', - 'repoze.sphinx.autointerface', -] - -autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# The suffix(es) of source filenames. -# You can specify multiple suffix as a list of string: -# source_suffix = ['.rst', '.md'] -source_suffix = '.rst' - -# The encoding of source files. -#source_encoding = 'utf-8-sig' - -# The master toctree document. -master_doc = 'index' - -# General information about the project. -project = u'certbot-compatibility-test' -copyright = u'2014-2015, Let\'s Encrypt Project' -author = u'Certbot Project' - -# The version info for the project you're documenting, acts as replacement for -# |version| and |release|, also used in various other places throughout the -# built documents. -# -# The short X.Y version. -version = '0' -# The full version, including alpha/beta/rc tags. -release = '0' - -# The language for content autogenerated by Sphinx. Refer to documentation -# for a list of supported languages. -# -# This is also used if you do content translation via gettext catalogs. -# Usually you set "language" from the command line for these cases. -language = 'en' - -# There are two options for replacing |today|: either, you set today to some -# non-false value, then it is used: -#today = '' -# Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -exclude_patterns = ['_build'] - -# The reST default role (used for this markup: `text`) to use for all -# documents. -default_role = 'py:obj' - -# If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True - -# If true, the current module name will be prepended to all description -# unit titles (such as .. function::). -#add_module_names = True - -# If true, sectionauthor and moduleauthor directives will be shown in the -# output. They are ignored by default. -#show_authors = False - -# The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' - -# A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] - -# If true, keep warnings as "system message" paragraphs in the built documents. -#keep_warnings = False - -# If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True - - -# -- Options for HTML output ---------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. - -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it - -# Theme options are theme-specific and customize the look and feel of a theme -# further. For a list of options available for each theme, see the -# documentation. -#html_theme_options = {} - -# Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] - -# The name for this set of Sphinx documents. If None, it defaults to -# " v documentation". -#html_title = None - -# A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None - -# The name of an image file (relative to this directory) to place at the top -# of the sidebar. -#html_logo = None - -# The name of an image file (within the static path) to use as favicon of the -# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 -# pixels large. -#html_favicon = None - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] - -# Add any extra paths that contain custom files (such as robots.txt or -# .htaccess) here, relative to this directory. These files are copied -# directly to the root of the documentation. -#html_extra_path = [] - -# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, -# using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' - -# If true, SmartyPants will be used to convert quotes and dashes to -# typographically correct entities. -#html_use_smartypants = True - -# Custom sidebar templates, maps document names to template names. -#html_sidebars = {} - -# Additional templates that should be rendered to pages, maps page names to -# template names. -#html_additional_pages = {} - -# If false, no module index is generated. -#html_domain_indices = True - -# If false, no index is generated. -#html_use_index = True - -# If true, the index is split into individual pages for each letter. -#html_split_index = False - -# If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True - -# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True - -# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True - -# If true, an OpenSearch description file will be output, and all pages will -# contain a tag referring to it. The value of this option must be the -# base URL from which the finished HTML is served. -#html_use_opensearch = '' - -# This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None - -# Language to be used for generating the HTML full-text search index. -# Sphinx supports the following languages: -# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' -# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' -#html_search_language = 'en' - -# A dictionary with options for the search language support, empty by default. -# Now only 'ja' uses this config value -#html_search_options = {'type': 'default'} - -# The name of a javascript file (relative to the configuration directory) that -# implements a search results scorer. If empty, the default will be used. -#html_search_scorer = 'scorer.js' - -# Output file base name for HTML help builder. -htmlhelp_basename = 'certbot-compatibility-testdoc' - -# -- Options for LaTeX output --------------------------------------------- - -latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - #'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - #'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - #'preamble': '', - - # Latex figure (float) alignment - #'figure_align': 'htbp', -} - -# Grouping the document tree into LaTeX files. List of tuples -# (source start file, target name, title, -# author, documentclass [howto, manual, or own class]). -latex_documents = [ - (master_doc, 'certbot-compatibility-test.tex', - u'certbot-compatibility-test Documentation', - u'Certbot Project', 'manual'), -] - -# The name of an image file (relative to this directory) to place at the top of -# the title page. -#latex_logo = None - -# For "manual" documents, if this is true, then toplevel headings are parts, -# not chapters. -#latex_use_parts = False - -# If true, show page references after internal links. -#latex_show_pagerefs = False - -# If true, show URL addresses after external links. -#latex_show_urls = False - -# Documents to append as an appendix to all manuals. -#latex_appendices = [] - -# If false, no module index is generated. -#latex_domain_indices = True - - -# -- Options for manual page output --------------------------------------- - -# One entry per manual page. List of tuples -# (source start file, name, description, authors, manual section). -man_pages = [ - (master_doc, 'certbot-compatibility-test', - u'certbot-compatibility-test Documentation', - [author], 1) -] - -# If true, show URL addresses after external links. -#man_show_urls = False - - -# -- Options for Texinfo output ------------------------------------------- - -# Grouping the document tree into Texinfo files. List of tuples -# (source start file, target name, title, author, -# dir menu entry, description, category) -texinfo_documents = [ - (master_doc, 'certbot-compatibility-test', - u'certbot-compatibility-test Documentation', - author, 'certbot-compatibility-test', - 'One line description of project.', 'Miscellaneous'), -] - -# Documents to append as an appendix to all manuals. -#texinfo_appendices = [] - -# If false, no module index is generated. -#texinfo_domain_indices = True - -# How to display URL addresses: 'footnote', 'no', or 'inline'. -#texinfo_show_urls = 'footnote' - -# If true, do not generate a @detailmenu in the "Top" node's menu. -#texinfo_no_detailmenu = False - - -intersphinx_mapping = { - 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), - 'certbot-apache': ( - 'https://letsencrypt-apache.readthedocs.org/en/latest/', None), - 'certbot-nginx': ( - 'https://letsencrypt-nginx.readthedocs.org/en/latest/', None), -} diff --git a/certbot-compatibility-test/docs/index.rst b/certbot-compatibility-test/docs/index.rst deleted file mode 100644 index a5e71e8445a..00000000000 --- a/certbot-compatibility-test/docs/index.rst +++ /dev/null @@ -1,27 +0,0 @@ -.. certbot-compatibility-test documentation master file, created by - sphinx-quickstart on Sun Oct 18 13:40:53 2015. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -Welcome to certbot-compatibility-test's documentation! -========================================================== - -Contents: - -.. toctree:: - :maxdepth: 2 - - -.. toctree:: - :maxdepth: 1 - - api - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` - diff --git a/certbot-compatibility-test/docs/make.bat b/certbot-compatibility-test/docs/make.bat deleted file mode 100644 index b6c0360f495..00000000000 --- a/certbot-compatibility-test/docs/make.bat +++ /dev/null @@ -1,263 +0,0 @@ -@ECHO OFF - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set BUILDDIR=_build -set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . -set I18NSPHINXOPTS=%SPHINXOPTS% . -if NOT "%PAPER%" == "" ( - set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% - set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% -) - -if "%1" == "" goto help - -if "%1" == "help" ( - :help - echo.Please use `make ^` where ^ is one of - echo. html to make standalone HTML files - echo. dirhtml to make HTML files named index.html in directories - echo. singlehtml to make a single large HTML file - echo. pickle to make pickle files - echo. json to make JSON files - echo. htmlhelp to make HTML files and a HTML help project - echo. qthelp to make HTML files and a qthelp project - echo. devhelp to make HTML files and a Devhelp project - echo. epub to make an epub - echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter - echo. text to make text files - echo. man to make manual pages - echo. texinfo to make Texinfo files - echo. gettext to make PO message catalogs - echo. changes to make an overview over all changed/added/deprecated items - echo. xml to make Docutils-native XML files - echo. pseudoxml to make pseudoxml-XML files for display purposes - echo. linkcheck to check all external links for integrity - echo. doctest to run all doctests embedded in the documentation if enabled - echo. coverage to run coverage check of the documentation if enabled - goto end -) - -if "%1" == "clean" ( - for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i - del /q /s %BUILDDIR%\* - goto end -) - - -REM Check if sphinx-build is available and fallback to Python version if any -%SPHINXBUILD% 2> nul -if errorlevel 9009 goto sphinx_python -goto sphinx_ok - -:sphinx_python - -set SPHINXBUILD=python -m sphinx.__init__ -%SPHINXBUILD% 2> nul -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ - exit /b 1 -) - -:sphinx_ok - - -if "%1" == "html" ( - %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/html. - goto end -) - -if "%1" == "dirhtml" ( - %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. - goto end -) - -if "%1" == "singlehtml" ( - %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. - goto end -) - -if "%1" == "pickle" ( - %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the pickle files. - goto end -) - -if "%1" == "json" ( - %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the JSON files. - goto end -) - -if "%1" == "htmlhelp" ( - %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run HTML Help Workshop with the ^ -.hhp project file in %BUILDDIR%/htmlhelp. - goto end -) - -if "%1" == "qthelp" ( - %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run "qcollectiongenerator" with the ^ -.qhcp project file in %BUILDDIR%/qthelp, like this: - echo.^> qcollectiongenerator %BUILDDIR%\qthelp\certbot-compatibility-test.qhcp - echo.To view the help file: - echo.^> assistant -collectionFile %BUILDDIR%\qthelp\certbot-compatibility-test.ghc - goto end -) - -if "%1" == "devhelp" ( - %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. - goto end -) - -if "%1" == "epub" ( - %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The epub file is in %BUILDDIR%/epub. - goto end -) - -if "%1" == "latex" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdf" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdfja" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf-ja - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "text" ( - %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The text files are in %BUILDDIR%/text. - goto end -) - -if "%1" == "man" ( - %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The manual pages are in %BUILDDIR%/man. - goto end -) - -if "%1" == "texinfo" ( - %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. - goto end -) - -if "%1" == "gettext" ( - %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The message catalogs are in %BUILDDIR%/locale. - goto end -) - -if "%1" == "changes" ( - %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes - if errorlevel 1 exit /b 1 - echo. - echo.The overview file is in %BUILDDIR%/changes. - goto end -) - -if "%1" == "linkcheck" ( - %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck - if errorlevel 1 exit /b 1 - echo. - echo.Link check complete; look for any errors in the above output ^ -or in %BUILDDIR%/linkcheck/output.txt. - goto end -) - -if "%1" == "doctest" ( - %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest - if errorlevel 1 exit /b 1 - echo. - echo.Testing of doctests in the sources finished, look at the ^ -results in %BUILDDIR%/doctest/output.txt. - goto end -) - -if "%1" == "coverage" ( - %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage - if errorlevel 1 exit /b 1 - echo. - echo.Testing of coverage in the sources finished, look at the ^ -results in %BUILDDIR%/coverage/python.txt. - goto end -) - -if "%1" == "xml" ( - %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The XML files are in %BUILDDIR%/xml. - goto end -) - -if "%1" == "pseudoxml" ( - %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. - goto end -) - -:end diff --git a/certbot-compatibility-test/nginx/README b/certbot-compatibility-test/nginx/README index f32de21488e..648faace4f0 100644 --- a/certbot-compatibility-test/nginx/README +++ b/certbot-compatibility-test/nginx/README @@ -4,7 +4,7 @@ Right now, this is data for the roundtrip test (checking that the parser can parse each file and that the reserialized config file it generates is identical to the original). -If run in a virtualenv or otherwise so that certbot_nginx can be imported, +If run in a virtualenv or otherwise so that certbot can be imported, the roundtrip test can run as python roundtrip.py nginx-roundtrip-testdata diff --git a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-10571 b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-10571 index fe95ac8dc0f..c25d06b34d9 100644 --- a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-10571 +++ b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-10571 @@ -61,7 +61,6 @@ server { server { listen 80; server_name random1413.example.org www.random1413.example.org; - server_name random28524.example.org www.random28524.example.org; server_name random25266.example.org www.random25266.example.org; server_name random26791.example.org www.random26791.example.org; diff --git a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-11849 b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-11849 index 8a8c90b7ebb..eb8e002e843 100644 --- a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-11849 +++ b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-11849 @@ -30,7 +30,6 @@ server { server_name www.random3140.example.org; server_name random28398.example.org; server_name random23689.example.org www.random23689.example.org; - server_name random25863.example.org www.random25863.example.org; rewrite ^ http://random3140.example.org$request_uri permanent; } diff --git a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-15291 b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-15291 index fa09bed93a5..833d136cbd5 100644 --- a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-15291 +++ b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-15291 @@ -19,7 +19,6 @@ server { listen 80 default_server; - listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; diff --git a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-19791 b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-19791 index 6e3112ad8dc..8247e8ae2b4 100644 --- a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-19791 +++ b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/79-configs/site-19791 @@ -29,6 +29,5 @@ server { server { server_name www.random1413.example.org; - server_name random28524.example.org www.random28524.example.org; rewrite ^ http://random1413.example.org$request_uri permanent; } diff --git a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/chive/chive-nginx-master/reverse_proxy.conf b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/chive/chive-nginx-master/reverse_proxy.conf index ee0faadd720..fc5014be535 100644 --- a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/chive/chive-nginx-master/reverse_proxy.conf +++ b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/chive/chive-nginx-master/reverse_proxy.conf @@ -2,7 +2,7 @@ ### Configuration for reverse proxy. Passing the necessary headers to ### the backend. Nginx doesn't tunnel the connection, it opens a new -### one. Hence whe need to send these headers to the backend so that +### one. Hence we need to send these headers to the backend so that ### the client(s) IP is available to them. The host is also sent. proxy_set_header X-Real-IP $remote_addr; diff --git a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/chive/chive-nginx-master/win-utf b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/chive/chive-nginx-master/win-utf index d0b7116c8a3..33b87d1a2cb 100644 --- a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/chive/chive-nginx-master/win-utf +++ b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/chive/chive-nginx-master/win-utf @@ -30,7 +30,7 @@ charset_map windows-1251 utf-8 { A4 C2A4 ; # currency sign A5 D290 ; # capital Ukrainian soft G - A6 C2A6 ; # borken bar + A6 C2A6 ; # broken bar A7 C2A7 ; # section sign A8 D081 ; # capital YO A9 C2A9 ; # (C) diff --git a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/piwik/nginx.conf b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/piwik/nginx.conf index 9d67932b906..77b84883f05 100644 --- a/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/piwik/nginx.conf +++ b/certbot-compatibility-test/nginx/nginx-roundtrip-testdata/piwik/nginx.conf @@ -24,7 +24,7 @@ server { # Disallow any usage of piwik assets if referer is non valid. location ~* ^.+\.(?:jpg|png|css|gif|jpeg|js|swf)$ { - # Defining the valid referers. + # Defining the valid referrers. valid_referers none blocked *.mysite.com othersite.com; if ($invalid_referer) { return 444; diff --git a/certbot-compatibility-test/nginx/roundtrip.py b/certbot-compatibility-test/nginx/roundtrip.py index 85d283c7842..a8ee236f595 100644 --- a/certbot-compatibility-test/nginx/roundtrip.py +++ b/certbot-compatibility-test/nginx/roundtrip.py @@ -3,7 +3,8 @@ import os import sys -from certbot_nginx import nginxparser +from certbot._internal.plugins.nginx import nginxparser + def roundtrip(stuff): success = True diff --git a/certbot-compatibility-test/pyproject.toml b/certbot-compatibility-test/pyproject.toml new file mode 100644 index 00000000000..97350e56d56 --- /dev/null +++ b/certbot-compatibility-test/pyproject.toml @@ -0,0 +1,44 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-compatibility-test" +dynamic = ["version"] +description = "Compatibility tests for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", +] +dependencies = [ + "certbot", + "certbot-apache", + "requests", +] + +[project.scripts] +certbot-compatibility-test = "certbot_compatibility_test.test_driver:main" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-compatibility-test/readthedocs.org.requirements.txt b/certbot-compatibility-test/readthedocs.org.requirements.txt deleted file mode 100644 index c2a0c1110d4..00000000000 --- a/certbot-compatibility-test/readthedocs.org.requirements.txt +++ /dev/null @@ -1,13 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-apache --e certbot-compatibility-test[docs] diff --git a/certbot-compatibility-test/setup.cfg b/certbot-compatibility-test/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-compatibility-test/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-compatibility-test/setup.py b/certbot-compatibility-test/setup.py index f519ed42282..11fc858b9b0 100644 --- a/certbot-compatibility-test/setup.py +++ b/certbot-compatibility-test/setup.py @@ -1,65 +1,7 @@ -import sys - from setuptools import setup -from setuptools import find_packages - - -version = '0.31.0.dev0' -install_requires = [ - 'certbot', - 'certbot-apache', - 'mock', - 'six', - 'requests', - 'zope.interface', -] - -if sys.version_info < (2, 7, 9): - # For secure SSL connexion with Python 2.7 (InsecurePlatformWarning) - install_requires.append('ndg-httpsclient') - install_requires.append('pyasn1') - -docs_extras = [ - 'repoze.sphinx.autointerface', - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +version = '5.8.0.dev0' setup( - name='certbot-compatibility-test', version=version, - description="Compatibility tests for Certbot", - url='https://github.com/letsencrypt/letsencrypt', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: Apache Software License', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - ], - - packages=find_packages(), - include_package_data=True, - install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'console_scripts': [ - 'certbot-compatibility-test = certbot_compatibility_test.test_driver:main', - ], - }, ) diff --git a/certbot-compatibility-test/certbot_compatibility_test/__init__.py b/certbot-compatibility-test/src/certbot_compatibility_test/__init__.py similarity index 100% rename from certbot-compatibility-test/certbot_compatibility_test/__init__.py rename to certbot-compatibility-test/src/certbot_compatibility_test/__init__.py diff --git a/certbot-compatibility-test/certbot_compatibility_test/configurators/__init__.py b/certbot-compatibility-test/src/certbot_compatibility_test/configurators/__init__.py similarity index 100% rename from certbot-compatibility-test/certbot_compatibility_test/configurators/__init__.py rename to certbot-compatibility-test/src/certbot_compatibility_test/configurators/__init__.py diff --git a/certbot-compatibility-test/certbot_compatibility_test/configurators/apache/__init__.py b/certbot-compatibility-test/src/certbot_compatibility_test/configurators/apache/__init__.py similarity index 100% rename from certbot-compatibility-test/certbot_compatibility_test/configurators/apache/__init__.py rename to certbot-compatibility-test/src/certbot_compatibility_test/configurators/apache/__init__.py diff --git a/certbot-compatibility-test/src/certbot_compatibility_test/configurators/apache/common.py b/certbot-compatibility-test/src/certbot_compatibility_test/configurators/apache/common.py new file mode 100644 index 00000000000..d6339e22c8e --- /dev/null +++ b/certbot-compatibility-test/src/certbot_compatibility_test/configurators/apache/common.py @@ -0,0 +1,106 @@ +"""Provides a common base for Apache proxies""" +import argparse +import os +import shutil +import subprocess +from unittest import mock + +from certbot import configuration +from certbot import errors as le_errors +from certbot import util as certbot_util +from certbot_apache._internal import entrypoint +from certbot_compatibility_test import errors +from certbot_compatibility_test import util +from certbot_compatibility_test.configurators import common as configurators_common + + +class Proxy(configurators_common.Proxy): + """A common base for Apache test configurators""" + + def __init__(self, args: argparse.Namespace) -> None: + """Initializes the plugin with the given command line args""" + super().__init__(args) + self.le_config.apache_le_vhost_ext = "-le-ssl.conf" + + self.modules = self.server_root = self.test_conf = self.version = None + patch = mock.patch( + "certbot._internal.plugins.apache.configurator.display_ops.select_vhost") + mock_display = patch.start() + mock_display.side_effect = le_errors.PluginError( + "Unable to determine vhost") + + def load_config(self) -> str: + """Loads the next configuration for the plugin to test""" + config = super().load_config() + self._all_names, self._test_names = _get_names(config) + + server_root = _get_server_root(config) + shutil.rmtree("/etc/apache2") + shutil.copytree(server_root, "/etc/apache2", symlinks=True) + + self._prepare_configurator() + + try: + subprocess.check_call("apachectl -k restart".split()) + except errors.Error: + raise errors.Error( + "Apache failed to load {0} before tests started".format( + config)) + + return config + + def _prepare_configurator(self) -> None: + """Prepares the Apache plugin for testing""" + for k in entrypoint.ENTRYPOINT.OS_DEFAULTS.__dict__.keys(): + setattr(self.le_config, "apache_" + k, + getattr(entrypoint.ENTRYPOINT.OS_DEFAULTS, k)) + + self._configurator = entrypoint.ENTRYPOINT( + config=configuration.NamespaceConfig(self.le_config), + name="apache") + self._configurator.prepare() + + def cleanup_from_tests(self) -> None: + """Performs any necessary cleanup from running plugin tests""" + super().cleanup_from_tests() + mock.patch.stopall() + + +def _get_server_root(config: str) -> str: + """Returns the server root directory in config""" + subdirs = [ + name for name in os.listdir(config) + if os.path.isdir(os.path.join(config, name))] + + if len(subdirs) != 1: + raise errors.Error("Malformed configuration directory {0}".format(config)) + + return os.path.join(config, subdirs[0].rstrip()) + + +def _get_names(config: str) -> tuple[set[str], set[str]]: + """Returns all and testable domain names in config""" + all_names = set() + non_ip_names = set() + with open(os.path.join(config, "vhosts")) as f: + for line in f: + # If parsing a specific vhost + if line[0].isspace(): + words = line.split() + if words[0] == "alias": + all_names.add(words[1]) + non_ip_names.add(words[1]) + # If for port 80 and not IP vhost + elif words[1] == "80" and not util.IP_REGEX.match(words[3]): + all_names.add(words[3]) + non_ip_names.add(words[3]) + elif "NameVirtualHost" not in line: + words = line.split() + if (words[0].endswith("*") or words[0].endswith("80") and + not util.IP_REGEX.match(words[1]) and + words[1].find(".") != -1): + all_names.add(words[1]) + return ( + certbot_util.get_filtered_names(all_names), + certbot_util.get_filtered_names(non_ip_names) + ) diff --git a/certbot-compatibility-test/src/certbot_compatibility_test/configurators/common.py b/certbot-compatibility-test/src/certbot_compatibility_test/configurators/common.py new file mode 100644 index 00000000000..713a277f10b --- /dev/null +++ b/certbot-compatibility-test/src/certbot_compatibility_test/configurators/common.py @@ -0,0 +1,156 @@ +"""Provides a common base for configurator proxies""" +from abc import abstractmethod +import argparse +import logging +import os +import shutil +import tempfile +from typing import Iterable +from typing import Optional +from typing import overload +from typing import Union + +from acme import challenges +from acme.challenges import Challenge +from certbot._internal import constants +from certbot.achallenges import AnnotatedChallenge +from certbot.plugins import common +from certbot_compatibility_test import errors +from certbot_compatibility_test import interfaces +from certbot_compatibility_test import util + +logger = logging.getLogger(__name__) + + +class Proxy(interfaces.ConfiguratorProxy): + """A common base for compatibility test configurators""" + + @classmethod + def add_parser_arguments(cls, parser: argparse.ArgumentParser) -> None: + """Adds command line arguments needed by the plugin""" + + def __init__(self, args: argparse.Namespace) -> None: + """Initializes the plugin with the given command line args""" + super().__init__(args) + self._temp_dir = tempfile.mkdtemp() + # tempfile.mkdtemp() creates folders with too restrictive permissions to be accessible + # to an Apache worker, leading to HTTP challenge failures. Let's fix that. + os.chmod(self._temp_dir, 0o755) + self.le_config = util.create_le_config(self._temp_dir) + config_dir = util.extract_configs(args.configs, self._temp_dir) + self._configs = [ + os.path.join(config_dir, config) + for config in os.listdir(config_dir)] + + self.args = args + self.http_port = 80 + self.https_port = 443 + self._configurator: common.Configurator + self._all_names: Optional[set[str]] = None + self._test_names: Optional[set[str]] = None + + def has_more_configs(self) -> bool: + """Returns true if there are more configs to test""" + return bool(self._configs) + + @abstractmethod + def cleanup_from_tests(self) -> None: + """Performs any necessary cleanup from running plugin tests""" + + def load_config(self) -> str: + """Returns the next config directory to be tested""" + shutil.rmtree(self.le_config.work_dir, ignore_errors=True) + backup = os.path.join(self.le_config.work_dir, constants.BACKUP_DIR) + os.makedirs(backup) + return self._configs.pop() + + @overload + def copy_certs_and_keys(self, cert_path: str, key_path: str, + chain_path: str) -> tuple[str, str, str]: ... + + @overload + def copy_certs_and_keys(self, cert_path: str, key_path: str, + chain_path: Optional[str]) -> tuple[str, str, Optional[str]]: ... + + def copy_certs_and_keys(self, cert_path: str, key_path: str, + chain_path: Optional[str] = None) -> tuple[str, str, Optional[str]]: + """Copies certs and keys into the temporary directory""" + cert_and_key_dir = os.path.join(self._temp_dir, "certs_and_keys") + if not os.path.isdir(cert_and_key_dir): + os.mkdir(cert_and_key_dir) + + cert = os.path.join(cert_and_key_dir, "cert") + shutil.copy(cert_path, cert) + key = os.path.join(cert_and_key_dir, "key") + shutil.copy(key_path, key) + chain = None + if chain_path: + chain = os.path.join(cert_and_key_dir, "chain") + shutil.copy(chain_path, chain) + + return cert, key, chain + + def get_all_names_answer(self) -> set[str]: + """Returns the set of domain names that the plugin should find""" + if self._all_names: + return self._all_names + raise errors.Error("No configuration file loaded") + + def get_testable_domain_names(self) -> set[str]: + """Returns the set of domain names that can be tested against""" + if self._test_names: + return self._test_names + return {"example.com"} + + def deploy_cert(self, domain: str, cert_path: str, key_path: str, chain_path: str, + fullchain_path: str) -> None: + """Installs cert""" + cert_path, key_path, chain_path = self.copy_certs_and_keys(cert_path, key_path, chain_path) + if not self._configurator: + raise ValueError("Configurator plugin is not set.") + self._configurator.deploy_cert( + domain, cert_path, key_path, chain_path, fullchain_path) + + def cleanup(self, achalls: list[AnnotatedChallenge]) -> None: + self._configurator.cleanup(achalls) + + def config_test(self) -> None: + self._configurator.config_test() + + def enhance(self, domain: str, enhancement: str, + options: Optional[Union[list[str], str]] = None) -> None: + self._configurator.enhance(domain, enhancement, options) + + def get_all_names(self) -> Iterable[str]: + return self._configurator.get_all_names() + + def get_chall_pref(self, identifier: str) -> Iterable[type[Challenge]]: + return self._configurator.get_chall_pref(identifier) + + @classmethod + def inject_parser_options(cls, parser: argparse.ArgumentParser, name: str) -> None: + pass + + def more_info(self) -> str: + return self._configurator.more_info() + + def perform(self, achalls: list[AnnotatedChallenge]) -> list[challenges.ChallengeResponse]: + return self._configurator.perform(achalls) + + def prepare(self) -> None: + self._configurator.prepare() + + def recovery_routine(self) -> None: + self._configurator.recovery_routine() + + def restart(self) -> None: + self._configurator.restart() + + def rollback_checkpoints(self, rollback: int = 1) -> None: + self._configurator.rollback_checkpoints(rollback) + + def save(self, title: Optional[str] = None, temporary: bool = False) -> None: + self._configurator.save(title, temporary) + + def supported_enhancements(self) -> list[str]: + return self._configurator.supported_enhancements() diff --git a/certbot-compatibility-test/certbot_compatibility_test/configurators/nginx/__init__.py b/certbot-compatibility-test/src/certbot_compatibility_test/configurators/nginx/__init__.py similarity index 100% rename from certbot-compatibility-test/certbot_compatibility_test/configurators/nginx/__init__.py rename to certbot-compatibility-test/src/certbot_compatibility_test/configurators/nginx/__init__.py diff --git a/certbot-compatibility-test/src/certbot_compatibility_test/configurators/nginx/common.py b/certbot-compatibility-test/src/certbot_compatibility_test/configurators/nginx/common.py new file mode 100644 index 00000000000..e0110920f4f --- /dev/null +++ b/certbot-compatibility-test/src/certbot_compatibility_test/configurators/nginx/common.py @@ -0,0 +1,87 @@ +"""Provides a common base for Nginx proxies""" +import os +import shutil +import subprocess + +from certbot import configuration +from certbot._internal.plugins.nginx import configurator +from certbot._internal.plugins.nginx import constants +from certbot_compatibility_test import errors +from certbot_compatibility_test import util +from certbot_compatibility_test.configurators import common as configurators_common + + +class Proxy(configurators_common.Proxy): + """A common base for Nginx test configurators""" + + def load_config(self) -> str: + """Loads the next configuration for the plugin to test""" + config = super().load_config() + self._all_names, self._test_names = _get_names(config) + + server_root = _get_server_root(config) + + # XXX: Deleting all of this is kind of scary unless the test + # instances really each have a complete configuration! + shutil.rmtree("/etc/nginx") + shutil.copytree(server_root, "/etc/nginx", symlinks=True) + + self._prepare_configurator() + + try: + subprocess.check_call("service nginx reload".split()) + except errors.Error: + raise errors.Error( + "Nginx failed to load {0} before tests started".format( + config)) + + return config + + def _prepare_configurator(self) -> None: + """Prepares the Nginx plugin for testing""" + for k in constants.CLI_DEFAULTS: + setattr(self.le_config, "nginx_" + k, constants.os_constant(k)) + + conf = configuration.NamespaceConfig(self.le_config) + self._configurator = configurator.NginxConfigurator(config=conf, name="nginx") + self._configurator.prepare() + + def cleanup_from_tests(self) -> None: + """Performs any necessary cleanup from running plugin tests""" + + +def _get_server_root(config: str) -> str: + """Returns the server root directory in config""" + subdirs = [ + name for name in os.listdir(config) + if os.path.isdir(os.path.join(config, name))] + + if len(subdirs) != 1: + raise errors.Error("Malformed configuration directory {0}".format(config)) + + return os.path.join(config, subdirs[0].rstrip()) + + +def _get_names(config: str) -> tuple[set[str], set[str]]: + """Returns all and testable domain names in config""" + all_names: set[str] = set() + for root, _dirs, files in os.walk(config): + for this_file in files: + update_names = _get_server_names(root, this_file) + all_names.update(update_names) + non_ip_names = {n for n in all_names if not util.IP_REGEX.match(n)} + return all_names, non_ip_names + + +def _get_server_names(root: str, filename: str) -> set[str]: + """Returns all names in a config file path""" + all_names = set() + with open(os.path.join(root, filename)) as f: + for line in f: + if line.strip().startswith("server_name"): + names = line.partition("server_name")[2].rpartition(";")[0] + for n in names.split(): + # Filter out wildcards in both all_names and test_names + if not n.startswith("*."): + all_names.add(n) + return all_names diff --git a/certbot-compatibility-test/certbot_compatibility_test/errors.py b/certbot-compatibility-test/src/certbot_compatibility_test/errors.py similarity index 100% rename from certbot-compatibility-test/certbot_compatibility_test/errors.py rename to certbot-compatibility-test/src/certbot_compatibility_test/errors.py diff --git a/certbot-compatibility-test/src/certbot_compatibility_test/interfaces.py b/certbot-compatibility-test/src/certbot_compatibility_test/interfaces.py new file mode 100644 index 00000000000..a47a85ae23f --- /dev/null +++ b/certbot-compatibility-test/src/certbot_compatibility_test/interfaces.py @@ -0,0 +1,64 @@ +"""Certbot compatibility test interfaces""" +from abc import ABCMeta +from abc import abstractmethod +import argparse +from typing import cast + +from certbot import interfaces +from certbot.configuration import NamespaceConfig + + +class PluginProxy(interfaces.Plugin, metaclass=ABCMeta): + """Wraps a Certbot plugin""" + + http_port: int = NotImplemented + """The port to connect to on localhost for HTTP traffic""" + + https_port: int = NotImplemented + """The port to connect to on localhost for HTTPS traffic""" + + @classmethod + @abstractmethod + def add_parser_arguments(cls, parser: argparse.ArgumentParser) -> None: + """Adds command line arguments needed by the parser""" + + @abstractmethod + def __init__(self, args: argparse.Namespace) -> None: + """Initializes the plugin with the given command line args""" + super().__init__(cast(NamespaceConfig, args), 'proxy') + + @abstractmethod + def cleanup_from_tests(self) -> None: + """Performs any necessary cleanup from running plugin tests. + + This is guaranteed to be called before the program exits. + + """ + + @abstractmethod + def has_more_configs(self) -> bool: + """Returns True if there are more configs to test""" + + @abstractmethod + def load_config(self) -> str: + """Loads the next config and returns its name""" + + @abstractmethod + def get_testable_domain_names(self) -> set[str]: + """Returns the domain names that can be used in testing""" + + +class AuthenticatorProxy(PluginProxy, interfaces.Authenticator, metaclass=ABCMeta): + """Wraps a Certbot authenticator""" + + +class InstallerProxy(PluginProxy, interfaces.Installer, metaclass=ABCMeta): + """Wraps a Certbot installer""" + + @abstractmethod + def get_all_names_answer(self) -> set[str]: + """Returns all names that should be found by the installer""" + + +class ConfiguratorProxy(AuthenticatorProxy, InstallerProxy, metaclass=ABCMeta): + """Wraps a Certbot configurator""" diff --git a/certbot-compatibility-test/src/certbot_compatibility_test/test_driver.py b/certbot-compatibility-test/src/certbot_compatibility_test/test_driver.py new file mode 100644 index 00000000000..384daa9fcf3 --- /dev/null +++ b/certbot-compatibility-test/src/certbot_compatibility_test/test_driver.py @@ -0,0 +1,401 @@ +"""Tests Certbot plugins against different server configurations.""" +import argparse +import contextlib +import filecmp +import logging +import os +import shutil +import socket +import sys +import tempfile +import time +from typing import Any +from typing import Generator +from typing import Iterable +from typing import Optional + +from cryptography.hazmat.primitives import serialization +from urllib3.util import connection + +from acme import challenges +from acme import messages +from certbot import achallenges +from certbot import errors as le_errors +from certbot._internal.display import obj as display_obj +from certbot.tests import acme_util +from certbot_compatibility_test import errors +from certbot_compatibility_test import util +from certbot_compatibility_test import validator +from certbot_compatibility_test.configurators import common +from certbot_compatibility_test.configurators.apache import common as a_common +from certbot_compatibility_test.configurators.nginx import common as n_common + +DESCRIPTION = """ +Tests Certbot plugins against different server configurations. It is +assumed that Docker is already installed. If no test type is specified, all +tests that the plugin supports are performed. + +""" + +PLUGINS: dict[str, type[common.Proxy]] = {"apache": a_common.Proxy, "nginx": n_common.Proxy} + + +logger = logging.getLogger(__name__) + + +def test_authenticator(plugin: common.Proxy, config: str, temp_dir: str) -> bool: + """Tests authenticator, returning True if the tests are successful""" + backup = _create_backup(config, temp_dir) + + achalls = _create_achalls(plugin) + if not achalls: + logger.error("The plugin and this program support no common " + "challenge types") + return False + + try: + responses = plugin.perform(achalls) + except le_errors.Error: + logger.error("Performing challenges on %s caused an error:", config, exc_info=True) + return False + + success = True + for i, response in enumerate(responses): + achall = achalls[i] + if not response: + logger.error( + "Plugin failed to complete %s for %s in %s", + type(achall), achall.identifier.value, config) + success = False + elif isinstance(response, challenges.HTTP01Response): + # We fake the DNS resolution to ensure that any domain is resolved + # to the local HTTP server setup for the compatibility tests + with _fake_dns_resolution("127.0.0.1"): + verified = response.simple_verify( + achall.chall, achall.identifier.value, + util.JWK.public_key(), port=plugin.http_port) + if verified: + logger.info( + "http-01 verification for %s succeeded", achall.identifier.value) + else: + logger.error( + "**** http-01 verification for %s in %s failed", + achall.identifier.value, config) + success = False + + if success: + try: + plugin.cleanup(achalls) + except le_errors.Error: + logger.error("Challenge cleanup for %s caused an error:", config, exc_info=True) + success = False + + if _dirs_are_unequal(config, backup): + logger.error("Challenge cleanup failed for %s", config) + return False + logger.info("Challenge cleanup succeeded") + + return success + + +def _create_achalls(plugin: common.Proxy) -> list[achallenges.AnnotatedChallenge]: + """Returns a list of annotated challenges to test on plugin""" + achalls: list[achallenges.AnnotatedChallenge] = [] + names = plugin.get_testable_domain_names() + for domain in names: + prefs = plugin.get_chall_pref(domain) + for chall_type in prefs: + if chall_type == challenges.HTTP01: + # challenges.HTTP01.TOKEN_SIZE is a float but os.urandom + # expects an integer. + chall = challenges.HTTP01( + token=os.urandom(int(challenges.HTTP01.TOKEN_SIZE))) + challb = acme_util.chall_to_challb( + chall, messages.STATUS_PENDING) + achall = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=challb, domain=domain, account_key=util.JWK) + achalls.append(achall) + + return achalls + + +def test_installer(args: argparse.Namespace, plugin: common.Proxy, config: str, + temp_dir: str) -> bool: + """Tests plugin as an installer""" + backup = _create_backup(config, temp_dir) + + names_match = plugin.get_all_names() == plugin.get_all_names_answer() + if names_match: + logger.info("get_all_names test succeeded") + else: + logger.error("**** get_all_names test failed for config %s", config) + + domains = list(plugin.get_testable_domain_names()) + success = test_deploy_cert(plugin, temp_dir, domains) + + if success and args.enhance: + success = test_enhancements(plugin, domains) + + good_rollback = test_rollback(plugin, config, backup) + return names_match and success and good_rollback + + +def test_deploy_cert(plugin: common.Proxy, temp_dir: str, domains: list[str]) -> bool: + """Tests deploy_cert returning True if the tests are successful""" + cert = util.make_self_signed_cert(util.KEY, domains) + cert_path = os.path.join(temp_dir, "cert.pem") + with open(cert_path, "wb") as f: + f.write(cert.public_bytes(serialization.Encoding.PEM)) + + for domain in domains: + try: + plugin.deploy_cert(domain, cert_path, util.KEY_PATH, cert_path, cert_path) + plugin.save() # Needed by the Apache plugin + except le_errors.Error: + logger.error("**** Plugin failed to deploy certificate for %s:", domain, exc_info=True) + return False + + if not _save_and_restart(plugin, "deployed"): + return False + + success = True + time.sleep(3) + for domain in domains: + verified = validator.Validator().certificate( + cert, domain, "127.0.0.1", plugin.https_port) + if not verified: + logger.error("**** Could not verify certificate for domain %s", domain) + success = False + + if success: + logger.info("HTTPS validation succeeded") + + return success + + +def test_enhancements(plugin: common.Proxy, domains: Iterable[str]) -> bool: + """Tests supported enhancements returning True if successful""" + supported = plugin.supported_enhancements() + + if "redirect" not in supported: + logger.error("The plugin and this program support no common " + "enhancements") + return False + + domains_and_info: list[tuple[str, list[bool]]] = [(domain, []) for domain in domains] + + for domain, info in domains_and_info: + try: + previous_redirect = validator.Validator().any_redirect( + "localhost", plugin.http_port, headers={"Host": domain}) + info.append(previous_redirect) + plugin.enhance(domain, "redirect") + plugin.save() # Needed by the Apache plugin + except le_errors.PluginError as error: + # Don't immediately fail because a redirect may already be enabled + logger.warning("*** Plugin failed to enable redirect for %s:", domain) + logger.warning("%s", error) + except le_errors.Error: + logger.error("*** An error occurred while enabling redirect for %s:", + domain, exc_info=True) + + if not _save_and_restart(plugin, "enhanced"): + return False + + success = True + for domain, info in domains_and_info: + previous_redirect = info[0] + if not previous_redirect: + verified = validator.Validator().redirect( + "localhost", plugin.http_port, headers={"Host": domain}) + if not verified: + logger.error("*** Improper redirect for domain %s", domain) + success = False + + if success: + logger.info("Enhancements test succeeded") + + return success + + +def _save_and_restart(plugin: common.Proxy, title: Optional[str] = None) -> bool: + """Saves and restart the plugin, returning True if no errors occurred""" + try: + plugin.save(title) + plugin.restart() + return True + except le_errors.Error: + logger.error("*** Plugin failed to save and restart server:", exc_info=True) + return False + + +def test_rollback(plugin: common.Proxy, config: str, backup: str) -> bool: + """Tests the rollback checkpoints function""" + try: + plugin.rollback_checkpoints(1337) + except le_errors.Error: + logger.error("*** Plugin raised an exception during rollback:", exc_info=True) + return False + + if _dirs_are_unequal(config, backup): + logger.error("*** Rollback failed for config `%s`", config) + return False + logger.info("Rollback succeeded") + return True + + +def _create_backup(config: str, temp_dir: str) -> str: + """Creates a backup of config in temp_dir""" + backup = os.path.join(temp_dir, "backup") + shutil.rmtree(backup, ignore_errors=True) + shutil.copytree(config, backup, symlinks=True) + + return backup + + +def _dirs_are_unequal(dir1: str, dir2: str) -> bool: + """Returns True if dir1 and dir2 are unequal""" + dircmps = [filecmp.dircmp(dir1, dir2)] + while dircmps: + dircmp = dircmps.pop() + if dircmp.left_only or dircmp.right_only: + logger.error("The following files and directories are only " + "present in one directory") + if dircmp.left_only: + logger.error(str(dircmp.left_only)) + else: + logger.error(str(dircmp.right_only)) + return True + elif dircmp.common_funny or dircmp.funny_files: + logger.error("The following files and directories could not be " + "compared:") + if dircmp.common_funny: + logger.error(str(dircmp.common_funny)) + else: + logger.error(str(dircmp.funny_files)) + return True + elif dircmp.diff_files: + logger.error("The following files differ:") + logger.error(str(dircmp.diff_files)) + return True + + for subdir in dircmp.subdirs.values(): + dircmps.append(subdir) + + return False + + +def get_args() -> argparse.Namespace: + """Returns parsed command line arguments.""" + parser = argparse.ArgumentParser( + description=DESCRIPTION, + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + group = parser.add_argument_group("general") + group.add_argument( + "-c", "--configs", default="configs.tar.gz", + help="a directory or tarball containing server configurations") + group.add_argument( + "-p", "--plugin", default="apache", help="the plugin to be tested") + group.add_argument( + "-v", "--verbose", dest="verbose_count", action="count", + default=0, help="you know how to use this") + group.add_argument( + "-a", "--auth", action="store_true", + help="tests the challenges the plugin supports") + group.add_argument( + "-i", "--install", action="store_true", + help="tests the plugin as an installer") + group.add_argument( + "-e", "--enhance", action="store_true", help="tests the enhancements " + "the plugin supports (implicitly includes installer tests)") + + for plugin in PLUGINS.values(): + plugin.add_parser_arguments(parser) + + args = parser.parse_args() + if args.enhance: + args.install = True + elif not (args.auth or args.install): + args.auth = args.install = args.enhance = True + + return args + + +def setup_logging(args: argparse.Namespace) -> None: + """Prepares logging for the program""" + handler = logging.StreamHandler() + + root_logger = logging.getLogger() + root_logger.setLevel(logging.ERROR - args.verbose_count * 10) + root_logger.addHandler(handler) + + +def setup_display() -> None: + """"Prepares a display utility instance for the Certbot plugins """ + displayer = display_obj.NoninteractiveDisplay(sys.stdout) + display_obj.set_display(displayer) + + +def main() -> None: + """Main test script execution.""" + args = get_args() + setup_logging(args) + setup_display() + + if args.plugin not in PLUGINS: + raise errors.Error("Unknown plugin {0}".format(args.plugin)) + + temp_dir = tempfile.mkdtemp() + plugin = PLUGINS[args.plugin](args) + try: + overall_success = True + while plugin.has_more_configs(): + success = True + + try: + config = plugin.load_config() + logger.info("Loaded configuration: %s", config) + if args.auth: + success = test_authenticator(plugin, config, temp_dir) + if success and args.install: + success = test_installer(args, plugin, config, temp_dir) + except errors.Error: + logger.error("Tests on %s raised:", config, exc_info=True) + success = False + + if success: + logger.info("All tests on %s succeeded", config) + else: + overall_success = False + logger.error("Tests on %s failed", config) + finally: + plugin.cleanup_from_tests() + + if overall_success: + logger.warning("All compatibility tests succeeded") + sys.exit(0) + else: + logger.warning("One or more compatibility tests failed") + sys.exit(1) + + +@contextlib.contextmanager +def _fake_dns_resolution(resolved_ip: str) -> Generator[None, None, None]: + """Monkey patch urllib3 to make any hostname be resolved to the provided IP""" + _original_create_connection = connection.create_connection + + def _patched_create_connection(address: tuple[str, int], + *args: Any, **kwargs: Any) -> socket.socket: + _, port = address + return _original_create_connection((resolved_ip, port), *args, **kwargs) + + try: + connection.create_connection = _patched_create_connection + yield + finally: + connection.create_connection = _original_create_connection + + +if __name__ == "__main__": + main() diff --git a/certbot-compatibility-test/certbot_compatibility_test/testdata/apache.tar.gz b/certbot-compatibility-test/src/certbot_compatibility_test/testdata/apache.tar.gz similarity index 100% rename from certbot-compatibility-test/certbot_compatibility_test/testdata/apache.tar.gz rename to certbot-compatibility-test/src/certbot_compatibility_test/testdata/apache.tar.gz diff --git a/certbot-compatibility-test/src/certbot_compatibility_test/testdata/empty_cert.pem b/certbot-compatibility-test/src/certbot_compatibility_test/testdata/empty_cert.pem new file mode 100644 index 00000000000..9404a443789 --- /dev/null +++ b/certbot-compatibility-test/src/certbot_compatibility_test/testdata/empty_cert.pem @@ -0,0 +1,17 @@ +-----BEGIN CERTIFICATE----- +MIICqDCCAZACCQCRC1UKg2WfRTANBgkqhkiG9w0BAQsFADAWMRQwEgYDVQQDDAtl +eGFtcGxlLmNvbTAeFw0yMDA4MTkyMzM5MjdaFw0yMDA5MTgyMzM5MjdaMBYxFDAS +BgNVBAMMC2V4YW1wbGUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC +AQEA5tViHnJx4y+BbCb8Qz9uxsnqp1ynONR7ET/XL+M/jQ4xPeJg4L2uZ3YnogPc +WdEoey17WXBg3KRqKfg+7PqIdGqVeonSCfXhD1HoGJRsThSUJ2fK3uoQ+zGgJTWR +FYWa8Cb6xsuq0xaYtw2jaJBp+697Np60PWs4pY5FkadT50wZ0TYDnYt3NSAdn+Pt +j3cpI4ocZZ2FLiOFn+UFOaRcetGtpnU1QwvmygD9tiL7kJ55B4CWGEv6DMRQk/UE +eMUETzse1NkVlaxQ1TCd5iAfBTluiV30EpmmWa+OsXJWxCK+EEOkXD1r3CdXAldY +nRYxJrn4udrFe69QX95wiRZNXwIDAQABMA0GCSqGSIb3DQEBCwUAA4IBAQCJvtDC +875CK7SKNf006gSciXsNPNSVORGPjc/5OQ23baK4iPhxftI4LGZN8773N14jWp3E +QnQLL1gZ9/G+98SlI5lm97a4m4XZyNaULbmQwRKgI22H0F1AWbvsG0SppjnhVlJ+ +93ZUqSQBXgbXelFHSsNfk1AB6Kvo6+UvS8s0vkz7SfkPOZGx0b+3RJSJZnZHvYih +ggudN/jJggSgRrb+F6lpaelJE9pZsznJFb9R7mFI33AGBpQWV4r3p1ZbM1vGMqGc +4PGBzDzi28BhLBplSOPZZxqRiINQzGiQ5T2SfN06usr7EafFr6+7YKNhgrCdlVjU +thzJ5MgHZgALNXsh +-----END CERTIFICATE----- diff --git a/certbot-compatibility-test/src/certbot_compatibility_test/testdata/nginx.tar.gz b/certbot-compatibility-test/src/certbot_compatibility_test/testdata/nginx.tar.gz new file mode 100644 index 00000000000..556e0497703 Binary files /dev/null and b/certbot-compatibility-test/src/certbot_compatibility_test/testdata/nginx.tar.gz differ diff --git a/certbot-compatibility-test/src/certbot_compatibility_test/util.py b/certbot-compatibility-test/src/certbot_compatibility_test/util.py new file mode 100644 index 00000000000..d6cbf8321e0 --- /dev/null +++ b/certbot-compatibility-test/src/certbot_compatibility_test/util.py @@ -0,0 +1,102 @@ +"""Utility functions for Certbot plugin tests.""" +import argparse +import copy +from datetime import datetime, timedelta, timezone +import os +import re +import shutil +import tarfile + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives.asymmetric import types +import josepy as jose + +from certbot._internal import constants +from certbot.tests import util as test_util +from certbot_compatibility_test import errors + +_KEY_BASE = "rsa2048_key.pem" +KEY_PATH = test_util.vector_path(_KEY_BASE) +KEY = test_util.load_rsa_private_key_pem(_KEY_BASE) +JWK = jose.JWKRSA(key=test_util.load_jose_rsa_private_key_pem(_KEY_BASE)) +IP_REGEX = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") + + +def create_le_config(parent_dir: str) -> argparse.Namespace: + """Sets up LE dirs in parent_dir and returns the config dict""" + config = copy.deepcopy(constants.CLI_DEFAULTS) + + le_dir = os.path.join(parent_dir, "certbot") + os.mkdir(le_dir) + for dir_name in ("config", "logs", "work"): + full_path = os.path.join(le_dir, dir_name) + os.mkdir(full_path) + full_name = dir_name + "_dir" + config[full_name] = full_path + + config["domains"] = None + + return argparse.Namespace(**config) + + +def extract_configs(configs: str, parent_dir: str) -> str: + """Extracts configs to a new dir under parent_dir and returns it""" + config_dir = os.path.join(parent_dir, "configs") + + if os.path.isdir(configs): + shutil.copytree(configs, config_dir, symlinks=True) + elif tarfile.is_tarfile(configs): + with tarfile.open(configs, "r") as tar: + tar.extractall(config_dir) + else: + raise errors.Error("Unknown configurations file type") + + return config_dir + + +def make_self_signed_cert(private_key: types.CertificateIssuerPrivateKeyTypes, + domains: list[str], + ) -> x509.Certificate: + """Generate new self-signed certificate. + + :param buffer private_key_pem: Private key, in PEM PKCS#8 format. + :type domains: `list` of `str` + :param buffer private_key_pem: One of + `cryptography.hazmat.primitives.asymmetric.types.CertificateIssuerPrivateKeyTypes` + + If more than one domain is provided, all of the domains are put into + ``subjectAltName`` X.509 extension and first domain is set as the + subject CN. If only one domain is provided no ``subjectAltName`` + extension is used. + + """ + assert domains, "Must provide one or more hostnames for the cert." + + builder = x509.CertificateBuilder() + builder = builder.serial_number(x509.random_serial_number()) + + builder = builder.add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + + name_attrs = [x509.NameAttribute(x509.OID_COMMON_NAME, domains[0])] + + builder = builder.subject_name(x509.Name(name_attrs)) + builder = builder.issuer_name(x509.Name(name_attrs)) + + sanlist: list[x509.GeneralName] = [] + for address in domains: + sanlist.append(x509.DNSName(address)) + if len(domains) > 1: + builder = builder.add_extension( + x509.SubjectAlternativeName(sanlist), + critical=False + ) + + not_before = datetime.now(tz=timezone.utc) + validity = timedelta(seconds=7 * 24 * 60 * 60) + builder = builder.not_valid_before(not_before) + builder = builder.not_valid_after(not_before + validity) + + public_key = private_key.public_key() + builder = builder.public_key(public_key) + return builder.sign(private_key, hashes.SHA256()) diff --git a/certbot-compatibility-test/src/certbot_compatibility_test/validator.py b/certbot-compatibility-test/src/certbot_compatibility_test/validator.py new file mode 100644 index 00000000000..1e1139514b2 --- /dev/null +++ b/certbot-compatibility-test/src/certbot_compatibility_test/validator.py @@ -0,0 +1,175 @@ +"""Validators to determine the current webserver configuration""" +import contextlib +import logging +import socket +from typing import cast +from typing import Mapping +from typing import Optional +from typing import Union + +from cryptography import x509 +from OpenSSL import SSL +import requests + +from acme import errors as acme_errors + +logger = logging.getLogger(__name__) + + +_VALIDATION_TIMEOUT = 10 + + +class Validator: + """Collection of functions to test a live webserver's configuration""" + + def certificate(self, cert: x509.Certificate, name: Union[str, bytes], + alt_host: Optional[str] = None, port: int = 443) -> bool: + """Verifies the certificate presented at name is cert""" + if alt_host is None: + # In fact, socket.gethostbyname accepts both bytes and str, but types do not know that. + host = socket.gethostbyname(cast(str, name)).encode() + elif isinstance(alt_host, bytes): + host = alt_host + else: + host = alt_host.encode() + name = name if isinstance(name, bytes) else name.encode() + + try: + presented_cert = _probe_sni(name, host, port) + except acme_errors.Error as error: + logger.exception(str(error)) + return False + + return presented_cert == cert + + def redirect(self, name: str, port: int = 80, + headers: Optional[Mapping[str, str]] = None) -> bool: + """Test whether webserver redirects to secure connection.""" + url = "http://{0}:{1}".format(name, port) + if headers: + response = requests.get(url, headers=headers, + allow_redirects=False, + timeout=_VALIDATION_TIMEOUT) + else: + response = requests.get(url, allow_redirects=False, + timeout=_VALIDATION_TIMEOUT) + + redirect_location = response.headers.get("location", "") + # We're checking that the redirect we added behaves correctly. + # It's okay for some server configuration to redirect to an + # http URL, as long as it's on some other domain. + if not redirect_location.startswith("https://"): + return False + + if response.status_code != 301: + logger.error("Server did not redirect with permanent code") + return False + + return True + + def any_redirect(self, name: str, port: int = 80, + headers: Optional[Mapping[str, str]] = None) -> bool: + """Test whether webserver redirects.""" + url = "http://{0}:{1}".format(name, port) + if headers: + response = requests.get(url, headers=headers, + allow_redirects=False, + timeout=_VALIDATION_TIMEOUT) + else: + response = requests.get(url, allow_redirects=False, + timeout=_VALIDATION_TIMEOUT) + + return response.status_code in range(300, 309) + + def hsts(self, name: str) -> bool: + """Test for HTTP Strict Transport Security header""" + headers = requests.get("https://" + name, + timeout=_VALIDATION_TIMEOUT).headers + hsts_header = headers.get("strict-transport-security") + + if not hsts_header: + return False + + # Split directives following RFC6797, section 6.1 + directives = [d.split("=") for d in hsts_header.split(";")] + max_age = [d for d in directives if d[0] == "max-age"] + + if not max_age: + logger.error("Server responded with invalid HSTS header field") + return False + + try: + max_age_value = int(max_age[0][1]) + except ValueError: + logger.error("Server responded with invalid HSTS header field") + return False + + # Test whether HSTS does not expire for at least two weeks. + if max_age_value <= (2 * 7 * 24 * 3600): + logger.error("HSTS should not expire in less than two weeks") + return False + + return True + + def ocsp_stapling(self, name: str) -> None: + """Verify ocsp stapling for domain.""" + raise NotImplementedError() + + + +def _probe_sni(name: bytes, host: bytes, port: int = 443) -> x509.Certificate: + """Probe SNI server for SSL certificate. + + :param bytes name: Byte string to send as the server name in the + client hello message. + :param bytes host: Host to connect to. + :param int port: Port to connect to. + + :raises acme.errors.Error: In case of any problems. + + :returns: SSL certificate presented by the server. + :rtype: cryptography.x509.Certificate + + """ + + # Default SSL method selected here is the most compatible, while secure + # SSL method: TLSv1_METHOD is only compatible with + # TLSv1_METHOD, while TLS_method is compatible with all other + # methods, including TLSv2_METHOD (read more at + # https://docs.openssl.org/master/man3/SSL_CTX_new/#notes). _serve_sni + # should be changed to use "set_options" to disable SSLv2 and SSLv3, + # in case it's used for things other than probing/serving! + context = SSL.Context(SSL.TLS_METHOD) + context.set_timeout(300) # timeout in seconds + + # Enables multi-path probing (selection + # of source interface). See `socket.creation_connection` for more + # info. Available only in Python 2.7+. + source_address: tuple[str, int] = ('', 0) + socket_kwargs = {'source_address': source_address} + + try: + logger.debug( + "Attempting to connect to %s:%d%s.", host, port, + " from {0}:{1}".format( + source_address[0], + source_address[1] + ) if any(source_address) else "" + ) + socket_tuple: tuple[bytes, int] = (host, port) + sock = socket.create_connection(socket_tuple, **socket_kwargs) # type: ignore[arg-type] + except OSError as error: + raise acme_errors.Error(error) + + with contextlib.closing(sock) as client: + client_ssl = SSL.Connection(context, client) + client_ssl.set_connect_state() + client_ssl.set_tlsext_host_name(name) # pyOpenSSL>=0.13 + try: + client_ssl.do_handshake() + client_ssl.shutdown() + except SSL.Error as error: + raise acme_errors.Error(error) + cert = client_ssl.get_peer_certificate() + assert cert # Appease mypy. We would have crashed out by now if there was no certificate. + return cert.to_cryptography() diff --git a/certbot-dns-cloudflare/.readthedocs.yaml b/certbot-dns-cloudflare/.readthedocs.yaml new file mode 100644 index 00000000000..b771c4dc2f2 --- /dev/null +++ b/certbot-dns-cloudflare/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-cloudflare[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-cloudflare/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-cloudflare/Dockerfile b/certbot-dns-cloudflare/Dockerfile deleted file mode 100644 index 27dcc875104..00000000000 --- a/certbot-dns-cloudflare/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-cloudflare - -RUN pip install --no-cache-dir --editable src/certbot-dns-cloudflare diff --git a/certbot-dns-cloudflare/MANIFEST.in b/certbot-dns-cloudflare/MANIFEST.in index 18f018c08d3..8a849f092ac 100644 --- a/certbot-dns-cloudflare/MANIFEST.in +++ b/certbot-dns-cloudflare/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_cloudflare/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-cloudflare/certbot_dns_cloudflare/__init__.py b/certbot-dns-cloudflare/certbot_dns_cloudflare/__init__.py deleted file mode 100644 index 7e53f83ced6..00000000000 --- a/certbot-dns-cloudflare/certbot_dns_cloudflare/__init__.py +++ /dev/null @@ -1,86 +0,0 @@ -""" -The `~certbot_dns_cloudflare.dns_cloudflare` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the Cloudflare API. - - -Named Arguments ---------------- - -======================================== ===================================== -``--dns-cloudflare-credentials`` Cloudflare credentials_ INI file. - (Required) -``--dns-cloudflare-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 10) -======================================== ===================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing Cloudflare API -credentials, obtained from your Cloudflare -`account page `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # Cloudflare API credentials used by Certbot - dns_cloudflare_email = cloudflare@example.com - dns_cloudflare_api_key = 0123456789abcdef0123456789abcdef01234567 - -The path to this file can be provided interactively or using the -``--dns-cloudflare-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - Cloudflare account. Users who can read this file can use these credentials - to issue arbitrary API calls on your behalf. Users who can cause Certbot to - run using these credentials can complete a ``dns-01`` challenge to acquire - new certificates or revoke existing certificates for associated domains, - even if those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-cloudflare \\ - --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-cloudflare \\ - --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 60 seconds - for DNS propagation - - certbot certonly \\ - --dns-cloudflare \\ - --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \\ - --dns-cloudflare-propagation-seconds 60 \\ - -d example.com - -""" diff --git a/certbot-dns-cloudflare/certbot_dns_cloudflare/dns_cloudflare.py b/certbot-dns-cloudflare/certbot_dns_cloudflare/dns_cloudflare.py deleted file mode 100644 index 7604a8baff2..00000000000 --- a/certbot-dns-cloudflare/certbot_dns_cloudflare/dns_cloudflare.py +++ /dev/null @@ -1,199 +0,0 @@ -"""DNS Authenticator for Cloudflare.""" -import logging - -import CloudFlare -import zope.interface - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common - -logger = logging.getLogger(__name__) - -ACCOUNT_URL = 'https://www.cloudflare.com/a/account/my-account' - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for Cloudflare - - This Authenticator uses the Cloudflare API to fulfill a dns-01 challenge. - """ - - description = ('Obtain certificates using a DNS TXT record (if you are using Cloudflare for ' - 'DNS).') - ttl = 120 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add) - add('credentials', help='Cloudflare credentials INI file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the Cloudflare API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'Cloudflare credentials INI file', - { - 'email': 'email address associated with Cloudflare account', - 'api-key': 'API key for Cloudflare account, obtained from {0}'.format(ACCOUNT_URL) - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_cloudflare_client().add_txt_record(domain, validation_name, validation, self.ttl) - - def _cleanup(self, domain, validation_name, validation): - self._get_cloudflare_client().del_txt_record(domain, validation_name, validation) - - def _get_cloudflare_client(self): - return _CloudflareClient(self.credentials.conf('email'), self.credentials.conf('api-key')) - - -class _CloudflareClient(object): - """ - Encapsulates all communication with the Cloudflare API. - """ - - def __init__(self, email, api_key): - self.cf = CloudFlare.CloudFlare(email, api_key) - - def add_txt_record(self, domain, record_name, record_content, record_ttl): - """ - Add a TXT record using the supplied information. - - :param str domain: The domain to use to look up the Cloudflare zone. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - :param int record_ttl: The record TTL (number of seconds that the record may be cached). - :raises certbot.errors.PluginError: if an error occurs communicating with the Cloudflare API - """ - - zone_id = self._find_zone_id(domain) - - data = {'type': 'TXT', - 'name': record_name, - 'content': record_content, - 'ttl': record_ttl} - - try: - logger.debug('Attempting to add record to zone %s: %s', zone_id, data) - self.cf.zones.dns_records.post(zone_id, data=data) # zones | pylint: disable=no-member - except CloudFlare.exceptions.CloudFlareAPIError as e: - logger.error('Encountered CloudFlareAPIError adding TXT record: %d %s', e, e) - raise errors.PluginError('Error communicating with the Cloudflare API: {0}'.format(e)) - - record_id = self._find_txt_record_id(zone_id, record_name, record_content) - logger.debug('Successfully added TXT record with record_id: %s', record_id) - - def del_txt_record(self, domain, record_name, record_content): - """ - Delete a TXT record using the supplied information. - - Note that both the record's name and content are used to ensure that similar records - created concurrently (e.g., due to concurrent invocations of this plugin) are not deleted. - - Failures are logged, but not raised. - - :param str domain: The domain to use to look up the Cloudflare zone. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - """ - - try: - zone_id = self._find_zone_id(domain) - except errors.PluginError as e: - logger.debug('Encountered error finding zone_id during deletion: %s', e) - return - - if zone_id: - record_id = self._find_txt_record_id(zone_id, record_name, record_content) - if record_id: - try: - # zones | pylint: disable=no-member - self.cf.zones.dns_records.delete(zone_id, record_id) - logger.debug('Successfully deleted TXT record.') - except CloudFlare.exceptions.CloudFlareAPIError as e: - logger.warning('Encountered CloudFlareAPIError deleting TXT record: %s', e) - else: - logger.debug('TXT record not found; no cleanup needed.') - else: - logger.debug('Zone not found; no cleanup needed.') - - def _find_zone_id(self, domain): - """ - Find the zone_id for a given domain. - - :param str domain: The domain for which to find the zone_id. - :returns: The zone_id, if found. - :rtype: str - :raises certbot.errors.PluginError: if no zone_id is found. - """ - - zone_name_guesses = dns_common.base_domain_name_guesses(domain) - - for zone_name in zone_name_guesses: - params = {'name': zone_name, - 'per_page': 1} - - try: - zones = self.cf.zones.get(params=params) # zones | pylint: disable=no-member - except CloudFlare.exceptions.CloudFlareAPIError as e: - code = int(e) - hint = None - - if code == 6003: - hint = 'Did you copy your entire API key?' - elif code == 9103: - hint = 'Did you enter the correct email address?' - - raise errors.PluginError('Error determining zone_id: {0} {1}. Please confirm that ' - 'you have supplied valid Cloudflare API credentials.{2}' - .format(code, e, ' ({0})'.format(hint) if hint else '')) - - if len(zones) > 0: - zone_id = zones[0]['id'] - logger.debug('Found zone_id of %s for %s using name %s', zone_id, domain, zone_name) - return zone_id - - raise errors.PluginError('Unable to determine zone_id for {0} using zone names: {1}. ' - 'Please confirm that the domain name has been entered correctly ' - 'and is already associated with the supplied Cloudflare account.' - .format(domain, zone_name_guesses)) - - def _find_txt_record_id(self, zone_id, record_name, record_content): - """ - Find the record_id for a TXT record with the given name and content. - - :param str zone_id: The zone_id which contains the record. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - :returns: The record_id, if found. - :rtype: str - """ - - params = {'type': 'TXT', - 'name': record_name, - 'content': record_content, - 'per_page': 1} - try: - # zones | pylint: disable=no-member - records = self.cf.zones.dns_records.get(zone_id, params=params) - except CloudFlare.exceptions.CloudFlareAPIError as e: - logger.debug('Encountered CloudFlareAPIError getting TXT record_id: %s', e) - records = [] - - if len(records) > 0: - # Cleanup is returning the system to the state we found it. If, for some reason, - # there are multiple matching records, we only delete one because we only added one. - return records[0]['id'] - else: - logger.debug('Unable to find TXT record.') diff --git a/certbot-dns-cloudflare/certbot_dns_cloudflare/dns_cloudflare_test.py b/certbot-dns-cloudflare/certbot_dns_cloudflare/dns_cloudflare_test.py deleted file mode 100644 index e60d6ff8b8c..00000000000 --- a/certbot-dns-cloudflare/certbot_dns_cloudflare/dns_cloudflare_test.py +++ /dev/null @@ -1,174 +0,0 @@ -"""Tests for certbot_dns_cloudflare.dns_cloudflare.""" - -import os -import unittest - -import CloudFlare -import mock - -from certbot import errors -from certbot.plugins import dns_test_common -from certbot.plugins.dns_test_common import DOMAIN -from certbot.tests import util as test_util - -API_ERROR = CloudFlare.exceptions.CloudFlareAPIError(1000, '', '') -API_KEY = 'an-api-key' -EMAIL = 'example@example.com' - - -class AuthenticatorTest(test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): - - def setUp(self): - from certbot_dns_cloudflare.dns_cloudflare import Authenticator - - super(AuthenticatorTest, self).setUp() - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"cloudflare_email": EMAIL, "cloudflare_api_key": API_KEY}, path) - - self.config = mock.MagicMock(cloudflare_credentials=path, - cloudflare_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "cloudflare") - - self.mock_client = mock.MagicMock() - # _get_cloudflare_client | pylint: disable=protected-access - self.auth._get_cloudflare_client = mock.MagicMock(return_value=self.mock_client) - - def test_perform(self): - self.auth.perform([self.achall]) - - expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - def test_cleanup(self): - # _attempt_cleanup | pylint: disable=protected-access - self.auth._attempt_cleanup = True - self.auth.cleanup([self.achall]) - - expected = [mock.call.del_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - -class CloudflareClientTest(unittest.TestCase): - record_name = "foo" - record_content = "bar" - record_ttl = 42 - zone_id = 1 - record_id = 2 - - def setUp(self): - from certbot_dns_cloudflare.dns_cloudflare import _CloudflareClient - - self.cloudflare_client = _CloudflareClient(EMAIL, API_KEY) - - self.cf = mock.MagicMock() - self.cloudflare_client.cf = self.cf - - def test_add_txt_record(self): - self.cf.zones.get.return_value = [{'id': self.zone_id}] - - self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, - self.record_ttl) - - self.cf.zones.dns_records.post.assert_called_with(self.zone_id, data=mock.ANY) - - post_data = self.cf.zones.dns_records.post.call_args[1]['data'] - - self.assertEqual('TXT', post_data['type']) - self.assertEqual(self.record_name, post_data['name']) - self.assertEqual(self.record_content, post_data['content']) - self.assertEqual(self.record_ttl, post_data['ttl']) - - def test_add_txt_record_error(self): - self.cf.zones.get.return_value = [{'id': self.zone_id}] - - self.cf.zones.dns_records.post.side_effect = API_ERROR - - self.assertRaises( - errors.PluginError, - self.cloudflare_client.add_txt_record, - DOMAIN, self.record_name, self.record_content, self.record_ttl) - - def test_add_txt_record_error_during_zone_lookup(self): - self.cf.zones.get.side_effect = API_ERROR - - self.assertRaises( - errors.PluginError, - self.cloudflare_client.add_txt_record, - DOMAIN, self.record_name, self.record_content, self.record_ttl) - - def test_add_txt_record_zone_not_found(self): - self.cf.zones.get.return_value = [] - - self.assertRaises( - errors.PluginError, - self.cloudflare_client.add_txt_record, - DOMAIN, self.record_name, self.record_content, self.record_ttl) - - def test_del_txt_record(self): - self.cf.zones.get.return_value = [{'id': self.zone_id}] - self.cf.zones.dns_records.get.return_value = [{'id': self.record_id}] - - self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - expected = [mock.call.zones.get(params=mock.ANY), - mock.call.zones.dns_records.get(self.zone_id, params=mock.ANY), - mock.call.zones.dns_records.delete(self.zone_id, self.record_id)] - - self.assertEqual(expected, self.cf.mock_calls) - - get_data = self.cf.zones.dns_records.get.call_args[1]['params'] - - self.assertEqual('TXT', get_data['type']) - self.assertEqual(self.record_name, get_data['name']) - self.assertEqual(self.record_content, get_data['content']) - - def test_del_txt_record_error_during_zone_lookup(self): - self.cf.zones.get.side_effect = API_ERROR - - self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - def test_del_txt_record_error_during_delete(self): - self.cf.zones.get.return_value = [{'id': self.zone_id}] - self.cf.zones.dns_records.get.return_value = [{'id': self.record_id}] - self.cf.zones.dns_records.delete.side_effect = API_ERROR - - self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - expected = [mock.call.zones.get(params=mock.ANY), - mock.call.zones.dns_records.get(self.zone_id, params=mock.ANY), - mock.call.zones.dns_records.delete(self.zone_id, self.record_id)] - - self.assertEqual(expected, self.cf.mock_calls) - - def test_del_txt_record_error_during_get(self): - self.cf.zones.get.return_value = [{'id': self.zone_id}] - self.cf.zones.dns_records.get.side_effect = API_ERROR - - self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - expected = [mock.call.zones.get(params=mock.ANY), - mock.call.zones.dns_records.get(self.zone_id, params=mock.ANY)] - - self.assertEqual(expected, self.cf.mock_calls) - - def test_del_txt_record_no_record(self): - self.cf.zones.get.return_value = [{'id': self.zone_id}] - self.cf.zones.dns_records.get.return_value = [] - - self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - expected = [mock.call.zones.get(params=mock.ANY), - mock.call.zones.dns_records.get(self.zone_id, params=mock.ANY)] - - self.assertEqual(expected, self.cf.mock_calls) - - def test_del_txt_record_no_zone(self): - self.cf.zones.get.return_value = [{'id': None}] - - self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - expected = [mock.call.zones.get(params=mock.ANY)] - - self.assertEqual(expected, self.cf.mock_calls) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-cloudflare/docs/api.rst b/certbot-dns-cloudflare/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-cloudflare/docs/api.rst +++ b/certbot-dns-cloudflare/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-cloudflare/docs/api/dns_cloudflare.rst b/certbot-dns-cloudflare/docs/api/dns_cloudflare.rst deleted file mode 100644 index 35f5252016d..00000000000 --- a/certbot-dns-cloudflare/docs/api/dns_cloudflare.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_cloudflare.dns_cloudflare` --------------------------------------------- - -.. automodule:: certbot_dns_cloudflare.dns_cloudflare - :members: diff --git a/certbot-dns-cloudflare/docs/conf.py b/certbot-dns-cloudflare/docs/conf.py index aa78092462e..d0a13027601 100644 --- a/certbot-dns-cloudflare/docs/conf.py +++ b/certbot-dns-cloudflare/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-cloudflare/docs/make.bat b/certbot-dns-cloudflare/docs/make.bat index 88867c7705e..7f30cb1ebe2 100644 --- a/certbot-dns-cloudflare/docs/make.bat +++ b/certbot-dns-cloudflare/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-cloudflare/local-oldest-requirements.txt b/certbot-dns-cloudflare/local-oldest-requirements.txt deleted file mode 100644 index 8368d266ee7..00000000000 --- a/certbot-dns-cloudflare/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -acme[dev]==0.21.1 -certbot[dev]==0.21.1 diff --git a/certbot-dns-cloudflare/pyproject.toml b/certbot-dns-cloudflare/pyproject.toml new file mode 100644 index 00000000000..4b8d3d5e3b3 --- /dev/null +++ b/certbot-dns-cloudflare/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-cloudflare" +dynamic = ["version", "dependencies"] +description = "Cloudflare DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-cloudflare = "certbot_dns_cloudflare._internal.dns_cloudflare:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-cloudflare/readthedocs.org.requirements.txt b/certbot-dns-cloudflare/readthedocs.org.requirements.txt deleted file mode 100644 index b18901111fa..00000000000 --- a/certbot-dns-cloudflare/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-cloudflare[docs] diff --git a/certbot-dns-cloudflare/setup.cfg b/certbot-dns-cloudflare/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-cloudflare/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-cloudflare/setup.py b/certbot-dns-cloudflare/setup.py index ff33293fe0a..6d8ec386d21 100644 --- a/certbot-dns-cloudflare/setup.py +++ b/certbot-dns-cloudflare/setup.py @@ -1,66 +1,26 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.21.1', - 'certbot>=0.21.1', - 'cloudflare>=1.5.1', - 'mock', - 'setuptools', - 'zope.interface', + 'cloudflare>=4.0', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) + setup( - name='certbot-dns-cloudflare', version=version, - description="Cloudflare DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-cloudflare = certbot_dns_cloudflare.dns_cloudflare:Authenticator', - ], - }, - test_suite='certbot_dns_cloudflare', ) diff --git a/certbot-dns-cloudflare/src/certbot_dns_cloudflare/__init__.py b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/__init__.py new file mode 100644 index 00000000000..0d899138cbe --- /dev/null +++ b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/__init__.py @@ -0,0 +1,124 @@ +""" +The `~certbot_dns_cloudflare.dns_cloudflare` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using the Cloudflare API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +======================================== ===================================== +``--dns-cloudflare-credentials`` Cloudflare credentials_ INI file. + (Required) +``--dns-cloudflare-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 10) +======================================== ===================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing Cloudflare API +credentials, obtained from your +`Cloudflare dashboard `_. + +Previously, Cloudflare's "Global API Key" was used for authentication, however +this key can access the entire Cloudflare API for all domains in your account, +meaning it could cause a lot of damage if leaked. + +Cloudflare's newer API Tokens can be restricted to specific domains and +operations, and are therefore now the recommended authentication option. + +The Token needed by Certbot requires ``Zone:DNS:Edit`` permissions for only the +zones you need certificates for. + +Using Cloudflare Tokens also requires at least version 2.3.1 of the ``cloudflare`` +Python module. If the version that automatically installed with this plugin is +older than that, and you can't upgrade it on your system, you'll have to stick to +the Global key. + +.. code-block:: ini + :name: certbot_cloudflare_token.ini + :caption: Example credentials file using restricted API Token (recommended): + + # Cloudflare API token used by Certbot + dns_cloudflare_api_token = 0123456789abcdef0123456789abcdef01234567 + +.. code-block:: ini + :name: certbot_cloudflare_key.ini + :caption: Example credentials file using Global API Key (not recommended): + + # Cloudflare API credentials used by Certbot + dns_cloudflare_email = cloudflare@example.com + dns_cloudflare_api_key = 0123456789abcdef0123456789abcdef01234 + +The path to this file can be provided interactively or using the +``--dns-cloudflare-credentials`` command-line argument. Certbot records the path +to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + Cloudflare account. Users who can read this file can use these credentials + to issue arbitrary API calls on your behalf. Users who can cause Certbot to + run using these credentials can complete a ``dns-01`` challenge to acquire + new certificates or revoke existing certificates for associated domains, + even if those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + +.. note:: + Please note that the ``cloudflare`` Python module used by the plugin has + additional methods of providing credentials to the module, e.g. environment + variables or the ``cloudflare.cfg`` configuration file. These methods are not + supported by Certbot. If any of those additional methods of providing + credentials is being used, they must provide the same credentials (i.e., + email and API key *or* an API token) as the credentials file provided to + Certbot. If there is a discrepancy, the ``cloudflare`` Python module will + raise an error. Also note that the credentials provided to Certbot will take + precedence over any other method of providing credentials to the ``cloudflare`` + Python module. + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-cloudflare \\ + --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-cloudflare \\ + --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 60 seconds + for DNS propagation + + certbot certonly \\ + --dns-cloudflare \\ + --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \\ + --dns-cloudflare-propagation-seconds 60 \\ + -d example.com + +""" diff --git a/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/__init__.py b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/__init__.py new file mode 100644 index 00000000000..93b0672b58a --- /dev/null +++ b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_cloudflare.dns_cloudflare` plugin.""" diff --git a/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/dns_cloudflare.py b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/dns_cloudflare.py new file mode 100644 index 00000000000..0023110646a --- /dev/null +++ b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/dns_cloudflare.py @@ -0,0 +1,307 @@ +"""DNS Authenticator for Cloudflare.""" +import logging +import warnings +from typing import Any +from typing import Callable +from typing import Literal +from typing import Optional +from typing import TypedDict + +# cloudflare 4.x includes a pydantic v1 compatibility shim that emits a +# UserWarning on Python 3.14+. Suppress it here so that this internal-detail +# warning is not shown to users during plugin discovery. In our test suite it +# is filtered out via pytest.ini so it does not affect filterwarnings=error. +with warnings.catch_warnings(): + warnings.filterwarnings('ignore', message='Core Pydantic V1 functionality', + category=UserWarning) + import cloudflare + from cloudflare.types.zones import Zone + +from certbot import errors +from certbot.plugins import dns_common +from certbot.plugins.dns_common import CredentialsConfiguration + +logger = logging.getLogger(__name__) + +ACCOUNT_URL = 'https://dash.cloudflare.com/?to=/:account/profile/api-tokens' + + +class Authenticator(dns_common.DNSAuthenticator): + """DNS Authenticator for Cloudflare + + This Authenticator uses the Cloudflare API to fulfill a dns-01 challenge. + """ + + description = ('Obtain certificates using a DNS TXT record (if you are using Cloudflare for ' + 'DNS).') + ttl = 120 + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.credentials: Optional[CredentialsConfiguration] = None + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 10) -> None: + super().add_parser_arguments(add, default_propagation_seconds) + add('credentials', help='Cloudflare credentials INI file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the Cloudflare API.' + + def _validate_credentials(self, credentials: CredentialsConfiguration) -> None: + token = credentials.conf('api-token') + email = credentials.conf('email') + key = credentials.conf('api-key') + if token: + if email or key: + raise errors.PluginError('{}: dns_cloudflare_email and dns_cloudflare_api_key are ' + 'not needed when using an API Token' + .format(credentials.confobj.filename)) + elif email or key: + if not email: + raise errors.PluginError('{}: dns_cloudflare_email is required when using a Global ' + 'API Key. (should be email address associated with ' + 'Cloudflare account)'.format(credentials.confobj.filename)) + if not key: + raise errors.PluginError('{}: dns_cloudflare_api_key is required when using a ' + 'Global API Key. (see {})' + .format(credentials.confobj.filename, ACCOUNT_URL)) + else: + raise errors.PluginError('{}: Either dns_cloudflare_api_token (recommended), or ' + 'dns_cloudflare_email and dns_cloudflare_api_key are required.' + ' (see {})'.format(credentials.confobj.filename, ACCOUNT_URL)) + + def _setup_credentials(self) -> None: + self.credentials = self._configure_credentials( + 'credentials', + 'Cloudflare credentials INI file', + None, + self._validate_credentials + ) + + def _perform(self, domain: str, validation_name: str, validation: str) -> None: + self._get_cloudflare_client().add_txt_record(domain, validation_name, validation, self.ttl) + + def _cleanup(self, domain: str, validation_name: str, validation: str) -> None: + self._get_cloudflare_client().del_txt_record(domain, validation_name, validation) + + def _get_cloudflare_client(self) -> "_CloudflareClient": + if not self.credentials: # pragma: no cover + raise errors.Error("Plugin has not been prepared.") + if self.credentials.conf('api-token'): + return _CloudflareClient(api_token = self.credentials.conf('api-token')) + return _CloudflareClient(email = self.credentials.conf('email'), + api_key = self.credentials.conf('api-key')) + + +class _CloudflareClient: + """ + Encapsulates all communication with the Cloudflare API. + """ + + def __init__(self, email: Optional[str] = None, api_key: Optional[str] = None, + api_token: Optional[str] = None) -> None: + if email: + # If an email was specified, we're using an email/key combination and not a token. + # We use named arguments here to match the cloudflare 4.x SDK's explicit parameter + # names (api_email and api_key), which correspond to the Global API Key credentials + # found in the Cloudflare dashboard under My Profile > API Tokens. + self.cf = cloudflare.Cloudflare(api_email=email, api_key=api_key) + else: + # If no email was specified, we're using just an API token. We use the named argument + # for clarity. API Tokens are the recommended authentication method as they support + # fine-grained permissions scoped to specific zones and operations. + self.cf = cloudflare.Cloudflare(api_token=api_token) + + def add_txt_record(self, domain: str, record_name: str, record_content: str, + record_ttl: int) -> None: + """ + Add a TXT record using the supplied information. + + :param str domain: The domain to use to look up the Cloudflare zone. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + :param int record_ttl: The record TTL (number of seconds that the record may be cached). + :raises certbot.errors.PluginError: if an error occurs communicating with the Cloudflare API + """ + + zone_id = self._find_zone_id(domain) + + data: _RecordData = { + 'type': 'TXT', + 'name': record_name, + 'content': record_content, + 'ttl': record_ttl, + } + + try: + logger.debug('Attempting to add record to zone %s: %s', zone_id, data) + self.cf.dns.records.create(zone_id=zone_id, **data) + except cloudflare.APIStatusError as e: + code = _cf_error_code(e) + hint = None + + if code == 1009: + hint = 'Does your API token have "Zone:DNS:Edit" permissions?' + + logger.error('Encountered Cloudflare API error adding TXT record: %s', e) + raise errors.PluginError('Error communicating with the Cloudflare API: {0}{1}' + .format(e, ' ({0})'.format(hint) if hint else '')) + except cloudflare.APIConnectionError as e: + logger.error('Network error talking to the Cloudflare API: %s', e) + raise errors.PluginError('Network error communicating with the Cloudflare API ' + 'while adding a TXT record: {0}'.format(e)) + + record_id = self._find_txt_record_id(zone_id, record_name, record_content) + logger.debug('Successfully added TXT record with record_id: %s', record_id) + + def del_txt_record(self, domain: str, record_name: str, record_content: str) -> None: + """ + Delete a TXT record using the supplied information. + + Note that both the record's name and content are used to ensure that similar records + created concurrently (e.g., due to concurrent invocations of this plugin) are not deleted. + + Failures are logged, but not raised. + + :param str domain: The domain to use to look up the Cloudflare zone. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + """ + + try: + zone_id = self._find_zone_id(domain) + except errors.PluginError as e: + logger.debug('Encountered error finding zone_id during deletion: %s', e) + logger.debug('Zone not found; no cleanup needed.') + return + + record_id = self._find_txt_record_id(zone_id, record_name, record_content) + if record_id: + try: + self.cf.dns.records.delete(dns_record_id=record_id, zone_id=zone_id) + logger.debug('Successfully deleted TXT record.') + except cloudflare.APIStatusError as e: + logger.warning('Encountered Cloudflare API error deleting TXT record: %s', e) + except cloudflare.APIConnectionError as e: + logger.warning('Network error deleting TXT record from Cloudflare: %s', e) + else: + logger.debug('TXT record not found; no cleanup needed.') + + def _find_zone_id(self, domain: str) -> str: + """ + Find the zone_id for a given domain. + + :param str domain: The domain for which to find the zone_id. + :returns: The zone_id for the first matching zone that has a non-empty + identifier. A zone with an empty/invalid id is treated as if no zone + were found, so this method never returns an empty string. + :rtype: str + :raises certbot.errors.PluginError: if no zone_id is found. + """ + + zone_name_guesses = dns_common.base_domain_name_guesses(domain) + zone: Zone | None = None + code = msg = None + + for zone_name in zone_name_guesses: + try: + zone = next(iter(self.cf.zones.list(name=zone_name, per_page=1)), None) + except cloudflare.APIStatusError as e: + code = _cf_error_code(e) + msg = str(e) + hint = None + + if code == 6003: + hint = ('Did you copy your entire API token/key? ' + 'See {} to manage your API tokens.'.format(ACCOUNT_URL)) + elif code == 9103: + hint = 'Did you enter the correct email address and Global key?' + elif code == 9109: + hint = 'Did you enter a valid Cloudflare Token?' + + if hint: + raise errors.PluginError('Error determining zone_id: {0} {1}. Please confirm ' + 'that you have supplied valid Cloudflare API credentials. ({2})' + .format(code, msg, hint)) + else: + logger.debug('Unrecognised Cloudflare API error while finding zone_id: %s. ' + 'Continuing with next zone guess...', e) + except cloudflare.APIConnectionError as e: + raise errors.PluginError('Network error contacting the Cloudflare API while ' + 'looking up the zone for {0}: {1}'.format(domain, e)) + + if zone: + zone_id = zone.id + if zone_id: + logger.debug('Found zone_id of %s for %s using name %s', + zone_id, domain, zone_name) + return zone_id + break # Found a zone but it has no usable ID; stop searching + + if msg is not None: + if 'com.cloudflare.api.account.zone.list' in msg: + raise errors.PluginError('Unable to determine zone_id for {0} using zone names: ' + '{1}. Please confirm that the domain name has been ' + 'entered correctly and your Cloudflare Token has access ' + 'to the domain.'.format(domain, zone_name_guesses)) + else: + raise errors.PluginError('Unable to determine zone_id for {0} using zone names: ' + '{1}. The error from Cloudflare was: {2} {3}.' + .format(domain, zone_name_guesses, code, msg)) + else: + raise errors.PluginError('Unable to determine zone_id for {0} using zone names: ' + '{1}. Please confirm that the domain name has been ' + 'entered correctly and is already associated with the ' + 'supplied Cloudflare account.' + .format(domain, zone_name_guesses)) + + def _find_txt_record_id(self, zone_id: str, record_name: str, + record_content: str) -> Optional[str]: + """ + Find the record_id for a TXT record with the given name and content. + + :param str zone_id: The zone_id which contains the record. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + :returns: The record_id, if found. + :rtype: str + """ + + try: + records = list(self.cf.dns.records.list( + zone_id=zone_id, type='TXT', name={'exact': record_name}, + content={'exact': record_content}, per_page=1)) + except cloudflare.APIStatusError as e: + logger.debug('Encountered Cloudflare API error getting TXT record_id: %s', e) + records = [] + except cloudflare.APIConnectionError as e: + logger.debug('Network error getting TXT record_id from Cloudflare: %s', e) + records = [] + + if records: + # Cleanup is returning the system to the state we found it. If, for some reason, + # there are multiple matching records, we only delete one because we only added one. + return records[0].id + logger.debug('Unable to find TXT record.') + return None + + +class _RecordData(TypedDict): + """Offers type hints for dictionaries of Cloudflare API parameters.""" + + type: Literal['TXT'] + name: str + content: str + ttl: int + + +def _cf_error_code(e: cloudflare.APIStatusError) -> int | None: + """Extract the first Cloudflare error code from an API error response.""" + try: + body = e.response.json() + return int(body['errors'][0]['code']) + except (ValueError, KeyError, IndexError, TypeError): # pragma: no cover + return None diff --git a/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/tests/__init__.py b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/tests/__init__.py new file mode 100644 index 00000000000..9edaeaa70a7 --- /dev/null +++ b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-cloudflare tests""" diff --git a/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/tests/dns_cloudflare_test.py b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/tests/dns_cloudflare_test.py new file mode 100644 index 00000000000..fe77d3b70c5 --- /dev/null +++ b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/tests/dns_cloudflare_test.py @@ -0,0 +1,305 @@ +"""Tests for certbot_dns_cloudflare._internal.dns_cloudflare.""" + +import sys +import unittest +from typing import Any +from unittest import mock + +import cloudflare +import pytest + +from certbot import errors +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins.dns_test_common import DOMAIN +from certbot.tests import util as test_util + + +def _make_api_error(cf_code: int, msg: str = '', http_status: int = 400 + ) -> cloudflare.APIStatusError: + """Build a cloudflare.APIStatusError with a Cloudflare error code in the body.""" + body: Any = {'success': False, 'errors': [{'code': cf_code, 'message': msg}]} + response = mock.Mock(status_code=http_status, request=mock.Mock()) + response.json.return_value = body + return cloudflare.APIStatusError(message=msg or str(cf_code), response=response, body=body) + + +def _make_connection_error(msg: str = 'Connection error.') -> cloudflare.APIConnectionError: + """Build a cloudflare.APIConnectionError (e.g. transient network failure).""" + return cloudflare.APIConnectionError(message=msg, request=mock.Mock()) + + +API_ERROR = _make_api_error(1000) +CONNECTION_ERROR = _make_connection_error() + +API_TOKEN = 'an-api-token' + +API_KEY = 'an-api-key' +EMAIL = 'example@example.com' + + +class AuthenticatorTest(test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): + + def setUp(self): + from certbot_dns_cloudflare._internal.dns_cloudflare import Authenticator + + super().setUp() + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write({"cloudflare_email": EMAIL, "cloudflare_api_key": API_KEY}, path) + + self.config = mock.MagicMock(cloudflare_credentials=path, + cloudflare_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "cloudflare") + + self.mock_client = mock.MagicMock() + # _get_cloudflare_client | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.auth, '_get_cloudflare_client', mock.MagicMock(return_value=self.mock_client)) + + @test_util.patch_display_util() + def test_perform(self, unused_mock_get_utility): + self.auth.perform([self.achall]) + + expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY, mock.ANY)] + assert expected == self.mock_client.mock_calls + + def test_cleanup(self): + # _attempt_cleanup | pylint: disable=protected-access + self.auth._attempt_cleanup = True + self.auth.cleanup([self.achall]) + + expected = [mock.call.del_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)] + assert expected == self.mock_client.mock_calls + + @test_util.patch_display_util() + def test_api_token(self, unused_mock_get_utility): + dns_test_common.write({"cloudflare_api_token": API_TOKEN}, + self.config.cloudflare_credentials) + self.auth.perform([self.achall]) + + expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY, mock.ANY)] + assert expected == self.mock_client.mock_calls + + def test_no_creds(self): + dns_test_common.write({}, self.config.cloudflare_credentials) + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + def test_missing_email_or_key(self): + dns_test_common.write({"cloudflare_api_key": API_KEY}, self.config.cloudflare_credentials) + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + dns_test_common.write({"cloudflare_email": EMAIL}, self.config.cloudflare_credentials) + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + def test_email_or_key_with_token(self): + dns_test_common.write({"cloudflare_api_token": API_TOKEN, "cloudflare_email": EMAIL}, + self.config.cloudflare_credentials) + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + dns_test_common.write({"cloudflare_api_token": API_TOKEN, "cloudflare_api_key": API_KEY}, + self.config.cloudflare_credentials) + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + dns_test_common.write({"cloudflare_api_token": API_TOKEN, "cloudflare_email": EMAIL, + "cloudflare_api_key": API_KEY}, self.config.cloudflare_credentials) + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + def test_get_cloudflare_client_with_api_token(self): + from certbot_dns_cloudflare._internal.dns_cloudflare import Authenticator + from certbot_dns_cloudflare._internal.dns_cloudflare import _CloudflareClient + mock_auth = mock.MagicMock() + mock_auth.credentials.conf.return_value = API_TOKEN + client = Authenticator._get_cloudflare_client(mock_auth) + self.assertIsInstance(client, _CloudflareClient) + + def test_get_cloudflare_client_with_email_key(self): + from certbot_dns_cloudflare._internal.dns_cloudflare import Authenticator + from certbot_dns_cloudflare._internal.dns_cloudflare import _CloudflareClient + mock_auth = mock.MagicMock() + mock_auth.credentials.conf.side_effect = lambda k: None if k == 'api-token' else 'some_value' + client = Authenticator._get_cloudflare_client(mock_auth) + self.assertIsInstance(client, _CloudflareClient) + + +def _mock_zone(zone_id: str | None) -> mock.MagicMock: + """Create a mock zone object with an .id attribute.""" + zone = mock.MagicMock() + zone.id = zone_id + return zone + + +def _mock_record(record_id: str | None) -> mock.MagicMock: + """Create a mock DNS record object with an .id attribute.""" + record = mock.MagicMock() + record.id = record_id + return record + + +class CloudflareClientTest(unittest.TestCase): + record_name = "foo" + record_content = "bar" + record_ttl = 42 + zone_id = "zone-id-1" + record_id = "record-id-2" + + def setUp(self): + from certbot_dns_cloudflare._internal.dns_cloudflare import _CloudflareClient + + self.cloudflare_client = _CloudflareClient(EMAIL, API_KEY) + + self.cf = mock.MagicMock() + self.cloudflare_client.cf = self.cf + + def test_add_txt_record(self): + self.cf.zones.list.return_value = [_mock_zone(self.zone_id)] + + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + self.cf.dns.records.create.assert_called_with( + zone_id=self.zone_id, type='TXT', name=self.record_name, + content=self.record_content, ttl=self.record_ttl) + + def test_add_txt_record_error(self): + self.cf.zones.list.return_value = [_mock_zone(self.zone_id)] + + self.cf.dns.records.create.side_effect = _make_api_error(1009) + + with pytest.raises(errors.PluginError): + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + def test_add_txt_record_error_during_zone_lookup(self): + self.cf.zones.list.side_effect = API_ERROR + + with pytest.raises(errors.PluginError): + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + def test_add_txt_record_zone_not_found(self): + self.cf.zones.list.return_value = [] + + with pytest.raises(errors.PluginError): + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + def test_add_txt_record_connection_error_on_create(self): + self.cf.zones.list.return_value = [_mock_zone(self.zone_id)] + self.cf.dns.records.create.side_effect = CONNECTION_ERROR + + with pytest.raises(errors.PluginError, match='Network error'): + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + def test_add_txt_record_connection_error_during_zone_lookup(self): + self.cf.zones.list.side_effect = CONNECTION_ERROR + + with pytest.raises(errors.PluginError, match='Network error'): + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + def test_add_txt_record_bad_creds(self): + self.cf.zones.list.side_effect = _make_api_error(6003) + with pytest.raises(errors.PluginError): + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + self.cf.zones.list.side_effect = _make_api_error(9103) + with pytest.raises(errors.PluginError): + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + self.cf.zones.list.side_effect = _make_api_error(9109) + with pytest.raises(errors.PluginError): + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + self.cf.zones.list.side_effect = _make_api_error(0, 'com.cloudflare.api.account.zone.list') + with pytest.raises(errors.PluginError): + self.cloudflare_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + def test_del_txt_record(self): + self.cf.zones.list.return_value = [_mock_zone(self.zone_id)] + self.cf.dns.records.list.return_value = [_mock_record(self.record_id)] + + self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + self.cf.zones.list.assert_called_once() + self.cf.dns.records.list.assert_called_once_with( + zone_id=self.zone_id, type='TXT', name={'exact': self.record_name}, + content={'exact': self.record_content}, per_page=1) + self.cf.dns.records.delete.assert_called_once_with( + dns_record_id=self.record_id, zone_id=self.zone_id) + + def test_del_txt_record_error_during_zone_lookup(self): + self.cf.zones.list.side_effect = API_ERROR + + self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + def test_del_txt_record_error_during_delete(self): + self.cf.zones.list.return_value = [_mock_zone(self.zone_id)] + self.cf.dns.records.list.return_value = [_mock_record(self.record_id)] + self.cf.dns.records.delete.side_effect = API_ERROR + + self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + self.cf.dns.records.delete.assert_called_once_with( + dns_record_id=self.record_id, zone_id=self.zone_id) + + def test_del_txt_record_error_during_get(self): + self.cf.zones.list.return_value = [_mock_zone(self.zone_id)] + self.cf.dns.records.list.side_effect = API_ERROR + + self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + self.cf.dns.records.list.assert_called_once() + self.cf.dns.records.delete.assert_not_called() + + def test_del_txt_record_no_record(self): + self.cf.zones.list.return_value = [_mock_zone(self.zone_id)] + self.cf.dns.records.list.return_value = [] + + self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + self.cf.dns.records.list.assert_called_once() + self.cf.dns.records.delete.assert_not_called() + + def test_del_txt_record_no_zone(self): + self.cf.zones.list.return_value = [_mock_zone(None)] + + self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + self.cf.zones.list.assert_called_once() + + def test_del_txt_record_connection_error_on_delete(self): + self.cf.zones.list.return_value = [_mock_zone(self.zone_id)] + self.cf.dns.records.list.return_value = [_mock_record(self.record_id)] + self.cf.dns.records.delete.side_effect = CONNECTION_ERROR + + self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + self.cf.dns.records.delete.assert_called_once_with( + dns_record_id=self.record_id, zone_id=self.zone_id) + + def test_del_txt_record_connection_error_on_get(self): + self.cf.zones.list.return_value = [_mock_zone(self.zone_id)] + self.cf.dns.records.list.side_effect = CONNECTION_ERROR + + self.cloudflare_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + self.cf.dns.records.list.assert_called_once() + self.cf.dns.records.delete.assert_not_called() + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/letshelp-certbot/docs/_templates/.gitignore b/certbot-dns-cloudflare/src/certbot_dns_cloudflare/py.typed similarity index 100% rename from letshelp-certbot/docs/_templates/.gitignore rename to certbot-dns-cloudflare/src/certbot_dns_cloudflare/py.typed diff --git a/certbot-dns-cloudxns/Dockerfile b/certbot-dns-cloudxns/Dockerfile deleted file mode 100644 index cc84ea65b16..00000000000 --- a/certbot-dns-cloudxns/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-cloudxns - -RUN pip install --no-cache-dir --editable src/certbot-dns-cloudxns diff --git a/certbot-dns-cloudxns/LICENSE.txt b/certbot-dns-cloudxns/LICENSE.txt deleted file mode 100644 index 981c46c9f93..00000000000 --- a/certbot-dns-cloudxns/LICENSE.txt +++ /dev/null @@ -1,190 +0,0 @@ - Copyright 2015 Electronic Frontier Foundation and others - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS diff --git a/certbot-dns-cloudxns/MANIFEST.in b/certbot-dns-cloudxns/MANIFEST.in deleted file mode 100644 index 18f018c08d3..00000000000 --- a/certbot-dns-cloudxns/MANIFEST.in +++ /dev/null @@ -1,3 +0,0 @@ -include LICENSE.txt -include README.rst -recursive-include docs * diff --git a/certbot-dns-cloudxns/README.rst b/certbot-dns-cloudxns/README.rst deleted file mode 100644 index b127770df6b..00000000000 --- a/certbot-dns-cloudxns/README.rst +++ /dev/null @@ -1 +0,0 @@ -CloudXNS DNS Authenticator plugin for Certbot diff --git a/certbot-dns-cloudxns/certbot_dns_cloudxns/__init__.py b/certbot-dns-cloudxns/certbot_dns_cloudxns/__init__.py deleted file mode 100644 index 6ddbdfe5a8c..00000000000 --- a/certbot-dns-cloudxns/certbot_dns_cloudxns/__init__.py +++ /dev/null @@ -1,86 +0,0 @@ -""" -The `~certbot_dns_cloudxns.dns_cloudxns` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the CloudXNS API. - - -Named Arguments ---------------- - -======================================== ===================================== -``--dns-cloudxns-credentials`` CloudXNS credentials_ INI file. - (Required) -``--dns-cloudxns-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 30) -======================================== ===================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing CloudXNS API -credentials, obtained from your CloudXNS -`API page `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # CloudXNS API credentials used by Certbot - dns_cloudxns_api_key = 1234567890abcdef1234567890abcdef - dns_cloudxns_secret_key = 1122334455667788 - -The path to this file can be provided interactively or using the -``--dns-cloudxns-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - CloudXNS account. Users who can read this file can use these credentials to - issue arbitrary API calls on your behalf. Users who can cause Certbot to run - using these credentials can complete a ``dns-01`` challenge to acquire new - certificates or revoke existing certificates for associated domains, even if - those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-cloudxns \\ - --dns-cloudxns-credentials ~/.secrets/certbot/cloudxns.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-cloudxns \\ - --dns-cloudxns-credentials ~/.secrets/certbot/cloudxns.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 60 seconds - for DNS propagation - - certbot certonly \\ - --dns-cloudxns \\ - --dns-cloudxns-credentials ~/.secrets/certbot/cloudxns.ini \\ - --dns-cloudxns-propagation-seconds 60 \\ - -d example.com - -""" diff --git a/certbot-dns-cloudxns/certbot_dns_cloudxns/dns_cloudxns.py b/certbot-dns-cloudxns/certbot_dns_cloudxns/dns_cloudxns.py deleted file mode 100644 index 5132137f808..00000000000 --- a/certbot-dns-cloudxns/certbot_dns_cloudxns/dns_cloudxns.py +++ /dev/null @@ -1,87 +0,0 @@ -"""DNS Authenticator for CloudXNS DNS.""" -import logging - -import zope.interface -from lexicon.providers import cloudxns - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common -from certbot.plugins import dns_common_lexicon - -logger = logging.getLogger(__name__) - -ACCOUNT_URL = 'https://www.cloudxns.net/en/AccountManage/apimanage.html' - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for CloudXNS DNS - - This Authenticator uses the CloudXNS DNS API to fulfill a dns-01 challenge. - """ - - description = 'Obtain certificates using a DNS TXT record (if you are using CloudXNS for DNS).' - ttl = 60 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=30) - add('credentials', help='CloudXNS credentials INI file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the CloudXNS API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'CloudXNS credentials INI file', - { - 'api-key': 'API key for CloudXNS account, obtained from {0}'.format(ACCOUNT_URL), - 'secret-key': 'Secret key for CloudXNS account, obtained from {0}' - .format(ACCOUNT_URL) - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_cloudxns_client().add_txt_record(domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_cloudxns_client().del_txt_record(domain, validation_name, validation) - - def _get_cloudxns_client(self): - return _CloudXNSLexiconClient(self.credentials.conf('api-key'), - self.credentials.conf('secret-key'), - self.ttl) - - -class _CloudXNSLexiconClient(dns_common_lexicon.LexiconClient): - """ - Encapsulates all communication with the CloudXNS via Lexicon. - """ - - def __init__(self, api_key, secret_key, ttl): - super(_CloudXNSLexiconClient, self).__init__() - - config = dns_common_lexicon.build_lexicon_config('cloudxns', { - 'ttl': ttl, - }, { - 'auth_username': api_key, - 'auth_token': secret_key, - }) - - self.provider = cloudxns.Provider(config) - - def _handle_http_error(self, e, domain_name): - hint = None - if str(e).startswith('400 Client Error:'): - hint = 'Are your API key and Secret key values correct?' - - return errors.PluginError('Error determining zone identifier for {0}: {1}.{2}' - .format(domain_name, e, ' ({0})'.format(hint) if hint else '')) diff --git a/certbot-dns-cloudxns/certbot_dns_cloudxns/dns_cloudxns_test.py b/certbot-dns-cloudxns/certbot_dns_cloudxns/dns_cloudxns_test.py deleted file mode 100644 index c9bad23abe8..00000000000 --- a/certbot-dns-cloudxns/certbot_dns_cloudxns/dns_cloudxns_test.py +++ /dev/null @@ -1,54 +0,0 @@ -"""Tests for certbot_dns_cloudxns.dns_cloudxns.""" - -import os -import unittest - -import mock -from requests.exceptions import HTTPError, RequestException - -from certbot.plugins import dns_test_common -from certbot.plugins import dns_test_common_lexicon -from certbot.tests import util as test_util - -DOMAIN_NOT_FOUND = Exception('No domain found') -GENERIC_ERROR = RequestException -LOGIN_ERROR = HTTPError('400 Client Error: ...') - -API_KEY = 'foo' -SECRET = 'bar' - - -class AuthenticatorTest(test_util.TempDirTestCase, - dns_test_common_lexicon.BaseLexiconAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_cloudxns.dns_cloudxns import Authenticator - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"cloudxns_api_key": API_KEY, "cloudxns_secret_key": SECRET}, path) - - self.config = mock.MagicMock(cloudxns_credentials=path, - cloudxns_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "cloudxns") - - self.mock_client = mock.MagicMock() - # _get_cloudxns_client | pylint: disable=protected-access - self.auth._get_cloudxns_client = mock.MagicMock(return_value=self.mock_client) - - -class CloudXNSLexiconClientTest(unittest.TestCase, dns_test_common_lexicon.BaseLexiconClientTest): - - def setUp(self): - from certbot_dns_cloudxns.dns_cloudxns import _CloudXNSLexiconClient - - self.client = _CloudXNSLexiconClient(API_KEY, SECRET, 0) - - self.provider_mock = mock.MagicMock() - self.client.provider = self.provider_mock - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-cloudxns/docs/.gitignore b/certbot-dns-cloudxns/docs/.gitignore deleted file mode 100644 index ba65b13af5e..00000000000 --- a/certbot-dns-cloudxns/docs/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/_build/ diff --git a/certbot-dns-cloudxns/docs/Makefile b/certbot-dns-cloudxns/docs/Makefile deleted file mode 100644 index ecda13dfe2c..00000000000 --- a/certbot-dns-cloudxns/docs/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -# Minimal makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -SPHINXPROJ = certbot-dns-cloudxns -SOURCEDIR = . -BUILDDIR = _build - -# Put it first so that "make" without argument is like "make help". -help: - @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) - -.PHONY: help Makefile - -# Catch-all target: route all unknown targets to Sphinx using the new -# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). -%: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) \ No newline at end of file diff --git a/certbot-dns-cloudxns/docs/api.rst b/certbot-dns-cloudxns/docs/api.rst deleted file mode 100644 index 8668ec5d8ad..00000000000 --- a/certbot-dns-cloudxns/docs/api.rst +++ /dev/null @@ -1,8 +0,0 @@ -================= -API Documentation -================= - -.. toctree:: - :glob: - - api/** diff --git a/certbot-dns-cloudxns/docs/api/dns_cloudxns.rst b/certbot-dns-cloudxns/docs/api/dns_cloudxns.rst deleted file mode 100644 index be794d1a094..00000000000 --- a/certbot-dns-cloudxns/docs/api/dns_cloudxns.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_cloudxns.dns_cloudxns` ----------------------------------------- - -.. automodule:: certbot_dns_cloudxns.dns_cloudxns - :members: diff --git a/certbot-dns-cloudxns/docs/conf.py b/certbot-dns-cloudxns/docs/conf.py deleted file mode 100644 index 9e2f4c0e6b6..00000000000 --- a/certbot-dns-cloudxns/docs/conf.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: utf-8 -*- -# -# certbot-dns-cloudxns documentation build configuration file, created by -# sphinx-quickstart on Wed May 10 16:05:50 2017. -# -# This file is execfile()d with the current directory set to its -# containing dir. -# -# Note that not all possible configuration values are present in this -# autogenerated file. -# -# All configuration values have a default; values that are commented out -# serve to show the default. - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -# -import os -# import sys -# sys.path.insert(0, os.path.abspath('.')) - - -# -- General configuration ------------------------------------------------ - -# If your documentation needs a minimal Sphinx version, state it here. -# -needs_sphinx = '1.0' - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = ['sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] - -autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# The suffix(es) of source filenames. -# You can specify multiple suffix as a list of string: -# -# source_suffix = ['.rst', '.md'] -source_suffix = '.rst' - -# The master toctree document. -master_doc = 'index' - -# General information about the project. -project = u'certbot-dns-cloudxns' -copyright = u'2017, Certbot Project' -author = u'Certbot Project' - -# The version info for the project you're documenting, acts as replacement for -# |version| and |release|, also used in various other places throughout the -# built documents. -# -# The short X.Y version. -version = u'0' -# The full version, including alpha/beta/rc tags. -release = u'0' - -# The language for content autogenerated by Sphinx. Refer to documentation -# for a list of supported languages. -# -# This is also used if you do content translation via gettext catalogs. -# Usually you set "language" from the command line for these cases. -language = 'en' - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -# This patterns also effect to html_static_path and html_extra_path -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] - -default_role = 'py:obj' - -# The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' - -# If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True - - -# -- Options for HTML output ---------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -# - -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it - -# Theme options are theme-specific and customize the look and feel of a theme -# further. For a list of options available for each theme, see the -# documentation. -# -# html_theme_options = {} - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] - - -# -- Options for HTMLHelp output ------------------------------------------ - -# Output file base name for HTML help builder. -htmlhelp_basename = 'certbot-dns-cloudxnsdoc' - - -# -- Options for LaTeX output --------------------------------------------- - -latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - # - # 'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - # - # 'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - # - # 'preamble': '', - - # Latex figure (float) alignment - # - # 'figure_align': 'htbp', -} - -# Grouping the document tree into LaTeX files. List of tuples -# (source start file, target name, title, -# author, documentclass [howto, manual, or own class]). -latex_documents = [ - (master_doc, 'certbot-dns-cloudxns.tex', u'certbot-dns-cloudxns Documentation', - u'Certbot Project', 'manual'), -] - - -# -- Options for manual page output --------------------------------------- - -# One entry per manual page. List of tuples -# (source start file, name, description, authors, manual section). -man_pages = [ - (master_doc, 'certbot-dns-cloudxns', u'certbot-dns-cloudxns Documentation', - [author], 1) -] - - -# -- Options for Texinfo output ------------------------------------------- - -# Grouping the document tree into Texinfo files. List of tuples -# (source start file, target name, title, author, -# dir menu entry, description, category) -texinfo_documents = [ - (master_doc, 'certbot-dns-cloudxns', u'certbot-dns-cloudxns Documentation', - author, 'certbot-dns-cloudxns', 'One line description of project.', - 'Miscellaneous'), -] - - - - -# Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = { - 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), -} diff --git a/certbot-dns-cloudxns/docs/index.rst b/certbot-dns-cloudxns/docs/index.rst deleted file mode 100644 index 83c6ca18db1..00000000000 --- a/certbot-dns-cloudxns/docs/index.rst +++ /dev/null @@ -1,28 +0,0 @@ -.. certbot-dns-cloudxns documentation master file, created by - sphinx-quickstart on Wed May 10 16:05:50 2017. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -Welcome to certbot-dns-cloudxns's documentation! -================================================ - -.. toctree:: - :maxdepth: 2 - :caption: Contents: - -.. automodule:: certbot_dns_cloudxns - :members: - -.. toctree:: - :maxdepth: 1 - - api - - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` diff --git a/certbot-dns-cloudxns/docs/make.bat b/certbot-dns-cloudxns/docs/make.bat deleted file mode 100644 index 12f4f0de6d8..00000000000 --- a/certbot-dns-cloudxns/docs/make.bat +++ /dev/null @@ -1,36 +0,0 @@ -@ECHO OFF - -pushd %~dp0 - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set SOURCEDIR=. -set BUILDDIR=_build -set SPHINXPROJ=certbot-dns-cloudxns - -if "%1" == "" goto help - -%SPHINXBUILD% >NUL 2>NUL -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ - exit /b 1 -) - -%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% -goto end - -:help -%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% - -:end -popd diff --git a/certbot-dns-cloudxns/local-oldest-requirements.txt b/certbot-dns-cloudxns/local-oldest-requirements.txt deleted file mode 100644 index 65f5a758ea0..00000000000 --- a/certbot-dns-cloudxns/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ --e acme[dev] --e .[dev] diff --git a/certbot-dns-cloudxns/readthedocs.org.requirements.txt b/certbot-dns-cloudxns/readthedocs.org.requirements.txt deleted file mode 100644 index ae2ff8165e5..00000000000 --- a/certbot-dns-cloudxns/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-cloudxns[docs] diff --git a/certbot-dns-cloudxns/setup.cfg b/certbot-dns-cloudxns/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-cloudxns/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-cloudxns/setup.py b/certbot-dns-cloudxns/setup.py deleted file mode 100644 index 1a6f900d8c2..00000000000 --- a/certbot-dns-cloudxns/setup.py +++ /dev/null @@ -1,66 +0,0 @@ -from setuptools import setup -from setuptools import find_packages - - -version = '0.31.0.dev0' - -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. -install_requires = [ - 'acme>=0.31.0.dev0', - 'certbot>=0.31.0.dev0', - 'dns-lexicon>=2.2.1', # Support for >1 TXT record per name - 'mock', - 'setuptools', - 'zope.interface', -] - -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] - -setup( - name='certbot-dns-cloudxns', - version=version, - description="CloudXNS DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, - install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-cloudxns = certbot_dns_cloudxns.dns_cloudxns:Authenticator', - ], - }, - test_suite='certbot_dns_cloudxns', -) diff --git a/certbot-dns-digitalocean/.readthedocs.yaml b/certbot-dns-digitalocean/.readthedocs.yaml new file mode 100644 index 00000000000..978ff17b061 --- /dev/null +++ b/certbot-dns-digitalocean/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-digitalocean[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-digitalocean/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-digitalocean/Dockerfile b/certbot-dns-digitalocean/Dockerfile deleted file mode 100644 index 8bdd0619fdb..00000000000 --- a/certbot-dns-digitalocean/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-digitalocean - -RUN pip install --no-cache-dir --editable src/certbot-dns-digitalocean diff --git a/certbot-dns-digitalocean/MANIFEST.in b/certbot-dns-digitalocean/MANIFEST.in index 18f018c08d3..decb38135a4 100644 --- a/certbot-dns-digitalocean/MANIFEST.in +++ b/certbot-dns-digitalocean/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_digitalocean/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-digitalocean/certbot_dns_digitalocean/__init__.py b/certbot-dns-digitalocean/certbot_dns_digitalocean/__init__.py deleted file mode 100644 index 3ab8df041de..00000000000 --- a/certbot-dns-digitalocean/certbot_dns_digitalocean/__init__.py +++ /dev/null @@ -1,86 +0,0 @@ -""" -The `~certbot_dns_digitalocean.dns_digitalocean` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the DigitalOcean API. - - -Named Arguments ---------------- - -========================================== =================================== -``--dns-digitalocean-credentials`` DigitalOcean credentials_ INI file. - (Required) -``--dns-digitalocean-propagation-seconds`` The number of seconds to wait for - DNS to propagate before asking the - ACME server to verify the DNS - record. - (Default: 10) -========================================== =================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing DigitalOcean API -credentials, obtained from your DigitalOcean account's `Applications & API -Tokens page `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # DigitalOcean API credentials used by Certbot - dns_digitalocean_token = 0000111122223333444455556666777788889999aaaabbbbccccddddeeeeffff - -The path to this file can be provided interactively or using the -``--dns-digitalocean-credentials`` command-line argument. Certbot records the -path to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - DigitalOcean account. Users who can read this file can use these credentials - to issue arbitrary API calls on your behalf. Users who can cause Certbot to - run using these credentials can complete a ``dns-01`` challenge to acquire - new certificates or revoke existing certificates for associated domains, - even if those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-digitalocean \\ - --dns-digitalocean-credentials ~/.secrets/certbot/digitalocean.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-digitalocean \\ - --dns-digitalocean-credentials ~/.secrets/certbot/digitalocean.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 60 seconds - for DNS propagation - - certbot certonly \\ - --dns-digitalocean \\ - --dns-digitalocean-credentials ~/.secrets/certbot/digitalocean.ini \\ - --dns-digitalocean-propagation-seconds 60 \\ - -d example.com - -""" diff --git a/certbot-dns-digitalocean/certbot_dns_digitalocean/dns_digitalocean.py b/certbot-dns-digitalocean/certbot_dns_digitalocean/dns_digitalocean.py deleted file mode 100644 index 5a4f22327f7..00000000000 --- a/certbot-dns-digitalocean/certbot_dns_digitalocean/dns_digitalocean.py +++ /dev/null @@ -1,168 +0,0 @@ -"""DNS Authenticator for DigitalOcean.""" -import logging - -import digitalocean -import zope.interface - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common - -logger = logging.getLogger(__name__) - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for DigitalOcean - - This Authenticator uses the DigitalOcean API to fulfill a dns-01 challenge. - """ - - description = 'Obtain certs using a DNS TXT record (if you are using DigitalOcean for DNS).' - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add) - add('credentials', help='DigitalOcean credentials INI file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the DigitalOcean API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'DigitalOcean credentials INI file', - { - 'token': 'API token for DigitalOcean account' - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_digitalocean_client().add_txt_record(domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_digitalocean_client().del_txt_record(domain, validation_name, validation) - - def _get_digitalocean_client(self): - return _DigitalOceanClient(self.credentials.conf('token')) - - -class _DigitalOceanClient(object): - """ - Encapsulates all communication with the DigitalOcean API. - """ - - def __init__(self, token): - self.manager = digitalocean.Manager(token=token) - - def add_txt_record(self, domain_name, record_name, record_content): - """ - Add a TXT record using the supplied information. - - :param str domain_name: The domain to use to associate the record with. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - :raises certbot.errors.PluginError: if an error occurs communicating with the DigitalOcean - API - """ - - try: - domain = self._find_domain(domain_name) - except digitalocean.Error as e: - hint = None - - if str(e).startswith("Unable to authenticate"): - hint = 'Did you provide a valid API token?' - - logger.debug('Error finding domain using the DigitalOcean API: %s', e) - raise errors.PluginError('Error finding domain using the DigitalOcean API: {0}{1}' - .format(e, ' ({0})'.format(hint) if hint else '')) - - try: - result = domain.create_new_domain_record( - type='TXT', - name=self._compute_record_name(domain, record_name), - data=record_content) - - record_id = result['domain_record']['id'] - - logger.debug('Successfully added TXT record with id: %d', record_id) - except digitalocean.Error as e: - logger.debug('Error adding TXT record using the DigitalOcean API: %s', e) - raise errors.PluginError('Error adding TXT record using the DigitalOcean API: {0}' - .format(e)) - - def del_txt_record(self, domain_name, record_name, record_content): - """ - Delete a TXT record using the supplied information. - - Note that both the record's name and content are used to ensure that similar records - created concurrently (e.g., due to concurrent invocations of this plugin) are not deleted. - - Failures are logged, but not raised. - - :param str domain_name: The domain to use to associate the record with. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - """ - - try: - domain = self._find_domain(domain_name) - except digitalocean.Error as e: - logger.debug('Error finding domain using the DigitalOcean API: %s', e) - return - - try: - domain_records = domain.get_records() - - matching_records = [record for record in domain_records - if record.type == 'TXT' - and record.name == self._compute_record_name(domain, record_name) - and record.data == record_content] - except digitalocean.Error as e: - logger.debug('Error getting DNS records using the DigitalOcean API: %s', e) - return - - for record in matching_records: - try: - logger.debug('Removing TXT record with id: %s', record.id) - record.destroy() - except digitalocean.Error as e: - logger.warning('Error deleting TXT record %s using the DigitalOcean API: %s', - record.id, e) - - def _find_domain(self, domain_name): - """ - Find the domain object for a given domain name. - - :param str domain_name: The domain name for which to find the corresponding Domain. - :returns: The Domain, if found. - :rtype: `~digitalocean.Domain` - :raises certbot.errors.PluginError: if no matching Domain is found. - """ - - domain_name_guesses = dns_common.base_domain_name_guesses(domain_name) - - domains = self.manager.get_all_domains() - - for guess in domain_name_guesses: - matches = [domain for domain in domains if domain.name == guess] - - if len(matches) > 0: - domain = matches[0] - logger.debug('Found base domain for %s using name %s', domain_name, guess) - return domain - - raise errors.PluginError('Unable to determine base domain for {0} using names: {1}.' - .format(domain_name, domain_name_guesses)) - - @staticmethod - def _compute_record_name(domain, full_record_name): - # The domain, from DigitalOcean's point of view, is automatically appended. - return full_record_name.rpartition("." + domain.name)[0] diff --git a/certbot-dns-digitalocean/certbot_dns_digitalocean/dns_digitalocean_test.py b/certbot-dns-digitalocean/certbot_dns_digitalocean/dns_digitalocean_test.py deleted file mode 100644 index 0e2043f5070..00000000000 --- a/certbot-dns-digitalocean/certbot_dns_digitalocean/dns_digitalocean_test.py +++ /dev/null @@ -1,171 +0,0 @@ -"""Tests for certbot_dns_digitalocean.dns_digitalocean.""" - -import os -import unittest - -import digitalocean -import mock - -from certbot import errors -from certbot.plugins import dns_test_common -from certbot.plugins.dns_test_common import DOMAIN -from certbot.tests import util as test_util - -API_ERROR = digitalocean.DataReadError() -TOKEN = 'a-token' - - -class AuthenticatorTest(test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): - - def setUp(self): - from certbot_dns_digitalocean.dns_digitalocean import Authenticator - - super(AuthenticatorTest, self).setUp() - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"digitalocean_token": TOKEN}, path) - - self.config = mock.MagicMock(digitalocean_credentials=path, - digitalocean_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "digitalocean") - - self.mock_client = mock.MagicMock() - # _get_digitalocean_client | pylint: disable=protected-access - self.auth._get_digitalocean_client = mock.MagicMock(return_value=self.mock_client) - - def test_perform(self): - self.auth.perform([self.achall]) - - expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - def test_cleanup(self): - # _attempt_cleanup | pylint: disable=protected-access - self.auth._attempt_cleanup = True - self.auth.cleanup([self.achall]) - - expected = [mock.call.del_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - -class DigitalOceanClientTest(unittest.TestCase): - - id_num = 1 - record_prefix = "_acme-challenge" - record_name = record_prefix + "." + DOMAIN - record_content = "bar" - - def setUp(self): - from certbot_dns_digitalocean.dns_digitalocean import _DigitalOceanClient - - self.digitalocean_client = _DigitalOceanClient(TOKEN) - - self.manager = mock.MagicMock() - self.digitalocean_client.manager = self.manager - - def test_add_txt_record(self): - wrong_domain_mock = mock.MagicMock() - wrong_domain_mock.name = "other.invalid" - wrong_domain_mock.create_new_domain_record.side_effect = AssertionError('Wrong Domain') - - domain_mock = mock.MagicMock() - domain_mock.name = DOMAIN - domain_mock.create_new_domain_record.return_value = {'domain_record': {'id': self.id_num}} - - self.manager.get_all_domains.return_value = [wrong_domain_mock, domain_mock] - - self.digitalocean_client.add_txt_record(DOMAIN, self.record_name, self.record_content) - - domain_mock.create_new_domain_record.assert_called_with(type='TXT', - name=self.record_prefix, - data=self.record_content) - - def test_add_txt_record_fail_to_find_domain(self): - self.manager.get_all_domains.return_value = [] - - self.assertRaises(errors.PluginError, - self.digitalocean_client.add_txt_record, - DOMAIN, self.record_name, self.record_content) - - def test_add_txt_record_error_finding_domain(self): - self.manager.get_all_domains.side_effect = API_ERROR - - self.assertRaises(errors.PluginError, - self.digitalocean_client.add_txt_record, - DOMAIN, self.record_name, self.record_content) - - def test_add_txt_record_error_creating_record(self): - domain_mock = mock.MagicMock() - domain_mock.name = DOMAIN - domain_mock.create_new_domain_record.side_effect = API_ERROR - - self.manager.get_all_domains.return_value = [domain_mock] - - self.assertRaises(errors.PluginError, - self.digitalocean_client.add_txt_record, - DOMAIN, self.record_name, self.record_content) - - def test_del_txt_record(self): - first_record_mock = mock.MagicMock() - first_record_mock.type = 'TXT' - first_record_mock.name = "DIFFERENT" - first_record_mock.data = self.record_content - - correct_record_mock = mock.MagicMock() - correct_record_mock.type = 'TXT' - correct_record_mock.name = self.record_prefix - correct_record_mock.data = self.record_content - - last_record_mock = mock.MagicMock() - last_record_mock.type = 'TXT' - last_record_mock.name = self.record_prefix - last_record_mock.data = "DIFFERENT" - - domain_mock = mock.MagicMock() - domain_mock.name = DOMAIN - domain_mock.get_records.return_value = [first_record_mock, - correct_record_mock, - last_record_mock] - - self.manager.get_all_domains.return_value = [domain_mock] - - self.digitalocean_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - self.assertTrue(correct_record_mock.destroy.called) - - self.assertFalse(first_record_mock.destroy.call_args_list) - self.assertFalse(last_record_mock.destroy.call_args_list) - - def test_del_txt_record_error_finding_domain(self): - self.manager.get_all_domains.side_effect = API_ERROR - - self.digitalocean_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - def test_del_txt_record_error_finding_record(self): - domain_mock = mock.MagicMock() - domain_mock.name = DOMAIN - domain_mock.get_records.side_effect = API_ERROR - - self.manager.get_all_domains.return_value = [domain_mock] - - self.digitalocean_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - def test_del_txt_record_error_deleting_record(self): - record_mock = mock.MagicMock() - record_mock.type = 'TXT' - record_mock.name = self.record_prefix - record_mock.data = self.record_content - record_mock.destroy.side_effect = API_ERROR - - domain_mock = mock.MagicMock() - domain_mock.name = DOMAIN - domain_mock.get_records.return_value = [record_mock] - - self.manager.get_all_domains.return_value = [domain_mock] - - self.digitalocean_client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-digitalocean/docs/api.rst b/certbot-dns-digitalocean/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-digitalocean/docs/api.rst +++ b/certbot-dns-digitalocean/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-digitalocean/docs/api/dns_digitalocean.rst b/certbot-dns-digitalocean/docs/api/dns_digitalocean.rst deleted file mode 100644 index 8a787987e3a..00000000000 --- a/certbot-dns-digitalocean/docs/api/dns_digitalocean.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_digitalocean.dns_digitalocean` ------------------------------------------------- - -.. automodule:: certbot_dns_digitalocean.dns_digitalocean - :members: diff --git a/certbot-dns-digitalocean/docs/conf.py b/certbot-dns-digitalocean/docs/conf.py index e223b153589..c1240d339a2 100644 --- a/certbot-dns-digitalocean/docs/conf.py +++ b/certbot-dns-digitalocean/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-digitalocean/docs/make.bat b/certbot-dns-digitalocean/docs/make.bat index e1bda5e2797..559293fbe4b 100644 --- a/certbot-dns-digitalocean/docs/make.bat +++ b/certbot-dns-digitalocean/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-digitalocean/local-oldest-requirements.txt b/certbot-dns-digitalocean/local-oldest-requirements.txt deleted file mode 100644 index 8368d266ee7..00000000000 --- a/certbot-dns-digitalocean/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -acme[dev]==0.21.1 -certbot[dev]==0.21.1 diff --git a/certbot-dns-digitalocean/pyproject.toml b/certbot-dns-digitalocean/pyproject.toml new file mode 100644 index 00000000000..fd74bb78ffa --- /dev/null +++ b/certbot-dns-digitalocean/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-digitalocean" +dynamic = ["version", "dependencies"] +description = "DigitalOcean DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-digitalocean = "certbot_dns_digitalocean._internal.dns_digitalocean:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-digitalocean/readthedocs.org.requirements.txt b/certbot-dns-digitalocean/readthedocs.org.requirements.txt deleted file mode 100644 index 08d973ab36e..00000000000 --- a/certbot-dns-digitalocean/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-digitalocean[docs] diff --git a/certbot-dns-digitalocean/setup.cfg b/certbot-dns-digitalocean/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-digitalocean/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-digitalocean/setup.py b/certbot-dns-digitalocean/setup.py index 2f7fa37d6e7..0925986d454 100644 --- a/certbot-dns-digitalocean/setup.py +++ b/certbot-dns-digitalocean/setup.py @@ -1,67 +1,25 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.21.1', - 'certbot>=0.21.1', - 'mock', - 'python-digitalocean>=1.11', - 'setuptools', - 'six', - 'zope.interface', + 'python-digitalocean>=1.15.0', # 1.15.0 or newer is recommended for TTL support ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-digitalocean', version=version, - description="DigitalOcean DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-digitalocean = certbot_dns_digitalocean.dns_digitalocean:Authenticator', - ], - }, - test_suite='certbot_dns_digitalocean', ) diff --git a/certbot-dns-digitalocean/src/certbot_dns_digitalocean/__init__.py b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/__init__.py new file mode 100644 index 00000000000..8fd50919624 --- /dev/null +++ b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/__init__.py @@ -0,0 +1,99 @@ +""" +The `~certbot_dns_digitalocean.dns_digitalocean` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using the DigitalOcean API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +========================================== =================================== +``--dns-digitalocean-credentials`` DigitalOcean credentials_ INI file. + (Required) +``--dns-digitalocean-propagation-seconds`` The number of seconds to wait for + DNS to propagate before asking the + ACME server to verify the DNS + record. + (Default: 10) +========================================== =================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing DigitalOcean API +credentials, obtained from your DigitalOcean account's `Applications & API +Tokens page `_. + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # DigitalOcean API credentials used by Certbot + dns_digitalocean_token = 0000111122223333444455556666777788889999aaaabbbbccccddddeeeeffff + +The path to this file can be provided interactively or using the +``--dns-digitalocean-credentials`` command-line argument. Certbot records the +path to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + DigitalOcean account. Users who can read this file can use these credentials + to issue arbitrary API calls on your behalf. Users who can cause Certbot to + run using these credentials can complete a ``dns-01`` challenge to acquire + new certificates or revoke existing certificates for associated domains, + even if those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-digitalocean \\ + --dns-digitalocean-credentials ~/.secrets/certbot/digitalocean.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-digitalocean \\ + --dns-digitalocean-credentials ~/.secrets/certbot/digitalocean.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a wildcard certificate for ``*.example.com`` + + certbot certonly \\ + --dns-digitalocean \\ + --dns-digitalocean-credentials ~/.secrets/certbot/digitalocean.ini \\ + -d example.com \\ + -d '*.example.com' + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 60 seconds + for DNS propagation + + certbot certonly \\ + --dns-digitalocean \\ + --dns-digitalocean-credentials ~/.secrets/certbot/digitalocean.ini \\ + --dns-digitalocean-propagation-seconds 60 \\ + -d example.com + +""" diff --git a/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/__init__.py b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/__init__.py new file mode 100644 index 00000000000..0291a93415e --- /dev/null +++ b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_digitalocean.dns_digitalocean` plugin.""" diff --git a/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/dns_digitalocean.py b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/dns_digitalocean.py new file mode 100644 index 00000000000..37fc215eddc --- /dev/null +++ b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/dns_digitalocean.py @@ -0,0 +1,182 @@ +"""DNS Authenticator for DigitalOcean.""" +import logging +from typing import Any +from typing import Callable +from typing import cast +from typing import Optional + +import digitalocean + +from certbot import errors +from certbot.plugins import dns_common +from certbot.plugins.dns_common import CredentialsConfiguration + +logger = logging.getLogger(__name__) + + +class Authenticator(dns_common.DNSAuthenticator): + """DNS Authenticator for DigitalOcean + + This Authenticator uses the DigitalOcean API to fulfill a dns-01 challenge. + """ + + description = 'Obtain certificates using a DNS TXT record (if you are ' + \ + 'using DigitalOcean for DNS).' + ttl = 30 + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.credentials: Optional[CredentialsConfiguration] = None + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 10) -> None: + super().add_parser_arguments(add, default_propagation_seconds) + add('credentials', help='DigitalOcean credentials INI file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the DigitalOcean API.' + + def _setup_credentials(self) -> None: + self.credentials = self._configure_credentials( + 'credentials', + 'DigitalOcean credentials INI file', + { + 'token': 'API token for DigitalOcean account' + } + ) + + def _perform(self, domain: str, validation_name: str, validation: str) -> None: + self._get_digitalocean_client().add_txt_record(domain, validation_name, validation, + self.ttl) + + def _cleanup(self, domain: str, validation_name: str, validation: str) -> None: + self._get_digitalocean_client().del_txt_record(domain, validation_name, validation) + + def _get_digitalocean_client(self) -> "_DigitalOceanClient": + if not self.credentials: # pragma: no cover + raise errors.Error("Plugin has not been prepared.") + return _DigitalOceanClient(cast(str, self.credentials.conf('token'))) + + +class _DigitalOceanClient: + """ + Encapsulates all communication with the DigitalOcean API. + """ + + def __init__(self, token: str) -> None: + self.manager = digitalocean.Manager(token=token) + + def add_txt_record(self, domain_name: str, record_name: str, record_content: str, + record_ttl: int) -> None: + """ + Add a TXT record using the supplied information. + + :param str domain_name: The domain to use to associate the record with. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + :param int record_ttl: The record TTL. + :raises certbot.errors.PluginError: if an error occurs communicating with the DigitalOcean + API + """ + + try: + domain = self._find_domain(domain_name) + # The TTL value is set to the SOA record's TTL. Unless set to a falsy value, + # the optional TTL argument to add_txt_record() would be ignored. + # See https://github.com/certbot/certbot/pull/9149 for details. + domain.ttl = None + except digitalocean.Error as e: + hint = None + + if str(e).startswith("Unable to authenticate"): + hint = 'Did you provide a valid API token?' + + logger.debug('Error finding domain using the DigitalOcean API: %s', e) + raise errors.PluginError('Error finding domain using the DigitalOcean API: {0}{1}' + .format(e, ' ({0})'.format(hint) if hint else '')) + + try: + result = domain.create_new_domain_record( + type='TXT', + name=self._compute_record_name(domain, record_name), + data=record_content, + ttl=record_ttl) # ttl kwarg is only effective starting python-digitalocean 1.15.0 + + record_id = result['domain_record']['id'] + + logger.debug('Successfully added TXT record with id: %d', record_id) + except digitalocean.Error as e: + logger.debug('Error adding TXT record using the DigitalOcean API: %s', e) + raise errors.PluginError('Error adding TXT record using the DigitalOcean API: {0}' + .format(e)) + + def del_txt_record(self, domain_name: str, record_name: str, record_content: str) -> None: + """ + Delete a TXT record using the supplied information. + + Note that both the record's name and content are used to ensure that similar records + created concurrently (e.g., due to concurrent invocations of this plugin) are not deleted. + + Failures are logged, but not raised. + + :param str domain_name: The domain to use to associate the record with. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + """ + + try: + domain = self._find_domain(domain_name) + except digitalocean.Error as e: + logger.debug('Error finding domain using the DigitalOcean API: %s', e) + return + + try: + domain_records = domain.get_records() + + matching_records = [record for record in domain_records + if record.type == 'TXT' + and record.name == self._compute_record_name(domain, record_name) + and record.data == record_content] + except digitalocean.Error as e: + logger.debug('Error getting DNS records using the DigitalOcean API: %s', e) + return + + for record in matching_records: + try: + logger.debug('Removing TXT record with id: %s', record.id) + record.destroy() + except digitalocean.Error as e: + logger.warning('Error deleting TXT record %s using the DigitalOcean API: %s', + record.id, e) + + def _find_domain(self, domain_name: str) -> digitalocean.Domain: + """ + Find the domain object for a given domain name. + + :param str domain_name: The domain name for which to find the corresponding Domain. + :returns: The Domain, if found. + :rtype: `~digitalocean.Domain` + :raises certbot.errors.PluginError: if no matching Domain is found. + """ + + domain_name_guesses = dns_common.base_domain_name_guesses(domain_name) + + domains = self.manager.get_all_domains() + + for guess in domain_name_guesses: + matches = [domain for domain in domains if domain.name == guess] + + if matches: + domain = matches[0] + logger.debug('Found base domain for %s using name %s', domain_name, guess) + return domain + + raise errors.PluginError(f'Unable to determine base domain for {domain_name} using names: ' + f'{domain_name_guesses}.') + + @staticmethod + def _compute_record_name(domain: digitalocean.Domain, full_record_name: str) -> str: + # The domain, from DigitalOcean's point of view, is automatically appended. + return full_record_name.rpartition("." + domain.name)[0] diff --git a/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/tests/__init__.py b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/tests/__init__.py new file mode 100644 index 00000000000..0272d510c22 --- /dev/null +++ b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-digitalocean tests""" diff --git a/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/tests/dns_digitalocean_test.py b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/tests/dns_digitalocean_test.py new file mode 100644 index 00000000000..95af0c85ec8 --- /dev/null +++ b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/_internal/tests/dns_digitalocean_test.py @@ -0,0 +1,177 @@ +"""Tests for certbot_dns_digitalocean._internal.dns_digitalocean.""" + +import sys +import unittest +from unittest import mock + +import digitalocean +import pytest + +from certbot import errors +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins.dns_test_common import DOMAIN +from certbot.tests import util as test_util + +API_ERROR = digitalocean.DataReadError() +TOKEN = 'a-token' + + +class AuthenticatorTest(test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): + + def setUp(self): + from certbot_dns_digitalocean._internal.dns_digitalocean import Authenticator + + super().setUp() + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write({"digitalocean_token": TOKEN}, path) + + self.config = mock.MagicMock(digitalocean_credentials=path, + digitalocean_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "digitalocean") + + self.mock_client = mock.MagicMock() + # _get_digitalocean_client | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.auth, '_get_digitalocean_client', + mock.MagicMock(return_value=self.mock_client)) + + @test_util.patch_display_util() + def test_perform(self, unused_mock_get_utility): + self.auth.perform([self.achall]) + + expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY, 30)] + assert expected == self.mock_client.mock_calls + + def test_cleanup(self): + # _attempt_cleanup | pylint: disable=protected-access + self.auth._attempt_cleanup = True + self.auth.cleanup([self.achall]) + + expected = [mock.call.del_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)] + assert expected == self.mock_client.mock_calls + + +class DigitalOceanClientTest(unittest.TestCase): + + id_num = 1 + record_prefix = "_acme-challenge" + record_name = record_prefix + "." + DOMAIN + record_content = "bar" + record_ttl = 60 + + def setUp(self): + from certbot_dns_digitalocean._internal.dns_digitalocean import _DigitalOceanClient + + self.digitalocean_client = _DigitalOceanClient(TOKEN) + + self.manager = mock.MagicMock() + self.digitalocean_client.manager = self.manager + + def test_add_txt_record(self): + wrong_domain_mock = mock.MagicMock() + wrong_domain_mock.name = "other.invalid" + wrong_domain_mock.create_new_domain_record.side_effect = AssertionError('Wrong Domain') + + domain_mock = mock.MagicMock() + domain_mock.name = DOMAIN + domain_mock.create_new_domain_record.return_value = {'domain_record': {'id': self.id_num}} + + self.manager.get_all_domains.return_value = [wrong_domain_mock, domain_mock] + + self.digitalocean_client.add_txt_record(DOMAIN, self.record_name, self.record_content, + self.record_ttl) + + domain_mock.create_new_domain_record.assert_called_with(type='TXT', + name=self.record_prefix, + data=self.record_content, + ttl=self.record_ttl) + + def test_add_txt_record_fail_to_find_domain(self): + self.manager.get_all_domains.return_value = [] + + with pytest.raises(errors.PluginError): + self.digitalocean_client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + def test_add_txt_record_error_finding_domain(self): + self.manager.get_all_domains.side_effect = API_ERROR + + with pytest.raises(errors.PluginError): + self.digitalocean_client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + def test_add_txt_record_error_creating_record(self): + domain_mock = mock.MagicMock() + domain_mock.name = DOMAIN + domain_mock.create_new_domain_record.side_effect = API_ERROR + + self.manager.get_all_domains.return_value = [domain_mock] + + with pytest.raises(errors.PluginError): + self.digitalocean_client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + def test_del_txt_record(self): + first_record_mock = mock.MagicMock() + first_record_mock.type = 'TXT' + first_record_mock.name = "DIFFERENT" + first_record_mock.data = self.record_content + + correct_record_mock = mock.MagicMock() + correct_record_mock.type = 'TXT' + correct_record_mock.name = self.record_prefix + correct_record_mock.data = self.record_content + + last_record_mock = mock.MagicMock() + last_record_mock.type = 'TXT' + last_record_mock.name = self.record_prefix + last_record_mock.data = "DIFFERENT" + + domain_mock = mock.MagicMock() + domain_mock.name = DOMAIN + domain_mock.get_records.return_value = [first_record_mock, + correct_record_mock, + last_record_mock] + + self.manager.get_all_domains.return_value = [domain_mock] + + self.digitalocean_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + assert correct_record_mock.destroy.called + + assert not first_record_mock.destroy.call_args_list + assert not last_record_mock.destroy.call_args_list + + def test_del_txt_record_error_finding_domain(self): + self.manager.get_all_domains.side_effect = API_ERROR + + self.digitalocean_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + def test_del_txt_record_error_finding_record(self): + domain_mock = mock.MagicMock() + domain_mock.name = DOMAIN + domain_mock.get_records.side_effect = API_ERROR + + self.manager.get_all_domains.return_value = [domain_mock] + + self.digitalocean_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + def test_del_txt_record_error_deleting_record(self): + record_mock = mock.MagicMock() + record_mock.type = 'TXT' + record_mock.name = self.record_prefix + record_mock.data = self.record_content + record_mock.destroy.side_effect = API_ERROR + + domain_mock = mock.MagicMock() + domain_mock.name = DOMAIN + domain_mock.get_records.return_value = [record_mock] + + self.manager.get_all_domains.return_value = [domain_mock] + + self.digitalocean_client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-digitalocean/src/certbot_dns_digitalocean/py.typed b/certbot-dns-digitalocean/src/certbot_dns_digitalocean/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-dnsimple/.readthedocs.yaml b/certbot-dns-dnsimple/.readthedocs.yaml new file mode 100644 index 00000000000..b3418f3b343 --- /dev/null +++ b/certbot-dns-dnsimple/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-dnsimple[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-dnsimple/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-dnsimple/Dockerfile b/certbot-dns-dnsimple/Dockerfile deleted file mode 100644 index 38d2be80eb3..00000000000 --- a/certbot-dns-dnsimple/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-dnsimple - -RUN pip install --no-cache-dir --editable src/certbot-dns-dnsimple diff --git a/certbot-dns-dnsimple/MANIFEST.in b/certbot-dns-dnsimple/MANIFEST.in index 18f018c08d3..9004f3e48f8 100644 --- a/certbot-dns-dnsimple/MANIFEST.in +++ b/certbot-dns-dnsimple/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_dnsimple/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-dnsimple/certbot_dns_dnsimple/__init__.py b/certbot-dns-dnsimple/certbot_dns_dnsimple/__init__.py deleted file mode 100644 index f8a2e83aa85..00000000000 --- a/certbot-dns-dnsimple/certbot_dns_dnsimple/__init__.py +++ /dev/null @@ -1,85 +0,0 @@ -""" -The `~certbot_dns_dnsimple.dns_dnsimple` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the DNSimple API. - - -Named Arguments ---------------- - -======================================== ===================================== -``--dns-dnsimple-credentials`` DNSimple credentials_ INI file. - (Required) -``--dns-dnsimple-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 30) -======================================== ===================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing DNSimple API -credentials, obtained from your DNSimple -`account page `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # DNSimple API credentials used by Certbot - dns_dnsimple_token = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw - -The path to this file can be provided interactively or using the -``--dns-dnsimple-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - DNSimple account. Users who can read this file can use these credentials - to issue arbitrary API calls on your behalf. Users who can cause Certbot to - run using these credentials can complete a ``dns-01`` challenge to acquire - new certificates or revoke existing certificates for associated domains, - even if those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-dnsimple \\ - --dns-dnsimple-credentials ~/.secrets/certbot/dnsimple.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-dnsimple \\ - --dns-dnsimple-credentials ~/.secrets/certbot/dnsimple.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 60 seconds - for DNS propagation - - certbot certonly \\ - --dns-dnsimple \\ - --dns-dnsimple-credentials ~/.secrets/certbot/dnsimple.ini \\ - --dns-dnsimple-propagation-seconds 60 \\ - -d example.com - -""" diff --git a/certbot-dns-dnsimple/certbot_dns_dnsimple/dns_dnsimple.py b/certbot-dns-dnsimple/certbot_dns_dnsimple/dns_dnsimple.py deleted file mode 100644 index ad2a3fa304f..00000000000 --- a/certbot-dns-dnsimple/certbot_dns_dnsimple/dns_dnsimple.py +++ /dev/null @@ -1,82 +0,0 @@ -"""DNS Authenticator for DNSimple DNS.""" -import logging - -import zope.interface -from lexicon.providers import dnsimple - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common -from certbot.plugins import dns_common_lexicon - -logger = logging.getLogger(__name__) - -ACCOUNT_URL = 'https://dnsimple.com/user' - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for DNSimple - - This Authenticator uses the DNSimple v2 API to fulfill a dns-01 challenge. - """ - - description = 'Obtain certificates using a DNS TXT record (if you are using DNSimple for DNS).' - ttl = 60 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=30) - add('credentials', help='DNSimple credentials INI file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the DNSimple API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'DNSimple credentials INI file', - { - 'token': 'User access token for DNSimple v2 API. (See {0}.)'.format(ACCOUNT_URL) - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_dnsimple_client().add_txt_record(domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_dnsimple_client().del_txt_record(domain, validation_name, validation) - - def _get_dnsimple_client(self): - return _DNSimpleLexiconClient(self.credentials.conf('token'), self.ttl) - - -class _DNSimpleLexiconClient(dns_common_lexicon.LexiconClient): - """ - Encapsulates all communication with the DNSimple via Lexicon. - """ - - def __init__(self, token, ttl): - super(_DNSimpleLexiconClient, self).__init__() - - config = dns_common_lexicon.build_lexicon_config('dnssimple', { - 'ttl': ttl, - }, { - 'auth_token': token, - }) - - self.provider = dnsimple.Provider(config) - - def _handle_http_error(self, e, domain_name): - hint = None - if str(e).startswith('401 Client Error: Unauthorized for url:'): - hint = 'Is your API token value correct?' - - return errors.PluginError('Error determining zone identifier for {0}: {1}.{2}' - .format(domain_name, e, ' ({0})'.format(hint) if hint else '')) diff --git a/certbot-dns-dnsimple/certbot_dns_dnsimple/dns_dnsimple_test.py b/certbot-dns-dnsimple/certbot_dns_dnsimple/dns_dnsimple_test.py deleted file mode 100644 index d8f3a23ea56..00000000000 --- a/certbot-dns-dnsimple/certbot_dns_dnsimple/dns_dnsimple_test.py +++ /dev/null @@ -1,51 +0,0 @@ -"""Tests for certbot_dns_dnsimple.dns_dnsimple.""" - -import os -import unittest - -import mock -from requests.exceptions import HTTPError - -from certbot.plugins import dns_test_common -from certbot.plugins import dns_test_common_lexicon -from certbot.tests import util as test_util - -TOKEN = 'foo' - - -class AuthenticatorTest(test_util.TempDirTestCase, - dns_test_common_lexicon.BaseLexiconAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_dnsimple.dns_dnsimple import Authenticator - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"dnsimple_token": TOKEN}, path) - - self.config = mock.MagicMock(dnsimple_credentials=path, - dnsimple_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "dnsimple") - - self.mock_client = mock.MagicMock() - # _get_dnsimple_client | pylint: disable=protected-access - self.auth._get_dnsimple_client = mock.MagicMock(return_value=self.mock_client) - - -class DNSimpleLexiconClientTest(unittest.TestCase, dns_test_common_lexicon.BaseLexiconClientTest): - - LOGIN_ERROR = HTTPError('401 Client Error: Unauthorized for url: ...') - - def setUp(self): - from certbot_dns_dnsimple.dns_dnsimple import _DNSimpleLexiconClient - - self.client = _DNSimpleLexiconClient(TOKEN, 0) - - self.provider_mock = mock.MagicMock() - self.client.provider = self.provider_mock - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-dnsimple/docs/api.rst b/certbot-dns-dnsimple/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-dnsimple/docs/api.rst +++ b/certbot-dns-dnsimple/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-dnsimple/docs/api/dns_dnsimple.rst b/certbot-dns-dnsimple/docs/api/dns_dnsimple.rst deleted file mode 100644 index b0544107b1d..00000000000 --- a/certbot-dns-dnsimple/docs/api/dns_dnsimple.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_dnsimple.dns_dnsimple` ----------------------------------------- - -.. automodule:: certbot_dns_dnsimple.dns_dnsimple - :members: diff --git a/certbot-dns-dnsimple/docs/conf.py b/certbot-dns-dnsimple/docs/conf.py index da692fb9ef3..201ffdca6ed 100644 --- a/certbot-dns-dnsimple/docs/conf.py +++ b/certbot-dns-dnsimple/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-dnsimple/docs/make.bat b/certbot-dns-dnsimple/docs/make.bat index 78e867256ab..997c0864e24 100644 --- a/certbot-dns-dnsimple/docs/make.bat +++ b/certbot-dns-dnsimple/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-dnsimple/local-oldest-requirements.txt b/certbot-dns-dnsimple/local-oldest-requirements.txt deleted file mode 100644 index 5ecd3cbc700..00000000000 --- a/certbot-dns-dnsimple/local-oldest-requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ --e acme[dev] --e .[dev] -dns-lexicon==2.2.1 \ No newline at end of file diff --git a/certbot-dns-dnsimple/pyproject.toml b/certbot-dns-dnsimple/pyproject.toml new file mode 100644 index 00000000000..8127fe8d63f --- /dev/null +++ b/certbot-dns-dnsimple/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-dnsimple" +dynamic = ["version", "dependencies"] +description = "DNSimple DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-dnsimple = "certbot_dns_dnsimple._internal.dns_dnsimple:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-dnsimple/readthedocs.org.requirements.txt b/certbot-dns-dnsimple/readthedocs.org.requirements.txt deleted file mode 100644 index fef73916c6a..00000000000 --- a/certbot-dns-dnsimple/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-dnsimple[docs] diff --git a/certbot-dns-dnsimple/setup.cfg b/certbot-dns-dnsimple/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-dnsimple/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-dnsimple/setup.py b/certbot-dns-dnsimple/setup.py index 1a2ce5d9205..8e16f34db7e 100644 --- a/certbot-dns-dnsimple/setup.py +++ b/certbot-dns-dnsimple/setup.py @@ -1,66 +1,27 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.31.0.dev0', - 'certbot>=0.31.0.dev0', - 'dns-lexicon>=2.2.1', # Support for >1 TXT record per name - 'mock', - 'setuptools', - 'zope.interface', + # This version of lexicon is required to address the problem described in + # https://github.com/AnalogJ/lexicon/issues/387. + 'dns-lexicon>=3.14.1', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-dnsimple', version=version, - description="DNSimple DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-dnsimple = certbot_dns_dnsimple.dns_dnsimple:Authenticator', - ], - }, - test_suite='certbot_dns_dnsimple', ) diff --git a/certbot-dns-dnsimple/src/certbot_dns_dnsimple/__init__.py b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/__init__.py new file mode 100644 index 00000000000..0f6168a137f --- /dev/null +++ b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/__init__.py @@ -0,0 +1,89 @@ +""" +The `~certbot_dns_dnsimple.dns_dnsimple` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using the DNSimple API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +======================================== ===================================== +``--dns-dnsimple-credentials`` DNSimple credentials_ INI file. + (Required) +``--dns-dnsimple-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 30) +======================================== ===================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing DNSimple API +credentials, obtained from your DNSimple +`account page `_. + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # DNSimple API credentials used by Certbot + dns_dnsimple_token = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw + +The path to this file can be provided interactively or using the +``--dns-dnsimple-credentials`` command-line argument. Certbot records the path +to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + DNSimple account. Users who can read this file can use these credentials + to issue arbitrary API calls on your behalf. Users who can cause Certbot to + run using these credentials can complete a ``dns-01`` challenge to acquire + new certificates or revoke existing certificates for associated domains, + even if those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-dnsimple \\ + --dns-dnsimple-credentials ~/.secrets/certbot/dnsimple.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-dnsimple \\ + --dns-dnsimple-credentials ~/.secrets/certbot/dnsimple.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 60 seconds + for DNS propagation + + certbot certonly \\ + --dns-dnsimple \\ + --dns-dnsimple-credentials ~/.secrets/certbot/dnsimple.ini \\ + --dns-dnsimple-propagation-seconds 60 \\ + -d example.com + +""" diff --git a/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/__init__.py b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/__init__.py new file mode 100644 index 00000000000..070927555a1 --- /dev/null +++ b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_dnsimple.dns_dnsimple` plugin.""" diff --git a/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/dns_dnsimple.py b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/dns_dnsimple.py new file mode 100644 index 00000000000..c9ef1cdd186 --- /dev/null +++ b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/dns_dnsimple.py @@ -0,0 +1,52 @@ +"""DNS Authenticator for DNSimple DNS.""" +import logging +from typing import Any +from typing import Callable + +from requests import HTTPError + +from certbot import errors +from certbot.plugins import dns_common_lexicon + +logger = logging.getLogger(__name__) + +ACCOUNT_URL = 'https://dnsimple.com/user' + + +class Authenticator(dns_common_lexicon.LexiconDNSAuthenticator): + """DNS Authenticator for DNSimple + + This Authenticator uses the DNSimple v2 API to fulfill a dns-01 challenge. + """ + + description = 'Obtain certificates using a DNS TXT record (if you are using DNSimple for DNS).' + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._add_provider_option('token', + f'User access token for DNSimple v2 API. (See {ACCOUNT_URL}.)', + 'auth_token') + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 30) -> None: + super().add_parser_arguments(add, default_propagation_seconds) + add('credentials', help='DNSimple credentials INI file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the DNSimple API.' + + @property + def _provider_name(self) -> str: + return 'dnsimple' + + def _handle_http_error(self, e: HTTPError, domain_name: str) -> errors.PluginError: + hint = None + if str(e).startswith('401 Client Error: Unauthorized for url:'): + hint = 'Is your API token value correct?' + + hint_disp = f' ({hint})' if hint else '' + + return errors.PluginError(f'Error determining zone identifier for {domain_name}: ' + f'{e}.{hint_disp}') diff --git a/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/tests/__init__.py b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/tests/__init__.py new file mode 100644 index 00000000000..a72b74812a1 --- /dev/null +++ b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-dnsimple tests""" diff --git a/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/tests/dns_dnsimple_test.py b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/tests/dns_dnsimple_test.py new file mode 100644 index 00000000000..e0d389be13d --- /dev/null +++ b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/_internal/tests/dns_dnsimple_test.py @@ -0,0 +1,37 @@ +"""Tests for certbot_dns_dnsimple._internal.dns_dnsimple.""" +from unittest import mock +import sys + +import pytest +from requests import Response +from requests.exceptions import HTTPError + +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins import dns_test_common_lexicon +from certbot.tests import util as test_util + +TOKEN = 'foo' + + +class AuthenticatorTest(test_util.TempDirTestCase, + dns_test_common_lexicon.BaseLexiconDNSAuthenticatorTest): + + LOGIN_ERROR = HTTPError('401 Client Error: Unauthorized for url: ...', response=Response()) + + def setUp(self): + super().setUp() + + from certbot_dns_dnsimple._internal.dns_dnsimple import Authenticator + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write({"dnsimple_token": TOKEN}, path) + + self.config = mock.MagicMock(dnsimple_credentials=path, + dnsimple_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "dnsimple") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-dnsimple/src/certbot_dns_dnsimple/py.typed b/certbot-dns-dnsimple/src/certbot_dns_dnsimple/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-dnsmadeeasy/.readthedocs.yaml b/certbot-dns-dnsmadeeasy/.readthedocs.yaml new file mode 100644 index 00000000000..b3be7b3a8c0 --- /dev/null +++ b/certbot-dns-dnsmadeeasy/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-dnsmadeeasy[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-dnsmadeeasy/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-dnsmadeeasy/Dockerfile b/certbot-dns-dnsmadeeasy/Dockerfile deleted file mode 100644 index ff7936925c3..00000000000 --- a/certbot-dns-dnsmadeeasy/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-dnsmadeeasy - -RUN pip install --no-cache-dir --editable src/certbot-dns-dnsmadeeasy diff --git a/certbot-dns-dnsmadeeasy/MANIFEST.in b/certbot-dns-dnsmadeeasy/MANIFEST.in index 18f018c08d3..1d6bcc4f385 100644 --- a/certbot-dns-dnsmadeeasy/MANIFEST.in +++ b/certbot-dns-dnsmadeeasy/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_dnsmadeeasy/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy/__init__.py b/certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy/__init__.py deleted file mode 100644 index 52f05523741..00000000000 --- a/certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy/__init__.py +++ /dev/null @@ -1,86 +0,0 @@ -""" -The `~certbot_dns_dnsmadeeasy.dns_dnsmadeeasy` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the DNS Made Easy API. - - -Named Arguments ---------------- - -========================================= ===================================== -``--dns-dnsmadeeasy-credentials`` DNS Made Easy credentials_ INI file. - (Required) -``--dns-dnsmadeeasy-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 60) -========================================= ===================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing DNS Made Easy API -credentials, obtained from your DNS Made Easy -`account page `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # DNS Made Easy API credentials used by Certbot - dns_dnsmadeeasy_api_key = 1c1a3c91-4770-4ce7-96f4-54c0eb0e457a - dns_dnsmadeeasy_secret_key = c9b5625f-9834-4ff8-baba-4ed5f32cae55 - -The path to this file can be provided interactively or using the -``--dns-dnsmadeeasy-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - DNS Made Easy account. Users who can read this file can use these credentials - to issue arbitrary API calls on your behalf. Users who can cause Certbot to - run using these credentials can complete a ``dns-01`` challenge to acquire - new certificates or revoke existing certificates for associated domains, - even if those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-dnsmadeeasy \\ - --dns-dnsmadeeasy-credentials ~/.secrets/certbot/dnsmadeeasy.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-dnsmadeeasy \\ - --dns-dnsmadeeasy-credentials ~/.secrets/certbot/dnsmadeeasy.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 2 minutes - for DNS propagation - - certbot certonly \\ - --dns-dnsmadeeasy \\ - --dns-dnsmadeeasy-credentials ~/.secrets/certbot/dnsmadeeasy.ini \\ - --dns-dnsmadeeasy-propagation-seconds 120 \\ - -d example.com - -""" diff --git a/certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy/dns_dnsmadeeasy.py b/certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy/dns_dnsmadeeasy.py deleted file mode 100644 index 4b63cb4b5d8..00000000000 --- a/certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy/dns_dnsmadeeasy.py +++ /dev/null @@ -1,92 +0,0 @@ -"""DNS Authenticator for DNS Made Easy DNS.""" -import logging - -import zope.interface -from lexicon.providers import dnsmadeeasy - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common -from certbot.plugins import dns_common_lexicon - -logger = logging.getLogger(__name__) - -ACCOUNT_URL = 'https://cp.dnsmadeeasy.com/account/info' - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for DNS Made Easy - - This Authenticator uses the DNS Made Easy API to fulfill a dns-01 challenge. - """ - - description = ('Obtain certificates using a DNS TXT record (if you are using DNS Made Easy for ' - 'DNS).') - ttl = 60 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=60) - add('credentials', help='DNS Made Easy credentials INI file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the DNS Made Easy API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'DNS Made Easy credentials INI file', - { - 'api-key': 'API key for DNS Made Easy account, obtained from {0}' - .format(ACCOUNT_URL), - 'secret-key': 'Secret key for DNS Made Easy account, obtained from {0}' - .format(ACCOUNT_URL) - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_dnsmadeeasy_client().add_txt_record(domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_dnsmadeeasy_client().del_txt_record(domain, validation_name, validation) - - def _get_dnsmadeeasy_client(self): - return _DNSMadeEasyLexiconClient(self.credentials.conf('api-key'), - self.credentials.conf('secret-key'), - self.ttl) - - -class _DNSMadeEasyLexiconClient(dns_common_lexicon.LexiconClient): - """ - Encapsulates all communication with the DNS Made Easy via Lexicon. - """ - - def __init__(self, api_key, secret_key, ttl): - super(_DNSMadeEasyLexiconClient, self).__init__() - - config = dns_common_lexicon.build_lexicon_config('dnsmadeeasy', { - 'ttl': ttl, - }, { - 'auth_username': api_key, - 'auth_token': secret_key, - }) - - self.provider = dnsmadeeasy.Provider(config) - - def _handle_http_error(self, e, domain_name): - if domain_name in str(e) and str(e).startswith('404 Client Error: Not Found for url:'): - return - - hint = None - if str(e).startswith('403 Client Error: Forbidden for url:'): - hint = 'Are your API key and Secret key values correct?' - - return errors.PluginError('Error determining zone identifier: {0}.{1}' - .format(e, ' ({0})'.format(hint) if hint else '')) diff --git a/certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy/dns_dnsmadeeasy_test.py b/certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy/dns_dnsmadeeasy_test.py deleted file mode 100644 index 44a777e1b41..00000000000 --- a/certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy/dns_dnsmadeeasy_test.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Tests for certbot_dns_dnsmadeeasy.dns_dnsmadeeasy.""" - -import os -import unittest - -import mock -from requests.exceptions import HTTPError - -from certbot.plugins import dns_test_common -from certbot.plugins import dns_test_common_lexicon -from certbot.plugins.dns_test_common import DOMAIN -from certbot.tests import util as test_util - -API_KEY = 'foo' -SECRET_KEY = 'bar' - - -class AuthenticatorTest(test_util.TempDirTestCase, - dns_test_common_lexicon.BaseLexiconAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_dnsmadeeasy.dns_dnsmadeeasy import Authenticator - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"dnsmadeeasy_api_key": API_KEY, - "dnsmadeeasy_secret_key": SECRET_KEY}, - path) - - self.config = mock.MagicMock(dnsmadeeasy_credentials=path, - dnsmadeeasy_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "dnsmadeeasy") - - self.mock_client = mock.MagicMock() - # _get_dnsmadeeasy_client | pylint: disable=protected-access - self.auth._get_dnsmadeeasy_client = mock.MagicMock(return_value=self.mock_client) - - -class DNSMadeEasyLexiconClientTest(unittest.TestCase, - dns_test_common_lexicon.BaseLexiconClientTest): - DOMAIN_NOT_FOUND = HTTPError('404 Client Error: Not Found for url: {0}.'.format(DOMAIN)) - LOGIN_ERROR = HTTPError('403 Client Error: Forbidden for url: {0}.'.format(DOMAIN)) - - def setUp(self): - from certbot_dns_dnsmadeeasy.dns_dnsmadeeasy import _DNSMadeEasyLexiconClient - - self.client = _DNSMadeEasyLexiconClient(API_KEY, SECRET_KEY, 0) - - self.provider_mock = mock.MagicMock() - self.client.provider = self.provider_mock - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-dnsmadeeasy/docs/api.rst b/certbot-dns-dnsmadeeasy/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-dnsmadeeasy/docs/api.rst +++ b/certbot-dns-dnsmadeeasy/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-dnsmadeeasy/docs/api/dns_dnsmadeeasy.rst b/certbot-dns-dnsmadeeasy/docs/api/dns_dnsmadeeasy.rst deleted file mode 100644 index 81948a77fd8..00000000000 --- a/certbot-dns-dnsmadeeasy/docs/api/dns_dnsmadeeasy.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_dnsmadeeasy.dns_dnsmadeeasy` ------------------------------------- - -.. automodule:: certbot_dns_dnsmadeeasy.dns_dnsmadeeasy - :members: diff --git a/certbot-dns-dnsmadeeasy/docs/conf.py b/certbot-dns-dnsmadeeasy/docs/conf.py index 7d26f9742f7..312ad687971 100644 --- a/certbot-dns-dnsmadeeasy/docs/conf.py +++ b/certbot-dns-dnsmadeeasy/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-dnsmadeeasy/docs/make.bat b/certbot-dns-dnsmadeeasy/docs/make.bat index f204c8393b4..dbaad7d4e70 100644 --- a/certbot-dns-dnsmadeeasy/docs/make.bat +++ b/certbot-dns-dnsmadeeasy/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-dnsmadeeasy/local-oldest-requirements.txt b/certbot-dns-dnsmadeeasy/local-oldest-requirements.txt deleted file mode 100644 index 65f5a758ea0..00000000000 --- a/certbot-dns-dnsmadeeasy/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ --e acme[dev] --e .[dev] diff --git a/certbot-dns-dnsmadeeasy/pyproject.toml b/certbot-dns-dnsmadeeasy/pyproject.toml new file mode 100644 index 00000000000..c5f4a9faa9a --- /dev/null +++ b/certbot-dns-dnsmadeeasy/pyproject.toml @@ -0,0 +1,55 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-dnsmadeeasy" +dynamic = ["version", "dependencies"] +description = "DNS Made Easy DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-dnsmadeeasy = "certbot_dns_dnsmadeeasy._internal.dns_dnsmadeeasy:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-dnsmadeeasy/readthedocs.org.requirements.txt b/certbot-dns-dnsmadeeasy/readthedocs.org.requirements.txt deleted file mode 100644 index 8f8c6c7316a..00000000000 --- a/certbot-dns-dnsmadeeasy/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-dnsmadeeasy[docs] diff --git a/certbot-dns-dnsmadeeasy/setup.cfg b/certbot-dns-dnsmadeeasy/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-dnsmadeeasy/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-dnsmadeeasy/setup.py b/certbot-dns-dnsmadeeasy/setup.py index 0a99f452d83..e17fd5066ee 100644 --- a/certbot-dns-dnsmadeeasy/setup.py +++ b/certbot-dns-dnsmadeeasy/setup.py @@ -1,66 +1,25 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.31.0.dev0', - 'certbot>=0.31.0.dev0', - 'dns-lexicon>=2.2.1', # Support for >1 TXT record per name - 'mock', - 'setuptools', - 'zope.interface', + 'dns-lexicon>=3.14.1', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-dnsmadeeasy', version=version, - description="DNS Made Easy DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-dnsmadeeasy = certbot_dns_dnsmadeeasy.dns_dnsmadeeasy:Authenticator', - ], - }, - test_suite='certbot_dns_dnsmadeeasy', ) diff --git a/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/__init__.py b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/__init__.py new file mode 100644 index 00000000000..fa49ee51677 --- /dev/null +++ b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/__init__.py @@ -0,0 +1,90 @@ +""" +The `~certbot_dns_dnsmadeeasy.dns_dnsmadeeasy` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using the DNS Made Easy API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +========================================= ===================================== +``--dns-dnsmadeeasy-credentials`` DNS Made Easy credentials_ INI file. + (Required) +``--dns-dnsmadeeasy-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 60) +========================================= ===================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing DNS Made Easy API +credentials, obtained from your DNS Made Easy +`account page `_. + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # DNS Made Easy API credentials used by Certbot + dns_dnsmadeeasy_api_key = 1c1a3c91-4770-4ce7-96f4-54c0eb0e457a + dns_dnsmadeeasy_secret_key = c9b5625f-9834-4ff8-baba-4ed5f32cae55 + +The path to this file can be provided interactively or using the +``--dns-dnsmadeeasy-credentials`` command-line argument. Certbot records the path +to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + DNS Made Easy account. Users who can read this file can use these credentials + to issue arbitrary API calls on your behalf. Users who can cause Certbot to + run using these credentials can complete a ``dns-01`` challenge to acquire + new certificates or revoke existing certificates for associated domains, + even if those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-dnsmadeeasy \\ + --dns-dnsmadeeasy-credentials ~/.secrets/certbot/dnsmadeeasy.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-dnsmadeeasy \\ + --dns-dnsmadeeasy-credentials ~/.secrets/certbot/dnsmadeeasy.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 2 minutes + for DNS propagation + + certbot certonly \\ + --dns-dnsmadeeasy \\ + --dns-dnsmadeeasy-credentials ~/.secrets/certbot/dnsmadeeasy.ini \\ + --dns-dnsmadeeasy-propagation-seconds 120 \\ + -d example.com + +""" diff --git a/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/__init__.py b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/__init__.py new file mode 100644 index 00000000000..37350ce0b1e --- /dev/null +++ b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_dnsmadeeasy.dns_dnsmadeeasy` plugin.""" diff --git a/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/dns_dnsmadeeasy.py b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/dns_dnsmadeeasy.py new file mode 100644 index 00000000000..24733ce1956 --- /dev/null +++ b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/dns_dnsmadeeasy.py @@ -0,0 +1,61 @@ +"""DNS Authenticator for DNS Made Easy DNS.""" +import logging +from typing import Any +from typing import Callable +from typing import Optional + +from requests import HTTPError + +from certbot import errors +from certbot.plugins import dns_common_lexicon + +logger = logging.getLogger(__name__) + +ACCOUNT_URL = 'https://cp.dnsmadeeasy.com/account/info' + + +class Authenticator(dns_common_lexicon.LexiconDNSAuthenticator): + """DNS Authenticator for DNS Made Easy + + This Authenticator uses the DNS Made Easy API to fulfill a dns-01 challenge. + """ + + description = ('Obtain certificates using a DNS TXT record (if you are using DNS Made Easy for ' + 'DNS).') + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._add_provider_option('api-key', + 'API key for DNS Made Easy account, ' + f'obtained from {ACCOUNT_URL}', + 'auth_username') + self._add_provider_option('secret-key', + 'Secret key for DNS Made Easy account, ' + f'obtained from {ACCOUNT_URL}', + 'auth_token') + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 60) -> None: + super().add_parser_arguments(add, default_propagation_seconds) + add('credentials', help='DNS Made Easy credentials INI file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the DNS Made Easy API.' + + @property + def _provider_name(self) -> str: + return 'dnsmadeeasy' + + def _handle_http_error(self, e: HTTPError, domain_name: str) -> Optional[errors.PluginError]: + if domain_name in str(e) and str(e).startswith('404 Client Error: Not Found for url:'): + return None + + hint = None + if str(e).startswith('403 Client Error: Forbidden for url:'): + hint = 'Are your API key and Secret key values correct?' + + hint_disp = f' ({hint})' if hint else '' + + return errors.PluginError(f'Error determining zone identifier: {e}.{hint_disp}') diff --git a/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/tests/__init__.py b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/tests/__init__.py new file mode 100644 index 00000000000..d388800190f --- /dev/null +++ b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-dnsmadeeasy tests""" diff --git a/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/tests/dns_dnsmadeeasy_test.py b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/tests/dns_dnsmadeeasy_test.py new file mode 100644 index 00000000000..4adb70643ad --- /dev/null +++ b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/_internal/tests/dns_dnsmadeeasy_test.py @@ -0,0 +1,43 @@ +"""Tests for certbot_dns_dnsmadeeasy._internal.dns_dnsmadeeasy.""" + +import sys +from unittest import mock + +import pytest +from requests import Response +from requests.exceptions import HTTPError + +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins import dns_test_common_lexicon +from certbot.plugins.dns_test_common import DOMAIN +from certbot.tests import util as test_util + +API_KEY = 'foo' +SECRET_KEY = 'bar' + + +class AuthenticatorTest(test_util.TempDirTestCase, + dns_test_common_lexicon.BaseLexiconDNSAuthenticatorTest): + + DOMAIN_NOT_FOUND = HTTPError(f'404 Client Error: Not Found for url: {DOMAIN}.', response=Response()) + LOGIN_ERROR = HTTPError(f'403 Client Error: Forbidden for url: {DOMAIN}.', response=Response()) + + def setUp(self): + super().setUp() + + from certbot_dns_dnsmadeeasy._internal.dns_dnsmadeeasy import Authenticator + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write({"dnsmadeeasy_api_key": API_KEY, + "dnsmadeeasy_secret_key": SECRET_KEY}, + path) + + self.config = mock.MagicMock(dnsmadeeasy_credentials=path, + dnsmadeeasy_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "dnsmadeeasy") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/py.typed b/certbot-dns-dnsmadeeasy/src/certbot_dns_dnsmadeeasy/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-gehirn/.readthedocs.yaml b/certbot-dns-gehirn/.readthedocs.yaml new file mode 100644 index 00000000000..5c42c23c7af --- /dev/null +++ b/certbot-dns-gehirn/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-gehirn[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-gehirn/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-gehirn/Dockerfile b/certbot-dns-gehirn/Dockerfile deleted file mode 100644 index 48ad902b51f..00000000000 --- a/certbot-dns-gehirn/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-gehirn - -RUN pip install --no-cache-dir --editable src/certbot-dns-gehirn diff --git a/certbot-dns-gehirn/MANIFEST.in b/certbot-dns-gehirn/MANIFEST.in index 18f018c08d3..4733013ff0d 100644 --- a/certbot-dns-gehirn/MANIFEST.in +++ b/certbot-dns-gehirn/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_gehirn/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-gehirn/README.rst b/certbot-dns-gehirn/README.rst index 16058eff844..7a825bd7e6f 100644 --- a/certbot-dns-gehirn/README.rst +++ b/certbot-dns-gehirn/README.rst @@ -1 +1 @@ -Gehirn Infrastracture Service DNS Authenticator plugin for Certbot +Gehirn Infrastructure Service DNS Authenticator plugin for Certbot diff --git a/certbot-dns-gehirn/certbot_dns_gehirn/__init__.py b/certbot-dns-gehirn/certbot_dns_gehirn/__init__.py deleted file mode 100644 index db54154ac84..00000000000 --- a/certbot-dns-gehirn/certbot_dns_gehirn/__init__.py +++ /dev/null @@ -1,88 +0,0 @@ -""" -The `~certbot_dns_gehirn.dns_gehirn` plugin automates the process of completing -a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and subsequently -removing, TXT records using the Gehirn Infrastracture Service DNS API. - - -Named Arguments ---------------- - -======================================== ===================================== -``--dns-gehirn-credentials`` Gehirn Infrastracture Service - credentials_ INI file. - (Required) -``--dns-gehirn-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 30) -======================================== ===================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing -Gehirn Infrastracture Service DNS API credentials, -obtained from your Gehirn Infrastracture Service -`dashboard `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # Gehirn Infrastracture Service API credentials used by Certbot - dns_gehirn_api_token = 00000000-0000-0000-0000-000000000000 - dns_gehirn_api_secret = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw - -The path to this file can be provided interactively or using the -``--dns-gehirn-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - Gehirn Infrastracture Service account. Users who can read this file can use - these credentials to issue arbitrary API calls on your behalf. Users who can - cause Certbot to run using these credentials can complete a ``dns-01`` - challenge to acquire new certificates or revoke existing certificates for - associated domains, even if those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-gehirn \\ - --dns-gehirn-credentials ~/.secrets/certbot/gehirn.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-gehirn \\ - --dns-gehirn-credentials ~/.secrets/certbot/gehirn.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 60 seconds - for DNS propagation - - certbot certonly \\ - --dns-gehirn \\ - --dns-gehirn-credentials ~/.secrets/certbot/gehirn.ini \\ - --dns-gehirn-propagation-seconds 60 \\ - -d example.com - -""" diff --git a/certbot-dns-gehirn/certbot_dns_gehirn/dns_gehirn.py b/certbot-dns-gehirn/certbot_dns_gehirn/dns_gehirn.py deleted file mode 100644 index edf5300724e..00000000000 --- a/certbot-dns-gehirn/certbot_dns_gehirn/dns_gehirn.py +++ /dev/null @@ -1,87 +0,0 @@ -"""DNS Authenticator for Gehirn Infrastracture Service DNS.""" -import logging - -import zope.interface -from lexicon.providers import gehirn - -from certbot import interfaces -from certbot.plugins import dns_common -from certbot.plugins import dns_common_lexicon - -logger = logging.getLogger(__name__) - -DASHBOARD_URL = "https://gis.gehirn.jp/" - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for Gehirn Infrastracture Service DNS - - This Authenticator uses the Gehirn Infrastracture Service API to fulfill - a dns-01 challenge. - """ - - description = 'Obtain certificates using a DNS TXT record ' + \ - '(if you are using Gehirn Infrastracture Service for DNS).' - ttl = 60 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=30) - add('credentials', help='Gehirn Infrastracture Service credentials file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the Gehirn Infrastracture Service API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'Gehirn Infrastracture Service credentials file', - { - 'api-token': 'API token for Gehirn Infrastracture Service ' + \ - 'API obtained from {0}'.format(DASHBOARD_URL), - 'api-secret': 'API secret for Gehirn Infrastracture Service ' + \ - 'API obtained from {0}'.format(DASHBOARD_URL), - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_gehirn_client().add_txt_record(domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_gehirn_client().del_txt_record(domain, validation_name, validation) - - def _get_gehirn_client(self): - return _GehirnLexiconClient( - self.credentials.conf('api-token'), - self.credentials.conf('api-secret'), - self.ttl - ) - - -class _GehirnLexiconClient(dns_common_lexicon.LexiconClient): - """ - Encapsulates all communication with the Gehirn Infrastracture Service via Lexicon. - """ - - def __init__(self, api_token, api_secret, ttl): - super(_GehirnLexiconClient, self).__init__() - - config = dns_common_lexicon.build_lexicon_config('gehirn', { - 'ttl': ttl, - }, { - 'auth_token': api_token, - 'auth_secret': api_secret, - }) - - self.provider = gehirn.Provider(config) - - def _handle_http_error(self, e, domain_name): - if domain_name in str(e) and (str(e).startswith('404 Client Error: Not Found for url:')): - return # Expected errors when zone name guess is wrong - return super(_GehirnLexiconClient, self)._handle_http_error(e, domain_name) diff --git a/certbot-dns-gehirn/certbot_dns_gehirn/dns_gehirn_test.py b/certbot-dns-gehirn/certbot_dns_gehirn/dns_gehirn_test.py deleted file mode 100644 index b771c103e77..00000000000 --- a/certbot-dns-gehirn/certbot_dns_gehirn/dns_gehirn_test.py +++ /dev/null @@ -1,55 +0,0 @@ -"""Tests for certbot_dns_gehirn.dns_gehirn.""" - -import os -import unittest - -import mock -from requests.exceptions import HTTPError - -from certbot.plugins import dns_test_common -from certbot.plugins import dns_test_common_lexicon -from certbot.plugins.dns_test_common import DOMAIN -from certbot.tests import util as test_util - -API_TOKEN = '00000000-0000-0000-0000-000000000000' -API_SECRET = 'MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw' - -class AuthenticatorTest(test_util.TempDirTestCase, - dns_test_common_lexicon.BaseLexiconAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_gehirn.dns_gehirn import Authenticator - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write( - {"gehirn_api_token": API_TOKEN, "gehirn_api_secret": API_SECRET}, - path - ) - - self.config = mock.MagicMock(gehirn_credentials=path, - gehirn_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "gehirn") - - self.mock_client = mock.MagicMock() - # _get_gehirn_client | pylint: disable=protected-access - self.auth._get_gehirn_client = mock.MagicMock(return_value=self.mock_client) - - -class GehirnLexiconClientTest(unittest.TestCase, dns_test_common_lexicon.BaseLexiconClientTest): - DOMAIN_NOT_FOUND = HTTPError('404 Client Error: Not Found for url: {0}.'.format(DOMAIN)) - LOGIN_ERROR = HTTPError('401 Client Error: Unauthorized for url: {0}.'.format(DOMAIN)) - - def setUp(self): - from certbot_dns_gehirn.dns_gehirn import _GehirnLexiconClient - - self.client = _GehirnLexiconClient(API_TOKEN, API_SECRET, 0) - - self.provider_mock = mock.MagicMock() - self.client.provider = self.provider_mock - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-gehirn/docs/api.rst b/certbot-dns-gehirn/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-gehirn/docs/api.rst +++ b/certbot-dns-gehirn/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-gehirn/docs/api/dns_gehirn.rst b/certbot-dns-gehirn/docs/api/dns_gehirn.rst deleted file mode 100644 index 35a13e9c14a..00000000000 --- a/certbot-dns-gehirn/docs/api/dns_gehirn.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_gehirn.dns_gehirn` ------------------------------------- - -.. automodule:: certbot_dns_gehirn.dns_gehirn - :members: diff --git a/certbot-dns-gehirn/docs/conf.py b/certbot-dns-gehirn/docs/conf.py index a1b2799fbeb..cb3397f90e6 100644 --- a/certbot-dns-gehirn/docs/conf.py +++ b/certbot-dns-gehirn/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-gehirn/docs/make.bat b/certbot-dns-gehirn/docs/make.bat index 905d4ee9011..d8c6363720b 100644 --- a/certbot-dns-gehirn/docs/make.bat +++ b/certbot-dns-gehirn/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-gehirn/local-oldest-requirements.txt b/certbot-dns-gehirn/local-oldest-requirements.txt deleted file mode 100644 index 65f5a758ea0..00000000000 --- a/certbot-dns-gehirn/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ --e acme[dev] --e .[dev] diff --git a/certbot-dns-gehirn/pyproject.toml b/certbot-dns-gehirn/pyproject.toml new file mode 100644 index 00000000000..129e4bc1186 --- /dev/null +++ b/certbot-dns-gehirn/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-gehirn" +dynamic = ["version", "dependencies"] +description = "Gehirn Infrastructure Service DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-gehirn = "certbot_dns_gehirn._internal.dns_gehirn:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-gehirn/readthedocs.org.requirements.txt b/certbot-dns-gehirn/readthedocs.org.requirements.txt deleted file mode 100644 index d9f4f9823d6..00000000000 --- a/certbot-dns-gehirn/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-gehirn[docs] diff --git a/certbot-dns-gehirn/setup.cfg b/certbot-dns-gehirn/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-gehirn/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-gehirn/setup.py b/certbot-dns-gehirn/setup.py index f4a75379c90..e17fd5066ee 100644 --- a/certbot-dns-gehirn/setup.py +++ b/certbot-dns-gehirn/setup.py @@ -1,64 +1,25 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Please update tox.ini when modifying dependency version requirements install_requires = [ - 'acme>=0.31.0.dev0', - 'certbot>=0.31.0.dev0', - 'dns-lexicon>=2.1.22', - 'mock', - 'setuptools', - 'zope.interface', + 'dns-lexicon>=3.14.1', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-gehirn', version=version, - description="Gehirn Infrastracture Service DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-gehirn = certbot_dns_gehirn.dns_gehirn:Authenticator', - ], - }, - test_suite='certbot_dns_gehirn', ) diff --git a/certbot-dns-gehirn/src/certbot_dns_gehirn/__init__.py b/certbot-dns-gehirn/src/certbot_dns_gehirn/__init__.py new file mode 100644 index 00000000000..fd81d071244 --- /dev/null +++ b/certbot-dns-gehirn/src/certbot_dns_gehirn/__init__.py @@ -0,0 +1,92 @@ +""" +The `~certbot_dns_gehirn.dns_gehirn` plugin automates the process of completing +a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and subsequently +removing, TXT records using the Gehirn Infrastructure Service DNS API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +======================================== ===================================== +``--dns-gehirn-credentials`` Gehirn Infrastructure Service + credentials_ INI file. + (Required) +``--dns-gehirn-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 30) +======================================== ===================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing +Gehirn Infrastructure Service DNS API credentials, +obtained from your Gehirn Infrastructure Service +`dashboard `_. + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # Gehirn Infrastructure Service API credentials used by Certbot + dns_gehirn_api_token = 00000000-0000-0000-0000-000000000000 + dns_gehirn_api_secret = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw + +The path to this file can be provided interactively or using the +``--dns-gehirn-credentials`` command-line argument. Certbot records the path +to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + Gehirn Infrastructure Service account. Users who can read this file can use + these credentials to issue arbitrary API calls on your behalf. Users who can + cause Certbot to run using these credentials can complete a ``dns-01`` + challenge to acquire new certificates or revoke existing certificates for + associated domains, even if those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-gehirn \\ + --dns-gehirn-credentials ~/.secrets/certbot/gehirn.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-gehirn \\ + --dns-gehirn-credentials ~/.secrets/certbot/gehirn.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 60 seconds + for DNS propagation + + certbot certonly \\ + --dns-gehirn \\ + --dns-gehirn-credentials ~/.secrets/certbot/gehirn.ini \\ + --dns-gehirn-propagation-seconds 60 \\ + -d example.com + +""" diff --git a/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/__init__.py b/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/__init__.py new file mode 100644 index 00000000000..f8d6485dc58 --- /dev/null +++ b/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_gehirn.dns_gehirn` plugin.""" diff --git a/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/dns_gehirn.py b/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/dns_gehirn.py new file mode 100644 index 00000000000..8fd5ecd52e7 --- /dev/null +++ b/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/dns_gehirn.py @@ -0,0 +1,55 @@ +"""DNS Authenticator for Gehirn Infrastructure Service DNS.""" +import logging +from typing import Any +from typing import Callable +from typing import Optional + +from requests import HTTPError + +from certbot import errors +from certbot.plugins import dns_common_lexicon + +logger = logging.getLogger(__name__) + +DASHBOARD_URL = "https://gis.gehirn.jp/" + + +class Authenticator(dns_common_lexicon.LexiconDNSAuthenticator): + """DNS Authenticator for Gehirn Infrastructure Service DNS + + This Authenticator uses the Gehirn Infrastructure Service API to fulfill + a dns-01 challenge. + """ + + description = 'Obtain certificates using a DNS TXT record ' + \ + '(if you are using Gehirn Infrastructure Service for DNS).' + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._add_provider_option('api-token', + 'API token for Gehirn Infrastructure Service ' + f'API obtained from {DASHBOARD_URL}', + 'auth_token') + self._add_provider_option('api-secret', + 'API secret for Gehirn Infrastructure Service ' + f'API obtained from {DASHBOARD_URL}', + 'auth_secret') + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 30) -> None: + super().add_parser_arguments(add, default_propagation_seconds) + add('credentials', help='Gehirn Infrastructure Service credentials file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the Gehirn Infrastructure Service API.' + + @property + def _provider_name(self) -> str: + return 'gehirn' + + def _handle_http_error(self, e: HTTPError, domain_name: str) -> Optional[errors.PluginError]: + if domain_name in str(e) and (str(e).startswith('404 Client Error: Not Found for url:')): + return None # Expected errors when zone name guess is wrong + return super()._handle_http_error(e, domain_name) diff --git a/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/tests/__init__.py b/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/tests/__init__.py new file mode 100644 index 00000000000..ef31d57a387 --- /dev/null +++ b/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-gehirn tests""" diff --git a/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/tests/dns_gehirn_test.py b/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/tests/dns_gehirn_test.py new file mode 100644 index 00000000000..331059a6098 --- /dev/null +++ b/certbot-dns-gehirn/src/certbot_dns_gehirn/_internal/tests/dns_gehirn_test.py @@ -0,0 +1,44 @@ +"""Tests for certbot_dns_gehirn._internal.dns_gehirn.""" + +import sys +from unittest import mock + +import pytest +from requests import Response +from requests.exceptions import HTTPError + +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins import dns_test_common_lexicon +from certbot.plugins.dns_test_common import DOMAIN +from certbot.tests import util as test_util + +API_TOKEN = '00000000-0000-0000-0000-000000000000' +API_SECRET = 'MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw' + + +class AuthenticatorTest(test_util.TempDirTestCase, + dns_test_common_lexicon.BaseLexiconDNSAuthenticatorTest): + + DOMAIN_NOT_FOUND = HTTPError(f'404 Client Error: Not Found for url: {DOMAIN}.', response=Response()) + LOGIN_ERROR = HTTPError(f'401 Client Error: Unauthorized for url: {DOMAIN}.', response=Response()) + + def setUp(self): + super().setUp() + + from certbot_dns_gehirn._internal.dns_gehirn import Authenticator + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write( + {"gehirn_api_token": API_TOKEN, "gehirn_api_secret": API_SECRET}, + path + ) + + self.config = mock.MagicMock(gehirn_credentials=path, + gehirn_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "gehirn") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-gehirn/src/certbot_dns_gehirn/py.typed b/certbot-dns-gehirn/src/certbot_dns_gehirn/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-google/.readthedocs.yaml b/certbot-dns-google/.readthedocs.yaml new file mode 100644 index 00000000000..9a36ea0e447 --- /dev/null +++ b/certbot-dns-google/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-google[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-google/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-google/Dockerfile b/certbot-dns-google/Dockerfile deleted file mode 100644 index 4a258d0ee1e..00000000000 --- a/certbot-dns-google/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-google - -RUN pip install --no-cache-dir --editable src/certbot-dns-google diff --git a/certbot-dns-google/MANIFEST.in b/certbot-dns-google/MANIFEST.in index c91330e3840..00d2f2f047e 100644 --- a/certbot-dns-google/MANIFEST.in +++ b/certbot-dns-google/MANIFEST.in @@ -1,4 +1,7 @@ include LICENSE.txt include README.rst recursive-include docs * -recursive-include certbot_dns_google/testdata * +recursive-include src/certbot_dns_google/_internal/tests/testdata * +include src/certbot_dns_google/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-google/certbot_dns_google/__init__.py b/certbot-dns-google/certbot_dns_google/__init__.py deleted file mode 100644 index b88260b0782..00000000000 --- a/certbot-dns-google/certbot_dns_google/__init__.py +++ /dev/null @@ -1,105 +0,0 @@ -""" -The `~certbot_dns_google.dns_google` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the Google Cloud DNS API. - - -Named Arguments ---------------- - -======================================== ===================================== -``--dns-google-credentials`` Google Cloud Platform credentials_ - JSON file. - (Required - Optional on Google Compute Engine) -``--dns-google-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 60) -======================================== ===================================== - - -Credentials ------------ - -Use of this plugin requires Google Cloud Platform API credentials -for an account with the following permissions: - -* ``dns.changes.create`` -* ``dns.changes.get`` -* ``dns.managedZones.list`` -* ``dns.resourceRecordSets.create`` -* ``dns.resourceRecordSets.delete`` -* ``dns.resourceRecordSets.list`` -* ``dns.resourceRecordSets.update`` - -Google provides instructions for `creating a service account `_ and -`information about the required permissions `_. If you're running on Google Compute Engine, -you can `assign the service account to the instance `_ which -is running certbot. A credentials file is not required in this case, as they -are automatically obtained by certbot through the `metadata service -`_ . - -.. code-block:: json - :name: credentials.json - :caption: Example credentials file: - - { - "type": "service_account", - ... - } - -The path to this file can be provided interactively or using the -``--dns-google-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would a password. Users who - can read this file can use these credentials to issue some types of API calls - on your behalf, limited by the permissions assigned to the account. Users who - can cause Certbot to run using these credentials can complete a ``dns-01`` - challenge to acquire new certificates or revoke existing certificates for - domains these credentials are authorized to manage. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-google \\ - --dns-google-credentials ~/.secrets/certbot/google.json \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-google \\ - --dns-google-credentials ~/.secrets/certbot/google.json \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 120 seconds - for DNS propagation - - certbot certonly \\ - --dns-google \\ - --dns-google-credentials ~/.secrets/certbot/google.json \\ - --dns-google-propagation-seconds 120 \\ - -d example.com - -""" diff --git a/certbot-dns-google/certbot_dns_google/dns_google.py b/certbot-dns-google/certbot_dns_google/dns_google.py deleted file mode 100644 index 0b84dddb06c..00000000000 --- a/certbot-dns-google/certbot_dns_google/dns_google.py +++ /dev/null @@ -1,307 +0,0 @@ -"""DNS Authenticator for Google Cloud DNS.""" -import json -import logging - -import httplib2 -import zope.interface -from googleapiclient import discovery -from googleapiclient import errors as googleapiclient_errors -from oauth2client.service_account import ServiceAccountCredentials - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common - -logger = logging.getLogger(__name__) - -ACCT_URL = 'https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount' -PERMISSIONS_URL = 'https://cloud.google.com/dns/access-control#permissions_and_roles' -METADATA_URL = 'http://metadata.google.internal/computeMetadata/v1/' -METADATA_HEADERS = {'Metadata-Flavor': 'Google'} - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for Google Cloud DNS - - This Authenticator uses the Google Cloud DNS API to fulfill a dns-01 challenge. - """ - - description = ('Obtain certificates using a DNS TXT record (if you are using Google Cloud DNS ' - 'for DNS).') - ttl = 60 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=60) - add('credentials', - help=('Path to Google Cloud DNS service account JSON file. (See {0} for' + - 'information about creating a service account and {1} for information about the' + - 'required permissions.)').format(ACCT_URL, PERMISSIONS_URL), - default=None) - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the Google Cloud DNS API.' - - def _setup_credentials(self): - if self.conf('credentials') is None: - try: - # use project_id query to check for availability of google metadata server - # we won't use the result but know we're not on GCP when an exception is thrown - _GoogleClient.get_project_id() - except (ValueError, httplib2.ServerNotFoundError): - raise errors.PluginError('Unable to get Google Cloud Metadata and no credentials' - ' specified. Automatic credential lookup is only ' - 'available on Google Cloud Platform. Please configure' - ' credentials using --dns-google-credentials ') - else: - self._configure_file('credentials', - 'path to Google Cloud DNS service account JSON file') - - dns_common.validate_file_permissions(self.conf('credentials')) - - def _perform(self, domain, validation_name, validation): - self._get_google_client().add_txt_record(domain, validation_name, validation, self.ttl) - - def _cleanup(self, domain, validation_name, validation): - self._get_google_client().del_txt_record(domain, validation_name, validation, self.ttl) - - def _get_google_client(self): - return _GoogleClient(self.conf('credentials')) - - -class _GoogleClient(object): - """ - Encapsulates all communication with the Google Cloud DNS API. - """ - - def __init__(self, account_json=None, dns_api=None): - - scopes = ['https://www.googleapis.com/auth/ndev.clouddns.readwrite'] - if account_json is not None: - credentials = ServiceAccountCredentials.from_json_keyfile_name(account_json, scopes) - with open(account_json) as account: - self.project_id = json.load(account)['project_id'] - else: - credentials = None - self.project_id = self.get_project_id() - - if not dns_api: - self.dns = discovery.build('dns', 'v1', - credentials=credentials, - cache_discovery=False) - else: - self.dns = dns_api - - def add_txt_record(self, domain, record_name, record_content, record_ttl): - """ - Add a TXT record using the supplied information. - - :param str domain: The domain to use to look up the managed zone. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - :param int record_ttl: The record TTL (number of seconds that the record may be cached). - :raises certbot.errors.PluginError: if an error occurs communicating with the Google API - """ - - zone_id = self._find_managed_zone_id(domain) - - record_contents = self.get_existing_txt_rrset(zone_id, record_name) - if record_contents is None: - record_contents = [] - add_records = record_contents[:] - - if "\""+record_content+"\"" in record_contents: - # The process was interrupted previously and validation token exists - return - - add_records.append(record_content) - - data = { - "kind": "dns#change", - "additions": [ - { - "kind": "dns#resourceRecordSet", - "type": "TXT", - "name": record_name + ".", - "rrdatas": add_records, - "ttl": record_ttl, - }, - ], - } - - if record_contents: - # We need to remove old records in the same request - data["deletions"] = [ - { - "kind": "dns#resourceRecordSet", - "type": "TXT", - "name": record_name + ".", - "rrdatas": record_contents, - "ttl": record_ttl, - }, - ] - - changes = self.dns.changes() # changes | pylint: disable=no-member - - try: - request = changes.create(project=self.project_id, managedZone=zone_id, body=data) - response = request.execute() - - status = response['status'] - change = response['id'] - while status == 'pending': - request = changes.get(project=self.project_id, managedZone=zone_id, changeId=change) - response = request.execute() - status = response['status'] - except googleapiclient_errors.Error as e: - logger.error('Encountered error adding TXT record: %s', e) - raise errors.PluginError('Error communicating with the Google Cloud DNS API: {0}' - .format(e)) - - def del_txt_record(self, domain, record_name, record_content, record_ttl): - """ - Delete a TXT record using the supplied information. - - :param str domain: The domain to use to look up the managed zone. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - :param int record_ttl: The record TTL (number of seconds that the record may be cached). - :raises certbot.errors.PluginError: if an error occurs communicating with the Google API - """ - - try: - zone_id = self._find_managed_zone_id(domain) - except errors.PluginError as e: - logger.warning('Error finding zone. Skipping cleanup.') - return - - record_contents = self.get_existing_txt_rrset(zone_id, record_name) - if record_contents is None: - record_contents = ["\"" + record_content + "\""] - - data = { - "kind": "dns#change", - "deletions": [ - { - "kind": "dns#resourceRecordSet", - "type": "TXT", - "name": record_name + ".", - "rrdatas": record_contents, - "ttl": record_ttl, - }, - ], - } - - # Remove the record being deleted from the list - readd_contents = [r for r in record_contents if r != "\"" + record_content + "\""] - if readd_contents: - # We need to remove old records in the same request - data["additions"] = [ - { - "kind": "dns#resourceRecordSet", - "type": "TXT", - "name": record_name + ".", - "rrdatas": readd_contents, - "ttl": record_ttl, - }, - ] - - changes = self.dns.changes() # changes | pylint: disable=no-member - - try: - request = changes.create(project=self.project_id, managedZone=zone_id, body=data) - request.execute() - except googleapiclient_errors.Error as e: - logger.warning('Encountered error deleting TXT record: %s', e) - - def get_existing_txt_rrset(self, zone_id, record_name): - """ - Get existing TXT records from the RRset for the record name. - - If an error occurs while requesting the record set, it is suppressed - and None is returned. - - :param str zone_id: The ID of the managed zone. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - - :returns: List of TXT record values or None - :rtype: `list` of `string` or `None` - - """ - rrs_request = self.dns.resourceRecordSets() # pylint: disable=no-member - request = rrs_request.list(managedZone=zone_id, project=self.project_id) - # Add dot as the API returns absolute domains - record_name += "." - try: - response = request.execute() - except googleapiclient_errors.Error: - logger.info("Unable to list existing records. If you're " - "requesting a wildcard certificate, this might not work.") - logger.debug("Error was:", exc_info=True) - else: - if response: - for rr in response["rrsets"]: - if rr["name"] == record_name and rr["type"] == "TXT": - return rr["rrdatas"] - return None - - def _find_managed_zone_id(self, domain): - """ - Find the managed zone for a given domain. - - :param str domain: The domain for which to find the managed zone. - :returns: The ID of the managed zone, if found. - :rtype: str - :raises certbot.errors.PluginError: if the managed zone cannot be found. - """ - - zone_dns_name_guesses = dns_common.base_domain_name_guesses(domain) - - mz = self.dns.managedZones() # managedZones | pylint: disable=no-member - for zone_name in zone_dns_name_guesses: - try: - request = mz.list(project=self.project_id, dnsName=zone_name + '.') - response = request.execute() - zones = response['managedZones'] - except googleapiclient_errors.Error as e: - raise errors.PluginError('Encountered error finding managed zone: {0}' - .format(e)) - - if len(zones) > 0: - zone_id = zones[0]['id'] - logger.debug('Found id of %s for %s using name %s', zone_id, domain, zone_name) - return zone_id - - raise errors.PluginError('Unable to determine managed zone for {0} using zone names: {1}.' - .format(domain, zone_dns_name_guesses)) - - @staticmethod - def get_project_id(): - """ - Query the google metadata service for the current project ID - - This only works on Google Cloud Platform - - :raises ServerNotFoundError: Not running on Google Compute or DNS not available - :raises ValueError: Server is found, but response code is not 200 - :returns: project id - """ - url = '{0}project/project-id'.format(METADATA_URL) - - # Request an access token from the metadata server. - http = httplib2.Http() - r, content = http.request(url, headers=METADATA_HEADERS) - if r.status != 200: - raise ValueError("Invalid status code: {0}".format(r)) - - if isinstance(content, bytes): - return content.decode() - else: - return content diff --git a/certbot-dns-google/certbot_dns_google/dns_google_test.py b/certbot-dns-google/certbot_dns_google/dns_google_test.py deleted file mode 100644 index 2b081885b04..00000000000 --- a/certbot-dns-google/certbot_dns_google/dns_google_test.py +++ /dev/null @@ -1,330 +0,0 @@ -"""Tests for certbot_dns_google.dns_google.""" - -import os -import unittest - -import mock -from googleapiclient import discovery -from googleapiclient.errors import Error -from googleapiclient.http import HttpMock -from httplib2 import ServerNotFoundError - -from certbot import errors -from certbot.errors import PluginError -from certbot.plugins import dns_test_common -from certbot.plugins.dns_test_common import DOMAIN -from certbot.tests import util as test_util - -ACCOUNT_JSON_PATH = '/not/a/real/path.json' -API_ERROR = Error() -PROJECT_ID = "test-test-1" - - -class AuthenticatorTest(test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_google.dns_google import Authenticator - - path = os.path.join(self.tempdir, 'file.json') - open(path, "wb").close() - - super(AuthenticatorTest, self).setUp() - self.config = mock.MagicMock(google_credentials=path, - google_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "google") - - self.mock_client = mock.MagicMock() - # _get_google_client | pylint: disable=protected-access - self.auth._get_google_client = mock.MagicMock(return_value=self.mock_client) - - def test_perform(self): - self.auth.perform([self.achall]) - - expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - def test_cleanup(self): - # _attempt_cleanup | pylint: disable=protected-access - self.auth._attempt_cleanup = True - self.auth.cleanup([self.achall]) - - expected = [mock.call.del_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - @mock.patch('httplib2.Http.request', side_effect=ServerNotFoundError) - def test_without_auth(self, unused_mock): - self.config.google_credentials = None - self.assertRaises(PluginError, self.auth.perform, [self.achall]) - - -class GoogleClientTest(unittest.TestCase): - record_name = "foo" - record_content = "bar" - record_ttl = 42 - zone = "ZONE_ID" - change = "an-id" - - def _setUp_client_with_mock(self, zone_request_side_effect): - from certbot_dns_google.dns_google import _GoogleClient - - pwd = os.path.dirname(__file__) - rel_path = 'testdata/discovery.json' - discovery_file = os.path.join(pwd, rel_path) - http_mock = HttpMock(discovery_file, {'status': '200'}) - dns_api = discovery.build('dns', 'v1', http=http_mock) - - client = _GoogleClient(ACCOUNT_JSON_PATH, dns_api) - - # Setup - mock_mz = mock.MagicMock() - mock_mz.list.return_value.execute.side_effect = zone_request_side_effect - - mock_rrs = mock.MagicMock() - rrsets = {"rrsets": [{"name": "_acme-challenge.example.org.", "type": "TXT", - "rrdatas": ["\"example-txt-contents\""]}]} - mock_rrs.list.return_value.execute.return_value = rrsets - mock_changes = mock.MagicMock() - - client.dns.managedZones = mock.MagicMock(return_value=mock_mz) - client.dns.changes = mock.MagicMock(return_value=mock_changes) - client.dns.resourceRecordSets = mock.MagicMock(return_value=mock_rrs) - - return client, mock_changes - - @mock.patch('googleapiclient.discovery.build') - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google._GoogleClient.get_project_id') - def test_client_without_credentials(self, get_project_id_mock, credential_mock, - unused_discovery_mock): - from certbot_dns_google.dns_google import _GoogleClient - _GoogleClient(None) - self.assertFalse(credential_mock.called) - self.assertTrue(get_project_id_mock.called) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - @mock.patch('certbot_dns_google.dns_google._GoogleClient.get_project_id') - def test_add_txt_record(self, get_project_id_mock, credential_mock): - client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone}]}]) - credential_mock.assert_called_once_with('/not/a/real/path.json', mock.ANY) - self.assertFalse(get_project_id_mock.called) - - client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) - - expected_body = { - "kind": "dns#change", - "additions": [ - { - "kind": "dns#resourceRecordSet", - "type": "TXT", - "name": self.record_name + ".", - "rrdatas": [self.record_content, ], - "ttl": self.record_ttl, - }, - ], - } - - changes.create.assert_called_with(body=expected_body, - managedZone=self.zone, - project=PROJECT_ID) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_add_txt_record_and_poll(self, unused_credential_mock): - client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone}]}]) - changes.create.return_value.execute.return_value = {'status': 'pending', 'id': self.change} - changes.get.return_value.execute.return_value = {'status': 'done'} - - client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) - - changes.create.assert_called_with(body=mock.ANY, - managedZone=self.zone, - project=PROJECT_ID) - - changes.get.assert_called_with(changeId=self.change, - managedZone=self.zone, - project=PROJECT_ID) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_add_txt_record_delete_old(self, unused_credential_mock): - client, changes = self._setUp_client_with_mock( - [{'managedZones': [{'id': self.zone}]}]) - mock_get_rrs = "certbot_dns_google.dns_google._GoogleClient.get_existing_txt_rrset" - with mock.patch(mock_get_rrs) as mock_rrs: - mock_rrs.return_value = ["sample-txt-contents"] - client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) - self.assertTrue(changes.create.called) - self.assertTrue("sample-txt-contents" in - changes.create.call_args_list[0][1]["body"]["deletions"][0]["rrdatas"]) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_add_txt_record_noop(self, unused_credential_mock): - client, changes = self._setUp_client_with_mock( - [{'managedZones': [{'id': self.zone}]}]) - client.add_txt_record(DOMAIN, "_acme-challenge.example.org", - "example-txt-contents", self.record_ttl) - self.assertFalse(changes.create.called) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_add_txt_record_error_during_zone_lookup(self, unused_credential_mock): - client, unused_changes = self._setUp_client_with_mock(API_ERROR) - - self.assertRaises(errors.PluginError, client.add_txt_record, - DOMAIN, self.record_name, self.record_content, self.record_ttl) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_add_txt_record_zone_not_found(self, unused_credential_mock): - client, unused_changes = self._setUp_client_with_mock([{'managedZones': []}, - {'managedZones': []}]) - - self.assertRaises(errors.PluginError, client.add_txt_record, - DOMAIN, self.record_name, self.record_content, self.record_ttl) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_add_txt_record_error_during_add(self, unused_credential_mock): - client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone}]}]) - changes.create.side_effect = API_ERROR - - self.assertRaises(errors.PluginError, client.add_txt_record, - DOMAIN, self.record_name, self.record_content, self.record_ttl) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_del_txt_record(self, unused_credential_mock): - client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone}]}]) - - mock_get_rrs = "certbot_dns_google.dns_google._GoogleClient.get_existing_txt_rrset" - with mock.patch(mock_get_rrs) as mock_rrs: - mock_rrs.return_value = ["\"sample-txt-contents\"", - "\"example-txt-contents\""] - client.del_txt_record(DOMAIN, "_acme-challenge.example.org", - "example-txt-contents", self.record_ttl) - - expected_body = { - "kind": "dns#change", - "deletions": [ - { - "kind": "dns#resourceRecordSet", - "type": "TXT", - "name": "_acme-challenge.example.org.", - "rrdatas": ["\"sample-txt-contents\"", "\"example-txt-contents\""], - "ttl": self.record_ttl, - }, - ], - "additions": [ - { - "kind": "dns#resourceRecordSet", - "type": "TXT", - "name": "_acme-challenge.example.org.", - "rrdatas": ["\"sample-txt-contents\"", ], - "ttl": self.record_ttl, - }, - ], - } - - changes.create.assert_called_with(body=expected_body, - managedZone=self.zone, - project=PROJECT_ID) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_del_txt_record_error_during_zone_lookup(self, unused_credential_mock): - client, unused_changes = self._setUp_client_with_mock(API_ERROR) - - client.del_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_del_txt_record_zone_not_found(self, unused_credential_mock): - client, unused_changes = self._setUp_client_with_mock([{'managedZones': []}, - {'managedZones': []}]) - - client.del_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_del_txt_record_error_during_delete(self, unused_credential_mock): - client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone}]}]) - changes.create.side_effect = API_ERROR - - client.del_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_get_existing(self, unused_credential_mock): - client, unused_changes = self._setUp_client_with_mock( - [{'managedZones': [{'id': self.zone}]}]) - # Record name mocked in setUp - found = client.get_existing_txt_rrset(self.zone, "_acme-challenge.example.org") - self.assertEqual(found, ["\"example-txt-contents\""]) - not_found = client.get_existing_txt_rrset(self.zone, "nonexistent.tld") - self.assertEqual(not_found, None) - - @mock.patch('oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_name') - @mock.patch('certbot_dns_google.dns_google.open', - mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) - def test_get_existing_fallback(self, unused_credential_mock): - client, unused_changes = self._setUp_client_with_mock( - [{'managedZones': [{'id': self.zone}]}]) - # pylint: disable=no-member - mock_execute = client.dns.resourceRecordSets.return_value.list.return_value.execute - mock_execute.side_effect = API_ERROR - - rrset = client.get_existing_txt_rrset(self.zone, "_acme-challenge.example.org") - self.assertFalse(rrset) - - def test_get_project_id(self): - from certbot_dns_google.dns_google import _GoogleClient - - response = DummyResponse() - response.status = 200 - - with mock.patch('httplib2.Http.request', return_value=(response, 'test-test-1')): - project_id = _GoogleClient.get_project_id() - self.assertEqual(project_id, 'test-test-1') - - with mock.patch('httplib2.Http.request', return_value=(response, b'test-test-1')): - project_id = _GoogleClient.get_project_id() - self.assertEqual(project_id, 'test-test-1') - - failed_response = DummyResponse() - failed_response.status = 404 - - with mock.patch('httplib2.Http.request', - return_value=(failed_response, "some detailed http error response")): - self.assertRaises(ValueError, _GoogleClient.get_project_id) - - with mock.patch('httplib2.Http.request', side_effect=ServerNotFoundError): - self.assertRaises(ServerNotFoundError, _GoogleClient.get_project_id) - - -class DummyResponse(object): - """ - Dummy object to create a fake HTTPResponse (the actual one requires a socket and we only - need the status attribute) - """ - def __init__(self): - self.status = 200 - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-google/docs/_ext/jsonlexer.py b/certbot-dns-google/docs/_ext/jsonlexer.py deleted file mode 100644 index 1ad004d2b20..00000000000 --- a/certbot-dns-google/docs/_ext/jsonlexer.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Copied from https://stackoverflow.com/a/16863232""" - -def setup(app): - # enable Pygments json lexer - try: - import pygments - if pygments.__version__ >= '1.5': - # use JSON lexer included in recent versions of Pygments - from pygments.lexers import JsonLexer - else: - # use JSON lexer from pygments-json if installed - from pygson.json_lexer import JSONLexer as JsonLexer - except ImportError: - pass # not fatal if we have old (or no) Pygments and no pygments-json - else: - app.add_lexer('json', JsonLexer()) diff --git a/certbot-dns-google/docs/api.rst b/certbot-dns-google/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-google/docs/api.rst +++ b/certbot-dns-google/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-google/docs/api/dns_google.rst b/certbot-dns-google/docs/api/dns_google.rst deleted file mode 100644 index 6f54599346a..00000000000 --- a/certbot-dns-google/docs/api/dns_google.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_google.dns_google` ------------------------------------- - -.. automodule:: certbot_dns_google.dns_google - :members: diff --git a/certbot-dns-google/docs/conf.py b/certbot-dns-google/docs/conf.py index bbb343ee8a4..0a1ccb0b5c5 100644 --- a/certbot-dns-google/docs/conf.py +++ b/certbot-dns-google/docs/conf.py @@ -18,6 +18,7 @@ # import os import sys + sys.path.insert(0, os.path.abspath('_ext')) @@ -35,10 +36,10 @@ 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.viewcode', - 'jsonlexer'] + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -84,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -93,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -111,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -176,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-google/docs/make.bat b/certbot-dns-google/docs/make.bat index 181c1269920..036df54997e 100644 --- a/certbot-dns-google/docs/make.bat +++ b/certbot-dns-google/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-google/local-oldest-requirements.txt b/certbot-dns-google/local-oldest-requirements.txt deleted file mode 100644 index 8368d266ee7..00000000000 --- a/certbot-dns-google/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -acme[dev]==0.21.1 -certbot[dev]==0.21.1 diff --git a/certbot-dns-google/pyproject.toml b/certbot-dns-google/pyproject.toml new file mode 100644 index 00000000000..09cc5397d9f --- /dev/null +++ b/certbot-dns-google/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-google" +dynamic = ["version", "dependencies"] +description = "Google Cloud DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-google = "certbot_dns_google._internal.dns_google:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-google/readthedocs.org.requirements.txt b/certbot-dns-google/readthedocs.org.requirements.txt deleted file mode 100644 index 6ea393f866f..00000000000 --- a/certbot-dns-google/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-google[docs] diff --git a/certbot-dns-google/setup.cfg b/certbot-dns-google/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-google/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-google/setup.py b/certbot-dns-google/setup.py index c99ad38aa79..c20f690725f 100644 --- a/certbot-dns-google/setup.py +++ b/certbot-dns-google/setup.py @@ -1,71 +1,26 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.21.1', - 'certbot>=0.21.1', - # 1.5 is the first version that supports oauth2client>=2.0 - 'google-api-python-client>=1.5', - 'mock', - # for oauth2client.service_account.ServiceAccountCredentials - 'oauth2client>=2.0', - 'setuptools', - 'zope.interface', - # already a dependency of google-api-python-client, but added for consistency - 'httplib2' + 'google-api-python-client>=1.6.5', + 'google-auth>=2.16.0', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-google', version=version, - description="Google Cloud DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-google = certbot_dns_google.dns_google:Authenticator', - ], - }, - test_suite='certbot_dns_google', ) diff --git a/certbot-dns-google/src/certbot_dns_google/__init__.py b/certbot-dns-google/src/certbot_dns_google/__init__.py new file mode 100644 index 00000000000..79830fd5197 --- /dev/null +++ b/certbot-dns-google/src/certbot_dns_google/__init__.py @@ -0,0 +1,224 @@ +""" +The `~certbot_dns_google.dns_google` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using the Google Cloud DNS API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +======================================== ===================================== +``--dns-google-credentials`` Google Cloud Platform credentials_ + JSON file. + + (Required if not using `Application Default + Credentials `_.) +``--dns-google-project`` The ID of the Google Cloud project that the Google + Cloud DNS managed zone(s) reside in. + + (Default: project that the Google credentials_ belong to) +``--dns-google-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 60) +======================================== ===================================== + + +Credentials +----------- + +Use of this plugin requires Google Cloud Platform credentials with the ability to modify the Cloud +DNS managed zone(s) for which certificates are being issued. + +In most cases, configuring credentials for Certbot will require `creating a service account +`_, and then either `granting permissions +with predefined roles`_ or `granting permissions with custom roles`_ using IAM. + + +Providing Credentials +^^^^^^^^^^^^^^^^^^^^^ + +The preferred method of providing credentials is to `set up Application Default Credentials +`_ (ADC) in the environment +that Certbot is running in. + +If you are running Certbot on Google Cloud then a service account can be assigned directly to most +types of workload, including `Compute Engine VMs `_, `Kubernetes Engine Pods `_, `Cloud Run jobs `_, `Cloud Functions `_, and `Cloud Builds `_. + +If you are not running Certbot on Google Cloud then a credentials file should be provided using the +``--dns-google-credentials`` command-line argument. Google provides documentation for `creating +service account keys `_, which is the +most common method of using a service account outside of Google Cloud. + +.. code-block:: json + :name: credentials-sa.json + :caption: Example service account key file: + + { + "type": "service_account", + "project_id": "...", + "private_key_id": "...", + "private_key": "...", + "client_email": "...", + "client_id": "...", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://accounts.google.com/o/oauth2/token", + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", + "client_x509_cert_url": "..." + } + +.. caution:: + You should protect these credentials as you would a password. Users who + can read this file can use these credentials to issue some types of API calls + on your behalf, limited by the permissions assigned to the account. Users who + can cause Certbot to run using these credentials can complete a ``dns-01`` + challenge to acquire new certificates or revoke existing certificates for + domains these credentials are authorized to manage. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + +If you are running Certbot within another cloud platform, a CI platform, or any other platform that +supports issuing OpenID Connect Tokens, then you may also have the option of securely authenticating +with `workload identity federation `_. Instructions are generally available for most platforms, including `AWS or Azure +`_, `GitHub +Actions `_, and `GitLab CI `_. + + +Granting Permissions with Predefined Roles +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The simplest method of granting the required permissions to the user or service account that Certbot +is authenticating with is to use either of these predefined role strategies: + +* `dns.admin `_ against the *DNS + zone(s)* that Certbot will be issuing certificates for. +* `dns.reader `_ against the *project* + containing the relevant DNS zones. + +*or* + +* `dns.admin `_ against the *project* + containing the relevant DNS zones + +For instructions on how to grant roles, please read the Google provided documentation for `granting +access roles against a project `_ and `granting access roles against zones `_. + +.. caution:: + Granting the ``dns.admin`` role at the project level can present a significant security risk. It + provides full administrative access to all DNS zones within the project, granting the ability to + perform any action up to and including deleting all zones within a project. + + +Granting Permissions with Custom Roles +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Custom roles are an alternative to predefined roles that provide the ability to define fine grained +permission sets for specific use cases. They should generally be used when it is desirable to adhere +to the principle of least privilege, such as within production or other security sensitive +workloads. + +The following is an example strategy for granting granular permissions to Certbot using custom +roles. If you are not already familiar with how to do so, Google provides documentation for +`creating a custom IAM role `_. + +Firstly, create a custom role containing the permissions required to make DNS record updates. We +suggest naming the custom role ``Certbot - Zone Editor`` with the ID ``certbot.zoneEditor``. The +following permissions are required: + +* ``dns.changes.create`` +* ``dns.changes.get`` +* ``dns.changes.list`` +* ``dns.resourceRecordSets.create`` +* ``dns.resourceRecordSets.delete`` +* ``dns.resourceRecordSets.list`` +* ``dns.resourceRecordSets.update`` + +Next, create a custom role granting Certbot the ability to discover DNS zones. We suggest naming the +custom role ``Certbot - Zone Lister`` with the ID ``certbot.zoneLister``. The following permissions +are required: + +* ``dns.managedZones.get`` +* ``dns.managedZones.list`` + +Finally, grant the custom roles to the user or service account that Certbot is authenticating with: + +* Grant your custom ``Certbot - Zone Editor`` role against the *DNS zone(s)* that Certbot will be + issuing certificates for. +* Grant your custom ``Certbot - Zone Lister`` role against the *project* containing the relevant DNS + zones. + +For instructions on how to grant roles, please read the Google provided documentation for `granting +access roles against a project `_ and `granting access roles against zones `_. + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, providing a credentials file + + certbot certonly \\ + --dns-google \\ + --dns-google-credentials ~/.secrets/certbot/google.json \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, where ADC is available and + a credentials file is not required + + certbot certonly \\ + --dns-google \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-google \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, where the managed DNS + zone resides in another Google Cloud project. + + certbot certonly \\ + --dns-google \\ + --dns-google-credentials ~/.secrets/certbot/google-project-test-foo.json \\ + --dns-google-project test-bar \\ + -d example.com + + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 120 seconds + for DNS propagation + + certbot certonly \\ + --dns-google \\ + --dns-google-propagation-seconds 120 \\ + -d example.com + +""" diff --git a/certbot-dns-google/src/certbot_dns_google/_internal/__init__.py b/certbot-dns-google/src/certbot_dns_google/_internal/__init__.py new file mode 100644 index 00000000000..f433213ff99 --- /dev/null +++ b/certbot-dns-google/src/certbot_dns_google/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_google.dns_google` plugin.""" diff --git a/certbot-dns-google/src/certbot_dns_google/_internal/dns_google.py b/certbot-dns-google/src/certbot_dns_google/_internal/dns_google.py new file mode 100644 index 00000000000..94502111787 --- /dev/null +++ b/certbot-dns-google/src/certbot_dns_google/_internal/dns_google.py @@ -0,0 +1,314 @@ +"""DNS Authenticator for Google Cloud DNS.""" +import logging +from typing import Any +from typing import Callable +from typing import Optional +from typing import cast + +import google.auth + +from google.auth import exceptions as googleauth_exceptions +from googleapiclient import discovery +from googleapiclient import errors as googleapiclient_errors + +from certbot import errors +from certbot.plugins import dns_common + +logger = logging.getLogger(__name__) + +ADC_URL = 'https://cloud.google.com/docs/authentication/application-default-credentials' +ACCT_URL = 'https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount' +PERMISSIONS_URL = 'https://cloud.google.com/dns/access-control#permissions_and_roles' +METADATA_URL = 'http://metadata.google.internal/computeMetadata/v1/' +METADATA_HEADERS = {'Metadata-Flavor': 'Google'} + + +class Authenticator(dns_common.DNSAuthenticator): + """DNS Authenticator for Google Cloud DNS + + This Authenticator uses the Google Cloud DNS API to fulfill a dns-01 challenge. + """ + + description = ('Obtain certificates using a DNS TXT record (if you are using Google Cloud DNS ' + 'for DNS).') + ttl = 60 + google_client = None + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 60) -> None: + super().add_parser_arguments(add, default_propagation_seconds=60) + add('credentials', + help=('Path to Google Cloud DNS service account JSON file to use instead of relying on' + ' Application Default Credentials (ADC). (See {0} for information about ADC, {1}' + ' for information about creating a service account, and {2} for information about' + ' the permissions required to modify Cloud DNS records.)') + .format(ADC_URL, ACCT_URL, PERMISSIONS_URL), + default=None) + + add('project', + help=('The ID of the Google Cloud project that the Google Cloud DNS managed zone(s)' + + ' reside in. This will be determined automatically if not specified.'), + default=None) + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the Google Cloud DNS API.' + + def _setup_credentials(self) -> None: + if self.conf('credentials') is not None: + self._configure_file('credentials', + 'path to Google Cloud DNS service account JSON file') + + dns_common.validate_file_permissions(self.conf('credentials')) + + try: + self._get_google_client() + except googleauth_exceptions.DefaultCredentialsError as e: + raise errors.PluginError('Authentication using Google Application Default Credentials ' + 'failed ({}). Please configure credentials using' + ' --dns-google-credentials '.format(e)) + + def _perform(self, domain: str, validation_name: str, validation: str) -> None: + self._get_google_client().add_txt_record(domain, validation_name, validation, self.ttl) + + def _cleanup(self, domain: str, validation_name: str, validation: str) -> None: + self._get_google_client().del_txt_record(domain, validation_name, validation, self.ttl) + + def _get_google_client(self) -> '_GoogleClient': + if self.google_client is None: + self.google_client = _GoogleClient(self.conf('credentials'), self.conf('project')) + return self.google_client + + + +class _GoogleClient: + """ + Encapsulates all communication with the Google Cloud DNS API. + """ + + def __init__(self, account_json: Optional[str] = None, + dns_project_id: Optional[str] = None, + dns_api: Optional[discovery.Resource] = None) -> None: + + scopes = ['https://www.googleapis.com/auth/ndev.clouddns.readwrite'] + credentials = None + project_id = None + + if account_json is not None: + try: + credentials, project_id = google.auth.load_credentials_from_file( # type: ignore [no-untyped-call, unused-ignore] # pylint: disable=line-too-long + account_json, scopes=scopes) + except googleauth_exceptions.GoogleAuthError as e: + raise errors.PluginError( + "Error loading credentials file '{}': {}".format(account_json, e)) + else: + credentials, project_id = google.auth.default(scopes=scopes) # type: ignore [no-untyped-call, unused-ignore] # pylint: disable=line-too-long + + if dns_project_id is not None: + project_id = dns_project_id + + if not project_id: + raise errors.PluginError('The Google Cloud project could not be automatically ' + 'determined. Please configure it using --dns-google-project' + ' .') + self.project_id = project_id + + if not dns_api: + self.dns = discovery.build('dns', 'v1', + credentials=credentials, + cache_discovery=False) + else: + self.dns = dns_api + + def add_txt_record(self, domain: str, record_name: str, record_content: str, + record_ttl: int) -> None: + """ + Add a TXT record using the supplied information. + + :param str domain: The domain to use to look up the managed zone. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + :param int record_ttl: The record TTL (number of seconds that the record may be cached). + :raises certbot.errors.PluginError: if an error occurs communicating with the Google API + """ + + zone_id = self._find_managed_zone_id(domain) + + record_contents = self.get_existing_txt_rrset(zone_id, record_name) + if record_contents is None: + # If it wasn't possible to fetch the records at this label (missing .list permission), + # assume there aren't any (#5678). If there are actually records here, this will fail + # with HTTP 409/412 API errors. + record_contents = {"rrdatas": []} + add_records = record_contents["rrdatas"][:] + + if "\""+record_content+"\"" in record_contents["rrdatas"]: + # The process was interrupted previously and validation token exists + return + + add_records.append(record_content) + + data = { + "kind": "dns#change", + "additions": [ + { + "kind": "dns#resourceRecordSet", + "type": "TXT", + "name": record_name + ".", + "rrdatas": add_records, + "ttl": record_ttl, + }, + ], + } + + if record_contents["rrdatas"]: + # We need to remove old records in the same request + data["deletions"] = [ + { + "kind": "dns#resourceRecordSet", + "type": "TXT", + "name": record_name + ".", + "rrdatas": record_contents["rrdatas"], + "ttl": record_contents["ttl"], + }, + ] + + changes = self.dns.changes() + + try: + request = changes.create(project=self.project_id, managedZone=zone_id, body=data) + response = request.execute() + + status = response['status'] + change = response['id'] + while status == 'pending': + request = changes.get(project=self.project_id, managedZone=zone_id, changeId=change) + response = request.execute() + status = response['status'] + except googleapiclient_errors.Error as e: + logger.error('Encountered error adding TXT record: %s', e) + raise errors.PluginError('Error communicating with the Google Cloud DNS API: {0}' + .format(e)) + + def del_txt_record(self, domain: str, record_name: str, record_content: str, + record_ttl: int) -> None: + """ + Delete a TXT record using the supplied information. + + :param str domain: The domain to use to look up the managed zone. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + :param int record_ttl: The record TTL (number of seconds that the record may be cached). + :raises certbot.errors.PluginError: if an error occurs communicating with the Google API + """ + + try: + zone_id = self._find_managed_zone_id(domain) + except errors.PluginError: + logger.warning('Error finding zone. Skipping cleanup.') + return + + record_contents = self.get_existing_txt_rrset(zone_id, record_name) + if record_contents is None: + # If it wasn't possible to fetch the records at this label (missing .list permission), + # assume there aren't any (#5678). If there are actually records here, this will fail + # with HTTP 409/412 API errors. + record_contents = {"rrdatas": ["\"" + record_content + "\""], "ttl": record_ttl} + + data = { + "kind": "dns#change", + "deletions": [ + { + "kind": "dns#resourceRecordSet", + "type": "TXT", + "name": record_name + ".", + "rrdatas": record_contents["rrdatas"], + "ttl": record_contents["ttl"], + }, + ], + } + + # Remove the record being deleted from the list + readd_contents = [r for r in record_contents["rrdatas"] + if r != "\"" + record_content + "\""] + if readd_contents: + # We need to remove old records in the same request + data["additions"] = [ + { + "kind": "dns#resourceRecordSet", + "type": "TXT", + "name": record_name + ".", + "rrdatas": readd_contents, + "ttl": record_contents["ttl"], + }, + ] + + changes = self.dns.changes() + + try: + request = changes.create(project=self.project_id, managedZone=zone_id, body=data) + request.execute() + except googleapiclient_errors.Error as e: + logger.warning('Encountered error deleting TXT record: %s', e) + + def get_existing_txt_rrset(self, zone_id: str, record_name: str) -> Optional[dict[str, Any]]: + """ + Get existing TXT records from the RRset for the record name. + + If an error occurs while requesting the record set, it is suppressed + and None is returned. + + :param str zone_id: The ID of the managed zone. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + + :returns: The resourceRecordSet corresponding to `record_name` or None + :rtype: `resourceRecordSet ` or `None` # pylint: disable=line-too-long + + """ + rrs_request = self.dns.resourceRecordSets() + # Add dot as the API returns absolute domains + record_name += "." + request = rrs_request.list(project=self.project_id, managedZone=zone_id, name=record_name, + type="TXT") + try: + response = request.execute() + except googleapiclient_errors.Error: + logger.info("Unable to list existing records. If you're " + "requesting a wildcard certificate, this might not work.") + logger.debug("Error was:", exc_info=True) + else: + if response and response["rrsets"]: + return cast(dict[str, Any], response["rrsets"][0]) + return None + + def _find_managed_zone_id(self, domain: str) -> str: + """ + Find the managed zone for a given domain. + + :param str domain: The domain for which to find the managed zone. + :returns: The ID of the managed zone, if found. + :rtype: str + :raises certbot.errors.PluginError: if the managed zone cannot be found. + """ + + zone_dns_name_guesses = dns_common.base_domain_name_guesses(domain) + + mz = self.dns.managedZones() + for zone_name in zone_dns_name_guesses: + try: + request = mz.list(project=self.project_id, dnsName=zone_name + '.') + response = request.execute() + zones = response['managedZones'] + except googleapiclient_errors.Error as e: + raise errors.PluginError('Encountered error finding managed zone: {0}' + .format(e)) + + for zone in zones: + zone_id: str = zone['id'] + if zone['visibility'] == "public": + logger.debug('Found id of %s for %s using name %s', zone_id, domain, zone_name) + return zone_id + + raise errors.PluginError('Unable to determine managed zone for {0} using zone names: {1}.' + .format(domain, zone_dns_name_guesses)) diff --git a/certbot-dns-google/src/certbot_dns_google/_internal/tests/__init__.py b/certbot-dns-google/src/certbot_dns_google/_internal/tests/__init__.py new file mode 100644 index 00000000000..06d4a72eb51 --- /dev/null +++ b/certbot-dns-google/src/certbot_dns_google/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-google tests""" diff --git a/certbot-dns-google/src/certbot_dns_google/_internal/tests/dns_google_test.py b/certbot-dns-google/src/certbot_dns_google/_internal/tests/dns_google_test.py new file mode 100644 index 00000000000..e5c6b638057 --- /dev/null +++ b/certbot-dns-google/src/certbot_dns_google/_internal/tests/dns_google_test.py @@ -0,0 +1,499 @@ +"""Tests for certbot_dns_google._internal.dns_google.""" +from __future__ import annotations +import sys +from typing import Optional +import unittest +from unittest import mock + +from google.auth import exceptions as googleauth_exceptions +from googleapiclient import discovery +from googleapiclient.errors import Error +from googleapiclient.http import HttpMock +import pytest + +from certbot import errors +from certbot.compat import os +from certbot.errors import PluginError +from certbot.plugins import dns_test_common +from certbot.plugins.dns_test_common import DOMAIN +from certbot.tests import util as test_util + +ACCOUNT_JSON_PATH = '/not/a/real/path.json' +API_ERROR = Error() +PROJECT_ID = "test-test-1" +SCOPES = ['https://www.googleapis.com/auth/ndev.clouddns.readwrite'] + +class AuthenticatorTest(test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): + + def setUp(self): + super().setUp() + + from certbot_dns_google._internal.dns_google import Authenticator + + path = os.path.join(self.tempdir, 'file.json') + open(path, "wb").close() + + super().setUp() + self.config = mock.MagicMock(google_credentials=path, + google_project=PROJECT_ID, + google_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "google") + + self.mock_client = mock.MagicMock() + + @test_util.patch_display_util() + def test_perform(self, unused_mock_get_utility): + # _get_google_client | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.auth, '_get_google_client', mock.MagicMock(return_value=self.mock_client)) + self.auth.perform([self.achall]) + + expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY, mock.ANY)] + assert expected == self.mock_client.mock_calls + + def test_cleanup(self): + # _get_google_client | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.auth, '_get_google_client', mock.MagicMock(return_value=self.mock_client)) + # _attempt_cleanup | pylint: disable=protected-access + self.auth._attempt_cleanup = True + self.auth.cleanup([self.achall]) + + expected = [mock.call.del_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY, mock.ANY)] + assert expected == self.mock_client.mock_calls + + @test_util.patch_display_util() + def test_without_auth(self, unused_mock_get_utility): + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.auth, '_get_google_client', mock.MagicMock(side_effect=googleauth_exceptions.DefaultCredentialsError)) + self.config.google_credentials = None + with pytest.raises(PluginError): + self.auth.perform([self.achall]) + + @mock.patch('certbot_dns_google._internal.dns_google._GoogleClient') + def test_get_google_client(self, client_mock): + test_client = mock.MagicMock() + client_mock.return_value = test_client + + self.auth._get_google_client() + assert client_mock.called + assert self.auth.google_client is test_client + + def test_get_google_client_cached(self): + test_client = mock.MagicMock() + self.auth.google_client = test_client + assert self.auth._get_google_client() is test_client + + +class GoogleClientTest(unittest.TestCase): + record_name = "foo" + record_content = "bar" + record_ttl = 42 + zone = "ZONE_ID" + change = "an-id" + visibility = "public" + + import certbot_dns_google # should get overwritten later; need for typing + + def _setUp_client_with_mock(self, + zone_request_side_effect: list[dict[str, list[dict[str, str]]]], + rrs_list_side_effect: Optional[Error] = None + ) -> tuple['certbot_dns_google._internal.dns_google._GoogleClient', mock.MagicMock]: + from certbot_dns_google._internal.dns_google import _GoogleClient + + pwd = os.path.dirname(__file__) + rel_path = 'testdata/discovery.json' + discovery_file = os.path.join(pwd, rel_path) + http_mock = HttpMock(discovery_file, {'status': '200'}) + dns_api = discovery.build('dns', 'v1', http=http_mock) + + client = _GoogleClient(ACCOUNT_JSON_PATH, None, dns_api) + + # Setup + mock_mz = mock.MagicMock() + mock_mz.list.return_value.execute.side_effect = zone_request_side_effect + + mock_rrs = mock.MagicMock() + def rrs_list(project=None, managedZone=None, name=None, type=None): + response: dict[str, list[dict[str, str | int | list[str]]]]= {"rrsets": []} + if name == "_acme-challenge.example.org.": + response = {"rrsets": [{"name": "_acme-challenge.example.org.", "type": "TXT", + "rrdatas": ["\"example-txt-contents\""], "ttl": 60}]} + mock_return = mock.MagicMock() + mock_return.execute.return_value = response + mock_return.execute.side_effect = rrs_list_side_effect + return mock_return + mock_rrs.list.side_effect = rrs_list + mock_changes = mock.MagicMock() + + client.dns.managedZones = mock.MagicMock(return_value=mock_mz) + client.dns.changes = mock.MagicMock(return_value=mock_changes) + client.dns.resourceRecordSets = mock.MagicMock(return_value=mock_rrs) + + return client, mock_changes + + @mock.patch('googleapiclient.discovery.build') + @mock.patch('google.auth.default') + def test_client_with_default_credentials(self, credential_mock, discovery_mock): + test_credentials = mock.MagicMock() + credential_mock.return_value = (test_credentials, PROJECT_ID) + from certbot_dns_google._internal.dns_google import _GoogleClient + client = _GoogleClient(None) + credential_mock.assert_called_once_with(scopes=SCOPES) + assert client.project_id == PROJECT_ID + discovery_mock.assert_called_once_with('dns', 'v1', + credentials=test_credentials, + cache_discovery=False) + + @mock.patch('googleapiclient.discovery.build') + @mock.patch('google.auth.load_credentials_from_file') + def test_client_with_json_credentials(self, credential_mock, discovery_mock): + test_credentials = mock.MagicMock() + credential_mock.return_value = (test_credentials, PROJECT_ID) + from certbot_dns_google._internal.dns_google import _GoogleClient + client = _GoogleClient(ACCOUNT_JSON_PATH) + credential_mock.assert_called_once_with(ACCOUNT_JSON_PATH, scopes=SCOPES) + assert credential_mock.called + assert client.project_id == PROJECT_ID + discovery_mock.assert_called_once_with('dns', 'v1', + credentials=test_credentials, + cache_discovery=False) + + @mock.patch('google.auth.load_credentials_from_file') + def test_client_bad_credentials_file(self, credential_mock): + google_dce: type[googleauth_exceptions.DefaultCredentialsError] = googleauth_exceptions.DefaultCredentialsError + credential_mock.side_effect = google_dce('Some exception buried in google.auth') + with pytest.raises(errors.PluginError) as exc_info: + self._setUp_client_with_mock([]) + assert str(exc_info.value) == \ + "Error loading credentials file '/not/a/real/path.json': " \ + "Some exception buried in google.auth" + + @mock.patch('google.auth.load_credentials_from_file') + def test_client_missing_project_id(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), "") + with pytest.raises(errors.PluginError) as exc_info: + self._setUp_client_with_mock([]) + assert str(exc_info.value) == \ + "The Google Cloud project could not be automatically determined. " \ + "Please configure it using --dns-google-project ." + + @mock.patch('googleapiclient.discovery.build') + @mock.patch('google.auth.default') + def test_client_with_project_id(self, credential_mock, unused_discovery_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + from certbot_dns_google._internal.dns_google import _GoogleClient + client = _GoogleClient(None, "test-project-2") + assert credential_mock.called + assert client.project_id == "test-project-2" + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_add_txt_record(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + credential_mock.assert_called_once_with('/not/a/real/path.json', scopes=SCOPES) + + client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + expected_body = { + "kind": "dns#change", + "additions": [ + { + "kind": "dns#resourceRecordSet", + "type": "TXT", + "name": self.record_name + ".", + "rrdatas": [self.record_content, ], + "ttl": self.record_ttl, + }, + ], + } + + changes.create.assert_called_with(body=expected_body, + managedZone=self.zone, + project=PROJECT_ID) + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_add_txt_record_and_poll(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + changes.create.return_value.execute.return_value = {'status': 'pending', 'id': self.change} + changes.get.return_value.execute.return_value = {'status': 'done'} + + client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + changes.create.assert_called_with(body=mock.ANY, + managedZone=self.zone, + project=PROJECT_ID) + + changes.get.assert_called_with(changeId=self.change, + managedZone=self.zone, + project=PROJECT_ID) + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_add_txt_record_and_poll_split_horizon(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': '{zone}-private'.format(zone=self.zone), 'dnsName': DOMAIN, 'visibility': 'private'},{'id': '{zone}-public'.format(zone=self.zone), 'dnsName': DOMAIN, 'visibility': self.visibility}]}]) + changes.create.return_value.execute.return_value = {'status': 'pending', 'id': self.change} + changes.get.return_value.execute.return_value = {'status': 'done'} + + client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + changes.create.assert_called_with(body=mock.ANY, + managedZone='{zone}-public'.format(zone=self.zone), + project=PROJECT_ID) + + changes.get.assert_called_with(changeId=self.change, + managedZone='{zone}-public'.format(zone=self.zone), + project=PROJECT_ID) + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_add_txt_record_delete_old(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock( + [{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + # pylint: disable=line-too-long + mock_get_rrs = "certbot_dns_google._internal.dns_google._GoogleClient.get_existing_txt_rrset" + with mock.patch(mock_get_rrs) as mock_rrs: + mock_rrs.return_value = {"rrdatas": ["sample-txt-contents"], "ttl": self.record_ttl} + client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + assert changes.create.called is True + deletions = changes.create.call_args_list[0][1]["body"]["deletions"][0] + assert "sample-txt-contents" in deletions["rrdatas"] + assert self.record_ttl == deletions["ttl"] + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_add_txt_record_delete_old_ttl_case(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock( + [{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + # pylint: disable=line-too-long + mock_get_rrs = "certbot_dns_google._internal.dns_google._GoogleClient.get_existing_txt_rrset" + with mock.patch(mock_get_rrs) as mock_rrs: + custom_ttl = 300 + mock_rrs.return_value = {"rrdatas": ["sample-txt-contents"], "ttl": custom_ttl} + client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + assert changes.create.called is True + deletions = changes.create.call_args_list[0][1]["body"]["deletions"][0] + assert "sample-txt-contents" in deletions["rrdatas"] + assert custom_ttl == deletions["ttl"] #otherwise HTTP 412 + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_add_txt_record_noop(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock( + [{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + client.add_txt_record(DOMAIN, "_acme-challenge.example.org", + "example-txt-contents", self.record_ttl) + assert changes.create.called is False + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_add_txt_record_error_during_zone_lookup(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, unused_changes = self._setUp_client_with_mock(API_ERROR) + + with pytest.raises(errors.PluginError): + client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_add_txt_record_zone_not_found(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, unused_changes = self._setUp_client_with_mock([{'managedZones': []}, + {'managedZones': []}]) + + with pytest.raises(errors.PluginError): + client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_add_txt_record_error_during_add(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + changes.create.side_effect = API_ERROR + + with pytest.raises(errors.PluginError): + client.add_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_del_txt_record_multi_rrdatas(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + # pylint: disable=line-too-long + mock_get_rrs = "certbot_dns_google._internal.dns_google._GoogleClient.get_existing_txt_rrset" + with mock.patch(mock_get_rrs) as mock_rrs: + mock_rrs.return_value = {"rrdatas": ["\"sample-txt-contents\"", + "\"example-txt-contents\""], "ttl": self.record_ttl} + client.del_txt_record(DOMAIN, "_acme-challenge.example.org", + "example-txt-contents", self.record_ttl) + + expected_body = { + "kind": "dns#change", + "deletions": [ + { + "kind": "dns#resourceRecordSet", + "type": "TXT", + "name": "_acme-challenge.example.org.", + "rrdatas": ["\"sample-txt-contents\"", "\"example-txt-contents\""], + "ttl": self.record_ttl, + }, + ], + "additions": [ + { + "kind": "dns#resourceRecordSet", + "type": "TXT", + "name": "_acme-challenge.example.org.", + "rrdatas": ["\"sample-txt-contents\"", ], + "ttl": self.record_ttl, + }, + ], + } + + changes.create.assert_called_with(body=expected_body, + managedZone=self.zone, + project=PROJECT_ID) + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_del_txt_record_single_rrdatas(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + # pylint: disable=line-too-long + mock_get_rrs = "certbot_dns_google._internal.dns_google._GoogleClient.get_existing_txt_rrset" + with mock.patch(mock_get_rrs) as mock_rrs: + mock_rrs.return_value = {"rrdatas": ["\"example-txt-contents\""], "ttl": self.record_ttl} + client.del_txt_record(DOMAIN, "_acme-challenge.example.org", + "example-txt-contents", self.record_ttl) + + expected_body = { + "kind": "dns#change", + "deletions": [ + { + "kind": "dns#resourceRecordSet", + "type": "TXT", + "name": "_acme-challenge.example.org.", + "rrdatas": ["\"example-txt-contents\""], + "ttl": self.record_ttl, + }, + ], + } + + changes.create.assert_called_with(body=expected_body, + managedZone=self.zone, + project=PROJECT_ID) + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_del_txt_record_error_during_zone_lookup(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock(API_ERROR) + client.del_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + changes.create.assert_not_called() + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_del_txt_record_zone_not_found(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock([{'managedZones': []}, + {'managedZones': []}]) + client.del_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + changes.create.assert_not_called() + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_del_txt_record_error_during_delete(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, changes = self._setUp_client_with_mock([{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + changes.create.side_effect = API_ERROR + + client.del_txt_record(DOMAIN, self.record_name, self.record_content, self.record_ttl) + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_get_existing_found(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, unused_changes = self._setUp_client_with_mock( + [{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + # Record name mocked in setUp + found = client.get_existing_txt_rrset(self.zone, "_acme-challenge.example.org") + assert found + assert found["rrdatas"] == ["\"example-txt-contents\""] + assert found["ttl"] == 60 + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_get_existing_not_found(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, unused_changes = self._setUp_client_with_mock( + [{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}]) + not_found = client.get_existing_txt_rrset(self.zone, "nonexistent.tld") + assert not_found is None + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_get_existing_with_error(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, unused_changes = self._setUp_client_with_mock( + [{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}], API_ERROR) + # Record name mocked in setUp + found = client.get_existing_txt_rrset(self.zone, "_acme-challenge.example.org") + assert found is None + + @mock.patch('google.auth.load_credentials_from_file') + @mock.patch('certbot_dns_google._internal.dns_google.open', + mock.mock_open(read_data='{"project_id": "' + PROJECT_ID + '"}'), create=True) + def test_get_existing_fallback(self, credential_mock): + credential_mock.return_value = (mock.MagicMock(), PROJECT_ID) + + client, unused_changes = self._setUp_client_with_mock( + [{'managedZones': [{'id': self.zone, 'visibility': self.visibility}]}], API_ERROR) + rrset = client.get_existing_txt_rrset(self.zone, "_acme-challenge.example.org") + assert not rrset + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-google/certbot_dns_google/testdata/discovery.json b/certbot-dns-google/src/certbot_dns_google/_internal/tests/testdata/discovery.json similarity index 99% rename from certbot-dns-google/certbot_dns_google/testdata/discovery.json rename to certbot-dns-google/src/certbot_dns_google/_internal/tests/testdata/discovery.json index 79a40664511..9d1f3c31997 100644 --- a/certbot-dns-google/certbot_dns_google/testdata/discovery.json +++ b/certbot-dns-google/src/certbot_dns_google/_internal/tests/testdata/discovery.json @@ -389,6 +389,11 @@ "items": { "type": "string" } + }, + "visibility": { + "type": "string", + "description": "The zone's visibility: public zones are exposed to the Internet, while private zones are visible only to Virtual Private Cloud resources.", + "default": "public" } } }, diff --git a/certbot-dns-google/src/certbot_dns_google/py.typed b/certbot-dns-google/src/certbot_dns_google/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-linode/.readthedocs.yaml b/certbot-dns-linode/.readthedocs.yaml new file mode 100644 index 00000000000..da324c78f45 --- /dev/null +++ b/certbot-dns-linode/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-linode[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-linode/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-linode/Dockerfile b/certbot-dns-linode/Dockerfile deleted file mode 100644 index 2e237b52118..00000000000 --- a/certbot-dns-linode/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-linode - -RUN pip install --no-cache-dir --editable src/certbot-dns-linode diff --git a/certbot-dns-linode/MANIFEST.in b/certbot-dns-linode/MANIFEST.in index 18f018c08d3..cb321889517 100644 --- a/certbot-dns-linode/MANIFEST.in +++ b/certbot-dns-linode/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_linode/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-linode/certbot_dns_linode/__init__.py b/certbot-dns-linode/certbot_dns_linode/__init__.py deleted file mode 100644 index 0a6ccec61cc..00000000000 --- a/certbot-dns-linode/certbot_dns_linode/__init__.py +++ /dev/null @@ -1,92 +0,0 @@ -""" -The `~certbot_dns_linode.dns_linode` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the Linode API. - - -Named Arguments ---------------- - -========================================== =================================== -``--dns-linode-credentials`` Linode credentials_ INI file. - (Required) -``--dns-linode-propagation-seconds`` The number of seconds to wait for - DNS to propagate before asking the - ACME server to verify the DNS - record. - (Default: 1200 because Linode - updates its first DNS every 15 - minutes and we allow 5 more minutes - for the update to reach the other 5 - servers) -========================================== =================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing Linode API -credentials, obtained from your Linode account's `Applications & API -Tokens page `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # Linode API credentials used by Certbot - dns_linode_key = 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ64 - -The path to this file can be provided interactively or using the -``--dns-linode-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - Linode account. Users who can read this file can use these credentials - to issue arbitrary API calls on your behalf. Users who can cause Certbot to - run using these credentials can complete a ``dns-01`` challenge to acquire - new certificates or revoke existing certificates for associated domains, - even if those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-linode \\ - --dns-linode-credentials ~/.secrets/certbot/linode.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-linode \\ - --dns-linode-credentials ~/.secrets/certbot/linode.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 1000 seconds - for DNS propagation (Linode updates its first DNS every 15 minutes - and we allow some extra time for the update to reach the other 5 - servers) - - certbot certonly \\ - --dns-linode \\ - --dns-linode-credentials ~/.secrets/certbot/linode.ini \\ - --dns-linode-propagation-seconds 1000 \\ - -d example.com - -""" diff --git a/certbot-dns-linode/certbot_dns_linode/dns_linode.py b/certbot-dns-linode/certbot_dns_linode/dns_linode.py deleted file mode 100644 index 4e0500fa099..00000000000 --- a/certbot-dns-linode/certbot_dns_linode/dns_linode.py +++ /dev/null @@ -1,76 +0,0 @@ -"""DNS Authenticator for Linode.""" -import logging - -import zope.interface -from lexicon.providers import linode - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common -from certbot.plugins import dns_common_lexicon - -logger = logging.getLogger(__name__) - -API_KEY_URL = 'https://manager.linode.com/profile/api' - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for Linode - - This Authenticator uses the Linode API to fulfill a dns-01 challenge. - """ - - description = 'Obtain certs using a DNS TXT record (if you are using Linode for DNS).' - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=1200) - add('credentials', help='Linode credentials INI file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the Linode API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'Linode credentials INI file', - { - 'key': 'API key for Linode account, obtained from {0}'.format(API_KEY_URL) - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_linode_client().add_txt_record(domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_linode_client().del_txt_record(domain, validation_name, validation) - - def _get_linode_client(self): - return _LinodeLexiconClient(self.credentials.conf('key')) - - -class _LinodeLexiconClient(dns_common_lexicon.LexiconClient): - """ - Encapsulates all communication with the Linode API. - """ - - def __init__(self, api_key): - super(_LinodeLexiconClient, self).__init__() - - config = dns_common_lexicon.build_lexicon_config('linode', {}, { - 'auth_token': api_key, - }) - - self.provider = linode.Provider(config) - - def _handle_general_error(self, e, domain_name): - if not str(e).startswith('Domain not found'): - return errors.PluginError('Unexpected error determining zone identifier for {0}: {1}' - .format(domain_name, e)) - diff --git a/certbot-dns-linode/certbot_dns_linode/dns_linode_test.py b/certbot-dns-linode/certbot_dns_linode/dns_linode_test.py deleted file mode 100644 index 2a0ee49f7c2..00000000000 --- a/certbot-dns-linode/certbot_dns_linode/dns_linode_test.py +++ /dev/null @@ -1,47 +0,0 @@ -"""Tests for certbot_dns_linode.dns_linode.""" - -import os -import unittest - -import mock - -from certbot.plugins import dns_test_common -from certbot.plugins import dns_test_common_lexicon -from certbot.tests import util as test_util - -TOKEN = 'a-token' - -class AuthenticatorTest(test_util.TempDirTestCase, - dns_test_common_lexicon.BaseLexiconAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_linode.dns_linode import Authenticator - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"linode_key": TOKEN}, path) - - self.config = mock.MagicMock(linode_credentials=path, - linode_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "linode") - - self.mock_client = mock.MagicMock() - # _get_linode_client | pylint: disable=protected-access - self.auth._get_linode_client = mock.MagicMock(return_value=self.mock_client) - -class LinodeLexiconClientTest(unittest.TestCase, dns_test_common_lexicon.BaseLexiconClientTest): - - DOMAIN_NOT_FOUND = Exception('Domain not found') - - def setUp(self): - from certbot_dns_linode.dns_linode import _LinodeLexiconClient - - self.client = _LinodeLexiconClient(TOKEN) - - self.provider_mock = mock.MagicMock() - self.client.provider = self.provider_mock - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-linode/docs/api.rst b/certbot-dns-linode/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-linode/docs/api.rst +++ b/certbot-dns-linode/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-linode/docs/api/dns_linode.rst b/certbot-dns-linode/docs/api/dns_linode.rst deleted file mode 100644 index 6380b3eba5d..00000000000 --- a/certbot-dns-linode/docs/api/dns_linode.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_linode.dns_linode` ------------------------------------------------- - -.. automodule:: certbot_dns_linode.dns_linode - :members: diff --git a/certbot-dns-linode/docs/conf.py b/certbot-dns-linode/docs/conf.py index 1fb721400d4..4d4586b472a 100644 --- a/certbot-dns-linode/docs/conf.py +++ b/certbot-dns-linode/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-linode/docs/make.bat b/certbot-dns-linode/docs/make.bat index 1f2a6867f1c..5352aaecf33 100644 --- a/certbot-dns-linode/docs/make.bat +++ b/certbot-dns-linode/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-linode/local-oldest-requirements.txt b/certbot-dns-linode/local-oldest-requirements.txt deleted file mode 100644 index 65f5a758ea0..00000000000 --- a/certbot-dns-linode/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ --e acme[dev] --e .[dev] diff --git a/certbot-dns-linode/pyproject.toml b/certbot-dns-linode/pyproject.toml new file mode 100644 index 00000000000..b3086df7115 --- /dev/null +++ b/certbot-dns-linode/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-linode" +dynamic = ["version", "dependencies"] +description = "Linode DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-linode = "certbot_dns_linode._internal.dns_linode:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-linode/readthedocs.org.requirements.txt b/certbot-dns-linode/readthedocs.org.requirements.txt deleted file mode 100644 index 47449454f59..00000000000 --- a/certbot-dns-linode/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-linode[docs] diff --git a/certbot-dns-linode/setup.cfg b/certbot-dns-linode/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-linode/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-linode/setup.py b/certbot-dns-linode/setup.py index 31c2c20bc6b..e17fd5066ee 100644 --- a/certbot-dns-linode/setup.py +++ b/certbot-dns-linode/setup.py @@ -1,64 +1,25 @@ +import os + from setuptools import setup -from setuptools import find_packages -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Please update tox.ini when modifying dependency version requirements install_requires = [ - 'acme>=0.31.0.dev0', - 'certbot>=0.31.0.dev0', - 'dns-lexicon>=2.2.1', - 'mock', - 'setuptools', - 'zope.interface', + 'dns-lexicon>=3.14.1', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-linode', version=version, - description="Linode DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-linode = certbot_dns_linode.dns_linode:Authenticator', - ], - }, - test_suite='certbot_dns_linode', ) diff --git a/certbot-dns-linode/src/certbot_dns_linode/__init__.py b/certbot-dns-linode/src/certbot_dns_linode/__init__.py new file mode 100644 index 00000000000..7b879778afa --- /dev/null +++ b/certbot-dns-linode/src/certbot_dns_linode/__init__.py @@ -0,0 +1,97 @@ +""" +The `~certbot_dns_linode.dns_linode` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using the Linode API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +========================================== =================================== +``--dns-linode-credentials`` Linode credentials_ INI file. + (Required) +``--dns-linode-propagation-seconds`` The number of seconds to wait for + DNS to propagate before asking the + ACME server to verify the DNS + record. + (Default: 120 because Linode + updates its first DNS every 60 + seconds and we allow 60 more seconds + for the update to reach other 5 + servers) +========================================== =================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing Linode API +credentials, obtained from your Linode account's `Applications & API +Tokens page (legacy) `_ or `Applications +& API Tokens page (new) `_. + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # Linode API credentials used by Certbot + dns_linode_key = 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ64 + +The path to this file can be provided interactively or using the +``--dns-linode-credentials`` command-line argument. Certbot records the path +to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + Linode account. Users who can read this file can use these credentials + to issue arbitrary API calls on your behalf. Users who can cause Certbot to + run using these credentials can complete a ``dns-01`` challenge to acquire + new certificates or revoke existing certificates for associated domains, + even if those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-linode \\ + --dns-linode-credentials ~/.secrets/certbot/linode.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-linode \\ + --dns-linode-credentials ~/.secrets/certbot/linode.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 120 seconds + for DNS propagation (Linode updates its first DNS every minute + and we allow some extra time for the update to reach other 5 + servers) + + certbot certonly \\ + --dns-linode \\ + --dns-linode-credentials ~/.secrets/certbot/linode.ini \\ + --dns-linode-propagation-seconds 120 \\ + -d example.com + +""" diff --git a/certbot-dns-linode/src/certbot_dns_linode/_internal/__init__.py b/certbot-dns-linode/src/certbot_dns_linode/_internal/__init__.py new file mode 100644 index 00000000000..9090d92d366 --- /dev/null +++ b/certbot-dns-linode/src/certbot_dns_linode/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_linode.dns_linode` plugin.""" diff --git a/certbot-dns-linode/src/certbot_dns_linode/_internal/dns_linode.py b/certbot-dns-linode/src/certbot_dns_linode/_internal/dns_linode.py new file mode 100644 index 00000000000..53148d25d47 --- /dev/null +++ b/certbot-dns-linode/src/certbot_dns_linode/_internal/dns_linode.py @@ -0,0 +1,58 @@ +"""DNS Authenticator for Linode.""" +import logging +from typing import Any +from typing import Callable +from typing import Optional + +from certbot import errors +from certbot.plugins import dns_common_lexicon + +logger = logging.getLogger(__name__) + +API_KEY_URL_V4 = 'https://cloud.linode.com/profile/tokens' + + +class Authenticator(dns_common_lexicon.LexiconDNSAuthenticator): + """DNS Authenticator for Linode + + This Authenticator uses the Linode API to fulfill a dns-01 challenge. + """ + + description = 'Obtain certificates using a DNS TXT record (if you are using Linode for DNS).' + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._add_provider_option('key', + 'API key for Linode account, ' + f'obtained from {API_KEY_URL_V4}', + 'auth_token') + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 120) -> None: + super().add_parser_arguments(add, default_propagation_seconds) + add('credentials', help='Linode credentials INI file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the Linode API.' + + @property + def _provider_name(self) -> str: + if not hasattr(self, '_credentials'): # pragma: no cover + self._setup_credentials() + + return 'linode4' + + def _setup_credentials(self) -> None: + self._credentials = self._configure_credentials( + key='credentials', + label='Credentials INI file for linode DNS authenticator', + required_variables={item[0]: item[1] for item in self._provider_options}, + ) + + def _handle_general_error(self, e: Exception, domain_name: str) -> Optional[errors.PluginError]: + if not str(e).startswith('Domain not found'): + return errors.PluginError('Unexpected error determining zone identifier ' + f'for {domain_name}: {e}') + return None diff --git a/certbot-dns-linode/src/certbot_dns_linode/_internal/tests/__init__.py b/certbot-dns-linode/src/certbot_dns_linode/_internal/tests/__init__.py new file mode 100644 index 00000000000..b096c54b23c --- /dev/null +++ b/certbot-dns-linode/src/certbot_dns_linode/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-linode tests""" diff --git a/certbot-dns-linode/src/certbot_dns_linode/_internal/tests/dns_linode_test.py b/certbot-dns-linode/src/certbot_dns_linode/_internal/tests/dns_linode_test.py new file mode 100644 index 00000000000..dfc1bce04fe --- /dev/null +++ b/certbot-dns-linode/src/certbot_dns_linode/_internal/tests/dns_linode_test.py @@ -0,0 +1,47 @@ +"""Tests for certbot_dns_linode._internal.dns_linode.""" + +import sys +from unittest import mock + +import pytest + +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins import dns_test_common_lexicon +from certbot.tests import util as test_util +from certbot_dns_linode._internal.dns_linode import Authenticator + +TOKEN = 'a-token' +TOKEN_V4 = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' + + +class AuthenticatorTest(test_util.TempDirTestCase, + dns_test_common_lexicon.BaseLexiconDNSAuthenticatorTest): + + DOMAIN_NOT_FOUND = Exception('Domain not found') + + def setUp(self): + super().setUp() + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write({"linode_key": TOKEN}, path) + + self.config = mock.MagicMock(linode_credentials=path, + linode_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "linode") + + # pylint: disable=protected-access + def test_api_version_4_detection(self): + path = os.path.join(self.tempdir, 'file_4_auto.ini') + dns_test_common.write({"linode_key": TOKEN_V4}, path) + + config = mock.MagicMock(linode_credentials=path, + linode_propagation_seconds=0) + auth = Authenticator(config, "linode") + + assert auth._provider_name == "linode4" + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-linode/src/certbot_dns_linode/py.typed b/certbot-dns-linode/src/certbot_dns_linode/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-luadns/.readthedocs.yaml b/certbot-dns-luadns/.readthedocs.yaml new file mode 100644 index 00000000000..1aec4af3f0f --- /dev/null +++ b/certbot-dns-luadns/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-luadns[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-luadns/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-luadns/Dockerfile b/certbot-dns-luadns/Dockerfile deleted file mode 100644 index 6efb4d7775a..00000000000 --- a/certbot-dns-luadns/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-luadns - -RUN pip install --no-cache-dir --editable src/certbot-dns-luadns diff --git a/certbot-dns-luadns/MANIFEST.in b/certbot-dns-luadns/MANIFEST.in index 18f018c08d3..8193d7a7fd3 100644 --- a/certbot-dns-luadns/MANIFEST.in +++ b/certbot-dns-luadns/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_luadns/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-luadns/certbot_dns_luadns/__init__.py b/certbot-dns-luadns/certbot_dns_luadns/__init__.py deleted file mode 100644 index e8e86f77c4b..00000000000 --- a/certbot-dns-luadns/certbot_dns_luadns/__init__.py +++ /dev/null @@ -1,86 +0,0 @@ -""" -The `~certbot_dns_luadns.dns_luadns` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the LuaDNS API. - - -Named Arguments ---------------- - -========================================= ===================================== -``--dns-luadns-credentials`` LuaDNS credentials_ INI file. - (Required) -``--dns-luadns-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 30) -========================================= ===================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing LuaDNS API -credentials, obtained from your LuaDNS -`account settings page `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # LuaDNS API credentials used by Certbot - dns_luadns_email = user@example.com - dns_luadns_token = 0123456789abcdef0123456789abcdef - -The path to this file can be provided interactively or using the -``--dns-luadns-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - LuaDNS account. Users who can read this file can use these credentials - to issue arbitrary API calls on your behalf. Users who can cause Certbot to - run using these credentials can complete a ``dns-01`` challenge to acquire - new certificates or revoke existing certificates for associated domains, - even if those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-luadns \\ - --dns-luadns-credentials ~/.secrets/certbot/luadns.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-luadns \\ - --dns-luadns-credentials ~/.secrets/certbot/luadns.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 2 minutes - for DNS propagation - - certbot certonly \\ - --dns-luadns \\ - --dns-luadns-credentials ~/.secrets/certbot/luadns.ini \\ - --dns-luadns-propagation-seconds 120 \\ - -d example.com - -""" diff --git a/certbot-dns-luadns/certbot_dns_luadns/dns_luadns.py b/certbot-dns-luadns/certbot_dns_luadns/dns_luadns.py deleted file mode 100644 index 7cdd4c8e18a..00000000000 --- a/certbot-dns-luadns/certbot_dns_luadns/dns_luadns.py +++ /dev/null @@ -1,86 +0,0 @@ -"""DNS Authenticator for LuaDNS DNS.""" -import logging - -import zope.interface -from lexicon.providers import luadns - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common -from certbot.plugins import dns_common_lexicon - -logger = logging.getLogger(__name__) - -ACCOUNT_URL = 'https://api.luadns.com/settings' - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for LuaDNS - - This Authenticator uses the LuaDNS API to fulfill a dns-01 challenge. - """ - - description = 'Obtain certificates using a DNS TXT record (if you are using LuaDNS for DNS).' - ttl = 60 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=30) - add('credentials', help='LuaDNS credentials INI file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the LuaDNS API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'LuaDNS credentials INI file', - { - 'email': 'email address associated with LuaDNS account', - 'token': 'API token for LuaDNS account, obtained from {0}'.format(ACCOUNT_URL) - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_luadns_client().add_txt_record(domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_luadns_client().del_txt_record(domain, validation_name, validation) - - def _get_luadns_client(self): - return _LuaDNSLexiconClient(self.credentials.conf('email'), - self.credentials.conf('token'), - self.ttl) - - -class _LuaDNSLexiconClient(dns_common_lexicon.LexiconClient): - """ - Encapsulates all communication with the LuaDNS via Lexicon. - """ - - def __init__(self, email, token, ttl): - super(_LuaDNSLexiconClient, self).__init__() - - config = dns_common_lexicon.build_lexicon_config('luadns', { - 'ttl': ttl, - }, { - 'auth_username': email, - 'auth_token': token, - }) - - self.provider = luadns.Provider(config) - - def _handle_http_error(self, e, domain_name): - hint = None - if str(e).startswith('401 Client Error: Unauthorized for url:'): - hint = 'Are your email and API token values correct?' - - return errors.PluginError('Error determining zone identifier for {0}: {1}.{2}' - .format(domain_name, e, ' ({0})'.format(hint) if hint else '')) diff --git a/certbot-dns-luadns/certbot_dns_luadns/dns_luadns_test.py b/certbot-dns-luadns/certbot_dns_luadns/dns_luadns_test.py deleted file mode 100644 index bf77e03e47a..00000000000 --- a/certbot-dns-luadns/certbot_dns_luadns/dns_luadns_test.py +++ /dev/null @@ -1,52 +0,0 @@ -"""Tests for certbot_dns_luadns.dns_luadns.""" - -import os -import unittest - -import mock -from requests.exceptions import HTTPError - -from certbot.plugins import dns_test_common -from certbot.plugins import dns_test_common_lexicon -from certbot.tests import util as test_util - -EMAIL = 'fake@example.com' -TOKEN = 'foo' - - -class AuthenticatorTest(test_util.TempDirTestCase, - dns_test_common_lexicon.BaseLexiconAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_luadns.dns_luadns import Authenticator - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"luadns_email": EMAIL, "luadns_token": TOKEN}, path) - - self.config = mock.MagicMock(luadns_credentials=path, - luadns_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "luadns") - - self.mock_client = mock.MagicMock() - # _get_luadns_client | pylint: disable=protected-access - self.auth._get_luadns_client = mock.MagicMock(return_value=self.mock_client) - - -class LuaDNSLexiconClientTest(unittest.TestCase, dns_test_common_lexicon.BaseLexiconClientTest): - - LOGIN_ERROR = HTTPError("401 Client Error: Unauthorized for url: ...") - - def setUp(self): - from certbot_dns_luadns.dns_luadns import _LuaDNSLexiconClient - - self.client = _LuaDNSLexiconClient(EMAIL, TOKEN, 0) - - self.provider_mock = mock.MagicMock() - self.client.provider = self.provider_mock - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-luadns/docs/api.rst b/certbot-dns-luadns/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-luadns/docs/api.rst +++ b/certbot-dns-luadns/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-luadns/docs/api/dns_luadns.rst b/certbot-dns-luadns/docs/api/dns_luadns.rst deleted file mode 100644 index 9aecbaf05e0..00000000000 --- a/certbot-dns-luadns/docs/api/dns_luadns.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_luadns.dns_luadns` ----------------------------------- - -.. automodule:: certbot_dns_luadns.dns_luadns - :members: diff --git a/certbot-dns-luadns/docs/conf.py b/certbot-dns-luadns/docs/conf.py index bd81d5a5f96..dcb4d99a09c 100644 --- a/certbot-dns-luadns/docs/conf.py +++ b/certbot-dns-luadns/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-luadns/docs/make.bat b/certbot-dns-luadns/docs/make.bat index 9cfe0400e3a..67ad8875866 100644 --- a/certbot-dns-luadns/docs/make.bat +++ b/certbot-dns-luadns/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-luadns/local-oldest-requirements.txt b/certbot-dns-luadns/local-oldest-requirements.txt deleted file mode 100644 index 65f5a758ea0..00000000000 --- a/certbot-dns-luadns/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ --e acme[dev] --e .[dev] diff --git a/certbot-dns-luadns/pyproject.toml b/certbot-dns-luadns/pyproject.toml new file mode 100644 index 00000000000..a1f5c5f0d51 --- /dev/null +++ b/certbot-dns-luadns/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-luadns" +dynamic = ["version", "dependencies"] +description = "LuaDNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-luadns = "certbot_dns_luadns._internal.dns_luadns:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-luadns/readthedocs.org.requirements.txt b/certbot-dns-luadns/readthedocs.org.requirements.txt deleted file mode 100644 index acb51e4eff0..00000000000 --- a/certbot-dns-luadns/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-luadns[docs] diff --git a/certbot-dns-luadns/setup.cfg b/certbot-dns-luadns/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-luadns/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-luadns/setup.py b/certbot-dns-luadns/setup.py index 31472e8cfff..e17fd5066ee 100644 --- a/certbot-dns-luadns/setup.py +++ b/certbot-dns-luadns/setup.py @@ -1,66 +1,25 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.31.0.dev0', - 'certbot>=0.31.0.dev0', - 'dns-lexicon>=2.2.1', # Support for >1 TXT record per name - 'mock', - 'setuptools', - 'zope.interface', + 'dns-lexicon>=3.14.1', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-luadns', version=version, - description="LuaDNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-luadns = certbot_dns_luadns.dns_luadns:Authenticator', - ], - }, - test_suite='certbot_dns_luadns', ) diff --git a/certbot-dns-luadns/src/certbot_dns_luadns/__init__.py b/certbot-dns-luadns/src/certbot_dns_luadns/__init__.py new file mode 100644 index 00000000000..302cb1392ea --- /dev/null +++ b/certbot-dns-luadns/src/certbot_dns_luadns/__init__.py @@ -0,0 +1,90 @@ +""" +The `~certbot_dns_luadns.dns_luadns` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using the LuaDNS API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +========================================= ===================================== +``--dns-luadns-credentials`` LuaDNS credentials_ INI file. + (Required) +``--dns-luadns-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 30) +========================================= ===================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing LuaDNS API +credentials, obtained from your LuaDNS +`account settings page `_. + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # LuaDNS API credentials used by Certbot + dns_luadns_email = user@example.com + dns_luadns_token = 0123456789abcdef0123456789abcdef + +The path to this file can be provided interactively or using the +``--dns-luadns-credentials`` command-line argument. Certbot records the path +to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + LuaDNS account. Users who can read this file can use these credentials + to issue arbitrary API calls on your behalf. Users who can cause Certbot to + run using these credentials can complete a ``dns-01`` challenge to acquire + new certificates or revoke existing certificates for associated domains, + even if those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-luadns \\ + --dns-luadns-credentials ~/.secrets/certbot/luadns.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-luadns \\ + --dns-luadns-credentials ~/.secrets/certbot/luadns.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 2 minutes + for DNS propagation + + certbot certonly \\ + --dns-luadns \\ + --dns-luadns-credentials ~/.secrets/certbot/luadns.ini \\ + --dns-luadns-propagation-seconds 120 \\ + -d example.com + +""" diff --git a/certbot-dns-luadns/src/certbot_dns_luadns/_internal/__init__.py b/certbot-dns-luadns/src/certbot_dns_luadns/_internal/__init__.py new file mode 100644 index 00000000000..8ab0a00e217 --- /dev/null +++ b/certbot-dns-luadns/src/certbot_dns_luadns/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_luadns.dns_luadns` plugin.""" diff --git a/certbot-dns-luadns/src/certbot_dns_luadns/_internal/dns_luadns.py b/certbot-dns-luadns/src/certbot_dns_luadns/_internal/dns_luadns.py new file mode 100644 index 00000000000..91d5a801dca --- /dev/null +++ b/certbot-dns-luadns/src/certbot_dns_luadns/_internal/dns_luadns.py @@ -0,0 +1,55 @@ +"""DNS Authenticator for LuaDNS DNS.""" +import logging +from typing import Any +from typing import Callable + +from requests import HTTPError + +from certbot import errors +from certbot.plugins import dns_common_lexicon + +logger = logging.getLogger(__name__) + +ACCOUNT_URL = 'https://api.luadns.com/settings' + + +class Authenticator(dns_common_lexicon.LexiconDNSAuthenticator): + """DNS Authenticator for LuaDNS + + This Authenticator uses the LuaDNS API to fulfill a dns-01 challenge. + """ + + description = 'Obtain certificates using a DNS TXT record (if you are using LuaDNS for DNS).' + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._add_provider_option('email', + 'email address associated with LuaDNS account', + 'auth_username') + self._add_provider_option('token', + f'API token for LuaDNS account, obtained from {ACCOUNT_URL}', + 'auth_token') + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 30) -> None: + super().add_parser_arguments(add, default_propagation_seconds) + add('credentials', help='LuaDNS credentials INI file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the LuaDNS API.' + + @property + def _provider_name(self) -> str: + return 'luadns' + + def _handle_http_error(self, e: HTTPError, domain_name: str) -> errors.PluginError: + hint = None + if str(e).startswith('401 Client Error: Unauthorized for url:'): + hint = 'Are your email and API token values correct?' + + hint_disp = f' ({hint})' if hint else '' + + return errors.PluginError(f'Error determining zone identifier for {domain_name}: ' + f'{e}.{hint_disp}') diff --git a/certbot-dns-luadns/src/certbot_dns_luadns/_internal/tests/__init__.py b/certbot-dns-luadns/src/certbot_dns_luadns/_internal/tests/__init__.py new file mode 100644 index 00000000000..2688bd48227 --- /dev/null +++ b/certbot-dns-luadns/src/certbot_dns_luadns/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-luadns tests""" diff --git a/certbot-dns-luadns/src/certbot_dns_luadns/_internal/tests/dns_luadns_test.py b/certbot-dns-luadns/src/certbot_dns_luadns/_internal/tests/dns_luadns_test.py new file mode 100644 index 00000000000..890616b5e2f --- /dev/null +++ b/certbot-dns-luadns/src/certbot_dns_luadns/_internal/tests/dns_luadns_test.py @@ -0,0 +1,38 @@ +"""Tests for certbot_dns_luadns._internal.dns_luadns.""" +import sys +from unittest import mock + +import pytest +from requests import Response +from requests.exceptions import HTTPError + +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins import dns_test_common_lexicon +from certbot.tests import util as test_util + +EMAIL = 'fake@example.com' +TOKEN = 'foo' + + +class AuthenticatorTest(test_util.TempDirTestCase, + dns_test_common_lexicon.BaseLexiconDNSAuthenticatorTest): + + LOGIN_ERROR = HTTPError("401 Client Error: Unauthorized for url: ...", response=Response()) + + def setUp(self): + super().setUp() + + from certbot_dns_luadns._internal.dns_luadns import Authenticator + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write({"luadns_email": EMAIL, "luadns_token": TOKEN}, path) + + self.config = mock.MagicMock(luadns_credentials=path, + luadns_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "luadns") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-luadns/src/certbot_dns_luadns/py.typed b/certbot-dns-luadns/src/certbot_dns_luadns/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-nsone/.readthedocs.yaml b/certbot-dns-nsone/.readthedocs.yaml new file mode 100644 index 00000000000..a4a1535e010 --- /dev/null +++ b/certbot-dns-nsone/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-nsone[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-nsone/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-nsone/Dockerfile b/certbot-dns-nsone/Dockerfile deleted file mode 100644 index 88fc13c578a..00000000000 --- a/certbot-dns-nsone/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-nsone - -RUN pip install --no-cache-dir --editable src/certbot-dns-nsone diff --git a/certbot-dns-nsone/MANIFEST.in b/certbot-dns-nsone/MANIFEST.in index 18f018c08d3..23139f7ee09 100644 --- a/certbot-dns-nsone/MANIFEST.in +++ b/certbot-dns-nsone/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_nsone/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-nsone/certbot_dns_nsone/__init__.py b/certbot-dns-nsone/certbot_dns_nsone/__init__.py deleted file mode 100644 index e59be74a755..00000000000 --- a/certbot-dns-nsone/certbot_dns_nsone/__init__.py +++ /dev/null @@ -1,85 +0,0 @@ -""" -The `~certbot_dns_nsone.dns_nsone` plugin automates the process of completing -a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and subsequently -removing, TXT records using the NS1 API. - - -Named Arguments ---------------- - -======================================== ===================================== -``--dns-nsone-credentials`` NS1 credentials_ INI file. - (Required) -``--dns-nsone-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 30) -======================================== ===================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing NS1 API credentials, -obtained from your NS1 -`account page `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # NS1 API credentials used by Certbot - dns_nsone_api_key = MDAwMDAwMDAwMDAwMDAw - -The path to this file can be provided interactively or using the -``--dns-nsone-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - NS1 account. Users who can read this file can use these credentials to issue - arbitrary API calls on your behalf. Users who can cause Certbot to run using - these credentials can complete a ``dns-01`` challenge to acquire new - certificates or revoke existing certificates for associated domains, even if - those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-nsone \\ - --dns-nsone-credentials ~/.secrets/certbot/nsone.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-nsone \\ - --dns-nsone-credentials ~/.secrets/certbot/nsone.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 60 seconds - for DNS propagation - - certbot certonly \\ - --dns-nsone \\ - --dns-nsone-credentials ~/.secrets/certbot/nsone.ini \\ - --dns-nsone-propagation-seconds 60 \\ - -d example.com - -""" diff --git a/certbot-dns-nsone/certbot_dns_nsone/dns_nsone.py b/certbot-dns-nsone/certbot_dns_nsone/dns_nsone.py deleted file mode 100644 index 3e23df11ce7..00000000000 --- a/certbot-dns-nsone/certbot_dns_nsone/dns_nsone.py +++ /dev/null @@ -1,86 +0,0 @@ -"""DNS Authenticator for NS1 DNS.""" -import logging - -import zope.interface -from lexicon.providers import nsone - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common -from certbot.plugins import dns_common_lexicon - -logger = logging.getLogger(__name__) - -ACCOUNT_URL = 'https://my.nsone.net/#/account/settings' - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for NS1 - - This Authenticator uses the NS1 API to fulfill a dns-01 challenge. - """ - - description = 'Obtain certificates using a DNS TXT record (if you are using NS1 for DNS).' - ttl = 60 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=30) - add('credentials', help='NS1 credentials file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the NS1 API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'NS1 credentials file', - { - 'api-key': 'API key for NS1 API, obtained from {0}'.format(ACCOUNT_URL) - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_nsone_client().add_txt_record(domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_nsone_client().del_txt_record(domain, validation_name, validation) - - def _get_nsone_client(self): - return _NS1LexiconClient(self.credentials.conf('api-key'), self.ttl) - - -class _NS1LexiconClient(dns_common_lexicon.LexiconClient): - """ - Encapsulates all communication with the NS1 via Lexicon. - """ - - def __init__(self, api_key, ttl): - super(_NS1LexiconClient, self).__init__() - - config = dns_common_lexicon.build_lexicon_config('nsone', { - 'ttl': ttl, - }, { - 'auth_token': api_key, - }) - - self.provider = nsone.Provider(config) - - def _handle_http_error(self, e, domain_name): - if domain_name in str(e) and (str(e).startswith('404 Client Error: Not Found for url:') or \ - str(e).startswith("400 Client Error: Bad Request for url:")): - return # Expected errors when zone name guess is wrong - else: - hint = None - if str(e).startswith('401 Client Error: Unauthorized for url:'): - hint = 'Is your API key correct?' - - return errors.PluginError('Error determining zone identifier: {0}.{1}' - .format(e, ' ({0})'.format(hint) if hint else '')) diff --git a/certbot-dns-nsone/certbot_dns_nsone/dns_nsone_test.py b/certbot-dns-nsone/certbot_dns_nsone/dns_nsone_test.py deleted file mode 100644 index 56668dd01fe..00000000000 --- a/certbot-dns-nsone/certbot_dns_nsone/dns_nsone_test.py +++ /dev/null @@ -1,52 +0,0 @@ -"""Tests for certbot_dns_nsone.dns_nsone.""" - -import os -import unittest - -import mock -from requests.exceptions import HTTPError - -from certbot.plugins import dns_test_common -from certbot.plugins import dns_test_common_lexicon -from certbot.plugins.dns_test_common import DOMAIN -from certbot.tests import util as test_util - -API_KEY = 'foo' - - -class AuthenticatorTest(test_util.TempDirTestCase, - dns_test_common_lexicon.BaseLexiconAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_nsone.dns_nsone import Authenticator - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"nsone_api_key": API_KEY}, path) - - self.config = mock.MagicMock(nsone_credentials=path, - nsone_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "nsone") - - self.mock_client = mock.MagicMock() - # _get_nsone_client | pylint: disable=protected-access - self.auth._get_nsone_client = mock.MagicMock(return_value=self.mock_client) - - -class NS1LexiconClientTest(unittest.TestCase, dns_test_common_lexicon.BaseLexiconClientTest): - DOMAIN_NOT_FOUND = HTTPError('404 Client Error: Not Found for url: {0}.'.format(DOMAIN)) - LOGIN_ERROR = HTTPError('401 Client Error: Unauthorized for url: {0}.'.format(DOMAIN)) - - def setUp(self): - from certbot_dns_nsone.dns_nsone import _NS1LexiconClient - - self.client = _NS1LexiconClient(API_KEY, 0) - - self.provider_mock = mock.MagicMock() - self.client.provider = self.provider_mock - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-nsone/docs/api.rst b/certbot-dns-nsone/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-nsone/docs/api.rst +++ b/certbot-dns-nsone/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-nsone/docs/api/dns_nsone.rst b/certbot-dns-nsone/docs/api/dns_nsone.rst deleted file mode 100644 index 788ce732a29..00000000000 --- a/certbot-dns-nsone/docs/api/dns_nsone.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_nsone.dns_nsone` ----------------------------------- - -.. automodule:: certbot_dns_nsone.dns_nsone - :members: diff --git a/certbot-dns-nsone/docs/conf.py b/certbot-dns-nsone/docs/conf.py index cffe2a25cb8..e76e486d176 100644 --- a/certbot-dns-nsone/docs/conf.py +++ b/certbot-dns-nsone/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-nsone/docs/make.bat b/certbot-dns-nsone/docs/make.bat index 0d19eff5ddb..c37cf5a85cf 100644 --- a/certbot-dns-nsone/docs/make.bat +++ b/certbot-dns-nsone/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-nsone/local-oldest-requirements.txt b/certbot-dns-nsone/local-oldest-requirements.txt deleted file mode 100644 index 65f5a758ea0..00000000000 --- a/certbot-dns-nsone/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ --e acme[dev] --e .[dev] diff --git a/certbot-dns-nsone/pyproject.toml b/certbot-dns-nsone/pyproject.toml new file mode 100644 index 00000000000..ab80603586c --- /dev/null +++ b/certbot-dns-nsone/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-nsone" +dynamic = ["version", "dependencies"] +description = "NS1 DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-nsone = "certbot_dns_nsone._internal.dns_nsone:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-nsone/readthedocs.org.requirements.txt b/certbot-dns-nsone/readthedocs.org.requirements.txt deleted file mode 100644 index dbdee4480bb..00000000000 --- a/certbot-dns-nsone/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-nsone[docs] diff --git a/certbot-dns-nsone/setup.cfg b/certbot-dns-nsone/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-nsone/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-nsone/setup.py b/certbot-dns-nsone/setup.py index 41b99cc739f..e17fd5066ee 100644 --- a/certbot-dns-nsone/setup.py +++ b/certbot-dns-nsone/setup.py @@ -1,66 +1,25 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.31.0.dev0', - 'certbot>=0.31.0.dev0', - 'dns-lexicon>=2.2.1', # Support for >1 TXT record per name - 'mock', - 'setuptools', - 'zope.interface', + 'dns-lexicon>=3.14.1', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-nsone', version=version, - description="NS1 DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-nsone = certbot_dns_nsone.dns_nsone:Authenticator', - ], - }, - test_suite='certbot_dns_nsone', ) diff --git a/certbot-dns-nsone/src/certbot_dns_nsone/__init__.py b/certbot-dns-nsone/src/certbot_dns_nsone/__init__.py new file mode 100644 index 00000000000..6c7d41ba46f --- /dev/null +++ b/certbot-dns-nsone/src/certbot_dns_nsone/__init__.py @@ -0,0 +1,89 @@ +""" +The `~certbot_dns_nsone.dns_nsone` plugin automates the process of completing +a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and subsequently +removing, TXT records using the NS1 API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +======================================== ===================================== +``--dns-nsone-credentials`` NS1 credentials_ INI file. + (Required) +``--dns-nsone-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 30) +======================================== ===================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing NS1 API credentials, +obtained from your NS1 +`account page `_. + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # NS1 API credentials used by Certbot + dns_nsone_api_key = MDAwMDAwMDAwMDAwMDAw + +The path to this file can be provided interactively or using the +``--dns-nsone-credentials`` command-line argument. Certbot records the path +to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + NS1 account. Users who can read this file can use these credentials to issue + arbitrary API calls on your behalf. Users who can cause Certbot to run using + these credentials can complete a ``dns-01`` challenge to acquire new + certificates or revoke existing certificates for associated domains, even if + those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-nsone \\ + --dns-nsone-credentials ~/.secrets/certbot/nsone.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-nsone \\ + --dns-nsone-credentials ~/.secrets/certbot/nsone.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 60 seconds + for DNS propagation + + certbot certonly \\ + --dns-nsone \\ + --dns-nsone-credentials ~/.secrets/certbot/nsone.ini \\ + --dns-nsone-propagation-seconds 60 \\ + -d example.com + +""" diff --git a/certbot-dns-nsone/src/certbot_dns_nsone/_internal/__init__.py b/certbot-dns-nsone/src/certbot_dns_nsone/_internal/__init__.py new file mode 100644 index 00000000000..40a095edfd6 --- /dev/null +++ b/certbot-dns-nsone/src/certbot_dns_nsone/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_nsone.dns_nsone` plugin.""" diff --git a/certbot-dns-nsone/src/certbot_dns_nsone/_internal/dns_nsone.py b/certbot-dns-nsone/src/certbot_dns_nsone/_internal/dns_nsone.py new file mode 100644 index 00000000000..018a7f7c09b --- /dev/null +++ b/certbot-dns-nsone/src/certbot_dns_nsone/_internal/dns_nsone.py @@ -0,0 +1,55 @@ +"""DNS Authenticator for NS1 DNS.""" +import logging +from typing import Any +from typing import Callable +from typing import Optional + +from requests import HTTPError + +from certbot import errors +from certbot.plugins import dns_common_lexicon + +logger = logging.getLogger(__name__) + +ACCOUNT_URL = 'https://my.nsone.net/#/account/settings' + + +class Authenticator(dns_common_lexicon.LexiconDNSAuthenticator): + """ + DNS Authenticator for NS1 + This Authenticator uses the NS1 API to fulfill a dns-01 challenge. + """ + + description = 'Obtain certificates using a DNS TXT record (if you are using NS1 for DNS).' + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._add_provider_option('api-key', + f'API key for NS1 API, obtained from {ACCOUNT_URL}', + 'auth_token') + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 30) -> None: + super().add_parser_arguments(add, default_propagation_seconds) + add('credentials', help='NS1 credentials file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the NS1 API.' + + @property + def _provider_name(self) -> str: + return 'nsone' + + def _handle_http_error(self, e: HTTPError, domain_name: str) -> Optional[errors.PluginError]: + if domain_name in str(e) and (str(e).startswith('404 Client Error: Not Found for url:') or + str(e).startswith("400 Client Error: Bad Request for url:")): + return None # Expected errors when zone name guess is wrong + hint = None + if str(e).startswith('401 Client Error: Unauthorized for url:'): + hint = 'Is your API key correct?' + + hint_disp = f' ({hint})' if hint else '' + + return errors.PluginError(f'Error determining zone identifier: {e}.{hint_disp}') diff --git a/certbot-dns-nsone/src/certbot_dns_nsone/_internal/tests/__init__.py b/certbot-dns-nsone/src/certbot_dns_nsone/_internal/tests/__init__.py new file mode 100644 index 00000000000..08e5305f8fa --- /dev/null +++ b/certbot-dns-nsone/src/certbot_dns_nsone/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-nsone tests""" diff --git a/certbot-dns-nsone/src/certbot_dns_nsone/_internal/tests/dns_nsone_test.py b/certbot-dns-nsone/src/certbot_dns_nsone/_internal/tests/dns_nsone_test.py new file mode 100644 index 00000000000..3f6e7e7c79d --- /dev/null +++ b/certbot-dns-nsone/src/certbot_dns_nsone/_internal/tests/dns_nsone_test.py @@ -0,0 +1,39 @@ +"""Tests for certbot_dns_nsone._internal.dns_nsone.""" +import sys +from unittest import mock + +import pytest +from requests import Response +from requests.exceptions import HTTPError + +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins import dns_test_common_lexicon +from certbot.plugins.dns_test_common import DOMAIN +from certbot.tests import util as test_util + +API_KEY = 'foo' + + +class AuthenticatorTest(test_util.TempDirTestCase, + dns_test_common_lexicon.BaseLexiconDNSAuthenticatorTest): + + DOMAIN_NOT_FOUND = HTTPError(f'404 Client Error: Not Found for url: {DOMAIN}.', response=Response()) + LOGIN_ERROR = HTTPError(f'401 Client Error: Unauthorized for url: {DOMAIN}.', response=Response()) + + def setUp(self): + super().setUp() + + from certbot_dns_nsone._internal.dns_nsone import Authenticator + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write({"nsone_api_key": API_KEY}, path) + + self.config = mock.MagicMock(nsone_credentials=path, + nsone_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "nsone") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-nsone/src/certbot_dns_nsone/py.typed b/certbot-dns-nsone/src/certbot_dns_nsone/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-ovh/.readthedocs.yaml b/certbot-dns-ovh/.readthedocs.yaml new file mode 100644 index 00000000000..70884021eac --- /dev/null +++ b/certbot-dns-ovh/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-ovh[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-ovh/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-ovh/Dockerfile b/certbot-dns-ovh/Dockerfile deleted file mode 100644 index e8da96d952f..00000000000 --- a/certbot-dns-ovh/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-ovh - -RUN pip install --no-cache-dir --editable src/certbot-dns-ovh diff --git a/certbot-dns-ovh/MANIFEST.in b/certbot-dns-ovh/MANIFEST.in index 18f018c08d3..1f0b301c0e9 100644 --- a/certbot-dns-ovh/MANIFEST.in +++ b/certbot-dns-ovh/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_ovh/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-ovh/certbot_dns_ovh/__init__.py b/certbot-dns-ovh/certbot_dns_ovh/__init__.py deleted file mode 100644 index 47f8bda9f21..00000000000 --- a/certbot-dns-ovh/certbot_dns_ovh/__init__.py +++ /dev/null @@ -1,98 +0,0 @@ -""" -The `~certbot_dns_ovh.dns_ovh` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the OVH API. - - -Named Arguments ---------------- - -=================================== ========================================== -``--dns-ovh-credentials`` OVH credentials_ INI file. - (Required) -``--dns-ovh-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 30) -=================================== ========================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing OVH API -credentials for an account with the following access rules: - -* ``GET /domain/zone/*`` -* ``PUT /domain/zone/*`` -* ``POST /domain/zone/*`` -* ``DELETE /domain/zone/*`` - -These credentials can be obtained there: - -* `OVH Europe `_ (endpoint: ``ovh-eu``) -* `OVH North America `_ (endpoint: - ``ovh-ca``) - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # OVH API credentials used by Certbot - dns_ovh_endpoint = ovh-eu - dns_ovh_application_key = MDAwMDAwMDAwMDAw - dns_ovh_application_secret = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw - dns_ovh_consumer_key = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw - -The path to this file can be provided interactively or using the -``--dns-ovh-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - OVH account. Users who can read this file can use these credentials - to issue arbitrary API calls on your behalf. Users who can cause Certbot to - run using these credentials can complete a ``dns-01`` challenge to acquire - new certificates or revoke existing certificates for associated domains, - even if those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-ovh \\ - --dns-ovh-credentials ~/.secrets/certbot/ohv.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-ovh \\ - --dns-ovh-credentials ~/.secrets/certbot/ovh.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 60 seconds - for DNS propagation - - certbot certonly \\ - --dns-ovh \\ - --dns-ovh-credentials ~/.secrets/certbot/ovh.ini \\ - --dns-ovh-propagation-seconds 60 \\ - -d example.com - -""" diff --git a/certbot-dns-ovh/certbot_dns_ovh/dns_ovh.py b/certbot-dns-ovh/certbot_dns_ovh/dns_ovh.py deleted file mode 100644 index 84771b0a8a3..00000000000 --- a/certbot-dns-ovh/certbot_dns_ovh/dns_ovh.py +++ /dev/null @@ -1,105 +0,0 @@ -"""DNS Authenticator for OVH DNS.""" -import logging - -import zope.interface -from lexicon.providers import ovh - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common -from certbot.plugins import dns_common_lexicon - -logger = logging.getLogger(__name__) - -TOKEN_URL = 'https://eu.api.ovh.com/createToken/ or https://ca.api.ovh.com/createToken/' - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for OVH - - This Authenticator uses the OVH API to fulfill a dns-01 challenge. - """ - - description = 'Obtain certificates using a DNS TXT record (if you are using OVH for DNS).' - ttl = 60 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=30) - add('credentials', help='OVH credentials INI file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the OVH API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'OVH credentials INI file', - { - 'endpoint': 'OVH API endpoint (ovh-eu or ovh-ca)', - 'application-key': 'Application key for OVH API, obtained from {0}' - .format(TOKEN_URL), - 'application-secret': 'Application secret for OVH API, obtained from {0}' - .format(TOKEN_URL), - 'consumer-key': 'Consumer key for OVH API, obtained from {0}' - .format(TOKEN_URL), - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_ovh_client().add_txt_record(domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_ovh_client().del_txt_record(domain, validation_name, validation) - - def _get_ovh_client(self): - return _OVHLexiconClient( - self.credentials.conf('endpoint'), - self.credentials.conf('application-key'), - self.credentials.conf('application-secret'), - self.credentials.conf('consumer-key'), - self.ttl - ) - - -class _OVHLexiconClient(dns_common_lexicon.LexiconClient): - """ - Encapsulates all communication with the OVH API via Lexicon. - """ - - def __init__(self, endpoint, application_key, application_secret, consumer_key, ttl): - super(_OVHLexiconClient, self).__init__() - - config = dns_common_lexicon.build_lexicon_config('ovh', { - 'ttl': ttl, - }, { - 'auth_entrypoint': endpoint, - 'auth_application_key': application_key, - 'auth_application_secret': application_secret, - 'auth_consumer_key': consumer_key, - }) - - self.provider = ovh.Provider(config) - - def _handle_http_error(self, e, domain_name): - hint = None - if str(e).startswith('400 Client Error:'): - hint = 'Is your Application Secret value correct?' - if str(e).startswith('403 Client Error:'): - hint = 'Are your Application Key and Consumer Key values correct?' - - return errors.PluginError('Error determining zone identifier for {0}: {1}.{2}' - .format(domain_name, e, ' ({0})'.format(hint) if hint else '')) - - def _handle_general_error(self, e, domain_name): - if domain_name in str(e) and str(e).endswith('not found'): - return - - super(_OVHLexiconClient, self)._handle_general_error(e, domain_name) diff --git a/certbot-dns-ovh/certbot_dns_ovh/dns_ovh_test.py b/certbot-dns-ovh/certbot_dns_ovh/dns_ovh_test.py deleted file mode 100644 index f2a10485d3e..00000000000 --- a/certbot-dns-ovh/certbot_dns_ovh/dns_ovh_test.py +++ /dev/null @@ -1,62 +0,0 @@ -"""Tests for certbot_dns_ovh.dns_ovh.""" - -import os -import unittest - -import mock -from requests.exceptions import HTTPError - -from certbot.plugins import dns_test_common -from certbot.plugins import dns_test_common_lexicon -from certbot.tests import util as test_util - -ENDPOINT = 'ovh-eu' -APPLICATION_KEY = 'foo' -APPLICATION_SECRET = 'bar' -CONSUMER_KEY = 'spam' - - -class AuthenticatorTest(test_util.TempDirTestCase, - dns_test_common_lexicon.BaseLexiconAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_ovh.dns_ovh import Authenticator - - path = os.path.join(self.tempdir, 'file.ini') - credentials = { - "ovh_endpoint": ENDPOINT, - "ovh_application_key": APPLICATION_KEY, - "ovh_application_secret": APPLICATION_SECRET, - "ovh_consumer_key": CONSUMER_KEY, - } - dns_test_common.write(credentials, path) - - self.config = mock.MagicMock(ovh_credentials=path, - ovh_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "ovh") - - self.mock_client = mock.MagicMock() - # _get_ovh_client | pylint: disable=protected-access - self.auth._get_ovh_client = mock.MagicMock(return_value=self.mock_client) - - -class OVHLexiconClientTest(unittest.TestCase, dns_test_common_lexicon.BaseLexiconClientTest): - DOMAIN_NOT_FOUND = Exception('Domain example.com not found') - LOGIN_ERROR = HTTPError('403 Client Error: Forbidden for url: https://eu.api.ovh.com/1.0/...') - - def setUp(self): - from certbot_dns_ovh.dns_ovh import _OVHLexiconClient - - self.client = _OVHLexiconClient( - ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY, 0 - ) - - self.provider_mock = mock.MagicMock() - self.client.provider = self.provider_mock - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-ovh/docs/api.rst b/certbot-dns-ovh/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-ovh/docs/api.rst +++ b/certbot-dns-ovh/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-ovh/docs/api/dns_ovh.rst b/certbot-dns-ovh/docs/api/dns_ovh.rst deleted file mode 100644 index 79863d05fea..00000000000 --- a/certbot-dns-ovh/docs/api/dns_ovh.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_ovh.dns_ovh` ------------------------------- - -.. automodule:: certbot_dns_ovh.dns_ovh - :members: diff --git a/certbot-dns-ovh/docs/conf.py b/certbot-dns-ovh/docs/conf.py index 57194666ec5..85c78c4e665 100644 --- a/certbot-dns-ovh/docs/conf.py +++ b/certbot-dns-ovh/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-ovh/docs/make.bat b/certbot-dns-ovh/docs/make.bat index 78f7dd669bd..fad760e2f37 100644 --- a/certbot-dns-ovh/docs/make.bat +++ b/certbot-dns-ovh/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-ovh/local-oldest-requirements.txt b/certbot-dns-ovh/local-oldest-requirements.txt deleted file mode 100644 index 65f5a758ea0..00000000000 --- a/certbot-dns-ovh/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ --e acme[dev] --e .[dev] diff --git a/certbot-dns-ovh/pyproject.toml b/certbot-dns-ovh/pyproject.toml new file mode 100644 index 00000000000..9013eb5d3ce --- /dev/null +++ b/certbot-dns-ovh/pyproject.toml @@ -0,0 +1,55 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-ovh" +dynamic = ["version", "dependencies"] +description = "OVH DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-ovh = "certbot_dns_ovh._internal.dns_ovh:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] + diff --git a/certbot-dns-ovh/readthedocs.org.requirements.txt b/certbot-dns-ovh/readthedocs.org.requirements.txt deleted file mode 100644 index 0780e12a192..00000000000 --- a/certbot-dns-ovh/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-ovh[docs] diff --git a/certbot-dns-ovh/setup.cfg b/certbot-dns-ovh/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-ovh/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-ovh/setup.py b/certbot-dns-ovh/setup.py index 5b3329568a5..0f91af74249 100644 --- a/certbot-dns-ovh/setup.py +++ b/certbot-dns-ovh/setup.py @@ -1,65 +1,26 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.31.0.dev0', - 'certbot>=0.31.0.dev0', - 'dns-lexicon>=2.7.14', # Correct proxy use on OVH provider - 'mock', - 'setuptools', - 'zope.interface', + 'dns-lexicon>=3.15.1', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-ovh', version=version, - description="OVH DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-ovh = certbot_dns_ovh.dns_ovh:Authenticator', - ], - }, - test_suite='certbot_dns_ovh', ) + diff --git a/certbot-dns-ovh/src/certbot_dns_ovh/__init__.py b/certbot-dns-ovh/src/certbot_dns_ovh/__init__.py new file mode 100644 index 00000000000..42eaed9a0b5 --- /dev/null +++ b/certbot-dns-ovh/src/certbot_dns_ovh/__init__.py @@ -0,0 +1,110 @@ +""" +The `~certbot_dns_ovh.dns_ovh` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using the OVH API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +=================================== ========================================== +``--dns-ovh-credentials`` OVH credentials_ INI file. + (Required) +``--dns-ovh-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 30) +=================================== ========================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing OVH API +credentials for an account with the following access rules (allowing all domains): + +* ``GET /domain/zone/*`` +* ``PUT /domain/zone/*`` +* ``POST /domain/zone/*`` +* ``DELETE /domain/zone/*`` + +Alternatively, to allow a single domain only, the following access rules apply: + +* ``GET /domain/zone/`` +* ``GET /domain/zone//*`` +* ``PUT /domain/zone//*`` +* ``POST /domain/zone//*`` +* ``DELETE /domain/zone//*`` + +These credentials can be obtained at the following links: + +* `OVH Europe `_ (endpoint: ``ovh-eu``) +* `OVH North America `_ (endpoint: + ``ovh-ca``) + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # OVH API credentials used by Certbot + dns_ovh_endpoint = ovh-eu + dns_ovh_application_key = MDAwMDAwMDAwMDAw + dns_ovh_application_secret = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw + dns_ovh_consumer_key = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw + +The path to this file can be provided interactively or using the +``--dns-ovh-credentials`` command-line argument. Certbot records the path +to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + OVH account. Users who can read this file can use these credentials + to issue arbitrary API calls on your behalf. Users who can cause Certbot to + run using these credentials can complete a ``dns-01`` challenge to acquire + new certificates or revoke existing certificates for associated domains, + even if those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-ovh \\ + --dns-ovh-credentials ~/.secrets/certbot/ovh.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-ovh \\ + --dns-ovh-credentials ~/.secrets/certbot/ovh.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 60 seconds + for DNS propagation + + certbot certonly \\ + --dns-ovh \\ + --dns-ovh-credentials ~/.secrets/certbot/ovh.ini \\ + --dns-ovh-propagation-seconds 60 \\ + -d example.com + +""" diff --git a/certbot-dns-ovh/src/certbot_dns_ovh/_internal/__init__.py b/certbot-dns-ovh/src/certbot_dns_ovh/_internal/__init__.py new file mode 100644 index 00000000000..133694b9e25 --- /dev/null +++ b/certbot-dns-ovh/src/certbot_dns_ovh/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_ovh.dns_ovh` plugin.""" diff --git a/certbot-dns-ovh/src/certbot_dns_ovh/_internal/dns_ovh.py b/certbot-dns-ovh/src/certbot_dns_ovh/_internal/dns_ovh.py new file mode 100644 index 00000000000..42a953a4358 --- /dev/null +++ b/certbot-dns-ovh/src/certbot_dns_ovh/_internal/dns_ovh.py @@ -0,0 +1,70 @@ +"""DNS Authenticator for OVH DNS.""" +import logging +from typing import Any +from typing import Callable +from typing import Optional + +from requests import HTTPError + +from certbot import errors +from certbot.plugins import dns_common_lexicon + +logger = logging.getLogger(__name__) + +TOKEN_URL = 'https://eu.api.ovh.com/createToken/ or https://ca.api.ovh.com/createToken/' + + +class Authenticator(dns_common_lexicon.LexiconDNSAuthenticator): + """DNS Authenticator for OVH + + This Authenticator uses the OVH API to fulfill a dns-01 challenge. + """ + + description = 'Obtain certificates using a DNS TXT record (if you are using OVH for DNS).' + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._add_provider_option('endpoint', + 'OVH API endpoint (ovh-eu or ovh-ca)', + 'auth_entrypoint') + self._add_provider_option('application-key', + f'Application key for OVH API, obtained from {TOKEN_URL}', + 'auth_application_key') + self._add_provider_option('application-secret', + f'Application secret for OVH API, obtained from {TOKEN_URL}', + 'auth_application_secret') + self._add_provider_option('consumer-key', + f'Consumer key for OVH API, obtained from {TOKEN_URL}', + 'auth_consumer_key') + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 120) -> None: + super().add_parser_arguments(add, default_propagation_seconds) + add('credentials', help='OVH credentials INI file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the OVH API.' + + @property + def _provider_name(self) -> str: + return 'ovh' + + def _handle_http_error(self, e: HTTPError, domain_name: str) -> errors.PluginError: + hint = None + if str(e).startswith('400 Client Error:'): + hint = 'Is your Application Secret value correct?' + if str(e).startswith('403 Client Error:'): + hint = 'Are your Application Key and Consumer Key values correct?' + + hint_disp = f' ({hint})' if hint else '' + + return errors.PluginError(f'Error determining zone identifier for {domain_name}: ' + f'{e}.{hint_disp}') + + def _handle_general_error(self, e: Exception, domain_name: str) -> Optional[errors.PluginError]: + if domain_name in str(e) and str(e).endswith('not found'): + return None + + return super()._handle_general_error(e, domain_name) diff --git a/certbot-dns-ovh/src/certbot_dns_ovh/_internal/tests/__init__.py b/certbot-dns-ovh/src/certbot_dns_ovh/_internal/tests/__init__.py new file mode 100644 index 00000000000..9c378993d5b --- /dev/null +++ b/certbot-dns-ovh/src/certbot_dns_ovh/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-ovh tests""" diff --git a/certbot-dns-ovh/src/certbot_dns_ovh/_internal/tests/dns_ovh_test.py b/certbot-dns-ovh/src/certbot_dns_ovh/_internal/tests/dns_ovh_test.py new file mode 100644 index 00000000000..15ae728f234 --- /dev/null +++ b/certbot-dns-ovh/src/certbot_dns_ovh/_internal/tests/dns_ovh_test.py @@ -0,0 +1,47 @@ +"""Tests for certbot_dns_ovh._internal.dns_ovh.""" +from unittest import mock +import sys + +import pytest +from requests import Response +from requests.exceptions import HTTPError + +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins import dns_test_common_lexicon +from certbot.tests import util as test_util + +ENDPOINT = 'ovh-eu' +APPLICATION_KEY = 'foo' +APPLICATION_SECRET = 'bar' +CONSUMER_KEY = 'spam' + + +class AuthenticatorTest(test_util.TempDirTestCase, + dns_test_common_lexicon.BaseLexiconDNSAuthenticatorTest): + + DOMAIN_NOT_FOUND = Exception('Domain example.com not found') + LOGIN_ERROR = HTTPError('403 Client Error: Forbidden for url: https://eu.api.ovh.com/1.0/...', response=Response()) + + def setUp(self): + super().setUp() + + from certbot_dns_ovh._internal.dns_ovh import Authenticator + + path = os.path.join(self.tempdir, 'file.ini') + credentials = { + "ovh_endpoint": ENDPOINT, + "ovh_application_key": APPLICATION_KEY, + "ovh_application_secret": APPLICATION_SECRET, + "ovh_consumer_key": CONSUMER_KEY, + } + dns_test_common.write(credentials, path) + + self.config = mock.MagicMock(ovh_credentials=path, + ovh_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, 'ovh') + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-ovh/src/certbot_dns_ovh/py.typed b/certbot-dns-ovh/src/certbot_dns_ovh/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-rfc2136/.readthedocs.yaml b/certbot-dns-rfc2136/.readthedocs.yaml new file mode 100644 index 00000000000..6bb266daf88 --- /dev/null +++ b/certbot-dns-rfc2136/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-rfc2136[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-rfc2136/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-rfc2136/Dockerfile b/certbot-dns-rfc2136/Dockerfile deleted file mode 100644 index 1b8feb2f810..00000000000 --- a/certbot-dns-rfc2136/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-rfc2136 - -RUN pip install --no-cache-dir --editable src/certbot-dns-rfc2136 diff --git a/certbot-dns-rfc2136/MANIFEST.in b/certbot-dns-rfc2136/MANIFEST.in index 18f018c08d3..a86536d5923 100644 --- a/certbot-dns-rfc2136/MANIFEST.in +++ b/certbot-dns-rfc2136/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_rfc2136/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-rfc2136/certbot_dns_rfc2136/__init__.py b/certbot-dns-rfc2136/certbot_dns_rfc2136/__init__.py deleted file mode 100644 index 12b360959ff..00000000000 --- a/certbot-dns-rfc2136/certbot_dns_rfc2136/__init__.py +++ /dev/null @@ -1,139 +0,0 @@ -""" -The `~certbot_dns_rfc2136.dns_rfc2136` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using RFC 2136 Dynamic Updates. - - -Named Arguments ---------------- - -===================================== ===================================== -``--dns-rfc2136-credentials`` RFC 2136 credentials_ INI file. - (Required) -``--dns-rfc2136-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 60) -===================================== ===================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing the target DNS -server and optional port that supports RFC 2136 Dynamic Updates, the name -of the TSIG key, the TSIG key secret itself and the algorithm used if it's -different to HMAC-MD5. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # Target DNS server - dns_rfc2136_server = 192.0.2.1 - # Target DNS port - dns_rfc2136_port = 53 - # TSIG key name - dns_rfc2136_name = keyname. - # TSIG key secret - dns_rfc2136_secret = 4q4wM/2I180UXoMyN4INVhJNi8V9BCV+jMw2mXgZw/CSuxUT8C7NKKFs \ -AmKd7ak51vWKgSl12ib86oQRPkpDjg== - # TSIG key algorithm - dns_rfc2136_algorithm = HMAC-SHA512 - -The path to this file can be provided interactively or using the -``--dns-rfc2136-credentials`` command-line argument. Certbot records the -path to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect this TSIG key material as it can be used to potentially - add, update, or delete any record in the target DNS server. Users who can - read this file can use these credentials to issue arbitrary API calls on - your behalf. Users who can cause Certbot to run using these credentials can - complete a ``dns-01`` challenge to acquire new certificates or revoke - existing certificates for associated domains, even if those domains aren't - being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - -Sample BIND configuration -''''''''''''''''''''''''' - -Here's a sample BIND configuration for Certbot to use. You will need to -generate a new TSIG key, include it in the BIND configuration and grant it -permission to issue updates on the target DNS zone. - -.. code-block:: bash - :caption: Generate a new SHA512 TSIG key - - dnssec-keygen -a HMAC-SHA512 -b 512 -n HOST keyname. - -.. note:: - There are a few tools shipped with BIND that can all generate TSIG keys; - ``dnssec-keygen``, ``rndc-confgen``, and ``ddns-confgen``. Try and use the - most secure algorithm supported by your DNS server. - -.. code-block:: none - :caption: Sample BIND configuration - - key "keyname." { - algorithm hmac-sha512; - secret "4q4wM/2I180UXoMyN4INVhJNi8V9BCV+jMw2mXgZw/CSuxUT8C7NKKFs \ -AmKd7ak51vWKgSl12ib86oQRPkpDjg=="; - }; - - zone "example.com." IN { - type master; - file "named.example.com"; - update-policy { - grant keyname. name _acme-challenge.example.com. txt; - }; - }; - -.. note:: - This configuration limits the scope of the TSIG key to just be able to - add and remove TXT records for one specific host for the purpose of - completing the ``dns-01`` challenge. If your version of BIND doesn't - support the - `update-policy `_ - directive then you can use the less-secure - `allow-update `_ - directive instead. - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-rfc2136 \\ - --dns-rfc2136-credentials ~/.secrets/certbot/rfc2136.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-rfc2136 \\ - --dns-rfc2136-credentials ~/.secrets/certbot/rfc2136.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 30 seconds - for DNS propagation - - certbot certonly \\ - --dns-rfc2136 \\ - --dns-rfc2136-credentials ~/.secrets/certbot/rfc2136.ini \\ - --dns-rfc2136-propagation-seconds 30 \\ - -d example.com - -""" diff --git a/certbot-dns-rfc2136/certbot_dns_rfc2136/dns_rfc2136.py b/certbot-dns-rfc2136/certbot_dns_rfc2136/dns_rfc2136.py deleted file mode 100644 index b8c01cdd347..00000000000 --- a/certbot-dns-rfc2136/certbot_dns_rfc2136/dns_rfc2136.py +++ /dev/null @@ -1,223 +0,0 @@ -"""DNS Authenticator using RFC 2136 Dynamic Updates.""" -import logging - -import dns.flags -import dns.message -import dns.name -import dns.query -import dns.rdataclass -import dns.rdatatype -import dns.tsig -import dns.tsigkeyring -import dns.update -import zope.interface - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common - -logger = logging.getLogger(__name__) - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator using RFC 2136 Dynamic Updates - - This Authenticator uses RFC 2136 Dynamic Updates to fulfull a dns-01 challenge. - """ - - ALGORITHMS = { - 'HMAC-MD5': dns.tsig.HMAC_MD5, - 'HMAC-SHA1': dns.tsig.HMAC_SHA1, - 'HMAC-SHA224': dns.tsig.HMAC_SHA224, - 'HMAC-SHA256': dns.tsig.HMAC_SHA256, - 'HMAC-SHA384': dns.tsig.HMAC_SHA384, - 'HMAC-SHA512': dns.tsig.HMAC_SHA512 - } - - PORT = 53 - - description = 'Obtain certificates using a DNS TXT record (if you are using BIND for DNS).' - ttl = 120 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments(add, default_propagation_seconds=60) - add('credentials', help='RFC 2136 credentials INI file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'RFC 2136 Dynamic Updates.' - - def _validate_algorithm(self, credentials): - algorithm = credentials.conf('algorithm') - if algorithm: - if not self.ALGORITHMS.get(algorithm): - raise errors.PluginError("Unknown algorithm: {0}.".format(algorithm)) - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'RFC 2136 credentials INI file', - { - 'name': 'TSIG key name', - 'secret': 'TSIG key secret', - 'server': 'The target DNS server' - }, - self._validate_algorithm - ) - - def _perform(self, _domain, validation_name, validation): - self._get_rfc2136_client().add_txt_record(validation_name, validation, self.ttl) - - def _cleanup(self, _domain, validation_name, validation): - self._get_rfc2136_client().del_txt_record(validation_name, validation) - - def _get_rfc2136_client(self): - return _RFC2136Client(self.credentials.conf('server'), - int(self.credentials.conf('port') or self.PORT), - self.credentials.conf('name'), - self.credentials.conf('secret'), - self.ALGORITHMS.get(self.credentials.conf('algorithm'), - dns.tsig.HMAC_MD5)) - - -class _RFC2136Client(object): - """ - Encapsulates all communication with the target DNS server. - """ - def __init__(self, server, port, key_name, key_secret, key_algorithm): - self.server = server - self.port = port - self.keyring = dns.tsigkeyring.from_text({ - key_name: key_secret - }) - self.algorithm = key_algorithm - - def add_txt_record(self, record_name, record_content, record_ttl): - """ - Add a TXT record using the supplied information. - - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - :param int record_ttl: The record TTL (number of seconds that the record may be cached). - :raises certbot.errors.PluginError: if an error occurs communicating with the DNS server - """ - - domain = self._find_domain(record_name) - - n = dns.name.from_text(record_name) - o = dns.name.from_text(domain) - rel = n.relativize(o) - - update = dns.update.Update( - domain, - keyring=self.keyring, - keyalgorithm=self.algorithm) - update.add(rel, record_ttl, dns.rdatatype.TXT, record_content) - - try: - response = dns.query.tcp(update, self.server, port=self.port) - except Exception as e: - raise errors.PluginError('Encountered error adding TXT record: {0}' - .format(e)) - rcode = response.rcode() - - if rcode == dns.rcode.NOERROR: - logger.debug('Successfully added TXT record') - else: - raise errors.PluginError('Received response from server: {0}' - .format(dns.rcode.to_text(rcode))) - - def del_txt_record(self, record_name, record_content): - """ - Delete a TXT record using the supplied information. - - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - :param int record_ttl: The record TTL (number of seconds that the record may be cached). - :raises certbot.errors.PluginError: if an error occurs communicating with the DNS server - """ - - domain = self._find_domain(record_name) - - n = dns.name.from_text(record_name) - o = dns.name.from_text(domain) - rel = n.relativize(o) - - update = dns.update.Update( - domain, - keyring=self.keyring, - keyalgorithm=self.algorithm) - update.delete(rel, dns.rdatatype.TXT, record_content) - - try: - response = dns.query.tcp(update, self.server, port=self.port) - except Exception as e: - raise errors.PluginError('Encountered error deleting TXT record: {0}' - .format(e)) - rcode = response.rcode() - - if rcode == dns.rcode.NOERROR: - logger.debug('Successfully deleted TXT record') - else: - raise errors.PluginError('Received response from server: {0}' - .format(dns.rcode.to_text(rcode))) - - def _find_domain(self, record_name): - """ - Find the closest domain with an SOA record for a given domain name. - - :param str record_name: The record name for which to find the closest SOA record. - :returns: The domain, if found. - :rtype: str - :raises certbot.errors.PluginError: if no SOA record can be found. - """ - - domain_name_guesses = dns_common.base_domain_name_guesses(record_name) - - # Loop through until we find an authoritative SOA record - for guess in domain_name_guesses: - if self._query_soa(guess): - return guess - - raise errors.PluginError('Unable to determine base domain for {0} using names: {1}.' - .format(record_name, domain_name_guesses)) - - def _query_soa(self, domain_name): - """ - Query a domain name for an authoritative SOA record. - - :param str domain_name: The domain name to query for an SOA record. - :returns: True if found, False otherwise. - :rtype: bool - :raises certbot.errors.PluginError: if no response is received. - """ - - domain = dns.name.from_text(domain_name) - - request = dns.message.make_query(domain, dns.rdatatype.SOA, dns.rdataclass.IN) - # Turn off Recursion Desired bit in query - request.flags ^= dns.flags.RD - - try: - response = dns.query.udp(request, self.server, port=self.port) - rcode = response.rcode() - - # Authoritative Answer bit should be set - if (rcode == dns.rcode.NOERROR and response.get_rrset(response.answer, - domain, dns.rdataclass.IN, dns.rdatatype.SOA) and response.flags & dns.flags.AA): - logger.debug('Received authoritative SOA response for %s', domain_name) - return True - - logger.debug('No authoritative SOA record found for %s', domain_name) - return False - except Exception as e: - raise errors.PluginError('Encountered error when making query: {0}' - .format(e)) - diff --git a/certbot-dns-rfc2136/certbot_dns_rfc2136/dns_rfc2136_test.py b/certbot-dns-rfc2136/certbot_dns_rfc2136/dns_rfc2136_test.py deleted file mode 100644 index 89ce3d93ecb..00000000000 --- a/certbot-dns-rfc2136/certbot_dns_rfc2136/dns_rfc2136_test.py +++ /dev/null @@ -1,198 +0,0 @@ -"""Tests for certbot_dns_rfc2136.dns_rfc2136.""" - -import os -import unittest - -import dns.flags -import dns.rcode -import dns.tsig -import mock - -from certbot import errors -from certbot.plugins import dns_test_common -from certbot.plugins.dns_test_common import DOMAIN -from certbot.tests import util as test_util - -SERVER = '192.0.2.1' -PORT = 53 -NAME = 'a-tsig-key.' -SECRET = 'SSB3b25kZXIgd2hvIHdpbGwgYm90aGVyIHRvIGRlY29kZSB0aGlzIHRleHQK' -VALID_CONFIG = {"rfc2136_server": SERVER, "rfc2136_name": NAME, "rfc2136_secret": SECRET} - - -class AuthenticatorTest(test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): - - def setUp(self): - from certbot_dns_rfc2136.dns_rfc2136 import Authenticator - - super(AuthenticatorTest, self).setUp() - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write(VALID_CONFIG, path) - - self.config = mock.MagicMock(rfc2136_credentials=path, - rfc2136_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "rfc2136") - - self.mock_client = mock.MagicMock() - # _get_rfc2136_client | pylint: disable=protected-access - self.auth._get_rfc2136_client = mock.MagicMock(return_value=self.mock_client) - - def test_perform(self): - self.auth.perform([self.achall]) - - expected = [mock.call.add_txt_record('_acme-challenge.'+DOMAIN, mock.ANY, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - def test_cleanup(self): - # _attempt_cleanup | pylint: disable=protected-access - self.auth._attempt_cleanup = True - self.auth.cleanup([self.achall]) - - expected = [mock.call.del_txt_record('_acme-challenge.'+DOMAIN, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - def test_invalid_algorithm_raises(self): - config = VALID_CONFIG.copy() - config["rfc2136_algorithm"] = "INVALID" - dns_test_common.write(config, self.config.rfc2136_credentials) - - self.assertRaises(errors.PluginError, - self.auth.perform, - [self.achall]) - - def test_valid_algorithm_passes(self): - config = VALID_CONFIG.copy() - config["rfc2136_algorithm"] = "HMAC-SHA512" - dns_test_common.write(config, self.config.rfc2136_credentials) - - self.auth.perform([self.achall]) - - -class RFC2136ClientTest(unittest.TestCase): - - def setUp(self): - from certbot_dns_rfc2136.dns_rfc2136 import _RFC2136Client - - self.rfc2136_client = _RFC2136Client(SERVER, PORT, NAME, SECRET, dns.tsig.HMAC_MD5) - - @mock.patch("dns.query.tcp") - def test_add_txt_record(self, query_mock): - query_mock.return_value.rcode.return_value = dns.rcode.NOERROR - # _find_domain | pylint: disable=protected-access - self.rfc2136_client._find_domain = mock.MagicMock(return_value="example.com") - - self.rfc2136_client.add_txt_record("bar", "baz", 42) - - query_mock.assert_called_with(mock.ANY, SERVER, port=PORT) - self.assertTrue("bar. 42 IN TXT \"baz\"" in str(query_mock.call_args[0][0])) - - @mock.patch("dns.query.tcp") - def test_add_txt_record_wraps_errors(self, query_mock): - query_mock.side_effect = Exception - # _find_domain | pylint: disable=protected-access - self.rfc2136_client._find_domain = mock.MagicMock(return_value="example.com") - - self.assertRaises( - errors.PluginError, - self.rfc2136_client.add_txt_record, - "bar", "baz", 42) - - @mock.patch("dns.query.tcp") - def test_add_txt_record_server_error(self, query_mock): - query_mock.return_value.rcode.return_value = dns.rcode.NXDOMAIN - # _find_domain | pylint: disable=protected-access - self.rfc2136_client._find_domain = mock.MagicMock(return_value="example.com") - - self.assertRaises( - errors.PluginError, - self.rfc2136_client.add_txt_record, - "bar", "baz", 42) - - @mock.patch("dns.query.tcp") - def test_del_txt_record(self, query_mock): - query_mock.return_value.rcode.return_value = dns.rcode.NOERROR - # _find_domain | pylint: disable=protected-access - self.rfc2136_client._find_domain = mock.MagicMock(return_value="example.com") - - self.rfc2136_client.del_txt_record("bar", "baz") - - query_mock.assert_called_with(mock.ANY, SERVER, port=PORT) - self.assertTrue("bar. 0 NONE TXT \"baz\"" in str(query_mock.call_args[0][0])) - - @mock.patch("dns.query.tcp") - def test_del_txt_record_wraps_errors(self, query_mock): - query_mock.side_effect = Exception - # _find_domain | pylint: disable=protected-access - self.rfc2136_client._find_domain = mock.MagicMock(return_value="example.com") - - self.assertRaises( - errors.PluginError, - self.rfc2136_client.del_txt_record, - "bar", "baz") - - @mock.patch("dns.query.tcp") - def test_del_txt_record_server_error(self, query_mock): - query_mock.return_value.rcode.return_value = dns.rcode.NXDOMAIN - # _find_domain | pylint: disable=protected-access - self.rfc2136_client._find_domain = mock.MagicMock(return_value="example.com") - - self.assertRaises( - errors.PluginError, - self.rfc2136_client.del_txt_record, - "bar", "baz") - - def test_find_domain(self): - # _query_soa | pylint: disable=protected-access - self.rfc2136_client._query_soa = mock.MagicMock(side_effect=[False, False, True]) - - # _find_domain | pylint: disable=protected-access - domain = self.rfc2136_client._find_domain('foo.bar.'+DOMAIN) - - self.assertTrue(domain == DOMAIN) - - def test_find_domain_wraps_errors(self): - # _query_soa | pylint: disable=protected-access - self.rfc2136_client._query_soa = mock.MagicMock(return_value=False) - - self.assertRaises( - errors.PluginError, - # _find_domain | pylint: disable=protected-access - self.rfc2136_client._find_domain, - 'foo.bar.'+DOMAIN) - - @mock.patch("dns.query.udp") - def test_query_soa_found(self, query_mock): - query_mock.return_value = mock.MagicMock(answer=[mock.MagicMock()], flags=dns.flags.AA) - query_mock.return_value.rcode.return_value = dns.rcode.NOERROR - - # _query_soa | pylint: disable=protected-access - result = self.rfc2136_client._query_soa(DOMAIN) - - query_mock.assert_called_with(mock.ANY, SERVER, port=PORT) - self.assertTrue(result == True) - - @mock.patch("dns.query.udp") - def test_query_soa_not_found(self, query_mock): - query_mock.return_value.rcode.return_value = dns.rcode.NXDOMAIN - - # _query_soa | pylint: disable=protected-access - result = self.rfc2136_client._query_soa(DOMAIN) - - query_mock.assert_called_with(mock.ANY, SERVER, port=PORT) - self.assertTrue(result == False) - - @mock.patch("dns.query.udp") - def test_query_soa_wraps_errors(self, query_mock): - query_mock.side_effect = Exception - - self.assertRaises( - errors.PluginError, - # _query_soa | pylint: disable=protected-access - self.rfc2136_client._query_soa, - DOMAIN) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-rfc2136/docs/api.rst b/certbot-dns-rfc2136/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-rfc2136/docs/api.rst +++ b/certbot-dns-rfc2136/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-rfc2136/docs/api/dns_rfc2136.rst b/certbot-dns-rfc2136/docs/api/dns_rfc2136.rst deleted file mode 100644 index f5e98454a10..00000000000 --- a/certbot-dns-rfc2136/docs/api/dns_rfc2136.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_rfc2136.dns_rfc2136` --------------------------------------- - -.. automodule:: certbot_dns_rfc2136.dns_rfc2136 - :members: diff --git a/certbot-dns-rfc2136/docs/conf.py b/certbot-dns-rfc2136/docs/conf.py index 8cc5d595f52..3c68dc087cb 100644 --- a/certbot-dns-rfc2136/docs/conf.py +++ b/certbot-dns-rfc2136/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-rfc2136/docs/make.bat b/certbot-dns-rfc2136/docs/make.bat index 8d09ca8fd8b..a331545d9ff 100644 --- a/certbot-dns-rfc2136/docs/make.bat +++ b/certbot-dns-rfc2136/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-rfc2136/local-oldest-requirements.txt b/certbot-dns-rfc2136/local-oldest-requirements.txt deleted file mode 100644 index 8368d266ee7..00000000000 --- a/certbot-dns-rfc2136/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -acme[dev]==0.21.1 -certbot[dev]==0.21.1 diff --git a/certbot-dns-rfc2136/pyproject.toml b/certbot-dns-rfc2136/pyproject.toml new file mode 100644 index 00000000000..e367579a41d --- /dev/null +++ b/certbot-dns-rfc2136/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-rfc2136" +dynamic = ["version", "dependencies"] +description = "RFC 2136 DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-rfc2136 = "certbot_dns_rfc2136._internal.dns_rfc2136:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-rfc2136/readthedocs.org.requirements.txt b/certbot-dns-rfc2136/readthedocs.org.requirements.txt deleted file mode 100644 index df89018ceff..00000000000 --- a/certbot-dns-rfc2136/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-rfc2136[docs] diff --git a/certbot-dns-rfc2136/setup.cfg b/certbot-dns-rfc2136/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-rfc2136/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-rfc2136/setup.py b/certbot-dns-rfc2136/setup.py index edf7b6ba6b0..271c75fccbf 100644 --- a/certbot-dns-rfc2136/setup.py +++ b/certbot-dns-rfc2136/setup.py @@ -1,66 +1,29 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.21.1', - 'certbot>=0.21.1', - 'dnspython', - 'mock', - 'setuptools', - 'zope.interface', + # This version was chosen because it is the version packaged in RHEL 9 and Debian unstable. It + # is possible this requirement could be relaxed to allow for an even older version of dnspython + # if necessary. + 'dnspython>=2.6.1', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-rfc2136', version=version, - description="RFC 2136 DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-rfc2136 = certbot_dns_rfc2136.dns_rfc2136:Authenticator', - ], - }, - test_suite='certbot_dns_rfc2136', ) + diff --git a/certbot-dns-rfc2136/src/certbot_dns_rfc2136/__init__.py b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/__init__.py new file mode 100644 index 00000000000..48c0f8aeb06 --- /dev/null +++ b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/__init__.py @@ -0,0 +1,230 @@ +""" +The `~certbot_dns_rfc2136.dns_rfc2136` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using RFC 2136 Dynamic Updates. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +===================================== ===================================== +``--dns-rfc2136-credentials`` RFC 2136 credentials_ INI file. + (Required) +``--dns-rfc2136-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 60) +===================================== ===================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing the target DNS +server and optional port that supports RFC 2136 Dynamic Updates, the name +of the TSIG key, the TSIG key secret itself, the algorithm used if it's +different to HMAC-MD5, and optionally whether to sign the initial SOA query. + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # Target DNS server (IPv4 or IPv6 address, not a hostname) + dns_rfc2136_server = 192.0.2.1 + # Target DNS port + dns_rfc2136_port = 53 + # TSIG key name + dns_rfc2136_name = keyname. + # TSIG key secret + dns_rfc2136_secret = 4q4wM/2I180UXoMyN4INVhJNi8V9BCV+jMw2mXgZw/CSuxUT8C7NKKFs \ +AmKd7ak51vWKgSl12ib86oQRPkpDjg== + # TSIG key algorithm + dns_rfc2136_algorithm = HMAC-SHA512 + # TSIG sign SOA query (optional, default: false) + dns_rfc2136_sign_query = false + + +The path to this file can be provided interactively or using the +``--dns-rfc2136-credentials`` command-line argument. Certbot records the +path to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect this TSIG key material as it can be used to potentially + add, update, or delete any record in the target DNS server. Users who can + read this file can use these credentials to issue arbitrary API calls on + your behalf. Users who can cause Certbot to run using these credentials can + complete a ``dns-01`` challenge to acquire new certificates or revoke + existing certificates for associated domains, even if those domains aren't + being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-rfc2136 \\ + --dns-rfc2136-credentials ~/.secrets/certbot/rfc2136.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-rfc2136 \\ + --dns-rfc2136-credentials ~/.secrets/certbot/rfc2136.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 30 seconds + for DNS propagation + + certbot certonly \\ + --dns-rfc2136 \\ + --dns-rfc2136-credentials ~/.secrets/certbot/rfc2136.ini \\ + --dns-rfc2136-propagation-seconds 30 \\ + -d example.com + + +Sample BIND configuration +''''''''''''''''''''''''' + +Here's a sample BIND configuration for Certbot to use. You will need to +generate a new TSIG key, include it in the BIND configuration and grant it +permission to issue updates on the target DNS zone. + +.. code-block:: bash + :caption: Generate a new SHA512 TSIG key + + tsig-keygen -a HMAC-SHA512 keyname. + +.. note:: + Prior to BIND version 9.10.0, you will need to use ``dnssec-keygen`` to generate + TSIG keys. Try and use the most secure algorithm supported by your DNS server. + +.. code-block:: none + :caption: Sample BIND configuration + + key "keyname." { + algorithm hmac-sha512; + secret "4q4wM/2I180UXoMyN4INVhJNi8V9BCV+jMw2mXgZw/CSuxUT8C7NKKFs \ +AmKd7ak51vWKgSl12ib86oQRPkpDjg=="; + }; + + zone "example.com." IN { + type master; + file "named.example.com"; + update-policy { + grant keyname. name _acme-challenge.example.com. txt; + }; + }; + +.. note:: + This configuration limits the scope of the TSIG key to just be able to + add and remove TXT records for one specific host for the purpose of + completing the ``dns-01`` challenge. If your version of BIND doesn't + support the ``update-policy`` directive, then you can use the less-secure + ``allow-update`` directive instead. `See the BIND documentation + `_ + for details. + +Special considerations for multiple views in BIND +''''''''''''''''''''''''''''''''''''''''''''''''' + +If your BIND configuration leverages multiple views, Certbot may fail with an +``Unable to determine base domain for _acme-challenge.example.com`` error. +This error occurs when Certbot isn't able to communicate with an authoritative +nameserver for the zone, one that answers with the AA (Authoritative Answer) flag +set in the response. + +A common multiple view configuration with two views, external and internal, +can cause this error. If the zone is only present in the external view, and +the credentials_ ``dns_rfc2136_server`` setting is set (e.g. 127.0.0.1) so the +DNS server's ``match-clients`` view option causes the DNS server to route +Certbot's query to the internal view; the internal view doesn't contain the +zone, so the response won't have the AA flag set. + +One solution is to logically place the zone into the view Certbot is sending +queries to, with an +`in-view `_ +zone option. The zone will be then visible in both zones with exactly the same content. + +.. note:: + Order matters in BIND views: the ``in-view`` zone option must refer to a + view defined preceding it. It cannot refer to a view defined later in the configuration file. + +.. code-block:: none + :caption: Split-view BIND configuration + + key "keyname." { + algorithm hmac-sha512; + secret "4q4wM/2I180UXoMyN4INVhJNi8V9BCV+jMw2mXgZw/CSuxUT8C7NKKFs \ +AmKd7ak51vWKgSl12ib86oQRPkpDjg=="; + }; + + // adjust internal-addresses to suit your needs + acl internal-address { 127.0.0.0/8; 10.0.0.0/8; 192.168.0.0/16; 172.16.0.0/12; }; + + view "external" { + match-clients { !internal-addresses; any; }; + + zone "example.com." IN { + type master; + file "named.example.com"; + update-policy { + grant keyname. name _acme-challenge.example.com. txt; + }; + }; + }; + + view "internal" { + zone "example.com." IN { + in-view external; + }; + }; + +Another solution is to add `dns_rfc2136_sign_query = true` to the configuration file and then +add the key to the `match-clients` list within the external zone view. All queries signed with +this key should then be directed to this view, regardless of source IP. + +.. code-block:: none + :caption: Split-view BIND configuration with key-based ACLs + + key "keyname." { + algorithm hmac-sha512; + secret "4q4wM/2I180UXoMyN4INVhJNi8V9BCV+jMw2mXgZw/CSuxUT8C7NKKFs \ +AmKd7ak51vWKgSl12ib86oQRPkpDjg=="; + }; + + // adjust internal-addresses to suit your needs + acl internal-address { 127.0.0.0/8; 10.0.0.0/8; 192.168.0.0/16; 172.16.0.0/12; }; + + acl certbot-keys { key keyname.; } + + view "external" { + match-clients { acl certbot-keys; !internal-addresses; any; }; + + zone "example.com." IN { + type master; + file "named.example.com"; + update-policy { + grant keyname. name _acme-challenge.example.com. txt; + }; + }; + }; + +""" diff --git a/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/__init__.py b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/__init__.py new file mode 100644 index 00000000000..44894bb3538 --- /dev/null +++ b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_rfc2136.dns_rfc2136` plugin.""" diff --git a/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/dns_rfc2136.py b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/dns_rfc2136.py new file mode 100644 index 00000000000..43dd27746ae --- /dev/null +++ b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/dns_rfc2136.py @@ -0,0 +1,249 @@ +"""DNS Authenticator using RFC 2136 Dynamic Updates.""" +import logging +from typing import Any +from typing import Callable +from typing import cast +from typing import Optional + +import dns.flags +import dns.message +import dns.name +import dns.query +import dns.rdataclass +import dns.rdatatype +import dns.tsig +import dns.tsigkeyring +import dns.update + +from certbot import errors +from certbot.plugins import dns_common +from certbot.plugins.dns_common import CredentialsConfiguration +from certbot.util import is_ipaddress + +logger = logging.getLogger(__name__) + +DEFAULT_NETWORK_TIMEOUT = 45 + + +class Authenticator(dns_common.DNSAuthenticator): + """DNS Authenticator using RFC 2136 Dynamic Updates + + This Authenticator uses RFC 2136 Dynamic Updates to fulfill a dns-01 challenge. + """ + + ALGORITHMS = { + 'HMAC-MD5': dns.tsig.HMAC_MD5, + 'HMAC-SHA1': dns.tsig.HMAC_SHA1, + 'HMAC-SHA224': dns.tsig.HMAC_SHA224, + 'HMAC-SHA256': dns.tsig.HMAC_SHA256, + 'HMAC-SHA384': dns.tsig.HMAC_SHA384, + 'HMAC-SHA512': dns.tsig.HMAC_SHA512 + } + + PORT = 53 + + description = 'Obtain certificates using a DNS TXT record (if you are using BIND for DNS).' + ttl = 120 + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.credentials: Optional[CredentialsConfiguration] = None + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 60) -> None: + super().add_parser_arguments(add, default_propagation_seconds=60) + add('credentials', help='RFC 2136 credentials INI file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'RFC 2136 Dynamic Updates.' + + def _validate_credentials(self, credentials: CredentialsConfiguration) -> None: + server = cast(str, credentials.conf('server')) + if not is_ipaddress(server): + raise errors.PluginError("The configured target DNS server ({0}) is not a valid IPv4 " + "or IPv6 address. A hostname is not allowed.".format(server)) + algorithm = credentials.conf('algorithm') + if algorithm: + if not self.ALGORITHMS.get(algorithm.upper()): + raise errors.PluginError("Unknown algorithm: {0}.".format(algorithm)) + + def _setup_credentials(self) -> None: + self.credentials = self._configure_credentials( + 'credentials', + 'RFC 2136 credentials INI file', + { + 'name': 'TSIG key name', + 'secret': 'TSIG key secret', + 'server': 'The target DNS server' + }, + self._validate_credentials + ) + + def _perform(self, _domain: str, validation_name: str, validation: str) -> None: + self._get_rfc2136_client().add_txt_record(validation_name, validation, self.ttl) + + def _cleanup(self, _domain: str, validation_name: str, validation: str) -> None: + self._get_rfc2136_client().del_txt_record(validation_name, validation) + + def _get_rfc2136_client(self) -> "_RFC2136Client": + if not self.credentials: # pragma: no cover + raise errors.Error("Plugin has not been prepared.") + + algorithm: str = (self.credentials.conf('algorithm') or '').upper() + + return _RFC2136Client(cast(str, self.credentials.conf('server')), + int(cast(str, self.credentials.conf('port')) or self.PORT), + cast(str, self.credentials.conf('name')), + cast(str, self.credentials.conf('secret')), + self.ALGORITHMS.get(algorithm, dns.tsig.HMAC_MD5), + (self.credentials.conf('sign_query') or '').upper() == "TRUE") + + +class _RFC2136Client: + """ + Encapsulates all communication with the target DNS server. + """ + def __init__(self, server: str, port: int, key_name: str, key_secret: str, + key_algorithm: dns.name.Name, sign_query: bool, + timeout: int = DEFAULT_NETWORK_TIMEOUT) -> None: + self.server = server + self.port = port + self.keyring = dns.tsigkeyring.from_text({ + key_name: key_secret + }) + self.algorithm = key_algorithm + self.sign_query = sign_query + self._default_timeout = timeout + + def add_txt_record(self, record_name: str, record_content: str, record_ttl: int) -> None: + """ + Add a TXT record using the supplied information. + + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + :param int record_ttl: The record TTL (number of seconds that the record may be cached). + :raises certbot.errors.PluginError: if an error occurs communicating with the DNS server + """ + + domain = self._find_domain(record_name) + + n = dns.name.from_text(record_name) + o = dns.name.from_text(domain) + rel = n.relativize(o) + + update = dns.update.Update( + domain, + keyring=self.keyring, + keyalgorithm=self.algorithm) + update.add(rel, record_ttl, dns.rdatatype.TXT, record_content) + + try: + response = dns.query.tcp(update, self.server, self._default_timeout, self.port) + except Exception as e: + raise errors.PluginError('Encountered error adding TXT record: {0}' + .format(e)) + rcode = response.rcode() + + if rcode == dns.rcode.NOERROR: + logger.debug('Successfully added TXT record %s', record_name) + else: + raise errors.PluginError('Received response from server: {0}' + .format(dns.rcode.to_text(rcode))) + + def del_txt_record(self, record_name: str, record_content: str) -> None: + """ + Delete a TXT record using the supplied information. + + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + :param int record_ttl: The record TTL (number of seconds that the record may be cached). + :raises certbot.errors.PluginError: if an error occurs communicating with the DNS server + """ + + domain = self._find_domain(record_name) + + n = dns.name.from_text(record_name) + o = dns.name.from_text(domain) + rel = n.relativize(o) + + update = dns.update.Update( + domain, + keyring=self.keyring, + keyalgorithm=self.algorithm) + update.delete(rel, dns.rdatatype.TXT, record_content) + + try: + response = dns.query.tcp(update, self.server, self._default_timeout, self.port) + except Exception as e: + raise errors.PluginError('Encountered error deleting TXT record: {0}' + .format(e)) + rcode = response.rcode() + + if rcode == dns.rcode.NOERROR: + logger.debug('Successfully deleted TXT record %s', record_name) + else: + raise errors.PluginError('Received response from server: {0}' + .format(dns.rcode.to_text(rcode))) + + def _find_domain(self, record_name: str) -> str: + """ + Find the closest domain with an SOA record for a given domain name. + + :param str record_name: The record name for which to find the closest SOA record. + :returns: The domain, if found. + :rtype: str + :raises certbot.errors.PluginError: if no SOA record can be found. + """ + + domain_name_guesses = dns_common.base_domain_name_guesses(record_name) + + # Loop through until we find an authoritative SOA record + for guess in domain_name_guesses: + if self._query_soa(guess): + return guess + + raise errors.PluginError('Unable to determine base domain for {0} using names: {1}.' + .format(record_name, domain_name_guesses)) + + def _query_soa(self, domain_name: str) -> bool: + """ + Query a domain name for an authoritative SOA record. + + :param str domain_name: The domain name to query for an SOA record. + :returns: True if found, False otherwise. + :rtype: bool + :raises certbot.errors.PluginError: if no response is received. + """ + + domain = dns.name.from_text(domain_name) + + request = dns.message.make_query(domain, dns.rdatatype.SOA, dns.rdataclass.IN) + # Turn off Recursion Desired bit in query + request.flags ^= dns.flags.RD + # Use our TSIG keyring if configured + if self.sign_query: + request.use_tsig(self.keyring, algorithm=self.algorithm) + + try: + try: + response = dns.query.tcp(request, self.server, self._default_timeout, self.port) + except (OSError, dns.exception.Timeout) as e: + logger.debug('TCP query failed, fallback to UDP: %s', e) + response = dns.query.udp(request, self.server, self._default_timeout, self.port) + rcode = response.rcode() + + # Authoritative Answer bit should be set + if (rcode == dns.rcode.NOERROR + and response.get_rrset(response.answer, + domain, dns.rdataclass.IN, dns.rdatatype.SOA) + and response.flags & dns.flags.AA): + logger.debug('Received authoritative SOA response for %s', domain_name) + return True + + logger.debug('No authoritative SOA record found for %s', domain_name) + return False + except Exception as e: + raise errors.PluginError('Encountered error when making query: {0}' + .format(e)) diff --git a/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/tests/__init__.py b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/tests/__init__.py new file mode 100644 index 00000000000..cf898294232 --- /dev/null +++ b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-rfc2136 tests""" diff --git a/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/tests/dns_rfc2136_test.py b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/tests/dns_rfc2136_test.py new file mode 100644 index 00000000000..d7dbe750ead --- /dev/null +++ b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/_internal/tests/dns_rfc2136_test.py @@ -0,0 +1,272 @@ +"""Tests for certbot_dns_rfc2136._internal.dns_rfc2136.""" + +import sys +import unittest +from unittest import mock + +import dns.flags +import dns.rcode +import dns.tsig +import pytest + +from certbot import errors +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins.dns_test_common import DOMAIN +from certbot.tests import util as test_util + +SERVER = '192.0.2.1' +PORT = 53 +NAME = 'a-tsig-key.' +SECRET = 'SSB3b25kZXIgd2hvIHdpbGwgYm90aGVyIHRvIGRlY29kZSB0aGlzIHRleHQK' +VALID_CONFIG = {"rfc2136_server": SERVER, "rfc2136_name": NAME, "rfc2136_secret": SECRET} +TIMEOUT = 45 + + +class AuthenticatorTest(test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): + + def setUp(self): + from certbot_dns_rfc2136._internal.dns_rfc2136 import Authenticator + + super().setUp() + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write(VALID_CONFIG, path) + + self.config = mock.MagicMock(rfc2136_credentials=path, + rfc2136_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "rfc2136") + + self.mock_client = mock.MagicMock() + # _get_rfc2136_client | pylint: disable=protected-access + self.orig_get_client = self.auth._get_rfc2136_client + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.auth, '_get_rfc2136_client', mock.MagicMock(return_value=self.mock_client)) + + def test_get_client_default_conf_values(self): + # algorithm and sign_query are intentionally absent to test that the default (None) + # value does not crash Certbot. + creds = { "server": SERVER, "port": PORT, "name": NAME, "secret": SECRET } + self.auth.credentials = mock.MagicMock() + self.auth.credentials.conf = lambda key: creds.get(key, None) + client = self.orig_get_client() + assert client.algorithm == self.auth.ALGORITHMS["HMAC-MD5"] + assert client.sign_query is False + + @test_util.patch_display_util() + def test_perform(self, unused_mock_get_utility): + self.auth.perform([self.achall]) + + expected = [mock.call.add_txt_record('_acme-challenge.'+DOMAIN, mock.ANY, mock.ANY)] + assert expected == self.mock_client.mock_calls + + def test_cleanup(self): + # _attempt_cleanup | pylint: disable=protected-access + self.auth._attempt_cleanup = True + self.auth.cleanup([self.achall]) + + expected = [mock.call.del_txt_record('_acme-challenge.'+DOMAIN, mock.ANY)] + assert expected == self.mock_client.mock_calls + + def test_invalid_algorithm_raises(self): + config = VALID_CONFIG.copy() + config["rfc2136_algorithm"] = "INVALID" + dns_test_common.write(config, self.config.rfc2136_credentials) + + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + @test_util.patch_display_util() + @mock.patch('certbot_dns_rfc2136._internal.dns_rfc2136._RFC2136Client') + def test_valid_algorithm_passes(self, client, unused_mock_get_utility): + from certbot_dns_rfc2136._internal.dns_rfc2136 import Authenticator + + config = VALID_CONFIG.copy() + config["rfc2136_algorithm"] = "HMAC-sha512" + dns_test_common.write(config, self.config.rfc2136_credentials) + self.auth = Authenticator(self.config, "rfc2136") + + self.auth.perform([self.achall]) + assert dns.tsig.HMAC_SHA512 in client.call_args.args + + def test_invalid_server_raises(self): + config = VALID_CONFIG.copy() + config["rfc2136_server"] = "example.com" + dns_test_common.write(config, self.config.rfc2136_credentials) + + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + @test_util.patch_display_util() + def test_valid_server_passes(self, unused_mock_get_utility): + config = VALID_CONFIG.copy() + dns_test_common.write(config, self.config.rfc2136_credentials) + + self.auth.perform([self.achall]) + + config["rfc2136_server"] = "2001:db8:3333:4444:cccc:dddd:eeee:ffff" + dns_test_common.write(config, self.config.rfc2136_credentials) + + self.auth.perform([self.achall]) + + +class RFC2136ClientTest(unittest.TestCase): + + def setUp(self): + from certbot_dns_rfc2136._internal.dns_rfc2136 import _RFC2136Client + + self.rfc2136_client = _RFC2136Client(SERVER, PORT, NAME, SECRET, dns.tsig.HMAC_MD5, + False, TIMEOUT) + + @mock.patch("dns.query.tcp") + def test_add_txt_record(self, query_mock): + query_mock.return_value.rcode.return_value = dns.rcode.NOERROR + # _find_domain | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.rfc2136_client, '_find_domain', mock.MagicMock(return_value="example.com")) + + self.rfc2136_client.add_txt_record("bar", "baz", 42) + + query_mock.assert_called_with(mock.ANY, SERVER, TIMEOUT, PORT) + assert 'bar. 42 IN TXT "baz"' in str(query_mock.call_args[0][0]) + + @mock.patch("dns.query.tcp") + def test_add_txt_record_wraps_errors(self, query_mock): + query_mock.side_effect = Exception + # _find_domain | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.rfc2136_client, '_find_domain', mock.MagicMock(return_value="example.com")) + + with pytest.raises(errors.PluginError): + self.rfc2136_client.add_txt_record("bar", "baz", 42) + + @mock.patch("dns.query.tcp") + def test_add_txt_record_server_error(self, query_mock): + query_mock.return_value.rcode.return_value = dns.rcode.NXDOMAIN + # _find_domain | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.rfc2136_client, '_find_domain', mock.MagicMock(return_value="example.com")) + + with pytest.raises(errors.PluginError): + self.rfc2136_client.add_txt_record("bar", "baz", 42) + + @mock.patch("dns.query.tcp") + def test_del_txt_record(self, query_mock): + query_mock.return_value.rcode.return_value = dns.rcode.NOERROR + # _find_domain | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.rfc2136_client, '_find_domain', mock.MagicMock(return_value="example.com")) + + self.rfc2136_client.del_txt_record("bar", "baz") + + query_mock.assert_called_with(mock.ANY, SERVER, TIMEOUT, PORT) + assert 'bar. 0 NONE TXT "baz"' in str(query_mock.call_args[0][0]) + + @mock.patch("dns.query.tcp") + def test_del_txt_record_wraps_errors(self, query_mock): + query_mock.side_effect = Exception + # _find_domain | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.rfc2136_client, '_find_domain', mock.MagicMock(return_value="example.com")) + + with pytest.raises(errors.PluginError): + self.rfc2136_client.del_txt_record("bar", "baz") + + @mock.patch("dns.query.tcp") + def test_del_txt_record_server_error(self, query_mock): + query_mock.return_value.rcode.return_value = dns.rcode.NXDOMAIN + # _find_domain | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.rfc2136_client, '_find_domain', mock.MagicMock(return_value="example.com")) + + with pytest.raises(errors.PluginError): + self.rfc2136_client.del_txt_record("bar", "baz") + + def test_find_domain(self): + # _query_soa | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.rfc2136_client, '_query_soa', mock.MagicMock(side_effect=[False, False, True])) + + # _find_domain | pylint: disable=protected-access + domain = self.rfc2136_client._find_domain('foo.bar.'+DOMAIN) + + assert domain == DOMAIN + + def test_find_domain_wraps_errors(self): + # _query_soa | pylint: disable=protected-access + # workaround for wont-fix https://github.com/python/mypy/issues/2427 that works with + # both strict and non-strict mypy + setattr(self.rfc2136_client, '_query_soa', mock.MagicMock(return_value=False)) + + with pytest.raises(errors.PluginError): + self.rfc2136_client._find_domain('foo.bar.'+DOMAIN) + + @mock.patch("dns.query.tcp") + @mock.patch("dns.message.make_query") + def test_query_soa_found(self, mock_make_query, query_mock): + query_mock.return_value = mock.MagicMock(answer=[mock.MagicMock()], flags=dns.flags.AA) + query_mock.return_value.rcode.return_value = dns.rcode.NOERROR + mock_make_query.return_value = mock.MagicMock() + + # _query_soa | pylint: disable=protected-access + result = self.rfc2136_client._query_soa(DOMAIN) + + query_mock.assert_called_with(mock.ANY, SERVER, TIMEOUT, PORT) + mock_make_query.return_value.use_tsig.assert_not_called() + assert result + + @mock.patch("dns.query.tcp") + def test_query_soa_not_found(self, query_mock): + query_mock.return_value.rcode.return_value = dns.rcode.NXDOMAIN + + # _query_soa | pylint: disable=protected-access + result = self.rfc2136_client._query_soa(DOMAIN) + + query_mock.assert_called_with(mock.ANY, SERVER, TIMEOUT, PORT) + assert not result + + @mock.patch("dns.query.tcp") + def test_query_soa_wraps_errors(self, query_mock): + query_mock.side_effect = Exception + + with pytest.raises(errors.PluginError): + self.rfc2136_client._query_soa(DOMAIN) + + @mock.patch("dns.query.udp") + @mock.patch("dns.query.tcp") + def test_query_soa_fallback_to_udp(self, tcp_mock, udp_mock): + tcp_mock.side_effect = OSError + udp_mock.return_value = mock.MagicMock(answer=[mock.MagicMock()], flags=dns.flags.AA) + udp_mock.return_value.rcode.return_value = dns.rcode.NOERROR + + # _query_soa | pylint: disable=protected-access + result = self.rfc2136_client._query_soa(DOMAIN) + + tcp_mock.assert_called_with(mock.ANY, SERVER, TIMEOUT, PORT) + udp_mock.assert_called_with(mock.ANY, SERVER, TIMEOUT, PORT) + assert result + + @mock.patch("dns.query.tcp") + @mock.patch("dns.message.make_query") + def test_query_soa_signed(self, mock_make_query, unused_mock_query): + mock_make_query.return_value = mock.MagicMock() + self.rfc2136_client.sign_query = True + self.rfc2136_client.algorithm = dns.tsig.HMAC_MD5 + + self.rfc2136_client._query_soa(DOMAIN) + + mock_make_query.return_value.use_tsig.assert_called_with(mock.ANY, + algorithm=dns.tsig.HMAC_MD5) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-rfc2136/src/certbot_dns_rfc2136/py.typed b/certbot-dns-rfc2136/src/certbot_dns_rfc2136/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-route53/.gitignore b/certbot-dns-route53/.gitignore deleted file mode 100644 index 1dbc687de01..00000000000 --- a/certbot-dns-route53/.gitignore +++ /dev/null @@ -1,62 +0,0 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -*.egg-info/ -.installed.cfg -*.egg - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*,cover -.hypothesis/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -#Ipython Notebook -.ipynb_checkpoints diff --git a/certbot-dns-route53/.readthedocs.yaml b/certbot-dns-route53/.readthedocs.yaml new file mode 100644 index 00000000000..4a71ca8c3ce --- /dev/null +++ b/certbot-dns-route53/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-route53[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-route53/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-route53/Dockerfile b/certbot-dns-route53/Dockerfile deleted file mode 100644 index a1b8d6caf0d..00000000000 --- a/certbot-dns-route53/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-route53 - -RUN pip install --no-cache-dir --editable src/certbot-dns-route53 diff --git a/certbot-dns-route53/LICENSE b/certbot-dns-route53/LICENSE.txt similarity index 100% rename from certbot-dns-route53/LICENSE rename to certbot-dns-route53/LICENSE.txt diff --git a/certbot-dns-route53/MANIFEST.in b/certbot-dns-route53/MANIFEST.in index c48c07e592d..3011f3ac180 100644 --- a/certbot-dns-route53/MANIFEST.in +++ b/certbot-dns-route53/MANIFEST.in @@ -1,3 +1,6 @@ -include LICENSE -include README +include LICENSE.txt +include README.rst recursive-include docs * +include src/certbot_dns_route53/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-route53/README.md b/certbot-dns-route53/README.md deleted file mode 100644 index 4af66aa0068..00000000000 --- a/certbot-dns-route53/README.md +++ /dev/null @@ -1,35 +0,0 @@ -## Route53 plugin for Let's Encrypt client - -### Before you start - -It's expected that the root hosted zone for the domain in question already -exists in your account. - -### Setup - -1. Create a virtual environment - -2. Update its pip and setuptools (`VENV/bin/pip install -U setuptools pip`) -to avoid problems with cryptography's dependency on setuptools>=11.3. - -3. Make sure you have libssl-dev and libffi (or your regional equivalents) -installed. You might have to set compiler flags to pick things up (I have to -use `CPPFLAGS=-I/usr/local/opt/openssl/include -LDFLAGS=-L/usr/local/opt/openssl/lib` on my macOS to pick up brew's openssl, -for example). - -4. Install this package. - -### How to use it - -Make sure you have access to AWS's Route53 service, either through IAM roles or -via `.aws/credentials`. Check out -[sample-aws-policy.json](examples/sample-aws-policy.json) for the necessary permissions. - -To generate a certificate: -``` -certbot certonly \ - -n --agree-tos --email DEVOPS@COMPANY.COM \ - --dns-route53 \ - -d MY.DOMAIN.NAME -``` diff --git a/certbot-dns-route53/README.rst b/certbot-dns-route53/README.rst new file mode 100644 index 00000000000..cf63762ca91 --- /dev/null +++ b/certbot-dns-route53/README.rst @@ -0,0 +1 @@ +Amazon Web Services Route 53 DNS Authenticator plugin for Certbot diff --git a/certbot-dns-route53/certbot_dns_route53/__init__.py b/certbot-dns-route53/certbot_dns_route53/__init__.py deleted file mode 100644 index 8659617efad..00000000000 --- a/certbot-dns-route53/certbot_dns_route53/__init__.py +++ /dev/null @@ -1,120 +0,0 @@ -""" -The `~certbot_dns_route53.dns_route53` plugin automates the process of -completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and -subsequently removing, TXT records using the Amazon Web Services Route 53 API. - - -Named Arguments ---------------- - -======================================== ===================================== -``--dns-route53-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 10) -======================================== ===================================== - - -Credentials ------------ -Use of this plugin requires a configuration file containing Amazon Web Sevices -API credentials for an account with the following permissions: - -* ``route53:ListHostedZones`` -* ``route53:GetChange`` -* ``route53:ChangeResourceRecordSets`` - -These permissions can be captured in an AWS policy like the one below. Amazon -provides `information about managing access `_ and `information about -the required permissions `_ - -.. code-block:: json - :name: sample-aws-policy.json - :caption: Example AWS policy file: - - { - "Version": "2012-10-17", - "Id": "certbot-dns-route53 sample policy", - "Statement": [ - { - "Effect": "Allow", - "Action": [ - "route53:ListHostedZones", - "route53:GetChange" - ], - "Resource": [ - "*" - ] - }, - { - "Effect" : "Allow", - "Action" : [ - "route53:ChangeResourceRecordSets" - ], - "Resource" : [ - "arn:aws:route53:::hostedzone/YOURHOSTEDZONEID" - ] - } - ] - } - -The `access keys `_ for an account -with these permissions must be supplied in one of the following ways, which are -discussed in more detail in the Boto3 library's documentation about `configuring -credentials `_. - -* Using the ``AWS_ACCESS_KEY_ID`` and ``AWS_SECRET_ACCESS_KEY`` environment - variables. -* Using a credentials configuration file at the default location, - ``~/.aws/config``. -* Using a credentials configuration file at a path supplied using the - ``AWS_CONFIG_FILE`` environment variable. - -.. code-block:: ini - :name: config.ini - :caption: Example credentials config file: - - [default] - aws_access_key_id=AKIAIOSFODNN7EXAMPLE - aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY - -.. caution:: - You should protect these API credentials as you would a password. Users who - can read this file can use these credentials to issue some types of API calls - on your behalf, limited by the permissions assigned to the account. Users who - can cause Certbot to run using these credentials can complete a ``dns-01`` - challenge to acquire new certificates or revoke existing certificates for - domains these credentials are authorized to manage. - - -Examples --------- -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-route53 \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-route53 \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 30 seconds - for DNS propagation - - certbot certonly \\ - --dns-route53 \\ - --dns-route53-propagation-seconds 30 \\ - -d example.com -""" diff --git a/certbot-dns-route53/certbot_dns_route53/authenticator.py b/certbot-dns-route53/certbot_dns_route53/authenticator.py deleted file mode 100644 index 53215ea1d51..00000000000 --- a/certbot-dns-route53/certbot_dns_route53/authenticator.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Shim around `~certbot_dns_route53.dns_route53` for backwards compatibility.""" -import warnings - -import zope.interface - -from certbot import interfaces -from certbot_dns_route53 import dns_route53 - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_route53.Authenticator): - """Shim around `~certbot_dns_route53.dns_route53.Authenticator` for backwards compatibility.""" - - hidden = True - - def __init__(self, *args, **kwargs): - warnings.warn("The 'authenticator' module was renamed 'dns_route53'", - DeprecationWarning) - super(Authenticator, self).__init__(*args, **kwargs) diff --git a/certbot-dns-route53/certbot_dns_route53/dns_route53.py b/certbot-dns-route53/certbot_dns_route53/dns_route53.py deleted file mode 100644 index f71935de250..00000000000 --- a/certbot-dns-route53/certbot_dns_route53/dns_route53.py +++ /dev/null @@ -1,151 +0,0 @@ -"""Certbot Route53 authenticator plugin.""" -import collections -import logging -import time - -import boto3 -import zope.interface -from botocore.exceptions import NoCredentialsError, ClientError - -from certbot import errors -from certbot import interfaces -from certbot.plugins import dns_common - -from acme.magic_typing import DefaultDict, List, Dict # pylint: disable=unused-import, no-name-in-module - -logger = logging.getLogger(__name__) - -INSTRUCTIONS = ( - "To use certbot-dns-route53, configure credentials as described at " - "https://boto3.readthedocs.io/en/latest/guide/configuration.html#best-practices-for-configuring-credentials " # pylint: disable=line-too-long - "and add the necessary permissions for Route53 access.") - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """Route53 Authenticator - - This authenticator solves a DNS01 challenge by uploading the answer to AWS - Route53. - """ - - description = ("Obtain certificates using a DNS TXT record (if you are using AWS Route53 for " - "DNS).") - ttl = 10 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.r53 = boto3.client("route53") - self._resource_records = collections.defaultdict(list) # type: DefaultDict[str, List[Dict[str, str]]] - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return "Solve a DNS01 challenge using AWS Route53" - - def _setup_credentials(self): - pass - - def _perform(self, domain, validation_domain_name, validation): # pylint: disable=missing-docstring - pass - - def perform(self, achalls): - self._attempt_cleanup = True - - try: - change_ids = [ - self._change_txt_record("UPSERT", - achall.validation_domain_name(achall.domain), - achall.validation(achall.account_key)) - for achall in achalls - ] - - for change_id in change_ids: - self._wait_for_change(change_id) - except (NoCredentialsError, ClientError) as e: - logger.debug('Encountered error during perform: %s', e, exc_info=True) - raise errors.PluginError("\n".join([str(e), INSTRUCTIONS])) - return [achall.response(achall.account_key) for achall in achalls] - - def _cleanup(self, domain, validation_domain_name, validation): - try: - self._change_txt_record("DELETE", validation_domain_name, validation) - except (NoCredentialsError, ClientError) as e: - logger.debug('Encountered error during cleanup: %s', e, exc_info=True) - - def _find_zone_id_for_domain(self, domain): - """Find the zone id responsible a given FQDN. - - That is, the id for the zone whose name is the longest parent of the - domain. - """ - paginator = self.r53.get_paginator("list_hosted_zones") - zones = [] - target_labels = domain.rstrip(".").split(".") - for page in paginator.paginate(): - for zone in page["HostedZones"]: - if zone["Config"]["PrivateZone"]: - continue - - candidate_labels = zone["Name"].rstrip(".").split(".") - if candidate_labels == target_labels[-len(candidate_labels):]: - zones.append((zone["Name"], zone["Id"])) - - if not zones: - raise errors.PluginError( - "Unable to find a Route53 hosted zone for {0}".format(domain) - ) - - # Order the zones that are suffixes for our desired to domain by - # length, this puts them in an order like: - # ["foo.bar.baz.com", "bar.baz.com", "baz.com", "com"] - # And then we choose the first one, which will be the most specific. - zones.sort(key=lambda z: len(z[0]), reverse=True) - return zones[0][1] - - def _change_txt_record(self, action, validation_domain_name, validation): - zone_id = self._find_zone_id_for_domain(validation_domain_name) - - rrecords = self._resource_records[validation_domain_name] - challenge = {"Value": '"{0}"'.format(validation)} - if action == "DELETE": - # Remove the record being deleted from the list of tracked records - rrecords.remove(challenge) - if rrecords: - # Need to update instead, as we're not deleting the rrset - action = "UPSERT" - else: - # Create a new list containing the record to use with DELETE - rrecords = [challenge] - else: - rrecords.append(challenge) - - response = self.r53.change_resource_record_sets( - HostedZoneId=zone_id, - ChangeBatch={ - "Comment": "certbot-dns-route53 certificate validation " + action, - "Changes": [ - { - "Action": action, - "ResourceRecordSet": { - "Name": validation_domain_name, - "Type": "TXT", - "TTL": self.ttl, - "ResourceRecords": rrecords, - } - } - ] - } - ) - return response["ChangeInfo"]["Id"] - - def _wait_for_change(self, change_id): - """Wait for a change to be propagated to all Route53 DNS servers. - https://docs.aws.amazon.com/Route53/latest/APIReference/API_GetChange.html - """ - for unused_n in range(0, 120): - response = self.r53.get_change(Id=change_id) - if response["ChangeInfo"]["Status"] == "INSYNC": - return - time.sleep(5) - raise errors.PluginError( - "Timed out waiting for Route53 change. Current status: %s" % - response["ChangeInfo"]["Status"]) diff --git a/certbot-dns-route53/certbot_dns_route53/dns_route53_test.py b/certbot-dns-route53/certbot_dns_route53/dns_route53_test.py deleted file mode 100644 index 71326c2af9f..00000000000 --- a/certbot-dns-route53/certbot_dns_route53/dns_route53_test.py +++ /dev/null @@ -1,263 +0,0 @@ -"""Tests for certbot_dns_route53.dns_route53.Authenticator""" - -import os -import unittest - -import mock -from botocore.exceptions import NoCredentialsError, ClientError - -from certbot import errors -from certbot.plugins import dns_test_common -from certbot.plugins.dns_test_common import DOMAIN - - -class AuthenticatorTest(unittest.TestCase, dns_test_common.BaseAuthenticatorTest): - # pylint: disable=protected-access - - def setUp(self): - from certbot_dns_route53.dns_route53 import Authenticator - - super(AuthenticatorTest, self).setUp() - - self.config = mock.MagicMock() - - # Set up dummy credentials for testing - os.environ["AWS_ACCESS_KEY_ID"] = "dummy_access_key" - os.environ["AWS_SECRET_ACCESS_KEY"] = "dummy_secret_access_key" - - self.auth = Authenticator(self.config, "route53") - - def tearDown(self): - # Remove the dummy credentials from env vars - del os.environ["AWS_ACCESS_KEY_ID"] - del os.environ["AWS_SECRET_ACCESS_KEY"] - super(AuthenticatorTest, self).tearDown() - - def test_perform(self): - self.auth._change_txt_record = mock.MagicMock() - self.auth._wait_for_change = mock.MagicMock() - - self.auth.perform([self.achall]) - - self.auth._change_txt_record.assert_called_once_with("UPSERT", - '_acme-challenge.' + DOMAIN, - mock.ANY) - self.assertEqual(self.auth._wait_for_change.call_count, 1) - - def test_perform_no_credentials_error(self): - self.auth._change_txt_record = mock.MagicMock(side_effect=NoCredentialsError) - - self.assertRaises(errors.PluginError, - self.auth.perform, - [self.achall]) - - def test_perform_client_error(self): - self.auth._change_txt_record = mock.MagicMock( - side_effect=ClientError({"Error": {"Code": "foo"}}, "bar")) - - self.assertRaises(errors.PluginError, - self.auth.perform, - [self.achall]) - - def test_cleanup(self): - self.auth._attempt_cleanup = True - - self.auth._change_txt_record = mock.MagicMock() - - self.auth.cleanup([self.achall]) - - self.auth._change_txt_record.assert_called_once_with("DELETE", - '_acme-challenge.'+DOMAIN, - mock.ANY) - - def test_cleanup_no_credentials_error(self): - self.auth._attempt_cleanup = True - - self.auth._change_txt_record = mock.MagicMock(side_effect=NoCredentialsError) - - self.auth.cleanup([self.achall]) - - def test_cleanup_client_error(self): - self.auth._attempt_cleanup = True - - self.auth._change_txt_record = mock.MagicMock( - side_effect=ClientError({"Error": {"Code": "foo"}}, "bar")) - - self.auth.cleanup([self.achall]) - - -class ClientTest(unittest.TestCase): - # pylint: disable=protected-access - - PRIVATE_ZONE = { - "Id": "BAD-PRIVATE", - "Name": "example.com", - "Config": { - "PrivateZone": True - } - } - - EXAMPLE_NET_ZONE = { - "Id": "BAD-WRONG-TLD", - "Name": "example.net", - "Config": { - "PrivateZone": False - } - } - - EXAMPLE_COM_ZONE = { - "Id": "EXAMPLE", - "Name": "example.com", - "Config": { - "PrivateZone": False - } - } - - FOO_EXAMPLE_COM_ZONE = { - "Id": "FOO", - "Name": "foo.example.com", - "Config": { - "PrivateZone": False - } - } - - def setUp(self): - from certbot_dns_route53.dns_route53 import Authenticator - - super(ClientTest, self).setUp() - - self.config = mock.MagicMock() - - # Set up dummy credentials for testing - os.environ["AWS_ACCESS_KEY_ID"] = "dummy_access_key" - os.environ["AWS_SECRET_ACCESS_KEY"] = "dummy_secret_access_key" - - self.client = Authenticator(self.config, "route53") - - def tearDown(self): - # Remove the dummy credentials from env vars - del os.environ["AWS_ACCESS_KEY_ID"] - del os.environ["AWS_SECRET_ACCESS_KEY"] - super(ClientTest, self).tearDown() - - def test_find_zone_id_for_domain(self): - self.client.r53.get_paginator = mock.MagicMock() - self.client.r53.get_paginator().paginate.return_value = [ - { - "HostedZones": [ - self.EXAMPLE_NET_ZONE, - self.EXAMPLE_COM_ZONE, - ] - } - ] - - result = self.client._find_zone_id_for_domain("foo.example.com") - self.assertEqual(result, "EXAMPLE") - - def test_find_zone_id_for_domain_pagination(self): - self.client.r53.get_paginator = mock.MagicMock() - self.client.r53.get_paginator().paginate.return_value = [ - { - "HostedZones": [ - self.PRIVATE_ZONE, - self.EXAMPLE_COM_ZONE, - ] - }, - { - "HostedZones": [ - self.PRIVATE_ZONE, - self.FOO_EXAMPLE_COM_ZONE, - ] - } - ] - - result = self.client._find_zone_id_for_domain("foo.example.com") - self.assertEqual(result, "FOO") - - def test_find_zone_id_for_domain_no_results(self): - self.client.r53.get_paginator = mock.MagicMock() - self.client.r53.get_paginator().paginate.return_value = [] - - self.assertRaises(errors.PluginError, - self.client._find_zone_id_for_domain, - "foo.example.com") - - def test_find_zone_id_for_domain_no_correct_results(self): - self.client.r53.get_paginator = mock.MagicMock() - self.client.r53.get_paginator().paginate.return_value = [ - { - "HostedZones": [ - self.PRIVATE_ZONE, - self.EXAMPLE_NET_ZONE, - ] - }, - ] - - self.assertRaises(errors.PluginError, - self.client._find_zone_id_for_domain, - "foo.example.com") - - def test_change_txt_record(self): - self.client._find_zone_id_for_domain = mock.MagicMock() - self.client.r53.change_resource_record_sets = mock.MagicMock( - return_value={"ChangeInfo": {"Id": 1}}) - - self.client._change_txt_record("FOO", DOMAIN, "foo") - - call_count = self.client.r53.change_resource_record_sets.call_count - self.assertEqual(call_count, 1) - - def test_change_txt_record_delete(self): - self.client._find_zone_id_for_domain = mock.MagicMock() - self.client.r53.change_resource_record_sets = mock.MagicMock( - return_value={"ChangeInfo": {"Id": 1}}) - - validation = "some-value" - validation_record = {"Value": '"{0}"'.format(validation)} - self.client._resource_records[DOMAIN] = [validation_record] - - self.client._change_txt_record("DELETE", DOMAIN, validation) - - call_count = self.client.r53.change_resource_record_sets.call_count - self.assertEqual(call_count, 1) - call_args = self.client.r53.change_resource_record_sets.call_args_list[0][1] - call_args_batch = call_args["ChangeBatch"]["Changes"][0] - self.assertEqual(call_args_batch["Action"], "DELETE") - self.assertEqual( - call_args_batch["ResourceRecordSet"]["ResourceRecords"], - [validation_record]) - - def test_change_txt_record_multirecord(self): - self.client._find_zone_id_for_domain = mock.MagicMock() - self.client._get_validation_rrset = mock.MagicMock() - self.client._resource_records[DOMAIN] = [ - {"Value": "\"pre-existing-value\""}, - {"Value": "\"pre-existing-value-two\""}, - ] - self.client.r53.change_resource_record_sets = mock.MagicMock( - return_value={"ChangeInfo": {"Id": 1}}) - - self.client._change_txt_record("DELETE", DOMAIN, "pre-existing-value") - - call_count = self.client.r53.change_resource_record_sets.call_count - call_args = self.client.r53.change_resource_record_sets.call_args_list[0][1] - call_args_batch = call_args["ChangeBatch"]["Changes"][0] - self.assertEqual(call_args_batch["Action"], "UPSERT") - self.assertEqual( - call_args_batch["ResourceRecordSet"]["ResourceRecords"], - [{"Value": "\"pre-existing-value-two\""}]) - - self.assertEqual(call_count, 1) - - def test_wait_for_change(self): - self.client.r53.get_change = mock.MagicMock( - side_effect=[{"ChangeInfo": {"Status": "PENDING"}}, - {"ChangeInfo": {"Status": "INSYNC"}}]) - - self.client._wait_for_change(1) - - self.assertTrue(self.client.r53.get_change.called) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-route53/docs/api.rst b/certbot-dns-route53/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-route53/docs/api.rst +++ b/certbot-dns-route53/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-route53/docs/api/authenticator.rst b/certbot-dns-route53/docs/api/authenticator.rst deleted file mode 100644 index 2d96a419b19..00000000000 --- a/certbot-dns-route53/docs/api/authenticator.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_route53.authenticator` ----------------------------------------- - -.. automodule:: certbot_dns_route53.authenticator - :members: diff --git a/certbot-dns-route53/docs/api/dns_route53.rst b/certbot-dns-route53/docs/api/dns_route53.rst deleted file mode 100644 index 7573f2e199b..00000000000 --- a/certbot-dns-route53/docs/api/dns_route53.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_route53.dns_route53` --------------------------------------- - -.. automodule:: certbot_dns_route53.dns_route53 - :members: diff --git a/certbot-dns-route53/docs/conf.py b/certbot-dns-route53/docs/conf.py index 25a7c6e4d30..963c158599b 100644 --- a/certbot-dns-route53/docs/conf.py +++ b/certbot-dns-route53/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-route53/docs/make.bat b/certbot-dns-route53/docs/make.bat index e92b5909ac0..bbf80a5b1c0 100644 --- a/certbot-dns-route53/docs/make.bat +++ b/certbot-dns-route53/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-route53/examples/sample-aws-policy.json b/certbot-dns-route53/examples/sample-aws-policy.json index 10a17de19dc..04e8a659c8d 100644 --- a/certbot-dns-route53/examples/sample-aws-policy.json +++ b/certbot-dns-route53/examples/sample-aws-policy.json @@ -19,7 +19,19 @@ ], "Resource" : [ "arn:aws:route53:::hostedzone/YOURHOSTEDZONEID" - ] + ], + "Condition": { + "ForAllValues:StringLike": { + "route53:ChangeResourceRecordSetsNormalizedRecordNames": [ + "_acme-challenge.*" + ] + }, + "ForAllValues:StringEquals": { + "route53:ChangeResourceRecordSetsRecordTypes": [ + "TXT" + ] + } + } } ] } diff --git a/certbot-dns-route53/local-oldest-requirements.txt b/certbot-dns-route53/local-oldest-requirements.txt deleted file mode 100644 index 4e4aadbd8ae..00000000000 --- a/certbot-dns-route53/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -acme[dev]==0.25.0 -certbot[dev]==0.21.1 diff --git a/certbot-dns-route53/pyproject.toml b/certbot-dns-route53/pyproject.toml new file mode 100644 index 00000000000..de2c7f38ea2 --- /dev/null +++ b/certbot-dns-route53/pyproject.toml @@ -0,0 +1,60 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-route53" +dynamic = ["version", "dependencies"] +description = "Route53 DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +keywords = [ + "aws", + "certbot", + "route53", +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +"certbot-route53:auth" = "certbot_dns_route53._internal.dns_route53:HiddenAuthenticator" +dns-route53 = "certbot_dns_route53._internal.dns_route53:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-route53/readthedocs.org.requirements.txt b/certbot-dns-route53/readthedocs.org.requirements.txt deleted file mode 100644 index 660a90d0e9e..00000000000 --- a/certbot-dns-route53/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-route53[docs] diff --git a/certbot-dns-route53/setup.cfg b/certbot-dns-route53/setup.cfg deleted file mode 100644 index 3c6e79cf31d..00000000000 --- a/certbot-dns-route53/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal=1 diff --git a/certbot-dns-route53/setup.py b/certbot-dns-route53/setup.py index 69c2c7ed382..1893beaccd1 100644 --- a/certbot-dns-route53/setup.py +++ b/certbot-dns-route53/setup.py @@ -1,58 +1,25 @@ +import os + from setuptools import setup -from setuptools import find_packages -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.25.0', - 'certbot>=0.21.1', - 'boto3', - 'mock', - 'setuptools', - 'zope.interface', + 'boto3>=1.20.34', ] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) + setup( - name='certbot-dns-route53', version=version, - description="Route53 DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - keywords=['certbot', 'route53', 'aws'], - entry_points={ - 'certbot.plugins': [ - 'dns-route53 = certbot_dns_route53.dns_route53:Authenticator', - 'certbot-route53:auth = certbot_dns_route53.authenticator:Authenticator' - ], - }, - test_suite='certbot_dns_route53', ) diff --git a/certbot-dns-route53/src/certbot_dns_route53/__init__.py b/certbot-dns-route53/src/certbot_dns_route53/__init__.py new file mode 100644 index 00000000000..f328fbd46be --- /dev/null +++ b/certbot-dns-route53/src/certbot_dns_route53/__init__.py @@ -0,0 +1,105 @@ +""" +The `~certbot_dns_route53.dns_route53` plugin automates the process of +completing a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and +subsequently removing, TXT records using the Amazon Web Services Route 53 API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Credentials +----------- +Use of this plugin requires a configuration file containing Amazon Web Services +API credentials for an account with the following permissions: + +* ``route53:ListHostedZones`` +* ``route53:GetChange`` +* ``route53:ChangeResourceRecordSets`` + +These permissions can be captured in an AWS policy like the one below. Amazon +provides `information about managing access `_ and `information about +the required permissions `_ + +.. code-block:: json + :name: sample-aws-policy.json + :caption: Example AWS policy file: + + { + "Version": "2012-10-17", + "Id": "certbot-dns-route53 sample policy", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "route53:ListHostedZones", + "route53:GetChange" + ], + "Resource": [ + "*" + ] + }, + { + "Effect" : "Allow", + "Action" : [ + "route53:ChangeResourceRecordSets" + ], + "Resource" : [ + "arn:aws:route53:::hostedzone/YOURHOSTEDZONEID" + ] + } + ] + } + +The `access keys `_ for an account +with these permissions must be supplied in one of the following ways, which are +discussed in more detail in the Boto3 library's documentation about `configuring +credentials `_. + +* Using the ``AWS_ACCESS_KEY_ID`` and ``AWS_SECRET_ACCESS_KEY`` environment + variables. +* Using a credentials configuration file at the default location, + ``~/.aws/credentials``. If you're running on sudo, the credentials + will be picked up from the root home. +* Using a credentials configuration file at a path supplied using the + ``AWS_CONFIG_FILE`` environment variable. + +.. code-block:: ini + :name: config.ini + :caption: Example credentials config file: + + [default] + aws_access_key_id=AKIAIOSFODNN7EXAMPLE + aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY + +.. caution:: + You should protect these API credentials as you would a password. Users who + can read this file can use these credentials to issue some types of API calls + on your behalf, limited by the permissions assigned to the account. Users who + can cause Certbot to run using these credentials can complete a ``dns-01`` + challenge to acquire new certificates or revoke existing certificates for + domains these credentials are authorized to manage. + + +Examples +-------- +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-route53 \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-route53 \\ + -d example.com \\ + -d www.example.com +""" diff --git a/certbot-dns-route53/src/certbot_dns_route53/_internal/__init__.py b/certbot-dns-route53/src/certbot_dns_route53/_internal/__init__.py new file mode 100644 index 00000000000..ac9ead79103 --- /dev/null +++ b/certbot-dns-route53/src/certbot_dns_route53/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_route53.dns_route53` plugin.""" diff --git a/certbot-dns-route53/src/certbot_dns_route53/_internal/dns_route53.py b/certbot-dns-route53/src/certbot_dns_route53/_internal/dns_route53.py new file mode 100644 index 00000000000..8fa35a5e568 --- /dev/null +++ b/certbot-dns-route53/src/certbot_dns_route53/_internal/dns_route53.py @@ -0,0 +1,187 @@ +"""Certbot Route53 authenticator plugin.""" +import collections +import logging +import time +from typing import Any +from typing import Callable +from typing import Iterable +from typing import cast + +import boto3 +from botocore.exceptions import ClientError +from botocore.exceptions import NoCredentialsError + +from acme import challenges +from certbot import achallenges +from certbot import errors +from certbot import interfaces +from certbot.achallenges import AnnotatedChallenge +from certbot.plugins import common + +logger = logging.getLogger(__name__) + +INSTRUCTIONS = ( + "To use certbot-dns-route53, configure credentials as described at " + "https://boto3.readthedocs.io/en/latest/guide/configuration.html#best-practices-for-configuring-credentials " # pylint: disable=line-too-long + "and add the necessary permissions for Route53 access.") + + +class Authenticator(common.Plugin, interfaces.Authenticator): + """Route53 Authenticator + + This authenticator solves a DNS01 challenge by uploading the answer to AWS + Route53. + """ + + description = ("Obtain certificates using a DNS TXT record (if you are using AWS Route53 for " + "DNS).") + ttl = 10 + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.r53 = boto3.client("route53") + self._attempt_cleanup = False + self._resource_records: collections.defaultdict[str, list[dict[str, str]]] = \ + collections.defaultdict(list) + + def more_info(self) -> str: + return "Solve a DNS01 challenge using AWS Route53" + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None]) -> None: + # This authenticator currently adds no extra arguments. + pass + + def auth_hint(self, failed_achalls: list[achallenges.AnnotatedChallenge]) -> str: + return ( + 'The Certificate Authority failed to verify the DNS TXT records created by ' + '--dns-route53. Ensure the above domains have their DNS hosted by AWS Route53.' + ) + + def prepare(self) -> None: + pass + + def get_chall_pref(self, unused_identifier: str) -> Iterable[type[challenges.Challenge]]: + return [challenges.DNS01] + + def perform(self, achalls: list[AnnotatedChallenge]) -> list[challenges.ChallengeResponse]: + self._attempt_cleanup = True + + try: + change_ids = [ + self._change_txt_record("UPSERT", + achall.validation_domain_name(achall.identifier.value), + achall.validation(achall.account_key)) + for achall in achalls + ] + + for change_id in change_ids: + self._wait_for_change(change_id) + except (NoCredentialsError, ClientError) as e: + logger.debug('Encountered error during perform: %s', e, exc_info=True) + raise errors.PluginError("\n".join([str(e), INSTRUCTIONS])) + return [achall.response(achall.account_key) for achall in achalls] + + def cleanup(self, achalls: list[achallenges.AnnotatedChallenge]) -> None: + if self._attempt_cleanup: + for achall in achalls: + domain = achall.identifier.value + validation_domain_name = achall.validation_domain_name(domain) + validation = achall.validation(achall.account_key) + + self._cleanup(validation_domain_name, validation) + + def _cleanup(self, validation_name: str, validation: str) -> None: + try: + self._change_txt_record("DELETE", validation_name, validation) + except (NoCredentialsError, ClientError) as e: + logger.debug('Encountered error during cleanup: %s', e, exc_info=True) + + def _find_zone_id_for_domain(self, domain: str) -> str: + """Find the zone id responsible a given FQDN. + + That is, the id for the zone whose name is the longest parent of the + domain. + """ + paginator = self.r53.get_paginator("list_hosted_zones") + zones: list[tuple[str, str]] = [] + target_labels = domain.rstrip(".").split(".") + for page in paginator.paginate(): + for zone in page["HostedZones"]: + if zone["Config"]["PrivateZone"]: + continue + + candidate_labels = zone["Name"].rstrip(".").split(".") + if candidate_labels == target_labels[-len(candidate_labels):]: + zones.append((zone["Name"], zone["Id"])) + + if not zones: + raise errors.PluginError( + "Unable to find a Route53 hosted zone for {0}".format(domain) + ) + + # Order the zones that are suffixes for our desired to domain by + # length, this puts them in an order like: + # ["foo.bar.baz.com", "bar.baz.com", "baz.com", "com"] + # And then we choose the first one, which will be the most specific. + zones.sort(key=lambda z: len(z[0]), reverse=True) + return zones[0][1] + + def _change_txt_record(self, action: str, validation_domain_name: str, validation: str) -> str: + zone_id = self._find_zone_id_for_domain(validation_domain_name) + + rrecords = self._resource_records[validation_domain_name] + challenge = {"Value": '"{0}"'.format(validation)} + if action == "DELETE": + # Remove the record being deleted from the list of tracked records + rrecords.remove(challenge) + if rrecords: + # Need to update instead, as we're not deleting the rrset + action = "UPSERT" + else: + # Create a new list containing the record to use with DELETE + rrecords = [challenge] + else: + rrecords.append(challenge) + + response = self.r53.change_resource_record_sets( + HostedZoneId=zone_id, + ChangeBatch={ + "Comment": "certbot-dns-route53 certificate validation " + action, + "Changes": [ + { + "Action": action, + "ResourceRecordSet": { + "Name": validation_domain_name, + "Type": "TXT", + "TTL": self.ttl, + "ResourceRecords": rrecords, + } + } + ] + } + ) + return cast(str, response["ChangeInfo"]["Id"]) + + def _wait_for_change(self, change_id: str) -> None: + """Wait for a change to be propagated to all Route53 DNS servers. + https://docs.aws.amazon.com/Route53/latest/APIReference/API_GetChange.html + """ + for unused_n in range(0, 120): + response = self.r53.get_change(Id=change_id) + if response["ChangeInfo"]["Status"] == "INSYNC": + return + time.sleep(5) + raise errors.PluginError( + "Timed out waiting for Route53 change. Current status: %s" % + response["ChangeInfo"]["Status"]) + + +# Our route53 plugin was initially a 3rd party plugin named `certbot-route53:auth` as described at +# https://github.com/certbot/certbot/issues/4688. This shim exists to allow installations using the +# old plugin name of `certbot-route53:auth` to continue to work without cluttering things like +# Certbot's help output with two route53 plugins. +class HiddenAuthenticator(Authenticator): + """A hidden shim around certbot-dns-route53 for backwards compatibility.""" + + hidden = True diff --git a/certbot-dns-route53/src/certbot_dns_route53/_internal/tests/__init__.py b/certbot-dns-route53/src/certbot_dns_route53/_internal/tests/__init__.py new file mode 100644 index 00000000000..a0ae87f1c3b --- /dev/null +++ b/certbot-dns-route53/src/certbot_dns_route53/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-route53 tests""" diff --git a/certbot-dns-route53/src/certbot_dns_route53/_internal/tests/conftest.py b/certbot-dns-route53/src/certbot_dns_route53/_internal/tests/conftest.py new file mode 100644 index 00000000000..1cddad61316 --- /dev/null +++ b/certbot-dns-route53/src/certbot_dns_route53/_internal/tests/conftest.py @@ -0,0 +1,8 @@ +from unittest import mock + +import pytest + +@pytest.fixture(autouse=True) +def mock_sleep(): + with mock.patch("time.sleep") as mocked: + yield mocked diff --git a/certbot-dns-route53/src/certbot_dns_route53/_internal/tests/dns_route53_test.py b/certbot-dns-route53/src/certbot_dns_route53/_internal/tests/dns_route53_test.py new file mode 100644 index 00000000000..c3530af032d --- /dev/null +++ b/certbot-dns-route53/src/certbot_dns_route53/_internal/tests/dns_route53_test.py @@ -0,0 +1,274 @@ +"""Tests for certbot_dns_route53._internal.dns_route53.Authenticator""" + +import sys +import unittest +from unittest import mock + +from botocore.exceptions import ClientError +from botocore.exceptions import NoCredentialsError +import josepy as jose +import pytest + +from acme import challenges, messages +from certbot import achallenges +from certbot import errors +from certbot.compat import os +from certbot.plugins.dns_test_common import DOMAIN +from certbot.tests import acme_util +from certbot.tests import util as test_util + +KEY = jose.jwk.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) + + +class AuthenticatorTest(unittest.TestCase): + # pylint: disable=protected-access + + achall = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.DNS01, + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value=DOMAIN), + account_key=KEY) + + def setUp(self): + from certbot_dns_route53._internal.dns_route53 import Authenticator + + super().setUp() + + self.config = mock.MagicMock() + + # Set up dummy credentials for testing + os.environ["AWS_ACCESS_KEY_ID"] = "dummy_access_key" + os.environ["AWS_SECRET_ACCESS_KEY"] = "dummy_secret_access_key" + + self.auth = Authenticator(self.config, "route53") + + def tearDown(self): + # Remove the dummy credentials from env vars + del os.environ["AWS_ACCESS_KEY_ID"] + del os.environ["AWS_SECRET_ACCESS_KEY"] + + def test_more_info(self) -> None: + self.assertTrue(isinstance(self.auth.more_info(), str)) + + def test_get_chall_pref(self) -> None: + self.assertEqual(self.auth.get_chall_pref("example.org"), [challenges.DNS01]) + + def test_perform(self): + self.auth._change_txt_record = mock.MagicMock() # type: ignore[method-assign, unused-ignore] + self.auth._wait_for_change = mock.MagicMock() # type: ignore [method-assign, unused-ignore] + + self.auth.perform([self.achall]) + + self.auth._change_txt_record.assert_called_once_with("UPSERT", + '_acme-challenge.' + DOMAIN, + mock.ANY) + assert self.auth._wait_for_change.call_count == 1 + + def test_perform_no_credentials_error(self): + self.auth._change_txt_record = mock.MagicMock( # type: ignore [method-assign, unused-ignore] + side_effect=NoCredentialsError) + + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + def test_perform_client_error(self): + self.auth._change_txt_record = mock.MagicMock( # type: ignore [method-assign, unused-ignore] + side_effect=ClientError({"Error": {"Code": "foo"}}, "bar")) + + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + def test_cleanup(self): + self.auth._attempt_cleanup = True + + self.auth._change_txt_record = mock.MagicMock() # type: ignore[method-assign, unused-ignore] + + self.auth.cleanup([self.achall]) + + self.auth._change_txt_record.assert_called_once_with("DELETE", + '_acme-challenge.'+DOMAIN, + mock.ANY) + + def test_cleanup_no_credentials_error(self): + self.auth._attempt_cleanup = True + + self.auth._change_txt_record = mock.MagicMock( # type: ignore [method-assign, unused-ignore] + side_effect=NoCredentialsError) + + self.auth.cleanup([self.achall]) + + def test_cleanup_client_error(self): + self.auth._attempt_cleanup = True + + self.auth._change_txt_record = mock.MagicMock( # type: ignore [method-assign, unused-ignore] + side_effect=ClientError({"Error": {"Code": "foo"}}, "bar")) + + self.auth.cleanup([self.achall]) + + +class ClientTest(unittest.TestCase): + # pylint: disable=protected-access + + PRIVATE_ZONE = { + "Id": "BAD-PRIVATE", + "Name": "example.com", + "Config": { + "PrivateZone": True + } + } + + EXAMPLE_NET_ZONE = { + "Id": "BAD-WRONG-TLD", + "Name": "example.net", + "Config": { + "PrivateZone": False + } + } + + EXAMPLE_COM_ZONE = { + "Id": "EXAMPLE", + "Name": "example.com", + "Config": { + "PrivateZone": False + } + } + + FOO_EXAMPLE_COM_ZONE = { + "Id": "FOO", + "Name": "foo.example.com", + "Config": { + "PrivateZone": False + } + } + + def setUp(self): + from certbot_dns_route53._internal.dns_route53 import Authenticator + + self.config = mock.MagicMock() + + # Set up dummy credentials for testing + os.environ["AWS_ACCESS_KEY_ID"] = "dummy_access_key" + os.environ["AWS_SECRET_ACCESS_KEY"] = "dummy_secret_access_key" + + self.client = Authenticator(self.config, "route53") + + def tearDown(self): + # Remove the dummy credentials from env vars + del os.environ["AWS_ACCESS_KEY_ID"] + del os.environ["AWS_SECRET_ACCESS_KEY"] + + def test_find_zone_id_for_domain(self): + self.client.r53.get_paginator = mock.MagicMock() + self.client.r53.get_paginator().paginate.return_value = [ + { + "HostedZones": [ + self.EXAMPLE_NET_ZONE, + self.EXAMPLE_COM_ZONE, + ] + } + ] + + result = self.client._find_zone_id_for_domain("foo.example.com") + assert result == "EXAMPLE" + + def test_find_zone_id_for_domain_pagination(self): + self.client.r53.get_paginator = mock.MagicMock() + self.client.r53.get_paginator().paginate.return_value = [ + { + "HostedZones": [ + self.PRIVATE_ZONE, + self.EXAMPLE_COM_ZONE, + ] + }, + { + "HostedZones": [ + self.PRIVATE_ZONE, + self.FOO_EXAMPLE_COM_ZONE, + ] + } + ] + + result = self.client._find_zone_id_for_domain("foo.example.com") + assert result == "FOO" + + def test_find_zone_id_for_domain_no_results(self): + self.client.r53.get_paginator = mock.MagicMock() + self.client.r53.get_paginator().paginate.return_value = [] + + with pytest.raises(errors.PluginError): + self.client._find_zone_id_for_domain("foo.example.com") + + def test_find_zone_id_for_domain_no_correct_results(self): + self.client.r53.get_paginator = mock.MagicMock() + self.client.r53.get_paginator().paginate.return_value = [ + { + "HostedZones": [ + self.PRIVATE_ZONE, + self.EXAMPLE_NET_ZONE, + ] + }, + ] + + with pytest.raises(errors.PluginError): + self.client._find_zone_id_for_domain("foo.example.com") + + def test_change_txt_record(self): + self.client._find_zone_id_for_domain = mock.MagicMock() # type: ignore [method-assign, unused-ignore] + self.client.r53.change_resource_record_sets = mock.MagicMock( + return_value={"ChangeInfo": {"Id": 1}}) + + self.client._change_txt_record("FOO", DOMAIN, "foo") + + call_count = self.client.r53.change_resource_record_sets.call_count + assert call_count == 1 + + def test_change_txt_record_delete(self): + self.client._find_zone_id_for_domain = mock.MagicMock() # type: ignore[ method-assign, unused-ignore] + self.client.r53.change_resource_record_sets = mock.MagicMock( + return_value={"ChangeInfo": {"Id": 1}}) + + validation = "some-value" + validation_record = {"Value": '"{0}"'.format(validation)} + self.client._resource_records[DOMAIN] = [validation_record] + + self.client._change_txt_record("DELETE", DOMAIN, validation) + + call_count = self.client.r53.change_resource_record_sets.call_count + assert call_count == 1 + call_args = self.client.r53.change_resource_record_sets.call_args_list[0][1] + call_args_batch = call_args["ChangeBatch"]["Changes"][0] + assert call_args_batch["Action"] == "DELETE" + assert call_args_batch["ResourceRecordSet"]["ResourceRecords"] == \ + [validation_record] + + def test_change_txt_record_multirecord(self): + self.client._find_zone_id_for_domain = mock.MagicMock() # type: ignore [method-assign, unused-ignore] + self.client._resource_records[DOMAIN] = [ + {"Value": "\"pre-existing-value\""}, + {"Value": "\"pre-existing-value-two\""}, + ] + self.client.r53.change_resource_record_sets = mock.MagicMock( + return_value={"ChangeInfo": {"Id": 1}}) + + self.client._change_txt_record("DELETE", DOMAIN, "pre-existing-value") + + call_count = self.client.r53.change_resource_record_sets.call_count + call_args = self.client.r53.change_resource_record_sets.call_args_list[0][1] + call_args_batch = call_args["ChangeBatch"]["Changes"][0] + assert call_args_batch["Action"] == "UPSERT" + assert call_args_batch["ResourceRecordSet"]["ResourceRecords"] == \ + [{"Value": "\"pre-existing-value-two\""}] + + assert call_count == 1 + + def test_wait_for_change(self): + self.client.r53.get_change = mock.MagicMock( + side_effect=[{"ChangeInfo": {"Status": "PENDING"}}, + {"ChangeInfo": {"Status": "INSYNC"}}]) + + self.client._wait_for_change("1") + + assert self.client.r53.get_change.called + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-route53/src/certbot_dns_route53/py.typed b/certbot-dns-route53/src/certbot_dns_route53/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-dns-route53/tools/tester.pkoch-macos_sierra.sh b/certbot-dns-route53/tools/tester.pkoch-macos_sierra.sh deleted file mode 100755 index dbbaa2251d0..00000000000 --- a/certbot-dns-route53/tools/tester.pkoch-macos_sierra.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -# I just wanted a place to dump the incantations I use for testing. -set -e - -brew install openssl libffi - -rm -rf scratch; mkdir scratch - -virtualenv scratch/venv -p /usr/local/bin/python2.7 -scratch/venv/bin/pip install -U pip setuptools - -CPPFLAGS=-I/usr/local/opt/openssl/include LDFLAGS=-L/usr/local/opt/openssl/lib scratch/venv/bin/pip install -e . - -scratch/venv/bin/certbot certonly -n --agree-tos --test-cert --email pkoch@lifeonmars.pt -a certbot-route53:auth -d pkoch.lifeonmars.pt --work-dir scratch --config-dir scratch --logs-dir scratch - -rm -rf scratch diff --git a/certbot-dns-sakuracloud/.readthedocs.yaml b/certbot-dns-sakuracloud/.readthedocs.yaml new file mode 100644 index 00000000000..bb0914d586e --- /dev/null +++ b/certbot-dns-sakuracloud/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot -e certbot-dns-sakuracloud[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot-dns-sakuracloud/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot-dns-sakuracloud/Dockerfile b/certbot-dns-sakuracloud/Dockerfile deleted file mode 100644 index 694773f61ba..00000000000 --- a/certbot-dns-sakuracloud/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM certbot/certbot - -COPY . src/certbot-dns-sakuracloud - -RUN pip install --no-cache-dir --editable src/certbot-dns-sakuracloud diff --git a/certbot-dns-sakuracloud/MANIFEST.in b/certbot-dns-sakuracloud/MANIFEST.in index 18f018c08d3..7ce321fe339 100644 --- a/certbot-dns-sakuracloud/MANIFEST.in +++ b/certbot-dns-sakuracloud/MANIFEST.in @@ -1,3 +1,6 @@ include LICENSE.txt include README.rst recursive-include docs * +include src/certbot_dns_sakuracloud/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-dns-sakuracloud/certbot_dns_sakuracloud/__init__.py b/certbot-dns-sakuracloud/certbot_dns_sakuracloud/__init__.py deleted file mode 100644 index f18780c1846..00000000000 --- a/certbot-dns-sakuracloud/certbot_dns_sakuracloud/__init__.py +++ /dev/null @@ -1,86 +0,0 @@ -""" -The `~certbot_dns_sakuracloud.dns_sakuracloud` plugin automates the process of completing -a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and subsequently -removing, TXT records using the Sakura Cloud DNS API. - - -Named Arguments ---------------- - -========================================== ====================================== -``--dns-sakuracloud-credentials`` Sakura Cloud credentials_ INI file. - (Required) -``--dns-sakuracloud-propagation-seconds`` The number of seconds to wait for DNS - to propagate before asking the ACME - server to verify the DNS record. - (Default: 90) -========================================== ====================================== - - -Credentials ------------ - -Use of this plugin requires a configuration file containing -Sakura Cloud DNS API credentials, obtained from your Sakura Cloud DNS -`apikey page `_. - -.. code-block:: ini - :name: credentials.ini - :caption: Example credentials file: - - # Sakura Cloud API credentials used by Certbot - dns_sakuracloud_api_token = 00000000-0000-0000-0000-000000000000 - dns_sakuracloud_api_secret = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw - -The path to this file can be provided interactively or using the -``--dns-sakuracloud-credentials`` command-line argument. Certbot records the path -to this file for use during renewal, but does not store the file's contents. - -.. caution:: - You should protect these API credentials as you would the password to your - Sakura Cloud account. Users who can read this file can use these credentials - to issue arbitrary API calls on your behalf. Users who can cause Certbot to - run using these credentials can complete a ``dns-01`` challenge to acquire new - certificates or revoke existing certificates for associated domains, even if - those domains aren't being managed by this server. - -Certbot will emit a warning if it detects that the credentials file can be -accessed by other users on your system. The warning reads "Unsafe permissions -on credentials configuration file", followed by the path to the credentials -file. This warning will be emitted each time Certbot uses the credentials file, -including for renewal, and cannot be silenced except by addressing the issue -(e.g., by using a command like ``chmod 600`` to restrict access to the file). - - -Examples --------- - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com`` - - certbot certonly \\ - --dns-sakuracloud \\ - --dns-sakuracloud-credentials ~/.secrets/certbot/sakuracloud.ini \\ - -d example.com - -.. code-block:: bash - :caption: To acquire a single certificate for both ``example.com`` and - ``www.example.com`` - - certbot certonly \\ - --dns-sakuracloud \\ - --dns-sakuracloud-credentials ~/.secrets/certbot/sakuracloud.ini \\ - -d example.com \\ - -d www.example.com - -.. code-block:: bash - :caption: To acquire a certificate for ``example.com``, waiting 60 seconds - for DNS propagation - - certbot certonly \\ - --dns-sakuracloud \\ - --dns-sakuracloud-credentials ~/.secrets/certbot/sakuracloud.ini \\ - --dns-sakuracloud-propagation-seconds 60 \\ - -d example.com - -""" diff --git a/certbot-dns-sakuracloud/certbot_dns_sakuracloud/dns_sakuracloud.py b/certbot-dns-sakuracloud/certbot_dns_sakuracloud/dns_sakuracloud.py deleted file mode 100644 index 7fd6d3ef5e5..00000000000 --- a/certbot-dns-sakuracloud/certbot_dns_sakuracloud/dns_sakuracloud.py +++ /dev/null @@ -1,90 +0,0 @@ -"""DNS Authenticator for Sakura Cloud DNS.""" -import logging - -import zope.interface -from lexicon.providers import sakuracloud - -from certbot import interfaces -from certbot.plugins import dns_common -from certbot.plugins import dns_common_lexicon - -logger = logging.getLogger(__name__) - -APIKEY_URL = "https://secure.sakura.ad.jp/cloud/#!/apikey/top/" - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(dns_common.DNSAuthenticator): - """DNS Authenticator for Sakura Cloud DNS - - This Authenticator uses the Sakura Cloud API to fulfill a dns-01 challenge. - """ - - description = 'Obtain certificates using a DNS TXT record ' + \ - '(if you are using Sakura Cloud for DNS).' - ttl = 60 - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.credentials = None - - @classmethod - def add_parser_arguments(cls, add): # pylint: disable=arguments-differ - super(Authenticator, cls).add_parser_arguments( - add, default_propagation_seconds=90) - add('credentials', help='Sakura Cloud credentials file.') - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ - 'the Sakura Cloud API.' - - def _setup_credentials(self): - self.credentials = self._configure_credentials( - 'credentials', - 'Sakura Cloud credentials file', - { - 'api-token': \ - 'API token for Sakura Cloud API obtained from {0}'.format(APIKEY_URL), - 'api-secret': \ - 'API secret for Sakura Cloud API obtained from {0}'.format(APIKEY_URL), - } - ) - - def _perform(self, domain, validation_name, validation): - self._get_sakuracloud_client().add_txt_record( - domain, validation_name, validation) - - def _cleanup(self, domain, validation_name, validation): - self._get_sakuracloud_client().del_txt_record( - domain, validation_name, validation) - - def _get_sakuracloud_client(self): - return _SakuraCloudLexiconClient( - self.credentials.conf('api-token'), - self.credentials.conf('api-secret'), - self.ttl - ) - - -class _SakuraCloudLexiconClient(dns_common_lexicon.LexiconClient): - """ - Encapsulates all communication with the Sakura Cloud via Lexicon. - """ - - def __init__(self, api_token, api_secret, ttl): - super(_SakuraCloudLexiconClient, self).__init__() - - config = dns_common_lexicon.build_lexicon_config('sakuracloud', { - 'ttl': ttl, - }, { - 'auth_token': api_token, - 'auth_secret': api_secret, - }) - - self.provider = sakuracloud.Provider(config) - - def _handle_http_error(self, e, domain_name): - if domain_name in str(e) and (str(e).startswith('404 Client Error: Not Found for url:')): - return # Expected errors when zone name guess is wrong - return super(_SakuraCloudLexiconClient, self)._handle_http_error(e, domain_name) diff --git a/certbot-dns-sakuracloud/certbot_dns_sakuracloud/dns_sakuracloud_test.py b/certbot-dns-sakuracloud/certbot_dns_sakuracloud/dns_sakuracloud_test.py deleted file mode 100644 index 1d9282f9ad8..00000000000 --- a/certbot-dns-sakuracloud/certbot_dns_sakuracloud/dns_sakuracloud_test.py +++ /dev/null @@ -1,56 +0,0 @@ -"""Tests for certbot_dns_sakuracloud.dns_sakuracloud.""" - -import os -import unittest - -import mock -from requests.exceptions import HTTPError - -from certbot.plugins import dns_test_common -from certbot.plugins import dns_test_common_lexicon -from certbot.plugins.dns_test_common import DOMAIN -from certbot.tests import util as test_util - -API_TOKEN = '00000000-0000-0000-0000-000000000000' -API_SECRET = 'MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw' - -class AuthenticatorTest(test_util.TempDirTestCase, - dns_test_common_lexicon.BaseLexiconAuthenticatorTest): - - def setUp(self): - super(AuthenticatorTest, self).setUp() - - from certbot_dns_sakuracloud.dns_sakuracloud import Authenticator - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write( - {"sakuracloud_api_token": API_TOKEN, "sakuracloud_api_secret": API_SECRET}, - path - ) - - self.config = mock.MagicMock(sakuracloud_credentials=path, - sakuracloud_propagation_seconds=0) # don't wait during tests - - self.auth = Authenticator(self.config, "sakuracloud") - - self.mock_client = mock.MagicMock() - # _get_sakuracloud_client | pylint: disable=protected-access - self.auth._get_sakuracloud_client = mock.MagicMock(return_value=self.mock_client) - - -class SakuraCloudLexiconClientTest(unittest.TestCase, - dns_test_common_lexicon.BaseLexiconClientTest): - DOMAIN_NOT_FOUND = HTTPError('404 Client Error: Not Found for url: {0}.'.format(DOMAIN)) - LOGIN_ERROR = HTTPError('401 Client Error: Unauthorized for url: {0}.'.format(DOMAIN)) - - def setUp(self): - from certbot_dns_sakuracloud.dns_sakuracloud import _SakuraCloudLexiconClient - - self.client = _SakuraCloudLexiconClient(API_TOKEN, API_SECRET, 0) - - self.provider_mock = mock.MagicMock() - self.client.provider = self.provider_mock - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-dns-sakuracloud/docs/api.rst b/certbot-dns-sakuracloud/docs/api.rst index 8668ec5d8ad..ac13c3df297 100644 --- a/certbot-dns-sakuracloud/docs/api.rst +++ b/certbot-dns-sakuracloud/docs/api.rst @@ -2,7 +2,4 @@ API Documentation ================= -.. toctree:: - :glob: - - api/** +Certbot plugins implement the Certbot plugins API, and do not otherwise have an external API. diff --git a/certbot-dns-sakuracloud/docs/api/dns_sakuracloud.rst b/certbot-dns-sakuracloud/docs/api/dns_sakuracloud.rst deleted file mode 100644 index 74692e15b00..00000000000 --- a/certbot-dns-sakuracloud/docs/api/dns_sakuracloud.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_dns_sakuracloud.dns_sakuracloud` ----------------------------------------------- - -.. automodule:: certbot_dns_sakuracloud.dns_sakuracloud - :members: diff --git a/certbot-dns-sakuracloud/docs/conf.py b/certbot-dns-sakuracloud/docs/conf.py index e14fe1d4c73..0fd7be08088 100644 --- a/certbot-dns-sakuracloud/docs/conf.py +++ b/certbot-dns-sakuracloud/docs/conf.py @@ -16,7 +16,8 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os +# import os + # import sys # sys.path.insert(0, os.path.abspath('.')) @@ -34,10 +35,11 @@ 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', - 'sphinx.ext.viewcode'] + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme'] autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] +autodoc_default_flags = ['show-inheritance'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -83,7 +85,7 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True +todo_include_todos = False # -- Options for HTML output ---------------------------------------------- @@ -92,14 +94,7 @@ # a list of builtin themes. # -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -110,7 +105,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] # -- Options for HTMLHelp output ------------------------------------------ @@ -175,6 +170,6 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), + 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None), } diff --git a/certbot-dns-sakuracloud/docs/make.bat b/certbot-dns-sakuracloud/docs/make.bat index 0d7706bc742..b340a2a1996 100644 --- a/certbot-dns-sakuracloud/docs/make.bat +++ b/certbot-dns-sakuracloud/docs/make.bat @@ -22,7 +22,7 @@ if errorlevel 9009 ( echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ + echo.https://www.sphinx-doc.org/ exit /b 1 ) diff --git a/certbot-dns-sakuracloud/local-oldest-requirements.txt b/certbot-dns-sakuracloud/local-oldest-requirements.txt deleted file mode 100644 index 65f5a758ea0..00000000000 --- a/certbot-dns-sakuracloud/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ --e acme[dev] --e .[dev] diff --git a/certbot-dns-sakuracloud/pyproject.toml b/certbot-dns-sakuracloud/pyproject.toml new file mode 100644 index 00000000000..fc5931dd696 --- /dev/null +++ b/certbot-dns-sakuracloud/pyproject.toml @@ -0,0 +1,54 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-dns-sakuracloud" +dynamic = ["version", "dependencies"] +description = "Sakura Cloud DNS Authenticator plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +docs = [ + "Sphinx>=1.0", # autodoc_member_order = 'bysource', autodoc_default_flags + "sphinx_rtd_theme", +] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +dns-sakuracloud = "certbot_dns_sakuracloud._internal.dns_sakuracloud:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-dns-sakuracloud/readthedocs.org.requirements.txt b/certbot-dns-sakuracloud/readthedocs.org.requirements.txt deleted file mode 100644 index 3f46d95ef62..00000000000 --- a/certbot-dns-sakuracloud/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-dns-sakuracloud[docs] diff --git a/certbot-dns-sakuracloud/setup.cfg b/certbot-dns-sakuracloud/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-dns-sakuracloud/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-dns-sakuracloud/setup.py b/certbot-dns-sakuracloud/setup.py index 4ebfc6e1d37..e17fd5066ee 100644 --- a/certbot-dns-sakuracloud/setup.py +++ b/certbot-dns-sakuracloud/setup.py @@ -1,64 +1,25 @@ -from setuptools import setup -from setuptools import find_packages +import os +from setuptools import setup -version = '0.31.0.dev0' +version = '5.8.0.dev0' -# Please update tox.ini when modifying dependency version requirements install_requires = [ - 'acme>=0.31.0.dev0', - 'certbot>=0.31.0.dev0', - 'dns-lexicon>=2.1.23', - 'mock', - 'setuptools', - 'zope.interface', + 'dns-lexicon>=3.14.1', ] -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] +if os.environ.get('SNAP_BUILD'): + install_requires.append('packaging') +else: + install_requires.extend([ + # We specify the minimum acme and certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'acme>={version}', + f'certbot>={version}', + ]) setup( - name='certbot-dns-sakuracloud', version=version, - description="Sakura Cloud DNS Authenticator plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'dns-sakuracloud = certbot_dns_sakuracloud.dns_sakuracloud:Authenticator', - ], - }, - test_suite='certbot_dns_sakuracloud', ) diff --git a/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/__init__.py b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/__init__.py new file mode 100644 index 00000000000..c16ee96efde --- /dev/null +++ b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/__init__.py @@ -0,0 +1,90 @@ +""" +The `~certbot_dns_sakuracloud.dns_sakuracloud` plugin automates the process of completing +a ``dns-01`` challenge (`~acme.challenges.DNS01`) by creating, and subsequently +removing, TXT records using the Sakura Cloud DNS API. + +.. note:: + The plugin is not installed by default. It can be installed by heading to + `certbot.eff.org `_, choosing your system and + selecting the Wildcard tab. + +Named Arguments +--------------- + +========================================== ====================================== +``--dns-sakuracloud-credentials`` Sakura Cloud credentials_ INI file. + (Required) +``--dns-sakuracloud-propagation-seconds`` The number of seconds to wait for DNS + to propagate before asking the ACME + server to verify the DNS record. + (Default: 90) +========================================== ====================================== + + +Credentials +----------- + +Use of this plugin requires a configuration file containing +Sakura Cloud DNS API credentials, obtained from your Sakura Cloud DNS +`apikey page `_. + +.. code-block:: ini + :name: credentials.ini + :caption: Example credentials file: + + # Sakura Cloud API credentials used by Certbot + dns_sakuracloud_api_token = 00000000-0000-0000-0000-000000000000 + dns_sakuracloud_api_secret = MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw + +The path to this file can be provided interactively or using the +``--dns-sakuracloud-credentials`` command-line argument. Certbot records the path +to this file for use during renewal, but does not store the file's contents. + +.. caution:: + You should protect these API credentials as you would the password to your + Sakura Cloud account. Users who can read this file can use these credentials + to issue arbitrary API calls on your behalf. Users who can cause Certbot to + run using these credentials can complete a ``dns-01`` challenge to acquire new + certificates or revoke existing certificates for associated domains, even if + those domains aren't being managed by this server. + +Certbot will emit a warning if it detects that the credentials file can be +accessed by other users on your system. The warning reads "Unsafe permissions +on credentials configuration file", followed by the path to the credentials +file. This warning will be emitted each time Certbot uses the credentials file, +including for renewal, and cannot be silenced except by addressing the issue +(e.g., by using a command like ``chmod 600`` to restrict access to the file). + + +Examples +-------- + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com`` + + certbot certonly \\ + --dns-sakuracloud \\ + --dns-sakuracloud-credentials ~/.secrets/certbot/sakuracloud.ini \\ + -d example.com + +.. code-block:: bash + :caption: To acquire a single certificate for both ``example.com`` and + ``www.example.com`` + + certbot certonly \\ + --dns-sakuracloud \\ + --dns-sakuracloud-credentials ~/.secrets/certbot/sakuracloud.ini \\ + -d example.com \\ + -d www.example.com + +.. code-block:: bash + :caption: To acquire a certificate for ``example.com``, waiting 60 seconds + for DNS propagation + + certbot certonly \\ + --dns-sakuracloud \\ + --dns-sakuracloud-credentials ~/.secrets/certbot/sakuracloud.ini \\ + --dns-sakuracloud-propagation-seconds 60 \\ + -d example.com + +""" diff --git a/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/__init__.py b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/__init__.py new file mode 100644 index 00000000000..0c8839024ee --- /dev/null +++ b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/__init__.py @@ -0,0 +1 @@ +"""Internal implementation of `~certbot_dns_sakuracloud.dns_sakuracloud` plugin.""" diff --git a/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/dns_sakuracloud.py b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/dns_sakuracloud.py new file mode 100644 index 00000000000..8f9934a0d6c --- /dev/null +++ b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/dns_sakuracloud.py @@ -0,0 +1,53 @@ +"""DNS Authenticator for Sakura Cloud DNS.""" +import logging +from typing import Any +from typing import Callable +from typing import Optional + +from requests import HTTPError + +from certbot import errors +from certbot.plugins import dns_common_lexicon + +logger = logging.getLogger(__name__) + +APIKEY_URL = "https://secure.sakura.ad.jp/cloud/#!/apikey/top/" + + +class Authenticator(dns_common_lexicon.LexiconDNSAuthenticator): + """DNS Authenticator for Sakura Cloud DNS + + This Authenticator uses the Sakura Cloud API to fulfill a dns-01 challenge. + """ + + description = 'Obtain certificates using a DNS TXT record ' + \ + '(if you are using Sakura Cloud for DNS).' + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._add_provider_option('api-token', + f'API token for Sakura Cloud API obtained from {APIKEY_URL}', + 'auth_token') + self._add_provider_option('api-secret', + f'API secret for Sakura Cloud API obtained from {APIKEY_URL}', + 'auth_secret') + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], + default_propagation_seconds: int = 30) -> None: + super().add_parser_arguments( + add, default_propagation_seconds=90) + add('credentials', help='Sakura Cloud credentials file.') + + def more_info(self) -> str: + return 'This plugin configures a DNS TXT record to respond to a dns-01 challenge using ' + \ + 'the Sakura Cloud API.' + + @property + def _provider_name(self) -> str: + return 'sakuracloud' + + def _handle_http_error(self, e: HTTPError, domain_name: str) -> Optional[errors.PluginError]: + if domain_name in str(e) and (str(e).startswith('404 Client Error: Not Found for url:')): + return None # Expected errors when zone name guess is wrong + return super()._handle_http_error(e, domain_name) diff --git a/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/tests/__init__.py b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/tests/__init__.py new file mode 100644 index 00000000000..e08a3ebac04 --- /dev/null +++ b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot-dns-sakuracloud tests""" diff --git a/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/tests/dns_sakuracloud_test.py b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/tests/dns_sakuracloud_test.py new file mode 100644 index 00000000000..44a36b9e01b --- /dev/null +++ b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/_internal/tests/dns_sakuracloud_test.py @@ -0,0 +1,43 @@ +"""Tests for certbot_dns_sakuracloud._internal.dns_sakuracloud.""" +import sys +from unittest import mock + +import pytest +from requests import Response +from requests.exceptions import HTTPError + +from certbot.compat import os +from certbot.plugins import dns_test_common +from certbot.plugins import dns_test_common_lexicon +from certbot.plugins.dns_test_common import DOMAIN +from certbot.tests import util as test_util + +API_TOKEN = '00000000-0000-0000-0000-000000000000' +API_SECRET = 'MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAw' + + +class AuthenticatorTest(test_util.TempDirTestCase, + dns_test_common_lexicon.BaseLexiconDNSAuthenticatorTest): + + DOMAIN_NOT_FOUND = HTTPError(f'404 Client Error: Not Found for url: {DOMAIN}.', response=Response()) + LOGIN_ERROR = HTTPError(f'401 Client Error: Unauthorized for url: {DOMAIN}.', response=Response()) + + def setUp(self): + super().setUp() + + from certbot_dns_sakuracloud._internal.dns_sakuracloud import Authenticator + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write( + {"sakuracloud_api_token": API_TOKEN, "sakuracloud_api_secret": API_SECRET}, + path + ) + + self.config = mock.MagicMock(sakuracloud_credentials=path, + sakuracloud_propagation_seconds=0) # don't wait during tests + + self.auth = Authenticator(self.config, "sakuracloud") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/py.typed b/certbot-dns-sakuracloud/src/certbot_dns_sakuracloud/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-nginx/MANIFEST.in b/certbot-nginx/MANIFEST.in index 2daca673865..7c473a9b10c 100644 --- a/certbot-nginx/MANIFEST.in +++ b/certbot-nginx/MANIFEST.in @@ -1,5 +1,5 @@ include LICENSE.txt include README.rst -recursive-include docs * -recursive-include certbot_nginx/tests/testdata * -include certbot_nginx/options-ssl-nginx.conf +include src/certbot_nginx/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot-nginx/certbot_nginx/configurator.py b/certbot-nginx/certbot_nginx/configurator.py deleted file mode 100644 index dd0bf9e8be1..00000000000 --- a/certbot-nginx/certbot_nginx/configurator.py +++ /dev/null @@ -1,1162 +0,0 @@ -"""Nginx Configuration""" -import logging -import os -import re -import socket -import subprocess -import tempfile -import time - -import OpenSSL -import zope.interface - -from acme import challenges -from acme import crypto_util as acme_crypto_util - -from certbot import constants as core_constants -from certbot import crypto_util -from certbot import errors -from certbot import interfaces -from certbot import util - -from certbot.plugins import common - -from certbot_nginx import constants -from certbot_nginx import display_ops -from certbot_nginx import nginxparser -from certbot_nginx import parser -from certbot_nginx import tls_sni_01 -from certbot_nginx import http_01 -from certbot_nginx import obj # pylint: disable=unused-import -from acme.magic_typing import List, Dict, Set # pylint: disable=unused-import, no-name-in-module - - -NAME_RANK = 0 -START_WILDCARD_RANK = 1 -END_WILDCARD_RANK = 2 -REGEX_RANK = 3 -NO_SSL_MODIFIER = 4 - - -logger = logging.getLogger(__name__) - - -@zope.interface.implementer(interfaces.IAuthenticator, interfaces.IInstaller) -@zope.interface.provider(interfaces.IPluginFactory) -class NginxConfigurator(common.Installer): - # pylint: disable=too-many-instance-attributes,too-many-public-methods - """Nginx configurator. - - .. todo:: Add proper support for comments in the config. Currently, - config files modified by the configurator will lose all their comments. - - :ivar config: Configuration. - :type config: :class:`~certbot.interfaces.IConfig` - - :ivar parser: Handles low level parsing - :type parser: :class:`~certbot_nginx.parser` - - :ivar str save_notes: Human-readable config change notes - - :ivar reverter: saves and reverts checkpoints - :type reverter: :class:`certbot.reverter.Reverter` - - :ivar tup version: version of Nginx - - """ - - description = "Nginx Web Server plugin" - - DEFAULT_LISTEN_PORT = '80' - - # SSL directives that Certbot can add when installing a new certificate. - SSL_DIRECTIVES = ['ssl_certificate', 'ssl_certificate_key', 'ssl_dhparam'] - - @classmethod - def add_parser_arguments(cls, add): - default_server_root = _determine_default_server_root() - add("server-root", default=constants.CLI_DEFAULTS["server_root"], - help="Nginx server root directory. (default: %s)" % default_server_root) - add("ctl", default=constants.CLI_DEFAULTS["ctl"], help="Path to the " - "'nginx' binary, used for 'configtest' and retrieving nginx " - "version number.") - - @property - def nginx_conf(self): - """Nginx config file path.""" - return os.path.join(self.conf("server_root"), "nginx.conf") - - def __init__(self, *args, **kwargs): - """Initialize an Nginx Configurator. - - :param tup version: version of Nginx as a tuple (1, 4, 7) - (used mostly for unittesting) - - """ - version = kwargs.pop("version", None) - super(NginxConfigurator, self).__init__(*args, **kwargs) - - # Verify that all directories and files exist with proper permissions - self._verify_setup() - - # Files to save - self.save_notes = "" - - # For creating new vhosts if no names match - self.new_vhost = None - - # List of vhosts configured per wildcard domain on this run. - # used by deploy_cert() and enhance() - self._wildcard_vhosts = {} # type: Dict[str, List[obj.VirtualHost]] - self._wildcard_redirect_vhosts = {} # type: Dict[str, List[obj.VirtualHost]] - - # Add number of outstanding challenges - self._chall_out = 0 - - # These will be set in the prepare function - self.parser = None - self.version = version - self._enhance_func = {"redirect": self._enable_redirect, - "ensure-http-header": self._set_http_header, - "staple-ocsp": self._enable_ocsp_stapling} - - self.reverter.recovery_routine() - - @property - def mod_ssl_conf(self): - """Full absolute path to SSL configuration file.""" - return os.path.join(self.config.config_dir, constants.MOD_SSL_CONF_DEST) - - @property - def updated_mod_ssl_conf_digest(self): - """Full absolute path to digest of updated SSL configuration file.""" - return os.path.join(self.config.config_dir, constants.UPDATED_MOD_SSL_CONF_DIGEST) - - # This is called in determine_authenticator and determine_installer - def prepare(self): - """Prepare the authenticator/installer. - - :raises .errors.NoInstallationError: If Nginx ctl cannot be found - :raises .errors.MisconfigurationError: If Nginx is misconfigured - """ - # Verify Nginx is installed - if not util.exe_exists(self.conf('ctl')): - raise errors.NoInstallationError( - "Could not find a usable 'nginx' binary. Ensure nginx exists, " - "the binary is executable, and your PATH is set correctly.") - - # Make sure configuration is valid - self.config_test() - - - self.parser = parser.NginxParser(self.conf('server-root')) - - install_ssl_options_conf(self.mod_ssl_conf, self.updated_mod_ssl_conf_digest) - - self.install_ssl_dhparams() - - # Set Version - if self.version is None: - self.version = self.get_version() - - # Prevent two Nginx plugins from modifying a config at once - try: - util.lock_dir_until_exit(self.conf('server-root')) - except (OSError, errors.LockError): - logger.debug('Encountered error:', exc_info=True) - raise errors.PluginError( - 'Unable to lock %s', self.conf('server-root')) - - - # Entry point in main.py for installing cert - def deploy_cert(self, domain, cert_path, key_path, - chain_path=None, fullchain_path=None): - # pylint: disable=unused-argument - """Deploys certificate to specified virtual host. - - .. note:: Aborts if the vhost is missing ssl_certificate or - ssl_certificate_key. - - .. note:: This doesn't save the config files! - - :raises errors.PluginError: When unable to deploy certificate due to - a lack of directives or configuration - - """ - if not fullchain_path: - raise errors.PluginError( - "The nginx plugin currently requires --fullchain-path to " - "install a cert.") - - vhosts = self.choose_vhosts(domain, create_if_no_match=True) - for vhost in vhosts: - self._deploy_cert(vhost, cert_path, key_path, chain_path, fullchain_path) - - def _deploy_cert(self, vhost, cert_path, key_path, chain_path, fullchain_path): - # pylint: disable=unused-argument - """ - Helper function for deploy_cert() that handles the actual deployment - this exists because we might want to do multiple deployments per - domain originally passed for deploy_cert(). This is especially true - with wildcard certificates - """ - cert_directives = [['\n ', 'ssl_certificate', ' ', fullchain_path], - ['\n ', 'ssl_certificate_key', ' ', key_path]] - - self.parser.update_or_add_server_directives(vhost, - cert_directives) - logger.info("Deploying Certificate to VirtualHost %s", vhost.filep) - - self.save_notes += ("Changed vhost at %s with addresses of %s\n" % - (vhost.filep, - ", ".join(str(addr) for addr in vhost.addrs))) - self.save_notes += "\tssl_certificate %s\n" % fullchain_path - self.save_notes += "\tssl_certificate_key %s\n" % key_path - - def _choose_vhosts_wildcard(self, domain, prefer_ssl, no_ssl_filter_port=None): - """Prompts user to choose vhosts to install a wildcard certificate for""" - if prefer_ssl: - vhosts_cache = self._wildcard_vhosts - preference_test = lambda x: x.ssl - else: - vhosts_cache = self._wildcard_redirect_vhosts - preference_test = lambda x: not x.ssl - - # Caching! - if domain in vhosts_cache: - # Vhosts for a wildcard domain were already selected - return vhosts_cache[domain] - - # Get all vhosts whether or not they are covered by the wildcard domain - vhosts = self.parser.get_vhosts() - - # Go through the vhosts, making sure that we cover all the names - # present, but preferring the SSL or non-SSL vhosts - filtered_vhosts = {} - for vhost in vhosts: - # Ensure we're listening non-sslishly on no_ssl_filter_port - if no_ssl_filter_port is not None: - if not self._vhost_listening_on_port_no_ssl(vhost, no_ssl_filter_port): - continue - for name in vhost.names: - if preference_test(vhost): - # Prefer either SSL or non-SSL vhosts - filtered_vhosts[name] = vhost - elif name not in filtered_vhosts: - # Add if not in list previously - filtered_vhosts[name] = vhost - - # Only unique VHost objects - dialog_input = set([vhost for vhost in filtered_vhosts.values()]) - - # Ask the user which of names to enable, expect list of names back - return_vhosts = display_ops.select_vhost_multiple(list(dialog_input)) - - for vhost in return_vhosts: - if domain not in vhosts_cache: - vhosts_cache[domain] = [] - vhosts_cache[domain].append(vhost) - - return return_vhosts - - ####################### - # Vhost parsing methods - ####################### - def _choose_vhost_single(self, target_name): - matches = self._get_ranked_matches(target_name) - vhosts = [x for x in [self._select_best_name_match(matches)] if x is not None] - return vhosts - - def choose_vhosts(self, target_name, create_if_no_match=False): - """Chooses a virtual host based on the given domain name. - - .. note:: This makes the vhost SSL-enabled if it isn't already. Follows - Nginx's server block selection rules preferring blocks that are - already SSL. - - .. todo:: This should maybe return list if no obvious answer - is presented. - - .. todo:: The special name "$hostname" corresponds to the machine's - hostname. Currently we just ignore this. - - :param str target_name: domain name - :param bool create_if_no_match: If we should create a new vhost from default - when there is no match found. If we can't choose a default, raise a - MisconfigurationError. - - :returns: ssl vhosts associated with name - :rtype: list of :class:`~certbot_nginx.obj.VirtualHost` - - """ - if util.is_wildcard_domain(target_name): - # Ask user which VHosts to support. - vhosts = self._choose_vhosts_wildcard(target_name, prefer_ssl=True) - else: - vhosts = self._choose_vhost_single(target_name) - if not vhosts: - if create_if_no_match: - # result will not be [None] because it errors on failure - vhosts = [self._vhost_from_duplicated_default(target_name, True, - str(self.config.tls_sni_01_port))] - else: - # No matches. Raise a misconfiguration error. - raise errors.MisconfigurationError( - ("Cannot find a VirtualHost matching domain %s. " - "In order for Certbot to correctly perform the challenge " - "please add a corresponding server_name directive to your " - "nginx configuration for every domain on your certificate: " - "https://nginx.org/en/docs/http/server_names.html") % (target_name)) - # Note: if we are enhancing with ocsp, vhost should already be ssl. - for vhost in vhosts: - if not vhost.ssl: - self._make_server_ssl(vhost) - - return vhosts - - def ipv6_info(self, port): - """Returns tuple of booleans (ipv6_active, ipv6only_present) - ipv6_active is true if any server block listens ipv6 address in any port - - ipv6only_present is true if ipv6only=on option exists in any server - block ipv6 listen directive for the specified port. - - :param str port: Port to check ipv6only=on directive for - - :returns: Tuple containing information if IPv6 is enabled in the global - configuration, and existence of ipv6only directive for specified port - :rtype: tuple of type (bool, bool) - """ - # port should be a string, but it's easy to mess up, so let's - # make sure it is one - port = str(port) - vhosts = self.parser.get_vhosts() - ipv6_active = False - ipv6only_present = False - for vh in vhosts: - for addr in vh.addrs: - if addr.ipv6: - ipv6_active = True - if addr.ipv6only and addr.get_port() == port: - ipv6only_present = True - return (ipv6_active, ipv6only_present) - - def _vhost_from_duplicated_default(self, domain, allow_port_mismatch, port): - """if allow_port_mismatch is False, only server blocks with matching ports will be - used as a default server block template. - """ - if self.new_vhost is None: - default_vhost = self._get_default_vhost(domain, allow_port_mismatch, port) - self.new_vhost = self.parser.duplicate_vhost(default_vhost, - remove_singleton_listen_params=True) - self.new_vhost.names = set() - - self._add_server_name_to_vhost(self.new_vhost, domain) - return self.new_vhost - - def _add_server_name_to_vhost(self, vhost, domain): - vhost.names.add(domain) - name_block = [['\n ', 'server_name']] - for name in vhost.names: - name_block[0].append(' ') - name_block[0].append(name) - self.parser.update_or_add_server_directives(vhost, name_block) - - def _get_default_vhost(self, domain, allow_port_mismatch, port): - """Helper method for _vhost_from_duplicated_default; see argument documentation there""" - vhost_list = self.parser.get_vhosts() - # if one has default_server set, return that one - all_default_vhosts = [] - port_matching_vhosts = [] - for vhost in vhost_list: - for addr in vhost.addrs: - if addr.default: - all_default_vhosts.append(vhost) - if self._port_matches(port, addr.get_port()): - port_matching_vhosts.append(vhost) - break - - if len(port_matching_vhosts) == 1: - return port_matching_vhosts[0] - elif len(all_default_vhosts) == 1 and allow_port_mismatch: - return all_default_vhosts[0] - - # TODO: present a list of vhosts for user to choose from - - raise errors.MisconfigurationError("Could not automatically find a matching server" - " block for %s. Set the `server_name` directive to use the Nginx installer." % domain) - - def _get_ranked_matches(self, target_name): - """Returns a ranked list of vhosts that match target_name. - The ranking gives preference to SSL vhosts. - - :param str target_name: The name to match - :returns: list of dicts containing the vhost, the matching name, and - the numerical rank - :rtype: list - - """ - vhost_list = self.parser.get_vhosts() - return self._rank_matches_by_name_and_ssl(vhost_list, target_name) - - def _select_best_name_match(self, matches): - """Returns the best name match of a ranked list of vhosts. - - :param list matches: list of dicts containing the vhost, the matching name, - and the numerical rank - :returns: the most matching vhost - :rtype: :class:`~certbot_nginx.obj.VirtualHost` - - """ - if not matches: - return None - elif matches[0]['rank'] in [START_WILDCARD_RANK, END_WILDCARD_RANK, - START_WILDCARD_RANK + NO_SSL_MODIFIER, END_WILDCARD_RANK + NO_SSL_MODIFIER]: - # Wildcard match - need to find the longest one - rank = matches[0]['rank'] - wildcards = [x for x in matches if x['rank'] == rank] - return max(wildcards, key=lambda x: len(x['name']))['vhost'] - else: - # Exact or regex match - return matches[0]['vhost'] - - def _rank_matches_by_name(self, vhost_list, target_name): - """Returns a ranked list of vhosts from vhost_list that match target_name. - This method should always be followed by a call to _select_best_name_match. - - :param list vhost_list: list of vhosts to filter and rank - :param str target_name: The name to match - :returns: list of dicts containing the vhost, the matching name, and - the numerical rank - :rtype: list - - """ - # Nginx chooses a matching server name for a request with precedence: - # 1. exact name match - # 2. longest wildcard name starting with * - # 3. longest wildcard name ending with * - # 4. first matching regex in order of appearance in the file - matches = [] - for vhost in vhost_list: - name_type, name = parser.get_best_match(target_name, vhost.names) - if name_type == 'exact': - matches.append({'vhost': vhost, - 'name': name, - 'rank': NAME_RANK}) - elif name_type == 'wildcard_start': - matches.append({'vhost': vhost, - 'name': name, - 'rank': START_WILDCARD_RANK}) - elif name_type == 'wildcard_end': - matches.append({'vhost': vhost, - 'name': name, - 'rank': END_WILDCARD_RANK}) - elif name_type == 'regex': - matches.append({'vhost': vhost, - 'name': name, - 'rank': REGEX_RANK}) - return sorted(matches, key=lambda x: x['rank']) - - def _rank_matches_by_name_and_ssl(self, vhost_list, target_name): - """Returns a ranked list of vhosts from vhost_list that match target_name. - The ranking gives preference to SSLishness before name match level. - - :param list vhost_list: list of vhosts to filter and rank - :param str target_name: The name to match - :returns: list of dicts containing the vhost, the matching name, and - the numerical rank - :rtype: list - - """ - matches = self._rank_matches_by_name(vhost_list, target_name) - for match in matches: - if not match['vhost'].ssl: - match['rank'] += NO_SSL_MODIFIER - return sorted(matches, key=lambda x: x['rank']) - - def choose_redirect_vhosts(self, target_name, port, create_if_no_match=False): - """Chooses a single virtual host for redirect enhancement. - - Chooses the vhost most closely matching target_name that is - listening to port without using ssl. - - .. todo:: This should maybe return list if no obvious answer - is presented. - - .. todo:: The special name "$hostname" corresponds to the machine's - hostname. Currently we just ignore this. - - :param str target_name: domain name - :param str port: port number - :param bool create_if_no_match: If we should create a new vhost from default - when there is no match found. If we can't choose a default, raise a - MisconfigurationError. - - :returns: vhosts associated with name - :rtype: list of :class:`~certbot_nginx.obj.VirtualHost` - - """ - if util.is_wildcard_domain(target_name): - # Ask user which VHosts to enhance. - vhosts = self._choose_vhosts_wildcard(target_name, prefer_ssl=False, - no_ssl_filter_port=port) - else: - matches = self._get_redirect_ranked_matches(target_name, port) - vhosts = [x for x in [self._select_best_name_match(matches)]if x is not None] - if not vhosts and create_if_no_match: - vhosts = [self._vhost_from_duplicated_default(target_name, False, port)] - return vhosts - - def _port_matches(self, test_port, matching_port): - # test_port is a number, matching is a number or "" or None - if matching_port == "" or matching_port is None: - # if no port is specified, Nginx defaults to listening on port 80. - return test_port == self.DEFAULT_LISTEN_PORT - else: - return test_port == matching_port - - def _vhost_listening_on_port_no_ssl(self, vhost, port): - found_matching_port = False - if len(vhost.addrs) == 0: - # if there are no listen directives at all, Nginx defaults to - # listening on port 80. - found_matching_port = (port == self.DEFAULT_LISTEN_PORT) - else: - for addr in vhost.addrs: - if self._port_matches(port, addr.get_port()) and addr.ssl == False: - found_matching_port = True - - if found_matching_port: - # make sure we don't have an 'ssl on' directive - return not self.parser.has_ssl_on_directive(vhost) - else: - return False - - def _get_redirect_ranked_matches(self, target_name, port): - """Gets a ranked list of plaintextish port-listening vhosts matching target_name - - Filter all hosts for those listening on port without using ssl. - Rank by how well these match target_name. - - :param str target_name: The name to match - :param str port: port number as a string - :returns: list of dicts containing the vhost, the matching name, and - the numerical rank - :rtype: list - - """ - all_vhosts = self.parser.get_vhosts() - - def _vhost_matches(vhost, port): - return self._vhost_listening_on_port_no_ssl(vhost, port) - - matching_vhosts = [vhost for vhost in all_vhosts if _vhost_matches(vhost, port)] - - return self._rank_matches_by_name(matching_vhosts, target_name) - - def get_all_names(self): - """Returns all names found in the Nginx Configuration. - - :returns: All ServerNames, ServerAliases, and reverse DNS entries for - virtual host addresses - :rtype: set - - """ - all_names = set() # type: Set[str] - - for vhost in self.parser.get_vhosts(): - all_names.update(vhost.names) - - for addr in vhost.addrs: - host = addr.get_addr() - if common.hostname_regex.match(host): - # If it's a hostname, add it to the names. - all_names.add(host) - elif not common.private_ips_regex.match(host): - # If it isn't a private IP, do a reverse DNS lookup - try: - if addr.ipv6: - host = addr.get_ipv6_exploded() - socket.inet_pton(socket.AF_INET6, host) - else: - socket.inet_pton(socket.AF_INET, host) - all_names.add(socket.gethostbyaddr(host)[0]) - except (socket.error, socket.herror, socket.timeout): - continue - - return util.get_filtered_names(all_names) - - def _get_snakeoil_paths(self): - """Generate invalid certs that let us create ssl directives for Nginx""" - # TODO: generate only once - tmp_dir = os.path.join(self.config.work_dir, "snakeoil") - le_key = crypto_util.init_save_key( - key_size=1024, key_dir=tmp_dir, keyname="key.pem") - key = OpenSSL.crypto.load_privatekey( - OpenSSL.crypto.FILETYPE_PEM, le_key.pem) - cert = acme_crypto_util.gen_ss_cert(key, domains=[socket.gethostname()]) - cert_pem = OpenSSL.crypto.dump_certificate( - OpenSSL.crypto.FILETYPE_PEM, cert) - cert_file, cert_path = util.unique_file( - os.path.join(tmp_dir, "cert.pem"), mode="wb") - with cert_file: - cert_file.write(cert_pem) - return cert_path, le_key.file - - def _make_server_ssl(self, vhost): - """Make a server SSL. - - Make a server SSL by adding new listen and SSL directives. - - :param vhost: The vhost to add SSL to. - :type vhost: :class:`~certbot_nginx.obj.VirtualHost` - - """ - ipv6info = self.ipv6_info(self.config.tls_sni_01_port) - ipv6_block = [''] - ipv4_block = [''] - - # If the vhost was implicitly listening on the default Nginx port, - # have it continue to do so. - if len(vhost.addrs) == 0: - listen_block = [['\n ', 'listen', ' ', self.DEFAULT_LISTEN_PORT]] - self.parser.add_server_directives(vhost, listen_block) - - if vhost.ipv6_enabled(): - ipv6_block = ['\n ', - 'listen', - ' ', - '[::]:{0}'.format(self.config.tls_sni_01_port), - ' ', - 'ssl'] - if not ipv6info[1]: - # ipv6only=on is absent in global config - ipv6_block.append(' ') - ipv6_block.append('ipv6only=on') - - if vhost.ipv4_enabled(): - ipv4_block = ['\n ', - 'listen', - ' ', - '{0}'.format(self.config.tls_sni_01_port), - ' ', - 'ssl'] - - snakeoil_cert, snakeoil_key = self._get_snakeoil_paths() - - ssl_block = ([ - ipv6_block, - ipv4_block, - ['\n ', 'ssl_certificate', ' ', snakeoil_cert], - ['\n ', 'ssl_certificate_key', ' ', snakeoil_key], - ['\n ', 'include', ' ', self.mod_ssl_conf], - ['\n ', 'ssl_dhparam', ' ', self.ssl_dhparams], - ]) - - self.parser.add_server_directives( - vhost, ssl_block) - - ################################## - # enhancement methods (IInstaller) - ################################## - def supported_enhancements(self): # pylint: disable=no-self-use - """Returns currently supported enhancements.""" - return ['redirect', 'ensure-http-header', 'staple-ocsp'] - - def enhance(self, domain, enhancement, options=None): - """Enhance configuration. - - :param str domain: domain to enhance - :param str enhancement: enhancement type defined in - :const:`~certbot.constants.ENHANCEMENTS` - :param options: options for the enhancement - See :const:`~certbot.constants.ENHANCEMENTS` - documentation for appropriate parameter. - - """ - try: - return self._enhance_func[enhancement](domain, options) - except (KeyError, ValueError): - raise errors.PluginError( - "Unsupported enhancement: {0}".format(enhancement)) - except errors.PluginError: - logger.warning("Failed %s for %s", enhancement, domain) - raise - - def _has_certbot_redirect(self, vhost, domain): - test_redirect_block = _test_block_from_block(_redirect_block_for_domain(domain)) - return vhost.contains_list(test_redirect_block) - - def _set_http_header(self, domain, header_substring): - """Enables header identified by header_substring on domain. - - If the vhost is listening plaintextishly, separates out the relevant - directives into a new server block, and only add header directive to - HTTPS block. - - :param str domain: the domain to enable header for. - :param str header_substring: String to uniquely identify a header. - e.g. Strict-Transport-Security, Upgrade-Insecure-Requests - :returns: Success - :raises .errors.PluginError: If no viable HTTPS host can be created or - set with header header_substring. - """ - vhosts = self.choose_vhosts(domain) - if not vhosts: - raise errors.PluginError( - "Unable to find corresponding HTTPS host for enhancement.") - for vhost in vhosts: - if vhost.has_header(header_substring): - raise errors.PluginEnhancementAlreadyPresent( - "Existing %s header" % (header_substring)) - - # if there is no separate SSL block, break the block into two and - # choose the SSL block. - if vhost.ssl and any([not addr.ssl for addr in vhost.addrs]): - _, vhost = self._split_block(vhost) - - header_directives = [ - ['\n ', 'add_header', ' ', header_substring, ' '] + - constants.HEADER_ARGS[header_substring], - ['\n']] - self.parser.add_server_directives(vhost, header_directives) - - def _add_redirect_block(self, vhost, domain): - """Add redirect directive to vhost - """ - redirect_block = _redirect_block_for_domain(domain) - - self.parser.add_server_directives( - vhost, redirect_block, insert_at_top=True) - - def _split_block(self, vhost, only_directives=None): - """Splits this "virtual host" (i.e. this nginx server block) into - separate HTTP and HTTPS blocks. - - :param vhost: The server block to break up into two. - :param list only_directives: If this exists, only duplicate these directives - when splitting the block. - :type vhost: :class:`~certbot_nginx.obj.VirtualHost` - :returns: tuple (http_vhost, https_vhost) - :rtype: tuple of type :class:`~certbot_nginx.obj.VirtualHost` - """ - http_vhost = self.parser.duplicate_vhost(vhost, only_directives=only_directives) - - def _ssl_match_func(directive): - return 'ssl' in directive - - def _ssl_config_match_func(directive): - return self.mod_ssl_conf in directive - - def _no_ssl_match_func(directive): - return 'ssl' not in directive - - # remove all ssl addresses and related directives from the new block - for directive in self.SSL_DIRECTIVES: - self.parser.remove_server_directives(http_vhost, directive) - self.parser.remove_server_directives(http_vhost, 'listen', match_func=_ssl_match_func) - self.parser.remove_server_directives(http_vhost, 'include', - match_func=_ssl_config_match_func) - - # remove all non-ssl addresses from the existing block - self.parser.remove_server_directives(vhost, 'listen', match_func=_no_ssl_match_func) - return http_vhost, vhost - - def _enable_redirect(self, domain, unused_options): - """Redirect all equivalent HTTP traffic to ssl_vhost. - - If the vhost is listening plaintextishly, separate out the - relevant directives into a new server block and add a rewrite directive. - - .. note:: This function saves the configuration - - :param str domain: domain to enable redirect for - :param unused_options: Not currently used - :type unused_options: Not Available - """ - - port = self.DEFAULT_LISTEN_PORT - # If there are blocks listening plaintextishly on self.DEFAULT_LISTEN_PORT, - # choose the most name-matching one. - - vhosts = self.choose_redirect_vhosts(domain, port) - - if not vhosts: - logger.info("No matching insecure server blocks listening on port %s found.", - self.DEFAULT_LISTEN_PORT) - return - - for vhost in vhosts: - self._enable_redirect_single(domain, vhost) - - def _enable_redirect_single(self, domain, vhost): - """Redirect all equivalent HTTP traffic to ssl_vhost. - - If the vhost is listening plaintextishly, separate out the - relevant directives into a new server block and add a rewrite directive. - - .. note:: This function saves the configuration - - :param str domain: domain to enable redirect for - :param `~obj.Vhost` vhost: vhost to enable redirect for - """ - - http_vhost = None - if vhost.ssl: - http_vhost, _ = self._split_block(vhost, ['listen', 'server_name']) - - # Add this at the bottom to get the right order of directives - return_404_directive = [['\n ', 'return', ' ', '404']] - self.parser.add_server_directives(http_vhost, return_404_directive) - - vhost = http_vhost - - if self._has_certbot_redirect(vhost, domain): - logger.info("Traffic on port %s already redirecting to ssl in %s", - self.DEFAULT_LISTEN_PORT, vhost.filep) - else: - # Redirect plaintextish host to https - self._add_redirect_block(vhost, domain) - logger.info("Redirecting all traffic on port %s to ssl in %s", - self.DEFAULT_LISTEN_PORT, vhost.filep) - - def _enable_ocsp_stapling(self, domain, chain_path): - """Include OCSP response in TLS handshake - - :param str domain: domain to enable OCSP response for - :param chain_path: chain file path - :type chain_path: `str` or `None` - - """ - vhosts = self.choose_vhosts(domain) - for vhost in vhosts: - self._enable_ocsp_stapling_single(vhost, chain_path) - - def _enable_ocsp_stapling_single(self, vhost, chain_path): - """Include OCSP response in TLS handshake - - :param str vhost: vhost to enable OCSP response for - :param chain_path: chain file path - :type chain_path: `str` or `None` - - """ - if self.version < (1, 3, 7): - raise errors.PluginError("Version 1.3.7 or greater of nginx " - "is needed to enable OCSP stapling") - - if chain_path is None: - raise errors.PluginError( - "--chain-path is required to enable " - "Online Certificate Status Protocol (OCSP) stapling " - "on nginx >= 1.3.7.") - - stapling_directives = [ - ['\n ', 'ssl_trusted_certificate', ' ', chain_path], - ['\n ', 'ssl_stapling', ' ', 'on'], - ['\n ', 'ssl_stapling_verify', ' ', 'on'], ['\n']] - - try: - self.parser.add_server_directives(vhost, - stapling_directives) - except errors.MisconfigurationError as error: - logger.debug(str(error)) - raise errors.PluginError("An error occurred while enabling OCSP " - "stapling for {0}.".format(vhost.names)) - - self.save_notes += ("OCSP Stapling was enabled " - "on SSL Vhost: {0}.\n".format(vhost.filep)) - self.save_notes += "\tssl_trusted_certificate {0}\n".format(chain_path) - self.save_notes += "\tssl_stapling on\n" - self.save_notes += "\tssl_stapling_verify on\n" - - ###################################### - # Nginx server management (IInstaller) - ###################################### - def restart(self): - """Restarts nginx server. - - :raises .errors.MisconfigurationError: If either the reload fails. - - """ - nginx_restart(self.conf('ctl'), self.nginx_conf) - - def config_test(self): # pylint: disable=no-self-use - """Check the configuration of Nginx for errors. - - :raises .errors.MisconfigurationError: If config_test fails - - """ - try: - util.run_script([self.conf('ctl'), "-c", self.nginx_conf, "-t"]) - except errors.SubprocessError as err: - raise errors.MisconfigurationError(str(err)) - - def _verify_setup(self): - """Verify the setup to ensure safe operating environment. - - Make sure that files/directories are setup with appropriate permissions - Aim for defensive coding... make sure all input files - have permissions of root. - - """ - uid = os.geteuid() - util.make_or_verify_dir( - self.config.work_dir, core_constants.CONFIG_DIRS_MODE, uid) - util.make_or_verify_dir( - self.config.backup_dir, core_constants.CONFIG_DIRS_MODE, uid) - util.make_or_verify_dir( - self.config.config_dir, core_constants.CONFIG_DIRS_MODE, uid) - - def get_version(self): - """Return version of Nginx Server. - - Version is returned as tuple. (ie. 2.4.7 = (2, 4, 7)) - - :returns: version - :rtype: tuple - - :raises .PluginError: - Unable to find Nginx version or version is unsupported - - """ - try: - proc = subprocess.Popen( - [self.conf('ctl'), "-c", self.nginx_conf, "-V"], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True) - text = proc.communicate()[1] # nginx prints output to stderr - except (OSError, ValueError) as error: - logger.debug(str(error), exc_info=True) - raise errors.PluginError( - "Unable to run %s -V" % self.conf('ctl')) - - version_regex = re.compile(r"nginx version: ([^/]+)/([0-9\.]*)", re.IGNORECASE) - version_matches = version_regex.findall(text) - - sni_regex = re.compile(r"TLS SNI support enabled", re.IGNORECASE) - sni_matches = sni_regex.findall(text) - - ssl_regex = re.compile(r" --with-http_ssl_module") - ssl_matches = ssl_regex.findall(text) - - if not version_matches: - raise errors.PluginError("Unable to find Nginx version") - if not ssl_matches: - raise errors.PluginError( - "Nginx build is missing SSL module (--with-http_ssl_module).") - if not sni_matches: - raise errors.PluginError("Nginx build doesn't support SNI") - - product_name, product_version = version_matches[0] - if product_name != 'nginx': - logger.warning("NGINX derivative %s is not officially supported by" - " certbot", product_name) - - nginx_version = tuple([int(i) for i in product_version.split(".")]) - - # nginx < 0.8.48 uses machine hostname as default server_name instead of - # the empty string - if nginx_version < (0, 8, 48): - raise errors.NotSupportedError("Nginx version must be 0.8.48+") - - return nginx_version - - def more_info(self): - """Human-readable string to help understand the module""" - return ( - "Configures Nginx to authenticate and install HTTPS.{0}" - "Server root: {root}{0}" - "Version: {version}".format( - os.linesep, root=self.parser.config_root, - version=".".join(str(i) for i in self.version)) - ) - - ################################################### - # Wrapper functions for Reverter class (IInstaller) - ################################################### - def save(self, title=None, temporary=False): - """Saves all changes to the configuration files. - - :param str title: The title of the save. If a title is given, the - configuration will be saved as a new checkpoint and put in a - timestamped directory. - - :param bool temporary: Indicates whether the changes made will - be quickly reversed in the future (ie. challenges) - - :raises .errors.PluginError: If there was an error in - an attempt to save the configuration, or an error creating a - checkpoint - - """ - save_files = set(self.parser.parsed.keys()) - self.add_to_checkpoint(save_files, self.save_notes, temporary) - self.save_notes = "" - - # Change 'ext' to something else to not override existing conf files - self.parser.filedump(ext='') - if title and not temporary: - self.finalize_checkpoint(title) - - def recovery_routine(self): - """Revert all previously modified files. - - Reverts all modified files that have not been saved as a checkpoint - - :raises .errors.PluginError: If unable to recover the configuration - - """ - super(NginxConfigurator, self).recovery_routine() - self.new_vhost = None - self.parser.load() - - def revert_challenge_config(self): - """Used to cleanup challenge configurations. - - :raises .errors.PluginError: If unable to revert the challenge config. - - """ - self.revert_temporary_config() - self.new_vhost = None - self.parser.load() - - def rollback_checkpoints(self, rollback=1): - """Rollback saved checkpoints. - - :param int rollback: Number of checkpoints to revert - - :raises .errors.PluginError: If there is a problem with the input or - the function is unable to correctly revert the configuration - - """ - super(NginxConfigurator, self).rollback_checkpoints(rollback) - self.new_vhost = None - self.parser.load() - - ########################################################################### - # Challenges Section for IAuthenticator - ########################################################################### - def get_chall_pref(self, unused_domain): # pylint: disable=no-self-use - """Return list of challenge preferences.""" - return [challenges.HTTP01, challenges.TLSSNI01] - - # Entry point in main.py for performing challenges - def perform(self, achalls): - """Perform the configuration related challenge. - - This function currently assumes all challenges will be fulfilled. - If this turns out not to be the case in the future. Cleanup and - outstanding challenges will have to be designed better. - - """ - self._chall_out += len(achalls) - responses = [None] * len(achalls) - sni_doer = tls_sni_01.NginxTlsSni01(self) - http_doer = http_01.NginxHttp01(self) - - for i, achall in enumerate(achalls): - # Currently also have chall_doer hold associated index of the - # challenge. This helps to put all of the responses back together - # when they are all complete. - if isinstance(achall.chall, challenges.HTTP01): - http_doer.add_chall(achall, i) - else: # tls-sni-01 - sni_doer.add_chall(achall, i) - - sni_response = sni_doer.perform() - http_response = http_doer.perform() - # Must restart in order to activate the challenges. - # Handled here because we may be able to load up other challenge types - self.restart() - - # Go through all of the challenges and assign them to the proper place - # in the responses return value. All responses must be in the same order - # as the original challenges. - for chall_response, chall_doer in ((sni_response, sni_doer), (http_response, http_doer)): - for i, resp in enumerate(chall_response): - responses[chall_doer.indices[i]] = resp - - return responses - - # called after challenges are performed - def cleanup(self, achalls): - """Revert all challenges.""" - self._chall_out -= len(achalls) - - # If all of the challenges have been finished, clean up everything - if self._chall_out <= 0: - self.revert_challenge_config() - self.restart() - - -def _test_block_from_block(block): - test_block = nginxparser.UnspacedList(block) - parser.comment_directive(test_block, 0) - return test_block[:-1] - - -def _redirect_block_for_domain(domain): - updated_domain = domain - match_symbol = '=' - if util.is_wildcard_domain(domain): - match_symbol = '~' - updated_domain = updated_domain.replace('.', r'\.') - updated_domain = updated_domain.replace('*', '[^.]+') - updated_domain = '^' + updated_domain + '$' - redirect_block = [[ - ['\n ', 'if', ' ', '($host', ' ', match_symbol, ' ', '%s)' % updated_domain, ' '], - [['\n ', 'return', ' ', '301', ' ', 'https://$host$request_uri'], - '\n ']], - ['\n']] - return redirect_block - - -def nginx_restart(nginx_ctl, nginx_conf): - """Restarts the Nginx Server. - - .. todo:: Nginx restart is fatal if the configuration references - non-existent SSL cert/key files. Remove references to /etc/letsencrypt - before restart. - - :param str nginx_ctl: Path to the Nginx binary. - - """ - try: - proc = subprocess.Popen([nginx_ctl, "-c", nginx_conf, "-s", "reload"]) - proc.communicate() - - if proc.returncode != 0: - # Maybe Nginx isn't running - # Write to temporary files instead of piping because of communication issues on Arch - # https://github.com/certbot/certbot/issues/4324 - with tempfile.TemporaryFile() as out: - with tempfile.TemporaryFile() as err: - nginx_proc = subprocess.Popen([nginx_ctl, "-c", nginx_conf], - stdout=out, stderr=err) - nginx_proc.communicate() - if nginx_proc.returncode != 0: - # Enter recovery routine... - raise errors.MisconfigurationError( - "nginx restart failed:\n%s\n%s" % (out.read(), err.read())) - - except (OSError, ValueError): - raise errors.MisconfigurationError("nginx restart failed") - # Nginx can take a moment to recognize a newly added TLS SNI servername, so sleep - # for a second. TODO: Check for expected servername and loop until it - # appears or return an error if looping too long. - time.sleep(1) - - -def install_ssl_options_conf(options_ssl, options_ssl_digest): - """Copy Certbot's SSL options file into the system's config dir if required.""" - return common.install_version_controlled_file(options_ssl, options_ssl_digest, - constants.MOD_SSL_CONF_SRC, constants.ALL_SSL_OPTIONS_HASHES) - -def _determine_default_server_root(): - if os.environ.get("CERTBOT_DOCS") == "1": - default_server_root = "%s or %s" % (constants.LINUX_SERVER_ROOT, - constants.FREEBSD_DARWIN_SERVER_ROOT) - else: - default_server_root = constants.CLI_DEFAULTS["server_root"] - return default_server_root diff --git a/certbot-nginx/certbot_nginx/constants.py b/certbot-nginx/certbot_nginx/constants.py deleted file mode 100644 index d749b6989b8..00000000000 --- a/certbot-nginx/certbot_nginx/constants.py +++ /dev/null @@ -1,58 +0,0 @@ -"""nginx plugin constants.""" -import pkg_resources -import platform - -FREEBSD_DARWIN_SERVER_ROOT = "/usr/local/etc/nginx" -LINUX_SERVER_ROOT = "/etc/nginx" - -if platform.system() in ('FreeBSD', 'Darwin'): - server_root_tmp = FREEBSD_DARWIN_SERVER_ROOT -else: - server_root_tmp = LINUX_SERVER_ROOT - -CLI_DEFAULTS = dict( - server_root=server_root_tmp, - ctl="nginx", -) -"""CLI defaults.""" - - -MOD_SSL_CONF_DEST = "options-ssl-nginx.conf" -"""Name of the mod_ssl config file as saved in `IConfig.config_dir`.""" - -MOD_SSL_CONF_SRC = pkg_resources.resource_filename( - "certbot_nginx", "options-ssl-nginx.conf") -"""Path to the nginx mod_ssl config file found in the Certbot -distribution.""" - -UPDATED_MOD_SSL_CONF_DIGEST = ".updated-options-ssl-nginx-conf-digest.txt" -"""Name of the hash of the updated or informed mod_ssl_conf as saved in `IConfig.config_dir`.""" - - -ALL_SSL_OPTIONS_HASHES = [ - '0f81093a1465e3d4eaa8b0c14e77b2a2e93568b0fc1351c2b87893a95f0de87c', - '9a7b32c49001fed4cff8ad24353329472a50e86ade1ef9b2b9e43566a619612e', - 'a6d9f1c7d6b36749b52ba061fff1421f9a0a3d2cfdafbd63c05d06f65b990937', - '7f95624dd95cf5afc708b9f967ee83a24b8025dc7c8d9df2b556bbc64256b3ff', - '394732f2bbe3e5e637c3fb5c6e980a1f1b90b01e2e8d6b7cff41dde16e2a756d', - '4b16fec2bcbcd8a2f3296d886f17f9953ffdcc0af54582452ca1e52f5f776f16', -] -"""SHA256 hashes of the contents of all versions of MOD_SSL_CONF_SRC""" - -def os_constant(key): - # XXX TODO: In the future, this could return different constants - # based on what OS we are running under. To see an - # approach to how to handle different OSes, see the - # apache version of this file. Currently, we do not - # actually have any OS-specific constants on Nginx. - """ - Get a constant value for operating system - - :param key: name of cli constant - :return: value of constant for active os - """ - return CLI_DEFAULTS[key] - -HSTS_ARGS = ['\"max-age=31536000\"', ' ', 'always'] - -HEADER_ARGS = {'Strict-Transport-Security': HSTS_ARGS} diff --git a/certbot-nginx/certbot_nginx/display_ops.py b/certbot-nginx/certbot_nginx/display_ops.py deleted file mode 100644 index 5d6bda6b061..00000000000 --- a/certbot-nginx/certbot_nginx/display_ops.py +++ /dev/null @@ -1,44 +0,0 @@ -"""Contains UI methods for Nginx operations.""" -import logging - -import zope.component - -from certbot import interfaces - -import certbot.display.util as display_util - - -logger = logging.getLogger(__name__) - - -def select_vhost_multiple(vhosts): - """Select multiple Vhosts to install the certificate for - :param vhosts: Available Nginx VirtualHosts - :type vhosts: :class:`list` of type `~obj.Vhost` - :returns: List of VirtualHosts - :rtype: :class:`list`of type `~obj.Vhost` - """ - if not vhosts: - return list() - tags_list = [vhost.display_repr()+"\n" for vhost in vhosts] - # Remove the extra newline from the last entry - if len(tags_list): - tags_list[-1] = tags_list[-1][:-1] - code, names = zope.component.getUtility(interfaces.IDisplay).checklist( - "Which server blocks would you like to modify?", - tags=tags_list, force_interactive=True) - if code == display_util.OK: - return_vhosts = _reversemap_vhosts(names, vhosts) - return return_vhosts - return [] - -def _reversemap_vhosts(names, vhosts): - """Helper function for select_vhost_multiple for mapping string - representations back to actual vhost objects""" - return_vhosts = list() - - for selection in names: - for vhost in vhosts: - if vhost.display_repr().strip() == selection.strip(): - return_vhosts.append(vhost) - return return_vhosts diff --git a/certbot-nginx/certbot_nginx/http_01.py b/certbot-nginx/certbot_nginx/http_01.py deleted file mode 100644 index e46d7b9b947..00000000000 --- a/certbot-nginx/certbot_nginx/http_01.py +++ /dev/null @@ -1,206 +0,0 @@ -"""A class that performs HTTP-01 challenges for Nginx""" - -import logging -import os - -from acme import challenges - -from certbot import errors -from certbot.plugins import common - -from certbot_nginx import obj -from certbot_nginx import nginxparser -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module - - -logger = logging.getLogger(__name__) - - -class NginxHttp01(common.ChallengePerformer): - """HTTP-01 authenticator for Nginx - - :ivar configurator: NginxConfigurator object - :type configurator: :class:`~nginx.configurator.NginxConfigurator` - - :ivar list achalls: Annotated - class:`~certbot.achallenges.KeyAuthorizationAnnotatedChallenge` - challenges - - :param list indices: Meant to hold indices of challenges in a - larger array. NginxHttp01 is capable of solving many challenges - at once which causes an indexing issue within NginxConfigurator - who must return all responses in order. Imagine NginxConfigurator - maintaining state about where all of the http-01 Challenges, - TLS-SNI-01 Challenges belong in the response array. This is an - optional utility. - - """ - - def __init__(self, configurator): - super(NginxHttp01, self).__init__(configurator) - self.challenge_conf = os.path.join( - configurator.config.config_dir, "le_http_01_cert_challenge.conf") - - def perform(self): - """Perform a challenge on Nginx. - - :returns: list of :class:`certbot.acme.challenges.HTTP01Response` - :rtype: list - - """ - if not self.achalls: - return [] - - responses = [x.response(x.account_key) for x in self.achalls] - - # Set up the configuration - self._mod_config() - - # Save reversible changes - self.configurator.save("HTTP Challenge", True) - - return responses - - def _mod_config(self): - """Modifies Nginx config to include server_names_hash_bucket_size directive - and server challenge blocks. - - :raises .MisconfigurationError: - Unable to find a suitable HTTP block in which to include - authenticator hosts. - """ - included = False - include_directive = ['\n', 'include', ' ', self.challenge_conf] - root = self.configurator.parser.config_root - - bucket_directive = ['\n', 'server_names_hash_bucket_size', ' ', '128'] - - main = self.configurator.parser.parsed[root] - for line in main: - if line[0] == ['http']: - body = line[1] - found_bucket = False - posn = 0 - for inner_line in body: - if inner_line[0] == bucket_directive[1]: - if int(inner_line[1]) < int(bucket_directive[3]): - body[posn] = bucket_directive - found_bucket = True - posn += 1 - if not found_bucket: - body.insert(0, bucket_directive) - if include_directive not in body: - body.insert(0, include_directive) - included = True - break - if not included: - raise errors.MisconfigurationError( - 'Certbot could not find a block to include ' - 'challenges in %s.' % root) - config = [self._make_or_mod_server_block(achall) for achall in self.achalls] - config = [x for x in config if x is not None] - config = nginxparser.UnspacedList(config) - logger.debug("Generated server block:\n%s", str(config)) - - self.configurator.reverter.register_file_creation( - True, self.challenge_conf) - - with open(self.challenge_conf, "w") as new_conf: - nginxparser.dump(config, new_conf) - - def _default_listen_addresses(self): - """Finds addresses for a challenge block to listen on. - :returns: list of :class:`certbot_nginx.obj.Addr` to apply - :rtype: list - """ - addresses = [] # type: List[obj.Addr] - default_addr = "%s" % self.configurator.config.http01_port - ipv6_addr = "[::]:{0}".format( - self.configurator.config.http01_port) - port = self.configurator.config.http01_port - - ipv6, ipv6only = self.configurator.ipv6_info(port) - - if ipv6: - # If IPv6 is active in Nginx configuration - if not ipv6only: - # If ipv6only=on is not already present in the config - ipv6_addr = ipv6_addr + " ipv6only=on" - addresses = [obj.Addr.fromstring(default_addr), - obj.Addr.fromstring(ipv6_addr)] - logger.info(("Using default addresses %s and %s for authentication."), - default_addr, - ipv6_addr) - else: - addresses = [obj.Addr.fromstring(default_addr)] - logger.info("Using default address %s for authentication.", - default_addr) - return addresses - - def _get_validation_path(self, achall): - return os.sep + os.path.join(challenges.HTTP01.URI_ROOT_PATH, achall.chall.encode("token")) - - def _make_server_block(self, achall): - """Creates a server block for a challenge. - :param achall: Annotated HTTP-01 challenge - :type achall: - :class:`certbot.achallenges.KeyAuthorizationAnnotatedChallenge` - :param list addrs: addresses of challenged domain - :class:`list` of type :class:`~nginx.obj.Addr` - :returns: server block for the challenge host - :rtype: list - """ - addrs = self._default_listen_addresses() - block = [['listen', ' ', addr.to_string(include_default=False)] for addr in addrs] - - # Ensure we 404 on any other request by setting a root - document_root = os.path.join( - self.configurator.config.work_dir, "http_01_nonexistent") - - block.extend([['server_name', ' ', achall.domain], - ['root', ' ', document_root], - self._location_directive_for_achall(achall) - ]) - # TODO: do we want to return something else if they otherwise access this block? - return [['server'], block] - - def _location_directive_for_achall(self, achall): - validation = achall.validation(achall.account_key) - validation_path = self._get_validation_path(achall) - - location_directive = [['location', ' ', '=', ' ', validation_path], - [['default_type', ' ', 'text/plain'], - ['return', ' ', '200', ' ', validation]]] - return location_directive - - - def _make_or_mod_server_block(self, achall): - """Modifies a server block to respond to a challenge. - - :param achall: Annotated HTTP-01 challenge - :type achall: - :class:`certbot.achallenges.KeyAuthorizationAnnotatedChallenge` - - """ - try: - vhosts = self.configurator.choose_redirect_vhosts(achall.domain, - '%i' % self.configurator.config.http01_port, create_if_no_match=True) - except errors.MisconfigurationError: - # Couldn't find either a matching name+port server block - # or a port+default_server block, so create a dummy block - return self._make_server_block(achall) - - # len is max 1 because Nginx doesn't authenticate wildcards - # if len were or vhosts None, we would have errored - vhost = vhosts[0] - - # Modify existing server block - location_directive = [self._location_directive_for_achall(achall)] - - self.configurator.parser.add_server_directives(vhost, - location_directive) - - rewrite_directive = [['rewrite', ' ', '^(/.well-known/acme-challenge/.*)', - ' ', '$1', ' ', 'break']] - self.configurator.parser.add_server_directives(vhost, - rewrite_directive, insert_at_top=True) diff --git a/certbot-nginx/certbot_nginx/nginxparser.py b/certbot-nginx/certbot_nginx/nginxparser.py deleted file mode 100644 index bfb75adcc14..00000000000 --- a/certbot-nginx/certbot_nginx/nginxparser.py +++ /dev/null @@ -1,269 +0,0 @@ -"""Very low-level nginx config parser based on pyparsing.""" -# Forked from https://github.com/fatiherikli/nginxparser (MIT Licensed) -import copy -import logging - -from pyparsing import ( - Literal, White, Forward, Group, Optional, OneOrMore, QuotedString, Regex, ZeroOrMore, Combine) -from pyparsing import stringEnd -from pyparsing import restOfLine -import six - -logger = logging.getLogger(__name__) - -class RawNginxParser(object): - # pylint: disable=expression-not-assigned - # pylint: disable=pointless-statement - """A class that parses nginx configuration with pyparsing.""" - - # constants - space = Optional(White()).leaveWhitespace() - required_space = White().leaveWhitespace() - - left_bracket = Literal("{").suppress() - right_bracket = space + Literal("}").suppress() - semicolon = Literal(";").suppress() - dquoted = QuotedString('"', multiline=True, unquoteResults=False, escChar='\\') - squoted = QuotedString("'", multiline=True, unquoteResults=False, escChar='\\') - quoted = dquoted | squoted - head_tokenchars = Regex(r"(\$\{)|[^{};\s'\"]") # if (last_space) - tail_tokenchars = Regex(r"(\$\{)|[^{;\s]") # else - tokenchars = Combine(head_tokenchars + ZeroOrMore(tail_tokenchars)) - paren_quote_extend = Combine(quoted + Literal(')') + ZeroOrMore(tail_tokenchars)) - # note: ')' allows extension, but then we fall into else, not last_space. - - token = paren_quote_extend | tokenchars | quoted - - whitespace_token_group = space + token + ZeroOrMore(required_space + token) + space - assignment = whitespace_token_group + semicolon - - comment = space + Literal('#') + restOfLine - - block = Forward() - - # order matters! see issue 518, and also http { # server { \n} - contents = Group(comment) | Group(block) | Group(assignment) - - block_begin = Group(whitespace_token_group) - block_innards = Group(ZeroOrMore(contents) + space).leaveWhitespace() - block << block_begin + left_bracket + block_innards + right_bracket - - script = OneOrMore(contents) + space + stringEnd - script.parseWithTabs().leaveWhitespace() - - def __init__(self, source): - self.source = source - - def parse(self): - """Returns the parsed tree.""" - return self.script.parseString(self.source) - - def as_list(self): - """Returns the parsed tree as a list.""" - return self.parse().asList() - -class RawNginxDumper(object): - # pylint: disable=too-few-public-methods - """A class that dumps nginx configuration from the provided tree.""" - def __init__(self, blocks): - self.blocks = blocks - - def __iter__(self, blocks=None): - """Iterates the dumped nginx content.""" - blocks = blocks or self.blocks - for b0 in blocks: - if isinstance(b0, six.string_types): - yield b0 - continue - item = copy.deepcopy(b0) - if spacey(item[0]): - yield item.pop(0) # indentation - if not item: - continue - - if isinstance(item[0], list): # block - yield "".join(item.pop(0)) + '{' - for parameter in item.pop(0): - for line in self.__iter__([parameter]): # negate "for b0 in blocks" - yield line - yield '}' - else: # not a block - list of strings - semicolon = ";" - if isinstance(item[0], six.string_types) and item[0].strip() == '#': # comment - semicolon = "" - yield "".join(item) + semicolon - - def __str__(self): - """Return the parsed block as a string.""" - return ''.join(self) - - -# Shortcut functions to respect Python's serialization interface -# (like pyyaml, picker or json) - -def loads(source): - """Parses from a string. - - :param str source: The string to parse - :returns: The parsed tree - :rtype: list - - """ - return UnspacedList(RawNginxParser(source).as_list()) - - -def load(_file): - """Parses from a file. - - :param file _file: The file to parse - :returns: The parsed tree - :rtype: list - - """ - return loads(_file.read()) - - -def dumps(blocks): - """Dump to a string. - - :param UnspacedList block: The parsed tree - :param int indentation: The number of spaces to indent - :rtype: str - - """ - return str(RawNginxDumper(blocks.spaced)) - - -def dump(blocks, _file): - """Dump to a file. - - :param UnspacedList block: The parsed tree - :param file _file: The file to dump to - :param int indentation: The number of spaces to indent - :rtype: NoneType - - """ - return _file.write(dumps(blocks)) - - -spacey = lambda x: (isinstance(x, six.string_types) and x.isspace()) or x == '' - -class UnspacedList(list): - """Wrap a list [of lists], making any whitespace entries magically invisible""" - - def __init__(self, list_source): - # ensure our argument is not a generator, and duplicate any sublists - self.spaced = copy.deepcopy(list(list_source)) - self.dirty = False - - # Turn self into a version of the source list that has spaces removed - # and all sub-lists also UnspacedList()ed - list.__init__(self, list_source) - for i, entry in reversed(list(enumerate(self))): - if isinstance(entry, list): - sublist = UnspacedList(entry) - list.__setitem__(self, i, sublist) - self.spaced[i] = sublist.spaced - elif spacey(entry): - # don't delete comments - if "#" not in self[:i]: - list.__delitem__(self, i) - - def _coerce(self, inbound): - """ - Coerce some inbound object to be appropriately usable in this object - - :param inbound: string or None or list or UnspacedList - :returns: (coerced UnspacedList or string or None, spaced equivalent) - :rtype: tuple - - """ - if not isinstance(inbound, list): # str or None - return (inbound, inbound) - else: - if not hasattr(inbound, "spaced"): - inbound = UnspacedList(inbound) - return (inbound, inbound.spaced) - - - def insert(self, i, x): - item, spaced_item = self._coerce(x) - slicepos = self._spaced_position(i) if i < len(self) else len(self.spaced) - self.spaced.insert(slicepos, spaced_item) - if not spacey(item): - list.insert(self, i, item) - self.dirty = True - - def append(self, x): - item, spaced_item = self._coerce(x) - self.spaced.append(spaced_item) - if not spacey(item): - list.append(self, item) - self.dirty = True - - def extend(self, x): - item, spaced_item = self._coerce(x) - self.spaced.extend(spaced_item) - list.extend(self, item) - self.dirty = True - - def __add__(self, other): - l = copy.deepcopy(self) - l.extend(other) - l.dirty = True - return l - - def pop(self, _i=None): - raise NotImplementedError("UnspacedList.pop() not yet implemented") - def remove(self, _): - raise NotImplementedError("UnspacedList.remove() not yet implemented") - def reverse(self): - raise NotImplementedError("UnspacedList.reverse() not yet implemented") - def sort(self, _cmp=None, _key=None, _Rev=None): - raise NotImplementedError("UnspacedList.sort() not yet implemented") - def __setslice__(self, _i, _j, _newslice): - raise NotImplementedError("Slice operations on UnspacedLists not yet implemented") - - def __setitem__(self, i, value): - if isinstance(i, slice): - raise NotImplementedError("Slice operations on UnspacedLists not yet implemented") - item, spaced_item = self._coerce(value) - self.spaced.__setitem__(self._spaced_position(i), spaced_item) - if not spacey(item): - list.__setitem__(self, i, item) - self.dirty = True - - def __delitem__(self, i): - self.spaced.__delitem__(self._spaced_position(i)) - list.__delitem__(self, i) - self.dirty = True - - def __deepcopy__(self, memo): - new_spaced = copy.deepcopy(self.spaced, memo=memo) - l = UnspacedList(new_spaced) - l.dirty = self.dirty - return l - - def is_dirty(self): - """Recurse through the parse tree to figure out if any sublists are dirty""" - if self.dirty: - return True - return any((isinstance(x, UnspacedList) and x.is_dirty() for x in self)) - - def _spaced_position(self, idx): - "Convert from indexes in the unspaced list to positions in the spaced one" - pos = spaces = 0 - # Normalize indexes like list[-1] etc, and save the result - if idx < 0: - idx = len(self) + idx - if not 0 <= idx < len(self): - raise IndexError("list index out of range") - idx0 = idx - # Count the number of spaces in the spaced list before idx in the unspaced one - while idx != -1: - if spacey(self.spaced[pos]): - spaces += 1 - else: - idx -= 1 - pos += 1 - return idx0 + spaces diff --git a/certbot-nginx/certbot_nginx/obj.py b/certbot-nginx/certbot_nginx/obj.py deleted file mode 100644 index 8868fcfadf3..00000000000 --- a/certbot-nginx/certbot_nginx/obj.py +++ /dev/null @@ -1,263 +0,0 @@ -"""Module contains classes used by the Nginx Configurator.""" -import re - -import six - -from certbot.plugins import common - -ADD_HEADER_DIRECTIVE = 'add_header' - -class Addr(common.Addr): - r"""Represents an Nginx address, i.e. what comes after the 'listen' - directive. - - According to the `documentation`_, this may be address[:port], port, - or unix:path. The latter is ignored here. - - The default value if no directive is specified is \*:80 (superuser) - or \*:8000 (otherwise). If no port is specified, the default is - 80. If no address is specified, listen on all addresses. - - .. _documentation: - http://nginx.org/en/docs/http/ngx_http_core_module.html#listen - - .. todo:: Old-style nginx configs define SSL vhosts in a separate - block instead of using 'ssl' in the listen directive. - - :param str addr: addr part of vhost address, may be hostname, IPv4, IPv6, - "", or "\*" - :param str port: port number or "\*" or "" - :param bool ssl: Whether the directive includes 'ssl' - :param bool default: Whether the directive includes 'default_server' - :param bool default: Whether this is an IPv6 address - :param bool ipv6only: Whether the directive includes 'ipv6only=on' - - """ - UNSPECIFIED_IPV4_ADDRESSES = ('', '*', '0.0.0.0') - CANONICAL_UNSPECIFIED_ADDRESS = UNSPECIFIED_IPV4_ADDRESSES[0] - - def __init__(self, host, port, ssl, default, ipv6, ipv6only): - # pylint: disable=too-many-arguments - super(Addr, self).__init__((host, port)) - self.ssl = ssl - self.default = default - self.ipv6 = ipv6 - self.ipv6only = ipv6only - self.unspecified_address = host in self.UNSPECIFIED_IPV4_ADDRESSES - - @classmethod - def fromstring(cls, str_addr): - """Initialize Addr from string.""" - parts = str_addr.split(' ') - ssl = False - default = False - ipv6 = False - ipv6only = False - host = '' - port = '' - - # The first part must be the address - addr = parts.pop(0) - - # Ignore UNIX-domain sockets - if addr.startswith('unix:'): - return None - - # IPv6 check - ipv6_match = re.match(r'\[.*\]', addr) - if ipv6_match: - ipv6 = True - # IPv6 handling - host = ipv6_match.group() - # The rest of the addr string will be the port, if any - port = addr[ipv6_match.end()+1:] - else: - # IPv4 handling - tup = addr.partition(':') - if re.match(r'^\d+$', tup[0]): - # This is a bare port, not a hostname. E.g. listen 80 - host = '' - port = tup[0] - else: - # This is a host-port tuple. E.g. listen 127.0.0.1:* - host = tup[0] - port = tup[2] - - # The rest of the parts are options; we only care about ssl and default - while len(parts) > 0: - nextpart = parts.pop() - if nextpart == 'ssl': - ssl = True - elif nextpart == 'default_server': - default = True - elif nextpart == 'default': - default = True - elif nextpart == "ipv6only=on": - ipv6only = True - - return cls(host, port, ssl, default, ipv6, ipv6only) - - def to_string(self, include_default=True): - """Return string representation of Addr""" - parts = '' - if self.tup[0] and self.tup[1]: - parts = "%s:%s" % self.tup - elif self.tup[0]: - parts = self.tup[0] - else: - parts = self.tup[1] - - if self.default and include_default: - parts += ' default_server' - if self.ssl: - parts += ' ssl' - - return parts - - def __str__(self): - return self.to_string() - - def __repr__(self): - return "Addr(" + self.__str__() + ")" - - def __hash__(self): - # Python 3 requires explicit overridden for __hash__ - # See certbot-apache/certbot_apache/obj.py for more information - return super(Addr, self).__hash__() - - def super_eq(self, other): - """Check ip/port equality, with IPv6 support. - """ - # If both addresses got an unspecified address, then make sure the - # host representation in each match when doing the comparison. - if self.unspecified_address and other.unspecified_address: - return common.Addr((self.CANONICAL_UNSPECIFIED_ADDRESS, - self.tup[1]), self.ipv6) == \ - common.Addr((other.CANONICAL_UNSPECIFIED_ADDRESS, - other.tup[1]), other.ipv6) - return super(Addr, self).__eq__(other) - - def __eq__(self, other): - if isinstance(other, self.__class__): - return (self.super_eq(other) and - self.ssl == other.ssl and - self.default == other.default) - return False - - -class VirtualHost(object): # pylint: disable=too-few-public-methods - """Represents an Nginx Virtualhost. - - :ivar str filep: file path of VH - :ivar set addrs: Virtual Host addresses (:class:`set` of :class:`Addr`) - :ivar set names: Server names/aliases of vhost - (:class:`list` of :class:`str`) - :ivar list raw: The raw form of the parsed server block - - :ivar bool ssl: SSLEngine on in vhost - :ivar bool enabled: Virtual host is enabled - :ivar list path: The indices into the parsed file used to access - the server block defining the vhost - - """ - - def __init__(self, filep, addrs, ssl, enabled, names, raw, path): - # pylint: disable=too-many-arguments - """Initialize a VH.""" - self.filep = filep - self.addrs = addrs - self.names = names - self.ssl = ssl - self.enabled = enabled - self.raw = raw - self.path = path - - def __str__(self): - addr_str = ", ".join(str(addr) for addr in sorted(self.addrs, key=str)) - # names might be a set, and it has different representations in Python - # 2 and 3. Force it to be a list here for consistent outputs - return ("file: %s\n" - "addrs: %s\n" - "names: %s\n" - "ssl: %s\n" - "enabled: %s" % (self.filep, addr_str, - list(self.names), self.ssl, self.enabled)) - - def __repr__(self): - return "VirtualHost(" + self.__str__().replace("\n", ", ") + ")\n" - - def __eq__(self, other): - if isinstance(other, self.__class__): - return (self.filep == other.filep and - sorted(self.addrs, key=str) == sorted(other.addrs, key=str) and - self.names == other.names and - self.ssl == other.ssl and - self.enabled == other.enabled and - self.path == other.path) - - return False - - def __hash__(self): - return hash((self.filep, tuple(self.path), - tuple(self.addrs), tuple(self.names), - self.ssl, self.enabled)) - - def has_header(self, header_name): - """Determine if this server block has a particular header set. - :param str header_name: The name of the header to check for, e.g. - 'Strict-Transport-Security' - """ - found = _find_directive(self.raw, ADD_HEADER_DIRECTIVE, header_name) - return found is not None - - def contains_list(self, test): - """Determine if raw server block contains test list at top level - """ - for i in six.moves.range(0, len(self.raw) - len(test) + 1): - if self.raw[i:i + len(test)] == test: - return True - return False - - def ipv6_enabled(self): - """Return true if one or more of the listen directives in vhost supports - IPv6""" - for a in self.addrs: - if a.ipv6: - return True - - def ipv4_enabled(self): - """Return true if one or more of the listen directives in vhost are IPv4 - only""" - if self.addrs is None or len(self.addrs) == 0: - return True - for a in self.addrs: - if not a.ipv6: - return True - - def display_repr(self): - """Return a representation of VHost to be used in dialog""" - return ( - "File: {filename}\n" - "Addresses: {addrs}\n" - "Names: {names}\n" - "HTTPS: {https}\n".format( - filename=self.filep, - addrs=", ".join(str(addr) for addr in self.addrs), - names=", ".join(self.names), - https="Yes" if self.ssl else "No")) - -def _find_directive(directives, directive_name, match_content=None): - """Find a directive of type directive_name in directives. If match_content is given, - Searches for `match_content` in the directive arguments. - """ - if not directives or isinstance(directives, six.string_types) or len(directives) == 0: - return None - - # If match_content is None, just match on directive type. Otherwise, match on - # both directive type -and- the content! - if directives[0] == directive_name and \ - (match_content is None or match_content in directives): - return directives - - matches = (_find_directive(line, directive_name, match_content) for line in directives) - return next((m for m in matches if m is not None), None) diff --git a/certbot-nginx/certbot_nginx/options-ssl-nginx.conf b/certbot-nginx/certbot_nginx/options-ssl-nginx.conf deleted file mode 100644 index 292d42984ab..00000000000 --- a/certbot-nginx/certbot_nginx/options-ssl-nginx.conf +++ /dev/null @@ -1,13 +0,0 @@ -# This file contains important security parameters. If you modify this file -# manually, Certbot will be unable to automatically provide future security -# updates. Instead, Certbot will print and log an error message with a path to -# the up-to-date file that you will need to refer to when manually updating -# this file. - -ssl_session_cache shared:le_nginx_SSL:1m; -ssl_session_timeout 1440m; - -ssl_protocols TLSv1 TLSv1.1 TLSv1.2; -ssl_prefer_server_ciphers on; - -ssl_ciphers "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS"; diff --git a/certbot-nginx/certbot_nginx/parser.py b/certbot-nginx/certbot_nginx/parser.py deleted file mode 100644 index 622eb8d5549..00000000000 --- a/certbot-nginx/certbot_nginx/parser.py +++ /dev/null @@ -1,765 +0,0 @@ -"""NginxParser is a member object of the NginxConfigurator class.""" -import copy -import functools -import glob -import logging -import os -import pyparsing -import re - -import six - -from certbot import errors - -from certbot_nginx import obj -from certbot_nginx import nginxparser -from acme.magic_typing import Union, Dict, Set, Any, List, Tuple # pylint: disable=unused-import, no-name-in-module - -logger = logging.getLogger(__name__) - - -class NginxParser(object): - """Class handles the fine details of parsing the Nginx Configuration. - - :ivar str root: Normalized absolute path to the server root - directory. Without trailing slash. - :ivar dict parsed: Mapping of file paths to parsed trees - - """ - - def __init__(self, root): - self.parsed = {} # type: Dict[str, Union[List, nginxparser.UnspacedList]] - self.root = os.path.abspath(root) - self.config_root = self._find_config_root() - - # Parse nginx.conf and included files. - # TODO: Check sites-available/ as well. For now, the configurator does - # not enable sites from there. - self.load() - - def load(self): - """Loads Nginx files into a parsed tree. - - """ - self.parsed = {} - self._parse_recursively(self.config_root) - - def _parse_recursively(self, filepath): - """Parses nginx config files recursively by looking at 'include' - directives inside 'http' and 'server' blocks. Note that this only - reads Nginx files that potentially declare a virtual host. - - :param str filepath: The path to the files to parse, as a glob - - """ - filepath = self.abs_path(filepath) - trees = self._parse_files(filepath) - for tree in trees: - for entry in tree: - if _is_include_directive(entry): - # Parse the top-level included file - self._parse_recursively(entry[1]) - elif entry[0] == ['http'] or entry[0] == ['server']: - # Look for includes in the top-level 'http'/'server' context - for subentry in entry[1]: - if _is_include_directive(subentry): - self._parse_recursively(subentry[1]) - elif entry[0] == ['http'] and subentry[0] == ['server']: - # Look for includes in a 'server' context within - # an 'http' context - for server_entry in subentry[1]: - if _is_include_directive(server_entry): - self._parse_recursively(server_entry[1]) - - def abs_path(self, path): - """Converts a relative path to an absolute path relative to the root. - Does nothing for paths that are already absolute. - - :param str path: The path - :returns: The absolute path - :rtype: str - - """ - if not os.path.isabs(path): - return os.path.join(self.root, path) - else: - return path - - def _build_addr_to_ssl(self): - """Builds a map from address to whether it listens on ssl in any server block - """ - servers = self._get_raw_servers() - - addr_to_ssl = {} # type: Dict[Tuple[str, str], bool] - for filename in servers: - for server, _ in servers[filename]: - # Parse the server block to save addr info - parsed_server = _parse_server_raw(server) - for addr in parsed_server['addrs']: - addr_tuple = addr.normalized_tuple() - if addr_tuple not in addr_to_ssl: - addr_to_ssl[addr_tuple] = addr.ssl - addr_to_ssl[addr_tuple] = addr.ssl or addr_to_ssl[addr_tuple] - return addr_to_ssl - - def _get_raw_servers(self): - # pylint: disable=cell-var-from-loop - # type: () -> Dict - """Get a map of unparsed all server blocks - """ - servers = {} # type: Dict[str, Union[List, nginxparser.UnspacedList]] - for filename in self.parsed: - tree = self.parsed[filename] - servers[filename] = [] - srv = servers[filename] # workaround undefined loop var in lambdas - - # Find all the server blocks - _do_for_subarray(tree, lambda x: len(x) >= 2 and x[0] == ['server'], - lambda x, y: srv.append((x[1], y))) - - # Find 'include' statements in server blocks and append their trees - for i, (server, path) in enumerate(servers[filename]): - new_server = self._get_included_directives(server) - servers[filename][i] = (new_server, path) - return servers - - def get_vhosts(self): - # pylint: disable=cell-var-from-loop - """Gets list of all 'virtual hosts' found in Nginx configuration. - Technically this is a misnomer because Nginx does not have virtual - hosts, it has 'server blocks'. - - :returns: List of :class:`~certbot_nginx.obj.VirtualHost` - objects found in configuration - :rtype: list - - """ - enabled = True # We only look at enabled vhosts for now - servers = self._get_raw_servers() - - vhosts = [] - for filename in servers: - for server, path in servers[filename]: - # Parse the server block into a VirtualHost object - - parsed_server = _parse_server_raw(server) - vhost = obj.VirtualHost(filename, - parsed_server['addrs'], - parsed_server['ssl'], - enabled, - parsed_server['names'], - server, - path) - vhosts.append(vhost) - - self._update_vhosts_addrs_ssl(vhosts) - - return vhosts - - def _update_vhosts_addrs_ssl(self, vhosts): - """Update a list of raw parsed vhosts to include global address sslishness - """ - addr_to_ssl = self._build_addr_to_ssl() - for vhost in vhosts: - for addr in vhost.addrs: - addr.ssl = addr_to_ssl[addr.normalized_tuple()] - if addr.ssl: - vhost.ssl = True - - def _get_included_directives(self, block): - """Returns array with the "include" directives expanded out by - concatenating the contents of the included file to the block. - - :param list block: - :rtype: list - - """ - result = copy.deepcopy(block) # Copy the list to keep self.parsed idempotent - for directive in block: - if _is_include_directive(directive): - included_files = glob.glob( - self.abs_path(directive[1])) - for incl in included_files: - try: - result.extend(self.parsed[incl]) - except KeyError: - pass - return result - - def _parse_files(self, filepath, override=False): - """Parse files from a glob - - :param str filepath: Nginx config file path - :param bool override: Whether to parse a file that has been parsed - :returns: list of parsed tree structures - :rtype: list - - """ - files = glob.glob(filepath) # nginx on unix calls glob(3) for this - # XXX Windows nginx uses FindFirstFile, and - # should have a narrower call here - trees = [] - for item in files: - if item in self.parsed and not override: - continue - try: - with open(item) as _file: - parsed = nginxparser.load(_file) - self.parsed[item] = parsed - trees.append(parsed) - except IOError: - logger.warning("Could not open file: %s", item) - except pyparsing.ParseException as err: - logger.debug("Could not parse file: %s due to %s", item, err) - return trees - - def _find_config_root(self): - """Return the Nginx Configuration Root file.""" - location = ['nginx.conf'] - - for name in location: - if os.path.isfile(os.path.join(self.root, name)): - return os.path.join(self.root, name) - - raise errors.NoInstallationError( - "Could not find Nginx root configuration file (nginx.conf)") - - def filedump(self, ext='tmp', lazy=True): - """Dumps parsed configurations into files. - - :param str ext: The file extension to use for the dumped files. If - empty, this overrides the existing conf files. - :param bool lazy: Only write files that have been modified - - """ - # Best-effort atomicity is enforced above us by reverter.py - for filename in self.parsed: - tree = self.parsed[filename] - if ext: - filename = filename + os.path.extsep + ext - try: - if lazy and not tree.is_dirty(): - continue - out = nginxparser.dumps(tree) - logger.debug('Writing nginx conf tree to %s:\n%s', filename, out) - with open(filename, 'w') as _file: - _file.write(out) - - except IOError: - logger.error("Could not open file for writing: %s", filename) - - def parse_server(self, server): - """Parses a list of server directives, accounting for global address sslishness. - - :param list server: list of directives in a server block - :rtype: dict - """ - addr_to_ssl = self._build_addr_to_ssl() - parsed_server = _parse_server_raw(server) - _apply_global_addr_ssl(addr_to_ssl, parsed_server) - return parsed_server - - def has_ssl_on_directive(self, vhost): - """Does vhost have ssl on for all ports? - - :param :class:`~certbot_nginx.obj.VirtualHost` vhost: The vhost in question - - :returns: True if 'ssl on' directive is included - :rtype: bool - - """ - server = vhost.raw - for directive in server: - if not directive: - continue - elif _is_ssl_on_directive(directive): - return True - - return False - - def add_server_directives(self, vhost, directives, insert_at_top=False): - """Add directives to the server block identified by vhost. - - This method modifies vhost to be fully consistent with the new directives. - - ..note :: It's an error to try and add a nonrepeatable directive that already - exists in the config block with a conflicting value. - - ..todo :: Doesn't match server blocks whose server_name directives are - split across multiple conf files. - - :param :class:`~certbot_nginx.obj.VirtualHost` vhost: The vhost - whose information we use to match on - :param list directives: The directives to add - :param bool insert_at_top: True if the directives need to be inserted at the top - of the server block instead of the bottom - - """ - self._modify_server_directives(vhost, - functools.partial(_add_directives, directives, insert_at_top)) - - def update_or_add_server_directives(self, vhost, directives, insert_at_top=False): - """Add or replace directives in the server block identified by vhost. - - This method modifies vhost to be fully consistent with the new directives. - - ..note :: When a directive with the same name already exists in the - config block, the first instance will be replaced. Otherwise, the directive - will be appended/prepended to the config block as in add_server_directives. - - ..todo :: Doesn't match server blocks whose server_name directives are - split across multiple conf files. - - :param :class:`~certbot_nginx.obj.VirtualHost` vhost: The vhost - whose information we use to match on - :param list directives: The directives to add - :param bool insert_at_top: True if the directives need to be inserted at the top - of the server block instead of the bottom - - """ - self._modify_server_directives(vhost, - functools.partial(_update_or_add_directives, directives, insert_at_top)) - - def remove_server_directives(self, vhost, directive_name, match_func=None): - """Remove all directives of type directive_name. - - :param :class:`~certbot_nginx.obj.VirtualHost` vhost: The vhost - to remove directives from - :param string directive_name: The directive type to remove - :param callable match_func: Function of the directive that returns true for directives - to be deleted. - """ - self._modify_server_directives(vhost, - functools.partial(_remove_directives, directive_name, match_func)) - - def _update_vhost_based_on_new_directives(self, vhost, directives_list): - new_server = self._get_included_directives(directives_list) - parsed_server = self.parse_server(new_server) - vhost.addrs = parsed_server['addrs'] - vhost.ssl = parsed_server['ssl'] - vhost.names = parsed_server['names'] - vhost.raw = new_server - - def _modify_server_directives(self, vhost, block_func): - filename = vhost.filep - try: - result = self.parsed[filename] - for index in vhost.path: - result = result[index] - if not isinstance(result, list) or len(result) != 2: - raise errors.MisconfigurationError("Not a server block.") - result = result[1] - block_func(result) - - self._update_vhost_based_on_new_directives(vhost, result) - except errors.MisconfigurationError as err: - raise errors.MisconfigurationError("Problem in %s: %s" % (filename, str(err))) - - def duplicate_vhost(self, vhost_template, remove_singleton_listen_params=False, - only_directives=None): - """Duplicate the vhost in the configuration files. - - :param :class:`~certbot_nginx.obj.VirtualHost` vhost_template: The vhost - whose information we copy - :param bool remove_singleton_listen_params: If we should remove parameters - from listen directives in the block that can only be used once per address - :param list only_directives: If it exists, only duplicate the named directives. Only - looks at first level of depth; does not expand includes. - - :returns: A vhost object for the newly created vhost - :rtype: :class:`~certbot_nginx.obj.VirtualHost` - """ - # TODO: https://github.com/certbot/certbot/issues/5185 - # put it in the same file as the template, at the same level - new_vhost = copy.deepcopy(vhost_template) - - enclosing_block = self.parsed[vhost_template.filep] - for index in vhost_template.path[:-1]: - enclosing_block = enclosing_block[index] - raw_in_parsed = copy.deepcopy(enclosing_block[vhost_template.path[-1]]) - - if only_directives is not None: - new_directives = nginxparser.UnspacedList([]) - for directive in raw_in_parsed[1]: - if len(directive) > 0 and directive[0] in only_directives: - new_directives.append(directive) - raw_in_parsed[1] = new_directives - - self._update_vhost_based_on_new_directives(new_vhost, new_directives) - - enclosing_block.append(raw_in_parsed) - new_vhost.path[-1] = len(enclosing_block) - 1 - if remove_singleton_listen_params: - for addr in new_vhost.addrs: - addr.default = False - addr.ipv6only = False - for directive in enclosing_block[new_vhost.path[-1]][1]: - if len(directive) > 0 and directive[0] == 'listen': - # Exclude one-time use parameters which will cause an error if repeated. - # https://nginx.org/en/docs/http/ngx_http_core_module.html#listen - exclude = set(('default_server', 'default', 'setfib', 'fastopen', 'backlog', - 'rcvbuf', 'sndbuf', 'accept_filter', 'deferred', 'bind', - 'ipv6only', 'reuseport', 'so_keepalive')) - - for param in exclude: - # See: github.com/certbot/certbot/pull/6223#pullrequestreview-143019225 - keys = [x.split('=')[0] for x in directive] - if param in keys: - del directive[keys.index(param)] - return new_vhost - - -def _parse_ssl_options(ssl_options): - if ssl_options is not None: - try: - with open(ssl_options) as _file: - return nginxparser.load(_file) - except IOError: - logger.warning("Missing NGINX TLS options file: %s", ssl_options) - except pyparsing.ParseBaseException as err: - logger.debug("Could not parse file: %s due to %s", ssl_options, err) - return [] - -def _do_for_subarray(entry, condition, func, path=None): - """Executes a function for a subarray of a nested array if it matches - the given condition. - - :param list entry: The list to iterate over - :param function condition: Returns true iff func should be executed on item - :param function func: The function to call for each matching item - - """ - if path is None: - path = [] - if isinstance(entry, list): - if condition(entry): - func(entry, path) - else: - for index, item in enumerate(entry): - _do_for_subarray(item, condition, func, path + [index]) - - -def get_best_match(target_name, names): - """Finds the best match for target_name out of names using the Nginx - name-matching rules (exact > longest wildcard starting with * > - longest wildcard ending with * > regex). - - :param str target_name: The name to match - :param set names: The candidate server names - :returns: Tuple of (type of match, the name that matched) - :rtype: tuple - - """ - exact = [] - wildcard_start = [] - wildcard_end = [] - regex = [] - - for name in names: - if _exact_match(target_name, name): - exact.append(name) - elif _wildcard_match(target_name, name, True): - wildcard_start.append(name) - elif _wildcard_match(target_name, name, False): - wildcard_end.append(name) - elif _regex_match(target_name, name): - regex.append(name) - - if len(exact) > 0: - # There can be more than one exact match; e.g. eff.org, .eff.org - match = min(exact, key=len) - return ('exact', match) - if len(wildcard_start) > 0: - # Return the longest wildcard - match = max(wildcard_start, key=len) - return ('wildcard_start', match) - if len(wildcard_end) > 0: - # Return the longest wildcard - match = max(wildcard_end, key=len) - return ('wildcard_end', match) - if len(regex) > 0: - # Just return the first one for now - match = regex[0] - return ('regex', match) - - return (None, None) - - -def _exact_match(target_name, name): - return target_name == name or '.' + target_name == name - - -def _wildcard_match(target_name, name, start): - # Degenerate case - if name == '*': - return True - - parts = target_name.split('.') - match_parts = name.split('.') - - # If the domain ends in a wildcard, do the match procedure in reverse - if not start: - parts.reverse() - match_parts.reverse() - - # The first part must be a wildcard or blank, e.g. '.eff.org' - first = match_parts.pop(0) - if first != '*' and first != '': - return False - - target_name = '.'.join(parts) - name = '.'.join(match_parts) - - # Ex: www.eff.org matches *.eff.org, eff.org does not match *.eff.org - return target_name.endswith('.' + name) - - -def _regex_match(target_name, name): - # Must start with a tilde - if len(name) < 2 or name[0] != '~': - return False - - # After tilde is a perl-compatible regex - try: - regex = re.compile(name[1:]) - if re.match(regex, target_name): - return True - else: - return False - except re.error: # pragma: no cover - # perl-compatible regexes are sometimes not recognized by python - return False - - -def _is_include_directive(entry): - """Checks if an nginx parsed entry is an 'include' directive. - - :param list entry: the parsed entry - :returns: Whether it's an 'include' directive - :rtype: bool - - """ - return (isinstance(entry, list) and - len(entry) == 2 and entry[0] == 'include' and - isinstance(entry[1], six.string_types)) - -def _is_ssl_on_directive(entry): - """Checks if an nginx parsed entry is an 'ssl on' directive. - - :param list entry: the parsed entry - :returns: Whether it's an 'ssl on' directive - :rtype: bool - - """ - return (isinstance(entry, list) and - len(entry) == 2 and entry[0] == 'ssl' and - entry[1] == 'on') - -def _add_directives(directives, insert_at_top, block): - """Adds directives to a config block.""" - for directive in directives: - _add_directive(block, directive, insert_at_top) - if block and '\n' not in block[-1]: # could be " \n " or ["\n"] ! - block.append(nginxparser.UnspacedList('\n')) - -def _update_or_add_directives(directives, insert_at_top, block): - """Adds or replaces directives in a config block.""" - for directive in directives: - _update_or_add_directive(block, directive, insert_at_top) - if block and '\n' not in block[-1]: # could be " \n " or ["\n"] ! - block.append(nginxparser.UnspacedList('\n')) - - -INCLUDE = 'include' -REPEATABLE_DIRECTIVES = set(['server_name', 'listen', INCLUDE, 'rewrite', 'add_header']) -COMMENT = ' managed by Certbot' -COMMENT_BLOCK = [' ', '#', COMMENT] - -def comment_directive(block, location): - """Add a ``#managed by Certbot`` comment to the end of the line at location. - - :param list block: The block containing the directive to be commented - :param int location: The location within ``block`` of the directive to be commented - """ - next_entry = block[location + 1] if location + 1 < len(block) else None - if isinstance(next_entry, list) and next_entry: - if len(next_entry) >= 2 and next_entry[-2] == "#" and COMMENT in next_entry[-1]: - return - elif isinstance(next_entry, nginxparser.UnspacedList): - next_entry = next_entry.spaced[0] - else: - next_entry = next_entry[0] - - block.insert(location + 1, COMMENT_BLOCK[:]) - if next_entry is not None and "\n" not in next_entry: - block.insert(location + 2, '\n') - -def _comment_out_directive(block, location, include_location): - """Comment out the line at location, with a note of explanation.""" - comment_message = ' duplicated in {0}'.format(include_location) - # add the end comment - # create a dumpable object out of block[location] (so it includes the ;) - directive = block[location] - new_dir_block = nginxparser.UnspacedList([]) # just a wrapper - new_dir_block.append(directive) - dumped = nginxparser.dumps(new_dir_block) - commented = dumped + ' #' + comment_message # add the comment directly to the one-line string - new_dir = nginxparser.loads(commented) # reload into UnspacedList - - # add the beginning comment - insert_location = 0 - if new_dir[0].spaced[0] != new_dir[0][0]: # if there's whitespace at the beginning - insert_location = 1 - new_dir[0].spaced.insert(insert_location, "# ") # comment out the line - new_dir[0].spaced.append(";") # directly add in the ;, because now dumping won't work properly - dumped = nginxparser.dumps(new_dir) - new_dir = nginxparser.loads(dumped) # reload into an UnspacedList - - block[location] = new_dir[0] # set the now-single-line-comment directive back in place - -def _find_location(block, directive_name, match_func=None): - """Finds the index of the first instance of directive_name in block. - If no line exists, use None.""" - return next((index for index, line in enumerate(block) \ - if line and line[0] == directive_name and (match_func is None or match_func(line))), None) - -def _is_whitespace_or_comment(directive): - """Is this directive either a whitespace or comment directive?""" - return len(directive) == 0 or directive[0] == '#' - -def _add_directive(block, directive, insert_at_top): - if not isinstance(directive, nginxparser.UnspacedList): - directive = nginxparser.UnspacedList(directive) - if _is_whitespace_or_comment(directive): - # whitespace or comment - block.append(directive) - return - - location = _find_location(block, directive[0]) - - # Append or prepend directive. Fail if the name is not a repeatable directive name, - # and there is already a copy of that directive with a different value - # in the config file. - - # handle flat include files - - directive_name = directive[0] - def can_append(loc, dir_name): - """ Can we append this directive to the block? """ - return loc is None or (isinstance(dir_name, six.string_types) - and dir_name in REPEATABLE_DIRECTIVES) - - err_fmt = 'tried to insert directive "{0}" but found conflicting "{1}".' - - # Give a better error message about the specific directive than Nginx's "fail to restart" - if directive_name == INCLUDE: - # in theory, we might want to do this recursively, but in practice, that's really not - # necessary because we know what file we're talking about (and if we don't recurse, we - # just give a worse error message) - included_directives = _parse_ssl_options(directive[1]) - - for included_directive in included_directives: - included_dir_loc = _find_location(block, included_directive[0]) - included_dir_name = included_directive[0] - if not _is_whitespace_or_comment(included_directive) \ - and not can_append(included_dir_loc, included_dir_name): - if block[included_dir_loc] != included_directive: - raise errors.MisconfigurationError(err_fmt.format(included_directive, - block[included_dir_loc])) - else: - _comment_out_directive(block, included_dir_loc, directive[1]) - - if can_append(location, directive_name): - if insert_at_top: - # Add a newline so the comment doesn't comment - # out existing directives - block.insert(0, nginxparser.UnspacedList('\n')) - block.insert(0, directive) - comment_directive(block, 0) - else: - block.append(directive) - comment_directive(block, len(block) - 1) - elif block[location] != directive: - raise errors.MisconfigurationError(err_fmt.format(directive, block[location])) - -def _update_directive(block, directive, location): - block[location] = directive - comment_directive(block, location) - -def _update_or_add_directive(block, directive, insert_at_top): - if not isinstance(directive, nginxparser.UnspacedList): - directive = nginxparser.UnspacedList(directive) - if _is_whitespace_or_comment(directive): - # whitespace or comment - block.append(directive) - return - - location = _find_location(block, directive[0]) - - # we can update directive - if location is not None: - _update_directive(block, directive, location) - return - - _add_directive(block, directive, insert_at_top) - -def _is_certbot_comment(directive): - return '#' in directive and COMMENT in directive - -def _remove_directives(directive_name, match_func, block): - """Removes directives of name directive_name from a config block if match_func matches. - """ - while True: - location = _find_location(block, directive_name, match_func=match_func) - if location is None: - return - # if the directive was made by us, remove the comment following - if location + 1 < len(block) and _is_certbot_comment(block[location + 1]): - del block[location + 1] - del block[location] - -def _apply_global_addr_ssl(addr_to_ssl, parsed_server): - """Apply global sslishness information to the parsed server block - """ - for addr in parsed_server['addrs']: - addr.ssl = addr_to_ssl[addr.normalized_tuple()] - if addr.ssl: - parsed_server['ssl'] = True - -def _parse_server_raw(server): - """Parses a list of server directives. - - :param list server: list of directives in a server block - :rtype: dict - - """ - addrs = set() # type: Set[obj.Addr] - ssl = False # type: bool - names = set() # type: Set[str] - - apply_ssl_to_all_addrs = False - - for directive in server: - if not directive: - continue - if directive[0] == 'listen': - addr = obj.Addr.fromstring(" ".join(directive[1:])) - if addr: - addrs.add(addr) - if addr.ssl: - ssl = True - elif directive[0] == 'server_name': - names.update(x.strip('"\'') for x in directive[1:]) - elif _is_ssl_on_directive(directive): - ssl = True - apply_ssl_to_all_addrs = True - - if apply_ssl_to_all_addrs: - for addr in addrs: - addr.ssl = True - - return { - 'addrs': addrs, - 'ssl': ssl, - 'names': names - } diff --git a/certbot-nginx/certbot_nginx/parser_obj.py b/certbot-nginx/certbot_nginx/parser_obj.py deleted file mode 100644 index f01cb2fd3e6..00000000000 --- a/certbot-nginx/certbot_nginx/parser_obj.py +++ /dev/null @@ -1,392 +0,0 @@ -""" This file contains parsing routines and object classes to help derive meaning from -raw lists of tokens from pyparsing. """ - -import abc -import logging -import six - -from certbot import errors - -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module - -logger = logging.getLogger(__name__) -COMMENT = " managed by Certbot" -COMMENT_BLOCK = ["#", COMMENT] - -class Parsable(object): - """ Abstract base class for "Parsable" objects whose underlying representation - is a tree of lists. - - :param .Parsable parent: This object's parsed parent in the tree - """ - - __metaclass__ = abc.ABCMeta - - def __init__(self, parent=None): - self._data = [] # type: List[object] - self._tabs = None - self.parent = parent - - @classmethod - def parsing_hooks(cls): - """Returns object types that this class should be able to `parse` recusrively. - The order of the objects indicates the order in which the parser should - try to parse each subitem. - :returns: A list of Parsable classes. - :rtype list: - """ - return (Block, Sentence, Statements) - - @staticmethod - @abc.abstractmethod - def should_parse(lists): - """ Returns whether the contents of `lists` can be parsed into this object. - - :returns: Whether `lists` can be parsed as this object. - :rtype bool: - """ - raise NotImplementedError() - - @abc.abstractmethod - def parse(self, raw_list, add_spaces=False): - """ Loads information into this object from underlying raw_list structure. - Each Parsable object might make different assumptions about the structure of - raw_list. - - :param list raw_list: A list or sublist of tokens from pyparsing, containing whitespace - as separate tokens. - :param bool add_spaces: If set, the method can and should manipulate and insert spacing - between non-whitespace tokens and lists to delimit them. - :raises .errors.MisconfigurationError: when the assumptions about the structure of - raw_list are not met. - """ - raise NotImplementedError() - - @abc.abstractmethod - def iterate(self, expanded=False, match=None): - """ Iterates across this object. If this object is a leaf object, only yields - itself. If it contains references other parsing objects, and `expanded` is set, - this function should first yield itself, then recursively iterate across all of them. - :param bool expanded: Whether to recursively iterate on possible children. - :param callable match: If provided, an object is only iterated if this callable - returns True when called on that object. - - :returns: Iterator over desired objects. - """ - raise NotImplementedError() - - @abc.abstractmethod - def get_tabs(self): - """ Guess at the tabbing style of this parsed object, based on whitespace. - - If this object is a leaf, it deducts the tabbing based on its own contents. - Other objects may guess by calling `get_tabs` recursively on child objects. - - :returns: Guess at tabbing for this object. Should only return whitespace strings - that does not contain newlines. - :rtype str: - """ - raise NotImplementedError() - - @abc.abstractmethod - def set_tabs(self, tabs=" "): - """This tries to set and alter the tabbing of the current object to a desired - whitespace string. Primarily meant for objects that were constructed, so they - can conform to surrounding whitespace. - - :param str tabs: A whitespace string (not containing newlines). - """ - raise NotImplementedError() - - def dump(self, include_spaces=False): - """ Dumps back to pyparsing-like list tree. The opposite of `parse`. - - Note: if this object has not been modified, `dump` with `include_spaces=True` - should always return the original input of `parse`. - - :param bool include_spaces: If set to False, magically hides whitespace tokens from - dumped output. - - :returns: Pyparsing-like list tree. - :rtype list: - """ - return [elem.dump(include_spaces) for elem in self._data] - -class Statements(Parsable): - """ A group or list of "Statements". A Statement is either a Block or a Sentence. - - The underlying representation is simply a list of these Statement objects, with - an extra `_trailing_whitespace` string to keep track of the whitespace that does not - precede any more statements. - """ - def __init__(self, parent=None): - super(Statements, self).__init__(parent) - self._trailing_whitespace = None - - # ======== Begin overridden functions - - @staticmethod - def should_parse(lists): - return isinstance(lists, list) - - def set_tabs(self, tabs=" "): - """ Sets the tabbing for this set of statements. Does this by calling `set_tabs` - on each of the child statements. - - Then, if a parent is present, sets trailing whitespace to parent tabbing. This - is so that the trailing } of any Block that contains Statements lines up - with parent tabbing. - """ - for statement in self._data: - statement.set_tabs(tabs) - if self.parent is not None: - self._trailing_whitespace = "\n" + self.parent.get_tabs() - - def parse(self, parse_this, add_spaces=False): - """ Parses a list of statements. - Expects all elements in `parse_this` to be parseable by `type(self).parsing_hooks`, - with an optional whitespace string at the last index of `parse_this`. - """ - if not isinstance(parse_this, list): - raise errors.MisconfigurationError("Statements parsing expects a list!") - # If there's a trailing whitespace in the list of statements, keep track of it. - if len(parse_this) > 0 and isinstance(parse_this[-1], six.string_types) \ - and parse_this[-1].isspace(): - self._trailing_whitespace = parse_this[-1] - parse_this = parse_this[:-1] - self._data = [parse_raw(elem, self, add_spaces) for elem in parse_this] - - def get_tabs(self): - """ Takes a guess at the tabbing of all contained Statements by retrieving the - tabbing of the first Statement.""" - if len(self._data) > 0: - return self._data[0].get_tabs() - return "" - - def dump(self, include_spaces=False): - """ Dumps this object by first dumping each statement, then appending its - trailing whitespace (if `include_spaces` is set) """ - data = super(Statements, self).dump(include_spaces) - if include_spaces and self._trailing_whitespace is not None: - return data + [self._trailing_whitespace] - return data - - def iterate(self, expanded=False, match=None): - """ Combines each statement's iterator. """ - for elem in self._data: - for sub_elem in elem.iterate(expanded, match): - yield sub_elem - - # ======== End overridden functions - -def _space_list(list_): - """ Inserts whitespace between adjacent non-whitespace tokens. """ - spaced_statement = [] # type: List[str] - for i in reversed(six.moves.xrange(len(list_))): - spaced_statement.insert(0, list_[i]) - if i > 0 and not list_[i].isspace() and not list_[i-1].isspace(): - spaced_statement.insert(0, " ") - return spaced_statement - -class Sentence(Parsable): - """ A list of words. Non-whitespace words are typically separated with whitespace tokens. """ - - # ======== Begin overridden functions - - @staticmethod - def should_parse(lists): - """ Returns True if `lists` can be parseable as a `Sentence`-- that is, - every element is a string type. - - :param list lists: The raw unparsed list to check. - - :returns: whether this lists is parseable by `Sentence`. - """ - return isinstance(lists, list) and len(lists) > 0 and \ - all([isinstance(elem, six.string_types) for elem in lists]) - - def parse(self, parse_this, add_spaces=False): - """ Parses a list of string types into this object. - If add_spaces is set, adds whitespace tokens between adjacent non-whitespace tokens.""" - if add_spaces: - parse_this = _space_list(parse_this) - if not isinstance(parse_this, list) or \ - any([not isinstance(elem, six.string_types) for elem in parse_this]): - raise errors.MisconfigurationError("Sentence parsing expects a list of string types.") - self._data = parse_this - - def iterate(self, expanded=False, match=None): - """ Simply yields itself. """ - if match is None or match(self): - yield self - - def set_tabs(self, tabs=" "): - """ Sets the tabbing on this sentence. Inserts a newline and `tabs` at the - beginning of `self._data`. """ - if self._data[0].isspace(): - return - self._data.insert(0, "\n" + tabs) - - def dump(self, include_spaces=False): - """ Dumps this sentence. If include_spaces is set, includes whitespace tokens.""" - if not include_spaces: - return self.words - return self._data - - def get_tabs(self): - """ Guesses at the tabbing of this sentence. If the first element is whitespace, - returns the whitespace after the rightmost newline in the string. """ - first = self._data[0] - if not first.isspace(): - return "" - rindex = first.rfind("\n") - return first[rindex+1:] - - # ======== End overridden functions - - @property - def words(self): - """ Iterates over words, but without spaces. Like Unspaced List. """ - return [word.strip("\"\'") for word in self._data if not word.isspace()] - - def __getitem__(self, index): - return self.words[index] - - def __contains__(self, word): - return word in self.words - -class Block(Parsable): - """ Any sort of bloc, denoted by a block name and curly braces, like so: - The parsed block: - block name { - content 1; - content 2; - } - might be represented with the list [names, contents], where - names = ["block", " ", "name", " "] - contents = [["\n ", "content", " ", "1"], ["\n ", "content", " ", "2"], "\n"] - """ - def __init__(self, parent=None): - super(Block, self).__init__(parent) - self.names = None # type: Sentence - self.contents = None # type: Block - - @staticmethod - def should_parse(lists): - """ Returns True if `lists` can be parseable as a `Block`-- that is, - it's got a length of 2, the first element is a `Sentence` and the second can be - a `Statements`. - - :param list lists: The raw unparsed list to check. - - :returns: whether this lists is parseable by `Block`. """ - return isinstance(lists, list) and len(lists) == 2 and \ - Sentence.should_parse(lists[0]) and isinstance(lists[1], list) - - def set_tabs(self, tabs=" "): - """ Sets tabs by setting equivalent tabbing on names, then adding tabbing - to contents.""" - self.names.set_tabs(tabs) - self.contents.set_tabs(tabs + " ") - - def iterate(self, expanded=False, match=None): - """ Iterator over self, and if expanded is set, over its contents. """ - if match is None or match(self): - yield self - if expanded: - for elem in self.contents.iterate(expanded, match): - yield elem - - def parse(self, parse_this, add_spaces=False): - """ Parses a list that resembles a block. - - The assumptions that this routine makes are: - 1. the first element of `parse_this` is a valid Sentence. - 2. the second element of `parse_this` is a valid Statement. - If add_spaces is set, we call it recursively on `names` and `contents`, and - add an extra trailing space to `names` (to separate the block's opening bracket - and the block name). - """ - if not Block.should_parse(parse_this): - raise errors.MisconfigurationError("Block parsing expects a list of length 2. " - "First element should be a list of string types (the bloc names), " - "and second should be another list of statements (the bloc content).") - self.names = Sentence(self) - if add_spaces: - parse_this[0].append(" ") - self.names.parse(parse_this[0], add_spaces) - self.contents = Statements(self) - self.contents.parse(parse_this[1], add_spaces) - self._data = [self.names, self.contents] - - def get_tabs(self): - """ Guesses tabbing by retrieving tabbing guess of self.names. """ - return self.names.get_tabs() - -def _is_comment(parsed_obj): - """ Checks whether parsed_obj is a comment. - - :param .Parsable parsed_obj: - - :returns: whether parsed_obj represents a comment sentence. - :rtype bool: - """ - if not isinstance(parsed_obj, Sentence): - return False - return parsed_obj.words[0] == "#" - -def _is_certbot_comment(parsed_obj): - """ Checks whether parsed_obj is a "managed by Certbot" comment. - - :param .Parsable parsed_obj: - - :returns: whether parsed_obj is a "managed by Certbot" comment. - :rtype bool: - """ - if not _is_comment(parsed_obj): - return False - if len(parsed_obj.words) != len(COMMENT_BLOCK): - return False - for i, word in enumerate(parsed_obj.words): - if word != COMMENT_BLOCK[i]: - return False - return True - -def _certbot_comment(parent, preceding_spaces=4): - """ A "Managed by Certbot" comment. - :param int preceding_spaces: Number of spaces between the end of the previous - statement and the comment. - :returns: Sentence containing the comment. - :rtype: .Sentence - """ - result = Sentence(parent) - result.parse([" " * preceding_spaces] + COMMENT_BLOCK) - return result - -def _choose_parser(parent, list_): - """ Choose a parser from type(parent).parsing_hooks, depending on whichever hook - returns True first. """ - hooks = Parsable.parsing_hooks() - if parent: - hooks = type(parent).parsing_hooks() - for type_ in hooks: - if type_.should_parse(list_): - return type_(parent) - raise errors.MisconfigurationError( - "None of the parsing hooks succeeded, so we don't know how to parse this set of lists.") - -def parse_raw(lists_, parent=None, add_spaces=False): - """ Primary parsing factory function. - - :param list lists_: raw lists from pyparsing to parse. - :param .Parent parent: The parent containing this object. - :param bool add_spaces: Whether to pass add_spaces to the parser. - - :returns .Parsable: The parsed object. - - :raises errors.MisconfigurationError: If no parsing hook passes, and we can't - determine which type to parse the raw lists into. - """ - parser = _choose_parser(parent, lists_) - parser.parse(lists_, add_spaces) - return parser diff --git a/certbot-nginx/certbot_nginx/tests/__init__.py b/certbot-nginx/certbot_nginx/tests/__init__.py deleted file mode 100644 index 32ca193d95f..00000000000 --- a/certbot-nginx/certbot_nginx/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Certbot Nginx Tests""" diff --git a/certbot-nginx/certbot_nginx/tests/configurator_test.py b/certbot-nginx/certbot_nginx/tests/configurator_test.py deleted file mode 100644 index 957588e2a41..00000000000 --- a/certbot-nginx/certbot_nginx/tests/configurator_test.py +++ /dev/null @@ -1,995 +0,0 @@ -# pylint: disable=too-many-public-methods -"""Test for certbot_nginx.configurator.""" -import os -import shutil -import unittest - -import mock -import OpenSSL - -from acme import challenges -from acme import messages - -from certbot import achallenges -from certbot import crypto_util -from certbot import errors -from certbot.tests import util as certbot_test_util - -from certbot_nginx import constants -from certbot_nginx import obj -from certbot_nginx import parser -from certbot_nginx.configurator import _redirect_block_for_domain -from certbot_nginx.nginxparser import UnspacedList -from certbot_nginx.tests import util - - -class NginxConfiguratorTest(util.NginxTest): - """Test a semi complex vhost configuration.""" - - - def setUp(self): - super(NginxConfiguratorTest, self).setUp() - - self.config = util.get_nginx_configurator( - self.config_path, self.config_dir, self.work_dir, self.logs_dir) - - def tearDown(self): - shutil.rmtree(self.temp_dir) - shutil.rmtree(self.config_dir) - shutil.rmtree(self.work_dir) - shutil.rmtree(self.logs_dir) - - @mock.patch("certbot_nginx.configurator.util.exe_exists") - def test_prepare_no_install(self, mock_exe_exists): - mock_exe_exists.return_value = False - self.assertRaises( - errors.NoInstallationError, self.config.prepare) - - def test_prepare(self): - self.assertEqual((1, 6, 2), self.config.version) - self.assertEqual(11, len(self.config.parser.parsed)) - - @mock.patch("certbot_nginx.configurator.util.exe_exists") - @mock.patch("certbot_nginx.configurator.subprocess.Popen") - def test_prepare_initializes_version(self, mock_popen, mock_exe_exists): - mock_popen().communicate.return_value = ( - "", "\n".join(["nginx version: nginx/1.6.2", - "built by clang 6.0 (clang-600.0.56)" - " (based on LLVM 3.5svn)", - "TLS SNI support enabled", - "configure arguments: --prefix=/usr/local/Cellar/" - "nginx/1.6.2 --with-http_ssl_module"])) - - mock_exe_exists.return_value = True - - self.config.version = None - self.config.config_test = mock.Mock() - self.config.prepare() - self.assertEqual((1, 6, 2), self.config.version) - - def test_prepare_locked(self): - server_root = self.config.conf("server-root") - self.config.config_test = mock.Mock() - os.remove(os.path.join(server_root, ".certbot.lock")) - certbot_test_util.lock_and_call(self._test_prepare_locked, server_root) - - @mock.patch("certbot_nginx.configurator.util.exe_exists") - def _test_prepare_locked(self, unused_exe_exists): - try: - self.config.prepare() - except errors.PluginError as err: - err_msg = str(err) - self.assertTrue("lock" in err_msg) - self.assertTrue(self.config.conf("server-root") in err_msg) - else: # pragma: no cover - self.fail("Exception wasn't raised!") - - @mock.patch("certbot_nginx.configurator.socket.gethostbyaddr") - def test_get_all_names(self, mock_gethostbyaddr): - mock_gethostbyaddr.return_value = ('155.225.50.69.nephoscale.net', [], []) - names = self.config.get_all_names() - self.assertEqual(names, set( - ["155.225.50.69.nephoscale.net", "www.example.org", "another.alias", - "migration.com", "summer.com", "geese.com", "sslon.com", - "globalssl.com", "globalsslsetssl.com", "ipv6.com", "ipv6ssl.com", - "headers.com"])) - - def test_supported_enhancements(self): - self.assertEqual(['redirect', 'ensure-http-header', 'staple-ocsp'], - self.config.supported_enhancements()) - - def test_enhance(self): - self.assertRaises( - errors.PluginError, self.config.enhance, 'myhost', 'unknown_enhancement') - - def test_get_chall_pref(self): - self.assertEqual([challenges.HTTP01, challenges.TLSSNI01], - self.config.get_chall_pref('myhost')) - - def test_save(self): - filep = self.config.parser.abs_path('sites-enabled/example.com') - mock_vhost = obj.VirtualHost(filep, - None, None, None, - set(['.example.com', 'example.*']), - None, [0]) - self.config.parser.add_server_directives( - mock_vhost, - [['listen', ' ', '5001', ' ', 'ssl']]) - self.config.save() - - # pylint: disable=protected-access - parsed = self.config.parser._parse_files(filep, override=True) - self.assertEqual([[['server'], - [['listen', '69.50.225.155:9000'], - ['listen', '127.0.0.1'], - ['server_name', '.example.com'], - ['server_name', 'example.*'], - ['listen', '5001', 'ssl'], - ['#', parser.COMMENT]]]], - parsed[0]) - - def test_choose_vhosts_alias(self): - self._test_choose_vhosts_common('alias', 'server_conf') - - def test_choose_vhosts_example_com(self): - self._test_choose_vhosts_common('example.com', 'example_conf') - - def test_choose_vhosts_localhost(self): - self._test_choose_vhosts_common('localhost', 'localhost_conf') - - def test_choose_vhosts_example_com_uk_test(self): - self._test_choose_vhosts_common('example.com.uk.test', 'example_conf') - - def test_choose_vhosts_www_example_com(self): - self._test_choose_vhosts_common('www.example.com', 'example_conf') - - def test_choose_vhosts_test_www_example_com(self): - self._test_choose_vhosts_common('test.www.example.com', 'foo_conf') - - def test_choose_vhosts_abc_www_foo_com(self): - self._test_choose_vhosts_common('abc.www.foo.com', 'foo_conf') - - def test_choose_vhosts_www_bar_co_uk(self): - self._test_choose_vhosts_common('www.bar.co.uk', 'localhost_conf') - - def test_choose_vhosts_ipv6_com(self): - self._test_choose_vhosts_common('ipv6.com', 'ipv6_conf') - - def _test_choose_vhosts_common(self, name, conf): - conf_names = {'localhost_conf': set(['localhost', r'~^(www\.)?(example|bar)\.']), - 'server_conf': set(['somename', 'another.alias', 'alias']), - 'example_conf': set(['.example.com', 'example.*']), - 'foo_conf': set(['*.www.foo.com', '*.www.example.com']), - 'ipv6_conf': set(['ipv6.com'])} - - conf_path = {'localhost': "etc_nginx/nginx.conf", - 'alias': "etc_nginx/nginx.conf", - 'example.com': "etc_nginx/sites-enabled/example.com", - 'example.com.uk.test': "etc_nginx/sites-enabled/example.com", - 'www.example.com': "etc_nginx/sites-enabled/example.com", - 'test.www.example.com': "etc_nginx/foo.conf", - 'abc.www.foo.com': "etc_nginx/foo.conf", - 'www.bar.co.uk': "etc_nginx/nginx.conf", - 'ipv6.com': "etc_nginx/sites-enabled/ipv6.com"} - - vhost = self.config.choose_vhosts(name)[0] - path = os.path.relpath(vhost.filep, self.temp_dir) - - self.assertEqual(conf_names[conf], vhost.names) - self.assertEqual(conf_path[name], path) - # IPv6 specific checks - if name == "ipv6.com": - self.assertTrue(vhost.ipv6_enabled()) - # Make sure that we have SSL enabled also for IPv6 addr - self.assertTrue( - any([True for x in vhost.addrs if x.ssl and x.ipv6])) - - def test_choose_vhosts_bad(self): - bad_results = ['www.foo.com', 'example', 't.www.bar.co', - '69.255.225.155'] - - for name in bad_results: - self.assertRaises(errors.MisconfigurationError, - self.config.choose_vhosts, name) - - def test_ipv6only(self): - # ipv6_info: (ipv6_active, ipv6only_present) - self.assertEqual((True, False), self.config.ipv6_info("80")) - # Port 443 has ipv6only=on because of ipv6ssl.com vhost - self.assertEqual((True, True), self.config.ipv6_info("443")) - - def test_ipv6only_detection(self): - self.config.version = (1, 3, 1) - - self.config.deploy_cert( - "ipv6.com", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - - for addr in self.config.choose_vhosts("ipv6.com")[0].addrs: - self.assertFalse(addr.ipv6only) - - def test_more_info(self): - self.assertTrue('nginx.conf' in self.config.more_info()) - - def test_deploy_cert_requires_fullchain_path(self): - self.config.version = (1, 3, 1) - self.assertRaises(errors.PluginError, self.config.deploy_cert, - "www.example.com", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - None) - - @mock.patch('certbot_nginx.parser.NginxParser.update_or_add_server_directives') - def test_deploy_cert_raise_on_add_error(self, mock_update_or_add_server_directives): - mock_update_or_add_server_directives.side_effect = errors.MisconfigurationError() - self.assertRaises( - errors.PluginError, - self.config.deploy_cert, - "migration.com", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - - def test_deploy_cert(self): - server_conf = self.config.parser.abs_path('server.conf') - nginx_conf = self.config.parser.abs_path('nginx.conf') - example_conf = self.config.parser.abs_path('sites-enabled/example.com') - self.config.version = (1, 3, 1) - - # Get the default SSL vhost - self.config.deploy_cert( - "www.example.com", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - self.config.deploy_cert( - "another.alias", - "/etc/nginx/cert.pem", - "/etc/nginx/key.pem", - "/etc/nginx/chain.pem", - "/etc/nginx/fullchain.pem") - self.config.save() - - self.config.parser.load() - - parsed_example_conf = util.filter_comments(self.config.parser.parsed[example_conf]) - parsed_server_conf = util.filter_comments(self.config.parser.parsed[server_conf]) - parsed_nginx_conf = util.filter_comments(self.config.parser.parsed[nginx_conf]) - - self.assertEqual([[['server'], - [ - ['listen', '69.50.225.155:9000'], - ['listen', '127.0.0.1'], - ['server_name', '.example.com'], - ['server_name', 'example.*'], - - ['listen', '5001', 'ssl'], - ['ssl_certificate', 'example/fullchain.pem'], - ['ssl_certificate_key', 'example/key.pem'], - ['include', self.config.mod_ssl_conf], - ['ssl_dhparam', self.config.ssl_dhparams], - ]]], - parsed_example_conf) - self.assertEqual([['server_name', 'somename', 'alias', 'another.alias']], - parsed_server_conf) - self.assertTrue(util.contains_at_depth( - parsed_nginx_conf, - [['server'], - [ - ['listen', '8000'], - ['listen', 'somename:8080'], - ['include', 'server.conf'], - [['location', '/'], - [['root', 'html'], - ['index', 'index.html', 'index.htm']]], - ['listen', '5001', 'ssl'], - ['ssl_certificate', '/etc/nginx/fullchain.pem'], - ['ssl_certificate_key', '/etc/nginx/key.pem'], - ['include', self.config.mod_ssl_conf], - ['ssl_dhparam', self.config.ssl_dhparams], - ]], - 2)) - - def test_deploy_cert_add_explicit_listen(self): - migration_conf = self.config.parser.abs_path('sites-enabled/migration.com') - self.config.deploy_cert( - "summer.com", - "summer/cert.pem", - "summer/key.pem", - "summer/chain.pem", - "summer/fullchain.pem") - self.config.save() - self.config.parser.load() - parsed_migration_conf = util.filter_comments(self.config.parser.parsed[migration_conf]) - self.assertEqual([['server'], - [ - ['server_name', 'migration.com'], - ['server_name', 'summer.com'], - - ['listen', '80'], - ['listen', '5001', 'ssl'], - ['ssl_certificate', 'summer/fullchain.pem'], - ['ssl_certificate_key', 'summer/key.pem'], - ['include', self.config.mod_ssl_conf], - ['ssl_dhparam', self.config.ssl_dhparams], - ]], - parsed_migration_conf[0]) - - @mock.patch("certbot_nginx.configurator.tls_sni_01.NginxTlsSni01.perform") - @mock.patch("certbot_nginx.configurator.http_01.NginxHttp01.perform") - @mock.patch("certbot_nginx.configurator.NginxConfigurator.restart") - @mock.patch("certbot_nginx.configurator.NginxConfigurator.revert_challenge_config") - def test_perform_and_cleanup(self, mock_revert, mock_restart, mock_http_perform, - mock_tls_perform): - # Only tests functionality specific to configurator.perform - # Note: As more challenges are offered this will have to be expanded - achall1 = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=messages.ChallengeBody( - chall=challenges.TLSSNI01(token=b"kNdwjwOeX0I_A8DXt9Msmg"), - uri="https://ca.org/chall0_uri", - status=messages.Status("pending"), - ), domain="localhost", account_key=self.rsa512jwk) - achall2 = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=messages.ChallengeBody( - chall=challenges.HTTP01(token=b"m8TdO1qik4JVFtgPPurJmg"), - uri="https://ca.org/chall1_uri", - status=messages.Status("pending"), - ), domain="example.com", account_key=self.rsa512jwk) - - expected = [ - achall1.response(self.rsa512jwk), - achall2.response(self.rsa512jwk), - ] - - mock_tls_perform.return_value = expected[:1] - mock_http_perform.return_value = expected[1:] - responses = self.config.perform([achall1, achall2]) - - self.assertEqual(mock_tls_perform.call_count, 1) - self.assertEqual(mock_http_perform.call_count, 1) - self.assertEqual(responses, expected) - - self.config.cleanup([achall1, achall2]) - self.assertEqual(0, self.config._chall_out) # pylint: disable=protected-access - self.assertEqual(mock_revert.call_count, 1) - self.assertEqual(mock_restart.call_count, 2) - - @mock.patch("certbot_nginx.configurator.subprocess.Popen") - def test_get_version(self, mock_popen): - mock_popen().communicate.return_value = ( - "", "\n".join(["nginx version: nginx/1.4.2", - "built by clang 6.0 (clang-600.0.56)" - " (based on LLVM 3.5svn)", - "TLS SNI support enabled", - "configure arguments: --prefix=/usr/local/Cellar/" - "nginx/1.6.2 --with-http_ssl_module"])) - self.assertEqual(self.config.get_version(), (1, 4, 2)) - - mock_popen().communicate.return_value = ( - "", "\n".join(["nginx version: nginx/0.9", - "built by clang 6.0 (clang-600.0.56)" - " (based on LLVM 3.5svn)", - "TLS SNI support enabled", - "configure arguments: --with-http_ssl_module"])) - self.assertEqual(self.config.get_version(), (0, 9)) - - mock_popen().communicate.return_value = ( - "", "\n".join(["blah 0.0.1", - "built by clang 6.0 (clang-600.0.56)" - " (based on LLVM 3.5svn)", - "TLS SNI support enabled", - "configure arguments: --with-http_ssl_module"])) - self.assertRaises(errors.PluginError, self.config.get_version) - - mock_popen().communicate.return_value = ( - "", "\n".join(["nginx version: nginx/1.4.2", - "TLS SNI support enabled"])) - self.assertRaises(errors.PluginError, self.config.get_version) - - mock_popen().communicate.return_value = ( - "", "\n".join(["nginx version: nginx/1.4.2", - "built by clang 6.0 (clang-600.0.56)" - " (based on LLVM 3.5svn)", - "configure arguments: --with-http_ssl_module"])) - self.assertRaises(errors.PluginError, self.config.get_version) - - mock_popen().communicate.return_value = ( - "", "\n".join(["nginx version: nginx/0.8.1", - "built by clang 6.0 (clang-600.0.56)" - " (based on LLVM 3.5svn)", - "TLS SNI support enabled", - "configure arguments: --with-http_ssl_module"])) - self.assertRaises(errors.NotSupportedError, self.config.get_version) - - mock_popen.side_effect = OSError("Can't find program") - self.assertRaises(errors.PluginError, self.config.get_version) - - @mock.patch("certbot_nginx.configurator.subprocess.Popen") - def test_nginx_restart(self, mock_popen): - mocked = mock_popen() - mocked.communicate.return_value = ('', '') - mocked.returncode = 0 - self.config.restart() - - @mock.patch("certbot_nginx.configurator.subprocess.Popen") - def test_nginx_restart_fail(self, mock_popen): - mocked = mock_popen() - mocked.communicate.return_value = ('', '') - mocked.returncode = 1 - self.assertRaises(errors.MisconfigurationError, self.config.restart) - - @mock.patch("certbot_nginx.configurator.subprocess.Popen") - def test_no_nginx_start(self, mock_popen): - mock_popen.side_effect = OSError("Can't find program") - self.assertRaises(errors.MisconfigurationError, self.config.restart) - - @mock.patch("certbot.util.run_script") - def test_config_test_bad_process(self, mock_run_script): - mock_run_script.side_effect = errors.SubprocessError - self.assertRaises(errors.MisconfigurationError, self.config.config_test) - - @mock.patch("certbot.util.run_script") - def test_config_test(self, _): - self.config.config_test() - - @mock.patch("certbot.reverter.Reverter.recovery_routine") - def test_recovery_routine_throws_error_from_reverter(self, mock_recovery_routine): - mock_recovery_routine.side_effect = errors.ReverterError("foo") - self.assertRaises(errors.PluginError, self.config.recovery_routine) - - @mock.patch("certbot.reverter.Reverter.view_config_changes") - def test_view_config_changes_throws_error_from_reverter(self, mock_view_config_changes): - mock_view_config_changes.side_effect = errors.ReverterError("foo") - self.assertRaises(errors.PluginError, self.config.view_config_changes) - - @mock.patch("certbot.reverter.Reverter.rollback_checkpoints") - def test_rollback_checkpoints_throws_error_from_reverter(self, mock_rollback_checkpoints): - mock_rollback_checkpoints.side_effect = errors.ReverterError("foo") - self.assertRaises(errors.PluginError, self.config.rollback_checkpoints) - - @mock.patch("certbot.reverter.Reverter.revert_temporary_config") - def test_revert_challenge_config_throws_error_from_reverter(self, mock_revert_temporary_config): - mock_revert_temporary_config.side_effect = errors.ReverterError("foo") - self.assertRaises(errors.PluginError, self.config.revert_challenge_config) - - @mock.patch("certbot.reverter.Reverter.add_to_checkpoint") - def test_save_throws_error_from_reverter(self, mock_add_to_checkpoint): - mock_add_to_checkpoint.side_effect = errors.ReverterError("foo") - self.assertRaises(errors.PluginError, self.config.save) - - def test_get_snakeoil_paths(self): - # pylint: disable=protected-access - cert, key = self.config._get_snakeoil_paths() - self.assertTrue(os.path.exists(cert)) - self.assertTrue(os.path.exists(key)) - with open(cert) as cert_file: - OpenSSL.crypto.load_certificate( - OpenSSL.crypto.FILETYPE_PEM, cert_file.read()) - with open(key) as key_file: - OpenSSL.crypto.load_privatekey( - OpenSSL.crypto.FILETYPE_PEM, key_file.read()) - - def test_redirect_enhance(self): - # Test that we successfully add a redirect when there is - # a listen directive - expected = UnspacedList(_redirect_block_for_domain("www.example.com"))[0] - - example_conf = self.config.parser.abs_path('sites-enabled/example.com') - self.config.enhance("www.example.com", "redirect") - - generated_conf = self.config.parser.parsed[example_conf] - self.assertTrue(util.contains_at_depth(generated_conf, expected, 2)) - - # Test that we successfully add a redirect when there is - # no listen directive - migration_conf = self.config.parser.abs_path('sites-enabled/migration.com') - self.config.enhance("migration.com", "redirect") - - expected = UnspacedList(_redirect_block_for_domain("migration.com"))[0] - - generated_conf = self.config.parser.parsed[migration_conf] - self.assertTrue(util.contains_at_depth(generated_conf, expected, 2)) - - def test_split_for_redirect(self): - example_conf = self.config.parser.abs_path('sites-enabled/example.com') - self.config.deploy_cert( - "example.org", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - self.config.enhance("www.example.com", "redirect") - generated_conf = self.config.parser.parsed[example_conf] - self.assertEqual( - [[['server'], [ - ['server_name', '.example.com'], - ['server_name', 'example.*'], [], - ['listen', '5001', 'ssl'], ['#', ' managed by Certbot'], - ['ssl_certificate', 'example/fullchain.pem'], ['#', ' managed by Certbot'], - ['ssl_certificate_key', 'example/key.pem'], ['#', ' managed by Certbot'], - ['include', self.config.mod_ssl_conf], ['#', ' managed by Certbot'], - ['ssl_dhparam', self.config.ssl_dhparams], ['#', ' managed by Certbot'], - [], []]], - [['server'], [ - [['if', '($host', '=', 'www.example.com)'], [ - ['return', '301', 'https://$host$request_uri']]], - ['#', ' managed by Certbot'], [], - ['listen', '69.50.225.155:9000'], - ['listen', '127.0.0.1'], - ['server_name', '.example.com'], - ['server_name', 'example.*'], - ['return', '404'], ['#', ' managed by Certbot'], [], [], []]]], - generated_conf) - - def test_split_for_headers(self): - example_conf = self.config.parser.abs_path('sites-enabled/example.com') - self.config.deploy_cert( - "example.org", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - self.config.enhance("www.example.com", "ensure-http-header", "Strict-Transport-Security") - generated_conf = self.config.parser.parsed[example_conf] - self.assertEqual( - [[['server'], [ - ['server_name', '.example.com'], - ['server_name', 'example.*'], [], - ['listen', '5001', 'ssl'], ['#', ' managed by Certbot'], - ['ssl_certificate', 'example/fullchain.pem'], ['#', ' managed by Certbot'], - ['ssl_certificate_key', 'example/key.pem'], ['#', ' managed by Certbot'], - ['include', self.config.mod_ssl_conf], ['#', ' managed by Certbot'], - ['ssl_dhparam', self.config.ssl_dhparams], ['#', ' managed by Certbot'], - [], [], - ['add_header', 'Strict-Transport-Security', '"max-age=31536000"', 'always'], - ['#', ' managed by Certbot'], - [], []]], - [['server'], [ - ['listen', '69.50.225.155:9000'], - ['listen', '127.0.0.1'], - ['server_name', '.example.com'], - ['server_name', 'example.*'], - [], [], []]]], - generated_conf) - - def test_http_header_hsts(self): - example_conf = self.config.parser.abs_path('sites-enabled/example.com') - self.config.enhance("www.example.com", "ensure-http-header", - "Strict-Transport-Security") - expected = ['add_header', 'Strict-Transport-Security', '"max-age=31536000"', 'always'] - generated_conf = self.config.parser.parsed[example_conf] - self.assertTrue(util.contains_at_depth(generated_conf, expected, 2)) - - def test_multiple_headers_hsts(self): - headers_conf = self.config.parser.abs_path('sites-enabled/headers.com') - self.config.enhance("headers.com", "ensure-http-header", - "Strict-Transport-Security") - expected = ['add_header', 'Strict-Transport-Security', '"max-age=31536000"', 'always'] - generated_conf = self.config.parser.parsed[headers_conf] - self.assertTrue(util.contains_at_depth(generated_conf, expected, 2)) - - def test_http_header_hsts_twice(self): - self.config.enhance("www.example.com", "ensure-http-header", - "Strict-Transport-Security") - self.assertRaises( - errors.PluginEnhancementAlreadyPresent, - self.config.enhance, "www.example.com", - "ensure-http-header", "Strict-Transport-Security") - - - @mock.patch('certbot_nginx.obj.VirtualHost.contains_list') - def test_certbot_redirect_exists(self, mock_contains_list): - # Test that we add no redirect statement if there is already a - # redirect in the block that is managed by certbot - # Has a certbot redirect - mock_contains_list.return_value = True - with mock.patch("certbot_nginx.configurator.logger") as mock_logger: - self.config.enhance("www.example.com", "redirect") - self.assertEqual(mock_logger.info.call_args[0][0], - "Traffic on port %s already redirecting to ssl in %s") - - def test_redirect_dont_enhance(self): - # Test that we don't accidentally add redirect to ssl-only block - with mock.patch("certbot_nginx.configurator.logger") as mock_logger: - self.config.enhance("geese.com", "redirect") - self.assertEqual(mock_logger.info.call_args[0][0], - 'No matching insecure server blocks listening on port %s found.') - - def test_double_redirect(self): - # Test that we add one redirect for each domain - example_conf = self.config.parser.abs_path('sites-enabled/example.com') - self.config.enhance("example.com", "redirect") - self.config.enhance("example.org", "redirect") - - expected1 = UnspacedList(_redirect_block_for_domain("example.com"))[0] - expected2 = UnspacedList(_redirect_block_for_domain("example.org"))[0] - - generated_conf = self.config.parser.parsed[example_conf] - self.assertTrue(util.contains_at_depth(generated_conf, expected1, 2)) - self.assertTrue(util.contains_at_depth(generated_conf, expected2, 2)) - - def test_staple_ocsp_bad_version(self): - self.config.version = (1, 3, 1) - self.assertRaises(errors.PluginError, self.config.enhance, - "www.example.com", "staple-ocsp", "chain_path") - - def test_staple_ocsp_no_chain_path(self): - self.assertRaises(errors.PluginError, self.config.enhance, - "www.example.com", "staple-ocsp", None) - - def test_staple_ocsp_internal_error(self): - self.config.enhance("www.example.com", "staple-ocsp", "chain_path") - # error is raised because the server block has conflicting directives - self.assertRaises(errors.PluginError, self.config.enhance, - "www.example.com", "staple-ocsp", "different_path") - - def test_staple_ocsp(self): - chain_path = "example/chain.pem" - self.config.enhance("www.example.com", "staple-ocsp", chain_path) - - example_conf = self.config.parser.abs_path('sites-enabled/example.com') - generated_conf = self.config.parser.parsed[example_conf] - - self.assertTrue(util.contains_at_depth( - generated_conf, - ['ssl_trusted_certificate', 'example/chain.pem'], 2)) - self.assertTrue(util.contains_at_depth( - generated_conf, ['ssl_stapling', 'on'], 2)) - self.assertTrue(util.contains_at_depth( - generated_conf, ['ssl_stapling_verify', 'on'], 2)) - - def test_deploy_no_match_default_set(self): - default_conf = self.config.parser.abs_path('sites-enabled/default') - foo_conf = self.config.parser.abs_path('foo.conf') - del self.config.parser.parsed[foo_conf][2][1][0][1][0] # remove default_server - self.config.version = (1, 3, 1) - - self.config.deploy_cert( - "www.nomatch.com", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - self.config.save() - - self.config.parser.load() - - parsed_default_conf = util.filter_comments(self.config.parser.parsed[default_conf]) - - self.assertEqual([[['server'], - [['listen', 'myhost', 'default_server'], - ['listen', 'otherhost', 'default_server'], - ['server_name', '"www.example.org"'], - [['location', '/'], - [['root', 'html'], - ['index', 'index.html', 'index.htm']]]]], - [['server'], - [['listen', 'myhost'], - ['listen', 'otherhost'], - ['server_name', 'www.nomatch.com'], - [['location', '/'], - [['root', 'html'], - ['index', 'index.html', 'index.htm']]], - ['listen', '5001', 'ssl'], - ['ssl_certificate', 'example/fullchain.pem'], - ['ssl_certificate_key', 'example/key.pem'], - ['include', self.config.mod_ssl_conf], - ['ssl_dhparam', self.config.ssl_dhparams]]]], - parsed_default_conf) - - self.config.deploy_cert( - "nomatch.com", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - self.config.save() - - self.config.parser.load() - - parsed_default_conf = util.filter_comments(self.config.parser.parsed[default_conf]) - - self.assertTrue(util.contains_at_depth(parsed_default_conf, "nomatch.com", 3)) - - def test_deploy_no_match_default_set_multi_level_path(self): - default_conf = self.config.parser.abs_path('sites-enabled/default') - foo_conf = self.config.parser.abs_path('foo.conf') - del self.config.parser.parsed[default_conf][0][1][0] - del self.config.parser.parsed[default_conf][0][1][0] - self.config.version = (1, 3, 1) - - self.config.deploy_cert( - "www.nomatch.com", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - self.config.save() - - self.config.parser.load() - - parsed_foo_conf = util.filter_comments(self.config.parser.parsed[foo_conf]) - - self.assertEqual([['server'], - [['listen', '*:80', 'ssl'], - ['server_name', 'www.nomatch.com'], - ['root', '/home/ubuntu/sites/foo/'], - [['location', '/status'], [[['types'], [['image/jpeg', 'jpg']]]]], - [['location', '~', 'case_sensitive\\.php$'], [['index', 'index.php'], - ['root', '/var/root']]], - [['location', '~*', 'case_insensitive\\.php$'], []], - [['location', '=', 'exact_match\\.php$'], []], - [['location', '^~', 'ignore_regex\\.php$'], []], - ['ssl_certificate', 'example/fullchain.pem'], - ['ssl_certificate_key', 'example/key.pem']]], - parsed_foo_conf[1][1][1]) - - def test_deploy_no_match_no_default_set(self): - default_conf = self.config.parser.abs_path('sites-enabled/default') - foo_conf = self.config.parser.abs_path('foo.conf') - del self.config.parser.parsed[default_conf][0][1][0] - del self.config.parser.parsed[default_conf][0][1][0] - del self.config.parser.parsed[foo_conf][2][1][0][1][0] - self.config.version = (1, 3, 1) - - self.assertRaises(errors.MisconfigurationError, self.config.deploy_cert, - "www.nomatch.com", "example/cert.pem", "example/key.pem", - "example/chain.pem", "example/fullchain.pem") - - def test_deploy_no_match_fail_multiple_defaults(self): - self.config.version = (1, 3, 1) - self.assertRaises(errors.MisconfigurationError, self.config.deploy_cert, - "www.nomatch.com", "example/cert.pem", "example/key.pem", - "example/chain.pem", "example/fullchain.pem") - - def test_deploy_no_match_multiple_defaults_ok(self): - foo_conf = self.config.parser.abs_path('foo.conf') - self.config.parser.parsed[foo_conf][2][1][0][1][0][1] = '*:5001' - self.config.version = (1, 3, 1) - self.config.deploy_cert("www.nomatch.com", "example/cert.pem", "example/key.pem", - "example/chain.pem", "example/fullchain.pem") - - def test_deploy_no_match_add_redirect(self): - default_conf = self.config.parser.abs_path('sites-enabled/default') - foo_conf = self.config.parser.abs_path('foo.conf') - del self.config.parser.parsed[foo_conf][2][1][0][1][0] # remove default_server - self.config.version = (1, 3, 1) - - self.config.deploy_cert( - "www.nomatch.com", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - - self.config.deploy_cert( - "nomatch.com", - "example/cert.pem", - "example/key.pem", - "example/chain.pem", - "example/fullchain.pem") - - self.config.enhance("www.nomatch.com", "redirect") - - self.config.save() - - self.config.parser.load() - - expected = UnspacedList(_redirect_block_for_domain("www.nomatch.com"))[0] - - generated_conf = self.config.parser.parsed[default_conf] - self.assertTrue(util.contains_at_depth(generated_conf, expected, 2)) - - @mock.patch('certbot.reverter.logger') - @mock.patch('certbot_nginx.parser.NginxParser.load') - def test_parser_reload_after_config_changes(self, mock_parser_load, unused_mock_logger): - self.config.recovery_routine() - self.config.revert_challenge_config() - self.config.rollback_checkpoints() - self.assertTrue(mock_parser_load.call_count == 3) - - def test_choose_vhosts_wildcard(self): - # pylint: disable=protected-access - mock_path = "certbot_nginx.display_ops.select_vhost_multiple" - with mock.patch(mock_path) as mock_select_vhs: - vhost = [x for x in self.config.parser.get_vhosts() - if 'summer.com' in x.names][0] - mock_select_vhs.return_value = [vhost] - vhs = self.config._choose_vhosts_wildcard("*.com", - prefer_ssl=True) - # Check that the dialog was called with migration.com - self.assertTrue(vhost in mock_select_vhs.call_args[0][0]) - - # And the actual returned values - self.assertEqual(len(vhs), 1) - self.assertEqual(vhs[0], vhost) - - def test_choose_vhosts_wildcard_redirect(self): - # pylint: disable=protected-access - mock_path = "certbot_nginx.display_ops.select_vhost_multiple" - with mock.patch(mock_path) as mock_select_vhs: - vhost = [x for x in self.config.parser.get_vhosts() - if 'summer.com' in x.names][0] - mock_select_vhs.return_value = [vhost] - vhs = self.config._choose_vhosts_wildcard("*.com", - prefer_ssl=False) - # Check that the dialog was called with migration.com - self.assertTrue(vhost in mock_select_vhs.call_args[0][0]) - - # And the actual returned values - self.assertEqual(len(vhs), 1) - self.assertEqual(vhs[0], vhost) - - def test_deploy_cert_wildcard(self): - # pylint: disable=protected-access - mock_choose_vhosts = mock.MagicMock() - vhost = [x for x in self.config.parser.get_vhosts() - if 'geese.com' in x.names][0] - mock_choose_vhosts.return_value = [vhost] - self.config._choose_vhosts_wildcard = mock_choose_vhosts - mock_d = "certbot_nginx.configurator.NginxConfigurator._deploy_cert" - with mock.patch(mock_d) as mock_dep: - self.config.deploy_cert("*.com", "/tmp/path", - "/tmp/path", "/tmp/path", "/tmp/path") - self.assertTrue(mock_dep.called) - self.assertEqual(len(mock_dep.call_args_list), 1) - self.assertEqual(vhost, mock_dep.call_args_list[0][0][0]) - - @mock.patch("certbot_nginx.display_ops.select_vhost_multiple") - def test_deploy_cert_wildcard_no_vhosts(self, mock_dialog): - # pylint: disable=protected-access - mock_dialog.return_value = [] - self.assertRaises(errors.PluginError, - self.config.deploy_cert, - "*.wild.cat", "/tmp/path", "/tmp/path", - "/tmp/path", "/tmp/path") - - @mock.patch("certbot_nginx.display_ops.select_vhost_multiple") - def test_enhance_wildcard_ocsp_after_install(self, mock_dialog): - # pylint: disable=protected-access - vhost = [x for x in self.config.parser.get_vhosts() - if 'geese.com' in x.names][0] - self.config._wildcard_vhosts["*.com"] = [vhost] - self.config.enhance("*.com", "staple-ocsp", "example/chain.pem") - self.assertFalse(mock_dialog.called) - - @mock.patch("certbot_nginx.display_ops.select_vhost_multiple") - def test_enhance_wildcard_redirect_or_ocsp_no_install(self, mock_dialog): - vhost = [x for x in self.config.parser.get_vhosts() - if 'summer.com' in x.names][0] - mock_dialog.return_value = [vhost] - self.config.enhance("*.com", "staple-ocsp", "example/chain.pem") - self.assertTrue(mock_dialog.called) - - @mock.patch("certbot_nginx.display_ops.select_vhost_multiple") - def test_enhance_wildcard_double_redirect(self, mock_dialog): - # pylint: disable=protected-access - vhost = [x for x in self.config.parser.get_vhosts() - if 'summer.com' in x.names][0] - self.config._wildcard_redirect_vhosts["*.com"] = [vhost] - self.config.enhance("*.com", "redirect") - self.assertFalse(mock_dialog.called) - - def test_choose_vhosts_wildcard_no_ssl_filter_port(self): - # pylint: disable=protected-access - mock_path = "certbot_nginx.display_ops.select_vhost_multiple" - with mock.patch(mock_path) as mock_select_vhs: - mock_select_vhs.return_value = [] - self.config._choose_vhosts_wildcard("*.com", - prefer_ssl=False, - no_ssl_filter_port='80') - # Check that the dialog was called with only port 80 vhosts - self.assertEqual(len(mock_select_vhs.call_args[0][0]), 5) - - -class InstallSslOptionsConfTest(util.NginxTest): - """Test that the options-ssl-nginx.conf file is installed and updated properly.""" - - def setUp(self): - super(InstallSslOptionsConfTest, self).setUp() - - self.config = util.get_nginx_configurator( - self.config_path, self.config_dir, self.work_dir, self.logs_dir) - - def _call(self): - from certbot_nginx.configurator import install_ssl_options_conf - install_ssl_options_conf(self.config.mod_ssl_conf, self.config.updated_mod_ssl_conf_digest) - - def _current_ssl_options_hash(self): - from certbot_nginx.constants import MOD_SSL_CONF_SRC - return crypto_util.sha256sum(MOD_SSL_CONF_SRC) - - def _assert_current_file(self): - self.assertTrue(os.path.isfile(self.config.mod_ssl_conf)) - self.assertEqual(crypto_util.sha256sum(self.config.mod_ssl_conf), - self._current_ssl_options_hash()) - - def test_no_file(self): - # prepare should have placed a file there - self._assert_current_file() - os.remove(self.config.mod_ssl_conf) - self.assertFalse(os.path.isfile(self.config.mod_ssl_conf)) - self._call() - self._assert_current_file() - - def test_current_file(self): - self._assert_current_file() - self._call() - self._assert_current_file() - - def test_prev_file_updates_to_current(self): - from certbot_nginx.constants import ALL_SSL_OPTIONS_HASHES - with mock.patch('certbot.crypto_util.sha256sum') as mock_sha256: - mock_sha256.return_value = ALL_SSL_OPTIONS_HASHES[0] - self._call() - self._assert_current_file() - - def test_manually_modified_current_file_does_not_update(self): - with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf: - mod_ssl_conf.write("a new line for the wrong hash\n") - with mock.patch("certbot.plugins.common.logger") as mock_logger: - self._call() - self.assertFalse(mock_logger.warning.called) - self.assertTrue(os.path.isfile(self.config.mod_ssl_conf)) - self.assertEqual(crypto_util.sha256sum(constants.MOD_SSL_CONF_SRC), - self._current_ssl_options_hash()) - self.assertNotEqual(crypto_util.sha256sum(self.config.mod_ssl_conf), - self._current_ssl_options_hash()) - - def test_manually_modified_past_file_warns(self): - with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf: - mod_ssl_conf.write("a new line for the wrong hash\n") - with open(self.config.updated_mod_ssl_conf_digest, "w") as f: - f.write("hashofanoldversion") - with mock.patch("certbot.plugins.common.logger") as mock_logger: - self._call() - self.assertEqual(mock_logger.warning.call_args[0][0], - "%s has been manually modified; updated file " - "saved to %s. We recommend updating %s for security purposes.") - self.assertEqual(crypto_util.sha256sum(constants.MOD_SSL_CONF_SRC), - self._current_ssl_options_hash()) - # only print warning once - with mock.patch("certbot.plugins.common.logger") as mock_logger: - self._call() - self.assertFalse(mock_logger.warning.called) - - def test_current_file_hash_in_all_hashes(self): - from certbot_nginx.constants import ALL_SSL_OPTIONS_HASHES - self.assertTrue(self._current_ssl_options_hash() in ALL_SSL_OPTIONS_HASHES, - "Constants.ALL_SSL_OPTIONS_HASHES must be appended" - " with the sha256 hash of self.config.mod_ssl_conf when it is updated.") - - -class DetermineDefaultServerRootTest(certbot_test_util.ConfigTestCase): - """Tests for certbot_nginx.configurator._determine_default_server_root.""" - - def _call(self): - from certbot_nginx.configurator import _determine_default_server_root - return _determine_default_server_root() - - @mock.patch.dict(os.environ, {"CERTBOT_DOCS": "1"}) - def test_docs_value(self): - self._test(expect_both_values=True) - - @mock.patch.dict(os.environ, {}) - def test_real_values(self): - self._test(expect_both_values=False) - - def _test(self, expect_both_values): - server_root = self._call() - - if expect_both_values: - self.assertIn("/usr/local/etc/nginx", server_root) - self.assertIn("/etc/nginx", server_root) - else: - self.assertTrue(server_root == "/etc/nginx" or server_root == "/usr/local/etc/nginx") - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-nginx/certbot_nginx/tests/display_ops_test.py b/certbot-nginx/certbot_nginx/tests/display_ops_test.py deleted file mode 100644 index e3c6fb66be9..00000000000 --- a/certbot-nginx/certbot_nginx/tests/display_ops_test.py +++ /dev/null @@ -1,45 +0,0 @@ -"""Test certbot_apache.display_ops.""" -import unittest - -from certbot.display import util as display_util - -from certbot.tests import util as certbot_util - -from certbot_nginx import parser - -from certbot_nginx.display_ops import select_vhost_multiple -from certbot_nginx.tests import util - - -class SelectVhostMultiTest(util.NginxTest): - """Tests for certbot_nginx.display_ops.select_vhost_multiple.""" - - def setUp(self): - super(SelectVhostMultiTest, self).setUp() - nparser = parser.NginxParser(self.config_path) - self.vhosts = nparser.get_vhosts() - - def test_select_no_input(self): - self.assertFalse(select_vhost_multiple([])) - - @certbot_util.patch_get_utility() - def test_select_correct(self, mock_util): - mock_util().checklist.return_value = ( - display_util.OK, [self.vhosts[3].display_repr(), - self.vhosts[2].display_repr()]) - vhs = select_vhost_multiple([self.vhosts[3], - self.vhosts[2], - self.vhosts[1]]) - self.assertTrue(self.vhosts[2] in vhs) - self.assertTrue(self.vhosts[3] in vhs) - self.assertFalse(self.vhosts[1] in vhs) - - @certbot_util.patch_get_utility() - def test_select_cancel(self, mock_util): - mock_util().checklist.return_value = (display_util.CANCEL, "whatever") - vhs = select_vhost_multiple([self.vhosts[2], self.vhosts[3]]) - self.assertFalse(vhs) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-nginx/certbot_nginx/tests/http_01_test.py b/certbot-nginx/certbot_nginx/tests/http_01_test.py deleted file mode 100644 index ed3c257eeee..00000000000 --- a/certbot-nginx/certbot_nginx/tests/http_01_test.py +++ /dev/null @@ -1,149 +0,0 @@ -"""Tests for certbot_nginx.http_01""" -import unittest -import shutil - -import mock -import six - -from acme import challenges - -from certbot import achallenges - -from certbot.plugins import common_test -from certbot.tests import acme_util - -from certbot_nginx.obj import Addr -from certbot_nginx.tests import util - - -class HttpPerformTest(util.NginxTest): - """Test the NginxHttp01 challenge.""" - - account_key = common_test.AUTH_KEY - achalls = [ - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.HTTP01(token=b"kNdwjwOeX0I_A8DXt9Msmg"), "pending"), - domain="www.example.com", account_key=account_key), - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.HTTP01( - token=b"\xba\xa9\xda?= 1 and - x[0] == 2, - lambda x, y, pts=paths: pts.append(y)) - self.assertEqual(paths, result) - - def test_get_vhosts_global_ssl(self): - nparser = parser.NginxParser(self.config_path) - vhosts = nparser.get_vhosts() - - vhost = obj.VirtualHost(nparser.abs_path('sites-enabled/globalssl.com'), - [obj.Addr('4.8.2.6', '57', True, False, - False, False)], - True, True, set(['globalssl.com']), [], [0]) - - globalssl_com = [x for x in vhosts if 'globalssl.com' in x.filep][0] - self.assertEqual(vhost, globalssl_com) - - def test_get_vhosts(self): - nparser = parser.NginxParser(self.config_path) - vhosts = nparser.get_vhosts() - - vhost1 = obj.VirtualHost(nparser.abs_path('nginx.conf'), - [obj.Addr('', '8080', False, False, - False, False)], - False, True, - set(['localhost', - r'~^(www\.)?(example|bar)\.']), - [], [10, 1, 9]) - vhost2 = obj.VirtualHost(nparser.abs_path('nginx.conf'), - [obj.Addr('somename', '8080', False, False, - False, False), - obj.Addr('', '8000', False, False, - False, False)], - False, True, - set(['somename', 'another.alias', 'alias']), - [], [10, 1, 12]) - vhost3 = obj.VirtualHost(nparser.abs_path('sites-enabled/example.com'), - [obj.Addr('69.50.225.155', '9000', - False, False, False, False), - obj.Addr('127.0.0.1', '', False, False, - False, False)], - False, True, - set(['.example.com', 'example.*']), [], [0]) - vhost4 = obj.VirtualHost(nparser.abs_path('sites-enabled/default'), - [obj.Addr('myhost', '', False, True, - False, False), - obj.Addr('otherhost', '', False, True, - False, False)], - False, True, set(['www.example.org']), - [], [0]) - vhost5 = obj.VirtualHost(nparser.abs_path('foo.conf'), - [obj.Addr('*', '80', True, True, - False, False)], - True, True, set(['*.www.foo.com', - '*.www.example.com']), - [], [2, 1, 0]) - - self.assertEqual(13, len(vhosts)) - example_com = [x for x in vhosts if 'example.com' in x.filep][0] - self.assertEqual(vhost3, example_com) - default = [x for x in vhosts if 'default' in x.filep][0] - self.assertEqual(vhost4, default) - fooconf = [x for x in vhosts if 'foo.conf' in x.filep][0] - self.assertEqual(vhost5, fooconf) - localhost = [x for x in vhosts if 'localhost' in x.names][0] - self.assertEqual(vhost1, localhost) - somename = [x for x in vhosts if 'somename' in x.names][0] - self.assertEqual(vhost2, somename) - - def test_has_ssl_on_directive(self): - nparser = parser.NginxParser(self.config_path) - mock_vhost = obj.VirtualHost(None, None, None, None, None, - [['listen', 'myhost default_server'], - ['server_name', 'www.example.org'], - [['location', '/'], [['root', 'html'], ['index', 'index.html index.htm']]] - ], None) - self.assertFalse(nparser.has_ssl_on_directive(mock_vhost)) - mock_vhost.raw = [['listen', '*:80', 'default_server', 'ssl'], - ['server_name', '*.www.foo.com', '*.www.example.com'], - ['root', '/home/ubuntu/sites/foo/']] - self.assertFalse(nparser.has_ssl_on_directive(mock_vhost)) - mock_vhost.raw = [['listen', '80 ssl'], - ['server_name', '*.www.foo.com', '*.www.example.com']] - self.assertFalse(nparser.has_ssl_on_directive(mock_vhost)) - mock_vhost.raw = [['listen', '80'], - ['ssl', 'on'], - ['server_name', '*.www.foo.com', '*.www.example.com']] - self.assertTrue(nparser.has_ssl_on_directive(mock_vhost)) - - - def test_remove_server_directives(self): - nparser = parser.NginxParser(self.config_path) - mock_vhost = obj.VirtualHost(nparser.abs_path('nginx.conf'), - None, None, None, - set(['localhost', - r'~^(www\.)?(example|bar)\.']), - None, [10, 1, 9]) - example_com = nparser.abs_path('sites-enabled/example.com') - names = set(['.example.com', 'example.*']) - mock_vhost.filep = example_com - mock_vhost.names = names - mock_vhost.path = [0] - nparser.add_server_directives(mock_vhost, - [['foo', 'bar'], ['ssl_certificate', - '/etc/ssl/cert2.pem']]) - nparser.remove_server_directives(mock_vhost, 'foo') - nparser.remove_server_directives(mock_vhost, 'ssl_certificate') - self.assertEqual(nparser.parsed[example_com], - [[['server'], [['listen', '69.50.225.155:9000'], - ['listen', '127.0.0.1'], - ['server_name', '.example.com'], - ['server_name', 'example.*'], - []]]]) - - def test_add_server_directives(self): - nparser = parser.NginxParser(self.config_path) - mock_vhost = obj.VirtualHost(nparser.abs_path('nginx.conf'), - None, None, None, - set(['localhost', - r'~^(www\.)?(example|bar)\.']), - None, [10, 1, 9]) - nparser.add_server_directives(mock_vhost, - [['foo', 'bar'], ['\n ', 'ssl_certificate', ' ', - '/etc/ssl/cert.pem']]) - ssl_re = re.compile(r'\n\s+ssl_certificate /etc/ssl/cert.pem') - dump = nginxparser.dumps(nparser.parsed[nparser.abs_path('nginx.conf')]) - self.assertEqual(1, len(re.findall(ssl_re, dump))) - - example_com = nparser.abs_path('sites-enabled/example.com') - names = set(['.example.com', 'example.*']) - mock_vhost.filep = example_com - mock_vhost.names = names - mock_vhost.path = [0] - nparser.add_server_directives(mock_vhost, - [['foo', 'bar'], ['ssl_certificate', - '/etc/ssl/cert2.pem']]) - nparser.add_server_directives(mock_vhost, [['foo', 'bar']]) - from certbot_nginx.parser import COMMENT - self.assertEqual(nparser.parsed[example_com], - [[['server'], [['listen', '69.50.225.155:9000'], - ['listen', '127.0.0.1'], - ['server_name', '.example.com'], - ['server_name', 'example.*'], - ['foo', 'bar'], - ['#', COMMENT], - ['ssl_certificate', '/etc/ssl/cert2.pem'], - ['#', COMMENT], [], [] - ]]]) - - server_conf = nparser.abs_path('server.conf') - names = set(['alias', 'another.alias', 'somename']) - mock_vhost.filep = server_conf - mock_vhost.names = names - mock_vhost.path = [] - self.assertRaises(errors.MisconfigurationError, - nparser.add_server_directives, - mock_vhost, - [['foo', 'bar'], - ['ssl_certificate', '/etc/ssl/cert2.pem']]) - - def test_comment_is_repeatable(self): - nparser = parser.NginxParser(self.config_path) - example_com = nparser.abs_path('sites-enabled/example.com') - mock_vhost = obj.VirtualHost(example_com, - None, None, None, - set(['.example.com', 'example.*']), - None, [0]) - nparser.add_server_directives(mock_vhost, - [['\n ', '#', ' ', 'what a nice comment']]) - nparser.add_server_directives(mock_vhost, - [['\n ', 'include', ' ', - nparser.abs_path('comment_in_file.conf')]]) - from certbot_nginx.parser import COMMENT - self.assertEqual(nparser.parsed[example_com], - [[['server'], [['listen', '69.50.225.155:9000'], - ['listen', '127.0.0.1'], - ['server_name', '.example.com'], - ['server_name', 'example.*'], - ['#', ' ', 'what a nice comment'], - [], - ['include', nparser.abs_path('comment_in_file.conf')], - ['#', COMMENT], - []]]] -) - - def test_replace_server_directives(self): - nparser = parser.NginxParser(self.config_path) - target = set(['.example.com', 'example.*']) - filep = nparser.abs_path('sites-enabled/example.com') - mock_vhost = obj.VirtualHost(filep, None, None, None, target, None, [0]) - nparser.update_or_add_server_directives( - mock_vhost, [['server_name', 'foobar.com']]) - from certbot_nginx.parser import COMMENT - self.assertEqual( - nparser.parsed[filep], - [[['server'], [['listen', '69.50.225.155:9000'], - ['listen', '127.0.0.1'], - ['server_name', 'foobar.com'], ['#', COMMENT], - ['server_name', 'example.*'], [] - ]]]) - mock_vhost.names = set(['foobar.com', 'example.*']) - nparser.update_or_add_server_directives( - mock_vhost, [['ssl_certificate', 'cert.pem']]) - self.assertEqual( - nparser.parsed[filep], - [[['server'], [['listen', '69.50.225.155:9000'], - ['listen', '127.0.0.1'], - ['server_name', 'foobar.com'], ['#', COMMENT], - ['server_name', 'example.*'], [], - ['ssl_certificate', 'cert.pem'], ['#', COMMENT], [], - ]]]) - - def test_get_best_match(self): - target_name = 'www.eff.org' - names = [set(['www.eff.org', 'irrelevant.long.name.eff.org', '*.org']), - set(['eff.org', 'ww2.eff.org', 'test.www.eff.org']), - set(['*.eff.org', '.www.eff.org']), - set(['.eff.org', '*.org']), - set(['www.eff.', 'www.eff.*', '*.www.eff.org']), - set(['example.com', r'~^(www\.)?(eff.+)', '*.eff.*']), - set(['*', r'~^(www\.)?(eff.+)']), - set(['www.*', r'~^(www\.)?(eff.+)', '.test.eff.org']), - set(['*.org', r'*.eff.org', 'www.eff.*']), - set(['*.www.eff.org', 'www.*']), - set(['*.org']), - set([]), - set(['example.com'])] - winners = [('exact', 'www.eff.org'), - (None, None), - ('exact', '.www.eff.org'), - ('wildcard_start', '.eff.org'), - ('wildcard_end', 'www.eff.*'), - ('regex', r'~^(www\.)?(eff.+)'), - ('wildcard_start', '*'), - ('wildcard_end', 'www.*'), - ('wildcard_start', '*.eff.org'), - ('wildcard_end', 'www.*'), - ('wildcard_start', '*.org'), - (None, None), - (None, None)] - - for i, winner in enumerate(winners): - self.assertEqual(winner, - parser.get_best_match(target_name, names[i])) - - def test_comment_directive(self): - # pylint: disable=protected-access - block = nginxparser.UnspacedList([ - ["\n", "a", " ", "b", "\n"], - ["c", " ", "d"], - ["\n", "e", " ", "f"]]) - from certbot_nginx.parser import comment_directive, COMMENT_BLOCK - comment_directive(block, 1) - comment_directive(block, 0) - self.assertEqual(block.spaced, [ - ["\n", "a", " ", "b", "\n"], - COMMENT_BLOCK, - "\n", - ["c", " ", "d"], - COMMENT_BLOCK, - ["\n", "e", " ", "f"]]) - - def test_comment_out_directive(self): - server_block = nginxparser.loads(""" - server { - listen 80; - root /var/www/html; - index star.html; - - server_name *.functorkitten.xyz; - ssl_session_timeout 1440m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; - - ssl_prefer_server_ciphers on; - }""") - block = server_block[0][1] - from certbot_nginx.parser import _comment_out_directive - _comment_out_directive(block, 4, "blah1") - _comment_out_directive(block, 5, "blah2") - _comment_out_directive(block, 6, "blah3") - self.assertEqual(block.spaced, [ - ['\n ', 'listen', ' ', '80'], - ['\n ', 'root', ' ', '/var/www/html'], - ['\n ', 'index', ' ', 'star.html'], - ['\n\n ', 'server_name', ' ', '*.functorkitten.xyz'], - ['\n ', '#', ' ssl_session_timeout 1440m; # duplicated in blah1'], - [' ', '#', ' ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # duplicated in blah2'], - ['\n\n ', '#', ' ssl_prefer_server_ciphers on; # duplicated in blah3'], - '\n ']) - - def test_parse_server_raw_ssl(self): - server = parser._parse_server_raw([ #pylint: disable=protected-access - ['listen', '443'] - ]) - self.assertFalse(server['ssl']) - - server = parser._parse_server_raw([ #pylint: disable=protected-access - ['listen', '443', 'ssl'] - ]) - self.assertTrue(server['ssl']) - - server = parser._parse_server_raw([ #pylint: disable=protected-access - ['listen', '443'], ['ssl', 'off'] - ]) - self.assertFalse(server['ssl']) - - server = parser._parse_server_raw([ #pylint: disable=protected-access - ['listen', '443'], ['ssl', 'on'] - ]) - self.assertTrue(server['ssl']) - - def test_parse_server_raw_unix(self): - server = parser._parse_server_raw([ #pylint: disable=protected-access - ['listen', 'unix:/var/run/nginx.sock'] - ]) - self.assertEqual(len(server['addrs']), 0) - - def test_parse_server_global_ssl_applied(self): - nparser = parser.NginxParser(self.config_path) - server = nparser.parse_server([ - ['listen', '443'] - ]) - self.assertTrue(server['ssl']) - - def test_duplicate_vhost(self): - nparser = parser.NginxParser(self.config_path) - - vhosts = nparser.get_vhosts() - default = [x for x in vhosts if 'default' in x.filep][0] - new_vhost = nparser.duplicate_vhost(default, remove_singleton_listen_params=True) - nparser.filedump(ext='') - - # check properties of new vhost - self.assertFalse(next(iter(new_vhost.addrs)).default) - self.assertNotEqual(new_vhost.path, default.path) - - # check that things are written to file correctly - new_nparser = parser.NginxParser(self.config_path) - new_vhosts = new_nparser.get_vhosts() - new_defaults = [x for x in new_vhosts if 'default' in x.filep] - self.assertEqual(len(new_defaults), 2) - new_vhost_parsed = new_defaults[1] - self.assertFalse(next(iter(new_vhost_parsed.addrs)).default) - self.assertEqual(next(iter(default.names)), next(iter(new_vhost_parsed.names))) - self.assertEqual(len(default.raw), len(new_vhost_parsed.raw)) - self.assertTrue(next(iter(default.addrs)).super_eq(next(iter(new_vhost_parsed.addrs)))) - - def test_duplicate_vhost_remove_ipv6only(self): - nparser = parser.NginxParser(self.config_path) - - vhosts = nparser.get_vhosts() - ipv6ssl = [x for x in vhosts if 'ipv6ssl' in x.filep][0] - new_vhost = nparser.duplicate_vhost(ipv6ssl, remove_singleton_listen_params=True) - nparser.filedump(ext='') - - for addr in new_vhost.addrs: - self.assertFalse(addr.ipv6only) - - identical_vhost = nparser.duplicate_vhost(ipv6ssl, remove_singleton_listen_params=False) - nparser.filedump(ext='') - - called = False - for addr in identical_vhost.addrs: - if addr.ipv6: - self.assertTrue(addr.ipv6only) - called = True - self.assertTrue(called) - - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/headers.com b/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/headers.com deleted file mode 100644 index 6c032928c75..00000000000 --- a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/headers.com +++ /dev/null @@ -1,4 +0,0 @@ -server { - server_name headers.com; - add_header X-Content-Type-Options nosniff; -} diff --git a/certbot-nginx/certbot_nginx/tests/tls_sni_01_test.py b/certbot-nginx/certbot_nginx/tests/tls_sni_01_test.py deleted file mode 100644 index 72b65911cfc..00000000000 --- a/certbot-nginx/certbot_nginx/tests/tls_sni_01_test.py +++ /dev/null @@ -1,164 +0,0 @@ -"""Tests for certbot_nginx.tls_sni_01""" -import unittest -import shutil - -import mock -import six - -from acme import challenges - -from certbot import achallenges -from certbot import errors - -from certbot.plugins import common_test -from certbot.tests import acme_util - -from certbot_nginx import obj -from certbot_nginx.tests import util - - -class TlsSniPerformTest(util.NginxTest): - """Test the NginxTlsSni01 challenge.""" - - account_key = common_test.AUTH_KEY - achalls = [ - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.TLSSNI01(token=b"kNdwjwOeX0I_A8DXt9Msmg"), "pending"), - domain="www.example.com", account_key=account_key), - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.TLSSNI01( - token=b"\xba\xa9\xda? 1 else None - else: - key = values = "" - if isinstance(key, list): - new = copy.deepcopy(entry) - new[1] = filter_comments(values) - yield new - else: - if key != '#' and spaceless: - yield spaceless - - return list(traverse(tree)) - - -def contains_at_depth(haystack, needle, n): - """Is the needle in haystack at depth n? - - Return true if the needle is present in one of the sub-iterables in haystack - at depth n. Haystack must be an iterable. - """ - # Specifically use hasattr rather than isinstance(..., collections.Iterable) - # because we want to include lists but reject strings. - if not hasattr(haystack, '__iter__') or hasattr(haystack, 'strip'): - return False - if n == 0: - return needle in haystack - else: - for item in haystack: - if contains_at_depth(item, needle, n - 1): - return True - return False diff --git a/certbot-nginx/certbot_nginx/tls_sni_01.py b/certbot-nginx/certbot_nginx/tls_sni_01.py deleted file mode 100644 index 60ec1ed1acb..00000000000 --- a/certbot-nginx/certbot_nginx/tls_sni_01.py +++ /dev/null @@ -1,177 +0,0 @@ -"""A class that performs TLS-SNI-01 challenges for Nginx""" - -import logging -import os - -import six - -from certbot import errors -from certbot.plugins import common - -from certbot_nginx import obj -from certbot_nginx import nginxparser - - -logger = logging.getLogger(__name__) - - -class NginxTlsSni01(common.TLSSNI01): - """TLS-SNI-01 authenticator for Nginx - - :ivar configurator: NginxConfigurator object - :type configurator: :class:`~nginx.configurator.NginxConfigurator` - - :ivar list achalls: Annotated - class:`~certbot.achallenges.KeyAuthorizationAnnotatedChallenge` - challenges - - :param list indices: Meant to hold indices of challenges in a - larger array. NginxTlsSni01 is capable of solving many challenges - at once which causes an indexing issue within NginxConfigurator - who must return all responses in order. Imagine NginxConfigurator - maintaining state about where all of the http-01 Challenges, - TLS-SNI-01 Challenges belong in the response array. This is an - optional utility. - - :param str challenge_conf: location of the challenge config file - - """ - - def perform(self): - """Perform a challenge on Nginx. - - :returns: list of :class:`certbot.acme.challenges.TLSSNI01Response` - :rtype: list - - """ - if not self.achalls: - return [] - - addresses = [] - default_addr = "{0} ssl".format( - self.configurator.config.tls_sni_01_port) - - for achall in self.achalls: - vhosts = self.configurator.choose_vhosts(achall.domain, create_if_no_match=True) - - # len is max 1 because Nginx doesn't authenticate wildcards - if vhosts and vhosts[0].addrs: - addresses.append(list(vhosts[0].addrs)) - else: - # choose_vhosts might have modified vhosts, so put this after - ipv6, ipv6only = self.configurator.ipv6_info( - self.configurator.config.tls_sni_01_port) - if ipv6: - # If IPv6 is active in Nginx configuration - ipv6_addr = "[::]:{0} ssl".format( - self.configurator.config.tls_sni_01_port) - if not ipv6only: - # If ipv6only=on is not already present in the config - ipv6_addr = ipv6_addr + " ipv6only=on" - addresses.append([obj.Addr.fromstring(default_addr), - obj.Addr.fromstring(ipv6_addr)]) - logger.info(("Using default addresses %s and %s for " + - "TLSSNI01 authentication."), - default_addr, - ipv6_addr) - else: - addresses.append([obj.Addr.fromstring(default_addr)]) - logger.info("Using default address %s for TLSSNI01 authentication.", - default_addr) - - # Create challenge certs - responses = [self._setup_challenge_cert(x) for x in self.achalls] - - # Set up the configuration - self._mod_config(addresses) - - # Save reversible changes - self.configurator.save("SNI Challenge", True) - - return responses - - def _mod_config(self, ll_addrs): - """Modifies Nginx config to include challenge server blocks. - - :param list ll_addrs: list of lists of - :class:`certbot_nginx.obj.Addr` to apply - - :raises .MisconfigurationError: - Unable to find a suitable HTTP block in which to include - authenticator hosts. - - """ - # Add the 'include' statement for the challenges if it doesn't exist - # already in the main config - included = False - include_directive = ['\n', 'include', ' ', self.challenge_conf] - root = self.configurator.parser.config_root - - bucket_directive = ['\n', 'server_names_hash_bucket_size', ' ', '128'] - - main = self.configurator.parser.parsed[root] - for line in main: - if line[0] == ['http']: - body = line[1] - found_bucket = False - posn = 0 - for inner_line in body: - if inner_line[0] == bucket_directive[1]: - if int(inner_line[1]) < int(bucket_directive[3]): - body[posn] = bucket_directive - found_bucket = True - posn += 1 - if not found_bucket: - body.insert(0, bucket_directive) - if include_directive not in body: - body.insert(0, include_directive) - included = True - break - if not included: - raise errors.MisconfigurationError( - 'Certbot could not find an HTTP block to include ' - 'TLS-SNI-01 challenges in %s.' % root) - config = [self._make_server_block(pair[0], pair[1]) - for pair in six.moves.zip(self.achalls, ll_addrs)] - config = nginxparser.UnspacedList(config) - - self.configurator.reverter.register_file_creation( - True, self.challenge_conf) - - with open(self.challenge_conf, "w") as new_conf: - nginxparser.dump(config, new_conf) - - logger.debug("Generated server block:\n%s", str(config)) - - def _make_server_block(self, achall, addrs): - """Creates a server block for a challenge. - - :param achall: Annotated TLS-SNI-01 challenge - :type achall: - :class:`certbot.achallenges.KeyAuthorizationAnnotatedChallenge` - - :param list addrs: addresses of challenged domain - :class:`list` of type :class:`~nginx.obj.Addr` - - :returns: server block for the challenge host - :rtype: list - - """ - document_root = os.path.join( - self.configurator.config.work_dir, "tls_sni_01_page") - - block = [['listen', ' ', addr.to_string(include_default=False)] for addr in addrs] - - block.extend([['server_name', ' ', - achall.response(achall.account_key).z_domain.decode('ascii')], - # access and error logs necessary for - # integration testing (non-root) - ['access_log', ' ', os.path.join( - self.configurator.config.work_dir, 'access.log')], - ['error_log', ' ', os.path.join( - self.configurator.config.work_dir, 'error.log')], - ['ssl_certificate', ' ', self.get_cert_path(achall)], - ['ssl_certificate_key', ' ', self.get_key_path(achall)], - ['include', ' ', self.configurator.mod_ssl_conf], - [['location', ' ', '/'], [['root', ' ', document_root]]]]) - return [['server'], block] diff --git a/certbot-nginx/docs/.gitignore b/certbot-nginx/docs/.gitignore deleted file mode 100644 index ba65b13af5e..00000000000 --- a/certbot-nginx/docs/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/_build/ diff --git a/certbot-nginx/docs/Makefile b/certbot-nginx/docs/Makefile deleted file mode 100644 index 0bd88a34752..00000000000 --- a/certbot-nginx/docs/Makefile +++ /dev/null @@ -1,192 +0,0 @@ -# Makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -PAPER = -BUILDDIR = _build - -# User-friendly check for sphinx-build -ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) -$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) -endif - -# Internal variables. -PAPEROPT_a4 = -D latex_paper_size=a4 -PAPEROPT_letter = -D latex_paper_size=letter -ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . -# the i18n builder cannot share the environment and doctrees with the others -I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . - -.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext - -help: - @echo "Please use \`make ' where is one of" - @echo " html to make standalone HTML files" - @echo " dirhtml to make HTML files named index.html in directories" - @echo " singlehtml to make a single large HTML file" - @echo " pickle to make pickle files" - @echo " json to make JSON files" - @echo " htmlhelp to make HTML files and a HTML help project" - @echo " qthelp to make HTML files and a qthelp project" - @echo " applehelp to make an Apple Help Book" - @echo " devhelp to make HTML files and a Devhelp project" - @echo " epub to make an epub" - @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" - @echo " latexpdf to make LaTeX files and run them through pdflatex" - @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" - @echo " text to make text files" - @echo " man to make manual pages" - @echo " texinfo to make Texinfo files" - @echo " info to make Texinfo files and run them through makeinfo" - @echo " gettext to make PO message catalogs" - @echo " changes to make an overview of all changed/added/deprecated items" - @echo " xml to make Docutils-native XML files" - @echo " pseudoxml to make pseudoxml-XML files for display purposes" - @echo " linkcheck to check all external links for integrity" - @echo " doctest to run all doctests embedded in the documentation (if enabled)" - @echo " coverage to run coverage check of the documentation (if enabled)" - -clean: - rm -rf $(BUILDDIR)/* - -html: - $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." - -dirhtml: - $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." - -singlehtml: - $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml - @echo - @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." - -pickle: - $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle - @echo - @echo "Build finished; now you can process the pickle files." - -json: - $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json - @echo - @echo "Build finished; now you can process the JSON files." - -htmlhelp: - $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp - @echo - @echo "Build finished; now you can run HTML Help Workshop with the" \ - ".hhp project file in $(BUILDDIR)/htmlhelp." - -qthelp: - $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp - @echo - @echo "Build finished; now you can run "qcollectiongenerator" with the" \ - ".qhcp project file in $(BUILDDIR)/qthelp, like this:" - @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/certbot-nginx.qhcp" - @echo "To view the help file:" - @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/certbot-nginx.qhc" - -applehelp: - $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp - @echo - @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." - @echo "N.B. You won't be able to view it unless you put it in" \ - "~/Library/Documentation/Help or install it in your application" \ - "bundle." - -devhelp: - $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp - @echo - @echo "Build finished." - @echo "To view the help file:" - @echo "# mkdir -p $$HOME/.local/share/devhelp/certbot-nginx" - @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/certbot-nginx" - @echo "# devhelp" - -epub: - $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub - @echo - @echo "Build finished. The epub file is in $(BUILDDIR)/epub." - -latex: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo - @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." - @echo "Run \`make' in that directory to run these through (pdf)latex" \ - "(use \`make latexpdf' here to do that automatically)." - -latexpdf: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through pdflatex..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -latexpdfja: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through platex and dvipdfmx..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -text: - $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text - @echo - @echo "Build finished. The text files are in $(BUILDDIR)/text." - -man: - $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man - @echo - @echo "Build finished. The manual pages are in $(BUILDDIR)/man." - -texinfo: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo - @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." - @echo "Run \`make' in that directory to run these through makeinfo" \ - "(use \`make info' here to do that automatically)." - -info: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo "Running Texinfo files through makeinfo..." - make -C $(BUILDDIR)/texinfo info - @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." - -gettext: - $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale - @echo - @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." - -changes: - $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes - @echo - @echo "The overview file is in $(BUILDDIR)/changes." - -linkcheck: - $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck - @echo - @echo "Link check complete; look for any errors in the above output " \ - "or in $(BUILDDIR)/linkcheck/output.txt." - -doctest: - $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest - @echo "Testing of doctests in the sources finished, look at the " \ - "results in $(BUILDDIR)/doctest/output.txt." - -coverage: - $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage - @echo "Testing of coverage in the sources finished, look at the " \ - "results in $(BUILDDIR)/coverage/python.txt." - -xml: - $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml - @echo - @echo "Build finished. The XML files are in $(BUILDDIR)/xml." - -pseudoxml: - $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml - @echo - @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/certbot-nginx/docs/api.rst b/certbot-nginx/docs/api.rst deleted file mode 100644 index 8668ec5d8ad..00000000000 --- a/certbot-nginx/docs/api.rst +++ /dev/null @@ -1,8 +0,0 @@ -================= -API Documentation -================= - -.. toctree:: - :glob: - - api/** diff --git a/certbot-nginx/docs/api/nginxparser.rst b/certbot-nginx/docs/api/nginxparser.rst deleted file mode 100644 index 6a3be52477c..00000000000 --- a/certbot-nginx/docs/api/nginxparser.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_nginx.nginxparser` ------------------------------------- - -.. automodule:: certbot_nginx.nginxparser - :members: diff --git a/certbot-nginx/docs/api/obj.rst b/certbot-nginx/docs/api/obj.rst deleted file mode 100644 index a2c94037bb6..00000000000 --- a/certbot-nginx/docs/api/obj.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_nginx.obj` ----------------------------- - -.. automodule:: certbot_nginx.obj - :members: diff --git a/certbot-nginx/docs/api/parser.rst b/certbot-nginx/docs/api/parser.rst deleted file mode 100644 index 0149f99cbbf..00000000000 --- a/certbot-nginx/docs/api/parser.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_nginx.parser` -------------------------------- - -.. automodule:: certbot_nginx.parser - :members: diff --git a/certbot-nginx/docs/api/tls_sni_01.rst b/certbot-nginx/docs/api/tls_sni_01.rst deleted file mode 100644 index 5074f63d9f1..00000000000 --- a/certbot-nginx/docs/api/tls_sni_01.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_nginx.tls_sni_01` ------------------------------------ - -.. automodule:: certbot_nginx.tls_sni_01 - :members: diff --git a/certbot-nginx/docs/conf.py b/certbot-nginx/docs/conf.py deleted file mode 100644 index 167abb4fb75..00000000000 --- a/certbot-nginx/docs/conf.py +++ /dev/null @@ -1,311 +0,0 @@ -# -*- coding: utf-8 -*- -# -# certbot-nginx documentation build configuration file, created by -# sphinx-quickstart on Sun Oct 18 13:39:39 2015. -# -# This file is execfile()d with the current directory set to its -# containing dir. -# -# Note that not all possible configuration values are present in this -# autogenerated file. -# -# All configuration values have a default; values that are commented out -# serve to show the default. - -import sys -import os -import shlex - - -here = os.path.abspath(os.path.dirname(__file__)) - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath(os.path.join(here, '..'))) - -# -- General configuration ------------------------------------------------ - -# If your documentation needs a minimal Sphinx version, state it here. -needs_sphinx = '1.0' - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.viewcode', -] - -autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# The suffix(es) of source filenames. -# You can specify multiple suffix as a list of string: -# source_suffix = ['.rst', '.md'] -source_suffix = '.rst' - -# The encoding of source files. -#source_encoding = 'utf-8-sig' - -# The master toctree document. -master_doc = 'index' - -# General information about the project. -project = u'certbot-nginx' -copyright = u'2014-2015, Let\'s Encrypt Project' -author = u'Let\'s Encrypt Project' - -# The version info for the project you're documenting, acts as replacement for -# |version| and |release|, also used in various other places throughout the -# built documents. -# -# The short X.Y version. -version = '0' -# The full version, including alpha/beta/rc tags. -release = '0' - -# The language for content autogenerated by Sphinx. Refer to documentation -# for a list of supported languages. -# -# This is also used if you do content translation via gettext catalogs. -# Usually you set "language" from the command line for these cases. -language = 'en' - -# There are two options for replacing |today|: either, you set today to some -# non-false value, then it is used: -#today = '' -# Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -exclude_patterns = ['_build'] - -# The reST default role (used for this markup: `text`) to use for all -# documents. -default_role = 'py:obj' - -# If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True - -# If true, the current module name will be prepended to all description -# unit titles (such as .. function::). -#add_module_names = True - -# If true, sectionauthor and moduleauthor directives will be shown in the -# output. They are ignored by default. -#show_authors = False - -# The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' - -# A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] - -# If true, keep warnings as "system message" paragraphs in the built documents. -#keep_warnings = False - -# If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True - - -# -- Options for HTML output ---------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. - -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it - -# Theme options are theme-specific and customize the look and feel of a theme -# further. For a list of options available for each theme, see the -# documentation. -#html_theme_options = {} - -# Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] - -# The name for this set of Sphinx documents. If None, it defaults to -# " v documentation". -#html_title = None - -# A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None - -# The name of an image file (relative to this directory) to place at the top -# of the sidebar. -#html_logo = None - -# The name of an image file (within the static path) to use as favicon of the -# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 -# pixels large. -#html_favicon = None - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] - -# Add any extra paths that contain custom files (such as robots.txt or -# .htaccess) here, relative to this directory. These files are copied -# directly to the root of the documentation. -#html_extra_path = [] - -# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, -# using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' - -# If true, SmartyPants will be used to convert quotes and dashes to -# typographically correct entities. -#html_use_smartypants = True - -# Custom sidebar templates, maps document names to template names. -#html_sidebars = {} - -# Additional templates that should be rendered to pages, maps page names to -# template names. -#html_additional_pages = {} - -# If false, no module index is generated. -#html_domain_indices = True - -# If false, no index is generated. -#html_use_index = True - -# If true, the index is split into individual pages for each letter. -#html_split_index = False - -# If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True - -# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True - -# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True - -# If true, an OpenSearch description file will be output, and all pages will -# contain a tag referring to it. The value of this option must be the -# base URL from which the finished HTML is served. -#html_use_opensearch = '' - -# This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None - -# Language to be used for generating the HTML full-text search index. -# Sphinx supports the following languages: -# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' -# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' -#html_search_language = 'en' - -# A dictionary with options for the search language support, empty by default. -# Now only 'ja' uses this config value -#html_search_options = {'type': 'default'} - -# The name of a javascript file (relative to the configuration directory) that -# implements a search results scorer. If empty, the default will be used. -#html_search_scorer = 'scorer.js' - -# Output file base name for HTML help builder. -htmlhelp_basename = 'certbot-nginxdoc' - -# -- Options for LaTeX output --------------------------------------------- - -latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - #'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - #'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - #'preamble': '', - - # Latex figure (float) alignment - #'figure_align': 'htbp', -} - -# Grouping the document tree into LaTeX files. List of tuples -# (source start file, target name, title, -# author, documentclass [howto, manual, or own class]). -latex_documents = [ - (master_doc, 'certbot-nginx.tex', u'certbot-nginx Documentation', - u'Let\'s Encrypt Project', 'manual'), -] - -# The name of an image file (relative to this directory) to place at the top of -# the title page. -#latex_logo = None - -# For "manual" documents, if this is true, then toplevel headings are parts, -# not chapters. -#latex_use_parts = False - -# If true, show page references after internal links. -#latex_show_pagerefs = False - -# If true, show URL addresses after external links. -#latex_show_urls = False - -# Documents to append as an appendix to all manuals. -#latex_appendices = [] - -# If false, no module index is generated. -#latex_domain_indices = True - - -# -- Options for manual page output --------------------------------------- - -# One entry per manual page. List of tuples -# (source start file, name, description, authors, manual section). -man_pages = [ - (master_doc, 'certbot-nginx', u'certbot-nginx Documentation', - [author], 1) -] - -# If true, show URL addresses after external links. -#man_show_urls = False - - -# -- Options for Texinfo output ------------------------------------------- - -# Grouping the document tree into Texinfo files. List of tuples -# (source start file, target name, title, author, -# dir menu entry, description, category) -texinfo_documents = [ - (master_doc, 'certbot-nginx', u'certbot-nginx Documentation', - author, 'certbot-nginx', 'One line description of project.', - 'Miscellaneous'), -] - -# Documents to append as an appendix to all manuals. -#texinfo_appendices = [] - -# If false, no module index is generated. -#texinfo_domain_indices = True - -# How to display URL addresses: 'footnote', 'no', or 'inline'. -#texinfo_show_urls = 'footnote' - -# If true, do not generate a @detailmenu in the "Top" node's menu. -#texinfo_no_detailmenu = False - - -intersphinx_mapping = { - 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), -} diff --git a/certbot-nginx/docs/index.rst b/certbot-nginx/docs/index.rst deleted file mode 100644 index 488a7ab9cb7..00000000000 --- a/certbot-nginx/docs/index.rst +++ /dev/null @@ -1,31 +0,0 @@ -.. certbot-nginx documentation master file, created by - sphinx-quickstart on Sun Oct 18 13:39:39 2015. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -Welcome to certbot-nginx's documentation! -============================================= - -Contents: - -.. toctree:: - :maxdepth: 2 - - -.. toctree:: - :maxdepth: 1 - - api - - -.. automodule:: certbot_nginx - :members: - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` - diff --git a/certbot-nginx/docs/make.bat b/certbot-nginx/docs/make.bat deleted file mode 100644 index b12255d4ce9..00000000000 --- a/certbot-nginx/docs/make.bat +++ /dev/null @@ -1,263 +0,0 @@ -@ECHO OFF - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set BUILDDIR=_build -set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . -set I18NSPHINXOPTS=%SPHINXOPTS% . -if NOT "%PAPER%" == "" ( - set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% - set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% -) - -if "%1" == "" goto help - -if "%1" == "help" ( - :help - echo.Please use `make ^` where ^ is one of - echo. html to make standalone HTML files - echo. dirhtml to make HTML files named index.html in directories - echo. singlehtml to make a single large HTML file - echo. pickle to make pickle files - echo. json to make JSON files - echo. htmlhelp to make HTML files and a HTML help project - echo. qthelp to make HTML files and a qthelp project - echo. devhelp to make HTML files and a Devhelp project - echo. epub to make an epub - echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter - echo. text to make text files - echo. man to make manual pages - echo. texinfo to make Texinfo files - echo. gettext to make PO message catalogs - echo. changes to make an overview over all changed/added/deprecated items - echo. xml to make Docutils-native XML files - echo. pseudoxml to make pseudoxml-XML files for display purposes - echo. linkcheck to check all external links for integrity - echo. doctest to run all doctests embedded in the documentation if enabled - echo. coverage to run coverage check of the documentation if enabled - goto end -) - -if "%1" == "clean" ( - for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i - del /q /s %BUILDDIR%\* - goto end -) - - -REM Check if sphinx-build is available and fallback to Python version if any -%SPHINXBUILD% 2> nul -if errorlevel 9009 goto sphinx_python -goto sphinx_ok - -:sphinx_python - -set SPHINXBUILD=python -m sphinx.__init__ -%SPHINXBUILD% 2> nul -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ - exit /b 1 -) - -:sphinx_ok - - -if "%1" == "html" ( - %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/html. - goto end -) - -if "%1" == "dirhtml" ( - %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. - goto end -) - -if "%1" == "singlehtml" ( - %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. - goto end -) - -if "%1" == "pickle" ( - %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the pickle files. - goto end -) - -if "%1" == "json" ( - %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the JSON files. - goto end -) - -if "%1" == "htmlhelp" ( - %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run HTML Help Workshop with the ^ -.hhp project file in %BUILDDIR%/htmlhelp. - goto end -) - -if "%1" == "qthelp" ( - %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run "qcollectiongenerator" with the ^ -.qhcp project file in %BUILDDIR%/qthelp, like this: - echo.^> qcollectiongenerator %BUILDDIR%\qthelp\certbot-nginx.qhcp - echo.To view the help file: - echo.^> assistant -collectionFile %BUILDDIR%\qthelp\certbot-nginx.ghc - goto end -) - -if "%1" == "devhelp" ( - %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. - goto end -) - -if "%1" == "epub" ( - %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The epub file is in %BUILDDIR%/epub. - goto end -) - -if "%1" == "latex" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdf" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdfja" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf-ja - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "text" ( - %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The text files are in %BUILDDIR%/text. - goto end -) - -if "%1" == "man" ( - %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The manual pages are in %BUILDDIR%/man. - goto end -) - -if "%1" == "texinfo" ( - %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. - goto end -) - -if "%1" == "gettext" ( - %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The message catalogs are in %BUILDDIR%/locale. - goto end -) - -if "%1" == "changes" ( - %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes - if errorlevel 1 exit /b 1 - echo. - echo.The overview file is in %BUILDDIR%/changes. - goto end -) - -if "%1" == "linkcheck" ( - %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck - if errorlevel 1 exit /b 1 - echo. - echo.Link check complete; look for any errors in the above output ^ -or in %BUILDDIR%/linkcheck/output.txt. - goto end -) - -if "%1" == "doctest" ( - %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest - if errorlevel 1 exit /b 1 - echo. - echo.Testing of doctests in the sources finished, look at the ^ -results in %BUILDDIR%/doctest/output.txt. - goto end -) - -if "%1" == "coverage" ( - %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage - if errorlevel 1 exit /b 1 - echo. - echo.Testing of coverage in the sources finished, look at the ^ -results in %BUILDDIR%/coverage/python.txt. - goto end -) - -if "%1" == "xml" ( - %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The XML files are in %BUILDDIR%/xml. - goto end -) - -if "%1" == "pseudoxml" ( - %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. - goto end -) - -:end diff --git a/certbot-nginx/local-oldest-requirements.txt b/certbot-nginx/local-oldest-requirements.txt deleted file mode 100644 index bcd02d19712..00000000000 --- a/certbot-nginx/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -acme[dev]==0.26.0 -certbot[dev]==0.22.0 diff --git a/certbot-nginx/pyproject.toml b/certbot-nginx/pyproject.toml new file mode 100644 index 00000000000..917891019d2 --- /dev/null +++ b/certbot-nginx/pyproject.toml @@ -0,0 +1,50 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot-nginx" +dynamic = ["version", "dependencies"] +description = "Nginx plugin for Certbot" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Plugins", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.optional-dependencies] +test = [ + "pytest", +] + +[project.entry-points."certbot.plugins"] +nginx = "certbot_nginx._internal.entrypoint:ENTRYPOINT" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/certbot-nginx/readthedocs.org.requirements.txt b/certbot-nginx/readthedocs.org.requirements.txt deleted file mode 100644 index ca5f333633c..00000000000 --- a/certbot-nginx/readthedocs.org.requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e . --e certbot-nginx[docs] diff --git a/certbot-nginx/setup.cfg b/certbot-nginx/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-nginx/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-nginx/setup.py b/certbot-nginx/setup.py index 70e11f62ba1..876a3919789 100644 --- a/certbot-nginx/setup.py +++ b/certbot-nginx/setup.py @@ -1,67 +1,15 @@ from setuptools import setup -from setuptools import find_packages +version = '5.8.0.dev0' -version = '0.31.0.dev0' - -# Remember to update local-oldest-requirements.txt when changing the minimum -# acme/certbot version. install_requires = [ - 'acme>=0.26.0', - 'certbot>=0.22.0', - 'mock', - 'PyOpenSSL', - 'pyparsing>=1.5.5', # Python3 support; perhaps unnecessary? - 'setuptools', - 'zope.interface', -] - -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', + # We specify the minimum certbot version as the current plugin + # version for simplicity. See + # https://github.com/certbot/certbot/issues/8761 for more info. + f'certbot[nginx]>={version}', ] setup( - name='certbot-nginx', version=version, - description="Nginx plugin for Certbot", - url='https://github.com/letsencrypt/letsencrypt', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 5 - Production/Stable', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'nginx = certbot_nginx.configurator:NginxConfigurator', - ], - }, - test_suite='certbot_nginx', ) diff --git a/certbot-nginx/certbot_nginx/__init__.py b/certbot-nginx/src/certbot_nginx/__init__.py similarity index 100% rename from certbot-nginx/certbot_nginx/__init__.py rename to certbot-nginx/src/certbot_nginx/__init__.py diff --git a/certbot-nginx/src/certbot_nginx/_internal/entrypoint.py b/certbot-nginx/src/certbot_nginx/_internal/entrypoint.py new file mode 100644 index 00000000000..5ef2508e5ca --- /dev/null +++ b/certbot-nginx/src/certbot_nginx/_internal/entrypoint.py @@ -0,0 +1,6 @@ +""" Entry point for Nginx Plugin """ + +import certbot.plugins.nginx + + +ENTRYPOINT = certbot.plugins.nginx.ENTRYPOINT diff --git a/certbot-nginx/src/certbot_nginx/py.typed b/certbot-nginx/src/certbot_nginx/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-nginx/tests/boulder-integration.conf.sh b/certbot-nginx/tests/boulder-integration.conf.sh deleted file mode 100755 index 470eab28e26..00000000000 --- a/certbot-nginx/tests/boulder-integration.conf.sh +++ /dev/null @@ -1,98 +0,0 @@ -#!/usr/bin/env bash -# Based on -# https://www.exratione.com/2014/03/running-nginx-as-a-non-root-user/ -# https://github.com/exratione/non-root-nginx/blob/9a77f62e5d5cb9c9026fd62eece76b9514011019/nginx.conf - -cat < $nginx_conf - - killall nginx || true - nginx -c $nginx_root/nginx.conf -} - -certbot_test_nginx () { - certbot_test \ - --authenticator nginx \ - --installer nginx \ - --nginx-server-root $nginx_root \ - "$@" -} - -test_deployment_and_rollback() { - # Arguments: certname - echo | openssl s_client -connect localhost:5001 \ - | openssl x509 -out $root/nginx.pem - diff -q $root/nginx.pem "$root/conf/live/$1/cert.pem" - - certbot_test_nginx rollback --checkpoints 9001 - diff -q <(echo "$original") $nginx_conf -} - -export default_server="default_server" -nginx -v -reload_nginx -certbot_test_nginx --domains nginx.wtf run -test_deployment_and_rollback nginx.wtf -certbot_test_nginx --domains nginx-tls.wtf run --preferred-challenges tls-sni -test_deployment_and_rollback nginx-tls.wtf -certbot_test_nginx --domains nginx2.wtf --preferred-challenges http -test_deployment_and_rollback nginx2.wtf -# Overlapping location block and server-block-level return 301 -certbot_test_nginx --domains nginx3.wtf --preferred-challenges http -test_deployment_and_rollback nginx3.wtf -# No matching server block; default_server exists -certbot_test_nginx --domains nginx4.wtf --preferred-challenges http -test_deployment_and_rollback nginx4.wtf -# No matching server block; default_server does not exist -export default_server="" -reload_nginx -if nginx -c $nginx_root/nginx.conf -T 2>/dev/null | grep "default_server"; then - echo "Failed to remove default_server" - exit 1 -fi -certbot_test_nginx --domains nginx5.wtf --preferred-challenges http -test_deployment_and_rollback nginx5.wtf -# Mutiple domains, mix of matching and not -certbot_test_nginx --domains nginx6.wtf,nginx7.wtf --preferred-challenges http -test_deployment_and_rollback nginx6.wtf - -# note: not reached if anything above fails, hence "killall" at the -# top -nginx -c $nginx_root/nginx.conf -s stop - -coverage report --fail-under 75 --include 'certbot-nginx/*' --show-missing diff --git a/certbot-postfix/LICENSE.txt b/certbot-postfix/LICENSE.txt deleted file mode 100644 index c8314fd1ce4..00000000000 --- a/certbot-postfix/LICENSE.txt +++ /dev/null @@ -1,190 +0,0 @@ - Copyright 2017 Electronic Frontier Foundation and others - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS diff --git a/certbot-postfix/MANIFEST.in b/certbot-postfix/MANIFEST.in deleted file mode 100644 index 2733814033d..00000000000 --- a/certbot-postfix/MANIFEST.in +++ /dev/null @@ -1,4 +0,0 @@ -include LICENSE.txt -include README.rst -recursive-include certbot_postfix/testdata * -recursive-include certbot_postfix/docs * diff --git a/certbot-postfix/README.rst b/certbot-postfix/README.rst deleted file mode 100644 index 1ae9cb98020..00000000000 --- a/certbot-postfix/README.rst +++ /dev/null @@ -1,23 +0,0 @@ -========================== -Postfix plugin for Certbot -========================== - -Note: this MTA installer is in **developer beta**-- we appreciate any testing, feedback, or -feature requests for this plugin. - -To install this plugin, in the root of this repo, run:: - - python tools/venv.py - source venv/bin/activate - -You can use this installer with any `authenticator plugin -`_. -For instance, with the `standalone authenticator -`_, which requires no extra server -software, you might run:: - - sudo ./venv/bin/certbot run --standalone -i postfix -d - -To just install existing certs with this plugin, run:: - - sudo ./venv/bin/certbot install -i postfix --cert-path --key-path -d diff --git a/certbot-postfix/certbot_postfix/__init__.py b/certbot-postfix/certbot_postfix/__init__.py deleted file mode 100644 index 122c54bc6e8..00000000000 --- a/certbot-postfix/certbot_postfix/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -"""Certbot Postfix plugin.""" - -from certbot_postfix.installer import Installer diff --git a/certbot-postfix/certbot_postfix/constants.py b/certbot-postfix/certbot_postfix/constants.py deleted file mode 100644 index 40a263a5368..00000000000 --- a/certbot-postfix/certbot_postfix/constants.py +++ /dev/null @@ -1,63 +0,0 @@ -"""Postfix plugin constants.""" - -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import Dict, Tuple, Union -# pylint: enable=unused-import, no-name-in-module - -MINIMUM_VERSION = (2, 11,) - -# If the value of a default VAR is a tuple, then the values which -# come LATER in the tuple are more strict/more secure. -# Certbot will default to the first value in the tuple, but will -# not override "more secure" settings. - -ACCEPTABLE_SERVER_SECURITY_LEVELS = ("may", "encrypt") -ACCEPTABLE_CLIENT_SECURITY_LEVELS = ("may", "encrypt", - "dane", "dane-only", - "fingerprint", - "verify", "secure") -ACCEPTABLE_CIPHER_LEVELS = ("medium", "high") - -# Exporting certain ciphers to prevent logjam: https://weakdh.org/sysadmin.html -EXCLUDE_CIPHERS = ("aNULL, eNULL, EXPORT, DES, RC4, MD5, PSK, aECDH, " - "EDH-DSS-DES-CBC3-SHA, EDH-RSA-DES-CBC3-SHA, KRB5-DES, CBC3-SHA",) - - -TLS_VERSIONS = ("SSLv2", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2") -# Should NOT use SSLv2/3. -ACCEPTABLE_TLS_VERSIONS = ("TLSv1", "TLSv1.1", "TLSv1.2") - -# Variables associated with enabling opportunistic TLS. -TLS_SERVER_VARS = { - "smtpd_tls_security_level": ACCEPTABLE_SERVER_SECURITY_LEVELS, -} # type:Dict[str, Tuple[str, ...]] -TLS_CLIENT_VARS = { - "smtp_tls_security_level": ACCEPTABLE_CLIENT_SECURITY_LEVELS, -} # type:Dict[str, Tuple[str, ...]] -# Default variables for a secure MTA server [receiver]. -DEFAULT_SERVER_VARS = { - "smtpd_tls_auth_only": ("yes",), - "smtpd_tls_mandatory_protocols": ("!SSLv2, !SSLv3",), - "smtpd_tls_protocols": ("!SSLv2, !SSLv3",), - "smtpd_tls_ciphers": ACCEPTABLE_CIPHER_LEVELS, - "smtpd_tls_mandatory_ciphers": ACCEPTABLE_CIPHER_LEVELS, - "smtpd_tls_exclude_ciphers": EXCLUDE_CIPHERS, - "smtpd_tls_eecdh_grade": ("strong",), -} # type:Dict[str, Tuple[str, ...]] - -# Default variables for a secure MTA client [sender]. -DEFAULT_CLIENT_VARS = { - "smtp_tls_ciphers": ACCEPTABLE_CIPHER_LEVELS, - "smtp_tls_exclude_ciphers": EXCLUDE_CIPHERS, - "smtp_tls_mandatory_ciphers": ACCEPTABLE_CIPHER_LEVELS, -} # type:Dict[str, Tuple[str, ...]] - -CLI_DEFAULTS = dict( - config_dir="/etc/postfix", - ctl="postfix", - config_utility="postconf", - tls_only=False, - ignore_master_overrides=False, - server_only=False, -) -"""CLI defaults.""" diff --git a/certbot-postfix/certbot_postfix/installer.py b/certbot-postfix/certbot_postfix/installer.py deleted file mode 100644 index 9ba92ef8fdf..00000000000 --- a/certbot-postfix/certbot_postfix/installer.py +++ /dev/null @@ -1,288 +0,0 @@ -"""certbot installer plugin for postfix.""" -import logging -import os - -import zope.interface -import zope.component -import six - -from certbot import errors -from certbot import interfaces -from certbot import util as certbot_util -from certbot.plugins import common as plugins_common - -from certbot_postfix import constants -from certbot_postfix import postconf -from certbot_postfix import util - -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import Callable, Dict, List -# pylint: enable=unused-import, no-name-in-module - -logger = logging.getLogger(__name__) - -@zope.interface.implementer(interfaces.IInstaller) -@zope.interface.provider(interfaces.IPluginFactory) -class Installer(plugins_common.Installer): - """Certbot installer plugin for Postfix. - - :ivar str config_dir: Postfix configuration directory to modify - :ivar list save_notes: documentation for proposed changes. This is - cleared and stored in Certbot checkpoints when save() is called - - :ivar postconf: Wrapper for Postfix configuration command-line tool. - :type postconf: :class: `certbot_postfix.postconf.ConfigMain` - :ivar postfix: Wrapper for Postfix command-line tool. - :type postfix: :class: `certbot_postfix.util.PostfixUtil` - """ - - description = "Configure TLS with the Postfix MTA" - - @classmethod - def add_parser_arguments(cls, add): - add("ctl", default=constants.CLI_DEFAULTS["ctl"], - help="Path to the 'postfix' control program.") - # This directory points to Postfix's configuration directory. - add("config-dir", default=constants.CLI_DEFAULTS["config_dir"], - help="Path to the directory containing the " - "Postfix main.cf file to modify instead of using the " - "default configuration paths.") - add("config-utility", default=constants.CLI_DEFAULTS["config_utility"], - help="Path to the 'postconf' executable.") - add("tls-only", action="store_true", default=constants.CLI_DEFAULTS["tls_only"], - help="Only set params to enable opportunistic TLS and install certificates.") - add("server-only", action="store_true", default=constants.CLI_DEFAULTS["server_only"], - help="Only set server params (prefixed with smtpd*)") - add("ignore-master-overrides", action="store_true", - default=constants.CLI_DEFAULTS["ignore_master_overrides"], - help="Ignore errors reporting overridden TLS parameters in master.cf.") - - def __init__(self, *args, **kwargs): - super(Installer, self).__init__(*args, **kwargs) - # Wrapper around postconf commands - self.postfix = None - self.postconf = None - - # Files to save - self.save_notes = [] # type: List[str] - - self._enhance_func = {} # type: Dict[str, Callable[[str, str], None]] - # Since we only need to enable TLS once for all domains, - # keep track of whether this enhancement was already called. - self._tls_enabled = False - - def prepare(self): - """Prepare the installer. - - :raises errors.PluginError: when an unexpected error occurs - :raises errors.MisconfigurationError: when the config is invalid - :raises errors.NoInstallationError: when can't find installation - :raises errors.NotSupportedError: when version is not supported - """ - # Verify postfix and postconf are installed - for param in ("ctl", "config_utility",): - util.verify_exe_exists(self.conf(param), - "Cannot find executable '{0}'. You can provide the " - "path to this command with --{1}".format( - self.conf(param), - self.option_name(param))) - - # Set up CLI tools - self.postfix = util.PostfixUtil(self.conf('config-dir')) - self.postconf = postconf.ConfigMain(self.conf('config-utility'), - self.conf('ignore-master-overrides'), - self.conf('config-dir')) - - # Ensure current configuration is valid. - self.config_test() - - # Check Postfix version - self._check_version() - self._lock_config_dir() - self.install_ssl_dhparams() - - def config_test(self): - """Test to see that the current Postfix configuration is valid. - - :raises errors.MisconfigurationError: If the configuration is invalid. - """ - self.postfix.test() - - def _check_version(self): - """Verifies that the installed Postfix version is supported. - - :raises errors.NotSupportedError: if the version is unsupported - """ - if self._get_version() < constants.MINIMUM_VERSION: - version_string = '.'.join([str(n) for n in constants.MINIMUM_VERSION]) - raise errors.NotSupportedError('Postfix version must be at least %s' % version_string) - - def _lock_config_dir(self): - """Stop two Postfix plugins from modifying the config at once. - - :raises .PluginError: if unable to acquire the lock - """ - try: - certbot_util.lock_dir_until_exit(self.conf('config-dir')) - except (OSError, errors.LockError): - logger.debug("Encountered error:", exc_info=True) - raise errors.PluginError( - "Unable to lock %s" % self.conf('config-dir')) - - def more_info(self): - """Human-readable string to help the user. Describes steps taken and any relevant - info to help the user decide which plugin to use. - - :rtype: str - """ - return ( - "Configures Postfix to try to authenticate mail servers, use " - "installed certificates and disable weak ciphers and protocols.{0}" - "Server root: {root}{0}" - "Version: {version}".format( - os.linesep, - root=self.conf('config-dir'), - version='.'.join([str(i) for i in self._get_version()])) - ) - - def _get_version(self): - """Return the version of Postfix, as a tuple. (e.g. '2.11.3' is (2, 11, 3)) - - :returns: version - :rtype: tuple - - :raises errors.PluginError: Unable to find Postfix version. - """ - mail_version = self.postconf.get_default("mail_version") - return tuple(int(i) for i in mail_version.split('.')) - - def get_all_names(self): - """Returns all names that may be authenticated. - - :rtype: `set` of `str` - - """ - return certbot_util.get_filtered_names(self.postconf.get(var) - for var in ('mydomain', 'myhostname', 'myorigin',)) - - def _set_vars(self, var_dict): - """Sets all parameters in var_dict to config file. If current value is already set - as more secure (acceptable), then don't set/overwrite it. - """ - for param, acceptable in six.iteritems(var_dict): - if not util.is_acceptable_value(param, self.postconf.get(param), acceptable): - self.postconf.set(param, acceptable[0], acceptable) - - def _confirm_changes(self): - """Confirming outstanding updates for configuration parameters. - - :raises errors.PluginError: when user rejects the configuration changes. - """ - updates = self.postconf.get_changes() - output_string = "Postfix TLS configuration parameters to update in main.cf:\n" - for name, value in six.iteritems(updates): - output_string += "{0} = {1}\n".format(name, value) - output_string += "Is this okay?\n" - if not zope.component.getUtility(interfaces.IDisplay).yesno(output_string, - force_interactive=True, default=True): - raise errors.PluginError( - "Manually rejected configuration changes.\n" - "Try using --tls-only or --server-only to change a particular" - "subset of configuration parameters.") - - def deploy_cert(self, domain, cert_path, - key_path, chain_path, fullchain_path): - """Configure the Postfix SMTP server to use the given TLS cert. - - :param str domain: domain to deploy certificate file - :param str cert_path: absolute path to the certificate file - :param str key_path: absolute path to the private key file - :param str chain_path: absolute path to the certificate chain file - :param str fullchain_path: absolute path to the certificate fullchain - file (cert plus chain) - - :raises .PluginError: when cert cannot be deployed - - """ - # pylint: disable=unused-argument - if self._tls_enabled: - return - self._tls_enabled = True - self.save_notes.append("Configuring TLS for {0}".format(domain)) - self.postconf.set("smtpd_tls_cert_file", cert_path) - self.postconf.set("smtpd_tls_key_file", key_path) - self._set_vars(constants.TLS_SERVER_VARS) - if not self.conf('server_only'): - self._set_vars(constants.TLS_CLIENT_VARS) - if not self.conf('tls_only'): - self._set_vars(constants.DEFAULT_SERVER_VARS) - if not self.conf('server_only'): - self._set_vars(constants.DEFAULT_CLIENT_VARS) - # Despite the name, this option also supports 2048-bit DH params. - # http://www.postfix.org/FORWARD_SECRECY_README.html#server_fs - self.postconf.set("smtpd_tls_dh1024_param_file", self.ssl_dhparams) - self._confirm_changes() - - def enhance(self, domain, enhancement, options=None): - """Raises an exception since this installer doesn't support any enhancements. - """ - # pylint: disable=unused-argument - raise errors.PluginError( - "Unsupported enhancement: {0}".format(enhancement)) - - def supported_enhancements(self): - """Returns a list of supported enhancements. - - :rtype: list - - """ - return [] - - def save(self, title=None, temporary=False): - """Creates backups and writes changes to configuration files. - - :param str title: The title of the save. If a title is given, the - configuration will be saved as a new checkpoint and put in a - timestamped directory. `title` has no effect if temporary is true. - - :param bool temporary: Indicates whether the changes made will - be quickly reversed in the future (challenges) - - :raises errors.PluginError: when save is unsuccessful - """ - save_files = set((os.path.join(self.conf('config-dir'), "main.cf"),)) - self.add_to_checkpoint(save_files, - "\n".join(self.save_notes), temporary) - self.postconf.flush() - - del self.save_notes[:] - - if title and not temporary: - self.finalize_checkpoint(title) - - def recovery_routine(self): - super(Installer, self).recovery_routine() - self.postconf = postconf.ConfigMain(self.conf('config-utility'), - self.conf('ignore-master-overrides'), - self.conf('config-dir')) - - def rollback_checkpoints(self, rollback=1): - """Rollback saved checkpoints. - - :param int rollback: Number of checkpoints to revert - - :raises .errors.PluginError: If there is a problem with the input or - the function is unable to correctly revert the configuration - """ - super(Installer, self).rollback_checkpoints(rollback) - self.postconf = postconf.ConfigMain(self.conf('config-utility'), - self.conf('ignore-master-overrides'), - self.conf('config-dir')) - - def restart(self): - """Restart or refresh the server content. - - :raises .PluginError: when server cannot be restarted - """ - self.postfix.restart() - diff --git a/certbot-postfix/certbot_postfix/postconf.py b/certbot-postfix/certbot_postfix/postconf.py deleted file mode 100644 index 466e0e63e0c..00000000000 --- a/certbot-postfix/certbot_postfix/postconf.py +++ /dev/null @@ -1,152 +0,0 @@ -"""Classes that wrap the postconf command line utility. -""" -import six -from certbot import errors -from certbot_postfix import util - -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import Dict, List, Tuple -# pylint: enable=unused-import, no-name-in-module - -class ConfigMain(util.PostfixUtilBase): - """A parser for Postfix's main.cf file.""" - - def __init__(self, executable, ignore_master_overrides=False, config_dir=None): - super(ConfigMain, self).__init__(executable, config_dir) - # Whether to ignore overrides from master. - self._ignore_master_overrides = ignore_master_overrides - # List of all current Postfix parameters, from `postconf` command. - self._db = {} # type: Dict[str, str] - # List of current master.cf overrides from Postfix config. Dictionary - # of parameter name => list of tuples (service name, paramter value) - # Note: We should never modify master without explicit permission. - self._master_db = {} # type: Dict[str, List[Tuple[str, str]]] - # List of all changes requested to the Postfix parameters as they are now - # in _db. These changes are flushed to `postconf` on `flush`. - self._updated = {} # type: Dict[str, str] - self._read_from_conf() - - def _read_from_conf(self): - """Reads initial parameter state from `main.cf` into this object. - """ - out = self._get_output() - for name, value in _parse_main_output(out): - self._db[name] = value - out = self._get_output_master() - for name, value in _parse_main_output(out): - service, param_name = name.rsplit("/", 1) - if param_name not in self._master_db: - self._master_db[param_name] = [] - self._master_db[param_name].append((service, value)) - - def _get_output_master(self): - """Retrieves output for `master.cf` parameters.""" - return self._get_output('-P') - - def get_default(self, name): - """Retrieves default value of parameter `name` from postfix parameters. - - :param str name: The name of the parameter to fetch. - :returns: The default value of parameter `name`. - :rtype: str - """ - out = self._get_output(['-d', name]) - _, value = next(_parse_main_output(out), (None, None)) - return value - - def get(self, name): - """Retrieves working value of parameter `name` from postfix parameters. - - :param str name: The name of the parameter to fetch. - :returns: The value of parameter `name`. - :rtype: str - """ - if name in self._updated: - return self._updated[name] - return self._db[name] - - def get_master_overrides(self, name): - """Retrieves list of overrides for parameter `name` in postfix's Master config - file. - - :returns: List of tuples (service, value), meaning that parameter `name` - is overridden as `value` for `service`. - :rtype: `list` of `tuple` of `str` - """ - if name in self._master_db: - return self._master_db[name] - return None - - def set(self, name, value, acceptable_overrides=None): - """Sets parameter `name` to `value`. If `name` is overridden by a particular service in - `master.cf`, reports any of these parameter conflicts as long as - `ignore_master_overrides` was not set. - - .. note:: that this function does not flush these parameter values to main.cf; - To do that, use `flush`. - - :param str name: The name of the parameter to set. - :param str value: The value of the parameter. - :param tuple acceptable_overrides: If the master configuration file overrides `value` - with a value in acceptable_overrides. - """ - if name not in self._db: - raise KeyError("Parameter name %s is not a valid Postfix parameter name.", name) - # Check to see if this parameter is overridden by master. - overrides = self.get_master_overrides(name) - if not self._ignore_master_overrides and overrides is not None: - util.report_master_overrides(name, overrides, acceptable_overrides) - if value != self._db[name]: - # _db contains the "original" state of parameters. We only care about - # writes if they cause a delta from the original state. - self._updated[name] = value - elif name in self._updated: - # If this write reverts a previously updated parameter back to the - # original DB's state, we don't have to keep track of it in _updated. - del self._updated[name] - - def flush(self): - """Flushes all parameter changes made using `self.set`, to `main.cf` - - :raises error.PluginError: When flush to main.cf fails for some reason. - """ - if len(self._updated) == 0: - return - args = ['-e'] - for name, value in six.iteritems(self._updated): - args.append('{0}={1}'.format(name, value)) - try: - self._get_output(args) - except IOError as e: - raise errors.PluginError("Unable to save to Postfix config: %v", e) - for name, value in six.iteritems(self._updated): - self._db[name] = value - self._updated = {} - - def get_changes(self): - """ Return queued changes to main.cf. - - :rtype: dict[str, str] - """ - return self._updated - -def _parse_main_output(output): - """Parses the raw output from Postconf about main.cf. - - Expects the output to look like: - - .. code-block:: none - - name1 = value1 - name2 = value2 - - :param str output: data postconf wrote to stdout about main.cf - - :returns: generator providing key-value pairs from main.cf - :rtype: Iterator[tuple(str, str)] - """ - for line in output.splitlines(): - name, _, value = line.partition(" =") - yield name, value.strip() - - diff --git a/certbot-postfix/certbot_postfix/tests/__init__.py b/certbot-postfix/certbot_postfix/tests/__init__.py deleted file mode 100644 index 7316b588894..00000000000 --- a/certbot-postfix/certbot_postfix/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ -""" Certbot Postfix Tests """ diff --git a/certbot-postfix/certbot_postfix/tests/installer_test.py b/certbot-postfix/certbot_postfix/tests/installer_test.py deleted file mode 100644 index 37b78bdca2d..00000000000 --- a/certbot-postfix/certbot_postfix/tests/installer_test.py +++ /dev/null @@ -1,314 +0,0 @@ -"""Tests for certbot_postfix.installer.""" -from contextlib import contextmanager -import copy -import functools -import os -import pkg_resources -import six -import unittest - -import mock - -from certbot import errors -from certbot.tests import util as certbot_test_util - -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import Dict, Tuple, Union -# pylint: enable=unused-import, no-name-in-module - -DEFAULT_MAIN_CF = { - "smtpd_tls_cert_file": "", - "smtpd_tls_key_file": "", - "smtpd_tls_dh1024_param_file": "", - "smtpd_tls_security_level": "none", - "smtpd_tls_auth_only": "", - "smtpd_tls_mandatory_protocols": "", - "smtpd_tls_protocols": "", - "smtpd_tls_ciphers": "", - "smtpd_tls_exclude_ciphers": "", - "smtpd_tls_mandatory_ciphers": "", - "smtpd_tls_eecdh_grade": "medium", - "smtp_tls_security_level": "", - "smtp_tls_ciphers": "", - "smtp_tls_exclude_ciphers": "", - "smtp_tls_mandatory_ciphers": "", - "mail_version": "3.2.3" -} - -def _main_cf_with(obj): - main_cf = copy.copy(DEFAULT_MAIN_CF) - main_cf.update(obj) - return main_cf - -class InstallerTest(certbot_test_util.ConfigTestCase): - # pylint: disable=too-many-public-methods - - def setUp(self): - super(InstallerTest, self).setUp() - _config_file = pkg_resources.resource_filename("certbot_postfix.tests", - os.path.join("testdata", "config.json")) - self.config.postfix_ctl = "postfix" - self.config.postfix_config_dir = self.tempdir - self.config.postfix_config_utility = "postconf" - self.config.postfix_tls_only = False - self.config.postfix_server_only = False - self.config.config_dir = self.tempdir - - @mock.patch("certbot_postfix.installer.util.is_acceptable_value") - def test_set_vars(self, mock_is_acceptable_value): - mock_is_acceptable_value.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - mock_is_acceptable_value.return_value = False - - @mock.patch("certbot_postfix.installer.util.is_acceptable_value") - def test_acceptable_value(self, mock_is_acceptable_value): - mock_is_acceptable_value.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - mock_is_acceptable_value.return_value = False - - @certbot_test_util.patch_get_utility() - def test_confirm_changes_no_raises_error(self, mock_util): - mock_util().yesno.return_value = False - with create_installer(self.config) as installer: - installer.prepare() - self.assertRaises(errors.PluginError, installer.deploy_cert, - "example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - - @certbot_test_util.patch_get_utility() - def test_save(self, mock_util): - mock_util().yesno.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - installer.postconf.flush = mock.Mock() - installer.reverter = mock.Mock() - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - installer.save() - self.assertEqual(installer.save_notes, []) - self.assertEqual(installer.postconf.flush.call_count, 1) - self.assertEqual(installer.reverter.add_to_checkpoint.call_count, 1) - - @certbot_test_util.patch_get_utility() - def test_save_with_title(self, mock_util): - mock_util().yesno.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - installer.postconf.flush = mock.Mock() - installer.reverter = mock.Mock() - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - installer.save(title="new_file!") - self.assertEqual(installer.reverter.finalize_checkpoint.call_count, 1) - - @certbot_test_util.patch_get_utility() - def test_rollback_checkpoints_resets_postconf(self, mock_util): - mock_util().yesno.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - installer.rollback_checkpoints() - self.assertEqual(installer.postconf.get_changes(), {}) - - @certbot_test_util.patch_get_utility() - def test_recovery_routine_resets_postconf(self, mock_util): - mock_util().yesno.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - installer.recovery_routine() - self.assertEqual(installer.postconf.get_changes(), {}) - - def test_restart(self): - with create_installer(self.config) as installer: - installer.prepare() - installer.restart() - self.assertEqual(installer.postfix.restart.call_count, 1) - - def test_add_parser_arguments(self): - options = set(("ctl", "config-dir", "config-utility", - "tls-only", "server-only", "ignore-master-overrides")) - mock_add = mock.MagicMock() - - from certbot_postfix import installer - installer.Installer.add_parser_arguments(mock_add) - - for call in mock_add.call_args_list: - self.assertTrue(call[0][0] in options) - - def test_no_postconf_prepare(self): - with create_installer(self.config) as installer: - installer_path = "certbot_postfix.installer" - exe_exists_path = installer_path + ".certbot_util.exe_exists" - path_surgery_path = "certbot_postfix.util.plugins_util.path_surgery" - with mock.patch(path_surgery_path, return_value=False): - with mock.patch(exe_exists_path, return_value=False): - self.assertRaises(errors.NoInstallationError, - installer.prepare) - - def test_old_version(self): - with create_installer(self.config, main_cf=_main_cf_with({"mail_version": "0.0.1"}))\ - as installer: - self.assertRaises(errors.NotSupportedError, installer.prepare) - - def test_lock_error(self): - with create_installer(self.config) as installer: - assert_raises = functools.partial(self.assertRaises, - errors.PluginError, - installer.prepare) - certbot_test_util.lock_and_call(assert_raises, self.tempdir) - - - @mock.patch('certbot.util.lock_dir_until_exit') - def test_dir_locked(self, lock_dir): - with create_installer(self.config) as installer: - lock_dir.side_effect = errors.LockError - self.assertRaises(errors.PluginError, installer.prepare) - - def test_more_info(self): - with create_installer(self.config) as installer: - installer.prepare() - output = installer.more_info() - self.assertTrue("Postfix" in output) - self.assertTrue(self.tempdir in output) - self.assertTrue(DEFAULT_MAIN_CF["mail_version"] in output) - - def test_get_all_names(self): - config = {"mydomain": "example.org", - "myhostname": "mail.example.org", - "myorigin": "example.org"} - with create_installer(self.config, main_cf=_main_cf_with(config)) as installer: - installer.prepare() - result = installer.get_all_names() - self.assertEqual(result, set(config.values())) - - @certbot_test_util.patch_get_utility() - def test_deploy(self, mock_util): - mock_util().yesno.return_value = True - from certbot_postfix import constants - with create_installer(self.config) as installer: - installer.prepare() - - # pylint: disable=protected-access - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - changes = installer.postconf.get_changes() - expected = {} # type: Dict[str, Tuple[str, ...]] - expected.update(constants.TLS_SERVER_VARS) - expected.update(constants.DEFAULT_SERVER_VARS) - expected.update(constants.DEFAULT_CLIENT_VARS) - self.assertEqual(changes["smtpd_tls_key_file"], "key_path") - self.assertEqual(changes["smtpd_tls_cert_file"], "cert_path") - for name, value in six.iteritems(expected): - self.assertEqual(changes[name], value[0]) - - @certbot_test_util.patch_get_utility() - def test_tls_only(self, mock_util): - mock_util().yesno.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - installer.conf = lambda x: x == "tls_only" - installer.postconf.set = mock.Mock() - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - self.assertEqual(installer.postconf.set.call_count, 4) - - @certbot_test_util.patch_get_utility() - def test_server_only(self, mock_util): - mock_util().yesno.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - installer.conf = lambda x: x == "server_only" - installer.postconf.set = mock.Mock() - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - self.assertEqual(installer.postconf.set.call_count, 11) - - @certbot_test_util.patch_get_utility() - def test_tls_and_server_only(self, mock_util): - mock_util().yesno.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - installer.conf = lambda x: True - installer.postconf.set = mock.Mock() - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - self.assertEqual(installer.postconf.set.call_count, 3) - - @certbot_test_util.patch_get_utility() - def test_deploy_twice(self, mock_util): - # Deploying twice on the same installer shouldn't do anything! - mock_util().yesno.return_value = True - with create_installer(self.config) as installer: - installer.prepare() - from certbot_postfix.postconf import ConfigMain - with mock.patch.object(ConfigMain, "set", wraps=installer.postconf.set) as fake_set: - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - self.assertEqual(fake_set.call_count, 15) - fake_set.reset_mock() - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - self.assertFalse(fake_set.called) - - @certbot_test_util.patch_get_utility() - def test_deploy_already_secure(self, mock_util): - # Should not overwrite "more-secure" parameters - mock_util().yesno.return_value = True - more_secure = { - "smtpd_tls_security_level": "encrypt", - "smtpd_tls_protocols": "!SSLv3, !SSLv2, !TLSv1", - "smtpd_tls_eecdh_grade": "strong" - } - with create_installer(self.config,\ - main_cf=_main_cf_with(more_secure)) as installer: - installer.prepare() - installer.deploy_cert("example.com", "cert_path", "key_path", - "chain_path", "fullchain_path") - for param in more_secure.keys(): - self.assertFalse(param in installer.postconf.get_changes()) - - def test_enhance(self): - with create_installer(self.config) as installer: - installer.prepare() - self.assertRaises(errors.PluginError, - installer.enhance, - "example.org", "redirect") - - def test_supported_enhancements(self): - with create_installer(self.config) as installer: - installer.prepare() - self.assertEqual(installer.supported_enhancements(), []) - -@contextmanager -def create_installer(config, main_cf=DEFAULT_MAIN_CF): -# pylint: disable=dangerous-default-value - """Creates a Postfix installer with calls to `postconf` and `postfix` mocked out. - - In particular, creates a ConfigMain object that does regular things, but seeds it - with values from `main_cf` and `master_cf` dicts. - """ - from certbot_postfix.postconf import ConfigMain - from certbot_postfix import installer - def _mock_init_postconf(postconf, executable, ignore_master_overrides=False, config_dir=None): - # pylint: disable=protected-access,unused-argument - postconf._ignore_master_overrides = ignore_master_overrides - postconf._db = main_cf - postconf._master_db = {} - postconf._updated = {} - # override get_default to get from main - postconf.get_default = lambda name: main_cf[name] - with mock.patch.object(ConfigMain, "__init__", _mock_init_postconf): - exe_exists_path = "certbot_postfix.installer.certbot_util.exe_exists" - with mock.patch(exe_exists_path, return_value=True): - with mock.patch("certbot_postfix.installer.util.PostfixUtil", - return_value=mock.Mock()): - yield installer.Installer(config, "postfix") - -if __name__ == "__main__": - unittest.main() # pragma: no cover - diff --git a/certbot-postfix/certbot_postfix/tests/postconf_test.py b/certbot-postfix/certbot_postfix/tests/postconf_test.py deleted file mode 100644 index 01a43773d88..00000000000 --- a/certbot-postfix/certbot_postfix/tests/postconf_test.py +++ /dev/null @@ -1,107 +0,0 @@ -"""Tests for certbot_postfix.postconf.""" - -import mock -import unittest - -from certbot import errors - -class PostConfTest(unittest.TestCase): - """Tests for certbot_postfix.util.PostConf.""" - def setUp(self): - from certbot_postfix.postconf import ConfigMain - super(PostConfTest, self).setUp() - with mock.patch('certbot_postfix.util.PostfixUtilBase._get_output') as mock_call: - with mock.patch('certbot_postfix.postconf.ConfigMain._get_output_master') as \ - mock_master_call: - with mock.patch('certbot_postfix.postconf.util.verify_exe_exists') as verify_exe: - verify_exe.return_value = True - mock_call.return_value = ('default_parameter = value\n' - 'extra_param =\n' - 'overridden_by_master = default\n') - mock_master_call.return_value = ( - 'service/type/overridden_by_master = master_value\n' - 'service2/type/overridden_by_master = master_value2\n' - ) - self.config = ConfigMain('postconf', False) - - @mock.patch('certbot_postfix.util.PostfixUtilBase._get_output') - @mock.patch('certbot_postfix.postconf.util.verify_exe_exists') - def test_get_output_master(self, mock_verify_exe, mock_get_output): - from certbot_postfix.postconf import ConfigMain - mock_verify_exe.return_value = True - ConfigMain('postconf', lambda x, y, z: None) - mock_get_output.assert_called_with('-P') - - @mock.patch('certbot_postfix.util.PostfixUtilBase._get_output') - def test_read_default(self, mock_get_output): - mock_get_output.return_value = 'param = default_value' - self.assertEqual(self.config.get_default('param'), 'default_value') - - @mock.patch('certbot_postfix.util.PostfixUtilBase._call') - def test_set(self, mock_call): - self.config.set('extra_param', 'other_value') - self.assertEqual(self.config.get('extra_param'), 'other_value') - self.config.flush() - mock_call.assert_called_with(['-e', 'extra_param=other_value']) - - def test_set_bad_param_name(self): - self.assertRaises(KeyError, self.config.set, 'nonexistent_param', 'some_value') - - @mock.patch('certbot_postfix.util.PostfixUtilBase._call') - def test_write_revert(self, mock_call): - self.config.set('default_parameter', 'fake_news') - # revert config set - self.config.set('default_parameter', 'value') - self.config.flush() - mock_call.assert_not_called() - - @mock.patch('certbot_postfix.util.PostfixUtilBase._call') - def test_write_default(self, mock_call): - self.config.set('default_parameter', 'value') - self.config.flush() - mock_call.assert_not_called() - - def test_master_overrides(self): - self.assertEqual(self.config.get_master_overrides('overridden_by_master'), - [('service/type', 'master_value'), - ('service2/type', 'master_value2')]) - - def test_set_check_override(self): - self.assertRaises(errors.PluginError, self.config.set, - 'overridden_by_master', 'new_value') - - def test_ignore_check_override(self): - # pylint: disable=protected-access - self.config._ignore_master_overrides = True - self.config.set('overridden_by_master', 'new_value') - - def test_check_acceptable_overrides(self): - self.config.set('overridden_by_master', 'new_value', - ('master_value', 'master_value2')) - - @mock.patch('certbot_postfix.util.PostfixUtilBase._get_output') - def test_flush(self, mock_out): - self.config.set('default_parameter', 'new_value') - self.config.set('extra_param', 'another_value') - self.config.flush() - arguments = mock_out.call_args_list[-1][0][0] - self.assertEqual('-e', arguments[0]) - self.assertTrue('default_parameter=new_value' in arguments) - self.assertTrue('extra_param=another_value' in arguments) - - @mock.patch('certbot_postfix.util.PostfixUtilBase._get_output') - def test_flush_updates_object(self, mock_out): - self.config.set('default_parameter', 'new_value') - self.config.flush() - mock_out.reset_mock() - self.config.set('default_parameter', 'new_value') - mock_out.assert_not_called() - - @mock.patch('certbot_postfix.util.PostfixUtilBase._get_output') - def test_flush_throws_error_on_fail(self, mock_out): - mock_out.side_effect = [IOError("oh no!")] - self.config.set('default_parameter', 'new_value') - self.assertRaises(errors.PluginError, self.config.flush) - -if __name__ == '__main__': # pragma: no cover - unittest.main() diff --git a/certbot-postfix/certbot_postfix/tests/util_test.py b/certbot-postfix/certbot_postfix/tests/util_test.py deleted file mode 100644 index fa38f83ab9d..00000000000 --- a/certbot-postfix/certbot_postfix/tests/util_test.py +++ /dev/null @@ -1,205 +0,0 @@ -"""Tests for certbot_postfix.util.""" - -import subprocess -import unittest - -import mock - -from certbot import errors - - -class PostfixUtilBaseTest(unittest.TestCase): - """Tests for certbot_postfix.util.PostfixUtilBase.""" - - @classmethod - def _create_object(cls, *args, **kwargs): - from certbot_postfix.util import PostfixUtilBase - return PostfixUtilBase(*args, **kwargs) - - @mock.patch('certbot_postfix.util.verify_exe_exists') - def test_no_exe(self, mock_verify): - expected_error = errors.NoInstallationError - mock_verify.side_effect = expected_error - self.assertRaises(expected_error, self._create_object, 'nonexistent') - - def test_object_creation(self): - with mock.patch('certbot_postfix.util.verify_exe_exists'): - self._create_object('existent') - - @mock.patch('certbot_postfix.util.check_all_output') - def test_call_extends_args(self, mock_output): - # pylint: disable=protected-access - with mock.patch('certbot_postfix.util.verify_exe_exists'): - mock_output.return_value = 'expected' - postfix = self._create_object('executable') - postfix._call(['many', 'extra', 'args']) - mock_output.assert_called_with(['executable', 'many', 'extra', 'args']) - postfix._call() - mock_output.assert_called_with(['executable']) - - def test_create_with_config(self): - # pylint: disable=protected-access - with mock.patch('certbot_postfix.util.verify_exe_exists'): - postfix = self._create_object('exec', 'config_dir') - self.assertEqual(postfix._base_command, ['exec', '-c', 'config_dir']) - -class PostfixUtilTest(unittest.TestCase): - def setUp(self): - # pylint: disable=protected-access - from certbot_postfix.util import PostfixUtil - with mock.patch('certbot_postfix.util.verify_exe_exists'): - self.postfix = PostfixUtil() - self.postfix._call = mock.Mock() - self.mock_call = self.postfix._call - - def test_test(self): - self.postfix.test() - self.mock_call.assert_called_with(['check']) - - def test_test_raises_error_when_check_fails(self): - self.mock_call.side_effect = [subprocess.CalledProcessError(1, "")] - self.assertRaises(errors.MisconfigurationError, self.postfix.test) - self.mock_call.assert_called_with(['check']) - - def test_restart_while_running(self): - self.mock_call.side_effect = [subprocess.CalledProcessError(1, ""), None] - self.postfix.restart() - self.mock_call.assert_called_with(['start']) - - def test_restart_while_not_running(self): - self.postfix.restart() - self.mock_call.assert_called_with(['reload']) - - def test_restart_raises_error_when_reload_fails(self): - self.mock_call.side_effect = [None, subprocess.CalledProcessError(1, "")] - self.assertRaises(errors.PluginError, self.postfix.restart) - self.mock_call.assert_called_with(['reload']) - - def test_restart_raises_error_when_start_fails(self): - self.mock_call.side_effect = [ - subprocess.CalledProcessError(1, ""), - subprocess.CalledProcessError(1, "")] - self.assertRaises(errors.PluginError, self.postfix.restart) - self.mock_call.assert_called_with(['start']) - -class CheckAllOutputTest(unittest.TestCase): - """Tests for certbot_postfix.util.check_all_output.""" - - @classmethod - def _call(cls, *args, **kwargs): - from certbot_postfix.util import check_all_output - return check_all_output(*args, **kwargs) - - @mock.patch('certbot_postfix.util.logger') - @mock.patch('certbot_postfix.util.subprocess.Popen') - def test_command_error(self, mock_popen, mock_logger): - command = 'foo' - retcode = 42 - output = 'bar' - err = 'baz' - - mock_popen().communicate.return_value = (output, err) - mock_popen().poll.return_value = 42 - - self.assertRaises(subprocess.CalledProcessError, self._call, command) - log_args = mock_logger.debug.call_args[0] - for value in (command, retcode, output, err,): - self.assertTrue(value in log_args) - - @mock.patch('certbot_postfix.util.subprocess.Popen') - def test_success(self, mock_popen): - command = 'foo' - expected = ('bar', '') - mock_popen().communicate.return_value = expected - mock_popen().poll.return_value = 0 - - self.assertEqual(self._call(command), expected) - - def test_stdout_error(self): - self.assertRaises(ValueError, self._call, stdout=None) - - def test_stderr_error(self): - self.assertRaises(ValueError, self._call, stderr=None) - - def test_universal_newlines_error(self): - self.assertRaises(ValueError, self._call, universal_newlines=False) - - -class VerifyExeExistsTest(unittest.TestCase): - """Tests for certbot_postfix.util.verify_exe_exists.""" - - @classmethod - def _call(cls, *args, **kwargs): - from certbot_postfix.util import verify_exe_exists - return verify_exe_exists(*args, **kwargs) - - @mock.patch('certbot_postfix.util.certbot_util.exe_exists') - @mock.patch('certbot_postfix.util.plugins_util.path_surgery') - def test_failure(self, mock_exe_exists, mock_path_surgery): - mock_exe_exists.return_value = mock_path_surgery.return_value = False - self.assertRaises(errors.NoInstallationError, self._call, 'foo') - - @mock.patch('certbot_postfix.util.certbot_util.exe_exists') - def test_simple_success(self, mock_exe_exists): - mock_exe_exists.return_value = True - self._call('foo') - - @mock.patch('certbot_postfix.util.certbot_util.exe_exists') - @mock.patch('certbot_postfix.util.plugins_util.path_surgery') - def test_successful_surgery(self, mock_exe_exists, mock_path_surgery): - mock_exe_exists.return_value = False - mock_path_surgery.return_value = True - self._call('foo') - -class TestUtils(unittest.TestCase): - """ Testing random utility functions in util.py - """ - def test_report_master_overrides(self): - from certbot_postfix.util import report_master_overrides - self.assertRaises(errors.PluginError, report_master_overrides, 'name', - [('service/type', 'value')]) - # Shouldn't raise error - report_master_overrides('name', [('service/type', 'value')], - acceptable_overrides=('value',)) - - def test_no_acceptable_value(self): - from certbot_postfix.util import is_acceptable_value - self.assertFalse(is_acceptable_value('name', 'value', None)) - - def test_is_acceptable_value(self): - from certbot_postfix.util import is_acceptable_value - self.assertTrue(is_acceptable_value('name', 'value', ('value',))) - self.assertFalse(is_acceptable_value('name', 'bad', ('value',))) - - def test_is_acceptable_tuples(self): - from certbot_postfix.util import is_acceptable_value - self.assertTrue(is_acceptable_value('name', 'value', ('value', 'value1'))) - self.assertFalse(is_acceptable_value('name', 'bad', ('value', 'value1'))) - - def test_is_acceptable_protocols(self): - from certbot_postfix.util import is_acceptable_value - # SSLv2 and SSLv3 are both not supported, unambiguously - self.assertFalse(is_acceptable_value('tls_mandatory_protocols_lol', - 'SSLv2, SSLv3', None)) - self.assertFalse(is_acceptable_value('tls_protocols_lol', - 'SSLv2, SSLv3', None)) - self.assertFalse(is_acceptable_value('tls_protocols_lol', - '!SSLv2, !TLSv1', None)) - self.assertFalse(is_acceptable_value('tls_protocols_lol', - '!SSLv2, SSLv3, !SSLv3, ', None)) - self.assertTrue(is_acceptable_value('tls_protocols_lol', - '!SSLv2, !SSLv3', None)) - self.assertTrue(is_acceptable_value('tls_protocols_lol', - '!SSLv3, !TLSv1, !SSLv2', None)) - # TLSv1.2 is supported unambiguously - self.assertFalse(is_acceptable_value('tls_protocols_lol', - 'TLSv1, TLSv1.1,', None)) - self.assertFalse(is_acceptable_value('tls_protocols_lol', - 'TLSv1.2, !TLSv1.2,', None)) - self.assertTrue(is_acceptable_value('tls_protocols_lol', - 'TLSv1.2, ', None)) - self.assertTrue(is_acceptable_value('tls_protocols_lol', - 'TLSv1, TLSv1.1, TLSv1.2', None)) - -if __name__ == '__main__': # pragma: no cover - unittest.main() diff --git a/certbot-postfix/certbot_postfix/util.py b/certbot-postfix/certbot_postfix/util.py deleted file mode 100644 index f0698990386..00000000000 --- a/certbot-postfix/certbot_postfix/util.py +++ /dev/null @@ -1,292 +0,0 @@ -"""Utility functions for use in the Postfix installer.""" -import logging -import re -import subprocess - -from certbot import errors -from certbot import util as certbot_util -from certbot.plugins import util as plugins_util -from certbot_postfix import constants - -logger = logging.getLogger(__name__) - -COMMAND = "postfix" - -class PostfixUtilBase(object): - """A base class for wrapping Postfix command line utilities.""" - - def __init__(self, executable, config_dir=None): - """Sets up the Postfix utility class. - - :param str executable: name or path of the Postfix utility - :param str config_dir: path to an alternative Postfix config - - :raises .NoInstallationError: when the executable isn't found - - """ - self.executable = executable - verify_exe_exists(executable) - self._set_base_command(config_dir) - self.config_dir = None - - def _set_base_command(self, config_dir): - self._base_command = [self.executable] - if config_dir is not None: - self._base_command.extend(('-c', config_dir,)) - - def _call(self, extra_args=None): - """Runs the Postfix utility and returns the result. - - :param list extra_args: additional arguments for the command - - :returns: data written to stdout and stderr - :rtype: `tuple` of `str` - - :raises subprocess.CalledProcessError: if the command fails - - """ - args = list(self._base_command) - if extra_args is not None: - args.extend(extra_args) - return check_all_output(args) - - def _get_output(self, extra_args=None): - """Runs the Postfix utility and returns only stdout output. - - This function relies on self._call for running the utility. - - :param list extra_args: additional arguments for the command - - :returns: data written to stdout - :rtype: str - - :raises subprocess.CalledProcessError: if the command fails - - """ - return self._call(extra_args)[0] - -class PostfixUtil(PostfixUtilBase): - """Wrapper around Postfix CLI tool. - """ - - def __init__(self, config_dir=None): - super(PostfixUtil, self).__init__(COMMAND, config_dir) - - def test(self): - """Make sure the configuration is valid. - - :raises .MisconfigurationError: if the config is invalid - """ - try: - self._call(["check"]) - except subprocess.CalledProcessError as e: - logger.debug("Could not check postfix configuration:\n%s", e) - raise errors.MisconfigurationError( - "Postfix failed internal configuration check.") - - def restart(self): - """Restart or refresh the server content. - - :raises .PluginError: when server cannot be restarted - - """ - logger.info("Reloading Postfix configuration...") - if self._is_running(): - self._reload() - else: - self._start() - - - def _is_running(self): - """Is Postfix currently running? - - Uses the 'postfix status' command to determine if Postfix is - currently running using the specified configuration files. - - :returns: True if Postfix is running, otherwise, False - :rtype: bool - - """ - try: - self._call(["status"]) - except subprocess.CalledProcessError: - return False - return True - - def _start(self): - """Instructions Postfix to start running. - - :raises .PluginError: when Postfix cannot start - - """ - try: - self._call(["start"]) - except subprocess.CalledProcessError: - raise errors.PluginError("Postfix failed to start") - - def _reload(self): - """Instructs Postfix to reload its configuration. - - If Postfix isn't currently running, this method will fail. - - :raises .PluginError: when Postfix cannot reload - """ - try: - self._call(["reload"]) - except subprocess.CalledProcessError: - raise errors.PluginError( - "Postfix failed to reload its configuration") - -def check_all_output(*args, **kwargs): - """A version of subprocess.check_output that also captures stderr. - - This is the same as :func:`subprocess.check_output` except output - written to stderr is also captured and returned to the caller. The - return value is a tuple of two strings (rather than byte strings). - To accomplish this, the caller cannot set the stdout, stderr, or - universal_newlines parameters to :class:`subprocess.Popen`. - - Additionally, if the command exits with a nonzero status, output is - not included in the raised :class:`subprocess.CalledProcessError` - because Python 2.6 does not support this. Instead, the failure - including the output is logged. - - :param tuple args: positional arguments for Popen - :param dict kwargs: keyword arguments for Popen - - :returns: data written to stdout and stderr - :rtype: `tuple` of `str` - - :raises ValueError: if arguments are invalid - :raises subprocess.CalledProcessError: if the command fails - - """ - for keyword in ('stdout', 'stderr', 'universal_newlines',): - if keyword in kwargs: - raise ValueError( - keyword + ' argument not allowed, it will be overridden.') - - kwargs['stdout'] = subprocess.PIPE - kwargs['stderr'] = subprocess.PIPE - kwargs['universal_newlines'] = True - - process = subprocess.Popen(*args, **kwargs) - output, err = process.communicate() - retcode = process.poll() - if retcode: - cmd = kwargs.get('args') - if cmd is None: - cmd = args[0] - logger.debug( - "'%s' exited with %d. stdout output was:\n%s\nstderr output was:\n%s", - cmd, retcode, output, err) - raise subprocess.CalledProcessError(retcode, cmd) - return (output, err) - - -def verify_exe_exists(exe, message=None): - """Ensures an executable with the given name is available. - - If an executable isn't found for the given path or name, extra - directories are added to the user's PATH to help find system - utilities that may not be available in the default cron PATH. - - :param str exe: executable path or name - :param str message: Error message to print. - - :raises .NoInstallationError: when the executable isn't found - - """ - if message is None: - message = "Cannot find executable '{0}'.".format(exe) - if not (certbot_util.exe_exists(exe) or plugins_util.path_surgery(exe)): - raise errors.NoInstallationError(message) - -def report_master_overrides(name, overrides, acceptable_overrides=None): - """If the value for a parameter `name` is overridden by other services, - report a warning to notify the user. If `parameter` is a TLS version parameter - (i.e., `parameter` contains 'tls_protocols' or 'tls_mandatory_protocols'), then - `acceptable_overrides` isn't used each value in overrides is inspected for secure TLS - versions. - - :param str name: The name of the parameter that is being overridden. - :param list overrides: The values that other services are setting for `name`. - Each override is a tuple: (service name, value) - :param tuple acceptable_overrides: Override values that are acceptable. For instance, if - another service is overriding our parameter with a more secure option, we don't have - to warn. If this is set to None, errors are raised for *any* overrides of `name`! - """ - error_string = "" - for override in overrides: - service, value = override - # If this override is acceptable: - if acceptable_overrides is not None and \ - is_acceptable_value(name, value, acceptable_overrides): - continue - error_string += " {0}: {1}\n".format(service, value) - if error_string: - raise errors.PluginError("{0} is overridden with less secure options by the " - "following services in master.cf:\n".format(name) + error_string) - - -def is_acceptable_value(parameter, value, acceptable=None): - """ Returns whether the `value` for this `parameter` is acceptable, - given a tuple of `acceptable` values. If `parameter` is a TLS version parameter - (i.e., `parameter` contains 'tls_protocols' or 'tls_mandatory_protocols'), then - `acceptable` isn't used and `value` is inspected for secure TLS versions. - - :param str parameter: The name of the parameter being set. - :param str value: Proposed new value for parameter. - :param tuple acceptable: List of acceptable values for parameter. - """ - # Check if param value is a comma-separated list of protocols. - # Otherwise, just check whether the value is in the acceptable list. - if 'tls_protocols' in parameter or 'tls_mandatory_protocols' in parameter: - return _has_acceptable_tls_versions(value) - if acceptable is not None: - return value in acceptable - return False - - -def _has_acceptable_tls_versions(parameter_string): - """ - Checks to see if the list of TLS protocols is acceptable. - This requires that TLSv1.2 is supported, and neither SSLv2 nor SSLv3 are supported. - - Should be a string of protocol names delimited by commas, spaces, or colons. - - Postfix's documents suggest listing protocols to exclude, like "!SSLv2, !SSLv3". - Listing the protocols to include, like "TLSv1, TLSv1.1, TLSv1.2" is okay as well, - though not recommended - - When these two modes are interspersed, the presence of a single non-negated protocol name - (i.e. "TLSv1" rather than "!TLSv1") automatically excludes all other unnamed protocols. - - In addition, the presence of both a protocol name inclusion and exclusion isn't explicitly - documented, so this method should return False if it encounters contradicting statements - about TLSv1.2, SSLv2, or SSLv3. (for instance, "SSLv3, !SSLv3"). - """ - if not parameter_string: - return False - bad_versions = list(constants.TLS_VERSIONS) - for version in constants.ACCEPTABLE_TLS_VERSIONS: - del bad_versions[bad_versions.index(version)] - supported_version_list = re.split("[, :]+", parameter_string) - # The presence of any non-"!" protocol listing excludes the others by default. - inclusion_list = False - for version in supported_version_list: - if not version: - continue - if version in bad_versions: # short-circuit if we recognize any bad version - return False - if version[0] != "!": - inclusion_list = True - if inclusion_list: # For any inclusion list, we still require TLS 1.2. - if "TLSv1.2" not in supported_version_list or "!TLSv1.2" in supported_version_list: - return False - else: - for bad_version in bad_versions: - if "!" + bad_version not in supported_version_list: - return False - return True - diff --git a/certbot-postfix/docs/.gitignore b/certbot-postfix/docs/.gitignore deleted file mode 100644 index ba65b13af5e..00000000000 --- a/certbot-postfix/docs/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/_build/ diff --git a/certbot-postfix/docs/Makefile b/certbot-postfix/docs/Makefile deleted file mode 100644 index 717ff654fa6..00000000000 --- a/certbot-postfix/docs/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -# Minimal makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -SPHINXPROJ = certbot-postfix -SOURCEDIR = . -BUILDDIR = _build - -# Put it first so that "make" without argument is like "make help". -help: - @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) - -.PHONY: help Makefile - -# Catch-all target: route all unknown targets to Sphinx using the new -# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). -%: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) \ No newline at end of file diff --git a/certbot-postfix/docs/api.rst b/certbot-postfix/docs/api.rst deleted file mode 100644 index 8668ec5d8ad..00000000000 --- a/certbot-postfix/docs/api.rst +++ /dev/null @@ -1,8 +0,0 @@ -================= -API Documentation -================= - -.. toctree:: - :glob: - - api/** diff --git a/certbot-postfix/docs/api/installer.rst b/certbot-postfix/docs/api/installer.rst deleted file mode 100644 index 121d58d5b0b..00000000000 --- a/certbot-postfix/docs/api/installer.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_postfix.installer` --------------------------------------- - -.. automodule:: certbot_postfix.installer - :members: diff --git a/certbot-postfix/docs/api/postconf.rst b/certbot-postfix/docs/api/postconf.rst deleted file mode 100644 index 917150e4582..00000000000 --- a/certbot-postfix/docs/api/postconf.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot_postfix.postconf` -------------------------------- - -.. automodule:: certbot_postfix.postconf - :members: diff --git a/certbot-postfix/docs/conf.py b/certbot-postfix/docs/conf.py deleted file mode 100644 index 51d99aab590..00000000000 --- a/certbot-postfix/docs/conf.py +++ /dev/null @@ -1,190 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Configuration file for the Sphinx documentation builder. -# -# This file does only contain a selection of the most common options. For a -# full list see the documentation: -# http://www.sphinx-doc.org/en/master/config - -# -- Path setup -------------------------------------------------------------- - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -# -import os -# import sys -# sys.path.insert(0, os.path.abspath('.')) - - -# -- Project information ----------------------------------------------------- - -project = u'certbot-postfix' -copyright = u'2018, Certbot Project' -author = u'Certbot Project' - -# The short X.Y version -version = u'0' -# The full version, including alpha/beta/rc tags -release = u'0' - - -# -- General configuration --------------------------------------------------- - -# If your documentation needs a minimal Sphinx version, state it here. -# -needs_sphinx = '1.0' - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.viewcode', -] - -autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# The suffix(es) of source filenames. -# You can specify multiple suffix as a list of string: -# -# source_suffix = ['.rst', '.md'] -source_suffix = '.rst' - -# The master toctree document. -master_doc = 'index' - -# The language for content autogenerated by Sphinx. Refer to documentation -# for a list of supported languages. -# -# This is also used if you do content translation via gettext catalogs. -# Usually you set "language" from the command line for these cases. -language = u'en' - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -# This pattern also affects html_static_path and html_extra_path . -exclude_patterns = [u'_build', 'Thumbs.db', '.DS_Store'] - -default_role = 'py:obj' - -# The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' - - -# -- Options for HTML output ------------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -# - -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it - -# Theme options are theme-specific and customize the look and feel of a theme -# further. For a list of options available for each theme, see the -# documentation. -# -# html_theme_options = {} - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -# html_static_path = ['_static'] - -# Custom sidebar templates, must be a dictionary that maps document names -# to template names. -# -# The default sidebars (for documents that don't match any pattern) are -# defined by theme itself. Builtin themes are using these templates by -# default: ``['localtoc.html', 'relations.html', 'sourcelink.html', -# 'searchbox.html']``. -# -# html_sidebars = {} - - -# -- Options for HTMLHelp output --------------------------------------------- - -# Output file base name for HTML help builder. -htmlhelp_basename = 'certbot-postfixdoc' - - -# -- Options for LaTeX output ------------------------------------------------ - -latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - # - # 'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - # - # 'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - # - # 'preamble': '', - - # Latex figure (float) alignment - # - # 'figure_align': 'htbp', -} - -# Grouping the document tree into LaTeX files. List of tuples -# (source start file, target name, title, -# author, documentclass [howto, manual, or own class]). -latex_documents = [ - (master_doc, 'certbot-postfix.tex', u'certbot-postfix Documentation', - u'Certbot Project', 'manual'), -] - - -# -- Options for manual page output ------------------------------------------ - -# One entry per manual page. List of tuples -# (source start file, name, description, authors, manual section). -man_pages = [ - (master_doc, 'certbot-postfix', u'certbot-postfix Documentation', - [author], 1) -] - - -# -- Options for Texinfo output ---------------------------------------------- - -# Grouping the document tree into Texinfo files. List of tuples -# (source start file, target name, title, author, -# dir menu entry, description, category) -texinfo_documents = [ - (master_doc, 'certbot-postfix', u'certbot-postfix Documentation', - author, 'certbot-postfix', 'One line description of project.', - 'Miscellaneous'), -] - - -# -- Extension configuration ------------------------------------------------- - -# -- Options for intersphinx extension --------------------------------------- - -# Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = { - 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), -} - -# -- Options for todo extension ---------------------------------------------- - -# If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True diff --git a/certbot-postfix/docs/index.rst b/certbot-postfix/docs/index.rst deleted file mode 100644 index 3d6697bcb7d..00000000000 --- a/certbot-postfix/docs/index.rst +++ /dev/null @@ -1,28 +0,0 @@ -.. certbot-postfix documentation master file, created by - sphinx-quickstart on Wed May 2 16:01:06 2018. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -Welcome to certbot-postfix's documentation! -=========================================== - -.. toctree:: - :maxdepth: 2 - :caption: Contents: - -.. automodule:: certbot_postfix - :members: - -.. toctree:: - :maxdepth: 1 - - api - - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` diff --git a/certbot-postfix/docs/make.bat b/certbot-postfix/docs/make.bat deleted file mode 100644 index 23fbdc93c6a..00000000000 --- a/certbot-postfix/docs/make.bat +++ /dev/null @@ -1,36 +0,0 @@ -@ECHO OFF - -pushd %~dp0 - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set SOURCEDIR=. -set BUILDDIR=_build -set SPHINXPROJ=certbot-postfix - -if "%1" == "" goto help - -%SPHINXBUILD% >NUL 2>NUL -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ - exit /b 1 -) - -%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% -goto end - -:help -%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% - -:end -popd diff --git a/certbot-postfix/local-oldest-requirements.txt b/certbot-postfix/local-oldest-requirements.txt deleted file mode 100644 index bc0cdbf0018..00000000000 --- a/certbot-postfix/local-oldest-requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -acme[dev]==0.25.0 -certbot[dev]==0.23.0 diff --git a/certbot-postfix/setup.cfg b/certbot-postfix/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/certbot-postfix/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/certbot-postfix/setup.py b/certbot-postfix/setup.py deleted file mode 100644 index 0ff2908dfc3..00000000000 --- a/certbot-postfix/setup.py +++ /dev/null @@ -1,64 +0,0 @@ -from setuptools import setup -from setuptools import find_packages - - -version = '0.26.0.dev0' - -install_requires = [ - 'acme>=0.25.0', - 'certbot>=0.23.0', - 'setuptools', - 'six', - 'zope.component', - 'zope.interface', -] - -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] - -setup( - name='certbot-postfix', - version=version, - description="Postfix plugin for Certbot", - url='https://github.com/certbot/certbot', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Environment :: Plugins', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Communications :: Email :: Mail Transport Agents', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, - install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'certbot.plugins': [ - 'postfix = certbot_postfix:Installer', - ], - }, - test_suite='certbot_postfix', -) diff --git a/certbot/.gitignore b/certbot/.gitignore deleted file mode 100644 index a4e94ecfc9b..00000000000 --- a/certbot/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.crt diff --git a/certbot/.readthedocs.yaml b/certbot/.readthedocs.yaml new file mode 100644 index 00000000000..95876831b90 --- /dev/null +++ b/certbot/.readthedocs.yaml @@ -0,0 +1,30 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + # You can also specify other tool versions + jobs: + post_install: + # Using a constraints file isn't natively supported. We use a + # post_install job with the following flags and method of invoking pip to + # most closely mimic the current default installation behavior. + - python -m pip install --exists-action=w --no-cache-dir -c tools/requirements.txt -e acme -e certbot[docs] + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: certbot/docs/conf.py + # You can configure Sphinx to use a different builder, for instance use the dirhtml builder for simpler URLs + # builder: "dirhtml" + # Fail on all warnings to avoid broken references + fail_on_warning: true + +# Optionally build your docs in additional formats such as PDF and ePub +formats: + - pdf diff --git a/certbot/CHANGELOG.md b/certbot/CHANGELOG.md new file mode 100644 index 00000000000..f020c279db9 --- /dev/null +++ b/certbot/CHANGELOG.md @@ -0,0 +1,3463 @@ +# Certbot change log + +Certbot adheres to [Semantic Versioning](https://semver.org/). + + + +## 5.7.0 - 2026-07-15 + +### Fixed + +- Fixed nginx configuration parsing when comments appear between tokens of a multi-line directive. ([#10598](https://github.com/certbot/certbot/issues/10598)) + + +## 5.6.0 - 2026-05-11 + +### Changed + +- certbot now requires version 1.7+ of the library distro and certbot-dns-cloudflare requires 4.0+ of the Cloudflare Python library. ([#10587](https://github.com/certbot/certbot/issues/10587)) + +### Fixed + +- The certbot-dns-ovh snap and docker image now properly delete any created TXT records after the challenge is completed by requiring dns-lexicon 3.25.1 which contains the fix. ([#10492](https://github.com/certbot/certbot/issues/10492)) +- Our Docker images have been updated to use Python 3.14 and Alpine Linux 3.23. ([#10619](https://github.com/certbot/certbot/issues/10619)) + + +## 5.5.0 - 2026-04-07 + +### Changed + +- Moved nearly all code for the certbot-nginx and certbot-apache plugins into private modules in the certbot package which now offers "apache" and "nginx" [extras](https://packaging.python.org/en/latest/specifications/dependency-specifiers/#extras). Users should notice no major changes. certbot-apache and certbot-nginx will continue to exist as simple packages that depend on certbot and also help register the plugin with certbot so it knows the functionality is available. Unit tests for these plugins are now also part of the certbot package. ([#10484](https://github.com/certbot/certbot/issues/10484)) +- The certbot.ocsp module has been deprecated and will be removed in the next major release. This is not a change to Certbot's OCSP functionality. The code is just being removed from Certbot's public API. ([#10584](https://github.com/certbot/certbot/issues/10584)) + + +## 5.4.0 - 2026-03-10 + +### Added + +- The webroot plugin now supports IP address issuance. ([#10543](https://github.com/certbot/certbot/issues/10543)) + +### Changed + +- certbot-nginx now requires pyparsing>=3.0.0. ([#10560](https://github.com/certbot/certbot/issues/10560)) + + +## 5.3.1 - 2026-02-09 + +### Fixed + +- We rebuilt our snaps to include updated versions our dependencies. ([#10569](https://github.com/certbot/certbot/issues/10569)) + + +## 5.3.0 - 2026-02-03 + +### Added + +- A new command line flag, --ip-address, has been added. This requests certificates with IP address SANs when using the standalone or manual plugin. Note that for Let's Encrypt's implementation of IP address certificates, you'll also need to pass `--preferred-profile shortlived`. ([#10495](https://github.com/certbot/certbot/issues/10495), [#10544](https://github.com/certbot/certbot/pull/10544)) + +### Changed + +- Deploy directory hooks are now also run when using `certbot certonly` or `certbot run` to get a new cert. This change was made for pre and post directory hooks in our 3.2.0 release so this change unifies Certbot's behavior here. ([#9978](https://github.com/certbot/certbot/issues/9978)) +- A few largely unused functions/types have been deprecated in our effort to remove our pyOpenSSL dependency: + * Deprecated: `certbot.crypto_util.get_sans_from_cert` + * Deprecated: `certbot.crypto_util.get_names_from_cert` + * Deprecated: `certbot.crypto_util.get_names_from_req` + * Deprecated: `certbot.crypto_util.import_csr_file` (and replaced by `certbot.crypto_util.read_csr_file`) + * Deprecated: `acme.crypto_util.Format` ([#10433](https://github.com/certbot/certbot/issues/10433)) +- `achallenges.KeyAuthorizationAnnotatedChallenge`, `achallenges.DNS`, and `achallenges.Other` have a new field `identifier`, of type `acme.messages.Identifier`. This should be used in place of the `domain` field, which is now deprecated both as an attribute and during object creation. ([#10491](https://github.com/certbot/certbot/issues/10491)) +- Authenticator.get_chall_pref's argument has been renamed from `domain` to `identifier`, and can now receive string-formatted IP addresses in addition to domain names. ([#10495](https://github.com/certbot/certbot/issues/10495)) +- san.DNSName now calls util.enforce_domain_sanity to reduce code duplication ([#10519](https://github.com/certbot/certbot/issues/10519)) + +### Fixed + +- Removed the outdated email address from our Python packages' metadata. ([#10533](https://github.com/certbot/certbot/issues/10533)) +- The HTTP01.uri method will now properly enclose IPv6 addresses in square brackets. ([#10548](https://github.com/certbot/certbot/issues/10548)) + + +## 5.2.2 - 2025-12-10 + +### Fixed + +- Fixed a regression that caused certbot to crash if multiple --webroot-path + values were set on the command line. + ([#10509](https://github.com/certbot/certbot/issues/10509)) + + +## 5.2.1 - 2025-12-03 + +### Added + +- Support for Python 3.14 was added. + ([#10477](https://github.com/certbot/certbot/issues/10477)) + +### Changed + +- While nothing significant should have changed from the user's perspective, + we've been doing a lot of internal refactoring in preparation for soon adding + support for IP address certificates to Certbot. + ([#10468](https://github.com/certbot/certbot/issues/10468), + [#10478](https://github.com/certbot/certbot/issues/10478)) + +### Fixed + +- Removed `vhost_combined` and `vhost_common` log formats from included Apache + configuration file. ([#9769](https://github.com/certbot/certbot/issues/9769)) +- Due to a mistake on our end playing with GitHub's new [immutable + releases](https://github.blog/changelog/2025-10-28-immutable-releases-are-now-generally-available/) + feature that prevented our CI from uploading additional release assets, + Certbot 5.2.0 was not and will not be uploaded to most platforms. Instead, + that version number will be skipped and we'll go straight to 5.2.1. + ([#10501](https://github.com/certbot/certbot/issues/10501)) + + +## 5.1.0 - 2025-10-07 + +### Changed + +- certbot-nginx no longer creates and uses self-signed certificates as an + intermediate step when installing certificates. The certificates the user + requested Certbot install are now always used instead. + ([#10465](https://github.com/certbot/certbot/issues/10465)) +- The function `acme.crypto_util.make_self_signed_cert` was deprecated and will + be removed in a future release. + ([#10466](https://github.com/certbot/certbot/issues/10466)) + +### Fixed + +- Fixed a bug in certbot-nginx that'd leave nginx configured with self-signed + certificates if a user ran `certbot enhance` and they didn't have matching + SSL server blocks. `certbot enhance` now requires the user to have a matching + SSL server block to enable HSTS or OCSP stapling enhancements. + ([#10455](https://github.com/certbot/certbot/issues/10455)) + + +## 5.0.0 - 2025-09-02 + +### Added + +- Certbot now stores the Retry-After value given by ACME Renewal Info (ARI) so + the value can be respected across multiple Certbot runs. + ([#10377](https://github.com/certbot/certbot/issues/10377)) +- Added `uv` as a test dependency, and switched most `pip` invocations to `uv + pip` for faster installs. + ([#10428](https://github.com/certbot/certbot/issues/10428)) + +### Changed + +- Removed final instances of pyopenssl x509 and PKey objects + * Removed `acme.crypto_util.SSLSocket` + * Removed `acme.crypto_util.probe_sni` + + ([#10079](https://github.com/certbot/certbot/issues/10079), + [#10381](https://github.com/certbot/certbot/issues/10381)) +- Removed a number of deprecated classes/interfaces + * Removed `acme.challenges.TLSALPN01Response` + * Removed `acme.challenges.TLSALPN01` + * Removed `acme.standalone.TLSServer` + * Removed `acme.standalone.TLSALPN01Server` + + ([#10274](https://github.com/certbot/certbot/issues/10274)) +- certbot.ocsp.RevocationChecker.__init__ no longer accepts the parameter + `enforce_openssl_binary_usage` and always uses the cryptography Python + library for OCSP checking. + ([#10291](https://github.com/certbot/certbot/issues/10291)) +- Python 3.9 support was removed. + ([#10389](https://github.com/certbot/certbot/issues/10389)) +- Migrated most functionality from `certbot/setup.py` to + `certbot/pyproject.toml` + ([#10402](https://github.com/certbot/certbot/issues/10402)) +- Migrated most functionality from `setup.py` to `pyproject.toml` for acme, + certbot-apache, and certbot-nginx. + ([#10417](https://github.com/certbot/certbot/issues/10417)) +- Migrated most functionality from `setup.py` to `pyproject.toml` for certbot + dns plugins. ([#10425](https://github.com/certbot/certbot/issues/10425)) +- Updated apache TLS configuration options based on changes to Mozilla's + intermediate configuration recommendations. + * Added `DHE-RSA-CHACHA20-POLY1305` to `SSLCipherSuite` list for better + compliance + * Configured curves using `SSLOpenSSLConfCmd` so FFDH won't be used with + OpenSSL 3.0 + + ([#10443](https://github.com/certbot/certbot/issues/10443)) + +### Fixed + +- certbot-apache no longer prints a warning claiming the version of OpenSSL + used by Apache is too old when we were unable determine the OpenSSL version. + ([#10444](https://github.com/certbot/certbot/issues/10444)) +- certbot-nginx no longer uses socket.gethostname when generating self-signed + certificates for use as a temporary step of installing certificates as it + would sometimes result in strings that are too long to be used in the common + name of a certificate. The static domain "temp-certbot-nginx.invalid" is now + used instead. ([#10447](https://github.com/certbot/certbot/issues/10447)) + + +## 4.2.0 - 2025-08-05 + +### Added + +- Added `--eab-hmac-alg` parameter to support custom HMAC algorithm for + External Account Binding. + ([#10281](https://github.com/certbot/certbot/issues/10281)) + +### Changed + +- Catches and ignores errors during the directory fetch for ARI checking so + that these errors do not hinder the actual certificate issuance. + ([#10342](https://github.com/certbot/certbot/issues/10342)) +- Removed the dependency on `pytz`. + ([#10350](https://github.com/certbot/certbot/issues/10350)) +- Deprecated `acme.crypto_util.probe_sni` + ([#10386](https://github.com/certbot/certbot/issues/10386)) +- Support for Python 3.9 was deprecated and will be removed in our next planned + release. ([#10390](https://github.com/certbot/certbot/issues/10390)) + +### Fixed + +- The Certbot snap no longer sets the environment variable PYTHONPATH stopping + it from picking up Python files in the current directory and polluting the + environment for Certbot hooks written in Python. + ([#10176](https://github.com/certbot/certbot/issues/10176), + [#10257](https://github.com/certbot/certbot/issues/10257)) +- Previously, we claimed to set FAILED_DOMAINS and RENEWED_DOMAINS env + variables for use by post-hooks when certificate renewals fail, but we were + not actually setting them. Now, we are. + ([#10259](https://github.com/certbot/certbot/issues/10259)) +- Certbot now always uses the server value from the renewal configuration file + for ARI checks instead of the server value from the current invocation of + Certbot. This helps prevent ARI requests from going to the wrong server if + the user changes CAs. + ([#10339](https://github.com/certbot/certbot/issues/10339)) + + +## 4.1.1 - 2025-06-12 + +### Fixed + +* When a CA fails to issue a certificate after finalization, print the ACME error from the order +* No longer checks ARI during certbot --dry-run, because --dry-run uses staging when used + with let's encrypt but the cert was issued against the default server. This would emit + a scary warning, even though the cert would renew successfully. +* Contacting the CA to check ARI is now skipped for certificate lineages that + have autorenew set to False. + +More details about these changes can be found on our GitHub repo. + +## 4.1.0 - 2025-06-10 + +### Added + +* ACME Renewal Info (ARI) support. https://datatracker.ietf.org/doc/draft-ietf-acme-ari/ + `certbot renew` will automatically check ARI when using an ACME server that supports it, + and may renew early based on the ARI information. For Let's Encrypt certificates this + will typically cause renewal at around 2/3rds of the certificate's lifetime, even if + the renew_before_expiry field of a lineage renewal config is set a later date. + +### Changed + +* Switched to src-layout from flat-layout to accommodate PEP 517 pip editable installs +* acme.client.ClientNetwork now makes the "key" parameter optional. +* Deprecated `acme.challenges.TLSALPN01Response` +* Deprecated `acme.challenges.TLSALPN01` +* Deprecated parameter `alpn_protocols` from `acme.crypto_util.probe_sni` +* Deprecated `acme.crypto_util.SSLSocket` +* Deprecated `acme.standalone.TLSServer` +* Deprecated `acme.standalone.TLSALPN01Server` +* Deprecated parameter `enforce_openssl_binary_usage` from certbot.ocsp.RevocationChecker. +* Dropped support for Python 3.9.0 and 3.9.1 for compatibility with newer + versions of the cryptography Python package. Python 3.9.2+ is still + supported. + +### Fixed + +* Order finalization now catches `orderNotReady` response, polls until order status is + `ready`, and resubmits finalization request before polling for `valid` to download + certificate. This conforms to RFC 8555 more accurately and avoids race conditions where + all authorizations are fulfilled but order has not yet transitioned to ready state on + the server when the finalization request is sent. It also respects retry-after when + polling for finalization readiness. +* The --preferred-profile and --required-profile flags now have their values stored in + the renewal configuration so the same setting will be used on renewal. +* Fixed an unintended change introduced in 4.0.0 where `renew_before_expiry` could not be + shorter than certbot's default renewal time. If the server does not provide an ARI + response, `renew_before_expiry` will continue to override certbot's default. However, + an early ARI response will override a later `renew_before_expiry` time, to account for + notifications in case of certificate revocation, especially with the impending deprecation + of OCSP (https://letsencrypt.org/2024/12/05/ending-ocsp/). To force a later date, users + can replace certbot's default cron job and/or systemd timer with one of their own timing. + +More details about these changes can be found on our GitHub repo. + +## 4.0.0 - 2025-04-07 + +### Added + +* The --preferred-profile and --required-profile flags allow requesting a profile. + https://datatracker.ietf.org/doc/draft-aaron-acme-profiles/ + +### Changed + +* Certificates now renew with 1/3rd of lifetime left (or 1/2 of lifetime left, + if the lifetime is shorter than 10 days). This is a change from a hardcoded + renewal at 30 days before expiration. The config field renew_before_expiry + still overrides this default. + +* removed `acme.crypto_util._pyopenssl_cert_or_req_all_names` +* removed `acme.crypto_util._pyopenssl_cert_or_req_san` +* removed `acme.crypto_util.dump_pyopenssl_chain` +* removed `acme.crypto_util.gen_ss_cert` +* removed `certbot.crypto_util.dump_pyopenssl_chain` +* removed `certbot.crypto_util.pyopenssl_load_certificate` + + +### Fixed + +* Moved `RewriteEngine on` directive added during apache http01 authentication + to the end of the virtual host, so that it overwrites any `RewriteEngine off` + directives that already exist and allows redirection to the challenge URL. + +More details about these changes can be found on our GitHub repo. + +## 3.3.0 - 2025-03-11 + +### Added + +* + +### Changed + +* The --register-unsafely-without-email flag is no longer needed in non-interactive mode. +* In interactive mode, pressing Enter at the email prompt will register without an email. +* deprecated `acme.crypto_util._pyopenssl_cert_or_req_all_names` +* deprecated `acme.crypto_util._pyopenssl_cert_or_req_san` +* deprecated `acme.crypto_util.dump_pyopenssl_chain` +* deprecated `certbot.crypto_util.dump_pyopenssl_chain` +* deprecated `certbot.crypto_util.pyopenssl_load_certificate` + +### Fixed + +* Fixed a bug introduced in Certbot 3.1.0 where OpenSSL environment variables + needed in our snap configuration were persisted in calls to external programs + like nginx which could cause them to fail to load OpenSSL. + +More details about these changes can be found on our GitHub repo. + +## 3.2.0 - 2025-02-11 + +### Added + +* + +### Changed + +* certbot-nginx now requires pyparsing>=2.4.7. +* certbot and its acme library now require cryptography>=43.0.0. +* certbot-nginx and our acme library now require pyOpenSSL>=25.0.0. +* Deprecated `gen_ss_cert` in `acme.crypto_util` as it uses deprecated + pyOpenSSL API. +* Add `make_self_signed_cert` to `acme.crypto_util` to replace `gen_ss_cert. +* Directory hooks are now run on all commands by default, not just `renew` +* Help output now shows `False` as default when it can be set via `cli.ini` instead of `None` +* Changed terms of service agreement text to have a newline after the TOS link +* certbot-cloudflare-dns is now pinned to version 2.19 of Cloudflare's python library +* Removed support for Linode API v3 which was sunset at the end of July 203. + +### Fixed + +* Private keys are now saved in PKCS#8 format instead of PKCS#1. Using PKCS#1 + was a regression introduced in Certbot 3.1.0. +* Allow nginx plugin to parse non-breaking spaces in nginx configuration files. +* Honor --reuse-key when --allow-subset-of-names is set +* Fixed regression in symlink parsing on Windows that was introduced in Certbot + 3.1.0. +* When adding ssl listen directives in nginx server blocks, IP addresses are now + preserved. +* Nginx configurations can now have the http block in files other than the root (nginx.conf) +* Nginx `server_name` directives with internal comments now ignore commented names + +More details about these changes can be found on our GitHub repo. + +## 3.1.0 - 2025-01-07 + +### Added + +* Support for Python 3.13 was added. + +### Changed + +* Python 3.8 support was removed. +* certbot-dns-rfc2136's minimum required version of dnspython is now 2.6.1. +* Updated our Docker images to be based on Alpine Linux 3.20. +* Our runtime dependency on setuptools has been dropped from all Certbot + components. +* Certbot's packages no longer depend on library importlib_resources. + +### Fixed + +* Included an OpenSSL library that was missing in our Certbot snap fixing + crashes affecting 32-bit ARM users. + +More details about these changes can be found on our GitHub repo. + +## 3.0.1 - 2024-11-14 + +### Fixed + +* Removed a CryptographyDeprecationWarning that was being displayed to users + when checking OCSP status. + +More details about these changes can be found on our GitHub repo. + +## 3.0.0 - 2024-11-05 + +### Added + +* + +### Changed + +* The update_symlinks command was removed. +* The `csr_dir` and `key_dir` attributes on + `certbot.configuration.NamespaceConfig` were removed. +* The `--manual-public-ip-logging-ok` command line flag was removed. +* The `--dns-route53-propagation-seconds` command line flag was removed. +* The `certbot_dns_route53.authenticator` module has been removed. This should + not affect any users of the plugin and instead would only affect developers + trying to develop on top of the old code. +* Support for Python 3.8 was deprecated and will be removed in our next planned + release. + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 2.11.0 - 2024-06-05 + +### Added + +* + +### Changed + +* In anticipation of backwards incompatible changes, certbot-dns-cloudflare now + requires less than version 2.20 of Cloudflare's python library. + +### Fixed + +* Fixed a bug in Certbot where a CSR's SANs did not always follow the order of + the domain names that the user requested interactively. In some cases, the + resulting cert's common name might seem picked up randomly from the SANs + when it should be the first item the user had in mind. + +More details about these changes can be found on our GitHub repo. + +## 2.10.0 - 2024-04-02 + +### Added + +* The Python source packages which we upload to [PyPI](https://pypi.org/) are + now also being uploaded to + [our releases on GitHub](https://github.com/certbot/certbot/releases) where + we now also include a SHA256SUMS checksum file and a PGP signature for that + file. + +### Changed + +* We no longer publish our beta Windows installer as was originally announced + [here](https://community.letsencrypt.org/t/certbot-discontinuing-windows-beta-support-in-2024/208101). + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 2.9.0 - 2024-02-08 + +### Added + +* Support for Python 3.12 was added. + +### Changed + +* + +### Fixed + +* Updates `joinpath` syntax to only use one addition per call, because the multiple inputs + version was causing mypy errors on Python 3.10. +* Makes the `reconfigure` verb actually use the staging server for the dry run to check the new + configuration. + +More details about these changes can be found on our GitHub repo. + +## 2.8.0 - 2023-12-05 + +### Added + +* Added support for [Alpine Linux](https://www.alpinelinux.org) distribution when is used the apache plugin + +### Changed + +* Support for Python 3.7 was removed. + +### Fixed + +* Stop using the deprecated `pkg_resources` API included in `setuptools`. + +More details about these changes can be found on our GitHub repo. + +## 2.7.4 - 2023-11-01 + +### Fixed + +* Fixed a bug introduced in version 2.7.0 that caused interactively entered + webroot plugin values to not be saved for renewal. +* Fixed a bug introduced in version 2.7.0 of our Lexicon based DNS plugins that + caused them to fail to find the DNS zone that needs to be modified in some + cases. + +More details about these changes can be found on our GitHub repo. + +## 2.7.3 - 2023-10-24 + +### Fixed + +* Fixed a bug where arguments with contained spaces weren't being handled correctly +* Fixed a bug that caused the ACME account to not be properly restored on + renewal causing problems in setups where the user had multiple accounts with + the same ACME server. + +More details about these changes can be found on our GitHub repo. + +## 2.7.2 - 2023-10-19 + +### Fixed + +* `certbot-dns-ovh` plugin now requires `lexicon>=3.15.1` to ensure a consistent behavior with OVH APIs. +* Fixed a bug where argument sources weren't correctly detected in abbreviated + arguments, short arguments, and some other circumstances + +More details about these changes can be found on our GitHub repo. + +## 2.7.1 - 2023-10-10 + +### Fixed + +* Fixed a bug that broke the DNS plugin for DNSimple that was introduced in + version 2.7.0 of the plugin. +* Correctly specified the new minimum version of the ConfigArgParse package + that Certbot requires which is 1.5.3. + +More details about these changes can be found on our GitHub repo. + +## 2.7.0 - 2023-10-03 + +### Added + +* Add `certbot.util.LooseVersion` class. See [GH #9489](https://github.com/certbot/certbot/issues/9489). +* Add a new base class `certbot.plugins.dns_common_lexicon.LexiconDNSAuthenticator` to implement a DNS + authenticator plugin backed by Lexicon to communicate with the provider DNS API. This approach relies + heavily on conventions to reduce the implementation complexity of a new plugin. +* Add a new test base class `certbot.plugins.dns_test_common_lexicon.BaseLexiconDNSAuthenticatorTest` to + help testing DNS plugins implemented on top of `LexiconDNSAuthenticator`. + +### Changed + +* `NamespaceConfig` now tracks how its arguments were set via a dictionary, allowing us to remove a bunch + of global state previously needed to inspect whether a user set an argument or not. +* Support for Python 3.7 was deprecated and will be removed in our next planned release. +* Added `RENEWED_DOMAINS` and `FAILED_DOMAINS` environment variables for consumption by post renewal hooks. +* Deprecates `LexiconClient` base class and `build_lexicon_config` function in + `certbot.plugins.dns_common_lexicon` module in favor of `LexiconDNSAuthenticator`. +* Deprecates `BaseLexiconAuthenticatorTest` and `BaseLexiconClientTest` test base classes of + `certbot.plugins.dns_test_common_lexicon` module in favor of `BaseLexiconDNSAuthenticatorTest`. + +### Fixed + +* Do not call deprecated datetime.utcnow() and datetime.utcfromtimestamp() +* Filter zones in `certbot-dns-google` to avoid usage of private DNS zones to create records + +More details about these changes can be found on our GitHub repo. + +## 2.6.0 - 2023-05-09 + +### Added + +* `--dns-google-project` optionally allows for specifying the project that the DNS zone(s) reside in, + which allows for Certbot usage in scenarios where the auth credentials reside in a different + project to the zone(s) that are being managed. +* There is now a new `Other` annotated challenge object to allow plugins to support entirely novel challenges. + +### Changed + +* Optionally sign the SOA query for dns-rfc2136, to help resolve problems with split-view + DNS setups and hidden primary setups. + * Certbot versions prior to v1.32.0 did not sign queries with the specified TSIG key + resulting in difficulty with split-horizon implementations. + * Certbot v1.32.0 through v2.5.0 signed queries by default, potentially causing + incompatibility with hidden primary setups with `allow-update-forwarding` enabled + if the secondary did not also have the TSIG key within its config. + * Certbot v2.6.0 and later no longer signs queries by default, but allows + the user to optionally sign these queries by explicit configuration using the + `dns_rfc2136_sign_query` option in the credentials .ini file. +* Lineage name validity is performed for new lineages. `--cert-name` may no longer contain + filepath separators (i.e. `/` or `\`, depending on the platform). +* `certbot-dns-google` now loads credentials using the standard [Application Default + Credentials](https://cloud.google.com/docs/authentication/application-default-credentials) strategy, + rather than explicitly requiring the Google Compute metadata server to be present if a service account + is not provided using `--dns-google-credentials`. +* `--dns-google-credentials` now supports additional types of file-based credential, such as + [External Account Credentials](https://google.aip.dev/auth/4117) created by Workload Identity + Federation. All file-based credentials implemented by the Google Auth library are supported. + +### Fixed + +* `certbot-dns-google` no longer requires deprecated `oauth2client` library. +* Certbot will no longer try to invoke plugins which do not subclass from the proper + `certbot.interfaces.{Installer,Authenticator}` interface (e.g. `certbot -i standalone` + will now be ignored). See [GH-9664](https://github.com/certbot/certbot/issues/9664). + +More details about these changes can be found on our GitHub repo. + +## 2.5.0 - 2023-04-04 + +### Added + +* `acme.messages.OrderResource` now supports being round-tripped + through JSON +* acme.client.ClientV2 now provides separate `begin_finalization` + and `poll_finalization` methods, in addition to the existing + `finalize_order` method. + +### Changed + +* `--dns-route53-propagation-seconds` is now deprecated. The Route53 plugin relies on the + [GetChange API](https://docs.aws.amazon.com/Route53/latest/APIReference/API_GetChange.html) + to determine if a DNS update is complete. The flag has never had any effect and will be + removed in a future version of Certbot. +* Packaged tests for all Certbot components besides josepy were moved inside + the `_internal/tests` module. + +### Fixed + +* Fixed `renew` sometimes not preserving the key type of RSA certificates. + * Users who upgraded from Certbot =v2.0.0 may + have had their RSA certificates inadvertently changed to ECDSA certificates. If desired, + the key type may be changed back to RSA. See the [User Guide](https://eff-certbot.readthedocs.io/en/stable/using.html#changing-a-certificate-s-key-type). +* Deprecated flags were inadvertently not printing warnings since v1.16.0. This is now fixed. + +More details about these changes can be found on our GitHub repo. + +## 2.4.0 - 2023-03-07 + +### Added + +* We deprecated support for the update_symlinks command. Support will be removed in a following + version of Certbot. + +### Changed + +* Docker build and deploy scripts now generate multiarch manifests for non-architecture-specific tags, instead of defaulting to amd64 images. + +### Fixed + +* Reverted [#9475](https://github.com/certbot/certbot/pull/9475) due to a performance regression in large nginx deployments. + +More details about these changes can be found on our GitHub repo. + +## 2.3.0 - 2023-02-14 + +### Added + +* Allow a user to modify the configuration of a certificate without renewing it using the new `reconfigure` subcommand. See `certbot help reconfigure` for details. +* `certbot show_account` now displays the [ACME Account Thumbprint](https://datatracker.ietf.org/doc/html/rfc8555#section-8.1). + +### Changed + +* Certbot will no longer save previous CSRs and certificate private keys to `/etc/letsencrypt/csr` and `/etc/letsencrypt/keys`, respectively. These directories may be safely deleted. +* Certbot will now only keep the current and 5 previous certificates in the `/etc/letsencrypt/archive` directory for each certificate lineage. Any prior certificates will be automatically deleted upon renewal. This number may be further lowered in future releases. + * As always, users should only reference the certificate files within `/etc/letsencrypt/live` and never use `/etc/letsencrypt/archive` directly. See [Where are my certificates?](https://eff-certbot.readthedocs.io/en/stable/using.html#where-are-my-certificates) in the Certbot User Guide. +* `certbot.configuration.NamespaceConfig.key_dir` and `.csr_dir` are now deprecated. +* All Certbot components now require `pytest` to run tests. + +### Fixed + +* Fixed a crash when registering an account with BuyPass' ACME server. +* Fixed a bug where Certbot would crash with `AttributeError: can't set attribute` on ACME server errors in Python 3.11. See [GH #9539](https://github.com/certbot/certbot/issues/9539). + +More details about these changes can be found on our GitHub repo. + +## 2.2.0 - 2023-01-11 + +### Added + +* + +### Changed + +* Certbot will no longer respect very long challenge polling intervals, which may be suggested + by some ACME servers. Certbot will continue to wait up to 90 seconds by default, or up to a + total of 30 minutes if requested by the server via `Retry-After`. + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 1.32.2 - 2022-12-16 + +### Fixed + +* Our snaps and Docker images were rebuilt to include updated versions of our dependencies. + +This release was not pushed to PyPI since those packages were unaffected. + +More details about these changes can be found on our GitHub repo. + +## 2.1.1 - 2022-12-15 + +### Fixed + +* Our snaps, Docker images, and Windows installer were rebuilt to include updated versions of our dependencies. + +This release was not pushed to PyPI since those packages were unaffected. + +More details about these changes can be found on our GitHub repo. + +## 2.1.0 - 2022-12-07 + +### Added + +* + +### Changed + +* + +### Fixed + +* Interfaces which plugins register themselves as implementing without inheriting from them now show up in `certbot plugins` output. +* `IPluginFactory`, `IPlugin`, `IAuthenticator` and `IInstaller` have been re-added to + `certbot.interfaces`. + - This is to fix compatibility with a number of third-party DNS plugins which may + have started erroring with `AttributeError` in Certbot v2.0.0. + - Plugin authors can find more information about Certbot 2.x compatibility + [here](https://github.com/certbot/certbot/wiki/Certbot-v2.x-Plugin-Compatibility). +* A bug causing our certbot-apache tests to crash on some systems has been resolved. + +More details about these changes can be found on our GitHub repo. + +## 1.32.1 - 2022-12-05 + +### Fixed + +* Our snaps and docker images were rebuilt to include updated versions of our dependencies. + +This release was not pushed to PyPI since those packages were unaffected. + +More details about these changes can be found on our GitHub repo. + +## 2.0.0 - 2022-11-21 + +### Added + +* Support for Python 3.11 was added to Certbot and all of its components. +* `acme.challenges.HTTP01Response.simple_verify` now accepts a timeout argument which defaults to 30 that causes the verification request to timeout after that many seconds. + +### Changed + +* The default key type for new certificates is now ECDSA `secp256r1` (P-256). It was previously RSA 2048-bit. Existing certificates are not affected. +* The Apache plugin no longer supports Apache 2.2. +* `acme` and Certbot no longer support versions of ACME from before the RFC 8555 standard. +* `acme` and Certbot no longer support the old `urn:acme:error:` ACME error prefix. +* Removed the deprecated `certbot-dns-cloudxns` plugin. +* Certbot will now error if a certificate has `--reuse-key` set and a conflicting `--key-type`, `--key-size` or `--elliptic-curve` is requested on the CLI. Use `--new-key` to change the key while preserving `--reuse-key`. +* 3rd party plugins no longer support the `dist_name:plugin_name` format on the CLI and in configuration files. Use the shorter `plugin_name` format. +* `acme.client.Client`, `acme.client.ClientBase`, `acme.client.BackwardsCompatibleClientV2`, `acme.mixins`, `acme.client.DER_CONTENT_TYPE`, `acme.fields.Resource`, `acme.fields.resource`, `acme.magic_typing`, `acme.messages.OLD_ERROR_PREFIX`, `acme.messages.Directory.register`, `acme.messages.Authorization.resolved_combinations`, `acme.messages.Authorization.combinations` have been removed. +* `acme.messages.Directory` now only supports lookups by the exact resource name string in the ACME directory (e.g. `directory['newOrder']`). +* Removed the deprecated `source_address` argument for `acme.client.ClientNetwork`. +* The `zope` based interfaces in `certbot.interfaces` have been removed in favor of the `abc` based interfaces found in the same module. +* Certbot no longer depends on `zope`. +* Removed deprecated function `certbot.util.get_strict_version`. +* Removed deprecated functions `certbot.crypto_util.init_save_csr`, `certbot.crypto_util.init_save_key`, + and `certbot.compat.misc.execute_command` +* The attributes `FileDisplay`, `NoninteractiveDisplay`, `SIDE_FRAME`, `input_with_timeout`, `separate_list_input`, `summarize_domain_list`, `HELP`, and `ESC` from `certbot.display.util` have been removed. +* Removed deprecated functions `certbot.tests.util.patch_get_utility*`. Plugins should now + patch `certbot.display.util` themselves in their tests or use + `certbot.tests.util.patch_display_util` as a temporary workaround. +* Certbot's test API under `certbot.tests` now uses `unittest.mock` instead of the 3rd party `mock` library. + +### Fixed + +* Fixes a bug where the certbot working directory has unusably restrictive permissions on systems with stricter default umasks. +* Requests to subscribe to the EFF mailing list now time out after 60 seconds. + +We plan to slowly roll out Certbot 2.0 to all of our snap users in the coming months. If you want to use the Certbot 2.0 snap now, please follow the instructions at https://community.letsencrypt.org/t/certbot-2-0-beta-call-for-testing/185945. + +More details about these changes can be found on our GitHub repo. + +## 1.32.0 - 2022-11-08 + +### Added + +* + +### Changed + +* DNS RFC2136 module now uses the TSIG key to check for an authoritative SOA record. Helps the use of split-horizon and multiple views in BIND9 using the key in an ACL to determine which view to use. + +### Fixed + +* CentOS 9 and other RHEL-derived OSes now correctly use httpd instead of apachectl for + various Apache-related commands + +More details about these changes can be found on our GitHub repo. + +## 1.31.0 - 2022-10-04 + +### Added + +* + +### Changed + +* If Certbot exits before setting up its usual log files, the temporary directory created to save logging information will begin with the name `certbot-log-` rather than a generic name. This should not be considered a [stable aspect of Certbot](https://certbot.eff.org/docs/compatibility.html) and may change again in the future. + +### Fixed + +* Fixed an incompatibility in the certbot-dns-cloudflare plugin and the Cloudflare library + which was introduced in the Cloudflare library version 2.10.1. The library would raise + an error if a token was specified in the Certbot `--dns-cloudflare-credentials` file as + well as the `cloudflare.cfg` configuration file of the Cloudflare library. + +More details about these changes can be found on our GitHub repo. + +## 1.30.0 - 2022-09-07 + +### Added + +* + +### Changed + +* `acme.client.ClientBase`, `acme.messages.Authorization.resolved_combinations`, + `acme.messages.Authorization.combinations`, `acme.mixins`, `acme.fields.resource`, + and `acme.fields.Resource` are deprecated and will be removed in a future release. +* `acme.messages.OLD_ERROR_PREFIX` (`urn:acme:error:`) is deprecated and support for + the old ACME error prefix in Certbot will be removed in the next major release of + Certbot. +* `acme.messages.Directory.register` is deprecated and will be removed in the next + major release of Certbot. Furthermore, `.Directory` will only support lookups + by the exact resource name string in the ACME directory (e.g. `directory['newOrder']`). +* The `certbot-dns-cloudxns` plugin is now deprecated and will be removed in the + next major release of Certbot. +* The `source_address` argument for `acme.client.ClientNetwork` is deprecated + and support for it will be removed in the next major release. +* Add UI text suggesting users create certs for multiple domains, when possible + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 1.29.0 - 2022-07-05 + +### Added + +* Updated Windows installer to be signed and trusted in Windows + +### Changed + +* `--allow-subset-of-names` will now additionally retry in cases where domains are rejected while creating or finalizing orders. This requires subproblem support from the ACME server. + +### Fixed + +* The `show_account` subcommand now uses the "newAccount" ACME endpoint to fetch the account + data, so it doesn't rely on the locally stored account URL. This fixes situations where Certbot + would use old ACMEv1 registration info with non-functional account URLs. + +* The generated Certificate Signing Requests are now generated as version 1 instead of version 3. This resolves situations in where strict enforcement of PKCS#10 meant that CSRs that were generated as version 3 were rejected. + +More details about these changes can be found on our GitHub repo. + +## 1.28.0 - 2022-06-07 + +### Added + +* Updated Apache/NGINX TLS configs to document contents are based on ssl-config.mozilla.org + + +### Changed + +* A change to order finalization has been made to the `acme` module and Certbot: + - An order's `certificate` field will only be processed if the order's `status` is `valid`. + - An order's `error` field will only be processed if the order's `status` is `invalid`. + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 1.27.0 - 2022-05-03 + +### Added + +* Added support for RFC8555 subproblems to our acme library. + +### Changed + +* The PGP key `F2871B4152AE13C49519111F447BF683AA3B26C3` was added as an + additional trusted key to sign our PyPI packages +* When `certonly` is run with an installer specified (e.g. `--nginx`), + `certonly` will now also run `restart` for that installer + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 1.26.0 - 2022-04-05 + +### Added + +* Added `--new-key`. When renewing or replacing a certificate that has `--reuse-key` + set, it will force a new private key to be generated, one time. + + As before, `--reuse-key` and `--no-reuse-key` can be used to enable and disable key + reuse. + +### Changed + +* The default propagation timeout for the OVH DNS plugin (`--dns-ovh-propagation-seconds`) + has been increased from 30 seconds to 120 seconds, based on user feedback. + +### Fixed + +* Certbot for Windows has been upgraded to use Python 3.9.11, in response to + https://www.openssl.org/news/secadv/20220315.txt. +* Previously, when Certbot was in the process of registering a new ACME account + and the ACME server did not present any Terms of Service, the user was asked to + agree with a non-existent Terms of Service ("None"). This bug is now fixed, so + that if an ACME server does not provide any Terms of Service to agree with, the + user is not asked to agree to a non-existent Terms of Service any longer. +* If account registration fails, Certbot did not relay the error from the ACME server + back to the user. This is now fixed: the error message from the ACME server is now + presented to the user when account registration fails. + +More details about these changes can be found on our GitHub repo. + +## 1.25.0 - 2022-03-16 + +### Added + +* + +### Changed + +* Dropped 32 bit support for the Windows beta installer +* Windows beta installer is now distributed as "certbot-beta-installer-win_amd64.exe". + Users of the Windows beta should uninstall the old version before running this. +* Added a check whether OCSP stapling is supported by the installer when requesting a + certificate with the `run` subcommand in combination with the `--must-staple` option. + If the installer does not support OCSP and the `--must-staple` option is used, Certbot + will raise an error and quit. +* Certbot and its acme module now depend on josepy>=1.13.0 due to better type annotation + support. + +### Fixed + +* Updated dependencies to use new version of cryptography that uses OpenSSL 1.1.1n, in + response to https://www.openssl.org/news/secadv/20220315.txt. + +More details about these changes can be found on our GitHub repo. + +## 1.24.0 - 2022-03-01 + +### Added + +* When the `--debug-challenges` option is used in combination with `-v`, Certbot + now displays the challenge URLs (for `http-01` challenges) or FQDNs (for + `dns-01` challenges) and their expected return values. +* + +### Changed + +* Support for Python 3.6 was removed. +* All Certbot components now require setuptools>=41.6.0. +* The acme library now requires requests>=2.20.0. +* Certbot and its acme library now require pytz>=2019.3. +* certbot-nginx now requires pyparsing>=2.2.1. +* certbot-dns-route53 now requires boto3>=1.15.15. + +### Fixed + +* Nginx plugin now checks included files for the singleton server_names_hash_bucket_size directive. +* + +More details about these changes can be found on our GitHub repo. + +## 1.23.0 - 2022-02-08 + +### Added + +* Added `show_account` subcommand, which will fetch the account information + from the ACME server and show the account details (account URL and, if + applicable, email address or addresses) +* We deprecated support for Python 3.6 in Certbot and its ACME library. + Support for Python 3.6 will be removed in the next major release of Certbot. + +### Changed + +* + +### Fixed + +* GCP Permission list for certbot-dns-google in plugin documentation +* dns-digitalocean used the SOA TTL for newly created records, rather than 30 seconds. +* Revoking a certificate based on an ECDSA key can now be done with `--key-path`. + See [GH #8569](https://github.com/certbot/certbot/issues/8569). + +More details about these changes can be found on our GitHub repo. + +## 1.22.0 - 2021-12-07 + +### Added + +* Support for Python 3.10 was added to Certbot and all of its components. +* The function certbot.util.parse_loose_version was added to parse version + strings in the same way as the now deprecated distutils.version.LooseVersion + class from the Python standard library. +* Added `--issuance-timeout`. This option specifies how long (in seconds) Certbot will wait + for the server to issue a certificate. + +### Changed + +* The function certbot.util.get_strict_version was deprecated and will be + removed in a future release. + +### Fixed + +* Fixed an issue on Windows where the `web.config` created by Certbot would sometimes + conflict with preexisting configurations (#9088). +* Fixed an issue on Windows where the `webroot` plugin would crash when multiple domains + had the same webroot. This affected Certbot 1.21.0. + +More details about these changes can be found on our GitHub repo. + +## 1.21.0 - 2021-11-02 + +### Added + +* Certbot will generate a `web.config` file on Windows in the challenge path + when the `webroot` plugin is used, if one does not exist. This `web.config` file + lets IIS serve challenge files while they do not have an extension. + +### Changed + +* We changed the PGP key used to sign the packages we upload to PyPI. Going + forward, releases will be signed with one of three different keys. All of + these keys are available on major key servers and signed by our previous PGP + key. The fingerprints of these new keys are: + * BF6BCFC89E90747B9A680FD7B6029E8500F7DB16 + * 86379B4F0AF371B50CD9E5FF3402831161D1D280 + * 20F201346BF8F3F455A73F9A780CC99432A28621 + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 1.20.0 - 2021-10-05 + +### Added + +* Added `--no-reuse-key`. This remains the default behavior, but the flag may be + useful to unset the `--reuse-key` option on existing certificates. + +### Changed + +* + +### Fixed + +* The certbot-dns-rfc2136 plugin in Certbot 1.19.0 inadvertently had an implicit + dependency on `dnspython>=2.0`. This has been relaxed to `dnspython>=1.15.0`. + +More details about these changes can be found on our GitHub repo. + +## 1.19.0 - 2021-09-07 + +### Added + +* The certbot-dns-rfc2136 plugin always assumed the use of an IP address as the + target server, but this was never checked. Until now. The plugin raises an error + if the configured target server is not a valid IPv4 or IPv6 address. +* Our acme library now supports requesting certificates for IP addresses. + This feature is still unsupported by Certbot and Let's Encrypt. + +### Changed + +* Several attributes in `certbot.display.util` module are deprecated and will + be removed in a future release of Certbot. Any import of these attributes will + emit a warning to prepare the transition for developers. +* `zope` based interfaces in `certbot.interfaces` module are deprecated and will + be removed in a future release of Certbot. Any import of these interfaces will + emit a warning to prepare the transition for developers. +* We removed the dependency on `chardet` from our acme library. Except for when + downloading a certificate in an alternate format, our acme library now + assumes all server responses are UTF-8 encoded which is required by RFC 8555. + +### Fixed + +* Fixed parsing of `Define`d values in the Apache plugin to allow for `=` in the value. +* Fixed a relatively harmless crash when issuing a certificate with `--quiet`/`-q`. + +More details about these changes can be found on our GitHub repo. + +## 1.18.0 - 2021-08-03 + +### Added + +* New functions that Certbot plugins can use to interact with the user have + been added to `certbot.display.util`. We plan to deprecate using `IDisplay` + with `zope` in favor of these new functions in the future. +* The `Plugin`, `Authenticator` and `Installer` classes are added to + `certbot.interfaces` module as alternatives to Certbot's current `zope` based + plugin interfaces. The API of these interfaces is identical, but they are + based on Python's `abc` module instead of `zope`. Certbot will continue to + detect plugins that implement either interface, but we plan to drop support + for `zope` based interfaces in a future version of Certbot. +* The class `certbot.configuration.NamespaceConfig` is added to the Certbot's + public API. + +### Changed + +* When self-validating HTTP-01 challenges using + acme.challenges.HTTP01Response.simple_verify, we now assume that the response + is composed of only ASCII characters. Previously we were relying on the + default behavior of the requests library which tries to guess the encoding of + the response which was error prone. +* `acme`: the `.client.Client` and `.client.BackwardsCompatibleClientV2` classes + are now deprecated in favor of `.client.ClientV2`. +* The `certbot.tests.patch_get_utility*` functions have been deprecated. + Plugins should now patch `certbot.display.util` themselves in their tests or + use `certbot.tests.util.patch_display_util` as a temporary workaround. +* In order to simplify the transition to Certbot's new plugin interfaces, the + classes `Plugin` and `Installer` in `certbot.plugins.common` module and + `certbot.plugins.dns_common.DNSAuthenticator` now implement Certbot's new + plugin interfaces. The Certbot plugins based on these classes are now + automatically detected as implementing these interfaces. +* We added a dependency on `chardet` to our acme library so that it will be + used over `charset_normalizer` in newer versions of `requests`. + +### Fixed + +* The Apache authenticator no longer crashes with "Unable to insert label" + when encountering a completely empty vhost. This issue affected Certbot 1.17.0. +* Users of the Certbot snap on Debian 9 (Stretch) should no longer encounter an + "access denied" error when installing DNS plugins. + +More details about these changes can be found on our GitHub repo. + +## 1.17.0 - 2021-07-06 + +### Added + +* Add Void Linux overrides for certbot-apache. + +### Changed + +* We changed how dependencies are specified between Certbot packages. For this + and future releases, higher level Certbot components will require that lower + level components are the same version or newer. More specifically, version X + of the Certbot package will now always require acme>=X and version Y of a + plugin package will always require acme>=Y and certbot=>Y. Specifying + dependencies in this way simplifies testing and development. +* The Apache authenticator now always configures virtual hosts which do not have + an explicit `ServerName`. This should make it work more reliably with the + default Apache configuration in Debian-based environments. + +### Fixed + +* When we increased the logging level on our nginx "Could not parse file" message, + it caused a previously-existing inability to parse empty files to become more + visible. We have now added the ability to correctly parse empty files, so that + message should only show for more significant errors. + +More details about these changes can be found on our GitHub repo. + +## 1.16.0 - 2021-06-01 + +### Added + +* + +### Changed + +* DNS plugins based on lexicon now require dns-lexicon >= v3.1.0 +* Use UTF-8 encoding for renewal configuration files +* Windows installer now cleans up old Certbot dependency packages + before installing the new ones to avoid version conflicts. +* This release contains a substantial command-line UX overhaul, + based on previous user research. The main goal was to streamline + and clarify output. If you would like to see more verbose output, use + the -v or -vv flags. UX improvements are an iterative process and + the Certbot team welcomes constructive feedback. +* Functions `certbot.crypto_util.init_save_key` and `certbot.crypto_util.init_save_csr`, + whose behaviors rely on the global Certbot `config` singleton, are deprecated and will + be removed in a future release. Please use `certbot.crypto_util.generate_key` and + `certbot.crypto_util.generate_csr` instead. + +### Fixed + +* Fix TypeError due to incompatibility with lexicon >= v3.6.0 +* Installers (e.g. nginx, Apache) were being restarted unnecessarily after dry-run renewals. +* Colors and bold text should properly render in all supported versions of Windows. + +More details about these changes can be found on our GitHub repo. + +## 1.15.0 - 2021-05-04 + +### Added + +* + +### Changed + +* + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 1.14.0 - 2021-04-06 + +### Added + +* + +### Changed + +* certbot-auto no longer checks for updates on any operating system. +* The module `acme.magic_typing` is deprecated and will be removed in a future release. + Please use the built-in module `typing` instead. +* The DigitalOcean plugin now creates TXT records for the DNS-01 challenge with a lower 30s TTL. + +### Fixed + +* Don't output an empty line for a hidden certificate when `certbot certificates` is being used + in combination with `--cert-name` or `-d`. + +More details about these changes can be found on our GitHub repo. + +## 1.13.0 - 2021-03-02 + +### Added + +* + +### Changed + +* CLI flags `--os-packages-only`, `--no-self-upgrade`, `--no-bootstrap` and `--no-permissions-check`, + which are related to certbot-auto, are deprecated and will be removed in a future release. +* Certbot no longer conditionally depends on an external mock module. Certbot's + test API will continue to use it if it is available for backwards + compatibility, however, this behavior has been deprecated and will be removed + in a future release. +* The acme library no longer depends on the `security` extras from `requests` + which was needed to support SNI in TLS requests when using old versions of + Python 2. +* Certbot and all of its components no longer depend on the library `six`. +* The update of certbot-auto itself is now disabled on all RHEL-like systems. +* When revoking a certificate by `--cert-name`, it is no longer necessary to specify the `--server` + if the certificate was obtained from a non-default ACME server. +* The nginx authenticator now configures all matching HTTP and HTTPS vhosts for the HTTP-01 + challenge. It is now compatible with external HTTPS redirection by a CDN or load balancer. + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 1.12.0 - 2021-02-02 + +### Added + +* + +### Changed + +* The `--preferred-chain` flag now only checks the Issuer Common Name of the + topmost (closest to the root) certificate in the chain, instead of checking + every certificate in the chain. + See [#8577](https://github.com/certbot/certbot/issues/8577). +* Support for Python 2 has been removed. +* In previous releases, we caused certbot-auto to stop updating its Certbot + installation. In this release, we are beginning to disable updates to the + certbot-auto script itself. This release includes Amazon Linux users, and all + other systems that are not based on Debian or RHEL. We plan to make this + change to the certbot-auto script for all users in the coming months. + +### Fixed + +* Fixed the apache component on openSUSE Tumbleweed which no longer provides + an apache2ctl symlink and uses apachectl instead. +* Fixed a typo in `certbot/crypto_util.py` causing an error upon attempting `secp521r1` key generation + +More details about these changes can be found on our GitHub repo. + +## 1.11.0 - 2021-01-05 + +### Added + +* + +### Changed + +* We deprecated support for Python 2 in Certbot and its ACME library. + Support for Python 2 will be removed in the next planned release of Certbot. +* certbot-auto was deprecated on all systems. For more information about this + change, see + https://community.letsencrypt.org/t/certbot-auto-no-longer-works-on-debian-based-systems/139702/7. +* We deprecated support for Apache 2.2 in the certbot-apache plugin and it will + be removed in a future release of Certbot. + +### Fixed + +* The Certbot snap no longer loads packages installed via `pip install --user`. This + was unintended and DNS plugins should be installed via `snap` instead. +* `certbot-dns-google` would sometimes crash with HTTP 409/412 errors when used with very large zones. See [#6036](https://github.com/certbot/certbot/issues/6036). +* `certbot-dns-google` would sometimes crash with an HTTP 412 error if preexisting records had an unexpected TTL, i.e.: different than Certbot's default TTL for this plugin. See [#8551](https://github.com/certbot/certbot/issues/8551). + +More details about these changes can be found on our GitHub repo. + +## 1.10.1 - 2020-12-03 + +### Fixed + +* Fixed a bug in `certbot.util.add_deprecated_argument` that caused the + deprecated `--manual-public-ip-logging-ok` flag to crash Certbot in some + scenarios. + +More details about these changes can be found on our GitHub repo. + +## 1.10.0 - 2020-12-01 + +### Added + +* Added timeout to DNS query function calls for dns-rfc2136 plugin. +* Confirmation when deleting certificates +* CLI flag `--key-type` has been added to specify 'rsa' or 'ecdsa' (default 'rsa'). +* CLI flag `--elliptic-curve` has been added which takes an NIST/SECG elliptic curve. Any of + `secp256r1`, `secp384r1` and `secp521r1` are accepted values. +* The command `certbot certificates` lists the which type of the private key that was used + for the private key. +* Support for Python 3.9 was added to Certbot and all of its components. + +### Changed + +* certbot-auto was deprecated on Debian based systems. +* CLI flag `--manual-public-ip-logging-ok` is now a no-op, generates a + deprecation warning, and will be removed in a future release. + +### Fixed + +* Fixed a Unicode-related crash in the nginx plugin when running under Python 2. + +More details about these changes can be found on our GitHub repo. + +## 1.9.0 - 2020-10-06 + +### Added + +* `--preconfigured-renewal` flag, for packager use only. + See the [packaging guide](https://certbot.eff.org/docs/packaging.html). + +### Changed + +* certbot-auto was deprecated on all systems except for those based on Debian or RHEL. +* Update the packaging instructions to promote usage of `python -m pytest` to test Certbot + instead of the deprecated `python setup.py test` setuptools approach. +* Reduced CLI logging when reloading nginx, if it is not running. +* Reduced CLI logging when handling some kinds of errors. + +### Fixed + +* Fixed `server_name` case-sensitivity in the nginx plugin. +* The minimum version of the `acme` library required by Certbot was corrected. + In the previous release, Certbot said it required `acme>=1.6.0` when it + actually required `acme>=1.8.0` to properly support removing contact + information from an ACME account. +* Upgraded the version of httplib2 used in our snaps and Docker images to add + support for proxy environment variables and fix the plugin for Google Cloud + DNS. + +More details about these changes can be found on our GitHub repo. + +## 1.8.0 - 2020-09-08 + +### Added + +* Added the ability to remove email and phone contact information from an account + using `update_account --register-unsafely-without-email` + +### Changed + +* Support for Python 3.5 has been removed. + +### Fixed + +* The problem causing the Apache plugin in the Certbot snap on ARM systems to + fail to load the Augeas library it depends on has been fixed. +* The `acme` library can now tell the ACME server to clear contact information by passing an empty + `tuple` to the `contact` field of a `Registration` message. +* Fixed the `*** stack smashing detected ***` error in the Certbot snap on some systems. + +More details about these changes can be found on our GitHub repo. + +## 1.7.0 - 2020-08-04 + +### Added + +* Third-party plugins can be used without prefix (`plugin_name` instead of `dist_name:plugin_name`): + this concerns the plugin name, CLI flags, and keys in credential files. + The prefixed form is still supported but is deprecated, and will be removed in a future release. +* Added `--nginx-sleep-seconds` (default `1`) for environments where nginx takes a long time to reload. + +### Changed + +* The Linode DNS plugin now waits 120 seconds for DNS propagation, instead of 1200, + due to https://www.linode.com/blog/linode/linode-turns-17/ +* We deprecated support for Python 3.5 in Certbot and its ACME library. + Support for Python 3.5 will be removed in the next major release of Certbot. + +### Fixed + + +More details about these changes can be found on our GitHub repo. + +## 1.6.0 - 2020-07-07 + +### Added + +* Certbot snaps are now available for the arm64 and armhf architectures. +* Add minimal code to run Nginx plugin on NetBSD. +* Make Certbot snap find externally snapped plugins +* Function `certbot.compat.filesystem.umask` is a drop-in replacement for `os.umask` + implementing umask for both UNIX and Windows systems. +* Support for alternative certificate chains in the `acme` module. +* Added `--preferred-chain `. If a CA offers multiple certificate chains, + it may be used to indicate to Certbot which chain should be preferred. + * e.g. `--preferred-chain "DST Root CA X3"` + +### Changed + +* Allow session tickets to be disabled in Apache when mod_ssl is statically linked. +* Generalize UI warning message on renewal rate limits +* Certbot behaves similarly on Windows to on UNIX systems regarding umask, and + the umask `022` is applied by default: all files/directories are not writable by anyone + other than the user running Certbot and the system/admin users. +* Read acmev1 Let's Encrypt server URL from renewal config as acmev2 URL to prepare + for impending acmev1 deprecation. + +### Fixed + +* Cloudflare API Tokens may now be restricted to individual zones. +* Don't use `StrictVersion`, but `LooseVersion` to check version requirements with setuptools, + to fix some packaging issues with libraries respecting PEP404 for version string, + with doesn't match `StrictVersion` requirements. +* Certbot output doesn't refer to SSL Labs due to confusing scoring behavior. +* Fix paths when calling to programs outside of the Certbot Snap, fixing the apache and nginx + plugins on, e.g., CentOS 7. + +More details about these changes can be found on our GitHub repo. + +## 1.5.0 - 2020-06-02 + +### Added + +* Require explicit confirmation of snap plugin permissions before connecting. + +### Changed + +* Improved error message in apache installer when mod_ssl is not available. + +### Fixed + +* Add support for OCSP responses which use a public key hash ResponderID, fixing + interoperability with Sectigo CAs. +* Fix TLS-ALPN test that fails when run with newer versions of OpenSSL. + +More details about these changes can be found on our GitHub repo. + +## 1.4.0 - 2020-05-05 + +### Added + +* Turn off session tickets for apache plugin by default when appropriate. +* Added serial number of certificate to the output of `certbot certificates` +* Expose two new environment variables in the authenticator and cleanup scripts used by + the `manual` plugin: `CERTBOT_REMAINING_CHALLENGES` is equal to the number of challenges + remaining after the current challenge, `CERTBOT_ALL_DOMAINS` is a comma-separated list + of all domains challenged for the current certificate. +* Added TLS-ALPN-01 challenge support in the `acme` library. Support of this + challenge in the Certbot client is planned to be added in a future release. +* Added minimal proxy support for OCSP verification. +* On Windows, hooks are now executed in a Powershell shell instead of a CMD shell, + allowing both `*.ps1` and `*.bat` as valid scripts for Certbot. + +### Changed + +* Reorganized error message when a user entered an invalid email address. +* Stop asking interactively if the user would like to add a redirect. +* `mock` dependency is now conditional on Python 2 in all of our packages. +* Deprecate certbot-auto on Gentoo, macOS, and FreeBSD. +* Allow existing but empty archive and live dir to be used when creating new lineage. + +### Fixed + +* When using an RFC 8555 compliant endpoint, the `acme` library no longer sends the + `resource` field in any requests or the `type` field when responding to challenges. +* Fix nginx plugin crash when non-ASCII configuration file is being read (instead, + the user will be warned that UTF-8 must be used). +* Fix hanging OCSP queries during revocation checking - added a 10 second timeout. +* Standalone servers now have a default socket timeout of 30 seconds, fixing + cases where an idle connection can cause the standalone plugin to hang. +* Parsing of the RFC 8555 application/pem-certificate-chain now tolerates CRLF line + endings. This should fix interoperability with Buypass' services. + +More details about these changes can be found on our GitHub repo. + +## 1.3.0 - 2020-03-03 + +### Added + +* Added certbot.ocsp Certbot's API. The certbot.ocsp module can be used to + determine the OCSP status of certificates. +* Don't verify the existing certificate in HTTP01Response.simple_verify, for + compatibility with the real-world ACME challenge checks. +* Added support for `$hostname` in nginx `server_name` directive + +### Changed + +* Certbot will now renew certificates early if they have been revoked according + to OCSP. +* Fix acme module warnings when response Content-Type includes params (e.g. charset). +* Fixed issue where webroot plugin would incorrectly raise `Read-only file system` + error when creating challenge directories (issue #7165). + +### Fixed + +* Fix Apache plugin to use less restrictive umask for making the challenge directory when a restrictive umask was set when certbot was started. + +More details about these changes can be found on our GitHub repo. + +## 1.2.0 - 2020-02-04 + +### Added + +* Added support for Cloudflare's limited-scope API Tokens + +### Changed + +* Add directory field to error message when field is missing. +* If MD5 hasher is not available, try it in non-security mode (fix for FIPS systems) -- [#1948](https://github.com/certbot/certbot/issues/1948) +* Disable old SSL versions and ciphersuites and remove `SSLCompression off` setting to follow Mozilla recommendations in Apache. +* Remove ECDHE-RSA-AES128-SHA from NGINX ciphers list now that Windows 2008 R2 and Windows 7 are EOLed +* Support for Python 3.4 has been removed. + +### Fixed + +* Fix collections.abc imports for Python 3.9. + +More details about these changes can be found on our GitHub repo. + +## 1.1.0 - 2020-01-14 + +### Added + +* + +### Changed + +* Removed the fallback introduced with 0.34.0 in `acme` to retry a POST-as-GET + request as a GET request when the targeted ACME CA server seems to not support + POST-as-GET requests. +* certbot-auto no longer supports architectures other than x86_64 on RHEL 6 + based systems. Existing certbot-auto installations affected by this will + continue to work, but they will no longer receive updates. To install a + newer version of Certbot on these systems, you should update your OS. +* Support for Python 3.4 in Certbot and its ACME library is deprecated and will be + removed in the next release of Certbot. certbot-auto users on x86_64 systems running + RHEL 6 or derivatives will be asked to enable Software Collections (SCL) repository + so Python 3.6 can be installed. certbot-auto can enable the SCL repo for you on CentOS 6 + while users on other RHEL 6 based systems will be asked to do this manually. + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 1.0.0 - 2019-12-03 + +### Added + +* + +### Removed + +* The `docs` extras for the `certbot-apache` and `certbot-nginx` packages + have been removed. + +### Changed + +* certbot-auto has deprecated support for systems using OpenSSL 1.0.1 that are + not running on x86-64. This primarily affects RHEL 6 based systems. +* Certbot's `config_changes` subcommand has been removed +* `certbot.plugins.common.TLSSNI01` has been removed. +* Deprecated attributes related to the TLS-SNI-01 challenge in + `acme.challenges` and `acme.standalone` + have been removed. +* The functions `certbot.client.view_config_changes`, + `certbot.main.config_changes`, + `certbot.plugins.common.Installer.view_config_changes`, + `certbot.reverter.Reverter.view_config_changes`, and + `certbot.util.get_systemd_os_info` have been removed +* Certbot's `register --update-registration` subcommand has been removed +* When possible, default to automatically configuring the webserver so all requests + redirect to secure HTTPS access. This is mostly relevant when running Certbot + in non-interactive mode. Previously, the default was to not redirect all requests. + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 0.40.1 - 2019-11-05 + +### Changed + +* Added back support for Python 3.4 to Certbot components and certbot-auto due + to a bug when requiring Python 2.7 or 3.5+ on RHEL 6 based systems. + +More details about these changes can be found on our GitHub repo. + +## 0.40.0 - 2019-11-05 + +### Added + +* + +### Changed + +* We deprecated support for Python 3.4 in Certbot and its ACME library. Support + for Python 3.4 will be removed in the next major release of Certbot. + certbot-auto users on RHEL 6 based systems will be asked to enable Software + Collections (SCL) repository so Python 3.6 can be installed. certbot-auto can + enable the SCL repo for you on CentOS 6 while users on other RHEL 6 based + systems will be asked to do this manually. +* `--server` may now be combined with `--dry-run`. Certbot will, as before, use the + staging server instead of the live server when `--dry-run` is used. +* `--dry-run` now requests fresh authorizations every time, fixing the issue + where it was prone to falsely reporting success. +* Updated certbot-dns-google to depend on newer versions of + google-api-python-client and oauth2client. +* The OS detection logic again uses distro library for Linux OSes +* certbot.plugins.common.TLSSNI01 has been deprecated and will be removed in a + future release. +* CLI flags --tls-sni-01-port and --tls-sni-01-address have been removed. +* The values tls-sni and tls-sni-01 for the --preferred-challenges flag are no + longer accepted. +* Removed the flags: `--agree-dev-preview`, `--dialog`, and `--apache-init-script` +* acme.standalone.BaseRequestHandlerWithLogging and + acme.standalone.simple_tls_sni_01_server have been deprecated and will be + removed in a future release of the library. +* certbot-dns-rfc2136 now use TCP to query SOA records. + +### Fixed + +* + +More details about these changes can be found on our GitHub repo. + +## 0.39.0 - 2019-10-01 + +### Added + +* Support for Python 3.8 was added to Certbot and all of its components. +* Support for CentOS 8 was added to certbot-auto. + +### Changed + +* Don't send OCSP requests for expired certificates +* Return to using platform.linux_distribution instead of distro.linux_distribution in OS fingerprinting for Python < 3.8 +* Updated the Nginx plugin's TLS configuration to keep support for some versions of IE11. + +### Fixed + +* Fixed OS detection in the Apache plugin on RHEL 6. + +More details about these changes can be found on our GitHub repo. + +## 0.38.0 - 2019-09-03 + +### Added + +* Disable session tickets for Nginx users when appropriate. + +### Changed + +* If Certbot fails to rollback your server configuration, the error message + links to the Let's Encrypt forum. Change the link to the Help category now + that the Server category has been closed. +* Replace platform.linux_distribution with distro.linux_distribution as a step + towards Python 3.8 support in Certbot. + +### Fixed + +* Fixed OS detection in the Apache plugin on Scientific Linux. + +More details about these changes can be found on our GitHub repo. + +## 0.37.2 - 2019-08-21 + +* Stop disabling TLS session tickets in Nginx as it caused TLS failures on + some systems. + +More details about these changes can be found on our GitHub repo. + +## 0.37.1 - 2019-08-08 + +### Fixed + +* Stop disabling TLS session tickets in Apache as it caused TLS failures on + some systems. + +More details about these changes can be found on our GitHub repo. + +## 0.37.0 - 2019-08-07 + +### Added + +* Turn off session tickets for apache plugin by default +* acme: Authz deactivation added to `acme` module. + +### Changed + +* Follow updated Mozilla recommendations for Nginx ssl_protocols, ssl_ciphers, + and ssl_prefer_server_ciphers + +### Fixed + +* Fix certbot-auto failures on RHEL 8. + +More details about these changes can be found on our GitHub repo. + +## 0.36.0 - 2019-07-11 + +### Added + +* Turn off session tickets for nginx plugin by default +* Added missing error types from RFC8555 to acme + +### Changed + +* Support for Ubuntu 14.04 Trusty has been removed. +* Update the 'manage your account' help to be more generic. +* The error message when Certbot's Apache plugin is unable to modify your + Apache configuration has been improved. +* Certbot's config_changes subcommand has been deprecated and will be + removed in a future release. +* `certbot config_changes` no longer accepts a --num parameter. +* The functions `certbot.plugins.common.Installer.view_config_changes` and + `certbot.reverter.Reverter.view_config_changes` have been deprecated and will + be removed in a future release. + +### Fixed + +* Replace some unnecessary platform-specific line separation. + +More details about these changes can be found on our GitHub repo. + +## 0.35.1 - 2019-06-10 + +### Fixed + +* Support for specifying an authoritative base domain in our dns-rfc2136 plugin + has been removed. This feature was added in our last release but had a bug + which caused the plugin to fail so the feature has been removed until it can + be added properly. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* certbot-dns-rfc2136 + +More details about these changes can be found on our GitHub repo. + +## 0.35.0 - 2019-06-05 + +### Added + +* dns_rfc2136 plugin now supports explicitly specifying an authoritative + base domain for cases when the automatic method does not work (e.g. + Split horizon DNS) + +### Changed + +* + +### Fixed + +* Renewal parameter `webroot_path` is always saved, avoiding some regressions + when `webroot` authenticator plugin is invoked with no challenge to perform. +* Certbot now accepts OCSP responses when an explicit authorized + responder, different from the issuer, is used to sign OCSP + responses. +* Scripts in Certbot hook directories are no longer executed when their + filenames end in a tilde. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* certbot +* certbot-dns-rfc2136 + +More details about these changes can be found on our GitHub repo. + +## 0.34.2 - 2019-05-07 + +### Fixed + +* certbot-auto no longer writes a check_permissions.py script at the root + of the filesystem. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +changes in this release were to certbot-auto. + +More details about these changes can be found on our GitHub repo. + +## 0.34.1 - 2019-05-06 + +### Fixed + +* certbot-auto no longer prints a blank line when there are no permissions + problems. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +changes in this release were to certbot-auto. + +More details about these changes can be found on our GitHub repo. + +## 0.34.0 - 2019-05-01 + +### Changed + +* Apache plugin now tries to restart httpd on Fedora using systemctl if a + configuration test error is detected. This has to be done due to the way + Fedora now generates the self signed certificate files upon first + restart. +* Updated Certbot and its plugins to improve the handling of file system permissions + on Windows as a step towards adding proper Windows support to Certbot. +* Updated urllib3 to 1.24.2 in certbot-auto. +* Removed the fallback introduced with 0.32.0 in `acme` to retry a challenge response + with a `keyAuthorization` if sending the response without this field caused a + `malformed` error to be received from the ACME server. +* Linode DNS plugin now supports api keys created from their new panel + at [cloud.linode.com](https://cloud.linode.com) + +### Fixed + +* Fixed Google DNS Challenge issues when private zones exist +* Adding a warning noting that future versions of Certbot will automatically configure the + webserver so that all requests redirect to secure HTTPS access. You can control this + behavior and disable this warning with the --redirect and --no-redirect flags. +* certbot-auto now prints warnings when run as root with insecure file system + permissions. If you see these messages, you should fix the problem by + following the instructions at + https://community.letsencrypt.org/t/certbot-auto-deployment-best-practices/91979/, + however, these warnings can be disabled as necessary with the flag + --no-permissions-check. +* `acme` module uses now a POST-as-GET request to retrieve the registration + from an ACME v2 server +* Convert the tsig algorithm specified in the certbot_dns_rfc2136 configuration file to + all uppercase letters before validating. This makes the value in the config case + insensitive. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* acme +* certbot +* certbot-apache +* certbot-dns-cloudflare +* certbot-dns-cloudxns +* certbot-dns-digitalocean +* certbot-dns-dnsimple +* certbot-dns-dnsmadeeasy +* certbot-dns-gehirn +* certbot-dns-google +* certbot-dns-linode +* certbot-dns-luadns +* certbot-dns-nsone +* certbot-dns-ovh +* certbot-dns-rfc2136 +* certbot-dns-route53 +* certbot-dns-sakuracloud +* certbot-nginx + +More details about these changes can be found on our GitHub repo. + +## 0.33.1 - 2019-04-04 + +### Fixed + +* A bug causing certbot-auto to print warnings or crash on some RHEL based + systems has been resolved. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +changes in this release were to certbot-auto. + +More details about these changes can be found on our GitHub repo. + +## 0.33.0 - 2019-04-03 + +### Added + +* Fedora 29+ is now supported by certbot-auto. Since Python 2.x is on a deprecation + path in Fedora, certbot-auto will install and use Python 3.x on Fedora 29+. +* CLI flag `--https-port` has been added for Nginx plugin exclusively, and replaces + `--tls-sni-01-port`. It defines the HTTPS port the Nginx plugin will use while + setting up a new SSL vhost. By default the HTTPS port is 443. + +### Changed + +* Support for TLS-SNI-01 has been removed from all official Certbot plugins. +* Attributes related to the TLS-SNI-01 challenge in `acme.challenges` and `acme.standalone` + modules are deprecated and will be removed soon. +* CLI flags `--tls-sni-01-port` and `--tls-sni-01-address` are now no-op, will + generate a deprecation warning if used, and will be removed soon. +* Options `tls-sni` and `tls-sni-01` in `--preferred-challenges` flag are now no-op, + will generate a deprecation warning if used, and will be removed soon. +* CLI flag `--standalone-supported-challenges` has been removed. + +### Fixed + +* Certbot uses the Python library cryptography for OCSP when cryptography>=2.5 + is installed. We fixed a bug in Certbot causing it to interpret timestamps in + the OCSP response as being in the local timezone rather than UTC. +* Issue causing the default CentOS 6 TLS configuration to ignore some of the + HTTPS VirtualHosts created by Certbot. mod_ssl loading is now moved to main + http.conf for this environment where possible. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* acme +* certbot +* certbot-apache +* certbot-nginx + +More details about these changes can be found on our GitHub repo. + +## 0.32.0 - 2019-03-06 + +### Added + +* If possible, Certbot uses built-in support for OCSP from recent cryptography + versions instead of the OpenSSL binary: as a consequence Certbot does not need + the OpenSSL binary to be installed anymore if cryptography>=2.5 is installed. + +### Changed + +* Certbot and its acme module now depend on josepy>=1.1.0 to avoid printing the + warnings described at https://github.com/certbot/josepy/issues/13. +* Apache plugin now respects CERTBOT_DOCS environment variable when adding + command line defaults. +* The running of manual plugin hooks is now always included in Certbot's log + output. +* Tests execution for certbot, certbot-apache and certbot-nginx packages now relies on pytest. +* An ACME CA server may return a "Retry-After" HTTP header on authorization polling, as + specified in the ACME protocol, to indicate when the next polling should occur. Certbot now + reads this header if set and respect its value. +* The `acme` module avoids sending the `keyAuthorization` field in the JWS + payload when responding to a challenge as the field is not included in the + current ACME protocol. To ease the migration path for ACME CA servers, + Certbot and its `acme` module will first try the request without the + `keyAuthorization` field but will temporarily retry the request with the + field included if a `malformed` error is received. This fallback will be + removed in version 0.34.0. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* acme +* certbot +* certbot-apache +* certbot-nginx + +More details about these changes can be found on our GitHub repo. + +## 0.31.0 - 2019-02-07 + +### Added + +* Avoid reprocessing challenges that are already validated + when a certificate is issued. +* Support for initiating (but not solving end-to-end) TLS-ALPN-01 challenges + with the `acme` module. + +### Changed + +* Certbot's official Docker images are now based on Alpine Linux 3.9 rather + than 3.7. The new version comes with OpenSSL 1.1.1. +* Lexicon-based DNS plugins are now fully compatible with Lexicon 3.x (support + on 2.x branch is maintained). +* Apache plugin now attempts to configure all VirtualHosts matching requested + domain name instead of only a single one when answering the HTTP-01 challenge. + +### Fixed + +* Fixed accessing josepy contents through acme.jose when the full acme.jose + path is used. +* Clarify behavior for deleting certs as part of revocation. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* acme +* certbot +* certbot-apache +* certbot-dns-cloudxns +* certbot-dns-dnsimple +* certbot-dns-dnsmadeeasy +* certbot-dns-gehirn +* certbot-dns-linode +* certbot-dns-luadns +* certbot-dns-nsone +* certbot-dns-ovh +* certbot-dns-sakuracloud + +More details about these changes can be found on our GitHub repo. + +## 0.30.2 - 2019-01-25 + +### Fixed + +* Update the version of setuptools pinned in certbot-auto to 40.6.3 to + solve installation problems on newer OSes. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, this +release only affects certbot-auto. + +More details about these changes can be found on our GitHub repo. + +## 0.30.1 - 2019-01-24 + +### Fixed + +* Always download the pinned version of pip in pipstrap to address breakages +* Rename old,default.conf to old-and-default.conf to address commas in filenames + breaking recent versions of pip. +* Add VIRTUALENV_NO_DOWNLOAD=1 to all calls to virtualenv to address breakages + from venv downloading the latest pip + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* certbot-apache + +More details about these changes can be found on our GitHub repo. + +## 0.30.0 - 2019-01-02 + +### Added + +* Added the `update_account` subcommand for account management commands. + +### Changed + +* Copied account management functionality from the `register` subcommand + to the `update_account` subcommand. +* Marked usage `register --update-registration` for deprecation and + removal in a future release. + +### Fixed + +* Older modules in the josepy library can now be accessed through acme.jose + like it could in previous versions of acme. This is only done to preserve + backwards compatibility and support for doing this with new modules in josepy + will not be added. Users of the acme library should switch to using josepy + directly if they haven't done so already. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* acme + +More details about these changes can be found on our GitHub repo. + +## 0.29.1 - 2018-12-05 + +### Added + +* + +### Changed + +* + +### Fixed + +* The default work and log directories have been changed back to + /var/lib/letsencrypt and /var/log/letsencrypt respectively. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* certbot + +More details about these changes can be found on our GitHub repo. + +## 0.29.0 - 2018-12-05 + +### Added + +* Noninteractive renewals with `certbot renew` (those not started from a + terminal) now randomly sleep 1-480 seconds before beginning work in + order to spread out load spikes on the server side. +* Added External Account Binding support in cli and acme library. + Command line arguments --eab-kid and --eab-hmac-key added. + +### Changed + +* Private key permissioning changes: Renewal preserves existing group mode + & gid of previous private key material. Private keys for new + lineages (i.e. new certs, not renewed) default to 0o600. + +### Fixed + +* Update code and dependencies to clean up Resource and Deprecation Warnings. +* Only depend on imgconverter extension for Sphinx >= 1.6 + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* acme +* certbot +* certbot-apache +* certbot-dns-cloudflare +* certbot-dns-digitalocean +* certbot-dns-google +* certbot-nginx + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/62?closed=1 + +## 0.28.0 - 2018-11-7 + +### Added + +* `revoke` accepts `--cert-name`, and doesn't accept both `--cert-name` and `--cert-path`. +* Use the ACMEv2 newNonce endpoint when a new nonce is needed, and newNonce is available in the directory. + +### Changed + +* Removed documentation mentions of `#letsencrypt` IRC on Freenode. +* Write README to the base of (config-dir)/live directory +* `--manual` will explicitly warn users that earlier challenges should remain in place when setting up subsequent challenges. +* Warn when using deprecated acme.challenges.TLSSNI01 +* Log warning about TLS-SNI deprecation in Certbot +* Stop preferring TLS-SNI in the Apache, Nginx, and standalone plugins +* OVH DNS plugin now relies on Lexicon>=2.7.14 to support HTTP proxies +* Default time the Linode plugin waits for DNS changes to propagate is now 1200 seconds. + +### Fixed + +* Match Nginx parser update in allowing variable names to start with `${`. +* Fix ranking of vhosts in Nginx so that all port-matching vhosts come first +* Correct OVH integration tests on machines without internet access. +* Stop caching the results of ipv6_info in http01.py +* Test fix for Route53 plugin to prevent boto3 making outgoing connections. +* The grammar used by Augeas parser in Apache plugin was updated to fix various parsing errors. +* The CloudXNS, DNSimple, DNS Made Easy, Gehirn, Linode, LuaDNS, NS1, OVH, and + Sakura Cloud DNS plugins are now compatible with Lexicon 3.0+. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* acme +* certbot +* certbot-apache +* certbot-dns-cloudxns +* certbot-dns-dnsimple +* certbot-dns-dnsmadeeasy +* certbot-dns-gehirn +* certbot-dns-linode +* certbot-dns-luadns +* certbot-dns-nsone +* certbot-dns-ovh +* certbot-dns-route53 +* certbot-dns-sakuracloud +* certbot-nginx + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/59?closed=1 + +## 0.27.1 - 2018-09-06 + +### Fixed + +* Fixed parameter name in OpenSUSE overrides for default parameters in the + Apache plugin. Certbot on OpenSUSE works again. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* certbot-apache + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/60?closed=1 + +## 0.27.0 - 2018-09-05 + +### Added + +* The Apache plugin now accepts the parameter --apache-ctl which can be + used to configure the path to the Apache control script. + +### Changed + +* When using `acme.client.ClientV2` (or + `acme.client.BackwardsCompatibleClientV2` with an ACME server that supports a + newer version of the ACME protocol), an `acme.errors.ConflictError` will be + raised if you try to create an ACME account with a key that has already been + used. Previously, a JSON parsing error was raised in this scenario when using + the library with Let's Encrypt's ACMEv2 endpoint. + +### Fixed + +* When Apache is not installed, Certbot's Apache plugin no longer prints + messages about being unable to find apachectl to the terminal when the plugin + is not selected. +* If you're using the Apache plugin with the --apache-vhost-root flag set to a + directory containing a disabled virtual host for the domain you're requesting + a certificate for, the virtual host will now be temporarily enabled if + necessary to pass the HTTP challenge. +* The documentation for the Certbot package can now be built using Sphinx 1.6+. +* You can now call `query_registration` without having to first call + `new_account` on `acme.client.ClientV2` objects. +* The requirement of `setuptools>=1.0` has been removed from `certbot-dns-ovh`. +* Names in certbot-dns-sakuracloud's tests have been updated to refer to Sakura + Cloud rather than NS1 whose plugin certbot-dns-sakuracloud was based on. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +package with changes other than its version number was: + +* acme +* certbot +* certbot-apache +* certbot-dns-ovh +* certbot-dns-sakuracloud + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/57?closed=1 + +## 0.26.1 - 2018-07-17 + +### Fixed + +* Fix a bug that was triggered when users who had previously manually set `--server` to get ACMEv2 certs tried to renew ACMEv1 certs. + +Despite us having broken lockstep, we are continuing to release new versions of all Certbot components during releases for the time being, however, the only package with changes other than its version number was: + +* certbot + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/58?closed=1 + +## 0.26.0 - 2018-07-11 + +### Added + +* A new security enhancement which we're calling AutoHSTS has been added to + Certbot's Apache plugin. This enhancement configures your webserver to send a + HTTP Strict Transport Security header with a low max-age value that is slowly + increased over time. The max-age value is not increased to a large value + until you've successfully managed to renew your certificate. This enhancement + can be requested with the --auto-hsts flag. +* New official DNS plugins have been created for Gehirn Infrastructure Service, + Linode, OVH, and Sakura Cloud. These plugins can be found on our Docker Hub + page at https://hub.docker.com/u/certbot and on PyPI. +* The ability to reuse ACME accounts from Let's Encrypt's ACMEv1 endpoint on + Let's Encrypt's ACMEv2 endpoint has been added. +* Certbot and its components now support Python 3.7. +* Certbot's install subcommand now allows you to interactively choose which + certificate to install from the list of certificates managed by Certbot. +* Certbot now accepts the flag `--no-autorenew` which causes any obtained + certificates to not be automatically renewed when it approaches expiration. +* Support for parsing the TLS-ALPN-01 challenge has been added back to the acme + library. + +### Changed + +* Certbot's default ACME server has been changed to Let's Encrypt's ACMEv2 + endpoint. By default, this server will now be used for both new certificate + lineages and renewals. +* The Nginx plugin is no longer marked labeled as an "Alpha" version. +* The `prepare` method of Certbot's plugins is no longer called before running + "Updater" enhancements that are run on every invocation of `certbot renew`. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +packages with functional changes were: + +* acme +* certbot +* certbot-apache +* certbot-dns-gehirn +* certbot-dns-linode +* certbot-dns-ovh +* certbot-dns-sakuracloud +* certbot-nginx + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/55?closed=1 + +## 0.25.1 - 2018-06-13 + +### Fixed + +* TLS-ALPN-01 support has been removed from our acme library. Using our current + dependencies, we are unable to provide a correct implementation of this + challenge so we decided to remove it from the library until we can provide + proper support. +* Issues causing test failures when running the tests in the acme package with + pytest<3.0 has been resolved. +* certbot-nginx now correctly depends on acme>=0.25.0. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +packages with changes other than their version number were: + +* acme +* certbot-nginx + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/56?closed=1 + +## 0.25.0 - 2018-06-06 + +### Added + +* Support for the ready status type was added to acme. Without this change, + Certbot and acme users will begin encountering errors when using Let's + Encrypt's ACMEv2 API starting on June 19th for the staging environment and + July 5th for production. See + https://community.letsencrypt.org/t/acmev2-order-ready-status/62866 for more + information. +* Certbot now accepts the flag --reuse-key which will cause the same key to be + used in the certificate when the lineage is renewed rather than generating a + new key. +* You can now add multiple email addresses to your ACME account with Certbot by + providing a comma separated list of emails to the --email flag. +* Support for Let's Encrypt's upcoming TLS-ALPN-01 challenge was added to acme. + For more information, see + https://community.letsencrypt.org/t/tls-alpn-validation-method/63814/1. +* acme now supports specifying the source address to bind to when sending + outgoing connections. You still cannot specify this address using Certbot. +* If you run Certbot against Let's Encrypt's ACMEv2 staging server but don't + already have an account registered at that server URL, Certbot will + automatically reuse your staging account from Let's Encrypt's ACMEv1 endpoint + if it exists. +* Interfaces were added to Certbot allowing plugins to be called at additional + points. The `GenericUpdater` interface allows plugins to perform actions + every time `certbot renew` is run, regardless of whether any certificates are + due for renewal, and the `RenewDeployer` interface allows plugins to perform + actions when a certificate is renewed. See `certbot.interfaces` for more + information. + +### Changed + +* When running Certbot with --dry-run and you don't already have a staging + account, the created account does not contain an email address even if one + was provided to avoid expiration emails from Let's Encrypt's staging server. +* certbot-nginx does a better job of automatically detecting the location of + Nginx's configuration files when run on BSD based systems. +* acme now requires and uses pytest when running tests with setuptools with + `python setup.py test`. +* `certbot config_changes` no longer waits for user input before exiting. + +### Fixed + +* Misleading log output that caused users to think that Certbot's standalone + plugin failed to bind to a port when performing a challenge has been + corrected. +* An issue where certbot-nginx would fail to enable HSTS if the server block + already had an `add_header` directive has been resolved. +* certbot-nginx now does a better job detecting the server block to base the + configuration for TLS-SNI challenges on. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +packages with functional changes were: + +* acme +* certbot +* certbot-apache +* certbot-nginx + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/54?closed=1 + +## 0.24.0 - 2018-05-02 + +### Added + +* certbot now has an enhance subcommand which allows you to configure security + enhancements like HTTP to HTTPS redirects, OCSP stapling, and HSTS without + reinstalling a certificate. +* certbot-dns-rfc2136 now allows the user to specify the port to use to reach + the DNS server in its credentials file. +* acme now parses the wildcard field included in authorizations so it can be + used by users of the library. + +### Changed + +* certbot-dns-route53 used to wait for each DNS update to propagate before + sending the next one, but now it sends all updates before waiting which + speeds up issuance for multiple domains dramatically. +* Certbot's official Docker images are now based on Alpine Linux 3.7 rather + than 3.4 because 3.4 has reached its end-of-life. +* We've doubled the time Certbot will spend polling authorizations before + timing out. +* The level of the message logged when Certbot is being used with + non-standard paths warning that crontabs for renewal included in Certbot + packages from OS package managers may not work has been reduced. This stops + the message from being written to stderr every time `certbot renew` runs. + +### Fixed + +* certbot-auto now works with Python 3.6. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +packages with changes other than their version number were: + +* acme +* certbot +* certbot-apache +* certbot-dns-digitalocean (only style improvements to tests) +* certbot-dns-rfc2136 + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/52?closed=1 + +## 0.23.0 - 2018-04-04 + +### Added + +* Support for OpenResty was added to the Nginx plugin. + +### Changed + +* The timestamps in Certbot's logfiles now use the system's local time zone + rather than UTC. +* Certbot's DNS plugins that use Lexicon now rely on Lexicon>=2.2.1 to be able + to create and delete multiple TXT records on a single domain. +* certbot-dns-google's test suite now works without an internet connection. + +### Fixed + +* Removed a small window that if during which an error occurred, Certbot + wouldn't clean up performed challenges. +* The parameters `default` and `ipv6only` are now removed from `listen` + directives when creating a new server block in the Nginx plugin. +* `server_name` directives enclosed in quotation marks in Nginx are now properly + supported. +* Resolved an issue preventing the Apache plugin from starting Apache when it's + not currently running on RHEL and Gentoo based systems. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +packages with changes other than their version number were: + +* certbot +* certbot-apache +* certbot-dns-cloudxns +* certbot-dns-dnsimple +* certbot-dns-dnsmadeeasy +* certbot-dns-google +* certbot-dns-luadns +* certbot-dns-nsone +* certbot-dns-rfc2136 +* certbot-nginx + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/50?closed=1 + +## 0.22.2 - 2018-03-19 + +### Fixed + +* A type error introduced in 0.22.1 that would occur during challenge cleanup + when a Certbot plugin raises an exception while trying to complete the + challenge was fixed. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +packages with changes other than their version number were: + +* certbot + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/53?closed=1 + +## 0.22.1 - 2018-03-19 + +### Changed + +* The ACME server used with Certbot's --dry-run and --staging flags is now + Let's Encrypt's ACMEv2 staging server which allows people to also test ACMEv2 + features with these flags. + +### Fixed + +* The HTTP Content-Type header is now set to the correct value during + certificate revocation with new versions of the ACME protocol. +* When using Certbot with Let's Encrypt's ACMEv2 server, it would add a blank + line to the top of chain.pem and between the certificates in fullchain.pem + for each lineage. These blank lines have been removed. +* Resolved a bug that caused Certbot's --allow-subset-of-names flag not to + work. +* Fixed a regression in acme.client.Client that caused the class to not work + when it was initialized without a ClientNetwork which is done by some of the + other projects using our ACME library. + +Despite us having broken lockstep, we are continuing to release new versions of +all Certbot components during releases for the time being, however, the only +packages with changes other than their version number were: + +* acme +* certbot + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/51?closed=1 + +## 0.22.0 - 2018-03-07 + +### Added + +* Support for obtaining wildcard certificates and a newer version of the ACME + protocol such as the one implemented by Let's Encrypt's upcoming ACMEv2 + endpoint was added to Certbot and its ACME library. Certbot still works with + older ACME versions and will automatically change the version of the protocol + used based on the version the ACME CA implements. +* The Apache and Nginx plugins are now able to automatically install a wildcard + certificate to multiple virtual hosts that you select from your server + configuration. +* The `certbot install` command now accepts the `--cert-name` flag for + selecting a certificate. +* `acme.client.BackwardsCompatibleClientV2` was added to Certbot's ACME library + which automatically handles most of the differences between new and old ACME + versions. `acme.client.ClientV2` is also available for people who only want + to support one version of the protocol or want to handle the differences + between versions themselves. +* certbot-auto now supports the flag --install-only which has the script + install Certbot and its dependencies and exit without invoking Certbot. +* Support for issuing a single certificate for a wildcard and base domain was + added to our Google Cloud DNS plugin. To do this, we now require your API + credentials have additional permissions, however, your credentials will + already have these permissions unless you defined a custom role with fewer + permissions than the standard DNS administrator role provided by Google. + These permissions are also only needed for the case described above so it + will continue to work for existing users. For more information about the + permissions changes, see the documentation in the plugin. + +### Changed + +* We have broken lockstep between our ACME library, Certbot, and its plugins. + This means that the different components do not need to be the same version + to work together like they did previously. This makes packaging easier + because not every piece of Certbot needs to be repackaged to ship a change to + a subset of its components. +* Support for Python 2.6 and Python 3.3 has been removed from ACME, Certbot, + Certbot's plugins, and certbot-auto. If you are using certbot-auto on a RHEL + 6 based system, it will walk you through the process of installing Certbot + with Python 3 and refuse to upgrade to a newer version of Certbot until you + have done so. +* Certbot's components now work with older versions of setuptools to simplify + packaging for EPEL 7. + +### Fixed + +* Issues caused by Certbot's Nginx plugin adding multiple ipv6only directives + has been resolved. +* A problem where Certbot's Apache plugin would add redundant include + directives for the TLS configuration managed by Certbot has been fixed. +* Certbot's webroot plugin now properly deletes any directories it creates. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/48?closed=1 + +## 0.21.1 - 2018-01-25 + +### Fixed + +* When creating an HTTP to HTTPS redirect in Nginx, we now ensure the Host + header of the request is set to an expected value before redirecting users to + the domain found in the header. The previous way Certbot configured Nginx + redirects was a potential security issue which you can read more about at + https://community.letsencrypt.org/t/security-issue-with-redirects-added-by-certbots-nginx-plugin/51493. +* Fixed a problem where Certbot's Apache plugin could fail HTTP-01 challenges + if basic authentication is configured for the domain you request a + certificate for. +* certbot-auto --no-bootstrap now properly tries to use Python 3.4 on RHEL 6 + based systems rather than Python 2.6. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/49?closed=1 + +## 0.21.0 - 2018-01-17 + +### Added + +* Support for the HTTP-01 challenge type was added to our Apache and Nginx + plugins. For those not aware, Let's Encrypt disabled the TLS-SNI-01 challenge + type which was what was previously being used by our Apache and Nginx plugins + last week due to a security issue. For more information about Let's Encrypt's + change, click + [here](https://community.letsencrypt.org/t/2018-01-11-update-regarding-acme-tls-sni-and-shared-hosting-infrastructure/50188). + Our Apache and Nginx plugins will automatically switch to use HTTP-01 so no + changes need to be made to your Certbot configuration, however, you should + make sure your server is accessible on port 80 and isn't behind an external + proxy doing things like redirecting all traffic from HTTP to HTTPS. HTTP to + HTTPS redirects inside Apache and Nginx are fine. +* IPv6 support was added to the Nginx plugin. +* Support for automatically creating server blocks based on the default server + block was added to the Nginx plugin. +* The flags --delete-after-revoke and --no-delete-after-revoke were added + allowing users to control whether the revoke subcommand also deletes the + certificates it is revoking. + +### Changed + +* We deprecated support for Python 2.6 and Python 3.3 in Certbot and its ACME + library. Support for these versions of Python will be removed in the next + major release of Certbot. If you are using certbot-auto on a RHEL 6 based + system, it will guide you through the process of installing Python 3. +* We split our implementation of JOSE (Javascript Object Signing and + Encryption) out of our ACME library and into a separate package named josepy. + This package is available on [PyPI](https://pypi.python.org/pypi/josepy) and + on [GitHub](https://github.com/certbot/josepy). +* We updated the ciphersuites used in Apache to the new [values recommended by + Mozilla](https://wiki.mozilla.org/Security/Server_Side_TLS#Intermediate_compatibility_.28default.29). + The major change here is adding ChaCha20 to the list of supported + ciphersuites. + +### Fixed + +* An issue with our Apache plugin on Gentoo due to differences in their + apache2ctl command have been resolved. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/47?closed=1 + +## 0.20.0 - 2017-12-06 + +### Added + +* Certbot's ACME library now recognizes URL fields in challenge objects in + preparation for Let's Encrypt's new ACME endpoint. The value is still + accessible in our ACME library through the name "uri". + +### Changed + +* The Apache plugin now parses some distro specific Apache configuration files + on non-Debian systems allowing it to get a clearer picture on the running + configuration. Internally, these changes were structured so that external + contributors can easily write patches to make the plugin work in new Apache + configurations. +* Certbot better reports network failures by removing information about + connection retries from the error output. +* An unnecessary question when using Certbot's webroot plugin interactively has + been removed. + +### Fixed + +* Certbot's NGINX plugin no longer sometimes incorrectly reports that it was + unable to deploy a HTTP->HTTPS redirect when requesting Certbot to enable a + redirect for multiple domains. +* Problems where the Apache plugin was failing to find directives and + duplicating existing directives on openSUSE have been resolved. +* An issue running the test shipped with Certbot and some our DNS plugins with + older versions of mock have been resolved. +* On some systems, users reported strangely interleaved output depending on + when stdout and stderr were flushed. This problem was resolved by having + Certbot regularly flush these streams. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/44?closed=1 + +## 0.19.0 - 2017-10-04 + +### Added + +* Certbot now has renewal hook directories where executable files can be placed + for Certbot to run with the renew subcommand. Pre-hooks, deploy-hooks, and + post-hooks can be specified in the renewal-hooks/pre, renewal-hooks/deploy, + and renewal-hooks/post directories respectively in Certbot's configuration + directory (which is /etc/letsencrypt by default). Certbot will automatically + create these directories when it is run if they do not already exist. +* After revoking a certificate with the revoke subcommand, Certbot will offer + to delete the lineage associated with the certificate. When Certbot is run + with --non-interactive, it will automatically try to delete the associated + lineage. +* When using Certbot's Google Cloud DNS plugin on Google Compute Engine, you no + longer have to provide a credential file to Certbot if you have configured + sufficient permissions for the instance which Certbot can automatically + obtain using Google's metadata service. + +### Changed + +* When deleting certificates interactively using the delete subcommand, Certbot + will now allow you to select multiple lineages to be deleted at once. +* Certbot's Apache plugin no longer always parses Apache's sites-available on + Debian based systems and instead only parses virtual hosts included in your + Apache configuration. You can provide an additional directory for Certbot to + parse using the command line flag --apache-vhost-root. + +### Fixed + +* The plugins subcommand can now be run without root access. +* certbot-auto now includes a timeout when updating itself so it no longer + hangs indefinitely when it is unable to connect to the external server. +* An issue where Certbot's Apache plugin would sometimes fail to deploy a + certificate on Debian based systems if mod_ssl wasn't already enabled has + been resolved. +* A bug in our Docker image where the certificates subcommand could not report + if certificates maintained by Certbot had been revoked has been fixed. +* Certbot's RFC 2136 DNS plugin (for use with software like BIND) now properly + performs DNS challenges when the domain being verified contains a CNAME + record. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/43?closed=1 + +## 0.18.2 - 2017-09-20 + +### Fixed + +* An issue where Certbot's ACME module would raise an AttributeError trying to + create self-signed certificates when used with pyOpenSSL 17.3.0 has been + resolved. For Certbot users with this version of pyOpenSSL, this caused + Certbot to crash when performing a TLS SNI challenge or when the Nginx plugin + tried to create an SSL server block. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/46?closed=1 + +## 0.18.1 - 2017-09-08 + +### Fixed + +* If certbot-auto was running as an unprivileged user and it upgraded from + 0.17.0 to 0.18.0, it would crash with a permissions error and would need to + be run again to successfully complete the upgrade. This has been fixed and + certbot-auto should upgrade cleanly to 0.18.1. +* Certbot usually uses "certbot-auto" or "letsencrypt-auto" in error messages + and the User-Agent string instead of "certbot" when you are using one of + these wrapper scripts. Proper detection of this was broken with Certbot's new + installation path in /opt in 0.18.0 but this problem has been resolved. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/45?closed=1 + +## 0.18.0 - 2017-09-06 + +### Added + +* The Nginx plugin now configures Nginx to use 2048-bit Diffie-Hellman + parameters. Java 6 clients do not support Diffie-Hellman parameters larger + than 1024 bits, so if you need to support these clients you will need to + manually modify your Nginx configuration after using the Nginx installer. + +### Changed + +* certbot-auto now installs Certbot in directories under `/opt/eff.org`. If you + had an existing installation from certbot-auto, a symlink is created to the + new directory. You can configure certbot-auto to use a different path by + setting the environment variable VENV_PATH. +* The Nginx plugin can now be selected in Certbot's interactive output. +* Output verbosity of renewal failures when running with `--quiet` has been + reduced. +* The default revocation reason shown in Certbot help output now is a human + readable string instead of a numerical code. +* Plugin selection is now included in normal terminal output. + +### Fixed + +* A newer version of ConfigArgParse is now installed when using certbot-auto + causing values set to false in a Certbot INI configuration file to be handled + intuitively. Setting a boolean command line flag to false is equivalent to + not including it in the configuration file at all. +* New naming conventions preventing certbot-auto from installing OS + dependencies on Fedora 26 have been resolved. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/42?closed=1 + +## 0.17.0 - 2017-08-02 + +### Added + +* Support in our nginx plugin for modifying SSL server blocks that do + not contain certificate or key directives. +* A `--max-log-backups` flag to allow users to configure or even completely + disable Certbot's built in log rotation. +* A `--user-agent-comment` flag to allow people who build tools around Certbot + to differentiate their user agent string by adding a comment to its default + value. + +### Changed + +* Due to some awesome work by + [cryptography project](https://github.com/pyca/cryptography), compilation can + now be avoided on most systems when using certbot-auto. This eliminates many + problems people have had in the past such as running out of memory, having + invalid headers/libraries, and changes to the OS packages on their system + after compilation breaking Certbot. +* The `--renew-hook` flag has been hidden in favor of `--deploy-hook`. This new + flag works exactly the same way except it is always run when a certificate is + issued rather than just when it is renewed. +* We have started printing deprecation warnings in certbot-auto for + experimentally supported systems with OS packages available. +* A certificate lineage's name is included in error messages during renewal. + +### Fixed + +* Encoding errors that could occur when parsing error messages from the ACME + server containing Unicode have been resolved. +* certbot-auto no longer prints misleading messages about there being a newer + pip version available when installation fails. +* Certbot's ACME library now properly extracts domains from critical SAN + extensions. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.17.0+is%3Aclosed + +## 0.16.0 - 2017-07-05 + +### Added + +* A plugin for performing DNS challenges using dynamic DNS updates as defined + in RFC 2316. This plugin is packaged separately from Certbot and is available + at https://pypi.python.org/pypi/certbot-dns-rfc2136. It supports Python 2.6, + 2.7, and 3.3+. At this time, there isn't a good way to install this plugin + when using certbot-auto, but this should change in the near future. +* Plugins for performing DNS challenges for the providers + [DNS Made Easy](https://pypi.python.org/pypi/certbot-dns-dnsmadeeasy) and + [LuaDNS](https://pypi.python.org/pypi/certbot-dns-luadns). These plugins are + packaged separately from Certbot and support Python 2.7 and 3.3+. Currently, + there isn't a good way to install these plugins when using certbot-auto, + but that should change soon. +* Support for performing TLS-SNI-01 challenges when using the manual plugin. +* Automatic detection of Arch Linux in the Apache plugin providing better + default settings for the plugin. + +### Changed + +* The text of the interactive question about whether a redirect from HTTP to + HTTPS should be added by Certbot has been rewritten to better explain the + choices to the user. +* Simplified HTTP challenge instructions in the manual plugin. + +### Fixed + +* Problems performing a dry run when using the Nginx plugin have been fixed. +* Resolved an issue where certbot-dns-digitalocean's test suite would sometimes + fail when ran using Python 3. +* On some systems, previous versions of certbot-auto would error out with a + message about a missing hash for setuptools. This has been fixed. +* A bug where Certbot would sometimes not print a space at the end of an + interactive prompt has been resolved. +* Nonfatal tracebacks are no longer shown in rare cases where Certbot + encounters an exception trying to close its TCP connection with the ACME + server. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.16.0+is%3Aclosed + +## 0.15.0 - 2017-06-08 + +### Added + +* Plugins for performing DNS challenges for popular providers. Like the Apache + and Nginx plugins, these plugins are packaged separately and not included in + Certbot by default. So far, we have plugins for + [Amazon Route 53](https://pypi.python.org/pypi/certbot-dns-route53), + [Cloudflare](https://pypi.python.org/pypi/certbot-dns-cloudflare), + [DigitalOcean](https://pypi.python.org/pypi/certbot-dns-digitalocean), and + [Google Cloud](https://pypi.python.org/pypi/certbot-dns-google) which all + work on Python 2.6, 2.7, and 3.3+. Additionally, we have plugins for + [CloudXNS](https://pypi.python.org/pypi/certbot-dns-cloudxns), + [DNSimple](https://pypi.python.org/pypi/certbot-dns-dnsimple), + [NS1](https://pypi.python.org/pypi/certbot-dns-nsone) which work on Python + 2.7 and 3.3+ (and not 2.6). Currently, there isn't a good way to install + these plugins when using `certbot-auto`, but that should change soon. +* IPv6 support in the standalone plugin. When performing a challenge, the + standalone plugin automatically handles listening for IPv4/IPv6 traffic based + on the configuration of your system. +* A mechanism for keeping your Apache and Nginx SSL/TLS configuration up to + date. When the Apache or Nginx plugins are used, they place SSL/TLS + configuration options in the root of Certbot's config directory + (`/etc/letsencrypt` by default). Now when a new version of these plugins run + on your system, they will automatically update the file to the newest + version if it is unmodified. If you manually modified the file, Certbot will + display a warning giving you a path to the updated file which you can use as + a reference to manually update your modified copy. +* `--http-01-address` and `--tls-sni-01-address` flags for controlling the + address Certbot listens on when using the standalone plugin. +* The command `certbot certificates` that lists certificates managed by Certbot + now performs additional validity checks to notify you if your files have + become corrupted. + +### Changed + +* Messages custom hooks print to `stdout` are now displayed by Certbot when not + running in `--quiet` mode. +* `jwk` and `alg` fields in JWS objects have been moved into the protected + header causing Certbot to more closely follow the latest version of the ACME + spec. + +### Fixed + +* Permissions on renewal configuration files are now properly preserved when + they are updated. +* A bug causing Certbot to display strange defaults in its help output when + using Python <= 2.7.4 has been fixed. +* Certbot now properly handles mixed case domain names found in custom CSRs. +* A number of poorly worded prompts and error messages. + +### Removed + +* Support for OpenSSL 1.0.0 in `certbot-auto` has been removed as we now pin a + newer version of `cryptography` which dropped support for this version. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.15.0+is%3Aclosed + +## 0.14.2 - 2017-05-25 + +### Fixed + +* Certbot 0.14.0 included a bug where Certbot would create a temporary log file +(usually in /tmp) if the program exited during argument parsing. If a user +provided -h/--help/help, --version, or an invalid command line argument, +Certbot would create this temporary log file. This was especially bothersome to +certbot-auto users as certbot-auto runs `certbot --version` internally to see +if the script needs to upgrade causing it to create at least one of these files +on every run. This problem has been resolved. + +More details about this change can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.14.2+is%3Aclosed + +## 0.14.1 - 2017-05-16 + +### Fixed + +* Certbot now works with configargparse 0.12.0. +* Issues with the Apache plugin and Augeas 1.7+ have been resolved. +* A problem where the Nginx plugin would fail to install certificates on +systems that had the plugin's SSL/TLS options file from 7+ months ago has been +fixed. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.14.1+is%3Aclosed + +## 0.14.0 - 2017-05-04 + +### Added + +* Python 3.3+ support for all Certbot packages. `certbot-auto` still currently +only supports Python 2, but the `acme`, `certbot`, `certbot-apache`, and +`certbot-nginx` packages on PyPI now fully support Python 2.6, 2.7, and 3.3+. +* Certbot's Apache plugin now handles multiple virtual hosts per file. +* Lockfiles to prevent multiple versions of Certbot running simultaneously. + +### Changed + +* When converting an HTTP virtual host to HTTPS in Apache, Certbot only copies +the virtual host rather than the entire contents of the file it's contained +in. +* The Nginx plugin now includes SSL/TLS directives in a separate file located +in Certbot's configuration directory rather than copying the contents of the +file into every modified `server` block. + +### Fixed + +* Ensure logging is configured before parts of Certbot attempt to log any +messages. +* Support for the `--quiet` flag in `certbot-auto`. +* Reverted a change made in a previous release to make the `acme` and `certbot` +packages always depend on `argparse`. This dependency is conditional again on +the user's Python version. +* Small bugs in the Nginx plugin such as properly handling empty `server` +blocks and setting `server_names_hash_bucket_size` during challenges. + +As always, a more complete list of changes can be found on GitHub: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.14.0+is%3Aclosed + +## 0.13.0 - 2017-04-06 + +### Added + +* `--debug-challenges` now pauses Certbot after setting up challenges for debugging. +* The Nginx parser can now handle all valid directives in configuration files. +* Nginx ciphersuites have changed to Mozilla Intermediate. +* `certbot-auto --no-bootstrap` provides the option to not install OS dependencies. + +### Fixed + +* `--register-unsafely-without-email` now respects `--quiet`. +* Hyphenated renewal parameters are now saved in renewal config files. +* `--dry-run` no longer persists keys and csrs. +* Certbot no longer hangs when trying to start Nginx in Arch Linux. +* Apache rewrite rules no longer double-encode characters. + +A full list of changes is available on GitHub: +https://github.com/certbot/certbot/issues?q=is%3Aissue%20milestone%3A0.13.0%20is%3Aclosed%20 + +## 0.12.0 - 2017-03-02 + +### Added + +* Certbot now allows non-camelcase Apache VirtualHost names. +* Certbot now allows more log messages to be silenced. + +### Fixed + +* Fixed a regression around using `--cert-name` when getting new certificates + +More information about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue%20milestone%3A0.12.0 + +## 0.11.1 - 2017-02-01 + +### Fixed + +* Resolved a problem where Certbot would crash while parsing command line +arguments in some cases. +* Fixed a typo. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/pulls?q=is%3Apr%20milestone%3A0.11.1%20is%3Aclosed + +## 0.11.0 - 2017-02-01 + +### Added + +* When using the standalone plugin while running Certbot interactively +and a required port is bound by another process, Certbot will give you +the option to retry to grab the port rather than immediately exiting. +* You are now able to deactivate your account with the Let's Encrypt +server using the `unregister` subcommand. +* When revoking a certificate using the `revoke` subcommand, you now +have the option to provide the reason the certificate is being revoked +to Let's Encrypt with `--reason`. + +### Changed + +* Providing `--quiet` to `certbot-auto` now silences package manager output. + +### Removed + +* Removed the optional `dnspython` dependency in our `acme` package. +Now the library does not support client side verification of the DNS +challenge. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.11.0+is%3Aclosed + +## 0.10.2 - 2017-01-25 + +### Added + +* If Certbot receives a request with a `badNonce` error, it now +automatically retries the request. Since nonces from Let's Encrypt expire, +this helps people performing the DNS challenge with the `manual` plugin +who may have to wait an extended period of time for their DNS changes to +propagate. + +### Fixed + +* Certbot now saves the `--preferred-challenges` values for renewal. Previously +these values were discarded causing a different challenge type to be used when +renewing certs in some cases. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.10.2+is%3Aclosed + +## 0.10.1 - 2017-01-13 + +### Fixed + +* Resolve problems where when asking Certbot to update a certificate at +an existing path to include different domain names, the old names would +continue to be used. +* Fix issues successfully running our unit test suite on some systems. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.10.1+is%3Aclosed + +## 0.10.0 - 2017-01-11 + +## Added + +* Added the ability to customize and automatically complete DNS and HTTP +domain validation challenges with the manual plugin. The flags +`--manual-auth-hook` and `--manual-cleanup-hook` can now be provided +when using the manual plugin to execute commands provided by the user to +perform and clean up challenges provided by the CA. This is best used in +complicated setups where the DNS challenge must be used or Certbot's +existing plugins cannot be used to perform HTTP challenges. For more +information on how this works, see `certbot --help manual`. +* Added a `--cert-name` flag for specifying the name to use for the +certificate in Certbot's configuration directory. Using this flag in +combination with `-d/--domains`, a user can easily request a new +certificate with different domains and save it with the name provided by +`--cert-name`. Additionally, `--cert-name` can be used to select a +certificate with the `certonly` and `run` subcommands so a full list of +domains in the certificate does not have to be provided. +* Added subcommand `certificates` for listing the certificates managed by +Certbot and their properties. +* Added the `delete` subcommand for removing certificates managed by Certbot +from the configuration directory. +* Certbot now supports requesting internationalized domain names (IDNs). +* Hooks provided to Certbot are now saved to be reused during renewal. +If you run Certbot with `--pre-hook`, `--renew-hook`, or `--post-hook` +flags when obtaining a certificate, the provided commands will +automatically be saved and executed again when renewing the certificate. +A pre-hook and/or post-hook can also be given to the `certbot renew` +command either on the command line or in a [configuration +file](https://certbot.eff.org/docs/using.html#configuration-file) to run +an additional command before/after any certificate is renewed. Hooks +will only be run if a certificate is renewed. +* Support Busybox in certbot-auto. + +### Changed + +* Recategorized `-h/--help` output to improve documentation and +discoverability. + +### Removed + +* Removed the ncurses interface. This change solves problems people +were having on many systems, reduces the number of Certbot +dependencies, and simplifies our code. Certbot's only interface now is +the text interface which was available by providing `-t/--text` to +earlier versions of Certbot. + +### Fixed + +* Many small bug fixes. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.10.0is%3Aclosed + +## 0.9.3 - 2016-10-13 + +### Added + +* The Apache plugin uses information about your OS to help determine the +layout of your Apache configuration directory. We added a patch to +ensure this code behaves the same way when testing on different systems +as the tests were failing in some cases. + +### Changed + +* Certbot adopted more conservative behavior about reporting a needed port as +unavailable when using the standalone plugin. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/27?closed=1 + +## 0.9.2 - 2016-10-12 + +### Added + +* Certbot stopped requiring that all possibly required ports are available when +using the standalone plugin. It now only verifies that the ports are available +when they are necessary. + +### Fixed + +* Certbot now verifies that our optional dependencies version matches what is +required by Certbot. +* Certnot now properly copies the `ssl on;` directives as necessary when +performing domain validation in the Nginx plugin. +* Fixed problem where symlinks were becoming files when they were +packaged, causing errors during testing and OS packaging. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/26?closed=1 + +## 0.9.1 - 2016-10-06 + +### Fixed + +* Fixed a bug that was introduced in version 0.9.0 where the command +line flag -q/--quiet wasn't respected in some cases. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/milestone/25?closed=1 + +## 0.9.0 - 2016-10-05 + +### Added + +* Added an alpha version of the Nginx plugin. This plugin fully automates the +process of obtaining and installing certificates with Nginx. +Additionally, it is able to automatically configure security +enhancements such as an HTTP to HTTPS redirect and OCSP stapling. To use +this plugin, you must have the `certbot-nginx` package installed (which +is installed automatically when using `certbot-auto`) and provide +`--nginx` on the command line. This plugin is still in its early stages +so we recommend you use it with some caution and make sure you have a +backup of your Nginx configuration. +* Added support for the `DNS` challenge in the `acme` library and `DNS` in +Certbot's `manual` plugin. This allows you to create DNS records to +prove to Let's Encrypt you control the requested domain name. To use +this feature, include `--manual --preferred-challenges dns` on the +command line. +* Certbot now helps with enabling Extra Packages for Enterprise Linux (EPEL) on +CentOS 6 when using `certbot-auto`. To use `certbot-auto` on CentOS 6, +the EPEL repository has to be enabled. `certbot-auto` will now prompt +users asking them if they would like the script to enable this for them +automatically. This is done without prompting users when using +`letsencrypt-auto` or if `-n/--non-interactive/--noninteractive` is +included on the command line. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.9.0+is%3Aclosed + +## 0.8.1 - 2016-06-14 + +### Added + +* Certbot now preserves a certificate's common name when using `renew`. +* Certbot now saves webroot values for renewal when they are entered interactively. +* Certbot now gracefully reports that the Apache plugin isn't usable when Augeas is not installed. +* Added experimental support for Mageia has been added to `certbot-auto`. + +### Fixed + +* Fixed problems with an invalid user-agent string on OS X. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.8.1+ + +## 0.8.0 - 2016-06-02 + +### Added + +* Added the `register` subcommand which can be used to register an account +with the Let's Encrypt CA. +* You can now run `certbot register --update-registration` to +change the e-mail address associated with your registration. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue+milestone%3A0.8.0+ + +## 0.7.0 - 2016-05-27 + +### Added + +* Added `--must-staple` to request certificates from Let's Encrypt +with the OCSP must staple extension. +* Certbot now automatically configures OSCP stapling for Apache. +* Certbot now allows requesting certificates for domains found in the common name +of a custom CSR. + +### Fixed + +* Fixed a number of miscellaneous bugs + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=milestone%3A0.7.0+is%3Aissue + +## 0.6.0 - 2016-05-12 + +### Added + +* Versioned the datetime dependency in setup.py. + +### Changed + +* Renamed the client from `letsencrypt` to `certbot`. + +### Fixed + +* Fixed a small json deserialization error. +* Certbot now preserves domain order in generated CSRs. +* Fixed some minor bugs. + +More details about these changes can be found on our GitHub repo: +https://github.com/certbot/certbot/issues?q=is%3Aissue%20milestone%3A0.6.0%20is%3Aclosed%20 + +## 0.5.0 - 2016-04-05 + +### Added + +* Added the ability to use the webroot plugin interactively. +* Added the flags --pre-hook, --post-hook, and --renew-hook which can be used with +the renew subcommand to register shell commands to run in response to +renewal events. Pre-hook commands will be run before any certs are +renewed, post-hook commands will be run after any certs are renewed, +and renew-hook commands will be run after each cert is renewed. If no +certs are due for renewal, no command is run. +* Added a -q/--quiet flag which silences all output except errors. +* Added an --allow-subset-of-domains flag which can be used with the renew +command to prevent renewal failures for a subset of the requested +domains from causing the client to exit. + +### Changed + +* Certbot now uses renewal configuration files. In /etc/letsencrypt/renewal +by default, these files can be used to control what parameters are +used when renewing a specific certificate. + +More details about these changes can be found on our GitHub repo: +https://github.com/letsencrypt/letsencrypt/issues?q=milestone%3A0.5.0+is%3Aissue + +## 0.4.2 - 2016-03-03 + +### Fixed + +* Resolved problems encountered when compiling letsencrypt +against the new OpenSSL release. +* Fixed problems encountered when using `letsencrypt renew` with configuration files +from the private beta. + +More details about these changes can be found on our GitHub repo: +https://github.com/letsencrypt/letsencrypt/issues?q=is%3Aissue+milestone%3A0.4.2 + +## 0.4.1 - 2016-02-29 + +### Fixed + +* Fixed Apache parsing errors encountered with some configurations. +* Fixed Werkzeug dependency problems encountered on some Red Hat systems. +* Fixed bootstrapping failures when using letsencrypt-auto with --no-self-upgrade. +* Fixed problems with parsing renewal config files from private beta. + +More details about these changes can be found on our GitHub repo: +https://github.com/letsencrypt/letsencrypt/issues?q=is:issue+milestone:0.4.1 + +## 0.4.0 - 2016-02-10 + +### Added + +* Added the verb/subcommand `renew` which can be used to renew your existing +certificates as they approach expiration. Running `letsencrypt renew` +will examine all existing certificate lineages and determine if any are +less than 30 days from expiration. If so, the client will use the +settings provided when you previously obtained the certificate to renew +it. The subcommand finishes by printing a summary of which renewals were +successful, failed, or not yet due. +* Added a `--dry-run` flag to help with testing configuration +without affecting production rate limits. Currently supported by the +`renew` and `certonly` subcommands, providing `--dry-run` on the command +line will obtain certificates from the staging server without saving the +resulting certificates to disk. +* Added major improvements to letsencrypt-auto. This script +has been rewritten to include full support for Python 2.6, the ability +for letsencrypt-auto to update itself, and improvements to the +stability, security, and performance of the script. +* Added support for Apache 2.2 to the Apache plugin. + +More details about these changes can be found on our GitHub repo: +https://github.com/letsencrypt/letsencrypt/issues?q=is%3Aissue+milestone%3A0.4.0 + +## 0.3.0 - 2016-01-27 + +### Added + +* Added a non-interactive mode which can be enabled by including `-n` or +`--non-interactive` on the command line. This can be used to guarantee +the client will not prompt when run automatically using cron/systemd. +* Added preparation for the new letsencrypt-auto script. Over the past +couple months, we've been working on increasing the reliability and +security of letsencrypt-auto. A number of changes landed in this +release to prepare for the new version of this script. + +More details about these changes can be found on our GitHub repo: +https://github.com/letsencrypt/letsencrypt/issues?q=is%3Aissue+milestone%3A0.3.0 + +## 0.2.0 - 2016-01-14 + +### Added + +* Added Apache plugin support for non-Debian based systems. Support has been +added for modern Red Hat based systems such as Fedora 23, Red Hat 7, +and CentOS 7 running Apache 2.4. In theory, this plugin should be +able to be configured to run on any Unix-like OS running Apache 2.4. +* Relaxed PyOpenSSL version requirements. This adds support for systems +with PyOpenSSL versions 0.13 or 0.14. +* Improved error messages from the client. + +### Fixed + +* Resolved issues with the Apache plugin enabling an HTTP to HTTPS +redirect on some systems. + +More details about these changes can be found on our GitHub repo: +https://github.com/letsencrypt/letsencrypt/issues?q=is%3Aissue+milestone%3A0.2.0 + +## 0.1.1 - 2015-12-15 + +### Added + +* Added a check that avoids attempting to issue for unqualified domain names like +"localhost". + +### Fixed + +* Fixed a confusing UI path that caused some users to repeatedly renew +their certs while experimenting with the client, in some cases hitting +issuance rate limits. +* Fixed numerous Apache configuration parser problems +* Fixed --webroot permission handling for non-root users + +More details about these changes can be found on our GitHub repo: +https://github.com/letsencrypt/letsencrypt/issues?q=milestone%3A0.1.1 diff --git a/certbot/LICENSE.txt b/certbot/LICENSE.txt new file mode 100644 index 00000000000..b905dd12027 --- /dev/null +++ b/certbot/LICENSE.txt @@ -0,0 +1,205 @@ +Certbot ACME Client +Copyright (c) Electronic Frontier Foundation and others +Licensed Apache Version 2.0 + +The nginx plugin incorporates code from nginxparser +Copyright (c) 2014 Fatih Erikli +Licensed MIT + + +Text of Apache License +====================== + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + +Text of MIT License +=================== +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/certbot/MANIFEST.in b/certbot/MANIFEST.in new file mode 100644 index 00000000000..8fd2a7bc91d --- /dev/null +++ b/certbot/MANIFEST.in @@ -0,0 +1,15 @@ +include README.rst +include CHANGELOG.md +include LICENSE.txt +recursive-include docs * +recursive-include examples * +recursive-include src/certbot/tests/testdata * +recursive-include src/certbot/_internal/plugins/nginx/tls_configs *.conf +recursive-include src/certbot/_internal/tests/plugins/nginx/testdata * +recursive-include src/certbot/_internal/plugins/apache/augeas_lens *.aug +recursive-include src/certbot/_internal/plugins/apache/tls_configs *.conf +recursive-include src/certbot/_internal/tests/plugins/apache/testdata * +include src/certbot/ssl-dhparams.pem +include src/certbot/py.typed +global-exclude __pycache__ +global-exclude *.py[cod] diff --git a/certbot/README.rst b/certbot/README.rst new file mode 100644 index 00000000000..b4cc7d4fccc --- /dev/null +++ b/certbot/README.rst @@ -0,0 +1,97 @@ +.. This file contains a series of comments that are used to include sections of this README in other files. Do not modify these comments unless you know what you are doing. tag:intro-begin + +|build-status| + +.. |build-status| image:: https://img.shields.io/github/actions/workflow/status/certbot/certbot/nightly.yml + :target: https://github.com/certbot/certbot/actions/workflows/nightly.yml + :alt: GitHub Actions nightly CI status + +.. image:: https://raw.githubusercontent.com/EFForg/design/master/logos/certbot/eff-certbot-lockup.png + :width: 200 + :alt: EFF Certbot Logo + +Certbot is part of EFF’s effort to encrypt the entire Internet. Secure communication over the Web relies on HTTPS, which requires the use of a digital certificate that lets browsers verify the identity of web servers (e.g., is that really google.com?). Web servers obtain their certificates from trusted third parties called certificate authorities (CAs). Certbot is an easy-to-use client that fetches a certificate from Let’s Encrypt—an open certificate authority launched by the EFF, Mozilla, and others—and deploys it to a web server. + +Anyone who has gone through the trouble of setting up a secure website knows what a hassle getting and maintaining a certificate is. Certbot and Let’s Encrypt can automate away the pain and let you turn on and manage HTTPS with simple commands. Using Certbot and Let's Encrypt is free. + +.. _installation: + +Getting Started +--------------- +The best way to get started is to use our `interactive guide `_. It generates instructions based on your configuration settings. In most cases, you’ll need `root or administrator access `_ to your web server to run Certbot. + +Certbot is meant to be run directly on your web server on the command line, not on your personal computer. If you’re using a hosted service and don’t have direct access to your web server, you might not be able to use Certbot. Check with your hosting provider for documentation about uploading certificates or using certificates issued by Let’s Encrypt. + +Contributing +------------ + +If you'd like to contribute to this project please read `Developer Guide +`_. + +This project is governed by `EFF's Public Projects Code of Conduct `_. + +Links +===== + +.. Do not modify this comment unless you know what you're doing. tag:links-begin + +Documentation: https://certbot.eff.org/docs + +Software project: https://github.com/certbot/certbot + +Changelog: https://github.com/certbot/certbot/blob/main/certbot/CHANGELOG.md + +For Contributors: https://certbot.eff.org/docs/contributing.html + +For Users: https://certbot.eff.org/docs/using.html + +Main Website: https://certbot.eff.org + +Let's Encrypt Website: https://letsencrypt.org + +Community: https://community.letsencrypt.org + +ACME spec: `RFC 8555 `_ + +ACME working area in github (archived): https://github.com/ietf-wg-acme/acme + +.. Do not modify this comment unless you know what you're doing. tag:links-end + +.. Do not modify this comment unless you know what you're doing. tag:intro-end + +.. Do not modify this comment unless you know what you're doing. tag:features-begin + +Current Features +===================== + +* Supports multiple web servers: + + - Apache 2.4+ + - nginx/0.8.48+ + - webroot (adds files to webroot directories in order to prove control of + domains and obtain certificates) + - standalone (runs its own simple webserver to prove you control a domain) + - other server software via `third party plugins `_ + +* The private key is generated locally on your system. +* Can talk to the Let's Encrypt CA or optionally to other ACME + compliant services. +* Can get domain-validated (DV) certificates. +* Can revoke certificates. +* Supports ECDSA (default) and RSA certificate private keys. +* Can optionally install a http -> https redirect, so your site effectively + runs https only. +* Fully automated. +* Configuration changes are logged and can be reverted. + +.. Do not modify this comment unless you know what you're doing. tag:features-end + +Thanks +------ + +We appreciate the donation of credits to help us test and develop Certbot from: + +.. image:: https://opensource.nyc3.cdn.digitaloceanspaces.com/attribution/assets/SVG/DO_Logo_horizontal_blue.svg + :width: 201 + :alt: DigitalOcean Logo + :target: https://www.digitalocean.com/ diff --git a/certbot/__init__.py b/certbot/__init__.py deleted file mode 100644 index bf68034c809..00000000000 --- a/certbot/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -"""Certbot client.""" - -# version number like 1.2.3a0, must have at least 2 parts, like 1.2 -__version__ = '0.31.0.dev0' diff --git a/certbot/account.py b/certbot/account.py deleted file mode 100644 index 76135c3aa9b..00000000000 --- a/certbot/account.py +++ /dev/null @@ -1,354 +0,0 @@ -"""Creates ACME accounts for server.""" -import datetime -import functools -import hashlib -import logging -import os -import shutil -import socket - -from cryptography.hazmat.primitives import serialization -import josepy as jose -import pyrfc3339 -import pytz -import six -import zope.component - -from acme import fields as acme_fields -from acme import messages - -from certbot import compat -from certbot import constants -from certbot import errors -from certbot import interfaces -from certbot import util - - -logger = logging.getLogger(__name__) - - -class Account(object): # pylint: disable=too-few-public-methods - """ACME protocol registration. - - :ivar .RegistrationResource regr: Registration Resource - :ivar .JWK key: Authorized Account Key - :ivar .Meta: Account metadata - :ivar str id: Globally unique account identifier. - - """ - - class Meta(jose.JSONObjectWithFields): - """Account metadata - - :ivar datetime.datetime creation_dt: Creation date and time (UTC). - :ivar str creation_host: FQDN of host, where account has been created. - - .. note:: ``creation_dt`` and ``creation_host`` are useful in - cross-machine migration scenarios. - - """ - creation_dt = acme_fields.RFC3339Field("creation_dt") - creation_host = jose.Field("creation_host") - - def __init__(self, regr, key, meta=None): - self.key = key - self.regr = regr - self.meta = self.Meta( - # pyrfc3339 drops microseconds, make sure __eq__ is sane - creation_dt=datetime.datetime.now( - tz=pytz.UTC).replace(microsecond=0), - creation_host=socket.getfqdn()) if meta is None else meta - - self.id = hashlib.md5( - self.key.key.public_key().public_bytes( - encoding=serialization.Encoding.PEM, - format=serialization.PublicFormat.SubjectPublicKeyInfo) - ).hexdigest() - # Implementation note: Email? Multiple accounts can have the - # same email address. Registration URI? Assigned by the - # server, not guaranteed to be stable over time, nor - # canonical URI can be generated. ACME protocol doesn't allow - # account key (and thus its fingerprint) to be updated... - - @property - def slug(self): - """Short account identification string, useful for UI.""" - return "{1}@{0} ({2})".format(pyrfc3339.generate( - self.meta.creation_dt), self.meta.creation_host, self.id[:4]) - - def __repr__(self): - return "<{0}({1}, {2}, {3})>".format( - self.__class__.__name__, self.regr, self.id, self.meta) - - def __eq__(self, other): - return (isinstance(other, self.__class__) and - self.key == other.key and self.regr == other.regr and - self.meta == other.meta) - - -def report_new_account(config): - """Informs the user about their new ACME account.""" - reporter = zope.component.queryUtility(interfaces.IReporter) - if reporter is None: - return - reporter.add_message( - "Your account credentials have been saved in your Certbot " - "configuration directory at {0}. You should make a secure backup " - "of this folder now. This configuration directory will also " - "contain certificates and private keys obtained by Certbot " - "so making regular backups of this folder is ideal.".format( - config.config_dir), - reporter.MEDIUM_PRIORITY) - - -class AccountMemoryStorage(interfaces.AccountStorage): - """In-memory account storage.""" - - def __init__(self, initial_accounts=None): - self.accounts = initial_accounts if initial_accounts is not None else {} - - def find_all(self): - return list(six.itervalues(self.accounts)) - - def save(self, account, acme): - # pylint: disable=unused-argument - if account.id in self.accounts: - logger.debug("Overwriting account: %s", account.id) - self.accounts[account.id] = account - - def load(self, account_id): - try: - return self.accounts[account_id] - except KeyError: - raise errors.AccountNotFound(account_id) - -class RegistrationResourceWithNewAuthzrURI(messages.RegistrationResource): - """A backwards-compatible RegistrationResource with a new-authz URI. - - Hack: Certbot versions pre-0.11.1 expect to load - new_authzr_uri as part of the account. Because people - sometimes switch between old and new versions, we will - continue to write out this field for some time so older - clients don't crash in that scenario. - """ - new_authzr_uri = jose.Field('new_authzr_uri') - -class AccountFileStorage(interfaces.AccountStorage): - """Accounts file storage. - - :ivar .IConfig config: Client configuration - - """ - def __init__(self, config): - self.config = config - util.make_or_verify_dir(config.accounts_dir, 0o700, compat.os_geteuid(), - self.config.strict_permissions) - - def _account_dir_path(self, account_id): - return self._account_dir_path_for_server_path(account_id, self.config.server_path) - - def _account_dir_path_for_server_path(self, account_id, server_path): - accounts_dir = self.config.accounts_dir_for_server_path(server_path) - return os.path.join(accounts_dir, account_id) - - @classmethod - def _regr_path(cls, account_dir_path): - return os.path.join(account_dir_path, "regr.json") - - @classmethod - def _key_path(cls, account_dir_path): - return os.path.join(account_dir_path, "private_key.json") - - @classmethod - def _metadata_path(cls, account_dir_path): - return os.path.join(account_dir_path, "meta.json") - - def _find_all_for_server_path(self, server_path): - accounts_dir = self.config.accounts_dir_for_server_path(server_path) - try: - candidates = os.listdir(accounts_dir) - except OSError: - return [] - - accounts = [] - for account_id in candidates: - try: - accounts.append(self._load_for_server_path(account_id, server_path)) - except errors.AccountStorageError: - logger.debug("Account loading problem", exc_info=True) - - if not accounts and server_path in constants.LE_REUSE_SERVERS: - # find all for the next link down - prev_server_path = constants.LE_REUSE_SERVERS[server_path] - prev_accounts = self._find_all_for_server_path(prev_server_path) - # if we found something, link to that - if prev_accounts: - try: - self._symlink_to_accounts_dir(prev_server_path, server_path) - except OSError: - return [] - accounts = prev_accounts - return accounts - - def find_all(self): - return self._find_all_for_server_path(self.config.server_path) - - def _symlink_to_account_dir(self, prev_server_path, server_path, account_id): - prev_account_dir = self._account_dir_path_for_server_path(account_id, prev_server_path) - new_account_dir = self._account_dir_path_for_server_path(account_id, server_path) - os.symlink(prev_account_dir, new_account_dir) - - def _symlink_to_accounts_dir(self, prev_server_path, server_path): - accounts_dir = self.config.accounts_dir_for_server_path(server_path) - if os.path.islink(accounts_dir): - os.unlink(accounts_dir) - else: - os.rmdir(accounts_dir) - prev_account_dir = self.config.accounts_dir_for_server_path(prev_server_path) - os.symlink(prev_account_dir, accounts_dir) - - def _load_for_server_path(self, account_id, server_path): - account_dir_path = self._account_dir_path_for_server_path(account_id, server_path) - if not os.path.isdir(account_dir_path): # isdir is also true for symlinks - if server_path in constants.LE_REUSE_SERVERS: - prev_server_path = constants.LE_REUSE_SERVERS[server_path] - prev_loaded_account = self._load_for_server_path(account_id, prev_server_path) - # we didn't error so we found something, so create a symlink to that - accounts_dir = self.config.accounts_dir_for_server_path(server_path) - # If accounts_dir isn't empty, make an account specific symlink - if os.listdir(accounts_dir): - self._symlink_to_account_dir(prev_server_path, server_path, account_id) - else: - self._symlink_to_accounts_dir(prev_server_path, server_path) - return prev_loaded_account - else: - raise errors.AccountNotFound( - "Account at %s does not exist" % account_dir_path) - - try: - with open(self._regr_path(account_dir_path)) as regr_file: - regr = messages.RegistrationResource.json_loads(regr_file.read()) - with open(self._key_path(account_dir_path)) as key_file: - key = jose.JWK.json_loads(key_file.read()) - with open(self._metadata_path(account_dir_path)) as metadata_file: - meta = Account.Meta.json_loads(metadata_file.read()) - except IOError as error: - raise errors.AccountStorageError(error) - - acc = Account(regr, key, meta) - if acc.id != account_id: - raise errors.AccountStorageError( - "Account ids mismatch (expected: {0}, found: {1}".format( - account_id, acc.id)) - return acc - - def load(self, account_id): - return self._load_for_server_path(account_id, self.config.server_path) - - def save(self, account, acme): - self._save(account, acme, regr_only=False) - - def save_regr(self, account, acme): - """Save the registration resource. - - :param Account account: account whose regr should be saved - - """ - self._save(account, acme, regr_only=True) - - def delete(self, account_id): - """Delete registration info from disk - - :param account_id: id of account which should be deleted - - """ - account_dir_path = self._account_dir_path(account_id) - if not os.path.isdir(account_dir_path): - raise errors.AccountNotFound( - "Account at %s does not exist" % account_dir_path) - # Step 1: Delete account specific links and the directory - self._delete_account_dir_for_server_path(account_id, self.config.server_path) - - # Step 2: Remove any accounts links and directories that are now empty - if not os.listdir(self.config.accounts_dir): - self._delete_accounts_dir_for_server_path(self.config.server_path) - - def _delete_account_dir_for_server_path(self, account_id, server_path): - link_func = functools.partial(self._account_dir_path_for_server_path, account_id) - nonsymlinked_dir = self._delete_links_and_find_target_dir(server_path, link_func) - shutil.rmtree(nonsymlinked_dir) - - def _delete_accounts_dir_for_server_path(self, server_path): - link_func = self.config.accounts_dir_for_server_path - nonsymlinked_dir = self._delete_links_and_find_target_dir(server_path, link_func) - os.rmdir(nonsymlinked_dir) - - def _delete_links_and_find_target_dir(self, server_path, link_func): - """Delete symlinks and return the nonsymlinked directory path. - - :param str server_path: file path based on server - :param callable link_func: callable that returns possible links - given a server_path - - :returns: the final, non-symlinked target - :rtype: str - - """ - dir_path = link_func(server_path) - - # does an appropriate directory link to me? if so, make sure that's gone - reused_servers = {} - for k in constants.LE_REUSE_SERVERS: - reused_servers[constants.LE_REUSE_SERVERS[k]] = k - - # is there a next one up? - possible_next_link = True - while possible_next_link: - possible_next_link = False - if server_path in reused_servers: - next_server_path = reused_servers[server_path] - next_dir_path = link_func(next_server_path) - if os.path.islink(next_dir_path) and os.readlink(next_dir_path) == dir_path: - possible_next_link = True - server_path = next_server_path - dir_path = next_dir_path - - # if there's not a next one up to delete, then delete me - # and whatever I link to - while os.path.islink(dir_path): - target = os.readlink(dir_path) - os.unlink(dir_path) - dir_path = target - - return dir_path - - def _save(self, account, acme, regr_only): - account_dir_path = self._account_dir_path(account.id) - util.make_or_verify_dir(account_dir_path, 0o700, compat.os_geteuid(), - self.config.strict_permissions) - try: - with open(self._regr_path(account_dir_path), "w") as regr_file: - regr = account.regr - # If we have a value for new-authz, save it for forwards - # compatibility with older versions of Certbot. If we don't - # have a value for new-authz, this is an ACMEv2 directory where - # an older version of Certbot won't work anyway. - if hasattr(acme.directory, "new-authz"): - regr = RegistrationResourceWithNewAuthzrURI( - new_authzr_uri=acme.directory.new_authz, - body={}, - uri=regr.uri) - else: - regr = messages.RegistrationResource( - body={}, - uri=regr.uri) - regr_file.write(regr.json_dumps()) - if not regr_only: - with util.safe_open(self._key_path(account_dir_path), - "w", chmod=0o400) as key_file: - key_file.write(account.key.json_dumps()) - with open(self._metadata_path( - account_dir_path), "w") as metadata_file: - metadata_file.write(account.meta.json_dumps()) - except IOError as error: - raise errors.AccountStorageError(error) diff --git a/certbot/achallenges.py b/certbot/achallenges.py deleted file mode 100644 index 6535a6b630d..00000000000 --- a/certbot/achallenges.py +++ /dev/null @@ -1,61 +0,0 @@ -"""Client annotated ACME challenges. - -Please use names such as ``achall`` to distinguish from variables "of type" -:class:`acme.challenges.Challenge` (denoted by ``chall``) -and :class:`.ChallengeBody` (denoted by ``challb``):: - - from acme import challenges - from acme import messages - from certbot import achallenges - - chall = challenges.DNS(token='foo') - challb = messages.ChallengeBody(chall=chall) - achall = achallenges.DNS(chall=challb, domain='example.com') - -Note, that all annotated challenges act as a proxy objects:: - - achall.token == challb.token - -""" -import logging - -import josepy as jose - -from acme import challenges - - -logger = logging.getLogger(__name__) - - -# pylint: disable=too-few-public-methods - -class AnnotatedChallenge(jose.ImmutableMap): - """Client annotated challenge. - - Wraps around server provided challenge and annotates with data - useful for the client. - - :ivar challb: Wrapped `~.ChallengeBody`. - - """ - __slots__ = ('challb',) - acme_type = NotImplemented - - def __getattr__(self, name): - return getattr(self.challb, name) - - -class KeyAuthorizationAnnotatedChallenge(AnnotatedChallenge): - """Client annotated `KeyAuthorizationChallenge` challenge.""" - __slots__ = ('challb', 'domain', 'account_key') - - def response_and_validation(self, *args, **kwargs): - """Generate response and validation.""" - return self.challb.chall.response_and_validation( - self.account_key, *args, **kwargs) - - -class DNS(AnnotatedChallenge): - """Client annotated "dns" ACME challenge.""" - __slots__ = ('challb', 'domain') - acme_type = challenges.DNS diff --git a/certbot/auth_handler.py b/certbot/auth_handler.py deleted file mode 100644 index 3dfaaf26f4e..00000000000 --- a/certbot/auth_handler.py +++ /dev/null @@ -1,576 +0,0 @@ -"""ACME AuthHandler.""" -import collections -import logging -import time - -import six -import zope.component - -from acme import challenges -from acme import messages -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import DefaultDict, Dict, List, Set, Collection -# pylint: enable=unused-import, no-name-in-module -from certbot import achallenges -from certbot import errors -from certbot import error_handler -from certbot import interfaces - - -logger = logging.getLogger(__name__) - - -AnnotatedAuthzr = collections.namedtuple("AnnotatedAuthzr", ["authzr", "achalls"]) -"""Stores an authorization resource and its active annotated challenges.""" - - -class AuthHandler(object): - """ACME Authorization Handler for a client. - - :ivar auth: Authenticator capable of solving - :class:`~acme.challenges.Challenge` types - :type auth: :class:`certbot.interfaces.IAuthenticator` - - :ivar acme.client.BackwardsCompatibleClientV2 acme_client: ACME client API. - - :ivar account: Client's Account - :type account: :class:`certbot.account.Account` - - :ivar list pref_challs: sorted user specified preferred challenges - type strings with the most preferred challenge listed first - - """ - def __init__(self, auth, acme_client, account, pref_challs): - self.auth = auth - self.acme = acme_client - - self.account = account - self.pref_challs = pref_challs - - def handle_authorizations(self, orderr, best_effort=False): - """Retrieve all authorizations for challenges. - - :param acme.messages.OrderResource orderr: must have - authorizations filled in - :param bool best_effort: Whether or not all authorizations are - required (this is useful in renewal) - - :returns: List of authorization resources - :rtype: list - - :raises .AuthorizationError: If unable to retrieve all - authorizations - - """ - aauthzrs = [AnnotatedAuthzr(authzr, []) - for authzr in orderr.authorizations] - - self._choose_challenges(aauthzrs) - config = zope.component.getUtility(interfaces.IConfig) - notify = zope.component.getUtility(interfaces.IDisplay).notification - - # While there are still challenges remaining... - while self._has_challenges(aauthzrs): - with error_handler.ExitHandler(self._cleanup_challenges, aauthzrs): - resp = self._solve_challenges(aauthzrs) - logger.info("Waiting for verification...") - if config.debug_challenges: - notify('Challenges loaded. Press continue to submit to CA. ' - 'Pass "-v" for more info about challenges.', pause=True) - - # Send all Responses - this modifies achalls - self._respond(aauthzrs, resp, best_effort) - - # Just make sure all decisions are complete. - self.verify_authzr_complete(aauthzrs) - - # Only return valid authorizations - ret_val = [aauthzr.authzr for aauthzr in aauthzrs - if aauthzr.authzr.body.status == messages.STATUS_VALID] - - if not ret_val: - raise errors.AuthorizationError( - "Challenges failed for all domains") - - return ret_val - - def _choose_challenges(self, aauthzrs): - """ - Retrieve necessary and pending challenges to satisfy server. - NB: Necessary and already validated challenges are not retrieved, - as they can be reused for a certificate issuance. - """ - pending_authzrs = [aauthzr for aauthzr in aauthzrs - if aauthzr.authzr.body.status != messages.STATUS_VALID] - if pending_authzrs: - logger.info("Performing the following challenges:") - for aauthzr in pending_authzrs: - aauthzr_challenges = aauthzr.authzr.body.challenges - if self.acme.acme_version == 1: - combinations = aauthzr.authzr.body.combinations - else: - combinations = tuple((i,) for i in range(len(aauthzr_challenges))) - - path = gen_challenge_path( - aauthzr_challenges, - self._get_chall_pref(aauthzr.authzr.body.identifier.value), - combinations) - - aauthzr_achalls = self._challenge_factory( - aauthzr.authzr, path) - aauthzr.achalls.extend(aauthzr_achalls) - - for aauthzr in aauthzrs: - for achall in aauthzr.achalls: - if isinstance(achall.chall, challenges.TLSSNI01): - logger.warning("TLS-SNI-01 is deprecated, and will stop working soon.") - return - - def _has_challenges(self, aauthzrs): - """Do we have any challenges to perform?""" - return any(aauthzr.achalls for aauthzr in aauthzrs) - - def _solve_challenges(self, aauthzrs): - """Get Responses for challenges from authenticators.""" - resp = [] # type: Collection[challenges.ChallengeResponse] - all_achalls = self._get_all_achalls(aauthzrs) - try: - if all_achalls: - resp = self.auth.perform(all_achalls) - except errors.AuthorizationError: - logger.critical("Failure in setting up challenges.") - logger.info("Attempting to clean up outstanding challenges...") - raise - - assert len(resp) == len(all_achalls) - - return resp - - def _get_all_achalls(self, aauthzrs): - """Return all active challenges.""" - all_achalls = [] # type: Collection[challenges.ChallengeResponse] - for aauthzr in aauthzrs: - all_achalls.extend(aauthzr.achalls) - return all_achalls - - def _respond(self, aauthzrs, resp, best_effort): - """Send/Receive confirmation of all challenges. - - .. note:: This method also cleans up the auth_handler state. - - """ - # TODO: chall_update is a dirty hack to get around acme-spec #105 - chall_update = dict() \ - # type: Dict[int, List[achallenges.KeyAuthorizationAnnotatedChallenge]] - self._send_responses(aauthzrs, resp, chall_update) - - # Check for updated status... - self._poll_challenges(aauthzrs, chall_update, best_effort) - - def _send_responses(self, aauthzrs, resps, chall_update): - """Send responses and make sure errors are handled. - - :param aauthzrs: authorizations and the selected annotated challenges - to try and perform - :type aauthzrs: `list` of `AnnotatedAuthzr` - :param resps: challenge responses from the authenticator where - each response at index i corresponds to the annotated - challenge at index i in the list returned by - :func:`_get_all_achalls` - :type resps: `collections.abc.Iterable` of - :class:`~acme.challenges.ChallengeResponse` or `False` or - `None` - :param dict chall_update: parameter that is updated to hold - aauthzr index to list of outstanding solved annotated challenges - - """ - active_achalls = [] - resps_iter = iter(resps) - for i, aauthzr in enumerate(aauthzrs): - for achall in aauthzr.achalls: - # This line needs to be outside of the if block below to - # ensure failed challenges are cleaned up correctly - active_achalls.append(achall) - - resp = next(resps_iter) - # Don't send challenges for None and False authenticator responses - if resp: - self.acme.answer_challenge(achall.challb, resp) - # TODO: answer_challenge returns challr, with URI, - # that can be used in _find_updated_challr - # comparisons... - chall_update.setdefault(i, []).append(achall) - - return active_achalls - - def _poll_challenges(self, aauthzrs, chall_update, - best_effort, min_sleep=3, max_rounds=30): - """Wait for all challenge results to be determined.""" - indices_to_check = set(chall_update.keys()) - comp_indices = set() - rounds = 0 - - while indices_to_check and rounds < max_rounds: - # TODO: Use retry-after... - time.sleep(min_sleep) - all_failed_achalls = set() # type: Set[achallenges.KeyAuthorizationAnnotatedChallenge] - for index in indices_to_check: - comp_achalls, failed_achalls = self._handle_check( - aauthzrs, index, chall_update[index]) - - if len(comp_achalls) == len(chall_update[index]): - comp_indices.add(index) - elif not failed_achalls: - for achall, _ in comp_achalls: - chall_update[index].remove(achall) - # We failed some challenges... damage control - else: - if best_effort: - comp_indices.add(index) - logger.warning( - "Challenge failed for domain %s", - aauthzrs[index].authzr.body.identifier.value) - else: - all_failed_achalls.update( - updated for _, updated in failed_achalls) - - if all_failed_achalls: - _report_failed_challs(all_failed_achalls) - raise errors.FailedChallenges(all_failed_achalls) - - indices_to_check -= comp_indices - comp_indices.clear() - rounds += 1 - - def _handle_check(self, aauthzrs, index, achalls): - """Returns tuple of ('completed', 'failed').""" - completed = [] - failed = [] - - original_aauthzr = aauthzrs[index] - updated_authzr, _ = self.acme.poll(original_aauthzr.authzr) - aauthzrs[index] = AnnotatedAuthzr(updated_authzr, original_aauthzr.achalls) - if updated_authzr.body.status == messages.STATUS_VALID: - return achalls, [] - - # Note: if the whole authorization is invalid, the individual failed - # challenges will be determined here... - for achall in achalls: - updated_achall = achall.update(challb=self._find_updated_challb( - updated_authzr, achall)) - - # This does nothing for challenges that have yet to be decided yet. - if updated_achall.status == messages.STATUS_VALID: - completed.append((achall, updated_achall)) - elif updated_achall.status == messages.STATUS_INVALID: - failed.append((achall, updated_achall)) - - return completed, failed - - def _find_updated_challb(self, authzr, achall): # pylint: disable=no-self-use - """Find updated challenge body within Authorization Resource. - - .. warning:: This assumes only one instance of type of challenge in - each challenge resource. - - :param .AuthorizationResource authzr: Authorization Resource - :param .AnnotatedChallenge achall: Annotated challenge for which - to get status - - """ - for authzr_challb in authzr.body.challenges: - if type(authzr_challb.chall) is type(achall.challb.chall): # noqa - return authzr_challb - raise errors.AuthorizationError( - "Target challenge not found in authorization resource") - - def _get_chall_pref(self, domain): - """Return list of challenge preferences. - - :param str domain: domain for which you are requesting preferences - - """ - chall_prefs = [] - # Make sure to make a copy... - plugin_pref = self.auth.get_chall_pref(domain) - if self.pref_challs: - plugin_pref_types = set(chall.typ for chall in plugin_pref) - for typ in self.pref_challs: - if typ in plugin_pref_types: - chall_prefs.append(challenges.Challenge.TYPES[typ]) - if chall_prefs: - return chall_prefs - raise errors.AuthorizationError( - "None of the preferred challenges " - "are supported by the selected plugin") - chall_prefs.extend(plugin_pref) - return chall_prefs - - def _cleanup_challenges(self, aauthzrs, achalls=None): - """Cleanup challenges. - - :param aauthzrs: authorizations and their selected annotated - challenges - :type aauthzrs: `list` of `AnnotatedAuthzr` - :param achalls: annotated challenges to cleanup - :type achalls: `list` of :class:`certbot.achallenges.AnnotatedChallenge` - - """ - logger.info("Cleaning up challenges") - if achalls is None: - achalls = self._get_all_achalls(aauthzrs) - if achalls: - self.auth.cleanup(achalls) - for achall in achalls: - for aauthzr in aauthzrs: - if achall in aauthzr.achalls: - aauthzr.achalls.remove(achall) - break - - def verify_authzr_complete(self, aauthzrs): - """Verifies that all authorizations have been decided. - - :param aauthzrs: authorizations and their selected annotated - challenges - :type aauthzrs: `list` of `AnnotatedAuthzr` - - :returns: Whether all authzr are complete - :rtype: bool - - """ - for aauthzr in aauthzrs: - authzr = aauthzr.authzr - if (authzr.body.status != messages.STATUS_VALID and - authzr.body.status != messages.STATUS_INVALID): - raise errors.AuthorizationError("Incomplete authorizations") - - def _challenge_factory(self, authzr, path): - """Construct Namedtuple Challenges - - :param messages.AuthorizationResource authzr: authorization - - :param list path: List of indices from `challenges`. - - :returns: achalls, list of challenge type - :class:`certbot.achallenges.Indexed` - :rtype: list - - :raises .errors.Error: if challenge type is not recognized - - """ - achalls = [] - - for index in path: - challb = authzr.body.challenges[index] - achalls.append(challb_to_achall( - challb, self.account.key, authzr.body.identifier.value)) - - return achalls - - -def challb_to_achall(challb, account_key, domain): - """Converts a ChallengeBody object to an AnnotatedChallenge. - - :param .ChallengeBody challb: ChallengeBody - :param .JWK account_key: Authorized Account Key - :param str domain: Domain of the challb - - :returns: Appropriate AnnotatedChallenge - :rtype: :class:`certbot.achallenges.AnnotatedChallenge` - - """ - chall = challb.chall - logger.info("%s challenge for %s", chall.typ, domain) - - if isinstance(chall, challenges.KeyAuthorizationChallenge): - return achallenges.KeyAuthorizationAnnotatedChallenge( - challb=challb, domain=domain, account_key=account_key) - elif isinstance(chall, challenges.DNS): - return achallenges.DNS(challb=challb, domain=domain) - else: - raise errors.Error( - "Received unsupported challenge of type: %s", chall.typ) - - -def gen_challenge_path(challbs, preferences, combinations): - """Generate a plan to get authority over the identity. - - .. todo:: This can be possibly be rewritten to use resolved_combinations. - - :param tuple challbs: A tuple of challenges - (:class:`acme.messages.Challenge`) from - :class:`acme.messages.AuthorizationResource` to be - fulfilled by the client in order to prove possession of the - identifier. - - :param list preferences: List of challenge preferences for domain - (:class:`acme.challenges.Challenge` subclasses) - - :param tuple combinations: A collection of sets of challenges from - :class:`acme.messages.Challenge`, each of which would - be sufficient to prove possession of the identifier. - - :returns: tuple of indices from ``challenges``. - :rtype: tuple - - :raises certbot.errors.AuthorizationError: If a - path cannot be created that satisfies the CA given the preferences and - combinations. - - """ - if combinations: - return _find_smart_path(challbs, preferences, combinations) - else: - return _find_dumb_path(challbs, preferences) - - -def _find_smart_path(challbs, preferences, combinations): - """Find challenge path with server hints. - - Can be called if combinations is included. Function uses a simple - ranking system to choose the combo with the lowest cost. - - """ - chall_cost = {} - max_cost = 1 - for i, chall_cls in enumerate(preferences): - chall_cost[chall_cls] = i - max_cost += i - - # max_cost is now equal to sum(indices) + 1 - - best_combo = None - # Set above completing all of the available challenges - best_combo_cost = max_cost - - combo_total = 0 - for combo in combinations: - for challenge_index in combo: - combo_total += chall_cost.get(challbs[ - challenge_index].chall.__class__, max_cost) - - if combo_total < best_combo_cost: - best_combo = combo - best_combo_cost = combo_total - - combo_total = 0 - - if not best_combo: - _report_no_chall_path(challbs) - - return best_combo - - -def _find_dumb_path(challbs, preferences): - """Find challenge path without server hints. - - Should be called if the combinations hint is not included by the - server. This function either returns a path containing all - challenges provided by the CA or raises an exception. - - """ - path = [] - for i, challb in enumerate(challbs): - # supported is set to True if the challenge type is supported - supported = next((True for pref_c in preferences - if isinstance(challb.chall, pref_c)), False) - if supported: - path.append(i) - else: - _report_no_chall_path(challbs) - - return path - - -def _report_no_chall_path(challbs): - """Logs and raises an error that no satisfiable chall path exists. - - :param challbs: challenges from the authorization that can't be satisfied - - """ - msg = ("Client with the currently selected authenticator does not support " - "any combination of challenges that will satisfy the CA.") - if len(challbs) == 1 and isinstance(challbs[0].chall, challenges.DNS01): - msg += ( - " You may need to use an authenticator " - "plugin that can do challenges over DNS.") - logger.critical(msg) - raise errors.AuthorizationError(msg) - - -_ERROR_HELP_COMMON = ( - "To fix these errors, please make sure that your domain name was entered " - "correctly and the DNS A/AAAA record(s) for that domain contain(s) the " - "right IP address.") - - -_ERROR_HELP = { - "connection": - _ERROR_HELP_COMMON + " Additionally, please check that your computer " - "has a publicly routable IP address and that no firewalls are preventing " - "the server from communicating with the client. If you're using the " - "webroot plugin, you should also verify that you are serving files " - "from the webroot path you provided.", - "dnssec": - _ERROR_HELP_COMMON + " Additionally, if you have DNSSEC enabled for " - "your domain, please ensure that the signature is valid.", - "malformed": - "To fix these errors, please make sure that you did not provide any " - "invalid information to the client, and try running Certbot " - "again.", - "serverInternal": - "Unfortunately, an error on the ACME server prevented you from completing " - "authorization. Please try again later.", - "tls": - _ERROR_HELP_COMMON + " Additionally, please check that you have an " - "up-to-date TLS configuration that allows the server to communicate " - "with the Certbot client.", - "unauthorized": _ERROR_HELP_COMMON, - "unknownHost": _ERROR_HELP_COMMON, -} - - -def _report_failed_challs(failed_achalls): - """Notifies the user about failed challenges. - - :param set failed_achalls: A set of failed - :class:`certbot.achallenges.AnnotatedChallenge`. - - """ - problems = collections.defaultdict(list)\ - # type: DefaultDict[str, List[achallenges.KeyAuthorizationAnnotatedChallenge]] - for achall in failed_achalls: - if achall.error: - problems[achall.error.typ].append(achall) - reporter = zope.component.getUtility(interfaces.IReporter) - for achalls in six.itervalues(problems): - reporter.add_message( - _generate_failed_chall_msg(achalls), reporter.MEDIUM_PRIORITY) - - -def _generate_failed_chall_msg(failed_achalls): - """Creates a user friendly error message about failed challenges. - - :param list failed_achalls: A list of failed - :class:`certbot.achallenges.AnnotatedChallenge` with the same error - type. - - :returns: A formatted error message for the client. - :rtype: str - - """ - error = failed_achalls[0].error - typ = error.typ - if messages.is_acme_error(error): - typ = error.code - msg = ["The following errors were reported by the server:"] - - for achall in failed_achalls: - msg.append("\n\nDomain: %s\nType: %s\nDetail: %s" % ( - achall.domain, typ, achall.error.detail)) - - if typ in _ERROR_HELP: - msg.append("\n\n") - msg.append(_ERROR_HELP[typ]) - - return "".join(msg) diff --git a/certbot/cert_manager.py b/certbot/cert_manager.py deleted file mode 100644 index 2a67f87659f..00000000000 --- a/certbot/cert_manager.py +++ /dev/null @@ -1,389 +0,0 @@ -"""Tools for managing certificates.""" -import datetime -import logging -import os -import pytz -import re -import traceback -import zope.component - -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module -from certbot import compat -from certbot import crypto_util -from certbot import errors -from certbot import interfaces -from certbot import ocsp -from certbot import storage -from certbot import util - -from certbot.display import util as display_util - -logger = logging.getLogger(__name__) - -################### -# Commands -################### - -def update_live_symlinks(config): - """Update the certificate file family symlinks to use archive_dir. - - Use the information in the config file to make symlinks point to - the correct archive directory. - - .. note:: This assumes that the installation is using a Reverter object. - - :param config: Configuration. - :type config: :class:`certbot.configuration.NamespaceConfig` - - """ - for renewal_file in storage.renewal_conf_files(config): - storage.RenewableCert(renewal_file, config, update_symlinks=True) - -def rename_lineage(config): - """Rename the specified lineage to the new name. - - :param config: Configuration. - :type config: :class:`certbot.configuration.NamespaceConfig` - - """ - disp = zope.component.getUtility(interfaces.IDisplay) - - certname = get_certnames(config, "rename")[0] - - new_certname = config.new_certname - if not new_certname: - code, new_certname = disp.input( - "Enter the new name for certificate {0}".format(certname), - flag="--updated-cert-name", force_interactive=True) - if code != display_util.OK or not new_certname: - raise errors.Error("User ended interaction.") - - lineage = lineage_for_certname(config, certname) - if not lineage: - raise errors.ConfigurationError("No existing certificate with name " - "{0} found.".format(certname)) - storage.rename_renewal_config(certname, new_certname, config) - disp.notification("Successfully renamed {0} to {1}." - .format(certname, new_certname), pause=False) - -def certificates(config): - """Display information about certs configured with Certbot - - :param config: Configuration. - :type config: :class:`certbot.configuration.NamespaceConfig` - """ - parsed_certs = [] - parse_failures = [] - for renewal_file in storage.renewal_conf_files(config): - try: - renewal_candidate = storage.RenewableCert(renewal_file, config) - crypto_util.verify_renewable_cert(renewal_candidate) - parsed_certs.append(renewal_candidate) - except Exception as e: # pylint: disable=broad-except - logger.warning("Renewal configuration file %s produced an " - "unexpected error: %s. Skipping.", renewal_file, e) - logger.debug("Traceback was:\n%s", traceback.format_exc()) - parse_failures.append(renewal_file) - - # Describe all the certs - _describe_certs(config, parsed_certs, parse_failures) - -def delete(config): - """Delete Certbot files associated with a certificate lineage.""" - certnames = get_certnames(config, "delete", allow_multiple=True) - for certname in certnames: - storage.delete_files(config, certname) - disp = zope.component.getUtility(interfaces.IDisplay) - disp.notification("Deleted all files relating to certificate {0}." - .format(certname), pause=False) - -################### -# Public Helpers -################### - -def lineage_for_certname(cli_config, certname): - """Find a lineage object with name certname.""" - configs_dir = cli_config.renewal_configs_dir - # Verify the directory is there - util.make_or_verify_dir(configs_dir, mode=0o755, uid=compat.os_geteuid()) - try: - renewal_file = storage.renewal_file_for_certname(cli_config, certname) - except errors.CertStorageError: - return None - try: - return storage.RenewableCert(renewal_file, cli_config) - except (errors.CertStorageError, IOError): - logger.debug("Renewal conf file %s is broken.", renewal_file) - logger.debug("Traceback was:\n%s", traceback.format_exc()) - return None - -def domains_for_certname(config, certname): - """Find the domains in the cert with name certname.""" - lineage = lineage_for_certname(config, certname) - return lineage.names() if lineage else None - -def find_duplicative_certs(config, domains): - """Find existing certs that match the given domain names. - - This function searches for certificates whose domains are equal to - the `domains` parameter and certificates whose domains are a subset - of the domains in the `domains` parameter. If multiple certificates - are found whose names are a subset of `domains`, the one whose names - are the largest subset of `domains` is returned. - - If multiple certificates' domains are an exact match or equally - sized subsets, which matching certificates are returned is - undefined. - - :param config: Configuration. - :type config: :class:`certbot.configuration.NamespaceConfig` - :param domains: List of domain names - :type domains: `list` of `str` - - :returns: lineages representing the identically matching cert and the - largest subset if they exist - :rtype: `tuple` of `storage.RenewableCert` or `None` - - """ - def update_certs_for_domain_matches(candidate_lineage, rv): - """Return cert as identical_names_cert if it matches, - or subset_names_cert if it matches as subset - """ - # TODO: Handle these differently depending on whether they are - # expired or still valid? - identical_names_cert, subset_names_cert = rv - candidate_names = set(candidate_lineage.names()) - if candidate_names == set(domains): - identical_names_cert = candidate_lineage - elif candidate_names.issubset(set(domains)): - # This logic finds and returns the largest subset-names cert - # in the case where there are several available. - if subset_names_cert is None: - subset_names_cert = candidate_lineage - elif len(candidate_names) > len(subset_names_cert.names()): - subset_names_cert = candidate_lineage - return (identical_names_cert, subset_names_cert) - - return _search_lineages(config, update_certs_for_domain_matches, (None, None)) - -def _archive_files(candidate_lineage, filetype): - """ In order to match things like: - /etc/letsencrypt/archive/example.com/chain1.pem. - - Anonymous functions which call this function are eventually passed (in a list) to - `match_and_check_overlaps` to help specify the acceptable_matches. - - :param `.storage.RenewableCert` candidate_lineage: Lineage whose archive dir is to - be searched. - :param str filetype: main file name prefix e.g. "fullchain" or "chain". - - :returns: Files in candidate_lineage's archive dir that match the provided filetype. - :rtype: list of str or None - """ - archive_dir = candidate_lineage.archive_dir - pattern = [os.path.join(archive_dir, f) for f in os.listdir(archive_dir) - if re.match("{0}[0-9]*.pem".format(filetype), f)] - if len(pattern) > 0: - return pattern - else: - return None - -def _acceptable_matches(): - """ Generates the list that's passed to match_and_check_overlaps. Is its own function to - make unit testing easier. - - :returns: list of functions - :rtype: list - """ - return [lambda x: x.fullchain_path, lambda x: x.cert_path, - lambda x: _archive_files(x, "cert"), lambda x: _archive_files(x, "fullchain")] - -def cert_path_to_lineage(cli_config): - """ If config.cert_path is defined, try to find an appropriate value for config.certname. - - :param `configuration.NamespaceConfig` cli_config: parsed command line arguments - - :returns: a lineage name - :rtype: str - - :raises `errors.Error`: If the specified cert path can't be matched to a lineage name. - :raises `errors.OverlappingMatchFound`: If the matched lineage's archive is shared. - """ - acceptable_matches = _acceptable_matches() - match = match_and_check_overlaps(cli_config, acceptable_matches, - lambda x: cli_config.cert_path[0], lambda x: x.lineagename) - return match[0] - -def match_and_check_overlaps(cli_config, acceptable_matches, match_func, rv_func): - """ Searches through all lineages for a match, and checks for duplicates. - If a duplicate is found, an error is raised, as performing operations on lineages - that have their properties incorrectly duplicated elsewhere is probably a bad idea. - - :param `configuration.NamespaceConfig` cli_config: parsed command line arguments - :param list acceptable_matches: a list of functions that specify acceptable matches - :param function match_func: specifies what to match - :param function rv_func: specifies what to return - - """ - def find_matches(candidate_lineage, return_value, acceptable_matches): - """Returns a list of matches using _search_lineages.""" - acceptable_matches = [func(candidate_lineage) for func in acceptable_matches] - acceptable_matches_rv = [] # type: List[str] - for item in acceptable_matches: - if isinstance(item, list): - acceptable_matches_rv += item - else: - acceptable_matches_rv.append(item) - match = match_func(candidate_lineage) - if match in acceptable_matches_rv: - return_value.append(rv_func(candidate_lineage)) - return return_value - - matched = _search_lineages(cli_config, find_matches, [], acceptable_matches) - if not matched: - raise errors.Error("No match found for cert-path {0}!".format(cli_config.cert_path[0])) - elif len(matched) > 1: - raise errors.OverlappingMatchFound() - else: - return matched - -def human_readable_cert_info(config, cert, skip_filter_checks=False): - """ Returns a human readable description of info about a RenewableCert object""" - certinfo = [] - checker = ocsp.RevocationChecker() - - if config.certname and cert.lineagename != config.certname and not skip_filter_checks: - return "" - if config.domains and not set(config.domains).issubset(cert.names()): - return "" - now = pytz.UTC.fromutc(datetime.datetime.utcnow()) - - reasons = [] - if cert.is_test_cert: - reasons.append('TEST_CERT') - if cert.target_expiry <= now: - reasons.append('EXPIRED') - if checker.ocsp_revoked(cert.cert, cert.chain): - reasons.append('REVOKED') - - if reasons: - status = "INVALID: " + ", ".join(reasons) - else: - diff = cert.target_expiry - now - if diff.days == 1: - status = "VALID: 1 day" - elif diff.days < 1: - status = "VALID: {0} hour(s)".format(diff.seconds // 3600) - else: - status = "VALID: {0} days".format(diff.days) - - valid_string = "{0} ({1})".format(cert.target_expiry, status) - certinfo.append(" Certificate Name: {0}\n" - " Domains: {1}\n" - " Expiry Date: {2}\n" - " Certificate Path: {3}\n" - " Private Key Path: {4}".format( - cert.lineagename, - " ".join(cert.names()), - valid_string, - cert.fullchain, - cert.privkey)) - return "".join(certinfo) - -def get_certnames(config, verb, allow_multiple=False, custom_prompt=None): - """Get certname from flag, interactively, or error out. - """ - certname = config.certname - if certname: - certnames = [certname] - else: - disp = zope.component.getUtility(interfaces.IDisplay) - filenames = storage.renewal_conf_files(config) - choices = [storage.lineagename_for_filename(name) for name in filenames] - if not choices: - raise errors.Error("No existing certificates found.") - if allow_multiple: - if not custom_prompt: - prompt = "Which certificate(s) would you like to {0}?".format(verb) - else: - prompt = custom_prompt - code, certnames = disp.checklist( - prompt, choices, cli_flag="--cert-name", force_interactive=True) - if code != display_util.OK: - raise errors.Error("User ended interaction.") - else: - if not custom_prompt: - prompt = "Which certificate would you like to {0}?".format(verb) - else: - prompt = custom_prompt - - code, index = disp.menu( - prompt, choices, cli_flag="--cert-name", force_interactive=True) - - if code != display_util.OK or index not in range(0, len(choices)): - raise errors.Error("User ended interaction.") - certnames = [choices[index]] - return certnames - -################### -# Private Helpers -################### - -def _report_lines(msgs): - """Format a results report for a category of single-line renewal outcomes""" - return " " + "\n ".join(str(msg) for msg in msgs) - -def _report_human_readable(config, parsed_certs): - """Format a results report for a parsed cert""" - certinfo = [] - for cert in parsed_certs: - certinfo.append(human_readable_cert_info(config, cert)) - return "\n".join(certinfo) - -def _describe_certs(config, parsed_certs, parse_failures): - """Print information about the certs we know about""" - out = [] # type: List[str] - - notify = out.append - - if not parsed_certs and not parse_failures: - notify("No certs found.") - else: - if parsed_certs: - match = "matching " if config.certname or config.domains else "" - notify("Found the following {0}certs:".format(match)) - notify(_report_human_readable(config, parsed_certs)) - if parse_failures: - notify("\nThe following renewal configurations " - "were invalid:") - notify(_report_lines(parse_failures)) - - disp = zope.component.getUtility(interfaces.IDisplay) - disp.notification("\n".join(out), pause=False, wrap=False) - -def _search_lineages(cli_config, func, initial_rv, *args): - """Iterate func over unbroken lineages, allowing custom return conditions. - - Allows flexible customization of return values, including multiple - return values and complex checks. - - :param `configuration.NamespaceConfig` cli_config: parsed command line arguments - :param function func: function used while searching over lineages - :param initial_rv: initial return value of the function (any type) - - :returns: Whatever was specified by `func` if a match is found. - """ - configs_dir = cli_config.renewal_configs_dir - # Verify the directory is there - util.make_or_verify_dir(configs_dir, mode=0o755, uid=compat.os_geteuid()) - - rv = initial_rv - for renewal_file in storage.renewal_conf_files(cli_config): - try: - candidate_lineage = storage.RenewableCert(renewal_file, cli_config) - except (errors.CertStorageError, IOError): - logger.debug("Renewal conf file %s is broken. Skipping.", renewal_file) - logger.debug("Traceback was:\n%s", traceback.format_exc()) - continue - rv = func(candidate_lineage, rv, *args) - return rv diff --git a/certbot/cli.py b/certbot/cli.py deleted file mode 100644 index 31f55711fb6..00000000000 --- a/certbot/cli.py +++ /dev/null @@ -1,1612 +0,0 @@ -"""Certbot command line argument & config processing.""" -# pylint: disable=too-many-lines -from __future__ import print_function -import argparse -import copy -import glob -import logging -import logging.handlers -import os -import sys - -import configargparse -import six -import zope.component -import zope.interface - -from zope.interface import interfaces as zope_interfaces - -from acme import challenges -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import Any, Dict, Optional -# pylint: enable=unused-import, no-name-in-module - -import certbot - -from certbot import constants -from certbot import crypto_util -from certbot import errors -from certbot import hooks -from certbot import interfaces -from certbot import util - -from certbot.display import util as display_util -from certbot.plugins import disco as plugins_disco -import certbot.plugins.enhancements as enhancements -import certbot.plugins.selection as plugin_selection - -logger = logging.getLogger(__name__) - -# Global, to save us from a lot of argument passing within the scope of this module -helpful_parser = None # type: Optional[HelpfulArgumentParser] - -# For help strings, figure out how the user ran us. -# When invoked from letsencrypt-auto, sys.argv[0] is something like: -# "/home/user/.local/share/certbot/bin/certbot" -# Note that this won't work if the user set VENV_PATH or XDG_DATA_HOME before -# running letsencrypt-auto (and sudo stops us from seeing if they did), so it -# should only be used for purposes where inability to detect letsencrypt-auto -# fails safely - -LEAUTO = "letsencrypt-auto" -if "CERTBOT_AUTO" in os.environ: - # if we're here, this is probably going to be certbot-auto, unless the - # user saved the script under a different name - LEAUTO = os.path.basename(os.environ["CERTBOT_AUTO"]) - -old_path_fragment = os.path.join(".local", "share", "letsencrypt") -new_path_prefix = os.path.abspath(os.path.join(os.sep, "opt", - "eff.org", "certbot", "venv")) -if old_path_fragment in sys.argv[0] or sys.argv[0].startswith(new_path_prefix): - cli_command = LEAUTO -else: - cli_command = "certbot" - -# Argparse's help formatting has a lot of unhelpful peculiarities, so we want -# to replace as much of it as we can... - -# This is the stub to include in help generated by argparse -SHORT_USAGE = """ - {0} [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ... - -Certbot can obtain and install HTTPS/TLS/SSL certificates. By default, -it will attempt to use a webserver both for obtaining and installing the -certificate. """.format(cli_command) - -# This section is used for --help and --help all ; it needs information -# about installed plugins to be fully formatted -COMMAND_OVERVIEW = """The most common SUBCOMMANDS and flags are: - -obtain, install, and renew certificates: - (default) run Obtain & install a certificate in your current webserver - certonly Obtain or renew a certificate, but do not install it - renew Renew all previously obtained certificates that are near expiry - enhance Add security enhancements to your existing configuration - -d DOMAINS Comma-separated list of domains to obtain a certificate for - - %s - --standalone Run a standalone webserver for authentication - %s - --webroot Place files in a server's webroot folder for authentication - --manual Obtain certificates interactively, or using shell script hooks - - -n Run non-interactively - --test-cert Obtain a test certificate from a staging server - --dry-run Test "renew" or "certonly" without saving any certificates to disk - -manage certificates: - certificates Display information about certificates you have from Certbot - revoke Revoke a certificate (supply --cert-path or --cert-name) - delete Delete a certificate - -manage your account with Let's Encrypt: - register Create a Let's Encrypt ACME account - update_account Update a Let's Encrypt ACME account - --agree-tos Agree to the ACME server's Subscriber Agreement - -m EMAIL Email address for important account notifications -""" - -# This is the short help for certbot --help, where we disable argparse -# altogether -HELP_USAGE = """ -More detailed help: - - -h, --help [TOPIC] print this message, or detailed help on a topic; - the available TOPICS are: - - all, automation, commands, paths, security, testing, or any of the - subcommands or plugins (certonly, renew, install, register, nginx, - apache, standalone, webroot, etc.) -""" - - -# These argparse parameters should be removed when detecting defaults. -ARGPARSE_PARAMS_TO_REMOVE = ("const", "nargs", "type",) - - -# These sets are used when to help detect options set by the user. -EXIT_ACTIONS = set(("help", "version",)) - - -ZERO_ARG_ACTIONS = set(("store_const", "store_true", - "store_false", "append_const", "count",)) - - -# Maps a config option to a set of config options that may have modified it. -# This dictionary is used recursively, so if A modifies B and B modifies C, -# it is determined that C was modified by the user if A was modified. -VAR_MODIFIERS = {"account": set(("server",)), - "renew_hook": set(("deploy_hook",)), - "server": set(("dry_run", "staging",)), - "webroot_map": set(("webroot_path",))} - - -def report_config_interaction(modified, modifiers): - """Registers config option interaction to be checked by set_by_cli. - - This function can be called by during the __init__ or - add_parser_arguments methods of plugins to register interactions - between config options. - - :param modified: config options that can be modified by modifiers - :type modified: iterable or str (string_types) - :param modifiers: config options that modify modified - :type modifiers: iterable or str (string_types) - - """ - if isinstance(modified, six.string_types): - modified = (modified,) - if isinstance(modifiers, six.string_types): - modifiers = (modifiers,) - - for var in modified: - VAR_MODIFIERS.setdefault(var, set()).update(modifiers) - - -def possible_deprecation_warning(config): - "A deprecation warning for users with the old, not-self-upgrading letsencrypt-auto." - if cli_command != LEAUTO: - return - if config.no_self_upgrade: - # users setting --no-self-upgrade might be hanging on a client version like 0.3.0 - # or 0.5.0 which is the new script, but doesn't set CERTBOT_AUTO; they don't - # need warnings - return - if "CERTBOT_AUTO" not in os.environ: - logger.warning("You are running with an old copy of letsencrypt-auto" - " that does not receive updates, and is less reliable than more" - " recent versions. The letsencrypt client has also been renamed" - " to Certbot. We recommend upgrading to the latest certbot-auto" - " script, or using native OS packages.") - logger.debug("Deprecation warning circumstances: %s / %s", sys.argv[0], os.environ) - - -class _Default(object): - """A class to use as a default to detect if a value is set by a user""" - - def __bool__(self): - return False - - def __eq__(self, other): - return isinstance(other, _Default) - - def __hash__(self): - return id(_Default) - - def __nonzero__(self): - return self.__bool__() - - -def set_by_cli(var): - """ - Return True if a particular config variable has been set by the user - (CLI or config file) including if the user explicitly set it to the - default. Returns False if the variable was assigned a default value. - """ - detector = set_by_cli.detector # type: ignore - if detector is None and helpful_parser is not None: - # Setup on first run: `detector` is a weird version of config in which - # the default value of every attribute is wrangled to be boolean-false - plugins = plugins_disco.PluginsRegistry.find_all() - # reconstructed_args == sys.argv[1:], or whatever was passed to main() - reconstructed_args = helpful_parser.args + [helpful_parser.verb] - detector = set_by_cli.detector = prepare_and_parse_args( # type: ignore - plugins, reconstructed_args, detect_defaults=True) - # propagate plugin requests: eg --standalone modifies config.authenticator - detector.authenticator, detector.installer = ( # type: ignore - plugin_selection.cli_plugin_requests(detector)) - - if not isinstance(getattr(detector, var), _Default): - logger.debug("Var %s=%s (set by user).", var, getattr(detector, var)) - return True - - for modifier in VAR_MODIFIERS.get(var, []): - if set_by_cli(modifier): - logger.debug("Var %s=%s (set by user).", - var, VAR_MODIFIERS.get(var, [])) - return True - - return False - -# static housekeeping var -# functions attributed are not supported by mypy -# https://github.com/python/mypy/issues/2087 -set_by_cli.detector = None # type: ignore - - -def has_default_value(option, value): - """Does option have the default value? - - If the default value of option is not known, False is returned. - - :param str option: configuration variable being considered - :param value: value of the configuration variable named option - - :returns: True if option has the default value, otherwise, False - :rtype: bool - - """ - if helpful_parser is not None: - return (option in helpful_parser.defaults and - helpful_parser.defaults[option] == value) - return False - - -def option_was_set(option, value): - """Was option set by the user or does it differ from the default? - - :param str option: configuration variable being considered - :param value: value of the configuration variable named option - - :returns: True if the option was set, otherwise, False - :rtype: bool - - """ - return set_by_cli(option) or not has_default_value(option, value) - - -def argparse_type(variable): - """Return our argparse type function for a config variable (default: str)""" - # pylint: disable=protected-access - if helpful_parser is not None: - for action in helpful_parser.parser._actions: - if action.type is not None and action.dest == variable: - return action.type - return str - -def read_file(filename, mode="rb"): - """Returns the given file's contents. - - :param str filename: path to file - :param str mode: open mode (see `open`) - - :returns: absolute path of filename and its contents - :rtype: tuple - - :raises argparse.ArgumentTypeError: File does not exist or is not readable. - - """ - try: - filename = os.path.abspath(filename) - with open(filename, mode) as the_file: - contents = the_file.read() - return filename, contents - except IOError as exc: - raise argparse.ArgumentTypeError(exc.strerror) - - -def flag_default(name): - """Default value for CLI flag.""" - # XXX: this is an internal housekeeping notion of defaults before - # argparse has been set up; it is not accurate for all flags. Call it - # with caution. Plugin defaults are missing, and some things are using - # defaults defined in this file, not in constants.py :( - return copy.deepcopy(constants.CLI_DEFAULTS[name]) - - -def config_help(name, hidden=False): - """Extract the help message for an `.IConfig` attribute.""" - # pylint: disable=no-member - if hidden: - return argparse.SUPPRESS - else: - field = interfaces.IConfig.__getitem__(name) # type: zope.interface.interface.Attribute - return field.__doc__ - - -class HelpfulArgumentGroup(object): - """Emulates an argparse group for use with HelpfulArgumentParser. - - This class is used in the add_group method of HelpfulArgumentParser. - Command line arguments can be added to the group, but help - suppression and default detection is applied by - HelpfulArgumentParser when necessary. - - """ - def __init__(self, helpful_arg_parser, topic): - self._parser = helpful_arg_parser - self._topic = topic - - def add_argument(self, *args, **kwargs): - """Add a new command line argument to the argument group.""" - self._parser.add(self._topic, *args, **kwargs) - -class CustomHelpFormatter(argparse.HelpFormatter): - """This is a clone of ArgumentDefaultsHelpFormatter, with bugfixes. - - In particular we fix https://bugs.python.org/issue28742 - """ - - def _get_help_string(self, action): - helpstr = action.help - if '%(default)' not in action.help and '(default:' not in action.help: - if action.default != argparse.SUPPRESS: - defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE] - if action.option_strings or action.nargs in defaulting_nargs: - helpstr += ' (default: %(default)s)' - return helpstr - -# The attributes here are: -# short: a string that will be displayed by "certbot -h commands" -# opts: a string that heads the section of flags with which this command is documented, -# both for "certbot -h SUBCOMMAND" and "certbot -h all" -# usage: an optional string that overrides the header of "certbot -h SUBCOMMAND" -VERB_HELP = [ - ("run (default)", { - "short": "Obtain/renew a certificate, and install it", - "opts": "Options for obtaining & installing certificates", - "usage": SHORT_USAGE.replace("[SUBCOMMAND]", ""), - "realname": "run" - }), - ("certonly", { - "short": "Obtain or renew a certificate, but do not install it", - "opts": "Options for modifying how a certificate is obtained", - "usage": ("\n\n certbot certonly [options] [-d DOMAIN] [-d DOMAIN] ...\n\n" - "This command obtains a TLS/SSL certificate without installing it anywhere.") - }), - ("renew", { - "short": "Renew all certificates (or one specified with --cert-name)", - "opts": ("The 'renew' subcommand will attempt to renew all" - " certificates (or more precisely, certificate lineages) you have" - " previously obtained if they are close to expiry, and print a" - " summary of the results. By default, 'renew' will reuse the options" - " used to create obtain or most recently successfully renew each" - " certificate lineage. You can try it with `--dry-run` first. For" - " more fine-grained control, you can renew individual lineages with" - " the `certonly` subcommand. Hooks are available to run commands" - " before and after renewal; see" - " https://certbot.eff.org/docs/using.html#renewal for more" - " information on these."), - "usage": "\n\n certbot renew [--cert-name CERTNAME] [options]\n\n" - }), - ("certificates", { - "short": "List certificates managed by Certbot", - "opts": "List certificates managed by Certbot", - "usage": ("\n\n certbot certificates [options] ...\n\n" - "Print information about the status of certificates managed by Certbot.") - }), - ("delete", { - "short": "Clean up all files related to a certificate", - "opts": "Options for deleting a certificate", - "usage": "\n\n certbot delete --cert-name CERTNAME\n\n" - }), - ("revoke", { - "short": "Revoke a certificate specified with --cert-path or --cert-name", - "opts": "Options for revocation of certificates", - "usage": "\n\n certbot revoke [--cert-path /path/to/fullchain.pem | " - "--cert-name example.com] [options]\n\n" - }), - ("register", { - "short": "Register for account with Let's Encrypt / other ACME server", - "opts": "Options for account registration", - "usage": "\n\n certbot register --email user@example.com [options]\n\n" - }), - ("update_account", { - "short": "Update existing account with Let's Encrypt / other ACME server", - "opts": "Options for account modification", - "usage": "\n\n certbot update_account --email updated_email@example.com [options]\n\n" - }), - ("unregister", { - "short": "Irrevocably deactivate your account", - "opts": "Options for account deactivation.", - "usage": "\n\n certbot unregister [options]\n\n" - }), - ("install", { - "short": "Install an arbitrary certificate in a server", - "opts": "Options for modifying how a certificate is deployed", - "usage": "\n\n certbot install --cert-path /path/to/fullchain.pem " - " --key-path /path/to/private-key [options]\n\n" - }), - ("config_changes", { - "short": "Show changes that Certbot has made to server configurations", - "opts": "Options for controlling which changes are displayed", - "usage": "\n\n certbot config_changes --num NUM [options]\n\n" - }), - ("rollback", { - "short": "Roll back server conf changes made during certificate installation", - "opts": "Options for rolling back server configuration changes", - "usage": "\n\n certbot rollback --checkpoints 3 [options]\n\n" - }), - ("plugins", { - "short": "List plugins that are installed and available on your system", - "opts": 'Options for for the "plugins" subcommand', - "usage": "\n\n certbot plugins [options]\n\n" - }), - ("update_symlinks", { - "short": "Recreate symlinks in your /etc/letsencrypt/live/ directory", - "opts": ("Recreates certificate and key symlinks in {0}, if you changed them by hand " - "or edited a renewal configuration file".format( - os.path.join(flag_default("config_dir"), "live"))), - "usage": "\n\n certbot update_symlinks [options]\n\n" - }), - ("enhance", { - "short": "Add security enhancements to your existing configuration", - "opts": ("Helps to harden the TLS configuration by adding security enhancements " - "to already existing configuration."), - "usage": "\n\n certbot enhance [options]\n\n" - }), - -] -# VERB_HELP is a list in order to preserve order, but a dict is sometimes useful -VERB_HELP_MAP = dict(VERB_HELP) - - -class HelpfulArgumentParser(object): - """Argparse Wrapper. - - This class wraps argparse, adding the ability to make --help less - verbose, and request help on specific subcategories at a time, eg - 'certbot --help security' for security options. - - """ - - - def __init__(self, args, plugins, detect_defaults=False): - from certbot import main - self.VERBS = { - "auth": main.certonly, - "certonly": main.certonly, - "config_changes": main.config_changes, - "run": main.run, - "install": main.install, - "plugins": main.plugins_cmd, - "register": main.register, - "update_account": main.update_account, - "unregister": main.unregister, - "renew": main.renew, - "revoke": main.revoke, - "rollback": main.rollback, - "everything": main.run, - "update_symlinks": main.update_symlinks, - "certificates": main.certificates, - "delete": main.delete, - "enhance": main.enhance, - } - - # Get notification function for printing - try: - self.notify = zope.component.getUtility( - interfaces.IDisplay).notification - except zope_interfaces.ComponentLookupError: - self.notify = display_util.NoninteractiveDisplay( - sys.stdout).notification - - - # List of topics for which additional help can be provided - HELP_TOPICS = ["all", "security", "paths", "automation", "testing"] - HELP_TOPICS += list(self.VERBS) + self.COMMANDS_TOPICS + ["manage"] - - plugin_names = list(plugins) - self.help_topics = HELP_TOPICS + plugin_names + [None] # type: ignore - - self.detect_defaults = detect_defaults - self.args = args - - if self.args and self.args[0] == 'help': - self.args[0] = '--help' - - self.determine_verb() - help1 = self.prescan_for_flag("-h", self.help_topics) - help2 = self.prescan_for_flag("--help", self.help_topics) - if isinstance(help1, bool) and isinstance(help2, bool): - self.help_arg = help1 or help2 - else: - self.help_arg = help1 if isinstance(help1, six.string_types) else help2 - - short_usage = self._usage_string(plugins, self.help_arg) - - self.visible_topics = self.determine_help_topics(self.help_arg) - - # elements are added by .add_group() - self.groups = {} # type: Dict[str, argparse._ArgumentGroup] - # elements are added by .parse_args() - self.defaults = {} # type: Dict[str, Any] - - self.parser = configargparse.ArgParser( - prog="certbot", - usage=short_usage, - formatter_class=CustomHelpFormatter, - args_for_setting_config_path=["-c", "--config"], - default_config_files=flag_default("config_files"), - config_arg_help_message="path to config file (default: {0})".format( - " and ".join(flag_default("config_files")))) - - # This is the only way to turn off overly verbose config flag documentation - self.parser._add_config_file_help = False # pylint: disable=protected-access - - # Help that are synonyms for --help subcommands - COMMANDS_TOPICS = ["command", "commands", "subcommand", "subcommands", "verbs"] - def _list_subcommands(self): - longest = max(len(v) for v in VERB_HELP_MAP.keys()) - - text = "The full list of available SUBCOMMANDS is:\n\n" - for verb, props in sorted(VERB_HELP): - doc = props.get("short", "") - text += '{0:<{length}} {1}\n'.format(verb, doc, length=longest) - - text += "\nYou can get more help on a specific subcommand with --help SUBCOMMAND\n" - return text - - def _usage_string(self, plugins, help_arg): - """Make usage strings late so that plugins can be initialised late - - :param plugins: all discovered plugins - :param help_arg: False for none; True for --help; "TOPIC" for --help TOPIC - :rtype: str - :returns: a short usage string for the top of --help TOPIC) - """ - if "nginx" in plugins: - nginx_doc = "--nginx Use the Nginx plugin for authentication & installation" - else: - nginx_doc = "(the certbot nginx plugin is not installed)" - if "apache" in plugins: - apache_doc = "--apache Use the Apache plugin for authentication & installation" - else: - apache_doc = "(the certbot apache plugin is not installed)" - - usage = SHORT_USAGE - if help_arg == True: - self.notify(usage + COMMAND_OVERVIEW % (apache_doc, nginx_doc) + HELP_USAGE) - sys.exit(0) - elif help_arg in self.COMMANDS_TOPICS: - self.notify(usage + self._list_subcommands()) - sys.exit(0) - elif help_arg == "all": - # if we're doing --help all, the OVERVIEW is part of the SHORT_USAGE at - # the top; if we're doing --help someothertopic, it's OT so it's not - usage += COMMAND_OVERVIEW % (apache_doc, nginx_doc) - else: - custom = VERB_HELP_MAP.get(help_arg, {}).get("usage", None) - usage = custom if custom else usage - - return usage - - def remove_config_file_domains_for_renewal(self, parsed_args): - """Make "certbot renew" safe if domains are set in cli.ini.""" - # Works around https://github.com/certbot/certbot/issues/4096 - if self.verb == "renew": - for source, flags in self.parser._source_to_settings.items(): # pylint: disable=protected-access - if source.startswith("config_file") and "domains" in flags: - parsed_args.domains = _Default() if self.detect_defaults else [] - - def parse_args(self): - """Parses command line arguments and returns the result. - - :returns: parsed command line arguments - :rtype: argparse.Namespace - - """ - parsed_args = self.parser.parse_args(self.args) - parsed_args.func = self.VERBS[self.verb] - parsed_args.verb = self.verb - - self.remove_config_file_domains_for_renewal(parsed_args) - - if self.detect_defaults: - return parsed_args - - self.defaults = dict((key, copy.deepcopy(self.parser.get_default(key))) - for key in vars(parsed_args)) - - # Do any post-parsing homework here - - if self.verb == "renew": - if parsed_args.force_interactive: - raise errors.Error( - "{0} cannot be used with renew".format( - constants.FORCE_INTERACTIVE_FLAG)) - parsed_args.noninteractive_mode = True - - if parsed_args.force_interactive and parsed_args.noninteractive_mode: - raise errors.Error( - "Flag for non-interactive mode and {0} conflict".format( - constants.FORCE_INTERACTIVE_FLAG)) - - if parsed_args.staging or parsed_args.dry_run: - self.set_test_server(parsed_args) - - if parsed_args.csr: - self.handle_csr(parsed_args) - - if parsed_args.must_staple: - parsed_args.staple = True - - if parsed_args.validate_hooks: - hooks.validate_hooks(parsed_args) - - if parsed_args.allow_subset_of_names: - if any(util.is_wildcard_domain(d) for d in parsed_args.domains): - raise errors.Error("Using --allow-subset-of-names with a" - " wildcard domain is not supported.") - - if parsed_args.hsts and parsed_args.auto_hsts: - raise errors.Error( - "Parameters --hsts and --auto-hsts cannot be used simultaneously.") - - possible_deprecation_warning(parsed_args) - - return parsed_args - - def set_test_server(self, parsed_args): - """We have --staging/--dry-run; perform sanity check and set config.server""" - - if parsed_args.server not in (flag_default("server"), constants.STAGING_URI): - conflicts = ["--staging"] if parsed_args.staging else [] - conflicts += ["--dry-run"] if parsed_args.dry_run else [] - raise errors.Error("--server value conflicts with {0}".format( - " and ".join(conflicts))) - - parsed_args.server = constants.STAGING_URI - - if parsed_args.dry_run: - if self.verb not in ["certonly", "renew"]: - raise errors.Error("--dry-run currently only works with the " - "'certonly' or 'renew' subcommands (%r)" % self.verb) - parsed_args.break_my_certs = parsed_args.staging = True - if glob.glob(os.path.join(parsed_args.config_dir, constants.ACCOUNTS_DIR, "*")): - # The user has a prod account, but might not have a staging - # one; we don't want to start trying to perform interactive registration - parsed_args.tos = True - parsed_args.register_unsafely_without_email = True - - def handle_csr(self, parsed_args): - """Process a --csr flag.""" - if parsed_args.verb != "certonly": - raise errors.Error("Currently, a CSR file may only be specified " - "when obtaining a new or replacement " - "via the certonly command. Please try the " - "certonly command instead.") - if parsed_args.allow_subset_of_names: - raise errors.Error("--allow-subset-of-names cannot be used with --csr") - - csrfile, contents = parsed_args.csr[0:2] - typ, csr, domains = crypto_util.import_csr_file(csrfile, contents) - - # This is not necessary for webroot to work, however, - # obtain_certificate_from_csr requires parsed_args.domains to be set - for domain in domains: - add_domains(parsed_args, domain) - - if not domains: - # TODO: add CN to domains instead: - raise errors.Error( - "Unfortunately, your CSR %s needs to have a SubjectAltName for every domain" - % parsed_args.csr[0]) - - parsed_args.actual_csr = (csr, typ) - - csr_domains = set([d.lower() for d in domains]) - config_domains = set(parsed_args.domains) - if csr_domains != config_domains: - raise errors.ConfigurationError( - "Inconsistent domain requests:\nFrom the CSR: {0}\nFrom command line/config: {1}" - .format(", ".join(csr_domains), ", ".join(config_domains))) - - - def determine_verb(self): - """Determines the verb/subcommand provided by the user. - - This function works around some of the limitations of argparse. - - """ - if "-h" in self.args or "--help" in self.args: - # all verbs double as help arguments; don't get them confused - self.verb = "help" - return - - for i, token in enumerate(self.args): - if token in self.VERBS: - verb = token - if verb == "auth": - verb = "certonly" - if verb == "everything": - verb = "run" - self.verb = verb - self.args.pop(i) - return - - self.verb = "run" - - def prescan_for_flag(self, flag, possible_arguments): - """Checks cli input for flags. - - Check for a flag, which accepts a fixed set of possible arguments, in - the command line; we will use this information to configure argparse's - help correctly. Return the flag's argument, if it has one that matches - the sequence @possible_arguments; otherwise return whether the flag is - present. - - """ - if flag not in self.args: - return False - pos = self.args.index(flag) - try: - nxt = self.args[pos + 1] - if nxt in possible_arguments: - return nxt - except IndexError: - pass - return True - - def add(self, topics, *args, **kwargs): - """Add a new command line argument. - - :param topics: str or [str] help topic(s) this should be listed under, - or None for "always documented". The first entry - determines where the flag lives in the "--help all" - output (None -> "optional arguments"). - :param list *args: the names of this argument flag - :param dict **kwargs: various argparse settings for this argument - - """ - - if isinstance(topics, list): - # if this flag can be listed in multiple sections, try to pick the one - # that the user has asked for help about - topic = self.help_arg if self.help_arg in topics else topics[0] - else: - topic = topics # there's only one - - if self.detect_defaults: - kwargs = self.modify_kwargs_for_default_detection(**kwargs) - - if self.visible_topics[topic]: - if topic in self.groups: - group = self.groups[topic] - group.add_argument(*args, **kwargs) - else: - self.parser.add_argument(*args, **kwargs) - else: - kwargs["help"] = argparse.SUPPRESS - self.parser.add_argument(*args, **kwargs) - - def modify_kwargs_for_default_detection(self, **kwargs): - """Modify an arg so we can check if it was set by the user. - - Changes the parameters given to argparse when adding an argument - so we can properly detect if the value was set by the user. - - :param dict kwargs: various argparse settings for this argument - - :returns: a modified versions of kwargs - :rtype: dict - - """ - action = kwargs.get("action", None) - if action not in EXIT_ACTIONS: - kwargs["action"] = ("store_true" if action in ZERO_ARG_ACTIONS else - "store") - kwargs["default"] = _Default() - for param in ARGPARSE_PARAMS_TO_REMOVE: - kwargs.pop(param, None) - - return kwargs - - def add_deprecated_argument(self, argument_name, num_args): - """Adds a deprecated argument with the name argument_name. - - Deprecated arguments are not shown in the help. If they are used - on the command line, a warning is shown stating that the - argument is deprecated and no other action is taken. - - :param str argument_name: Name of deprecated argument. - :param int nargs: Number of arguments the option takes. - - """ - util.add_deprecated_argument( - self.parser.add_argument, argument_name, num_args) - - def add_group(self, topic, verbs=(), **kwargs): - """Create a new argument group. - - This method must be called once for every topic, however, calls - to this function are left next to the argument definitions for - clarity. - - :param str topic: Name of the new argument group. - :param str verbs: List of subcommands that should be documented as part of - this help group / topic - - :returns: The new argument group. - :rtype: `HelpfulArgumentGroup` - - """ - if self.visible_topics[topic]: - self.groups[topic] = self.parser.add_argument_group(topic, **kwargs) - if self.help_arg: - for v in verbs: - self.groups[topic].add_argument(v, help=VERB_HELP_MAP[v]["short"]) - return HelpfulArgumentGroup(self, topic) - - def add_plugin_args(self, plugins): - """ - - Let each of the plugins add its own command line arguments, which - may or may not be displayed as help topics. - - """ - for name, plugin_ep in six.iteritems(plugins): - parser_or_group = self.add_group(name, - description=plugin_ep.long_description) - plugin_ep.plugin_cls.inject_parser_options(parser_or_group, name) - - def determine_help_topics(self, chosen_topic): - """ - - The user may have requested help on a topic, return a dict of which - topics to display. @chosen_topic has prescan_for_flag's return type - - :returns: dict - - """ - # topics maps each topic to whether it should be documented by - # argparse on the command line - if chosen_topic == "auth": - chosen_topic = "certonly" - if chosen_topic == "everything": - chosen_topic = "run" - if chosen_topic == "all": - # Addition of condition closes #6209 (removal of duplicate route53 option). - return dict([(t, True) if t != 'certbot-route53:auth' else (t, False) - for t in self.help_topics]) - elif not chosen_topic: - return dict([(t, False) for t in self.help_topics]) - else: - return dict([(t, t == chosen_topic) for t in self.help_topics]) - -def _add_all_groups(helpful): - helpful.add_group("automation", description="Flags for automating execution & other tweaks") - helpful.add_group("security", description="Security parameters & server settings") - helpful.add_group("testing", - description="The following flags are meant for testing and integration purposes only.") - helpful.add_group("paths", description="Flags for changing execution paths & servers") - helpful.add_group("manage", - description="Various subcommands and flags are available for managing your certificates:", - verbs=["certificates", "delete", "renew", "revoke", "update_symlinks"]) - - # VERBS - for verb, docs in VERB_HELP: - name = docs.get("realname", verb) - helpful.add_group(name, description=docs["opts"]) - - -def prepare_and_parse_args(plugins, args, detect_defaults=False): # pylint: disable=too-many-statements - """Returns parsed command line arguments. - - :param .PluginsRegistry plugins: available plugins - :param list args: command line arguments with the program name removed - - :returns: parsed command line arguments - :rtype: argparse.Namespace - - """ - - # pylint: disable=too-many-statements - - helpful = HelpfulArgumentParser(args, plugins, detect_defaults) - _add_all_groups(helpful) - - # --help is automatically provided by argparse - helpful.add( - None, "-v", "--verbose", dest="verbose_count", action="count", - default=flag_default("verbose_count"), help="This flag can be used " - "multiple times to incrementally increase the verbosity of output, " - "e.g. -vvv.") - helpful.add( - None, "-t", "--text", dest="text_mode", action="store_true", - default=flag_default("text_mode"), help=argparse.SUPPRESS) - helpful.add( - None, "--max-log-backups", type=nonnegative_int, - default=flag_default("max_log_backups"), - help="Specifies the maximum number of backup logs that should " - "be kept by Certbot's built in log rotation. Setting this " - "flag to 0 disables log rotation entirely, causing " - "Certbot to always append to the same log file.") - helpful.add( - [None, "automation", "run", "certonly", "enhance"], - "-n", "--non-interactive", "--noninteractive", - dest="noninteractive_mode", action="store_true", - default=flag_default("noninteractive_mode"), - help="Run without ever asking for user input. This may require " - "additional command line flags; the client will try to explain " - "which ones are required if it finds one missing") - helpful.add( - [None, "register", "run", "certonly", "enhance"], - constants.FORCE_INTERACTIVE_FLAG, action="store_true", - default=flag_default("force_interactive"), - help="Force Certbot to be interactive even if it detects it's not " - "being run in a terminal. This flag cannot be used with the " - "renew subcommand.") - helpful.add( - [None, "run", "certonly", "certificates", "enhance"], - "-d", "--domains", "--domain", dest="domains", - metavar="DOMAIN", action=_DomainsAction, - default=flag_default("domains"), - help="Domain names to apply. For multiple domains you can use " - "multiple -d flags or enter a comma separated list of domains " - "as a parameter. The first domain provided will be the " - "subject CN of the certificate, and all domains will be " - "Subject Alternative Names on the certificate. " - "The first domain will also be used in " - "some software user interfaces and as the file paths for the " - "certificate and related material unless otherwise " - "specified or you already have a certificate with the same " - "name. In the case of a name collision it will append a number " - "like 0001 to the file path name. (default: Ask)") - helpful.add( - [None, "run", "certonly", "register"], - "--eab-kid", dest="eab_kid", - metavar="EAB_KID", - help="Key Identifier for External Account Binding" - ) - helpful.add( - [None, "run", "certonly", "register"], - "--eab-hmac-key", dest="eab_hmac_key", - metavar="EAB_HMAC_KEY", - help="HMAC key for External Account Binding" - ) - helpful.add( - [None, "run", "certonly", "manage", "delete", "certificates", - "renew", "enhance"], "--cert-name", dest="certname", - metavar="CERTNAME", default=flag_default("certname"), - help="Certificate name to apply. This name is used by Certbot for housekeeping " - "and in file paths; it doesn't affect the content of the certificate itself. " - "To see certificate names, run 'certbot certificates'. " - "When creating a new certificate, specifies the new certificate's name. " - "(default: the first provided domain or the name of an existing " - "certificate on your system for the same domains)") - helpful.add( - [None, "testing", "renew", "certonly"], - "--dry-run", action="store_true", dest="dry_run", - default=flag_default("dry_run"), - help="Perform a test run of the client, obtaining test (invalid) certificates" - " but not saving them to disk. This can currently only be used" - " with the 'certonly' and 'renew' subcommands. \nNote: Although --dry-run" - " tries to avoid making any persistent changes on a system, it " - " is not completely side-effect free: if used with webserver authenticator plugins" - " like apache and nginx, it makes and then reverts temporary config changes" - " in order to obtain test certificates, and reloads webservers to deploy and then" - " roll back those changes. It also calls --pre-hook and --post-hook commands" - " if they are defined because they may be necessary to accurately simulate" - " renewal. --deploy-hook commands are not called.") - helpful.add( - ["register", "automation"], "--register-unsafely-without-email", action="store_true", - default=flag_default("register_unsafely_without_email"), - help="Specifying this flag enables registering an account with no " - "email address. This is strongly discouraged, because in the " - "event of key loss or account compromise you will irrevocably " - "lose access to your account. You will also be unable to receive " - "notice about impending expiration or revocation of your " - "certificates. Updates to the Subscriber Agreement will still " - "affect you, and will be effective 14 days after posting an " - "update to the web site.") - # TODO: When `certbot register --update-registration` is fully deprecated, - # delete following helpful.add - helpful.add( - "register", "--update-registration", action="store_true", - default=flag_default("update_registration"), dest="update_registration", - help=argparse.SUPPRESS) - helpful.add( - ["register", "update_account", "unregister", "automation"], "-m", "--email", - default=flag_default("email"), - help=config_help("email")) - helpful.add(["register", "update_account", "automation"], "--eff-email", action="store_true", - default=flag_default("eff_email"), dest="eff_email", - help="Share your e-mail address with EFF") - helpful.add(["register", "update_account", "automation"], "--no-eff-email", - action="store_false", default=flag_default("eff_email"), dest="eff_email", - help="Don't share your e-mail address with EFF") - helpful.add( - ["automation", "certonly", "run"], - "--keep-until-expiring", "--keep", "--reinstall", - dest="reinstall", action="store_true", default=flag_default("reinstall"), - help="If the requested certificate matches an existing certificate, always keep the " - "existing one until it is due for renewal (for the " - "'run' subcommand this means reinstall the existing certificate). (default: Ask)") - helpful.add( - "automation", "--expand", action="store_true", default=flag_default("expand"), - help="If an existing certificate is a strict subset of the requested names, " - "always expand and replace it with the additional names. (default: Ask)") - helpful.add( - "automation", "--version", action="version", - version="%(prog)s {0}".format(certbot.__version__), - help="show program's version number and exit") - helpful.add( - ["automation", "renew"], - "--force-renewal", "--renew-by-default", dest="renew_by_default", - action="store_true", default=flag_default("renew_by_default"), - help="If a certificate " - "already exists for the requested domains, renew it now, " - "regardless of whether it is near expiry. (Often " - "--keep-until-expiring is more appropriate). Also implies " - "--expand.") - helpful.add( - "automation", "--renew-with-new-domains", dest="renew_with_new_domains", - action="store_true", default=flag_default("renew_with_new_domains"), - help="If a " - "certificate already exists for the requested certificate name " - "but does not match the requested domains, renew it now, " - "regardless of whether it is near expiry.") - helpful.add( - "automation", "--reuse-key", dest="reuse_key", - action="store_true", default=flag_default("reuse_key"), - help="When renewing, use the same private key as the existing " - "certificate.") - - helpful.add( - ["automation", "renew", "certonly"], - "--allow-subset-of-names", action="store_true", - default=flag_default("allow_subset_of_names"), - help="When performing domain validation, do not consider it a failure " - "if authorizations can not be obtained for a strict subset of " - "the requested domains. This may be useful for allowing renewals for " - "multiple domains to succeed even if some domains no longer point " - "at this system. This option cannot be used with --csr.") - helpful.add( - "automation", "--agree-tos", dest="tos", action="store_true", - default=flag_default("tos"), - help="Agree to the ACME Subscriber Agreement (default: Ask)") - helpful.add( - ["unregister", "automation"], "--account", metavar="ACCOUNT_ID", - default=flag_default("account"), - help="Account ID to use") - helpful.add( - "automation", "--duplicate", dest="duplicate", action="store_true", - default=flag_default("duplicate"), - help="Allow making a certificate lineage that duplicates an existing one " - "(both can be renewed in parallel)") - helpful.add( - "automation", "--os-packages-only", action="store_true", - default=flag_default("os_packages_only"), - help="(certbot-auto only) install OS package dependencies and then stop") - helpful.add( - "automation", "--no-self-upgrade", action="store_true", - default=flag_default("no_self_upgrade"), - help="(certbot-auto only) prevent the certbot-auto script from" - " upgrading itself to newer released versions (default: Upgrade" - " automatically)") - helpful.add( - "automation", "--no-bootstrap", action="store_true", - default=flag_default("no_bootstrap"), - help="(certbot-auto only) prevent the certbot-auto script from" - " installing OS-level dependencies (default: Prompt to install " - " OS-wide dependencies, but exit if the user says 'No')") - helpful.add( - ["automation", "renew", "certonly", "run"], - "-q", "--quiet", dest="quiet", action="store_true", - default=flag_default("quiet"), - help="Silence all output except errors. Useful for automation via cron." - " Implies --non-interactive.") - # overwrites server, handled in HelpfulArgumentParser.parse_args() - helpful.add(["testing", "revoke", "run"], "--test-cert", "--staging", - dest="staging", action="store_true", default=flag_default("staging"), - help="Use the staging server to obtain or revoke test (invalid) certificates; equivalent" - " to --server " + constants.STAGING_URI) - helpful.add( - "testing", "--debug", action="store_true", default=flag_default("debug"), - help="Show tracebacks in case of errors, and allow certbot-auto " - "execution on experimental platforms") - helpful.add( - [None, "certonly", "run"], "--debug-challenges", action="store_true", - default=flag_default("debug_challenges"), - help="After setting up challenges, wait for user input before " - "submitting to CA") - helpful.add( - "testing", "--no-verify-ssl", action="store_true", - help=config_help("no_verify_ssl"), - default=flag_default("no_verify_ssl")) - helpful.add( - ["testing", "standalone", "apache", "nginx"], "--tls-sni-01-port", type=int, - default=flag_default("tls_sni_01_port"), - help=config_help("tls_sni_01_port")) - helpful.add( - ["testing", "standalone"], "--tls-sni-01-address", - default=flag_default("tls_sni_01_address"), - help=config_help("tls_sni_01_address")) - helpful.add( - ["testing", "standalone", "manual"], "--http-01-port", type=int, - dest="http01_port", - default=flag_default("http01_port"), help=config_help("http01_port")) - helpful.add( - ["testing", "standalone"], "--http-01-address", - dest="http01_address", - default=flag_default("http01_address"), help=config_help("http01_address")) - helpful.add( - "testing", "--break-my-certs", action="store_true", - default=flag_default("break_my_certs"), - help="Be willing to replace or renew valid certificates with invalid " - "(testing/staging) certificates") - helpful.add( - "security", "--rsa-key-size", type=int, metavar="N", - default=flag_default("rsa_key_size"), help=config_help("rsa_key_size")) - helpful.add( - "security", "--must-staple", action="store_true", - dest="must_staple", default=flag_default("must_staple"), - help=config_help("must_staple")) - helpful.add( - ["security", "enhance"], - "--redirect", action="store_true", dest="redirect", - default=flag_default("redirect"), - help="Automatically redirect all HTTP traffic to HTTPS for the newly " - "authenticated vhost. (default: Ask)") - helpful.add( - "security", "--no-redirect", action="store_false", dest="redirect", - default=flag_default("redirect"), - help="Do not automatically redirect all HTTP traffic to HTTPS for the newly " - "authenticated vhost. (default: Ask)") - helpful.add( - ["security", "enhance"], - "--hsts", action="store_true", dest="hsts", default=flag_default("hsts"), - help="Add the Strict-Transport-Security header to every HTTP response." - " Forcing browser to always use SSL for the domain." - " Defends against SSL Stripping.") - helpful.add( - "security", "--no-hsts", action="store_false", dest="hsts", - default=flag_default("hsts"), help=argparse.SUPPRESS) - helpful.add( - ["security", "enhance"], - "--uir", action="store_true", dest="uir", default=flag_default("uir"), - help='Add the "Content-Security-Policy: upgrade-insecure-requests"' - ' header to every HTTP response. Forcing the browser to use' - ' https:// for every http:// resource.') - helpful.add( - "security", "--no-uir", action="store_false", dest="uir", default=flag_default("uir"), - help=argparse.SUPPRESS) - helpful.add( - "security", "--staple-ocsp", action="store_true", dest="staple", - default=flag_default("staple"), - help="Enables OCSP Stapling. A valid OCSP response is stapled to" - " the certificate that the server offers during TLS.") - helpful.add( - "security", "--no-staple-ocsp", action="store_false", dest="staple", - default=flag_default("staple"), help=argparse.SUPPRESS) - helpful.add( - "security", "--strict-permissions", action="store_true", - default=flag_default("strict_permissions"), - help="Require that all configuration files are owned by the current " - "user; only needed if your config is somewhere unsafe like /tmp/") - helpful.add( - ["manual", "standalone", "certonly", "renew"], - "--preferred-challenges", dest="pref_challs", - action=_PrefChallAction, default=flag_default("pref_challs"), - help='A sorted, comma delimited list of the preferred challenge to ' - 'use during authorization with the most preferred challenge ' - 'listed first (Eg, "dns" or "tls-sni-01,http,dns"). ' - 'Not all plugins support all challenges. See ' - 'https://certbot.eff.org/docs/using.html#plugins for details. ' - 'ACME Challenges are versioned, but if you pick "http" rather ' - 'than "http-01", Certbot will select the latest version ' - 'automatically.') - helpful.add( - "renew", "--pre-hook", - help="Command to be run in a shell before obtaining any certificates." - " Intended primarily for renewal, where it can be used to temporarily" - " shut down a webserver that might conflict with the standalone" - " plugin. This will only be called if a certificate is actually to be" - " obtained/renewed. When renewing several certificates that have" - " identical pre-hooks, only the first will be executed.") - helpful.add( - "renew", "--post-hook", - help="Command to be run in a shell after attempting to obtain/renew" - " certificates. Can be used to deploy renewed certificates, or to" - " restart any servers that were stopped by --pre-hook. This is only" - " run if an attempt was made to obtain/renew a certificate. If" - " multiple renewed certificates have identical post-hooks, only" - " one will be run.") - helpful.add("renew", "--renew-hook", - action=_RenewHookAction, help=argparse.SUPPRESS) - helpful.add( - "renew", "--no-random-sleep-on-renew", action="store_false", - default=flag_default("random_sleep_on_renew"), dest="random_sleep_on_renew", - help=argparse.SUPPRESS) - helpful.add( - "renew", "--deploy-hook", action=_DeployHookAction, - help='Command to be run in a shell once for each successfully' - ' issued certificate. For this command, the shell variable' - ' $RENEWED_LINEAGE will point to the config live subdirectory' - ' (for example, "/etc/letsencrypt/live/example.com") containing' - ' the new certificates and keys; the shell variable' - ' $RENEWED_DOMAINS will contain a space-delimited list of' - ' renewed certificate domains (for example, "example.com' - ' www.example.com"') - helpful.add( - "renew", "--disable-hook-validation", - action="store_false", dest="validate_hooks", - default=flag_default("validate_hooks"), - help="Ordinarily the commands specified for" - " --pre-hook/--post-hook/--deploy-hook will be checked for" - " validity, to see if the programs being run are in the $PATH," - " so that mistakes can be caught early, even when the hooks" - " aren't being run just yet. The validation is rather" - " simplistic and fails if you use more advanced shell" - " constructs, so you can use this switch to disable it." - " (default: False)") - helpful.add( - "renew", "--no-directory-hooks", action="store_false", - default=flag_default("directory_hooks"), dest="directory_hooks", - help="Disable running executables found in Certbot's hook directories" - " during renewal. (default: False)") - helpful.add( - "renew", "--disable-renew-updates", action="store_true", - default=flag_default("disable_renew_updates"), dest="disable_renew_updates", - help="Disable automatic updates to your server configuration that" - " would otherwise be done by the selected installer plugin, and triggered" - " when the user executes \"certbot renew\", regardless of if the certificate" - " is renewed. This setting does not apply to important TLS configuration" - " updates.") - helpful.add( - "renew", "--no-autorenew", action="store_false", - default=flag_default("autorenew"), dest="autorenew", - help="Disable auto renewal of certificates.") - - helpful.add_deprecated_argument("--agree-dev-preview", 0) - helpful.add_deprecated_argument("--dialog", 0) - - # Populate the command line parameters for new style enhancements - enhancements.populate_cli(helpful.add) - - _create_subparsers(helpful) - _paths_parser(helpful) - # _plugins_parsing should be the last thing to act upon the main - # parser (--help should display plugin-specific options last) - _plugins_parsing(helpful, plugins) - - if not detect_defaults: - global helpful_parser # pylint: disable=global-statement - helpful_parser = helpful - return helpful.parse_args() - - -def _create_subparsers(helpful): - helpful.add("config_changes", "--num", type=int, default=flag_default("num"), - help="How many past revisions you want to be displayed") - - from certbot.client import sample_user_agent # avoid import loops - helpful.add( - None, "--user-agent", default=flag_default("user_agent"), - help='Set a custom user agent string for the client. User agent strings allow ' - 'the CA to collect high level statistics about success rates by OS, ' - 'plugin and use case, and to know when to deprecate support for past Python ' - "versions and flags. If you wish to hide this information from the Let's " - 'Encrypt server, set this to "". ' - '(default: {0}). The flags encoded in the user agent are: ' - '--duplicate, --force-renew, --allow-subset-of-names, -n, and ' - 'whether any hooks are set.'.format(sample_user_agent())) - helpful.add( - None, "--user-agent-comment", default=flag_default("user_agent_comment"), - type=_user_agent_comment_type, - help="Add a comment to the default user agent string. May be used when repackaging Certbot " - "or calling it from another tool to allow additional statistical data to be collected." - " Ignored if --user-agent is set. (Example: Foo-Wrapper/1.0)") - helpful.add("certonly", - "--csr", default=flag_default("csr"), type=read_file, - help="Path to a Certificate Signing Request (CSR) in DER or PEM format." - " Currently --csr only works with the 'certonly' subcommand.") - helpful.add("revoke", - "--reason", dest="reason", - choices=CaseInsensitiveList(sorted(constants.REVOCATION_REASONS, - key=constants.REVOCATION_REASONS.get)), - action=_EncodeReasonAction, default=flag_default("reason"), - help="Specify reason for revoking certificate. (default: unspecified)") - helpful.add("revoke", - "--delete-after-revoke", action="store_true", - default=flag_default("delete_after_revoke"), - help="Delete certificates after revoking them, along with all previous and later " - "versions of those certificates.") - helpful.add("revoke", - "--no-delete-after-revoke", action="store_false", - dest="delete_after_revoke", - default=flag_default("delete_after_revoke"), - help="Do not delete certificates after revoking them. This " - "option should be used with caution because the 'renew' " - "subcommand will attempt to renew undeleted revoked " - "certificates.") - helpful.add("rollback", - "--checkpoints", type=int, metavar="N", - default=flag_default("rollback_checkpoints"), - help="Revert configuration N number of checkpoints.") - helpful.add("plugins", - "--init", action="store_true", default=flag_default("init"), - help="Initialize plugins.") - helpful.add("plugins", - "--prepare", action="store_true", default=flag_default("prepare"), - help="Initialize and prepare plugins.") - helpful.add("plugins", - "--authenticators", action="append_const", dest="ifaces", - default=flag_default("ifaces"), - const=interfaces.IAuthenticator, help="Limit to authenticator plugins only.") - helpful.add("plugins", - "--installers", action="append_const", dest="ifaces", - default=flag_default("ifaces"), - const=interfaces.IInstaller, help="Limit to installer plugins only.") - - -class CaseInsensitiveList(list): - """A list that will ignore case when searching. - - This class is passed to the `choices` argument of `argparse.add_arguments` - through the `helpful` wrapper. It is necessary due to special handling of - command line arguments by `set_by_cli` in which the `type_func` is not applied.""" - def __contains__(self, element): - return super(CaseInsensitiveList, self).__contains__(element.lower()) - - -def _paths_parser(helpful): - add = helpful.add - verb = helpful.verb - if verb == "help": - verb = helpful.help_arg - - cph = "Path to where certificate is saved (with auth --csr), installed from, or revoked." - sections = ["paths", "install", "revoke", "certonly", "manage"] - if verb == "certonly": - add(sections, "--cert-path", type=os.path.abspath, - default=flag_default("auth_cert_path"), help=cph) - elif verb == "revoke": - add(sections, "--cert-path", type=read_file, required=False, help=cph) - else: - add(sections, "--cert-path", type=os.path.abspath, help=cph) - - section = "paths" - if verb in ("install", "revoke"): - section = verb - # revoke --key-path reads a file, install --key-path takes a string - add(section, "--key-path", - type=((verb == "revoke" and read_file) or os.path.abspath), - help="Path to private key for certificate installation " - "or revocation (if account key is missing)") - - default_cp = None - if verb == "certonly": - default_cp = flag_default("auth_chain_path") - add(["paths", "install"], "--fullchain-path", default=default_cp, type=os.path.abspath, - help="Accompanying path to a full certificate chain (certificate plus chain).") - add("paths", "--chain-path", default=default_cp, type=os.path.abspath, - help="Accompanying path to a certificate chain.") - add("paths", "--config-dir", default=flag_default("config_dir"), - help=config_help("config_dir")) - add("paths", "--work-dir", default=flag_default("work_dir"), - help=config_help("work_dir")) - add("paths", "--logs-dir", default=flag_default("logs_dir"), - help="Logs directory.") - add("paths", "--server", default=flag_default("server"), - help=config_help("server")) - - -def _plugins_parsing(helpful, plugins): - # It's nuts, but there are two "plugins" topics. Somehow this works - helpful.add_group( - "plugins", description="Plugin Selection: Certbot client supports an " - "extensible plugins architecture. See '%(prog)s plugins' for a " - "list of all installed plugins and their names. You can force " - "a particular plugin by setting options provided below. Running " - "--help will list flags specific to that plugin.") - - helpful.add("plugins", "--configurator", default=flag_default("configurator"), - help="Name of the plugin that is both an authenticator and an installer." - " Should not be used together with --authenticator or --installer. " - "(default: Ask)") - helpful.add("plugins", "-a", "--authenticator", default=flag_default("authenticator"), - help="Authenticator plugin name.") - helpful.add("plugins", "-i", "--installer", default=flag_default("installer"), - help="Installer plugin name (also used to find domains).") - helpful.add(["plugins", "certonly", "run", "install", "config_changes"], - "--apache", action="store_true", default=flag_default("apache"), - help="Obtain and install certificates using Apache") - helpful.add(["plugins", "certonly", "run", "install", "config_changes"], - "--nginx", action="store_true", default=flag_default("nginx"), - help="Obtain and install certificates using Nginx") - helpful.add(["plugins", "certonly"], "--standalone", action="store_true", - default=flag_default("standalone"), - help='Obtain certificates using a "standalone" webserver.') - helpful.add(["plugins", "certonly"], "--manual", action="store_true", - default=flag_default("manual"), - help="Provide laborious manual instructions for obtaining a certificate") - helpful.add(["plugins", "certonly"], "--webroot", action="store_true", - default=flag_default("webroot"), - help="Obtain certificates by placing files in a webroot directory.") - helpful.add(["plugins", "certonly"], "--dns-cloudflare", action="store_true", - default=flag_default("dns_cloudflare"), - help=("Obtain certificates using a DNS TXT record (if you are " - "using Cloudflare for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-cloudxns", action="store_true", - default=flag_default("dns_cloudxns"), - help=("Obtain certificates using a DNS TXT record (if you are " - "using CloudXNS for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-digitalocean", action="store_true", - default=flag_default("dns_digitalocean"), - help=("Obtain certificates using a DNS TXT record (if you are " - "using DigitalOcean for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-dnsimple", action="store_true", - default=flag_default("dns_dnsimple"), - help=("Obtain certificates using a DNS TXT record (if you are " - "using DNSimple for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-dnsmadeeasy", action="store_true", - default=flag_default("dns_dnsmadeeasy"), - help=("Obtain certificates using a DNS TXT record (if you are" - "using DNS Made Easy for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-gehirn", action="store_true", - default=flag_default("dns_gehirn"), - help=("Obtain certificates using a DNS TXT record " - "(if you are using Gehirn Infrastracture Service for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-google", action="store_true", - default=flag_default("dns_google"), - help=("Obtain certificates using a DNS TXT record (if you are " - "using Google Cloud DNS).")) - helpful.add(["plugins", "certonly"], "--dns-linode", action="store_true", - default=flag_default("dns_linode"), - help=("Obtain certificates using a DNS TXT record (if you are " - "using Linode for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-luadns", action="store_true", - default=flag_default("dns_luadns"), - help=("Obtain certificates using a DNS TXT record (if you are " - "using LuaDNS for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-nsone", action="store_true", - default=flag_default("dns_nsone"), - help=("Obtain certificates using a DNS TXT record (if you are " - "using NS1 for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-ovh", action="store_true", - default=flag_default("dns_ovh"), - help=("Obtain certificates using a DNS TXT record (if you are " - "using OVH for DNS).")) - helpful.add(["plugins", "certonly"], "--dns-rfc2136", action="store_true", - default=flag_default("dns_rfc2136"), - help="Obtain certificates using a DNS TXT record (if you are using BIND for DNS).") - helpful.add(["plugins", "certonly"], "--dns-route53", action="store_true", - default=flag_default("dns_route53"), - help=("Obtain certificates using a DNS TXT record (if you are using Route53 for " - "DNS).")) - helpful.add(["plugins", "certonly"], "--dns-sakuracloud", action="store_true", - default=flag_default("dns_sakuracloud"), - help=("Obtain certificates using a DNS TXT record " - "(if you are using Sakura Cloud for DNS).")) - - # things should not be reorder past/pre this comment: - # plugins_group should be displayed in --help before plugin - # specific groups (so that plugins_group.description makes sense) - - helpful.add_plugin_args(plugins) - - -class _EncodeReasonAction(argparse.Action): - """Action class for parsing revocation reason.""" - - def __call__(self, parser, namespace, reason, option_string=None): - """Encodes the reason for certificate revocation.""" - code = constants.REVOCATION_REASONS[reason.lower()] - setattr(namespace, self.dest, code) - - -class _DomainsAction(argparse.Action): - """Action class for parsing domains.""" - - def __call__(self, parser, namespace, domain, option_string=None): - """Just wrap add_domains in argparseese.""" - add_domains(namespace, domain) - -def add_domains(args_or_config, domains): - """Registers new domains to be used during the current client run. - - Domains are not added to the list of requested domains if they have - already been registered. - - :param args_or_config: parsed command line arguments - :type args_or_config: argparse.Namespace or - configuration.NamespaceConfig - :param str domain: one or more comma separated domains - - :returns: domains after they have been normalized and validated - :rtype: `list` of `str` - - """ - validated_domains = [] - for domain in domains.split(","): - domain = util.enforce_domain_sanity(domain.strip()) - validated_domains.append(domain) - if domain not in args_or_config.domains: - args_or_config.domains.append(domain) - - return validated_domains - -class _PrefChallAction(argparse.Action): - """Action class for parsing preferred challenges.""" - - def __call__(self, parser, namespace, pref_challs, option_string=None): - try: - challs = parse_preferred_challenges(pref_challs.split(",")) - except errors.Error as error: - raise argparse.ArgumentError(self, str(error)) - namespace.pref_challs.extend(challs) - - -def parse_preferred_challenges(pref_challs): - """Translate and validate preferred challenges. - - :param pref_challs: list of preferred challenge types - :type pref_challs: `list` of `str` - - :returns: validated list of preferred challenge types - :rtype: `list` of `str` - - :raises errors.Error: if pref_challs is invalid - - """ - aliases = {"dns": "dns-01", "http": "http-01", "tls-sni": "tls-sni-01"} - challs = [c.strip() for c in pref_challs] - challs = [aliases.get(c, c) for c in challs] - unrecognized = ", ".join(name for name in challs - if name not in challenges.Challenge.TYPES) - if unrecognized: - raise errors.Error( - "Unrecognized challenges: {0}".format(unrecognized)) - return challs - -def _user_agent_comment_type(value): - if "(" in value or ")" in value: - raise argparse.ArgumentTypeError("may not contain parentheses") - return value - -class _DeployHookAction(argparse.Action): - """Action class for parsing deploy hooks.""" - - def __call__(self, parser, namespace, values, option_string=None): - renew_hook_set = namespace.deploy_hook != namespace.renew_hook - if renew_hook_set and namespace.renew_hook != values: - raise argparse.ArgumentError( - self, "conflicts with --renew-hook value") - namespace.deploy_hook = namespace.renew_hook = values - - -class _RenewHookAction(argparse.Action): - """Action class for parsing renew hooks.""" - - def __call__(self, parser, namespace, values, option_string=None): - deploy_hook_set = namespace.deploy_hook is not None - if deploy_hook_set and namespace.deploy_hook != values: - raise argparse.ArgumentError( - self, "conflicts with --deploy-hook value") - namespace.renew_hook = values - - -def nonnegative_int(value): - """Converts value to an int and checks that it is not negative. - - This function should used as the type parameter for argparse - arguments. - - :param str value: value provided on the command line - - :returns: integer representation of value - :rtype: int - - :raises argparse.ArgumentTypeError: if value isn't a non-negative integer - - """ - try: - int_value = int(value) - except ValueError: - raise argparse.ArgumentTypeError("value must be an integer") - - if int_value < 0: - raise argparse.ArgumentTypeError("value must be non-negative") - return int_value diff --git a/certbot/client.py b/certbot/client.py deleted file mode 100644 index 38b77a7721d..00000000000 --- a/certbot/client.py +++ /dev/null @@ -1,750 +0,0 @@ -"""Certbot client API.""" -import datetime -import logging -import os -import platform - - -from cryptography.hazmat.backends import default_backend -# https://github.com/python/typeshed/blob/master/third_party/ -# 2/cryptography/hazmat/primitives/asymmetric/rsa.pyi -from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key # type: ignore -import josepy as jose -import OpenSSL -import zope.component - -from acme import client as acme_client -from acme import crypto_util as acme_crypto_util -from acme import errors as acme_errors -from acme import messages -from acme.magic_typing import Optional # pylint: disable=unused-import,no-name-in-module - -import certbot - -from certbot import account -from certbot import auth_handler -from certbot import cli -from certbot import compat -from certbot import constants -from certbot import crypto_util -from certbot import eff -from certbot import error_handler -from certbot import errors -from certbot import interfaces -from certbot import reverter -from certbot import storage -from certbot import util - -from certbot.display import ops as display_ops -from certbot.display import enhancements -from certbot.plugins import selection as plugin_selection - - -logger = logging.getLogger(__name__) - - -def acme_from_config_key(config, key, regr=None): - "Wrangle ACME client construction" - # TODO: Allow for other alg types besides RS256 - net = acme_client.ClientNetwork(key, account=regr, verify_ssl=(not config.no_verify_ssl), - user_agent=determine_user_agent(config)) - return acme_client.BackwardsCompatibleClientV2(net, key, config.server) - - -def determine_user_agent(config): - """ - Set a user_agent string in the config based on the choice of plugins. - (this wasn't knowable at construction time) - - :returns: the client's User-Agent string - :rtype: `str` - """ - - # WARNING: To ensure changes are in line with Certbot's privacy - # policy, talk to a core Certbot team member before making any - # changes here. - if config.user_agent is None: - ua = ("CertbotACMEClient/{0} ({1}; {2}{8}) Authenticator/{3} Installer/{4} " - "({5}; flags: {6}) Py/{7}") - if os.environ.get("CERTBOT_DOCS") == "1": - cli_command = "certbot(-auto)" - os_info = "OS_NAME OS_VERSION" - python_version = "major.minor.patchlevel" - else: - cli_command = cli.cli_command - os_info = util.get_os_info_ua() - python_version = platform.python_version() - ua = ua.format(certbot.__version__, cli_command, os_info, - config.authenticator, config.installer, config.verb, - ua_flags(config), python_version, - "; " + config.user_agent_comment if config.user_agent_comment else "") - else: - ua = config.user_agent - return ua - -def ua_flags(config): - "Turn some very important CLI flags into clues in the user agent." - if isinstance(config, DummyConfig): - return "FLAGS" - flags = [] - if config.duplicate: - flags.append("dup") - if config.renew_by_default: - flags.append("frn") - if config.allow_subset_of_names: - flags.append("asn") - if config.noninteractive_mode: - flags.append("n") - hook_names = ("pre", "post", "renew", "manual_auth", "manual_cleanup") - hooks = [getattr(config, h + "_hook") for h in hook_names] - if any(hooks): - flags.append("hook") - return " ".join(flags) - -class DummyConfig(object): - "Shim for computing a sample user agent." - def __init__(self): - self.authenticator = "XXX" - self.installer = "YYY" - self.user_agent = None - self.verb = "SUBCOMMAND" - - def __getattr__(self, name): - "Any config properties we might have are None." - return None - -def sample_user_agent(): - "Document what this Certbot's user agent string will be like." - - return determine_user_agent(DummyConfig()) - - -def register(config, account_storage, tos_cb=None): - """Register new account with an ACME CA. - - This function takes care of generating fresh private key, - registering the account, optionally accepting CA Terms of Service - and finally saving the account. It should be called prior to - initialization of `Client`, unless account has already been created. - - :param .IConfig config: Client configuration. - - :param .AccountStorage account_storage: Account storage where newly - registered account will be saved to. Save happens only after TOS - acceptance step, so any account private keys or - `.RegistrationResource` will not be persisted if `tos_cb` - returns ``False``. - - :param tos_cb: If ACME CA requires the user to accept a Terms of - Service before registering account, client action is - necessary. For example, a CLI tool would prompt the user - acceptance. `tos_cb` must be a callable that should accept - `.RegistrationResource` and return a `bool`: ``True`` iff the - Terms of Service present in the contained - `.Registration.terms_of_service` is accepted by the client, and - ``False`` otherwise. ``tos_cb`` will be called only if the - client action is necessary, i.e. when ``terms_of_service is not - None``. This argument is optional, if not supplied it will - default to automatic acceptance! - - :raises certbot.errors.Error: In case of any client problems, in - particular registration failure, or unaccepted Terms of Service. - :raises acme.errors.Error: In case of any protocol problems. - - :returns: Newly registered and saved account, as well as protocol - API handle (should be used in `Client` initialization). - :rtype: `tuple` of `.Account` and `acme.client.Client` - - """ - # Log non-standard actions, potentially wrong API calls - if account_storage.find_all(): - logger.info("There are already existing accounts for %s", config.server) - if config.email is None: - if not config.register_unsafely_without_email: - msg = ("No email was provided and " - "--register-unsafely-without-email was not present.") - logger.warning(msg) - raise errors.Error(msg) - if not config.dry_run: - logger.info("Registering without email!") - - # If --dry-run is used, and there is no staging account, create one with no email. - if config.dry_run: - config.email = None - - # Each new registration shall use a fresh new key - rsa_key = generate_private_key( - public_exponent=65537, - key_size=config.rsa_key_size, - backend=default_backend()) - key = jose.JWKRSA(key=jose.ComparableRSAKey(rsa_key)) - acme = acme_from_config_key(config, key) - # TODO: add phone? - regr = perform_registration(acme, config, tos_cb) - - acc = account.Account(regr, key) - account.report_new_account(config) - account_storage.save(acc, acme) - - eff.handle_subscription(config) - - return acc, acme - - -def perform_registration(acme, config, tos_cb): - """ - Actually register new account, trying repeatedly if there are email - problems - - :param acme.client.Client client: ACME client object. - :param .IConfig config: Client configuration. - :param Callable tos_cb: a callback to handle Term of Service agreement. - - :returns: Registration Resource. - :rtype: `acme.messages.RegistrationResource` - """ - - eab_credentials_supplied = config.eab_kid and config.eab_hmac_key - if eab_credentials_supplied: - account_public_key = acme.client.net.key.public_key() - eab = messages.ExternalAccountBinding.from_data(account_public_key=account_public_key, - kid=config.eab_kid, - hmac_key=config.eab_hmac_key, - directory=acme.client.directory) - else: - eab = None - - if acme.external_account_required(): - if not eab_credentials_supplied: - msg = ("Server requires external account binding." - " Please use --eab-kid and --eab-hmac-key.") - raise errors.Error(msg) - - try: - newreg = messages.NewRegistration.from_data(email=config.email, - external_account_binding=eab) - return acme.new_account_and_tos(newreg, tos_cb) - except messages.Error as e: - if e.code == "invalidEmail" or e.code == "invalidContact": - if config.noninteractive_mode: - msg = ("The ACME server believes %s is an invalid email address. " - "Please ensure it is a valid email and attempt " - "registration again." % config.email) - raise errors.Error(msg) - else: - config.email = display_ops.get_email(invalid=True) - return perform_registration(acme, config, tos_cb) - else: - raise - - -class Client(object): - """Certbot's client. - - :ivar .IConfig config: Client configuration. - :ivar .Account account: Account registered with `register`. - :ivar .AuthHandler auth_handler: Authorizations handler that will - dispatch DV challenges to appropriate authenticators - (providing `.IAuthenticator` interface). - :ivar .IAuthenticator auth: Prepared (`.IAuthenticator.prepare`) - authenticator that can solve ACME challenges. - :ivar .IInstaller installer: Installer. - :ivar acme.client.BackwardsCompatibleClientV2 acme: Optional ACME - client API handle. You might already have one from `register`. - - """ - - def __init__(self, config, account_, auth, installer, acme=None): - """Initialize a client.""" - self.config = config - self.account = account_ - self.auth = auth - self.installer = installer - - # Initialize ACME if account is provided - if acme is None and self.account is not None: - acme = acme_from_config_key(config, self.account.key, self.account.regr) - self.acme = acme - - if auth is not None: - self.auth_handler = auth_handler.AuthHandler( - auth, self.acme, self.account, self.config.pref_challs) - else: - self.auth_handler = None - - def obtain_certificate_from_csr(self, csr, orderr=None): - """Obtain certificate. - - :param .util.CSR csr: PEM-encoded Certificate Signing - Request. The key used to generate this CSR can be different - than `authkey`. - :param acme.messages.OrderResource orderr: contains authzrs - - :returns: certificate and chain as PEM byte strings - :rtype: tuple - - """ - if self.auth_handler is None: - msg = ("Unable to obtain certificate because authenticator is " - "not set.") - logger.warning(msg) - raise errors.Error(msg) - if self.account.regr is None: - raise errors.Error("Please register with the ACME server first.") - - logger.debug("CSR: %s", csr) - - if orderr is None: - orderr = self._get_order_and_authorizations(csr.data, best_effort=False) - - deadline = datetime.datetime.now() + datetime.timedelta(seconds=90) - orderr = self.acme.finalize_order(orderr, deadline) - cert, chain = crypto_util.cert_and_chain_from_fullchain(orderr.fullchain_pem) - return cert.encode(), chain.encode() - - def obtain_certificate(self, domains, old_keypath=None): - """Obtains a certificate from the ACME server. - - `.register` must be called before `.obtain_certificate` - - :param list domains: domains to get a certificate - - :returns: certificate as PEM string, chain as PEM string, - newly generated private key (`.util.Key`), and DER-encoded - Certificate Signing Request (`.util.CSR`). - :rtype: tuple - - """ - - # We need to determine the key path, key PEM data, CSR path, - # and CSR PEM data. For a dry run, the paths are None because - # they aren't permanently saved to disk. For a lineage with - # --reuse-key, the key path and PEM data are derived from an - # existing file. - - if old_keypath is not None: - # We've been asked to reuse a specific existing private key. - # Therefore, we'll read it now and not generate a new one in - # either case below. - # - # We read in bytes here because the type of `key.pem` - # created below is also bytes. - with open(old_keypath, "rb") as f: - keypath = old_keypath - keypem = f.read() - key = util.Key(file=keypath, pem=keypem) # type: Optional[util.Key] - logger.info("Reusing existing private key from %s.", old_keypath) - else: - # The key is set to None here but will be created below. - key = None - - # Create CSR from names - if self.config.dry_run: - key = key or util.Key(file=None, - pem=crypto_util.make_key(self.config.rsa_key_size)) - csr = util.CSR(file=None, form="pem", - data=acme_crypto_util.make_csr( - key.pem, domains, self.config.must_staple)) - else: - key = key or crypto_util.init_save_key(self.config.rsa_key_size, - self.config.key_dir) - csr = crypto_util.init_save_csr(key, domains, self.config.csr_dir) - - orderr = self._get_order_and_authorizations(csr.data, self.config.allow_subset_of_names) - authzr = orderr.authorizations - auth_domains = set(a.body.identifier.value for a in authzr) - successful_domains = [d for d in domains if d in auth_domains] - - # allow_subset_of_names is currently disabled for wildcard - # certificates. The reason for this and checking allow_subset_of_names - # below is because successful_domains == domains is never true if - # domains contains a wildcard because the ACME spec forbids identifiers - # in authzs from containing a wildcard character. - if self.config.allow_subset_of_names and successful_domains != domains: - if not self.config.dry_run: - os.remove(key.file) - os.remove(csr.file) - return self.obtain_certificate(successful_domains) - else: - cert, chain = self.obtain_certificate_from_csr(csr, orderr) - - return cert, chain, key, csr - - def _get_order_and_authorizations(self, csr_pem, best_effort): - """Request a new order and complete its authorizations. - - :param str csr_pem: A CSR in PEM format. - :param bool best_effort: True if failing to complete all - authorizations should not raise an exception - - :returns: order resource containing its completed authorizations - :rtype: acme.messages.OrderResource - - """ - try: - orderr = self.acme.new_order(csr_pem) - except acme_errors.WildcardUnsupportedError: - raise errors.Error("The currently selected ACME CA endpoint does" - " not support issuing wildcard certificates.") - authzr = self.auth_handler.handle_authorizations(orderr, best_effort) - return orderr.update(authorizations=authzr) - - # pylint: disable=no-member - def obtain_and_enroll_certificate(self, domains, certname): - """Obtain and enroll certificate. - - Get a new certificate for the specified domains using the specified - authenticator and installer, and then create a new renewable lineage - containing it. - - :param domains: domains to request a certificate for - :type domains: `list` of `str` - :param certname: requested name of lineage - :type certname: `str` or `None` - - :returns: A new :class:`certbot.storage.RenewableCert` instance - referred to the enrolled cert lineage, False if the cert could not - be obtained, or None if doing a successful dry run. - - """ - cert, chain, key, _ = self.obtain_certificate(domains) - - if (self.config.config_dir != constants.CLI_DEFAULTS["config_dir"] or - self.config.work_dir != constants.CLI_DEFAULTS["work_dir"]): - logger.info( - "Non-standard path(s), might not work with crontab installed " - "by your operating system package manager") - - new_name = self._choose_lineagename(domains, certname) - - if self.config.dry_run: - logger.debug("Dry run: Skipping creating new lineage for %s", - new_name) - return None - else: - return storage.RenewableCert.new_lineage( - new_name, cert, - key.pem, chain, - self.config) - - def _choose_lineagename(self, domains, certname): - """Chooses a name for the new lineage. - - :param domains: domains in certificate request - :type domains: `list` of `str` - :param certname: requested name of lineage - :type certname: `str` or `None` - - :returns: lineage name that should be used - :rtype: str - - """ - if certname: - return certname - elif util.is_wildcard_domain(domains[0]): - # Don't make files and directories starting with *. - return domains[0][2:] - else: - return domains[0] - - def save_certificate(self, cert_pem, chain_pem, - cert_path, chain_path, fullchain_path): - """Saves the certificate received from the ACME server. - - :param str cert_pem: - :param str chain_pem: - :param str cert_path: Candidate path to a certificate. - :param str chain_path: Candidate path to a certificate chain. - :param str fullchain_path: Candidate path to a full cert chain. - - :returns: cert_path, chain_path, and fullchain_path as absolute - paths to the actual files - :rtype: `tuple` of `str` - - :raises IOError: If unable to find room to write the cert files - - """ - for path in cert_path, chain_path, fullchain_path: - util.make_or_verify_dir( - os.path.dirname(path), 0o755, compat.os_geteuid(), - self.config.strict_permissions) - - - cert_file, abs_cert_path = _open_pem_file('cert_path', cert_path) - - try: - cert_file.write(cert_pem) - finally: - cert_file.close() - logger.info("Server issued certificate; certificate written to %s", - abs_cert_path) - - chain_file, abs_chain_path =\ - _open_pem_file('chain_path', chain_path) - fullchain_file, abs_fullchain_path =\ - _open_pem_file('fullchain_path', fullchain_path) - - _save_chain(chain_pem, chain_file) - _save_chain(cert_pem + chain_pem, fullchain_file) - - return abs_cert_path, abs_chain_path, abs_fullchain_path - - def deploy_certificate(self, domains, privkey_path, - cert_path, chain_path, fullchain_path): - """Install certificate - - :param list domains: list of domains to install the certificate - :param str privkey_path: path to certificate private key - :param str cert_path: certificate file path (optional) - :param str chain_path: chain file path - - """ - if self.installer is None: - logger.warning("No installer specified, client is unable to deploy" - "the certificate") - raise errors.Error("No installer available") - - chain_path = None if chain_path is None else os.path.abspath(chain_path) - - msg = ("Unable to install the certificate") - with error_handler.ErrorHandler(self._recovery_routine_with_msg, msg): - for dom in domains: - self.installer.deploy_cert( - domain=dom, cert_path=os.path.abspath(cert_path), - key_path=os.path.abspath(privkey_path), - chain_path=chain_path, - fullchain_path=fullchain_path) - self.installer.save() # needed by the Apache plugin - - self.installer.save("Deployed ACME Certificate") - - msg = ("We were unable to install your certificate, " - "however, we successfully restored your " - "server to its prior configuration.") - with error_handler.ErrorHandler(self._rollback_and_restart, msg): - # sites may have been enabled / final cleanup - self.installer.restart() - - def enhance_config(self, domains, chain_path, ask_redirect=True): - """Enhance the configuration. - - :param list domains: list of domains to configure - :param chain_path: chain file path - :type chain_path: `str` or `None` - - :raises .errors.Error: if no installer is specified in the - client. - - """ - if self.installer is None: - logger.warning("No installer is specified, there isn't any " - "configuration to enhance.") - raise errors.Error("No installer available") - - enhanced = False - enhancement_info = ( - ("hsts", "ensure-http-header", "Strict-Transport-Security"), - ("redirect", "redirect", None), - ("staple", "staple-ocsp", chain_path), - ("uir", "ensure-http-header", "Upgrade-Insecure-Requests"),) - supported = self.installer.supported_enhancements() - - for config_name, enhancement_name, option in enhancement_info: - config_value = getattr(self.config, config_name) - if enhancement_name in supported: - if ask_redirect: - if config_name == "redirect" and config_value is None: - config_value = enhancements.ask(enhancement_name) - if config_value: - self.apply_enhancement(domains, enhancement_name, option) - enhanced = True - elif config_value: - logger.warning( - "Option %s is not supported by the selected installer. " - "Skipping enhancement.", config_name) - - msg = ("We were unable to restart web server") - if enhanced: - with error_handler.ErrorHandler(self._rollback_and_restart, msg): - self.installer.restart() - - def apply_enhancement(self, domains, enhancement, options=None): - """Applies an enhancement on all domains. - - :param list domains: list of ssl_vhosts (as strings) - :param str enhancement: name of enhancement, e.g. ensure-http-header - :param str options: options to enhancement, e.g. Strict-Transport-Security - - .. note:: When more `options` are needed, make options a list. - - :raises .errors.PluginError: If Enhancement is not supported, or if - there is any other problem with the enhancement. - - - """ - msg = ("We were unable to set up enhancement %s for your server, " - "however, we successfully installed your certificate." - % (enhancement)) - with error_handler.ErrorHandler(self._recovery_routine_with_msg, msg): - for dom in domains: - try: - self.installer.enhance(dom, enhancement, options) - except errors.PluginEnhancementAlreadyPresent: - if enhancement == "ensure-http-header": - logger.warning("Enhancement %s was already set.", - options) - else: - logger.warning("Enhancement %s was already set.", - enhancement) - except errors.PluginError: - logger.warning("Unable to set enhancement %s for %s", - enhancement, dom) - raise - - self.installer.save("Add enhancement %s" % (enhancement)) - - def _recovery_routine_with_msg(self, success_msg): - """Calls the installer's recovery routine and prints success_msg - - :param str success_msg: message to show on successful recovery - - """ - self.installer.recovery_routine() - reporter = zope.component.getUtility(interfaces.IReporter) - reporter.add_message(success_msg, reporter.HIGH_PRIORITY) - - def _rollback_and_restart(self, success_msg): - """Rollback the most recent checkpoint and restart the webserver - - :param str success_msg: message to show on successful rollback - - """ - logger.critical("Rolling back to previous server configuration...") - reporter = zope.component.getUtility(interfaces.IReporter) - try: - self.installer.rollback_checkpoints() - self.installer.restart() - except: - reporter.add_message( - "An error occurred and we failed to restore your config and " - "restart your server. Please post to " - "https://community.letsencrypt.org/c/server-config " - "with details about your configuration and this error you received.", - reporter.HIGH_PRIORITY) - raise - reporter.add_message(success_msg, reporter.HIGH_PRIORITY) - - -def validate_key_csr(privkey, csr=None): - """Validate Key and CSR files. - - Verifies that the client key and csr arguments are valid and correspond to - one another. This does not currently check the names in the CSR due to - the inability to read SANs from CSRs in python crypto libraries. - - If csr is left as None, only the key will be validated. - - :param privkey: Key associated with CSR - :type privkey: :class:`certbot.util.Key` - - :param .util.CSR csr: CSR - - :raises .errors.Error: when validation fails - - """ - # TODO: Handle all of these problems appropriately - # The client can eventually do things like prompt the user - # and allow the user to take more appropriate actions - - # Key must be readable and valid. - if privkey.pem and not crypto_util.valid_privkey(privkey.pem): - raise errors.Error("The provided key is not a valid key") - - if csr: - if csr.form == "der": - csr_obj = OpenSSL.crypto.load_certificate_request( - OpenSSL.crypto.FILETYPE_ASN1, csr.data) - cert_buffer = OpenSSL.crypto.dump_certificate_request( - OpenSSL.crypto.FILETYPE_PEM, csr_obj - ) - csr = util.CSR(csr.file, cert_buffer, "pem") - - # If CSR is provided, it must be readable and valid. - if csr.data and not crypto_util.valid_csr(csr.data): - raise errors.Error("The provided CSR is not a valid CSR") - - # If both CSR and key are provided, the key must be the same key used - # in the CSR. - if csr.data and privkey.pem: - if not crypto_util.csr_matches_pubkey( - csr.data, privkey.pem): - raise errors.Error("The key and CSR do not match") - - -def rollback(default_installer, checkpoints, config, plugins): - """Revert configuration the specified number of checkpoints. - - :param int checkpoints: Number of checkpoints to revert. - - :param config: Configuration. - :type config: :class:`certbot.interfaces.IConfig` - - """ - # Misconfigurations are only a slight problems... allow the user to rollback - installer = plugin_selection.pick_installer( - config, default_installer, plugins, question="Which installer " - "should be used for rollback?") - - # No Errors occurred during init... proceed normally - # If installer is None... couldn't find an installer... there shouldn't be - # anything to rollback - if installer is not None: - installer.rollback_checkpoints(checkpoints) - installer.restart() - - -def view_config_changes(config, num=None): - """View checkpoints and associated configuration changes. - - .. note:: This assumes that the installation is using a Reverter object. - - :param config: Configuration. - :type config: :class:`certbot.interfaces.IConfig` - - """ - rev = reverter.Reverter(config) - rev.recovery_routine() - rev.view_config_changes(num) - -def _open_pem_file(cli_arg_path, pem_path): - """Open a pem file. - - If cli_arg_path was set by the client, open that. - Otherwise, uniquify the file path. - - :param str cli_arg_path: the cli arg name, e.g. cert_path - :param str pem_path: the pem file path to open - - :returns: a tuple of file object and its absolute file path - - """ - if cli.set_by_cli(cli_arg_path): - return util.safe_open(pem_path, chmod=0o644, mode="wb"),\ - os.path.abspath(pem_path) - else: - uniq = util.unique_file(pem_path, 0o644, "wb") - return uniq[0], os.path.abspath(uniq[1]) - -def _save_chain(chain_pem, chain_file): - """Saves chain_pem at a unique path based on chain_path. - - :param str chain_pem: certificate chain in PEM format - :param str chain_file: chain file object - - """ - try: - chain_file.write(chain_pem) - finally: - chain_file.close() - - logger.info("Cert chain written to %s", chain_file.name) diff --git a/certbot/compat.py b/certbot/compat.py deleted file mode 100644 index 7d936aa9d80..00000000000 --- a/certbot/compat.py +++ /dev/null @@ -1,201 +0,0 @@ -""" -Compatibility layer to run certbot both on Linux and Windows. - -The approach used here is similar to Modernizr for Web browsers. -We do not check the platform type to determine if a particular logic is supported. -Instead, we apply a logic, and then fallback to another logic if first logic -is not supported at runtime. - -Then logic chains are abstracted into single functions to be exposed to certbot. -""" -import os -import select -import sys -import errno -import ctypes -import stat - -from certbot import errors - -try: - # Linux specific - import fcntl # pylint: disable=import-error -except ImportError: - # Windows specific - import msvcrt # pylint: disable=import-error - -UNPRIVILEGED_SUBCOMMANDS_ALLOWED = [ - 'certificates', 'enhance', 'revoke', 'delete', - 'register', 'unregister', 'config_changes', 'plugins'] -def raise_for_non_administrative_windows_rights(subcommand): - """ - On Windows, raise if current shell does not have the administrative rights. - Do nothing on Linux. - - :param str subcommand: The subcommand (like 'certonly') passed to the certbot client. - - :raises .errors.Error: If the provided subcommand must be run on a shell with - administrative rights, and current shell does not have these rights. - - """ - # Why not simply try ctypes.windll.shell32.IsUserAnAdmin() and catch AttributeError ? - # Because windll exists only on a Windows runtime, and static code analysis engines - # do not like at all non existent objects when run from Linux (even if we handle properly - # all the cases in the code). - # So we access windll only by reflection to trick theses engines. - if hasattr(ctypes, 'windll') and subcommand not in UNPRIVILEGED_SUBCOMMANDS_ALLOWED: - windll = getattr(ctypes, 'windll') - if windll.shell32.IsUserAnAdmin() == 0: - raise errors.Error( - 'Error, "{0}" subcommand must be run on a shell with administrative rights.' - .format(subcommand)) - -def os_geteuid(): - """ - Get current user uid - - :returns: The current user uid. - :rtype: int - - """ - try: - # Linux specific - return os.geteuid() - except AttributeError: - # Windows specific - return 0 - -def os_rename(src, dst): - """ - Rename a file to a destination path and handles situations where the destination exists. - - :param str src: The current file path. - :param str dst: The new file path. - """ - try: - os.rename(src, dst) - except OSError as err: - # Windows specific, renaming a file on an existing path is not possible. - # On Python 3, the best fallback with atomic capabilities we have is os.replace. - if err.errno != errno.EEXIST: - # Every other error is a legitimate exception. - raise - if not hasattr(os, 'replace'): # pragma: no cover - # We should never go on this line. Either we are on Linux and os.rename has succeeded, - # either we are on Windows, and only Python >= 3.4 is supported where os.replace is - # available. - raise RuntimeError('Error: tried to run os_rename on Python < 3.3. ' - 'Certbot supports only Python 3.4 >= on Windows.') - getattr(os, 'replace')(src, dst) - - -def readline_with_timeout(timeout, prompt): - """ - Read user input to return the first line entered, or raise after specified timeout. - - :param float timeout: The timeout in seconds given to the user. - :param str prompt: The prompt message to display to the user. - - :returns: The first line entered by the user. - :rtype: str - - """ - try: - # Linux specific - # - # Call to select can only be done like this on UNIX - rlist, _, _ = select.select([sys.stdin], [], [], timeout) - if not rlist: - raise errors.Error( - "Timed out waiting for answer to prompt '{0}'".format(prompt)) - return rlist[0].readline() - except OSError: - # Windows specific - # - # No way with select to make a timeout to the user input on Windows, - # as select only supports socket in this case. - # So no timeout on Windows for now. - return sys.stdin.readline() - -def lock_file(fd): - """ - Lock the file linked to the specified file descriptor. - - :param int fd: The file descriptor of the file to lock. - - """ - if 'fcntl' in sys.modules: - # Linux specific - fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) - else: - # Windows specific - msvcrt.locking(fd, msvcrt.LK_NBLCK, 1) - -def release_locked_file(fd, path): - """ - Remove, close, and release a lock file specified by its file descriptor and its path. - - :param int fd: The file descriptor of the lock file. - :param str path: The path of the lock file. - - """ - # Linux specific - # - # It is important the lock file is removed before it's released, - # otherwise: - # - # process A: open lock file - # process B: release lock file - # process A: lock file - # process A: check device and inode - # process B: delete file - # process C: open and lock a different file at the same path - try: - os.remove(path) - except OSError as err: - if err.errno == errno.EACCES: - # Windows specific - # We will not be able to remove a file before closing it. - # To avoid race conditions described for Linux, we will not delete the lockfile, - # just close it to be reused on the next Certbot call. - pass - else: - raise - finally: - os.close(fd) - -def compare_file_modes(mode1, mode2): - """Return true if the two modes can be considered as equals for this platform""" - if 'fcntl' in sys.modules: - # Linux specific: standard compare - return oct(stat.S_IMODE(mode1)) == oct(stat.S_IMODE(mode2)) - # Windows specific: most of mode bits are ignored on Windows. Only check user R/W rights. - return (stat.S_IMODE(mode1) & stat.S_IREAD == stat.S_IMODE(mode2) & stat.S_IREAD - and stat.S_IMODE(mode1) & stat.S_IWRITE == stat.S_IMODE(mode2) & stat.S_IWRITE) - -WINDOWS_DEFAULT_FOLDERS = { - 'config': 'C:\\Certbot', - 'work': 'C:\\Certbot\\lib', - 'logs': 'C:\\Certbot\\log', -} -LINUX_DEFAULT_FOLDERS = { - 'config': '/etc/letsencrypt', - 'work': '/var/lib/letsencrypt', - 'logs': '/var/log/letsencrypt', -} - -def get_default_folder(folder_type): - """ - Return the relevant default folder for the current OS - - :param str folder_type: The type of folder to retrieve (config, work or logs) - - :returns: The relevant default folder. - :rtype: str - - """ - if 'fcntl' in sys.modules: - # Linux specific - return LINUX_DEFAULT_FOLDERS[folder_type] - # Windows specific - return WINDOWS_DEFAULT_FOLDERS[folder_type] diff --git a/certbot/configuration.py b/certbot/configuration.py deleted file mode 100644 index daf514be802..00000000000 --- a/certbot/configuration.py +++ /dev/null @@ -1,158 +0,0 @@ -"""Certbot user-supplied configuration.""" -import copy -import os - -from six.moves.urllib import parse # pylint: disable=import-error -import zope.interface - -from certbot import constants -from certbot import errors -from certbot import interfaces -from certbot import util - - -@zope.interface.implementer(interfaces.IConfig) -class NamespaceConfig(object): - """Configuration wrapper around :class:`argparse.Namespace`. - - For more documentation, including available attributes, please see - :class:`certbot.interfaces.IConfig`. However, note that - the following attributes are dynamically resolved using - :attr:`~certbot.interfaces.IConfig.work_dir` and relative - paths defined in :py:mod:`certbot.constants`: - - - `accounts_dir` - - `csr_dir` - - `in_progress_dir` - - `key_dir` - - `temp_checkpoint_dir` - - And the following paths are dynamically resolved using - :attr:`~certbot.interfaces.IConfig.config_dir` and relative - paths defined in :py:mod:`certbot.constants`: - - - `default_archive_dir` - - `live_dir` - - `renewal_configs_dir` - - :ivar namespace: Namespace typically produced by - :meth:`argparse.ArgumentParser.parse_args`. - :type namespace: :class:`argparse.Namespace` - - """ - - def __init__(self, namespace): - object.__setattr__(self, 'namespace', namespace) - - self.namespace.config_dir = os.path.abspath(self.namespace.config_dir) - self.namespace.work_dir = os.path.abspath(self.namespace.work_dir) - self.namespace.logs_dir = os.path.abspath(self.namespace.logs_dir) - - # Check command line parameters sanity, and error out in case of problem. - check_config_sanity(self) - - def __getattr__(self, name): - return getattr(self.namespace, name) - - def __setattr__(self, name, value): - setattr(self.namespace, name, value) - - @property - def server_path(self): - """File path based on ``server``.""" - parsed = parse.urlparse(self.namespace.server) - return (parsed.netloc + parsed.path).replace('/', os.path.sep) - - @property - def accounts_dir(self): # pylint: disable=missing-docstring - return self.accounts_dir_for_server_path(self.server_path) - - def accounts_dir_for_server_path(self, server_path): - """Path to accounts directory based on server_path""" - return os.path.join( - self.namespace.config_dir, constants.ACCOUNTS_DIR, server_path) - - @property - def backup_dir(self): # pylint: disable=missing-docstring - return os.path.join(self.namespace.work_dir, constants.BACKUP_DIR) - - @property - def csr_dir(self): # pylint: disable=missing-docstring - return os.path.join(self.namespace.config_dir, constants.CSR_DIR) - - @property - def in_progress_dir(self): # pylint: disable=missing-docstring - return os.path.join(self.namespace.work_dir, constants.IN_PROGRESS_DIR) - - @property - def key_dir(self): # pylint: disable=missing-docstring - return os.path.join(self.namespace.config_dir, constants.KEY_DIR) - - @property - def temp_checkpoint_dir(self): # pylint: disable=missing-docstring - return os.path.join( - self.namespace.work_dir, constants.TEMP_CHECKPOINT_DIR) - - def __deepcopy__(self, _memo): - # Work around https://bugs.python.org/issue1515 for py26 tests :( :( - # https://travis-ci.org/letsencrypt/letsencrypt/jobs/106900743#L3276 - new_ns = copy.deepcopy(self.namespace) - return type(self)(new_ns) - - @property - def default_archive_dir(self): # pylint: disable=missing-docstring - return os.path.join(self.namespace.config_dir, constants.ARCHIVE_DIR) - - @property - def live_dir(self): # pylint: disable=missing-docstring - return os.path.join(self.namespace.config_dir, constants.LIVE_DIR) - - @property - def renewal_configs_dir(self): # pylint: disable=missing-docstring - return os.path.join( - self.namespace.config_dir, constants.RENEWAL_CONFIGS_DIR) - - @property - def renewal_hooks_dir(self): - """Path to directory with hooks to run with the renew subcommand.""" - return os.path.join(self.namespace.config_dir, - constants.RENEWAL_HOOKS_DIR) - - @property - def renewal_pre_hooks_dir(self): - """Path to the pre-hook directory for the renew subcommand.""" - return os.path.join(self.renewal_hooks_dir, - constants.RENEWAL_PRE_HOOKS_DIR) - - @property - def renewal_deploy_hooks_dir(self): - """Path to the deploy-hook directory for the renew subcommand.""" - return os.path.join(self.renewal_hooks_dir, - constants.RENEWAL_DEPLOY_HOOKS_DIR) - - @property - def renewal_post_hooks_dir(self): - """Path to the post-hook directory for the renew subcommand.""" - return os.path.join(self.renewal_hooks_dir, - constants.RENEWAL_POST_HOOKS_DIR) - - -def check_config_sanity(config): - """Validate command line options and display error message if - requirements are not met. - - :param config: IConfig instance holding user configuration - :type args: :class:`certbot.interfaces.IConfig` - - """ - # Port check - if config.http01_port == config.tls_sni_01_port: - raise errors.ConfigurationError( - "Trying to run http-01 and tls-sni-01 " - "on the same port ({0})".format(config.tls_sni_01_port)) - - # Domain checks - if config.namespace.domains is not None: - for domain in config.namespace.domains: - # This may be redundant, but let's be paranoid - util.enforce_domain_sanity(domain) diff --git a/certbot/constants.py b/certbot/constants.py deleted file mode 100644 index 7d0e0c87952..00000000000 --- a/certbot/constants.py +++ /dev/null @@ -1,230 +0,0 @@ -"""Certbot constants.""" -import logging -import os -import pkg_resources - -from acme import challenges -from certbot import compat - -SETUPTOOLS_PLUGINS_ENTRY_POINT = "certbot.plugins" -"""Setuptools entry point group name for plugins.""" - -OLD_SETUPTOOLS_PLUGINS_ENTRY_POINT = "letsencrypt.plugins" -"""Plugins Setuptools entry point before rename.""" - -CLI_DEFAULTS = dict( - config_files=[ - os.path.join(compat.get_default_folder('config'), 'cli.ini'), - # http://freedesktop.org/wiki/Software/xdg-user-dirs/ - os.path.join(os.environ.get("XDG_CONFIG_HOME", "~/.config"), - "letsencrypt", "cli.ini"), - ], - - # Main parser - verbose_count=-int(logging.INFO / 10), - text_mode=False, - max_log_backups=1000, - noninteractive_mode=False, - force_interactive=False, - domains=[], - certname=None, - dry_run=False, - register_unsafely_without_email=False, - update_registration=False, - email=None, - eff_email=None, - reinstall=False, - expand=False, - renew_by_default=False, - renew_with_new_domains=False, - autorenew=True, - allow_subset_of_names=False, - tos=False, - account=None, - duplicate=False, - os_packages_only=False, - no_self_upgrade=False, - no_bootstrap=False, - quiet=False, - staging=False, - debug=False, - debug_challenges=False, - no_verify_ssl=False, - tls_sni_01_port=challenges.TLSSNI01Response.PORT, - tls_sni_01_address="", - http01_port=challenges.HTTP01Response.PORT, - http01_address="", - break_my_certs=False, - rsa_key_size=2048, - must_staple=False, - redirect=None, - auto_hsts=False, - hsts=None, - uir=None, - staple=None, - strict_permissions=False, - pref_challs=[], - validate_hooks=True, - directory_hooks=True, - reuse_key=False, - disable_renew_updates=False, - random_sleep_on_renew=True, - eab_hmac_key=None, - eab_kid=None, - - # Subparsers - num=None, - user_agent=None, - user_agent_comment=None, - csr=None, - reason=0, - delete_after_revoke=None, - rollback_checkpoints=1, - init=False, - prepare=False, - ifaces=None, - - # Path parsers - auth_cert_path="./cert.pem", - auth_chain_path="./chain.pem", - key_path=None, - config_dir=compat.get_default_folder('config'), - work_dir=compat.get_default_folder('work'), - logs_dir=compat.get_default_folder('logs'), - server="https://acme-v02.api.letsencrypt.org/directory", - - # Plugins parsers - configurator=None, - authenticator=None, - installer=None, - apache=False, - nginx=False, - standalone=False, - manual=False, - webroot=False, - dns_cloudflare=False, - dns_cloudxns=False, - dns_digitalocean=False, - dns_dnsimple=False, - dns_dnsmadeeasy=False, - dns_gehirn=False, - dns_google=False, - dns_linode=False, - dns_luadns=False, - dns_nsone=False, - dns_ovh=False, - dns_rfc2136=False, - dns_route53=False, - dns_sakuracloud=False - -) -STAGING_URI = "https://acme-staging-v02.api.letsencrypt.org/directory" - -# The set of reasons for revoking a certificate is defined in RFC 5280 in -# section 5.3.1. The reasons that users are allowed to submit are restricted to -# those accepted by the ACME server implementation. They are listed in -# `letsencrypt.boulder.revocation.reasons.go`. -REVOCATION_REASONS = { - "unspecified": 0, - "keycompromise": 1, - "affiliationchanged": 3, - "superseded": 4, - "cessationofoperation": 5} - -"""Defaults for CLI flags and `.IConfig` attributes.""" - -QUIET_LOGGING_LEVEL = logging.WARNING -"""Logging level to use in quiet mode.""" - -RENEWER_DEFAULTS = dict( - renewer_enabled="yes", - renew_before_expiry="30 days", - # This value should ensure that there is never a deployment delay by - # default. - deploy_before_expiry="99 years", -) -"""Defaults for renewer script.""" - - -ENHANCEMENTS = ["redirect", "ensure-http-header", "ocsp-stapling", "spdy"] -"""List of possible :class:`certbot.interfaces.IInstaller` -enhancements. - -List of expected options parameters: -- redirect: None -- ensure-http-header: name of header (i.e. Strict-Transport-Security) -- ocsp-stapling: certificate chain file path -- spdy: TODO - -""" - -ARCHIVE_DIR = "archive" -"""Archive directory, relative to `IConfig.config_dir`.""" - -CONFIG_DIRS_MODE = 0o755 -"""Directory mode for ``.IConfig.config_dir`` et al.""" - -ACCOUNTS_DIR = "accounts" -"""Directory where all accounts are saved.""" - -LE_REUSE_SERVERS = { - 'acme-v02.api.letsencrypt.org/directory': 'acme-v01.api.letsencrypt.org/directory', - 'acme-staging-v02.api.letsencrypt.org/directory': - 'acme-staging.api.letsencrypt.org/directory' -} -"""Servers that can reuse accounts from other servers.""" - -BACKUP_DIR = "backups" -"""Directory (relative to `IConfig.work_dir`) where backups are kept.""" - -CSR_DIR = "csr" -"""See `.IConfig.csr_dir`.""" - -IN_PROGRESS_DIR = "IN_PROGRESS" -"""Directory used before a permanent checkpoint is finalized (relative to -`IConfig.work_dir`).""" - -KEY_DIR = "keys" -"""Directory (relative to `IConfig.config_dir`) where keys are saved.""" - -LIVE_DIR = "live" -"""Live directory, relative to `IConfig.config_dir`.""" - -TEMP_CHECKPOINT_DIR = "temp_checkpoint" -"""Temporary checkpoint directory (relative to `IConfig.work_dir`).""" - -RENEWAL_CONFIGS_DIR = "renewal" -"""Renewal configs directory, relative to `IConfig.config_dir`.""" - -RENEWAL_HOOKS_DIR = "renewal-hooks" -"""Basename of directory containing hooks to run with the renew command.""" - -RENEWAL_PRE_HOOKS_DIR = "pre" -"""Basename of directory containing pre-hooks to run with the renew command.""" - -RENEWAL_DEPLOY_HOOKS_DIR = "deploy" -"""Basename of directory containing deploy-hooks to run with the renew command.""" - -RENEWAL_POST_HOOKS_DIR = "post" -"""Basename of directory containing post-hooks to run with the renew command.""" - -FORCE_INTERACTIVE_FLAG = "--force-interactive" -"""Flag to disable TTY checking in IDisplay.""" - -EFF_SUBSCRIBE_URI = "https://supporters.eff.org/subscribe/certbot" -"""EFF URI used to submit the e-mail address of users who opt-in.""" - -SSL_DHPARAMS_DEST = "ssl-dhparams.pem" -"""Name of the ssl_dhparams file as saved in `IConfig.config_dir`.""" - -SSL_DHPARAMS_SRC = pkg_resources.resource_filename( - "certbot", "ssl-dhparams.pem") -"""Path to the nginx ssl_dhparams file found in the Certbot distribution.""" - -UPDATED_SSL_DHPARAMS_DIGEST = ".updated-ssl-dhparams-pem-digest.txt" -"""Name of the hash of the updated or informed ssl_dhparams as saved in `IConfig.config_dir`.""" - -ALL_SSL_DHPARAMS_HASHES = [ - '9ba6429597aeed2d8617a7705b56e96d044f64b07971659382e426675105654b', -] -"""SHA256 hashes of the contents of all versions of SSL_DHPARAMS_SRC""" diff --git a/certbot/crypto_util.py b/certbot/crypto_util.py deleted file mode 100644 index c4a389cd5ec..00000000000 --- a/certbot/crypto_util.py +++ /dev/null @@ -1,477 +0,0 @@ -"""Certbot client crypto utility functions. - -.. todo:: Make the transition to use PSS rather than PKCS1_v1_5 when the server - is capable of handling the signatures. - -""" -import hashlib -import logging -import os -import warnings - -import pyrfc3339 -import six -import zope.component -from cryptography.exceptions import InvalidSignature -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives.asymmetric.ec import ECDSA -from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey -from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 -from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey -# https://github.com/python/typeshed/tree/master/third_party/2/cryptography -from cryptography import x509 # type: ignore -from OpenSSL import crypto -from OpenSSL import SSL # type: ignore - -from acme import crypto_util as acme_crypto_util -from acme.magic_typing import IO # pylint: disable=unused-import, no-name-in-module -from certbot import compat -from certbot import errors -from certbot import interfaces -from certbot import util - - -logger = logging.getLogger(__name__) - - -# High level functions -def init_save_key(key_size, key_dir, keyname="key-certbot.pem"): - """Initializes and saves a privkey. - - Inits key and saves it in PEM format on the filesystem. - - .. note:: keyname is the attempted filename, it may be different if a file - already exists at the path. - - :param int key_size: RSA key size in bits - :param str key_dir: Key save directory. - :param str keyname: Filename of key - - :returns: Key - :rtype: :class:`certbot.util.Key` - - :raises ValueError: If unable to generate the key given key_size. - - """ - try: - key_pem = make_key(key_size) - except ValueError as err: - logger.error("", exc_info=True) - raise err - - config = zope.component.getUtility(interfaces.IConfig) - # Save file - util.make_or_verify_dir(key_dir, 0o700, compat.os_geteuid(), - config.strict_permissions) - key_f, key_path = util.unique_file( - os.path.join(key_dir, keyname), 0o600, "wb") - with key_f: - key_f.write(key_pem) - logger.debug("Generating key (%d bits): %s", key_size, key_path) - - return util.Key(key_path, key_pem) - - -def init_save_csr(privkey, names, path): - """Initialize a CSR with the given private key. - - :param privkey: Key to include in the CSR - :type privkey: :class:`certbot.util.Key` - - :param set names: `str` names to include in the CSR - - :param str path: Certificate save directory. - - :returns: CSR - :rtype: :class:`certbot.util.CSR` - - """ - config = zope.component.getUtility(interfaces.IConfig) - - csr_pem = acme_crypto_util.make_csr( - privkey.pem, names, must_staple=config.must_staple) - - # Save CSR - util.make_or_verify_dir(path, 0o755, compat.os_geteuid(), - config.strict_permissions) - csr_f, csr_filename = util.unique_file( - os.path.join(path, "csr-certbot.pem"), 0o644, "wb") - with csr_f: - csr_f.write(csr_pem) - logger.debug("Creating CSR: %s", csr_filename) - - return util.CSR(csr_filename, csr_pem, "pem") - - -# WARNING: the csr and private key file are possible attack vectors for TOCTOU -# We should either... -# A. Do more checks to verify that the CSR is trusted/valid -# B. Audit the parsing code for vulnerabilities - -def valid_csr(csr): - """Validate CSR. - - Check if `csr` is a valid CSR for the given domains. - - :param str csr: CSR in PEM. - - :returns: Validity of CSR. - :rtype: bool - - """ - try: - req = crypto.load_certificate_request( - crypto.FILETYPE_PEM, csr) - return req.verify(req.get_pubkey()) - except crypto.Error: - logger.debug("", exc_info=True) - return False - - -def csr_matches_pubkey(csr, privkey): - """Does private key correspond to the subject public key in the CSR? - - :param str csr: CSR in PEM. - :param str privkey: Private key file contents (PEM) - - :returns: Correspondence of private key to CSR subject public key. - :rtype: bool - - """ - req = crypto.load_certificate_request( - crypto.FILETYPE_PEM, csr) - pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, privkey) - try: - return req.verify(pkey) - except crypto.Error: - logger.debug("", exc_info=True) - return False - - -def import_csr_file(csrfile, data): - """Import a CSR file, which can be either PEM or DER. - - :param str csrfile: CSR filename - :param str data: contents of the CSR file - - :returns: (`crypto.FILETYPE_PEM`, - util.CSR object representing the CSR, - list of domains requested in the CSR) - :rtype: tuple - - """ - PEM = crypto.FILETYPE_PEM - load = crypto.load_certificate_request - try: - # Try to parse as DER first, then fall back to PEM. - csr = load(crypto.FILETYPE_ASN1, data) - except crypto.Error: - try: - csr = load(PEM, data) - except crypto.Error: - raise errors.Error("Failed to parse CSR file: {0}".format(csrfile)) - - domains = _get_names_from_loaded_cert_or_req(csr) - # Internally we always use PEM, so re-encode as PEM before returning. - data_pem = crypto.dump_certificate_request(PEM, csr) - return PEM, util.CSR(file=csrfile, data=data_pem, form="pem"), domains - - -def make_key(bits): - """Generate PEM encoded RSA key. - - :param int bits: Number of bits, at least 1024. - - :returns: new RSA key in PEM form with specified number of bits - :rtype: str - - """ - assert bits >= 1024 # XXX - key = crypto.PKey() - key.generate_key(crypto.TYPE_RSA, bits) - return crypto.dump_privatekey(crypto.FILETYPE_PEM, key) - - -def valid_privkey(privkey): - """Is valid RSA private key? - - :param str privkey: Private key file contents in PEM - - :returns: Validity of private key. - :rtype: bool - - """ - try: - return crypto.load_privatekey( - crypto.FILETYPE_PEM, privkey).check() - except (TypeError, crypto.Error): - return False - - -def verify_renewable_cert(renewable_cert): - """For checking that your certs were not corrupted on disk. - - Several things are checked: - 1. Signature verification for the cert. - 2. That fullchain matches cert and chain when concatenated. - 3. Check that the private key matches the certificate. - - :param `.storage.RenewableCert` renewable_cert: cert to verify - - :raises errors.Error: If verification fails. - """ - verify_renewable_cert_sig(renewable_cert) - verify_fullchain(renewable_cert) - verify_cert_matches_priv_key(renewable_cert.cert, renewable_cert.privkey) - - -def verify_renewable_cert_sig(renewable_cert): - """ Verifies the signature of a `.storage.RenewableCert` object. - - :param `.storage.RenewableCert` renewable_cert: cert to verify - - :raises errors.Error: If signature verification fails. - """ - try: - with open(renewable_cert.chain, 'rb') as chain_file: # type: IO[bytes] - chain = x509.load_pem_x509_certificate(chain_file.read(), default_backend()) - with open(renewable_cert.cert, 'rb') as cert_file: # type: IO[bytes] - cert = x509.load_pem_x509_certificate(cert_file.read(), default_backend()) - pk = chain.public_key() - with warnings.catch_warnings(): - warnings.simplefilter("ignore") - if isinstance(pk, RSAPublicKey): - # https://github.com/python/typeshed/blob/master/third_party/2/cryptography/hazmat/primitives/asymmetric/rsa.pyi - verifier = pk.verifier( # type: ignore - cert.signature, PKCS1v15(), cert.signature_hash_algorithm - ) - verifier.update(cert.tbs_certificate_bytes) - verifier.verify() - elif isinstance(pk, EllipticCurvePublicKey): - verifier = pk.verifier( - cert.signature, ECDSA(cert.signature_hash_algorithm) - ) - verifier.update(cert.tbs_certificate_bytes) - verifier.verify() - else: - raise errors.Error("Unsupported public key type") - except (IOError, ValueError, InvalidSignature) as e: - error_str = "verifying the signature of the cert located at {0} has failed. \ - Details: {1}".format(renewable_cert.cert, e) - logger.exception(error_str) - raise errors.Error(error_str) - - -def verify_cert_matches_priv_key(cert_path, key_path): - """ Verifies that the private key and cert match. - - :param str cert_path: path to a cert in PEM format - :param str key_path: path to a private key file - - :raises errors.Error: If they don't match. - """ - try: - context = SSL.Context(SSL.SSLv23_METHOD) - context.use_certificate_file(cert_path) - context.use_privatekey_file(key_path) - context.check_privatekey() - except (IOError, SSL.Error) as e: - error_str = "verifying the cert located at {0} matches the \ - private key located at {1} has failed. \ - Details: {2}".format(cert_path, - key_path, e) - logger.exception(error_str) - raise errors.Error(error_str) - - -def verify_fullchain(renewable_cert): - """ Verifies that fullchain is indeed cert concatenated with chain. - - :param `.storage.RenewableCert` renewable_cert: cert to verify - - :raises errors.Error: If cert and chain do not combine to fullchain. - """ - try: - with open(renewable_cert.chain) as chain_file: # type: IO[str] - chain = chain_file.read() - with open(renewable_cert.cert) as cert_file: # type: IO[str] - cert = cert_file.read() - with open(renewable_cert.fullchain) as fullchain_file: # type: IO[str] - fullchain = fullchain_file.read() - if (cert + chain) != fullchain: - error_str = "fullchain does not match cert + chain for {0}!" - error_str = error_str.format(renewable_cert.lineagename) - raise errors.Error(error_str) - except IOError as e: - error_str = "reading one of cert, chain, or fullchain has failed: {0}".format(e) - logger.exception(error_str) - raise errors.Error(error_str) - except errors.Error as e: - raise e - - -def pyopenssl_load_certificate(data): - """Load PEM/DER certificate. - - :raises errors.Error: - - """ - - openssl_errors = [] - - for file_type in (crypto.FILETYPE_PEM, crypto.FILETYPE_ASN1): - try: - return crypto.load_certificate(file_type, data), file_type - except crypto.Error as error: # TODO: other errors? - openssl_errors.append(error) - raise errors.Error("Unable to load: {0}".format(",".join( - str(error) for error in openssl_errors))) - - -def _load_cert_or_req(cert_or_req_str, load_func, - typ=crypto.FILETYPE_PEM): - try: - return load_func(typ, cert_or_req_str) - except crypto.Error: - logger.error("", exc_info=True) - raise - - -def _get_sans_from_cert_or_req(cert_or_req_str, load_func, - typ=crypto.FILETYPE_PEM): - # pylint: disable=protected-access - return acme_crypto_util._pyopenssl_cert_or_req_san(_load_cert_or_req( - cert_or_req_str, load_func, typ)) - - -def get_sans_from_cert(cert, typ=crypto.FILETYPE_PEM): - """Get a list of Subject Alternative Names from a certificate. - - :param str cert: Certificate (encoded). - :param typ: `crypto.FILETYPE_PEM` or `crypto.FILETYPE_ASN1` - - :returns: A list of Subject Alternative Names. - :rtype: list - - """ - return _get_sans_from_cert_or_req( - cert, crypto.load_certificate, typ) - - -def _get_names_from_cert_or_req(cert_or_req, load_func, typ): - loaded_cert_or_req = _load_cert_or_req(cert_or_req, load_func, typ) - return _get_names_from_loaded_cert_or_req(loaded_cert_or_req) - - -def _get_names_from_loaded_cert_or_req(loaded_cert_or_req): - # pylint: disable=protected-access - return acme_crypto_util._pyopenssl_cert_or_req_all_names(loaded_cert_or_req) - - -def get_names_from_cert(csr, typ=crypto.FILETYPE_PEM): - """Get a list of domains from a cert, including the CN if it is set. - - :param str cert: Certificate (encoded). - :param typ: `crypto.FILETYPE_PEM` or `crypto.FILETYPE_ASN1` - - :returns: A list of domain names. - :rtype: list - - """ - return _get_names_from_cert_or_req( - csr, crypto.load_certificate, typ) - - -def dump_pyopenssl_chain(chain, filetype=crypto.FILETYPE_PEM): - """Dump certificate chain into a bundle. - - :param list chain: List of `crypto.X509` (or wrapped in - :class:`josepy.util.ComparableX509`). - - """ - # XXX: returns empty string when no chain is available, which - # shuts up RenewableCert, but might not be the best solution... - return acme_crypto_util.dump_pyopenssl_chain(chain, filetype) - - -def notBefore(cert_path): - """When does the cert at cert_path start being valid? - - :param str cert_path: path to a cert in PEM format - - :returns: the notBefore value from the cert at cert_path - :rtype: :class:`datetime.datetime` - - """ - return _notAfterBefore(cert_path, crypto.X509.get_notBefore) - - -def notAfter(cert_path): - """When does the cert at cert_path stop being valid? - - :param str cert_path: path to a cert in PEM format - - :returns: the notAfter value from the cert at cert_path - :rtype: :class:`datetime.datetime` - - """ - return _notAfterBefore(cert_path, crypto.X509.get_notAfter) - - -def _notAfterBefore(cert_path, method): - """Internal helper function for finding notbefore/notafter. - - :param str cert_path: path to a cert in PEM format - :param function method: one of ``crypto.X509.get_notBefore`` - or ``crypto.X509.get_notAfter`` - - :returns: the notBefore or notAfter value from the cert at cert_path - :rtype: :class:`datetime.datetime` - - """ - # pylint: disable=redefined-outer-name - with open(cert_path) as f: - x509 = crypto.load_certificate(crypto.FILETYPE_PEM, - f.read()) - # pyopenssl always returns bytes - timestamp = method(x509) - reformatted_timestamp = [timestamp[0:4], b"-", timestamp[4:6], b"-", - timestamp[6:8], b"T", timestamp[8:10], b":", - timestamp[10:12], b":", timestamp[12:]] - timestamp_str = b"".join(reformatted_timestamp) - # pyrfc3339 uses "native" strings. That is, bytes on Python 2 and unicode - # on Python 3 - if six.PY3: - timestamp_str = timestamp_str.decode('ascii') - return pyrfc3339.parse(timestamp_str) - - -def sha256sum(filename): - """Compute a sha256sum of a file. - - NB: In given file, platform specific newlines characters will be converted - into their equivalent unicode counterparts before calculating the hash. - - :param str filename: path to the file whose hash will be computed - - :returns: sha256 digest of the file in hexadecimal - :rtype: str - """ - sha256 = hashlib.sha256() - with open(filename, 'r') as file_d: - sha256.update(file_d.read().encode('UTF-8')) - return sha256.hexdigest() - -def cert_and_chain_from_fullchain(fullchain_pem): - """Split fullchain_pem into cert_pem and chain_pem - - :param str fullchain_pem: concatenated cert + chain - - :returns: tuple of string cert_pem and chain_pem - :rtype: tuple - - """ - cert = crypto.dump_certificate(crypto.FILETYPE_PEM, - crypto.load_certificate(crypto.FILETYPE_PEM, fullchain_pem)).decode() - chain = fullchain_pem[len(cert):].lstrip() - return (cert, chain) diff --git a/certbot/display/completer.py b/certbot/display/completer.py deleted file mode 100644 index 509a1051a11..00000000000 --- a/certbot/display/completer.py +++ /dev/null @@ -1,61 +0,0 @@ -"""Provides Tab completion when prompting users for a path.""" -import glob -# readline module is not available on all systems -try: - import readline -except ImportError: - import certbot.display.dummy_readline as readline # type: ignore - - -class Completer(object): - """Provides Tab completion when prompting users for a path. - - This class is meant to be used with readline to provide Tab - completion for users entering paths. The complete method can be - passed to readline.set_completer directly, however, this function - works best as a context manager. For example: - - with Completer(): - raw_input() - - In this example, Tab completion will be available during the call to - raw_input above, however, readline will be restored to its previous - state when exiting the body of the with statement. - - """ - - def __init__(self): - self._iter = self._original_completer = self._original_delims = None - - def complete(self, text, state): - """Provides path completion for use with readline. - - :param str text: text to offer completions for - :param int state: which completion to return - - :returns: possible completion for text or ``None`` if all - completions have been returned - :rtype: str - - """ - if state == 0: - self._iter = glob.iglob(text + '*') - return next(self._iter, None) - - def __enter__(self): - self._original_completer = readline.get_completer() - self._original_delims = readline.get_completer_delims() - - readline.set_completer(self.complete) - readline.set_completer_delims(' \t\n;') - - # readline can be implemented using GNU readline, pyreadline or libedit - # which have different configuration syntax - if readline.__doc__ is not None and 'libedit' in readline.__doc__: - readline.parse_and_bind('bind ^I rl_complete') - else: - readline.parse_and_bind('tab: complete') - - def __exit__(self, unused_type, unused_value, unused_traceback): - readline.set_completer_delims(self._original_delims) - readline.set_completer(self._original_completer) diff --git a/certbot/display/dummy_readline.py b/certbot/display/dummy_readline.py deleted file mode 100644 index fb3d807bb9f..00000000000 --- a/certbot/display/dummy_readline.py +++ /dev/null @@ -1,21 +0,0 @@ -"""A dummy module with no effect for use on systems without readline.""" - - -def get_completer(): - """An empty implementation of readline.get_completer.""" - - -def get_completer_delims(): - """An empty implementation of readline.get_completer_delims.""" - - -def parse_and_bind(unused_command): - """An empty implementation of readline.parse_and_bind.""" - - -def set_completer(unused_function=None): - """An empty implementation of readline.set_completer.""" - - -def set_completer_delims(unused_delims): - """An empty implementation of readline.set_completer_delims.""" diff --git a/certbot/display/enhancements.py b/certbot/display/enhancements.py deleted file mode 100644 index 0f6b6c57d60..00000000000 --- a/certbot/display/enhancements.py +++ /dev/null @@ -1,64 +0,0 @@ -"""Certbot Enhancement Display""" -import logging - -import zope.component - -from certbot import errors -from certbot import interfaces -from certbot.display import util as display_util - - -logger = logging.getLogger(__name__) - -# Define a helper function to avoid verbose code -util = zope.component.getUtility - - -def ask(enhancement): - """Display the enhancement to the user. - - :param str enhancement: One of the - :class:`certbot.CONFIG.ENHANCEMENTS` enhancements - - :returns: True if feature is desired, False otherwise - :rtype: bool - - :raises .errors.Error: if the enhancement provided is not supported - - """ - try: - # Call the appropriate function based on the enhancement - return DISPATCH[enhancement]() - except KeyError: - logger.error("Unsupported enhancement given to ask(): %s", enhancement) - raise errors.Error("Unsupported Enhancement") - - -def redirect_by_default(): - """Determines whether the user would like to redirect to HTTPS. - - :returns: True if redirect is desired, False otherwise - :rtype: bool - - """ - choices = [ - ("No redirect", "Make no further changes to the webserver configuration."), - ("Redirect", "Make all requests redirect to secure HTTPS access. " - "Choose this for new sites, or if you're confident your site works on HTTPS. " - "You can undo this change by editing your web server's configuration."), - ] - - code, selection = util(interfaces.IDisplay).menu( - "Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.", - choices, default=0, - cli_flag="--redirect / --no-redirect", force_interactive=True) - - if code != display_util.OK: - return False - - return selection == 1 - - -DISPATCH = { - "redirect": redirect_by_default -} diff --git a/certbot/display/ops.py b/certbot/display/ops.py deleted file mode 100644 index 3dae1070be1..00000000000 --- a/certbot/display/ops.py +++ /dev/null @@ -1,372 +0,0 @@ -"""Contains UI methods for LE user operations.""" -import logging -import os - -import zope.component - -from certbot import compat -from certbot import errors -from certbot import interfaces -from certbot import util - -from certbot.display import util as display_util - -logger = logging.getLogger(__name__) - -# Define a helper function to avoid verbose code -z_util = zope.component.getUtility - - -def get_email(invalid=False, optional=True): - """Prompt for valid email address. - - :param bool invalid: True if an invalid address was provided by the user - :param bool optional: True if the user can use - --register-unsafely-without-email to avoid providing an e-mail - - :returns: e-mail address - :rtype: str - - :raises errors.Error: if the user cancels - - """ - invalid_prefix = "There seem to be problems with that address. " - msg = "Enter email address (used for urgent renewal and security notices)" - unsafe_suggestion = ("\n\nIf you really want to skip this, you can run " - "the client with --register-unsafely-without-email " - "but make sure you then backup your account key from " - "{0}\n\n".format(os.path.join( - compat.get_default_folder('config'), 'accounts'))) - if optional: - if invalid: - msg += unsafe_suggestion - suggest_unsafe = False - else: - suggest_unsafe = True - else: - suggest_unsafe = False - - while True: - try: - code, email = z_util(interfaces.IDisplay).input( - invalid_prefix + msg if invalid else msg, - force_interactive=True) - except errors.MissingCommandlineFlag: - msg = ("You should register before running non-interactively, " - "or provide --agree-tos and --email flags.") - raise errors.MissingCommandlineFlag(msg) - - if code != display_util.OK: - if optional: - raise errors.Error( - "An e-mail address or " - "--register-unsafely-without-email must be provided.") - else: - raise errors.Error("An e-mail address must be provided.") - elif util.safe_email(email): - return email - elif suggest_unsafe: - msg += unsafe_suggestion - suggest_unsafe = False # add this message at most once - - invalid = bool(email) - - -def choose_account(accounts): - """Choose an account. - - :param list accounts: Containing at least one - :class:`~certbot.account.Account` - - """ - # Note this will get more complicated once we start recording authorizations - labels = [acc.slug for acc in accounts] - - code, index = z_util(interfaces.IDisplay).menu( - "Please choose an account", labels, force_interactive=True) - if code == display_util.OK: - return accounts[index] - else: - return None - -def choose_values(values, question=None): - """Display screen to let user pick one or multiple values from the provided - list. - - :param list values: Values to select from - - :returns: List of selected values - :rtype: list - """ - code, items = z_util(interfaces.IDisplay).checklist( - question, tags=values, force_interactive=True) - if code == display_util.OK and items: - return items - else: - return [] - -def choose_names(installer, question=None): - """Display screen to select domains to validate. - - :param installer: An installer object - :type installer: :class:`certbot.interfaces.IInstaller` - - :param `str` question: Overriding dialog question to ask the user if asked - to choose from domain names. - - :returns: List of selected names - :rtype: `list` of `str` - - """ - if installer is None: - logger.debug("No installer, picking names manually") - return _choose_names_manually() - - domains = list(installer.get_all_names()) - names = get_valid_domains(domains) - - if not names: - return _choose_names_manually( - "No names were found in your configuration files. ") - - code, names = _filter_names(names, question) - if code == display_util.OK and names: - return names - else: - return [] - - -def get_valid_domains(domains): - """Helper method for choose_names that implements basic checks - on domain names - - :param list domains: Domain names to validate - :return: List of valid domains - :rtype: list - """ - valid_domains = [] - for domain in domains: - try: - valid_domains.append(util.enforce_domain_sanity(domain)) - except errors.ConfigurationError: - continue - return valid_domains - -def _sort_names(FQDNs): - """Sort FQDNs by SLD (and if many, by their subdomains) - - :param list FQDNs: list of domain names - - :returns: Sorted list of domain names - :rtype: list - """ - return sorted(FQDNs, key=lambda fqdn: fqdn.split('.')[::-1][1:]) - - -def _filter_names(names, override_question=None): - """Determine which names the user would like to select from a list. - - :param list names: domain names - - :returns: tuple of the form (`code`, `names`) where - `code` - str display exit code - `names` - list of names selected - :rtype: tuple - - """ - #Sort by domain first, and then by subdomain - sorted_names = _sort_names(names) - if override_question: - question = override_question - else: - question = "Which names would you like to activate HTTPS for?" - code, names = z_util(interfaces.IDisplay).checklist( - question, tags=sorted_names, cli_flag="--domains", force_interactive=True) - return code, [str(s) for s in names] - - -def _choose_names_manually(prompt_prefix=""): - """Manually input names for those without an installer. - - :param str prompt_prefix: string to prepend to prompt for domains - - :returns: list of provided names - :rtype: `list` of `str` - - """ - code, input_ = z_util(interfaces.IDisplay).input( - prompt_prefix + - "Please enter in your domain name(s) (comma and/or space separated) ", - cli_flag="--domains", force_interactive=True) - - if code == display_util.OK: - invalid_domains = dict() - retry_message = "" - try: - domain_list = display_util.separate_list_input(input_) - except UnicodeEncodeError: - domain_list = [] - retry_message = ( - "Internationalized domain names are not presently " - "supported.{0}{0}Would you like to re-enter the " - "names?{0}").format(os.linesep) - - for i, domain in enumerate(domain_list): - try: - domain_list[i] = util.enforce_domain_sanity(domain) - except errors.ConfigurationError as e: - invalid_domains[domain] = str(e) - - if len(invalid_domains): - retry_message = ( - "One or more of the entered domain names was not valid:" - "{0}{0}").format(os.linesep) - for domain in invalid_domains: - retry_message = retry_message + "{1}: {2}{0}".format( - os.linesep, domain, invalid_domains[domain]) - retry_message = retry_message + ( - "{0}Would you like to re-enter the names?{0}").format( - os.linesep) - - if retry_message: - # We had error in input - retry = z_util(interfaces.IDisplay).yesno(retry_message, - force_interactive=True) - if retry: - return _choose_names_manually() - else: - return domain_list - return [] - - -def success_installation(domains): - """Display a box confirming the installation of HTTPS. - - :param list domains: domain names which were enabled - - """ - z_util(interfaces.IDisplay).notification( - "Congratulations! You have successfully enabled {0}{1}{1}" - "You should test your configuration at:{1}{2}".format( - _gen_https_names(domains), - os.linesep, - os.linesep.join(_gen_ssl_lab_urls(domains))), - pause=False) - - -def success_renewal(domains): - """Display a box confirming the renewal of an existing certificate. - - :param list domains: domain names which were renewed - - """ - z_util(interfaces.IDisplay).notification( - "Your existing certificate has been successfully renewed, and the " - "new certificate has been installed.{1}{1}" - "The new certificate covers the following domains: {0}{1}{1}" - "You should test your configuration at:{1}{2}".format( - _gen_https_names(domains), - os.linesep, - os.linesep.join(_gen_ssl_lab_urls(domains))), - pause=False) - -def success_revocation(cert_path): - """Display a box confirming a certificate has been revoked. - - :param list cert_path: path to certificate which was revoked. - - """ - z_util(interfaces.IDisplay).notification( - "Congratulations! You have successfully revoked the certificate " - "that was located at {0}{1}{1}".format( - cert_path, - os.linesep), - pause=False) - - -def _gen_ssl_lab_urls(domains): - """Returns a list of urls. - - :param list domains: Each domain is a 'str' - - """ - return ["https://www.ssllabs.com/ssltest/analyze.html?d=%s" % dom for dom in domains] - - -def _gen_https_names(domains): - """Returns a string of the https domains. - - Domains are formatted nicely with https:// prepended to each. - - :param list domains: Each domain is a 'str' - - """ - if len(domains) == 1: - return "https://{0}".format(domains[0]) - elif len(domains) == 2: - return "https://{dom[0]} and https://{dom[1]}".format(dom=domains) - elif len(domains) > 2: - return "{0}{1}{2}".format( - ", ".join("https://%s" % dom for dom in domains[:-1]), - ", and https://", - domains[-1]) - - return "" - - -def _get_validated(method, validator, message, default=None, **kwargs): - if default is not None: - try: - validator(default) - except errors.Error as error: - logger.debug('Encountered invalid default value "%s" when prompting for "%s"', - default, - message, - exc_info=True) - raise AssertionError('Invalid default "{0}"'.format(default)) - - while True: - code, raw = method(message, default=default, **kwargs) - if code == display_util.OK: - try: - validator(raw) - return code, raw - except errors.Error as error: - logger.debug('Validator rejected "%s" when prompting for "%s"', - raw, - message, - exc_info=True) - zope.component.getUtility(interfaces.IDisplay).notification(str(error), pause=False) - else: - return code, raw - - -def validated_input(validator, *args, **kwargs): - """Like `~certbot.interfaces.IDisplay.input`, but with validation. - - :param callable validator: A method which will be called on the - supplied input. If the method raises a `errors.Error`, its - text will be displayed and the user will be re-prompted. - :param list `*args`: Arguments to be passed to `~certbot.interfaces.IDisplay.input`. - :param dict `**kwargs`: Arguments to be passed to `~certbot.interfaces.IDisplay.input`. - :return: as `~certbot.interfaces.IDisplay.input` - :rtype: tuple - """ - return _get_validated(zope.component.getUtility(interfaces.IDisplay).input, - validator, *args, **kwargs) - - -def validated_directory(validator, *args, **kwargs): - """Like `~certbot.interfaces.IDisplay.directory_select`, but with validation. - - :param callable validator: A method which will be called on the - supplied input. If the method raises a `errors.Error`, its - text will be displayed and the user will be re-prompted. - :param list `*args`: Arguments to be passed to `~certbot.interfaces.IDisplay.directory_select`. - :param dict `**kwargs`: Arguments to be passed to - `~certbot.interfaces.IDisplay.directory_select`. - :return: as `~certbot.interfaces.IDisplay.directory_select` - :rtype: tuple - """ - return _get_validated(zope.component.getUtility(interfaces.IDisplay).directory_select, - validator, *args, **kwargs) diff --git a/certbot/display/util.py b/certbot/display/util.py deleted file mode 100644 index 772b67d7492..00000000000 --- a/certbot/display/util.py +++ /dev/null @@ -1,607 +0,0 @@ -"""Certbot display.""" -import logging -import os -import sys -import textwrap - -import zope.interface - -from certbot import compat -from certbot import constants -from certbot import interfaces -from certbot import errors -from certbot.display import completer - -logger = logging.getLogger(__name__) - -WIDTH = 72 - -# Display exit codes -OK = "ok" -"""Display exit code indicating user acceptance.""" - -CANCEL = "cancel" -"""Display exit code for a user canceling the display.""" - -HELP = "help" -"""Display exit code when for when the user requests more help. (UNUSED)""" - -ESC = "esc" -"""Display exit code when the user hits Escape (UNUSED)""" - -# Display constants -SIDE_FRAME = ("- " * 39) + "-" -"""Display boundary (alternates spaces, so when copy-pasted, markdown doesn't interpret -it as a heading)""" - -def _wrap_lines(msg): - """Format lines nicely to 80 chars. - - :param str msg: Original message - - :returns: Formatted message respecting newlines in message - :rtype: str - - """ - lines = msg.splitlines() - fixed_l = [] - - for line in lines: - fixed_l.append(textwrap.fill( - line, - 80, - break_long_words=False, - break_on_hyphens=False)) - - return '\n'.join(fixed_l) - - -def input_with_timeout(prompt=None, timeout=36000.0): - """Get user input with a timeout. - - Behaves the same as six.moves.input, however, an error is raised if - a user doesn't answer after timeout seconds. The default timeout - value was chosen to place it just under 12 hours for users following - our advice and running Certbot twice a day. - - :param str prompt: prompt to provide for input - :param float timeout: maximum number of seconds to wait for input - - :returns: user response - :rtype: str - - :raises errors.Error if no answer is given before the timeout - - """ - # use of sys.stdin and sys.stdout to mimic six.moves.input based on - # https://github.com/python/cpython/blob/baf7bb30a02aabde260143136bdf5b3738a1d409/Lib/getpass.py#L129 - if prompt: - sys.stdout.write(prompt) - sys.stdout.flush() - - line = compat.readline_with_timeout(timeout, prompt) - - if not line: - raise EOFError - return line.rstrip('\n') - - -@zope.interface.implementer(interfaces.IDisplay) -class FileDisplay(object): - """File-based display.""" - # pylint: disable=too-many-arguments - # see https://github.com/certbot/certbot/issues/3915 - - def __init__(self, outfile, force_interactive): - super(FileDisplay, self).__init__() - self.outfile = outfile - self.force_interactive = force_interactive - self.skipped_interaction = False - - def notification(self, message, pause=True, - wrap=True, force_interactive=False): - """Displays a notification and waits for user acceptance. - - :param str message: Message to display - :param bool pause: Whether or not the program should pause for the - user's confirmation - :param bool wrap: Whether or not the application should wrap text - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - """ - if wrap: - message = _wrap_lines(message) - self.outfile.write( - "{line}{frame}{line}{msg}{line}{frame}{line}".format( - line=os.linesep, frame=SIDE_FRAME, msg=message)) - self.outfile.flush() - if pause: - if self._can_interact(force_interactive): - input_with_timeout("Press Enter to Continue") - else: - logger.debug("Not pausing for user confirmation") - - def menu(self, message, choices, ok_label=None, cancel_label=None, - help_label=None, default=None, - cli_flag=None, force_interactive=False, **unused_kwargs): - # pylint: disable=unused-argument - """Display a menu. - - .. todo:: This doesn't enable the help label/button (I wasn't sold on - any interface I came up with for this). It would be a nice feature - - :param str message: title of menu - :param choices: Menu lines, len must be > 0 - :type choices: list of tuples (tag, item) or - list of descriptions (tags will be enumerated) - :param default: default value to return (if one exists) - :param str cli_flag: option used to set this value with the CLI - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: tuple of (`code`, `index`) where - `code` - str display exit code - `index` - int index of the user's selection - - :rtype: tuple - - """ - if self._return_default(message, default, cli_flag, force_interactive): - return OK, default - - self._print_menu(message, choices) - - code, selection = self._get_valid_int_ans(len(choices)) - - return code, selection - 1 - - def input(self, message, default=None, - cli_flag=None, force_interactive=False, **unused_kwargs): - """Accept input from the user. - - :param str message: message to display to the user - :param default: default value to return (if one exists) - :param str cli_flag: option used to set this value with the CLI - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: tuple of (`code`, `input`) where - `code` - str display exit code - `input` - str of the user's input - :rtype: tuple - - """ - if self._return_default(message, default, cli_flag, force_interactive): - return OK, default - - # Trailing space must be added outside of _wrap_lines to be preserved - message = _wrap_lines("%s (Enter 'c' to cancel):" % message) + " " - ans = input_with_timeout(message) - - if ans == "c" or ans == "C": - return CANCEL, "-1" - else: - return OK, ans - - def yesno(self, message, yes_label="Yes", no_label="No", default=None, - cli_flag=None, force_interactive=False, **unused_kwargs): - """Query the user with a yes/no question. - - Yes and No label must begin with different letters, and must contain at - least one letter each. - - :param str message: question for the user - :param str yes_label: Label of the "Yes" parameter - :param str no_label: Label of the "No" parameter - :param default: default value to return (if one exists) - :param str cli_flag: option used to set this value with the CLI - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: True for "Yes", False for "No" - :rtype: bool - - """ - if self._return_default(message, default, cli_flag, force_interactive): - return default - - message = _wrap_lines(message) - - self.outfile.write("{0}{frame}{msg}{0}{frame}".format( - os.linesep, frame=SIDE_FRAME + os.linesep, msg=message)) - self.outfile.flush() - - while True: - ans = input_with_timeout("{yes}/{no}: ".format( - yes=_parens_around_char(yes_label), - no=_parens_around_char(no_label))) - - # Couldn't get pylint indentation right with elif - # elif doesn't matter in this situation - if (ans.startswith(yes_label[0].lower()) or - ans.startswith(yes_label[0].upper())): - return True - if (ans.startswith(no_label[0].lower()) or - ans.startswith(no_label[0].upper())): - return False - - def checklist(self, message, tags, default=None, - cli_flag=None, force_interactive=False, **unused_kwargs): - # pylint: disable=unused-argument - """Display a checklist. - - :param str message: Message to display to user - :param list tags: `str` tags to select, len(tags) > 0 - :param default: default value to return (if one exists) - :param str cli_flag: option used to set this value with the CLI - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: tuple of (`code`, `tags`) where - `code` - str display exit code - `tags` - list of selected tags - :rtype: tuple - - """ - if self._return_default(message, default, cli_flag, force_interactive): - return OK, default - - while True: - self._print_menu(message, tags) - - code, ans = self.input("Select the appropriate numbers separated " - "by commas and/or spaces, or leave input " - "blank to select all options shown", - force_interactive=True) - - if code == OK: - if len(ans.strip()) == 0: - ans = " ".join(str(x) for x in range(1, len(tags)+1)) - indices = separate_list_input(ans) - selected_tags = self._scrub_checklist_input(indices, tags) - if selected_tags: - return code, selected_tags - else: - self.outfile.write( - "** Error - Invalid selection **%s" % os.linesep) - self.outfile.flush() - else: - return code, [] - - def _return_default(self, prompt, default, cli_flag, force_interactive): - """Should we return the default instead of prompting the user? - - :param str prompt: prompt for the user - :param default: default answer to prompt - :param str cli_flag: command line option for setting an answer - to this question - :param bool force_interactive: if interactivity is forced by the - IDisplay call - - :returns: True if we should return the default without prompting - :rtype: bool - - """ - # assert_valid_call(prompt, default, cli_flag, force_interactive) - if self._can_interact(force_interactive): - return False - elif default is None: - msg = "Unable to get an answer for the question:\n{0}".format(prompt) - if cli_flag: - msg += ( - "\nYou can provide an answer on the " - "command line with the {0} flag.".format(cli_flag)) - raise errors.Error(msg) - else: - logger.debug( - "Falling back to default %s for the prompt:\n%s", - default, prompt) - return True - - def _can_interact(self, force_interactive): - """Can we safely interact with the user? - - :param bool force_interactive: if interactivity is forced by the - IDisplay call - - :returns: True if the display can interact with the user - :rtype: bool - - """ - if (self.force_interactive or force_interactive or - sys.stdin.isatty() and self.outfile.isatty()): - return True - elif not self.skipped_interaction: - logger.warning( - "Skipped user interaction because Certbot doesn't appear to " - "be running in a terminal. You should probably include " - "--non-interactive or %s on the command line.", - constants.FORCE_INTERACTIVE_FLAG) - self.skipped_interaction = True - return False - - def directory_select(self, message, default=None, cli_flag=None, - force_interactive=False, **unused_kwargs): - """Display a directory selection screen. - - :param str message: prompt to give the user - :param default: default value to return (if one exists) - :param str cli_flag: option used to set this value with the CLI - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: tuple of the form (`code`, `string`) where - `code` - display exit code - `string` - input entered by the user - - """ - with completer.Completer(): - return self.input(message, default, cli_flag, force_interactive) - - def _scrub_checklist_input(self, indices, tags): - # pylint: disable=no-self-use - """Validate input and transform indices to appropriate tags. - - :param list indices: input - :param list tags: Original tags of the checklist - - :returns: valid tags the user selected - :rtype: :class:`list` of :class:`str` - - """ - # They should all be of type int - try: - indices = [int(index) for index in indices] - except ValueError: - return [] - - # Remove duplicates - indices = list(set(indices)) - - # Check all input is within range - for index in indices: - if index < 1 or index > len(tags): - return [] - # Transform indices to appropriate tags - return [tags[index - 1] for index in indices] - - def _print_menu(self, message, choices): - """Print a menu on the screen. - - :param str message: title of menu - :param choices: Menu lines - :type choices: list of tuples (tag, item) or - list of descriptions (tags will be enumerated) - - """ - # Can take either tuples or single items in choices list - if choices and isinstance(choices[0], tuple): - choices = ["%s - %s" % (c[0], c[1]) for c in choices] - - # Write out the message to the user - self.outfile.write( - "{new}{msg}{new}".format(new=os.linesep, msg=message)) - self.outfile.write(SIDE_FRAME + os.linesep) - - # Write out the menu choices - for i, desc in enumerate(choices, 1): - msg = "{num}: {desc}".format(num=i, desc=desc) - self.outfile.write(_wrap_lines(msg)) - - # Keep this outside of the textwrap - self.outfile.write(os.linesep) - - self.outfile.write(SIDE_FRAME + os.linesep) - self.outfile.flush() - - def _get_valid_int_ans(self, max_): - """Get a numerical selection. - - :param int max: The maximum entry (len of choices), must be positive - - :returns: tuple of the form (`code`, `selection`) where - `code` - str display exit code ('ok' or cancel') - `selection` - int user's selection - :rtype: tuple - - """ - selection = -1 - if max_ > 1: - input_msg = ("Select the appropriate number " - "[1-{max_}] then [enter] (press 'c' to " - "cancel): ".format(max_=max_)) - else: - input_msg = ("Press 1 [enter] to confirm the selection " - "(press 'c' to cancel): ") - while selection < 1: - ans = input_with_timeout(input_msg) - if ans.startswith("c") or ans.startswith("C"): - return CANCEL, -1 - try: - selection = int(ans) - if selection < 1 or selection > max_: - selection = -1 - raise ValueError - - except ValueError: - self.outfile.write( - "{0}** Invalid input **{0}".format(os.linesep)) - self.outfile.flush() - - return OK, selection - - -def assert_valid_call(prompt, default, cli_flag, force_interactive): - """Verify that provided arguments is a valid IDisplay call. - - :param str prompt: prompt for the user - :param default: default answer to prompt - :param str cli_flag: command line option for setting an answer - to this question - :param bool force_interactive: if interactivity is forced by the - IDisplay call - - """ - msg = "Invalid IDisplay call for this prompt:\n{0}".format(prompt) - if cli_flag: - msg += ("\nYou can set an answer to " - "this prompt with the {0} flag".format(cli_flag)) - assert default is not None or force_interactive, msg - - -@zope.interface.implementer(interfaces.IDisplay) -class NoninteractiveDisplay(object): - """An iDisplay implementation that never asks for interactive user input""" - - def __init__(self, outfile, *unused_args, **unused_kwargs): - super(NoninteractiveDisplay, self).__init__() - self.outfile = outfile - - def _interaction_fail(self, message, cli_flag, extra=""): - "Error out in case of an attempt to interact in noninteractive mode" - msg = "Missing command line flag or config entry for this setting:\n" - msg += message - if extra: - msg += "\n" + extra - if cli_flag: - msg += "\n\n(You can set this with the {0} flag)".format(cli_flag) - raise errors.MissingCommandlineFlag(msg) - - def notification(self, message, pause=False, wrap=True, **unused_kwargs): - # pylint: disable=unused-argument - """Displays a notification without waiting for user acceptance. - - :param str message: Message to display to stdout - :param bool pause: The NoninteractiveDisplay waits for no keyboard - :param bool wrap: Whether or not the application should wrap text - - """ - if wrap: - message = _wrap_lines(message) - self.outfile.write( - "{line}{frame}{line}{msg}{line}{frame}{line}".format( - line=os.linesep, frame=SIDE_FRAME, msg=message)) - self.outfile.flush() - - def menu(self, message, choices, ok_label=None, cancel_label=None, - help_label=None, default=None, cli_flag=None, **unused_kwargs): - # pylint: disable=unused-argument,too-many-arguments - """Avoid displaying a menu. - - :param str message: title of menu - :param choices: Menu lines, len must be > 0 - :type choices: list of tuples (tag, item) or - list of descriptions (tags will be enumerated) - :param int default: the default choice - :param dict kwargs: absorbs various irrelevant labelling arguments - - :returns: tuple of (`code`, `index`) where - `code` - str display exit code - `index` - int index of the user's selection - :rtype: tuple - :raises errors.MissingCommandlineFlag: if there was no default - - """ - if default is None: - self._interaction_fail(message, cli_flag, "Choices: " + repr(choices)) - - return OK, default - - def input(self, message, default=None, cli_flag=None, **unused_kwargs): - """Accept input from the user. - - :param str message: message to display to the user - - :returns: tuple of (`code`, `input`) where - `code` - str display exit code - `input` - str of the user's input - :rtype: tuple - :raises errors.MissingCommandlineFlag: if there was no default - - """ - if default is None: - self._interaction_fail(message, cli_flag) - else: - return OK, default - - def yesno(self, message, yes_label=None, no_label=None, - default=None, cli_flag=None, **unused_kwargs): - # pylint: disable=unused-argument - """Decide Yes or No, without asking anybody - - :param str message: question for the user - :param dict kwargs: absorbs yes_label, no_label - - :raises errors.MissingCommandlineFlag: if there was no default - :returns: True for "Yes", False for "No" - :rtype: bool - - """ - if default is None: - self._interaction_fail(message, cli_flag) - else: - return default - - def checklist(self, message, tags, default=None, - cli_flag=None, **unused_kwargs): - """Display a checklist. - - :param str message: Message to display to user - :param list tags: `str` tags to select, len(tags) > 0 - :param dict kwargs: absorbs default_status arg - - :returns: tuple of (`code`, `tags`) where - `code` - str display exit code - `tags` - list of selected tags - :rtype: tuple - - """ - if default is None: - self._interaction_fail(message, cli_flag, "? ".join(tags)) - else: - return OK, default - - def directory_select(self, message, default=None, - cli_flag=None, **unused_kwargs): - """Simulate prompting the user for a directory. - - This function returns default if it is not ``None``, otherwise, - an exception is raised explaining the problem. If cli_flag is - not ``None``, the error message will include the flag that can - be used to set this value with the CLI. - - :param str message: prompt to give the user - :param default: default value to return (if one exists) - :param str cli_flag: option used to set this value with the CLI - - :returns: tuple of the form (`code`, `string`) where - `code` - int display exit code - `string` - input entered by the user - - """ - return self.input(message, default, cli_flag) - - -def separate_list_input(input_): - """Separate a comma or space separated list. - - :param str input_: input from the user - - :returns: strings - :rtype: list - - """ - no_commas = input_.replace(",", " ") - # Each string is naturally unicode, this causes problems with M2Crypto SANs - # TODO: check if above is still true when M2Crypto is gone ^ - return [str(string) for string in no_commas.split()] - - -def _parens_around_char(label): - """Place parens around first character of label. - - :param str label: Must contain at least one character - - """ - return "({first}){rest}".format(first=label[0], rest=label[1:]) diff --git a/certbot-apache/docs/.gitignore b/certbot/docs/.gitignore similarity index 100% rename from certbot-apache/docs/.gitignore rename to certbot/docs/.gitignore diff --git a/certbot/docs/Makefile b/certbot/docs/Makefile new file mode 100644 index 00000000000..be4a93c18a8 --- /dev/null +++ b/certbot/docs/Makefile @@ -0,0 +1,183 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# User-friendly check for sphinx-build +ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from https://www.sphinx-doc.org/) +endif + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + @echo " coverage to run coverage check of the documentation (if enabled)" + +clean: + rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/LetsEncrypt.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/LetsEncrypt.qhc" + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/LetsEncrypt" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/LetsEncrypt" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +coverage: + $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage + @echo "Testing of coverage in the sources finished, look at the " \ + "results in $(BUILDDIR)/coverage/python.txt." + +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/certbot/docs/_static/.gitignore b/certbot/docs/_static/.gitignore new file mode 100644 index 00000000000..e69de29bb2d diff --git a/docs/_templates/footer.html b/certbot/docs/_templates/footer.html similarity index 88% rename from docs/_templates/footer.html rename to certbot/docs/_templates/footer.html index 8fd0f127de7..48be1294dea 100644 --- a/docs/_templates/footer.html +++ b/certbot/docs/_templates/footer.html @@ -44,7 +44,7 @@ {%- if show_sphinx %} - {% trans %}Built with Sphinx using a theme provided by Read the Docs{% endtrans %}. + {% trans %}Built with Sphinx using a theme provided by Read the Docs{% endtrans %}. {%- endif %} {%- block extrafooter %} {% endblock %} diff --git a/certbot/docs/api.rst b/certbot/docs/api.rst new file mode 100644 index 00000000000..9c8b2f1fe7f --- /dev/null +++ b/certbot/docs/api.rst @@ -0,0 +1,8 @@ +================= +API Documentation +================= + +.. toctree:: + :maxdepth: 4 + + api/certbot diff --git a/certbot/docs/api/certbot.achallenges.rst b/certbot/docs/api/certbot.achallenges.rst new file mode 100644 index 00000000000..3fd2f2a42fb --- /dev/null +++ b/certbot/docs/api/certbot.achallenges.rst @@ -0,0 +1,7 @@ +certbot.achallenges module +========================== + +.. automodule:: certbot.achallenges + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.compat.filesystem.rst b/certbot/docs/api/certbot.compat.filesystem.rst new file mode 100644 index 00000000000..d4f1e2fe06c --- /dev/null +++ b/certbot/docs/api/certbot.compat.filesystem.rst @@ -0,0 +1,7 @@ +certbot.compat.filesystem module +================================ + +.. automodule:: certbot.compat.filesystem + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.compat.misc.rst b/certbot/docs/api/certbot.compat.misc.rst new file mode 100644 index 00000000000..35c2913e753 --- /dev/null +++ b/certbot/docs/api/certbot.compat.misc.rst @@ -0,0 +1,7 @@ +certbot.compat.misc module +========================== + +.. automodule:: certbot.compat.misc + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.compat.os.rst b/certbot/docs/api/certbot.compat.os.rst new file mode 100644 index 00000000000..c46cee66817 --- /dev/null +++ b/certbot/docs/api/certbot.compat.os.rst @@ -0,0 +1,5 @@ +certbot.compat.os module +======================== + +.. automodule:: certbot.compat.os + :members: chmod, umask, chown, open, mkdir, makedirs, rename, replace, access, stat, fstat diff --git a/certbot/docs/api/certbot.compat.rst b/certbot/docs/api/certbot.compat.rst new file mode 100644 index 00000000000..f6f2b3739d3 --- /dev/null +++ b/certbot/docs/api/certbot.compat.rst @@ -0,0 +1,17 @@ +certbot.compat package +====================== + +.. automodule:: certbot.compat + :members: + :undoc-members: + :show-inheritance: + +Submodules +---------- + +.. toctree:: + + certbot.compat.filesystem + certbot.compat.misc + certbot.compat.os + diff --git a/certbot/docs/api/certbot.crypto_util.rst b/certbot/docs/api/certbot.crypto_util.rst new file mode 100644 index 00000000000..34aa665b974 --- /dev/null +++ b/certbot/docs/api/certbot.crypto_util.rst @@ -0,0 +1,7 @@ +certbot.crypto\_util module +=========================== + +.. automodule:: certbot.crypto_util + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.display.ops.rst b/certbot/docs/api/certbot.display.ops.rst new file mode 100644 index 00000000000..544b0dad328 --- /dev/null +++ b/certbot/docs/api/certbot.display.ops.rst @@ -0,0 +1,7 @@ +certbot.display.ops module +========================== + +.. automodule:: certbot.display.ops + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.display.rst b/certbot/docs/api/certbot.display.rst new file mode 100644 index 00000000000..04bc68b07a6 --- /dev/null +++ b/certbot/docs/api/certbot.display.rst @@ -0,0 +1,16 @@ +certbot.display package +======================= + +.. automodule:: certbot.display + :members: + :undoc-members: + :show-inheritance: + +Submodules +---------- + +.. toctree:: + + certbot.display.ops + certbot.display.util + diff --git a/certbot/docs/api/certbot.display.util.rst b/certbot/docs/api/certbot.display.util.rst new file mode 100644 index 00000000000..22b59dc9812 --- /dev/null +++ b/certbot/docs/api/certbot.display.util.rst @@ -0,0 +1,7 @@ +certbot.display.util module +=========================== + +.. automodule:: certbot.display.util + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.errors.rst b/certbot/docs/api/certbot.errors.rst new file mode 100644 index 00000000000..731b7695d15 --- /dev/null +++ b/certbot/docs/api/certbot.errors.rst @@ -0,0 +1,7 @@ +certbot.errors module +===================== + +.. automodule:: certbot.errors + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.interfaces.rst b/certbot/docs/api/certbot.interfaces.rst new file mode 100644 index 00000000000..2665aaa0154 --- /dev/null +++ b/certbot/docs/api/certbot.interfaces.rst @@ -0,0 +1,7 @@ +certbot.interfaces module +========================= + +.. automodule:: certbot.interfaces + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.main.rst b/certbot/docs/api/certbot.main.rst new file mode 100644 index 00000000000..ce0539f5c84 --- /dev/null +++ b/certbot/docs/api/certbot.main.rst @@ -0,0 +1,7 @@ +certbot.main module +=================== + +.. automodule:: certbot.main + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.plugins.common.rst b/certbot/docs/api/certbot.plugins.common.rst new file mode 100644 index 00000000000..e94b2d12e7a --- /dev/null +++ b/certbot/docs/api/certbot.plugins.common.rst @@ -0,0 +1,7 @@ +certbot.plugins.common module +============================= + +.. automodule:: certbot.plugins.common + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.plugins.dns_common.rst b/certbot/docs/api/certbot.plugins.dns_common.rst new file mode 100644 index 00000000000..36c7a642806 --- /dev/null +++ b/certbot/docs/api/certbot.plugins.dns_common.rst @@ -0,0 +1,7 @@ +certbot.plugins.dns\_common module +================================== + +.. automodule:: certbot.plugins.dns_common + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.plugins.dns_common_lexicon.rst b/certbot/docs/api/certbot.plugins.dns_common_lexicon.rst new file mode 100644 index 00000000000..1a961accd6b --- /dev/null +++ b/certbot/docs/api/certbot.plugins.dns_common_lexicon.rst @@ -0,0 +1,7 @@ +certbot.plugins.dns\_common\_lexicon module +=========================================== + +.. automodule:: certbot.plugins.dns_common_lexicon + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.plugins.dns_test_common.rst b/certbot/docs/api/certbot.plugins.dns_test_common.rst new file mode 100644 index 00000000000..69e672f0a5c --- /dev/null +++ b/certbot/docs/api/certbot.plugins.dns_test_common.rst @@ -0,0 +1,7 @@ +certbot.plugins.dns\_test\_common module +======================================== + +.. automodule:: certbot.plugins.dns_test_common + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.plugins.dns_test_common_lexicon.rst b/certbot/docs/api/certbot.plugins.dns_test_common_lexicon.rst new file mode 100644 index 00000000000..92d516c99f9 --- /dev/null +++ b/certbot/docs/api/certbot.plugins.dns_test_common_lexicon.rst @@ -0,0 +1,7 @@ +certbot.plugins.dns\_test\_common\_lexicon module +================================================= + +.. automodule:: certbot.plugins.dns_test_common_lexicon + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.plugins.enhancements.rst b/certbot/docs/api/certbot.plugins.enhancements.rst new file mode 100644 index 00000000000..16db737c738 --- /dev/null +++ b/certbot/docs/api/certbot.plugins.enhancements.rst @@ -0,0 +1,7 @@ +certbot.plugins.enhancements module +=================================== + +.. automodule:: certbot.plugins.enhancements + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.plugins.rst b/certbot/docs/api/certbot.plugins.rst new file mode 100644 index 00000000000..517a209e60e --- /dev/null +++ b/certbot/docs/api/certbot.plugins.rst @@ -0,0 +1,22 @@ +certbot.plugins package +======================= + +.. automodule:: certbot.plugins + :members: + :undoc-members: + :show-inheritance: + +Submodules +---------- + +.. toctree:: + + certbot.plugins.common + certbot.plugins.dns_common + certbot.plugins.dns_common_lexicon + certbot.plugins.dns_test_common + certbot.plugins.dns_test_common_lexicon + certbot.plugins.enhancements + certbot.plugins.storage + certbot.plugins.util + diff --git a/certbot/docs/api/certbot.plugins.storage.rst b/certbot/docs/api/certbot.plugins.storage.rst new file mode 100644 index 00000000000..9ed0fe724d8 --- /dev/null +++ b/certbot/docs/api/certbot.plugins.storage.rst @@ -0,0 +1,7 @@ +certbot.plugins.storage module +============================== + +.. automodule:: certbot.plugins.storage + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.plugins.util.rst b/certbot/docs/api/certbot.plugins.util.rst new file mode 100644 index 00000000000..c5453564e70 --- /dev/null +++ b/certbot/docs/api/certbot.plugins.util.rst @@ -0,0 +1,7 @@ +certbot.plugins.util module +=========================== + +.. automodule:: certbot.plugins.util + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.reverter.rst b/certbot/docs/api/certbot.reverter.rst new file mode 100644 index 00000000000..002b753600a --- /dev/null +++ b/certbot/docs/api/certbot.reverter.rst @@ -0,0 +1,7 @@ +certbot.reverter module +======================= + +.. automodule:: certbot.reverter + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.rst b/certbot/docs/api/certbot.rst new file mode 100644 index 00000000000..6f5b4b40379 --- /dev/null +++ b/certbot/docs/api/certbot.rst @@ -0,0 +1,31 @@ +certbot package +=============== + +.. automodule:: certbot + :members: + :undoc-members: + :show-inheritance: + +Subpackages +----------- + +.. toctree:: + + certbot.compat + certbot.display + certbot.plugins + certbot.tests + +Submodules +---------- + +.. toctree:: + + certbot.achallenges + certbot.crypto_util + certbot.errors + certbot.interfaces + certbot.main + certbot.reverter + certbot.util + diff --git a/certbot/docs/api/certbot.tests.acme_util.rst b/certbot/docs/api/certbot.tests.acme_util.rst new file mode 100644 index 00000000000..9083975967c --- /dev/null +++ b/certbot/docs/api/certbot.tests.acme_util.rst @@ -0,0 +1,7 @@ +certbot.tests.acme\_util module +=============================== + +.. automodule:: certbot.tests.acme_util + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.tests.rst b/certbot/docs/api/certbot.tests.rst new file mode 100644 index 00000000000..336f0eabce5 --- /dev/null +++ b/certbot/docs/api/certbot.tests.rst @@ -0,0 +1,16 @@ +certbot.tests package +===================== + +.. automodule:: certbot.tests + :members: + :undoc-members: + :show-inheritance: + +Submodules +---------- + +.. toctree:: + + certbot.tests.acme_util + certbot.tests.util + diff --git a/certbot/docs/api/certbot.tests.util.rst b/certbot/docs/api/certbot.tests.util.rst new file mode 100644 index 00000000000..3f03358499d --- /dev/null +++ b/certbot/docs/api/certbot.tests.util.rst @@ -0,0 +1,7 @@ +certbot.tests.util module +========================= + +.. automodule:: certbot.tests.util + :members: + :undoc-members: + :show-inheritance: diff --git a/certbot/docs/api/certbot.util.rst b/certbot/docs/api/certbot.util.rst new file mode 100644 index 00000000000..11cb33b092c --- /dev/null +++ b/certbot/docs/api/certbot.util.rst @@ -0,0 +1,7 @@ +certbot.util module +=================== + +.. automodule:: certbot.util + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/challenges.rst b/certbot/docs/challenges.rst similarity index 87% rename from docs/challenges.rst rename to certbot/docs/challenges.rst index ee8bb8e6134..0d026242db8 100644 --- a/docs/challenges.rst +++ b/certbot/docs/challenges.rst @@ -1,16 +1,16 @@ Challenges ========== -To receive a certificate from Let's Encrypt certificate authority (CA), you must pass a *challenge* to -prove you control each of the domain names that will be listed in the certificate. A challenge is one of +To receive a certificate from Let's Encrypt certificate authority (CA), you must pass a *challenge* to +prove you control each of the domain names that will be listed in the certificate. A challenge is one of a list of specified tasks that only someone who controls the domain should be able to accomplish, such as: * Posting a specified file in a specified location on a web site (the HTTP-01 challenge) * Posting a specified DNS record in the domain name system (the DNS-01 challenge) -It’s possible to complete each type of challenge *automatically* (Certbot directly makes the necessary -changes itself, or runs another program that does so), or *manually* (Certbot tells you to make a -certain change, and you edit a configuration file of some kind in order to accomplish it). Certbot's +It’s possible to complete each type of challenge *automatically* (Certbot directly makes the necessary +changes itself, or runs another program that does so), or *manually* (Certbot tells you to make a +certain change, and you edit a configuration file of some kind in order to accomplish it). Certbot's design favors performing challenges automatically, and this is the normal case for most users of Certbot. Some plugins offer an *authenticator*, meaning that they can satisfy challenges: @@ -22,29 +22,29 @@ Some plugins offer an *authenticator*, meaning that they can satisfy challenges: satisfy challenges from the certificate authority. Use the nginx plugin when you're running Certbot on a web server with nginx listening on port 80. * Webroot plugin: (HTTP-01) Tries to place a file where it can be served over HTTP on port 80 by a - web server running on your system. Use the Webroot plugin when you're running Certbot on + web server running on your system. Use the Webroot plugin when you're running Certbot on a web server with any server application listening on port 80 serving files from a folder on disk in response. * Standalone plugin: (HTTP-01) Tries to run a temporary web server listening on HTTP on port 80. Use the Standalone plugin if no existing program is listening to this port. -* Manual plugin: (DNS-01 or HTTP-01) Either tells you what changes to make to your configuration or updates - your DNS records using an external script (for DNS-01) or your webroot (for HTTP-01). Use the Manual +* Manual plugin: (DNS-01 or HTTP-01) Either tells you what changes to make to your configuration or updates + your DNS records using an external script (for DNS-01) or your webroot (for HTTP-01). Use the Manual plugin if you have the technical knowledge to make configuration changes yourself when asked to do so, - and are prepared to repeat these steps every time the certificate needs to be renewed. + and are prepared to repeat these steps every time the certificate needs to be renewed. Tips for Challenges ------------------- General tips: * Run Certbot on your web server, not on your laptop or another server. It’s usually the easiest way to get a certificate. -* Use a tool like the DNSchecker at dnsstuff.com to check your DNS records to make sure - there are no serious errors. A DNS error can prevent a certificate authority from +* Use a tool like the DNSchecker at dnsstuff.com to check your DNS records to make sure + there are no serious errors. A DNS error can prevent a certificate authority from issuing a certificate, even if it does not prevent your site from loading in a browser. * If you are using Apache or NGINX plugins, make sure the configuration of your Apache or NGINX server is correct. HTTP-01 Challenge ~~~~~~~~~~~~~~~~~ -* Make sure the domain name exists and is already pointed to the public IP address of the server where +* Make sure the domain name exists and is already pointed to the public IP address of the server where you’re requesting the certificate. * Make sure port 80 is open, publicly reachable from the Internet, and not blocked by a router or firewall. * When using the Webroot plugin or the manual plugin, make sure the the webroot directory exists and that you @@ -52,12 +52,12 @@ HTTP-01 Challenge then a file placed in `/var/www/example.com/.well-known/acme-challenge/testfile` should appear on your web site at `http://example.com/.well-known/acme-challenge/testfile` (A redirection to HTTPS is OK here and should not stop the challenge from working.) -* In some web server configurations, all pages are dynamically generated by some kind of framework, - usually using a database backend. In this case, there might not be a particular directory - from which the web server can serve filesdirectly. Using the Webroot plugin in this case +* In some web server configurations, all pages are dynamically generated by some kind of framework, + usually using a database backend. In this case, there might not be a particular directory + from which the web server can serve filesdirectly. Using the Webroot plugin in this case requires making a change to your web server configuration first. -* Make sure your web server serves files properly from the directory where the challenge - file is placed (e. g. `/.well-known/acme-challenge`) to the expected location on the +* Make sure your web server serves files properly from the directory where the challenge + file is placed (e. g. `/.well-known/acme-challenge`) to the expected location on the website without adding a header or footer. * When using the Standalone plugin, make sure another program is not already listening to port 80 on the server. * When using the Webroot plugin, make sure there is a web server listening on port 80. @@ -65,6 +65,6 @@ HTTP-01 Challenge DNS-01 Challenge ~~~~~~~~~~~~~~~~ -* When using the manual plugin, make sure your DNS records are correctly updated; +* When using the manual plugin, make sure your DNS records are correctly updated; you must be able to make appropriate changes to your DNS zone in order to pass the challenge. diff --git a/docs/ciphers.rst b/certbot/docs/ciphers.rst similarity index 81% rename from docs/ciphers.rst rename to certbot/docs/ciphers.rst index 1b320cdf91a..883d62090e6 100644 --- a/docs/ciphers.rst +++ b/certbot/docs/ciphers.rst @@ -1,3 +1,11 @@ +.. + Sphinx complains that this file isn't included in any toctree, however, we + currently link to it in the section about installing Certbot through Docker. + Setting :orphan: below suppresses this warning. See + https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html#special-metadata-fields. + +:orphan: + ============ Ciphersuites ============ @@ -80,8 +88,8 @@ its preferences in accordance with its own policy or its administrators' preferences, and use different cryptographic mechanisms or parameters, or a different priority order, than the defaults provided by Certbot. -If you don't use Certbot to configure your server directly, because the -client doesn't integrate with your server software or because you chose +If you don't use Certbot to configure your server directly, because the +client doesn't integrate with your server software or because you chose not to use this integration, then the cryptographic defaults haven't been modified, and the cryptography chosen by the server will still be whatever the default for your software was. For example, if you obtain a @@ -110,13 +118,9 @@ to most-backwards compatible). The client will follow the Mozilla defaults for the *Intermediate* configuration by default, at least with regards to ciphersuites and TLS versions. Mozilla's web site describes which client software will be compatible with each configuration. You can also use -the Qualys SSL Labs site, which Certbot will suggest -when installing a certificate, to test your server and see whether it +the Qualys SSL Labs site to test your server and see whether it will be compatible with particular software versions. -It will be possible to ask Certbot to instead apply (and track) Modern -or Old configurations. - The Let's Encrypt project expects to follow the Mozilla recommendations in the future as those recommendations are updated. (For example, some users have proposed prioritizing a new ciphersuite known as ``0xcc13`` @@ -159,51 +163,6 @@ welcome suggestions of other resources to add to this list. Please keep in mind that different recommendations may reflect different priorities or evaluations of trade-offs, especially related to compatibility! - -Changing your settings ----------------------- - -This will probably look something like - -.. code-block:: shell - - certbot --cipher-recommendations mozilla-secure - certbot --cipher-recommendations mozilla-intermediate - certbot --cipher-recommendations mozilla-old - -to track Mozilla's *Secure*, *Intermediate*, or *Old* recommendations, -and - -.. code-block:: shell - - certbot --update-ciphers on - -to enable updating ciphers with each new Certbot release, or - -.. code-block:: shell - - certbot --update-ciphers off - -to disable automatic configuration updates. These features have not yet -been implemented and this syntax may change when they are implemented. - - -TODO ----- - -The status of this feature is tracked as part of issue #1123 in our -bug tracker. - -https://github.com/certbot/certbot/issues/1123 - -Prior to implementation of #1123, the client does not actually modify -ciphersuites (this is intended to be implemented as a "configuration -enhancement", but the only configuration enhancement implemented -so far is redirecting HTTP requests to HTTPS in web servers, the -"redirect" enhancement). The changes here would probably be either a new -"ciphersuite" enhancement in each plugin that provides an installer, -or a family of enhancements, one per selectable ciphersuite configuration. - Feedback ======== We receive lots of feedback on the type of ciphersuites that Let's Encrypt supports and list some collated feedback below. This section aims to track suggestions and references that people have offered or identified to improve the ciphersuites that Let's Encrypt enables when configuring TLS on servers. @@ -227,28 +186,33 @@ BetterCrypto.org BetterCrypto.org, a collaboration of mostly European IT security experts, has published a draft paper, "Applied Crypto Hardening" -https://bettercrypto.org/static/applied-crypto-hardening.pdf +https://bettercrypto.org/ -FF-DHE Internet-Draft -~~~~~~~~~~~~~~~~~~~~~ +RFC 7919 +~~~~~~~~ -Gillmor's Internet-Draft "Negotiated Discrete Log Diffie-Hellman Ephemeral Parameters for TLS" is being developed at the IETF TLS WG. It advocates using *standardized* DH groups in all cases, not individually-chosen ones (mostly because of the Triple Handshake attack which can involve maliciously choosing invalid DH groups). The draft provides a list of recommended groups, with primes beginning at 2048 bits and going up from there. It also has a new protocol mechanism for agreeing to use these groups, with the possibility of backwards compatibility (and use of weaker DH groups) for older clients and servers that don't know about this mechanism. +IETF has published a document, RFC 7919, "Negotiated Discrete Log Diffie-Hellman Ephemeral Parameters for TLS". +It advocates using *standardized* DH groups in all cases, not individually-chosen ones (mostly because of the Triple +Handshake attack which can involve maliciously choosing invalid DH groups). The RFC provides a list of recommended +groups, with primes beginning at 2048 bits and going up from there. It also has a new protocol mechanism for agreeing +to use these groups, with the possibility of backwards compatibility (and use of weaker DH groups) for older clients +and servers that don't know about this mechanism. -https://tools.ietf.org/html/draft-ietf-tls-negotiated-ff-dhe-10 +https://datatracker.ietf.org/doc/html/rfc7919 Mozilla ~~~~~~~ Mozilla's general server configuration guidance is available at https://wiki.mozilla.org/Security/Server_Side_TLS -Mozilla has also produced a configuration generator: https://mozilla.github.io/server-side-tls/ssl-config-generator/ +Mozilla has also produced a configuration generator: https://ssl-config.mozilla.org Dutch National Cyber Security Centre ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The Dutch National Cyber Security Centre has published guidance on "ICT-beveiligingsrichtlijnen voor Transport Layer Security (TLS)" ("IT Security Guidelines for Transport Layer Security (TLS)"). These are available only in Dutch at -https://www.ncsc.nl/dienstverlening/expertise-advies/kennisdeling/whitepapers/ict-beveiligingsrichtlijnen-voor-transport-layer-security-tls.html +https://web.archive.org/web/20190516085116/https://www.ncsc.nl/actueel/whitepapers/ict-beveiligingsrichtlijnen-voor-transport-layer-security-tls.html I have access to an English-language summary of the recommendations. @@ -257,17 +221,17 @@ Keylength.com Damien Giry collects recommendations by academic researchers and standards organizations about keylengths for particular cryptoperiods, years, or security levels. The keylength recommendations of the various sources are summarized in a chart. This site has been updated over time and includes expert guidance from eight sources published between 2000 and 2017. -http://www.keylength.com/ +https://www.keylength.com/ NIST ~~~~ -NISA published its "NIST Special Publication 800-52 Revision 1: Guidelines for the Selection, Configuration, and Use of Transport Layer Security (TLS) Implementations" +NIST published its "NIST Special Publication 800-52 Revision 2: Guidelines for the Selection, Configuration, and Use of Transport Layer Security (TLS) Implementations" -http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-52r1.pdf +https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-52r2.pdf -and its "NIST Special Publication 800-57: Recommendation for Key Management – Part 1: General (Revision 3)" +and its "NIST Special Publication 800-57: Recommendation for Key Management – Part 1: General (Revision 5)" -http://csrc.nist.gov/publications/nistpubs/800-57/sp800-57_part1_rev3_general.pdf +https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r5.pdf ENISA ~~~~~ @@ -286,7 +250,7 @@ https://weakdh.org/sysadmin.html These lists may have been derived from Mozilla's recommendations. One of the authors clarified his view of the priorities for various changes as a result of the research at -https://www.ietf.org/mail-archive/web/tls/current/msg16496.html +https://web.archive.org/web/20150526022820/https://www.ietf.org/mail-archive/web/tls/current/msg16496.html In particular, he supports ECDHE and also supports the use of the standardized groups in the FF-DHE Internet-Draft mentioned above (which isn't clear from the group's original recommendations). @@ -303,7 +267,7 @@ https://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elb-secur U.S. Government 18F ~~~~~~~~~~~~~~~~~~~ -The 18F site (https://18f.gsa.gov/) is using +The 18F site (https://18f.gsa.gov/) is using :: diff --git a/certbot/docs/cli-help.txt b/certbot/docs/cli-help.txt new file mode 100644 index 00000000000..bc55950d8e2 --- /dev/null +++ b/certbot/docs/cli-help.txt @@ -0,0 +1,776 @@ +usage: + certbot [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ... + +Certbot can obtain and install HTTPS/TLS/SSL certificates. By default, +it will attempt to use a webserver both for obtaining and installing the +certificate. The most common SUBCOMMANDS and flags are: + +obtain, install, and renew certificates: + (default) run Obtain & install a certificate in your current webserver + certonly Obtain or renew a certificate, but do not install it + renew Renew all previously obtained certificates that are near expiry + enhance Add security enhancements to your existing configuration + -d DOMAINS Comma-separated list of domains to obtain a certificate for + + --apache Use the Apache plugin for authentication & installation + --standalone Run a standalone webserver for authentication + --nginx Use the Nginx plugin for authentication & installation + --webroot Place files in a server's webroot folder for authentication + --manual Obtain certificates interactively, or using shell script hooks + + -n Run non-interactively + --test-cert Obtain a test certificate from a staging server + --dry-run Test "renew" or "certonly" without saving any certificates to disk + +manage certificates: + certificates Display information about certificates you have from Certbot + revoke Revoke a certificate (supply --cert-name or --cert-path) + delete Delete a certificate (supply --cert-name) + reconfigure Update a certificate's configuration (supply --cert-name) + +manage your account: + register Create an ACME account + unregister Deactivate an ACME account + update_account Update an ACME account + show_account Display account details + --agree-tos Agree to the ACME server's Subscriber Agreement + -m EMAIL Email address for important account notifications + +options: + -h, --help show this help message and exit + -c CONFIG_FILE, --config CONFIG_FILE + path to config file (default: /etc/letsencrypt/cli.ini + and ~/.config/letsencrypt/cli.ini) + -v, --verbose This flag can be used multiple times to incrementally + increase the verbosity of output, e.g. -vvv. (default: + 0) + --max-log-backups MAX_LOG_BACKUPS + Specifies the maximum number of backup logs that + should be kept by Certbot's built in log rotation. + Setting this flag to 0 disables log rotation entirely, + causing Certbot to always append to the same log file. + (default: 1000) + -n, --non-interactive, --noninteractive + Run without ever asking for user input. This may + require additional command line flags; the client will + try to explain which ones are required if it finds one + missing (default: False) + --force-interactive Force Certbot to be interactive even if it detects + it's not being run in a terminal. This flag cannot be + used with the renew subcommand. (default: False) + -d DOMAIN, --domains DOMAIN, --domain DOMAIN + Domain names to include. For multiple domains you can + use multiple -d flags or enter a comma separated list + of domains as a parameter. All domains will be + included as Subject Alternative Names on the + certificate. The first domain will be used as the + certificate name, unless otherwise specified or if you + already have a certificate with the same name. In the + case of a name conflict, a number like -0001 will be + appended to the certificate name. (default: Ask) + --ip-address IP_ADDRESSES + IP addresses to include. For multiple IP addresses you + can use multiple --ip-address flags. All IP addresses + will be included as Subject Alternative Names on the + certificate. (default: []) + --eab-kid EAB_KID Key Identifier for External Account Binding (default: + None) + --eab-hmac-key EAB_HMAC_KEY + HMAC key for External Account Binding (default: None) + --eab-hmac-alg EAB_HMAC_ALG + HMAC algorithm for External Account Binding (default: + HS256) + --cert-name CERTNAME Certificate name to apply. This name is used by + Certbot for housekeeping and in file paths; it doesn't + affect the content of the certificate itself. + Certificate name cannot contain filepath separators + (i.e. '/' or '\', depending on the platform). To see + certificate names, run 'certbot certificates'. When + creating a new certificate, specifies the new + certificate's name. (default: the first provided + domain or the name of an existing certificate on your + system for the same domains) + --dry-run Perform a test run against the Let's Encrypt staging + server, obtaining test (invalid) certificates but not + saving them to disk. This can only be used with the + 'certonly' and 'renew' subcommands. It may trigger + webserver reloads to temporarily modify & roll back + configuration files. --pre-hook and --post-hook + commands run by default. --deploy-hook commands do not + run, unless enabled by --run-deploy-hooks. The test + server may be overridden with --server. (default: + False) + --debug-challenges After setting up challenges, wait for user input + before submitting to CA. When used in combination with + the `-v` option, the challenge URLs or FQDNs and their + expected return values are shown. (default: False) + --required-profile REQUIRED_PROFILE + Request the given profile name from the ACME server. + If the ACME server returns an error, issuance (or + renewal) will fail. For long-term reliability, setting + preferred_profile instead may be preferable because it + allows fallback to a default. Use this setting when + renewal failure is preferable to fallback. (default: + None) + --preferred-profile PREFERRED_PROFILE + Request the given profile name from the ACME server, + or fallback to default. If the given profile name + exists in the ACME directory, use it to request a a + certificate. Otherwise, fall back to requesting a + certificate without a profile (which means the CA will + use its default profile). This allows renewals to + succeed even if the CA deprecates and removes a given + profile. (default: None) + --preferred-chain PREFERRED_CHAIN + Set the preferred certificate chain. If the CA offers + multiple certificate chains, prefer the chain whose + topmost certificate was issued from this Subject + Common Name. If no match, the default offered chain + will be used. (default: None) + --preferred-challenges PREF_CHALLS + A sorted, comma delimited list of the preferred + challenge to use during authorization with the most + preferred challenge listed first (Eg, "dns" or + "http,dns"). Not all plugins support all challenges. + See https://certbot.eff.org/docs/using.html#plugins + for details. ACME Challenges are versioned, but if you + pick "http" rather than "http-01", Certbot will select + the latest version automatically. (default: []) + --issuance-timeout ISSUANCE_TIMEOUT + This option specifies how long (in seconds) Certbot + will wait for the server to issue a certificate. + (default: 90) + --user-agent USER_AGENT + Set a custom user agent string for the client. User + agent strings allow the CA to collect high level + statistics about success rates by OS, plugin and use + case, and to know when to deprecate support for past + Python versions and flags. If you wish to hide this + information from the Let's Encrypt server, set this to + "". (default: CertbotACMEClient/5.7.0 (certbot; + OS_NAME OS_VERSION) Authenticator/XXX Installer/YYY + (SUBCOMMAND; flags: FLAGS) Py/major.minor.patchlevel). + The flags encoded in the user agent are: --duplicate, + --force-renew, --allow-subset-of-names, -n, and + whether any hooks are set. + --user-agent-comment USER_AGENT_COMMENT + Add a comment to the default user agent string. May be + used when repackaging Certbot or calling it from + another tool to allow additional statistical data to + be collected. Ignored if --user-agent is set. + (Example: Foo-Wrapper/1.0) (default: None) + +automation: + Flags for automating execution & other tweaks + + --keep-until-expiring, --keep, --reinstall + If the requested certificate matches an existing + certificate, always keep the existing one until it is + due for renewal (for the 'run' subcommand this means + reinstall the existing certificate). (default: Ask) + --expand If an existing certificate is a strict subset of the + requested names, always expand and replace it with the + additional names. (default: Ask) + --version show program's version number and exit + --force-renewal, --renew-by-default + If a certificate already exists for the requested + domains, renew it now, regardless of whether it is + near expiry. (Often --keep-until-expiring is more + appropriate). Also implies --expand. (default: False) + --renew-with-new-domains + If a certificate already exists for the requested + certificate name but does not match the requested + domains, renew it now, regardless of whether it is + near expiry. (default: False) + --reuse-key When renewing, use the same private key as the + existing certificate. (default: False) + --no-reuse-key When renewing, do not use the same private key as the + existing certificate. Not reusing private keys is the + default behavior of Certbot. This option may be used + to unset --reuse-key on an existing certificate. + (default: False) + --new-key When renewing or replacing a certificate, generate a + new private key, even if --reuse-key is set on the + existing certificate. Combining --new-key and --reuse- + key will result in the private key being replaced and + then reused in future renewals. (default: False) + --allow-subset-of-names + When performing domain validation, do not consider it + a failure if authorizations can not be obtained for a + strict subset of the requested domains. This may be + useful for allowing renewals for multiple domains to + succeed even if some domains no longer point at this + system. This option cannot be used with --csr. + (default: False) + --agree-tos Agree to the ACME Subscriber Agreement (default: Ask) + --duplicate Allow making a certificate lineage that duplicates an + existing one (both can be renewed in parallel) + (default: False) + -q, --quiet Silence all output except errors. Useful for + automation via cron. Implies --non-interactive. + (default: False) + +security: + Security parameters & server settings + + --rsa-key-size N Size of the RSA key. (default: 2048) + --key-type {rsa,ecdsa} + Type of generated private key. Only *ONE* per + invocation can be provided at this time. (default: + ecdsa) + --elliptic-curve N The SECG elliptic curve name to use. Please see RFC + 8446 for supported values. (default: secp256r1) + --must-staple Adds the OCSP Must-Staple extension to the + certificate. Autoconfigures OCSP Stapling for + supported setups (Apache version >= 2.3.3 ). (default: + False) + --redirect Automatically redirect all HTTP traffic to HTTPS for + the newly authenticated vhost. (default: redirect + enabled for install and run, disabled for enhance) + --no-redirect Do not automatically redirect all HTTP traffic to + HTTPS for the newly authenticated vhost. (default: + redirect enabled for install and run, disabled for + enhance) + --hsts Add the Strict-Transport-Security header to every HTTP + response. Forcing browser to always use SSL for the + domain. Defends against SSL Stripping. (default: + False) + --uir Add the "Content-Security-Policy: upgrade-insecure- + requests" header to every HTTP response. Forcing the + browser to use https:// for every http:// resource. + (default: False) + --staple-ocsp Enables OCSP Stapling. A valid OCSP response is + stapled to the certificate that the server offers + during TLS. (default: False) + --strict-permissions Require that all configuration files are owned by the + current user; only needed if your config is somewhere + unsafe like /tmp/ (default: False) + --auto-hsts Gradually increasing max-age value for HTTP Strict + Transport Security security header (default: False) + +testing: + The following flags are meant for testing and integration purposes only. + + --run-deploy-hooks When performing a test run using `--dry-run` or + `reconfigure`, run any applicable deploy hooks. This + includes hooks set on the command line, saved in the + certificate's renewal configuration file, or present + in the renewal-hooks directory. To exclude directory + hooks, use --no-directory-hooks. The hook(s) will only + be run if the dry run succeeds, and will use the + current active certificate, not the temporary test + certificate acquired during the dry run. This flag is + recommended when modifying the deploy hook using + `reconfigure`. (default: False) + --test-cert, --staging + Use the Let's Encrypt staging server to obtain or + revoke test (invalid) certificates; equivalent to + --server https://acme- + staging-v02.api.letsencrypt.org/directory (default: + False) + --debug Show tracebacks in case of errors (default: False) + --no-verify-ssl Disable verification of the ACME server's certificate. + The root certificates trusted by Certbot can be + overridden by setting the REQUESTS_CA_BUNDLE + environment variable. (default: False) + --http-01-port HTTP01_PORT + Port used in the http-01 challenge. This only affects + the port Certbot listens on. A conforming ACME server + will still attempt to connect on port 80. (default: + 80) + --http-01-address HTTP01_ADDRESS + The address the server listens to during http-01 + challenge. (default: ) + --https-port HTTPS_PORT + Port used to serve HTTPS. This affects which port + Nginx will listen on after a LE certificate is + installed. (default: 443) + --break-my-certs Be willing to replace or renew valid certificates with + invalid (testing/staging) certificates (default: + False) + +paths: + Flags for changing execution paths & servers + + --cert-path CERT_PATH + Path to where certificate is saved (with certonly + --csr), installed from, or revoked (default: None) + --key-path KEY_PATH Path to private key for certificate installation or + revocation (if account key is missing) (default: None) + --fullchain-path FULLCHAIN_PATH + Accompanying path to a full certificate chain + (certificate plus chain). (default: None) + --chain-path CHAIN_PATH + Accompanying path to a certificate chain. (default: + None) + --config-dir CONFIG_DIR + Configuration directory. (default: /etc/letsencrypt) + --work-dir WORK_DIR Working directory. (default: /var/lib/letsencrypt) + --logs-dir LOGS_DIR Logs directory. (default: /var/log/letsencrypt) + --server SERVER ACME Directory Resource URI. (default: + https://acme-v02.api.letsencrypt.org/directory) + +manage: + Various subcommands and flags are available for managing your + certificates: + + certificates List certificates managed by Certbot + delete Clean up all files related to a certificate + renew Renew all certificates (or one specified with --cert- + name) + revoke Revoke a certificate specified with --cert-path or + --cert-name + reconfigure Update renewal configuration for a certificate + specified by --cert-name + +run: + Options for obtaining & installing certificates + +certonly: + Options for modifying how a certificate is obtained + + --deploy-hook DEPLOY_HOOK + Command to be run in a shell once for each + successfully issued certificate, including on + subsequent renewals. Unless --disable-hook-validation + is used, the command’s first word must be the absolute + pathname of an executable or one found via the PATH + environment variable. For this command, the shell + variable $RENEWED_LINEAGE will point to the config + live subdirectory (for example, + "/etc/letsencrypt/live/example.com") containing the + new certificates and keys; the shell variable + $RENEWED_DOMAINS will contain a space-delimited list + of renewed certificate domains (for example, + "example.com www.example.com") (default: None) + --csr CSR Path to a Certificate Signing Request (CSR) in DER or + PEM format. Currently --csr only works with the + 'certonly' subcommand. (default: None) + +renew: + The 'renew' subcommand will attempt to renew any certificates previously + obtained if they are close to expiry, and print a summary of the results. + By default, 'renew' will reuse the plugins and options used to obtain or + most recently renew each certificate. You can test whether future renewals + will succeed with `--dry-run`. Individual certificates can be renewed with + the `--cert-name` option. Hooks are available to run commands before and + after renewal; see https://certbot.eff.org/docs/using.html#renewal for + more information on these. + + --pre-hook PRE_HOOK Command to be run in a shell before obtaining any + certificates. Unless --disable-hook-validation is + used, the command’s first word must be the absolute + pathname of an executable or one found via the PATH + environment variable. Intended primarily for renewal, + where it can be used to temporarily shut down a + webserver that might conflict with the standalone + plugin. This will only be called if a certificate is + actually to be obtained/renewed. When renewing several + certificates that have identical pre-hooks, only the + first will be executed. (default: None) + --post-hook POST_HOOK + Command to be run in a shell after attempting to + obtain/renew certificates. Unless --disable-hook- + validation is used, the command’s first word must be + the absolute pathname of an executable or one found + via the PATH environment variable. Can be used to + deploy renewed certificates, or to restart any servers + that were stopped by --pre-hook. This is only run if + an attempt was made to obtain/renew a certificate. If + multiple renewed certificates have identical post- + hooks, only one will be run. (default: None) + --disable-hook-validation + Ordinarily the commands specified for --pre- + hook/--post-hook/--deploy-hook will be checked for + validity, to see if the programs being run are in the + $PATH, so that mistakes can be caught early, even when + the hooks aren't being run just yet. The validation is + rather simplistic and fails if you use more advanced + shell constructs, so you can use this switch to + disable it. (default: False) + --no-directory-hooks Disable running executables found in Certbot's hook + directories. (default: False) + --disable-renew-updates + Disable automatic updates to your server configuration + that would otherwise be done by the selected installer + plugin, and triggered when the user executes "certbot + renew", regardless of if the certificate is renewed. + This setting does not apply to important TLS + configuration updates. (default: False) + --no-autorenew Disable auto renewal of certificates. (default: False) + +certificates: + List certificates managed by Certbot + +delete: + Options for deleting a certificate + +revoke: + Options for revocation of certificates + + --reason {unspecified,keycompromise,affiliationchanged,superseded,cessationofoperation} + Specify reason for revoking certificate. (default: + unspecified) + --delete-after-revoke + Delete certificates after revoking them, along with + all previous and later versions of those certificates. + (default: Ask) + --no-delete-after-revoke + Do not delete certificates after revoking them. This + option should be used with caution because the 'renew' + subcommand will attempt to renew undeleted revoked + certificates. (default: Ask) + +register: + Options for account registration + + -m EMAIL, --email EMAIL + Email used for registration and recovery contact. Use + comma to register multiple emails, ex: + u1@example.com,u2@example.com. (default: Ask). + --eff-email Share your e-mail address with EFF (default: Ask) + --no-eff-email Don't share your e-mail address with EFF (default: + Ask) + +update_account: + Options for account modification + +unregister: + Options for account deactivation. + + --account ACCOUNT_ID Account ID to use (default: None) + +install: + Options for modifying how a certificate is deployed + +rollback: + Options for rolling back server configuration changes + + --checkpoints N Revert configuration N number of checkpoints. + (default: 1) + +plugins: + Options for the "plugins" subcommand + + --init Initialize plugins. (default: False) + --prepare Initialize and prepare plugins. (default: False) + --authenticators Limit to authenticator plugins only. (default: None) + --installers Limit to installer plugins only. (default: None) + +enhance: + Helps to harden the TLS configuration by adding security enhancements to + already existing configuration. + +show_account: + Options useful for the "show_account" subcommand: + +reconfigure: + Common options that may be updated with the "reconfigure" subcommand: + +plugins: + Plugin Selection: Certbot client supports an extensible plugins + architecture. See 'certbot plugins' for a list of all installed plugins + and their names. You can force a particular plugin by setting options + provided below. Running --help will list flags specific to + that plugin. + + --configurator CONFIGURATOR + Name of the plugin that is both an authenticator and + an installer. Should not be used together with + --authenticator or --installer. (default: Ask) + -a AUTHENTICATOR, --authenticator AUTHENTICATOR + Authenticator plugin name. (default: None) + -i INSTALLER, --installer INSTALLER + Installer plugin name (also used to find domains). + (default: None) + --apache Obtain and install certificates using Apache (default: + False) + --nginx Obtain and install certificates using Nginx (default: + False) + --standalone Obtain certificates using a "standalone" webserver. + (default: False) + --manual Provide laborious manual instructions for obtaining a + certificate (default: False) + --webroot Obtain certificates by placing files in a webroot + directory. (default: False) + --dns-cloudflare Obtain certificates using a DNS TXT record (if you are + using Cloudflare for DNS). (default: False) + --dns-digitalocean Obtain certificates using a DNS TXT record (if you are + using DigitalOcean for DNS). (default: False) + --dns-dnsimple Obtain certificates using a DNS TXT record (if you are + using DNSimple for DNS). (default: False) + --dns-dnsmadeeasy Obtain certificates using a DNS TXT record (if you are + using DNS Made Easy for DNS). (default: False) + --dns-gehirn Obtain certificates using a DNS TXT record (if you are + using Gehirn Infrastructure Service for DNS). + (default: False) + --dns-google Obtain certificates using a DNS TXT record (if you are + using Google Cloud DNS). (default: False) + --dns-linode Obtain certificates using a DNS TXT record (if you are + using Linode for DNS). (default: False) + --dns-luadns Obtain certificates using a DNS TXT record (if you are + using LuaDNS for DNS). (default: False) + --dns-nsone Obtain certificates using a DNS TXT record (if you are + using NS1 for DNS). (default: False) + --dns-ovh Obtain certificates using a DNS TXT record (if you are + using OVH for DNS). (default: False) + --dns-rfc2136 Obtain certificates using a DNS TXT record (if you are + using BIND for DNS). (default: False) + --dns-route53 Obtain certificates using a DNS TXT record (if you are + using Route53 for DNS). (default: False) + --dns-sakuracloud Obtain certificates using a DNS TXT record (if you are + using Sakura Cloud for DNS). (default: False) + +apache: + Apache Web Server plugin (Please note that the default values of the + Apache plugin options change depending on the operating system Certbot is + run on.) + + --apache-enmod APACHE_ENMOD + Path to the Apache 'a2enmod' binary (default: None) + --apache-dismod APACHE_DISMOD + Path to the Apache 'a2dismod' binary (default: None) + --apache-le-vhost-ext APACHE_LE_VHOST_EXT + SSL vhost configuration extension (default: -le- + ssl.conf) + --apache-server-root APACHE_SERVER_ROOT + Apache server root directory (default: /etc/apache2) + --apache-vhost-root APACHE_VHOST_ROOT + Apache server VirtualHost configuration root (default: + None) + --apache-logs-root APACHE_LOGS_ROOT + Apache server logs directory (default: + /var/log/apache2) + --apache-challenge-location APACHE_CHALLENGE_LOCATION + Directory path for challenge configuration (default: + /etc/apache2) + --apache-handle-modules APACHE_HANDLE_MODULES + Let installer handle enabling required modules for you + (Only Ubuntu/Debian currently) (default: False) + --apache-handle-sites APACHE_HANDLE_SITES + Let installer handle enabling sites for you (Only + Ubuntu/Debian currently) (default: False) + --apache-ctl APACHE_CTL + Full path to Apache control script (default: + apache2ctl) + --apache-bin APACHE_BIN + Full path to apache2/httpd binary (default: None) + +dns-cloudflare: + Obtain certificates using a DNS TXT record (if you are using Cloudflare + for DNS). + + --dns-cloudflare-propagation-seconds DNS_CLOUDFLARE_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 10) + --dns-cloudflare-credentials DNS_CLOUDFLARE_CREDENTIALS + Cloudflare credentials INI file. (default: None) + +dns-digitalocean: + Obtain certificates using a DNS TXT record (if you are using DigitalOcean + for DNS). + + --dns-digitalocean-propagation-seconds DNS_DIGITALOCEAN_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 10) + --dns-digitalocean-credentials DNS_DIGITALOCEAN_CREDENTIALS + DigitalOcean credentials INI file. (default: None) + +dns-dnsimple: + Obtain certificates using a DNS TXT record (if you are using DNSimple for + DNS). + + --dns-dnsimple-propagation-seconds DNS_DNSIMPLE_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 30) + --dns-dnsimple-credentials DNS_DNSIMPLE_CREDENTIALS + DNSimple credentials INI file. (default: None) + +dns-dnsmadeeasy: + Obtain certificates using a DNS TXT record (if you are using DNS Made Easy + for DNS). + + --dns-dnsmadeeasy-propagation-seconds DNS_DNSMADEEASY_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 60) + --dns-dnsmadeeasy-credentials DNS_DNSMADEEASY_CREDENTIALS + DNS Made Easy credentials INI file. (default: None) + +dns-gehirn: + Obtain certificates using a DNS TXT record (if you are using Gehirn + Infrastructure Service for DNS). + + --dns-gehirn-propagation-seconds DNS_GEHIRN_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 30) + --dns-gehirn-credentials DNS_GEHIRN_CREDENTIALS + Gehirn Infrastructure Service credentials file. + (default: None) + +dns-google: + Obtain certificates using a DNS TXT record (if you are using Google Cloud + DNS for DNS). + + --dns-google-propagation-seconds DNS_GOOGLE_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 60) + --dns-google-credentials DNS_GOOGLE_CREDENTIALS + Path to Google Cloud DNS service account JSON file to + use instead of relying on Application Default + Credentials (ADC). (See https://cloud.google.com/docs/ + authentication/application-default-credentials for + information about ADC, https://developers.google.com/i + dentity/protocols/OAuth2ServiceAccount#creatinganaccou + nt for information about creating a service account, + and https://cloud.google.com/dns/access- + control#permissions_and_roles for information about + the permissions required to modify Cloud DNS records.) + (default: None) + --dns-google-project DNS_GOOGLE_PROJECT + The ID of the Google Cloud project that the Google + Cloud DNS managed zone(s) reside in. This will be + determined automatically if not specified. (default: + None) + +dns-linode: + Obtain certificates using a DNS TXT record (if you are using Linode for + DNS). + + --dns-linode-propagation-seconds DNS_LINODE_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 120) + --dns-linode-credentials DNS_LINODE_CREDENTIALS + Linode credentials INI file. (default: None) + +dns-luadns: + Obtain certificates using a DNS TXT record (if you are using LuaDNS for + DNS). + + --dns-luadns-propagation-seconds DNS_LUADNS_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 30) + --dns-luadns-credentials DNS_LUADNS_CREDENTIALS + LuaDNS credentials INI file. (default: None) + +dns-nsone: + Obtain certificates using a DNS TXT record (if you are using NS1 for DNS). + + --dns-nsone-propagation-seconds DNS_NSONE_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 30) + --dns-nsone-credentials DNS_NSONE_CREDENTIALS + NS1 credentials file. (default: None) + +dns-ovh: + Obtain certificates using a DNS TXT record (if you are using OVH for DNS). + + --dns-ovh-propagation-seconds DNS_OVH_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 120) + --dns-ovh-credentials DNS_OVH_CREDENTIALS + OVH credentials INI file. (default: None) + +dns-rfc2136: + Obtain certificates using a DNS TXT record (if you are using BIND for + DNS). + + --dns-rfc2136-propagation-seconds DNS_RFC2136_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 60) + --dns-rfc2136-credentials DNS_RFC2136_CREDENTIALS + RFC 2136 credentials INI file. (default: None) + +dns-route53: + Obtain certificates using a DNS TXT record (if you are using AWS Route53 + for DNS). + +dns-sakuracloud: + Obtain certificates using a DNS TXT record (if you are using Sakura Cloud + for DNS). + + --dns-sakuracloud-propagation-seconds DNS_SAKURACLOUD_PROPAGATION_SECONDS + The number of seconds to wait for DNS to propagate + before asking the ACME server to verify the DNS + record. (default: 90) + --dns-sakuracloud-credentials DNS_SAKURACLOUD_CREDENTIALS + Sakura Cloud credentials file. (default: None) + +manual: + Authenticate through manual configuration or custom shell scripts. When + using shell scripts, an authenticator script must be provided. The + environment variables available to this script depend on the type of + challenge. $CERTBOT_IDENTIFIER will always contain the domain or IP + address being authenticated. For HTTP-01 and DNS-01, $CERTBOT_VALIDATION + is the validation string, and $CERTBOT_TOKEN is the filename of the + resource requested when performing an HTTP-01 challenge. An additional + cleanup script can also be provided and can use the additional variable + $CERTBOT_AUTH_OUTPUT which contains the stdout output from the auth + script. For both authenticator and cleanup script, on HTTP-01 and DNS-01 + challenges, $CERTBOT_REMAINING_CHALLENGES will be equal to the number of + challenges that remain after the current one, and $CERTBOT_ALL_IDENTIFIERS + contains a comma-separated list of all identifiers that are challenged for + the current certificate. + + --manual-auth-hook MANUAL_AUTH_HOOK + Path or command to execute for the authentication + script (default: None) + --manual-cleanup-hook MANUAL_CLEANUP_HOOK + Path or command to execute for the cleanup script + (default: None) + +nginx: + Nginx Web Server plugin + + --nginx-server-root NGINX_SERVER_ROOT + Nginx server root directory. (default: /etc/nginx or + /usr/local/etc/nginx) + --nginx-ctl NGINX_CTL + Path to the 'nginx' binary, used for 'configtest' and + retrieving nginx version number. (default: nginx) + --nginx-sleep-seconds NGINX_SLEEP_SECONDS + Number of seconds to wait for nginx configuration + changes to apply when reloading. (default: 1) + +null: + Null Installer + +standalone: + Runs an HTTP server locally which serves the necessary validation files + under the /.well-known/acme-challenge/ request path. Suitable if there is + no HTTP server already running. HTTP challenge only (wildcards not + supported). + +webroot: + Saves the necessary validation files to a .well-known/acme-challenge/ + directory within the nominated webroot path. A separate HTTP server must + be running and serving files from the webroot path. HTTP challenge only + (wildcards not supported). + + --webroot-path WEBROOT_PATH, -w WEBROOT_PATH + public_html / webroot path. This can be specified + multiple times to handle different identifiers; each + identifier will have the webroot path that preceded + it. For instance: `-w /var/www/example -d example.com + -d www.example.com -w /var/www/thing -d thing.net -d + m.thing.net` (default: Ask) + --webroot-map WEBROOT_MAP + JSON dictionary mapping identifiers to webroot paths; + this implies -d or --ip-address for each entry. You + may need to escape this from your shell. E.g.: + --webroot-map '{"eg1.is,m.eg1.is":"/www/eg1/", + "eg2.is":"/www/eg2"}' This option is merged with, but + takes precedence over, -w / -d entries. At present, if + you put webroot-map in a config file, it needs to be + on a single line, like: webroot-map = + {"example.com":"/var/www"}. (default: {}) diff --git a/certbot/docs/compatibility.rst b/certbot/docs/compatibility.rst new file mode 100644 index 00000000000..d94642ec658 --- /dev/null +++ b/certbot/docs/compatibility.rst @@ -0,0 +1,39 @@ +======================= +Backwards Compatibility +======================= + +All Certbot components including `acme `_, +Certbot, and :ref:`non-third party plugins ` follow `Semantic +Versioning `_ both for its Python :doc:`API ` and for the +application itself. This means that we will not change behavior in a backwards +incompatible way except in a new major version of the project. + +.. note:: None of this applies to the behavior of Certbot distribution + mechanisms such as :ref:`our snaps ` or OS packages whose + behavior may change at any time. Semantic versioning only applies to the + common Certbot components that are installed by various distribution + methods. + +For Certbot as an application, the command line interface and non-interactive +behavior can be considered stable with two exceptions. The first is that no +aspects of Certbot's console or log output should be considered stable and it +may change at any time. The second is that Certbot's behavior should only be +considered stable with certain files but not all. Files with which users should +expect Certbot to maintain its current behavior with are: + +* ``/etc/letsencrypt/live/$domain/{cert,chain,fullchain,privkey}.pem``, where + ``$domain`` is the certificate name (see :ref:`where-certs` + for more details) +* :ref:`CLI configuration files ` +* Hook directories in ``/etc/letsencrypt/renewal-hooks`` + +Certbot's behavior with other files may change at any point. + +Another area where Certbot should not be considered stable is its behavior when +not run in non-interactive mode which also may change at any point. + +In general, if we're making a change that we expect will break some users, we +will bump the major version and will have warned about it in a prior release +when possible. For our Python API, we will issue warnings using Python's +warning module. For application level changes, we will print and log warning +messages. diff --git a/certbot/docs/conf.py b/certbot/docs/conf.py new file mode 100644 index 00000000000..f644836c2c6 --- /dev/null +++ b/certbot/docs/conf.py @@ -0,0 +1,318 @@ +# -*- coding: utf-8 -*- +# +# Certbot documentation build configuration file, created by +# sphinx-quickstart on Sun Nov 23 20:35:21 2014. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import codecs +import os +import re +import sys + +import sphinx + +here = os.path.abspath(os.path.dirname(__file__)) + +# read version number (and other metadata) from package init +init_fn = os.path.join(here, '..', 'src', 'certbot', '__init__.py') +with codecs.open(init_fn, encoding='utf8') as fd: + meta = dict(re.findall(r"""__([a-z]+)__ = '([^']+)""", fd.read())) + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +sys.path.insert(0, os.path.abspath(os.path.join(here, '..'))) + +# -- General configuration ------------------------------------------------ + +# If your documentation needs a minimal Sphinx version, state it here. +needs_sphinx = '1.2' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.intersphinx', + 'sphinx.ext.todo', + 'sphinx.ext.coverage', + 'sphinx.ext.viewcode', + 'sphinx_rtd_theme', +] + +if sphinx.version_info >= (1, 6): + extensions.append('sphinx.ext.imgconverter') + +autodoc_member_order = 'bysource' +autodoc_default_flags = ['show-inheritance'] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'Certbot' +# this is now overridden by the footer.html template +copyright = u'2014-2018 - The Certbot software and documentation are licensed under the Apache 2.0 license as described at https://eff.org/cb-license.' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '.'.join(meta['version'].split('.')[:2]) +# The full version, including alpha/beta/rc tags. +release = meta['version'] + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = 'en' + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = [ + '_build', + 'challenges.rst', +] + +# The reST default role (used for this markup: `text`) to use for all +# documents. +default_role = 'py:obj' + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +#keep_warnings = False + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = False + +suppress_warnings = ['image.nonlocal_uri'] + +# -- Options for HTML output ---------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. + +html_theme = 'sphinx_rtd_theme' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# Add any extra paths that contain custom files (such as robots.txt or +# .htaccess) here, relative to this directory. These files are copied +# directly to the root of the documentation. +#html_extra_path = [] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Language to be used for generating the HTML full-text search index. +# Sphinx supports the following languages: +# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' +# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' +#html_search_language = 'en' + +# A dictionary with options for the search language support, empty by default. +# Now only 'ja' uses this config value +#html_search_options = {'type': 'default'} + +# The name of a javascript file (relative to the configuration directory) that +# implements a search results scorer. If empty, the default will be used. +#html_search_scorer = 'scorer.js' + +# Output file base name for HTML help builder. +htmlhelp_basename = 'Certbotdoc' + +# -- Options for LaTeX output --------------------------------------------- + +latex_elements = { + # The paper size ('letterpaper' or 'a4paper'). + #'papersize': 'letterpaper', + + # The font size ('10pt', '11pt' or '12pt'). + #'pointsize': '10pt', + + # Additional stuff for the LaTeX preamble. + #'preamble': '', + + # Latex figure (float) alignment + #'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + ('index', 'Certbot.tex', u'Certbot Documentation', + u'Certbot Project', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output --------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + ('index', 'certbot', u'Certbot Documentation', + [project], 7), + ('man/certbot', 'certbot', u"Automatically configure HTTPS using Let's Encrypt", + [project], 1), +] + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + ('index', 'Certbot', u'Certbot Documentation', + u'Certbot Project', 'Certbot', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +#texinfo_no_detailmenu = False + + +intersphinx_mapping = { + 'python': ('https://docs.python.org/', None), + 'acme': ('https://acme-python.readthedocs.io/en/latest/', None), +} diff --git a/certbot/docs/contributing.rst b/certbot/docs/contributing.rst new file mode 100644 index 00000000000..cc16a947da1 --- /dev/null +++ b/certbot/docs/contributing.rst @@ -0,0 +1,660 @@ +=============== +Developer Guide +=============== + +.. contents:: Table of Contents + :local: + + +.. _getting_started: + +Getting Started +=============== + +Certbot has the same :ref:`system requirements ` when set +up for development. While the section below will help you install Certbot and +its dependencies, Certbot needs to be run on a UNIX-like OS so if you're using +Windows, you'll need to set up a (virtual) machine running an OS such as Linux +and continue with these instructions on that UNIX-like OS. + +If you're using macOS, it is recommended to first check out the `macOS +suggestions`_ section before continuing with the installation instructions +below. + +.. _local copy: + +Running a local copy of the client +---------------------------------- + +Running the client in developer mode from your local tree is a little different +than running Certbot as a user. To get set up, clone our git repository by +running: + +.. code-block:: shell + + git clone https://github.com/certbot/certbot + +If you're running on a UNIX-like OS, you can run the following commands to +install dependencies and set up a virtual environment where you can run +Certbot. + +Install and configure the OS system dependencies required to run Certbot. + +.. code-block:: shell + + # For APT-based distributions (e.g. Debian, Ubuntu ...) + sudo apt update + sudo apt install python3-dev python3-venv libaugeas-dev gcc + # For RPM-based distributions (e.g. Fedora, CentOS ...) + # NB1: old distributions will use yum instead of dnf + # NB2: RHEL-based distributions use python3X instead of python3 (e.g. python38) + sudo dnf install python3 python3-devel augeas-devel gcc + # For macOS installations with Homebrew already installed and configured + # NB: CFLAGS are needed to compile and link to Augeas installed through + # Homebrew and some of our developer scripts expect GNU coreutils be first in + # your PATH. The commands below set this up for bash and zsh, but your + # instructions may be slightly different if you use an alternate shell. + brew install augeas coreutils gnu-sed + BREW_PREFIX=$(brew --prefix) + RC_LINES="export CFLAGS=\"\$CFLAGS -I$BREW_PREFIX/include -L$BREW_PREFIX/lib\"\n" + RC_LINES+="export PATH=\"$BREW_PREFIX/opt/coreutils/libexec/gnubin:" + RC_LINES+="$BREW_PREFIX/opt/gnu-sed/libexec/gnubin:\$PATH\"\n" + printf "$RC_LINES" >> ~/.bashrc # for bash + printf "$RC_LINES" >> ~/.zshrc # for zsh + +.. note:: If you have trouble creating the virtual environment below, you may + need to install additional dependencies. See the `cryptography project's + site`_ for more information. + +.. _`cryptography project's site`: https://cryptography.io/en/latest/installation.html#building-cryptography-on-linux + +Set up the Python virtual environment that will host your Certbot local instance. + +.. code-block:: shell + + cd certbot + python3 tools/venv.py + +.. note:: You may need to repeat this when + Certbot's dependencies change or when a new plugin is introduced. + +You can now run the copy of Certbot from git either by executing +``venv/bin/certbot``, or by activating the virtual environment. You can do the +latter by running: + +.. code-block:: shell + + source venv/bin/activate + +After running this command, ``certbot`` and development tools like ``ipdb3``, +``ipython``, ``pytest``, and ``tox`` are available in the shell where you ran +the command. These tools are installed in the virtual environment and are kept +separate from your global Python installation. This works by setting +environment variables so the right executables are found and Python can pull in +the versions of various packages needed by Certbot. More information can be +found in the `virtualenv docs`_. + +.. _`virtualenv docs`: https://virtualenv.pypa.io + +Find issues to work on +---------------------- + +You can find the open issues in the `github issue tracker`_. If you're starting +work on something, post a comment to let others know and seek feedback on your +plan where appropriate. + +Once you've got a working branch, you can open a pull request. All changes in +your pull request must have thorough unit test coverage, pass our +tests, and be compliant with the :ref:`coding style `. + +.. _github issue tracker: https://github.com/certbot/certbot/issues + +.. _testing: + +Testing +------- + +You can test your code in several ways: + +- running the `automated unit`_ tests, +- running the `automated integration`_ tests +- running an *ad hoc* `manual integration`_ test + +.. _automated unit: + +Running automated unit tests +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +To run all unittests, mypy, and lint: + +.. code-block:: shell + + tox + +If you're working on a specific test and would like to run just that one: + +.. code-block:: shell + + pytest acme/src/acme/_internal/tests/messages_test.py # Use the test file you're working on + +To run a specific test case within a file: + +.. code-block:: shell + + pytest acme/src/acme/_internal/tests/messages_test.py -k test_to_partial_json + +For debugging, we recommend putting +``import ipdb; ipdb.set_trace()`` statements inside the source code, which will require +adding the `-s` flag to `pytest` invocations. + +.. warning:: The full test suite may attempt to modify your system's Apache + config if your user has sudo permissions, so it should not be run on a + production Apache server. + +.. _automated integration: + +Running automated integration tests +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Generally it is sufficient to open a pull request and let GitHub run +integration tests for you. However, you may want to run them locally before submitting +your pull request. You need Docker installed and working. + +The tox environment `integration` will setup `Pebble`_, the Let's Encrypt ACME CA server +for integration testing, then launch the Certbot integration tests. + +With a user allowed to access your local Docker daemon, run: + +.. code-block:: shell + + tox run -e integration + +Tests will be run using pytest. A test report and a code coverage report will be +displayed at the end of the integration tests execution. + +.. _Pebble: https://github.com/letsencrypt/pebble + +.. _manual integration: + +Running manual integration tests +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can also manually execute Certbot against a local instance of the `Pebble`_ ACME server. +This is useful to verify that the modifications done to the code makes Certbot behave as expected. + +To do so you need: + +- Docker installed, and a user with access to the Docker client, +- an available `local copy`_ of Certbot. + +The virtual environment set up with `python3 tools/venv.py` contains two CLI tools +that can be used once the virtual environment is activated: + +.. code-block:: shell + + run_acme_server + +- Starts a local instance of Pebble and runs in the foreground printing its logs. +- Press CTRL+C to stop this instance. +- This instance is configured to validate challenges against certbot executed locally. + +.. note:: Some options are available to tweak the local ACME server. You can execute + ``run_acme_server --help`` to see the inline help of the ``run_acme_server`` tool. + +.. code-block:: shell + + certbot_test [ARGS...] + +- Execute certbot with the provided arguments and other arguments useful for testing purposes, + such as: verbose output, full tracebacks in case Certbot crashes, *etc.* +- Execution is preconfigured to interact with the Pebble CA started with ``run_acme_server``. +- Any arguments can be passed as they would be to Certbot (eg. ``certbot_test certonly -d test.example.com``). + +Here is a typical workflow to verify that Certbot successfully issued a certificate +using an HTTP-01 challenge on a machine with Python 3: + +.. code-block:: shell + + python3 tools/venv.py + source venv/bin/activate + run_acme_server & + certbot_test certonly --standalone -d test.example.com + # To stop Pebble, launch `fg` to get back the background job, then press CTRL+C + +Running tests in CI +~~~~~~~~~~~~~~~~~~~ + +Certbot uses GitHub Actions to run continuous integration tests. If you are using our +GitHub Actions setup, a branch whose name starts with `test-` will run all tests on that branch. + +Code components and layout +========================== + +The following components of the Certbot repository are distributed to users: + +acme + contains all protocol specific code +certbot + main client code +certbot-apache and certbot-nginx + client code to configure specific web servers +certbot-dns-* + client code to configure DNS providers + +Plugin-architecture +------------------- + +Certbot has a plugin architecture to facilitate support for +different webservers, other TLS servers, and operating systems. +The interfaces available for plugins to implement are defined in +`interfaces.py`_ and `plugins/common.py`_. + +The main two plugin interfaces are `~certbot.interfaces.Authenticator`, which +implements various ways of proving domain control to a certificate authority, +and `~certbot.interfaces.Installer`, which configures a server to use a +certificate once it is issued. Some plugins, like the built-in Apache and Nginx +plugins, implement both interfaces and perform both tasks. Others, like the +built-in Standalone authenticator, implement just one interface. + +.. _interfaces.py: https://github.com/certbot/certbot/blob/main/certbot/src/certbot/interfaces.py +.. _plugins/common.py: https://github.com/certbot/certbot/blob/main/certbot/src/certbot/plugins/common.py#L45 + + +Authenticators +-------------- + +Authenticators are plugins that prove control of a domain name by solving a +challenge provided by the ACME server. ACME currently defines two types of +challenges: HTTP and DNS, represented by classes in `acme.challenges`. +An authenticator plugin should implement support for at least one challenge type. + +An Authenticator indicates which challenges it supports by implementing +`get_chall_pref(identifier)` to return a sorted list of challenge types in +preference order. + +An Authenticator must also implement `perform(achalls)`, which "performs" a list +of challenges by, for instance, provisioning a file on an HTTP server, or +setting a TXT record in DNS. Once all challenges have succeeded or failed, +Certbot will call the plugin's `cleanup(achalls)` method to remove any files or +DNS records that were needed only during authentication. + +Installer +--------- + +Installers plugins exist to actually setup the certificate in a server, +possibly tweak the security configuration to make it more correct and secure +(Fix some mixed content problems, turn on HSTS, redirect to HTTPS, etc). +Installer plugins tell the main client about their abilities to do the latter +via the :meth:`~.Installer.supported_enhancements` call. We currently +have two Installers in the tree, the `~.ApacheConfigurator`. and the +`~.NginxConfigurator`. External projects have made some progress toward +support for IIS, Icecast and Plesk. + +Installers and Authenticators will oftentimes be the same class/object +(because for instance both tasks can be performed by a webserver like nginx) +though this is not always the case (the standalone plugin is an authenticator +that listens on port 80, but it cannot install certificates; a postfix plugin +would be an installer but not an authenticator). + +Installers and Authenticators are kept separate because +it should be possible to use the `~.StandaloneAuthenticator` (it sets +up its own Python server to perform challenges) with a program that +cannot solve challenges itself (Such as MTA installers). + + +Installer Development +--------------------- + +There are a few existing classes that may be beneficial while +developing a new `~certbot.interfaces.Installer`. +Installers aimed to reconfigure UNIX servers may use Augeas for +configuration parsing and can inherit from `~.AugeasConfigurator` class +to handle much of the interface. Installers that are unable to use +Augeas may still find the `~.Reverter` class helpful in handling +configuration checkpoints and rollback. + + +.. _dev-plugin: + +Writing your own plugin +----------------------- + +.. note:: The Certbot team is not currently accepting any new plugins + because we want to rethink our approach to the challenge and resolve some + issues like `#6464 `_, + `#6503 `_, and `#6504 + `_ first. + + In the meantime, you're welcome to release it as a third-party plugin. See + `certbot-dns-ispconfig `_ + for one example of that. + +Certbot client supports dynamic discovery of plugins through the +`importlib.metadata entry points`_ using the `certbot.plugins` group. +This way you can, for example, create a custom implementation of +`~certbot.interfaces.Authenticator` or the +`~certbot.interfaces.Installer` without having to merge it +with the core upstream source code. An example is provided in +``examples/plugins/`` directory. + +While developing, you can install your plugin into a Certbot development +virtualenv like this: + +.. code-block:: shell + + . venv/bin/activate + pip install -e examples/plugins/ + certbot_test plugins + +Your plugin should show up in the output of the last command. If not, +it was not installed properly. + +Once you've finished your plugin and published it, you can have your +users install it system-wide with `pip install`. Note that this will +only work for users who have Certbot installed from OS packages or via +pip. + +.. _`importlib.metadata entry points`: + https://importlib-metadata.readthedocs.io/en/latest/using.html#entry-points + +Writing your own plugin snap +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you'd like your plugin to be used alongside the Certbot snap, you +will also have to publish your plugin as a snap. Plugin snaps are +regular confined snaps, but normally do not provide any "apps" +themselves. Plugin snaps export loadable Python modules to the Certbot +snap. + +When the Certbot snap runs, it will use its version of Python and prefer +Python modules contained in its own snap over modules contained in +external snaps. This means that your snap doesn't have to contain things +like an extra copy of Python, Certbot, or their dependencies, but also +that if you need a different version of a dependency than is already +installed in the Certbot snap, the Certbot snap will have to be updated. + +Certbot plugin snaps expose their Python modules to the Certbot snap via a +`snap content interface`_ where ``certbot-1`` is the value for the ``content`` +attribute. The Certbot snap only uses this to find the names of connected +plugin snaps and it expects to find the Python modules to be loaded under +``lib/python3.12/site-packages/`` in the plugin snap. This location is the +default when using the ``core24`` `base snap`_ and the `python snapcraft +plugin`_. + +The Certbot snap also provides a separate content interface which +you can use to get metadata about the Certbot snap using the ``content`` +identifier ``metadata-1``. + +The script used to generate the snapcraft.yaml files for our own externally +snapped plugins can be found at +https://github.com/certbot/certbot/blob/main/tools/snap/generate_dnsplugins_snapcraft.sh. + +For more information on building externally snapped plugins, see the section on +:ref:`Building snaps`. + +Once you have created your own snap, if you have the snap file locally, +it can be installed for use with Certbot by running: + +.. code-block:: shell + + snap install --classic certbot + snap set certbot trust-plugin-with-root=ok + snap install --dangerous your-snap-filename.snap + sudo snap connect certbot:plugin your-snap-name + sudo /snap/bin/certbot plugins + +If everything worked, the last command should list your plugin in the +list of plugins found by Certbot. Once your snap is published to the +snap store, it will be installable through the name of the snap on the +snap store without the ``--dangerous`` flag. If you are also using +Certbot's metadata interface, you can run ``sudo snap connect +your-snap-name:your-plug-name-for-metadata certbot:certbot-metadata`` to +connect your snap to it. + +.. _`snap content interface`: + https://snapcraft.io/docs/content-interface +.. _`base snap`: + https://snapcraft.io/docs/base-snaps +.. _`python snapcraft plugin`: + https://snapcraft.io/docs/python-plugin + +.. _coding-style: + +Coding style +============ + +Please: + +1. **Be consistent with the rest of the code**. + +2. Read `PEP 8 - Style Guide for Python Code`_. + +3. Follow the `Google Python Style Guide`_, with the exception that we + use `Sphinx-style`_ documentation:: + + def foo(arg): + """Short description. + + :param int arg: Some number. + + :returns: Argument + :rtype: int + + """ + return arg + +4. Remember to use ``pylint``. + +5. You may consider installing a plugin for `editorconfig`_ in + your editor to prevent some linting warnings. + +6. Please avoid `unittest.assertTrue` or `unittest.assertFalse` when + possible, and use `assertEqual` or more specific assert. They give + better messages when it's failing, and are generally more correct. + +.. _Google Python Style Guide: + https://google.github.io/styleguide/pyguide.html +.. _Sphinx-style: https://www.sphinx-doc.org/ +.. _PEP 8 - Style Guide for Python Code: + https://www.python.org/dev/peps/pep-0008 +.. _editorconfig: https://editorconfig.org/ + +Use ``certbot.compat.os`` instead of ``os`` +=========================================== + +Python's standard library ``os`` module lacks full support for several Windows +security features about file permissions (eg. DACLs). However several files +handled by Certbot (eg. private keys) need strongly restricted access +on both Linux and Windows. + +To help with this, the ``certbot.compat.os`` module wraps the standard +``os`` module, and forbids usage of methods that lack support for these Windows +security features. + +As a developer, when working on Certbot or its plugins, you must use ``certbot.compat.os`` +in every place you would need ``os`` (eg. ``from certbot.compat import os`` instead of +``import os``). Otherwise the tests will fail when your PR is submitted. + +.. _type annotations: + +Mypy type annotations +===================== + +Certbot uses the `mypy`_ static type checker. Python 3 natively supports official type annotations, +which can then be tested for consistency using mypy. Mypy does some type checks even without type +annotations; we can find bugs in Certbot even without a fully annotated codebase. + +Zulip wrote a `great guide`_ to using mypy. It’s useful, but you don’t have to read the whole thing +to start contributing to Certbot. + +To run mypy on Certbot, use ``tox run -e mypy`` on a machine that has Python 3 installed. + +Also note that OpenSSL, which we rely on, has type definitions for crypto but not SSL. We use both. +Those imports should look like this: + +.. code-block:: python + + from OpenSSL import crypto + from OpenSSL import SSL + +.. _mypy: https://mypy.readthedocs.io +.. _added in comments: https://mypy.readthedocs.io/en/latest/cheat_sheet.html +.. _great guide: https://blog.zulip.org/2016/10/13/static-types-in-python-oh-mypy/ + +Submitting a pull request +========================= + +Steps: + +0. We recommend you talk with us in a GitHub issue or :ref:`Mattermost ` before writing a pull request to ensure the changes you're making is + something we have the time and interest to review. +1. Write your code! When doing this, you should add :ref:`mypy type annotations + ` for any functions you add or modify. You can check that + you've done this correctly by running ``tox run -e mypy`` on a machine that has + Python 3 installed. +2. Make sure your environment is set up properly and that you're in your + virtualenv. You can do this by following the instructions in the + :ref:`Getting Started ` section. +3. Run ``tox run -e lint`` to check for pylint errors. Fix any errors. +4. Run ``tox --skip-missing-interpreters`` to run all the tests we recommend + developers run locally. The ``--skip-missing-interpreters`` argument ignores + missing versions of Python needed for running the tests. Fix any errors. +5. If any documentation should be added or updated as part of the changes you + have made, please include the documentation changes in your PR. +6. Submit the PR. Once your PR is open, please do not force push to the branch + containing your pull request to squash or amend commits. We use `squash + merges `_ on PRs and + rewriting commits makes changes harder to track between reviews. +7. Did your tests pass on GitHub Actions? If they didn't, fix any errors. + +.. _ask for help: + +Asking for help +=============== + +If you have any questions while working on a Certbot issue, don't hesitate to +ask for help! You can do this in the Certbot channel in EFF's Mattermost +instance for its open source projects as described below. + +You can get involved with several of EFF's software projects such as Certbot at +the `EFF Open Source Contributor Chat Platform +`_. +By signing up for the EFF Open Source Contributor Chat Platform, you consent to +share your personal information with the Electronic Frontier Foundation, which +is the operator and data controller for this platform. The channels will be +available both to EFF, and to other users of EFFOSCCP, who may use or disclose +information in these channels outside of EFFOSCCP. EFF will use your +information, according to the `Privacy Policy `_, +to further the mission of EFF, including hosting and moderating the discussions +on this platform. + +Use of EFFOSCCP is subject to the `EFF Code of Conduct +`_. When investigating an alleged Code of +Conduct violation, EFF may review discussion channels or direct messages. + +.. _Building snaps: + +Building the Certbot and DNS plugin snaps +========================================= + +Instructions for how to manually build and run the Certbot snap and the externally +snapped DNS plugins that the Certbot project supplies are located in the README +file at https://github.com/certbot/certbot/tree/main/tools/snap. + +Updating the documentation +========================== + +Many of the packages in the Certbot repository have documentation in a +``docs/`` directory. This directory is located under the top level directory +for the package. For instance, Certbot's documentation is under +``certbot/docs``. + +To build the documentation of a package, make sure you have followed the +instructions to set up a `local copy`_ of Certbot including activating the +virtual environment. After that, ``cd`` to the docs directory you want to build +and run the command: + +.. code-block:: shell + + make clean html + +This would generate the HTML documentation in ``_build/html`` in your current +``docs/`` directory. + +Certbot's dependencies +====================== + +We attempt to pin all of Certbot's dependencies whenever we can for reliability +and consistency. Some of the places we have Certbot's dependencies pinned +include our snaps, Docker images, CI, and our development environments. + +In most cases, the file where dependency versions are specified is +``tools/requirements.txt``. The one exception to this is our "oldest" tests +where ``tools/oldest_constraints.txt`` is used instead. The purpose of the +"oldest" tests is to ensure Certbot continues to work with the oldest versions +of our dependencies which we claim to support. The oldest versions of the +dependencies we support should also be declared in our setup.py files to +communicate this information to our users. + +The choices of whether Certbot's dependencies are pinned and what file is used +if they are should be automatically handled for you most of the time by +Certbot's tooling. The way it works though is ``tools/pip_install.py`` (which +many of our other tools build on) checks for the presence of environment +variables. If ``CERTBOT_OLDEST`` is set to 1, ``tools/oldest_constraints.txt`` +will be used as constraints for ``pip``, otherwise, ``tools/requirements.txt`` +is used as constraints. + +Updating dependency versions +---------------------------- + +``tools/requirements.txt`` and ``tools/oldest_constraints.txt`` can be updated +using ``tools/pinning/current/repin.sh`` and ``tools/pinning/oldest/repin.sh`` +respectively. This works by using ``poetry`` to generate pinnings based on a +Poetry project defined by the ``pyproject.toml`` file in the same directory as +the script. In many cases, you can just run the script to generate updated +dependencies, however, if you need to pin back packages or unpin packages that +were previously restricted to an older version, you will need to modify the +``pyproject.toml`` file. The syntax used by this file is described at +https://python-poetry.org/docs/pyproject/ and how dependencies are specified in +this file is further described at +https://python-poetry.org/docs/dependency-specification/. + +If you want to learn more about the design used here, see +``tools/pinning/DESIGN.md`` in the Certbot repo. + +Choosing dependency versions +---------------------------- + +When choosing dependency versions, we should choose whatever minimum versions +simplify development of Certbot and our own distribution methods such as snaps, +pip, and docker. Since these approaches have full access to PyPI, it's OK if +the required packages declared in ``setup.py`` are quite new. + +If this approach to development creates significant trouble for some of our users, we +can revisit this decision and weigh their trouble against the difficulties +involved in maintaining support for a wider range of package versions. When +doing this, we should also be sure to consider the feasibility of users getting +access to these newer packages on their system rather than changing our own +approach here. Their OS distribution may be able to package it, especially in +an alternate repository and/or for a different version of Python to help avoid +conflicts with other packages on their system. + +macOS suggestions +================= + +If you're developing on macOS, before :ref:`setting up your Certbot development +environment `, it is recommended you perform the following steps. +None of this is required, but it is the approach used by all/most of the +current Certbot developers on macOS as of writing this: + +0. Install `Homebrew `_. It is the most popular package + manager on macOS by a wide margin and works well enough. +1. Install `pyenv `_, ideally through Homebrew + by running ``brew install pyenv``. Using Homebrew's Python for Certbot + development is annoying because it regularly updates and every time it does + it breaks your virtual environments. Using Python from ``pyenv`` avoids this + problem and gives you easy access to all versions of Python. +2. If you're using ``pyenv``, make sure you've set up your shell for it by + following instructions like + https://github.com/pyenv/pyenv?tab=readme-ov-file#set-up-your-shell-environment-for-pyenv. diff --git a/certbot/docs/index.rst b/certbot/docs/index.rst new file mode 100644 index 00000000000..a7fc75c5b09 --- /dev/null +++ b/certbot/docs/index.rst @@ -0,0 +1,27 @@ +Welcome to the Certbot documentation! +================================================== + +.. toctree:: + :maxdepth: 2 + + intro + what + install + using + contributing + packaging + compatibility + resources + +.. toctree:: + :maxdepth: 1 + + api + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/certbot/docs/install.rst b/certbot/docs/install.rst new file mode 100644 index 00000000000..71f56b28503 --- /dev/null +++ b/certbot/docs/install.rst @@ -0,0 +1,129 @@ +===================== +Get Certbot +===================== + +.. contents:: Table of Contents + :local: + + +.. _system_requirements: + +System Requirements +------------------- +- Linux, macOS, BSD and Windows +- Recommended root access on Linux/BSD/Required Administrator access on Windows +- Port 80 Open + +.. Note:: Certbot is most useful when run with root privileges, because it is then able to automatically configure TLS/SSL for Apache and nginx. \ + + *Certbot is meant to be run directly on a web server*, normally by a system administrator. In most cases, running Certbot on your personal computer is not a useful option. The instructions below relate to installing and running Certbot on a server. + +Installation +------------ + +Unless you have very specific requirements, we kindly suggest that you use the installation instructions for your system found at https://certbot.eff.org/instructions. + +.. _snap-install: + +Snap (Recommended) +------------------ +Our instructions are the same across all systems that use Snap. You can find instructions for installing Certbot through Snap can be found at https://certbot.eff.org/instructions by selecting your server software and then choosing "snapd" in the "System" dropdown menu. + +Most modern Linux distributions (basically any that use systemd) can install Certbot packaged as a snap. Snaps are available for x86_64, ARMv7 and ARMv8 architectures. The Certbot snap provides an easy way to ensure you have the latest version of Certbot with features like automated certificate renewal preconfigured. + +If you unable to use snaps, you can use an alternate method for installing ``certbot``. + + +.. _docker-user: + +Alternative 1: Docker +--------------------- + +Docker_ is an amazingly simple and quick way to obtain a +certificate. However, this mode of operation is unable to install +certificates or configure your webserver, because our installer +plugins cannot reach your webserver from inside the Docker container. + +Most users should use the instructions at certbot.eff.org_. You should only use Docker if you are sure you know what you are doing and have a good reason to do so. + +You should definitely read the :ref:`where-certs` section, in order to +know how to manage the certificates +manually. `Our ciphersuites page `__ +provides some information about recommended ciphersuites. If none of +these make much sense to you, you should definitely use the installation method +recommended for your system at certbot.eff.org_, which enables you to use +installer plugins that cover both of those hard topics. + +If you're still not convinced and have decided to use this method, from +the server that the domain you're requesting a certificate for resolves +to, `install Docker`_, then issue a command like the one found below. If +you are using Certbot with the :ref:`Standalone` plugin, you will need +to make the port it uses accessible from outside of the container by +including something like ``-p 80:80`` or ``-p 443:443`` on the command +line before ``certbot/certbot``. + +.. code-block:: shell + + sudo docker run -it --rm --name certbot \ + -v "/etc/letsencrypt:/etc/letsencrypt" \ + -v "/var/lib/letsencrypt:/var/lib/letsencrypt" \ + certbot/certbot certonly + +Running Certbot with the ``certonly`` command will obtain a certificate and place it in the directory +``/etc/letsencrypt/live`` on your system. Because Certonly cannot install the certificate from +within Docker, you must install the certificate manually according to the procedure +recommended by the provider of your webserver. + +There are also Docker images for each of Certbot's DNS plugins available +at https://hub.docker.com/u/certbot which automate doing domain +validation over DNS for popular providers. To use one, just replace +``certbot/certbot`` in the command above with the name of the image you +want to use. For example, to use Certbot's plugin for Amazon Route 53, +you'd use ``certbot/dns-route53``. You may also need to add flags to +Certbot and/or mount additional directories to provide access to your +DNS API credentials as specified in the :ref:`DNS plugin documentation +`. + +For more information about the layout +of the ``/etc/letsencrypt`` directory, see :ref:`where-certs`. + +.. _Docker: https://docker.com +.. _`install Docker`: https://docs.docker.com/engine/installation/ +.. _certbot.eff.org: https://certbot.eff.org/instructions + + +.. _pip: + +Alternative 2: Pip +------------------ + +Installing Certbot through pip is only supported on a best effort basis and +when using a virtual environment. Instructions for installing Certbot through +pip can be found at https://certbot.eff.org/instructions by selecting your +server software and then choosing "pip" in the "System" dropdown menu. + + +.. _third-party: + +Alternative 3: Third Party Distributions +---------------------------------------- + +Third party distributions exist for other specific needs. They often are maintained +by these parties outside of Certbot and tend to rapidly fall out of date on LTS-style distributions. + + +.. _certbot-auto: + +Certbot-Auto [Deprecated] +------------------------- +.. toctree:: + :hidden: + +We used to have a shell script named ``certbot-auto`` to help people install +Certbot on UNIX operating systems, however, this script is no longer supported. + +Please remove ``certbot-auto``. To do so, you need to do three things: + +1. If you added a cron job or systemd timer to automatically run certbot-auto to renew your certificates, you should delete it. If you did this by following our instructions, you can delete the entry added to `/etc/crontab` by running a command like `sudo sed -i '/certbot-auto/d' /etc/crontab`. +2. Delete the certbot-auto script. If you placed it in `/usr/local/bin`` like we recommended, you can delete it by running `sudo rm /usr/local/bin/certbot-auto`. +3. Delete the Certbot installation created by certbot-auto by running `sudo rm -rf /opt/eff.org`. diff --git a/docs/intro.rst b/certbot/docs/intro.rst similarity index 100% rename from docs/intro.rst rename to certbot/docs/intro.rst diff --git a/certbot/docs/make.bat b/certbot/docs/make.bat new file mode 100644 index 00000000000..ac9e1d69f9d --- /dev/null +++ b/certbot/docs/make.bat @@ -0,0 +1,263 @@ +@ECHO OFF + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set BUILDDIR=_build +set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . +set I18NSPHINXOPTS=%SPHINXOPTS% . +if NOT "%PAPER%" == "" ( + set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% + set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% +) + +if "%1" == "" goto help + +if "%1" == "help" ( + :help + echo.Please use `make ^` where ^ is one of + echo. html to make standalone HTML files + echo. dirhtml to make HTML files named index.html in directories + echo. singlehtml to make a single large HTML file + echo. pickle to make pickle files + echo. json to make JSON files + echo. htmlhelp to make HTML files and a HTML help project + echo. qthelp to make HTML files and a qthelp project + echo. devhelp to make HTML files and a Devhelp project + echo. epub to make an epub + echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter + echo. text to make text files + echo. man to make manual pages + echo. texinfo to make Texinfo files + echo. gettext to make PO message catalogs + echo. changes to make an overview over all changed/added/deprecated items + echo. xml to make Docutils-native XML files + echo. pseudoxml to make pseudoxml-XML files for display purposes + echo. linkcheck to check all external links for integrity + echo. doctest to run all doctests embedded in the documentation if enabled + echo. coverage to run coverage check of the documentation if enabled + goto end +) + +if "%1" == "clean" ( + for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i + del /q /s %BUILDDIR%\* + goto end +) + + +REM Check if sphinx-build is available and fallback to Python version if any +%SPHINXBUILD% 2> nul +if errorlevel 9009 goto sphinx_python +goto sphinx_ok + +:sphinx_python + +set SPHINXBUILD=python -m sphinx.__init__ +%SPHINXBUILD% 2> nul +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.https://www.sphinx-doc.org/ + exit /b 1 +) + +:sphinx_ok + + +if "%1" == "html" ( + %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/html. + goto end +) + +if "%1" == "dirhtml" ( + %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. + goto end +) + +if "%1" == "singlehtml" ( + %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. + goto end +) + +if "%1" == "pickle" ( + %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the pickle files. + goto end +) + +if "%1" == "json" ( + %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can process the JSON files. + goto end +) + +if "%1" == "htmlhelp" ( + %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run HTML Help Workshop with the ^ +.hhp project file in %BUILDDIR%/htmlhelp. + goto end +) + +if "%1" == "qthelp" ( + %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; now you can run "qcollectiongenerator" with the ^ +.qhcp project file in %BUILDDIR%/qthelp, like this: + echo.^> qcollectiongenerator %BUILDDIR%\qthelp\LetsEncrypt.qhcp + echo.To view the help file: + echo.^> assistant -collectionFile %BUILDDIR%\qthelp\LetsEncrypt.ghc + goto end +) + +if "%1" == "devhelp" ( + %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. + goto end +) + +if "%1" == "epub" ( + %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The epub file is in %BUILDDIR%/epub. + goto end +) + +if "%1" == "latex" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + if errorlevel 1 exit /b 1 + echo. + echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "latexpdf" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + cd %BUILDDIR%/latex + make all-pdf + cd %~dp0 + echo. + echo.Build finished; the PDF files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "latexpdfja" ( + %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex + cd %BUILDDIR%/latex + make all-pdf-ja + cd %~dp0 + echo. + echo.Build finished; the PDF files are in %BUILDDIR%/latex. + goto end +) + +if "%1" == "text" ( + %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The text files are in %BUILDDIR%/text. + goto end +) + +if "%1" == "man" ( + %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The manual pages are in %BUILDDIR%/man. + goto end +) + +if "%1" == "texinfo" ( + %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. + goto end +) + +if "%1" == "gettext" ( + %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The message catalogs are in %BUILDDIR%/locale. + goto end +) + +if "%1" == "changes" ( + %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes + if errorlevel 1 exit /b 1 + echo. + echo.The overview file is in %BUILDDIR%/changes. + goto end +) + +if "%1" == "linkcheck" ( + %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck + if errorlevel 1 exit /b 1 + echo. + echo.Link check complete; look for any errors in the above output ^ +or in %BUILDDIR%/linkcheck/output.txt. + goto end +) + +if "%1" == "doctest" ( + %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest + if errorlevel 1 exit /b 1 + echo. + echo.Testing of doctests in the sources finished, look at the ^ +results in %BUILDDIR%/doctest/output.txt. + goto end +) + +if "%1" == "coverage" ( + %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage + if errorlevel 1 exit /b 1 + echo. + echo.Testing of coverage in the sources finished, look at the ^ +results in %BUILDDIR%/coverage/python.txt. + goto end +) + +if "%1" == "xml" ( + %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The XML files are in %BUILDDIR%/xml. + goto end +) + +if "%1" == "pseudoxml" ( + %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml + if errorlevel 1 exit /b 1 + echo. + echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. + goto end +) + +:end diff --git a/certbot/docs/man/certbot.rst b/certbot/docs/man/certbot.rst new file mode 100644 index 00000000000..cc690d2da1d --- /dev/null +++ b/certbot/docs/man/certbot.rst @@ -0,0 +1,23 @@ +:orphan: + +======= +certbot +======= + +Synopsis +======== +The objective of Certbot, Let's Encrypt, and the ACME (Automated Certificate Management +Environment) protocol is to make it possible to set up an HTTPS server and have it automatically +obtain a browser-trusted certificate, without any human intervention. This is accomplished by +running a certificate management agent on the web server. + +This agent is used to: + +- Automatically prove to the Let's Encrypt CA that you control the website +- Obtain a browser-trusted certificate and set it up on your web server +- Keep track of when your certificate is going to expire, and renew it +- Help you revoke the certificate if that ever becomes necessary. + +Options +======= +.. literalinclude:: ../cli-help.txt \ No newline at end of file diff --git a/certbot/docs/packaging.rst b/certbot/docs/packaging.rst new file mode 100644 index 00000000000..ecf66c8a03c --- /dev/null +++ b/certbot/docs/packaging.rst @@ -0,0 +1,66 @@ +=============== +Packaging Guide +=============== + +Releases +======== + +We release packages and upload them to PyPI (wheels and source tarballs). + +- https://pypi.org/project/acme/ +- https://pypi.org/project/certbot/ +- https://pypi.org/project/certbot-apache/ +- https://pypi.org/project/certbot-nginx/ +- https://pypi.org/project/certbot-dns-cloudflare/ +- https://pypi.org/project/certbot-dns-digitalocean/ +- https://pypi.org/project/certbot-dns-dnsimple/ +- https://pypi.org/project/certbot-dns-dnsmadeeasy/ +- https://pypi.org/project/certbot-dns-google/ +- https://pypi.org/project/certbot-dns-linode/ +- https://pypi.org/project/certbot-dns-luadns/ +- https://pypi.org/project/certbot-dns-nsone/ +- https://pypi.org/project/certbot-dns-ovh/ +- https://pypi.org/project/certbot-dns-rfc2136/ +- https://pypi.org/project/certbot-dns-route53/ + +The following scripts are used in the process: + +- https://github.com/certbot/certbot/blob/main/tools/release.sh + +We use git tags to identify releases, using `Semantic Versioning`_. For +example: `v0.11.1`. + +.. _`Semantic Versioning`: https://semver.org/ + +Since version 1.21.0, our packages are cryptographically signed by one of four +PGP keys: + +- ``BF6BCFC89E90747B9A680FD7B6029E8500F7DB16`` +- ``86379B4F0AF371B50CD9E5FF3402831161D1D280`` +- ``20F201346BF8F3F455A73F9A780CC99432A28621`` +- ``F2871B4152AE13C49519111F447BF683AA3B26C3``` + +These keys can be found on major key servers. + +Releases before 1.21.0 were signed by the PGP key +``A2CFB51FA275A7286234E7B24D17C995CD9775F2`` which can still be found on major +key servers. + +Notes for package maintainers +============================= + +0. Please use our tagged releases, not ``main``! + +1. Do not package ``certbot-compatibility-test`` as it's only used internally. + +2. To run tests on our packages, you should use pytest by running the command ``python -m pytest``. Running ``pytest`` directly may not work because PYTHONPATH is not handled the same way and local modules may not be found by the test runner. + +3. If you'd like to include automated renewal in your package: + + - ``certbot renew -q`` should be added to crontab or systemd timer. + - A random per-machine time offset should be included to avoid having a large number of your clients hit Let's Encrypt's servers simultaneously. + - ``--preconfigured-renewal`` should be included on the CLI or in ``cli.ini`` for all invocations of Certbot, so that it can adjust its interactive output regarding automated renewal (Certbot >= 1.9.0). + +4. ``jws`` is an internal script for ``acme`` module and it doesn't have to be packaged - it's mostly for debugging: you can use it as ``echo foo | jws sign | jws verify``. + +5. Do get in touch with us. We are happy to make any changes that will make packaging easier. If you need to apply some patches don't do it downstream - make a PR here. diff --git a/docs/resources.rst b/certbot/docs/resources.rst similarity index 100% rename from docs/resources.rst rename to certbot/docs/resources.rst diff --git a/certbot/docs/using.rst b/certbot/docs/using.rst new file mode 100644 index 00000000000..9350b0f0436 --- /dev/null +++ b/certbot/docs/using.rst @@ -0,0 +1,1329 @@ +========== +User Guide +========== + +.. contents:: Table of Contents + :local: + +Certbot Commands +================ + +Certbot uses a number of different commands (also referred +to as "subcommands") to request specific actions such as +obtaining, renewing, or revoking certificates. The most important +and commonly-used commands will be discussed throughout this +document; an exhaustive list also appears near the end of the document. + +The ``certbot`` script on your web server might be named ``letsencrypt`` if your system uses an older package. Throughout the docs, whenever you see ``certbot``, swap in the correct name as needed. + +.. _plugins: + +Getting certificates (and choosing plugins) +=========================================== + +Certbot helps you achieve two tasks: + +1. Obtaining a certificate: automatically performing the required authentication steps to prove that you control the domain(s), + saving the certificate to ``/etc/letsencrypt/live/`` and renewing it on a regular schedule. +2. Optionally, installing that certificate to supported web servers (like Apache or nginx) and other kinds of servers. This is + done by automatically modifying the configuration of your server in order to use the certificate. + +To obtain a certificate and also install it, use the ``certbot run`` command (or ``certbot``, which is the same). + +To just obtain the certificate without installing it anywhere, the ``certbot certonly`` ("certificate only") command can be used. + +Some example ways to use Certbot:: + + # Obtain and install a certificate: + certbot + + # Obtain a certificate but don't install it: + certbot certonly + + # You may specify multiple domains with -d and obtain and + # install different certificates by running Certbot multiple times: + certbot certonly -d example.com -d www.example.com + certbot certonly -d app.example.com -d api.example.com + +To perform these tasks, Certbot will ask you to choose from a selection of authenticator and installer plugins. The appropriate +choice of plugins will depend on what kind of server software you are running and plan to use your certificates with. + +**Authenticators** are plugins which automatically perform the required steps to prove that you control the domain names you're trying +to request a certificate for. An authenticator is always required to obtain a certificate. + +**Installers** are plugins which can automatically modify your web server's configuration to serve your website over HTTPS, using the +certificates obtained by Certbot. An installer is only required if you want Certbot to install the certificate to your web server. + +Some plugins are both authenticators and installers and it is possible to specify a distinct combination_ of authenticator and plugin. + +=========== ==== ==== =============================================================== ============================= +Plugin Auth Inst Notes Challenge types (and port) +=========== ==== ==== =============================================================== ============================= +apache_ Y Y | Automates obtaining and installing a certificate with Apache. http-01_ (80) +nginx_ Y Y | Automates obtaining and installing a certificate with Nginx. http-01_ (80) +webroot_ Y N | Obtains a certificate by writing to the webroot directory of http-01_ (80) + | an already running webserver. +standalone_ Y N | Uses a "standalone" webserver to obtain a certificate. http-01_ (80) + | Requires port 80 to be available. This is useful on + | systems with no webserver, or when direct integration with + | the local webserver is not supported or not desired. +|dns_plugs| Y N | This category of plugins automates obtaining a certificate by dns-01_ (53) + | modifying DNS records to prove you have control over a + | domain. Doing domain validation in this way is + | the only way to obtain wildcard certificates from Let's + | Encrypt. +manual_ Y N | Obtain a certificate by manually following instructions to http-01_ (80) or + | perform domain validation yourself. Certificates created this dns-01_ (53) + | way do not support autorenewal. + | Autorenewal may be enabled by providing an authentication + | hook script to automate the domain validation steps. +=========== ==== ==== =============================================================== ============================= + +.. |dns_plugs| replace:: :ref:`DNS plugins ` + +Under the hood, plugins use one of several ACME protocol challenges_ to +prove you control a domain. The options are http-01_ (which uses port 80) +and dns-01_ (requiring configuration of a DNS server on +port 53, though that's often not the same machine as your webserver). A few +plugins support more than one challenge type, in which case you can choose one +with ``--preferred-challenges``. + +There are also many third-party-plugins_ available. Below we describe in more detail +the circumstances in which each plugin can be used, and how to use it. + +.. _challenges: https://datatracker.ietf.org/doc/html/rfc8555#section-8 +.. _http-01: https://datatracker.ietf.org/doc/html/rfc8555#section-8.3 +.. _dns-01: https://datatracker.ietf.org/doc/html/rfc8555#section-8.4 + +Apache +------ + +The Apache plugin currently `supports +`_ +modern OSes based on Debian, Fedora, SUSE, Gentoo, CentOS and Darwin. +This automates both obtaining *and* installing certificates on an Apache +webserver. To specify this plugin on the command line, simply include +``--apache``. + +Webroot +------- + +If you're running a local webserver for which you have the ability +to modify the content being served, and you'd prefer not to stop the +webserver during the certificate issuance process, you can use the webroot +plugin to obtain a certificate by including ``certonly`` and ``--webroot`` on +the command line. In addition, you'll need to specify ``--webroot-path`` +or ``-w`` with the top-level directory ("web root") containing the files +served by your webserver. For example, ``--webroot-path /var/www/html`` +or ``--webroot-path /usr/share/nginx/html`` are two common webroot paths. + +If you're getting a certificate for many domains at once, the plugin +needs to know where each domain's files are served from, which could +potentially be a separate directory for each domain. When requesting a +certificate for multiple domains, each domain will use the most recently +specified ``--webroot-path``. So, for instance, + +:: + + certbot certonly --webroot -w /var/www/example -d www.example.com -d example.com -w /var/www/other -d other.example.net -d another.other.example.net + +would obtain a single certificate for all of those names, using the +``/var/www/example`` webroot directory for the first two, and +``/var/www/other`` for the second two. + +The webroot plugin works by creating a temporary file for each of your requested +domains in ``${webroot-path}/.well-known/acme-challenge``. Then the Let's Encrypt +validation server makes HTTP requests to validate that the DNS for each +requested domain resolves to the server running certbot. An example request +made to your web server would look like: + +:: + + 66.133.109.36 - - [05/Jan/2016:20:11:24 -0500] "GET /.well-known/acme-challenge/HGr8U1IeTW4kY_Z6UIyaakzOkyQgPr_7ArlLgtZE8SX HTTP/1.1" 200 87 "-" "Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)" + +Note that to use the webroot plugin, your server must be configured to serve +files from hidden directories. If ``/.well-known`` is treated specially by +your webserver configuration, you might need to modify the configuration +to ensure that files inside ``/.well-known/acme-challenge`` are served by +the webserver. + +Under Windows, Certbot will generate a ``web.config`` file, if one does not already exist, +in ``/.well-known/acme-challenge`` in order to let IIS serve the challenge files even if they +do not have an extension. + +Nginx +----- + +The Nginx plugin should work for most configurations. We recommend backing up +Nginx configurations before using it (though you can also revert changes to +configurations with ``certbot --nginx rollback``). You can use it by providing +the ``--nginx`` flag on the commandline. + +:: + + certbot --nginx + +.. _standalone: + +Standalone +---------- + +Use standalone mode to obtain a certificate if you don't want to use (or don't currently have) +existing server software. The standalone plugin does not rely on any other server +software running on the machine where you obtain the certificate. + +To obtain a certificate using a "standalone" webserver, you can use the +standalone plugin by including ``certonly`` and ``--standalone`` +on the command line. This plugin needs to bind to port 80 in +order to perform domain validation, so you may need to stop your +existing webserver. + +It must still be possible for your machine to accept inbound connections from +the Internet on the specified port using each requested domain name. + +By default, Certbot first attempts to bind to the port for all interfaces using +IPv6 and then bind to that port using IPv4; Certbot continues so long as at +least one bind succeeds. On most Linux systems, IPv4 traffic will be routed to +the bound IPv6 port and the failure during the second bind is expected. + +Use ``---address`` to explicitly tell Certbot which interface +(and protocol) to bind. + +.. _dns_plugins: + +DNS Plugins +----------- + +If you'd like to obtain a wildcard certificate from Let's Encrypt or run +``certbot`` on a machine other than your target webserver, you can use one of +Certbot's DNS plugins. + +These plugins are not included in a default Certbot installation and must be +installed separately. They are available in many OS package managers, as Docker +images, and as snaps. Visit https://certbot.eff.org to learn the best way to +use the DNS plugins on your system. + +Once installed, you can find documentation on how to use each plugin at: + +* `certbot-dns-cloudflare `_ +* `certbot-dns-digitalocean `_ +* `certbot-dns-dnsimple `_ +* `certbot-dns-dnsmadeeasy `_ +* `certbot-dns-gehirn `_ +* `certbot-dns-google `_ +* `certbot-dns-linode `_ +* `certbot-dns-luadns `_ +* `certbot-dns-nsone `_ +* `certbot-dns-ovh `_ +* `certbot-dns-rfc2136 `_ +* `certbot-dns-route53 `_ +* `certbot-dns-sakuracloud `_ + +Manual +------ + +If you'd like to obtain a certificate running ``certbot`` on a machine +other than your target webserver or perform the steps for domain +validation yourself, you can use the manual plugin. While hidden from +the UI, you can use the plugin to obtain a certificate by specifying +``certonly`` and ``--manual`` on the command line. This requires you +to copy and paste commands into another terminal session, which may +be on a different computer. + +The manual plugin can use either the ``http`` or the ``dns`` challenge. You can use the ``--preferred-challenges`` option +to choose the challenge of your preference. + +The ``http`` challenge will ask you to place a file with a specific name and +specific content in the ``/.well-known/acme-challenge/`` directory directly +in the top-level directory (“web rootâ€) containing the files served by your +webserver. In essence it's the same as the webroot_ plugin, but not automated. + +When using the ``dns`` challenge, ``certbot`` will ask you to place a TXT DNS +record with specific contents under the domain name consisting of the hostname +for which you want a certificate issued, prepended by ``_acme-challenge``. + +For example, for the domain ``example.com``, a zone file entry would look like: + +:: + + _acme-challenge.example.com. 300 IN TXT "gfj9Xq...Rg85nM" + +.. _manual-renewal: + +**Renewal with the manual plugin** + +Certificates created using ``--manual`` **do not** support automatic renewal unless +combined with an `authentication hook script <#hooks>`_ via ``--manual-auth-hook`` +to automatically set up the required HTTP and/or TXT challenges. + +If you can use one of the other plugins_ which support autorenewal to create +your certificate, doing so is highly recommended. + +To manually renew a certificate using ``--manual`` without hooks, repeat the same +``certbot --manual`` command you used to create the certificate originally. As this +will require you to copy and paste new HTTP files or DNS TXT records, the command +cannot be automated with a cron job. + +.. _combination: + +Combining plugins +----------------- + +Sometimes you may want to specify a combination of distinct authenticator and +installer plugins. To do so, specify the authenticator plugin with +``--authenticator`` or ``-a`` and the installer plugin with ``--installer`` or +``-i``. + +For instance, you could create a certificate using the webroot_ plugin +for authentication and the apache_ plugin for installation. + +:: + + certbot run -a webroot -i apache -w /var/www/html -d example.com + +Or you could create a certificate using the manual_ plugin for authentication +and the nginx_ plugin for installation. (Note that this certificate cannot +be renewed automatically.) + +:: + + certbot run -a manual -i nginx -d example.com + +.. _third-party-plugins: + +Third-party plugins +------------------- + +There are also a number of third-party plugins for the client, provided by +other developers. Many are beta/experimental, but some are already in +widespread use: + +======================= ==== ==== ================================================================= +Plugin Auth Inst Notes +======================= ==== ==== ================================================================= +haproxy_ Y Y Integration with the HAProxy load balancer +s3front_ Y Y Integration with Amazon CloudFront distribution of S3 buckets +gandi_ Y N Obtain certificates via the Gandi LiveDNS API +varnish_ Y N Obtain certificates via a Varnish server +external-auth_ Y Y A plugin for convenient scripting +pritunl_ N Y Install certificates in pritunl distributed OpenVPN servers +proxmox_ N Y Install certificates in Proxmox Virtualization servers +dns-standalone_ Y N Obtain certificates via an integrated DNS server +dns-ispconfig_ Y N DNS Authentication using ISPConfig as DNS server +dns-cloudns_ Y N DNS Authentication using ClouDNS API +dns-clouddns_ Y N DNS Authentication using CloudDNS API +dns-lightsail_ Y N DNS Authentication using Amazon Lightsail DNS API +dns-inwx_ Y Y DNS Authentication for INWX through the XML API +dns-azure_ Y N DNS Authentication using Azure DNS +dns-godaddy_ Y N DNS Authentication using Godaddy DNS +dns-yandexcloud_ Y N DNS Authentication using Yandex Cloud DNS +dns-bunny_ Y N DNS Authentication using BunnyDNS +njalla_ Y N DNS Authentication for njalla +DuckDNS_ Y N DNS Authentication for DuckDNS +Porkbun_ Y N DNS Authentication for Porkbun +Infomaniak_ Y N DNS Authentication using Infomaniak Domains API +dns-multi_ Y N DNS authentication of 100+ providers using go-acme/lego +dns-dnsmanager_ Y N DNS Authentication for dnsmanager.io +standalone-nfq_ Y N HTTP Authentication that works with any webserver (Linux only) +dns-solidserver_ Y N DNS Authentication using SOLIDserver (EfficientIP) +dns-stackit_ Y N DNS Authentication using STACKIT DNS +dns-ionos_ Y N DNS Authentication using IONOS Cloud DNS +dns-mijn-host_ Y N DNS Authentication using mijn.host DNS +nginx-unit_ Y Y Automates obtaining and installing a certificate with Nginx Unit +dns-cdmon_ Y N DNS Authentication using cdmon's API +dns-synergy-wholesale_ Y N DNS Authentication using Synergy Wholesale DNS +pkcs12_ N Y Install certificates as PKCS#12 archives +dns-hetzner-cloud_ Y N DNS Authentication for Hetzner Cloud DNS +dns-czechia_ Y N DNS Authentication for czechia.com +dns-eurodns_ Y N DNS Authentication for EuroDNS +dns-dnscale_ Y N DNS Authenticator for DNScale +======================= ==== ==== ================================================================= + +.. _haproxy: https://github.com/greenhost/certbot-haproxy +.. _s3front: https://github.com/dlapiduz/letsencrypt-s3front +.. _gandi: https://github.com/obynio/certbot-plugin-gandi +.. _varnish: https://git.sesse.net/?p=letsencrypt-varnish-plugin +.. _pritunl: https://github.com/kharkevich/letsencrypt-pritunl +.. _proxmox: https://github.com/kharkevich/letsencrypt-proxmox +.. _external-auth: https://github.com/EnigmaBridge/certbot-external-auth +.. _dns-standalone: https://github.com/siilike/certbot-dns-standalone +.. _dns-ispconfig: https://github.com/m42e/certbot-dns-ispconfig +.. _dns-cloudns: https://github.com/inventage/certbot-dns-cloudns +.. _dns-clouddns: https://github.com/vshosting/certbot-dns-clouddns +.. _dns-lightsail: https://github.com/noi/certbot-dns-lightsail +.. _dns-inwx: https://github.com/oGGy990/certbot-dns-inwx/ +.. _dns-azure: https://github.com/terricain/certbot-dns-azure +.. _dns-godaddy: https://github.com/miigotu/certbot-dns-godaddy +.. _dns-yandexcloud: https://github.com/PykupeJIbc/certbot-dns-yandexcloud +.. _dns-bunny: https://github.com/mwt/certbot-dns-bunny +.. _njalla: https://github.com/chaptergy/certbot-dns-njalla +.. _DuckDNS: https://github.com/infinityofspace/certbot_dns_duckdns +.. _Porkbun: https://github.com/infinityofspace/certbot_dns_porkbun +.. _Infomaniak: https://github.com/Infomaniak/certbot-dns-infomaniak +.. _dns-multi: https://github.com/alexzorin/certbot-dns-multi +.. _dns-dnsmanager: https://github.com/stayallive/certbot-dns-dnsmanager +.. _standalone-nfq: https://github.com/alexzorin/certbot-standalone-nfq +.. _dns-solidserver: https://gitlab.com/charlyhong/certbot-dns-solidserver +.. _dns-stackit: https://github.com/stackitcloud/certbot-dns-stackit +.. _dns-ionos: https://github.com/ionos-cloud/certbot-dns-ionos-cloud +.. _dns-mijn-host: https://github.com/mijnhost/certbot-dns-mijn-host +.. _nginx-unit: https://github.com/kea/certbot-nginx-unit +.. _dns-cdmon: https://github.com/rascazzione/certbot-dns-cdmon +.. _dns-synergy-wholesale: https://github.com/ALameLlama/certbot-dns-synergy-wholesale +.. _pkcs12: https://github.com/nasa-gcn/certbot-pkcs12 +.. _dns-hetzner-cloud: https://github.com/rolschewsky/certbot-dns-hetzner-cloud +.. _dns-czechia: https://github.com/CZECHIA-COM/certbot-dns-czechia +.. _dns-eurodns: https://pypi.org/project/certbot-dns-eurodns/ +.. _dns-dnscale: https://github.com/dnscaleou/certbot-dns-dnscale + +If you're interested, you can also :ref:`write your own plugin `. + +.. _managing-certs: + +Managing certificates +===================== + +To view a list of the certificates Certbot knows about, run +the ``certificates`` subcommand: + +``certbot certificates`` + +This returns information in the following format:: + + Found the following certificates: + Certificate Name: example.com + Domains: example.com, www.example.com + Expiry Date: 2017-02-19 19:53:00+00:00 (VALID: 30 days) + Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem + Key Type: RSA + Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem + +``Certificate Name`` shows the name of the certificate. Pass this name +using the ``--cert-name`` flag to specify a particular certificate for the ``run``, +``certonly``, ``certificates``, ``renew``, and ``delete`` commands. The certificate +name cannot contain filepath separators (i.e. '/' or '\\', depending on the platform). +Example:: + + certbot certonly --cert-name example.com + +.. _updating_certs: + +Re-creating and Updating Existing Certificates +---------------------------------------------- + +You can use ``certonly`` or ``run`` subcommands to request +the creation of a single new certificate even if you already have an +existing certificate with some of the same domain names. + +If a certificate is requested with ``run`` or ``certonly`` specifying a +certificate name that already exists, Certbot updates +the existing certificate. Otherwise a new certificate +is created and assigned the specified name. + +The ``--force-renewal``, ``--duplicate``, and ``--expand`` options +control Certbot's behavior when re-creating +a certificate with the same name as an existing certificate. +If you don't specify a requested behavior, Certbot may ask you what you intended. + +``--force-renewal`` tells Certbot to request a new certificate +with the same domains as an existing certificate. Each domain +must be explicitly specified via ``-d``. If successful, this certificate +is saved alongside the earlier one and symbolic links (the "``live``" +reference) will be updated to point to the new certificate. This is a +valid method of renewing a specific individual +certificate. + +``--duplicate`` tells Certbot to create a separate, unrelated certificate +with the same domains as an existing certificate. This certificate is +saved completely separately from the prior one. Most users will not +need to issue this command in normal circumstances. + +``--expand`` tells Certbot to update an existing certificate with a new +certificate that contains all of the old domains and one or more additional +new domains. With the ``--expand`` option, use the ``-d`` option to specify +all existing domains and one or more new domains. + +Example: + +.. code-block:: none + + certbot --expand -d existing.com,example.com,newdomain.com + +If you prefer, you can specify the domains individually like this: + +.. code-block:: none + + certbot --expand -d existing.com -d example.com -d newdomain.com + +Consider using ``--cert-name`` instead of ``--expand``, as it gives more control +over which certificate is modified and it lets you remove domains as well as adding them. + +``--allow-subset-of-names`` tells Certbot to continue with certificate generation if +only some of the specified domain authorizations can be obtained. This may +be useful if some domains specified in a certificate no longer point at this +system. + +Whenever you obtain a new certificate in any of these ways, the new +certificate exists alongside any previously obtained certificates, whether +or not the previous certificates have expired. The generation of a new +certificate counts against several rate limits that are intended to prevent +abuse of the ACME protocol, as described +`here `__. + +.. _changing: + +Changing a Certificate's Domains +-------------------------------- + +The ``--cert-name`` flag can also be used to modify the domains a certificate contains, +by specifying new domains using the ``-d`` or ``--domains`` flag. If certificate ``example.com`` +previously contained ``example.com`` and ``www.example.com``, it can be modified to only +contain ``example.com`` by specifying only ``example.com`` with the ``-d`` or ``--domains`` flag. Example:: + + certbot certonly --cert-name example.com -d example.com + +The same format can be used to expand the set of domains a certificate contains, or to +replace that set entirely:: + + certbot certonly --cert-name example.com -d example.org,www.example.org + +.. _using-ecdsa-keys: + +RSA and ECDSA keys +------------------------ + +Certbot supports two certificate private key algorithms: ``rsa`` and ``ecdsa``. + +As of version 2.0.0, Certbot defaults to ECDSA ``secp256r1`` (P-256) certificate private keys +for all new certificates. Existing certificates will continue to renew using their existing key +type, unless a key type change is requested. + +The type of key used by Certbot can be controlled through the ``--key-type`` option. +You can use the ``--elliptic-curve`` option to control the curve used in ECDSA +certificates and the ``--rsa-key-size`` option to control the size of RSA keys. + +.. warning:: If you obtain certificates using ECDSA keys, you should be careful + not to downgrade to a Certbot version earlier than 1.10.0 where ECDSA keys were + not supported. Downgrades like this are possible if you switch from something like + the snaps or pip to packages provided by your operating system which often lag behind. + +Changing a certificate's key type +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Unless you are aware that you need to support very old HTTPS clients that are +not supported by most sites, you can safely transition your site to use +ECDSA keys instead of RSA keys. + +If you want to change a single certificate to use ECDSA keys, you'll need to +create or renew a certificate while setting ``--key-type ecdsa`` on the command line: + +.. code-block:: shell + + certbot renew --key-type ecdsa --cert-name example.com --force-renewal + +If you want to use ECDSA keys for all certificates in the future (including renewals +of existing certificates), you can add the following line to Certbot's +:ref:`configuration file `: + +.. code-block:: ini + + key-type = ecdsa + +which will take effect upon the next renewal of each certificate. + +Revoking certificates +--------------------- + +If you need to revoke a certificate, use the ``revoke`` subcommand to do so. + +A certificate may be revoked by providing its name (see ``certbot certificates``) or by providing +its path directly:: + + certbot revoke --cert-name example.com + + certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem + +If the certificate being revoked was obtained via the ``--staging``, ``--test-cert`` or a non-default ``--server`` flag, +that flag must be passed to the ``revoke`` subcommand. + +.. note:: After revocation, Certbot will (by default) ask whether you want to **delete** the certificate. + Unless deleted, Certbot will try to renew revoked certificates the next time ``certbot renew`` runs. + +You can also specify the reason for revoking your certificate by using the ``reason`` flag. +Reasons include ``unspecified`` which is the default, as well as ``keycompromise``, +``affiliationchanged``, ``superseded``, and ``cessationofoperation``:: + + certbot revoke --cert-name example.com --reason keycompromise + +Revoking by account key or certificate private key +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +By default, Certbot will try revoke the certificate using your ACME account key. If the certificate was created from +the same ACME account, the revocation will be successful. + +If you instead have the corresponding private key file to the certificate you wish to revoke, use ``--key-path`` to perform the +revocation from any ACME account:: + + certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem --key-path /etc/letsencrypt/live/example.com/privkey.pem + +.. _deleting: + +Deleting certificates +--------------------- + +If you need to delete a certificate, use the ``delete`` subcommand. + +.. note:: Read this and the `Safely deleting certificates`_ sections carefully. This is an irreversible operation and must + be done with care. + +Certbot does not automatically revoke a certificate before deleting it. If you're no longer using a certificate and don't +plan to use it anywhere else, you may want to follow the instructions in `Revoking certificates`_ instead. Generally, there's +no need to revoke a certificate if its private key has not been compromised, but you may still receive expiration emails +from Let's Encrypt unless you revoke. + +.. note:: Do not manually delete certificate files from inside ``/etc/letsencrypt/``. Always use the ``delete`` subcommand. + +A certificate may be deleted by providing its name with ``--cert-name``. \ +You may find its name using ``certbot certificates``. + +Otherwise, you will be prompted to choose one or more +certificates to delete:: + + certbot delete --cert-name example.com + # or to choose from a list: + certbot delete + +Safely deleting certificates +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Deleting a certificate without following the proper steps can result in a non-functioning server. To safely delete a +certificate, follow all the steps below to make sure that references to a certificate are removed from the configuration +of any installed server software (Apache, nginx, Postfix, etc) *before* deleting the certificate. + +To explain further, when installing a certificate, Certbot modifies Apache or nginx's configuration to load the certificate +and its private key from the ``/etc/letsencrypt/live/`` directory. Before deleting a certificate, it is necessary to undo +that modification, by removing any references to the certificate from the webserver's configuration files. + +Follow these steps to safely delete a certificate: + +1. Find all references to the certificate (substitute ``example.com`` in the command for the name of the certificate + you wish to delete):: + + sudo bash -c 'grep -R live/example.com /etc/{nginx,httpd,apache2}' + + If there are no references found, skip directly to Step 4. + + If some references are found, they will look something like:: + + /etc/apache2/sites-available/000-default-le-ssl.conf:SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem + /etc/apache2/sites-available/000-default-le-ssl.conf:SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem + +2. You will need a self-signed certificate to replace the certificate you are deleting. The following command will generate one + for you, saving the certificate at ``/etc/letsencrypt/self-signed-cert.pem`` and its private key at + ``/etc/letsencrypt/self-signed-privkey.pem``:: + + sudo openssl req -nodes -batch -x509 -newkey rsa:2048 -keyout /etc/letsencrypt/self-signed-privkey.pem -out /etc/letsencrypt/self-signed-cert.pem -days 356 + +3. For each reference found in Step 1, open the file in a text editor and replace the reference to the existing + certificate with a reference to the self-signed certificate. + + Continuing from the previous example, you would open ``/etc/apache2/sites-available/000-default-le-ssl.conf`` in a text editor + and modify the two matching lines of text to instead say:: + + SSLCertificateFile /etc/letsencrypt/self-signed-cert.pem + SSLCertificateKeyFile /etc/letsencrypt/self-signed-privkey.pem + +4. It is now safe to delete the certificate. Do so by running:: + + sudo certbot delete --cert-name example.com + +.. _renewal: + +Renewing certificates +--------------------- + +.. seealso:: Most Certbot installations come with automatic + renewal out of the box. See `Automated Renewals`_ for more details. + +.. seealso:: Users of the `Manual`_ plugin should note that ``--manual`` certificates + will not renew automatically, unless combined with authentication hook scripts. + See `Renewal with the manual plugin <#manual-renewal>`_. + +Certbot supports a ``renew`` action to check all installed certificates for +impending expiry and attempt to renew them. The simplest form is simply + +``certbot renew`` + +This command attempts to renew any previously-obtained certificates which are ready +for renewal. As of Certbot 4.0.0, a certificate is considered ready for renewal +when less than 1/3rd of its lifetime remains. For certificates with a lifetime +of 10 days or less, that threshold is 1/2 of the lifetime. Prior to Certbot 4.0.0 +the threshold was a fixed 30 days. + +The same plugin and options that were used +at the time the certificate was originally issued will be used for the +renewal attempt, unless you specify other plugins or options. Unlike ``certonly``, ``renew`` acts on +multiple certificates and always takes into account whether each one is near +expiry. Because of this, ``renew`` is suitable (and designed) for automated use, +to allow your system to automatically renew each certificate when appropriate. +Since ``renew`` only renews certificates that are near expiry it can be +run as frequently as you want - since it will usually take no action. + +The ``renew`` command includes hooks for running commands or scripts before or after a certificate is +renewed. For example, if you have a single certificate obtained using +the standalone_ plugin, you might need to stop the webserver +before renewing so standalone can bind to the necessary ports, and +then restart it after the plugin is finished. Example:: + + certbot renew --pre-hook "service nginx stop" --post-hook "service nginx start" + +If a hook exits with a non-zero exit code, the error will be printed +to ``stderr`` but renewal will be attempted anyway. A failing hook +doesn't directly cause Certbot to exit with a non-zero exit code, but +since Certbot exits with a non-zero exit code when renewals fail, a +failed hook causing renewal failures will indirectly result in a +non-zero exit code. Hooks will only be run if a certificate is due for +renewal, so you can run the above command frequently without +unnecessarily stopping your webserver. + +When Certbot detects that a certificate is due for renewal, ``--pre-hook`` +and ``--post-hook`` hooks run before and after each attempt to renew it. +If you want your hook to run only after a successful renewal, use +``--deploy-hook`` in a command like this. + +``certbot renew --deploy-hook /path/to/deploy-hook-script`` + +You can also specify hooks by placing files in subdirectories of Certbot's +configuration directory. Assuming your configuration directory is +``/etc/letsencrypt``, any executable files found in +``/etc/letsencrypt/renewal-hooks/pre``, +``/etc/letsencrypt/renewal-hooks/deploy``, and +``/etc/letsencrypt/renewal-hooks/post`` will be run as pre, deploy, and post +hooks respectively. These hooks are run in alphabetical order. (The order the +hooks are run is determined by the byte value of the characters in their +filenames and is not dependent on your locale.) + +Prior to certbot 3.2.0, hooks in directories were only run when certificates +were renewed with the ``renew`` subcommand, but as of 3.2.0, they are run for +any subcommand. + +Hooks specified in the command line, :ref:`configuration file +`, or :ref:`renewal configuration files ` are +run as usual after running all hooks in these directories. One minor exception +to this is if a hook specified elsewhere is simply the path to an executable +file in the hook directory of the same type (e.g. your pre-hook is the path to +an executable in ``/etc/letsencrypt/renewal-hooks/pre``), the file is not run a +second time. You can stop Certbot from automatically running executables found +in these directories by including ``--no-directory-hooks`` on the command line. + +More information about hooks can be found by running +``certbot --help renew``. + +If you're sure that this command executes successfully without human +intervention, you can add the command to ``crontab`` (since certificates +are only renewed when they're determined to be near expiry, the command +can run on a regular basis, like every week or every day). In that case, +you are likely to want to use the ``-q`` or ``--quiet`` quiet flag to +silence all output except errors. + +If you are manually renewing all of your certificates, the +``--force-renewal`` flag may be helpful; it causes the expiration time of +the certificate(s) to be ignored when considering renewal, and attempts to +renew each and every installed certificate regardless of its age. (This +form is not appropriate to run daily because each certificate will be +renewed every day, which will quickly run into the certificate authority +rate limit.) + +Starting with Certbot 2.7.0, certbot provides the environment variables +`RENEWED_DOMAINS` and `FAILED_DOMAINS` to all post renewal hooks. These +variables contain a space separated list of domains. These variables can be used +to determine if a renewal has succeeded or failed as part of your post renewal +hook. + +Note that options provided to ``certbot renew`` will apply to +*every* certificate for which renewal is attempted; for example, +``certbot renew --rsa-key-size 4096`` would try to replace every +near-expiry certificate with an equivalent certificate using a 4096-bit +RSA public key. If a certificate is successfully renewed using +specified options, those options will be saved and used for future +renewals of that certificate. + +An alternative form that provides for more fine-grained control over the +renewal process (while renewing specified certificates one at a time), +is ``certbot certonly`` with the complete set of subject domains of +a specific certificate specified via `-d` flags. You may also want to +include the ``-n`` or ``--noninteractive`` flag to prevent blocking on +user input (which is useful when running the command from cron). + +``certbot certonly -n -d example.com -d www.example.com`` + +All of the domains covered by the certificate must be specified in +this case in order to renew and replace the old certificate rather +than obtaining a new one; don't forget any `www.` domains! Specifying +a subset of the domains creates a new, separate certificate containing +only those domains, rather than replacing the original certificate. +When run with a set of domains corresponding to an existing certificate, +the ``certonly`` command attempts to renew that specific certificate. + +Please note that the CA will send notification emails to the address +you provide if you do not renew certificates that are about to expire. + +Certbot is working hard to improve the renewal process, and we +apologize for any inconvenience you encounter in integrating these +commands into your individual environment. + +.. note:: ``certbot renew`` exit status will only be 1 if a renewal attempt failed. + This means ``certbot renew`` exit status will be 0 if no certificate needs to be updated. + If you write a custom script and expect to run a command only after a certificate was actually renewed + you will need to use the ``--deploy-hook`` since the exit status will be 0 both on successful renewal + and when renewal is not necessary. + +.. _renaming: + +Renaming certificates +------------------------------------------------------------ + +While certbot does not have an internal rename function, it is possible to rename certs by +requesting a new certificate using certbot's ``--cert-name`` flag. + +1. View certificates with ``certbot certificates`` + + The output might be something like:: + + Found the following certs: + Certificate Name: yourdomain.com-0001 + Serial Number: 2ce74f4e2b822211dd7648ea26c8927bdef6 + Key Type: ECDSA + Domains: yourdomain.com subdomain.yourdomain.com + Expiry Date: 2025-11-16 20:27:27+00:00 (INVALID: TEST_CERT) + Certificate Path: /etc/letsencrypt/live/yourdomain.com-0001/fullchain.pem + Private Key Path: /etc/letsencrypt/live/yourdomain.com-0001/privkey.pem + Certificate Name: yourdomain.com + Serial Number: 2c72aa8cf58aa9fe9a33208d82304642d9ed + Key Type: ECDSA + Domains: yourdomain.com + Expiry Date: 2025-11-16 19:22:47+00:00 (INVALID: TEST_CERT) + Certificate Path: /etc/letsencrypt/live/yourdomain.com/fullchain.pem + Private Key Path: /etc/letsencrypt/live/yourdomain.com/privkey.pem + + In this example, we will rename certificate ``yourdomain.com-0001`` to ``yourdomain.com``. + +2. If the name you want is in use and you're not using that cert, delete it using the instructions + in the `Deleting certificates`_ section. + + You'll then have:: + + Found the following certs: + Certificate Name: yourdomain.com-0001 + Serial Number: 2ce74f4e2b822211dd7648ea26c8927bdef6 + Key Type: ECDSA + Domains: yourdomain.com subdomain.yourdomain.com + Expiry Date: 2025-11-16 20:27:27+00:00 (INVALID: TEST_CERT) + Certificate Path: /etc/letsencrypt/live/yourdomain.com-0001/fullchain.pem + Private Key Path: /etc/letsencrypt/live/yourdomain.com-0001/privkey.pem + +3. If you're not sure how you set the cert up initially, note relevant configuration options by + inspecting ``/etc/letsencrypt/renewal/yourdomain.com-0001.conf`` + + It will look something like:: + + version = 4.2.0 + archive_dir = /etc/letsencrypt/archive/yourdomain.com-0001 + cert = /etc/letsencrypt/live/yourdomain.com-0001/cert.pem + privkey = /etc/letsencrypt/live/yourdomain.com-0001/privkey.pem + chain = /etc/letsencrypt/live/yourdomain.com-0001/chain.pem + fullchain = /etc/letsencrypt/live/yourdomain.com-0001/fullchain.pem + [renewalparams] + account = abcdef12345678910 + server = https://acme-staging-v02.api.letsencrypt.org/directory + authenticator = standalone + key_type = ecdsa + post_hook = "echo 'shut down server'" + + You'll want to note the authenticator, installer, server, any hooks, etc. + +4. Create a new cert with the options and name you want by running + ``certbot certonly --cert-name yourdomain.com -d yourdomain.com -d otherdomain.com --any-other-options``. + The value for ``--cert-name`` can be anything you want; by default it is the first domain + listed on the cert. + +5. Make sure that any links to your certificates are updated. If you're using certbot to manage + the installation of certs on your webserver, you can run + ``certbot install --cert-name yourdomain.com``. + +6. Restart your webserver, if applicable. + +7. You can now delete the old certificate, again using the `Deleting certificates`_ section. + +.. _renewal-config-file: +.. _Modifying the Renewal Configuration File: + +Modifying the Renewal Configuration of Existing Certificates +------------------------------------------------------------ + +When creating a certificate, Certbot will keep track of all of the relevant options chosen by the user. At renewal +time, Certbot will remember these options and apply them once again. + +Sometimes, you may encounter the need to change some of these options for future certificate renewals. To achieve this, +you will need to perform the following steps: + +Certbot v2.3.0 and newer +~~~~~~~~~~~~~~~~~~~~~~~~ +The ``certbot reconfigure`` command can be used to change a certificate's renewal options. +This command will use the new renewal options to perform a test renewal against the Let's Encrypt staging server. +If this is successful, the new renewal options will be saved and will apply to future renewals. + +You will need to specify the ``--cert-name``, which can be found by running ``certbot certificates``. + +A list of common options that may be updated with the ``reconfigure`` command can be found by running +``certbot help reconfigure``. + +As a practical example, if you were using the ``webroot`` authenticator and had relocated your website to another directory, +you can change the ``--webroot-path`` to the new directory using the following command: + +.. code-block:: shell + + certbot reconfigure --cert-name example.com --webroot-path /path/to/new/location + +Certbot v2.2.0 and older +~~~~~~~~~~~~~~~~~~~~~~~~ +1. Perform a *dry run renewal* with the amended options on the command line. This allows you to confirm that the change + is valid and will result in successful future renewals. +2. If the dry run is successful, perform a *live renewal* of the certificate. This will persist the change for future + renewals. If the certificate is not yet due to expire, you will need to force a renewal using ``--force-renewal``. + +.. note:: Rate limits from the certificate authority may prevent you from performing multiple renewals in a short + period of time. It is strongly recommended to perform the second step only once, when you have decided on what + options should change. + +As a practical example, if you were using the ``webroot`` authenticator and had relocated your website to another directory, +you would need to change the ``--webroot-path`` to the new directory. Following the above advice: + +1. Perform a *dry-run renewal* of the individual certificate with the amended options:: + + certbot renew --cert-name example.com --webroot-path /path/to/new/location --dry-run + +2. If the dry-run was successful, make the change permanent by performing a *live renewal* of the certificate with the + amended options, including ``--force-renewal``:: + + certbot renew --cert-name example.com --webroot-path /path/to/new/location --force-renewal + + ``--cert-name`` selects the particular certificate to be modified. Without this option, all certificates will be selected. + + ``--webroot-path`` is the option intended to be changed. All other previously selected options will be kept the same + and do not need to be included in the command. + +For advanced certificate management tasks, it is also possible to manually modify the certificate's renewal configuration +file, but this is discouraged since it can easily break Certbot's ability to renew your certificates. These renewal +configuration files are located at ``/etc/letsencrypt/renewal/CERTNAME.conf``. If you choose to modify the renewal +configuration file we advise you to make a backup of the file beforehand and test its validity with the ``certbot renew --dry-run`` command. + +.. warning:: Manually modifying files under ``/etc/letsencrypt/renewal/`` can damage them if done improperly and we do not recommend doing so. + + +Automated Renewals +------------------ + +Most Certbot installations come with automatic renewals preconfigured. This +is done by means of a scheduled task which runs ``certbot renew`` periodically. + +If you are unsure whether you need to configure automated renewal: + +1. Review the instructions for your system and installation method at + https://certbot.eff.org/instructions. They will describe how to set up a scheduled task, + if necessary. If no step is listed, your system comes with automated renewal pre-installed, + and you should not need to take any additional actions. +2. On Linux and BSD, you can check to see if your installation method has pre-installed a timer + for you. To do so, look for the ``certbot renew`` command in either your system's crontab + (typically `/etc/crontab` or `/etc/cron.*/*`) or systemd timers (``systemctl list-timers``). +3. If you're still not sure, you can configure automated renewal manually by following the steps + in the next section. Certbot has been carefully engineered to handle the case where both manual + automated renewal and pre-installed automated renewal are set up. + +Setting up automated renewal +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you think you may need to set up automated renewal, follow these instructions to set up a +scheduled task to automatically renew your certificates in the background. If you are unsure +whether your system has a pre-installed scheduled task for Certbot, it is safe to follow these +instructions to create one. + +.. note:: + If you're using Windows, these instructions are not necessary as Certbot on Windows comes with + a scheduled task for automated renewal pre-installed. + + If you are using macOS and installed Certbot using Homebrew, follow the instructions at + https://certbot.eff.org/instructions to set up automated renewal. The instructions below + are not applicable on macOS. + +Run the following line, which will add a cron job to `/etc/crontab`: + +.. code-block:: shell + + SLEEPTIME=$(awk 'BEGIN{srand(); print int(rand()*(3600+1))}'); echo "0 0,12 * * * root sleep $SLEEPTIME && certbot renew -q" | sudo tee -a /etc/crontab > /dev/null + +If you needed to stop your webserver to run Certbot, you'll want to +add ``pre`` and ``post`` hooks to stop and start your webserver automatically. +For example, if your webserver is HAProxy, run the following commands to create the hook files +in the appropriate directory: + +.. code-block:: shell + + sudo sh -c 'printf "#!/bin/sh\nservice haproxy stop\n" > /etc/letsencrypt/renewal-hooks/pre/haproxy.sh' + sudo sh -c 'printf "#!/bin/sh\nservice haproxy start\n" > /etc/letsencrypt/renewal-hooks/post/haproxy.sh' + sudo chmod 755 /etc/letsencrypt/renewal-hooks/pre/haproxy.sh + sudo chmod 755 /etc/letsencrypt/renewal-hooks/post/haproxy.sh + +Congratulations, Certbot will now automatically renew your certificates in the background. + +If you are interested in learning more about how Certbot renews your certificates, see the +`Renewing certificates`_ section above. + +.. _where-certs: + +Where are my certificates? +========================== + +All generated keys and issued certificates can be found in +``/etc/letsencrypt/live/$domain``, where ``$domain`` is the certificate +name (see the note below). Rather than copying, please point your (web) +server configuration directly to those files (or create symlinks). +During the renewal_, ``/etc/letsencrypt/live`` is updated with the latest +necessary files. + +.. note:: + The certificate name ``$domain`` used in the path ``/etc/letsencrypt/live/$domain`` + follows this convention: + + * it is the name given to ``--cert-name``, + * if ``--cert-name`` is not set by the user it is the first domain given to + ``--domains``, + * if the first domain is a wildcard domain (eg. ``*.example.com``) the + certificate name will be ``example.com``, + * if a name collision would occur with a certificate already named ``example.com``, + the new certificate name will be constructed using a numerical sequence + as ``example.com-001``. + +For historical reasons, the containing directories are created with +permissions of ``0700`` meaning that certificates are accessible only +to servers that run as the root user. **If you will never downgrade +to an older version of Certbot**, then you can safely fix this using +``chmod 0755 /etc/letsencrypt/{live,archive}``. + +For servers that drop root privileges before attempting to read the +private key file, you will also need to use ``chgrp`` and ``chmod +0640`` to allow the server to read +``/etc/letsencrypt/live/$domain/privkey.pem``. + +The following files are available: + +``privkey.pem`` + Private key for the certificate. + + .. warning:: This **must be kept secret at all times**! Never share + it with anyone, including Certbot developers. You cannot + put it into a safe, however - your server still needs to access + this file in order for SSL/TLS to work. + + .. note:: As of Certbot version 0.29.0, private keys for new certificate + default to ``0600``. Any changes to the group mode or group owner (gid) + of this file will be preserved on renewals. + + This is what Apache needs for `SSLCertificateKeyFile + `_, + and Nginx for `ssl_certificate_key + `_. + +``fullchain.pem`` + All certificates, **including** server certificate (aka leaf certificate or + end-entity certificate). The server certificate is the first one in this file, + followed by any intermediates. + + This is what Apache >= 2.4.8 needs for `SSLCertificateFile + `_, + and what Nginx needs for `ssl_certificate + `_. + +``cert.pem`` and ``chain.pem`` (less common) + ``cert.pem`` contains the server certificate by itself, and + ``chain.pem`` contains the additional intermediate certificate or + certificates that web browsers will need in order to validate the + server certificate. If you provide one of these files to your web + server, you **must** provide both of them, or some browsers will show + "This Connection is Untrusted" errors for your site, `some of the time + `_. + + Apache < 2.4.8 needs these for `SSLCertificateFile + `_. + and `SSLCertificateChainFile + `_, + respectively. + + If you're using OCSP stapling with Nginx >= 1.3.7, ``chain.pem`` should be + provided as the `ssl_trusted_certificate + `_ + to validate OCSP responses. + +.. note:: All files are PEM-encoded. + If you need other format, such as DER or PFX, then you + could convert using ``openssl``. You can automate that with + ``--deploy-hook`` if you're using automatic renewal_. + +.. _hooks: + +Pre and Post Validation Hooks +============================= + +Certbot allows for the specification of pre and post validation hooks when run +in manual mode. The flags to specify these scripts are ``--manual-auth-hook`` +and ``--manual-cleanup-hook`` respectively and can be used as follows: + +:: + + certbot certonly --manual --manual-auth-hook /path/to/http/authenticator.sh --manual-cleanup-hook /path/to/http/cleanup.sh -d secure.example.com + +This will run the ``authenticator.sh`` script, attempt the validation, and then run +the ``cleanup.sh`` script. Additionally certbot will pass relevant environment +variables to these scripts: + +- ``CERTBOT_IDENTIFIER``: The domain or IP address being authenticated +- ``CERTBOT_VALIDATION``: The validation string +- ``CERTBOT_TOKEN``: Resource name part of the HTTP-01 challenge (HTTP-01 only) +- ``CERTBOT_REMAINING_CHALLENGES``: Number of challenges remaining after the current challenge +- ``CERTBOT_ALL_IDENTIFIERS``: A comma-separated list of all identifiers challenged for the current certificate + +Additionally for cleanup: + +- ``CERTBOT_AUTH_OUTPUT``: Whatever the auth script wrote to stdout + +Certbot also sets ``CERTBOT_DOMAIN`` and ``CERTBOT_ALL_DOMAINS`` to the same values as +``CERTBOT_IDENTIFIER`` and ``CERTBOT_ALL_IDENTIFIERS`` respectively for backwards compatibility, +however, the variables names containing "identifier" are preferred since Certbot has added support +for obtaining certificates for IP addresses. + +Example usage for HTTP-01: + +:: + + certbot certonly --manual --preferred-challenges=http --manual-auth-hook /path/to/http/authenticator.sh --manual-cleanup-hook /path/to/http/cleanup.sh -d secure.example.com + +/path/to/http/authenticator.sh + +.. code-block:: none + + #!/bin/bash + echo $CERTBOT_VALIDATION > /var/www/htdocs/.well-known/acme-challenge/$CERTBOT_TOKEN + +/path/to/http/cleanup.sh + +.. code-block:: none + + #!/bin/bash + rm -f /var/www/htdocs/.well-known/acme-challenge/$CERTBOT_TOKEN + +Example usage for DNS-01 (Cloudflare API v4) (for example purposes only, do not use as-is) + +:: + + certbot certonly --manual --preferred-challenges=dns --manual-auth-hook /path/to/dns/authenticator.sh --manual-cleanup-hook /path/to/dns/cleanup.sh -d secure.example.com + +/path/to/dns/authenticator.sh + +.. code-block:: none + + #!/bin/bash + + # Get your API key from https://www.cloudflare.com/a/account/my-account + API_KEY="your-api-key" + EMAIL="your.email@example.com" + + # Strip only the top domain to get the zone id + DOMAIN=$(expr match "$CERTBOT_IDENTIFIER" '.*\.\(.*\..*\)') + + # Get the Cloudflare zone id + ZONE_EXTRA_PARAMS="status=active&page=1&per_page=20&order=status&direction=desc&match=all" + ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN&$ZONE_EXTRA_PARAMS" \ + -H "X-Auth-Email: $EMAIL" \ + -H "X-Auth-Key: $API_KEY" \ + -H "Content-Type: application/json" | python -c "import sys,json;print(json.load(sys.stdin)['result'][0]['id'])") + + # Create TXT record + CREATE_DOMAIN="_acme-challenge.$CERTBOT_IDENTIFIER" + RECORD_ID=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \ + -H "X-Auth-Email: $EMAIL" \ + -H "X-Auth-Key: $API_KEY" \ + -H "Content-Type: application/json" \ + --data '{"type":"TXT","name":"'"$CREATE_DOMAIN"'","content":"'"$CERTBOT_VALIDATION"'","ttl":120}' \ + | python -c "import sys,json;print(json.load(sys.stdin)['result']['id'])") + # Save info for cleanup + if [ ! -d /tmp/CERTBOT_$CERTBOT_IDENTIFIER ];then + mkdir -m 0700 /tmp/CERTBOT_$CERTBOT_IDENTIFIER + fi + echo $ZONE_ID > /tmp/CERTBOT_$CERTBOT_IDENTIFIER/ZONE_ID + echo $RECORD_ID > /tmp/CERTBOT_$CERTBOT_IDENTIFIER/RECORD_ID + + # Sleep to make sure the change has time to propagate over to DNS + sleep 25 + +/path/to/dns/cleanup.sh + +.. code-block:: none + + #!/bin/bash + + # Get your API key from https://www.cloudflare.com/a/account/my-account + API_KEY="your-api-key" + EMAIL="your.email@example.com" + + if [ -f /tmp/CERTBOT_$CERTBOT_IDENTIFIER/ZONE_ID ]; then + ZONE_ID=$(cat /tmp/CERTBOT_$CERTBOT_IDENTIFIER/ZONE_ID) + rm -f /tmp/CERTBOT_$CERTBOT_IDENTIFIER/ZONE_ID + fi + + if [ -f /tmp/CERTBOT_$CERTBOT_IDENTIFIER/RECORD_ID ]; then + RECORD_ID=$(cat /tmp/CERTBOT_$CERTBOT_IDENTIFIER/RECORD_ID) + rm -f /tmp/CERTBOT_$CERTBOT_IDENTIFIER/RECORD_ID + fi + + # Remove the challenge TXT record from the zone + if [ -n "${ZONE_ID}" ]; then + if [ -n "${RECORD_ID}" ]; then + curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ + -H "X-Auth-Email: $EMAIL" \ + -H "X-Auth-Key: $API_KEY" \ + -H "Content-Type: application/json" + fi + fi + +.. _lock-files: + +Changing the ACME Server +======================== + +By default, Certbot uses Let's Encrypt's production server at +https://acme-v02.api.letsencrypt.org/directory. You can tell Certbot to use a +different CA by providing ``--server`` on the command line or in a +:ref:`configuration file ` with the URL of the server's +ACME directory. For example, if you would like to use Let's Encrypt's +staging server, you would add ``--server +https://acme-staging-v02.api.letsencrypt.org/directory`` to the command line. + +.. note:: ``--dry-run`` uses the Let's Encrypt staging server, unless ``--server`` + is specified on the CLI or in the :ref:`cli.ini configuration file `. + Take caution when using ``--dry-run`` with a custom server, as it may cause real + certificates to be issued and discarded. + +If Certbot does not trust the SSL certificate used by the ACME server, you +can use the `REQUESTS_CA_BUNDLE +`_ +environment variable to override the root certificates trusted by Certbot. Certbot +uses the ``requests`` library, which does not use the operating system trusted root store. +Make sure that ``REQUESTS_CA_BUNDLE`` is set globally in the environment and not only on +the CLI, or scheduled renewal will not succeed. + + +Lock Files +========== + +When processing a validation Certbot writes a number of lock files on your system +to prevent multiple instances from overwriting each other's changes. This means +that by default two instances of Certbot will not be able to run in parallel. + +Since the directories used by Certbot are configurable, Certbot +will write a lock file for all of the directories it uses. This include Certbot's +``--work-dir``, ``--logs-dir``, and ``--config-dir``. By default these are +``/var/lib/letsencrypt``, ``/var/log/letsencrypt``, and ``/etc/letsencrypt`` +respectively. Additionally if you are using Certbot with Apache or nginx it will +lock the configuration folder for that program, which are typically also in the +``/etc`` directory. + +Note that these lock files will only prevent other instances of Certbot from +using those directories, not other processes. If you'd like to run multiple +instances of Certbot simultaneously you should specify different directories +as the ``--work-dir``, ``--logs-dir``, and ``--config-dir`` for each instance +of Certbot that you would like to run. + +.. _config-file: + +Configuration file +================== + +Certbot accepts a global configuration file that applies its options to all invocations +of Certbot. Certificate-specific configuration choices are stored in the ``.conf`` +files that can be found in ``/etc/letsencrypt/renewal``. See +`Modifying the Renewal Configuration of Existing Certificates`_ for more information +about modifying certificate-specific options. Note that it is not recommended to modify +these certificate-specific renewal configuration files manually. + +By default no cli.ini file is created (though it may exist already if you installed Certbot +via a package manager, for instance). +After creating one it is possible to specify the location of this configuration file with +``certbot --config cli.ini`` (or shorter ``-c cli.ini``). An +example configuration file is shown below: + +.. include:: ../examples/cli.ini + :code: ini + +By default, the following locations are searched: + +- ``/etc/letsencrypt/cli.ini`` +- ``$XDG_CONFIG_HOME/letsencrypt/cli.ini`` (or + ``~/.config/letsencrypt/cli.ini`` if ``$XDG_CONFIG_HOME`` is not + set). + +Since this configuration file applies to all invocations of certbot it is incorrect +to list domains in it. Listing domains in cli.ini may prevent renewal from working. +Additionally due to how arguments in cli.ini are parsed, options which wish to +not be set should not be listed. Options set to false will instead be read +as being set to true by older versions of Certbot, since they have been listed +in the config file. + +.. keep it up to date with constants.py + +.. _log-rotation: + +Log Rotation +============ + +By default certbot stores status logs in ``/var/log/letsencrypt``. By default +certbot will begin rotating logs once there are 1000 logs in the log directory. +Meaning that once 1000 files are in ``/var/log/letsencrypt`` Certbot will delete +the oldest one to make room for new logs. The number of subsequent logs can be +changed by passing the desired number to the command line flag +``--max-log-backups``. Setting this flag to 0 disables log rotation entirely, +causing certbot to always append to the same log file. + +.. note:: Some distributions, including Debian and Ubuntu, disable + certbot's internal log rotation in favor of a more traditional + logrotate script. If you are using a distribution's packages and + want to alter the log rotation, check `/etc/logrotate.d/` for a + certbot rotation script. + +.. _command-line: + +Certbot command-line options +============================ + +Certbot supports a lot of command line options. Here's the full list, from +``certbot --help all``: + +.. literalinclude:: cli-help.txt + +Getting help +============ + +If you're having problems, we recommend posting on the Let's Encrypt +`Community Forum `_. + +If you find a bug in the software, please do report it in our `issue +tracker `_. Remember to +give us as much information as possible: + +- copy and paste exact command line used and the output (though mind + that the latter might include some personally identifiable + information, including your email and domains) +- copy and paste logs from ``/var/log/letsencrypt`` (though mind they + also might contain personally identifiable information) +- copy and paste ``certbot --version`` output +- your operating system, including specific version +- specify which installation method you've chosen diff --git a/docs/what.rst b/certbot/docs/what.rst similarity index 88% rename from docs/what.rst rename to certbot/docs/what.rst index 3d33346c2dd..b5e50d8824a 100644 --- a/docs/what.rst +++ b/certbot/docs/what.rst @@ -2,14 +2,14 @@ What is a Certificate? ====================== -A public key or digital *certificate* (formerly called an SSL certificate) uses a public key -and a private key to enable secure communication between a client program (web browser, email client, +A public key or digital *certificate* (formerly called an SSL certificate) uses a public key +and a private key to enable secure communication between a client program (web browser, email client, etc.) and a server over an encrypted SSL (secure socket layer) or TLS (transport layer security) connection. -The certificate is used both to encrypt the initial stage of communication (secure key exchange) +The certificate is used both to encrypt the initial stage of communication (secure key exchange) and to identify the server. The certificate includes information about the key, information about the server identity, and the digital signature of the certificate issuer. If the issuer is trusted by the software that initiates the communication, -and the signature is valid, then the key can be used to communicate securely with the server identified by +and the signature is valid, then the key can be used to communicate securely with the server identified by the certificate. Using a certificate is a good way to prevent "man-in-the-middle" attacks, in which someone in between you and the server you think you are talking to is able to insert their own (harmful) content. @@ -23,9 +23,9 @@ Certificates and Lineages Certbot introduces the concept of a *lineage,* which is a collection of all the versions of a certificate plus Certbot configuration information maintained for that certificate from renewal to renewal. Whenever you renew a certificate, Certbot keeps the same configuration unless -you explicitly change it, for example by adding or removing domains. If you add domains, you can +you explicitly change it, for example by adding or removing domains. If you add domains, you can either add them to an existing lineage or create -a new one. +a new one. See also: :ref:`updating_certs` diff --git a/certbot/eff.py b/certbot/eff.py deleted file mode 100644 index 388ae986bee..00000000000 --- a/certbot/eff.py +++ /dev/null @@ -1,98 +0,0 @@ -"""Subscribes users to the EFF newsletter.""" -import logging - -import requests -import zope.component - -from certbot import constants -from certbot import interfaces - - -logger = logging.getLogger(__name__) - - -def handle_subscription(config): - """High level function to take care of EFF newsletter subscriptions. - - The user may be asked if they want to sign up for the newsletter if - they have not already specified. - - :param .IConfig config: Client configuration. - - """ - if config.email is None: - if config.eff_email: - _report_failure("you didn't provide an e-mail address") - return - if config.eff_email is None: - config.eff_email = _want_subscription() - if config.eff_email: - subscribe(config.email) - - -def _want_subscription(): - """Does the user want to be subscribed to the EFF newsletter? - - :returns: True if we should subscribe the user, otherwise, False - :rtype: bool - - """ - prompt = ( - 'Would you be willing to share your email address with the ' - "Electronic Frontier Foundation, a founding partner of the Let's " - 'Encrypt project and the non-profit organization that develops ' - "Certbot? We'd like to send you email about our work encrypting " - "the web, EFF news, campaigns, and ways to support digital freedom. ") - display = zope.component.getUtility(interfaces.IDisplay) - return display.yesno(prompt, default=False) - - -def subscribe(email): - """Subscribe the user to the EFF mailing list. - - :param str email: the e-mail address to subscribe - - """ - url = constants.EFF_SUBSCRIBE_URI - data = {'data_type': 'json', - 'email': email, - 'form_id': 'eff_supporters_library_subscribe_form'} - logger.debug('Sending POST request to %s:\n%s', url, data) - _check_response(requests.post(url, data=data)) - - -def _check_response(response): - """Check for errors in the server's response. - - If an error occurred, it will be reported to the user. - - :param requests.Response response: the server's response to the - subscription request - - """ - logger.debug('Received response:\n%s', response.content) - try: - response.raise_for_status() - if response.json()['status'] == False: - _report_failure('your e-mail address appears to be invalid') - except requests.exceptions.HTTPError: - _report_failure() - except (ValueError, KeyError): - _report_failure('there was a problem with the server response') - - -def _report_failure(reason=None): - """Notify the user of failing to sign them up for the newsletter. - - :param reason: a phrase describing what the problem was - beginning with a lowercase letter and no closing punctuation - :type reason: `str` or `None` - - """ - msg = ['We were unable to subscribe you the EFF mailing list'] - if reason is not None: - msg.append(' because ') - msg.append(reason) - msg.append('. You can try again later by visiting https://act.eff.org.') - reporter = zope.component.getUtility(interfaces.IReporter) - reporter.add_message(''.join(msg), reporter.LOW_PRIORITY) diff --git a/certbot/error_handler.py b/certbot/error_handler.py deleted file mode 100644 index 5e72f815301..00000000000 --- a/certbot/error_handler.py +++ /dev/null @@ -1,157 +0,0 @@ -"""Registers functions to be called if an exception or signal occurs.""" -import functools -import logging -import os -import signal -import traceback - -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import Any, Callable, Dict, List, Union -# pylint: enable=unused-import, no-name-in-module - -from certbot import errors - -logger = logging.getLogger(__name__) - - -# _SIGNALS stores the signals that will be handled by the ErrorHandler. These -# signals were chosen as their default handler terminates the process and could -# potentially occur from inside Python. Signals such as SIGILL were not -# included as they could be a sign of something devious and we should terminate -# immediately. -_SIGNALS = [signal.SIGTERM] -if os.name != "nt": - for signal_code in [signal.SIGHUP, signal.SIGQUIT, - signal.SIGXCPU, signal.SIGXFSZ]: - # Adding only those signals that their default action is not Ignore. - # This is platform-dependent, so we check it dynamically. - if signal.getsignal(signal_code) != signal.SIG_IGN: - _SIGNALS.append(signal_code) - -class ErrorHandler(object): - """Context manager for running code that must be cleaned up on failure. - - The context manager allows you to register functions that will be called - when an exception (excluding SystemExit) or signal is encountered. - Usage:: - - handler = ErrorHandler(cleanup1_func, *cleanup1_args, **cleanup1_kwargs) - handler.register(cleanup2_func, *cleanup2_args, **cleanup2_kwargs) - - with handler: - do_something() - - Or for one cleanup function:: - - with ErrorHandler(func, args, kwargs): - do_something() - - If an exception is raised out of do_something, the cleanup functions will - be called in last in first out order. Then the exception is raised. - Similarly, if a signal is encountered, the cleanup functions are called - followed by the previously received signal handler. - - Each registered cleanup function is called exactly once. If a registered - function raises an exception, it is logged and the next function is called. - Signals received while the registered functions are executing are - deferred until they finish. - - """ - def __init__(self, func=None, *args, **kwargs): - self.call_on_regular_exit = False - self.body_executed = False - self.funcs = [] # type: List[Callable[[], Any]] - self.prev_handlers = {} # type: Dict[int, Union[int, None, Callable]] - self.received_signals = [] # type: List[int] - if func is not None: - self.register(func, *args, **kwargs) - - def __enter__(self): - self.body_executed = False - self._set_signal_handlers() - - def __exit__(self, exec_type, exec_value, trace): - self.body_executed = True - retval = False - # SystemExit is ignored to properly handle forks that don't exec - if exec_type is SystemExit: - return retval - elif exec_type is None: - if not self.call_on_regular_exit: - return retval - elif exec_type is errors.SignalExit: - logger.debug("Encountered signals: %s", self.received_signals) - retval = True - else: - logger.debug("Encountered exception:\n%s", "".join( - traceback.format_exception(exec_type, exec_value, trace))) - - self._call_registered() - self._reset_signal_handlers() - self._call_signals() - return retval - - def register(self, func, *args, **kwargs): - # type: (Callable, *Any, **Any) -> None - """Sets func to be run with the given arguments during cleanup. - - :param function func: function to be called in case of an error - - """ - self.funcs.append(functools.partial(func, *args, **kwargs)) - - def _call_registered(self): - """Calls all registered functions""" - logger.debug("Calling registered functions") - while self.funcs: - try: - self.funcs[-1]() - except Exception: # pylint: disable=broad-except - logger.error("Encountered exception during recovery: ", exc_info=True) - self.funcs.pop() - - def _set_signal_handlers(self): - """Sets signal handlers for signals in _SIGNALS.""" - for signum in _SIGNALS: - prev_handler = signal.getsignal(signum) - # If prev_handler is None, the handler was set outside of Python - if prev_handler is not None: - self.prev_handlers[signum] = prev_handler - signal.signal(signum, self._signal_handler) - - def _reset_signal_handlers(self): - """Resets signal handlers for signals in _SIGNALS.""" - for signum in self.prev_handlers: - signal.signal(signum, self.prev_handlers[signum]) - self.prev_handlers.clear() - - def _signal_handler(self, signum, unused_frame): - """Replacement function for handling received signals. - - Store the received signal. If we are executing the code block in - the body of the context manager, stop by raising signal exit. - - :param int signum: number of current signal - - """ - self.received_signals.append(signum) - if not self.body_executed: - raise errors.SignalExit - - def _call_signals(self): - """Finally call the deferred signals.""" - for signum in self.received_signals: - logger.debug("Calling signal %s", signum) - os.kill(os.getpid(), signum) - -class ExitHandler(ErrorHandler): - """Context manager for running code that must be cleaned up. - - Subclass of ErrorHandler, with the same usage and parameters. - In addition to cleaning up on all signals, also cleans up on - regular exit. - """ - def __init__(self, func=None, *args, **kwargs): - ErrorHandler.__init__(self, func, *args, **kwargs) - self.call_on_regular_exit = True - diff --git a/certbot/errors.py b/certbot/errors.py deleted file mode 100644 index 48aebc26773..00000000000 --- a/certbot/errors.py +++ /dev/null @@ -1,110 +0,0 @@ -"""Certbot client errors.""" - - -class Error(Exception): - """Generic Certbot client error.""" - - -class AccountStorageError(Error): - """Generic `.AccountStorage` error.""" - - -class AccountNotFound(AccountStorageError): - """Account not found error.""" - - -class ReverterError(Error): - """Certbot Reverter error.""" - - -class SubprocessError(Error): - """Subprocess handling error.""" - - -class CertStorageError(Error): - """Generic `.CertStorage` error.""" - - -class HookCommandNotFound(Error): - """Failed to find a hook command in the PATH.""" - - -class SignalExit(Error): - """A Unix signal was received while in the ErrorHandler context manager.""" - -class OverlappingMatchFound(Error): - """Multiple lineages matched what should have been a unique result.""" - -class LockError(Error): - """File locking error.""" - - -# Auth Handler Errors -class AuthorizationError(Error): - """Authorization error.""" - - -class FailedChallenges(AuthorizationError): - """Failed challenges error. - - :ivar set failed_achalls: Failed `.AnnotatedChallenge` instances. - - """ - def __init__(self, failed_achalls): - assert failed_achalls - self.failed_achalls = failed_achalls - super(FailedChallenges, self).__init__() - - def __str__(self): - return "Failed authorization procedure. {0}".format( - ", ".join( - "{0} ({1}): {2}".format(achall.domain, achall.typ, achall.error) - for achall in self.failed_achalls if achall.error is not None)) - - -# Plugin Errors -class PluginError(Error): - """Certbot Plugin error.""" - - -class PluginEnhancementAlreadyPresent(Error): - """ Enhancement was already set """ - - -class PluginSelectionError(Error): - """A problem with plugin/configurator selection or setup""" - - -class NoInstallationError(PluginError): - """Certbot No Installation error.""" - - -class MisconfigurationError(PluginError): - """Certbot Misconfiguration error.""" - - -class NotSupportedError(PluginError): - """Certbot Plugin function not supported error.""" - - -class PluginStorageError(PluginError): - """Certbot Plugin Storage error.""" - - -class StandaloneBindError(Error): - """Standalone plugin bind error.""" - - def __init__(self, socket_error, port): - super(StandaloneBindError, self).__init__( - "Problem binding to port {0}: {1}".format(port, socket_error)) - self.socket_error = socket_error - self.port = port - - -class ConfigurationError(Error): - """Configuration sanity error.""" - -# NoninteractiveDisplay iDisplay plugin error: - -class MissingCommandlineFlag(Error): - """A command line argument was missing in noninteractive usage""" diff --git a/examples/.gitignore b/certbot/examples/.gitignore similarity index 100% rename from examples/.gitignore rename to certbot/examples/.gitignore diff --git a/certbot/examples/cli.ini b/certbot/examples/cli.ini new file mode 100644 index 00000000000..ac648c4f0cb --- /dev/null +++ b/certbot/examples/cli.ini @@ -0,0 +1,34 @@ +# This is an example of the kind of things you can do in a configuration file. +# All flags used by the client can be configured here. Run Certbot with +# "--help" to learn more about the available options. +# +# Note that these options apply automatically to all use of Certbot for +# obtaining or renewing certificates, so options specific to a single +# certificate on a system with several certificates should not be placed +# here. + +# Use ECC for the private key +key-type = ecdsa +elliptic-curve = secp384r1 + +# Use a 4096 bit RSA key instead of 2048 +rsa-key-size = 4096 + +# Uncomment and update to register with the specified e-mail address +# email = foo@example.com + +# Uncomment to use the standalone authenticator on port 80 +# authenticator = standalone + +# Uncomment to use the webroot authenticator. Replace webroot-path with the +# path to the public_html / webroot folder being served by your web server. +# authenticator = webroot +# webroot-path = /usr/share/nginx/html + +# Uncomment to automatically agree to the terms of service of the ACME server +# agree-tos = true + +# An example of using an alternate ACME server that uses EAB credentials +# server = https://acme.sectigo.com/v2/InCommonRSAOV +# eab-kid = somestringofstuffwithoutquotes +# eab-hmac-key = yaddayaddahexhexnotquoted diff --git a/examples/dev-cli.ini b/certbot/examples/dev-cli.ini similarity index 78% rename from examples/dev-cli.ini rename to certbot/examples/dev-cli.ini index a405a0aefda..21f7db85c8f 100644 --- a/examples/dev-cli.ini +++ b/certbot/examples/dev-cli.ini @@ -13,8 +13,6 @@ domains = example.com text = True agree-tos = True debug = True -# Unfortunately, it's not possible to specify "verbose" multiple times -# (correspondingly to -vvvvvv) -verbose = True +verbose-level = 2 # -vv (debug) authenticator = standalone diff --git a/examples/generate-csr.sh b/certbot/examples/generate-csr.sh similarity index 100% rename from examples/generate-csr.sh rename to certbot/examples/generate-csr.sh diff --git a/examples/openssl.cnf b/certbot/examples/openssl.cnf similarity index 100% rename from examples/openssl.cnf rename to certbot/examples/openssl.cnf diff --git a/certbot/examples/plugins/certbot_example_plugins.py b/certbot/examples/plugins/certbot_example_plugins.py new file mode 100644 index 00000000000..a4a073cced5 --- /dev/null +++ b/certbot/examples/plugins/certbot_example_plugins.py @@ -0,0 +1,25 @@ +"""Example Certbot plugins. + +For full examples, see `certbot.plugins`. + +""" +from certbot import interfaces +from certbot.plugins import common + + +class Authenticator(common.Plugin, interfaces.Authenticator): + """Example Authenticator.""" + + description = "Example Authenticator plugin" + + # Implement all methods from Authenticator, remembering to add + # "self" as first argument, e.g. def prepare(self)... + + +class Installer(common.Plugin, interfaces.Installer): + """Example Installer.""" + + description = "Example Installer plugin" + + # Implement all methods from Installer, remembering to add + # "self" as first argument, e.g. def get_all_names(self)... diff --git a/certbot/examples/plugins/setup.py b/certbot/examples/plugins/setup.py new file mode 100644 index 00000000000..b8f9e7701d8 --- /dev/null +++ b/certbot/examples/plugins/setup.py @@ -0,0 +1,15 @@ +from setuptools import setup + +setup( + name='certbot-example-plugins', + package='certbot_example_plugins.py', + install_requires=[ + 'certbot', + ], + entry_points={ + 'certbot.plugins': [ + 'example_authenticator = certbot_example_plugins:Authenticator', + 'example_installer = certbot_example_plugins:Installer', + ], + }, +) diff --git a/certbot/hooks.py b/certbot/hooks.py deleted file mode 100644 index d5239a43777..00000000000 --- a/certbot/hooks.py +++ /dev/null @@ -1,267 +0,0 @@ -"""Facilities for implementing hooks that call shell commands.""" -from __future__ import print_function - -import logging -import os - -from subprocess import Popen, PIPE - -from acme.magic_typing import Set, List # pylint: disable=unused-import, no-name-in-module -from certbot import errors -from certbot import util - -from certbot.plugins import util as plug_util - -logger = logging.getLogger(__name__) - - -def validate_hooks(config): - """Check hook commands are executable.""" - validate_hook(config.pre_hook, "pre") - validate_hook(config.post_hook, "post") - validate_hook(config.deploy_hook, "deploy") - validate_hook(config.renew_hook, "renew") - - -def _prog(shell_cmd): - """Extract the program run by a shell command. - - :param str shell_cmd: command to be executed - - :returns: basename of command or None if the command isn't found - :rtype: str or None - - """ - if not util.exe_exists(shell_cmd): - plug_util.path_surgery(shell_cmd) - if not util.exe_exists(shell_cmd): - return None - return os.path.basename(shell_cmd) - - -def validate_hook(shell_cmd, hook_name): - """Check that a command provided as a hook is plausibly executable. - - :raises .errors.HookCommandNotFound: if the command is not found - """ - if shell_cmd: - cmd = shell_cmd.split(None, 1)[0] - if not _prog(cmd): - path = os.environ["PATH"] - if os.path.exists(cmd): - msg = "{1}-hook command {0} exists, but is not executable.".format(cmd, hook_name) - else: - msg = "Unable to find {2}-hook command {0} in the PATH.\n(PATH is {1})".format( - cmd, path, hook_name) - - raise errors.HookCommandNotFound(msg) - - -def pre_hook(config): - """Run pre-hooks if they exist and haven't already been run. - - When Certbot is running with the renew subcommand, this function - runs any hooks found in the config.renewal_pre_hooks_dir (if they - have not already been run) followed by any pre-hook in the config. - If hooks in config.renewal_pre_hooks_dir are run and the pre-hook in - the config is a path to one of these scripts, it is not run twice. - - :param configuration.NamespaceConfig config: Certbot settings - - """ - if config.verb == "renew" and config.directory_hooks: - for hook in list_hooks(config.renewal_pre_hooks_dir): - _run_pre_hook_if_necessary(hook) - - cmd = config.pre_hook - if cmd: - _run_pre_hook_if_necessary(cmd) - - -executed_pre_hooks = set() # type: Set[str] - - -def _run_pre_hook_if_necessary(command): - """Run the specified pre-hook if we haven't already. - - If we've already run this exact command before, a message is logged - saying the pre-hook was skipped. - - :param str command: pre-hook to be run - - """ - if command in executed_pre_hooks: - logger.info("Pre-hook command already run, skipping: %s", command) - else: - logger.info("Running pre-hook command: %s", command) - _run_hook(command) - executed_pre_hooks.add(command) - - -def post_hook(config): - """Run post-hooks if defined. - - This function also registers any executables found in - config.renewal_post_hooks_dir to be run when Certbot is used with - the renew subcommand. - - If the verb is renew, we delay executing any post-hooks until - :func:`run_saved_post_hooks` is called. In this case, this function - registers all hooks found in config.renewal_post_hooks_dir to be - called followed by any post-hook in the config. If the post-hook in - the config is a path to an executable in the post-hook directory, it - is not scheduled to be run twice. - - :param configuration.NamespaceConfig config: Certbot settings - - """ - - cmd = config.post_hook - # In the "renew" case, we save these up to run at the end - if config.verb == "renew": - if config.directory_hooks: - for hook in list_hooks(config.renewal_post_hooks_dir): - _run_eventually(hook) - if cmd: - _run_eventually(cmd) - # certonly / run - elif cmd: - logger.info("Running post-hook command: %s", cmd) - _run_hook(cmd) - - -post_hooks = [] # type: List[str] - - -def _run_eventually(command): - """Registers a post-hook to be run eventually. - - All commands given to this function will be run exactly once in the - order they were given when :func:`run_saved_post_hooks` is called. - - :param str command: post-hook to register to be run - - """ - if command not in post_hooks: - post_hooks.append(command) - - -def run_saved_post_hooks(): - """Run any post hooks that were saved up in the course of the 'renew' verb""" - for cmd in post_hooks: - logger.info("Running post-hook command: %s", cmd) - _run_hook(cmd) - - -def deploy_hook(config, domains, lineage_path): - """Run post-issuance hook if defined. - - :param configuration.NamespaceConfig config: Certbot settings - :param domains: domains in the obtained certificate - :type domains: `list` of `str` - :param str lineage_path: live directory path for the new cert - - """ - if config.deploy_hook: - _run_deploy_hook(config.deploy_hook, domains, - lineage_path, config.dry_run) - - -def renew_hook(config, domains, lineage_path): - """Run post-renewal hooks. - - This function runs any hooks found in - config.renewal_deploy_hooks_dir followed by any renew-hook in the - config. If the renew-hook in the config is a path to a script in - config.renewal_deploy_hooks_dir, it is not run twice. - - If Certbot is doing a dry run, no hooks are run and messages are - logged saying that they were skipped. - - :param configuration.NamespaceConfig config: Certbot settings - :param domains: domains in the obtained certificate - :type domains: `list` of `str` - :param str lineage_path: live directory path for the new cert - - """ - executed_dir_hooks = set() - if config.directory_hooks: - for hook in list_hooks(config.renewal_deploy_hooks_dir): - _run_deploy_hook(hook, domains, lineage_path, config.dry_run) - executed_dir_hooks.add(hook) - - if config.renew_hook: - if config.renew_hook in executed_dir_hooks: - logger.info("Skipping deploy-hook '%s' as it was already run.", - config.renew_hook) - else: - _run_deploy_hook(config.renew_hook, domains, - lineage_path, config.dry_run) - - -def _run_deploy_hook(command, domains, lineage_path, dry_run): - """Run the specified deploy-hook (if not doing a dry run). - - If dry_run is True, command is not run and a message is logged - saying that it was skipped. If dry_run is False, the hook is run - after setting the appropriate environment variables. - - :param str command: command to run as a deploy-hook - :param domains: domains in the obtained certificate - :type domains: `list` of `str` - :param str lineage_path: live directory path for the new cert - :param bool dry_run: True iff Certbot is doing a dry run - - """ - if dry_run: - logger.warning("Dry run: skipping deploy hook command: %s", - command) - return - - os.environ["RENEWED_DOMAINS"] = " ".join(domains) - os.environ["RENEWED_LINEAGE"] = lineage_path - logger.info("Running deploy-hook command: %s", command) - _run_hook(command) - - -def _run_hook(shell_cmd): - """Run a hook command. - - :returns: stderr if there was any""" - - err, _ = execute(shell_cmd) - return err - - -def execute(shell_cmd): - """Run a command. - - :returns: `tuple` (`str` stderr, `str` stdout)""" - - # universal_newlines causes Popen.communicate() - # to return str objects instead of bytes in Python 3 - cmd = Popen(shell_cmd, shell=True, stdout=PIPE, - stderr=PIPE, universal_newlines=True) - out, err = cmd.communicate() - base_cmd = os.path.basename(shell_cmd.split(None, 1)[0]) - if out: - logger.info('Output from %s:\n%s', base_cmd, out) - if cmd.returncode != 0: - logger.error('Hook command "%s" returned error code %d', - shell_cmd, cmd.returncode) - if err: - logger.error('Error output from %s:\n%s', base_cmd, err) - return (err, out) - - -def list_hooks(dir_path): - """List paths to all hooks found in dir_path in sorted order. - - :param str dir_path: directory to search - - :returns: `list` of `str` - :rtype: sorted list of absolute paths to executables in dir_path - - """ - paths = (os.path.join(dir_path, f) for f in os.listdir(dir_path)) - return sorted(path for path in paths if util.is_exe(path)) diff --git a/certbot/interfaces.py b/certbot/interfaces.py deleted file mode 100644 index 2e837d1d2f4..00000000000 --- a/certbot/interfaces.py +++ /dev/null @@ -1,671 +0,0 @@ -"""Certbot client interfaces.""" -import abc -import six -import zope.interface - -# pylint: disable=no-self-argument,no-method-argument,no-init,inherit-non-class -# pylint: disable=too-few-public-methods - - -@six.add_metaclass(abc.ABCMeta) -class AccountStorage(object): - """Accounts storage interface.""" - - @abc.abstractmethod - def find_all(self): # pragma: no cover - """Find all accounts. - - :returns: All found accounts. - :rtype: list - - """ - raise NotImplementedError() - - @abc.abstractmethod - def load(self, account_id): # pragma: no cover - """Load an account by its id. - - :raises .AccountNotFound: if account could not be found - :raises .AccountStorageError: if account could not be loaded - - """ - raise NotImplementedError() - - @abc.abstractmethod - def save(self, account, client): # pragma: no cover - """Save account. - - :raises .AccountStorageError: if account could not be saved - - """ - raise NotImplementedError() - - -class IPluginFactory(zope.interface.Interface): - """IPlugin factory. - - Objects providing this interface will be called without satisfying - any entry point "extras" (extra dependencies) you might have defined - for your plugin, e.g (excerpt from ``setup.py`` script):: - - setup( - ... - entry_points={ - 'certbot.plugins': [ - 'name=example_project.plugin[plugin_deps]', - ], - }, - extras_require={ - 'plugin_deps': ['dep1', 'dep2'], - } - ) - - Therefore, make sure such objects are importable and usable without - extras. This is necessary, because CLI does the following operations - (in order): - - - loads an entry point, - - calls `inject_parser_options`, - - requires an entry point, - - creates plugin instance (`__call__`). - - """ - - description = zope.interface.Attribute("Short plugin description") - - def __call__(config, name): - """Create new `IPlugin`. - - :param IConfig config: Configuration. - :param str name: Unique plugin name. - - """ - - def inject_parser_options(parser, name): - """Inject argument parser options (flags). - - 1. Be nice and prepend all options and destinations with - `~.common.option_namespace` and `~common.dest_namespace`. - - 2. Inject options (flags) only. Positional arguments are not - allowed, as this would break the CLI. - - :param ArgumentParser parser: (Almost) top-level CLI parser. - :param str name: Unique plugin name. - - """ - - -class IPlugin(zope.interface.Interface): - """Certbot plugin.""" - - def prepare(): # type: ignore - """Prepare the plugin. - - Finish up any additional initialization. - - :raises .PluginError: - when full initialization cannot be completed. - :raises .MisconfigurationError: - when full initialization cannot be completed. Plugin will - be displayed on a list of available plugins. - :raises .NoInstallationError: - when the necessary programs/files cannot be located. Plugin - will NOT be displayed on a list of available plugins. - :raises .NotSupportedError: - when the installation is recognized, but the version is not - currently supported. - - """ - - def more_info(): # type: ignore - """Human-readable string to help the user. - - Should describe the steps taken and any relevant info to help the user - decide which plugin to use. - - :rtype str: - - """ - - -class IAuthenticator(IPlugin): - """Generic Certbot Authenticator. - - Class represents all possible tools processes that have the - ability to perform challenges and attain a certificate. - - """ - - def get_chall_pref(domain): - """Return `collections.Iterable` of challenge preferences. - - :param str domain: Domain for which challenge preferences are sought. - - :returns: `collections.Iterable` of challenge types (subclasses of - :class:`acme.challenges.Challenge`) with the most - preferred challenges first. If a type is not specified, it means the - Authenticator cannot perform the challenge. - :rtype: `collections.Iterable` - - """ - - def perform(achalls): - """Perform the given challenge. - - :param list achalls: Non-empty (guaranteed) list of - :class:`~certbot.achallenges.AnnotatedChallenge` - instances, such that it contains types found within - :func:`get_chall_pref` only. - - :returns: `collections.Iterable` of ACME - :class:`~acme.challenges.ChallengeResponse` instances - or if the :class:`~acme.challenges.Challenge` cannot - be fulfilled then: - - ``None`` - Authenticator can perform challenge, but not at this time. - ``False`` - Authenticator will never be able to perform (error). - - :rtype: :class:`collections.Iterable` of - :class:`acme.challenges.ChallengeResponse`, - where responses are required to be returned in - the same order as corresponding input challenges - - :raises .PluginError: If challenges cannot be performed - - """ - - def cleanup(achalls): - """Revert changes and shutdown after challenges complete. - - This method should be able to revert all changes made by - perform, even if perform exited abnormally. - - :param list achalls: Non-empty (guaranteed) list of - :class:`~certbot.achallenges.AnnotatedChallenge` - instances, a subset of those previously passed to :func:`perform`. - - :raises PluginError: if original configuration cannot be restored - - """ - - -class IConfig(zope.interface.Interface): - """Certbot user-supplied configuration. - - .. warning:: The values stored in the configuration have not been - filtered, stripped or sanitized. - - """ - server = zope.interface.Attribute("ACME Directory Resource URI.") - email = zope.interface.Attribute( - "Email used for registration and recovery contact. Use comma to " - "register multiple emails, ex: u1@example.com,u2@example.com. " - "(default: Ask).") - rsa_key_size = zope.interface.Attribute("Size of the RSA key.") - must_staple = zope.interface.Attribute( - "Adds the OCSP Must Staple extension to the certificate. " - "Autoconfigures OCSP Stapling for supported setups " - "(Apache version >= 2.3.3 ).") - - config_dir = zope.interface.Attribute("Configuration directory.") - work_dir = zope.interface.Attribute("Working directory.") - - accounts_dir = zope.interface.Attribute( - "Directory where all account information is stored.") - backup_dir = zope.interface.Attribute("Configuration backups directory.") - csr_dir = zope.interface.Attribute( - "Directory where newly generated Certificate Signing Requests " - "(CSRs) are saved.") - in_progress_dir = zope.interface.Attribute( - "Directory used before a permanent checkpoint is finalized.") - key_dir = zope.interface.Attribute("Keys storage.") - temp_checkpoint_dir = zope.interface.Attribute( - "Temporary checkpoint directory.") - - no_verify_ssl = zope.interface.Attribute( - "Disable verification of the ACME server's certificate.") - tls_sni_01_port = zope.interface.Attribute( - "Port used during tls-sni-01 challenge. " - "This only affects the port Certbot listens on. " - "A conforming ACME server will still attempt to connect on port 443.") - tls_sni_01_address = zope.interface.Attribute( - "The address the server listens to during tls-sni-01 challenge.") - - http01_port = zope.interface.Attribute( - "Port used in the http-01 challenge. " - "This only affects the port Certbot listens on. " - "A conforming ACME server will still attempt to connect on port 80.") - - http01_address = zope.interface.Attribute( - "The address the server listens to during http-01 challenge.") - - pref_challs = zope.interface.Attribute( - "Sorted user specified preferred challenges" - "type strings with the most preferred challenge listed first") - - allow_subset_of_names = zope.interface.Attribute( - "When performing domain validation, do not consider it a failure " - "if authorizations can not be obtained for a strict subset of " - "the requested domains. This may be useful for allowing renewals for " - "multiple domains to succeed even if some domains no longer point " - "at this system. This is a boolean") - - strict_permissions = zope.interface.Attribute( - "Require that all configuration files are owned by the current " - "user; only needed if your config is somewhere unsafe like /tmp/." - "This is a boolean") - - disable_renew_updates = zope.interface.Attribute( - "If updates provided by installer enhancements when Certbot is being run" - " with \"renew\" verb should be disabled.") - -class IInstaller(IPlugin): - """Generic Certbot Installer Interface. - - Represents any server that an X509 certificate can be placed. - - It is assumed that :func:`save` is the only method that finalizes a - checkpoint. This is important to ensure that checkpoints are - restored in a consistent manner if requested by the user or in case - of an error. - - Using :class:`certbot.reverter.Reverter` to implement checkpoints, - rollback, and recovery can dramatically simplify plugin development. - - """ - - def get_all_names(): # type: ignore - """Returns all names that may be authenticated. - - :rtype: `collections.Iterable` of `str` - - """ - - def deploy_cert(domain, cert_path, key_path, chain_path, fullchain_path): - """Deploy certificate. - - :param str domain: domain to deploy certificate file - :param str cert_path: absolute path to the certificate file - :param str key_path: absolute path to the private key file - :param str chain_path: absolute path to the certificate chain file - :param str fullchain_path: absolute path to the certificate fullchain - file (cert plus chain) - - :raises .PluginError: when cert cannot be deployed - - """ - - def enhance(domain, enhancement, options=None): - """Perform a configuration enhancement. - - :param str domain: domain for which to provide enhancement - :param str enhancement: An enhancement as defined in - :const:`~certbot.constants.ENHANCEMENTS` - :param options: Flexible options parameter for enhancement. - Check documentation of - :const:`~certbot.constants.ENHANCEMENTS` - for expected options for each enhancement. - - :raises .PluginError: If Enhancement is not supported, or if - an error occurs during the enhancement. - - """ - - def supported_enhancements(): # type: ignore - """Returns a `collections.Iterable` of supported enhancements. - - :returns: supported enhancements which should be a subset of - :const:`~certbot.constants.ENHANCEMENTS` - :rtype: :class:`collections.Iterable` of :class:`str` - - """ - - def save(title=None, temporary=False): - """Saves all changes to the configuration files. - - Both title and temporary are needed because a save may be - intended to be permanent, but the save is not ready to be a full - checkpoint. - - It is assumed that at most one checkpoint is finalized by this - method. Additionally, if an exception is raised, it is assumed a - new checkpoint was not finalized. - - :param str title: The title of the save. If a title is given, the - configuration will be saved as a new checkpoint and put in a - timestamped directory. `title` has no effect if temporary is true. - - :param bool temporary: Indicates whether the changes made will - be quickly reversed in the future (challenges) - - :raises .PluginError: when save is unsuccessful - - """ - - def rollback_checkpoints(rollback=1): - """Revert `rollback` number of configuration checkpoints. - - :raises .PluginError: when configuration cannot be fully reverted - - """ - - def recovery_routine(): # type: ignore - """Revert configuration to most recent finalized checkpoint. - - Remove all changes (temporary and permanent) that have not been - finalized. This is useful to protect against crashes and other - execution interruptions. - - :raises .errors.PluginError: If unable to recover the configuration - - """ - - def view_config_changes(): # type: ignore - """Display all of the LE config changes. - - :raises .PluginError: when config changes cannot be parsed - - """ - - def config_test(): # type: ignore - """Make sure the configuration is valid. - - :raises .MisconfigurationError: when the config is not in a usable state - - """ - - def restart(): # type: ignore - """Restart or refresh the server content. - - :raises .PluginError: when server cannot be restarted - - """ - - -class IDisplay(zope.interface.Interface): - """Generic display.""" - # pylint: disable=too-many-arguments - # see https://github.com/certbot/certbot/issues/3915 - - def notification(message, pause, wrap=True, force_interactive=False): - """Displays a string message - - :param str message: Message to display - :param bool pause: Whether or not the application should pause for - confirmation (if available) - :param bool wrap: Whether or not the application should wrap text - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - """ - - def menu(message, choices, ok_label=None, - cancel_label=None, help_label=None, - default=None, cli_flag=None, force_interactive=False): - """Displays a generic menu. - - When not setting force_interactive=True, you must provide a - default value. - - :param str message: message to display - - :param choices: choices - :type choices: :class:`list` of :func:`tuple` or :class:`str` - - :param str ok_label: label for OK button (UNUSED) - :param str cancel_label: label for Cancel button (UNUSED) - :param str help_label: label for Help button (UNUSED) - :param int default: default (non-interactive) choice from the menu - :param str cli_flag: to automate choice from the menu, eg "--keep" - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: tuple of (`code`, `index`) where - `code` - str display exit code - `index` - int index of the user's selection - - :raises errors.MissingCommandlineFlag: if called in non-interactive - mode without a default set - - """ - - def input(message, default=None, cli_args=None, force_interactive=False): - """Accept input from the user. - - When not setting force_interactive=True, you must provide a - default value. - - :param str message: message to display to the user - :param str default: default (non-interactive) response to prompt - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: tuple of (`code`, `input`) where - `code` - str display exit code - `input` - str of the user's input - :rtype: tuple - - :raises errors.MissingCommandlineFlag: if called in non-interactive - mode without a default set - - """ - - def yesno(message, yes_label="Yes", no_label="No", default=None, - cli_args=None, force_interactive=False): - """Query the user with a yes/no question. - - Yes and No label must begin with different letters. - - When not setting force_interactive=True, you must provide a - default value. - - :param str message: question for the user - :param str default: default (non-interactive) choice from the menu - :param str cli_flag: to automate choice from the menu, eg "--redirect / --no-redirect" - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: True for "Yes", False for "No" - :rtype: bool - - :raises errors.MissingCommandlineFlag: if called in non-interactive - mode without a default set - - """ - - def checklist(message, tags, default=None, cli_args=None, force_interactive=False): - """Allow for multiple selections from a menu. - - When not setting force_interactive=True, you must provide a - default value. - - :param str message: message to display to the user - :param list tags: where each is of type :class:`str` len(tags) > 0 - :param str default: default (non-interactive) state of the checklist - :param str cli_flag: to automate choice from the menu, eg "--domains" - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: tuple of the form (code, list_tags) where - `code` - int display exit code - `list_tags` - list of str tags selected by the user - :rtype: tuple - - :raises errors.MissingCommandlineFlag: if called in non-interactive - mode without a default set - - """ - - def directory_select(self, message, default=None, - cli_flag=None, force_interactive=False): - """Display a directory selection screen. - - When not setting force_interactive=True, you must provide a - default value. - - :param str message: prompt to give the user - :param default: the default value to return, if one exists, when - using the NoninteractiveDisplay - :param str cli_flag: option used to set this value with the CLI, - if one exists, to be included in error messages given by - NoninteractiveDisplay - :param bool force_interactive: True if it's safe to prompt the user - because it won't cause any workflow regressions - - :returns: tuple of the form (`code`, `string`) where - `code` - int display exit code - `string` - input entered by the user - - """ - - -class IValidator(zope.interface.Interface): - """Configuration validator.""" - - def certificate(cert, name, alt_host=None, port=443): - """Verifies the certificate presented at name is cert - - :param OpenSSL.crypto.X509 cert: Expected certificate - :param str name: Server's domain name - :param bytes alt_host: Host to connect to instead of the IP - address of host - :param int port: Port to connect to - - :returns: True if the certificate was verified successfully - :rtype: bool - - """ - - def redirect(name, port=80, headers=None): - """Verify redirect to HTTPS - - :param str name: Server's domain name - :param int port: Port to connect to - :param dict headers: HTTP headers to include in request - - :returns: True if redirect is successfully enabled - :rtype: bool - - """ - - def hsts(name): - """Verify HSTS header is enabled - - :param str name: Server's domain name - - :returns: True if HSTS header is successfully enabled - :rtype: bool - - """ - - def ocsp_stapling(name): - """Verify ocsp stapling for domain - - :param str name: Server's domain name - - :returns: True if ocsp stapling is successfully enabled - :rtype: bool - - """ - - -class IReporter(zope.interface.Interface): - """Interface to collect and display information to the user.""" - - HIGH_PRIORITY = zope.interface.Attribute( - "Used to denote high priority messages") - MEDIUM_PRIORITY = zope.interface.Attribute( - "Used to denote medium priority messages") - LOW_PRIORITY = zope.interface.Attribute( - "Used to denote low priority messages") - - def add_message(self, msg, priority, on_crash=True): - """Adds msg to the list of messages to be printed. - - :param str msg: Message to be displayed to the user. - - :param int priority: One of HIGH_PRIORITY, MEDIUM_PRIORITY, or - LOW_PRIORITY. - - :param bool on_crash: Whether or not the message should be printed if - the program exits abnormally. - - """ - - def print_messages(self): - """Prints messages to the user and clears the message queue.""" - - -# Updater interfaces -# -# When "certbot renew" is run, Certbot will iterate over each lineage and check -# if the selected installer for that lineage is a subclass of each updater -# class. If it is and the update of that type is configured to be run for that -# lineage, the relevant update function will be called for it. These functions -# are never called for other subcommands, so if an installer wants to perform -# an update during the run or install subcommand, it should do so when -# :func:`IInstaller.deploy_cert` is called. - -@six.add_metaclass(abc.ABCMeta) -class GenericUpdater(object): - """Interface for update types not currently specified by Certbot. - - This class allows plugins to perform types of updates that Certbot hasn't - defined (yet). - - To make use of this interface, the installer should implement the interface - methods, and interfaces.GenericUpdater.register(InstallerClass) should - be called from the installer code. - - The plugins implementing this enhancement are responsible of handling - the saving of configuration checkpoints as well as other calls to - interface methods of `interfaces.IInstaller` such as prepare() and restart() - """ - - @abc.abstractmethod - def generic_updates(self, lineage, *args, **kwargs): - """Perform any update types defined by the installer. - - If an installer is a subclass of the class containing this method, this - function will always be called when "certbot renew" is run. If the - update defined by the installer should be run conditionally, the - installer needs to handle checking the conditions itself. - - This method is called once for each lineage. - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - """ - - -@six.add_metaclass(abc.ABCMeta) -class RenewDeployer(object): - """Interface for update types run when a lineage is renewed - - This class allows plugins to perform types of updates that need to run at - lineage renewal that Certbot hasn't defined (yet). - - To make use of this interface, the installer should implement the interface - methods, and interfaces.RenewDeployer.register(InstallerClass) should - be called from the installer code. - """ - - @abc.abstractmethod - def renew_deploy(self, lineage, *args, **kwargs): - """Perform updates defined by installer when a certificate has been renewed - - If an installer is a subclass of the class containing this method, this - function will always be called when a certficate has been renewed by - running "certbot renew". For example if a plugin needs to copy a - certificate over, or change configuration based on the new certificate. - - This method is called once for each lineage renewed - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - """ diff --git a/certbot/lock.py b/certbot/lock.py deleted file mode 100644 index 3ff46518d95..00000000000 --- a/certbot/lock.py +++ /dev/null @@ -1,124 +0,0 @@ -"""Implements file locks for locking files and directories in UNIX.""" -import errno -import logging -import os - -from certbot import compat -from certbot import errors - -logger = logging.getLogger(__name__) - - -def lock_dir(dir_path): - """Place a lock file on the directory at dir_path. - - The lock file is placed in the root of dir_path with the name - .certbot.lock. - - :param str dir_path: path to directory - - :returns: the locked LockFile object - :rtype: LockFile - - :raises errors.LockError: if unable to acquire the lock - - """ - return LockFile(os.path.join(dir_path, '.certbot.lock')) - - -class LockFile(object): - """A UNIX lock file. - - This lock file is released when the locked file is closed or the - process exits. It cannot be used to provide synchronization between - threads. It is based on the lock_file package by Martin Horcicka. - - """ - def __init__(self, path): - """Initialize and acquire the lock file. - - :param str path: path to the file to lock - - :raises errors.LockError: if unable to acquire the lock - - """ - super(LockFile, self).__init__() - self._path = path - self._fd = None - - self.acquire() - - def acquire(self): - """Acquire the lock file. - - :raises errors.LockError: if lock is already held - :raises OSError: if unable to open or stat the lock file - - """ - while self._fd is None: - # Open the file - fd = os.open(self._path, os.O_CREAT | os.O_WRONLY, 0o600) - try: - self._try_lock(fd) - if self._lock_success(fd): - self._fd = fd - finally: - # Close the file if it is not the required one - if self._fd is None: - os.close(fd) - - def _try_lock(self, fd): - """Try to acquire the lock file without blocking. - - :param int fd: file descriptor of the opened file to lock - - """ - try: - compat.lock_file(fd) - except IOError as err: - if err.errno in (errno.EACCES, errno.EAGAIN): - logger.debug( - "A lock on %s is held by another process.", self._path) - raise errors.LockError( - "Another instance of Certbot is already running.") - raise - - def _lock_success(self, fd): - """Did we successfully grab the lock? - - Because this class deletes the locked file when the lock is - released, it is possible another process removed and recreated - the file between us opening the file and acquiring the lock. - - :param int fd: file descriptor of the opened file to lock - - :returns: True if the lock was successfully acquired - :rtype: bool - - """ - try: - stat1 = os.stat(self._path) - except OSError as err: - if err.errno == errno.ENOENT: - return False - raise - - stat2 = os.fstat(fd) - # If our locked file descriptor and the file on disk refer to - # the same device and inode, they're the same file. - return stat1.st_dev == stat2.st_dev and stat1.st_ino == stat2.st_ino - - def __repr__(self): - repr_str = '{0}({1}) <'.format(self.__class__.__name__, self._path) - if self._fd is None: - repr_str += 'released>' - else: - repr_str += 'acquired>' - return repr_str - - def release(self): - """Remove, close, and release the lock file.""" - try: - compat.release_locked_file(self._fd, self._path) - finally: - self._fd = None diff --git a/certbot/log.py b/certbot/log.py deleted file mode 100644 index b883936f30f..00000000000 --- a/certbot/log.py +++ /dev/null @@ -1,353 +0,0 @@ -"""Logging utilities for Certbot. - -The best way to use this module is through `pre_arg_parse_setup` and -`post_arg_parse_setup`. `pre_arg_parse_setup` configures a minimal -terminal logger and ensures a detailed log is written to a secure -temporary file if Certbot exits before `post_arg_parse_setup` is called. -`post_arg_parse_setup` relies on the parsed command line arguments and -does the full logging setup with terminal and rotating file handling as -configured by the user. Any logged messages before -`post_arg_parse_setup` is called are sent to the rotating file handler. -Special care is taken by both methods to ensure all errors are logged -and properly flushed before program exit. - -""" -from __future__ import print_function -import functools -import logging -import logging.handlers -import os -import sys -import tempfile -import traceback - -from acme import messages - -from certbot import compat -from certbot import constants -from certbot import errors -from certbot import util - -# Logging format -CLI_FMT = "%(message)s" -FILE_FMT = "%(asctime)s:%(levelname)s:%(name)s:%(message)s" - - -logger = logging.getLogger(__name__) - - -def pre_arg_parse_setup(): - """Setup logging before command line arguments are parsed. - - Terminal logging is setup using - `certbot.constants.QUIET_LOGGING_LEVEL` so Certbot is as quiet as - possible. File logging is setup so that logging messages are - buffered in memory. If Certbot exits before `post_arg_parse_setup` - is called, these buffered messages are written to a temporary file. - If Certbot doesn't exit, `post_arg_parse_setup` writes the messages - to the normal log files. - - This function also sets `logging.shutdown` to be called on program - exit which automatically flushes logging handlers and - `sys.excepthook` to properly log/display fatal exceptions. - - """ - temp_handler = TempHandler() - temp_handler.setFormatter(logging.Formatter(FILE_FMT)) - temp_handler.setLevel(logging.DEBUG) - memory_handler = MemoryHandler(temp_handler) - - stream_handler = ColoredStreamHandler() - stream_handler.setFormatter(logging.Formatter(CLI_FMT)) - stream_handler.setLevel(constants.QUIET_LOGGING_LEVEL) - - root_logger = logging.getLogger() - root_logger.setLevel(logging.DEBUG) # send all records to handlers - root_logger.addHandler(memory_handler) - root_logger.addHandler(stream_handler) - - # logging.shutdown will flush the memory handler because flush() and - # close() are explicitly called - util.atexit_register(logging.shutdown) - sys.excepthook = functools.partial( - pre_arg_parse_except_hook, memory_handler, - debug='--debug' in sys.argv, log_path=temp_handler.path) - - -def post_arg_parse_setup(config): - """Setup logging after command line arguments are parsed. - - This function assumes `pre_arg_parse_setup` was called earlier and - the root logging configuration has not been modified. A rotating - file logging handler is created and the buffered log messages are - sent to that handler. Terminal logging output is set to the level - requested by the user. - - :param certbot.interface.IConfig config: Configuration object - - """ - file_handler, file_path = setup_log_file_handler( - config, 'letsencrypt.log', FILE_FMT) - logs_dir = os.path.dirname(file_path) - - root_logger = logging.getLogger() - memory_handler = stderr_handler = None - for handler in root_logger.handlers: - if isinstance(handler, ColoredStreamHandler): - stderr_handler = handler - elif isinstance(handler, MemoryHandler): - memory_handler = handler - msg = 'Previously configured logging handlers have been removed!' - assert memory_handler is not None and stderr_handler is not None, msg - - root_logger.addHandler(file_handler) - root_logger.removeHandler(memory_handler) - temp_handler = memory_handler.target - memory_handler.setTarget(file_handler) - memory_handler.flush(force=True) - memory_handler.close() - temp_handler.close() - - if config.quiet: - level = constants.QUIET_LOGGING_LEVEL - else: - level = -config.verbose_count * 10 - stderr_handler.setLevel(level) - logger.debug('Root logging level set at %d', level) - logger.info('Saving debug log to %s', file_path) - - sys.excepthook = functools.partial( - post_arg_parse_except_hook, debug=config.debug, log_path=logs_dir) - - -def setup_log_file_handler(config, logfile, fmt): - """Setup file debug logging. - - :param certbot.interface.IConfig config: Configuration object - :param str logfile: basename for the log file - :param str fmt: logging format string - - :returns: file handler and absolute path to the log file - :rtype: tuple - - """ - # TODO: logs might contain sensitive data such as contents of the - # private key! #525 - util.set_up_core_dir( - config.logs_dir, 0o700, compat.os_geteuid(), config.strict_permissions) - log_file_path = os.path.join(config.logs_dir, logfile) - try: - handler = logging.handlers.RotatingFileHandler( - log_file_path, maxBytes=2 ** 20, - backupCount=config.max_log_backups) - except IOError as error: - raise errors.Error(util.PERM_ERR_FMT.format(error)) - # rotate on each invocation, rollover only possible when maxBytes - # is nonzero and backupCount is nonzero, so we set maxBytes as big - # as possible not to overrun in single CLI invocation (1MB). - handler.doRollover() # TODO: creates empty letsencrypt.log.1 file - handler.setLevel(logging.DEBUG) - handler_formatter = logging.Formatter(fmt=fmt) - handler.setFormatter(handler_formatter) - return handler, log_file_path - - -class ColoredStreamHandler(logging.StreamHandler): - """Sends colored logging output to a stream. - - If the specified stream is not a tty, the class works like the - standard `logging.StreamHandler`. Default red_level is - `logging.WARNING`. - - :ivar bool colored: True if output should be colored - :ivar bool red_level: The level at which to output - - """ - def __init__(self, stream=None): - super(ColoredStreamHandler, self).__init__(stream) - self.colored = (sys.stderr.isatty() if stream is None else - stream.isatty()) - self.red_level = logging.WARNING - - def format(self, record): - """Formats the string representation of record. - - :param logging.LogRecord record: Record to be formatted - - :returns: Formatted, string representation of record - :rtype: str - - """ - out = super(ColoredStreamHandler, self).format(record) - if self.colored and record.levelno >= self.red_level: - return ''.join((util.ANSI_SGR_RED, out, util.ANSI_SGR_RESET)) - else: - return out - - -class MemoryHandler(logging.handlers.MemoryHandler): - """Buffers logging messages in memory until the buffer is flushed. - - This differs from `logging.handlers.MemoryHandler` in that flushing - only happens when flush(force=True) is called. - - """ - def __init__(self, target=None, capacity=10000): - # capacity doesn't matter because should_flush() is overridden - super(MemoryHandler, self).__init__(capacity, target=target) - - def close(self): - """Close the memory handler, but don't set the target to None.""" - # This allows the logging module which may only have a weak - # reference to the target handler to properly flush and close it. - target = self.target - super(MemoryHandler, self).close() - self.target = target - - def flush(self, force=False): # pylint: disable=arguments-differ - """Flush the buffer if force=True. - - If force=False, this call is a noop. - - :param bool force: True if the buffer should be flushed. - - """ - # This method allows flush() calls in logging.shutdown to be a - # noop so we can control when this handler is flushed. - if force: - super(MemoryHandler, self).flush() - - def shouldFlush(self, record): - """Should the buffer be automatically flushed? - - :param logging.LogRecord record: log record to be considered - - :returns: False because the buffer should never be auto-flushed - :rtype: bool - - """ - return False - - -class TempHandler(logging.StreamHandler): - """Safely logs messages to a temporary file. - - The file is created with permissions 600. If no log records are sent - to this handler, the temporary file is deleted when the handler is - closed. - - :ivar str path: file system path to the temporary log file - - """ - def __init__(self): - stream = tempfile.NamedTemporaryFile('w', delete=False) - super(TempHandler, self).__init__(stream) - self.path = stream.name - self._delete = True - - def emit(self, record): - """Log the specified logging record. - - :param logging.LogRecord record: Record to be formatted - - """ - self._delete = False - super(TempHandler, self).emit(record) - - def close(self): - """Close the handler and the temporary log file. - - The temporary log file is deleted if it wasn't used. - - """ - self.acquire() - try: - # StreamHandler.close() doesn't close the stream to allow a - # stream like stderr to be used - self.stream.close() - if self._delete: - os.remove(self.path) - self._delete = False - super(TempHandler, self).close() - finally: - self.release() - - -def pre_arg_parse_except_hook(memory_handler, *args, **kwargs): - """A simple wrapper around post_arg_parse_except_hook. - - The additional functionality provided by this wrapper is the memory - handler will be flushed before Certbot exits. This allows us to - write logging messages to a temporary file if we crashed before - logging was fully configured. - - Since sys.excepthook isn't called on SystemExit exceptions, the - memory handler will not be flushed in this case which prevents us - from creating temporary log files when argparse exits because a - command line argument was invalid or -h, --help, or --version was - provided on the command line. - - :param MemoryHandler memory_handler: memory handler to flush - :param tuple args: args for post_arg_parse_except_hook - :param dict kwargs: kwargs for post_arg_parse_except_hook - - """ - try: - post_arg_parse_except_hook(*args, **kwargs) - finally: - # flush() is called here so messages logged during - # post_arg_parse_except_hook are also flushed. - memory_handler.flush(force=True) - - -def post_arg_parse_except_hook(exc_type, exc_value, trace, debug, log_path): - """Logs fatal exceptions and reports them to the user. - - If debug is True, the full exception and traceback is shown to the - user, otherwise, it is suppressed. sys.exit is always called with a - nonzero status. - - :param type exc_type: type of the raised exception - :param BaseException exc_value: raised exception - :param traceback trace: traceback of where the exception was raised - :param bool debug: True if the traceback should be shown to the user - :param str log_path: path to file or directory containing the log - - """ - exc_info = (exc_type, exc_value, trace) - # constants.QUIET_LOGGING_LEVEL or higher should be used to - # display message the user, otherwise, a lower level like - # logger.DEBUG should be used - if debug or not issubclass(exc_type, Exception): - assert constants.QUIET_LOGGING_LEVEL <= logging.ERROR - logger.error('Exiting abnormally:', exc_info=exc_info) - else: - logger.debug('Exiting abnormally:', exc_info=exc_info) - if issubclass(exc_type, errors.Error): - sys.exit(exc_value) - logger.error('An unexpected error occurred:') - if messages.is_acme_error(exc_value): - # Remove the ACME error prefix from the exception - _, _, exc_str = str(exc_value).partition(':: ') - logger.error(exc_str) - else: - traceback.print_exception(exc_type, exc_value, None) - exit_with_log_path(log_path) - - -def exit_with_log_path(log_path): - """Print a message about the log location and exit. - - The message is printed to stderr and the program will exit with a - nonzero status. - - :param str log_path: path to file or directory containing the log - - """ - msg = 'Please see the ' - if os.path.isdir(log_path): - msg += 'logfiles in {0} '.format(log_path) - else: - msg += "logfile '{0}' ".format(log_path) - msg += 'for more details.' - sys.exit(msg) diff --git a/certbot/main.py b/certbot/main.py deleted file mode 100644 index a0c0ab64de9..00000000000 --- a/certbot/main.py +++ /dev/null @@ -1,1372 +0,0 @@ -"""Certbot main entry point.""" -# pylint: disable=too-many-lines -from __future__ import print_function -import functools -import logging.handlers -import os -import sys - -import configobj -import josepy as jose -import zope.component - -from acme import errors as acme_errors -from acme.magic_typing import Union # pylint: disable=unused-import, no-name-in-module - -import certbot - -from certbot import account -from certbot import cert_manager -from certbot import cli -from certbot import client -from certbot import compat -from certbot import configuration -from certbot import constants -from certbot import crypto_util -from certbot import eff -from certbot import errors -from certbot import hooks -from certbot import interfaces -from certbot import log -from certbot import renewal -from certbot import reporter -from certbot import storage -from certbot import updater -from certbot import util - -from certbot.display import util as display_util, ops as display_ops -from certbot.plugins import disco as plugins_disco -from certbot.plugins import selection as plug_sel -from certbot.plugins import enhancements - -USER_CANCELLED = ("User chose to cancel the operation and may " - "reinvoke the client.") - - -logger = logging.getLogger(__name__) - - -def _suggest_donation_if_appropriate(config): - """Potentially suggest a donation to support Certbot. - - :param config: Configuration object - :type config: interfaces.IConfig - - :returns: `None` - :rtype: None - - """ - assert config.verb != "renew" - if config.staging: - # --dry-run implies --staging - return - reporter_util = zope.component.getUtility(interfaces.IReporter) - msg = ("If you like Certbot, please consider supporting our work by:\n\n" - "Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate\n" - "Donating to EFF: https://eff.org/donate-le\n\n") - reporter_util.add_message(msg, reporter_util.LOW_PRIORITY) - -def _report_successful_dry_run(config): - """Reports on successful dry run - - :param config: Configuration object - :type config: interfaces.IConfig - - :returns: `None` - :rtype: None - - """ - reporter_util = zope.component.getUtility(interfaces.IReporter) - assert config.verb != "renew" - reporter_util.add_message("The dry run was successful.", - reporter_util.HIGH_PRIORITY, on_crash=False) - - -def _get_and_save_cert(le_client, config, domains=None, certname=None, lineage=None): - """Authenticate and enroll certificate. - - This method finds the relevant lineage, figures out what to do with it, - then performs that action. Includes calls to hooks, various reports, - checks, and requests for user input. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param domains: List of domain names to get a certificate. Defaults to `None` - :type domains: `list` of `str` - - :param certname: Name of new certificate. Defaults to `None` - :type certname: str - - :param lineage: Certificate lineage object. Defaults to `None` - :type lineage: storage.RenewableCert - - :returns: the issued certificate or `None` if doing a dry run - :rtype: storage.RenewableCert or None - - :raises errors.Error: if certificate could not be obtained - - """ - hooks.pre_hook(config) - try: - if lineage is not None: - # Renewal, where we already know the specific lineage we're - # interested in - logger.info("Renewing an existing certificate") - renewal.renew_cert(config, domains, le_client, lineage) - else: - # TREAT AS NEW REQUEST - assert domains is not None - logger.info("Obtaining a new certificate") - lineage = le_client.obtain_and_enroll_certificate(domains, certname) - if lineage is False: - raise errors.Error("Certificate could not be obtained") - elif lineage is not None: - hooks.deploy_hook(config, lineage.names(), lineage.live_dir) - finally: - hooks.post_hook(config) - - return lineage - - -def _handle_subset_cert_request(config, domains, cert): - """Figure out what to do if a previous cert had a subset of the names now requested - - :param config: Configuration object - :type config: interfaces.IConfig - - :param domains: List of domain names - :type domains: `list` of `str` - - :param cert: Certificate object - :type cert: storage.RenewableCert - - :returns: Tuple of (str action, cert_or_None) as per _find_lineage_for_domains_and_certname - action can be: "newcert" | "renew" | "reinstall" - :rtype: `tuple` of `str` - - """ - existing = ", ".join(cert.names()) - question = ( - "You have an existing certificate that contains a portion of " - "the domains you requested (ref: {0}){br}{br}It contains these " - "names: {1}{br}{br}You requested these names for the new " - "certificate: {2}.{br}{br}Do you want to expand and replace this existing " - "certificate with the new certificate?" - ).format(cert.configfile.filename, - existing, - ", ".join(domains), - br=os.linesep) - if config.expand or config.renew_by_default or zope.component.getUtility( - interfaces.IDisplay).yesno(question, "Expand", "Cancel", - cli_flag="--expand", - force_interactive=True): - return "renew", cert - else: - reporter_util = zope.component.getUtility(interfaces.IReporter) - reporter_util.add_message( - "To obtain a new certificate that contains these names without " - "replacing your existing certificate for {0}, you must use the " - "--duplicate option.{br}{br}" - "For example:{br}{br}{1} --duplicate {2}".format( - existing, - sys.argv[0], " ".join(sys.argv[1:]), - br=os.linesep - ), - reporter_util.HIGH_PRIORITY) - raise errors.Error(USER_CANCELLED) - - -def _handle_identical_cert_request(config, lineage): - """Figure out what to do if a lineage has the same names as a previously obtained one - - :param config: Configuration object - :type config: interfaces.IConfig - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :returns: Tuple of (str action, cert_or_None) as per _find_lineage_for_domains_and_certname - action can be: "newcert" | "renew" | "reinstall" - :rtype: `tuple` of `str` - - """ - if not lineage.ensure_deployed(): - return "reinstall", lineage - if renewal.should_renew(config, lineage): - return "renew", lineage - if config.reinstall: - # Set with --reinstall, force an identical certificate to be - # reinstalled without further prompting. - return "reinstall", lineage - question = ( - "You have an existing certificate that has exactly the same " - "domains or certificate name you requested and isn't close to expiry." - "{br}(ref: {0}){br}{br}What would you like to do?" - ).format(lineage.configfile.filename, br=os.linesep) - - if config.verb == "run": - keep_opt = "Attempt to reinstall this existing certificate" - elif config.verb == "certonly": - keep_opt = "Keep the existing certificate for now" - choices = [keep_opt, - "Renew & replace the cert (limit ~5 per 7 days)"] - - display = zope.component.getUtility(interfaces.IDisplay) - response = display.menu(question, choices, - default=0, force_interactive=True) - if response[0] == display_util.CANCEL: - # TODO: Add notification related to command-line options for - # skipping the menu for this case. - raise errors.Error( - "Operation canceled. You may re-run the client.") - elif response[1] == 0: - return "reinstall", lineage - elif response[1] == 1: - return "renew", lineage - else: - assert False, "This is impossible" - -def _find_lineage_for_domains(config, domains): - """Determine whether there are duplicated names and how to handle - them (renew, reinstall, newcert, or raising an error to stop - the client run if the user chooses to cancel the operation when - prompted). - - :param config: Configuration object - :type config: interfaces.IConfig - - :param domains: List of domain names - :type domains: `list` of `str` - - :returns: Two-element tuple containing desired new-certificate behavior as - a string token ("reinstall", "renew", or "newcert"), plus either - a RenewableCert instance or `None` if renewal shouldn't occur. - :rtype: `tuple` of `str` and :class:`storage.RenewableCert` or `None` - - :raises errors.Error: If the user would like to rerun the client again. - - """ - # Considering the possibility that the requested certificate is - # related to an existing certificate. (config.duplicate, which - # is set with --duplicate, skips all of this logic and forces any - # kind of certificate to be obtained with renewal = False.) - if config.duplicate: - return "newcert", None - # TODO: Also address superset case - ident_names_cert, subset_names_cert = cert_manager.find_duplicative_certs(config, domains) - # XXX ^ schoen is not sure whether that correctly reads the systemwide - # configuration file. - if ident_names_cert is None and subset_names_cert is None: - return "newcert", None - - if ident_names_cert is not None: - return _handle_identical_cert_request(config, ident_names_cert) - elif subset_names_cert is not None: - return _handle_subset_cert_request(config, domains, subset_names_cert) - -def _find_cert(config, domains, certname): - """Finds an existing certificate object given domains and/or a certificate name. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param domains: List of domain names - :type domains: `list` of `str` - - :param certname: Name of certificate - :type certname: str - - :returns: Two-element tuple of a boolean that indicates if this function should be - followed by a call to fetch a certificate from the server, and either a - RenewableCert instance or None. - :rtype: `tuple` of `bool` and :class:`storage.RenewableCert` or `None` - - """ - action, lineage = _find_lineage_for_domains_and_certname(config, domains, certname) - if action == "reinstall": - logger.info("Keeping the existing certificate") - return (action != "reinstall"), lineage - -def _find_lineage_for_domains_and_certname(config, domains, certname): - """Find appropriate lineage based on given domains and/or certname. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param domains: List of domain names - :type domains: `list` of `str` - - :param certname: Name of certificate - :type certname: str - - :returns: Two-element tuple containing desired new-certificate behavior as - a string token ("reinstall", "renew", or "newcert"), plus either - a RenewableCert instance or None if renewal should not occur. - - :rtype: `tuple` of `str` and :class:`storage.RenewableCert` or `None` - - :raises errors.Error: If the user would like to rerun the client again. - - """ - if not certname: - return _find_lineage_for_domains(config, domains) - else: - lineage = cert_manager.lineage_for_certname(config, certname) - if lineage: - if domains: - if set(cert_manager.domains_for_certname(config, certname)) != set(domains): - _ask_user_to_confirm_new_names(config, domains, certname, - lineage.names()) # raises if no - return "renew", lineage - # unnecessarily specified domains or no domains specified - return _handle_identical_cert_request(config, lineage) - else: - if domains: - return "newcert", None - else: - raise errors.ConfigurationError("No certificate with name {0} found. " - "Use -d to specify domains, or run certbot certificates to see " - "possible certificate names.".format(certname)) - -def _get_added_removed(after, before): - """Get lists of items removed from `before` - and a lists of items added to `after` - """ - added = list(set(after) - set(before)) - removed = list(set(before) - set(after)) - added.sort() - removed.sort() - return added, removed - -def _format_list(character, strings): - """Format list with given character - """ - if len(strings) == 0: - formatted = "{br}(None)" - else: - formatted = "{br}{ch} " + "{br}{ch} ".join(strings) - return formatted.format( - ch=character, - br=os.linesep - ) - -def _ask_user_to_confirm_new_names(config, new_domains, certname, old_domains): - """Ask user to confirm update cert certname to contain new_domains. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param new_domains: List of new domain names - :type new_domains: `list` of `str` - - :param certname: Name of certificate - :type certname: str - - :param old_domains: List of old domain names - :type old_domains: `list` of `str` - - :returns: None - :rtype: None - - :raises errors.ConfigurationError: if cert name and domains mismatch - - """ - if config.renew_with_new_domains: - return - - added, removed = _get_added_removed(new_domains, old_domains) - - msg = ("You are updating certificate {0} to include new domain(s): {1}{br}{br}" - "You are also removing previously included domain(s): {2}{br}{br}" - "Did you intend to make this change?".format( - certname, - _format_list("+", added), - _format_list("-", removed), - br=os.linesep)) - obj = zope.component.getUtility(interfaces.IDisplay) - if not obj.yesno(msg, "Update cert", "Cancel", default=True): - raise errors.ConfigurationError("Specified mismatched cert name and domains.") - -def _find_domains_or_certname(config, installer, question=None): - """Retrieve domains and certname from config or user input. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param installer: Installer object - :type installer: interfaces.IInstaller - - :param `str` question: Overriding dialog question to ask the user if asked - to choose from domain names. - - :returns: Two-part tuple of domains and certname - :rtype: `tuple` of list of `str` and `str` - - :raises errors.Error: Usage message, if parameters are not used correctly - - """ - domains = None - certname = config.certname - # first, try to get domains from the config - if config.domains: - domains = config.domains - # if we can't do that but we have a certname, get the domains - # with that certname - elif certname: - domains = cert_manager.domains_for_certname(config, certname) - - # that certname might not have existed, or there was a problem. - # try to get domains from the user. - if not domains: - domains = display_ops.choose_names(installer, question) - - if not domains and not certname: - raise errors.Error("Please specify --domains, or --installer that " - "will help in domain names autodiscovery, or " - "--cert-name for an existing certificate name.") - - return domains, certname - - -def _report_new_cert(config, cert_path, fullchain_path, key_path=None): - """Reports the creation of a new certificate to the user. - - :param cert_path: path to certificate - :type cert_path: str - - :param fullchain_path: path to full chain - :type fullchain_path: str - - :param key_path: path to private key, if available - :type key_path: str - - :returns: `None` - :rtype: None - - """ - if config.dry_run: - _report_successful_dry_run(config) - return - - assert cert_path and fullchain_path, "No certificates saved to report." - - expiry = crypto_util.notAfter(cert_path).date() - reporter_util = zope.component.getUtility(interfaces.IReporter) - # Print the path to fullchain.pem because that's what modern webservers - # (Nginx and Apache2.4) will want. - - verbswitch = ' with the "certonly" option' if config.verb == "run" else "" - privkey_statement = 'Your key file has been saved at:{br}{0}{br}'.format( - key_path, br=os.linesep) if key_path else "" - # XXX Perhaps one day we could detect the presence of known old webservers - # and say something more informative here. - msg = ('Congratulations! Your certificate and chain have been saved at:{br}' - '{0}{br}{1}' - 'Your cert will expire on {2}. To obtain a new or tweaked version of this ' - 'certificate in the future, simply run {3} again{4}. ' - 'To non-interactively renew *all* of your certificates, run "{3} renew"' - .format(fullchain_path, privkey_statement, expiry, cli.cli_command, verbswitch, - br=os.linesep)) - reporter_util.add_message(msg, reporter_util.MEDIUM_PRIORITY) - - -def _determine_account(config): - """Determine which account to use. - - If ``config.account`` is ``None``, it will be updated based on the - user input. Same for ``config.email``. - - :param config: Configuration object - :type config: interfaces.IConfig - - :returns: Account and optionally ACME client API (biproduct of new - registration). - :rtype: tuple of :class:`certbot.account.Account` and :class:`acme.client.Client` - - :raises errors.Error: If unable to register an account with ACME server - - """ - def _tos_cb(terms_of_service): - if config.tos: - return True - msg = ("Please read the Terms of Service at {0}. You " - "must agree in order to register with the ACME " - "server at {1}".format( - terms_of_service, config.server)) - obj = zope.component.getUtility(interfaces.IDisplay) - result = obj.yesno(msg, "Agree", "Cancel", - cli_flag="--agree-tos", force_interactive=True) - if not result: - raise errors.Error( - "Registration cannot proceed without accepting " - "Terms of Service.") - - account_storage = account.AccountFileStorage(config) - acme = None - - if config.account is not None: - acc = account_storage.load(config.account) - else: - accounts = account_storage.find_all() - if len(accounts) > 1: - acc = display_ops.choose_account(accounts) - elif len(accounts) == 1: - acc = accounts[0] - else: # no account registered yet - if config.email is None and not config.register_unsafely_without_email: - config.email = display_ops.get_email() - try: - acc, acme = client.register( - config, account_storage, tos_cb=_tos_cb) - except errors.MissingCommandlineFlag: - raise - except errors.Error: - logger.debug("", exc_info=True) - raise errors.Error( - "Unable to register an account with ACME server") - - config.account = acc.id - return acc, acme - - -def _delete_if_appropriate(config): # pylint: disable=too-many-locals,too-many-branches - """Does the user want to delete their now-revoked certs? If run in non-interactive mode, - deleting happens automatically. - - :param config: parsed command line arguments - :type config: interfaces.IConfig - - :returns: `None` - :rtype: None - - :raises errors.Error: If anything goes wrong, including bad user input, if an overlapping - archive dir is found for the specified lineage, etc ... - """ - display = zope.component.getUtility(interfaces.IDisplay) - reporter_util = zope.component.getUtility(interfaces.IReporter) - - attempt_deletion = config.delete_after_revoke - if attempt_deletion is None: - msg = ("Would you like to delete the cert(s) you just revoked, along with all earlier and " - "later versions of the cert?") - attempt_deletion = display.yesno(msg, yes_label="Yes (recommended)", no_label="No", - force_interactive=True, default=True) - - if not attempt_deletion: - reporter_util.add_message("Not deleting revoked certs.", reporter_util.LOW_PRIORITY) - return - - # config.cert_path must have been set - # config.certname may have been set - assert config.cert_path - - if not config.certname: - config.certname = cert_manager.cert_path_to_lineage(config) - - # don't delete if the archive_dir is used by some other lineage - archive_dir = storage.full_archive_path( - configobj.ConfigObj(storage.renewal_file_for_certname(config, config.certname)), - config, config.certname) - try: - cert_manager.match_and_check_overlaps(config, [lambda x: archive_dir], - lambda x: x.archive_dir, lambda x: x) - except errors.OverlappingMatchFound: - msg = ('Not deleting revoked certs due to overlapping archive dirs. More than ' - 'one lineage is using {0}'.format(archive_dir)) - reporter_util.add_message(''.join(msg), reporter_util.MEDIUM_PRIORITY) - return - except Exception as e: - msg = ('config.default_archive_dir: {0}, config.live_dir: {1}, archive_dir: {2},' - 'original exception: {3}') - msg = msg.format(config.default_archive_dir, config.live_dir, archive_dir, e) - raise errors.Error(msg) - - cert_manager.delete(config) - - -def _init_le_client(config, authenticator, installer): - """Initialize Let's Encrypt Client - - :param config: Configuration object - :type config: interfaces.IConfig - - :param authenticator: Acme authentication handler - :type authenticator: interfaces.IAuthenticator - :param installer: Installer object - :type installer: interfaces.IInstaller - - :returns: client: Client object - :rtype: client.Client - - """ - if authenticator is not None: - # if authenticator was given, then we will need account... - acc, acme = _determine_account(config) - logger.debug("Picked account: %r", acc) - # XXX - #crypto_util.validate_key_csr(acc.key) - else: - acc, acme = None, None - - return client.Client(config, acc, authenticator, installer, acme=acme) - - -def unregister(config, unused_plugins): - """Deactivate account on server - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - account_storage = account.AccountFileStorage(config) - accounts = account_storage.find_all() - reporter_util = zope.component.getUtility(interfaces.IReporter) - - if not accounts: - return "Could not find existing account to deactivate." - yesno = zope.component.getUtility(interfaces.IDisplay).yesno - prompt = ("Are you sure you would like to irrevocably deactivate " - "your account?") - wants_deactivate = yesno(prompt, yes_label='Deactivate', no_label='Abort', - default=True) - - if not wants_deactivate: - return "Deactivation aborted." - - acc, acme = _determine_account(config) - cb_client = client.Client(config, acc, None, None, acme=acme) - - # delete on boulder - cb_client.acme.deactivate_registration(acc.regr) - account_files = account.AccountFileStorage(config) - # delete local account files - account_files.delete(config.account) - - reporter_util.add_message("Account deactivated.", reporter_util.MEDIUM_PRIORITY) - - -def register(config, unused_plugins): - """Create accounts on the server. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` or a string indicating and error - :rtype: None or str - - """ - # TODO: When `certbot register --update-registration` is fully deprecated, - # delete the true case of if block - if config.update_registration: - msg = ("Usage 'certbot register --update-registration' is deprecated.\n" - "Please use 'cerbot update_account [options]' instead.\n") - logger.warning(msg) - return update_account(config, unused_plugins) - - # Portion of _determine_account logic to see whether accounts already - # exist or not. - account_storage = account.AccountFileStorage(config) - accounts = account_storage.find_all() - - if len(accounts) > 0: - # TODO: add a flag to register a duplicate account (this will - # also require extending _determine_account's behavior - # or else extracting the registration code from there) - return ("There is an existing account; registration of a " - "duplicate account with this command is currently " - "unsupported.") - # _determine_account will register an account - _determine_account(config) - return - - -def update_account(config, unused_plugins): - """Modify accounts on the server. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` or a string indicating and error - :rtype: None or str - - """ - # Portion of _determine_account logic to see whether accounts already - # exist or not. - account_storage = account.AccountFileStorage(config) - accounts = account_storage.find_all() - reporter_util = zope.component.getUtility(interfaces.IReporter) - add_msg = lambda m: reporter_util.add_message(m, reporter_util.MEDIUM_PRIORITY) - - if len(accounts) == 0: - return "Could not find an existing account to update." - if config.email is None: - if config.register_unsafely_without_email: - return ("--register-unsafely-without-email provided, however, a " - "new e-mail address must\ncurrently be provided when " - "updating a registration.") - config.email = display_ops.get_email(optional=False) - - acc, acme = _determine_account(config) - cb_client = client.Client(config, acc, None, None, acme=acme) - # We rely on an exception to interrupt this process if it didn't work. - acc_contacts = ['mailto:' + email for email in config.email.split(',')] - prev_regr_uri = acc.regr.uri - acc.regr = cb_client.acme.update_registration(acc.regr.update( - body=acc.regr.body.update(contact=acc_contacts))) - # A v1 account being used as a v2 account will result in changing the uri to - # the v2 uri. Since it's the same object on disk, put it back to the v1 uri - # so that we can also continue to use the account object with acmev1. - acc.regr = acc.regr.update(uri=prev_regr_uri) - account_storage.save_regr(acc, cb_client.acme) - eff.handle_subscription(config) - add_msg("Your e-mail address was updated to {0}.".format(config.email)) - -def _install_cert(config, le_client, domains, lineage=None): - """Install a cert - - :param config: Configuration object - :type config: interfaces.IConfig - - :param le_client: Client object - :type le_client: client.Client - - :param domains: List of domains - :type domains: `list` of `str` - - :param lineage: Certificate lineage object. Defaults to `None` - :type lineage: storage.RenewableCert - - :returns: `None` - :rtype: None - - """ - path_provider = lineage if lineage else config - assert path_provider.cert_path is not None - - le_client.deploy_certificate(domains, path_provider.key_path, - path_provider.cert_path, path_provider.chain_path, path_provider.fullchain_path) - le_client.enhance_config(domains, path_provider.chain_path) - -def install(config, plugins): - """Install a previously obtained cert in a server. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param plugins: List of plugins - :type plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - # XXX: Update for renewer/RenewableCert - # FIXME: be consistent about whether errors are raised or returned from - # this function ... - - try: - installer, _ = plug_sel.choose_configurator_plugins(config, plugins, "install") - except errors.PluginSelectionError as e: - return str(e) - - custom_cert = (config.key_path and config.cert_path) - if not config.certname and not custom_cert: - certname_question = "Which certificate would you like to install?" - config.certname = cert_manager.get_certnames( - config, "install", allow_multiple=False, - custom_prompt=certname_question)[0] - - if not enhancements.are_supported(config, installer): - raise errors.NotSupportedError("One ore more of the requested enhancements " - "are not supported by the selected installer") - # If cert-path is defined, populate missing (ie. not overridden) values. - # Unfortunately this can't be done in argument parser, as certificate - # manager needs the access to renewal directory paths - if config.certname: - config = _populate_from_certname(config) - elif enhancements.are_requested(config): - # Preflight config check - raise errors.ConfigurationError("One or more of the requested enhancements " - "require --cert-name to be provided") - - if config.key_path and config.cert_path: - _check_certificate_and_key(config) - domains, _ = _find_domains_or_certname(config, installer) - le_client = _init_le_client(config, authenticator=None, installer=installer) - _install_cert(config, le_client, domains) - else: - raise errors.ConfigurationError("Path to certificate or key was not defined. " - "If your certificate is managed by Certbot, please use --cert-name " - "to define which certificate you would like to install.") - - if enhancements.are_requested(config): - # In the case where we don't have certname, we have errored out already - lineage = cert_manager.lineage_for_certname(config, config.certname) - enhancements.enable(lineage, domains, installer, config) - -def _populate_from_certname(config): - """Helper function for install to populate missing config values from lineage - defined by --cert-name.""" - - lineage = cert_manager.lineage_for_certname(config, config.certname) - if not lineage: - return config - if not config.key_path: - config.namespace.key_path = lineage.key_path - if not config.cert_path: - config.namespace.cert_path = lineage.cert_path - if not config.chain_path: - config.namespace.chain_path = lineage.chain_path - if not config.fullchain_path: - config.namespace.fullchain_path = lineage.fullchain_path - return config - -def _check_certificate_and_key(config): - if not os.path.isfile(os.path.realpath(config.cert_path)): - raise errors.ConfigurationError("Error while reading certificate from path " - "{0}".format(config.cert_path)) - if not os.path.isfile(os.path.realpath(config.key_path)): - raise errors.ConfigurationError("Error while reading private key from path " - "{0}".format(config.key_path)) -def plugins_cmd(config, plugins): - """List server software plugins. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param plugins: List of plugins - :type plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - logger.debug("Expected interfaces: %s", config.ifaces) - - ifaces = [] if config.ifaces is None else config.ifaces - filtered = plugins.visible().ifaces(ifaces) - logger.debug("Filtered plugins: %r", filtered) - - notify = functools.partial(zope.component.getUtility( - interfaces.IDisplay).notification, pause=False) - if not config.init and not config.prepare: - notify(str(filtered)) - return - - filtered.init(config) - verified = filtered.verify(ifaces) - logger.debug("Verified plugins: %r", verified) - - if not config.prepare: - notify(str(verified)) - return - - verified.prepare() - available = verified.available() - logger.debug("Prepared plugins: %s", available) - notify(str(available)) - -def enhance(config, plugins): - """Add security enhancements to existing configuration - - :param config: Configuration object - :type config: interfaces.IConfig - - :param plugins: List of plugins - :type plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - supported_enhancements = ["hsts", "redirect", "uir", "staple"] - # Check that at least one enhancement was requested on command line - oldstyle_enh = any([getattr(config, enh) for enh in supported_enhancements]) - if not enhancements.are_requested(config) and not oldstyle_enh: - msg = ("Please specify one or more enhancement types to configure. To list " - "the available enhancement types, run:\n\n%s --help enhance\n") - logger.warning(msg, sys.argv[0]) - raise errors.MisconfigurationError("No enhancements requested, exiting.") - - try: - installer, _ = plug_sel.choose_configurator_plugins(config, plugins, "enhance") - except errors.PluginSelectionError as e: - return str(e) - - if not enhancements.are_supported(config, installer): - raise errors.NotSupportedError("One ore more of the requested enhancements " - "are not supported by the selected installer") - - certname_question = ("Which certificate would you like to use to enhance " - "your configuration?") - config.certname = cert_manager.get_certnames( - config, "enhance", allow_multiple=False, - custom_prompt=certname_question)[0] - cert_domains = cert_manager.domains_for_certname(config, config.certname) - if config.noninteractive_mode: - domains = cert_domains - else: - domain_question = ("Which domain names would you like to enable the " - "selected enhancements for?") - domains = display_ops.choose_values(cert_domains, domain_question) - if not domains: - raise errors.Error("User cancelled the domain selection. No domains " - "defined, exiting.") - - lineage = cert_manager.lineage_for_certname(config, config.certname) - if not config.chain_path: - config.chain_path = lineage.chain_path - if oldstyle_enh: - le_client = _init_le_client(config, authenticator=None, installer=installer) - le_client.enhance_config(domains, config.chain_path, ask_redirect=False) - if enhancements.are_requested(config): - enhancements.enable(lineage, domains, installer, config) - - -def rollback(config, plugins): - """Rollback server configuration changes made during install. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param plugins: List of plugins - :type plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - client.rollback(config.installer, config.checkpoints, config, plugins) - - -def config_changes(config, unused_plugins): - """Show changes made to server config during installation - - View checkpoints and associated configuration changes. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - client.view_config_changes(config, num=config.num) - -def update_symlinks(config, unused_plugins): - """Update the certificate file family symlinks - - Use the information in the config file to make symlinks point to - the correct archive directory. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - cert_manager.update_live_symlinks(config) - -def rename(config, unused_plugins): - """Rename a certificate - - Use the information in the config file to rename an existing - lineage. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - cert_manager.rename_lineage(config) - -def delete(config, unused_plugins): - """Delete a certificate - - Use the information in the config file to delete an existing - lineage. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - cert_manager.delete(config) - -def certificates(config, unused_plugins): - """Display information about certs configured with Certbot - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - cert_manager.certificates(config) - -def revoke(config, unused_plugins): # TODO: coop with renewal config - """Revoke a previously obtained certificate. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` or string indicating error in case of error - :rtype: None or str - - """ - # For user-agent construction - config.installer = config.authenticator = None - - if config.cert_path is None and config.certname: - config.cert_path = storage.cert_path_for_cert_name(config, config.certname) - elif not config.cert_path or (config.cert_path and config.certname): - # intentionally not supporting --cert-path & --cert-name together, - # to avoid dealing with mismatched values - raise errors.Error("Error! Exactly one of --cert-path or --cert-name must be specified!") - - if config.key_path is not None: # revocation by cert key - logger.debug("Revoking %s using cert key %s", - config.cert_path[0], config.key_path[0]) - crypto_util.verify_cert_matches_priv_key(config.cert_path[0], config.key_path[0]) - key = jose.JWK.load(config.key_path[1]) - acme = client.acme_from_config_key(config, key) - else: # revocation by account key - logger.debug("Revoking %s using Account Key", config.cert_path[0]) - acc, _ = _determine_account(config) - acme = client.acme_from_config_key(config, acc.key, acc.regr) - cert = crypto_util.pyopenssl_load_certificate(config.cert_path[1])[0] - logger.debug("Reason code for revocation: %s", config.reason) - try: - acme.revoke(jose.ComparableX509(cert), config.reason) - _delete_if_appropriate(config) - except acme_errors.ClientError as e: - return str(e) - - display_ops.success_revocation(config.cert_path[0]) - - -def run(config, plugins): # pylint: disable=too-many-branches,too-many-locals - """Obtain a certificate and install. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param plugins: List of plugins - :type plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - # TODO: Make run as close to auth + install as possible - # Possible difficulties: config.csr was hacked into auth - try: - installer, authenticator = plug_sel.choose_configurator_plugins(config, plugins, "run") - except errors.PluginSelectionError as e: - return str(e) - - # Preflight check for enhancement support by the selected installer - if not enhancements.are_supported(config, installer): - raise errors.NotSupportedError("One ore more of the requested enhancements " - "are not supported by the selected installer") - - # TODO: Handle errors from _init_le_client? - le_client = _init_le_client(config, authenticator, installer) - - domains, certname = _find_domains_or_certname(config, installer) - should_get_cert, lineage = _find_cert(config, domains, certname) - - new_lineage = lineage - if should_get_cert: - new_lineage = _get_and_save_cert(le_client, config, domains, - certname, lineage) - - cert_path = new_lineage.cert_path if new_lineage else None - fullchain_path = new_lineage.fullchain_path if new_lineage else None - key_path = new_lineage.key_path if new_lineage else None - _report_new_cert(config, cert_path, fullchain_path, key_path) - - _install_cert(config, le_client, domains, new_lineage) - - if enhancements.are_requested(config) and new_lineage: - enhancements.enable(new_lineage, domains, installer, config) - - if lineage is None or not should_get_cert: - display_ops.success_installation(domains) - else: - display_ops.success_renewal(domains) - - _suggest_donation_if_appropriate(config) - - -def _csr_get_and_save_cert(config, le_client): - """Obtain a cert using a user-supplied CSR - - This works differently in the CSR case (for now) because we don't - have the privkey, and therefore can't construct the files for a lineage. - So we just save the cert & chain to disk :/ - - :param config: Configuration object - :type config: interfaces.IConfig - - :param client: Client object - :type client: client.Client - - :returns: `cert_path` and `fullchain_path` as absolute paths to the actual files - :rtype: `tuple` of `str` - - """ - csr, _ = config.actual_csr - cert, chain = le_client.obtain_certificate_from_csr(csr) - if config.dry_run: - logger.debug( - "Dry run: skipping saving certificate to %s", config.cert_path) - return None, None - cert_path, _, fullchain_path = le_client.save_certificate( - cert, chain, os.path.normpath(config.cert_path), - os.path.normpath(config.chain_path), os.path.normpath(config.fullchain_path)) - return cert_path, fullchain_path - -def renew_cert(config, plugins, lineage): - """Renew & save an existing cert. Do not install it. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param plugins: List of plugins - :type plugins: `list` of `str` - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :returns: `None` - :rtype: None - - :raises errors.PluginSelectionError: MissingCommandlineFlag if supplied parameters do not pass - - """ - try: - # installers are used in auth mode to determine domain names - installer, auth = plug_sel.choose_configurator_plugins(config, plugins, "certonly") - except errors.PluginSelectionError as e: - logger.info("Could not choose appropriate plugin: %s", e) - raise - le_client = _init_le_client(config, auth, installer) - - renewed_lineage = _get_and_save_cert(le_client, config, lineage=lineage) - - notify = zope.component.getUtility(interfaces.IDisplay).notification - if installer is None: - notify("new certificate deployed without reload, fullchain is {0}".format( - lineage.fullchain), pause=False) - else: - # In case of a renewal, reload server to pick up new certificate. - # In principle we could have a configuration option to inhibit this - # from happening. - # Run deployer - updater.run_renewal_deployer(config, renewed_lineage, installer) - installer.restart() - notify("new certificate deployed with reload of {0} server; fullchain is {1}".format( - config.installer, lineage.fullchain), pause=False) - -def certonly(config, plugins): - """Authenticate & obtain cert, but do not install it. - - This implements the 'certonly' subcommand. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param plugins: List of plugins - :type plugins: `list` of `str` - - :returns: `None` - :rtype: None - - :raises errors.Error: If specified plugin could not be used - - """ - # SETUP: Select plugins and construct a client instance - try: - # installers are used in auth mode to determine domain names - installer, auth = plug_sel.choose_configurator_plugins(config, plugins, "certonly") - except errors.PluginSelectionError as e: - logger.info("Could not choose appropriate plugin: %s", e) - raise - - le_client = _init_le_client(config, auth, installer) - - if config.csr: - cert_path, fullchain_path = _csr_get_and_save_cert(config, le_client) - _report_new_cert(config, cert_path, fullchain_path) - _suggest_donation_if_appropriate(config) - return - - domains, certname = _find_domains_or_certname(config, installer) - should_get_cert, lineage = _find_cert(config, domains, certname) - - if not should_get_cert: - notify = zope.component.getUtility(interfaces.IDisplay).notification - notify("Certificate not yet due for renewal; no action taken.", pause=False) - return - - lineage = _get_and_save_cert(le_client, config, domains, certname, lineage) - - cert_path = lineage.cert_path if lineage else None - fullchain_path = lineage.fullchain_path if lineage else None - key_path = lineage.key_path if lineage else None - _report_new_cert(config, cert_path, fullchain_path, key_path) - _suggest_donation_if_appropriate(config) - -def renew(config, unused_plugins): - """Renew previously-obtained certificates. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param unused_plugins: List of plugins (deprecated) - :type unused_plugins: `list` of `str` - - :returns: `None` - :rtype: None - - """ - try: - renewal.handle_renewal_request(config) - finally: - hooks.run_saved_post_hooks() - - -def make_or_verify_needed_dirs(config): - """Create or verify existence of config, work, and hook directories. - - :param config: Configuration object - :type config: interfaces.IConfig - - :returns: `None` - :rtype: None - - """ - util.set_up_core_dir(config.config_dir, constants.CONFIG_DIRS_MODE, - compat.os_geteuid(), config.strict_permissions) - util.set_up_core_dir(config.work_dir, constants.CONFIG_DIRS_MODE, - compat.os_geteuid(), config.strict_permissions) - - hook_dirs = (config.renewal_pre_hooks_dir, - config.renewal_deploy_hooks_dir, - config.renewal_post_hooks_dir,) - for hook_dir in hook_dirs: - util.make_or_verify_dir(hook_dir, - uid=compat.os_geteuid(), - strict=config.strict_permissions) - - -def set_displayer(config): - """Set the displayer - - :param config: Configuration object - :type config: interfaces.IConfig - - :returns: `None` - :rtype: None - - """ - if config.quiet: - config.noninteractive_mode = True - displayer = display_util.NoninteractiveDisplay(open(os.devnull, "w")) \ - # type: Union[None, display_util.NoninteractiveDisplay, display_util.FileDisplay] - elif config.noninteractive_mode: - displayer = display_util.NoninteractiveDisplay(sys.stdout) - else: - displayer = display_util.FileDisplay(sys.stdout, - config.force_interactive) - zope.component.provideUtility(displayer) - - -def main(cli_args=sys.argv[1:]): - """Command line argument parsing and main script execution. - - :returns: result of requested command - - :raises errors.Error: OS errors triggered by wrong permissions - :raises errors.Error: error if plugin command is not supported - - """ - - log.pre_arg_parse_setup() - - plugins = plugins_disco.PluginsRegistry.find_all() - logger.debug("certbot version: %s", certbot.__version__) - # do not log `config`, as it contains sensitive data (e.g. revoke --key)! - logger.debug("Arguments: %r", cli_args) - logger.debug("Discovered plugins: %r", plugins) - - # note: arg parser internally handles --help (and exits afterwards) - args = cli.prepare_and_parse_args(plugins, cli_args) - config = configuration.NamespaceConfig(args) - zope.component.provideUtility(config) - - # On windows, shell without administrative right cannot create symlinks required by certbot. - # So we check the rights before continuing. - compat.raise_for_non_administrative_windows_rights(config.verb) - - try: - log.post_arg_parse_setup(config) - make_or_verify_needed_dirs(config) - except errors.Error: - # Let plugins_cmd be run as un-privileged user. - if config.func != plugins_cmd: - raise - - set_displayer(config) - - # Reporter - report = reporter.Reporter(config) - zope.component.provideUtility(report) - util.atexit_register(report.print_messages) - - return config.func(config, plugins) - - -if __name__ == "__main__": - err_string = main() - if err_string: - logger.warning("Exiting with message %s", err_string) - sys.exit(err_string) # pragma: no cover diff --git a/certbot/notify.py b/certbot/notify.py deleted file mode 100644 index dda0a85af02..00000000000 --- a/certbot/notify.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Send e-mail notification to system administrators.""" - -import email -import smtplib -import socket -import subprocess - - -def notify(subject, whom, what): - """Send email notification. - - Try to notify the addressee (``whom``) by e-mail, with Subject: - defined by ``subject`` and message body by ``what``. - - """ - msg = email.message_from_string(what) - msg.add_header("From", "Certbot renewal agent ") - msg.add_header("To", whom) - msg.add_header("Subject", subject) - msg = msg.as_string() - try: - lmtp = smtplib.LMTP() - lmtp.connect() - lmtp.sendmail("root", [whom], msg) - except (smtplib.SMTPHeloError, smtplib.SMTPRecipientsRefused, - smtplib.SMTPSenderRefused, smtplib.SMTPDataError, socket.error): - # We should try using /usr/sbin/sendmail in this case - try: - proc = subprocess.Popen(["/usr/sbin/sendmail", "-t"], - stdin=subprocess.PIPE) - proc.communicate(msg) - except OSError: - return False - return True diff --git a/certbot/ocsp.py b/certbot/ocsp.py deleted file mode 100644 index 049e1482745..00000000000 --- a/certbot/ocsp.py +++ /dev/null @@ -1,120 +0,0 @@ -"""Tools for checking certificate revocation.""" -import logging -import re - -from subprocess import Popen, PIPE - -from certbot import errors -from certbot import util - -logger = logging.getLogger(__name__) - -class RevocationChecker(object): - "This class figures out OCSP checking on this system, and performs it." - - def __init__(self): - self.broken = False - - if not util.exe_exists("openssl"): - logger.info("openssl not installed, can't check revocation") - self.broken = True - return - - # New versions of openssl want -header var=val, old ones want -header var val - test_host_format = Popen(["openssl", "ocsp", "-header", "var", "val"], - stdout=PIPE, stderr=PIPE, universal_newlines=True) - _out, err = test_host_format.communicate() - if "Missing =" in err: - self.host_args = lambda host: ["Host=" + host] - else: - self.host_args = lambda host: ["Host", host] - - - def ocsp_revoked(self, cert_path, chain_path): - """Get revoked status for a particular cert version. - - .. todo:: Make this a non-blocking call - - :param str cert_path: Path to certificate - :param str chain_path: Path to intermediate cert - :rtype bool or None: - :returns: True if revoked; False if valid or the check failed - - """ - if self.broken: - return False - - - url, host = self.determine_ocsp_server(cert_path) - if not host: - return False - # jdkasten thanks "Bulletproof SSL and TLS - Ivan Ristic" for documenting this! - cmd = ["openssl", "ocsp", - "-no_nonce", - "-issuer", chain_path, - "-cert", cert_path, - "-url", url, - "-CAfile", chain_path, - "-verify_other", chain_path, - "-trust_other", - "-header"] + self.host_args(host) - logger.debug("Querying OCSP for %s", cert_path) - logger.debug(" ".join(cmd)) - try: - output, err = util.run_script(cmd, log=logger.debug) - except errors.SubprocessError: - logger.info("OCSP check failed for %s (are we offline?)", cert_path) - return False - - return _translate_ocsp_query(cert_path, output, err) - - - def determine_ocsp_server(self, cert_path): - """Extract the OCSP server host from a certificate. - - :param str cert_path: Path to the cert we're checking OCSP for - :rtype tuple: - :returns: (OCSP server URL or None, OCSP server host or None) - - """ - try: - url, _err = util.run_script( - ["openssl", "x509", "-in", cert_path, "-noout", "-ocsp_uri"], - log=logger.debug) - except errors.SubprocessError: - logger.info("Cannot extract OCSP URI from %s", cert_path) - return None, None - - url = url.rstrip() - host = url.partition("://")[2].rstrip("/") - if host: - return url, host - else: - logger.info("Cannot process OCSP host from URL (%s) in cert at %s", url, cert_path) - return None, None - -def _translate_ocsp_query(cert_path, ocsp_output, ocsp_errors): - """Parse openssl's weird output to work out what it means.""" - - states = ("good", "revoked", "unknown") - patterns = [r"{0}: (WARNING.*)?{1}".format(cert_path, s) for s in states] - good, revoked, unknown = (re.search(p, ocsp_output, flags=re.DOTALL) for p in patterns) - - warning = good.group(1) if good else None - - if (not "Response verify OK" in ocsp_errors) or (good and warning) or unknown: - logger.info("Revocation status for %s is unknown", cert_path) - logger.debug("Uncertain output:\n%s\nstderr:\n%s", ocsp_output, ocsp_errors) - return False - elif good and not warning: - return False - elif revoked: - warning = revoked.group(1) - if warning: - logger.info("OCSP revocation warning: %s", warning) - return True - else: - logger.warning("Unable to properly parse OCSP output: %s\nstderr:%s", - ocsp_output, ocsp_errors) - return False - diff --git a/certbot/plugins/__init__.py b/certbot/plugins/__init__.py deleted file mode 100644 index 7b1aca2b49e..00000000000 --- a/certbot/plugins/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Certbot client.plugins.""" diff --git a/certbot/plugins/common.py b/certbot/plugins/common.py deleted file mode 100644 index ee1af497812..00000000000 --- a/certbot/plugins/common.py +++ /dev/null @@ -1,496 +0,0 @@ -"""Plugin common functions.""" -import logging -import os -import re -import shutil -import tempfile - -import OpenSSL -import pkg_resources -import zope.interface - -from josepy import util as jose_util - -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module -from certbot import achallenges # pylint: disable=unused-import -from certbot import constants -from certbot import crypto_util -from certbot import errors -from certbot import interfaces -from certbot import reverter -from certbot import util - -from certbot.plugins.storage import PluginStorage - -logger = logging.getLogger(__name__) - - -def option_namespace(name): - """ArgumentParser options namespace (prefix of all options).""" - return name + "-" - - -def dest_namespace(name): - """ArgumentParser dest namespace (prefix of all destinations).""" - return name.replace("-", "_") + "_" - -private_ips_regex = re.compile( - r"(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|" - r"(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)") -hostname_regex = re.compile( - r"^(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*[a-z]+$", re.IGNORECASE) - - -@zope.interface.implementer(interfaces.IPlugin) -class Plugin(object): - """Generic plugin.""" - # provider is not inherited, subclasses must define it on their own - # @zope.interface.provider(interfaces.IPluginFactory) - - def __init__(self, config, name): - self.config = config - self.name = name - - @jose_util.abstractclassmethod - def add_parser_arguments(cls, add): - """Add plugin arguments to the CLI argument parser. - - NOTE: If some of your flags interact with others, you can - use cli.report_config_interaction to register this to ensure - values are correctly saved/overridable during renewal. - - :param callable add: Function that proxies calls to - `argparse.ArgumentParser.add_argument` prepending options - with unique plugin name prefix. - - """ - - @classmethod - def inject_parser_options(cls, parser, name): - """Inject parser options. - - See `~.IPlugin.inject_parser_options` for docs. - - """ - # dummy function, doesn't check if dest.startswith(self.dest_namespace) - def add(arg_name_no_prefix, *args, **kwargs): - # pylint: disable=missing-docstring - return parser.add_argument( - "--{0}{1}".format(option_namespace(name), arg_name_no_prefix), - *args, **kwargs) - return cls.add_parser_arguments(add) - - @property - def option_namespace(self): - """ArgumentParser options namespace (prefix of all options).""" - return option_namespace(self.name) - - def option_name(self, name): - """Option name (include plugin namespace).""" - return self.option_namespace + name - - @property - def dest_namespace(self): - """ArgumentParser dest namespace (prefix of all destinations).""" - return dest_namespace(self.name) - - def dest(self, var): - """Find a destination for given variable ``var``.""" - # this should do exactly the same what ArgumentParser(arg), - # does to "arg" to compute "dest" - return self.dest_namespace + var.replace("-", "_") - - def conf(self, var): - """Find a configuration value for variable ``var``.""" - return getattr(self.config, self.dest(var)) - - -class Installer(Plugin): - """An installer base class with reverter and ssl_dhparam methods defined. - - Installer plugins do not have to inherit from this class. - - """ - def __init__(self, *args, **kwargs): - super(Installer, self).__init__(*args, **kwargs) - self.storage = PluginStorage(self.config, self.name) - self.reverter = reverter.Reverter(self.config) - - def add_to_checkpoint(self, save_files, save_notes, temporary=False): - """Add files to a checkpoint. - - :param set save_files: set of filepaths to save - :param str save_notes: notes about changes during the save - :param bool temporary: True if the files should be added to a - temporary checkpoint rather than a permanent one. This is - usually used for changes that will soon be reverted. - - :raises .errors.PluginError: when unable to add to checkpoint - - """ - if temporary: - checkpoint_func = self.reverter.add_to_temp_checkpoint - else: - checkpoint_func = self.reverter.add_to_checkpoint - - try: - checkpoint_func(save_files, save_notes) - except errors.ReverterError as err: - raise errors.PluginError(str(err)) - - def finalize_checkpoint(self, title): - """Timestamp and save changes made through the reverter. - - :param str title: Title describing checkpoint - - :raises .errors.PluginError: when an error occurs - - """ - try: - self.reverter.finalize_checkpoint(title) - except errors.ReverterError as err: - raise errors.PluginError(str(err)) - - def recovery_routine(self): - """Revert all previously modified files. - - Reverts all modified files that have not been saved as a checkpoint - - :raises .errors.PluginError: If unable to recover the configuration - - """ - try: - self.reverter.recovery_routine() - except errors.ReverterError as err: - raise errors.PluginError(str(err)) - - def revert_temporary_config(self): - """Rollback temporary checkpoint. - - :raises .errors.PluginError: when unable to revert config - - """ - try: - self.reverter.revert_temporary_config() - except errors.ReverterError as err: - raise errors.PluginError(str(err)) - - def rollback_checkpoints(self, rollback=1): - """Rollback saved checkpoints. - - :param int rollback: Number of checkpoints to revert - - :raises .errors.PluginError: If there is a problem with the input or - the function is unable to correctly revert the configuration - - """ - try: - self.reverter.rollback_checkpoints(rollback) - except errors.ReverterError as err: - raise errors.PluginError(str(err)) - - def view_config_changes(self): - """Show all of the configuration changes that have taken place. - - :raises .errors.PluginError: If there is a problem while processing - the checkpoints directories. - - """ - try: - self.reverter.view_config_changes() - except errors.ReverterError as err: - raise errors.PluginError(str(err)) - - @property - def ssl_dhparams(self): - """Full absolute path to ssl_dhparams file.""" - return os.path.join(self.config.config_dir, constants.SSL_DHPARAMS_DEST) - - @property - def updated_ssl_dhparams_digest(self): - """Full absolute path to digest of updated ssl_dhparams file.""" - return os.path.join(self.config.config_dir, constants.UPDATED_SSL_DHPARAMS_DIGEST) - - def install_ssl_dhparams(self): - """Copy Certbot's ssl_dhparams file into the system's config dir if required.""" - return install_version_controlled_file( - self.ssl_dhparams, - self.updated_ssl_dhparams_digest, - constants.SSL_DHPARAMS_SRC, - constants.ALL_SSL_DHPARAMS_HASHES) - - -class Addr(object): - r"""Represents an virtual host address. - - :param str addr: addr part of vhost address - :param str port: port number or \*, or "" - - """ - def __init__(self, tup, ipv6=False): - self.tup = tup - self.ipv6 = ipv6 - - @classmethod - def fromstring(cls, str_addr): - """Initialize Addr from string.""" - if str_addr.startswith('['): - # ipv6 addresses starts with [ - endIndex = str_addr.rfind(']') - host = str_addr[:endIndex + 1] - port = '' - if len(str_addr) > endIndex + 2 and str_addr[endIndex + 1] == ':': - port = str_addr[endIndex + 2:] - return cls((host, port), ipv6=True) - else: - tup = str_addr.partition(':') - return cls((tup[0], tup[2])) - - def __str__(self): - if self.tup[1]: - return "%s:%s" % self.tup - return self.tup[0] - - def normalized_tuple(self): - """Normalized representation of addr/port tuple - """ - if self.ipv6: - return (self.get_ipv6_exploded(), self.tup[1]) - return self.tup - - def __eq__(self, other): - if isinstance(other, self.__class__): - # compare normalized to take different - # styles of representation into account - return self.normalized_tuple() == other.normalized_tuple() - - return False - - def __hash__(self): - return hash(self.tup) - - def get_addr(self): - """Return addr part of Addr object.""" - return self.tup[0] - - def get_port(self): - """Return port.""" - return self.tup[1] - - def get_addr_obj(self, port): - """Return new address object with same addr and new port.""" - return self.__class__((self.tup[0], port), self.ipv6) - - def _normalize_ipv6(self, addr): - """Return IPv6 address in normalized form, helper function""" - addr = addr.lstrip("[") - addr = addr.rstrip("]") - return self._explode_ipv6(addr) - - def get_ipv6_exploded(self): - """Return IPv6 in normalized form""" - if self.ipv6: - return ":".join(self._normalize_ipv6(self.tup[0])) - return "" - - def _explode_ipv6(self, addr): - """Explode IPv6 address for comparison""" - result = ['0', '0', '0', '0', '0', '0', '0', '0'] - addr_list = addr.split(":") - if len(addr_list) > len(result): - # too long, truncate - addr_list = addr_list[0:len(result)] - append_to_end = False - for i in range(0, len(addr_list)): - block = addr_list[i] - if len(block) == 0: - # encountered ::, so rest of the blocks should be - # appended to the end - append_to_end = True - continue - elif len(block) > 1: - # remove leading zeros - block = block.lstrip("0") - if not append_to_end: - result[i] = str(block) - else: - # count the location from the end using negative indices - result[i-len(addr_list)] = str(block) - return result - - -class ChallengePerformer(object): - """Abstract base for challenge performers. - - :ivar configurator: Authenticator and installer plugin - :ivar achalls: Annotated challenges - :vartype achalls: `list` of `.KeyAuthorizationAnnotatedChallenge` - :ivar indices: Holds the indices of challenges from a larger array - so the user of the class doesn't have to. - :vartype indices: `list` of `int` - - """ - - def __init__(self, configurator): - self.configurator = configurator - self.achalls = [] # type: List[achallenges.KeyAuthorizationAnnotatedChallenge] - self.indices = [] # type: List[int] - - def add_chall(self, achall, idx=None): - """Store challenge to be performed when perform() is called. - - :param .KeyAuthorizationAnnotatedChallenge achall: Annotated - challenge. - :param int idx: index to challenge in a larger array - - """ - self.achalls.append(achall) - if idx is not None: - self.indices.append(idx) - - def perform(self): - """Perform all added challenges. - - :returns: challenge respones - :rtype: `list` of `acme.challenges.KeyAuthorizationChallengeResponse` - - - """ - raise NotImplementedError() - - -class TLSSNI01(ChallengePerformer): - # pylint: disable=abstract-method - """Abstract base for TLS-SNI-01 challenge performers""" - - def __init__(self, configurator): - super(TLSSNI01, self).__init__(configurator) - self.challenge_conf = os.path.join( - configurator.config.config_dir, "le_tls_sni_01_cert_challenge.conf") - # self.completed = 0 - - def get_cert_path(self, achall): - """Returns standardized name for challenge certificate. - - :param .KeyAuthorizationAnnotatedChallenge achall: Annotated - tls-sni-01 challenge. - - :returns: certificate file name - :rtype: str - - """ - return os.path.join(self.configurator.config.work_dir, - achall.chall.encode("token") + ".crt") - - def get_key_path(self, achall): - """Get standardized path to challenge key.""" - return os.path.join(self.configurator.config.work_dir, - achall.chall.encode("token") + '.pem') - - def get_z_domain(self, achall): - """Returns z_domain (SNI) name for the challenge.""" - return achall.response(achall.account_key).z_domain.decode("utf-8") - - def _setup_challenge_cert(self, achall, cert_key=None): - - """Generate and write out challenge certificate.""" - cert_path = self.get_cert_path(achall) - key_path = self.get_key_path(achall) - # Register the path before you write out the file - self.configurator.reverter.register_file_creation(True, key_path) - self.configurator.reverter.register_file_creation(True, cert_path) - - response, (cert, key) = achall.response_and_validation( - cert_key=cert_key) - cert_pem = OpenSSL.crypto.dump_certificate( - OpenSSL.crypto.FILETYPE_PEM, cert) - key_pem = OpenSSL.crypto.dump_privatekey( - OpenSSL.crypto.FILETYPE_PEM, key) - - # Write out challenge cert and key - with open(cert_path, "wb") as cert_chall_fd: - cert_chall_fd.write(cert_pem) - with util.safe_open(key_path, 'wb', chmod=0o400) as key_file: - key_file.write(key_pem) - - return response - - -def install_version_controlled_file(dest_path, digest_path, src_path, all_hashes): - """Copy a file into an active location (likely the system's config dir) if required. - - :param str dest_path: destination path for version controlled file - :param str digest_path: path to save a digest of the file in - :param str src_path: path to version controlled file found in distribution - :param list all_hashes: hashes of every released version of the file - """ - current_hash = crypto_util.sha256sum(src_path) - - def _write_current_hash(): - with open(digest_path, "w") as f: - f.write(current_hash) - - def _install_current_file(): - shutil.copyfile(src_path, dest_path) - _write_current_hash() - - # Check to make sure options-ssl.conf is installed - if not os.path.isfile(dest_path): - _install_current_file() - return - # there's already a file there. if it's up to date, do nothing. if it's not but - # it matches a known file hash, we can update it. - # otherwise, print a warning once per new version. - active_file_digest = crypto_util.sha256sum(dest_path) - if active_file_digest == current_hash: # already up to date - return - elif active_file_digest in all_hashes: # safe to update - _install_current_file() - else: # has been manually modified, not safe to update - # did they modify the current version or an old version? - if os.path.isfile(digest_path): - with open(digest_path, "r") as f: - saved_digest = f.read() - # they modified it after we either installed or told them about this version, so return - if saved_digest == current_hash: - return - # there's a new version but we couldn't update the file, or they deleted the digest. - # save the current digest so we only print this once, and print a warning - _write_current_hash() - logger.warning("%s has been manually modified; updated file " - "saved to %s. We recommend updating %s for security purposes.", - dest_path, src_path, dest_path) - - -# test utils used by certbot_apache/certbot_nginx (hence -# "pragma: no cover") TODO: this might quickly lead to dead code (also -# c.f. #383) - -def dir_setup(test_dir, pkg): # pragma: no cover - """Setup the directories necessary for the configurator.""" - def expanded_tempdir(prefix): - """Return the real path of a temp directory with the specified prefix - - Some plugins rely on real paths of symlinks for working correctly. For - example, certbot-apache uses real paths of configuration files to tell - a virtual host from another. On systems where TMP itself is a symbolic - link, (ex: OS X) such plugins will be confused. This function prevents - such a case. - """ - return os.path.realpath(tempfile.mkdtemp(prefix)) - - temp_dir = expanded_tempdir("temp") - config_dir = expanded_tempdir("config") - work_dir = expanded_tempdir("work") - - os.chmod(temp_dir, constants.CONFIG_DIRS_MODE) - os.chmod(config_dir, constants.CONFIG_DIRS_MODE) - os.chmod(work_dir, constants.CONFIG_DIRS_MODE) - - test_configs = pkg_resources.resource_filename( - pkg, os.path.join("testdata", test_dir)) - - shutil.copytree( - test_configs, os.path.join(temp_dir, test_dir), symlinks=True) - - return temp_dir, config_dir, work_dir diff --git a/certbot/plugins/common_test.py b/certbot/plugins/common_test.py deleted file mode 100644 index 103a124993d..00000000000 --- a/certbot/plugins/common_test.py +++ /dev/null @@ -1,422 +0,0 @@ -"""Tests for certbot.plugins.common.""" -import functools -import os -import shutil -import tempfile -import unittest - -import josepy as jose -import mock -import OpenSSL - -from acme import challenges - -from certbot import achallenges -from certbot import crypto_util -from certbot import errors - -from certbot.tests import acme_util -from certbot.tests import util as test_util - -AUTH_KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) -ACHALLS = [ - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.TLSSNI01(token=b'token1'), "pending"), - domain="encryption-example.demo", account_key=AUTH_KEY), - achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.TLSSNI01(token=b'token2'), "pending"), - domain="certbot.demo", account_key=AUTH_KEY), -] - -class NamespaceFunctionsTest(unittest.TestCase): - """Tests for certbot.plugins.common.*_namespace functions.""" - - def test_option_namespace(self): - from certbot.plugins.common import option_namespace - self.assertEqual("foo-", option_namespace("foo")) - - def test_dest_namespace(self): - from certbot.plugins.common import dest_namespace - self.assertEqual("foo_", dest_namespace("foo")) - - def test_dest_namespace_with_dashes(self): - from certbot.plugins.common import dest_namespace - self.assertEqual("foo_bar_", dest_namespace("foo-bar")) - - -class PluginTest(unittest.TestCase): - """Test for certbot.plugins.common.Plugin.""" - - def setUp(self): - from certbot.plugins.common import Plugin - - class MockPlugin(Plugin): # pylint: disable=missing-docstring - @classmethod - def add_parser_arguments(cls, add): - add("foo-bar", dest="different_to_foo_bar", x=1, y=None) - - self.plugin_cls = MockPlugin - self.config = mock.MagicMock() - self.plugin = MockPlugin(config=self.config, name="mock") - - def test_init(self): - self.assertEqual("mock", self.plugin.name) - self.assertEqual(self.config, self.plugin.config) - - def test_option_namespace(self): - self.assertEqual("mock-", self.plugin.option_namespace) - - def test_option_name(self): - self.assertEqual("mock-foo_bar", self.plugin.option_name("foo_bar")) - - def test_dest_namespace(self): - self.assertEqual("mock_", self.plugin.dest_namespace) - - def test_dest(self): - self.assertEqual("mock_foo_bar", self.plugin.dest("foo-bar")) - self.assertEqual("mock_foo_bar", self.plugin.dest("foo_bar")) - - def test_conf(self): - self.assertEqual(self.config.mock_foo_bar, self.plugin.conf("foo-bar")) - - def test_inject_parser_options(self): - parser = mock.MagicMock() - self.plugin_cls.inject_parser_options(parser, "mock") - # note that inject_parser_options doesn't check if dest has - # correct prefix - parser.add_argument.assert_called_once_with( - "--mock-foo-bar", dest="different_to_foo_bar", x=1, y=None) - - -class InstallerTest(test_util.ConfigTestCase): - """Tests for certbot.plugins.common.Installer.""" - - def setUp(self): - super(InstallerTest, self).setUp() - os.mkdir(self.config.config_dir) - from certbot.plugins.common import Installer - - with mock.patch("certbot.plugins.common.reverter.Reverter"): - self.installer = Installer(config=self.config, - name="Installer") - self.reverter = self.installer.reverter - - def test_add_to_real_checkpoint(self): - files = set(("foo.bar", "baz.qux",)) - save_notes = "foo bar baz qux" - self._test_wrapped_method("add_to_checkpoint", files, save_notes) - - def test_add_to_real_checkpoint2(self): - self._test_add_to_checkpoint_common(False) - - def test_add_to_temporary_checkpoint(self): - self._test_add_to_checkpoint_common(True) - - def _test_add_to_checkpoint_common(self, temporary): - files = set(("foo.bar", "baz.qux",)) - save_notes = "foo bar baz qux" - - installer_func = functools.partial(self.installer.add_to_checkpoint, - temporary=temporary) - - if temporary: - reverter_func = self.reverter.add_to_temp_checkpoint - else: - reverter_func = self.reverter.add_to_checkpoint - - self._test_adapted_method( - installer_func, reverter_func, files, save_notes) - - def test_finalize_checkpoint(self): - self._test_wrapped_method("finalize_checkpoint", "foo") - - def test_recovery_routine(self): - self._test_wrapped_method("recovery_routine") - - def test_revert_temporary_config(self): - self._test_wrapped_method("revert_temporary_config") - - def test_rollback_checkpoints(self): - self._test_wrapped_method("rollback_checkpoints", 42) - - def test_view_config_changes(self): - self._test_wrapped_method("view_config_changes") - - def _test_wrapped_method(self, name, *args, **kwargs): - """Test a wrapped reverter method. - - :param str name: name of the method to test - :param tuple args: position arguments to method - :param dict kwargs: keyword arguments to method - - """ - installer_func = getattr(self.installer, name) - reverter_func = getattr(self.reverter, name) - self._test_adapted_method( - installer_func, reverter_func, *args, **kwargs) - - def _test_adapted_method(self, installer_func, - reverter_func, *passed_args, **passed_kwargs): - """Test an adapted reverter method - - :param callable installer_func: installer method to test - :param mock.MagicMock reverter_func: mocked adapated - reverter method - :param tuple passed_args: positional arguments passed from - installer method to the reverter method - :param dict passed_kargs: keyword arguments passed from - installer method to the reverter method - - """ - installer_func(*passed_args, **passed_kwargs) - reverter_func.assert_called_once_with(*passed_args, **passed_kwargs) - reverter_func.side_effect = errors.ReverterError - self.assertRaises( - errors.PluginError, installer_func, *passed_args, **passed_kwargs) - - def test_install_ssl_dhparams(self): - self.installer.install_ssl_dhparams() - self.assertTrue(os.path.isfile(self.installer.ssl_dhparams)) - - def _current_ssl_dhparams_hash(self): - from certbot.constants import SSL_DHPARAMS_SRC - return crypto_util.sha256sum(SSL_DHPARAMS_SRC) - - def test_current_file_hash_in_all_hashes(self): - from certbot.constants import ALL_SSL_DHPARAMS_HASHES - self.assertTrue(self._current_ssl_dhparams_hash() in ALL_SSL_DHPARAMS_HASHES, - "Constants.ALL_SSL_DHPARAMS_HASHES must be appended" - " with the sha256 hash of self.config.ssl_dhparams when it is updated.") - - -class AddrTest(unittest.TestCase): - """Tests for certbot.client.plugins.common.Addr.""" - - def setUp(self): - from certbot.plugins.common import Addr - self.addr1 = Addr.fromstring("192.168.1.1") - self.addr2 = Addr.fromstring("192.168.1.1:*") - self.addr3 = Addr.fromstring("192.168.1.1:80") - self.addr4 = Addr.fromstring("[fe00::1]") - self.addr5 = Addr.fromstring("[fe00::1]:*") - self.addr6 = Addr.fromstring("[fe00::1]:80") - self.addr7 = Addr.fromstring("[fe00::1]:5") - self.addr8 = Addr.fromstring("[fe00:1:2:3:4:5:6:7:8:9]:8080") - - def test_fromstring(self): - self.assertEqual(self.addr1.get_addr(), "192.168.1.1") - self.assertEqual(self.addr1.get_port(), "") - self.assertEqual(self.addr2.get_addr(), "192.168.1.1") - self.assertEqual(self.addr2.get_port(), "*") - self.assertEqual(self.addr3.get_addr(), "192.168.1.1") - self.assertEqual(self.addr3.get_port(), "80") - self.assertEqual(self.addr4.get_addr(), "[fe00::1]") - self.assertEqual(self.addr4.get_port(), "") - self.assertEqual(self.addr5.get_addr(), "[fe00::1]") - self.assertEqual(self.addr5.get_port(), "*") - self.assertEqual(self.addr6.get_addr(), "[fe00::1]") - self.assertEqual(self.addr6.get_port(), "80") - self.assertEqual(self.addr6.get_ipv6_exploded(), - "fe00:0:0:0:0:0:0:1") - self.assertEqual(self.addr1.get_ipv6_exploded(), - "") - self.assertEqual(self.addr7.get_port(), "5") - self.assertEqual(self.addr8.get_ipv6_exploded(), - "fe00:1:2:3:4:5:6:7") - - def test_str(self): - self.assertEqual(str(self.addr1), "192.168.1.1") - self.assertEqual(str(self.addr2), "192.168.1.1:*") - self.assertEqual(str(self.addr3), "192.168.1.1:80") - self.assertEqual(str(self.addr4), "[fe00::1]") - self.assertEqual(str(self.addr5), "[fe00::1]:*") - self.assertEqual(str(self.addr6), "[fe00::1]:80") - - def test_get_addr_obj(self): - self.assertEqual(str(self.addr1.get_addr_obj("443")), "192.168.1.1:443") - self.assertEqual(str(self.addr2.get_addr_obj("")), "192.168.1.1") - self.assertEqual(str(self.addr1.get_addr_obj("*")), "192.168.1.1:*") - self.assertEqual(str(self.addr4.get_addr_obj("443")), "[fe00::1]:443") - self.assertEqual(str(self.addr5.get_addr_obj("")), "[fe00::1]") - self.assertEqual(str(self.addr4.get_addr_obj("*")), "[fe00::1]:*") - - def test_eq(self): - self.assertEqual(self.addr1, self.addr2.get_addr_obj("")) - self.assertNotEqual(self.addr1, self.addr2) - self.assertFalse(self.addr1 == 3333) - - self.assertEqual(self.addr4, self.addr4.get_addr_obj("")) - self.assertNotEqual(self.addr4, self.addr5) - self.assertFalse(self.addr4 == 3333) - from certbot.plugins.common import Addr - self.assertEqual(self.addr4, Addr.fromstring("[fe00:0:0::1]")) - self.assertEqual(self.addr4, Addr.fromstring("[fe00:0::0:0:1]")) - - - def test_set_inclusion(self): - from certbot.plugins.common import Addr - set_a = set([self.addr1, self.addr2]) - addr1b = Addr.fromstring("192.168.1.1") - addr2b = Addr.fromstring("192.168.1.1:*") - set_b = set([addr1b, addr2b]) - - self.assertEqual(set_a, set_b) - - set_c = set([self.addr4, self.addr5]) - addr4b = Addr.fromstring("[fe00::1]") - addr5b = Addr.fromstring("[fe00::1]:*") - set_d = set([addr4b, addr5b]) - - self.assertEqual(set_c, set_d) - - -class ChallengePerformerTest(unittest.TestCase): - """Tests for certbot.plugins.common.ChallengePerformer.""" - - def setUp(self): - configurator = mock.MagicMock() - - from certbot.plugins.common import ChallengePerformer - self.performer = ChallengePerformer(configurator) - - def test_add_chall(self): - self.performer.add_chall(ACHALLS[0], 0) - self.assertEqual(1, len(self.performer.achalls)) - self.assertEqual([0], self.performer.indices) - - def test_perform(self): - self.assertRaises(NotImplementedError, self.performer.perform) - - -class TLSSNI01Test(unittest.TestCase): - """Tests for certbot.plugins.common.TLSSNI01.""" - - def setUp(self): - self.tempdir = tempfile.mkdtemp() - configurator = mock.MagicMock() - configurator.config.config_dir = os.path.join(self.tempdir, "config") - configurator.config.work_dir = os.path.join(self.tempdir, "work") - - from certbot.plugins.common import TLSSNI01 - self.sni = TLSSNI01(configurator=configurator) - - def tearDown(self): - shutil.rmtree(self.tempdir) - - def test_setup_challenge_cert(self): - # This is a helper function that can be used for handling - # open context managers more elegantly. It avoids dealing with - # __enter__ and __exit__ calls. - # http://www.voidspace.org.uk/python/mock/helpers.html#mock.mock_open - mock_open, mock_safe_open = mock.mock_open(), mock.mock_open() - - response = challenges.TLSSNI01Response() - achall = mock.MagicMock() - achall.chall.encode.return_value = "token" - key = test_util.load_pyopenssl_private_key("rsa512_key.pem") - achall.response_and_validation.return_value = ( - response, (test_util.load_cert("cert_512.pem"), key)) - - with mock.patch("certbot.plugins.common.open", - mock_open, create=True): - with mock.patch("certbot.plugins.common.util.safe_open", - mock_safe_open): - # pylint: disable=protected-access - self.assertEqual(response, self.sni._setup_challenge_cert( - achall, "randomS1")) - - # pylint: disable=no-member - mock_open.assert_called_once_with(self.sni.get_cert_path(achall), "wb") - mock_open.return_value.write.assert_called_once_with( - test_util.load_vector("cert_512.pem")) - mock_safe_open.assert_called_once_with( - self.sni.get_key_path(achall), "wb", chmod=0o400) - mock_safe_open.return_value.write.assert_called_once_with( - OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key)) - - def test_get_z_domain(self): - achall = ACHALLS[0] - self.assertEqual(self.sni.get_z_domain(achall), - achall.response(achall.account_key).z_domain.decode("utf-8")) - - -class InstallVersionControlledFileTest(test_util.TempDirTestCase): - """Tests for certbot.plugins.common.install_version_controlled_file.""" - - def setUp(self): - super(InstallVersionControlledFileTest, self).setUp() - self.hashes = ["someotherhash"] - self.dest_path = os.path.join(self.tempdir, "options-ssl-dest.conf") - self.hash_path = os.path.join(self.tempdir, ".options-ssl-conf.txt") - self.old_path = os.path.join(self.tempdir, "options-ssl-old.conf") - self.source_path = os.path.join(self.tempdir, "options-ssl-src.conf") - for path in (self.source_path, self.old_path,): - with open(path, "w") as f: - f.write(path) - self.hashes.append(crypto_util.sha256sum(path)) - - def _call(self): - from certbot.plugins.common import install_version_controlled_file - install_version_controlled_file(self.dest_path, - self.hash_path, - self.source_path, - self.hashes) - - def _current_file_hash(self): - return crypto_util.sha256sum(self.source_path) - - def _assert_current_file(self): - self.assertTrue(os.path.isfile(self.dest_path)) - self.assertEqual(crypto_util.sha256sum(self.dest_path), - self._current_file_hash()) - - def test_no_file(self): - self.assertFalse(os.path.isfile(self.dest_path)) - self._call() - self._assert_current_file() - - def test_current_file(self): - # 1st iteration installs the file, the 2nd checks if it needs updating - for _ in range(2): - self._call() - self._assert_current_file() - - def test_prev_file_updates_to_current(self): - shutil.copyfile(self.old_path, self.dest_path) - self._call() - self._assert_current_file() - - def test_manually_modified_current_file_does_not_update(self): - self._call() - with open(self.dest_path, "a") as mod_ssl_conf: - mod_ssl_conf.write("a new line for the wrong hash\n") - with mock.patch("certbot.plugins.common.logger") as mock_logger: - self._call() - self.assertFalse(mock_logger.warning.called) - self.assertTrue(os.path.isfile(self.dest_path)) - self.assertEqual(crypto_util.sha256sum(self.source_path), - self._current_file_hash()) - self.assertNotEqual(crypto_util.sha256sum(self.dest_path), - self._current_file_hash()) - - def test_manually_modified_past_file_warns(self): - with open(self.dest_path, "a") as mod_ssl_conf: - mod_ssl_conf.write("a new line for the wrong hash\n") - with open(self.hash_path, "w") as f: - f.write("hashofanoldversion") - with mock.patch("certbot.plugins.common.logger") as mock_logger: - self._call() - self.assertEqual(mock_logger.warning.call_args[0][0], - "%s has been manually modified; updated file " - "saved to %s. We recommend updating %s for security purposes.") - self.assertEqual(crypto_util.sha256sum(self.source_path), - self._current_file_hash()) - # only print warning once - with mock.patch("certbot.plugins.common.logger") as mock_logger: - self._call() - self.assertFalse(mock_logger.warning.called) - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/plugins/disco.py b/certbot/plugins/disco.py deleted file mode 100644 index 7be320efcba..00000000000 --- a/certbot/plugins/disco.py +++ /dev/null @@ -1,289 +0,0 @@ -"""Utilities for plugins discovery and selection.""" -import collections -import itertools -import logging -import pkg_resources -import six - -from collections import OrderedDict - -import zope.interface -import zope.interface.verify - -from acme.magic_typing import Dict # pylint: disable=unused-import, no-name-in-module -from certbot import constants -from certbot import errors -from certbot import interfaces - - -logger = logging.getLogger(__name__) - - -class PluginEntryPoint(object): - """Plugin entry point.""" - - PREFIX_FREE_DISTRIBUTIONS = [ - "certbot", - "certbot-apache", - "certbot-dns-cloudflare", - "certbot-dns-cloudxns", - "certbot-dns-digitalocean", - "certbot-dns-dnsimple", - "certbot-dns-dnsmadeeasy", - "certbot-dns-gehirn", - "certbot-dns-google", - "certbot-dns-linode", - "certbot-dns-luadns", - "certbot-dns-nsone", - "certbot-dns-ovh", - "certbot-dns-rfc2136", - "certbot-dns-route53", - "certbot-dns-sakuracloud", - "certbot-nginx", - "certbot-postfix", - ] - """Distributions for which prefix will be omitted.""" - - # this object is mutable, don't allow it to be hashed! - __hash__ = None # type: ignore - - def __init__(self, entry_point): - self.name = self.entry_point_to_plugin_name(entry_point) - self.plugin_cls = entry_point.load() - self.entry_point = entry_point - self._initialized = None - self._prepared = None - - @classmethod - def entry_point_to_plugin_name(cls, entry_point): - """Unique plugin name for an ``entry_point``""" - if entry_point.dist.key in cls.PREFIX_FREE_DISTRIBUTIONS: - return entry_point.name - return entry_point.dist.key + ":" + entry_point.name - - @property - def description(self): - """Description of the plugin.""" - return self.plugin_cls.description - - @property - def description_with_name(self): - """Description with name. Handy for UI.""" - return "{0} ({1})".format(self.description, self.name) - - @property - def long_description(self): - """Long description of the plugin.""" - try: - return self.plugin_cls.long_description - except AttributeError: - return self.description - - @property - def hidden(self): - """Should this plugin be hidden from UI?""" - return getattr(self.plugin_cls, "hidden", False) - - def ifaces(self, *ifaces_groups): - """Does plugin implements specified interface groups?""" - return not ifaces_groups or any( - all(iface.implementedBy(self.plugin_cls) - for iface in ifaces) - for ifaces in ifaces_groups) - - @property - def initialized(self): - """Has the plugin been initialized already?""" - return self._initialized is not None - - def init(self, config=None): - """Memoized plugin initialization.""" - if not self.initialized: - self.entry_point.require() # fetch extras! - self._initialized = self.plugin_cls(config, self.name) - return self._initialized - - def verify(self, ifaces): - """Verify that the plugin conforms to the specified interfaces.""" - assert self.initialized - for iface in ifaces: # zope.interface.providedBy(plugin) - try: - zope.interface.verify.verifyObject(iface, self.init()) - except zope.interface.exceptions.BrokenImplementation as error: - if iface.implementedBy(self.plugin_cls): - logger.debug( - "%s implements %s but object does not verify: %s", - self.plugin_cls, iface.__name__, error, exc_info=True) - return False - return True - - @property - def prepared(self): - """Has the plugin been prepared already?""" - if not self.initialized: - logger.debug(".prepared called on uninitialized %r", self) - return self._prepared is not None - - def prepare(self): - """Memoized plugin preparation.""" - assert self.initialized - if self._prepared is None: - try: - self._initialized.prepare() - except errors.MisconfigurationError as error: - logger.debug("Misconfigured %r: %s", self, error, exc_info=True) - self._prepared = error - except errors.NoInstallationError as error: - logger.debug( - "No installation (%r): %s", self, error, exc_info=True) - self._prepared = error - except errors.PluginError as error: - logger.debug("Other error:(%r): %s", self, error, exc_info=True) - self._prepared = error - else: - self._prepared = True - return self._prepared - - @property - def misconfigured(self): - """Is plugin misconfigured?""" - return isinstance(self._prepared, errors.MisconfigurationError) - - @property - def problem(self): - """Return the Exception raised during plugin setup, or None if all is well""" - if isinstance(self._prepared, Exception): - return self._prepared - return None - - @property - def available(self): - """Is plugin available, i.e. prepared or misconfigured?""" - return self._prepared is True or self.misconfigured - - def __repr__(self): - return "PluginEntryPoint#{0}".format(self.name) - - def __str__(self): - lines = [ - "* {0}".format(self.name), - "Description: {0}".format(self.plugin_cls.description), - "Interfaces: {0}".format(", ".join( - iface.__name__ for iface in zope.interface.implementedBy( - self.plugin_cls))), - "Entry point: {0}".format(self.entry_point), - ] - - if self.initialized: - lines.append("Initialized: {0}".format(self.init())) - if self.prepared: - lines.append("Prep: {0}".format(self.prepare())) - - return "\n".join(lines) - - -class PluginsRegistry(collections.Mapping): - """Plugins registry.""" - - def __init__(self, plugins): - # plugins are sorted so the same order is used between runs. - # This prevents deadlock caused by plugins acquiring a lock - # and ensures at least one concurrent Certbot instance will run - # successfully. - self._plugins = OrderedDict(sorted(six.iteritems(plugins))) - - @classmethod - def find_all(cls): - """Find plugins using setuptools entry points.""" - plugins = {} # type: Dict[str, PluginEntryPoint] - # pylint: disable=not-callable - entry_points = itertools.chain( - pkg_resources.iter_entry_points( - constants.SETUPTOOLS_PLUGINS_ENTRY_POINT), - pkg_resources.iter_entry_points( - constants.OLD_SETUPTOOLS_PLUGINS_ENTRY_POINT),) - for entry_point in entry_points: - plugin_ep = PluginEntryPoint(entry_point) - assert plugin_ep.name not in plugins, ( - "PREFIX_FREE_DISTRIBUTIONS messed up") - # providedBy | pylint: disable=no-member - if interfaces.IPluginFactory.providedBy(plugin_ep.plugin_cls): - plugins[plugin_ep.name] = plugin_ep - else: # pragma: no cover - logger.warning( - "%r does not provide IPluginFactory, skipping", plugin_ep) - return cls(plugins) - - def __getitem__(self, name): - return self._plugins[name] - - def __iter__(self): - return iter(self._plugins) - - def __len__(self): - return len(self._plugins) - - def init(self, config): - """Initialize all plugins in the registry.""" - return [plugin_ep.init(config) for plugin_ep - in six.itervalues(self._plugins)] - - def filter(self, pred): - """Filter plugins based on predicate.""" - return type(self)(dict((name, plugin_ep) for name, plugin_ep - in six.iteritems(self._plugins) if pred(plugin_ep))) - - def visible(self): - """Filter plugins based on visibility.""" - return self.filter(lambda plugin_ep: not plugin_ep.hidden) - - def ifaces(self, *ifaces_groups): - """Filter plugins based on interfaces.""" - # pylint: disable=star-args - return self.filter(lambda p_ep: p_ep.ifaces(*ifaces_groups)) - - def verify(self, ifaces): - """Filter plugins based on verification.""" - return self.filter(lambda p_ep: p_ep.verify(ifaces)) - - def prepare(self): - """Prepare all plugins in the registry.""" - return [plugin_ep.prepare() for plugin_ep in six.itervalues(self._plugins)] - - def available(self): - """Filter plugins based on availability.""" - return self.filter(lambda p_ep: p_ep.available) - # successfully prepared + misconfigured - - def find_init(self, plugin): - """Find an initialized plugin. - - This is particularly useful for finding a name for the plugin - (although `.IPluginFactory.__call__` takes ``name`` as one of - the arguments, ``IPlugin.name`` is not part of the interface):: - - # plugin is an instance providing IPlugin, initialized - # somewhere else in the code - plugin_registry.find_init(plugin).name - - Returns ``None`` if ``plugin`` is not found in the registry. - - """ - # use list instead of set because PluginEntryPoint is not hashable - candidates = [plugin_ep for plugin_ep in six.itervalues(self._plugins) - if plugin_ep.initialized and plugin_ep.init() is plugin] - assert len(candidates) <= 1 - if candidates: - return candidates[0] - else: - return None - - def __repr__(self): - return "{0}({1})".format( - self.__class__.__name__, ','.join( - repr(p_ep) for p_ep in six.itervalues(self._plugins))) - - def __str__(self): - if not self._plugins: - return "No plugins" - return "\n\n".join(str(p_ep) for p_ep in six.itervalues(self._plugins)) diff --git a/certbot/plugins/disco_test.py b/certbot/plugins/disco_test.py deleted file mode 100644 index 720b90b16dd..00000000000 --- a/certbot/plugins/disco_test.py +++ /dev/null @@ -1,292 +0,0 @@ -"""Tests for certbot.plugins.disco.""" -import functools -import string -import unittest - -import mock -import pkg_resources -import six -import zope.interface - -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module -from certbot import errors -from certbot import interfaces - -from certbot.plugins import standalone -from certbot.plugins import webroot - -EP_SA = pkg_resources.EntryPoint( - "sa", "certbot.plugins.standalone", - attrs=("Authenticator",), - dist=mock.MagicMock(key="certbot")) -EP_WR = pkg_resources.EntryPoint( - "wr", "certbot.plugins.webroot", - attrs=("Authenticator",), - dist=mock.MagicMock(key="certbot")) - - -class PluginEntryPointTest(unittest.TestCase): - """Tests for certbot.plugins.disco.PluginEntryPoint.""" - - def setUp(self): - self.ep1 = pkg_resources.EntryPoint( - "ep1", "p1.ep1", dist=mock.MagicMock(key="p1")) - self.ep1prim = pkg_resources.EntryPoint( - "ep1", "p2.ep2", dist=mock.MagicMock(key="p2")) - # nested - self.ep2 = pkg_resources.EntryPoint( - "ep2", "p2.foo.ep2", dist=mock.MagicMock(key="p2")) - # project name != top-level package name - self.ep3 = pkg_resources.EntryPoint( - "ep3", "a.ep3", dist=mock.MagicMock(key="p3")) - - from certbot.plugins.disco import PluginEntryPoint - self.plugin_ep = PluginEntryPoint(EP_SA) - - def test_entry_point_to_plugin_name(self): - from certbot.plugins.disco import PluginEntryPoint - - names = { - self.ep1: "p1:ep1", - self.ep1prim: "p2:ep1", - self.ep2: "p2:ep2", - self.ep3: "p3:ep3", - EP_SA: "sa", - } - - for entry_point, name in six.iteritems(names): - self.assertEqual( - name, PluginEntryPoint.entry_point_to_plugin_name(entry_point)) - - def test_description(self): - self.assertTrue("temporary webserver" in self.plugin_ep.description) - - def test_description_with_name(self): - self.plugin_ep.plugin_cls = mock.MagicMock(description="Desc") - self.assertEqual( - "Desc (sa)", self.plugin_ep.description_with_name) - - def test_long_description(self): - self.plugin_ep.plugin_cls = mock.MagicMock( - long_description="Long desc") - self.assertEqual( - "Long desc", self.plugin_ep.long_description) - - def test_long_description_nonexistent(self): - self.plugin_ep.plugin_cls = mock.MagicMock( - description="Long desc not found", spec=["description"]) - self.assertEqual( - "Long desc not found", self.plugin_ep.long_description) - - def test_ifaces(self): - self.assertTrue(self.plugin_ep.ifaces((interfaces.IAuthenticator,))) - self.assertFalse(self.plugin_ep.ifaces((interfaces.IInstaller,))) - self.assertFalse(self.plugin_ep.ifaces(( - interfaces.IInstaller, interfaces.IAuthenticator))) - - def test__init__(self): - self.assertFalse(self.plugin_ep.initialized) - self.assertFalse(self.plugin_ep.prepared) - self.assertFalse(self.plugin_ep.misconfigured) - self.assertFalse(self.plugin_ep.available) - self.assertTrue(self.plugin_ep.problem is None) - self.assertTrue(self.plugin_ep.entry_point is EP_SA) - self.assertEqual("sa", self.plugin_ep.name) - - self.assertTrue(self.plugin_ep.plugin_cls is standalone.Authenticator) - - def test_init(self): - config = mock.MagicMock() - plugin = self.plugin_ep.init(config=config) - self.assertTrue(self.plugin_ep.initialized) - self.assertTrue(plugin.config is config) - # memoize! - self.assertTrue(self.plugin_ep.init() is plugin) - self.assertTrue(plugin.config is config) - # try to give different config - self.assertTrue(self.plugin_ep.init(123) is plugin) - self.assertTrue(plugin.config is config) - - self.assertFalse(self.plugin_ep.prepared) - self.assertFalse(self.plugin_ep.misconfigured) - self.assertFalse(self.plugin_ep.available) - - def test_verify(self): - iface1 = mock.MagicMock(__name__="iface1") - iface2 = mock.MagicMock(__name__="iface2") - iface3 = mock.MagicMock(__name__="iface3") - # pylint: disable=protected-access - self.plugin_ep._initialized = plugin = mock.MagicMock() - - exceptions = zope.interface.exceptions - with mock.patch("certbot.plugins." - "disco.zope.interface") as mock_zope: - mock_zope.exceptions = exceptions - - def verify_object(iface, obj): # pylint: disable=missing-docstring - assert obj is plugin - assert iface is iface1 or iface is iface2 or iface is iface3 - if iface is iface3: - raise mock_zope.exceptions.BrokenImplementation(None, None) - mock_zope.verify.verifyObject.side_effect = verify_object - self.assertTrue(self.plugin_ep.verify((iface1,))) - self.assertTrue(self.plugin_ep.verify((iface1, iface2))) - self.assertFalse(self.plugin_ep.verify((iface3,))) - self.assertFalse(self.plugin_ep.verify((iface1, iface3))) - - def test_prepare(self): - config = mock.MagicMock() - self.plugin_ep.init(config=config) - self.plugin_ep.prepare() - self.assertTrue(self.plugin_ep.prepared) - self.assertFalse(self.plugin_ep.misconfigured) - - # output doesn't matter that much, just test if it runs - str(self.plugin_ep) - - def test_prepare_misconfigured(self): - plugin = mock.MagicMock() - plugin.prepare.side_effect = errors.MisconfigurationError - # pylint: disable=protected-access - self.plugin_ep._initialized = plugin - self.assertTrue(isinstance(self.plugin_ep.prepare(), - errors.MisconfigurationError)) - self.assertTrue(self.plugin_ep.prepared) - self.assertTrue(self.plugin_ep.misconfigured) - self.assertTrue(isinstance(self.plugin_ep.problem, - errors.MisconfigurationError)) - self.assertTrue(self.plugin_ep.available) - - def test_prepare_no_installation(self): - plugin = mock.MagicMock() - plugin.prepare.side_effect = errors.NoInstallationError - # pylint: disable=protected-access - self.plugin_ep._initialized = plugin - self.assertTrue(isinstance(self.plugin_ep.prepare(), - errors.NoInstallationError)) - self.assertTrue(self.plugin_ep.prepared) - self.assertFalse(self.plugin_ep.misconfigured) - self.assertFalse(self.plugin_ep.available) - - def test_prepare_generic_plugin_error(self): - plugin = mock.MagicMock() - plugin.prepare.side_effect = errors.PluginError - # pylint: disable=protected-access - self.plugin_ep._initialized = plugin - self.assertTrue(isinstance(self.plugin_ep.prepare(), errors.PluginError)) - self.assertTrue(self.plugin_ep.prepared) - self.assertFalse(self.plugin_ep.misconfigured) - self.assertFalse(self.plugin_ep.available) - - def test_repr(self): - self.assertEqual("PluginEntryPoint#sa", repr(self.plugin_ep)) - - -class PluginsRegistryTest(unittest.TestCase): - """Tests for certbot.plugins.disco.PluginsRegistry.""" - - @classmethod - def _create_new_registry(cls, plugins): - from certbot.plugins.disco import PluginsRegistry - return PluginsRegistry(plugins) - - def setUp(self): - self.plugin_ep = mock.MagicMock() - self.plugin_ep.name = "mock" - self.plugin_ep.__hash__.side_effect = TypeError - self.plugins = {self.plugin_ep.name: self.plugin_ep} - self.reg = self._create_new_registry(self.plugins) - - def test_find_all(self): - from certbot.plugins.disco import PluginsRegistry - with mock.patch("certbot.plugins.disco.pkg_resources") as mock_pkg: - mock_pkg.iter_entry_points.side_effect = [iter([EP_SA]), - iter([EP_WR])] - plugins = PluginsRegistry.find_all() - self.assertTrue(plugins["sa"].plugin_cls is standalone.Authenticator) - self.assertTrue(plugins["sa"].entry_point is EP_SA) - self.assertTrue(plugins["wr"].plugin_cls is webroot.Authenticator) - self.assertTrue(plugins["wr"].entry_point is EP_WR) - - def test_getitem(self): - self.assertEqual(self.plugin_ep, self.reg["mock"]) - - def test_iter(self): - self.assertEqual(["mock"], list(self.reg)) - - def test_len(self): - self.assertEqual(0, len(self._create_new_registry({}))) - self.assertEqual(1, len(self.reg)) - - def test_init(self): - self.plugin_ep.init.return_value = "baz" - self.assertEqual(["baz"], self.reg.init("bar")) - self.plugin_ep.init.assert_called_once_with("bar") - - def test_filter(self): - self.assertEqual( - self.plugins, - self.reg.filter(lambda p_ep: p_ep.name.startswith("m"))) - self.assertEqual( - {}, self.reg.filter(lambda p_ep: p_ep.name.startswith("b"))) - - def test_ifaces(self): - self.plugin_ep.ifaces.return_value = True - # pylint: disable=protected-access - self.assertEqual(self.plugins, self.reg.ifaces()._plugins) - self.plugin_ep.ifaces.return_value = False - self.assertEqual({}, self.reg.ifaces()._plugins) - - def test_verify(self): - self.plugin_ep.verify.return_value = True - # pylint: disable=protected-access - self.assertEqual( - self.plugins, self.reg.verify(mock.MagicMock())._plugins) - self.plugin_ep.verify.return_value = False - self.assertEqual({}, self.reg.verify(mock.MagicMock())._plugins) - - def test_prepare(self): - self.plugin_ep.prepare.return_value = "baz" - self.assertEqual(["baz"], self.reg.prepare()) - self.plugin_ep.prepare.assert_called_once_with() - - def test_prepare_order(self): - order = [] # type: List[str] - plugins = dict( - (c, mock.MagicMock(prepare=functools.partial(order.append, c))) - for c in string.ascii_letters) - reg = self._create_new_registry(plugins) - reg.prepare() - # order of prepare calls must be sorted to prevent deadlock - # caused by plugins acquiring locks during prepare - self.assertEqual(order, sorted(string.ascii_letters)) - - def test_available(self): - self.plugin_ep.available = True - # pylint: disable=protected-access - self.assertEqual(self.plugins, self.reg.available()._plugins) - self.plugin_ep.available = False - self.assertEqual({}, self.reg.available()._plugins) - - def test_find_init(self): - self.assertTrue(self.reg.find_init(mock.Mock()) is None) - self.plugin_ep.initialized = True - self.assertTrue( - self.reg.find_init(self.plugin_ep.init()) is self.plugin_ep) - - def test_repr(self): - self.plugin_ep.__repr__ = lambda _: "PluginEntryPoint#mock" - self.assertEqual("PluginsRegistry(PluginEntryPoint#mock)", - repr(self.reg)) - - def test_str(self): - self.assertEqual("No plugins", str(self._create_new_registry({}))) - self.plugin_ep.__str__ = lambda _: "Mock" - self.assertEqual("Mock", str(self.reg)) - plugins = {self.plugin_ep.name: self.plugin_ep, "foo": "Bar"} - reg = self._create_new_registry(plugins) - self.assertEqual("Bar\n\nMock", str(reg)) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/plugins/dns_common.py b/certbot/plugins/dns_common.py deleted file mode 100644 index ba88b7aef45..00000000000 --- a/certbot/plugins/dns_common.py +++ /dev/null @@ -1,335 +0,0 @@ -"""Common code for DNS Authenticator Plugins.""" - -import abc -import logging -import os -import stat -from time import sleep - -import configobj -import zope.interface -from acme import challenges - -from certbot import errors -from certbot import interfaces -from certbot.display import ops -from certbot.display import util as display_util -from certbot.plugins import common - -logger = logging.getLogger(__name__) - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class DNSAuthenticator(common.Plugin): - """Base class for DNS Authenticators""" - - def __init__(self, config, name): - super(DNSAuthenticator, self).__init__(config, name) - - self._attempt_cleanup = False - - @classmethod - def add_parser_arguments(cls, add, default_propagation_seconds=10): # pylint: disable=arguments-differ - add('propagation-seconds', - default=default_propagation_seconds, - type=int, - help='The number of seconds to wait for DNS to propagate before asking the ACME server ' - 'to verify the DNS record.') - - def get_chall_pref(self, unused_domain): # pylint: disable=missing-docstring,no-self-use - return [challenges.DNS01] - - def prepare(self): # pylint: disable=missing-docstring - pass - - def perform(self, achalls): # pylint: disable=missing-docstring - self._setup_credentials() - - self._attempt_cleanup = True - - responses = [] - for achall in achalls: - domain = achall.domain - validation_domain_name = achall.validation_domain_name(domain) - validation = achall.validation(achall.account_key) - - self._perform(domain, validation_domain_name, validation) - responses.append(achall.response(achall.account_key)) - - # DNS updates take time to propagate and checking to see if the update has occurred is not - # reliable (the machine this code is running on might be able to see an update before - # the ACME server). So: we sleep for a short amount of time we believe to be long enough. - logger.info("Waiting %d seconds for DNS changes to propagate", - self.conf('propagation-seconds')) - sleep(self.conf('propagation-seconds')) - - return responses - - def cleanup(self, achalls): # pylint: disable=missing-docstring - if self._attempt_cleanup: - for achall in achalls: - domain = achall.domain - validation_domain_name = achall.validation_domain_name(domain) - validation = achall.validation(achall.account_key) - - self._cleanup(domain, validation_domain_name, validation) - - @abc.abstractmethod - def _setup_credentials(self): # pragma: no cover - """ - Establish credentials, prompting if necessary. - """ - raise NotImplementedError() - - @abc.abstractmethod - def _perform(self, domain, validation_domain_name, validation): # pragma: no cover - """ - Performs a dns-01 challenge by creating a DNS TXT record. - - :param str domain: The domain being validated. - :param str validation_domain_name: The validation record domain name. - :param str validation: The validation record content. - :raises errors.PluginError: If the challenge cannot be performed - """ - raise NotImplementedError() - - @abc.abstractmethod - def _cleanup(self, domain, validation_domain_name, validation): # pragma: no cover - """ - Deletes the DNS TXT record which would have been created by `_perform_achall`. - - Fails gracefully if no such record exists. - - :param str domain: The domain being validated. - :param str validation_domain_name: The validation record domain name. - :param str validation: The validation record content. - """ - raise NotImplementedError() - - def _configure(self, key, label): - """ - Ensure that a configuration value is available. - - If necessary, prompts the user and stores the result. - - :param str key: The configuration key. - :param str label: The user-friendly label for this piece of information. - """ - - configured_value = self.conf(key) - if not configured_value: - new_value = self._prompt_for_data(label) - - setattr(self.config, self.dest(key), new_value) - - def _configure_file(self, key, label, validator=None): - """ - Ensure that a configuration value is available for a path. - - If necessary, prompts the user and stores the result. - - :param str key: The configuration key. - :param str label: The user-friendly label for this piece of information. - """ - - configured_value = self.conf(key) - if not configured_value: - new_value = self._prompt_for_file(label, validator) - - setattr(self.config, self.dest(key), os.path.abspath(os.path.expanduser(new_value))) - - def _configure_credentials(self, key, label, required_variables=None, validator=None): - """ - As `_configure_file`, but for a credential configuration file. - - If necessary, prompts the user and stores the result. - - Always stores absolute paths to avoid issues during renewal. - - :param str key: The configuration key. - :param str label: The user-friendly label for this piece of information. - :param dict required_variables: Map of variable which must be present to error to display. - :param callable validator: A method which will be called to validate the - `CredentialsConfiguration` resulting from the supplied input after it has been validated - to contain the `required_variables`. Should throw a `~certbot.errors.PluginError` to - indicate any issue. - """ - - def __validator(filename): - configuration = CredentialsConfiguration(filename, self.dest) - - if required_variables: - configuration.require(required_variables) - - if validator: - validator(configuration) - - self._configure_file(key, label, __validator) - - credentials_configuration = CredentialsConfiguration(self.conf(key), self.dest) - if required_variables: - credentials_configuration.require(required_variables) - - if validator: - validator(credentials_configuration) - - return credentials_configuration - - @staticmethod - def _prompt_for_data(label): - """ - Prompt the user for a piece of information. - - :param str label: The user-friendly label for this piece of information. - :returns: The user's response (guaranteed non-empty). - :rtype: str - """ - - def __validator(i): - if not i: - raise errors.PluginError('Please enter your {0}.'.format(label)) - - code, response = ops.validated_input( - __validator, - 'Input your {0}'.format(label), - force_interactive=True) - - if code == display_util.OK: - return response - else: - raise errors.PluginError('{0} required to proceed.'.format(label)) - - @staticmethod - def _prompt_for_file(label, validator=None): - """ - Prompt the user for a path. - - :param str label: The user-friendly label for the file. - :param callable validator: A method which will be called to validate the supplied input - after it has been validated to be a non-empty path to an existing file. Should throw a - `~certbot.errors.PluginError` to indicate any issue. - :returns: The user's response (guaranteed to exist). - :rtype: str - """ - - def __validator(filename): - if not filename: - raise errors.PluginError('Please enter a valid path to your {0}.'.format(label)) - - filename = os.path.expanduser(filename) - - validate_file(filename) - - if validator: - validator(filename) - - code, response = ops.validated_directory( - __validator, - 'Input the path to your {0}'.format(label), - force_interactive=True) - - if code == display_util.OK: - return response - else: - raise errors.PluginError('{0} required to proceed.'.format(label)) - - -class CredentialsConfiguration(object): - """Represents a user-supplied filed which stores API credentials.""" - - def __init__(self, filename, mapper=lambda x: x): - """ - :param str filename: A path to the configuration file. - :param callable mapper: A transformation to apply to configuration key names - :raises errors.PluginError: If the file does not exist or is not a valid format. - """ - validate_file_permissions(filename) - - try: - self.confobj = configobj.ConfigObj(filename) - except configobj.ConfigObjError as e: - logger.debug("Error parsing credentials configuration: %s", e, exc_info=True) - raise errors.PluginError("Error parsing credentials configuration: {0}".format(e)) - - self.mapper = mapper - - def require(self, required_variables): - """Ensures that the supplied set of variables are all present in the file. - - :param dict required_variables: Map of variable which must be present to error to display. - :raises errors.PluginError: If one or more are missing. - """ - messages = [] - - for var in required_variables: - if not self._has(var): - messages.append('Property "{0}" not found (should be {1}).' - .format(self.mapper(var), required_variables[var])) - elif not self._get(var): - messages.append('Property "{0}" not set (should be {1}).' - .format(self.mapper(var), required_variables[var])) - - if messages: - raise errors.PluginError( - 'Missing {0} in credentials configuration file {1}:\n * {2}'.format( - 'property' if len(messages) == 1 else 'properties', - self.confobj.filename, - '\n * '.join(messages) - ) - ) - - def conf(self, var): - """Find a configuration value for variable `var`, as transformed by `mapper`. - - :param str var: The variable to get. - :returns: The value of the variable. - :rtype: str - """ - - return self._get(var) - - def _has(self, var): - return self.mapper(var) in self.confobj - - def _get(self, var): - return self.confobj.get(self.mapper(var)) - - -def validate_file(filename): - """Ensure that the specified file exists.""" - - if not os.path.exists(filename): - raise errors.PluginError('File not found: {0}'.format(filename)) - - if not os.path.isfile(filename): - raise errors.PluginError('Path is not a file: {0}'.format(filename)) - - -def validate_file_permissions(filename): - """Ensure that the specified file exists and warn about unsafe permissions.""" - - validate_file(filename) - - permissions = stat.S_IMODE(os.stat(filename).st_mode) - if permissions & stat.S_IRWXO: - logger.warning('Unsafe permissions on credentials configuration file: %s', filename) - - -def base_domain_name_guesses(domain): - """Return a list of progressively less-specific domain names. - - One of these will probably be the domain name known to the DNS provider. - - :Example: - - >>> base_domain_name_guesses('foo.bar.baz.example.com') - ['foo.bar.baz.example.com', 'bar.baz.example.com', 'baz.example.com', 'example.com', 'com'] - - :param str domain: The domain for which to return guesses. - :returns: The a list of less specific domain names. - :rtype: list - """ - - fragments = domain.split('.') - return ['.'.join(fragments[i:]) for i in range(0, len(fragments))] diff --git a/certbot/plugins/dns_common_lexicon.py b/certbot/plugins/dns_common_lexicon.py deleted file mode 100644 index 5b50cc28582..00000000000 --- a/certbot/plugins/dns_common_lexicon.py +++ /dev/null @@ -1,137 +0,0 @@ -"""Common code for DNS Authenticator Plugins built on Lexicon.""" -import logging - -from requests.exceptions import HTTPError, RequestException - -from acme.magic_typing import Union, Dict, Any # pylint: disable=unused-import,no-name-in-module -from certbot import errors -from certbot.plugins import dns_common - -# Lexicon is not declared as a dependency in Certbot itself, -# but in the Certbot plugins backed by Lexicon. -# So we catch import error here to allow this module to be -# always importable, even if it does not make sense to use it -# if Lexicon is not available, obviously. -try: - from lexicon.config import ConfigResolver -except ImportError: - ConfigResolver = None # type: ignore - -logger = logging.getLogger(__name__) - - -class LexiconClient(object): - """ - Encapsulates all communication with a DNS provider via Lexicon. - """ - - def __init__(self): - self.provider = None - - def add_txt_record(self, domain, record_name, record_content): - """ - Add a TXT record using the supplied information. - - :param str domain: The domain to use to look up the managed zone. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - :raises errors.PluginError: if an error occurs communicating with the DNS Provider API - """ - self._find_domain_id(domain) - - try: - self.provider.create_record(type='TXT', name=record_name, content=record_content) - except RequestException as e: - logger.debug('Encountered error adding TXT record: %s', e, exc_info=True) - raise errors.PluginError('Error adding TXT record: {0}'.format(e)) - - def del_txt_record(self, domain, record_name, record_content): - """ - Delete a TXT record using the supplied information. - - :param str domain: The domain to use to look up the managed zone. - :param str record_name: The record name (typically beginning with '_acme-challenge.'). - :param str record_content: The record content (typically the challenge validation). - :raises errors.PluginError: if an error occurs communicating with the DNS Provider API - """ - try: - self._find_domain_id(domain) - except errors.PluginError as e: - logger.debug('Encountered error finding domain_id during deletion: %s', e, - exc_info=True) - return - - try: - self.provider.delete_record(type='TXT', name=record_name, content=record_content) - except RequestException as e: - logger.debug('Encountered error deleting TXT record: %s', e, exc_info=True) - - def _find_domain_id(self, domain): - """ - Find the domain_id for a given domain. - - :param str domain: The domain for which to find the domain_id. - :raises errors.PluginError: if the domain_id cannot be found. - """ - - domain_name_guesses = dns_common.base_domain_name_guesses(domain) - - for domain_name in domain_name_guesses: - try: - if hasattr(self.provider, 'options'): - # For Lexicon 2.x - self.provider.options['domain'] = domain_name - else: - # For Lexicon 3.x - self.provider.domain = domain_name - - self.provider.authenticate() - - return # If `authenticate` doesn't throw an exception, we've found the right name - except HTTPError as e: - result = self._handle_http_error(e, domain_name) - - if result: - raise result - except Exception as e: # pylint: disable=broad-except - result = self._handle_general_error(e, domain_name) - - if result: - raise result - - raise errors.PluginError('Unable to determine zone identifier for {0} using zone names: {1}' - .format(domain, domain_name_guesses)) - - def _handle_http_error(self, e, domain_name): - return errors.PluginError('Error determining zone identifier for {0}: {1}.' - .format(domain_name, e)) - - def _handle_general_error(self, e, domain_name): - if not str(e).startswith('No domain found'): - return errors.PluginError('Unexpected error determining zone identifier for {0}: {1}' - .format(domain_name, e)) - - -def build_lexicon_config(lexicon_provider_name, lexicon_options, provider_options): - # type: (str, Dict, Dict) -> Union[ConfigResolver, Dict] - """ - Convenient function to build a Lexicon 2.x/3.x config object. - :param str lexicon_provider_name: the name of the lexicon provider to use - :param dict lexicon_options: options specific to lexicon - :param dict provider_options: options specific to provider - :return: configuration to apply to the provider - :rtype: ConfigurationResolver or dict - """ - config = {'provider_name': lexicon_provider_name} # type: Dict[str, Any] - config.update(lexicon_options) - if not ConfigResolver: - # Lexicon 2.x - config.update(provider_options) - else: - # Lexicon 3.x - provider_config = {} - provider_config.update(provider_options) - config[lexicon_provider_name] = provider_config - config = ConfigResolver().with_dict(config).with_env() - - return config diff --git a/certbot/plugins/dns_common_lexicon_test.py b/certbot/plugins/dns_common_lexicon_test.py deleted file mode 100644 index 986362ca9cb..00000000000 --- a/certbot/plugins/dns_common_lexicon_test.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Tests for certbot.plugins.dns_common_lexicon.""" - -import unittest - -import mock - -from certbot.plugins import dns_common_lexicon -from certbot.plugins import dns_test_common_lexicon - - -class LexiconClientTest(unittest.TestCase, dns_test_common_lexicon.BaseLexiconClientTest): - - class _FakeLexiconClient(dns_common_lexicon.LexiconClient): - pass - - def setUp(self): - super(LexiconClientTest, self).setUp() - - self.client = LexiconClientTest._FakeLexiconClient() - self.provider_mock = mock.MagicMock() - - self.client.provider = self.provider_mock - - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/plugins/dns_common_test.py b/certbot/plugins/dns_common_test.py deleted file mode 100644 index 9b0f0c87568..00000000000 --- a/certbot/plugins/dns_common_test.py +++ /dev/null @@ -1,233 +0,0 @@ -"""Tests for certbot.plugins.dns_common.""" - -import collections -import logging -import os -import unittest - -import mock - -from certbot import errors -from certbot.display import util as display_util -from certbot.plugins import dns_common -from certbot.plugins import dns_test_common -from certbot.tests import util - - -class DNSAuthenticatorTest(util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): - # pylint: disable=protected-access - - class _FakeDNSAuthenticator(dns_common.DNSAuthenticator): - _setup_credentials = mock.MagicMock() - _perform = mock.MagicMock() - _cleanup = mock.MagicMock() - - def __init__(self, *args, **kwargs): - # pylint: disable=protected-access - super(DNSAuthenticatorTest._FakeDNSAuthenticator, self).__init__(*args, **kwargs) - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return 'A fake authenticator for testing.' - - class _FakeConfig(object): - fake_propagation_seconds = 0 - fake_config_key = 1 - fake_other_key = None - fake_file_path = None - - def setUp(self): - super(DNSAuthenticatorTest, self).setUp() - - self.config = DNSAuthenticatorTest._FakeConfig() - - self.auth = DNSAuthenticatorTest._FakeDNSAuthenticator(self.config, "fake") - - def test_perform(self): - self.auth.perform([self.achall]) - - self.auth._perform.assert_called_once_with(dns_test_common.DOMAIN, mock.ANY, mock.ANY) - - def test_cleanup(self): - self.auth._attempt_cleanup = True - - self.auth.cleanup([self.achall]) - - self.auth._cleanup.assert_called_once_with(dns_test_common.DOMAIN, mock.ANY, mock.ANY) - - @util.patch_get_utility() - def test_prompt(self, mock_get_utility): - mock_display = mock_get_utility() - mock_display.input.side_effect = ((display_util.OK, "",), - (display_util.OK, "value",)) - - self.auth._configure("other_key", "") - self.assertEqual(self.auth.config.fake_other_key, "value") - - @util.patch_get_utility() - def test_prompt_canceled(self, mock_get_utility): - mock_display = mock_get_utility() - mock_display.input.side_effect = ((display_util.CANCEL, "c",),) - - self.assertRaises(errors.PluginError, self.auth._configure, "other_key", "") - - @util.patch_get_utility() - def test_prompt_file(self, mock_get_utility): - path = os.path.join(self.tempdir, 'file.ini') - open(path, "wb").close() - - mock_display = mock_get_utility() - mock_display.directory_select.side_effect = ((display_util.OK, "",), - (display_util.OK, "not-a-file.ini",), - (display_util.OK, self.tempdir), - (display_util.OK, path,)) - - self.auth._configure_file("file_path", "") - self.assertEqual(self.auth.config.fake_file_path, path) - - @util.patch_get_utility() - def test_prompt_file_canceled(self, mock_get_utility): - mock_display = mock_get_utility() - mock_display.directory_select.side_effect = ((display_util.CANCEL, "c",),) - - self.assertRaises(errors.PluginError, self.auth._configure_file, "file_path", "") - - def test_configure_credentials(self): - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"fake_test": "value"}, path) - setattr(self.config, "fake_credentials", path) - - credentials = self.auth._configure_credentials("credentials", "", {"test": ""}) - - self.assertEqual(credentials.conf("test"), "value") - - @util.patch_get_utility() - def test_prompt_credentials(self, mock_get_utility): - bad_path = os.path.join(self.tempdir, 'bad-file.ini') - dns_test_common.write({"fake_other": "other_value"}, bad_path) - - path = os.path.join(self.tempdir, 'file.ini') - dns_test_common.write({"fake_test": "value"}, path) - setattr(self.config, "fake_credentials", "") - - mock_display = mock_get_utility() - mock_display.directory_select.side_effect = ((display_util.OK, "",), - (display_util.OK, "not-a-file.ini",), - (display_util.OK, self.tempdir), - (display_util.OK, bad_path), - (display_util.OK, path,)) - - credentials = self.auth._configure_credentials("credentials", "", {"test": ""}) - self.assertEqual(credentials.conf("test"), "value") - - -class CredentialsConfigurationTest(util.TempDirTestCase): - class _MockLoggingHandler(logging.Handler): - messages = None - - def __init__(self, *args, **kwargs): - self.reset() - logging.Handler.__init__(self, *args, **kwargs) - - def emit(self, record): - self.messages[record.levelname.lower()].append(record.getMessage()) - - def reset(self): - """Allows the handler to be reset between tests.""" - self.messages = collections.defaultdict(list) - - def test_valid_file(self): - path = os.path.join(self.tempdir, 'too-permissive-file.ini') - - dns_test_common.write({"test": "value", "other": 1}, path) - - credentials_configuration = dns_common.CredentialsConfiguration(path) - self.assertEqual("value", credentials_configuration.conf("test")) - self.assertEqual("1", credentials_configuration.conf("other")) - - def test_nonexistent_file(self): - path = os.path.join(self.tempdir, 'not-a-file.ini') - - self.assertRaises(errors.PluginError, dns_common.CredentialsConfiguration, path) - - def test_valid_file_with_unsafe_permissions(self): - log = self._MockLoggingHandler() - dns_common.logger.addHandler(log) - - path = os.path.join(self.tempdir, 'too-permissive-file.ini') - open(path, "wb").close() - - dns_common.CredentialsConfiguration(path) - - self.assertEqual(1, len([_ for _ in log.messages['warning'] if _.startswith("Unsafe")])) - - -class CredentialsConfigurationRequireTest(util.TempDirTestCase): - - def setUp(self): - super(CredentialsConfigurationRequireTest, self).setUp() - - self.path = os.path.join(self.tempdir, 'file.ini') - - def _write(self, values): - dns_test_common.write(values, self.path) - - def test_valid(self): - self._write({"test": "value", "other": 1}) - - credentials_configuration = dns_common.CredentialsConfiguration(self.path) - credentials_configuration.require({"test": "", "other": ""}) - - def test_valid_but_extra(self): - self._write({"test": "value", "other": 1}) - - credentials_configuration = dns_common.CredentialsConfiguration(self.path) - credentials_configuration.require({"test": ""}) - - def test_valid_empty(self): - self._write({}) - - credentials_configuration = dns_common.CredentialsConfiguration(self.path) - credentials_configuration.require({}) - - def test_missing(self): - self._write({}) - - credentials_configuration = dns_common.CredentialsConfiguration(self.path) - self.assertRaises(errors.PluginError, credentials_configuration.require, {"test": ""}) - - def test_blank(self): - self._write({"test": ""}) - - credentials_configuration = dns_common.CredentialsConfiguration(self.path) - self.assertRaises(errors.PluginError, credentials_configuration.require, {"test": ""}) - - def test_typo(self): - self._write({"tets": "typo!"}) - - credentials_configuration = dns_common.CredentialsConfiguration(self.path) - self.assertRaises(errors.PluginError, credentials_configuration.require, {"test": ""}) - - -class DomainNameGuessTest(unittest.TestCase): - - def test_simple_case(self): - self.assertTrue( - 'example.com' in - dns_common.base_domain_name_guesses("example.com") - ) - - def test_sub_domain(self): - self.assertTrue( - 'example.com' in - dns_common.base_domain_name_guesses("foo.bar.baz.example.com") - ) - - def test_second_level_domain(self): - self.assertTrue( - 'example.co.uk' in - dns_common.base_domain_name_guesses("foo.bar.baz.example.co.uk") - ) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/plugins/dns_test_common.py b/certbot/plugins/dns_test_common.py deleted file mode 100644 index 54b656b20ac..00000000000 --- a/certbot/plugins/dns_test_common.py +++ /dev/null @@ -1,63 +0,0 @@ -"""Base test class for DNS authenticators.""" - -import os - -import configobj -import josepy as jose -import mock -import six -from acme import challenges - -from certbot import achallenges -from certbot.tests import acme_util -from certbot.tests import util as test_util - -DOMAIN = 'example.com' -KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) - - -class BaseAuthenticatorTest(object): - """ - A base test class to reduce duplication between test code for DNS Authenticator Plugins. - - Assumes: - * That subclasses also subclass unittest.TestCase - * That the authenticator is stored as self.auth - """ - - achall = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.DNS01, domain=DOMAIN, account_key=KEY) - - def test_more_info(self): - # pylint: disable=no-member - self.assertTrue(isinstance(self.auth.more_info(), six.string_types)) - - def test_get_chall_pref(self): - # pylint: disable=no-member - self.assertEqual(self.auth.get_chall_pref(None), [challenges.DNS01]) - - def test_parser_arguments(self): - m = mock.MagicMock() - - # pylint: disable=no-member - self.auth.add_parser_arguments(m) - - m.assert_any_call('propagation-seconds', type=int, default=mock.ANY, help=mock.ANY) - - -def write(values, path): - """Write the specified values to a config file. - - :param dict values: A map of values to write. - :param str path: Where to write the values. - """ - - config = configobj.ConfigObj() - - for key in values: - config[key] = values[key] - - with open(path, "wb") as f: - config.write(outfile=f) - - os.chmod(path, 0o600) diff --git a/certbot/plugins/dns_test_common_lexicon.py b/certbot/plugins/dns_test_common_lexicon.py deleted file mode 100644 index a221cf1bf0b..00000000000 --- a/certbot/plugins/dns_test_common_lexicon.py +++ /dev/null @@ -1,128 +0,0 @@ -"""Base test class for DNS authenticators built on Lexicon.""" - -import josepy as jose -import mock -from requests.exceptions import HTTPError, RequestException - -from certbot import errors -from certbot.plugins import dns_test_common -from certbot.tests import util as test_util - -DOMAIN = 'example.com' -KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) - -# These classes are intended to be subclassed/mixed in, so not all members are defined. -# pylint: disable=no-member - -class BaseLexiconAuthenticatorTest(dns_test_common.BaseAuthenticatorTest): - - def test_perform(self): - self.auth.perform([self.achall]) - - expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - def test_cleanup(self): - self.auth._attempt_cleanup = True # _attempt_cleanup | pylint: disable=protected-access - self.auth.cleanup([self.achall]) - - expected = [mock.call.del_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)] - self.assertEqual(expected, self.mock_client.mock_calls) - - -class BaseLexiconClientTest(object): - DOMAIN_NOT_FOUND = Exception('No domain found') - GENERIC_ERROR = RequestException - LOGIN_ERROR = HTTPError('400 Client Error: ...') - UNKNOWN_LOGIN_ERROR = HTTPError('500 Surprise! Error: ...') - - record_prefix = "_acme-challenge" - record_name = record_prefix + "." + DOMAIN - record_content = "bar" - - def test_add_txt_record(self): - self.client.add_txt_record(DOMAIN, self.record_name, self.record_content) - - self.provider_mock.create_record.assert_called_with(type='TXT', - name=self.record_name, - content=self.record_content) - - def test_add_txt_record_try_twice_to_find_domain(self): - self.provider_mock.authenticate.side_effect = [self.DOMAIN_NOT_FOUND, ''] - - self.client.add_txt_record(DOMAIN, self.record_name, self.record_content) - - self.provider_mock.create_record.assert_called_with(type='TXT', - name=self.record_name, - content=self.record_content) - - def test_add_txt_record_fail_to_find_domain(self): - self.provider_mock.authenticate.side_effect = [self.DOMAIN_NOT_FOUND, - self.DOMAIN_NOT_FOUND, - self.DOMAIN_NOT_FOUND,] - - self.assertRaises(errors.PluginError, - self.client.add_txt_record, - DOMAIN, self.record_name, self.record_content) - - def test_add_txt_record_fail_to_authenticate(self): - self.provider_mock.authenticate.side_effect = self.LOGIN_ERROR - - self.assertRaises(errors.PluginError, - self.client.add_txt_record, - DOMAIN, self.record_name, self.record_content) - - def test_add_txt_record_fail_to_authenticate_with_unknown_error(self): - self.provider_mock.authenticate.side_effect = self.UNKNOWN_LOGIN_ERROR - - self.assertRaises(errors.PluginError, - self.client.add_txt_record, - DOMAIN, self.record_name, self.record_content) - - def test_add_txt_record_error_finding_domain(self): - self.provider_mock.authenticate.side_effect = self.GENERIC_ERROR - - self.assertRaises(errors.PluginError, - self.client.add_txt_record, - DOMAIN, self.record_name, self.record_content) - - def test_add_txt_record_error_adding_record(self): - self.provider_mock.create_record.side_effect = self.GENERIC_ERROR - - self.assertRaises(errors.PluginError, - self.client.add_txt_record, - DOMAIN, self.record_name, self.record_content) - - def test_del_txt_record(self): - self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - self.provider_mock.delete_record.assert_called_with(type='TXT', - name=self.record_name, - content=self.record_content) - - def test_del_txt_record_fail_to_find_domain(self): - self.provider_mock.authenticate.side_effect = [self.DOMAIN_NOT_FOUND, - self.DOMAIN_NOT_FOUND, - self.DOMAIN_NOT_FOUND, ] - - self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - def test_del_txt_record_fail_to_authenticate(self): - self.provider_mock.authenticate.side_effect = self.LOGIN_ERROR - - self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - def test_del_txt_record_fail_to_authenticate_with_unknown_error(self): - self.provider_mock.authenticate.side_effect = self.UNKNOWN_LOGIN_ERROR - - self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - def test_del_txt_record_error_finding_domain(self): - self.provider_mock.authenticate.side_effect = self.GENERIC_ERROR - - self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) - - def test_del_txt_record_error_deleting_record(self): - self.provider_mock.delete_record.side_effect = self.GENERIC_ERROR - - self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) diff --git a/certbot/plugins/enhancements.py b/certbot/plugins/enhancements.py deleted file mode 100644 index 7ca09661005..00000000000 --- a/certbot/plugins/enhancements.py +++ /dev/null @@ -1,164 +0,0 @@ -"""New interface style Certbot enhancements""" -import abc -import six - -from certbot import constants - -from acme.magic_typing import Dict, List, Any # pylint: disable=unused-import, no-name-in-module - -def enabled_enhancements(config): - """ - Generator to yield the enabled new style enhancements. - - :param config: Configuration. - :type config: :class:`certbot.interfaces.IConfig` - """ - for enh in _INDEX: - if getattr(config, enh["cli_dest"]): - yield enh - -def are_requested(config): - """ - Checks if one or more of the requested enhancements are those of the new - enhancement interfaces. - - :param config: Configuration. - :type config: :class:`certbot.interfaces.IConfig` - """ - return any(enabled_enhancements(config)) - -def are_supported(config, installer): - """ - Checks that all of the requested enhancements are supported by the - installer. - - :param config: Configuration. - :type config: :class:`certbot.interfaces.IConfig` - - :param installer: Installer object - :type installer: interfaces.IInstaller - - :returns: If all the requested enhancements are supported by the installer - :rtype: bool - """ - for enh in enabled_enhancements(config): - if not isinstance(installer, enh["class"]): - return False - return True - -def enable(lineage, domains, installer, config): - """ - Run enable method for each requested enhancement that is supported. - - :param lineage: Certificate lineage object - :type lineage: certbot.storage.RenewableCert - - :param domains: List of domains in certificate to enhance - :type domains: str - - :param installer: Installer object - :type installer: interfaces.IInstaller - - :param config: Configuration. - :type config: :class:`certbot.interfaces.IConfig` - """ - for enh in enabled_enhancements(config): - getattr(installer, enh["enable_function"])(lineage, domains) - -def populate_cli(add): - """ - Populates the command line flags for certbot.cli.HelpfulParser - - :param add: Add function of certbot.cli.HelpfulParser - :type add: func - """ - for enh in _INDEX: - add(enh["cli_groups"], enh["cli_flag"], action=enh["cli_action"], - dest=enh["cli_dest"], default=enh["cli_flag_default"], - help=enh["cli_help"]) - - -@six.add_metaclass(abc.ABCMeta) -class AutoHSTSEnhancement(object): - """ - Enhancement interface that installer plugins can implement in order to - provide functionality that configures the software to have a - 'Strict-Transport-Security' with initially low max-age value that will - increase over time. - - The plugins implementing new style enhancements are responsible of handling - the saving of configuration checkpoints as well as calling possible restarts - of managed software themselves. For update_autohsts method, the installer may - have to call prepare() to finalize the plugin initialization. - - Methods: - enable_autohsts is called when the header is initially installed using a - low max-age value. - - update_autohsts is called every time when Certbot is run using 'renew' - verb. The max-age value should be increased over time using this method. - - deploy_autohsts is called for every lineage that has had its certificate - renewed. A long HSTS max-age value should be set here, as we should be - confident that the user is able to automatically renew their certificates. - - - """ - - @abc.abstractmethod - def update_autohsts(self, lineage, *args, **kwargs): - """ - Gets called for each lineage every time Certbot is run with 'renew' verb. - Implementation of this method should increase the max-age value. - - :param lineage: Certificate lineage object - :type lineage: certbot.storage.RenewableCert - - .. note:: prepare() method inherited from `interfaces.IPlugin` might need - to be called manually within implementation of this interface method - to finalize the plugin initialization. - """ - - @abc.abstractmethod - def deploy_autohsts(self, lineage, *args, **kwargs): - """ - Gets called for a lineage when its certificate is successfully renewed. - Long max-age value should be set in implementation of this method. - - :param lineage: Certificate lineage object - :type lineage: certbot.storage.RenewableCert - """ - - @abc.abstractmethod - def enable_autohsts(self, lineage, domains, *args, **kwargs): - """ - Enables the AutoHSTS enhancement, installing - Strict-Transport-Security header with a low initial value to be increased - over the subsequent runs of Certbot renew. - - :param lineage: Certificate lineage object - :type lineage: certbot.storage.RenewableCert - - :param domains: List of domains in certificate to enhance - :type domains: str - """ - -# This is used to configure internal new style enhancements in Certbot. These -# enhancement interfaces need to be defined in this file. Please do not modify -# this list from plugin code. -_INDEX = [ - { - "name": "AutoHSTS", - "cli_help": "Gradually increasing max-age value for HTTP Strict Transport "+ - "Security security header", - "cli_flag": "--auto-hsts", - "cli_flag_default": constants.CLI_DEFAULTS["auto_hsts"], - "cli_groups": ["security", "enhance"], - "cli_dest": "auto_hsts", - "cli_action": "store_true", - "class": AutoHSTSEnhancement, - "updater_function": "update_autohsts", - "deployer_function": "deploy_autohsts", - "enable_function": "enable_autohsts" - } -] # type: List[Dict[str, Any]] diff --git a/certbot/plugins/enhancements_test.py b/certbot/plugins/enhancements_test.py deleted file mode 100644 index 22f6f54e970..00000000000 --- a/certbot/plugins/enhancements_test.py +++ /dev/null @@ -1,65 +0,0 @@ -"""Tests for new style enhancements""" -import unittest -import mock - -from certbot.plugins import enhancements -from certbot.plugins import null - -import certbot.tests.util as test_util - - -class EnhancementTest(test_util.ConfigTestCase): - """Tests for new style enhancements in certbot.plugins.enhancements""" - - def setUp(self): - super(EnhancementTest, self).setUp() - self.mockinstaller = mock.MagicMock(spec=enhancements.AutoHSTSEnhancement) - - - @test_util.patch_get_utility() - def test_enhancement_enabled_enhancements(self, _): - FAKEINDEX = [ - { - "name": "autohsts", - "cli_dest": "auto_hsts", - }, - { - "name": "somethingelse", - "cli_dest": "something", - } - ] - with mock.patch("certbot.plugins.enhancements._INDEX", FAKEINDEX): - self.config.auto_hsts = True - self.config.something = True - enabled = list(enhancements.enabled_enhancements(self.config)) - self.assertEqual(len(enabled), 2) - self.assertTrue([i for i in enabled if i["name"] == "autohsts"]) - self.assertTrue([i for i in enabled if i["name"] == "somethingelse"]) - - def test_are_requested(self): - self.assertEqual( - len([i for i in enhancements.enabled_enhancements(self.config)]), 0) - self.assertFalse(enhancements.are_requested(self.config)) - self.config.auto_hsts = True - self.assertEqual( - len([i for i in enhancements.enabled_enhancements(self.config)]), 1) - self.assertTrue(enhancements.are_requested(self.config)) - - def test_are_supported(self): - self.config.auto_hsts = True - unsupported = null.Installer(self.config, "null") - self.assertTrue(enhancements.are_supported(self.config, self.mockinstaller)) - self.assertFalse(enhancements.are_supported(self.config, unsupported)) - - def test_enable(self): - self.config.auto_hsts = True - domains = ["example.com", "www.example.com"] - lineage = "lineage" - enhancements.enable(lineage, domains, self.mockinstaller, self.config) - self.assertTrue(self.mockinstaller.enable_autohsts.called) - self.assertEqual(self.mockinstaller.enable_autohsts.call_args[0], - (lineage, domains)) - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/certbot/plugins/manual.py b/certbot/plugins/manual.py deleted file mode 100644 index 8723a1c62a3..00000000000 --- a/certbot/plugins/manual.py +++ /dev/null @@ -1,247 +0,0 @@ -"""Manual authenticator plugin""" -import os - -import zope.component -import zope.interface - -from acme import challenges -from acme.magic_typing import Dict # pylint: disable=unused-import, no-name-in-module - -from certbot import achallenges # pylint: disable=unused-import -from certbot import interfaces -from certbot import errors -from certbot import hooks -from certbot import reverter -from certbot.plugins import common - - -class ManualTlsSni01(common.TLSSNI01): - """TLS-SNI-01 authenticator for the Manual plugin - - :ivar configurator: Authenticator object - :type configurator: :class:`~certbot.plugins.manual.Authenticator` - - :ivar list achalls: Annotated - class:`~certbot.achallenges.KeyAuthorizationAnnotatedChallenge` - challenges - - :param list indices: Meant to hold indices of challenges in a - larger array. NginxTlsSni01 is capable of solving many challenges - at once which causes an indexing issue within NginxConfigurator - who must return all responses in order. Imagine NginxConfigurator - maintaining state about where all of the http-01 Challenges, - TLS-SNI-01 Challenges belong in the response array. This is an - optional utility. - - :param str challenge_conf: location of the challenge config file - """ - - def perform(self): - """Create the SSL certificates and private keys""" - - for achall in self.achalls: - self._setup_challenge_cert(achall) - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(common.Plugin): - """Manual authenticator - - This plugin allows the user to perform the domain validation - challenge(s) themselves. This either be done manually by the user or - through shell scripts provided to Certbot. - - """ - - description = 'Manual configuration or run your own shell scripts' - hidden = True - long_description = ( - 'Authenticate through manual configuration or custom shell scripts. ' - 'When using shell scripts, an authenticator script must be provided. ' - 'The environment variables available to this script depend on the ' - 'type of challenge. $CERTBOT_DOMAIN will always contain the domain ' - 'being authenticated. For HTTP-01 and DNS-01, $CERTBOT_VALIDATION ' - 'is the validation string, and $CERTBOT_TOKEN is the filename of the ' - 'resource requested when performing an HTTP-01 challenge. When ' - 'performing a TLS-SNI-01 challenge, $CERTBOT_SNI_DOMAIN will contain ' - 'the SNI name for which the ACME server expects to be presented with ' - 'the self-signed certificate located at $CERTBOT_CERT_PATH. The ' - 'secret key needed to complete the TLS handshake is located at ' - '$CERTBOT_KEY_PATH. An additional cleanup script can also be ' - 'provided and can use the additional variable $CERTBOT_AUTH_OUTPUT ' - 'which contains the stdout output from the auth script.') - _DNS_INSTRUCTIONS = """\ -Please deploy a DNS TXT record under the name -{domain} with the following value: - -{validation} - -Before continuing, verify the record is deployed.""" - _HTTP_INSTRUCTIONS = """\ -Create a file containing just this data: - -{validation} - -And make it available on your web server at this URL: - -{uri} -""" - _TLSSNI_INSTRUCTIONS = """\ -Configure the service listening on port {port} to present the certificate -{cert} -using the secret key -{key} -when it receives a TLS ClientHello with the SNI extension set to -{sni_domain} -""" - _SUBSEQUENT_CHALLENGE_INSTRUCTIONS = """ -(This must be set up in addition to the previous challenges; do not remove, -replace, or undo the previous challenge tasks yet.) -""" - _SUBSEQUENT_DNS_CHALLENGE_INSTRUCTIONS = """ -(This must be set up in addition to the previous challenges; do not remove, -replace, or undo the previous challenge tasks yet. Note that you might be -asked to create multiple distinct TXT records with the same name. This is -permitted by DNS standards.) -""" - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.reverter = reverter.Reverter(self.config) - self.reverter.recovery_routine() - self.env = dict() \ - # type: Dict[achallenges.KeyAuthorizationAnnotatedChallenge, Dict[str, str]] - self.tls_sni_01 = None - self.subsequent_dns_challenge = False - self.subsequent_any_challenge = False - - @classmethod - def add_parser_arguments(cls, add): - add('auth-hook', - help='Path or command to execute for the authentication script') - add('cleanup-hook', - help='Path or command to execute for the cleanup script') - add('public-ip-logging-ok', action='store_true', - help='Automatically allows public IP logging (default: Ask)') - - def prepare(self): # pylint: disable=missing-docstring - if self.config.noninteractive_mode and not self.conf('auth-hook'): - raise errors.PluginError( - 'An authentication script must be provided with --{0} when ' - 'using the manual plugin non-interactively.'.format( - self.option_name('auth-hook'))) - self._validate_hooks() - - def _validate_hooks(self): - if self.config.validate_hooks: - for name in ('auth-hook', 'cleanup-hook'): - hook = self.conf(name) - if hook is not None: - hook_prefix = self.option_name(name)[:-len('-hook')] - hooks.validate_hook(hook, hook_prefix) - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return ( - 'This plugin allows the user to customize setup for domain ' - 'validation challenges either through shell scripts provided by ' - 'the user or by performing the setup manually.') - - def get_chall_pref(self, domain): - # pylint: disable=missing-docstring,no-self-use,unused-argument - return [challenges.HTTP01, challenges.DNS01, challenges.TLSSNI01] - - def perform(self, achalls): # pylint: disable=missing-docstring - self._verify_ip_logging_ok() - if self.conf('auth-hook'): - perform_achall = self._perform_achall_with_script - else: - perform_achall = self._perform_achall_manually - - responses = [] - for achall in achalls: - if isinstance(achall.chall, challenges.TLSSNI01): - # Make a new ManualTlsSni01 instance for each challenge - # because the manual plugin deals with one challenge at a time. - self.tls_sni_01 = ManualTlsSni01(self) - self.tls_sni_01.add_chall(achall) - self.tls_sni_01.perform() - perform_achall(achall) - responses.append(achall.response(achall.account_key)) - return responses - - def _verify_ip_logging_ok(self): - if not self.conf('public-ip-logging-ok'): - cli_flag = '--{0}'.format(self.option_name('public-ip-logging-ok')) - msg = ('NOTE: The IP of this machine will be publicly logged as ' - "having requested this certificate. If you're running " - 'certbot in manual mode on a machine that is not your ' - "server, please ensure you're okay with that.\n\n" - 'Are you OK with your IP being logged?') - display = zope.component.getUtility(interfaces.IDisplay) - if display.yesno(msg, cli_flag=cli_flag, force_interactive=True): - setattr(self.config, self.dest('public-ip-logging-ok'), True) - else: - raise errors.PluginError('Must agree to IP logging to proceed') - - def _perform_achall_with_script(self, achall): - env = dict(CERTBOT_DOMAIN=achall.domain, - CERTBOT_VALIDATION=achall.validation(achall.account_key)) - if isinstance(achall.chall, challenges.HTTP01): - env['CERTBOT_TOKEN'] = achall.chall.encode('token') - else: - os.environ.pop('CERTBOT_TOKEN', None) - if isinstance(achall.chall, challenges.TLSSNI01): - env['CERTBOT_CERT_PATH'] = self.tls_sni_01.get_cert_path(achall) - env['CERTBOT_KEY_PATH'] = self.tls_sni_01.get_key_path(achall) - env['CERTBOT_SNI_DOMAIN'] = self.tls_sni_01.get_z_domain(achall) - os.environ.pop('CERTBOT_VALIDATION', None) - env.pop('CERTBOT_VALIDATION') - else: - os.environ.pop('CERTBOT_CERT_PATH', None) - os.environ.pop('CERTBOT_KEY_PATH', None) - os.environ.pop('CERTBOT_SNI_DOMAIN', None) - os.environ.update(env) - _, out = hooks.execute(self.conf('auth-hook')) - env['CERTBOT_AUTH_OUTPUT'] = out.strip() - self.env[achall] = env - - def _perform_achall_manually(self, achall): - validation = achall.validation(achall.account_key) - if isinstance(achall.chall, challenges.HTTP01): - msg = self._HTTP_INSTRUCTIONS.format( - achall=achall, encoded_token=achall.chall.encode('token'), - port=self.config.http01_port, - uri=achall.chall.uri(achall.domain), validation=validation) - elif isinstance(achall.chall, challenges.DNS01): - msg = self._DNS_INSTRUCTIONS.format( - domain=achall.validation_domain_name(achall.domain), - validation=validation) - else: - assert isinstance(achall.chall, challenges.TLSSNI01) - msg = self._TLSSNI_INSTRUCTIONS.format( - cert=self.tls_sni_01.get_cert_path(achall), - key=self.tls_sni_01.get_key_path(achall), - port=self.config.tls_sni_01_port, - sni_domain=self.tls_sni_01.get_z_domain(achall)) - if isinstance(achall.chall, challenges.DNS01): - if self.subsequent_dns_challenge: - # 2nd or later dns-01 challenge - msg += self._SUBSEQUENT_DNS_CHALLENGE_INSTRUCTIONS - self.subsequent_dns_challenge = True - elif self.subsequent_any_challenge: - # 2nd or later challenge of another type - msg += self._SUBSEQUENT_CHALLENGE_INSTRUCTIONS - display = zope.component.getUtility(interfaces.IDisplay) - display.notification(msg, wrap=False, force_interactive=True) - self.subsequent_any_challenge = True - - def cleanup(self, achalls): # pylint: disable=missing-docstring - if self.conf('cleanup-hook'): - for achall in achalls: - env = self.env.pop(achall) - if 'CERTBOT_TOKEN' not in env: - os.environ.pop('CERTBOT_TOKEN', None) - os.environ.update(env) - hooks.execute(self.conf('cleanup-hook')) - self.reverter.recovery_routine() diff --git a/certbot/plugins/manual_test.py b/certbot/plugins/manual_test.py deleted file mode 100644 index 0938e8a7def..00000000000 --- a/certbot/plugins/manual_test.py +++ /dev/null @@ -1,171 +0,0 @@ -"""Tests for certbot.plugins.manual""" -import os -import unittest - -import six -import mock -import sys - -from acme import challenges - -from certbot import errors - -from certbot.tests import acme_util -from certbot.tests import util as test_util - - -class AuthenticatorTest(test_util.TempDirTestCase): - """Tests for certbot.plugins.manual.Authenticator.""" - - def setUp(self): - super(AuthenticatorTest, self).setUp() - self.http_achall = acme_util.HTTP01_A - self.dns_achall = acme_util.DNS01_A - self.dns_achall_2 = acme_util.DNS01_A_2 - self.tls_sni_achall = acme_util.TLSSNI01_A - self.achalls = [self.http_achall, self.dns_achall, self.tls_sni_achall, self.dns_achall_2] - for d in ["config_dir", "work_dir", "in_progress"]: - os.mkdir(os.path.join(self.tempdir, d)) - # "backup_dir" and "temp_checkpoint_dir" get created in - # certbot.util.make_or_verify_dir() during the Reverter - # initialization. - self.config = mock.MagicMock( - http01_port=0, manual_auth_hook=None, manual_cleanup_hook=None, - manual_public_ip_logging_ok=False, noninteractive_mode=False, - validate_hooks=False, - config_dir=os.path.join(self.tempdir, "config_dir"), - work_dir=os.path.join(self.tempdir, "work_dir"), - backup_dir=os.path.join(self.tempdir, "backup_dir"), - temp_checkpoint_dir=os.path.join( - self.tempdir, "temp_checkpoint_dir"), - in_progress_dir=os.path.join(self.tempdir, "in_progess"), - tls_sni_01_port=5001) - - from certbot.plugins.manual import Authenticator - self.auth = Authenticator(self.config, name='manual') - - def test_prepare_no_hook_noninteractive(self): - self.config.noninteractive_mode = True - self.assertRaises(errors.PluginError, self.auth.prepare) - - def test_prepare_bad_hook(self): - self.config.manual_auth_hook = os.path.abspath(os.sep) # is / on UNIX - self.config.validate_hooks = True - self.assertRaises(errors.HookCommandNotFound, self.auth.prepare) - - def test_more_info(self): - self.assertTrue(isinstance(self.auth.more_info(), six.string_types)) - - def test_get_chall_pref(self): - self.assertEqual(self.auth.get_chall_pref('example.org'), - [challenges.HTTP01, - challenges.DNS01, - challenges.TLSSNI01]) - - @test_util.patch_get_utility() - def test_ip_logging_not_ok(self, mock_get_utility): - mock_get_utility().yesno.return_value = False - self.assertRaises(errors.PluginError, self.auth.perform, []) - - @test_util.patch_get_utility() - def test_ip_logging_ok(self, mock_get_utility): - mock_get_utility().yesno.return_value = True - self.auth.perform([]) - self.assertTrue(self.config.manual_public_ip_logging_ok) - - def test_script_perform(self): - self.config.manual_public_ip_logging_ok = True - self.config.manual_auth_hook = ( - '{0} -c "from __future__ import print_function;' - 'import os; print(os.environ.get(\'CERTBOT_DOMAIN\'));' - 'print(os.environ.get(\'CERTBOT_TOKEN\', \'notoken\'));' - 'print(os.environ.get(\'CERTBOT_CERT_PATH\', \'nocert\'));' - 'print(os.environ.get(\'CERTBOT_KEY_PATH\', \'nokey\'));' - 'print(os.environ.get(\'CERTBOT_SNI_DOMAIN\', \'nosnidomain\'));' - 'print(os.environ.get(\'CERTBOT_VALIDATION\', \'novalidation\'));"' - .format(sys.executable)) - dns_expected = '{0}\n{1}\n{2}\n{3}\n{4}\n{5}'.format( - self.dns_achall.domain, 'notoken', - 'nocert', 'nokey', 'nosnidomain', - self.dns_achall.validation(self.dns_achall.account_key)) - http_expected = '{0}\n{1}\n{2}\n{3}\n{4}\n{5}'.format( - self.http_achall.domain, self.http_achall.chall.encode('token'), - 'nocert', 'nokey', 'nosnidomain', - self.http_achall.validation(self.http_achall.account_key)) - - self.assertEqual( - self.auth.perform(self.achalls), - [achall.response(achall.account_key) for achall in self.achalls]) - self.assertEqual( - self.auth.env[self.dns_achall]['CERTBOT_AUTH_OUTPUT'], - dns_expected) - self.assertEqual( - self.auth.env[self.http_achall]['CERTBOT_AUTH_OUTPUT'], - http_expected) - # tls_sni_01 challenge must be perform()ed above before we can - # get the cert_path and key_path. - tls_sni_expected = '{0}\n{1}\n{2}\n{3}\n{4}\n{5}'.format( - self.tls_sni_achall.domain, 'notoken', - self.auth.tls_sni_01.get_cert_path(self.tls_sni_achall), - self.auth.tls_sni_01.get_key_path(self.tls_sni_achall), - self.auth.tls_sni_01.get_z_domain(self.tls_sni_achall), - 'novalidation') - self.assertEqual( - self.auth.env[self.tls_sni_achall]['CERTBOT_AUTH_OUTPUT'], - tls_sni_expected) - - @test_util.patch_get_utility() - def test_manual_perform(self, mock_get_utility): - self.config.manual_public_ip_logging_ok = True - self.assertEqual( - self.auth.perform(self.achalls), - [achall.response(achall.account_key) for achall in self.achalls]) - for i, (args, kwargs) in enumerate(mock_get_utility().notification.call_args_list): - achall = self.achalls[i] - if isinstance(achall.chall, challenges.TLSSNI01): - self.assertTrue( - self.auth.tls_sni_01.get_cert_path( - self.tls_sni_achall) in args[0]) - else: - self.assertTrue( - achall.validation(achall.account_key) in args[0]) - self.assertFalse(kwargs['wrap']) - - @test_util.broken_on_windows - def test_cleanup(self): - self.config.manual_public_ip_logging_ok = True - self.config.manual_auth_hook = 'echo foo;' - self.config.manual_cleanup_hook = '# cleanup' - self.auth.perform(self.achalls) - - for achall in self.achalls: - self.auth.cleanup([achall]) - self.assertEqual(os.environ['CERTBOT_AUTH_OUTPUT'], 'foo') - self.assertEqual(os.environ['CERTBOT_DOMAIN'], achall.domain) - if (isinstance(achall.chall, challenges.HTTP01) or - isinstance(achall.chall, challenges.DNS01)): - self.assertEqual( - os.environ['CERTBOT_VALIDATION'], - achall.validation(achall.account_key)) - if isinstance(achall.chall, challenges.HTTP01): - self.assertEqual( - os.environ['CERTBOT_TOKEN'], - achall.chall.encode('token')) - else: - self.assertFalse('CERTBOT_TOKEN' in os.environ) - if isinstance(achall.chall, challenges.TLSSNI01): - self.assertEqual( - os.environ['CERTBOT_CERT_PATH'], - self.auth.tls_sni_01.get_cert_path(achall)) - self.assertEqual( - os.environ['CERTBOT_KEY_PATH'], - self.auth.tls_sni_01.get_key_path(achall)) - self.assertFalse( - os.path.exists(os.environ['CERTBOT_CERT_PATH'])) - self.assertFalse( - os.path.exists(os.environ['CERTBOT_KEY_PATH'])) - - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/certbot/plugins/null.py b/certbot/plugins/null.py deleted file mode 100644 index 87c0737a5c0..00000000000 --- a/certbot/plugins/null.py +++ /dev/null @@ -1,59 +0,0 @@ -"""Null plugin.""" -import logging - -import zope.component -import zope.interface - -from certbot import interfaces -from certbot.plugins import common - - -logger = logging.getLogger(__name__) - - -@zope.interface.implementer(interfaces.IInstaller) -@zope.interface.provider(interfaces.IPluginFactory) -class Installer(common.Plugin): - """Null installer.""" - - description = "Null Installer" - hidden = True - - # pylint: disable=missing-docstring,no-self-use - - def prepare(self): - pass # pragma: no cover - - def more_info(self): - return "Installer that doesn't do anything (for testing)." - - def get_all_names(self): - return [] - - def deploy_cert(self, domain, cert_path, key_path, - chain_path=None, fullchain_path=None): - pass # pragma: no cover - - def enhance(self, domain, enhancement, options=None): - pass # pragma: no cover - - def supported_enhancements(self): - return [] - - def save(self, title=None, temporary=False): - pass # pragma: no cover - - def rollback_checkpoints(self, rollback=1): - pass # pragma: no cover - - def recovery_routine(self): - pass # pragma: no cover - - def view_config_changes(self): - pass # pragma: no cover - - def config_test(self): - pass # pragma: no cover - - def restart(self): - pass # pragma: no cover diff --git a/certbot/plugins/null_test.py b/certbot/plugins/null_test.py deleted file mode 100644 index d5de33fb349..00000000000 --- a/certbot/plugins/null_test.py +++ /dev/null @@ -1,22 +0,0 @@ -"""Tests for certbot.plugins.null.""" -import unittest -import six - -import mock - - -class InstallerTest(unittest.TestCase): - """Tests for certbot.plugins.null.Installer.""" - - def setUp(self): - from certbot.plugins.null import Installer - self.installer = Installer(config=mock.MagicMock(), name="null") - - def test_it(self): - self.assertTrue(isinstance(self.installer.more_info(), six.string_types)) - self.assertEqual([], self.installer.get_all_names()) - self.assertEqual([], self.installer.supported_enhancements()) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/plugins/selection.py b/certbot/plugins/selection.py deleted file mode 100644 index 9c2138247ae..00000000000 --- a/certbot/plugins/selection.py +++ /dev/null @@ -1,341 +0,0 @@ -"""Decide which plugins to use for authentication & installation""" -from __future__ import print_function - -import os -import logging - -import six -import zope.component - -from certbot import errors -from certbot import interfaces - -from certbot.display import util as display_util - -logger = logging.getLogger(__name__) -z_util = zope.component.getUtility - -def pick_configurator( - config, default, plugins, - question="How would you like to authenticate and install " - "certificates?"): - """Pick configurator plugin.""" - return pick_plugin( - config, default, plugins, question, - (interfaces.IAuthenticator, interfaces.IInstaller)) - - -def pick_installer(config, default, plugins, - question="How would you like to install certificates?"): - """Pick installer plugin.""" - return pick_plugin( - config, default, plugins, question, (interfaces.IInstaller,)) - - -def pick_authenticator( - config, default, plugins, question="How would you " - "like to authenticate with the ACME CA?"): - """Pick authentication plugin.""" - return pick_plugin( - config, default, plugins, question, (interfaces.IAuthenticator,)) - -def get_unprepared_installer(config, plugins): - """ - Get an unprepared interfaces.IInstaller object. - - :param certbot.interfaces.IConfig config: Configuration - :param certbot.plugins.disco.PluginsRegistry plugins: - All plugins registered as entry points. - - :returns: Unprepared installer plugin or None - :rtype: IPlugin or None - """ - - _, req_inst = cli_plugin_requests(config) - if not req_inst: - return None - installers = plugins.filter(lambda p_ep: p_ep.name == req_inst) - installers.init(config) - installers = installers.verify((interfaces.IInstaller,)) - if len(installers) > 1: - raise errors.PluginSelectionError( - "Found multiple installers with the name %s, Certbot is unable to " - "determine which one to use. Skipping." % req_inst) - if installers: - inst = list(installers.values())[0] - logger.debug("Selecting plugin: %s", inst) - return inst.init(config) - else: - raise errors.PluginSelectionError( - "Could not select or initialize the requested installer %s." % req_inst) - -def pick_plugin(config, default, plugins, question, ifaces): - """Pick plugin. - - :param certbot.interfaces.IConfig: Configuration - :param str default: Plugin name supplied by user or ``None``. - :param certbot.plugins.disco.PluginsRegistry plugins: - All plugins registered as entry points. - :param str question: Question to be presented to the user in case - multiple candidates are found. - :param list ifaces: Interfaces that plugins must provide. - - :returns: Initialized plugin. - :rtype: IPlugin - - """ - if default is not None: - # throw more UX-friendly error if default not in plugins - filtered = plugins.filter(lambda p_ep: p_ep.name == default) - else: - if config.noninteractive_mode: - # it's really bad to auto-select the single available plugin in - # non-interactive mode, because an update could later add a second - # available plugin - raise errors.MissingCommandlineFlag( - "Missing command line flags. For non-interactive execution, " - "you will need to specify a plugin on the command line. Run " - "with '--help plugins' to see a list of options, and see " - "https://eff.org/letsencrypt-plugins for more detail on what " - "the plugins do and how to use them.") - - filtered = plugins.visible().ifaces(ifaces) - - filtered.init(config) - verified = filtered.verify(ifaces) - verified.prepare() - prepared = verified.available() - - if len(prepared) > 1: - logger.debug("Multiple candidate plugins: %s", prepared) - plugin_ep = choose_plugin(list(six.itervalues(prepared)), question) - if plugin_ep is None: - return None - else: - return plugin_ep.init() - elif len(prepared) == 1: - plugin_ep = list(prepared.values())[0] - logger.debug("Single candidate plugin: %s", plugin_ep) - if plugin_ep.misconfigured: - return None - return plugin_ep.init() - else: - logger.debug("No candidate plugin") - return None - - -def choose_plugin(prepared, question): - """Allow the user to choose their plugin. - - :param list prepared: List of `~.PluginEntryPoint`. - :param str question: Question to be presented to the user. - - :returns: Plugin entry point chosen by the user. - :rtype: `~.PluginEntryPoint` - - """ - opts = [plugin_ep.description_with_name + - (" [Misconfigured]" if plugin_ep.misconfigured else "") - for plugin_ep in prepared] - names = set(plugin_ep.name for plugin_ep in prepared) - - while True: - disp = z_util(interfaces.IDisplay) - if "CERTBOT_AUTO" in os.environ and names == set(("apache", "nginx")): - # The possibility of being offered exactly apache and nginx here - # is new interactivity brought by https://github.com/certbot/certbot/issues/4079, - # so set apache as a default for those kinds of non-interactive use - # (the user will get a warning to set --non-interactive or --force-interactive) - apache_idx = [n for n, p in enumerate(prepared) if p.name == "apache"][0] - code, index = disp.menu(question, opts, default=apache_idx) - else: - code, index = disp.menu(question, opts, force_interactive=True) - - if code == display_util.OK: - plugin_ep = prepared[index] - if plugin_ep.misconfigured: - z_util(interfaces.IDisplay).notification( - "The selected plugin encountered an error while parsing " - "your server configuration and cannot be used. The error " - "was:\n\n{0}".format(plugin_ep.prepare()), pause=False) - else: - return plugin_ep - else: - return None - -noninstaller_plugins = ["webroot", "manual", "standalone", "dns-cloudflare", "dns-cloudxns", - "dns-digitalocean", "dns-dnsimple", "dns-dnsmadeeasy", "dns-gehirn", - "dns-google", "dns-linode", "dns-luadns", "dns-nsone", "dns-ovh", - "dns-rfc2136", "dns-route53", "dns-sakuracloud"] - -def record_chosen_plugins(config, plugins, auth, inst): - "Update the config entries to reflect the plugins we actually selected." - config.authenticator = plugins.find_init(auth).name if auth else None - config.installer = plugins.find_init(inst).name if inst else None - logger.info("Plugins selected: Authenticator %s, Installer %s", - config.authenticator, config.installer) - - -def choose_configurator_plugins(config, plugins, verb): - # pylint: disable=too-many-branches - """ - Figure out which configurator we're going to use, modifies - config.authenticator and config.installer strings to reflect that choice if - necessary. - - :raises errors.PluginSelectionError if there was a problem - - :returns: (an `IAuthenticator` or None, an `IInstaller` or None) - :rtype: tuple - """ - - req_auth, req_inst = cli_plugin_requests(config) - installer_question = None - - if verb == "enhance": - installer_question = ("Which installer would you like to use to " - "configure the selected enhancements?") - - # Which plugins do we need? - if verb == "run": - need_inst = need_auth = True - from certbot.cli import cli_command - if req_auth in noninstaller_plugins and not req_inst: - msg = ('With the {0} plugin, you probably want to use the "certonly" command, eg:{1}' - '{1} {2} certonly --{0}{1}{1}' - '(Alternatively, add a --installer flag. See https://eff.org/letsencrypt-plugins' - '{1} and "--help plugins" for more information.)'.format( - req_auth, os.linesep, cli_command)) - - raise errors.MissingCommandlineFlag(msg) - else: - need_inst = need_auth = False - if verb == "certonly": - need_auth = True - if verb == "install" or verb == "enhance": - need_inst = True - if config.authenticator: - logger.warning("Specifying an authenticator doesn't make sense when " - "running Certbot with verb \"%s\"", verb) - # Try to meet the user's request and/or ask them to pick plugins - authenticator = installer = None - if verb == "run" and req_auth == req_inst: - # Unless the user has explicitly asked for different auth/install, - # only consider offering a single choice - authenticator = installer = pick_configurator(config, req_inst, plugins) - else: - if need_inst or req_inst: - installer = pick_installer(config, req_inst, plugins, installer_question) - if need_auth: - authenticator = pick_authenticator(config, req_auth, plugins) - logger.debug("Selected authenticator %s and installer %s", authenticator, installer) - - # Report on any failures - if need_inst and not installer: - diagnose_configurator_problem("installer", req_inst, plugins) - if need_auth and not authenticator: - diagnose_configurator_problem("authenticator", req_auth, plugins) - - record_chosen_plugins(config, plugins, authenticator, installer) - return installer, authenticator - - -def set_configurator(previously, now): - """ - Setting configurators multiple ways is okay, as long as they all agree - :param str previously: previously identified request for the installer/authenticator - :param str requested: the request currently being processed - """ - if not now: - # we're not actually setting anything - return previously - if previously: - if previously != now: - msg = "Too many flags setting configurators/installers/authenticators {0} -> {1}" - raise errors.PluginSelectionError(msg.format(repr(previously), repr(now))) - return now - - -def cli_plugin_requests(config): # pylint: disable=too-many-branches - """ - Figure out which plugins the user requested with CLI and config options - - :returns: (requested authenticator string or None, requested installer string or None) - :rtype: tuple - """ - req_inst = req_auth = config.configurator - req_inst = set_configurator(req_inst, config.installer) - req_auth = set_configurator(req_auth, config.authenticator) - - if config.nginx: - req_inst = set_configurator(req_inst, "nginx") - req_auth = set_configurator(req_auth, "nginx") - if config.apache: - req_inst = set_configurator(req_inst, "apache") - req_auth = set_configurator(req_auth, "apache") - if config.standalone: - req_auth = set_configurator(req_auth, "standalone") - if config.webroot: - req_auth = set_configurator(req_auth, "webroot") - if config.manual: - req_auth = set_configurator(req_auth, "manual") - if config.dns_cloudflare: - req_auth = set_configurator(req_auth, "dns-cloudflare") - if config.dns_cloudxns: - req_auth = set_configurator(req_auth, "dns-cloudxns") - if config.dns_digitalocean: - req_auth = set_configurator(req_auth, "dns-digitalocean") - if config.dns_dnsimple: - req_auth = set_configurator(req_auth, "dns-dnsimple") - if config.dns_dnsmadeeasy: - req_auth = set_configurator(req_auth, "dns-dnsmadeeasy") - if config.dns_gehirn: - req_auth = set_configurator(req_auth, "dns-gehirn") - if config.dns_google: - req_auth = set_configurator(req_auth, "dns-google") - if config.dns_linode: - req_auth = set_configurator(req_auth, "dns-linode") - if config.dns_luadns: - req_auth = set_configurator(req_auth, "dns-luadns") - if config.dns_nsone: - req_auth = set_configurator(req_auth, "dns-nsone") - if config.dns_ovh: - req_auth = set_configurator(req_auth, "dns-ovh") - if config.dns_rfc2136: - req_auth = set_configurator(req_auth, "dns-rfc2136") - if config.dns_route53: - req_auth = set_configurator(req_auth, "dns-route53") - if config.dns_sakuracloud: - req_auth = set_configurator(req_auth, "dns-sakuracloud") - logger.debug("Requested authenticator %s and installer %s", req_auth, req_inst) - return req_auth, req_inst - - -def diagnose_configurator_problem(cfg_type, requested, plugins): - """ - Raise the most helpful error message about a plugin being unavailable - - :param str cfg_type: either "installer" or "authenticator" - :param str requested: the plugin that was requested - :param .PluginsRegistry plugins: available plugins - - :raises error.PluginSelectionError: if there was a problem - """ - - if requested: - if requested not in plugins: - msg = "The requested {0} plugin does not appear to be installed".format(requested) - else: - msg = ("The {0} plugin is not working; there may be problems with " - "your existing configuration.\nThe error was: {1!r}" - .format(requested, plugins[requested].problem)) - elif cfg_type == "installer": - from certbot.cli import cli_command - msg = ('Certbot doesn\'t know how to automatically configure the web ' - 'server on this system. However, it can still get a certificate for ' - 'you. Please run "{0} certonly" to do so. You\'ll need to ' - 'manually configure your web server to use the resulting ' - 'certificate.').format(cli_command) - else: - msg = "{0} could not be determined or is not installed".format(cfg_type) - raise errors.PluginSelectionError(msg) diff --git a/certbot/plugins/selection_test.py b/certbot/plugins/selection_test.py deleted file mode 100644 index 5f8e425168a..00000000000 --- a/certbot/plugins/selection_test.py +++ /dev/null @@ -1,220 +0,0 @@ -"""Tests for letsencrypt.plugins.selection""" -import os -import sys -import unittest - -import mock -import zope.component - -from certbot import errors -from certbot import interfaces - -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module -from certbot.display import util as display_util -from certbot.plugins.disco import PluginsRegistry -from certbot.tests import util as test_util - - -class ConveniencePickPluginTest(unittest.TestCase): - """Tests for certbot.plugins.selection.pick_*.""" - - def _test(self, fun, ifaces): - config = mock.Mock() - default = mock.Mock() - plugins = mock.Mock() - - with mock.patch("certbot.plugins.selection.pick_plugin") as mock_p: - mock_p.return_value = "foo" - self.assertEqual("foo", fun(config, default, plugins, "Question?")) - mock_p.assert_called_once_with( - config, default, plugins, "Question?", ifaces) - - def test_authenticator(self): - from certbot.plugins.selection import pick_authenticator - self._test(pick_authenticator, (interfaces.IAuthenticator,)) - - def test_installer(self): - from certbot.plugins.selection import pick_installer - self._test(pick_installer, (interfaces.IInstaller,)) - - def test_configurator(self): - from certbot.plugins.selection import pick_configurator - self._test(pick_configurator, - (interfaces.IAuthenticator, interfaces.IInstaller)) - - -class PickPluginTest(unittest.TestCase): - """Tests for certbot.plugins.selection.pick_plugin.""" - - def setUp(self): - self.config = mock.Mock(noninteractive_mode=False) - self.default = None - self.reg = mock.MagicMock() - self.question = "Question?" - self.ifaces = [] # type: List[interfaces.IPlugin] - - def _call(self): - from certbot.plugins.selection import pick_plugin - return pick_plugin(self.config, self.default, self.reg, - self.question, self.ifaces) - - def test_default_provided(self): - self.default = "foo" - self._call() - self.assertEqual(1, self.reg.filter.call_count) - - def test_no_default(self): - self._call() - self.assertEqual(1, self.reg.visible().ifaces.call_count) - - def test_no_candidate(self): - self.assertTrue(self._call() is None) - - def test_single(self): - plugin_ep = mock.MagicMock() - plugin_ep.init.return_value = "foo" - plugin_ep.misconfigured = False - - self.reg.visible().ifaces().verify().available.return_value = { - "bar": plugin_ep} - self.assertEqual("foo", self._call()) - - def test_single_misconfigured(self): - plugin_ep = mock.MagicMock() - plugin_ep.init.return_value = "foo" - plugin_ep.misconfigured = True - - self.reg.visible().ifaces().verify().available.return_value = { - "bar": plugin_ep} - self.assertTrue(self._call() is None) - - def test_multiple(self): - plugin_ep = mock.MagicMock() - plugin_ep.init.return_value = "foo" - self.reg.visible().ifaces().verify().available.return_value = { - "bar": plugin_ep, - "baz": plugin_ep, - } - with mock.patch("certbot.plugins.selection.choose_plugin") as mock_choose: - mock_choose.return_value = plugin_ep - self.assertEqual("foo", self._call()) - mock_choose.assert_called_once_with( - [plugin_ep, plugin_ep], self.question) - - def test_choose_plugin_none(self): - self.reg.visible().ifaces().verify().available.return_value = { - "bar": None, - "baz": None, - } - - with mock.patch("certbot.plugins.selection.choose_plugin") as mock_choose: - mock_choose.return_value = None - self.assertTrue(self._call() is None) - - -class ChoosePluginTest(unittest.TestCase): - """Tests for certbot.plugins.selection.choose_plugin.""" - - def setUp(self): - zope.component.provideUtility(display_util.FileDisplay(sys.stdout, - False)) - self.mock_apache = mock.Mock( - description_with_name="a", misconfigured=True) - self.mock_apache.name = "apache" - self.mock_stand = mock.Mock( - description_with_name="s", misconfigured=False) - self.mock_stand.init().more_info.return_value = "standalone" - self.plugins = [ - self.mock_apache, - self.mock_stand, - ] - - def _call(self): - from certbot.plugins.selection import choose_plugin - return choose_plugin(self.plugins, "Question?") - - @test_util.patch_get_utility("certbot.plugins.selection.z_util") - def test_selection(self, mock_util): - mock_util().menu.side_effect = [(display_util.OK, 0), - (display_util.OK, 1)] - self.assertEqual(self.mock_stand, self._call()) - self.assertEqual(mock_util().notification.call_count, 1) - - @test_util.patch_get_utility("certbot.plugins.selection.z_util") - def test_more_info(self, mock_util): - mock_util().menu.side_effect = [ - (display_util.OK, 1), - ] - - self.assertEqual(self.mock_stand, self._call()) - - @test_util.patch_get_utility("certbot.plugins.selection.z_util") - def test_no_choice(self, mock_util): - mock_util().menu.return_value = (display_util.CANCEL, 0) - self.assertTrue(self._call() is None) - - @test_util.patch_get_utility("certbot.plugins.selection.z_util") - def test_new_interaction_avoidance(self, mock_util): - mock_nginx = mock.Mock( - description_with_name="n", misconfigured=False) - mock_nginx.init().more_info.return_value = "nginx plugin" - mock_nginx.name = "nginx" - self.plugins[1] = mock_nginx - mock_util().menu.return_value = (display_util.CANCEL, 0) - - unset_cb_auto = os.environ.get("CERTBOT_AUTO") is None - if unset_cb_auto: - os.environ["CERTBOT_AUTO"] = "foo" - try: - self._call() - finally: - if unset_cb_auto: - del os.environ["CERTBOT_AUTO"] - - self.assertTrue("default" in mock_util().menu.call_args[1]) - -class GetUnpreparedInstallerTest(test_util.ConfigTestCase): - """Tests for certbot.plugins.selection.get_unprepared_installer.""" - - def setUp(self): - super(GetUnpreparedInstallerTest, self).setUp() - self.mock_apache_fail_ep = mock.Mock( - description_with_name="afail") - self.mock_apache_fail_ep.name = "afail" - self.mock_apache_ep = mock.Mock( - description_with_name="apache") - self.mock_apache_ep.name = "apache" - self.mock_apache_plugin = mock.MagicMock() - self.mock_apache_ep.init.return_value = self.mock_apache_plugin - self.plugins = PluginsRegistry({ - "afail": self.mock_apache_fail_ep, - "apache": self.mock_apache_ep, - }) - - def _call(self): - from certbot.plugins.selection import get_unprepared_installer - return get_unprepared_installer(self.config, self.plugins) - - def test_no_installer_defined(self): - self.config.configurator = None - self.assertEqual(self._call(), None) - - def test_no_available_installers(self): - self.config.configurator = "apache" - self.plugins = PluginsRegistry({}) - self.assertRaises(errors.PluginSelectionError, self._call) - - def test_get_plugin(self): - self.config.configurator = "apache" - installer = self._call() - self.assertTrue(installer is self.mock_apache_plugin) - - def test_multiple_installers_returned(self): - self.config.configurator = "apache" - # Two plugins with the same name - self.mock_apache_fail_ep.name = "apache" - self.assertRaises(errors.PluginSelectionError, self._call) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/plugins/standalone.py b/certbot/plugins/standalone.py deleted file mode 100644 index 16f872a3f43..00000000000 --- a/certbot/plugins/standalone.py +++ /dev/null @@ -1,299 +0,0 @@ -"""Standalone Authenticator.""" -import argparse -import collections -import logging -import socket -# https://github.com/python/typeshed/blob/master/stdlib/2and3/socket.pyi -from socket import errno as socket_errors # type: ignore - -import OpenSSL -import six -import zope.interface - -from acme import challenges -from acme import standalone as acme_standalone -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import DefaultDict, Dict, Set, Tuple, List, Type, TYPE_CHECKING - -from certbot import achallenges # pylint: disable=unused-import -from certbot import errors -from certbot import interfaces - -from certbot.plugins import common - -logger = logging.getLogger(__name__) - -if TYPE_CHECKING: - ServedType = DefaultDict[ - acme_standalone.BaseDualNetworkedServers, - Set[achallenges.KeyAuthorizationAnnotatedChallenge] - ] - -class ServerManager(object): - """Standalone servers manager. - - Manager for `ACMEServer` and `ACMETLSServer` instances. - - `certs` and `http_01_resources` correspond to - `acme.crypto_util.SSLSocket.certs` and - `acme.crypto_util.SSLSocket.http_01_resources` respectively. All - created servers share the same certificates and resources, so if - you're running both TLS and non-TLS instances, HTTP01 handlers - will serve the same URLs! - - """ - def __init__(self, certs, http_01_resources): - self._instances = {} # type: Dict[int, acme_standalone.BaseDualNetworkedServers] - self.certs = certs - self.http_01_resources = http_01_resources - - def run(self, port, challenge_type, listenaddr=""): - """Run ACME server on specified ``port``. - - This method is idempotent, i.e. all calls with the same pair of - ``(port, challenge_type)`` will reuse the same server. - - :param int port: Port to run the server on. - :param challenge_type: Subclass of `acme.challenges.Challenge`, - either `acme.challenge.HTTP01` or `acme.challenges.TLSSNI01`. - :param str listenaddr: (optional) The address to listen on. Defaults to all addrs. - - :returns: DualNetworkedServers instance. - :rtype: ACMEServerMixin - - """ - assert challenge_type in (challenges.TLSSNI01, challenges.HTTP01) - if port in self._instances: - return self._instances[port] - - address = (listenaddr, port) - try: - if challenge_type is challenges.TLSSNI01: - servers = acme_standalone.TLSSNI01DualNetworkedServers( - address, self.certs) # type: acme_standalone.BaseDualNetworkedServers - else: # challenges.HTTP01 - servers = acme_standalone.HTTP01DualNetworkedServers( - address, self.http_01_resources) - except socket.error as error: - raise errors.StandaloneBindError(error, port) - - servers.serve_forever() - - # if port == 0, then random free port on OS is taken - # pylint: disable=no-member - # both servers, if they exist, have the same port - real_port = servers.getsocknames()[0][1] - self._instances[real_port] = servers - return servers - - def stop(self, port): - """Stop ACME server running on the specified ``port``. - - :param int port: - - """ - instance = self._instances[port] - for sockname in instance.getsocknames(): - logger.debug("Stopping server at %s:%d...", - *sockname[:2]) - # Not calling server_close causes problems when renewing multiple - # certs with `certbot renew` using TLSSNI01 and PyOpenSSL 0.13 - instance.shutdown_and_server_close() - del self._instances[port] - - def running(self): - """Return all running instances. - - Once the server is stopped using `stop`, it will not be - returned. - - :returns: Mapping from ``port`` to ``servers``. - :rtype: tuple - - """ - return self._instances.copy() - - -SUPPORTED_CHALLENGES = [challenges.HTTP01, challenges.TLSSNI01] \ -# type: List[Type[challenges.KeyAuthorizationChallenge]] - - -class SupportedChallengesAction(argparse.Action): - """Action class for parsing standalone_supported_challenges.""" - - def __call__(self, parser, namespace, values, option_string=None): - logger.warning( - "The standalone specific supported challenges flag is " - "deprecated. Please use the --preferred-challenges flag " - "instead.") - converted_values = self._convert_and_validate(values) - namespace.standalone_supported_challenges = converted_values - - def _convert_and_validate(self, data): - """Validate the value of supported challenges provided by the user. - - References to "dvsni" are automatically converted to "tls-sni-01". - - :param str data: comma delimited list of challenge types - - :returns: validated and converted list of challenge types - :rtype: str - - """ - challs = data.split(",") - - # tls-sni-01 was dvsni during private beta - if "dvsni" in challs: - logger.info( - "Updating legacy standalone_supported_challenges value") - challs = [challenges.TLSSNI01.typ if chall == "dvsni" else chall - for chall in challs] - data = ",".join(challs) - - unrecognized = [name for name in challs - if name not in challenges.Challenge.TYPES] - - # argparse.ArgumentErrors raised out of argparse.Action objects - # are caught by argparse which prints usage information and the - # error that occurred before calling sys.exit. - if unrecognized: - raise argparse.ArgumentError( - self, - "Unrecognized challenges: {0}".format(", ".join(unrecognized))) - - choices = set(chall.typ for chall in SUPPORTED_CHALLENGES) - if not set(challs).issubset(choices): - raise argparse.ArgumentError( - self, - "Plugin does not support the following (valid) " - "challenges: {0}".format(", ".join(set(challs) - choices))) - - return data - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(common.Plugin): - """Standalone Authenticator. - - This authenticator creates its own ephemeral TCP listener on the - necessary port in order to respond to incoming tls-sni-01 and http-01 - challenges from the certificate authority. Therefore, it does not - rely on any existing server program. - """ - - description = "Spin up a temporary webserver" - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - - # one self-signed key for all tls-sni-01 certificates - self.key = OpenSSL.crypto.PKey() - self.key.generate_key(OpenSSL.crypto.TYPE_RSA, 2048) - - self.served = collections.defaultdict(set) # type: ServedType - - # Stuff below is shared across threads (i.e. servers read - # values, main thread writes). Due to the nature of CPython's - # GIL, the operations are safe, c.f. - # https://docs.python.org/2/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe - self.certs = {} # type: Dict[bytes, Tuple[OpenSSL.crypto.PKey, OpenSSL.crypto.X509]] - self.http_01_resources = set() \ - # type: Set[acme_standalone.HTTP01RequestHandler.HTTP01Resource] - - self.servers = ServerManager(self.certs, self.http_01_resources) - - @classmethod - def add_parser_arguments(cls, add): - add("supported-challenges", - help=argparse.SUPPRESS, - action=SupportedChallengesAction, - default=",".join(chall.typ for chall in SUPPORTED_CHALLENGES)) - - @property - def supported_challenges(self): - """Challenges supported by this plugin.""" - return [challenges.Challenge.TYPES[name] for name in - self.conf("supported-challenges").split(",")] - - def more_info(self): # pylint: disable=missing-docstring - return("This authenticator creates its own ephemeral TCP listener " - "on the necessary port in order to respond to incoming " - "tls-sni-01 and http-01 challenges from the certificate " - "authority. Therefore, it does not rely on any existing " - "server program.") - - def prepare(self): # pylint: disable=missing-docstring - pass - - def get_chall_pref(self, domain): - # pylint: disable=unused-argument,missing-docstring - return self.supported_challenges - - def perform(self, achalls): # pylint: disable=missing-docstring - return [self._try_perform_single(achall) for achall in achalls] - - def _try_perform_single(self, achall): - while True: - try: - return self._perform_single(achall) - except errors.StandaloneBindError as error: - _handle_perform_error(error) - - def _perform_single(self, achall): - if isinstance(achall.chall, challenges.HTTP01): - servers, response = self._perform_http_01(achall) - else: # tls-sni-01 - servers, response = self._perform_tls_sni_01(achall) - self.served[servers].add(achall) - return response - - def _perform_http_01(self, achall): - port = self.config.http01_port - addr = self.config.http01_address - servers = self.servers.run(port, challenges.HTTP01, listenaddr=addr) - response, validation = achall.response_and_validation() - resource = acme_standalone.HTTP01RequestHandler.HTTP01Resource( - chall=achall.chall, response=response, validation=validation) - self.http_01_resources.add(resource) - return servers, response - - def _perform_tls_sni_01(self, achall): - port = self.config.tls_sni_01_port - addr = self.config.tls_sni_01_address - servers = self.servers.run(port, challenges.TLSSNI01, listenaddr=addr) - response, (cert, _) = achall.response_and_validation(cert_key=self.key) - self.certs[response.z_domain] = (self.key, cert) - return servers, response - - def cleanup(self, achalls): # pylint: disable=missing-docstring - # reduce self.served and close servers if no challenges are served - for unused_servers, server_achalls in self.served.items(): - for achall in achalls: - if achall in server_achalls: - server_achalls.remove(achall) - for port, servers in six.iteritems(self.servers.running()): - if not self.served[servers]: - self.servers.stop(port) - - -def _handle_perform_error(error): - if error.socket_error.errno == socket_errors.EACCES: - raise errors.PluginError( - "Could not bind TCP port {0} because you don't have " - "the appropriate permissions (for example, you " - "aren't running this program as " - "root).".format(error.port)) - elif error.socket_error.errno == socket_errors.EADDRINUSE: - display = zope.component.getUtility(interfaces.IDisplay) - msg = ( - "Could not bind TCP port {0} because it is already in " - "use by another process on this system (such as a web " - "server). Please stop the program in question and " - "then try again.".format(error.port)) - should_retry = display.yesno(msg, "Retry", - "Cancel", default=False) - if not should_retry: - raise errors.PluginError(msg) - else: - raise diff --git a/certbot/plugins/standalone_test.py b/certbot/plugins/standalone_test.py deleted file mode 100644 index 9b741fc6fac..00000000000 --- a/certbot/plugins/standalone_test.py +++ /dev/null @@ -1,247 +0,0 @@ -"""Tests for certbot.plugins.standalone.""" -import argparse -import socket -import unittest -# https://github.com/python/typeshed/blob/master/stdlib/2and3/socket.pyi -from socket import errno as socket_errors # type: ignore - -import josepy as jose -import mock -import six - -import OpenSSL.crypto # pylint: disable=unused-import - -from acme import challenges -from acme import standalone as acme_standalone # pylint: disable=unused-import -from acme.magic_typing import Dict, Tuple, Set # pylint: disable=unused-import, no-name-in-module - -from certbot import achallenges -from certbot import errors - -from certbot.tests import acme_util -from certbot.tests import util as test_util - - -class ServerManagerTest(unittest.TestCase): - """Tests for certbot.plugins.standalone.ServerManager.""" - - def setUp(self): - from certbot.plugins.standalone import ServerManager - self.certs = {} # type: Dict[bytes, Tuple[OpenSSL.crypto.PKey, OpenSSL.crypto.X509]] - self.http_01_resources = {} \ - # type: Set[acme_standalone.HTTP01RequestHandler.HTTP01Resource] - self.mgr = ServerManager(self.certs, self.http_01_resources) - - def test_init(self): - self.assertTrue(self.mgr.certs is self.certs) - self.assertTrue( - self.mgr.http_01_resources is self.http_01_resources) - - def _test_run_stop(self, challenge_type): - server = self.mgr.run(port=0, challenge_type=challenge_type) - port = server.getsocknames()[0][1] # pylint: disable=no-member - self.assertEqual(self.mgr.running(), {port: server}) - self.mgr.stop(port=port) - self.assertEqual(self.mgr.running(), {}) - - def test_run_stop_tls_sni_01(self): - self._test_run_stop(challenges.TLSSNI01) - - def test_run_stop_http_01(self): - self._test_run_stop(challenges.HTTP01) - - def test_run_idempotent(self): - server = self.mgr.run(port=0, challenge_type=challenges.HTTP01) - port = server.getsocknames()[0][1] # pylint: disable=no-member - server2 = self.mgr.run(port=port, challenge_type=challenges.HTTP01) - self.assertEqual(self.mgr.running(), {port: server}) - self.assertTrue(server is server2) - self.mgr.stop(port) - self.assertEqual(self.mgr.running(), {}) - - def test_run_bind_error(self): - some_server = socket.socket(socket.AF_INET6) - some_server.bind(("", 0)) - port = some_server.getsockname()[1] - maybe_another_server = socket.socket() - try: - maybe_another_server.bind(("", port)) - except socket.error: - pass - self.assertRaises( - errors.StandaloneBindError, self.mgr.run, port, - challenge_type=challenges.HTTP01) - self.assertEqual(self.mgr.running(), {}) - some_server.close() - maybe_another_server.close() - - -class SupportedChallengesActionTest(unittest.TestCase): - """Tests for plugins.standalone.SupportedChallengesAction.""" - - def _call(self, value): - with mock.patch("certbot.plugins.standalone.logger") as mock_logger: - # stderr is mocked to prevent potential argparse error - # output from cluttering test output - with mock.patch("sys.stderr"): - config = self.parser.parse_args([self.flag, value]) - - self.assertTrue(mock_logger.warning.called) - return getattr(config, self.dest) - - def setUp(self): - self.flag = "--standalone-supported-challenges" - self.dest = self.flag[2:].replace("-", "_") - self.parser = argparse.ArgumentParser() - - from certbot.plugins.standalone import SupportedChallengesAction - self.parser.add_argument(self.flag, action=SupportedChallengesAction) - - def test_correct(self): - self.assertEqual("tls-sni-01", self._call("tls-sni-01")) - self.assertEqual("http-01", self._call("http-01")) - self.assertEqual("tls-sni-01,http-01", self._call("tls-sni-01,http-01")) - self.assertEqual("http-01,tls-sni-01", self._call("http-01,tls-sni-01")) - - def test_unrecognized(self): - assert "foo" not in challenges.Challenge.TYPES - self.assertRaises(SystemExit, self._call, "foo") - - def test_not_subset(self): - self.assertRaises(SystemExit, self._call, "dns") - - def test_dvsni(self): - self.assertEqual("tls-sni-01", self._call("dvsni")) - self.assertEqual("http-01,tls-sni-01", self._call("http-01,dvsni")) - self.assertEqual("tls-sni-01,http-01", self._call("dvsni,http-01")) - - -def get_open_port(): - """Gets an open port number from the OS.""" - open_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) - open_socket.bind(("", 0)) - port = open_socket.getsockname()[1] - open_socket.close() - return port - - -class AuthenticatorTest(unittest.TestCase): - """Tests for certbot.plugins.standalone.Authenticator.""" - - def setUp(self): - from certbot.plugins.standalone import Authenticator - - self.config = mock.MagicMock( - tls_sni_01_port=get_open_port(), http01_port=get_open_port(), - standalone_supported_challenges="tls-sni-01,http-01") - self.auth = Authenticator(self.config, name="standalone") - self.auth.servers = mock.MagicMock() - - def test_supported_challenges(self): - self.assertEqual(self.auth.supported_challenges, - [challenges.TLSSNI01, challenges.HTTP01]) - - def test_supported_challenges_configured(self): - self.config.standalone_supported_challenges = "tls-sni-01" - self.assertEqual(self.auth.supported_challenges, - [challenges.TLSSNI01]) - - def test_more_info(self): - self.assertTrue(isinstance(self.auth.more_info(), six.string_types)) - - def test_get_chall_pref(self): - self.assertEqual(self.auth.get_chall_pref(domain=None), - [challenges.TLSSNI01, challenges.HTTP01]) - - def test_get_chall_pref_configured(self): - self.config.standalone_supported_challenges = "tls-sni-01" - self.assertEqual(self.auth.get_chall_pref(domain=None), - [challenges.TLSSNI01]) - - def test_perform(self): - achalls = self._get_achalls() - response = self.auth.perform(achalls) - - expected = [achall.response(achall.account_key) for achall in achalls] - self.assertEqual(response, expected) - - @test_util.patch_get_utility() - def test_perform_eaddrinuse_retry(self, mock_get_utility): - mock_utility = mock_get_utility() - errno = socket_errors.EADDRINUSE - error = errors.StandaloneBindError(mock.MagicMock(errno=errno), -1) - self.auth.servers.run.side_effect = [error] + 2 * [mock.MagicMock()] - mock_yesno = mock_utility.yesno - mock_yesno.return_value = True - - self.test_perform() - self._assert_correct_yesno_call(mock_yesno) - - @test_util.patch_get_utility() - def test_perform_eaddrinuse_no_retry(self, mock_get_utility): - mock_utility = mock_get_utility() - mock_yesno = mock_utility.yesno - mock_yesno.return_value = False - - errno = socket_errors.EADDRINUSE - self.assertRaises(errors.PluginError, self._fail_perform, errno) - self._assert_correct_yesno_call(mock_yesno) - - def _assert_correct_yesno_call(self, mock_yesno): - yesno_args, yesno_kwargs = mock_yesno.call_args - self.assertTrue("in use" in yesno_args[0]) - self.assertFalse(yesno_kwargs.get("default", True)) - - def test_perform_eacces(self): - errno = socket_errors.EACCES - self.assertRaises(errors.PluginError, self._fail_perform, errno) - - def test_perform_unexpected_socket_error(self): - errno = socket_errors.ENOTCONN - self.assertRaises( - errors.StandaloneBindError, self._fail_perform, errno) - - def _fail_perform(self, errno): - error = errors.StandaloneBindError(mock.MagicMock(errno=errno), -1) - self.auth.servers.run.side_effect = error - self.auth.perform(self._get_achalls()) - - @classmethod - def _get_achalls(cls): - domain = b'localhost' - key = jose.JWK.load(test_util.load_vector('rsa512_key.pem')) - http_01 = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.HTTP01_P, domain=domain, account_key=key) - tls_sni_01 = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.TLSSNI01_P, domain=domain, account_key=key) - - return [http_01, tls_sni_01] - - def test_cleanup(self): - self.auth.servers.running.return_value = { - 1: "server1", - 2: "server2", - } - self.auth.served["server1"].add("chall1") - self.auth.served["server2"].update(["chall2", "chall3"]) - - self.auth.cleanup(["chall1"]) - self.assertEqual(self.auth.served, { - "server1": set(), "server2": set(["chall2", "chall3"])}) - self.auth.servers.stop.assert_called_once_with(1) - - self.auth.servers.running.return_value = { - 2: "server2", - } - self.auth.cleanup(["chall2"]) - self.assertEqual(self.auth.served, { - "server1": set(), "server2": set(["chall3"])}) - self.assertEqual(1, self.auth.servers.stop.call_count) - - self.auth.cleanup(["chall3"]) - self.assertEqual(self.auth.served, { - "server1": set(), "server2": set([])}) - self.auth.servers.stop.assert_called_with(2) - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/plugins/storage.py b/certbot/plugins/storage.py deleted file mode 100644 index ae3ca188945..00000000000 --- a/certbot/plugins/storage.py +++ /dev/null @@ -1,119 +0,0 @@ -"""Plugin storage class.""" -import json -import logging -import os - -from acme.magic_typing import Any, Dict # pylint: disable=unused-import, no-name-in-module -from certbot import errors - -logger = logging.getLogger(__name__) - -class PluginStorage(object): - """Class implementing storage functionality for plugins""" - - def __init__(self, config, classkey): - """Initializes PluginStorage object storing required configuration - options. - - :param .configuration.NamespaceConfig config: Configuration object - :param str classkey: class name to use as root key in storage file - - """ - - self._config = config - self._classkey = classkey - self._initialized = False - self._data = None - self._storagepath = None - - def _initialize_storage(self): - """Initializes PluginStorage data and reads current state from the disk - if the storage json exists.""" - - self._storagepath = os.path.join(self._config.config_dir, ".pluginstorage.json") - self._load() - self._initialized = True - - def _load(self): - """Reads PluginStorage content from the disk to a dict structure - - :raises .errors.PluginStorageError: when unable to open or read the file - """ - data = dict() # type: Dict[str, Any] - filedata = "" - try: - with open(self._storagepath, 'r') as fh: - filedata = fh.read() - except IOError as e: - errmsg = "Could not read PluginStorage data file: {0} : {1}".format( - self._storagepath, str(e)) - if os.path.isfile(self._storagepath): - # Only error out if file exists, but cannot be read - logger.error(errmsg) - raise errors.PluginStorageError(errmsg) - try: - data = json.loads(filedata) - except ValueError: - if not filedata: - logger.debug("Plugin storage file %s was empty, no values loaded", - self._storagepath) - else: - errmsg = "PluginStorage file {0} is corrupted.".format( - self._storagepath) - logger.error(errmsg) - raise errors.PluginStorageError(errmsg) - self._data = data - - def save(self): - """Saves PluginStorage content to disk - - :raises .errors.PluginStorageError: when unable to serialize the data - or write it to the filesystem - """ - if not self._initialized: - errmsg = "Unable to save, no values have been added to PluginStorage." - logger.error(errmsg) - raise errors.PluginStorageError(errmsg) - - try: - serialized = json.dumps(self._data) - except TypeError as e: - errmsg = "Could not serialize PluginStorage data: {0}".format( - str(e)) - logger.error(errmsg) - raise errors.PluginStorageError(errmsg) - try: - with os.fdopen(os.open(self._storagepath, - os.O_WRONLY | os.O_CREAT | os.O_TRUNC, - 0o600), 'w') as fh: - fh.write(serialized) - except IOError as e: - errmsg = "Could not write PluginStorage data to file {0} : {1}".format( - self._storagepath, str(e)) - logger.error(errmsg) - raise errors.PluginStorageError(errmsg) - - def put(self, key, value): - """Put configuration value to PluginStorage - - :param str key: Key to store the value to - :param value: Data to store - """ - if not self._initialized: - self._initialize_storage() - - if not self._classkey in self._data.keys(): - self._data[self._classkey] = dict() - self._data[self._classkey][key] = value - - def fetch(self, key): - """Get configuration value from PluginStorage - - :param str key: Key to get value from the storage - - :raises KeyError: If the key doesn't exist in the storage - """ - if not self._initialized: - self._initialize_storage() - - return self._data[self._classkey][key] diff --git a/certbot/plugins/storage_test.py b/certbot/plugins/storage_test.py deleted file mode 100644 index 8d96f400c1a..00000000000 --- a/certbot/plugins/storage_test.py +++ /dev/null @@ -1,117 +0,0 @@ -"""Tests for certbot.plugins.storage.PluginStorage""" -import json -import mock -import os -import unittest - -from certbot import errors - -from certbot.plugins import common -from certbot.tests import util as test_util - -class PluginStorageTest(test_util.ConfigTestCase): - """Test for certbot.plugins.storage.PluginStorage""" - - def setUp(self): - super(PluginStorageTest, self).setUp() - self.plugin_cls = common.Installer - os.mkdir(self.config.config_dir) - with mock.patch("certbot.reverter.util"): - self.plugin = self.plugin_cls(config=self.config, name="mockplugin") - - def test_load_errors_cant_read(self): - with open(os.path.join(self.config.config_dir, - ".pluginstorage.json"), "w") as fh: - fh.write("dummy") - # When unable to read file that exists - mock_open = mock.mock_open() - mock_open.side_effect = IOError - self.plugin.storage.storagepath = os.path.join(self.config.config_dir, - ".pluginstorage.json") - with mock.patch("six.moves.builtins.open", mock_open): - with mock.patch('os.path.isfile', return_value=True): - with mock.patch("certbot.reverter.util"): - self.assertRaises(errors.PluginStorageError, - self.plugin.storage._load) # pylint: disable=protected-access - - def test_load_errors_empty(self): - with open(os.path.join(self.config.config_dir, ".pluginstorage.json"), "w") as fh: - fh.write('') - with mock.patch("certbot.plugins.storage.logger.debug") as mock_log: - # Should not error out but write a debug log line instead - with mock.patch("certbot.reverter.util"): - nocontent = self.plugin_cls(self.config, "mockplugin") - self.assertRaises(KeyError, - nocontent.storage.fetch, "value") - self.assertTrue(mock_log.called) - self.assertTrue("no values loaded" in mock_log.call_args[0][0]) - - def test_load_errors_corrupted(self): - with open(os.path.join(self.config.config_dir, - ".pluginstorage.json"), "w") as fh: - fh.write('invalid json') - with mock.patch("certbot.plugins.storage.logger.error") as mock_log: - with mock.patch("certbot.reverter.util"): - corrupted = self.plugin_cls(self.config, "mockplugin") - self.assertRaises(errors.PluginError, - corrupted.storage.fetch, - "value") - self.assertTrue("is corrupted" in mock_log.call_args[0][0]) - - def test_save_errors_cant_serialize(self): - with mock.patch("certbot.plugins.storage.logger.error") as mock_log: - # Set data as something that can't be serialized - self.plugin.storage._initialized = True # pylint: disable=protected-access - self.plugin.storage.storagepath = "/tmp/whatever" - self.plugin.storage._data = self.plugin_cls # pylint: disable=protected-access - self.assertRaises(errors.PluginStorageError, - self.plugin.storage.save) - self.assertTrue("Could not serialize" in mock_log.call_args[0][0]) - - def test_save_errors_unable_to_write_file(self): - mock_open = mock.mock_open() - mock_open.side_effect = IOError - with mock.patch("os.open", mock_open): - with mock.patch("certbot.plugins.storage.logger.error") as mock_log: - self.plugin.storage._data = {"valid": "data"} # pylint: disable=protected-access - self.plugin.storage._initialized = True # pylint: disable=protected-access - self.assertRaises(errors.PluginStorageError, - self.plugin.storage.save) - self.assertTrue("Could not write" in mock_log.call_args[0][0]) - - def test_save_uninitialized(self): - with mock.patch("certbot.reverter.util"): - self.assertRaises(errors.PluginStorageError, - self.plugin_cls(self.config, "x").storage.save) - - def test_namespace_isolation(self): - with mock.patch("certbot.reverter.util"): - plugin1 = self.plugin_cls(self.config, "first") - plugin2 = self.plugin_cls(self.config, "second") - plugin1.storage.put("first_key", "first_value") - self.assertRaises(KeyError, - plugin2.storage.fetch, "first_key") - self.assertRaises(KeyError, - plugin2.storage.fetch, "first") - self.assertEqual(plugin1.storage.fetch("first_key"), "first_value") - - - def test_saved_state(self): - self.plugin.storage.put("testkey", "testvalue") - # Write to disk - self.plugin.storage.save() - with mock.patch("certbot.reverter.util"): - another = self.plugin_cls(self.config, "mockplugin") - self.assertEqual(another.storage.fetch("testkey"), "testvalue") - - with open(os.path.join(self.config.config_dir, - ".pluginstorage.json"), 'r') as fh: - psdata = fh.read() - psjson = json.loads(psdata) - self.assertTrue("mockplugin" in psjson.keys()) - self.assertEqual(len(psjson), 1) - self.assertEqual(psjson["mockplugin"]["testkey"], "testvalue") - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/plugins/util.py b/certbot/plugins/util.py deleted file mode 100644 index 5c682c3ffce..00000000000 --- a/certbot/plugins/util.py +++ /dev/null @@ -1,57 +0,0 @@ -"""Plugin utilities.""" -import logging -import os - -from certbot import util - -logger = logging.getLogger(__name__) - -def get_prefixes(path): - """Retrieves all possible path prefixes of a path, in descending order - of length. For instance, - (linux) /a/b/c returns ['/a/b/c', '/a/b', '/a', '/'] - (windows) C:\\a\\b\\c returns ['C:\\a\\b\\c', 'C:\\a\\b', 'C:\\a', 'C:'] - :param str path: the path to break into prefixes - - :returns: all possible path prefixes of given path in descending order - :rtype: `list` of `str` - """ - prefix = os.path.normpath(path) - prefixes = [] - while len(prefix) > 0: - prefixes.append(prefix) - prefix, _ = os.path.split(prefix) - # break once we hit the root path - if prefix == prefixes[-1]: - break - return prefixes - -def path_surgery(cmd): - """Attempt to perform PATH surgery to find cmd - - Mitigates https://github.com/certbot/certbot/issues/1833 - - :param str cmd: the command that is being searched for in the PATH - - :returns: True if the operation succeeded, False otherwise - """ - dirs = ("/usr/sbin", "/usr/local/bin", "/usr/local/sbin") - path = os.environ["PATH"] - added = [] - for d in dirs: - if d not in path: - path += os.pathsep + d - added.append(d) - - if any(added): - logger.debug("Can't find %s, attempting PATH mitigation by adding %s", - cmd, os.pathsep.join(added)) - os.environ["PATH"] = path - - if util.exe_exists(cmd): - return True - else: - expanded = " expanded" if any(added) else "" - logger.debug("Failed to find executable %s in%s PATH: %s", cmd, - expanded, path) - return False diff --git a/certbot/plugins/util_test.py b/certbot/plugins/util_test.py deleted file mode 100644 index 8ecd380b8e7..00000000000 --- a/certbot/plugins/util_test.py +++ /dev/null @@ -1,40 +0,0 @@ -"""Tests for certbot.plugins.util.""" -import os -import unittest - -import mock - -class GetPrefixTest(unittest.TestCase): - """Tests for certbot.plugins.get_prefixes.""" - def test_get_prefix(self): - from certbot.plugins.util import get_prefixes - self.assertEqual( - get_prefixes('/a/b/c'), - [os.path.normpath(path) for path in ['/a/b/c', '/a/b', '/a', '/']]) - self.assertEqual(get_prefixes('/'), [os.path.normpath('/')]) - self.assertEqual(get_prefixes('a'), ['a']) - -class PathSurgeryTest(unittest.TestCase): - """Tests for certbot.plugins.path_surgery.""" - - @mock.patch("certbot.plugins.util.logger.debug") - def test_path_surgery(self, mock_debug): - from certbot.plugins.util import path_surgery - all_path = {"PATH": "/usr/local/bin:/bin/:/usr/sbin/:/usr/local/sbin/"} - with mock.patch.dict('os.environ', all_path): - with mock.patch('certbot.util.exe_exists') as mock_exists: - mock_exists.return_value = True - self.assertEqual(path_surgery("eg"), True) - self.assertEqual(mock_debug.call_count, 0) - self.assertEqual(os.environ["PATH"], all_path["PATH"]) - no_path = {"PATH": "/tmp/"} - with mock.patch.dict('os.environ', no_path): - path_surgery("thingy") - self.assertEqual(mock_debug.call_count, 2) - self.assertTrue("Failed to find" in mock_debug.call_args[0][0]) - self.assertTrue("/usr/local/bin" in os.environ["PATH"]) - self.assertTrue("/tmp" in os.environ["PATH"]) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/plugins/webroot.py b/certbot/plugins/webroot.py deleted file mode 100644 index 529094705ef..00000000000 --- a/certbot/plugins/webroot.py +++ /dev/null @@ -1,287 +0,0 @@ -"""Webroot plugin.""" -import argparse -import collections -import errno -import json -import logging -import os - -import six -import zope.component -import zope.interface - -from acme import challenges # pylint: disable=unused-import -# pylint: disable=unused-import, no-name-in-module -from acme.magic_typing import Dict, Set, DefaultDict, List -# pylint: enable=unused-import, no-name-in-module - -from certbot import achallenges # pylint: disable=unused-import -from certbot import cli -from certbot import errors -from certbot import interfaces -from certbot.display import util as display_util -from certbot.display import ops -from certbot.plugins import common -from certbot.plugins import util - - -logger = logging.getLogger(__name__) - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(common.Plugin): - """Webroot Authenticator.""" - - description = "Place files in webroot directory" - - MORE_INFO = """\ -Authenticator plugin that performs http-01 challenge by saving -necessary validation resources to appropriate paths on the file -system. It expects that there is some other HTTP server configured -to serve all files under specified web root ({0}).""" - - def more_info(self): # pylint: disable=missing-docstring,no-self-use - return self.MORE_INFO.format(self.conf("path")) - - @classmethod - def add_parser_arguments(cls, add): - add("path", "-w", default=[], action=_WebrootPathAction, - help="public_html / webroot path. This can be specified multiple " - "times to handle different domains; each domain will have " - "the webroot path that preceded it. For instance: `-w " - "/var/www/example -d example.com -d www.example.com -w " - "/var/www/thing -d thing.net -d m.thing.net` (default: Ask)") - add("map", default={}, action=_WebrootMapAction, - help="JSON dictionary mapping domains to webroot paths; this " - "implies -d for each entry. You may need to escape this from " - "your shell. E.g.: --webroot-map " - '\'{"eg1.is,m.eg1.is":"/www/eg1/", "eg2.is":"/www/eg2"}\' ' - "This option is merged with, but takes precedence over, -w / " - "-d entries. At present, if you put webroot-map in a config " - "file, it needs to be on a single line, like: webroot-map = " - '{"example.com":"/var/www"}.') - - def get_chall_pref(self, domain): # pragma: no cover - # pylint: disable=missing-docstring,no-self-use,unused-argument - return [challenges.HTTP01] - - def __init__(self, *args, **kwargs): - super(Authenticator, self).__init__(*args, **kwargs) - self.full_roots = {} # type: Dict[str, str] - self.performed = collections.defaultdict(set) \ - # type: DefaultDict[str, Set[achallenges.KeyAuthorizationAnnotatedChallenge]] - # stack of dirs successfully created by this authenticator - self._created_dirs = [] # type: List[str] - - def prepare(self): # pylint: disable=missing-docstring - pass - - def perform(self, achalls): # pylint: disable=missing-docstring - self._set_webroots(achalls) - - self._create_challenge_dirs() - - return [self._perform_single(achall) for achall in achalls] - - def _set_webroots(self, achalls): - if self.conf("path"): - webroot_path = self.conf("path")[-1] - logger.info("Using the webroot path %s for all unmatched domains.", - webroot_path) - for achall in achalls: - self.conf("map").setdefault(achall.domain, webroot_path) - else: - known_webroots = list(set(six.itervalues(self.conf("map")))) - for achall in achalls: - if achall.domain not in self.conf("map"): - new_webroot = self._prompt_for_webroot(achall.domain, - known_webroots) - # Put the most recently input - # webroot first for easy selection - try: - known_webroots.remove(new_webroot) - except ValueError: - pass - known_webroots.insert(0, new_webroot) - self.conf("map")[achall.domain] = new_webroot - - def _prompt_for_webroot(self, domain, known_webroots): - webroot = None - - while webroot is None: - if known_webroots: - # Only show the menu if we have options for it - webroot = self._prompt_with_webroot_list(domain, known_webroots) - if webroot is None: - webroot = self._prompt_for_new_webroot(domain) - else: - # Allow prompt to raise PluginError instead of looping forever - webroot = self._prompt_for_new_webroot(domain, True) - - return webroot - - def _prompt_with_webroot_list(self, domain, known_webroots): - display = zope.component.getUtility(interfaces.IDisplay) - path_flag = "--" + self.option_name("path") - - while True: - code, index = display.menu( - "Select the webroot for {0}:".format(domain), - ["Enter a new webroot"] + known_webroots, - cli_flag=path_flag, force_interactive=True) - if code == display_util.CANCEL: - raise errors.PluginError( - "Every requested domain must have a " - "webroot when using the webroot plugin.") - else: # code == display_util.OK - return None if index == 0 else known_webroots[index - 1] - - def _prompt_for_new_webroot(self, domain, allowraise=False): - code, webroot = ops.validated_directory( - _validate_webroot, - "Input the webroot for {0}:".format(domain), - force_interactive=True) - if code == display_util.CANCEL: - if not allowraise: - return None - else: - raise errors.PluginError( - "Every requested domain must have a " - "webroot when using the webroot plugin.") - else: # code == display_util.OK - return _validate_webroot(webroot) - - def _create_challenge_dirs(self): - path_map = self.conf("map") - if not path_map: - raise errors.PluginError( - "Missing parts of webroot configuration; please set either " - "--webroot-path and --domains, or --webroot-map. Run with " - " --help webroot for examples.") - for name, path in path_map.items(): - self.full_roots[name] = os.path.join(path, challenges.HTTP01.URI_ROOT_PATH) - logger.debug("Creating root challenges validation dir at %s", - self.full_roots[name]) - - # Change the permissions to be writable (GH #1389) - # Umask is used instead of chmod to ensure the client can also - # run as non-root (GH #1795) - old_umask = os.umask(0o022) - try: - stat_path = os.stat(path) - # We ignore the last prefix in the next iteration, - # as it does not correspond to a folder path ('/' or 'C:') - for prefix in sorted(util.get_prefixes(self.full_roots[name])[:-1], key=len): - try: - # This is coupled with the "umask" call above because - # os.mkdir's "mode" parameter may not always work: - # https://docs.python.org/3/library/os.html#os.mkdir - os.mkdir(prefix, 0o0755) - self._created_dirs.append(prefix) - # Set owner as parent directory if possible - try: - os.chown(prefix, stat_path.st_uid, stat_path.st_gid) - except (OSError, AttributeError) as exception: - logger.info("Unable to change owner and uid of webroot directory") - logger.debug("Error was: %s", exception) - except OSError as exception: - if exception.errno not in (errno.EEXIST, errno.EISDIR): - raise errors.PluginError( - "Couldn't create root for {0} http-01 " - "challenge responses: {1}".format(name, exception)) - finally: - os.umask(old_umask) - - def _get_validation_path(self, root_path, achall): - return os.path.join(root_path, achall.chall.encode("token")) - - def _perform_single(self, achall): - response, validation = achall.response_and_validation() - - root_path = self.full_roots[achall.domain] - validation_path = self._get_validation_path(root_path, achall) - logger.debug("Attempting to save validation to %s", validation_path) - - # Change permissions to be world-readable, owner-writable (GH #1795) - old_umask = os.umask(0o022) - - try: - with open(validation_path, "wb") as validation_file: - validation_file.write(validation.encode()) - finally: - os.umask(old_umask) - - self.performed[root_path].add(achall) - return response - - def cleanup(self, achalls): # pylint: disable=missing-docstring - for achall in achalls: - root_path = self.full_roots.get(achall.domain, None) - if root_path is not None: - validation_path = self._get_validation_path(root_path, achall) - logger.debug("Removing %s", validation_path) - os.remove(validation_path) - self.performed[root_path].remove(achall) - - not_removed = [] # type: List[str] - while len(self._created_dirs) > 0: - path = self._created_dirs.pop() - try: - os.rmdir(path) - except OSError as exc: - not_removed.insert(0, path) - logger.info("Challenge directory %s was not empty, didn't remove", path) - logger.debug("Error was: %s", exc) - self._created_dirs = not_removed - logger.debug("All challenges cleaned up") - - -class _WebrootMapAction(argparse.Action): - """Action class for parsing webroot_map.""" - - def __call__(self, parser, namespace, webroot_map, option_string=None): - for domains, webroot_path in six.iteritems(json.loads(webroot_map)): - webroot_path = _validate_webroot(webroot_path) - namespace.webroot_map.update( - (d, webroot_path) for d in cli.add_domains(namespace, domains)) - - -class _WebrootPathAction(argparse.Action): - """Action class for parsing webroot_path.""" - - def __init__(self, *args, **kwargs): - super(_WebrootPathAction, self).__init__(*args, **kwargs) - self._domain_before_webroot = False - - def __call__(self, parser, namespace, webroot_path, option_string=None): - if self._domain_before_webroot: - raise errors.PluginError( - "If you specify multiple webroot paths, " - "one of them must precede all domain flags") - - if namespace.webroot_path: - # Apply previous webroot to all matched - # domains before setting the new webroot path - prev_webroot = namespace.webroot_path[-1] - for domain in namespace.domains: - namespace.webroot_map.setdefault(domain, prev_webroot) - elif namespace.domains: - self._domain_before_webroot = True - - namespace.webroot_path.append(_validate_webroot(webroot_path)) - - -def _validate_webroot(webroot_path): - """Validates and returns the absolute path of webroot_path. - - :param str webroot_path: path to the webroot directory - - :returns: absolute path of webroot_path - :rtype: str - - """ - if not os.path.isdir(webroot_path): - raise errors.PluginError(webroot_path + " does not exist or is not a directory") - - return os.path.abspath(webroot_path) diff --git a/certbot/plugins/webroot_test.py b/certbot/plugins/webroot_test.py deleted file mode 100644 index 5303fe4da6f..00000000000 --- a/certbot/plugins/webroot_test.py +++ /dev/null @@ -1,308 +0,0 @@ -"""Tests for certbot.plugins.webroot.""" - -from __future__ import print_function - -import argparse -import errno -import json -import os -import shutil -import tempfile -import unittest - -import josepy as jose -import mock -import six - -from acme import challenges - -from certbot import achallenges -from certbot import compat -from certbot import errors -from certbot.display import util as display_util - -from certbot.tests import acme_util -from certbot.tests import util as test_util - - -KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) - - -class AuthenticatorTest(unittest.TestCase): - """Tests for certbot.plugins.webroot.Authenticator.""" - - achall = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.HTTP01_P, domain="thing.com", account_key=KEY) - - def setUp(self): - from certbot.plugins.webroot import Authenticator - self.path = tempfile.mkdtemp() - self.partial_root_challenge_path = os.path.join( - self.path, ".well-known") - self.root_challenge_path = os.path.join( - self.path, ".well-known", "acme-challenge") - self.validation_path = os.path.join( - self.root_challenge_path, - "ZXZhR3hmQURzNnBTUmIyTEF2OUlaZjE3RHQzanV4R0orUEN0OTJ3citvQQ") - self.config = mock.MagicMock(webroot_path=self.path, - webroot_map={"thing.com": self.path}) - self.auth = Authenticator(self.config, "webroot") - - def tearDown(self): - shutil.rmtree(self.path) - - def test_more_info(self): - more_info = self.auth.more_info() - self.assertTrue(isinstance(more_info, six.string_types)) - self.assertTrue(self.path in more_info) - - def test_add_parser_arguments(self): - add = mock.MagicMock() - self.auth.add_parser_arguments(add) - self.assertEqual(2, add.call_count) - - def test_prepare(self): - self.auth.prepare() # shouldn't raise any exceptions - - @test_util.patch_get_utility() - def test_webroot_from_list(self, mock_get_utility): - self.config.webroot_path = [] - self.config.webroot_map = {"otherthing.com": self.path} - mock_display = mock_get_utility() - mock_display.menu.return_value = (display_util.OK, 1,) - - self.auth.perform([self.achall]) - self.assertTrue(mock_display.menu.called) - for call in mock_display.menu.call_args_list: - self.assertTrue(self.achall.domain in call[0][0]) - self.assertTrue(all( - webroot in call[0][1] - for webroot in six.itervalues(self.config.webroot_map))) - self.assertEqual(self.config.webroot_map[self.achall.domain], - self.path) - - @test_util.patch_get_utility() - def test_webroot_from_list_help_and_cancel(self, mock_get_utility): - self.config.webroot_path = [] - self.config.webroot_map = {"otherthing.com": self.path} - - mock_display = mock_get_utility() - mock_display.menu.side_effect = ((display_util.CANCEL, -1),) - self.assertRaises(errors.PluginError, self.auth.perform, [self.achall]) - self.assertTrue(mock_display.menu.called) - for call in mock_display.menu.call_args_list: - self.assertTrue(self.achall.domain in call[0][0]) - self.assertTrue(all( - webroot in call[0][1] - for webroot in six.itervalues(self.config.webroot_map))) - - @test_util.patch_get_utility() - def test_new_webroot(self, mock_get_utility): - self.config.webroot_path = [] - self.config.webroot_map = {"something.com": self.path} - - mock_display = mock_get_utility() - mock_display.menu.return_value = (display_util.OK, 0,) - with mock.patch('certbot.display.ops.validated_directory') as m: - m.side_effect = ((display_util.CANCEL, -1), - (display_util.OK, self.path,)) - - self.auth.perform([self.achall]) - - self.assertEqual(self.config.webroot_map[self.achall.domain], self.path) - - @test_util.patch_get_utility() - def test_new_webroot_empty_map_cancel(self, mock_get_utility): - self.config.webroot_path = [] - self.config.webroot_map = {} - - mock_display = mock_get_utility() - mock_display.menu.return_value = (display_util.OK, 0,) - with mock.patch('certbot.display.ops.validated_directory') as m: - m.return_value = (display_util.CANCEL, -1) - self.assertRaises(errors.PluginError, - self.auth.perform, - [self.achall]) - - def test_perform_missing_root(self): - self.config.webroot_path = None - self.config.webroot_map = {} - self.assertRaises(errors.PluginError, self.auth.perform, []) - - def test_perform_reraises_other_errors(self): - self.auth.full_path = os.path.join(self.path, "null") - permission_canary = os.path.join(self.path, "rnd") - with open(permission_canary, "w") as f: - f.write("thingimy") - os.chmod(self.path, 0o000) - try: - open(permission_canary, "r") - print("Warning, running tests as root skips permissions tests...") - except IOError: - # ok, permissions work, test away... - self.assertRaises(errors.PluginError, self.auth.perform, []) - os.chmod(self.path, 0o700) - - @test_util.skip_on_windows('On Windows, there is no chown.') - @mock.patch("certbot.plugins.webroot.os.chown") - def test_failed_chown(self, mock_chown): - mock_chown.side_effect = OSError(errno.EACCES, "msg") - self.auth.perform([self.achall]) # exception caught and logged - - - @test_util.patch_get_utility() - def test_perform_new_webroot_not_in_map(self, mock_get_utility): - new_webroot = tempfile.mkdtemp() - self.config.webroot_path = [] - self.config.webroot_map = {"whatever.com": self.path} - mock_display = mock_get_utility() - mock_display.menu.side_effect = ((display_util.OK, 0), - (display_util.OK, new_webroot)) - achall = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.HTTP01_P, domain="something.com", account_key=KEY) - with mock.patch('certbot.display.ops.validated_directory') as m: - m.return_value = (display_util.OK, new_webroot,) - self.auth.perform([achall]) - self.assertEqual(self.config.webroot_map[achall.domain], new_webroot) - - def test_perform_permissions(self): - self.auth.prepare() - - # Remove exec bit from permission check, so that it - # matches the file - self.auth.perform([self.achall]) - self.assertTrue(compat.compare_file_modes(os.stat(self.validation_path).st_mode, 0o644)) - - # Check permissions of the directories - - for dirpath, dirnames, _ in os.walk(self.path): - for directory in dirnames: - full_path = os.path.join(dirpath, directory) - self.assertTrue(compat.compare_file_modes(os.stat(full_path).st_mode, 0o755)) - - parent_gid = os.stat(self.path).st_gid - parent_uid = os.stat(self.path).st_uid - - self.assertEqual(os.stat(self.validation_path).st_gid, parent_gid) - self.assertEqual(os.stat(self.validation_path).st_uid, parent_uid) - - def test_perform_cleanup(self): - self.auth.prepare() - responses = self.auth.perform([self.achall]) - self.assertEqual(1, len(responses)) - self.assertTrue(os.path.exists(self.validation_path)) - with open(self.validation_path) as validation_f: - validation = validation_f.read() - self.assertTrue( - challenges.KeyAuthorizationChallengeResponse( - key_authorization=validation).verify( - self.achall.chall, KEY.public_key())) - - self.auth.cleanup([self.achall]) - self.assertFalse(os.path.exists(self.validation_path)) - self.assertFalse(os.path.exists(self.root_challenge_path)) - self.assertFalse(os.path.exists(self.partial_root_challenge_path)) - - def test_perform_cleanup_existing_dirs(self): - os.mkdir(self.partial_root_challenge_path) - self.auth.prepare() - self.auth.perform([self.achall]) - self.auth.cleanup([self.achall]) - - # Ensure we don't "clean up" directories that previously existed - self.assertFalse(os.path.exists(self.validation_path)) - self.assertFalse(os.path.exists(self.root_challenge_path)) - - def test_perform_cleanup_multiple_challenges(self): - bingo_achall = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.chall_to_challb( - challenges.HTTP01(token=b"bingo"), "pending"), - domain="thing.com", account_key=KEY) - - bingo_validation_path = "YmluZ28" - os.mkdir(self.partial_root_challenge_path) - self.auth.prepare() - self.auth.perform([bingo_achall, self.achall]) - - self.auth.cleanup([self.achall]) - self.assertFalse(os.path.exists(bingo_validation_path)) - self.assertTrue(os.path.exists(self.root_challenge_path)) - self.auth.cleanup([bingo_achall]) - self.assertFalse(os.path.exists(self.validation_path)) - self.assertFalse(os.path.exists(self.root_challenge_path)) - - def test_cleanup_leftovers(self): - self.auth.prepare() - self.auth.perform([self.achall]) - - leftover_path = os.path.join(self.root_challenge_path, 'leftover') - os.mkdir(leftover_path) - - self.auth.cleanup([self.achall]) - self.assertFalse(os.path.exists(self.validation_path)) - self.assertTrue(os.path.exists(self.root_challenge_path)) - - os.rmdir(leftover_path) - - @mock.patch('os.rmdir') - def test_cleanup_failure(self, mock_rmdir): - self.auth.prepare() - self.auth.perform([self.achall]) - - os_error = OSError() - os_error.errno = errno.EACCES - mock_rmdir.side_effect = os_error - - self.auth.cleanup([self.achall]) - self.assertFalse(os.path.exists(self.validation_path)) - self.assertTrue(os.path.exists(self.root_challenge_path)) - - -class WebrootActionTest(unittest.TestCase): - """Tests for webroot argparse actions.""" - - achall = achallenges.KeyAuthorizationAnnotatedChallenge( - challb=acme_util.HTTP01_P, domain="thing.com", account_key=KEY) - - def setUp(self): - from certbot.plugins.webroot import Authenticator - self.path = tempfile.mkdtemp() - self.parser = argparse.ArgumentParser() - self.parser.add_argument("-d", "--domains", - action="append", default=[]) - Authenticator.inject_parser_options(self.parser, "webroot") - - def test_webroot_map_action(self): - args = self.parser.parse_args( - ["--webroot-map", json.dumps({'thing.com': self.path})]) - self.assertEqual(args.webroot_map["thing.com"], self.path) - - def test_domain_before_webroot(self): - args = self.parser.parse_args( - "-d {0} -w {1}".format(self.achall.domain, self.path).split()) - config = self._get_config_after_perform(args) - self.assertEqual(config.webroot_map[self.achall.domain], self.path) - - def test_domain_before_webroot_error(self): - self.assertRaises(errors.PluginError, self.parser.parse_args, - "-d foo -w bar -w baz".split()) - self.assertRaises(errors.PluginError, self.parser.parse_args, - "-d foo -w bar -d baz -w qux".split()) - - def test_multiwebroot(self): - args = self.parser.parse_args("-w {0} -d {1} -w {2} -d bar".format( - self.path, self.achall.domain, tempfile.mkdtemp()).split()) - self.assertEqual(args.webroot_map[self.achall.domain], self.path) - config = self._get_config_after_perform(args) - self.assertEqual( - config.webroot_map[self.achall.domain], self.path) - - def _get_config_after_perform(self, config): - from certbot.plugins.webroot import Authenticator - auth = Authenticator(config, "webroot") - auth.perform([self.achall]) - return auth.config - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/pyproject.toml b/certbot/pyproject.toml new file mode 100644 index 00000000000..03226ae826f --- /dev/null +++ b/certbot/pyproject.toml @@ -0,0 +1,55 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "certbot" +dynamic = ["version", "dependencies", "optional-dependencies"] +description = "ACME client" +readme = "README.rst" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Environment :: Console", + "Intended Audience :: System Administrators", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", + "Topic :: System :: Installation/Setup", + "Topic :: System :: Networking", + "Topic :: System :: Systems Administration", + "Topic :: Utilities", +] + +[project.scripts] +certbot = "certbot.main:main" + +[project.entry-points."certbot.plugins"] +manual = "certbot._internal.plugins.manual:Authenticator" +null = "certbot._internal.plugins.null:Installer" +standalone = "certbot._internal.plugins.standalone:Authenticator" +webroot = "certbot._internal.plugins.webroot:Authenticator" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools] +package-dir = {"" = "src"} + +[tool.setuptools.packages.find] +where = ["src"] # list of folders that contain the packages (["."] by default) +exclude = ['docs', 'examples', 'tests', 'venv'] # exclude packages matching these glob patterns + +[tool.setuptools.dynamic] +version = {attr = "certbot.__version__"} # any module attribute compatible with ast.literal_eval diff --git a/certbot/renewal.py b/certbot/renewal.py deleted file mode 100644 index 4c592b27ff5..00000000000 --- a/certbot/renewal.py +++ /dev/null @@ -1,479 +0,0 @@ -"""Functionality for autorenewal and associated juggling of configurations""" -from __future__ import print_function -import copy -import itertools -import logging -import os -import traceback -import sys -import time -import random - -import six -import zope.component - -import OpenSSL - -from acme.magic_typing import List # pylint: disable=unused-import, no-name-in-module - -from certbot import cli -from certbot import crypto_util -from certbot import errors -from certbot import interfaces -from certbot import util -from certbot import hooks -from certbot import storage -from certbot import updater - -from certbot.plugins import disco as plugins_disco - -logger = logging.getLogger(__name__) - -# These are the items which get pulled out of a renewal configuration -# file's renewalparams and actually used in the client configuration -# during the renewal process. We have to record their types here because -# the renewal configuration process loses this information. -STR_CONFIG_ITEMS = ["config_dir", "logs_dir", "work_dir", "user_agent", - "server", "account", "authenticator", "installer", - "standalone_supported_challenges", "renew_hook", - "pre_hook", "post_hook", "tls_sni_01_address", - "http01_address"] -INT_CONFIG_ITEMS = ["rsa_key_size", "tls_sni_01_port", "http01_port"] -BOOL_CONFIG_ITEMS = ["must_staple", "allow_subset_of_names", "reuse_key", - "autorenew"] - -CONFIG_ITEMS = set(itertools.chain( - BOOL_CONFIG_ITEMS, INT_CONFIG_ITEMS, STR_CONFIG_ITEMS, ('pref_challs',))) - - -def _reconstitute(config, full_path): - """Try to instantiate a RenewableCert, updating config with relevant items. - - This is specifically for use in renewal and enforces several checks - and policies to ensure that we can try to proceed with the renewal - request. The config argument is modified by including relevant options - read from the renewal configuration file. - - :param configuration.NamespaceConfig config: configuration for the - current lineage - :param str full_path: Absolute path to the configuration file that - defines this lineage - - :returns: the RenewableCert object or None if a fatal error occurred - :rtype: `storage.RenewableCert` or NoneType - - """ - try: - renewal_candidate = storage.RenewableCert(full_path, config) - except (errors.CertStorageError, IOError): - logger.warning("", exc_info=True) - logger.warning("Renewal configuration file %s is broken. Skipping.", full_path) - logger.debug("Traceback was:\n%s", traceback.format_exc()) - return None - if "renewalparams" not in renewal_candidate.configuration: - logger.warning("Renewal configuration file %s lacks " - "renewalparams. Skipping.", full_path) - return None - renewalparams = renewal_candidate.configuration["renewalparams"] - if "authenticator" not in renewalparams: - logger.warning("Renewal configuration file %s does not specify " - "an authenticator. Skipping.", full_path) - return None - # Now restore specific values along with their data types, if - # those elements are present. - try: - restore_required_config_elements(config, renewalparams) - _restore_plugin_configs(config, renewalparams) - except (ValueError, errors.Error) as error: - logger.warning( - "An error occurred while parsing %s. The error was %s. " - "Skipping the file.", full_path, str(error)) - logger.debug("Traceback was:\n%s", traceback.format_exc()) - return None - - try: - config.domains = [util.enforce_domain_sanity(d) - for d in renewal_candidate.names()] - except errors.ConfigurationError as error: - logger.warning("Renewal configuration file %s references a cert " - "that contains an invalid domain name. The problem " - "was: %s. Skipping.", full_path, error) - return None - - return renewal_candidate - - -def _restore_webroot_config(config, renewalparams): - """ - webroot_map is, uniquely, a dict, and the general-purpose configuration - restoring logic is not able to correctly parse it from the serialized - form. - """ - if "webroot_map" in renewalparams: - if not cli.set_by_cli("webroot_map"): - config.webroot_map = renewalparams["webroot_map"] - elif "webroot_path" in renewalparams: - logger.debug("Ancient renewal conf file without webroot-map, restoring webroot-path") - wp = renewalparams["webroot_path"] - if isinstance(wp, six.string_types): # prior to 0.1.0, webroot_path was a string - wp = [wp] - config.webroot_path = wp - - -def _restore_plugin_configs(config, renewalparams): - """Sets plugin specific values in config from renewalparams - - :param configuration.NamespaceConfig config: configuration for the - current lineage - :param configobj.Section renewalparams: Parameters from the renewal - configuration file that defines this lineage - - """ - # Now use parser to get plugin-prefixed items with correct types - # XXX: the current approach of extracting only prefixed items - # related to the actually-used installer and authenticator - # works as long as plugins don't need to read plugin-specific - # variables set by someone else (e.g., assuming Apache - # configurator doesn't need to read webroot_ variables). - # Note: if a parameter that used to be defined in the parser is no - # longer defined, stored copies of that parameter will be - # deserialized as strings by this logic even if they were - # originally meant to be some other type. - plugin_prefixes = [] # type: List[str] - if renewalparams["authenticator"] == "webroot": - _restore_webroot_config(config, renewalparams) - else: - plugin_prefixes.append(renewalparams["authenticator"]) - - if renewalparams.get("installer") is not None: - plugin_prefixes.append(renewalparams["installer"]) - - for plugin_prefix in set(plugin_prefixes): - plugin_prefix = plugin_prefix.replace('-', '_') - for config_item, config_value in six.iteritems(renewalparams): - if config_item.startswith(plugin_prefix + "_") and not cli.set_by_cli(config_item): - # Values None, True, and False need to be treated specially, - # As their types aren't handled correctly by configobj - if config_value in ("None", "True", "False"): - # bool("False") == True - # pylint: disable=eval-used - setattr(config, config_item, eval(config_value)) - else: - cast = cli.argparse_type(config_item) - setattr(config, config_item, cast(config_value)) - - -def restore_required_config_elements(config, renewalparams): - """Sets non-plugin specific values in config from renewalparams - - :param configuration.NamespaceConfig config: configuration for the - current lineage - :param configobj.Section renewalparams: parameters from the renewal - configuration file that defines this lineage - - """ - - required_items = itertools.chain( - (("pref_challs", _restore_pref_challs),), - six.moves.zip(BOOL_CONFIG_ITEMS, itertools.repeat(_restore_bool)), - six.moves.zip(INT_CONFIG_ITEMS, itertools.repeat(_restore_int)), - six.moves.zip(STR_CONFIG_ITEMS, itertools.repeat(_restore_str))) - for item_name, restore_func in required_items: - if item_name in renewalparams and not cli.set_by_cli(item_name): - value = restore_func(item_name, renewalparams[item_name]) - setattr(config, item_name, value) - - -def _restore_pref_challs(unused_name, value): - """Restores preferred challenges from a renewal config file. - - If value is a `str`, it should be a single challenge type. - - :param str unused_name: option name - :param value: option value - :type value: `list` of `str` or `str` - - :returns: converted option value to be stored in the runtime config - :rtype: `list` of `str` - - :raises errors.Error: if value can't be converted to an bool - - """ - # If pref_challs has only one element, configobj saves the value - # with a trailing comma so it's parsed as a list. If this comma is - # removed by the user, the value is parsed as a str. - value = [value] if isinstance(value, six.string_types) else value - return cli.parse_preferred_challenges(value) - - -def _restore_bool(name, value): - """Restores an boolean key-value pair from a renewal config file. - - :param str name: option name - :param str value: option value - - :returns: converted option value to be stored in the runtime config - :rtype: bool - - :raises errors.Error: if value can't be converted to an bool - - """ - lowercase_value = value.lower() - if lowercase_value not in ("true", "false"): - raise errors.Error( - "Expected True or False for {0} but found {1}".format(name, value)) - return lowercase_value == "true" - - -def _restore_int(name, value): - """Restores an integer key-value pair from a renewal config file. - - :param str name: option name - :param str value: option value - - :returns: converted option value to be stored in the runtime config - :rtype: int - - :raises errors.Error: if value can't be converted to an int - - """ - if name == "http01_port" and value == "None": - logger.info("updating legacy http01_port value") - return cli.flag_default("http01_port") - - try: - return int(value) - except ValueError: - raise errors.Error("Expected a numeric value for {0}".format(name)) - - -def _restore_str(unused_name, value): - """Restores an string key-value pair from a renewal config file. - - :param str unused_name: option name - :param str value: option value - - :returns: converted option value to be stored in the runtime config - :rtype: str or None - - """ - return None if value == "None" else value - - -def should_renew(config, lineage): - "Return true if any of the circumstances for automatic renewal apply." - if config.renew_by_default: - logger.debug("Auto-renewal forced with --force-renewal...") - return True - if lineage.should_autorenew(): - logger.info("Cert is due for renewal, auto-renewing...") - return True - if config.dry_run: - logger.info("Cert not due for renewal, but simulating renewal for dry run") - return True - logger.info("Cert not yet due for renewal") - return False - - -def _avoid_invalidating_lineage(config, lineage, original_server): - "Do not renew a valid cert with one from a staging server!" - # Some lineages may have begun with --staging, but then had production certs - # added to them - with open(lineage.cert) as the_file: - contents = the_file.read() - latest_cert = OpenSSL.crypto.load_certificate( - OpenSSL.crypto.FILETYPE_PEM, contents) - # all our test certs are from happy hacker fake CA, though maybe one day - # we should test more methodically - now_valid = "fake" not in repr(latest_cert.get_issuer()).lower() - - if util.is_staging(config.server): - if not util.is_staging(original_server) or now_valid: - if not config.break_my_certs: - names = ", ".join(lineage.names()) - raise errors.Error( - "You've asked to renew/replace a seemingly valid certificate with " - "a test certificate (domains: {0}). We will not do that " - "unless you use the --break-my-certs flag!".format(names)) - - -def renew_cert(config, domains, le_client, lineage): - "Renew a certificate lineage." - renewal_params = lineage.configuration["renewalparams"] - original_server = renewal_params.get("server", cli.flag_default("server")) - _avoid_invalidating_lineage(config, lineage, original_server) - if not domains: - domains = lineage.names() - # The private key is the existing lineage private key if reuse_key is set. - # Otherwise, generate a fresh private key by passing None. - new_key = os.path.normpath(lineage.privkey) if config.reuse_key else None - new_cert, new_chain, new_key, _ = le_client.obtain_certificate(domains, new_key) - if config.dry_run: - logger.debug("Dry run: skipping updating lineage at %s", - os.path.dirname(lineage.cert)) - else: - prior_version = lineage.latest_common_version() - # TODO: Check return value of save_successor - lineage.save_successor(prior_version, new_cert, new_key.pem, new_chain, config) - lineage.update_all_links_to(lineage.latest_common_version()) - - hooks.renew_hook(config, domains, lineage.live_dir) - - -def report(msgs, category): - "Format a results report for a category of renewal outcomes" - lines = ("%s (%s)" % (m, category) for m in msgs) - return " " + "\n ".join(lines) - -def _renew_describe_results(config, renew_successes, renew_failures, - renew_skipped, parse_failures): - - out = [] # type: List[str] - notify = out.append - disp = zope.component.getUtility(interfaces.IDisplay) - - def notify_error(err): - """Notify and log errors.""" - notify(str(err)) - logger.error(err) - - if config.dry_run: - notify("** DRY RUN: simulating 'certbot renew' close to cert expiry") - notify("** (The test certificates below have not been saved.)") - notify("") - if renew_skipped: - notify("The following certs are not due for renewal yet:") - notify(report(renew_skipped, "skipped")) - if not renew_successes and not renew_failures: - notify("No renewals were attempted.") - if (config.pre_hook is not None or - config.renew_hook is not None or config.post_hook is not None): - notify("No hooks were run.") - elif renew_successes and not renew_failures: - notify("Congratulations, all renewals succeeded. The following certs " - "have been renewed:") - notify(report(renew_successes, "success")) - elif renew_failures and not renew_successes: - notify_error("All renewal attempts failed. The following certs could " - "not be renewed:") - notify_error(report(renew_failures, "failure")) - elif renew_failures and renew_successes: - notify("The following certs were successfully renewed:") - notify(report(renew_successes, "success") + "\n") - notify_error("The following certs could not be renewed:") - notify_error(report(renew_failures, "failure")) - - if parse_failures: - notify("\nAdditionally, the following renewal configurations " - "were invalid: ") - notify(report(parse_failures, "parsefail")) - - if config.dry_run: - notify("** DRY RUN: simulating 'certbot renew' close to cert expiry") - notify("** (The test certificates above have not been saved.)") - - disp.notification("\n".join(out), wrap=False) - - -def handle_renewal_request(config): # pylint: disable=too-many-locals,too-many-branches,too-many-statements - """Examine each lineage; renew if due and report results""" - - # This is trivially False if config.domains is empty - if any(domain not in config.webroot_map for domain in config.domains): - # If more plugins start using cli.add_domains, - # we may want to only log a warning here - raise errors.Error("Currently, the renew verb is capable of either " - "renewing all installed certificates that are due " - "to be renewed or renewing a single certificate specified " - "by its name. If you would like to renew specific " - "certificates by their domains, use the certonly command " - "instead. The renew verb may provide other options " - "for selecting certificates to renew in the future.") - - if config.certname: - conf_files = [storage.renewal_file_for_certname(config, config.certname)] - else: - conf_files = storage.renewal_conf_files(config) - - renew_successes = [] - renew_failures = [] - renew_skipped = [] - parse_failures = [] - - # Noninteractive renewals include a random delay in order to spread - # out the load on the certificate authority servers, even if many - # users all pick the same time for renewals. This delay precedes - # running any hooks, so that side effects of the hooks (such as - # shutting down a web service) aren't prolonged unnecessarily. - apply_random_sleep = not sys.stdin.isatty() and config.random_sleep_on_renew - - for renewal_file in conf_files: - disp = zope.component.getUtility(interfaces.IDisplay) - disp.notification("Processing " + renewal_file, pause=False) - lineage_config = copy.deepcopy(config) - lineagename = storage.lineagename_for_filename(renewal_file) - - # Note that this modifies config (to add back the configuration - # elements from within the renewal configuration file). - try: - renewal_candidate = _reconstitute(lineage_config, renewal_file) - except Exception as e: # pylint: disable=broad-except - logger.warning("Renewal configuration file %s (cert: %s) " - "produced an unexpected error: %s. Skipping.", - renewal_file, lineagename, e) - logger.debug("Traceback was:\n%s", traceback.format_exc()) - parse_failures.append(renewal_file) - continue - - try: - if renewal_candidate is None: - parse_failures.append(renewal_file) - else: - # XXX: ensure that each call here replaces the previous one - zope.component.provideUtility(lineage_config) - renewal_candidate.ensure_deployed() - from certbot import main - plugins = plugins_disco.PluginsRegistry.find_all() - if should_renew(lineage_config, renewal_candidate): - # Apply random sleep upon first renewal if needed - if apply_random_sleep: - sleep_time = random.randint(1, 60 * 8) - logger.info("Non-interactive renewal: random delay of %s seconds", - sleep_time) - time.sleep(sleep_time) - # We will sleep only once this day, folks. - apply_random_sleep = False - - # domains have been restored into lineage_config by reconstitute - # but they're unnecessary anyway because renew_cert here - # will just grab them from the certificate - # we already know it's time to renew based on should_renew - # and we have a lineage in renewal_candidate - main.renew_cert(lineage_config, plugins, renewal_candidate) - renew_successes.append(renewal_candidate.fullchain) - else: - expiry = crypto_util.notAfter(renewal_candidate.version( - "cert", renewal_candidate.latest_common_version())) - renew_skipped.append("%s expires on %s" % (renewal_candidate.fullchain, - expiry.strftime("%Y-%m-%d"))) - # Run updater interface methods - updater.run_generic_updaters(lineage_config, renewal_candidate, - plugins) - - except Exception as e: # pylint: disable=broad-except - # obtain_cert (presumably) encountered an unanticipated problem. - logger.warning("Attempting to renew cert (%s) from %s produced an " - "unexpected error: %s. Skipping.", lineagename, - renewal_file, e) - logger.debug("Traceback was:\n%s", traceback.format_exc()) - renew_failures.append(renewal_candidate.fullchain) - - # Describe all the results - _renew_describe_results(config, renew_successes, renew_failures, - renew_skipped, parse_failures) - - if renew_failures or parse_failures: - raise errors.Error("{0} renew failure(s), {1} parse failure(s)".format( - len(renew_failures), len(parse_failures))) - else: - logger.debug("no renewal failures") diff --git a/certbot/reporter.py b/certbot/reporter.py deleted file mode 100644 index e0063d8e585..00000000000 --- a/certbot/reporter.py +++ /dev/null @@ -1,100 +0,0 @@ -"""Collects and displays information to the user.""" -from __future__ import print_function - -import collections -import logging -import sys -import textwrap - -from six.moves import queue # type: ignore # pylint: disable=import-error -import zope.interface - -from certbot import interfaces -from certbot import util - - -logger = logging.getLogger(__name__) - - -@zope.interface.implementer(interfaces.IReporter) -class Reporter(object): - """Collects and displays information to the user. - - :ivar `queue.PriorityQueue` messages: Messages to be displayed to - the user. - - """ - - HIGH_PRIORITY = 0 - """High priority constant. See `add_message`.""" - MEDIUM_PRIORITY = 1 - """Medium priority constant. See `add_message`.""" - LOW_PRIORITY = 2 - """Low priority constant. See `add_message`.""" - - _msg_type = collections.namedtuple('ReporterMsg', 'priority text on_crash') - - def __init__(self, config): - self.messages = queue.PriorityQueue() - self.config = config - - def add_message(self, msg, priority, on_crash=True): - """Adds msg to the list of messages to be printed. - - :param str msg: Message to be displayed to the user. - - :param int priority: One of `HIGH_PRIORITY`, `MEDIUM_PRIORITY`, - or `LOW_PRIORITY`. - - :param bool on_crash: Whether or not the message should be - printed if the program exits abnormally. - - """ - assert self.HIGH_PRIORITY <= priority <= self.LOW_PRIORITY - self.messages.put(self._msg_type(priority, msg, on_crash)) - logger.debug("Reporting to user: %s", msg) - - def print_messages(self): - """Prints messages to the user and clears the message queue. - - If there is an unhandled exception, only messages for which - ``on_crash`` is ``True`` are printed. - - """ - bold_on = False - if not self.messages.empty(): - no_exception = sys.exc_info()[0] is None - bold_on = sys.stdout.isatty() - if not self.config.quiet: - if bold_on: - print(util.ANSI_SGR_BOLD) - print('IMPORTANT NOTES:') - first_wrapper = textwrap.TextWrapper( - initial_indent=' - ', - subsequent_indent=(' ' * 3), - break_long_words=False, - break_on_hyphens=False) - next_wrapper = textwrap.TextWrapper( - initial_indent=first_wrapper.subsequent_indent, - subsequent_indent=first_wrapper.subsequent_indent, - break_long_words=False, - break_on_hyphens=False) - while not self.messages.empty(): - msg = self.messages.get() - if self.config.quiet: - # In --quiet mode, we only print high priority messages that - # are flagged for crash cases - if not (msg.priority == self.HIGH_PRIORITY and msg.on_crash): - continue - if no_exception or msg.on_crash: - if bold_on and msg.priority > self.HIGH_PRIORITY: - if not self.config.quiet: - sys.stdout.write(util.ANSI_SGR_RESET) - bold_on = False - lines = msg.text.splitlines() - print(first_wrapper.fill(lines[0])) - if len(lines) > 1: - print("\n".join( - next_wrapper.fill(line) for line in lines[1:])) - if bold_on and not self.config.quiet: - sys.stdout.write(util.ANSI_SGR_RESET) diff --git a/certbot/setup.py b/certbot/setup.py new file mode 100644 index 00000000000..6542a4e60e8 --- /dev/null +++ b/certbot/setup.py @@ -0,0 +1,105 @@ +import codecs +import os +import re + +from setuptools import setup + + +def read_file(filename, encoding='utf8'): + """Read unicode from given file.""" + with codecs.open(filename, encoding=encoding) as fd: + return fd.read() + + +here = os.path.abspath(os.path.dirname(__file__)) + +# read version number (and other metadata) from package init +init_fn = os.path.join(here, 'src', 'certbot', '__init__.py') +meta = dict(re.findall(r"""__([a-z]+)__ = '([^']+)""", read_file(init_fn))) + +version = meta['version'] + +# This package relies on PyOpenSSL and requests, however, it isn't specified +# here to avoid masking the more specific request requirements in acme. See +# https://github.com/pypa/pip/issues/988 for more info. +install_requires = [ + # We specify the minimum acme version as the current Certbot version for + # simplicity. See https://github.com/certbot/certbot/issues/8761 for more + # info. + f'acme>={version}', + 'ConfigArgParse>=1.5.3', + 'configobj>=5.0.6', + 'cryptography>=47.0.0', + 'distro>=1.7.0', + 'importlib_metadata>=8.6.1; python_version < "3.10"', + 'josepy>=2.0.0', + 'parsedatetime>=2.6', + 'pyrfc3339', + # This dependency needs to be added using environment markers to avoid its + # installation on Linux. + 'pywin32>=300 ; sys_platform == "win32"', +] + +extras_require = { + "dev": [ + "apacheconfig>=0.3.2", + "build", + "ipdb", + # allows us to use newer urllib3 https://github.com/python-poetry/poetry-plugin-export/issues/183 + "poetry-plugin-export>=1.9.0", + # poetry 1.2.0+ is required for it to pin pip, setuptools, and wheel. See + # https://github.com/python-poetry/poetry/issues/1584. + "poetry>=1.2.0", + "towncrier", + "twine", + ], + "docs": [ + # If you have Sphinx<1.5.1, you need docutils<0.13.1 + # https://github.com/sphinx-doc/sphinx/issues/3212 + "Sphinx>=1.2", # Annotation support + "sphinx_rtd_theme", + ], + # Tools like pip, wheel, and tox are listed here to ensure they are properly + # pinned and installed during automated testing. + "test": [ + "coverage", + "mypy", + "pip", + "pylint", + "pytest", + "pytest-cov>=4.1.0", # https://github.com/pytest-dev/pytest-cov/pull/558 + "pytest-xdist", + "ruff", + "setuptools", + "tox", + "types-httplib2", + "types-pyRFC3339", + "types-pywin32", + "types-requests", + "types-setuptools", + "uv", + "wheel", + ], + "apache": [ + # If a user installs `certbot[apache]`, we want to include the shim + f'certbot-apache>={version}', + 'python-augeas', + ], + "nginx": [ + # If a user installs `certbot[nginx]`, we want to include the shim + f'certbot-nginx>={version}', + # PyOpenSSL>=25.0.0 is just needed to satisfy mypy right now so this dependency can probably be + # relaxed to >=24.0.0 if needed. + 'PyOpenSSL>=25.0.0', + 'pyparsing>=3.0.0', + ], + "all": [ + "certbot[dev,docs,test,apache,nginx]" + ], +} + + +setup( + install_requires=install_requires, + extras_require=extras_require, +) diff --git a/certbot/src/certbot/__init__.py b/certbot/src/certbot/__init__.py new file mode 100644 index 00000000000..8da3222d9de --- /dev/null +++ b/certbot/src/certbot/__init__.py @@ -0,0 +1,4 @@ +"""Certbot client.""" + +# version number like 1.2.3a0, must have at least 2 parts, like 1.2 +__version__ = '5.8.0.dev0' diff --git a/certbot/src/certbot/_internal/__init__.py b/certbot/src/certbot/_internal/__init__.py new file mode 100644 index 00000000000..45ec6ac9c53 --- /dev/null +++ b/certbot/src/certbot/_internal/__init__.py @@ -0,0 +1,6 @@ +""" +Modules internal to Certbot. + +This package contains modules that are not considered part of Certbot's public +API. They may be changed without updating Certbot's major version. +""" diff --git a/certbot/src/certbot/_internal/account.py b/certbot/src/certbot/_internal/account.py new file mode 100644 index 00000000000..f2b281f12d0 --- /dev/null +++ b/certbot/src/certbot/_internal/account.py @@ -0,0 +1,354 @@ +"""Creates ACME accounts for server.""" +import datetime +import functools +import hashlib +import logging +import shutil +import socket +from typing import Any +from typing import Callable +from typing import cast +from typing import Mapping +from typing import Optional + +from cryptography.hazmat.primitives import serialization +import josepy as jose +import pyrfc3339 + +from acme import fields as acme_fields +from acme import messages +from acme.client import ClientV2 +from certbot import configuration +from certbot import errors +from certbot import interfaces +from certbot import util +from certbot._internal import constants +from certbot.compat import filesystem +from certbot.compat import os + +logger = logging.getLogger(__name__) + + +class Account: + """ACME protocol registration. + + :ivar .RegistrationResource regr: Registration Resource + :ivar .JWK key: Authorized Account Key + :ivar .Meta: Account metadata + :ivar str id: Globally unique account identifier. + + """ + + class Meta(jose.JSONObjectWithFields): + """Account metadata + + :ivar datetime.datetime creation_dt: Creation date and time (UTC). + :ivar str creation_host: FQDN of host, where account has been created. + :ivar str register_to_eff: If not None, Certbot will register the provided + email during the account registration. + + .. note:: ``creation_dt`` and ``creation_host`` are useful in + cross-machine migration scenarios. + + """ + creation_dt: datetime.datetime = acme_fields.rfc3339("creation_dt") + creation_host: str = jose.field("creation_host") + register_to_eff: str = jose.field("register_to_eff", omitempty=True) + + def __init__(self, regr: messages.RegistrationResource, key: jose.JWK, + meta: Optional['Meta'] = None) -> None: + self.key = key + self.regr = regr + self.meta = self.Meta( + # pyrfc3339 drops microseconds, make sure __eq__ is sane + creation_dt=datetime.datetime.now(tz=datetime.timezone.utc).replace(microsecond=0), + creation_host=socket.gethostname(), + register_to_eff=None) if meta is None else meta + + # try MD5, else use MD5 in non-security mode (e.g. for FIPS systems / RHEL) + try: + hasher = hashlib.md5() + except ValueError: + # This cast + dictionary expansion is made to make mypy happy without the need of a + # "type: ignore" directive that will also require to disable the check on useless + # "type: ignore" directives when mypy is run on Python 3.9+. + hasher = hashlib.new('md5', **cast(Mapping[str, Any], {"usedforsecurity": False})) + + hasher.update(self.key.key.public_key().public_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PublicFormat.SubjectPublicKeyInfo) + ) + + self.id = hasher.hexdigest() + # Implementation note: Email? Multiple accounts can have the + # same email address. Registration URI? Assigned by the + # server, not guaranteed to be stable over time, nor + # canonical URI can be generated. ACME protocol doesn't allow + # account key (and thus its fingerprint) to be updated... + + @property + def slug(self) -> str: + """Short account identification string, useful for UI.""" + return "{1}@{0} ({2})".format(pyrfc3339.generate( + self.meta.creation_dt), self.meta.creation_host, self.id[:4]) + + def __repr__(self) -> str: + return "<{0}({1}, {2}, {3})>".format( + self.__class__.__name__, self.regr, self.id, self.meta) + + def __eq__(self, other: Any) -> bool: + return (isinstance(other, self.__class__) and + self.key == other.key and self.regr == other.regr and + self.meta == other.meta) + + +class AccountMemoryStorage(interfaces.AccountStorage): + """In-memory account storage.""" + + def __init__(self, initial_accounts: Optional[dict[str, Account]] = None) -> None: + self.accounts = initial_accounts if initial_accounts is not None else {} + + def find_all(self) -> list[Account]: + return list(self.accounts.values()) + + def save(self, account: Account, client: ClientV2) -> None: + if account.id in self.accounts: + logger.debug("Overwriting account: %s", account.id) + self.accounts[account.id] = account + + def load(self, account_id: str) -> Account: + try: + return self.accounts[account_id] + except KeyError: + raise errors.AccountNotFound(account_id) + + +class AccountFileStorage(interfaces.AccountStorage): + """Accounts file storage. + + :ivar certbot.configuration.NamespaceConfig config: Client configuration + + """ + def __init__(self, config: configuration.NamespaceConfig) -> None: + self.config = config + util.make_or_verify_dir(config.accounts_dir, 0o700, self.config.strict_permissions) + + def _account_dir_path(self, account_id: str) -> str: + return self._account_dir_path_for_server_path(account_id, self.config.server_path) + + def _account_dir_path_for_server_path(self, account_id: str, server_path: str) -> str: + accounts_dir = self.config.accounts_dir_for_server_path(server_path) + return os.path.join(accounts_dir, account_id) + + @classmethod + def _regr_path(cls, account_dir_path: str) -> str: + return os.path.join(account_dir_path, "regr.json") + + @classmethod + def _key_path(cls, account_dir_path: str) -> str: + return os.path.join(account_dir_path, "private_key.json") + + @classmethod + def _metadata_path(cls, account_dir_path: str) -> str: + return os.path.join(account_dir_path, "meta.json") + + def _find_all_for_server_path(self, server_path: str) -> list[Account]: + accounts_dir = self.config.accounts_dir_for_server_path(server_path) + try: + candidates = os.listdir(accounts_dir) + except OSError: + return [] + + accounts = [] + for account_id in candidates: + try: + accounts.append(self._load_for_server_path(account_id, server_path)) + except errors.AccountStorageError: + logger.debug("Account loading problem", exc_info=True) + + if not accounts and server_path in constants.LE_REUSE_SERVERS: + # find all for the next link down + prev_server_path = constants.LE_REUSE_SERVERS[server_path] + prev_accounts = self._find_all_for_server_path(prev_server_path) + # if we found something, link to that + if prev_accounts: + try: + self._symlink_to_accounts_dir(prev_server_path, server_path) + except OSError: + return [] + accounts = prev_accounts + return accounts + + def find_all(self) -> list[Account]: + return self._find_all_for_server_path(self.config.server_path) + + def _symlink_to_account_dir(self, prev_server_path: str, server_path: str, + account_id: str) -> None: + prev_account_dir = self._account_dir_path_for_server_path(account_id, prev_server_path) + new_account_dir = self._account_dir_path_for_server_path(account_id, server_path) + os.symlink(prev_account_dir, new_account_dir) + + def _symlink_to_accounts_dir(self, prev_server_path: str, server_path: str) -> None: + accounts_dir = self.config.accounts_dir_for_server_path(server_path) + if os.path.islink(accounts_dir): + os.unlink(accounts_dir) + else: + os.rmdir(accounts_dir) + prev_account_dir = self.config.accounts_dir_for_server_path(prev_server_path) + os.symlink(prev_account_dir, accounts_dir) + + def _load_for_server_path(self, account_id: str, server_path: str) -> Account: + account_dir_path = self._account_dir_path_for_server_path(account_id, server_path) + if not os.path.isdir(account_dir_path): # isdir is also true for symlinks + if server_path in constants.LE_REUSE_SERVERS: + prev_server_path = constants.LE_REUSE_SERVERS[server_path] + prev_loaded_account = self._load_for_server_path(account_id, prev_server_path) + # we didn't error so we found something, so create a symlink to that + accounts_dir = self.config.accounts_dir_for_server_path(server_path) + # If accounts_dir isn't empty, make an account specific symlink + if os.listdir(accounts_dir): + self._symlink_to_account_dir(prev_server_path, server_path, account_id) + else: + self._symlink_to_accounts_dir(prev_server_path, server_path) + return prev_loaded_account + raise errors.AccountNotFound(f"Account at {account_dir_path} does not exist") + + try: + with open(self._regr_path(account_dir_path)) as regr_file: + regr = messages.RegistrationResource.json_loads(regr_file.read()) + with open(self._key_path(account_dir_path)) as key_file: + key = jose.JWK.json_loads(key_file.read()) + with open(self._metadata_path(account_dir_path)) as metadata_file: + meta = Account.Meta.json_loads(metadata_file.read()) + except OSError as error: + raise errors.AccountStorageError(error) + + return Account(regr, key, meta) + + def load(self, account_id: str) -> Account: + return self._load_for_server_path(account_id, self.config.server_path) + + def save(self, account: Account, client: ClientV2) -> None: + """Create a new account. + + :param Account account: account to create + :param ClientV2 client: ACME client associated to the account + + """ + try: + dir_path = self._prepare(account) + self._create(account, dir_path) + self._update_meta(account, dir_path) + self._update_regr(account, dir_path) + except OSError as error: + raise errors.AccountStorageError(error) + + def update_regr(self, account: Account) -> None: + """Update the registration resource. + + :param Account account: account to update + + """ + try: + dir_path = self._prepare(account) + self._update_regr(account, dir_path) + except OSError as error: + raise errors.AccountStorageError(error) + + def update_meta(self, account: Account) -> None: + """Update the meta resource. + + :param Account account: account to update + + """ + try: + dir_path = self._prepare(account) + self._update_meta(account, dir_path) + except OSError as error: + raise errors.AccountStorageError(error) + + def delete(self, account_id: str) -> None: + """Delete registration info from disk + + :param account_id: id of account which should be deleted + + """ + account_dir_path = self._account_dir_path(account_id) + if not os.path.isdir(account_dir_path): + raise errors.AccountNotFound(f"Account at {account_dir_path} does not exist") + # Step 1: Delete account specific links and the directory + self._delete_account_dir_for_server_path(account_id, self.config.server_path) + + # Step 2: Remove any accounts links and directories that are now empty + if not os.listdir(self.config.accounts_dir): + self._delete_accounts_dir_for_server_path(self.config.server_path) + + def _delete_account_dir_for_server_path(self, account_id: str, server_path: str) -> None: + link_func = functools.partial(self._account_dir_path_for_server_path, account_id) + nonsymlinked_dir = self._delete_links_and_find_target_dir(server_path, link_func) + shutil.rmtree(nonsymlinked_dir) + + def _delete_accounts_dir_for_server_path(self, server_path: str) -> None: + link_func = self.config.accounts_dir_for_server_path + nonsymlinked_dir = self._delete_links_and_find_target_dir(server_path, link_func) + os.rmdir(nonsymlinked_dir) + + def _delete_links_and_find_target_dir(self, server_path: str, + link_func: Callable[[str], str]) -> str: + """Delete symlinks and return the nonsymlinked directory path. + + :param str server_path: file path based on server + :param callable link_func: callable that returns possible links + given a server_path + + :returns: the final, non-symlinked target + :rtype: str + + """ + dir_path = link_func(server_path) + + # does an appropriate directory link to me? if so, make sure that's gone + reused_servers = {} + for k, v in constants.LE_REUSE_SERVERS.items(): + reused_servers[v] = k + + # is there a next one up? + possible_next_link = True + while possible_next_link: + possible_next_link = False + if server_path in reused_servers: + next_server_path = reused_servers[server_path] + next_dir_path = link_func(next_server_path) + if os.path.islink(next_dir_path) and filesystem.readlink(next_dir_path) == dir_path: + possible_next_link = True + server_path = next_server_path + dir_path = next_dir_path + + # if there's not a next one up to delete, then delete me + # and whatever I link to + while os.path.islink(dir_path): + target = filesystem.readlink(dir_path) + os.unlink(dir_path) + dir_path = target + + return dir_path + + def _prepare(self, account: Account) -> str: + account_dir_path = self._account_dir_path(account.id) + util.make_or_verify_dir(account_dir_path, 0o700, self.config.strict_permissions) + return account_dir_path + + def _create(self, account: Account, dir_path: str) -> None: + with util.safe_open(self._key_path(dir_path), "w", chmod=0o400) as key_file: + key_file.write(account.key.json_dumps()) + + def _update_regr(self, account: Account, dir_path: str) -> None: + with open(self._regr_path(dir_path), "w") as regr_file: + regr = messages.RegistrationResource( + body={}, + uri=account.regr.uri) + regr_file.write(regr.json_dumps()) + + def _update_meta(self, account: Account, dir_path: str) -> None: + with open(self._metadata_path(dir_path), "w") as metadata_file: + metadata_file.write(account.meta.json_dumps()) diff --git a/certbot/src/certbot/_internal/auth_handler.py b/certbot/src/certbot/_internal/auth_handler.py new file mode 100644 index 00000000000..ad53064c42f --- /dev/null +++ b/certbot/src/certbot/_internal/auth_handler.py @@ -0,0 +1,482 @@ +"""ACME AuthHandler.""" +import datetime +import logging +import time +from typing import Iterable +from typing import Optional +from typing import Sequence + +import josepy +from requests.models import Response + +from acme import challenges +from acme import client +from acme import errors as acme_errors +from acme import messages +from certbot import achallenges +from certbot import configuration +from certbot import errors +from certbot import interfaces +from certbot._internal import error_handler +from certbot._internal.account import Account +from certbot.display import util as display_util +from certbot.plugins import common as plugin_common + +logger = logging.getLogger(__name__) + + +class AuthHandler: + """ACME Authorization Handler for a client. + + :ivar auth: Authenticator capable of solving + :class:`~acme.challenges.Challenge` types + :type auth: certbot.interfaces.Authenticator + + :ivar acme.client.ClientV2 acme_client: ACME client API. + + :ivar account: Client's Account + :type account: :class:`certbot._internal.account.Account` + + :ivar list pref_challs: sorted user specified preferred challenges + type strings with the most preferred challenge listed first + + """ + def __init__(self, auth: interfaces.Authenticator, acme_client: Optional[client.ClientV2], + account: Optional[Account], pref_challs: list[str]) -> None: + self.auth = auth + self.acme = acme_client + + self.account = account + self.pref_challs = pref_challs + + def handle_authorizations(self, orderr: messages.OrderResource, + config: configuration.NamespaceConfig, best_effort: bool = False, + max_retries: int = 30, + max_time_mins: float = 30) -> list[messages.AuthorizationResource]: + """ + Retrieve all authorizations, perform all challenges required to validate + these authorizations, then poll and wait for the authorization to be checked. + :param acme.messages.OrderResource orderr: must have authorizations filled in + :param certbot.configuration.NamespaceConfig config: current Certbot configuration + :param bool best_effort: if True, not all authorizations need to be validated (eg. renew) + :param int max_retries: maximum number of retries to poll authorizations + :param float max_time_mins: maximum time (in minutes) to poll authorizations + :returns: list of all validated authorizations + :rtype: List + + :raises .AuthorizationError: If unable to retrieve all authorizations + """ + authzrs = orderr.authorizations[:] + if not authzrs: + raise errors.AuthorizationError('No authorization to handle.') + if not self.acme: + raise errors.Error("No ACME client defined, authorizations cannot be handled.") + + # Retrieve challenges that need to be performed to validate authorizations. + achalls = self._choose_challenges(authzrs) + if not achalls: + return authzrs + + # Starting now, challenges will be cleaned at the end no matter what. + with error_handler.ExitHandler(self._cleanup_challenges, achalls): + # To begin, let's ask the authenticator plugin to perform all challenges. + try: + resps = self.auth.perform(achalls) + + # If debug is on, wait for user input before starting the verification process. + if config.debug_challenges: + display_util.notification( + 'Challenges loaded. Press continue to submit to CA.\n' + + self._debug_challenges_msg(achalls, config), pause=True) + except errors.AuthorizationError as error: + logger.critical('Failure in setting up challenges.') + logger.info('Attempting to clean up outstanding challenges...') + raise error + # All challenges should have been processed by the authenticator. + assert len(resps) == len(achalls), 'Some challenges have not been performed.' + + # Inform the ACME CA server that challenges are available for validation. + for achall, resp in zip(achalls, resps): + self.acme.answer_challenge(achall.challb, resp) + + # Wait for authorizations to be checked. + logger.info('Waiting for verification...') + self._poll_authorizations(authzrs, max_retries, max_time_mins, best_effort) + + # Keep validated authorizations only. If there is none, no certificate can be issued. + authzrs_validated = [authzr for authzr in authzrs + if authzr.body.status == messages.STATUS_VALID] + if not authzrs_validated: + raise errors.AuthorizationError('All challenges have failed.') + + return authzrs_validated + + raise errors.Error("An unexpected error occurred while handling the authorizations.") + + def deactivate_valid_authorizations(self, orderr: messages.OrderResource) -> tuple[list, list]: + """ + Deactivate all `valid` authorizations in the order, so that they cannot be reused + in subsequent orders. + :param messages.OrderResource orderr: must have authorizations filled in + :returns: tuple of list of successfully deactivated authorizations, and + list of unsuccessfully deactivated authorizations. + :rtype: tuple + """ + if not self.acme: + raise errors.Error("No ACME client defined, cannot deactivate valid authorizations.") + + to_deactivate = [authzr for authzr in orderr.authorizations + if authzr.body.status == messages.STATUS_VALID] + deactivated = [] + failed = [] + + for authzr in to_deactivate: + try: + authzr = self.acme.deactivate_authorization(authzr) + deactivated.append(authzr) + except acme_errors.Error as e: + failed.append(authzr) + logger.debug('Failed to deactivate authorization %s: %s', authzr.uri, e) + + return (deactivated, failed) + + def _poll_authorizations(self, authzrs: list[messages.AuthorizationResource], max_retries: int, + deadline_minutes: float, best_effort: bool) -> None: + """ + Poll the ACME CA server, to wait for confirmation that authorizations have their challenges + all verified. The poll may occur several times, until all authorizations are checked + (valid or invalid), or a maximum of retries, or the polling deadline is reached. + """ + if not self.acme: + raise errors.Error("No ACME client defined, cannot poll authorizations.") + + authzrs_to_check: dict[int, tuple[messages.AuthorizationResource, + Optional[Response]]] = {index: (authzr, None) + for index, authzr in enumerate(authzrs)} + authzrs_failed_to_report = [] + deadline = datetime.datetime.now() + datetime.timedelta(minutes=deadline_minutes) + # Give an initial second to the ACME CA server to check the authorizations + sleep_seconds: float = 1 + for _ in range(max_retries): + # Wait for appropriate time (from Retry-After, initial wait, or no wait) + if sleep_seconds > 0: + time.sleep(sleep_seconds) + # Poll all updated authorizations. + authzrs_to_check = {index: self.acme.poll(authzr) for index, (authzr, _) + in authzrs_to_check.items()} + # Update the original list of authzr with the updated authzrs from server. + for index, (authzr, _) in authzrs_to_check.items(): + authzrs[index] = authzr + + # Gather failed authorizations + authzrs_failed = [authzr for authzr, _ in authzrs_to_check.values() + if authzr.body.status == messages.STATUS_INVALID] + for authzr_failed in authzrs_failed: + logger.info('Challenge failed for domain %s', + authzr_failed.body.identifier.value) + # Accumulating all failed authzrs to build a consolidated report + # on them at the end of the polling. + authzrs_failed_to_report.extend(authzrs_failed) + + # Extract out the authorization already checked for next poll iteration. + # Poll may stop here because there is no pending authorizations anymore. + authzrs_to_check = {index: (authzr, resp) for index, (authzr, resp) + in authzrs_to_check.items() + if authzr.body.status == messages.STATUS_PENDING} + if not authzrs_to_check or datetime.datetime.now() > deadline: + # Polling process is finished, we can leave the loop + break + + # Be merciful with the ACME server CA, check the Retry-After header returned, + # and wait this time before polling again in next loop iteration. + # From all the pending authorizations, we take the greatest Retry-After value + # to avoid polling an authorization before its relevant Retry-After value. + # (by construction resp cannot be None at that time, but mypy do not know it). + retry_after = max(self.acme.retry_after(resp, 3) + for _, resp in authzrs_to_check.values() + if resp is not None) + # Whatever Retry-After the ACME server requests, the polling must not take + # longer than the overall deadline (https://github.com/certbot/certbot/issues/9526). + retry_after = min(retry_after, deadline) + sleep_seconds = (retry_after - datetime.datetime.now()).total_seconds() + + # In case of failed authzrs, create a report to the user. + if authzrs_failed_to_report: + self._report_failed_authzrs(authzrs_failed_to_report) + if not best_effort: + # Without best effort, having failed authzrs is critical and fail the process. + raise errors.AuthorizationError('Some challenges have failed.') + + if authzrs_to_check: + # Here authzrs_to_check is still not empty, meaning we exceeded the max polling attempt. + raise errors.AuthorizationError('All authorizations were not finalized by the CA.') + + def _choose_challenges(self, authzrs: Iterable[messages.AuthorizationResource] + ) -> list[achallenges.AnnotatedChallenge]: + """ + Retrieve necessary and pending challenges to satisfy server. + NB: Necessary and already validated challenges are not retrieved, + as they can be reused for a certificate issuance. + """ + if not self.acme: + raise errors.Error("No ACME client defined, cannot choose the challenges.") + + pending_authzrs = [authzr for authzr in authzrs + if authzr.body.status != messages.STATUS_VALID] + achalls: list[achallenges.AnnotatedChallenge] = [] + if pending_authzrs: + logger.info("Performing the following challenges:") + for authzr in pending_authzrs: + authzr_challenges = authzr.body.challenges + + path = gen_challenge_path( + authzr_challenges, + self._get_chall_pref(authzr.body.identifier.value)) + + achalls.extend(self._challenge_factory(authzr, path)) + + return achalls + + def _get_chall_pref(self, identifier: str) -> list[type[challenges.Challenge]]: + """Return list of challenge preferences. + + :param str domain: domain for which you are requesting preferences + + """ + chall_prefs = [] + # The 'identifier' parameter of `get_chall_pref` used to be called `domain`. + # There may be plugins in the wild that name their parameter `domain`. To keep + # working with those plugins, make sure to continue to pass `identifier` as a + # positional parameter rather than a kwarg. + # Also, make sure to make a copy... + plugin_pref = self.auth.get_chall_pref(identifier) + if self.pref_challs: + plugin_pref_types = {chall.typ for chall in plugin_pref} + for typ in self.pref_challs: + if typ in plugin_pref_types: + chall_prefs.append(challenges.Challenge.TYPES[typ]) + if chall_prefs: + return chall_prefs + raise errors.AuthorizationError( + "None of the preferred challenges " + "are supported by the selected plugin") + chall_prefs.extend(plugin_pref) + return chall_prefs + + def _cleanup_challenges(self, achalls: list[achallenges.AnnotatedChallenge]) -> None: + """Cleanup challenges. + + :param achalls: annotated challenges to cleanup + :type achalls: `list` of :class:`certbot.achallenges.AnnotatedChallenge` + + """ + logger.info("Cleaning up challenges") + self.auth.cleanup(achalls) + + def _challenge_factory(self, authzr: messages.AuthorizationResource, + path: Sequence[int]) -> list[achallenges.AnnotatedChallenge]: + """Construct Namedtuple Challenges + + :param messages.AuthorizationResource authzr: authorization + + :param list path: List of indices from `challenges`. + + :returns: achalls, list of challenge type + :class:`certbot.achallenges.AnnotatedChallenge` + :rtype: list + + :raises .errors.Error: if challenge type is not recognized + + """ + if not self.account: + raise errors.Error("Account is not set.") + achalls = [] + + for index in path: + challb = authzr.body.challenges[index] + achalls.append(challb_to_achall(challb, self.account.key, authzr.body.identifier)) + + return achalls + + def _report_failed_authzrs(self, failed_authzrs: list[messages.AuthorizationResource]) -> None: + """Notifies the user about failed authorizations.""" + if not self.account: + raise errors.Error("Account is not set.") + problems: dict[str, list[achallenges.AnnotatedChallenge]] = {} + failed_achalls = [challb_to_achall(challb, self.account.key, authzr.body.identifier) + for authzr in failed_authzrs for challb in authzr.body.challenges + if challb.error] + + for achall in failed_achalls: + problems.setdefault(achall.error.typ, []).append(achall) + + msg = ["\nCertbot failed to authenticate some domains " + f"(authenticator: {self.auth.name})." + " The Certificate Authority reported these problems:"] + + for _, achalls in sorted(problems.items(), key=lambda item: item[0]): + msg.append(_generate_failed_chall_msg(achalls)) + + # auth_hint will only be called on authenticators that subclass + # plugin_common.Plugin. Refer to comment on that function. + if failed_achalls and isinstance(self.auth, plugin_common.Plugin): + msg.append(f"\nHint: {self.auth.auth_hint(failed_achalls)}\n") + + display_util.notify("".join(msg)) + + def _debug_challenges_msg(self, achalls: list[achallenges.AnnotatedChallenge], + config: configuration.NamespaceConfig) -> str: + """Construct message for debug challenges prompt + + :param list achalls: A list of + :class:`certbot.achallenges.AnnotatedChallenge`. + :param certbot.configuration.NamespaceConfig config: current Certbot configuration + :returns: Message containing challenge debug info + :rtype: str + + """ + if config.verbose_count > 0: + msg = [] + http01_achalls = {} + dns01_achalls = {} + for achall in achalls: + if isinstance(achall.chall, challenges.HTTP01): + http01_achalls[achall.chall.uri(achall.identifier.value)] = ( + achall.validation(achall.account_key) + "\n" + ) + if isinstance(achall.chall, challenges.DNS01): + dns01_achalls[achall.validation_domain_name(achall.identifier.value)] = ( + achall.validation(achall.account_key) + "\n" + ) + if http01_achalls: + msg.append("The following URLs should be accessible from the " + "internet and return the value mentioned:\n") + for uri, key_authz in http01_achalls.items(): + msg.append(f"URL: {uri}\nExpected value: {key_authz}") + if dns01_achalls: + msg.append("The following FQDNs should return a TXT resource " + "record with the value mentioned:\n") + for fqdn, key_authz_hash in dns01_achalls.items(): + msg.append(f"FQDN: {fqdn}\nExpected value: {key_authz_hash}") + return "\n" + "\n".join(msg) + else: + return 'Pass "-v" for more info about challenges.' + + +def challb_to_achall(challb: messages.ChallengeBody, account_key: josepy.JWK, + identifier: messages.Identifier) -> achallenges.AnnotatedChallenge: + """Converts a ChallengeBody object to an AnnotatedChallenge. + + :param .ChallengeBody challb: ChallengeBody + :param .JWK account_key: Authorized Account Key + :param str domain: Domain of the challb + + :returns: Appropriate AnnotatedChallenge + :rtype: :class:`certbot.achallenges.AnnotatedChallenge` + + """ + chall = challb.chall + logger.info("%s challenge for %s", chall.typ, identifier) + + if isinstance(chall, challenges.KeyAuthorizationChallenge): + return achallenges.KeyAuthorizationAnnotatedChallenge( + challb=challb, account_key=account_key, identifier=identifier) + elif isinstance(chall, challenges.DNS): + return achallenges.DNS(challb=challb, identifier=identifier) + else: + return achallenges.Other(challb=challb, identifier=identifier) + + +def gen_challenge_path(challbs: list[messages.ChallengeBody], + preferences: list[type[challenges.Challenge]]) -> tuple[int, ...]: + """Generate a plan to get authority over the identity. + + :param tuple challbs: A tuple of challenges + (:class:`acme.messages.Challenge`) from + :class:`acme.messages.AuthorizationResource` to be + fulfilled by the client in order to prove possession of the + identifier. + + :param list preferences: List of challenge preferences for domain + (:class:`acme.challenges.Challenge` subclasses) + + :returns: list of indices from ``challenges``. + :rtype: list + + :raises certbot.errors.AuthorizationError: If a + path cannot be created that satisfies the CA given the preferences and + combinations. + + """ + chall_cost = {} + max_cost = 1 + for i, chall_cls in enumerate(preferences): + chall_cost[chall_cls] = i + max_cost += i + + # max_cost is now equal to sum(indices) + 1 + + best_combo: Optional[tuple[int, ...]] = None + # Set above completing all of the available challenges + best_combo_cost = max_cost + + combinations = tuple((i,) for i in range(len(challbs))) + + combo_total = 0 + for combo in combinations: + for challenge_index in combo: + combo_total += chall_cost.get(challbs[ + challenge_index].chall.__class__, max_cost) + + if combo_total < best_combo_cost: + best_combo = combo + best_combo_cost = combo_total + + combo_total = 0 + + if not best_combo: + raise _report_no_chall_path(challbs) + + return best_combo + + +def _report_no_chall_path(challbs: list[messages.ChallengeBody]) -> errors.AuthorizationError: + """Logs and return a raisable error reporting that no satisfiable chall path exists. + + :param challbs: challenges from the authorization that can't be satisfied + + :returns: An authorization error + :rtype: certbot.errors.AuthorizationError + + """ + msg = ("Client with the currently selected authenticator does not support " + "any combination of challenges that will satisfy the CA.") + if len(challbs) == 1 and isinstance(challbs[0].chall, challenges.DNS01): + msg += ( + " You may need to use an authenticator " + "plugin that can do challenges over DNS.") + logger.critical(msg) + return errors.AuthorizationError(msg) + + +def _generate_failed_chall_msg(failed_achalls: list[achallenges.AnnotatedChallenge]) -> str: + """Creates a user friendly error message about failed challenges. + + :param list failed_achalls: A list of failed + :class:`certbot.achallenges.AnnotatedChallenge` with the same error + type. + :returns: A formatted error message for the client. + :rtype: str + + """ + error = failed_achalls[0].error + typ = error.typ + if messages.is_acme_error(error): + typ = error.code + msg = [] + + for achall in failed_achalls: + msg.append("\n Identifier: %s\n Type: %s\n Detail: %s\n" % ( + achall.identifier.value, typ, achall.error.detail)) + + return "".join(msg) diff --git a/certbot/src/certbot/_internal/cert_manager.py b/certbot/src/certbot/_internal/cert_manager.py new file mode 100644 index 00000000000..05f80d917fb --- /dev/null +++ b/certbot/src/certbot/_internal/cert_manager.py @@ -0,0 +1,402 @@ +"""Tools for managing certificates.""" +import datetime +import logging +import re +import traceback +from typing import Any +from typing import Callable +from typing import Iterable +from typing import Optional +from typing import TypeVar +from typing import Union + +from certbot import configuration +from certbot import crypto_util +from certbot import errors +from certbot import util +from certbot._internal import ocsp +from certbot._internal import san +from certbot._internal import storage +from certbot.compat import os +from certbot.display import util as display_util + +logger = logging.getLogger(__name__) + +################### +# Commands +################### + + +def certificates(config: configuration.NamespaceConfig) -> None: + """Display information about certs configured with Certbot + + :param config: Configuration. + :type config: :class:`certbot._internal.configuration.NamespaceConfig` + """ + parsed_certs = [] + parse_failures = [] + for renewal_file in storage.renewal_conf_files(config): + try: + renewal_candidate = storage.RenewableCert(renewal_file, config) + crypto_util.verify_renewable_cert(renewal_candidate) + parsed_certs.append(renewal_candidate) + except Exception as e: # pylint: disable=broad-except + logger.warning("Renewal configuration file %s produced an " + "unexpected error: %s. Skipping.", renewal_file, e) + logger.debug("Traceback was:\n%s", traceback.format_exc()) + parse_failures.append(renewal_file) + + # Describe all the certs + _describe_certs(config, parsed_certs, parse_failures) + + +def delete(config: configuration.NamespaceConfig) -> None: + """Delete Certbot files associated with a certificate lineage.""" + certnames = get_certnames(config, "delete", allow_multiple=True) + msg = ["The following certificate(s) are selected for deletion:\n"] + for certname in certnames: + msg.append(" * " + certname) + msg.append( + "\nWARNING: Before continuing, ensure that the listed certificates are not being used " + "by any installed server software (e.g. Apache, nginx, mail servers). Deleting a " + "certificate that is still being used will cause the server software to stop working. " + "See https://certbot.org/deleting-certs for information on deleting certificates safely." + ) + msg.append("\nAre you sure you want to delete the above certificate(s)?") + if not display_util.yesno("\n".join(msg), default=True): + logger.info("Deletion of certificate(s) canceled.") + return + for certname in certnames: + storage.delete_files(config, certname) + display_util.notify("Deleted all files relating to certificate {0}." + .format(certname)) + +################### +# Public Helpers +################### + + +def lineage_for_certname(cli_config: configuration.NamespaceConfig, + certname: str) -> Optional[storage.RenewableCert]: + """Find a lineage object with name certname.""" + configs_dir = cli_config.renewal_configs_dir + # Verify the directory is there + util.make_or_verify_dir(configs_dir, mode=0o755) + try: + renewal_file = storage.renewal_file_for_certname(cli_config, certname) + except errors.CertStorageError: + return None + try: + return storage.RenewableCert(renewal_file, cli_config) + except (OSError, errors.CertStorageError): + logger.debug("Renewal conf file %s is broken.", renewal_file) + logger.debug("Traceback was:\n%s", traceback.format_exc()) + return None + + +def sans_for_certname(config: configuration.NamespaceConfig, + certname: str) -> Optional[list[san.SAN]]: + """Find the domains and/or IP addresses in the cert with name certname.""" + lineage = lineage_for_certname(config, certname) + return lineage.sans() if lineage else None + + +def find_duplicative_certs(config: configuration.NamespaceConfig, + sans: list[san.SAN]) -> tuple[Optional[storage.RenewableCert], + Optional[storage.RenewableCert]]: + """Find existing certs that match the given domain names. + + This function searches for certificates whose domains are equal to + the `domains` parameter and certificates whose domains are a subset + of the domains in the `domains` parameter. If multiple certificates + are found whose names are a subset of `domains`, the one whose names + are the largest subset of `domains` is returned. + + If multiple certificates' domains are an exact match or equally + sized subsets, which matching certificates are returned is + undefined. + + :param config: Configuration. + :type config: :class:`certbot._internal.configuration.NamespaceConfig` + :param domains: List of domain names + :type domains: `list` of `str` + + :returns: lineages representing the identically matching cert and the + largest subset if they exist + :rtype: `tuple` of `storage.RenewableCert` or `None` + + """ + def update_certs_for_domain_matches(candidate_lineage: storage.RenewableCert, + rv: tuple[Optional[storage.RenewableCert], + Optional[storage.RenewableCert]] + ) -> tuple[Optional[storage.RenewableCert], + Optional[storage.RenewableCert]]: + """Return cert as identical_names_cert if it matches, + or subset_names_cert if it matches as subset + """ + # TODO: Handle these differently depending on whether they are + # expired or still valid? + identical_names_cert, subset_names_cert = rv + candidate_names = set(candidate_lineage.sans()) + if candidate_names == set(sans): + identical_names_cert = candidate_lineage + elif candidate_names.issubset(set(sans)): + # This logic finds and returns the largest subset-names cert + # in the case where there are several available. + if subset_names_cert is None: + subset_names_cert = candidate_lineage + elif len(candidate_names) > len(subset_names_cert.sans()): + subset_names_cert = candidate_lineage + return (identical_names_cert, subset_names_cert) + + init: tuple[Optional[storage.RenewableCert], Optional[storage.RenewableCert]] = (None, None) + + return _search_lineages(config, update_certs_for_domain_matches, init) + + +def _archive_files(candidate_lineage: storage.RenewableCert, filetype: str) -> Optional[list[str]]: + """ In order to match things like: + /etc/letsencrypt/archive/example.com/chain1.pem. + + Anonymous functions which call this function are eventually passed (in a list) to + `match_and_check_overlaps` to help specify the acceptable_matches. + + :param `.storage.RenewableCert` candidate_lineage: Lineage whose archive dir is to + be searched. + :param str filetype: main file name prefix e.g. "fullchain" or "chain". + + :returns: Files in candidate_lineage's archive dir that match the provided filetype. + :rtype: list of str or None + """ + archive_dir = candidate_lineage.archive_dir + pattern = [os.path.join(archive_dir, f) for f in os.listdir(archive_dir) + if re.match("{0}[0-9]*.pem".format(filetype), f)] + if pattern: + return pattern + return None + + +def _acceptable_matches() -> list[Union[Callable[[storage.RenewableCert], str], + Callable[[storage.RenewableCert], Optional[list[str]]]]]: + """ Generates the list that's passed to match_and_check_overlaps. Is its own function to + make unit testing easier. + + :returns: list of functions + :rtype: list + """ + return [lambda x: x.fullchain_path, lambda x: x.cert_path, + lambda x: _archive_files(x, "cert"), lambda x: _archive_files(x, "fullchain")] + + +def cert_path_to_lineage(cli_config: configuration.NamespaceConfig) -> str: + """ If config.cert_path is defined, try to find an appropriate value for config.certname. + + :param `configuration.NamespaceConfig` cli_config: parsed command line arguments + + :returns: a lineage name + :rtype: str + + :raises `errors.Error`: If the specified cert path can't be matched to a lineage name. + :raises `errors.OverlappingMatchFound`: If the matched lineage's archive is shared. + """ + acceptable_matches = _acceptable_matches() + match = match_and_check_overlaps(cli_config, acceptable_matches, + lambda x: cli_config.cert_path, lambda x: x.lineagename) + return match[0] + + +def match_and_check_overlaps(cli_config: configuration.NamespaceConfig, + acceptable_matches: Iterable[Union[ + Callable[[storage.RenewableCert], str], + Callable[[storage.RenewableCert], Optional[list[str]]]]], + match_func: Callable[[storage.RenewableCert], str], + rv_func: Callable[[storage.RenewableCert], str]) -> list[str]: + """ Searches through all lineages for a match, and checks for duplicates. + If a duplicate is found, an error is raised, as performing operations on lineages + that have their properties incorrectly duplicated elsewhere is probably a bad idea. + + :param `configuration.NamespaceConfig` cli_config: parsed command line arguments + :param list acceptable_matches: a list of functions that specify acceptable matches + :param function match_func: specifies what to match + :param function rv_func: specifies what to return + + """ + def find_matches(candidate_lineage: storage.RenewableCert, return_value: list[str], + acceptable_matches: Iterable[Union[ + Callable[[storage.RenewableCert], str], + Callable[[storage.RenewableCert], Optional[list[str]]]]]) -> list[str]: + """Returns a list of matches using _search_lineages.""" + acceptable_matches_resolved = [func(candidate_lineage) for func in acceptable_matches] + acceptable_matches_rv: list[str] = [] + for item in acceptable_matches_resolved: + if isinstance(item, list): + acceptable_matches_rv += item + elif item: + acceptable_matches_rv.append(item) + match = match_func(candidate_lineage) + if match in acceptable_matches_rv: + return_value.append(rv_func(candidate_lineage)) + return return_value + + matched: list[str] = _search_lineages(cli_config, find_matches, [], acceptable_matches) + if not matched: + raise errors.Error(f"No match found for cert-path {cli_config.cert_path}!") + elif len(matched) > 1: + raise errors.OverlappingMatchFound() + return matched + + +def human_readable_cert_info(config: configuration.NamespaceConfig, cert: storage.RenewableCert, + skip_filter_checks: bool = False) -> Optional[str]: + """ Returns a human readable description of info about a RenewableCert object""" + certinfo = [] + checker = ocsp.RevocationChecker() + + config_sans = set(config.domains + config.ip_addresses) + + if config.certname and cert.lineagename != config.certname and not skip_filter_checks: + return None + if config_sans and not config_sans.issubset(cert.sans()): + return None + now = datetime.datetime.now(datetime.timezone.utc) + + reasons = [] + if cert.is_test_cert: + reasons.append('TEST_CERT') + if cert.target_expiry <= now: + reasons.append('EXPIRED') + elif checker.ocsp_revoked(cert): + reasons.append('REVOKED') + + if reasons: + status = "INVALID: " + ", ".join(reasons) + else: + diff = cert.target_expiry - now + if diff.days == 1: + status = "VALID: 1 day" + elif diff.days < 1: + status = f"VALID: {diff.seconds // 3600} hour(s)" + else: + status = f"VALID: {diff.days} days" + + valid_string = "{0} ({1})".format(cert.target_expiry, status) + serial = format(crypto_util.get_serial_from_cert(cert.cert_path), 'x') + certinfo.append(f" Certificate Name: {cert.lineagename}\n" + f" Serial Number: {serial}\n" + f" Key Type: {cert.private_key_type}\n" + f' Identifiers: {" ".join(map(str, cert.sans()))}\n' + f" Expiry Date: {valid_string}\n" + f" Certificate Path: {cert.fullchain}\n" + f" Private Key Path: {cert.privkey}") + return "".join(certinfo) + + +def get_certnames(config: configuration.NamespaceConfig, verb: str, allow_multiple: bool = False, + custom_prompt: Optional[str] = None) -> list[str]: + """Get certname from flag, interactively, or error out.""" + certname = config.certname + if certname: + certnames = [certname] + else: + filenames = storage.renewal_conf_files(config) + choices = [storage.lineagename_for_filename(name) for name in filenames] + if not choices: + raise errors.Error("No existing certificates found.") + if allow_multiple: + if not custom_prompt: + prompt = "Which certificate(s) would you like to {0}?".format(verb) + else: + prompt = custom_prompt + code, certnames = display_util.checklist( + prompt, choices, cli_flag="--cert-name", force_interactive=True) + if code != display_util.OK: + raise errors.Error("User ended interaction.") + else: + if not custom_prompt: + prompt = "Which certificate would you like to {0}?".format(verb) + else: + prompt = custom_prompt + + code, index = display_util.menu( + prompt, choices, cli_flag="--cert-name", force_interactive=True) + + if code != display_util.OK or index not in range(0, len(choices)): + raise errors.Error("User ended interaction.") + certnames = [choices[index]] + return certnames + +################### +# Private Helpers +################### + + +def _report_lines(msgs: Iterable[str]) -> str: + """Format a results report for a category of single-line renewal outcomes""" + return " " + "\n ".join(str(msg) for msg in msgs) + + +def _report_human_readable(config: configuration.NamespaceConfig, + parsed_certs: Iterable[storage.RenewableCert]) -> str: + """Format a results report for a parsed cert""" + certinfo = [] + for cert in parsed_certs: + cert_info = human_readable_cert_info(config, cert) + if cert_info is not None: + certinfo.append(cert_info) + return "\n".join(certinfo) + + +def _describe_certs(config: configuration.NamespaceConfig, + parsed_certs: Iterable[storage.RenewableCert], + parse_failures: Iterable[str]) -> None: + """Print information about the certs we know about""" + out: list[str] = [] + + notify = out.append + + if not parsed_certs and not parse_failures: + notify("No certificates found.") + else: + if parsed_certs: + if config.certname or config.domains or config.ip_addresses: + notify("Found the following matching certs:") + else: + notify("Found the following certs:") + notify(_report_human_readable(config, parsed_certs)) + if parse_failures: + notify("\nThe following renewal configurations " + "were invalid:") + notify(_report_lines(parse_failures)) + + display_util.notification("\n".join(out), pause=False, wrap=False) + + +T = TypeVar('T') + +def _search_lineages(cli_config: configuration.NamespaceConfig, func: Callable[..., T], + initial_rv: T, *args: Any) -> T: + """Iterate func over unbroken lineages, allowing custom return conditions. + + Allows flexible customization of return values, including multiple + return values and complex checks. + + :param `configuration.NamespaceConfig` cli_config: parsed command line arguments + :param function func: function used while searching over lineages + :param initial_rv: initial return value of the function (any type) + + :returns: Whatever was specified by `func` if a match is found. + """ + configs_dir = cli_config.renewal_configs_dir + # Verify the directory is there + util.make_or_verify_dir(configs_dir, mode=0o755) + + rv = initial_rv + for renewal_file in storage.renewal_conf_files(cli_config): + try: + candidate_lineage = storage.RenewableCert(renewal_file, cli_config) + except (OSError, errors.CertStorageError): + logger.debug("Renewal conf file %s is broken. Skipping.", renewal_file) + logger.debug("Traceback was:\n%s", traceback.format_exc()) + continue + rv = func(candidate_lineage, rv, *args) + return rv diff --git a/certbot/src/certbot/_internal/cli/__init__.py b/certbot/src/certbot/_internal/cli/__init__.py new file mode 100644 index 00000000000..67932f5268d --- /dev/null +++ b/certbot/src/certbot/_internal/cli/__init__.py @@ -0,0 +1,509 @@ +"""Certbot command line argument & config processing.""" +# pylint: disable=too-many-lines +# ruff: noqa: F401 +import argparse +import logging +import logging.handlers +import sys +from typing import Any +from typing import List +from typing import Optional +from typing import Type + +import certbot +from certbot.configuration import NamespaceConfig +from certbot._internal import constants +from certbot._internal.cli.cli_constants import ARGPARSE_PARAMS_TO_REMOVE +from certbot._internal.cli.cli_constants import cli_command +from certbot._internal.cli.cli_constants import COMMAND_OVERVIEW +from certbot._internal.cli.cli_constants import DEPRECATED_OPTIONS +from certbot._internal.cli.cli_constants import EXIT_ACTIONS +from certbot._internal.cli.cli_constants import HELP_AND_VERSION_USAGE +from certbot._internal.cli.cli_constants import SHORT_USAGE +from certbot._internal.cli.cli_constants import VAR_MODIFIERS +from certbot._internal.cli.cli_constants import ZERO_ARG_ACTIONS +from certbot._internal.cli.cli_utils import _EncodeReasonAction +from certbot._internal.cli.cli_utils import _PrefChallAction +from certbot._internal.cli.cli_utils import _user_agent_comment_type +from certbot._internal.cli.cli_utils import add_dns_name +from certbot._internal.cli.cli_utils import add_ip_address +from certbot._internal.cli.cli_utils import CaseInsensitiveList +from certbot._internal.cli.cli_utils import config_help +from certbot._internal.cli.cli_utils import CustomHelpFormatter +from certbot._internal.cli.cli_utils import DomainsAction +from certbot._internal.cli.cli_utils import IPAddressAction +from certbot._internal.cli.cli_utils import flag_default +from certbot._internal.cli.cli_utils import HelpfulArgumentGroup +from certbot._internal.cli.cli_utils import nonnegative_int +from certbot._internal.cli.cli_utils import parse_preferred_challenges +from certbot._internal.cli.cli_utils import read_file +from certbot._internal.cli.cli_utils import set_test_server_options +from certbot._internal.cli.group_adder import _add_all_groups +from certbot._internal.cli.helpful import HelpfulArgumentParser +from certbot._internal.cli.paths_parser import _paths_parser +from certbot._internal.cli.plugins_parsing import _plugins_parsing +from certbot._internal.cli.subparsers import _create_subparsers +from certbot._internal.cli.verb_help import VERB_HELP +from certbot._internal.cli.verb_help import VERB_HELP_MAP +from certbot._internal.plugins import disco as plugins_disco +import certbot._internal.plugins.selection as plugin_selection +from certbot.plugins import enhancements + +logger = logging.getLogger(__name__) + + +# Global, to save us from a lot of argument passing within the scope of this module +helpful_parser: Optional[HelpfulArgumentParser] = None + + +def prepare_and_parse_args(plugins: plugins_disco.PluginsRegistry, args: list[str] + ) -> NamespaceConfig: + """Returns parsed command line arguments. + + :param .PluginsRegistry plugins: available plugins + :param list args: command line arguments with the program name removed + + :returns: parsed command line arguments + :rtype: configuration.NamespaceConfig + + """ + + helpful = HelpfulArgumentParser(args, plugins) + _add_all_groups(helpful) + + # --help is automatically provided by argparse + helpful.add( + None, "-v", "--verbose", dest="verbose_count", action="count", + default=flag_default("verbose_count"), help="This flag can be used " + "multiple times to incrementally increase the verbosity of output, " + "e.g. -vvv.") + # This is for developers to set the level in the cli.ini, and overrides + # the --verbose flag + helpful.add( + None, "--verbose-level", dest="verbose_level", + default=flag_default("verbose_level"), help=argparse.SUPPRESS) + helpful.add( + None, "-t", "--text", dest="text_mode", action="store_true", + default=flag_default("text_mode"), help=argparse.SUPPRESS) + helpful.add( + None, "--max-log-backups", type=nonnegative_int, + default=flag_default("max_log_backups"), + help="Specifies the maximum number of backup logs that should " + "be kept by Certbot's built in log rotation. Setting this " + "flag to 0 disables log rotation entirely, causing " + "Certbot to always append to the same log file.") + helpful.add( + None, "--preconfigured-renewal", dest="preconfigured_renewal", + action="store_true", default=flag_default("preconfigured_renewal"), + help=argparse.SUPPRESS + ) + helpful.add( + [None, "automation", "run", "certonly", "enhance"], + "-n", "--non-interactive", "--noninteractive", + dest="noninteractive_mode", action="store_true", + default=flag_default("noninteractive_mode"), + help="Run without ever asking for user input. This may require " + "additional command line flags; the client will try to explain " + "which ones are required if it finds one missing") + helpful.add( + [None, "register", "run", "certonly", "enhance"], + constants.FORCE_INTERACTIVE_FLAG, action="store_true", + default=flag_default("force_interactive"), + help="Force Certbot to be interactive even if it detects it's not " + "being run in a terminal. This flag cannot be used with the " + "renew subcommand.") + helpful.add( + [None, "run", "certonly", "certificates", "enhance"], + "-d", "--domains", "--domain", dest="domains", + metavar="DOMAIN", action=DomainsAction, + default=flag_default("domains"), + help="Domain names to include. For multiple domains you can use multiple -d flags " + "or enter a comma separated list of domains as a parameter. All domains will " + "be included as Subject Alternative Names on the certificate. The first domain " + "will be used as the certificate name, unless otherwise specified or if you " + "already have a certificate with the same name. In the case of a name conflict, " + "a number like -0001 will be appended to the certificate name. (default: Ask)") + helpful.add( + [None, "certonly", "certificates"], + "--ip-address", dest="ip_addresses", + action=IPAddressAction, + default=flag_default("ip_addresses"), + help="IP addresses to include. For multiple IP addresses you can use multiple " + "--ip-address flags. All IP addresses will be included as Subject Alternative Names " + "on the certificate.") + helpful.add( + [None, "run", "certonly", "register"], + "--eab-kid", dest="eab_kid", + metavar="EAB_KID", + help="Key Identifier for External Account Binding" + ) + helpful.add( + [None, "run", "certonly", "register"], + "--eab-hmac-key", dest="eab_hmac_key", + metavar="EAB_HMAC_KEY", + help="HMAC key for External Account Binding" + ) + helpful.add( + [None, "run", "certonly", "register"], + "--eab-hmac-alg", dest="eab_hmac_alg", + metavar="EAB_HMAC_ALG", + default=flag_default("eab_hmac_alg"), + help="HMAC algorithm for External Account Binding" + ) + helpful.add( + [None, "run", "certonly", "manage", "delete", "certificates", + "renew", "enhance", "reconfigure"], "--cert-name", dest="certname", + metavar="CERTNAME", default=flag_default("certname"), + help="Certificate name to apply. This name is used by Certbot for housekeeping " + "and in file paths; it doesn't affect the content of the certificate itself. " + "Certificate name cannot contain filepath separators (i.e. '/' or '\\', depending " + "on the platform). " + "To see certificate names, run 'certbot certificates'. " + "When creating a new certificate, specifies the new certificate's name. " + "(default: the first provided domain or the name of an existing " + "certificate on your system for the same domains)") + helpful.add( + [None, "testing", "renew", "certonly"], + "--dry-run", action="store_true", dest="dry_run", + default=flag_default("dry_run"), + help="Perform a test run against the Let's Encrypt staging server, obtaining test" + " (invalid) certificates but not saving them to disk. This can only be used with the" + " 'certonly' and 'renew' subcommands. It may trigger webserver reloads to " + " temporarily modify & roll back configuration files." + " --pre-hook and --post-hook commands run by default." + " --deploy-hook commands do not run, unless enabled by --run-deploy-hooks." + " The test server may be overridden with --server.") + helpful.add( + ["testing", "renew", "certonly", "reconfigure"], + "--run-deploy-hooks", action="store_true", dest="run_deploy_hooks", + default=flag_default("run_deploy_hooks"), + help="When performing a test run using `--dry-run` or `reconfigure`, run any applicable" + " deploy hooks. This includes hooks set on the command line, saved in the" + " certificate's renewal configuration file, or present in the renewal-hooks directory." + " To exclude directory hooks, use --no-directory-hooks. The hook(s) will only" + " be run if the dry run succeeds, and will use the current active certificate, not" + " the temporary test certificate acquired during the dry run. This flag is recommended" + " when modifying the deploy hook using `reconfigure`.") + helpful.add( + ["register", "automation"], "--register-unsafely-without-email", action="store_true", + default=flag_default("register_unsafely_without_email"), + help=argparse.SUPPRESS) + helpful.add( + ["register", "update_account", "unregister", "automation"], "-m", "--email", + default=flag_default("email"), + help=config_help("email")) + helpful.add(["register", "update_account", "automation"], "--eff-email", action="store_true", + default=flag_default("eff_email"), dest="eff_email", + help="Share your e-mail address with EFF (default: Ask)") + helpful.add(["register", "update_account", "automation"], "--no-eff-email", + action="store_false", default=flag_default("eff_email"), dest="eff_email", + help="Don't share your e-mail address with EFF (default: Ask)") + helpful.add( + ["automation", "certonly", "run"], + "--keep-until-expiring", "--keep", "--reinstall", + dest="reinstall", action="store_true", default=flag_default("reinstall"), + help="If the requested certificate matches an existing certificate, always keep the " + "existing one until it is due for renewal (for the " + "'run' subcommand this means reinstall the existing certificate). (default: Ask)") + helpful.add( + "automation", "--expand", action="store_true", default=flag_default("expand"), + help="If an existing certificate is a strict subset of the requested names, " + "always expand and replace it with the additional names. (default: Ask)") + helpful.add( + "automation", "--version", action="version", + version="%(prog)s {0}".format(certbot.__version__), + help="show program's version number and exit") + helpful.add( + ["automation", "renew"], + "--force-renewal", "--renew-by-default", dest="renew_by_default", + action="store_true", default=flag_default("renew_by_default"), + help="If a certificate " + "already exists for the requested domains, renew it now, " + "regardless of whether it is near expiry. (Often " + "--keep-until-expiring is more appropriate). Also implies " + "--expand.") + helpful.add( + "automation", "--renew-with-new-domains", dest="renew_with_new_domains", + action="store_true", default=flag_default("renew_with_new_domains"), + help="If a " + "certificate already exists for the requested certificate name " + "but does not match the requested domains, renew it now, " + "regardless of whether it is near expiry.") + helpful.add( + "automation", "--reuse-key", dest="reuse_key", + action="store_true", default=flag_default("reuse_key"), + help="When renewing, use the same private key as the existing " + "certificate.") + helpful.add( + "automation", "--no-reuse-key", dest="reuse_key", + action="store_false", default=flag_default("reuse_key"), + help="When renewing, do not use the same private key as the existing " + "certificate. Not reusing private keys is the default behavior of " + "Certbot. This option may be used to unset --reuse-key on an " + "existing certificate.") + helpful.add( + "automation", "--new-key", + dest="new_key", action="store_true", default=flag_default("new_key"), + help="When renewing or replacing a certificate, generate a new private key, " + "even if --reuse-key is set on the existing certificate. Combining " + "--new-key and --reuse-key will result in the private key being replaced and " + "then reused in future renewals.") + + helpful.add( + ["automation", "renew", "certonly"], + "--allow-subset-of-names", action="store_true", + default=flag_default("allow_subset_of_names"), + help="When performing domain validation, do not consider it a failure " + "if authorizations can not be obtained for a strict subset of " + "the requested domains. This may be useful for allowing renewals for " + "multiple domains to succeed even if some domains no longer point " + "at this system. This option cannot be used with --csr.") + helpful.add( + "automation", "--agree-tos", dest="tos", action="store_true", + default=flag_default("tos"), + help="Agree to the ACME Subscriber Agreement (default: Ask)") + helpful.add( + ["unregister", "automation"], "--account", metavar="ACCOUNT_ID", + default=flag_default("account"), + help="Account ID to use") + helpful.add( + "automation", "--duplicate", dest="duplicate", action="store_true", + default=flag_default("duplicate"), + help="Allow making a certificate lineage that duplicates an existing one " + "(both can be renewed in parallel)") + helpful.add( + ["automation", "renew", "certonly", "run"], + "-q", "--quiet", dest="quiet", action="store_true", + default=flag_default("quiet"), + help="Silence all output except errors. Useful for automation via cron." + " Implies --non-interactive.") + # overwrites server, handled in HelpfulArgumentParser.parse_args() + helpful.add(["testing", "revoke", "run"], "--test-cert", "--staging", + dest="staging", action="store_true", default=flag_default("staging"), + help="Use the Let's Encrypt staging server to obtain or revoke test (invalid) " + "certificates; equivalent to --server " + constants.STAGING_URI) + helpful.add( + "testing", "--debug", action="store_true", default=flag_default("debug"), + help="Show tracebacks in case of errors") + helpful.add( + [None, "certonly", "run"], "--debug-challenges", action="store_true", + default=flag_default("debug_challenges"), + help="After setting up challenges, wait for user input before " + "submitting to CA. When used in combination with the `-v` " + "option, the challenge URLs or FQDNs and their expected " + "return values are shown.") + helpful.add( + "testing", "--no-verify-ssl", action="store_true", + help=config_help("no_verify_ssl"), + default=flag_default("no_verify_ssl")) + helpful.add( + ["testing", "standalone", "manual"], "--http-01-port", type=int, + dest="http01_port", + default=flag_default("http01_port"), help=config_help("http01_port")) + helpful.add( + ["testing", "standalone"], "--http-01-address", + dest="http01_address", + default=flag_default("http01_address"), help=config_help("http01_address")) + helpful.add( + ["testing", "nginx"], "--https-port", type=int, + default=flag_default("https_port"), + help=config_help("https_port")) + helpful.add( + "testing", "--break-my-certs", action="store_true", + default=flag_default("break_my_certs"), + help="Be willing to replace or renew valid certificates with invalid " + "(testing/staging) certificates") + helpful.add( + "security", "--rsa-key-size", type=int, metavar="N", + default=flag_default("rsa_key_size"), help=config_help("rsa_key_size")) + helpful.add( + "security", "--key-type", choices=['rsa', 'ecdsa'], type=str, + default=flag_default("key_type"), help=config_help("key_type")) + helpful.add( + "security", "--elliptic-curve", type=str, choices=[ + 'secp256r1', + 'secp384r1', + 'secp521r1', + ], metavar="N", + default=flag_default("elliptic_curve"), help=config_help("elliptic_curve")) + helpful.add( + "security", "--must-staple", action="store_true", + dest="must_staple", default=flag_default("must_staple"), + help=config_help("must_staple")) + helpful.add( + ["security", "enhance"], + "--redirect", action="store_true", dest="redirect", + default=flag_default("redirect"), + help="Automatically redirect all HTTP traffic to HTTPS for the newly " + "authenticated vhost. (default: redirect enabled for install and run, " + "disabled for enhance)") + helpful.add( + "security", "--no-redirect", action="store_false", dest="redirect", + default=flag_default("redirect"), + help="Do not automatically redirect all HTTP traffic to HTTPS for the newly " + "authenticated vhost. (default: redirect enabled for install and run, " + "disabled for enhance)") + helpful.add( + ["security", "enhance"], + "--hsts", action="store_true", dest="hsts", default=flag_default("hsts"), + help="Add the Strict-Transport-Security header to every HTTP response." + " Forcing browser to always use SSL for the domain." + " Defends against SSL Stripping. (default: False)") + helpful.add( + "security", "--no-hsts", action="store_false", dest="hsts", + default=flag_default("hsts"), help=argparse.SUPPRESS) + helpful.add( + ["security", "enhance"], + "--uir", action="store_true", dest="uir", default=flag_default("uir"), + help='Add the "Content-Security-Policy: upgrade-insecure-requests"' + ' header to every HTTP response. Forcing the browser to use' + ' https:// for every http:// resource. (default: False)') + helpful.add( + "security", "--no-uir", action="store_false", dest="uir", default=flag_default("uir"), + help=argparse.SUPPRESS) + helpful.add( + "security", "--staple-ocsp", action="store_true", dest="staple", + default=flag_default("staple"), + help="Enables OCSP Stapling. A valid OCSP response is stapled to" + " the certificate that the server offers during TLS. (default: False)") + helpful.add( + "security", "--no-staple-ocsp", action="store_false", dest="staple", + default=flag_default("staple"), help=argparse.SUPPRESS) + helpful.add( + "security", "--strict-permissions", action="store_true", + default=flag_default("strict_permissions"), + help="Require that all configuration files are owned by the current " + "user; only needed if your config is somewhere unsafe like /tmp/") + helpful.add( + [None, "certonly", "renew", "run"], + "--required-profile", dest="required_profile", + default=flag_default("required_profile"), help=config_help("required_profile") + ) + helpful.add( + [None, "certonly", "renew", "run"], + "--preferred-profile", dest="preferred_profile", + default=flag_default("preferred_profile"), help=config_help("preferred_profile") + ) + helpful.add( + [None, "certonly", "renew", "run"], + "--preferred-chain", dest="preferred_chain", + default=flag_default("preferred_chain"), help=config_help("preferred_chain") + ) + helpful.add( + ["manual", "standalone", "certonly", "renew"], + "--preferred-challenges", dest="pref_challs", + action=_PrefChallAction, default=flag_default("pref_challs"), + help='A sorted, comma delimited list of the preferred challenge to ' + 'use during authorization with the most preferred challenge ' + 'listed first (Eg, "dns" or "http,dns"). ' + 'Not all plugins support all challenges. See ' + 'https://certbot.eff.org/docs/using.html#plugins for details. ' + 'ACME Challenges are versioned, but if you pick "http" rather ' + 'than "http-01", Certbot will select the latest version ' + 'automatically.') + helpful.add( + [None, "certonly", "run"], "--issuance-timeout", type=nonnegative_int, + dest="issuance_timeout", + default=flag_default("issuance_timeout"), + help=config_help("issuance_timeout")) + helpful.add( + ["renew", "reconfigure"], "--pre-hook", + help="Command to be run in a shell before obtaining any certificates." + " Unless --disable-hook-validation is used, the command’s first word" + " must be the absolute pathname of an executable or one found via the" + " PATH environment variable." + " Intended primarily for renewal, where it can be used to temporarily" + " shut down a webserver that might conflict with the standalone" + " plugin. This will only be called if a certificate is actually to be" + " obtained/renewed. When renewing several certificates that have" + " identical pre-hooks, only the first will be executed.") + helpful.add( + ["renew", "reconfigure"], "--post-hook", + help="Command to be run in a shell after attempting to obtain/renew" + " certificates." + " Unless --disable-hook-validation is used, the command’s first word" + " must be the absolute pathname of an executable or one found via the" + " PATH environment variable." + " Can be used to deploy renewed certificates, or to" + " restart any servers that were stopped by --pre-hook. This is only" + " run if an attempt was made to obtain/renew a certificate. If" + " multiple renewed certificates have identical post-hooks, only" + " one will be run.") + helpful.add(["renew", "reconfigure"], "--renew-hook", + dest="deploy_hook", help=argparse.SUPPRESS) + helpful.add( + "renew", "--no-random-sleep-on-renew", action="store_false", + default=flag_default("random_sleep_on_renew"), dest="random_sleep_on_renew", + help=argparse.SUPPRESS) + helpful.add( + ["certonly", "renew", "reconfigure", "run"], "--deploy-hook", + help='Command to be run in a shell once for each successfully' + ' issued certificate, including on subsequent renewals.' + ' Unless --disable-hook-validation is used, the command’s first word' + ' must be the absolute pathname of an executable or one found via the' + ' PATH environment variable.' + ' For this command, the shell variable' + ' $RENEWED_LINEAGE will point to the config live subdirectory' + ' (for example, "/etc/letsencrypt/live/example.com") containing' + ' the new certificates and keys; the shell variable' + ' $RENEWED_DOMAINS will contain a space-delimited list of' + ' renewed certificate domains (for example, "example.com' + ' www.example.com")') + helpful.add( + "renew", "--disable-hook-validation", + action="store_false", dest="validate_hooks", + default=flag_default("validate_hooks"), + help="Ordinarily the commands specified for" + " --pre-hook/--post-hook/--deploy-hook will be checked for" + " validity, to see if the programs being run are in the $PATH," + " so that mistakes can be caught early, even when the hooks" + " aren't being run just yet. The validation is rather" + " simplistic and fails if you use more advanced shell" + " constructs, so you can use this switch to disable it." + " (default: False)") + helpful.add( + "renew", "--no-directory-hooks", action="store_false", + default=flag_default("directory_hooks"), dest="directory_hooks", + help="Disable running executables found in Certbot's hook directories." + " (default: False)") + helpful.add( + "renew", "--disable-renew-updates", action="store_true", + default=flag_default("disable_renew_updates"), dest="disable_renew_updates", + help="Disable automatic updates to your server configuration that" + " would otherwise be done by the selected installer plugin, and triggered" + " when the user executes \"certbot renew\", regardless of if the certificate" + " is renewed. This setting does not apply to important TLS configuration" + " updates.") + helpful.add( + "renew", "--no-autorenew", action="store_false", + default=flag_default("autorenew"), dest="autorenew", + help="Disable auto renewal of certificates. (default: False)") + + # Deprecated arguments + helpful.add_deprecated_argument("--os-packages-only", 0) + helpful.add_deprecated_argument("--no-self-upgrade", 0) + helpful.add_deprecated_argument("--no-bootstrap", 0) + helpful.add_deprecated_argument("--no-permissions-check", 0) + + # Populate the command line parameters for new style enhancements + enhancements.populate_cli(helpful.add) + + _create_subparsers(helpful) + _paths_parser(helpful) + # _plugins_parsing should be the last thing to act upon the main + # parser (--help should display plugin-specific options last) + _plugins_parsing(helpful, plugins) + + global helpful_parser # pylint: disable=global-statement + helpful_parser = helpful + return helpful.parse_args() + + +def argparse_type(variable: Any) -> type: + """Return our argparse type function for a config variable (default: str)""" + # pylint: disable=protected-access + if helpful_parser is not None: + for action in helpful_parser.actions: + if action.type is not None and action.dest == variable: + return action.type + return str diff --git a/certbot/src/certbot/_internal/cli/cli_constants.py b/certbot/src/certbot/_internal/cli/cli_constants.py new file mode 100644 index 00000000000..d490714de4f --- /dev/null +++ b/certbot/src/certbot/_internal/cli/cli_constants.py @@ -0,0 +1,97 @@ +"""Certbot command line constants""" +cli_command = "certbot" + + +# Argparse's help formatting has a lot of unhelpful peculiarities, so we want +# to replace as much of it as we can... + +# This is the stub to include in help generated by argparse +SHORT_USAGE = """ + {0} [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ... + +Certbot can obtain and install HTTPS/TLS/SSL certificates. By default, +it will attempt to use a webserver both for obtaining and installing the +certificate. """.format(cli_command) + +# This section is used for --help and --help all ; it needs information +# about installed plugins to be fully formatted +COMMAND_OVERVIEW = """The most common SUBCOMMANDS and flags are: + +obtain, install, and renew certificates: + (default) run Obtain & install a certificate in your current webserver + certonly Obtain or renew a certificate, but do not install it + renew Renew all previously obtained certificates that are near expiry + enhance Add security enhancements to your existing configuration + -d DOMAINS Comma-separated list of domains to obtain a certificate for + + %s + --standalone Run a standalone webserver for authentication + %s + --webroot Place files in a server's webroot folder for authentication + --manual Obtain certificates interactively, or using shell script hooks + + -n Run non-interactively + --test-cert Obtain a test certificate from a staging server + --dry-run Test "renew" or "certonly" without saving any certificates to disk + +manage certificates: + certificates Display information about certificates you have from Certbot + revoke Revoke a certificate (supply --cert-name or --cert-path) + delete Delete a certificate (supply --cert-name) + reconfigure Update a certificate's configuration (supply --cert-name) + +manage your account: + register Create an ACME account + unregister Deactivate an ACME account + update_account Update an ACME account + show_account Display account details + --agree-tos Agree to the ACME server's Subscriber Agreement + -m EMAIL Email address for important account notifications +""" + +# This is the short help for certbot --help, where we disable argparse +# altogether +HELP_AND_VERSION_USAGE = """ +More detailed help: + + -h, --help [TOPIC] print this message, or detailed help on a topic; + the available TOPICS are: + + all, automation, commands, paths, security, testing, or any of the + subcommands or plugins (certonly, renew, install, register, nginx, + apache, standalone, webroot, etc.) + -h all print a detailed help page including all topics + --version print the version number +""" + +# These argparse parameters should be removed when detecting defaults. +ARGPARSE_PARAMS_TO_REMOVE = ("const", "nargs", "type",) + + +# These sets are used when to help detect options set by the user. +EXIT_ACTIONS = {"help", "version",} + + +ZERO_ARG_ACTIONS = {"store_const", "store_true", + "store_false", "append_const", "count",} + + +# Maps a config option to a set of config options that may have modified it. +# This dictionary is used recursively, so if A modifies B and B modifies C, +# it is determined that C was modified by the user if A was modified. +VAR_MODIFIERS = {"account": {"server",}, + "server": {"dry_run", "staging",}, + "webroot_map": {"webroot_path",}} + +# This is a list of all CLI options that we have ever deprecated. It lets us +# opt out of the default detection, which can interact strangely with option +# deprecation. See https://github.com/certbot/certbot/issues/8540 for more info. +DEPRECATED_OPTIONS = { + "manual_public_ip_logging_ok", + "os_packages_only", + "no_self_upgrade", + "no_bootstrap", + "no_permissions_check", + "dns_route53_propagation_seconds", + "certbot_route53:auth_propagation_seconds" +} diff --git a/certbot/src/certbot/_internal/cli/cli_utils.py b/certbot/src/certbot/_internal/cli/cli_utils.py new file mode 100644 index 00000000000..e545a899613 --- /dev/null +++ b/certbot/src/certbot/_internal/cli/cli_utils.py @@ -0,0 +1,290 @@ +"""Certbot command line util function""" +import argparse +import copy +import glob +import inspect +from typing import Any +from typing import Iterable +from typing import Optional +from typing import Sequence +from typing import TYPE_CHECKING +from typing import Union + +from acme import challenges +from certbot import configuration +from certbot import errors +from certbot._internal import constants +from certbot._internal import san +from certbot.compat import os + +if TYPE_CHECKING: + from certbot._internal.cli import helpful + + +def read_file(filename: str, mode: str = "rb") -> tuple[str, Any]: + """Returns the given file's contents. + + :param str filename: path to file + :param str mode: open mode (see `open`) + + :returns: absolute path of filename and its contents + :rtype: tuple + + :raises argparse.ArgumentTypeError: File does not exist or is not readable. + + """ + try: + filename = os.path.abspath(filename) + with open(filename, mode) as the_file: + contents = the_file.read() + return filename, contents + except OSError as exc: + raise argparse.ArgumentTypeError(exc.strerror) + + +def flag_default(name: str) -> Any: + """Default value for CLI flag.""" + # XXX: this is an internal housekeeping notion of defaults before + # argparse has been set up; it is not accurate for all flags. Call it + # with caution. Plugin defaults are missing, and some things are using + # defaults defined in this file, not in constants.py :( + return copy.deepcopy(constants.CLI_DEFAULTS[name]) + + +def config_help(name: str, hidden: bool = False) -> Optional[str]: + """Extract the help message for a `configuration.NamespaceConfig` property docstring.""" + if hidden: + return argparse.SUPPRESS + return inspect.getdoc(getattr(configuration.NamespaceConfig, name)) + + +class HelpfulArgumentGroup: + """Emulates an argparse group for use with HelpfulArgumentParser. + + This class is used in the add_group method of HelpfulArgumentParser. + Command line arguments can be added to the group, but help + suppression and default detection is applied by + HelpfulArgumentParser when necessary. + + """ + def __init__(self, helpful_arg_parser: "helpful.HelpfulArgumentParser", topic: str) -> None: + self._parser = helpful_arg_parser + self._topic = topic + + def add_argument(self, *args: Any, **kwargs: Any) -> None: + """Add a new command line argument to the argument group.""" + self._parser.add(self._topic, *args, **kwargs) + + +class CustomHelpFormatter(argparse.HelpFormatter): + """This is a clone of ArgumentDefaultsHelpFormatter, with bugfixes. + + In particular we fix https://bugs.python.org/issue28742 + """ + + def _get_help_string(self, action: argparse.Action) -> Optional[str]: + helpstr = action.help + if action.help and '%(default)' not in action.help and '(default:' not in action.help: + if action.default != argparse.SUPPRESS: + defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE] + if helpstr and (action.option_strings or action.nargs in defaulting_nargs): + helpstr += ' (default: %(default)s)' + return helpstr + + +class DomainsAction(argparse.Action): + """Action class for parsing domains.""" + + def __call__(self, parser: argparse.ArgumentParser, namespace: argparse.Namespace, + values: str | Sequence[Any] | None, + option_string: str | None = None) -> None: + match values: + case str(): + for domain in values.split(","): + add_dns_name(namespace, san.DNSName(domain.strip())) + case _: + # https://docs.python.org/3/library/argparse.html#nargs + raise TypeError("shouldn't happen: non-str passed by argparse when nargs=None") + + +def add_dns_name(args_or_config: Union[argparse.Namespace, configuration.NamespaceConfig], + dns_name: san.DNSName) -> None: + """Registers a new domain to be used during the current client run. + + The domain is not added if it has already been registered. + + :param args_or_config: parsed command line arguments + :type args_or_config: argparse.Namespace or + configuration.NamespaceConfig + :param san.DNSName dns_name: a DNS name + """ + if dns_name not in args_or_config.domains: + args_or_config.domains.append(dns_name) + + +class IPAddressAction(argparse.Action): + """Action class for parsing IP addresses.""" + + def __call__(self, parser: argparse.ArgumentParser, namespace: argparse.Namespace, + values: str | Sequence[Any] | None, + option_string: Optional[str] = None) -> None: + match values: + case str(): + # This will throw an exception if the IP address doesn't parse. + add_ip_address(namespace, san.IPAddress(values)) + case _: + # https://docs.python.org/3/library/argparse.html#nargs + raise TypeError("shouldn't happen: non-str passed by argparse when nargs=None") + + +def add_ip_address(args_or_config: Union[argparse.Namespace, configuration.NamespaceConfig], + ip_address: san.IPAddress) -> None: + """Registers a new IP address to be used during the current client run. + + The IP address is not added if it has already been registered. + + :param args_or_config: parsed command line arguments + :type args_or_config: argparse.Namespace or + configuration.NamespaceConfig + :param san.IPAddress ip_address: an IP address + """ + if ip_address not in args_or_config.ip_addresses: + args_or_config.ip_addresses.append(ip_address) + +class CaseInsensitiveList(list): + """A list that will ignore case when searching. + + This class is passed to the `choices` argument of `argparse.add_arguments` + through the `helpful` wrapper. It is necessary due to special handling of + command line arguments by `set_by_cli` in which the `type_func` is not applied.""" + def __contains__(self, element: object) -> bool: + if not isinstance(element, str): + return False + return super().__contains__(element.lower()) + + +def _user_agent_comment_type(value: str) -> str: + if "(" in value or ")" in value: + raise argparse.ArgumentTypeError("may not contain parentheses") + return value + + +class _EncodeReasonAction(argparse.Action): + """Action class for parsing revocation reason.""" + + def __call__(self, parser: argparse.ArgumentParser, namespace: argparse.Namespace, + reason: Union[str, Sequence[Any], None], + option_string: Optional[str] = None) -> None: + """Encodes the reason for certificate revocation.""" + if reason is None: + raise ValueError("Unexpected null reason.") + code = constants.REVOCATION_REASONS[str(reason).lower()] + setattr(namespace, self.dest, code) + + +def parse_preferred_challenges(pref_challs: Iterable[str]) -> list[str]: + """Translate and validate preferred challenges. + + :param pref_challs: list of preferred challenge types + :type pref_challs: `list` of `str` + + :returns: validated list of preferred challenge types + :rtype: `list` of `str` + + :raises errors.Error: if pref_challs is invalid + + """ + aliases = {"dns": "dns-01", "http": "http-01"} + challs = [c.strip() for c in pref_challs] + challs = [aliases.get(c, c) for c in challs] + + unrecognized = ", ".join(name for name in challs + if name not in challenges.Challenge.TYPES) + if unrecognized: + raise errors.Error( + "Unrecognized challenges: {0}".format(unrecognized)) + return challs + + +class _PrefChallAction(argparse.Action): + """Action class for parsing preferred challenges.""" + + def __call__(self, parser: argparse.ArgumentParser, namespace: argparse.Namespace, + pref_challs: Union[str, Sequence[Any], None], + option_string: Optional[str] = None) -> None: + if pref_challs is None: + raise ValueError("Unexpected null pref_challs.") + try: + challs = parse_preferred_challenges(str(pref_challs).split(",")) + except errors.Error as error: + raise argparse.ArgumentError(self, str(error)) + namespace.pref_challs.extend(challs) + + +def nonnegative_int(value: str) -> int: + """Converts value to an int and checks that it is not negative. + + This function should used as the type parameter for argparse + arguments. + + :param str value: value provided on the command line + + :returns: integer representation of value + :rtype: int + + :raises argparse.ArgumentTypeError: if value isn't a non-negative integer + + """ + try: + int_value = int(value) + except ValueError: + raise argparse.ArgumentTypeError("value must be an integer") + + if int_value < 0: + raise argparse.ArgumentTypeError("value must be non-negative") + return int_value + +def set_test_server_options(verb: str, config: configuration.NamespaceConfig) -> None: + """Updates server, break_my_certs, staging, tos, and + register_unsafely_without_email in config as necessary to prepare + to use the test server. + + We have --staging/--dry-run; perform sanity check and set config.server + + :param str verb: subcommand called + + :param config: parsed command line arguments + :type config: configuration.NamespaceConfig + + :raises errors.Error: if non-default server is used and --staging is set + :raises errors.Error: if inapplicable verb is used and --dry-run is set + """ + + # Flag combinations should produce these results: + # | --staging | --dry-run | + # ------------------------------------------------------------ + # | --server acme-v02 | Use staging | Use staging | + # | --server acme-staging-v02 | Use staging | Use staging | + # | --server | Conflict error | Use | + + default_servers = (flag_default("server"), constants.STAGING_URI) + + if config.staging and config.server not in default_servers: + raise errors.Error("--server value conflicts with --staging") + + if config.server == flag_default("server"): + config.server = constants.STAGING_URI + # If the account has already been loaded (such as by calling reconstitute before this), + # clear it so that we don't try to use the prod account on the staging server. + config.account = None + + if config.dry_run: + if verb not in ["certonly", "renew", "reconfigure"]: + raise errors.Error("--dry-run currently only works with the " + "'certonly' or 'renew' subcommands (%r)" % verb) + config.break_my_certs = config.staging = True + if glob.glob(os.path.join(config.config_dir, constants.ACCOUNTS_DIR, "*")): + # The user has a prod account, but might not have a staging + # one; we don't want to start trying to perform interactive registration + config.tos = True + config.register_unsafely_without_email = True diff --git a/certbot/src/certbot/_internal/cli/group_adder.py b/certbot/src/certbot/_internal/cli/group_adder.py new file mode 100644 index 00000000000..69cb3438376 --- /dev/null +++ b/certbot/src/certbot/_internal/cli/group_adder.py @@ -0,0 +1,24 @@ +"""This module contains a function to add the groups of arguments for the help +display""" +from typing import TYPE_CHECKING + +from certbot._internal.cli.verb_help import VERB_HELP + +if TYPE_CHECKING: + from certbot._internal.cli import helpful + + +def _add_all_groups(helpful: "helpful.HelpfulArgumentParser") -> None: + helpful.add_group("automation", description="Flags for automating execution & other tweaks") + helpful.add_group("security", description="Security parameters & server settings") + helpful.add_group("testing", + description="The following flags are meant for testing and integration purposes only.") + helpful.add_group("paths", description="Flags for changing execution paths & servers") + helpful.add_group("manage", + description="Various subcommands and flags are available for managing your certificates:", + verbs=["certificates", "delete", "renew", "revoke", "reconfigure"]) + + # VERBS + for verb, docs in VERB_HELP: + name = docs.get("realname", verb) + helpful.add_group(name, description=docs["opts"]) diff --git a/certbot/src/certbot/_internal/cli/helpful.py b/certbot/src/certbot/_internal/cli/helpful.py new file mode 100644 index 00000000000..f496b8536e0 --- /dev/null +++ b/certbot/src/certbot/_internal/cli/helpful.py @@ -0,0 +1,534 @@ +"""Certbot command line argument parser""" + +import argparse +import functools +import sys +from typing import Any +from typing import Iterable +from typing import Optional +from typing import Union + +import configargparse +from cryptography import x509 + +from certbot import crypto_util +from certbot import errors +from certbot import util +from certbot._internal import constants +from certbot._internal import hooks +from certbot._internal import san +from certbot._internal.cli.cli_constants import COMMAND_OVERVIEW +from certbot._internal.cli.cli_constants import HELP_AND_VERSION_USAGE +from certbot._internal.cli.cli_constants import SHORT_USAGE +from certbot._internal.cli.cli_utils import CustomHelpFormatter +from certbot._internal.cli.cli_utils import flag_default +from certbot._internal.cli.cli_utils import HelpfulArgumentGroup +from certbot._internal.cli.cli_utils import set_test_server_options +from certbot._internal.cli.verb_help import VERB_HELP +from certbot._internal.cli.verb_help import VERB_HELP_MAP +from certbot._internal.display import obj as display_obj +from certbot._internal.plugins import disco +from certbot.configuration import ArgumentSource +from certbot.configuration import NamespaceConfig + + +class HelpfulArgumentParser: + """Argparse Wrapper. + + This class wraps argparse, adding the ability to make --help less + verbose, and request help on specific subcategories at a time, eg + 'certbot --help security' for security options. + + """ + def __init__(self, args: list[str], plugins: Iterable[str]) -> None: + from certbot._internal import main + self.VERBS = { + "auth": main.certonly, + "certonly": main.certonly, + "run": main.run, + "install": main.install, + "plugins": main.plugins_cmd, + "register": main.register, + "update_account": main.update_account, + "show_account": main.show_account, + "unregister": main.unregister, + "renew": main.renew, + "revoke": main.revoke, + "rollback": main.rollback, + "everything": main.run, + "certificates": main.certificates, + "delete": main.delete, + "enhance": main.enhance, + "reconfigure": main.reconfigure, + } + + # Get notification function for printing + self.notify = display_obj.NoninteractiveDisplay(sys.stdout).notification + + self.actions: list[configargparse.Action] = [] + + # List of topics for which additional help can be provided + HELP_TOPICS: list[Optional[str]] = ["all", "security", "paths", "automation", "testing"] + HELP_TOPICS += list(self.VERBS) + self.COMMANDS_TOPICS + ["manage"] + + plugin_names: list[Optional[str]] = list(plugins) + self.help_topics: list[Optional[str]] = HELP_TOPICS + plugin_names + [None] + + self.args = args + + if self.args and self.args[0] == 'help': + self.args[0] = '--help' + + self.determine_verb() + help1 = self.prescan_for_flag("-h", self.help_topics) + help2 = self.prescan_for_flag("--help", self.help_topics) + self.help_arg: Union[str, bool] + if isinstance(help1, bool) and isinstance(help2, bool): + self.help_arg = help1 or help2 + else: + self.help_arg = help1 if isinstance(help1, str) else help2 + + short_usage = self._usage_string(plugins, self.help_arg) + + self.visible_topics = self.determine_help_topics(self.help_arg) + + # elements are added by .add_group() + self.groups: dict[str, argparse._ArgumentGroup] = {} + + self.parser = configargparse.ArgParser( + prog="certbot", + usage=short_usage, + formatter_class=CustomHelpFormatter, + args_for_setting_config_path=["-c", "--config"], + default_config_files=flag_default("config_files"), + config_arg_help_message="path to config file (default: {0})".format( + " and ".join(flag_default("config_files")))) + + # This is the only way to turn off overly verbose config flag documentation + self.parser._add_config_file_help = False + + self.verb: str + + # Help that are synonyms for --help subcommands + COMMANDS_TOPICS = ["command", "commands", "subcommand", "subcommands", "verbs"] + + def _list_subcommands(self) -> str: + longest = max(len(v) for v in VERB_HELP_MAP) + + text = "The full list of available SUBCOMMANDS is:\n\n" + for verb, props in sorted(VERB_HELP): + doc = props.get("short", "") + text += '{0:<{length}} {1}\n'.format(verb, doc, length=longest) + + text += "\nYou can get more help on a specific subcommand with --help SUBCOMMAND\n" + return text + + def _usage_string(self, plugins: Iterable[str], help_arg: Union[str, bool]) -> str: + """Make usage strings late so that plugins can be initialised late + + :param plugins: all discovered plugins + :param help_arg: False for none; True for --help; "TOPIC" for --help TOPIC + :rtype: str + :returns: a short usage string for the top of --help TOPIC) + """ + if "nginx" in plugins: + nginx_doc = "--nginx Use the Nginx plugin for authentication & installation" + else: + nginx_doc = "(the certbot nginx plugin is not installed)" + if "apache" in plugins: + apache_doc = "--apache Use the Apache plugin for authentication & installation" + else: + apache_doc = "(the certbot apache plugin is not installed)" + + usage = SHORT_USAGE + if help_arg is True: + self.notify(usage + COMMAND_OVERVIEW % (apache_doc, nginx_doc) + HELP_AND_VERSION_USAGE) + sys.exit(0) + elif help_arg in self.COMMANDS_TOPICS: + self.notify(usage + self._list_subcommands()) + sys.exit(0) + elif help_arg == "all": + # if we're doing --help all, the OVERVIEW is part of the SHORT_USAGE at + # the top; if we're doing --help someothertopic, it's OT so it's not + usage += COMMAND_OVERVIEW % (apache_doc, nginx_doc) + elif isinstance(help_arg, str): + custom = VERB_HELP_MAP.get(help_arg, {}).get("usage", None) + usage = custom if custom else usage + # Only remaining case is help_arg == False, which gives effectively usage == SHORT_USAGE. + + return usage + + def remove_config_file_domains_for_renewal(self, config: NamespaceConfig) -> None: + """Make "certbot renew" safe if domains are set in cli.ini.""" + # Works around https://github.com/certbot/certbot/issues/4096 + assert config.argument_sources is not None + if (config.argument_sources['domains'] == ArgumentSource.CONFIG_FILE and + self.verb == "renew"): + config.domains = [] + + def _build_sources_dict(self) -> dict[str, ArgumentSource]: + # ConfigArgparse's get_source_to_settings_dict doesn't actually create + # default entries for each argument with a default value, omitting many + # args we'd otherwise care about. So in general, unless an argument was + # specified in a config file/environment variable/command line arg, + # consider it as having a "default" value + result = { action.dest: ArgumentSource.DEFAULT for action in self.actions } + + source_to_settings_dict: dict[str, dict[str, tuple[configargparse.Action, str]]] + source_to_settings_dict = self.parser.get_source_to_settings_dict() + + # We'll process the sources dict in order of each source's "priority", + # i.e. the order in which ConfigArgparse ultimately sets argument + # values: + # 1. defaults (`result` already has everything marked as such) + # 2. config files + # 3. env vars (shouldn't be any) + # 4. command line + + def update_result(settings_dict: dict[str, tuple[configargparse.Action, str]], + source: ArgumentSource) -> None: + actions = [self._find_action_for_arg(arg) if action is None else action + for arg, (action, _) in settings_dict.items()] + result.update({ action.dest: source for action in actions }) + + # config file sources look like "config_file|" + for source_key in source_to_settings_dict: + if source_key.startswith('config_file'): + update_result(source_to_settings_dict[source_key], ArgumentSource.CONFIG_FILE) + + update_result(source_to_settings_dict.get('env_var', {}), ArgumentSource.ENV_VAR) + + # The command line settings dict is weird, so handle it separately + if 'command_line' in source_to_settings_dict: + settings_dict: dict[str, tuple[None, list[str]]] + settings_dict = source_to_settings_dict['command_line'] # type: ignore + (_, unprocessed_args) = settings_dict[''] + args = [] + for arg in unprocessed_args: + # ignore non-arguments + if not arg.startswith('-'): + continue + + # special case for config file argument, which we don't have an action for + if arg in ['-c', '--config']: + result['config_dir'] = ArgumentSource.COMMAND_LINE + continue + + if '=' in arg: + arg = arg.split('=')[0] + elif ' ' in arg: + arg = arg.split(' ')[0] + + if arg.startswith('--'): + args.append(arg) + # for short args (ones that start with a single hyphen), handle + # the case of multiple short args together, e.g. "-tvm" + else: + for short_arg in arg[1:]: + args.append(f"-{short_arg}") + + for arg in args: + # find the action corresponding to this arg + action = self._find_action_for_arg(arg) + result[action.dest] = ArgumentSource.COMMAND_LINE + + return result + + def _find_action_for_arg(self, arg: str) -> configargparse.Action: + # Finds a configargparse Action which matches the given arg, where arg + # can either be preceded by hyphens (as on the command line) or not (as + # in config files) + + # if the argument doesn't have leading hyphens, prefix it so it can be + # compared directly w/ action option strings + if arg[0] != '-': + arg = '--' + arg + + # first, check for exact matches + for action in self.actions: + if arg in action.option_strings: + return action + + # now check for abbreviated (i.e. prefix) matches + for action in self.actions: + for option_string in action.option_strings: + if option_string.startswith(arg): + return action + + raise AssertionError(f"Action corresponding to argument {arg} is None") + + def parse_args(self) -> NamespaceConfig: + """Parses command line arguments and returns the result. + + :returns: parsed command line arguments + :rtype: configuration.NamespaceConfig + + """ + parsed_args = self.parser.parse_args(self.args) + parsed_args.func = self.VERBS[self.verb] + parsed_args.verb = self.verb + config = NamespaceConfig(parsed_args) + config.set_argument_sources(self._build_sources_dict()) + + self.remove_config_file_domains_for_renewal(config) + + # Do any post-parsing homework here + + if self.verb == "renew": + if config.force_interactive: + raise errors.Error( + "{0} cannot be used with renew".format( + constants.FORCE_INTERACTIVE_FLAG)) + config.noninteractive_mode = True + + if config.force_interactive and config.noninteractive_mode: + raise errors.Error( + "Flag for non-interactive mode and {0} conflict".format( + constants.FORCE_INTERACTIVE_FLAG)) + + if config.staging or config.dry_run: + self.set_test_server(config) + + if config.csr: + self.handle_csr(config) + + if config.must_staple and not config.staple: + config.staple = True + + if config.validate_hooks: + hooks.validate_hooks(config) + + if config.allow_subset_of_names: + if any(d.is_wildcard() for d in config.domains): + raise errors.Error("Using --allow-subset-of-names with a" + " wildcard domain is not supported.") + + if config.hsts and config.auto_hsts: + raise errors.Error( + "Parameters --hsts and --auto-hsts cannot be used simultaneously.") + + if isinstance(config.key_type, list) and len(config.key_type) > 1: + raise errors.Error( + "Only *one* --key-type type may be provided at this time.") + + return config + + def set_test_server(self, config: NamespaceConfig) -> None: + """Updates server, break_my_certs, staging, tos, and + register_unsafely_without_email in config as necessary to prepare + to use the test server.""" + return set_test_server_options(self.verb, config) + + def handle_csr(self, config: NamespaceConfig) -> None: + """Process a --csr flag.""" + if config.verb != "certonly": + raise errors.Error("Currently, a CSR file may only be specified " + "when obtaining a new or replacement " + "via the certonly command. Please try the " + "certonly command instead.") + if config.allow_subset_of_names: + raise errors.Error("--allow-subset-of-names cannot be used with --csr") + + csrfile, contents = config.csr[0:2] + util_csr = crypto_util.read_csr_file(csrfile, contents) + x509_req = x509.load_pem_x509_csr(util_csr.data) + domains, ip_addresses = san.from_x509(x509_req.subject, x509_req.extensions) + + # The SANs from the CSR are added to the domains from command line flags as this config + # setting is where main.certonly gets the list of identifiers to request. + config.domains.extend(domains) + + if not domains + ip_addresses: + # TODO: add CN to domains instead: + raise errors.Error( + f"Unfortunately, your CSR {config.csr[0]} needs to have a SubjectAltName for " + + "every domain or IP address") + + config.actual_csr = util_csr + + # Check that the original values for --domain and --ip-address set by the user were + # a subset of the domains listed in the CSR. + if set(config.domains) != set(domains): + raise errors.ConfigurationError( + "Inconsistent requests:\nFrom the CSR: {0}\nFrom command line/config: {1}" + .format(san.display(san.join(domains, ip_addresses)), + san.display(config.domains))) + + + def determine_verb(self) -> None: + """Determines the verb/subcommand provided by the user. + + This function works around some of the limitations of argparse. + + """ + if "-h" in self.args or "--help" in self.args: + # all verbs double as help arguments; don't get them confused + self.verb = "help" + return + + for i, token in enumerate(self.args): + if token in self.VERBS: + verb = token + if verb == "auth": + verb = "certonly" + if verb == "everything": + verb = "run" + self.verb = verb + self.args.pop(i) + return + + self.verb = "run" + + def prescan_for_flag(self, flag: str, possible_arguments: Iterable[Optional[str]] + ) -> Union[str, bool]: + """Checks cli input for flags. + + Check for a flag, which accepts a fixed set of possible arguments, in + the command line; we will use this information to configure argparse's + help correctly. Return the flag's argument, if it has one that matches + the sequence @possible_arguments; otherwise return whether the flag is + present. + + """ + if flag not in self.args: + return False + pos = self.args.index(flag) + try: + nxt = self.args[pos + 1] + if nxt in possible_arguments: + return nxt + except IndexError: + pass + return True + + def add(self, topics: Optional[Union[list[Optional[str]], str]], *args: Any, + **kwargs: Any) -> None: + """Add a new command line argument. + + :param topics: str or [str] help topic(s) this should be listed under, + or None for options that don't fit under a specific + topic which will only be shown in "--help all" output. + The first entry determines where the flag lives in the + "--help all" output (None -> "optional arguments"). + :param list *args: the names of this argument flag + :param dict **kwargs: various argparse settings for this argument + + """ + self.actions.append(self._add(topics, *args, **kwargs)) + + def _add(self, topics: Optional[Union[list[Optional[str]], str]], *args: Any, + **kwargs: Any) -> configargparse.Action: + action = kwargs.get("action") + if action is util.DeprecatedArgumentAction: + # If the argument is deprecated through + # certbot.util.add_deprecated_argument, it is not shown in the help + # output and any value given to the argument is thrown away during + # argument parsing. Because of this, we handle this case early + # skipping putting the argument in different help topics and + # handling default detection since these actions aren't needed and + # can cause bugs like + # https://github.com/certbot/certbot/issues/8495. + return self.parser.add_argument(*args, **kwargs) + + if isinstance(topics, list): + # if this flag can be listed in multiple sections, try to pick the one + # that the user has asked for help about + topic = self.help_arg if self.help_arg in topics else topics[0] + else: + topic = topics # there's only one + + if not isinstance(topic, bool) and self.visible_topics[topic]: + if topic in self.groups: + group = self.groups[topic] + return group.add_argument(*args, **kwargs) + else: + return self.parser.add_argument(*args, **kwargs) + else: + kwargs["help"] = argparse.SUPPRESS + return self.parser.add_argument(*args, **kwargs) + + def add_deprecated_argument(self, argument_name: str, num_args: int) -> None: + """Adds a deprecated argument with the name argument_name. + + Deprecated arguments are not shown in the help. If they are used + on the command line, a warning is shown stating that the + argument is deprecated and no other action is taken. + + :param str argument_name: Name of deprecated argument. + :param int num_args: Number of arguments the option takes. + + """ + # certbot.util.add_deprecated_argument expects the normal add_argument + # interface provided by argparse. This is what is given including when + # certbot.util.add_deprecated_argument is used by plugins, however, in + # that case the first argument to certbot.util.add_deprecated_argument + # is certbot._internal.cli.HelpfulArgumentGroup.add_argument which + # internally calls the add method of this class. + # + # The difference between the add method of this class and the standard + # argparse add_argument method caused a bug in the past (see + # https://github.com/certbot/certbot/issues/8495) so we use the same + # code path here for consistency and to ensure it works. To do that, we + # wrap the add method in a similar way to + # HelpfulArgumentGroup.add_argument by providing a help topic (which in + # this case is set to None). + add_func = functools.partial(self.add, None) + util.add_deprecated_argument(add_func, argument_name, num_args) + + def add_group(self, topic: str, verbs: Iterable[str] = (), + **kwargs: Any) -> HelpfulArgumentGroup: + """Create a new argument group. + + This method must be called once for every topic, however, calls + to this function are left next to the argument definitions for + clarity. + + :param str topic: Name of the new argument group. + :param str verbs: List of subcommands that should be documented as part of + this help group / topic + + :returns: The new argument group. + :rtype: `HelpfulArgumentGroup` + + """ + if self.visible_topics[topic]: + self.groups[topic] = self.parser.add_argument_group(topic, **kwargs) + if self.help_arg: + for v in verbs: + self.groups[topic].add_argument(v, help=VERB_HELP_MAP[v]["short"]) + return HelpfulArgumentGroup(self, topic) + + def add_plugin_args(self, plugins: disco.PluginsRegistry) -> None: + """ + + Let each of the plugins add its own command line arguments, which + may or may not be displayed as help topics. + + """ + for name, plugin_ep in plugins.items(): + parser_or_group = self.add_group(name, + description=plugin_ep.long_description) + plugin_ep.plugin_cls.inject_parser_options(parser_or_group, name) + + def determine_help_topics(self, chosen_topic: Union[str, bool] + ) -> dict[Optional[str], bool]: + """ + + The user may have requested help on a topic, return a dict of which + topics to display. @chosen_topic has prescan_for_flag's return type + + :returns: dict + + """ + # topics maps each topic to whether it should be documented by + # argparse on the command line + if chosen_topic == "auth": + chosen_topic = "certonly" + if chosen_topic == "everything": + chosen_topic = "run" + if chosen_topic == "all": + # Addition of condition closes #6209 (removal of duplicate route53 option). + return {t: t != 'certbot-route53:auth' for t in self.help_topics} + elif not chosen_topic: + return {t: False for t in self.help_topics} + return {t: t == chosen_topic for t in self.help_topics} diff --git a/certbot/src/certbot/_internal/cli/paths_parser.py b/certbot/src/certbot/_internal/cli/paths_parser.py new file mode 100644 index 00000000000..de199c56874 --- /dev/null +++ b/certbot/src/certbot/_internal/cli/paths_parser.py @@ -0,0 +1,50 @@ +"""This is a module that adds configuration to the argument parser regarding +paths for certificates""" +from typing import TYPE_CHECKING +from typing import Union + +from certbot._internal.cli.cli_utils import config_help +from certbot._internal.cli.cli_utils import flag_default +from certbot.compat import os + +if TYPE_CHECKING: + from certbot._internal.cli import helpful + + +def _paths_parser(helpful: "helpful.HelpfulArgumentParser") -> None: + add = helpful.add + verb: Union[str, bool] = helpful.verb + if verb == "help": + verb = helpful.help_arg + + cpkwargs = { + "type": os.path.abspath, + "help": "Path to where certificate is saved (with certonly --csr), installed " + "from, or revoked" + } + if verb == "certonly": + cpkwargs["default"] = flag_default("auth_cert_path") + add(["paths", "install", "revoke", "certonly", "manage"], "--cert-path", **cpkwargs) + + section = "paths" + if isinstance(verb, str) and verb in ("install", "revoke"): + section = verb + add(section, "--key-path", type=os.path.abspath, + help="Path to private key for certificate installation " + "or revocation (if account key is missing)") + + default_cp = None + if verb == "certonly": + default_cp = flag_default("auth_chain_path") + add(["paths", "install"], "--fullchain-path", default=default_cp, type=os.path.abspath, + help="Accompanying path to a full certificate chain (certificate plus chain).") + add(["paths", "install"], "--chain-path", default=default_cp, type=os.path.abspath, + help="Accompanying path to a certificate chain.") + add("paths", "--config-dir", default=flag_default("config_dir"), + help=config_help("config_dir")) + add("paths", "--work-dir", default=flag_default("work_dir"), + help=config_help("work_dir")) + add("paths", "--logs-dir", default=flag_default("logs_dir"), + help="Logs directory.") + add(["paths", "show_account"], "--server", default=flag_default("server"), + help=config_help("server")) diff --git a/certbot/src/certbot/_internal/cli/plugins_parsing.py b/certbot/src/certbot/_internal/cli/plugins_parsing.py new file mode 100644 index 00000000000..d01d9903ba1 --- /dev/null +++ b/certbot/src/certbot/_internal/cli/plugins_parsing.py @@ -0,0 +1,100 @@ +"""This is a module that handles parsing of plugins for the argument parser""" +from typing import TYPE_CHECKING + +from certbot._internal.cli.cli_utils import flag_default +from certbot._internal.plugins import disco + +if TYPE_CHECKING: + from certbot._internal.cli import helpful + + +def _plugins_parsing(helpful: "helpful.HelpfulArgumentParser", + plugins: disco.PluginsRegistry) -> None: + # It's nuts, but there are two "plugins" topics. Somehow this works + helpful.add_group( + "plugins", description="Plugin Selection: Certbot client supports an " + "extensible plugins architecture. See '%(prog)s plugins' for a " + "list of all installed plugins and their names. You can force " + "a particular plugin by setting options provided below. Running " + "--help will list flags specific to that plugin.") + + helpful.add("plugins", "--configurator", default=flag_default("configurator"), + help="Name of the plugin that is both an authenticator and an installer." + " Should not be used together with --authenticator or --installer. " + "(default: Ask)") + helpful.add(["plugins", "reconfigure"], "-a", "--authenticator", + default=flag_default("authenticator"), help="Authenticator plugin name.") + helpful.add(["plugins", "reconfigure"], "-i", "--installer", default=flag_default("installer"), + help="Installer plugin name (also used to find domains).") + helpful.add(["plugins", "certonly", "run", "install"], + "--apache", action="store_true", default=flag_default("apache"), + help="Obtain and install certificates using Apache") + helpful.add(["plugins", "certonly", "run", "install"], + "--nginx", action="store_true", default=flag_default("nginx"), + help="Obtain and install certificates using Nginx") + helpful.add(["plugins", "certonly"], "--standalone", action="store_true", + default=flag_default("standalone"), + help='Obtain certificates using a "standalone" webserver.') + helpful.add(["plugins", "certonly"], "--manual", action="store_true", + default=flag_default("manual"), + help="Provide laborious manual instructions for obtaining a certificate") + helpful.add(["plugins", "certonly", "reconfigure"], "--webroot", action="store_true", + default=flag_default("webroot"), + help="Obtain certificates by placing files in a webroot directory.") + helpful.add(["plugins", "certonly"], "--dns-cloudflare", action="store_true", + default=flag_default("dns_cloudflare"), + help=("Obtain certificates using a DNS TXT record (if you are " + "using Cloudflare for DNS).")) + helpful.add(["plugins", "certonly"], "--dns-digitalocean", action="store_true", + default=flag_default("dns_digitalocean"), + help=("Obtain certificates using a DNS TXT record (if you are " + "using DigitalOcean for DNS).")) + helpful.add(["plugins", "certonly"], "--dns-dnsimple", action="store_true", + default=flag_default("dns_dnsimple"), + help=("Obtain certificates using a DNS TXT record (if you are " + "using DNSimple for DNS).")) + helpful.add(["plugins", "certonly"], "--dns-dnsmadeeasy", action="store_true", + default=flag_default("dns_dnsmadeeasy"), + help=("Obtain certificates using a DNS TXT record (if you are " + "using DNS Made Easy for DNS).")) + helpful.add(["plugins", "certonly"], "--dns-gehirn", action="store_true", + default=flag_default("dns_gehirn"), + help=("Obtain certificates using a DNS TXT record " + "(if you are using Gehirn Infrastructure Service for DNS).")) + helpful.add(["plugins", "certonly"], "--dns-google", action="store_true", + default=flag_default("dns_google"), + help=("Obtain certificates using a DNS TXT record (if you are " + "using Google Cloud DNS).")) + helpful.add(["plugins", "certonly"], "--dns-linode", action="store_true", + default=flag_default("dns_linode"), + help=("Obtain certificates using a DNS TXT record (if you are " + "using Linode for DNS).")) + helpful.add(["plugins", "certonly"], "--dns-luadns", action="store_true", + default=flag_default("dns_luadns"), + help=("Obtain certificates using a DNS TXT record (if you are " + "using LuaDNS for DNS).")) + helpful.add(["plugins", "certonly"], "--dns-nsone", action="store_true", + default=flag_default("dns_nsone"), + help=("Obtain certificates using a DNS TXT record (if you are " + "using NS1 for DNS).")) + helpful.add(["plugins", "certonly"], "--dns-ovh", action="store_true", + default=flag_default("dns_ovh"), + help=("Obtain certificates using a DNS TXT record (if you are " + "using OVH for DNS).")) + helpful.add(["plugins", "certonly"], "--dns-rfc2136", action="store_true", + default=flag_default("dns_rfc2136"), + help="Obtain certificates using a DNS TXT record (if you are using BIND for DNS).") + helpful.add(["plugins", "certonly"], "--dns-route53", action="store_true", + default=flag_default("dns_route53"), + help=("Obtain certificates using a DNS TXT record (if you are using Route53 for " + "DNS).")) + helpful.add(["plugins", "certonly"], "--dns-sakuracloud", action="store_true", + default=flag_default("dns_sakuracloud"), + help=("Obtain certificates using a DNS TXT record " + "(if you are using Sakura Cloud for DNS).")) + + # things should not be reorder past/pre this comment: + # plugins_group should be displayed in --help before plugin + # specific groups (so that plugins_group.description makes sense) + + helpful.add_plugin_args(plugins) diff --git a/certbot/src/certbot/_internal/cli/subparsers.py b/certbot/src/certbot/_internal/cli/subparsers.py new file mode 100644 index 00000000000..332b89fd5f4 --- /dev/null +++ b/certbot/src/certbot/_internal/cli/subparsers.py @@ -0,0 +1,73 @@ +"""This module creates subparsers for the argument parser""" +from typing import TYPE_CHECKING + +from certbot import interfaces +from certbot._internal import constants +from certbot._internal.cli.cli_utils import _EncodeReasonAction +from certbot._internal.cli.cli_utils import _user_agent_comment_type +from certbot._internal.cli.cli_utils import CaseInsensitiveList +from certbot._internal.cli.cli_utils import flag_default +from certbot._internal.cli.cli_utils import read_file + +if TYPE_CHECKING: + from certbot._internal.cli import helpful + + +def _create_subparsers(helpful: "helpful.HelpfulArgumentParser") -> None: + from certbot._internal.client import sample_user_agent # avoid import loops + helpful.add( + None, "--user-agent", default=flag_default("user_agent"), + help='Set a custom user agent string for the client. User agent strings allow ' + 'the CA to collect high level statistics about success rates by OS, ' + 'plugin and use case, and to know when to deprecate support for past Python ' + "versions and flags. If you wish to hide this information from the Let's " + 'Encrypt server, set this to "". ' + '(default: {0}). The flags encoded in the user agent are: ' + '--duplicate, --force-renew, --allow-subset-of-names, -n, and ' + 'whether any hooks are set.'.format(sample_user_agent())) + helpful.add( + None, "--user-agent-comment", default=flag_default("user_agent_comment"), + type=_user_agent_comment_type, + help="Add a comment to the default user agent string. May be used when repackaging Certbot " + "or calling it from another tool to allow additional statistical data to be collected." + " Ignored if --user-agent is set. (Example: Foo-Wrapper/1.0)") + helpful.add("certonly", + "--csr", default=flag_default("csr"), type=read_file, + help="Path to a Certificate Signing Request (CSR) in DER or PEM format." + " Currently --csr only works with the 'certonly' subcommand.") + helpful.add("revoke", + "--reason", dest="reason", + choices=CaseInsensitiveList(constants.REVOCATION_REASONS.keys()), + action=_EncodeReasonAction, default=flag_default("reason"), + help="Specify reason for revoking certificate. (default: unspecified)") + helpful.add("revoke", + "--delete-after-revoke", action="store_true", + default=flag_default("delete_after_revoke"), + help="Delete certificates after revoking them, along with all previous and later " + "versions of those certificates. (default: Ask)") + helpful.add("revoke", + "--no-delete-after-revoke", action="store_false", + dest="delete_after_revoke", + default=flag_default("delete_after_revoke"), + help="Do not delete certificates after revoking them. This " + "option should be used with caution because the 'renew' " + "subcommand will attempt to renew undeleted revoked " + "certificates. (default: Ask)") + helpful.add("rollback", + "--checkpoints", type=int, metavar="N", + default=flag_default("rollback_checkpoints"), + help="Revert configuration N number of checkpoints.") + helpful.add("plugins", + "--init", action="store_true", default=flag_default("init"), + help="Initialize plugins.") + helpful.add("plugins", + "--prepare", action="store_true", default=flag_default("prepare"), + help="Initialize and prepare plugins.") + helpful.add("plugins", + "--authenticators", action="append_const", dest="ifaces", + default=flag_default("ifaces"), + const=interfaces.Authenticator, help="Limit to authenticator plugins only.") + helpful.add("plugins", + "--installers", action="append_const", dest="ifaces", + default=flag_default("ifaces"), + const=interfaces.Installer, help="Limit to installer plugins only.") diff --git a/certbot/src/certbot/_internal/cli/verb_help.py b/certbot/src/certbot/_internal/cli/verb_help.py new file mode 100644 index 00000000000..f73b2b46a47 --- /dev/null +++ b/certbot/src/certbot/_internal/cli/verb_help.py @@ -0,0 +1,105 @@ +"""This module contain help information for verbs supported by certbot""" +from certbot._internal.cli.cli_constants import SHORT_USAGE + +# The attributes here are: +# short: a string that will be displayed by "certbot -h commands" +# opts: a string that heads the section of flags with which this command is documented, +# both for "certbot -h SUBCOMMAND" and "certbot -h all" +# usage: an optional string that overrides the header of "certbot -h SUBCOMMAND" +VERB_HELP = [ + ("run (default)", { + "short": "Obtain/renew a certificate, and install it", + "opts": "Options for obtaining & installing certificates", + "usage": SHORT_USAGE.replace("[SUBCOMMAND]", ""), + "realname": "run" + }), + ("certonly", { + "short": "Obtain or renew a certificate, but do not install it", + "opts": "Options for modifying how a certificate is obtained", + "usage": ("\n\n certbot certonly [options] [-d DOMAIN] [-d DOMAIN] ...\n\n" + "This command obtains a TLS/SSL certificate without installing it anywhere.") + }), + ("renew", { + "short": "Renew all certificates (or one specified with --cert-name)", + "opts": ("The 'renew' subcommand will attempt to renew any certificates" + " previously obtained if they are close to expiry, and print a" + " summary of the results. By default, 'renew' will reuse the" + " plugins and options used to obtain or most recently renew each" + " certificate. You can test whether future renewals will succeed" + " with `--dry-run`." + " Individual certificates can be renewed with the `--cert-name`" + " option. Hooks are available to run commands" + " before and after renewal; see" + " https://certbot.eff.org/docs/using.html#renewal for more" + " information on these."), + "usage": "\n\n certbot renew [--cert-name CERTNAME] [options]\n\n" + }), + ("certificates", { + "short": "List certificates managed by Certbot", + "opts": "List certificates managed by Certbot", + "usage": ("\n\n certbot certificates [options] ...\n\n" + "Print information about the status of certificates managed by Certbot.") + }), + ("delete", { + "short": "Clean up all files related to a certificate", + "opts": "Options for deleting a certificate", + "usage": "\n\n certbot delete --cert-name CERTNAME\n\n" + }), + ("revoke", { + "short": "Revoke a certificate specified with --cert-path or --cert-name", + "opts": "Options for revocation of certificates", + "usage": "\n\n certbot revoke [--cert-path /path/to/fullchain.pem | " + "--cert-name example.com] [options]\n\n" + }), + ("register", { + "short": "Register for account with Let's Encrypt / other ACME server", + "opts": "Options for account registration", + "usage": "\n\n certbot register --email user@example.com [options]\n\n" + }), + ("update_account", { + "short": "Update existing account with Let's Encrypt / other ACME server", + "opts": "Options for account modification", + "usage": "\n\n certbot update_account --email updated_email@example.com [options]\n\n" + }), + ("unregister", { + "short": "Irrevocably deactivate your account", + "opts": "Options for account deactivation.", + "usage": "\n\n certbot unregister [options]\n\n" + }), + ("install", { + "short": "Install an arbitrary certificate in a server", + "opts": "Options for modifying how a certificate is deployed", + "usage": "\n\n certbot install --cert-path /path/to/fullchain.pem " + " --key-path /path/to/private-key [options]\n\n" + }), + ("rollback", { + "short": "Roll back server conf changes made during certificate installation", + "opts": "Options for rolling back server configuration changes", + "usage": "\n\n certbot rollback --checkpoints 3 [options]\n\n" + }), + ("plugins", { + "short": "List plugins that are installed and available on your system", + "opts": 'Options for the "plugins" subcommand', + "usage": "\n\n certbot plugins [options]\n\n" + }), + ("enhance", { + "short": "Add security enhancements to your existing configuration", + "opts": ("Helps to harden the TLS configuration by adding security enhancements " + "to already existing configuration."), + "usage": "\n\n certbot enhance [options]\n\n" + }), + ("show_account", { + "short": "Show account details from an ACME server", + "opts": 'Options useful for the "show_account" subcommand:', + "usage": "\n\n certbot show_account [options]\n\n" + }), + ("reconfigure", { + "short": "Update renewal configuration for a certificate specified by --cert-name", + "opts": 'Common options that may be updated with the "reconfigure" subcommand:', + "usage": "\n\n certbot reconfigure --cert-name CERTNAME [options]\n\n" + }), +] + + +# VERB_HELP is a list in order to preserve order, but a dict is sometimes useful +VERB_HELP_MAP = dict(VERB_HELP) diff --git a/certbot/src/certbot/_internal/client.py b/certbot/src/certbot/_internal/client.py new file mode 100644 index 00000000000..28054d933b5 --- /dev/null +++ b/certbot/src/certbot/_internal/client.py @@ -0,0 +1,906 @@ +"""Certbot client API.""" +import datetime +import logging +import platform +from typing import Any +from typing import Callable +from typing import cast +from typing import IO +from typing import Optional + +from cryptography import x509 +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key +import josepy as jose +from josepy import ES256 +from josepy import ES384 +from josepy import ES512 +from josepy import RS256 + +from acme import client as acme_client +from acme import crypto_util as acme_crypto_util +from acme import errors as acme_errors +from acme import messages +import certbot +from certbot import configuration +from certbot import crypto_util +from certbot import errors +from certbot import interfaces +from certbot import util +from certbot._internal import account +from certbot._internal import auth_handler +from certbot._internal import cli +from certbot._internal import constants +from certbot._internal import eff +from certbot._internal import error_handler +from certbot._internal import san +from certbot._internal import storage +from certbot._internal.plugins import disco as plugin_disco +from certbot._internal.plugins import selection as plugin_selection +from certbot.compat import os +from certbot.display import ops as display_ops +from certbot.display import util as display_util +from certbot.interfaces import AccountStorage + +logger = logging.getLogger(__name__) + + +def create_acme_client(config: configuration.NamespaceConfig, + key: Optional[jose.JWK] = None, + regr: Optional[messages.RegistrationResource] = None, + server_override: Optional[str] = None, + ) -> acme_client.ClientV2: + """Wrangle ACME client construction""" + alg = RS256 + if key and key.typ == 'EC': + public_key = key.key + if public_key.key_size == 256: + alg = ES256 + elif public_key.key_size == 384: + alg = ES384 + elif public_key.key_size == 521: + alg = ES512 + else: + raise errors.NotSupportedError( + "No matching signing algorithm can be found for the key" + ) + net = acme_client.ClientNetwork(key, alg=alg, account=regr, + verify_ssl=(not config.no_verify_ssl), + user_agent=determine_user_agent(config)) + + server = config.server + if server_override: + server = server_override + directory = acme_client.ClientV2.get_directory(server, net) + return acme_client.ClientV2(directory, net) + + +def determine_user_agent(config: configuration.NamespaceConfig) -> str: + """ + Set a user_agent string in the config based on the choice of plugins. + (this wasn't knowable at construction time) + + :returns: the client's User-Agent string + :rtype: `str` + """ + + # WARNING: To ensure changes are in line with Certbot's privacy + # policy, talk to a core Certbot team member before making any + # changes here. + if config.user_agent is None: + ua = ("CertbotACMEClient/{0} ({1}; {2}{8}) Authenticator/{3} Installer/{4} " + "({5}; flags: {6}) Py/{7}") + if os.environ.get("CERTBOT_DOCS") == "1": + cli_command = "certbot" + os_info = "OS_NAME OS_VERSION" + python_version = "major.minor.patchlevel" + else: + cli_command = cli.cli_command + os_info = util.get_os_info_ua() + python_version = platform.python_version() + ua = ua.format(certbot.__version__, cli_command, os_info, + config.authenticator, config.installer, config.verb, + ua_flags(config), python_version, + "; " + config.user_agent_comment if config.user_agent_comment else "") + else: + ua = config.user_agent + return ua + + +def ua_flags(config: configuration.NamespaceConfig) -> str: + """Turn some very important CLI flags into clues in the user agent.""" + if isinstance(config, DummyConfig): + return "FLAGS" + flags = [] + if config.duplicate: + flags.append("dup") + if config.renew_by_default: + flags.append("frn") + if config.allow_subset_of_names: + flags.append("asn") + if config.noninteractive_mode: + flags.append("n") + hook_names = ("pre", "post", "deploy", "manual_auth", "manual_cleanup") + hooks = [getattr(config, h + "_hook") for h in hook_names] + if any(hooks): + flags.append("hook") + return " ".join(flags) + + +class DummyConfig: + """Shim for computing a sample user agent.""" + def __init__(self) -> None: + self.authenticator = "XXX" + self.installer = "YYY" + self.user_agent = None + self.verb = "SUBCOMMAND" + + def __getattr__(self, name: str) -> Any: + """Any config properties we might have are None.""" + return None + + +def sample_user_agent() -> str: + """Document what this Certbot's user agent string will be like.""" + # DummyConfig is designed to mock certbot.configuration.NamespaceConfig. + # Let mypy accept that. + return determine_user_agent(cast(configuration.NamespaceConfig, DummyConfig())) + + +def register(config: configuration.NamespaceConfig, account_storage: AccountStorage, + tos_cb: Optional[Callable[[str], None]] = None + ) -> tuple[account.Account, acme_client.ClientV2]: + """Register new account with an ACME CA. + + This function takes care of generating fresh private key, + registering the account, optionally accepting CA Terms of Service + and finally saving the account. It should be called prior to + initialization of `Client`, unless account has already been created. + + :param certbot.configuration.NamespaceConfig config: Client configuration. + + :param .AccountStorage account_storage: Account storage where newly + registered account will be saved to. Save happens only after TOS + acceptance step, so any account private keys or + `.RegistrationResource` will not be persisted if `tos_cb` + returns ``False``. + + :param tos_cb: If ACME CA requires the user to accept a Terms of + Service before registering account, client action is + necessary. For example, a CLI tool would prompt the user + acceptance. `tos_cb` must be a callable that should accept + a Term of Service URL as a string, and raise an exception + if the TOS is not accepted by the client. ``tos_cb`` will be + called only if the client action is necessary, i.e. when + ``terms_of_service is not None``. This argument is optional, + if not supplied it will default to automatic acceptance! + + :raises certbot.errors.Error: In case of any client problems, in + particular registration failure, or unaccepted Terms of Service. + :raises acme.errors.Error: In case of any protocol problems. + + :returns: Newly registered and saved account, as well as protocol + API handle (should be used in `Client` initialization). + :rtype: `tuple` of `.Account` and `acme.client.Client` + + """ + # Log non-standard actions, potentially wrong API calls + if account_storage.find_all(): + logger.info("There are already existing accounts for %s", config.server) + + if config.email == "": + config.email = None + # If --dry-run is used, and there is no staging account, create one with no email. + if config.dry_run: + config.email = None + + # Each new registration shall use a fresh new key + rsa_key = generate_private_key( + public_exponent=65537, + key_size=config.rsa_key_size, + backend=default_backend()) + key = jose.JWKRSA(key=jose.ComparableRSAKey(rsa_key)) + acme = create_acme_client(config, key) + # TODO: add phone? + regr = perform_registration(acme, config, tos_cb) + + acc = account.Account(regr, key) + account_storage.save(acc, acme) + + eff.prepare_subscription(config, acc) + + return acc, acme + + +def perform_registration(acme: acme_client.ClientV2, config: configuration.NamespaceConfig, + tos_cb: Optional[Callable[[str], None]]) -> messages.RegistrationResource: + """ + Actually register new account, trying repeatedly if there are email + problems + + :param acme.client.Client acme: ACME client object. + :param certbot.configuration.NamespaceConfig config: Client configuration. + :param Callable tos_cb: a callback to handle Term of Service agreement. + + :returns: Registration Resource. + :rtype: `acme.messages.RegistrationResource` + """ + if not acme.net.key: + raise errors.Error("acme client with no private key cannot register account.") + + eab_credentials_supplied = config.eab_kid and config.eab_hmac_key + eab: Optional[dict[str, Any]] + if eab_credentials_supplied: + account_public_key = acme.net.key.public_key() + eab = messages.ExternalAccountBinding.from_data(account_public_key=account_public_key, + kid=config.eab_kid, + hmac_key=config.eab_hmac_key, + directory=acme.directory, + hmac_alg=config.eab_hmac_alg) + else: + eab = None + + if acme.external_account_required(): + if not eab_credentials_supplied: + msg = ("Server requires external account binding." + " Please use --eab-kid and --eab-hmac-key.") + raise errors.Error(msg) + + tos = acme.directory.meta.terms_of_service + if tos_cb and tos: + tos_cb(tos) + + try: + return acme.new_account(messages.NewRegistration.from_data( + email=config.email, terms_of_service_agreed=True, external_account_binding=eab)) + except messages.Error as e: + if e.code in ("invalidEmail", "invalidContact"): + if config.noninteractive_mode: + msg = (f"The ACME server believes {config.email} is an invalid email address. " + "Please ensure it is a valid email and attempt " + "registration again.") + raise errors.Error(msg) + config.email = display_ops.get_email(invalid=True) + return perform_registration(acme, config, tos_cb) + raise + + +class Client: + """Certbot's client. + + :ivar certbot.configuration.NamespaceConfig config: Client configuration. + :ivar .Account account: Account registered with `register`. + :ivar .AuthHandler auth_handler: Authorizations handler that will + dispatch DV challenges to appropriate authenticators + (providing `.Authenticator` interface). + :ivar .Authenticator auth: Prepared (`.Authenticator.prepare`) + authenticator that can solve ACME challenges. + :ivar .Installer installer: Installer. + :ivar acme.client.ClientV2 acme: Optional ACME client API handle. You might + already have one from `register`. + + """ + + def __init__(self, config: configuration.NamespaceConfig, account_: Optional[account.Account], + auth: Optional[interfaces.Authenticator], + installer: Optional[interfaces.Installer], + acme: Optional[acme_client.ClientV2] = None) -> None: + """Initialize a client.""" + self.config = config + self.account = account_ + self.auth = auth + self.installer = installer + + # Initialize ACME if account is provided + if acme is None and self.account is not None: + acme = create_acme_client(config, self.account.key, self.account.regr) + self.acme = acme + + self.auth_handler: Optional[auth_handler.AuthHandler] + if auth is not None: + self.auth_handler = auth_handler.AuthHandler( + auth, self.acme, self.account, self.config.pref_challs) + else: + self.auth_handler = None + + def obtain_certificate_from_csr(self, csr: util.CSR, + orderr: Optional[messages.OrderResource] = None + ) -> tuple[bytes, bytes]: + """Obtain certificate. + + :param .util.CSR csr: PEM-encoded Certificate Signing + Request. The key used to generate this CSR can be different + than `authkey`. + :param acme.messages.OrderResource orderr: contains authzrs + + :returns: certificate and chain as PEM byte strings + :rtype: tuple + + """ + if self.auth_handler is None: + msg = ("Unable to obtain certificate because authenticator is " + "not set.") + logger.error(msg) + raise errors.Error(msg) + if self.account is None or self.account.regr is None: + raise errors.Error("Please register with the ACME server first.") + if self.acme is None: + raise errors.Error("ACME client is not set.") + + logger.debug("CSR: %s", csr) + + if orderr is None: + orderr = self._get_order_and_authorizations(csr.data, best_effort=False) + + deadline = datetime.datetime.now() + datetime.timedelta( + seconds=self.config.issuance_timeout) + + logger.debug("Will poll for certificate issuance until %s", deadline) + + orderr = self.acme.finalize_order( + orderr, deadline, fetch_alternative_chains=self.config.preferred_chain is not None) + + fullchain = orderr.fullchain_pem + if self.config.preferred_chain and orderr.alternative_fullchains_pem: + fullchain = crypto_util.find_chain_with_issuer( + [fullchain] + orderr.alternative_fullchains_pem, + self.config.preferred_chain, not self.config.dry_run) + cert, chain = crypto_util.cert_and_chain_from_fullchain(fullchain) + return cert.encode(), chain.encode() + + def obtain_certificate(self, sans: list[san.SAN], old_keypath: Optional[str] = None + ) -> tuple[bytes, bytes, util.Key, util.CSR]: + """Obtains a certificate from the ACME server. + + `.register` must be called before `.obtain_certificate` + + :param list sans: domains and/or IP addresses for which to get a certificate. + + :returns: certificate as PEM string, chain as PEM string, + newly generated private key (`.util.Key`), and DER-encoded + Certificate Signing Request (`.util.CSR`). + :rtype: tuple + + """ + # We need to determine the key path, key PEM data, CSR path, + # and CSR PEM data. For a dry run, the paths are None because + # they aren't permanently saved to disk. For a lineage with + # --reuse-key, the key path and PEM data are derived from an + # existing file. + + if old_keypath is not None: + # We've been asked to reuse a specific existing private key. + # Therefore, we'll read it now and not generate a new one in + # either case below. + # + # We read in bytes here because the type of `key.pem` + # created below is also bytes. + with open(old_keypath, "rb") as f: + keypath = old_keypath + keypem = f.read() + key: Optional[util.Key] = util.Key(file=keypath, pem=keypem) + logger.info("Reusing existing private key from %s.", old_keypath) + else: + # The key is set to None here but will be created below. + key = None + + key_size = self.config.rsa_key_size + elliptic_curve = "secp256r1" + + # key-type defaults to a list, but we are only handling 1 currently + if isinstance(self.config.key_type, list): + self.config.key_type = self.config.key_type[0] + if self.config.elliptic_curve and self.config.key_type == 'ecdsa': + elliptic_curve = self.config.elliptic_curve + self.config.auth_chain_path = "./chain-ecdsa.pem" + self.config.auth_cert_path = "./cert-ecdsa.pem" + self.config.key_path = "./key-ecdsa.pem" + elif self.config.rsa_key_size and self.config.key_type.lower() == 'rsa': + key_size = self.config.rsa_key_size + + domains, ip_addresses = san.split(sans) + domains_str = [d.dns_name for d in domains] + ip_addresses_typed = [i.ip_address for i in ip_addresses] + + # Create CSR from names + if self.config.dry_run: + key = key or util.Key( + file=None, + pem=crypto_util.make_key( + bits=key_size, + elliptic_curve=elliptic_curve, + key_type=self.config.key_type, + + ), + ) + csr = util.CSR(file=None, form="pem", + data=acme_crypto_util.make_csr( + key.pem, domains_str, self.config.must_staple, + ipaddrs=ip_addresses_typed)) + else: + key = key or crypto_util.generate_key( + key_size=key_size, + key_dir=None, + key_type=self.config.key_type, + elliptic_curve=elliptic_curve, + strict_permissions=self.config.strict_permissions, + ) + csr = crypto_util.generate_csr( + key, domains_str, None, self.config.must_staple, self.config.strict_permissions, + ipaddrs=ip_addresses_typed) + + try: + orderr = self._get_order_and_authorizations(csr.data, self.config.allow_subset_of_names) + except messages.Error as error: + # Some sans may be rejected during order creation. + # Certbot can retry the operation without the rejected + # sans contained within subproblems. + if self.config.allow_subset_of_names: + successful_sans = self._successful_sans_from_error(error, sans) + if successful_sans != sans and len(successful_sans) != 0: + return self._retry_obtain_certificate(sans, successful_sans, old_keypath) + raise + authzr = orderr.authorizations + auth_ident_values = {a.body.identifier.value for a in authzr} + successful_sans = [s for s in sans if str(s) in auth_ident_values] + + # allow_subset_of_names is currently disabled for wildcard + # certificates. The reason for this and checking allow_subset_of_names + # below is because successful_sans == sans is never true if + # sans contains a wildcard because the ACME spec forbids identifiers + # in authzs from containing a wildcard character. + if self.config.allow_subset_of_names and successful_sans != sans: + return self._retry_obtain_certificate(sans, successful_sans, old_keypath) + else: + try: + cert, chain = self.obtain_certificate_from_csr(csr, orderr) + return cert, chain, key, csr + except messages.Error as error: + # Some sans may be rejected during the very late stage of + # order finalization. Certbot can retry the operation without + # the rejected sans contained within subproblems. + if self.config.allow_subset_of_names: + successful_sans = self._successful_sans_from_error(error, sans) + if successful_sans != sans and len(successful_sans) != 0: + return self._retry_obtain_certificate( + sans, successful_sans, old_keypath) + raise + + def _get_order_and_authorizations(self, csr_pem: bytes, + best_effort: bool) -> messages.OrderResource: + """Request a new order and complete its authorizations. + + :param bytes csr_pem: A CSR in PEM format. + :param bool best_effort: True if failing to complete all + authorizations should not raise an exception + + :returns: order resource containing its completed authorizations + :rtype: acme.messages.OrderResource + + """ + if not self.acme: + raise errors.Error("ACME client is not set.") + + profile = None + available_profiles = self.acme.directory.meta.profiles + preferred_profile = self.config.preferred_profile + if self.config.required_profile is not None: + profile = self.config.required_profile + elif (preferred_profile and available_profiles and + preferred_profile in available_profiles): + profile = preferred_profile + try: + orderr = self.acme.new_order(csr_pem, profile=profile) + except acme_errors.WildcardUnsupportedError: + raise errors.Error("The currently selected ACME CA endpoint does" + " not support issuing wildcard certificates.") + + if not self.auth_handler: + raise errors.Error("No authorization handler has been set.") + + # For a dry run, ensure we have an order with fresh authorizations + if orderr and self.config.dry_run: + deactivated, failed = self.auth_handler.deactivate_valid_authorizations(orderr) + if deactivated: + logger.debug("Recreating order after authz deactivations") + orderr = self.acme.new_order(csr_pem, profile=profile) + if failed: + logger.warning("Certbot was unable to obtain fresh authorizations for every domain" + ". The dry run will continue, but results may not be accurate.") + + authzr = self.auth_handler.handle_authorizations(orderr, self.config, best_effort) + return orderr.update(authorizations=authzr) + + def obtain_and_enroll_certificate(self, sans: list[san.SAN], certname: Optional[str] + ) -> Optional[storage.RenewableCert]: + """Obtain and enroll certificate. + + Get a new certificate for the specified domains and/or IP addresses using the specified + authenticator and installer, and then create a new renewable lineage + containing it. + + :param sans: domains and/or IP addresses to request a certificate for + :type sans: `list` of `san.SAN` + :param certname: requested name of lineage + :type certname: `str` or `None` + + :returns: A new :class:`certbot._internal.storage.RenewableCert` instance + referred to the enrolled cert lineage, or None if doing a successful dry run. + + """ + new_name = self._choose_lineagename(sans, certname) + cert, chain, key, _ = self.obtain_certificate(sans) + + if (self.config.config_dir != constants.CLI_DEFAULTS["config_dir"] or + self.config.work_dir != constants.CLI_DEFAULTS["work_dir"]): + logger.info( + "Non-standard path(s), might not work with crontab installed " + "by your operating system package manager") + + if self.config.dry_run: + logger.debug("Dry run: Skipping creating new lineage for %s", new_name) + return None + return storage.RenewableCert.new_lineage( + new_name, cert, + key.pem, chain, + self.config) + + def _successful_sans_from_error(self, error: messages.Error, sans: list[san.SAN], + ) -> list[san.SAN]: + if error.subproblems is not None: + failed_sans: list[san.SAN] = [] + for problem in error.subproblems: + if not problem.identifier: + continue + match problem.identifier.typ: + case messages.IDENTIFIER_FQDN: + failed_sans.append(san.DNSName(problem.identifier.value)) + case messages.IDENTIFIER_IP: + failed_sans.append(san.IPAddress(problem.identifier.value)) + case _: + raise TypeError(f"invalid identifier type {problem.identifier.typ}") + successful_sans = [x for x in sans if x not in failed_sans] + return successful_sans + return [] + + def _retry_obtain_certificate(self, sans: list[san.SAN], successful_sans: list[san.SAN], + old_keypath: Optional[str] + ) -> tuple[bytes, bytes, util.Key, util.CSR]: + failed_sans = [s for s in sans if s not in successful_sans] + failed_sans_list = san.display(failed_sans) + display_util.notify("Unable to obtain a certificate with every requested " + f"domain. Retrying without: {failed_sans_list}") + return self.obtain_certificate(successful_sans, old_keypath) + + def _choose_lineagename(self, sans: list[san.SAN], certname: Optional[str]) -> str: + """Chooses a name for the new lineage. + + :param sans: domains and/or IP addresses in certificate request + :type sans: `list` of `san.SAN` + :param certname: requested name of lineage + :type certname: `str` or `None` + + :returns: lineage name that should be used + :rtype: str + + :raises errors.Error: If the chosen lineage name is invalid. + + """ + # Remember chosen name for new lineage + lineagename = None + if certname: + lineagename = certname + elif sans[0].is_wildcard(): + # Don't make files and directories starting with *. + lineagename = str(sans[0])[2:] + else: + lineagename = str(sans[0]) + # Verify whether chosen lineage is valid + if self._is_valid_lineagename(lineagename): + return lineagename + else: + raise errors.Error( + "The provided certname cannot be used as a lineage name because it contains " + "an illegal character (i.e. filepath separator)." if certname else + "Cannot use domain name as lineage name because it contains an illegal " + "character (i.e. filepath separator). Specify an explicit lineage name " + "with --cert-name.") + + def _is_valid_lineagename(self, name: str) -> bool: + """Determines whether the provided name is a valid lineagename. A lineagename + is invalid when it contains filepath separators. + + :param name: the lineage name to determine validity for + :type name: `str` + + :returns: Whether the provided string constitutes a valid lineage name. + :rtype: bool + + """ + return os.path.sep not in name + + def save_certificate(self, cert_pem: bytes, chain_pem: bytes, + cert_path: str, chain_path: str, fullchain_path: str + ) -> tuple[str, str, str]: + """Saves the certificate received from the ACME server. + + :param bytes cert_pem: + :param bytes chain_pem: + :param str cert_path: Candidate path to a certificate. + :param str chain_path: Candidate path to a certificate chain. + :param str fullchain_path: Candidate path to a full cert chain. + + :returns: cert_path, chain_path, and fullchain_path as absolute + paths to the actual files + :rtype: `tuple` of `str` + + :raises IOError: If unable to find room to write the cert files + + """ + for path in cert_path, chain_path, fullchain_path: + util.make_or_verify_dir(os.path.dirname(path), 0o755, self.config.strict_permissions) + + cert_file, abs_cert_path = _open_pem_file(self.config, 'cert_path', cert_path) + + try: + cert_file.write(cert_pem) + finally: + cert_file.close() + + chain_file, abs_chain_path = _open_pem_file(self.config, 'chain_path', chain_path) + fullchain_file, abs_fullchain_path = _open_pem_file( + self.config, 'fullchain_path', fullchain_path) + + _save_chain(chain_pem, chain_file) + _save_chain(cert_pem + chain_pem, fullchain_file) + + return abs_cert_path, abs_chain_path, abs_fullchain_path + + def deploy_certificate(self, sans: list[san.DNSName], privkey_path: str, cert_path: str, + chain_path: str, fullchain_path: str) -> None: + """Install certificate + + :param list sans: list of domains/and or IP addresses to install the certificate + :param str privkey_path: path to certificate private key + :param str cert_path: certificate file path (optional) + :param str fullchain_path: path to the full chain of the certificate + :param str chain_path: chain file path + + """ + if self.installer is None: + logger.error("No installer specified, client is unable to deploy" + "the certificate") + raise errors.Error("No installer available") + + chain_path = None if chain_path is None else os.path.abspath(chain_path) + + display_util.notify("Deploying certificate") + + msg = "Could not install certificate" + domains, ip_addresses = san.split(sans) + if ip_addresses: + raise TypeError("deploy of IP address certificate not supported") + with error_handler.ErrorHandler(self._recovery_routine_with_msg, msg): + for dom in domains: + self.installer.deploy_cert( + domain=dom.dns_name, cert_path=os.path.abspath(cert_path), + key_path=os.path.abspath(privkey_path), + chain_path=chain_path, + fullchain_path=fullchain_path) + self.installer.save() # needed by the Apache plugin + + self.installer.save("Deployed ACME Certificate") + + msg = ("We were unable to install your certificate, " + "however, we successfully restored your " + "server to its prior configuration.") + with error_handler.ErrorHandler(self._rollback_and_restart, msg): + # sites may have been enabled / final cleanup + self.installer.restart() + + def enhance_config(self, domains: list[san.DNSName], chain_path: str, + redirect_default: bool = True) -> None: + """Enhance the configuration. + + :param list domains: list of domains to configure. + :param chain_path: chain file path + :type chain_path: `str` or `None` + :param redirect_default: boolean value that the "redirect" flag should default to + + :raises .errors.Error: if no installer is specified in the + client. + """ + if self.installer is None: + logger.error("No installer is specified, there isn't any " + "configuration to enhance.") + raise errors.Error("No installer available") + + enhanced = False + enhancement_info = ( + ("hsts", "ensure-http-header", "Strict-Transport-Security"), + ("redirect", "redirect", None), + ("staple", "staple-ocsp", chain_path), + ("uir", "ensure-http-header", "Upgrade-Insecure-Requests"),) + supported = self.installer.supported_enhancements() + + for config_name, enhancement_name, option in enhancement_info: + config_value = getattr(self.config, config_name) + if enhancement_name in supported: + if config_name == "redirect" and config_value is None: + config_value = redirect_default + if config_value: + self.apply_enhancement(domains, enhancement_name, option) + enhanced = True + elif config_value: + logger.error( + "Option %s is not supported by the selected installer. " + "Skipping enhancement.", config_name) + + msg = "We were unable to restart web server" + if enhanced: + with error_handler.ErrorHandler(self._rollback_and_restart, msg): + self.installer.restart() + + def apply_enhancement(self, domains: list[san.DNSName], enhancement: str, + options: Optional[str] = None) -> None: + """Applies an enhancement on all domains. + + :param list domains: list of ssl_vhosts (as san.DNSName) + :param str enhancement: name of enhancement, e.g. ensure-http-header + :param str options: options to enhancement, e.g. Strict-Transport-Security + + .. note:: When more `options` are needed, make options a list. + + :raises .errors.PluginError: If Enhancement is not supported, or if + there is any other problem with the enhancement. + + + """ + if not self.installer: + raise errors.Error("No installer plugin has been set.") + enh_label = options if enhancement == "ensure-http-header" else enhancement + with error_handler.ErrorHandler(self._recovery_routine_with_msg, None): + for dom in domains: + try: + self.installer.enhance(dom.dns_name, enhancement, options) + except errors.PluginEnhancementAlreadyPresent: + logger.info("Enhancement %s was already set.", enh_label) + except errors.PluginError: + logger.error("Unable to set the %s enhancement for %s.", enh_label, dom) + raise + + self.installer.save(f"Add enhancement {enh_label}") + + def _recovery_routine_with_msg(self, success_msg: Optional[str]) -> None: + """Calls the installer's recovery routine and prints success_msg + + :param str success_msg: message to show on successful recovery + + """ + if self.installer: + self.installer.recovery_routine() + if success_msg: + display_util.notify(success_msg) + + def _rollback_and_restart(self, success_msg: str) -> None: + """Rollback the most recent checkpoint and restart the webserver + + :param str success_msg: message to show on successful rollback + + """ + if self.installer: + logger.info("Rolling back to previous server configuration...") + try: + self.installer.rollback_checkpoints() + self.installer.restart() + except: + logger.error( + "An error occurred and we failed to restore your config and " + "restart your server. Please post to " + "https://community.letsencrypt.org/c/help " + "with details about your configuration and this error you received." + ) + raise + display_util.notify(success_msg) + + +def validate_key_csr(privkey: util.Key, csr: Optional[util.CSR] = None) -> None: + """Validate Key and CSR files. + + Verifies that the client key and csr arguments are valid and correspond to + one another. This does not currently check the names in the CSR due to + the inability to read SANs from CSRs in python crypto libraries. + + If csr is left as None, only the key will be validated. + + :param privkey: Key associated with CSR + :type privkey: :class:`certbot.util.Key` + + :param .util.CSR csr: CSR + + :raises .errors.Error: when validation fails + + """ + # TODO: Handle all of these problems appropriately + # The client can eventually do things like prompt the user + # and allow the user to take more appropriate actions + + # Key must be readable and valid. + if privkey.pem and not crypto_util.valid_privkey(privkey.pem): + raise errors.Error("The provided key is not a valid key") + + if csr: + if csr.form == "der": + csr_obj = x509.load_der_x509_csr(csr.data) + csr_pem = csr_obj.public_bytes(serialization.Encoding.PEM) + csr = util.CSR(csr.file, csr_pem, "pem") + + # If CSR is provided, it must be readable and valid. + if csr.data and not crypto_util.valid_csr(csr.data): + raise errors.Error("The provided CSR is not a valid CSR") + + # If both CSR and key are provided, the key must be the same key used + # in the CSR. + if csr.data and privkey.pem: + if not crypto_util.csr_matches_pubkey( + csr.data, privkey.pem): + raise errors.Error("The key and CSR do not match") + + +def rollback(default_installer: str, checkpoints: int, + config: configuration.NamespaceConfig, plugins: plugin_disco.PluginsRegistry) -> None: + """Revert configuration the specified number of checkpoints. + + :param str default_installer: Default installer name to use for the rollback + :param int checkpoints: Number of checkpoints to revert. + :param config: Configuration. + :type config: :class:`certbot.configuration.NamespaceConfiguration` + :param plugins: Plugins available + :type plugins: :class:`certbot._internal.plugins.disco.PluginsRegistry` + + """ + # Misconfigurations are only a slight problems... allow the user to rollback + installer = plugin_selection.pick_installer( + config, default_installer, plugins, question="Which installer " + "should be used for rollback?") + + # No Errors occurred during init... proceed normally + # If installer is None... couldn't find an installer... there shouldn't be + # anything to rollback + if installer is not None: + installer.rollback_checkpoints(checkpoints) + installer.restart() + + +def _open_pem_file(config: configuration.NamespaceConfig, + cli_arg_path: str, pem_path: str) -> tuple[IO, str]: + """Open a pem file. + + If cli_arg_path was set by the client, open that. + Otherwise, uniquify the file path. + + :param str cli_arg_path: the cli arg name, e.g. cert_path + :param str pem_path: the pem file path to open + + :returns: a tuple of file object and its absolute file path + + """ + if config.set_by_user(cli_arg_path): + return util.safe_open(pem_path, chmod=0o644, mode="wb"),\ + os.path.abspath(pem_path) + uniq = util.unique_file(pem_path, 0o644, "wb") + return uniq[0], os.path.abspath(uniq[1]) + + +def _save_chain(chain_pem: bytes, chain_file: IO) -> None: + """Saves chain_pem at a unique path based on chain_path. + + :param bytes chain_pem: certificate chain in PEM format + :param str chain_file: chain file object + + """ + try: + chain_file.write(chain_pem) + finally: + chain_file.close() diff --git a/certbot/src/certbot/_internal/constants.py b/certbot/src/certbot/_internal/constants.py new file mode 100644 index 00000000000..bf4243bd94b --- /dev/null +++ b/certbot/src/certbot/_internal/constants.py @@ -0,0 +1,239 @@ +"""Certbot constants.""" +import atexit +import importlib.resources +import logging +from contextlib import ExitStack +from typing import Any + +from acme import challenges +from certbot.compat import misc +from certbot.compat import os + +SETUPTOOLS_PLUGINS_ENTRY_POINT = "certbot.plugins" +"""Setuptools entry point group name for plugins.""" + +OLD_SETUPTOOLS_PLUGINS_ENTRY_POINT = "letsencrypt.plugins" +"""Plugins Setuptools entry point before rename.""" + +CLI_DEFAULTS: dict[str, Any] = dict( # pylint: disable=use-dict-literal + config_files=[ + os.path.join(misc.get_default_folder('config'), 'cli.ini'), + # https://freedesktop.org/wiki/Software/xdg-user-dirs/ + os.path.join(os.environ.get("XDG_CONFIG_HOME", "~/.config"), + "letsencrypt", "cli.ini"), + ], + + # Main parser + verbose_count=0, + verbose_level=None, + text_mode=False, + max_log_backups=1000, + preconfigured_renewal=False, + noninteractive_mode=False, + force_interactive=False, + domains=[], + ip_addresses=[], + certname=None, + dry_run=False, + register_unsafely_without_email=False, + email=None, + eff_email=None, # listed as Ask in help output + reinstall=False, + expand=False, + renew_by_default=False, + renew_with_new_domains=False, + autorenew=True, + allow_subset_of_names=False, + tos=False, + account=None, + duplicate=False, + os_packages_only=False, + no_self_upgrade=False, + no_permissions_check=False, + no_bootstrap=False, + quiet=False, + staging=False, + debug=False, + debug_challenges=False, + no_verify_ssl=False, + http01_port=challenges.HTTP01Response.PORT, + http01_address="", + https_port=443, + break_my_certs=False, + rsa_key_size=2048, + elliptic_curve="secp256r1", + key_type="ecdsa", + must_staple=False, + redirect=None, # default described manually in text in help output + auto_hsts=False, + hsts=None, # listed as False in help output + uir=None, # listed as False in help output + staple=None, # listed as False in help output + strict_permissions=False, + required_profile=None, + preferred_profile=None, + preferred_chain=None, + pref_challs=[], + validate_hooks=True, + directory_hooks=True, + reuse_key=False, + new_key=False, + disable_renew_updates=False, + random_sleep_on_renew=True, + eab_hmac_key=None, + eab_kid=None, + eab_hmac_alg="HS256", + issuance_timeout=90, + run_deploy_hooks=False, + + # Subparsers + num=None, + user_agent=None, + user_agent_comment=None, + csr=None, + reason=0, + delete_after_revoke=None, # listed as Ask in help output + rollback_checkpoints=1, + init=False, + prepare=False, + ifaces=None, + + # Path parsers + auth_cert_path="./cert.pem", + auth_chain_path="./chain.pem", + key_path=None, + config_dir=misc.get_default_folder('config'), + work_dir=misc.get_default_folder('work'), + logs_dir=misc.get_default_folder('logs'), + server="https://acme-v02.api.letsencrypt.org/directory", + + # Plugins parsers + configurator=None, + authenticator=None, + installer=None, + apache=False, + nginx=False, + standalone=False, + manual=False, + webroot=False, + dns_cloudflare=False, + dns_digitalocean=False, + dns_dnsimple=False, + dns_dnsmadeeasy=False, + dns_gehirn=False, + dns_google=False, + dns_linode=False, + dns_luadns=False, + dns_nsone=False, + dns_ovh=False, + dns_rfc2136=False, + dns_route53=False, + dns_sakuracloud=False + +) +STAGING_URI = "https://acme-staging-v02.api.letsencrypt.org/directory" + +V1_URI = "https://acme-v01.api.letsencrypt.org/directory" + +# The set of reasons for revoking a certificate is defined in RFC 5280 in +# section 5.3.1. The reasons that users are allowed to submit are restricted to +# those accepted by the ACME server implementation. They are listed in +# `letsencrypt.boulder.revocation.reasons.go`. +REVOCATION_REASONS = { + "unspecified": 0, + "keycompromise": 1, + "affiliationchanged": 3, + "superseded": 4, + "cessationofoperation": 5} + +"""Defaults for CLI flags and `certbot.configuration.NamespaceConfig` attributes.""" + +QUIET_LOGGING_LEVEL = logging.ERROR +"""Logging level to use in quiet mode.""" + +DEFAULT_LOGGING_LEVEL = logging.WARNING +"""Default logging level to use when not in quiet mode.""" + +ARCHIVE_DIR = "archive" +"""Archive directory, relative to `certbot.configuration.NamespaceConfig.config_dir`.""" + +CONFIG_DIRS_MODE = 0o755 +"""Directory mode for ``certbot.configuration.NamespaceConfig.config_dir`` et al.""" + +ACCOUNTS_DIR = "accounts" +"""Directory where all accounts are saved.""" + +LE_REUSE_SERVERS = { + os.path.normpath('acme-v02.api.letsencrypt.org/directory'): + os.path.normpath('acme-v01.api.letsencrypt.org/directory'), + os.path.normpath('acme-staging-v02.api.letsencrypt.org/directory'): + os.path.normpath('acme-staging.api.letsencrypt.org/directory') +} +"""Servers that can reuse accounts from other servers.""" + +BACKUP_DIR = "backups" +"""Directory (relative to `certbot.configuration.NamespaceConfig.work_dir`) +where backups are kept.""" + +IN_PROGRESS_DIR = "IN_PROGRESS" +"""Directory used before a permanent checkpoint is finalized (relative to +`certbot.configuration.NamespaceConfig.work_dir`).""" + +KEY_DIR = "keys" +"""Directory (relative to `certbot.configuration.NamespaceConfig.config_dir`) +where keys are saved.""" + +LIVE_DIR = "live" +"""Live directory, relative to `certbot.configuration.NamespaceConfig.config_dir`.""" + +TEMP_CHECKPOINT_DIR = "temp_checkpoint" +"""Temporary checkpoint directory, relative +to `certbot.configuration.NamespaceConfig.work_dir`.""" + +RENEWAL_CONFIGS_DIR = "renewal" +"""Renewal configs directory, relative +to `certbot.configuration.NamespaceConfig.config_dir`.""" + +RENEWAL_HOOKS_DIR = "renewal-hooks" +"""Basename of directory containing hooks to run when getting or renewing certificates.""" + +RENEWAL_PRE_HOOKS_DIR = "pre" +"""Basename of directory containing pre-hooks to run +before attempting to get or renew certs""" + +RENEWAL_DEPLOY_HOOKS_DIR = "deploy" +"""Basename of directory containing deploy-hooks to run +upon successfully getting or renewing certs.""" + +RENEWAL_POST_HOOKS_DIR = "post" +"""Basename of directory containing post-hooks to run after attempting to get or renew certs.""" + +FORCE_INTERACTIVE_FLAG = "--force-interactive" +"""Flag to disable TTY checking in certbot.display.util.""" + +EFF_SUBSCRIBE_URI = "https://supporters.eff.org/subscribe/certbot" +"""EFF URI used to submit the e-mail address of users who opt-in.""" + +SSL_DHPARAMS_DEST = "ssl-dhparams.pem" +"""Name of the ssl_dhparams file as saved +in `certbot.configuration.NamespaceConfig.config_dir`.""" + +def _generate_ssl_dhparams_src_static() -> str: + # This code ensures that the resource is accessible as file for the lifetime of current + # Python process, and will be automatically cleaned up on exit. + file_manager = ExitStack() + atexit.register(file_manager.close) + ssl_dhparams_src_ref = importlib.resources.files("certbot") / "ssl-dhparams.pem" + return str(file_manager.enter_context(importlib.resources.as_file(ssl_dhparams_src_ref))) + +SSL_DHPARAMS_SRC = _generate_ssl_dhparams_src_static() +"""Path to the nginx ssl_dhparams file found in the Certbot distribution.""" + +UPDATED_SSL_DHPARAMS_DIGEST = ".updated-ssl-dhparams-pem-digest.txt" +"""Name of the hash of the updated or informed ssl_dhparams as saved +in `certbot.configuration.NamespaceConfig.config_dir`.""" + +ALL_SSL_DHPARAMS_HASHES = [ + '9ba6429597aeed2d8617a7705b56e96d044f64b07971659382e426675105654b', +] +"""SHA256 hashes of the contents of all versions of SSL_DHPARAMS_SRC""" diff --git a/certbot/display/__init__.py b/certbot/src/certbot/_internal/display/__init__.py similarity index 100% rename from certbot/display/__init__.py rename to certbot/src/certbot/_internal/display/__init__.py diff --git a/certbot/src/certbot/_internal/display/completer.py b/certbot/src/certbot/_internal/display/completer.py new file mode 100644 index 00000000000..ec440339db8 --- /dev/null +++ b/certbot/src/certbot/_internal/display/completer.py @@ -0,0 +1,72 @@ +"""Provides Tab completion when prompting users for a path.""" +import glob +from types import TracebackType +from typing import Callable +from typing import Iterator +from typing import Literal +from typing import Optional + +# readline module is not available on all systems +try: + import readline +except ImportError: + import certbot._internal.display.dummy_readline as readline # type: ignore + + +class Completer: + """Provides Tab completion when prompting users for a path. + + This class is meant to be used with readline to provide Tab + completion for users entering paths. The complete method can be + passed to readline.set_completer directly, however, this function + works best as a context manager. For example: + + with Completer(): + raw_input() + + In this example, Tab completion will be available during the call to + raw_input above, however, readline will be restored to its previous + state when exiting the body of the with statement. + + """ + + def __init__(self) -> None: + self._iter: Iterator[str] + self._original_completer: Optional[Callable] + self._original_delims: str + + def complete(self, text: str, state: int) -> Optional[str]: + """Provides path completion for use with readline. + + :param str text: text to offer completions for + :param int state: which completion to return + + :returns: possible completion for text or ``None`` if all + completions have been returned + :rtype: str + + """ + if state == 0: + self._iter = glob.iglob(text + '*') + return next(self._iter, None) + + def __enter__(self) -> None: + self._original_completer = readline.get_completer() + self._original_delims = readline.get_completer_delims() + + readline.set_completer(self.complete) + readline.set_completer_delims(' \t\n;') + + # readline can be implemented using GNU readline, pyreadline or libedit + # which have different configuration syntax + if readline.__doc__ is not None and 'libedit' in readline.__doc__: + readline.parse_and_bind('bind ^I rl_complete') + else: + readline.parse_and_bind('tab: complete') + + def __exit__(self, unused_type: Optional[type[BaseException]], + unused_value: Optional[BaseException], + unused_traceback: Optional[TracebackType]) -> 'Literal[False]': + readline.set_completer_delims(self._original_delims) + readline.set_completer(self._original_completer) + return False diff --git a/certbot/src/certbot/_internal/display/dummy_readline.py b/certbot/src/certbot/_internal/display/dummy_readline.py new file mode 100644 index 00000000000..0575a68cd92 --- /dev/null +++ b/certbot/src/certbot/_internal/display/dummy_readline.py @@ -0,0 +1,25 @@ +"""A dummy module with no effect for use on systems without readline.""" +from typing import Callable +from typing import Iterable +from typing import Optional + + +def get_completer() -> Optional[Callable[[], str]]: + """An empty implementation of readline.get_completer.""" + + +def get_completer_delims() -> list[str]: + """An empty implementation of readline.get_completer_delims.""" + return [] + + +def parse_and_bind(unused_command: str) -> None: + """An empty implementation of readline.parse_and_bind.""" + + +def set_completer(unused_function: Optional[Callable[[], str]] = None) -> None: + """An empty implementation of readline.set_completer.""" + + +def set_completer_delims(unused_delims: Iterable[str]) -> None: + """An empty implementation of readline.set_completer_delims.""" diff --git a/certbot/src/certbot/_internal/display/obj.py b/certbot/src/certbot/_internal/display/obj.py new file mode 100644 index 00000000000..8e8978ca11f --- /dev/null +++ b/certbot/src/certbot/_internal/display/obj.py @@ -0,0 +1,566 @@ +"""This modules define the actual display implementations used in Certbot""" +import logging +import sys +from typing import Any +from typing import Iterable +from typing import Optional +from typing import TextIO +from typing import TypeVar +from typing import Union + +from certbot import errors +from certbot._internal import constants +from certbot._internal.display import completer +from certbot._internal.display import util +from certbot.compat import os + +logger = logging.getLogger(__name__) + +# Display exit codes +OK = "ok" +"""Display exit code indicating user acceptance.""" + +CANCEL = "cancel" +"""Display exit code for a user canceling the display.""" + +# Display constants +SIDE_FRAME = ("- " * 39) + "-" +"""Display boundary (alternates spaces, so when copy-pasted, markdown doesn't interpret +it as a heading)""" + + +# This class holds the global state of the display service. Using this class +# eliminates potential gotchas that exist if self.display was just a global +# variable. In particular, in functions `_DISPLAY = ` would create a +# local variable unless the programmer remembered to use the `global` keyword. +# Adding a level of indirection causes the lookup of the global _DisplayService +# object to happen first avoiding this potential bug. +class _DisplayService: + def __init__(self) -> None: + self.display: Optional[Union[FileDisplay, NoninteractiveDisplay]] = None + + +_SERVICE = _DisplayService() + +T = TypeVar("T") + + +class FileDisplay: + """File-based display.""" + # see https://github.com/certbot/certbot/issues/3915 + + def __init__(self, outfile: TextIO, force_interactive: bool) -> None: + super().__init__() + self.outfile = outfile + self.force_interactive = force_interactive + self.skipped_interaction = False + + def notification(self, message: str, pause: bool = True, wrap: bool = True, + force_interactive: bool = False, decorate: bool = True) -> None: + """Displays a notification and waits for user acceptance. + + :param str message: Message to display + :param bool pause: Whether or not the program should pause for the + user's confirmation + :param bool wrap: Whether or not the application should wrap text + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + :param bool decorate: Whether to surround the message with a + decorated frame + + """ + if wrap: + message = util.wrap_lines(message) + + logger.debug("Notifying user: %s", message) + + self.outfile.write( + (("{line}{frame}{line}" if decorate else "") + + "{msg}{line}" + + ("{frame}{line}" if decorate else "")) + .format(line=os.linesep, frame=SIDE_FRAME, msg=message) + ) + self.outfile.flush() + + if pause: + if self._can_interact(force_interactive): + util.input_with_timeout("Press Enter to Continue") + else: + logger.debug("Not pausing for user confirmation") + + def menu(self, message: str, choices: Union[list[tuple[str, str]], list[str]], + ok_label: Optional[str] = None, cancel_label: Optional[str] = None, # pylint: disable=unused-argument + help_label: Optional[str] = None, default: Optional[int] = None, # pylint: disable=unused-argument + cli_flag: Optional[str] = None, force_interactive: bool = False, + **unused_kwargs: Any) -> tuple[str, int]: + """Display a menu. + + .. todo:: This doesn't enable the help label/button (I wasn't sold on + any interface I came up with for this). It would be a nice feature + + :param str message: title of menu + :param choices: Menu lines, len must be > 0 + :type choices: list of tuples (tag, item) or + list of descriptions (tags will be enumerated) + :param default: default value to return (if one exists) + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: tuple of (`code`, `index`) where + `code` - str display exit code + `index` - int index of the user's selection + + :rtype: tuple + + """ + return_default = self._return_default(message, default, cli_flag, force_interactive) + if return_default is not None: + return OK, return_default + + self._print_menu(message, choices) + + code, selection = self._get_valid_int_ans(len(choices)) + + return code, selection - 1 + + def input(self, message: str, default: Optional[str] = None, cli_flag: Optional[str] = None, + force_interactive: bool = False, **unused_kwargs: Any) -> tuple[str, str]: + """Accept input from the user. + + :param str message: message to display to the user + :param default: default value to return (if one exists) + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: tuple of (`code`, `input`) where + `code` - str display exit code + `input` - str of the user's input + :rtype: tuple + + """ + return_default = self._return_default(message, default, cli_flag, force_interactive) + if return_default is not None: + return OK, return_default + + # Trailing space must be added outside of util.wrap_lines to + # be preserved + message = util.wrap_lines("%s (Enter 'c' to cancel):" % message) + " " + ans = util.input_with_timeout(message) + + if ans in ("c", "C"): + return CANCEL, "-1" + return OK, ans + + def yesno(self, message: str, yes_label: str = "Yes", no_label: str = "No", + default: Optional[bool] = None, cli_flag: Optional[str] = None, + force_interactive: bool = False, **unused_kwargs: Any) -> bool: + """Query the user with a yes/no question. + + Yes and No label must begin with different letters, and must contain at + least one letter each. + + :param str message: question for the user + :param str yes_label: Label of the "Yes" parameter + :param str no_label: Label of the "No" parameter + :param default: default value to return (if one exists) + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: True for "Yes", False for "No" + :rtype: bool + + """ + return_default = self._return_default(message, default, cli_flag, force_interactive) + if return_default is not None: + return return_default + + message = util.wrap_lines(message) + + self.outfile.write("{0}{frame}{msg}{0}{frame}".format( + os.linesep, frame=SIDE_FRAME + os.linesep, msg=message)) + self.outfile.flush() + + while True: + ans = util.input_with_timeout("{yes}/{no}: ".format( + yes=util.parens_around_char(yes_label), + no=util.parens_around_char(no_label))) + + # Couldn't get pylint indentation right with elif + # elif doesn't matter in this situation + if (ans.startswith(yes_label[0].lower()) or + ans.startswith(yes_label[0].upper())): + return True + if (ans.startswith(no_label[0].lower()) or + ans.startswith(no_label[0].upper())): + return False + + def checklist(self, message: str, tags: list[str], default: Optional[list[str]] = None, + cli_flag: Optional[str] = None, force_interactive: bool = False, + **unused_kwargs: Any) -> tuple[str, list[str]]: + """Display a checklist. + + :param str message: Message to display to user + :param list tags: `str` tags to select, len(tags) > 0 + :param default: default value to return (if one exists) + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: tuple of (`code`, `tags`) where + `code` - str display exit code + `tags` - list of selected tags + :rtype: tuple + + """ + return_default = self._return_default(message, default, cli_flag, force_interactive) + if return_default is not None: + return OK, return_default + + while True: + self._print_menu(message, tags) + + code, ans = self.input("Select the appropriate numbers separated " + "by commas and/or spaces, or leave input " + "blank to select all options shown", + force_interactive=True) + + if code == OK: + if not ans.strip(): + ans = " ".join(str(x) for x in range(1, len(tags)+1)) + indices = util.separate_list_input(ans) + selected_tags = self._scrub_checklist_input(indices, tags) + if selected_tags: + return code, selected_tags + self.outfile.write( + "** Error - Invalid selection **%s" % os.linesep) + self.outfile.flush() + else: + return code, [] + + def _return_default(self, prompt: str, default: Optional[T], + cli_flag: Optional[str], force_interactive: bool) -> Optional[T]: + """Should we return the default instead of prompting the user? + + :param str prompt: prompt for the user + :param T default: default answer to prompt + :param str cli_flag: command line option for setting an answer + to this question + :param bool force_interactive: if interactivity is forced + + :returns: The default value if we should return it else `None` + :rtype: T or `None` + + """ + # assert_valid_call(prompt, default, cli_flag, force_interactive) + if self._can_interact(force_interactive): + return None + if default is None: + msg = "Unable to get an answer for the question:\n{0}".format(prompt) + if cli_flag: + msg += ( + "\nYou can provide an answer on the " + "command line with the {0} flag.".format(cli_flag)) + raise errors.Error(msg) + logger.debug( + "Falling back to default %s for the prompt:\n%s", + default, prompt) + return default + + def _can_interact(self, force_interactive: bool) -> bool: + """Can we safely interact with the user? + + :param bool force_interactive: if interactivity is forced + + :returns: True if the display can interact with the user + :rtype: bool + + """ + if (self.force_interactive or force_interactive or + sys.stdin.isatty() and self.outfile.isatty()): + return True + if not self.skipped_interaction: + logger.warning( + "Skipped user interaction because Certbot doesn't appear to " + "be running in a terminal. You should probably include " + "--non-interactive or %s on the command line.", + constants.FORCE_INTERACTIVE_FLAG) + self.skipped_interaction = True + return False + + def directory_select(self, message: str, default: Optional[str] = None, + cli_flag: Optional[str] = None, force_interactive: bool = False, + **unused_kwargs: Any) -> tuple[str, str]: + """Display a directory selection screen. + + :param str message: prompt to give the user + :param default: default value to return (if one exists) + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: tuple of the form (`code`, `string`) where + `code` - display exit code + `string` - input entered by the user + + """ + with completer.Completer(): + return self.input(message, default, cli_flag, force_interactive) + + def _scrub_checklist_input(self, indices: Iterable[Union[str, int]], + tags: list[str]) -> list[str]: + """Validate input and transform indices to appropriate tags. + + :param list indices: input + :param list tags: Original tags of the checklist + + :returns: valid tags the user selected + :rtype: :class:`list` of :class:`str` + + """ + # They should all be of type int + try: + indices_int = [int(index) for index in indices] + except ValueError: + return [] + + # Remove duplicates. dict is used instead of set, since dict preserves + # insertion order as of Python 3.7 + indices_int = list(dict.fromkeys(indices_int).keys()) + + # Check all input is within range + for index in indices_int: + if index < 1 or index > len(tags): + return [] + # Transform indices_int to appropriate tags + return [tags[index - 1] for index in indices_int] + + def _print_menu(self, message: str, + choices: Union[list[tuple[str, str]], list[str]]) -> None: + """Print a menu on the screen. + + :param str message: title of menu + :param choices: Menu lines + :type choices: list of tuples (tag, item) or + list of descriptions (tags will be enumerated) + + """ + # Can take either tuples or single items in choices list + if choices and isinstance(choices[0], tuple): + choices = [f"{c[0]} - {c[1]}" for c in choices] + + # Write out the message to the user + self.outfile.write(f"{os.linesep}{message}{os.linesep}") + self.outfile.write(SIDE_FRAME + os.linesep) + + # Write out the menu choices + for i, desc in enumerate(choices, 1): + msg = f"{i}: {desc}" + self.outfile.write(util.wrap_lines(msg)) + + # Keep this outside of the textwrap + self.outfile.write(os.linesep) + + self.outfile.write(SIDE_FRAME + os.linesep) + self.outfile.flush() + + def _get_valid_int_ans(self, max_: int) -> tuple[str, int]: + """Get a numerical selection. + + :param int max: The maximum entry (len of choices), must be positive + + :returns: tuple of the form (`code`, `selection`) where + `code` - str display exit code ('ok' or cancel') + `selection` - int user's selection + :rtype: tuple + + """ + selection = -1 + if max_ > 1: + input_msg = ("Select the appropriate number " + "[1-{max_}] then [enter] (press 'c' to " + "cancel): ".format(max_=max_)) + else: + input_msg = ("Press 1 [enter] to confirm the selection " + "(press 'c' to cancel): ") + while selection < 1: + ans = util.input_with_timeout(input_msg) + if ans.startswith("c") or ans.startswith("C"): + return CANCEL, -1 + try: + selection = int(ans) + if selection < 1 or selection > max_: + selection = -1 + raise ValueError + + except ValueError: + self.outfile.write( + "{0}** Invalid input **{0}".format(os.linesep)) + self.outfile.flush() + + return OK, selection + + +class NoninteractiveDisplay: + """A display utility implementation that never asks for interactive user input""" + + def __init__(self, outfile: TextIO, *unused_args: Any, **unused_kwargs: Any) -> None: + super().__init__() + self.outfile = outfile + + def _interaction_fail(self, message: str, cli_flag: Optional[str], + extra: str = "") -> errors.MissingCommandlineFlag: + """Return error to raise in case of an attempt to interact in noninteractive mode""" + msg = "Missing command line flag or config entry for this setting:\n" + msg += message + if extra: + msg += "\n" + extra + if cli_flag: + msg += "\n\n(You can set this with the {0} flag)".format(cli_flag) + return errors.MissingCommandlineFlag(msg) + + def notification(self, message: str, pause: bool = False, wrap: bool = True, # pylint: disable=unused-argument + decorate: bool = True, **unused_kwargs: Any) -> None: + """Displays a notification without waiting for user acceptance. + + :param str message: Message to display to stdout + :param bool pause: The NoninteractiveDisplay waits for no keyboard + :param bool wrap: Whether or not the application should wrap text + :param bool decorate: Whether to apply a decorated frame to the message + + """ + if wrap: + message = util.wrap_lines(message) + + logger.debug("Notifying user: %s", message) + + self.outfile.write( + (("{line}{frame}{line}" if decorate else "") + + "{msg}{line}" + + ("{frame}{line}" if decorate else "")) + .format(line=os.linesep, frame=SIDE_FRAME, msg=message) + ) + self.outfile.flush() + + def menu(self, message: str, choices: Union[list[tuple[str, str]], list[str]], + ok_label: Optional[str] = None, cancel_label: Optional[str] = None, + help_label: Optional[str] = None, default: Optional[int] = None, + cli_flag: Optional[str] = None, **unused_kwargs: Any) -> tuple[str, int]: + # pylint: disable=unused-argument + """Avoid displaying a menu. + + :param str message: title of menu + :param choices: Menu lines, len must be > 0 + :type choices: list of tuples (tag, item) or + list of descriptions (tags will be enumerated) + :param int default: the default choice + :param dict kwargs: absorbs various irrelevant labelling arguments + + :returns: tuple of (`code`, `index`) where + `code` - str display exit code + `index` - int index of the user's selection + :rtype: tuple + :raises errors.MissingCommandlineFlag: if there was no default + + """ + if default is None: + raise self._interaction_fail(message, cli_flag, "Choices: " + repr(choices)) + + return OK, default + + def input(self, message: str, default: Optional[str] = None, cli_flag: Optional[str] = None, + **unused_kwargs: Any) -> tuple[str, str]: + """Accept input from the user. + + :param str message: message to display to the user + + :returns: tuple of (`code`, `input`) where + `code` - str display exit code + `input` - str of the user's input + :rtype: tuple + :raises errors.MissingCommandlineFlag: if there was no default + + """ + if default is None: + raise self._interaction_fail(message, cli_flag) + return OK, default + + def yesno(self, message: str, yes_label: Optional[str] = None, no_label: Optional[str] = None, # pylint: disable=unused-argument + default: Optional[bool] = None, cli_flag: Optional[str] = None, + **unused_kwargs: Any) -> bool: + """Decide Yes or No, without asking anybody + + :param str message: question for the user + :param dict kwargs: absorbs yes_label, no_label + + :raises errors.MissingCommandlineFlag: if there was no default + :returns: True for "Yes", False for "No" + :rtype: bool + + """ + if default is None: + raise self._interaction_fail(message, cli_flag) + return default + + def checklist(self, message: str, tags: Iterable[str], default: Optional[list[str]] = None, + cli_flag: Optional[str] = None, **unused_kwargs: Any) -> tuple[str, list[str]]: + """Display a checklist. + + :param str message: Message to display to user + :param list tags: `str` tags to select, len(tags) > 0 + :param dict kwargs: absorbs default_status arg + + :returns: tuple of (`code`, `tags`) where + `code` - str display exit code + `tags` - list of selected tags + :rtype: tuple + + """ + if default is None: + raise self._interaction_fail(message, cli_flag, "? ".join(tags) + "?") + return OK, default + + def directory_select(self, message: str, default: Optional[str] = None, + cli_flag: Optional[str] = None, **unused_kwargs: Any) -> tuple[str, str]: + """Simulate prompting the user for a directory. + + This function returns default if it is not ``None``, otherwise, + an exception is raised explaining the problem. If cli_flag is + not ``None``, the error message will include the flag that can + be used to set this value with the CLI. + + :param str message: prompt to give the user + :param default: default value to return (if one exists) + :param str cli_flag: option used to set this value with the CLI + + :returns: tuple of the form (`code`, `string`) where + `code` - int display exit code + `string` - input entered by the user + + """ + return self.input(message, default, cli_flag) + + +def get_display() -> Union[FileDisplay, NoninteractiveDisplay]: + """Get the display utility. + + :return: the display utility + :rtype: Union[FileDisplay, NoninteractiveDisplay] + :raise: ValueError if the display utility is not configured yet. + + """ + if not _SERVICE.display: + raise ValueError("This function was called too early in Certbot's execution " + "as the display utility hasn't been configured yet.") + return _SERVICE.display + + +def set_display(display: Union[FileDisplay, NoninteractiveDisplay]) -> None: + """Set the display service. + + :param Union[FileDisplay, NoninteractiveDisplay] display: the display service + + """ + _SERVICE.display = display diff --git a/certbot/src/certbot/_internal/display/util.py b/certbot/src/certbot/_internal/display/util.py new file mode 100644 index 00000000000..9836167422f --- /dev/null +++ b/certbot/src/certbot/_internal/display/util.py @@ -0,0 +1,123 @@ +"""Internal Certbot display utilities.""" +import sys +import textwrap +from typing import Optional + +from acme import messages as acme_messages +from certbot._internal import san +from certbot.compat import misc + + +def wrap_lines(msg: str) -> str: + """Format lines nicely to 80 chars. + + :param str msg: Original message + + :returns: Formatted message respecting newlines in message + :rtype: str + + """ + lines = msg.splitlines() + fixed_l = [] + + for line in lines: + fixed_l.append(textwrap.fill( + line, + 80, + break_long_words=False, + break_on_hyphens=False)) + + return '\n'.join(fixed_l) + + +def parens_around_char(label: str) -> str: + """Place parens around first character of label. + + :param str label: Must contain at least one character + + """ + return "({first}){rest}".format(first=label[0], rest=label[1:]) + + +def input_with_timeout(prompt: Optional[str] = None, timeout: float = 36000.0) -> str: + """Get user input with a timeout. + + Behaves the same as the builtin input, however, an error is raised if + a user doesn't answer after timeout seconds. The default timeout + value was chosen to place it just under 12 hours for users following + our advice and running Certbot twice a day. + + :param str prompt: prompt to provide for input + :param float timeout: maximum number of seconds to wait for input + + :returns: user response + :rtype: str + + :raises errors.Error if no answer is given before the timeout + + """ + # use of sys.stdin and sys.stdout to mimic the builtin input based on + # https://github.com/python/cpython/blob/baf7bb30a02aabde260143136bdf5b3738a1d409/Lib/getpass.py#L129 + if prompt: + sys.stdout.write(prompt) + sys.stdout.flush() + + line = misc.readline_with_timeout(timeout, prompt) + + if not line: + raise EOFError + return line.rstrip('\n') + + +def separate_list_input(input_: str) -> list[str]: + """Separate a comma or space separated list. + + :param str input_: input from the user + + :returns: strings + :rtype: list + + """ + no_commas = input_.replace(",", " ") + # Each string is naturally unicode, this causes problems with M2Crypto SANs + # TODO: check if above is still true when M2Crypto is gone ^ + return [str(string) for string in no_commas.split()] + + +def summarize_sans(sans: list[san.SAN]) -> str: + """Summarizes a list of identifiers in the format of: + example.com.com and N more + or if there are only two identifiers: + example.com and 192.0.2.77 + or if there is only one identifier: + example.com + + :param list sans: `san.SAN` list of domains and/or IP addresses + :returns: a summary + :rtype: str + """ + if not sans: + return "" + + length = len(sans) + if length == 1: + return str(sans[0]) + elif length == 2: + return f"{sans[0]} and {sans[1]}" + else: + return f"{sans[0]} and {length - 1} more" + + +def describe_acme_error(error: acme_messages.Error) -> str: + """Returns a human-readable description of an RFC7807 error. + + :param error: The ACME error + :returns: a string describing the error, suitable for human consumption. + :rtype: str + """ + parts = (error.title, error.detail) + if any(parts): + return ' :: '.join(part for part in parts if part is not None) + if error.description: + return error.description + return error.typ diff --git a/certbot/src/certbot/_internal/eff.py b/certbot/src/certbot/_internal/eff.py new file mode 100644 index 00000000000..6b7ceeb48ef --- /dev/null +++ b/certbot/src/certbot/_internal/eff.py @@ -0,0 +1,127 @@ +"""Subscribes users to the EFF newsletter.""" +import logging +from typing import Optional + +import requests + +from certbot import configuration +from certbot._internal import constants +from certbot._internal.account import Account +from certbot._internal.account import AccountFileStorage +from certbot.display import util as display_util + +logger = logging.getLogger(__name__) + + +def prepare_subscription(config: configuration.NamespaceConfig, acc: Account) -> None: + """High level function to store potential EFF newsletter subscriptions. + + The user may be asked if they want to sign up for the newsletter if + they have not given their explicit approval or refusal using --eff-mail + or --no-eff-mail flag. + + Decision about EFF subscription will be stored in the account metadata. + + :param configuration.NamespaceConfig config: Client configuration. + :param Account acc: Current client account. + + """ + if config.eff_email is False: + return + if config.eff_email is True: + if config.email is None: + _report_failure("you didn't provide an e-mail address") + else: + acc.meta = acc.meta.update(register_to_eff=config.email) + elif config.email and _want_subscription(): + acc.meta = acc.meta.update(register_to_eff=config.email) + + if acc.meta.register_to_eff: + storage = AccountFileStorage(config) + storage.update_meta(acc) + + +def handle_subscription(config: configuration.NamespaceConfig, acc: Optional[Account]) -> None: + """High level function to take care of EFF newsletter subscriptions. + + Once subscription is handled, it will not be handled again. + + :param configuration.NamespaceConfig config: Client configuration. + :param Account acc: Current client account. + + """ + if config.dry_run or not acc: + return + if acc.meta.register_to_eff: + subscribe(acc.meta.register_to_eff) + + acc.meta = acc.meta.update(register_to_eff=None) + storage = AccountFileStorage(config) + storage.update_meta(acc) + + +def _want_subscription() -> bool: + """Does the user want to be subscribed to the EFF newsletter? + + :returns: True if we should subscribe the user, otherwise, False + :rtype: bool + + """ + prompt = ( + 'Would you be willing, once your first certificate is successfully issued, ' + 'to share your email address with the Electronic Frontier Foundation, a ' + "founding partner of the Let's Encrypt project and the non-profit organization " + "that develops Certbot? We'd like to send you email about our work encrypting " + "the web, EFF news, campaigns, and ways to support digital freedom. ") + return display_util.yesno(prompt, default=False) + + +def subscribe(email: str) -> None: + """Subscribe the user to the EFF mailing list. + + :param str email: the e-mail address to subscribe + + """ + url = constants.EFF_SUBSCRIBE_URI + data = {'data_type': 'json', + 'email': email, + 'form_id': 'eff_supporters_library_subscribe_form'} + logger.info('Subscribe to the EFF mailing list (email: %s).', email) + logger.debug('Sending POST request to %s:\n%s', url, data) + _check_response(requests.post(url, data=data, timeout=60)) + + +def _check_response(response: requests.Response) -> None: + """Check for errors in the server's response. + + If an error occurred, it will be reported to the user. + + :param requests.Response response: the server's response to the + subscription request + + """ + logger.debug('Received response:\n%s', response.content) + try: + response.raise_for_status() + if not response.json()['status']: + _report_failure('your e-mail address appears to be invalid') + except requests.exceptions.HTTPError: + _report_failure() + except (ValueError, KeyError): + _report_failure('there was a problem with the server response') + + +def _report_failure(reason: Optional[str] = None) -> None: + """Notify the user of failing to sign them up for the newsletter. + + :param reason: a phrase describing what the problem was + beginning with a lowercase letter and no closing punctuation + :type reason: `str` or `None` + + """ + msg = ['We were unable to subscribe you the EFF mailing list'] + if reason is not None: + msg.append(' because ') + msg.append(reason) + msg.append('. You can try again later by visiting https://act.eff.org.') + display_util.notify(''.join(msg)) diff --git a/certbot/src/certbot/_internal/error_handler.py b/certbot/src/certbot/_internal/error_handler.py new file mode 100644 index 00000000000..5c7fb7d6df1 --- /dev/null +++ b/certbot/src/certbot/_internal/error_handler.py @@ -0,0 +1,177 @@ +"""Registers functions to be called if an exception or signal occurs.""" +import functools +import logging +import signal +import traceback +from types import TracebackType +from typing import Any +from typing import Callable +from typing import Optional +from typing import Union + +from certbot import errors +from certbot.compat import os + +logger = logging.getLogger(__name__) + + +# _SIGNALS stores the signals that will be handled by the ErrorHandler. These +# signals were chosen as their default handler terminates the process and could +# potentially occur from inside Python. Signals such as SIGILL were not +# included as they could be a sign of something devious and we should terminate +# immediately. +if os.name != "nt": + _SIGNALS = [signal.SIGTERM] + for signal_code in [signal.SIGHUP, signal.SIGQUIT, + signal.SIGXCPU, signal.SIGXFSZ]: + # Adding only those signals that their default action is not Ignore. + # This is platform-dependent, so we check it dynamically. + if signal.getsignal(signal_code) != signal.SIG_IGN: + _SIGNALS.append(signal_code) +else: + # POSIX signals are not implemented natively in Windows, but emulated from the C runtime. + # As consumed by CPython, most of handlers on these signals are useless, in particular + # SIGTERM: for instance, os.kill(pid, signal.SIGTERM) will call TerminateProcess, that stops + # immediately the process without calling the attached handler. Besides, non-POSIX signals + # (CTRL_C_EVENT and CTRL_BREAK_EVENT) are implemented in a console context to handle the + # CTRL+C event to a process launched from the console. Only CTRL_C_EVENT has a reliable + # behavior in fact, and maps to the handler to SIGINT. However in this case, a + # KeyboardInterrupt is raised, that will be handled by ErrorHandler through the context manager + # protocol. Finally, no signal on Windows is electable to be handled using ErrorHandler. + # + # Refs: https://stackoverflow.com/a/35792192, https://maruel.ca/post/python_windows_signal, + # https://docs.python.org/2/library/os.html#os.kill, + # https://www.reddit.com/r/Python/comments/1dsblt/windows_command_line_automation_ctrlc_question + _SIGNALS = [] + + +class ErrorHandler: + """Context manager for running code that must be cleaned up on failure. + + The context manager allows you to register functions that will be called + when an exception (excluding SystemExit) or signal is encountered. + Usage:: + + handler = ErrorHandler(cleanup1_func, *cleanup1_args, **cleanup1_kwargs) + handler.register(cleanup2_func, *cleanup2_args, **cleanup2_kwargs) + + with handler: + do_something() + + Or for one cleanup function:: + + with ErrorHandler(func, args, kwargs): + do_something() + + If an exception is raised out of do_something, the cleanup functions will + be called in last in first out order. Then the exception is raised. + Similarly, if a signal is encountered, the cleanup functions are called + followed by the previously received signal handler. + + Each registered cleanup function is called exactly once. If a registered + function raises an exception, it is logged and the next function is called. + Signals received while the registered functions are executing are + deferred until they finish. + + """ + def __init__(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> None: + self.call_on_regular_exit = False + self.body_executed = False + self.funcs: list[Callable[[], Any]] = [] + self.prev_handlers: dict[int, Union[int, None, Callable]] = {} + self.received_signals: list[int] = [] + if func is not None: + self.register(func, *args, **kwargs) + + def __enter__(self) -> None: + self.body_executed = False + self._set_signal_handlers() + + def __exit__(self, exec_type: Optional[type[BaseException]], + exec_value: Optional[BaseException], + trace: Optional[TracebackType]) -> bool: + self.body_executed = True + retval = False + # SystemExit is ignored to properly handle forks that don't exec + if exec_type is SystemExit: + return retval + if exec_type is None: + if not self.call_on_regular_exit: + return retval + elif exec_type is errors.SignalExit: + logger.debug("Encountered signals: %s", self.received_signals) + retval = True + else: + logger.debug("Encountered exception:\n%s", "".join( + traceback.format_exception(exec_type, exec_value, trace))) + + self._call_registered() + self._reset_signal_handlers() + self._call_signals() + return retval + + def register(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> None: + """Sets func to be run with the given arguments during cleanup. + + :param function func: function to be called in case of an error + + """ + self.funcs.append(functools.partial(func, *args, **kwargs)) + + def _call_registered(self) -> None: + """Calls all registered functions""" + logger.debug("Calling registered functions") + while self.funcs: + try: + self.funcs[-1]() + except Exception as exc: # pylint: disable=broad-except + output = traceback.format_exception_only(type(exc), exc) + logger.error("Encountered exception during recovery: %s", + ''.join(output).rstrip()) + self.funcs.pop() + + def _set_signal_handlers(self) -> None: + """Sets signal handlers for signals in _SIGNALS.""" + for signum in _SIGNALS: + prev_handler = signal.getsignal(signum) + # If prev_handler is None, the handler was set outside of Python + if prev_handler is not None: + self.prev_handlers[signum] = prev_handler + signal.signal(signum, self._signal_handler) + + def _reset_signal_handlers(self) -> None: + """Resets signal handlers for signals in _SIGNALS.""" + for signum, handler in self.prev_handlers.items(): + signal.signal(signum, handler) + self.prev_handlers.clear() + + def _signal_handler(self, signum: int, unused_frame: Any) -> None: + """Replacement function for handling received signals. + + Store the received signal. If we are executing the code block in + the body of the context manager, stop by raising signal exit. + + :param int signum: number of current signal + + """ + self.received_signals.append(signum) + if not self.body_executed: + raise errors.SignalExit + + def _call_signals(self) -> None: + """Finally call the deferred signals.""" + for signum in self.received_signals: + logger.debug("Calling signal %s", signum) + os.kill(os.getpid(), signum) + + +class ExitHandler(ErrorHandler): + """Context manager for running code that must be cleaned up. + + Subclass of ErrorHandler, with the same usage and parameters. + In addition to cleaning up on all signals, also cleans up on + regular exit. + """ + def __init__(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> None: + super().__init__(func, *args, **kwargs) + self.call_on_regular_exit = True diff --git a/certbot/src/certbot/_internal/hooks.py b/certbot/src/certbot/_internal/hooks.py new file mode 100644 index 00000000000..a7c77b6ae34 --- /dev/null +++ b/certbot/src/certbot/_internal/hooks.py @@ -0,0 +1,277 @@ +"""Facilities for implementing hooks that call shell commands.""" + +import logging +from typing import Optional + +from certbot import configuration +from certbot import errors +from certbot import util +from certbot._internal import san +from certbot.compat import filesystem +from certbot.compat import misc +from certbot.compat import os +from certbot.display import ops as display_ops +from certbot.plugins import util as plug_util + +logger = logging.getLogger(__name__) + + +def validate_hooks(config: configuration.NamespaceConfig) -> None: + """Check hook commands are executable.""" + validate_hook(config.pre_hook, "pre") + validate_hook(config.post_hook, "post") + validate_hook(config.deploy_hook, "deploy") + + +def _prog(shell_cmd: str) -> Optional[str]: + """Extract the program run by a shell command. + + :param str shell_cmd: command to be executed + + :returns: basename of command or None if the command isn't found + :rtype: str or None + + """ + if not util.exe_exists(shell_cmd): + plug_util.path_surgery(shell_cmd) + if not util.exe_exists(shell_cmd): + return None + + return os.path.basename(shell_cmd) + + +def validate_hook(shell_cmd: str, hook_name: str) -> None: + """Check that a command provided as a hook is plausibly executable. + + :raises .errors.HookCommandNotFound: if the command is not found + """ + if shell_cmd: + cmd = shell_cmd.split(None, 1)[0] + if not _prog(cmd): + path = os.environ["PATH"] + if os.path.exists(cmd): + msg = f"{cmd}-hook command {hook_name} exists, but is not executable." + else: + msg = ( + f"Unable to find {hook_name}-hook command {cmd} in the PATH.\n(PATH is " + f"{path})\nSee also the --disable-hook-validation option." + ) + + raise errors.HookCommandNotFound(msg) + + +def pre_hook(config: configuration.NamespaceConfig) -> None: + """Run pre-hooks if they exist and haven't already been run. + + When Certbot is running with the renew subcommand, this function + runs any hooks found in the config.renewal_pre_hooks_dir (if they + have not already been run) followed by any pre-hook in the config. + If hooks in config.renewal_pre_hooks_dir are run and the pre-hook in + the config is a path to one of these scripts, it is not run twice. + + :param configuration.NamespaceConfig config: Certbot settings + + """ + all_hooks: list[str] = (list_hooks(config.renewal_pre_hooks_dir) if config.directory_hooks + else []) + all_hooks += [config.pre_hook] if config.pre_hook else [] + for hook in all_hooks: + _run_pre_hook_if_necessary(hook) + + +executed_pre_hooks: set[str] = set() + + +def _run_pre_hook_if_necessary(command: str) -> None: + """Run the specified pre-hook if we haven't already. + + If we've already run this exact command before, a message is logged + saying the pre-hook was skipped. + + :param str command: pre-hook to be run + + """ + if command in executed_pre_hooks: + logger.info("Pre-hook command already run, skipping: %s", command) + else: + _run_hook("pre-hook", command) + executed_pre_hooks.add(command) + + +def post_hook( + config: configuration.NamespaceConfig, + renewed_sans: list[san.SAN] +) -> None: + + """Run post-hooks if defined. + + This function also registers any executables found in + config.renewal_post_hooks_dir to be run when Certbot is used with + the renew subcommand. + + If the verb is renew, we delay executing any post-hooks until + :func:`run_saved_post_hooks` is called. In this case, this function + registers all hooks found in config.renewal_post_hooks_dir to be + called followed by any post-hook in the config. If the post-hook in + the config is a path to an executable in the post-hook directory, it + is not scheduled to be run twice. + + :param configuration.NamespaceConfig config: Certbot settings + + """ + + all_hooks: list[str] = (list_hooks(config.renewal_post_hooks_dir) if config.directory_hooks + else []) + all_hooks += [config.post_hook] if config.post_hook else [] + # In the "renew" case, we save these up to run at the end + if config.verb == "renew": + for hook in all_hooks: + _run_eventually(hook) + # certonly / run + else: + renewed_sans_str = ' '.join(map(str, renewed_sans)) + # 32k is reasonable on Windows and likely quite conservative on other platforms + if len(renewed_sans_str) > 32_000: + logger.warning("Limiting RENEWED_DOMAINS environment variable to 32k characters") + renewed_sans_str = renewed_sans_str[:32_000] + for hook in all_hooks: + _run_hook( + "post-hook", + hook, + { + 'RENEWED_DOMAINS': renewed_sans_str, + # Since other commands stop certbot execution on failure, + # it doesn't make sense to have a FAILED_DOMAINS variable + 'FAILED_DOMAINS': "" + } + ) + + +post_hooks: list[str] = [] + + +def _run_eventually(command: str) -> None: + """Registers a post-hook to be run eventually. + + All commands given to this function will be run exactly once in the + order they were given when :func:`run_saved_post_hooks` is called. + + :param str command: post-hook to register to be run + + """ + if command not in post_hooks: + post_hooks.append(command) + + +def run_saved_post_hooks(renewed_sans: list[san.SAN], failed_sans: list[san.SAN]) -> None: + """Run any post hooks that were saved up in the course of the 'renew' verb""" + + renewed_sans_str = ' '.join(map(str, renewed_sans)) + failed_sans_str = ' '.join(map(str, failed_sans)) + + # 32k combined is reasonable on Windows and likely quite conservative on other platforms + if len(renewed_sans_str) > 16_000: + logger.warning("Limiting RENEWED_DOMAINS environment variable to 16k characters") + renewed_sans_str = renewed_sans_str[:16_000] + + if len(failed_sans_str) > 16_000: + logger.warning("Limiting FAILED_DOMAINS environment variable to 16k characters") + renewed_sans_str = failed_sans_str[:16_000] + + for cmd in post_hooks: + _run_hook( + "post-hook", + cmd, + { + 'RENEWED_DOMAINS': renewed_sans_str, + 'FAILED_DOMAINS': failed_sans_str + } + ) + + +def deploy_hook(config: configuration.NamespaceConfig, sans: list[san.SAN], + lineage_path: str) -> None: + """Run post-renewal/post-issuance hooks. + + This function runs any hooks found in + config.renewal_deploy_hooks_dir followed by any deploy-hook in the + config. If the deploy-hook in the config is a path to a script in + config.renewal_deploy_hooks_dir, it is not run twice. + + If Certbot is doing a dry run, no hooks are run and messages are + logged saying that they were skipped. + + :param configuration.NamespaceConfig config: Certbot settings + :param sans: domains and/or IP addresses in the obtained certificate + :type sans: `list` of `san.SAN` + :param str lineage_path: live directory path for the new cert + + """ + executed_hooks = set() + all_hooks: list[str] = (list_hooks(config.renewal_deploy_hooks_dir) if config.directory_hooks + else []) + all_hooks += [config.deploy_hook] if config.deploy_hook else [] + for hook in all_hooks: + if hook in executed_hooks: + logger.info("Skipping deploy-hook '%s' as it was already run.", hook) + else: + _run_deploy_hook(hook, sans, lineage_path, config.dry_run, config.run_deploy_hooks) + executed_hooks.add(hook) + + +def _run_deploy_hook(command: str, sans: list[san.SAN], lineage_path: str, dry_run: bool, + run_deploy_hooks: bool) -> None: + """Run the specified deploy-hook (if not doing a dry run). + + If dry_run is True, command is not run and a message is logged + saying that it was skipped. If dry_run is False, the hook is run + after setting the appropriate environment variables. + + :param str command: command to run as a deploy-hook + :param sans: domains and/or IP addresses in the obtained certificate + :type sans: `list` of `san.SAN` + :param str lineage_path: live directory path for the new cert + :param bool dry_run: True iff Certbot is doing a dry run + :param bool run_deploy_hooks: True if deploy hooks should run despite Certbot doing a dry run + + """ + if dry_run and not run_deploy_hooks: + logger.info("Dry run: skipping deploy hook command: %s", + command) + return + + os.environ["RENEWED_DOMAINS"] = " ".join(map(str, sans)) + os.environ["RENEWED_LINEAGE"] = lineage_path + _run_hook("deploy-hook", command) + + +def _run_hook(cmd_name: str, shell_cmd: str, extra_env: Optional[dict[str, str]] = None) -> str: + """Run a hook command. + + :param str cmd_name: the user facing name of the hook being run + :param shell_cmd: shell command to execute + :type shell_cmd: `list` of `str` or `str` + :param dict extra_env: extra environment variables to set + :type extra_env: `dict` of `str` to `str` + + :returns: stderr if there was any""" + env = util.env_no_snap_for_external_calls() + env.update(extra_env or {}) + returncode, err, out = misc.execute_command_status( + cmd_name, shell_cmd, env=env) + display_ops.report_executed_command(f"Hook '{cmd_name}'", returncode, out, err) + return err + + +def list_hooks(dir_path: str) -> list[str]: + """List paths to all hooks found in dir_path in sorted order. + + :param str dir_path: directory to search + + :returns: `list` of `str` + :rtype: sorted list of absolute paths to executables in dir_path + + """ + allpaths = (os.path.join(dir_path, f) for f in os.listdir(dir_path)) + hooks = [path for path in allpaths if filesystem.is_executable(path) and not path.endswith('~')] + return sorted(hooks) diff --git a/certbot/src/certbot/_internal/lock.py b/certbot/src/certbot/_internal/lock.py new file mode 100644 index 00000000000..4627c57d2d2 --- /dev/null +++ b/certbot/src/certbot/_internal/lock.py @@ -0,0 +1,259 @@ +"""Implements file locks compatible with Linux and Windows for locking files and directories.""" +import errno +import logging +from typing import Optional + +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os + +try: + import fcntl +except ImportError: + import msvcrt + POSIX_MODE = False +else: + POSIX_MODE = True + + +logger = logging.getLogger(__name__) + + +class LockFile: + """ + Platform independent file lock system. + LockFile accepts a parameter, the path to a file acting as a lock. Once the LockFile, + instance is created, the associated file is 'locked from the point of view of the OS, + meaning that if another instance of Certbot try at the same time to acquire the same lock, + it will raise an Exception. Calling release method will release the lock, and make it + available to every other instance. + Upon exit, Certbot will also release all the locks. + This allows us to protect a file or directory from being concurrently accessed + or modified by two Certbot instances. + LockFile is platform independent: it will proceed to the appropriate OS lock mechanism + depending on Linux or Windows. + """ + def __init__(self, path: str) -> None: + """ + Create a LockFile instance on the given file path, and acquire lock. + :param str path: the path to the file that will hold a lock + """ + self._path = path + mechanism = _UnixLockMechanism if POSIX_MODE else _WindowsLockMechanism + self._lock_mechanism = mechanism(path) + + self.acquire() + + def __repr__(self) -> str: + repr_str = '{0}({1}) <'.format(self.__class__.__name__, self._path) + if self.is_locked(): + repr_str += 'acquired>' + else: + repr_str += 'released>' + return repr_str + + def acquire(self) -> None: + """ + Acquire the lock on the file, forbidding any other Certbot instance to acquire it. + :raises errors.LockError: if unable to acquire the lock + """ + self._lock_mechanism.acquire() + + def release(self) -> None: + """ + Release the lock on the file, allowing any other Certbot instance to acquire it. + """ + self._lock_mechanism.release() + + def is_locked(self) -> bool: + """ + Check if the file is currently locked. + :return: True if the file is locked, False otherwise + """ + return self._lock_mechanism.is_locked() + + +class _BaseLockMechanism: + def __init__(self, path: str) -> None: + """ + Create a lock file mechanism for Unix. + :param str path: the path to the lock file + """ + self._path = path + self._fd: Optional[int] = None + + def is_locked(self) -> bool: + """Check if lock file is currently locked. + :return: True if the lock file is locked + :rtype: bool + """ + return self._fd is not None + + def acquire(self) -> None: # pylint: disable=missing-function-docstring + pass # pragma: no cover + + def release(self) -> None: # pylint: disable=missing-function-docstring + pass # pragma: no cover + + +class _UnixLockMechanism(_BaseLockMechanism): + """ + A UNIX lock file mechanism. + This lock file is released when the locked file is closed or the + process exits. It cannot be used to provide synchronization between + threads. It is based on the lock_file package by Martin Horcicka. + """ + def acquire(self) -> None: + """Acquire the lock.""" + while self._fd is None: + # Open the file + fd = filesystem.open(self._path, os.O_CREAT | os.O_WRONLY, 0o600) + try: + self._try_lock(fd) + if self._lock_success(fd): + self._fd = fd + finally: + # Close the file if it is not the required one + if self._fd is None: + os.close(fd) + + def _try_lock(self, fd: int) -> None: + """ + Try to acquire the lock file without blocking. + :param int fd: file descriptor of the opened file to lock + """ + try: + fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError as err: + if err.errno in (errno.EACCES, errno.EAGAIN): + logger.debug('A lock on %s is held by another process.', self._path) + raise errors.LockError('Another instance of Certbot is already running.') + raise + + def _lock_success(self, fd: int) -> bool: + """ + Did we successfully grab the lock? + Because this class deletes the locked file when the lock is + released, it is possible another process removed and recreated + the file between us opening the file and acquiring the lock. + :param int fd: file descriptor of the opened file to lock + :returns: True if the lock was successfully acquired + :rtype: bool + """ + # Normally os module should not be imported in certbot codebase except in certbot.compat + # for the sake of compatibility over Windows and Linux. + # We make an exception here, since _lock_success is private and called only on Linux. + from os import fstat # pylint: disable=os-module-forbidden + from os import stat # pylint: disable=os-module-forbidden + try: + stat1 = stat(self._path) + except OSError as err: + if err.errno == errno.ENOENT: + return False + raise + + stat2 = fstat(fd) + # If our locked file descriptor and the file on disk refer to + # the same device and inode, they're the same file. + return stat1.st_dev == stat2.st_dev and stat1.st_ino == stat2.st_ino + + def release(self) -> None: + """Remove, close, and release the lock file.""" + # It is important the lock file is removed before it's released, + # otherwise: + # + # process A: open lock file + # process B: release lock file + # process A: lock file + # process A: check device and inode + # process B: delete file + # process C: open and lock a different file at the same path + try: + os.remove(self._path) + finally: + # Following check is done to make mypy happy: it ensure that self._fd, marked + # as Optional[int] is effectively int to make it compatible with os.close signature. + if self._fd is None: # pragma: no cover + raise TypeError('Error, self._fd is None.') + try: + os.close(self._fd) + finally: + self._fd = None + + +class _WindowsLockMechanism(_BaseLockMechanism): + """ + A Windows lock file mechanism. + By default on Windows, acquiring a file handler gives exclusive access to the process + and results in an effective lock. However, it is possible to explicitly acquire the + file handler in shared access in terms of read and write, and this is done by os.open + in Python. So an explicit lock needs to be done through the call of + msvcrt.locking, that will lock the first byte of the file. In theory, it is also + possible to access a file in shared delete access, allowing other processes to delete an + opened file. But this needs also to be done explicitly by all processes using the Windows + low level APIs, and Python does not do it. As of Python 3.7 and below, Python developers + state that deleting a file opened by a process from another process is not possible with + os.open. + Consequently, msvcrt.locking is sufficient to obtain an effective lock, and the race + condition encountered on Linux is not possible on Windows, leading to a simpler workflow. + """ + def acquire(self) -> None: + """Acquire the lock""" + open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC + + fd = None + try: + # Under Windows, filesystem.open will raise directly an EACCES error + # if the lock file is already locked. + fd = filesystem.open(self._path, open_mode, 0o600) + # This "type: ignore" is currently needed because msvcrt methods + # are only defined on Windows. See + # https://github.com/python/typeshed/blob/16ae4c61201cd8b96b8b22cdfb2ab9e89ba5bcf2/stdlib/msvcrt.pyi. + msvcrt.locking(fd, msvcrt.LK_NBLCK, 1) # type: ignore # pylint: disable=used-before-assignment + except OSError as err: + if fd: + os.close(fd) + # Anything except EACCES is unexpected. Raise directly the error in that case. + if err.errno != errno.EACCES: + raise + logger.debug('A lock on %s is held by another process.', self._path) + raise errors.LockError('Another instance of Certbot is already running.') + + self._fd = fd + + def release(self) -> None: + """Release the lock.""" + try: + if not self._fd: + raise errors.Error("The lock has not been acquired first.") + # This "type: ignore" is currently needed because msvcrt methods + # are only defined on Windows. See + # https://github.com/python/typeshed/blob/16ae4c61201cd8b96b8b22cdfb2ab9e89ba5bcf2/stdlib/msvcrt.pyi. + msvcrt.locking(self._fd, msvcrt.LK_UNLCK, 1) # type: ignore # pylint: disable=used-before-assignment + os.close(self._fd) + + try: + os.remove(self._path) + except OSError as e: + # If the lock file cannot be removed, it is not a big deal. + # Likely another instance is acquiring the lock we just released. + logger.debug(str(e)) + finally: + self._fd = None + + +def lock_dir(dir_path: str) -> LockFile: + """Place a lock file on the directory at dir_path. + + The lock file is placed in the root of dir_path with the name + .certbot.lock. + + :param str dir_path: path to directory + + :returns: the locked LockFile object + :rtype: LockFile + + :raises errors.LockError: if unable to acquire the lock + + """ + return LockFile(os.path.join(dir_path, '.certbot.lock')) diff --git a/certbot/src/certbot/_internal/log.py b/certbot/src/certbot/_internal/log.py new file mode 100644 index 00000000000..2c3a7de3807 --- /dev/null +++ b/certbot/src/certbot/_internal/log.py @@ -0,0 +1,403 @@ +"""Logging utilities for Certbot. + +The best way to use this module is through `pre_arg_parse_setup` and +`post_arg_parse_setup`. `pre_arg_parse_setup` configures a minimal +terminal logger and ensures a detailed log is written to a secure +temporary file if Certbot exits before `post_arg_parse_setup` is called. +`post_arg_parse_setup` relies on the parsed command line arguments and +does the full logging setup with terminal and rotating file handling as +configured by the user. Any logged messages before +`post_arg_parse_setup` is called are sent to the rotating file handler. +Special care is taken by both methods to ensure all errors are logged +and properly flushed before program exit. + +The `logging` module is useful for recording messages about about what +Certbot is doing under the hood, but do not necessarily need to be shown +to the user on the terminal. The default verbosity is WARNING. + +The preferred method to display important information to the user is to +use `certbot.display.util` and `certbot.display.ops`. + +""" + + +import functools +import logging +import logging.handlers +import shutil +import sys +import tempfile +import traceback +from types import TracebackType +from typing import Any +from typing import cast +from typing import IO +from typing import Optional + +from acme import messages +from certbot import configuration +from certbot import errors +from certbot import util +from certbot._internal import constants +from certbot._internal.display import util as display_util +from certbot.compat import os + +# Logging format +CLI_FMT = "%(message)s" +FILE_FMT = "%(asctime)s:%(levelname)s:%(name)s:%(message)s" + + +logger = logging.getLogger(__name__) + + +def pre_arg_parse_setup() -> None: + """Setup logging before command line arguments are parsed. + + Terminal logging is setup using + `certbot._internal.constants.QUIET_LOGGING_LEVEL` so Certbot is as quiet as + possible. File logging is setup so that logging messages are + buffered in memory. If Certbot exits before `post_arg_parse_setup` + is called, these buffered messages are written to a temporary file. + If Certbot doesn't exit, `post_arg_parse_setup` writes the messages + to the normal log files. + + This function also sets `logging.shutdown` to be called on program + exit which automatically flushes logging handlers and + `sys.excepthook` to properly log/display fatal exceptions. + + """ + temp_handler = TempHandler() + temp_handler.setFormatter(logging.Formatter(FILE_FMT)) + temp_handler.setLevel(logging.DEBUG) + memory_handler = MemoryHandler(temp_handler) + + stream_handler = ColoredStreamHandler() + stream_handler.setFormatter(logging.Formatter(CLI_FMT)) + # The pre-argparse logging level is set to WARNING here. This is to ensure that + # deprecated flags (see DeprecatedArgumentAction) print something to the terminal. + # See https://github.com/certbot/certbot/issues/9618. + stream_handler.setLevel(logging.WARNING) + + root_logger = logging.getLogger() + root_logger.setLevel(logging.DEBUG) # send all records to handlers + root_logger.addHandler(memory_handler) + root_logger.addHandler(stream_handler) + + # logging.shutdown will flush the memory handler because flush() and + # close() are explicitly called + util.atexit_register(logging.shutdown) + sys.excepthook = functools.partial( + pre_arg_parse_except_hook, memory_handler, + debug='--debug' in sys.argv, + quiet='--quiet' in sys.argv or '-q' in sys.argv, + log_path=temp_handler.path) + + +def post_arg_parse_setup(config: configuration.NamespaceConfig) -> None: + """Setup logging after command line arguments are parsed. + + This function assumes `pre_arg_parse_setup` was called earlier and + the root logging configuration has not been modified. A rotating + file logging handler is created and the buffered log messages are + sent to that handler. Terminal logging output is set to the level + requested by the user. + + :param certbot.configuration.NamespaceConfig config: Configuration object + + """ + file_handler, file_path = setup_log_file_handler( + config, 'letsencrypt.log', FILE_FMT) + + root_logger = logging.getLogger() + memory_handler = stderr_handler = None + for handler in root_logger.handlers: + if isinstance(handler, ColoredStreamHandler): + stderr_handler = handler + elif isinstance(handler, MemoryHandler): + memory_handler = handler + msg = 'Previously configured logging handlers have been removed!' + assert memory_handler is not None and stderr_handler is not None, msg + + root_logger.addHandler(file_handler) + root_logger.removeHandler(memory_handler) + temp_handler = getattr(memory_handler, 'target', None) + memory_handler.setTarget(file_handler) # pylint: disable=no-member + memory_handler.flush(force=True) # pylint: disable=unexpected-keyword-arg + memory_handler.close() + if temp_handler: + temp_handler.close() + + if config.quiet: + level = constants.QUIET_LOGGING_LEVEL + elif config.verbose_level is not None: + level = constants.DEFAULT_LOGGING_LEVEL - int(config.verbose_level) * 10 + else: + level = constants.DEFAULT_LOGGING_LEVEL - config.verbose_count * 10 + + stderr_handler.setLevel(level) + logger.debug('Root logging level set at %d', level) + + if not config.quiet: + print(f'Saving debug log to {file_path}', file=sys.stderr) + + sys.excepthook = functools.partial( + post_arg_parse_except_hook, + debug=config.debug, quiet=config.quiet, log_path=file_path) + + +def setup_log_file_handler(config: configuration.NamespaceConfig, logfile: str, + fmt: str) -> tuple[logging.Handler, str]: + """Setup file debug logging. + + :param certbot.configuration.NamespaceConfig config: Configuration object + :param str logfile: basename for the log file + :param str fmt: logging format string + + :returns: file handler and absolute path to the log file + :rtype: tuple + + """ + # TODO: logs might contain sensitive data such as contents of the + # private key! #525 + util.set_up_core_dir(config.logs_dir, 0o700, config.strict_permissions) + log_file_path = os.path.join(config.logs_dir, logfile) + try: + handler = logging.handlers.RotatingFileHandler( + log_file_path, maxBytes=2 ** 20, + backupCount=config.max_log_backups) + except OSError as error: + raise errors.Error(util.PERM_ERR_FMT.format(error)) + # rotate on each invocation, rollover only possible when maxBytes + # is nonzero and backupCount is nonzero, so we set maxBytes as big + # as possible not to overrun in single CLI invocation (1MB). + handler.doRollover() # TODO: creates empty letsencrypt.log.1 file + handler.setLevel(logging.DEBUG) + handler_formatter = logging.Formatter(fmt=fmt) + handler.setFormatter(handler_formatter) + return handler, log_file_path + + +class ColoredStreamHandler(logging.StreamHandler): + """Sends colored logging output to a stream. + + If the specified stream is not a tty, the class works like the + standard `logging.StreamHandler`. Default red_level is + `logging.WARNING`. + + :ivar bool colored: True if output should be colored + :ivar bool red_level: The level at which to output + + """ + def __init__(self, stream: Optional[IO] = None) -> None: + super().__init__(stream) + self.colored = (sys.stderr.isatty() if stream is None else + stream.isatty()) + self.red_level = logging.WARNING + + def format(self, record: logging.LogRecord) -> str: + """Formats the string representation of record. + + :param logging.LogRecord record: Record to be formatted + + :returns: Formatted, string representation of record + :rtype: str + + """ + out = super().format(record) + if self.colored and record.levelno >= self.red_level: + return ''.join((util.ANSI_SGR_RED, out, util.ANSI_SGR_RESET)) + return out + + +class MemoryHandler(logging.handlers.MemoryHandler): + """Buffers logging messages in memory until the buffer is flushed. + + This differs from `logging.handlers.MemoryHandler` in that flushing + only happens when flush(force=True) is called. + + """ + def __init__(self, target: Optional[logging.Handler] = None, + capacity: int = 10000) -> None: + # capacity doesn't matter because should_flush() is overridden + super().__init__(capacity, target=target) + + def close(self) -> None: + """Close the memory handler, but don't set the target to None.""" + # This allows the logging module which may only have a weak + # reference to the target handler to properly flush and close it. + target = getattr(self, 'target') + super().close() + self.target = target + + def flush(self, force: bool = False) -> None: # pylint: disable=arguments-differ + """Flush the buffer if force=True. + + If force=False, this call is a noop. + + :param bool force: True if the buffer should be flushed. + + """ + # This method allows flush() calls in logging.shutdown to be a + # noop so we can control when this handler is flushed. + if force: + super().flush() + + def shouldFlush(self, record: logging.LogRecord) -> bool: + """Should the buffer be automatically flushed? + + :param logging.LogRecord record: log record to be considered + + :returns: False because the buffer should never be auto-flushed + :rtype: bool + + """ + return False + + +class TempHandler(logging.StreamHandler): + """Safely logs messages to a temporary file. + + The file is created with permissions 600. If no log records are sent + to this handler, the temporary file is deleted when the handler is + closed. + + :ivar str path: file system path to the temporary log file + + """ + def __init__(self) -> None: + self._workdir = tempfile.mkdtemp(prefix="certbot-log-") + self.path = os.path.join(self._workdir, 'log') + stream = util.safe_open(self.path, mode='w', chmod=0o600) + super().__init__(stream) + # Super constructor assigns the provided stream object to self.stream. + # Let's help mypy be aware of this by giving a type hint. + self.stream: IO[str] + self._delete = True + + def emit(self, record: logging.LogRecord) -> None: + """Log the specified logging record. + + :param logging.LogRecord record: Record to be formatted + + """ + self._delete = False + super().emit(record) + + def close(self) -> None: + """Close the handler and the temporary log file. + + The temporary log file is deleted if it wasn't used. + + """ + self.acquire() + try: + # StreamHandler.close() doesn't close the stream to allow a + # stream like stderr to be used + self.stream.close() + if self._delete: + shutil.rmtree(self._workdir) + self._delete = False + super().close() + finally: + self.release() + + +def pre_arg_parse_except_hook(memory_handler: MemoryHandler, + *args: Any, **kwargs: Any) -> None: + """A simple wrapper around post_arg_parse_except_hook. + + The additional functionality provided by this wrapper is the memory + handler will be flushed before Certbot exits. This allows us to + write logging messages to a temporary file if we crashed before + logging was fully configured. + + Since sys.excepthook isn't called on SystemExit exceptions, the + memory handler will not be flushed in this case which prevents us + from creating temporary log files when argparse exits because a + command line argument was invalid or -h, --help, or --version was + provided on the command line. + + :param MemoryHandler memory_handler: memory handler to flush + :param tuple args: args for post_arg_parse_except_hook + :param dict kwargs: kwargs for post_arg_parse_except_hook + + """ + try: + post_arg_parse_except_hook(*args, **kwargs) + finally: + # flush() is called here so messages logged during + # post_arg_parse_except_hook are also flushed. + memory_handler.flush(force=True) + + +def post_arg_parse_except_hook(exc_type: type[BaseException], exc_value: BaseException, + trace: TracebackType, debug: bool, quiet: bool, + log_path: str) -> None: + """Logs fatal exceptions and reports them to the user. + + If debug is True, the full exception and traceback is shown to the + user, otherwise, it is suppressed. sys.exit is always called with a + nonzero status. + + :param type exc_type: type of the raised exception + :param BaseException exc_value: raised exception + :param traceback trace: traceback of where the exception was raised + :param bool debug: True if the traceback should be shown to the user + :param bool quiet: True if Certbot is running in quiet mode + :param str log_path: path to file or directory containing the log + + """ + exc_info = (exc_type, exc_value, trace) + # Only print human advice if not running under --quiet + def exit_func() -> None: + if quiet: + sys.exit(1) + else: + exit_with_advice(log_path) + # constants.QUIET_LOGGING_LEVEL or higher should be used to + # display message the user, otherwise, a lower level like + # logger.DEBUG should be used + if debug or not issubclass(exc_type, Exception): + assert constants.QUIET_LOGGING_LEVEL <= logging.ERROR + if exc_type is KeyboardInterrupt: + logger.error('Exiting due to user request.') + sys.exit(1) + logger.error('Exiting abnormally:', exc_info=exc_info) + else: + logger.debug('Exiting abnormally:', exc_info=exc_info) + # Use logger to print the error message to take advantage of + # our logger printing warnings and errors in red text. + if issubclass(exc_type, errors.Error): + logger.error(str(exc_value)) + exit_func() + logger.error('An unexpected error occurred:') + acme_error = getattr(exc_value, "error", exc_value) + if messages.is_acme_error(acme_error): + logger.error(display_util.describe_acme_error(cast(messages.Error, acme_error))) + else: + output = traceback.format_exception_only(exc_type, exc_value) + # format_exception_only returns a list of strings each + # terminated by a newline. We combine them into one string + # and remove the final newline before passing it to + # logger.error. + logger.error(''.join(output).rstrip()) + exit_func() + + +def exit_with_advice(log_path: str) -> None: + """Print a link to the community forums, the debug log path, and exit + + The message is printed to stderr and the program will exit with a + nonzero status. + + :param str log_path: path to file or directory containing the log + + """ + msg = ("Ask for help or search for solutions at https://community.letsencrypt.org. " + "See the ") + if os.path.isdir(log_path): + msg += f'logfiles in {log_path} ' + else: + msg += f"logfile {log_path} " + msg += 'or re-run Certbot with -v for more details.' + sys.exit(msg) diff --git a/certbot/src/certbot/_internal/main.py b/certbot/src/certbot/_internal/main.py new file mode 100644 index 00000000000..e8654185cc2 --- /dev/null +++ b/certbot/src/certbot/_internal/main.py @@ -0,0 +1,1886 @@ +"""Certbot main entry point.""" +# pylint: disable=too-many-lines + +from contextlib import contextmanager +import copy +import functools +import logging.handlers +import sys +from typing import cast +from typing import Generator +from typing import IO +from typing import Iterable +from typing import Optional +from typing import TypeVar +from typing import Union + +import configobj +from cryptography import x509 +import josepy as jose +from josepy import b64 + +from acme import client as acme_client +from acme import errors as acme_errors +from acme import messages as acme_messages +import certbot +from certbot import configuration +from certbot import crypto_util +from certbot import errors +from certbot import interfaces +from certbot import util +from certbot._internal import account +from certbot._internal import cert_manager +from certbot._internal import cli +from certbot._internal import client +from certbot._internal import constants +from certbot._internal import eff +from certbot._internal import hooks +from certbot._internal import log +from certbot._internal import renewal +from certbot._internal import san +from certbot._internal import snap_config +from certbot._internal import storage +from certbot._internal import updater +from certbot._internal.display import obj as display_obj +from certbot._internal.display import util as internal_display_util +from certbot._internal.plugins import disco as plugins_disco +from certbot._internal.plugins import selection as plug_sel +from certbot.compat import filesystem +from certbot.compat import misc +from certbot.compat import os +from certbot.display import ops as display_ops +from certbot.display import util as display_util +from certbot.plugins import enhancements + +USER_CANCELLED = ("User chose to cancel the operation and may " + "reinvoke the client.") + + +logger = logging.getLogger(__name__) + + +def _suggest_donation_if_appropriate(config: configuration.NamespaceConfig) -> None: + """Potentially suggest a donation to support Certbot. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :returns: `None` + :rtype: None + + """ + # don't prompt for donation if: + # - renewing + # - using the staging server (--staging or --dry-run) + # - running with --quiet (display fd won't be available during atexit calls #8995) + assert config.verb != "renew" + if config.staging or config.quiet: + return + util.atexit_register( + display_util.notification, + "If you like Certbot, please consider supporting our work by:\n" + " * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate\n" + " * Donating to EFF: https://eff.org/donate-le", + pause=False + ) + + +def _get_and_save_cert(le_client: client.Client, config: configuration.NamespaceConfig, + sans: Optional[list[san.SAN]] = None, certname: Optional[str] = None, + lineage: Optional[storage.RenewableCert] = None + ) -> Optional[storage.RenewableCert]: + """Authenticate and enroll certificate. + + This method finds the relevant lineage, figures out what to do with it, + then performs that action. Includes calls to hooks, various reports, + checks, and requests for user input. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param sans: List of domain names and/or IP addresses for which to get a certificate. + Defaults to `None` + :type sans: `list` of `san.SAN` + + :param certname: Name of new certificate. Defaults to `None` + :type certname: str + + :param lineage: Certificate lineage object. Defaults to `None` + :type lineage: storage.RenewableCert + + :returns: the issued certificate or `None` if doing a dry run + :rtype: storage.RenewableCert or None + + :raises errors.Error: if certificate could not be obtained + + """ + hooks.pre_hook(config) + renewed_sans: list[san.SAN] = [] + + try: + if lineage is not None: + # Renewal, where we already know the specific lineage we're + # interested in + display_util.notify( + "{action} for {identifiers}".format( + action="Simulating renewal of an existing certificate" + if config.dry_run else "Renewing an existing certificate", + identifiers=internal_display_util.summarize_sans(sans or lineage.sans()) + ) + ) + renewal.renew_cert(config, sans, le_client, lineage) + else: + # TREAT AS NEW REQUEST + if sans is None: + raise errors.Error("Domain list cannot be none if the lineage is not set.") + display_util.notify( + "{action} for {identifiers}".format( + action="Simulating a certificate request" if config.dry_run else + "Requesting a certificate", + identifiers=internal_display_util.summarize_sans(sans) + ) + ) + lineage = le_client.obtain_and_enroll_certificate(sans, certname) + if lineage is not None: + hooks.deploy_hook(config, lineage.sans(), lineage.live_dir) + renewed_sans.extend(sans) + finally: + hooks.post_hook(config, renewed_sans) + + return lineage + + +def _handle_unexpected_key_type_migration(config: configuration.NamespaceConfig, + cert: storage.RenewableCert) -> bool: + """ + This function ensures that the user will not implicitly migrate an existing key + from one type to another in the situation where a certificate for that lineage + already exist and they have not provided explicitly --key-type and --cert-name. + :param config: Current configuration provided by the client + :param cert: Matching certificate that could be renewed + :returns: Whether a key type migration is going ahead. + :rtype: `bool` + """ + new_key_type = config.key_type.upper() + cur_key_type = cert.private_key_type.upper() + + if new_key_type == cur_key_type: + return False + + # If both --key-type and --cert-name are provided, we consider the user's intent to + # be unambiguous: to change the key type of this lineage. + is_confirmed_via_cli = config.set_by_user("key_type") and config.set_by_user("certname") + + # Failing that, we interactively prompt the user to confirm the change. + if is_confirmed_via_cli or display_util.yesno( + f'An {cur_key_type} certificate named {cert.lineagename} already exists. Do you want to ' + f'update its key type to {new_key_type}?', + yes_label='Update key type', no_label='Keep existing key type', + default=False, force_interactive=False, + ): + return True + + # If --key-type was set on the CLI but the user did not confirm the key type change using + # one of the two above methods, their intent is ambiguous. Error out. + if config.set_by_user("key_type"): + raise errors.Error( + 'Are you trying to change the key type of the certificate named ' + f'{cert.lineagename} from {cur_key_type} to {new_key_type}? Please provide ' + 'both --cert-name and --key-type on the command line to confirm the change ' + 'you are trying to make.' + ) + + # The mismatch between the lineage's key type and config.key_type is caused by Certbot's + # default value. The user is not asking for a key change: keep the key type of the existing + # lineage. + config.key_type = cur_key_type.lower() + return False + + +def _handle_subset_cert_request(config: configuration.NamespaceConfig, + sans: Iterable[san.SAN], + cert: storage.RenewableCert + ) -> tuple[str, Optional[storage.RenewableCert]]: + """Figure out what to do if a previous cert had a subset of the names now requested + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param sans: List of domain names and/or IP addresses + :type sans: `list` of `san.SAN` + + :param cert: Certificate object + :type cert: storage.RenewableCert + + :returns: Tuple of (str action, cert_or_None) as per _find_lineage_for_sans_and_certname + action can be: "newcert" | "renew" | "reinstall" + :rtype: `tuple` of `str` + + """ + _handle_unexpected_key_type_migration(config, cert) + + question = ( + "You have an existing certificate that contains a portion of " + "the domains you requested (ref: {0}){br}{br}It contains these " + "names: {1}{br}{br}You requested these names for the new " + "certificate: {2}.{br}{br}Do you want to expand and replace this existing " + "certificate with the new certificate?" + ).format(cert.configfile.filename, + san.display(cert.sans()), + san.display(sans), + br=os.linesep) + if config.expand or config.renew_by_default or display_util.yesno( + question, "Expand", "Cancel", cli_flag="--expand", force_interactive=True): + return "renew", cert + display_util.notify( + "To obtain a new certificate that contains these names without " + "replacing your existing certificate for {0}, you must use the " + "--duplicate option.{br}{br}" + "For example:{br}{br}{1} --duplicate {2}".format( + san.display(cert.sans()), + cli.cli_command, " ".join(sys.argv[1:]), + br=os.linesep + )) + raise errors.Error(USER_CANCELLED) + + +def _handle_identical_cert_request(config: configuration.NamespaceConfig, + lineage: storage.RenewableCert, + ) -> tuple[str, Optional[storage.RenewableCert]]: + """Figure out what to do if a lineage has the same names as a previously obtained one + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :returns: Tuple of (str action, cert_or_None) as per _find_lineage_for_sans_and_certname + action can be: "newcert" | "renew" | "reinstall" + :rtype: `tuple` of `str` + + """ + is_key_type_changing = _handle_unexpected_key_type_migration(config, lineage) + + if not lineage.ensure_deployed(): + return "reinstall", lineage + if is_key_type_changing: + return "renew", lineage + + ari_clients = renewal.AriClientPool(config) + + if renewal.should_renew(config, lineage, ari_clients): + return "renew", lineage + + if config.reinstall: + # Set with --reinstall, force an identical certificate to be + # reinstalled without further prompting. + return "reinstall", lineage + question = ( + "You have an existing certificate that has exactly the same " + "domains or certificate name you requested and isn't close to expiry." + "{br}(ref: {0}){br}{br}What would you like to do?" + ).format(lineage.configfile.filename, br=os.linesep) + + if config.verb == "run": + keep_opt = "Attempt to reinstall this existing certificate" + else: + assert config.verb == "certonly", "Unexpected Certbot subcommand" + keep_opt = "Keep the existing certificate for now" + choices = [keep_opt, + "Renew & replace the certificate (may be subject to CA rate limits)"] + + response = display_util.menu(question, choices, + default=0, force_interactive=True) + if response[0] == display_util.CANCEL: + # TODO: Add notification related to command-line options for + # skipping the menu for this case. + raise errors.Error( + "Operation canceled. You may re-run the client.") + if response[1] == 0: + return "reinstall", lineage + elif response[1] == 1: + return "renew", lineage + raise AssertionError('This is impossible') + + +def _find_lineage_for_sans(config: configuration.NamespaceConfig, sans: list[san.SAN] + ) -> tuple[Optional[str], Optional[storage.RenewableCert]]: + """Determine whether there are duplicated names and how to handle + them (renew, reinstall, newcert, or raising an error to stop + the client run if the user chooses to cancel the operation when + prompted). + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param sans: List of domain names and/or IP addresses + :type sans: `list` of `san.SAN` + + :returns: Two-element tuple containing desired new-certificate behavior as + a string token ("reinstall", "renew", or "newcert"), plus either + a RenewableCert instance or `None` if renewal shouldn't occur. + :rtype: `tuple` of `str` and :class:`storage.RenewableCert` or `None` + + :raises errors.Error: If the user would like to rerun the client again. + + """ + # Considering the possibility that the requested certificate is + # related to an existing certificate. (config.duplicate, which + # is set with --duplicate, skips all of this logic and forces any + # kind of certificate to be obtained with renewal = False.) + if config.duplicate: + return "newcert", None + # TODO: Also address superset case + ident_names_cert, subset_names_cert = cert_manager.find_duplicative_certs(config, sans) + # XXX ^ schoen is not sure whether that correctly reads the systemwide + # configuration file. + if ident_names_cert is None and subset_names_cert is None: + return "newcert", None + + if ident_names_cert is not None: + return _handle_identical_cert_request(config, ident_names_cert) + elif subset_names_cert is not None: + return _handle_subset_cert_request(config, sans, subset_names_cert) + return None, None + + +def _find_cert(config: configuration.NamespaceConfig, sans: list[san.SAN], certname: str + ) -> tuple[bool, Optional[storage.RenewableCert]]: + """Finds an existing certificate object given domains and/or a certificate name. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param sans: List of domain names and/or IP addresses + :type sans: `list` of `san.SAN` + + :param certname: Name of certificate + :type certname: str + + :returns: Two-element tuple of a boolean that indicates if this function should be + followed by a call to fetch a certificate from the server, and either a + RenewableCert instance or None. + :rtype: `tuple` of `bool` and :class:`storage.RenewableCert` or `None` + + """ + action, lineage = _find_lineage_for_sans_and_certname(config, sans, certname) + if action == "reinstall": + logger.info("Keeping the existing certificate") + return (action != "reinstall"), lineage + + +def _find_lineage_for_sans_and_certname( + config: configuration.NamespaceConfig, sans: list[san.SAN], + certname: str) -> tuple[Optional[str], Optional[storage.RenewableCert]]: + """Find appropriate lineage based on given domains and/or certname. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param sans: List of domain names and/or IP addresses + :type sans: `list` of `san.SAN` + + :param certname: Name of certificate + :type certname: str + + :returns: Two-element tuple containing desired new-certificate behavior as + a string token ("reinstall", "renew", or "newcert"), plus either + a RenewableCert instance or None if renewal should not occur. + + :rtype: `tuple` of `str` and :class:`storage.RenewableCert` or `None` + + :raises errors.Error: If the user would like to rerun the client again. + + """ + if not certname: + return _find_lineage_for_sans(config, sans) + lineage = cert_manager.lineage_for_certname(config, certname) + if lineage: + if sans: + computed_domains = cert_manager.sans_for_certname(config, certname) + if computed_domains and set(computed_domains) != set(sans): + _handle_unexpected_key_type_migration(config, lineage) + _ask_user_to_confirm_new_sans(config, sans, certname, + lineage.sans()) # raises if no + return "renew", lineage + # unnecessarily specified domains or no domains specified + return _handle_identical_cert_request(config, lineage) + elif sans: + return "newcert", None + raise errors.ConfigurationError("No certificate with name {0} found. " + "Use -d to specify domains, or run certbot certificates to see " + "possible certificate names.".format(certname)) + + +T = TypeVar("T") + + +def _get_added_removed(after: Iterable[T], before: Iterable[T]) -> tuple[list[T], list[T]]: + """Get lists of items removed from `before` + and a lists of items added to `after` + """ + added = list(set(after) - set(before)) + removed = list(set(before) - set(after)) + added.sort() + removed.sort() + return added, removed + + +def _format_list(character: str, strings: Iterable[str]) -> str: + """Format list with given character + """ + if not strings: + formatted = "{br}(None)" + else: + formatted = "{br}{ch} " + "{br}{ch} ".join(strings) + return formatted.format( + ch=character, + br=os.linesep + ) + + +def _ask_user_to_confirm_new_sans(config: configuration.NamespaceConfig, + new_sans: Iterable[san.SAN], + certname: str, + old_sans: Iterable[san.SAN]) -> None: + """Ask user to confirm update cert certname to contain new_sans. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param new_sans: List of new domain names and/or IP addresses + :type new_sans: `list` of `san.SAN` + + :param certname: Name of certificate + :type certname: str + + :param old_sans: List of old domain names and/or IP addresses + :type old_sans: `list` of `san.SAN` + + :returns: None + :rtype: None + + :raises errors.ConfigurationError: if cert name and domains mismatch + + """ + if config.renew_with_new_domains: + return + + added, removed = _get_added_removed(map(str, new_sans), map(str, old_sans)) + + msg = ("You are updating certificate {0} to include new domain(s): {1}{br}{br}" + "You are also removing previously included domain(s): {2}{br}{br}" + "Did you intend to make this change?".format( + certname, + _format_list("+", added), + _format_list("-", removed), + br=os.linesep)) + if not display_util.yesno(msg, "Update certificate", "Cancel", default=True): + raise errors.ConfigurationError("Specified mismatched certificate name and domains.") + + +def _find_sans_or_certname(config: configuration.NamespaceConfig, + installer: Optional[interfaces.Installer], + question: Optional[str] = None) -> tuple[list[san.SAN], str]: + """Retrieve domains and certname from config or user input. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param installer: Installer object + :type installer: interfaces.Installer + + :param `str` question: Overriding default question to ask the user if asked + to choose from domain names. + + :returns: Two-part tuple of domains and certname + :rtype: `tuple` of list of `str` and `str` + + :raises errors.Error: Usage message, if parameters are not used correctly + + """ + domains: list[san.SAN] = [] + ip_addresses: list[san.SAN] = [] + certname = config.certname + + # first, try to get domains from the config + if config.domains: + domains = config.domains + if config.ip_addresses: + ip_addresses = config.ip_addresses + + sans: Optional[list[san.SAN]] = domains + ip_addresses + + # if we can't do that but we have a certname, get the sans + # by loading the latest certificate with that certname + if certname and not sans: + sans = cert_manager.sans_for_certname(config, certname) + + # that certname might not have existed, or there was a problem. + # try to get domains from the user. + if not sans: + sans = san.guess(display_ops.choose_names(installer, question)) + + if not sans: + raise errors.Error("Please specify --domains, --ip-address, or --installer that " + "will help in domain names autodiscovery, or " + "--cert-name for an existing certificate name.") + + return sans, certname + + +def _report_next_steps(config: configuration.NamespaceConfig, installer_err: Optional[errors.Error], + lineage: Optional[storage.RenewableCert], + new_or_renewed_cert: bool = True) -> None: + """Displays post-run/certonly advice to the user about renewal and installation. + + The output varies by runtime configuration and any errors encountered during installation. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param installer_err: The installer/enhancement error encountered, if any. + :type error: Optional[errors.Error] + + :param lineage: The resulting certificate lineage from the issuance, if any. + :type lineage: Optional[storage.RenewableCert] + + :param bool new_or_renewed_cert: Whether the verb execution resulted in a certificate + being saved (created or renewed). + + """ + steps: list[str] = [] + + # If the installation or enhancement raised an error, show advice on trying again + if installer_err: + # Special case where either --nginx or --apache were used, causing us to + # run the "installer" (i.e. reloading the nginx/apache config) + if config.verb == 'certonly': + steps.append( + "The certificate was saved, but was not successfully loaded by the installer " + f"({config.installer}) due to the installer failing to reload. " + f"After fixing the error shown below, try reloading {config.installer} manually." + ) + else: + steps.append( + "The certificate was saved, but could not be installed (installer: " + f"{config.installer}). After fixing the error shown below, try installing it again " + f"by running:\n {cli.cli_command} install --cert-name " + f"{_cert_name_from_config_or_lineage(config, lineage)}" + ) + + # If a certificate was obtained or renewed, show applicable renewal advice + if new_or_renewed_cert: + if config.csr: + steps.append( + "Certificates created using --csr will not be renewed automatically by Certbot. " + "You will need to renew the certificate before it expires, by running the same " + "Certbot command again.") + elif _is_interactive_only_auth(config): + steps.append( + "This certificate will not be renewed automatically. Autorenewal of " + "--manual certificates requires the use of an authentication hook script " + "(--manual-auth-hook) but one was not provided. To renew this certificate, repeat " + f"this same {cli.cli_command} command before the certificate's expiry date." + ) + elif not config.preconfigured_renewal: + steps.append( + "The certificate will need to be renewed before it expires. Certbot can " + "automatically renew the certificate in the background, but you may need " + "to take steps to enable that functionality. " + "See https://certbot.org/renewal-setup for instructions.") + + if not steps: + return + + # TODO: refactor ANSI escapes during https://github.com/certbot/certbot/issues/8848 + (bold_on, nl, bold_off) = [c if sys.stdout.isatty() and not config.quiet else '' \ + for c in (util.ANSI_SGR_BOLD, '\n', util.ANSI_SGR_RESET)] + print(bold_on, end=nl) + display_util.notify("NEXT STEPS:") + print(bold_off, end='') + + for step in steps: + display_util.notify(f"- {step}") + + # If there was an installer error, segregate the error output with a trailing newline + if installer_err: + print() + + +def _report_new_cert(config: configuration.NamespaceConfig, cert_path: Optional[str], + fullchain_path: Optional[str], key_path: Optional[str] = None) -> None: + """Reports the creation of a new certificate to the user. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param cert_path: path to certificate + :type cert_path: str + + :param fullchain_path: path to full chain + :type fullchain_path: str + + :param key_path: path to private key, if available + :type key_path: str + + :returns: `None` + :rtype: None + + """ + if config.dry_run: + display_util.notify("The dry run was successful.") + return + + assert cert_path and fullchain_path, "No certificates saved to report." + + renewal_msg = "" + if config.preconfigured_renewal and not _is_interactive_only_auth(config): + renewal_msg = ("\nCertbot has set up a scheduled task to automatically renew this " + "certificate in the background.") + + display_util.notify( + ("\nSuccessfully received certificate.\n" + "Certificate is saved at: {cert_path}\n{key_msg}" + "This certificate expires on {expiry}.\n" + "These files will be updated when the certificate renews.{renewal_msg}{nl}").format( + cert_path=fullchain_path, + expiry=crypto_util.notAfter(cert_path).date(), + key_msg="Key is saved at: {}\n".format(key_path) if key_path else "", + renewal_msg=renewal_msg, + nl="\n" if config.verb == "run" else "" # Normalize spacing across verbs + ) + ) + + +def _is_interactive_only_auth(config: configuration.NamespaceConfig) -> bool: + """ Whether the current authenticator params only support interactive renewal. + """ + # --manual without --manual-auth-hook can never autorenew + if config.authenticator == "manual" and config.manual_auth_hook is None: + return True + + return False + + +def _csr_report_new_cert(config: configuration.NamespaceConfig, cert_path: Optional[str], + chain_path: Optional[str], fullchain_path: Optional[str]) -> None: + """ --csr variant of _report_new_cert. + + Until --csr is overhauled (#8332) this is transitional function to report the creation + of a new certificate using --csr. + TODO: remove this function and just call _report_new_cert when --csr is overhauled. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param str cert_path: path to cert.pem + + :param str chain_path: path to chain.pem + + :param str fullchain_path: path to fullchain.pem + + """ + if config.dry_run: + display_util.notify("The dry run was successful.") + return + + assert cert_path and fullchain_path, "No certificates saved to report." + + expiry = crypto_util.notAfter(cert_path).date() + + display_util.notify( + ("\nSuccessfully received certificate.\n" + "Certificate is saved at: {cert_path}\n" + "Intermediate CA chain is saved at: {chain_path}\n" + "Full certificate chain is saved at: {fullchain_path}\n" + "This certificate expires on {expiry}.").format( + cert_path=cert_path, chain_path=chain_path, + fullchain_path=fullchain_path, expiry=expiry, + ) + ) + + +def _determine_account(config: configuration.NamespaceConfig + ) -> tuple[account.Account, + Optional[acme_client.ClientV2]]: + """Determine which account to use. + + If ``config.account`` is ``None``, it will be updated based on the + user input. Same for ``config.email``. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :returns: Account and optionally ACME client API (biproduct of new + registration). + :rtype: tuple of :class:`certbot._internal.account.Account` and :class:`acme.client.Client` + + :raises errors.Error: If unable to register an account with ACME server + + """ + def _tos_cb(terms_of_service: str) -> None: + if config.tos: + return + msg = ("Please read the Terms of Service at: {0}\n" + "You must agree in order to register with the ACME " + "server. Do you agree?".format(terms_of_service)) + result = display_util.yesno(msg, cli_flag="--agree-tos", force_interactive=True) + if not result: + raise errors.Error( + "Registration cannot proceed without accepting " + "Terms of Service.") + + account_storage = account.AccountFileStorage(config) + acme: Optional[acme_client.ClientV2] = None + + if config.account is not None: + acc = account_storage.load(config.account) + else: + accounts = account_storage.find_all() + if len(accounts) > 1: + potential_acc = display_ops.choose_account(accounts) + if not potential_acc: + raise errors.Error("No account has been chosen.") + acc = potential_acc + elif len(accounts) == 1: + acc = accounts[0] + else: # no account registered yet + if config.email is None and not config.register_unsafely_without_email: + config.email = display_ops.get_email() + try: + acc, acme = client.register( + config, account_storage, tos_cb=_tos_cb) + display_util.notify("Account registered.") + except errors.MissingCommandlineFlag: + raise + except (errors.Error, acme_messages.Error) as err: + logger.debug("", exc_info=True) + if acme_messages.is_acme_error(err): + err_msg = internal_display_util.describe_acme_error( + cast(acme_messages.Error, err)) + err_msg = f"Error returned by the ACME server: {err_msg}" + else: + err_msg = str(err) + raise errors.Error( + f"Unable to register an account with ACME server. {err_msg}") + + config.account = acc.id + return acc, acme + + +def _delete_if_appropriate(config: configuration.NamespaceConfig) -> None: + """Does the user want to delete their now-revoked certs? If run in non-interactive mode, + deleting happens automatically. + + :param config: parsed command line arguments + :type config: configuration.NamespaceConfig + + :returns: `None` + :rtype: None + + :raises errors.Error: If anything goes wrong, including bad user input, if an overlapping + archive dir is found for the specified lineage, etc ... + """ + attempt_deletion = config.delete_after_revoke + if attempt_deletion is None: + msg = ("Would you like to delete the certificate(s) you just revoked, " + "along with all earlier and later versions of the certificate?") + attempt_deletion = display_util.yesno(msg, yes_label="Yes (recommended)", no_label="No", + force_interactive=True, default=True) + + if not attempt_deletion: + return + + # config.cert_path must have been set + # config.certname may have been set + assert config.cert_path + + if not config.certname: + config.certname = cert_manager.cert_path_to_lineage(config) + + # don't delete if the archive_dir is used by some other lineage + archive_dir = storage.full_archive_path( + configobj.ConfigObj( + storage.renewal_file_for_certname(config, config.certname), + encoding='utf-8', default_encoding='utf-8'), + config, config.certname) + try: + cert_manager.match_and_check_overlaps(config, [lambda x: archive_dir], + lambda x: x.archive_dir, lambda x: x.lineagename) + except errors.OverlappingMatchFound: + logger.warning("Not deleting revoked certificates due to overlapping archive dirs. " + "More than one certificate is using %s", archive_dir) + return + except Exception as e: + msg = ('config.default_archive_dir: {0}, config.live_dir: {1}, archive_dir: {2},' + 'original exception: {3}') + msg = msg.format(config.default_archive_dir, config.live_dir, archive_dir, e) + raise errors.Error(msg) + + cert_manager.delete(config) + + +def _init_le_client(config: configuration.NamespaceConfig, + authenticator: Optional[interfaces.Authenticator], + installer: Optional[interfaces.Installer]) -> client.Client: + """Initialize Let's Encrypt Client + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param authenticator: Acme authentication handler + :type authenticator: Optional[interfaces.Authenticator] + :param installer: Installer object + :type installer: interfaces.Installer + + :returns: client: Client object + :rtype: client.Client + + """ + acc: Optional[account.Account] + if authenticator is not None: + # if authenticator was given, then we will need account... + acc, acme = _determine_account(config) + logger.debug("Picked account: %r", acc) + else: + acc, acme = None, None + + return client.Client(config, acc, authenticator, installer, acme=acme) + + +def unregister(config: configuration.NamespaceConfig, + unused_plugins: plugins_disco.PluginsRegistry) -> Optional[str]: + """Deactivate account on server + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param unused_plugins: List of plugins (deprecated) + :type unused_plugins: plugins_disco.PluginsRegistry + + :returns: `None` or a string indicating an error + :rtype: None or str + + """ + account_storage = account.AccountFileStorage(config) + accounts = account_storage.find_all() + + if not accounts: + return f"Could not find existing account for server {config.server}." + prompt = ("Are you sure you would like to irrevocably deactivate " + "your account?") + wants_deactivate = display_util.yesno(prompt, yes_label='Deactivate', no_label='Abort', + default=True) + + if not wants_deactivate: + return "Deactivation aborted." + + acc, acme = _determine_account(config) + cb_client = client.Client(config, acc, None, None, acme=acme) + + if not cb_client.acme: + raise errors.Error("ACME client is not set.") + + # delete on boulder + cb_client.acme.deactivate_registration(acc.regr) + account_files = account.AccountFileStorage(config) + # delete local account files + account_files.delete(config.account) + + display_util.notify("Account deactivated.") + return None + + +def register(config: configuration.NamespaceConfig, + unused_plugins: plugins_disco.PluginsRegistry) -> Optional[str]: + """Create accounts on the server. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param unused_plugins: List of plugins (deprecated) + :type unused_plugins: plugins_disco.PluginsRegistry + + :returns: `None` or a string indicating an error + :rtype: None or str + + """ + # Portion of _determine_account logic to see whether accounts already + # exist or not. + account_storage = account.AccountFileStorage(config) + accounts = account_storage.find_all() + + if accounts: + # TODO: add a flag to register a duplicate account (this will + # also require extending _determine_account's behavior + # or else extracting the registration code from there) + return ("There is an existing account; registration of a " + "duplicate account with this command is currently " + "unsupported.") + # _determine_account will register an account + _determine_account(config) + return None + + +def update_account(config: configuration.NamespaceConfig, + unused_plugins: plugins_disco.PluginsRegistry) -> Optional[str]: + """Modify accounts on the server. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param unused_plugins: List of plugins (deprecated) + :type unused_plugins: plugins_disco.PluginsRegistry + + :returns: `None` or a string indicating an error + :rtype: None or str + + """ + # Portion of _determine_account logic to see whether accounts already + # exist or not. + account_storage = account.AccountFileStorage(config) + accounts = account_storage.find_all() + + if not accounts: + return f"Could not find an existing account for server {config.server}." + if config.email is None and not config.register_unsafely_without_email: + config.email = display_ops.get_email() + + acc, acme = _determine_account(config) + cb_client = client.Client(config, acc, None, None, acme=acme) + + if not cb_client.acme: + raise errors.Error("ACME client is not set.") + + # Empty list of contacts in case the user is removing all emails + acc_contacts: Iterable[str] = () + if config.email: + acc_contacts = ['mailto:' + email for email in config.email.split(',')] + # We rely on an exception to interrupt this process if it didn't work. + prev_regr_uri = acc.regr.uri + acc.regr = cb_client.acme.update_registration(acc.regr.update( + body=acc.regr.body.update(contact=acc_contacts))) + # A v1 account being used as a v2 account will result in changing the uri to + # the v2 uri. Since it's the same object on disk, put it back to the v1 uri + # so that we can also continue to use the account object with acmev1. + acc.regr = acc.regr.update(uri=prev_regr_uri) + account_storage.update_regr(acc) + + if not config.email: + display_util.notify("Any contact information associated " + "with this account has been removed.") + else: + eff.prepare_subscription(config, acc) + display_util.notify("Your e-mail address was updated to {0}.".format(config.email)) + + return None + + +def show_account(config: configuration.NamespaceConfig, + unused_plugins: plugins_disco.PluginsRegistry) -> Optional[str]: + """Fetch account info from the ACME server and show it to the user. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param unused_plugins: List of plugins (deprecated) + :type unused_plugins: plugins_disco.PluginsRegistry + + :returns: `None` or a string indicating an error + :rtype: None or str + + """ + # Portion of _determine_account logic to see whether accounts already + # exist or not. + account_storage = account.AccountFileStorage(config) + accounts = account_storage.find_all() + + if not accounts: + return f"Could not find an existing account for server {config.server}." + + acc, acme = _determine_account(config) + cb_client = client.Client(config, acc, None, None, acme=acme) + + if not cb_client.acme: + raise errors.Error("ACME client is not set.") + + regr = cb_client.acme.query_registration(acc.regr) + output = [f"Account details for server {config.server}:", + f" Account URL: {regr.uri}"] + + thumbprint = b64.b64encode(acc.key.thumbprint()).decode() + output.append(f" Account Thumbprint: {thumbprint}") + + emails = [] + + for contact in regr.body.contact: + if contact.startswith('mailto:'): + emails.append(contact[7:]) + + output.append(" Email contact{}: {}".format( + "s" if len(emails) > 1 else "", + ", ".join(emails) if len(emails) > 0 else "none")) + + display_util.notify("\n".join(output)) + + return None + + +def _cert_name_from_config_or_lineage(config: configuration.NamespaceConfig, + lineage: Optional[storage.RenewableCert]) -> Optional[str]: + if lineage: + return lineage.lineagename + elif config.certname: + return config.certname + try: + cert_name = cert_manager.cert_path_to_lineage(config) + return cert_name + except errors.Error: + pass + + return None + + +def _install_cert(config: configuration.NamespaceConfig, le_client: client.Client, + sans: list[san.SAN], lineage: Optional[storage.RenewableCert] = None) -> None: + """Install a cert + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param le_client: Client object + :type le_client: client.Client + + :param sans: List of domains + :type sans: `list` of `str` + + :param lineage: Certificate lineage object. Defaults to `None` + :type lineage: storage.RenewableCert + + :returns: `None` + :rtype: None + + """ + path_provider: Union[storage.RenewableCert, + configuration.NamespaceConfig] = lineage if lineage else config + assert path_provider.cert_path is not None + + domains, ip_addresses = san.split(sans) + if len(ip_addresses) > 0: + # Our apache and nginx plugins are currently relying on this check for a user friendly error + # message about their lack of support for IP certificates. If you're removing this check, + # please check that the plugins can process IP addresses. + raise errors.ConfigurationError("Enhancements not supported for IP address certificates") + + le_client.deploy_certificate(domains, path_provider.key_path, path_provider.cert_path, + path_provider.chain_path, path_provider.fullchain_path) + + le_client.enhance_config(domains, path_provider.chain_path) + + +def install(config: configuration.NamespaceConfig, + plugins: plugins_disco.PluginsRegistry) -> Optional[str]: + """Install a previously obtained cert in a server. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param plugins: List of plugins + :type plugins: plugins_disco.PluginsRegistry + + :returns: `None` or the error message + :rtype: None or str + + """ + # XXX: Update for renewer/RenewableCert + # FIXME: be consistent about whether errors are raised or returned from + # this function ... + + try: + installer, _ = plug_sel.choose_configurator_plugins(config, plugins, "install") + except errors.PluginSelectionError as e: + return str(e) + + custom_cert = (config.key_path and config.cert_path) + if not config.certname and not custom_cert: + certname_question = "Which certificate would you like to install?" + config.certname = cert_manager.get_certnames( + config, "install", allow_multiple=False, + custom_prompt=certname_question)[0] + + if not enhancements.are_supported(config, installer): + raise errors.NotSupportedError("One or more of the requested enhancements " + "are not supported by the selected installer") + # If cert-path is defined, populate missing (ie. not overridden) values. + # Unfortunately this can't be done in argument parser, as certificate + # manager needs the access to renewal directory paths + if config.certname: + config = _populate_from_certname(config) + elif enhancements.are_requested(config): + # Preflight config check + raise errors.ConfigurationError("One or more of the requested enhancements " + "require --cert-name to be provided") + + if config.key_path and config.cert_path: + _check_certificate_and_key(config) + sans, _ = _find_sans_or_certname(config, installer) + le_client = _init_le_client(config, authenticator=None, installer=installer) + _install_cert(config, le_client, sans) + else: + raise errors.ConfigurationError("Path to certificate or key was not defined. " + "If your certificate is managed by Certbot, please use --cert-name " + "to define which certificate you would like to install.") + + if enhancements.are_requested(config): + # In the case where we don't have certname, we have errored out already + lineage = cert_manager.lineage_for_certname(config, config.certname) + domains, ip_addresses = san.split(sans) + if ip_addresses: + raise TypeError("enhancements not supported for IP address certificates") + domains_str = [d.dns_name for d in domains] + enhancements.enable(lineage, domains_str, installer, config) + + return None + + +def _populate_from_certname(config: configuration.NamespaceConfig) -> configuration.NamespaceConfig: + """Helper function for install to populate missing config values from lineage + defined by --cert-name.""" + + lineage = cert_manager.lineage_for_certname(config, config.certname) + if not lineage: + return config + if not config.key_path: + config.key_path = lineage.key_path + if not config.cert_path: + config.cert_path = lineage.cert_path + if not config.chain_path: + config.chain_path = lineage.chain_path + if not config.fullchain_path: + config.fullchain_path = lineage.fullchain_path + return config + + +def _check_certificate_and_key(config: configuration.NamespaceConfig) -> None: + if not os.path.isfile(filesystem.realpath(config.cert_path)): + raise errors.ConfigurationError("Error while reading certificate from path " + "{0}".format(config.cert_path)) + if not os.path.isfile(filesystem.realpath(config.key_path)): + raise errors.ConfigurationError("Error while reading private key from path " + "{0}".format(config.key_path)) + + +def plugins_cmd(config: configuration.NamespaceConfig, + plugins: plugins_disco.PluginsRegistry) -> None: + """List server software plugins. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param plugins: List of plugins + :type plugins: plugins_disco.PluginsRegistry + + :returns: `None` + :rtype: None + + """ + logger.debug("Expected interfaces: %s", config.ifaces) + + ifaces = [] if config.ifaces is None else config.ifaces + filtered = plugins.visible().ifaces(ifaces) + logger.debug("Filtered plugins: %r", filtered) + + notify = functools.partial(display_util.notification, pause=False) + if not config.init and not config.prepare: + notify(str(filtered)) + return + + filtered.init(config) + logger.debug("Filtered plugins: %r", filtered) + + if not config.prepare: + notify(str(filtered)) + return + + filtered.prepare() + available = filtered.available() + logger.debug("Prepared plugins: %s", available) + notify(str(available)) + + +def enhance(config: configuration.NamespaceConfig, + plugins: plugins_disco.PluginsRegistry) -> Optional[str]: + """Add security enhancements to existing configuration + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param plugins: List of plugins + :type plugins: plugins_disco.PluginsRegistry + + :returns: `None` or a string indicating an error + :rtype: None or str + + """ + supported_enhancements = ["hsts", "redirect", "uir", "staple"] + # Check that at least one enhancement was requested on command line + oldstyle_enh = any(getattr(config, enh) for enh in supported_enhancements) + if not enhancements.are_requested(config) and not oldstyle_enh: + msg = ("Please specify one or more enhancement types to configure. To list " + "the available enhancement types, run:\n\n%s --help enhance\n") + logger.error(msg, cli.cli_command) + raise errors.MisconfigurationError("No enhancements requested, exiting.") + + try: + installer, _ = plug_sel.choose_configurator_plugins(config, plugins, "enhance") + except errors.PluginSelectionError as e: + return str(e) + + if not enhancements.are_supported(config, installer): + raise errors.NotSupportedError("One or more of the requested enhancements " + "are not supported by the selected installer") + + certname_question = ("Which certificate would you like to use to enhance " + "your configuration?") + config.certname = cert_manager.get_certnames( + config, "enhance", allow_multiple=False, + custom_prompt=certname_question)[0] + cert_sans = cert_manager.sans_for_certname(config, config.certname) + if cert_sans is None: + raise errors.Error("Could not find the list of domains for the given certificate name.") + cert_domains, ip_addresses = san.split(cert_sans) + if len(ip_addresses) > 0: + # Our apache and nginx plugins are currently relying on this check for a user friendly error + # message about their lack of support for IP certificates. If you're removing this check, + # please check that the plugins can process IP addresses. + raise errors.ConfigurationError("Enhancements not supported for IP address certificates") + + if config.noninteractive_mode: + domains = cert_domains + else: + domain_question = ("Which domain names would you like to enable the " + "selected enhancements for?") + domain_strs = display_ops.choose_values(list(map(str, cert_domains)), domain_question) + if not domain_strs: + raise errors.Error("User cancelled the domain selection. No domains " + "defined, exiting.") + domains = list(map(san.DNSName, domain_strs)) + + lineage = cert_manager.lineage_for_certname(config, config.certname) + if not lineage: + raise errors.Error("Could not find the lineage for the given certificate name.") + if not config.chain_path: + config.chain_path = lineage.chain_path + if oldstyle_enh: + le_client = _init_le_client(config, authenticator=None, installer=installer) + le_client.enhance_config(domains, config.chain_path, redirect_default=False) + if enhancements.are_requested(config): + enhancements.enable(lineage, [d.dns_name for d in domains], installer, config) + + return None + + +def rollback(config: configuration.NamespaceConfig, plugins: plugins_disco.PluginsRegistry) -> None: + """Rollback server configuration changes made during install. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param plugins: List of plugins + :type plugins: plugins_disco.PluginsRegistry + + :returns: `None` + :rtype: None + + """ + client.rollback(config.installer, config.checkpoints, config, plugins) + + +def delete(config: configuration.NamespaceConfig, + unused_plugins: plugins_disco.PluginsRegistry) -> None: + """Delete a certificate + + Use the information in the config file to delete an existing + lineage. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param unused_plugins: List of plugins (deprecated) + :type unused_plugins: plugins_disco.PluginsRegistry + + :returns: `None` + :rtype: None + + """ + cert_manager.delete(config) + + +def certificates(config: configuration.NamespaceConfig, + unused_plugins: plugins_disco.PluginsRegistry) -> None: + """Display information about certs configured with Certbot + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param unused_plugins: List of plugins (deprecated) + :type unused_plugins: plugins_disco.PluginsRegistry + + :returns: `None` + :rtype: None + + """ + cert_manager.certificates(config) + + +def revoke(config: configuration.NamespaceConfig, + unused_plugins: plugins_disco.PluginsRegistry) -> Optional[str]: + """Revoke a previously obtained certificate. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param unused_plugins: List of plugins (deprecated) + :type unused_plugins: plugins_disco.PluginsRegistry + + :returns: `None` or string indicating error in case of error + :rtype: None or str + + """ + # For user-agent construction + config.installer = config.authenticator = None + + if config.cert_path is None and config.certname: + # When revoking via --cert-name, take the cert path and server from renewalparams + lineage = storage.RenewableCert( + storage.renewal_file_for_certname(config, config.certname), config) + config.cert_path = lineage.cert_path + # --server takes priority over lineage.server + if lineage.server and not config.set_by_user("server"): + config.server = lineage.server + elif not config.cert_path or (config.cert_path and config.certname): + # intentionally not supporting --cert-path & --cert-name together, + # to avoid dealing with mismatched values + raise errors.Error("Error! Exactly one of --cert-path or --cert-name must be specified!") + + if config.key_path is not None: # revocation by cert key + logger.debug("Revoking %s using certificate key %s", + config.cert_path, config.key_path) + crypto_util.verify_cert_matches_priv_key(config.cert_path, config.key_path) + with open(config.key_path, 'rb') as f: + key = jose.JWK.load(f.read()) + acme = client.create_acme_client(config, key) + else: # revocation by account key + logger.debug("Revoking %s using Account Key", config.cert_path) + acc, _ = _determine_account(config) + acme = client.create_acme_client(config, acc.key, acc.regr) + + with open(config.cert_path, 'rb') as f: + cert = x509.load_pem_x509_certificate(f.read()) + logger.debug("Reason code for revocation: %s", config.reason) + try: + acme.revoke(cert, config.reason) + _delete_if_appropriate(config) + except acme_errors.ClientError as e: + return str(e) + + display_ops.success_revocation(config.cert_path) + return None + + +def run(config: configuration.NamespaceConfig, + plugins: plugins_disco.PluginsRegistry) -> Optional[str]: + """Obtain a certificate and install. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param plugins: List of plugins + :type plugins: plugins_disco.PluginsRegistry + + :returns: `None` + :rtype: None + + """ + # TODO: Make run as close to auth + install as possible + # Possible difficulties: config.csr was hacked into auth + try: + installer, authenticator = plug_sel.choose_configurator_plugins(config, plugins, "run") + except errors.PluginSelectionError as e: + return str(e) + + if config.must_staple and installer and "staple-ocsp" not in installer.supported_enhancements(): + raise errors.NotSupportedError( + "Must-Staple extension requested, but OCSP stapling is not supported by the selected " + f"installer ({config.installer})\n\n" + "You can either:\n" + " * remove the --must-staple option from the command line and obtain a certificate " + "without the Must-Staple extension, or;\n" + " * use the `certonly` subcommand and manually install the certificate into the " + "intended service (e.g. webserver). You must also then manually enable OCSP stapling, " + "as it is required for certificates with the Must-Staple extension to " + "function properly.\n" + " * choose a different installer plugin (such as --nginx or --apache), if possible." + ) + + # Preflight check for enhancement support by the selected installer + if not enhancements.are_supported(config, installer): + raise errors.NotSupportedError("One or more of the requested enhancements " + "are not supported by the selected installer") + + # TODO: Handle errors from _init_le_client? + le_client = _init_le_client(config, authenticator, installer) + + sans, certname = _find_sans_or_certname(config, installer) + + domains, ip_addresses = san.split(sans) + if ip_addresses: + raise errors.Error("installation of IP address certificate not supported") + + should_get_cert, lineage = _find_cert(config, sans, certname) + + new_lineage = lineage + if should_get_cert: + new_lineage = _get_and_save_cert(le_client, config, sans, + certname, lineage) + + cert_path = new_lineage.cert_path if new_lineage else None + fullchain_path = new_lineage.fullchain_path if new_lineage else None + key_path = new_lineage.key_path if new_lineage else None + + if should_get_cert: + _report_new_cert(config, cert_path, fullchain_path, key_path) + + # The installer error, if any, is being stored as a value here, in order to first print + # relevant advice in a nice way, before re-raising the error for normal processing. + installer_err: Optional[errors.Error] = None + try: + _install_cert(config, le_client, sans, new_lineage) + + if enhancements.are_requested(config) and new_lineage: + enhancements.enable(new_lineage, [d.dns_name for d in domains], installer, config) + + sans_strs = list(map(str, sans)) + if lineage is None or not should_get_cert: + display_ops.success_installation(sans_strs) + else: + display_ops.success_renewal(sans_strs) + except errors.Error as e: + installer_err = e + finally: + _report_next_steps(config, installer_err, new_lineage, + new_or_renewed_cert=should_get_cert) + # If the installer did fail, re-raise the error to bail out + if installer_err: + raise installer_err + + _suggest_donation_if_appropriate(config) + eff.handle_subscription(config, le_client.account) + return None + + +def _csr_get_and_save_cert(config: configuration.NamespaceConfig, + le_client: client.Client) -> tuple[ + Optional[str], Optional[str], Optional[str]]: + """Obtain a cert using a user-supplied CSR + + This works differently in the CSR case (for now) because we don't + have the privkey, and therefore can't construct the files for a lineage. + So we just save the cert & chain to disk :/ + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param client: Client object + :type client: client.Client + + :returns: `cert_path`, `chain_path` and `fullchain_path` as absolute + paths to the actual files, or None for each if it's a dry-run. + :rtype: `tuple` of `str` + + """ + util_csr = config.actual_csr + x509_req = x509.load_pem_x509_csr(util_csr.data) + domains, ip_addresses = san.from_x509(x509_req.subject, x509_req.extensions) + display_util.notify( + "{action} for {sans}".format( + action="Simulating a certificate request" if config.dry_run else + "Requesting a certificate", + sans=internal_display_util.summarize_sans(san.join(domains, ip_addresses)) + ) + ) + cert, chain = le_client.obtain_certificate_from_csr(util_csr) + if config.dry_run: + logger.debug( + "Dry run: skipping saving certificate to %s", config.cert_path) + return None, None, None + cert_path, chain_path, fullchain_path = le_client.save_certificate( + cert, chain, os.path.normpath(config.cert_path), + os.path.normpath(config.chain_path), os.path.normpath(config.fullchain_path)) + return cert_path, chain_path, fullchain_path + + +def renew_cert(config: configuration.NamespaceConfig, plugins: plugins_disco.PluginsRegistry, + lineage: storage.RenewableCert) -> None: + """Renew & save an existing cert. Do not install it. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param plugins: List of plugins + :type plugins: plugins_disco.PluginsRegistry + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :returns: `None` + :rtype: None + + :raises errors.PluginSelectionError: MissingCommandlineFlag if supplied parameters do not pass + + """ + # installers are used in auth mode to determine domain names + installer, auth = plug_sel.choose_configurator_plugins(config, plugins, "certonly") + le_client = _init_le_client(config, auth, installer) + + renewed_lineage = _get_and_save_cert(le_client, config, lineage=lineage) + + if not renewed_lineage: + raise errors.Error("An existing certificate for the given name could not be found.") + + if installer and not config.dry_run: + # In case of a renewal, reload server to pick up new certificate. + updater.run_renewal_deployer(config, renewed_lineage, installer) + display_util.notify(f"Reloading {config.installer} server after certificate renewal") + installer.restart() + + +def certonly(config: configuration.NamespaceConfig, plugins: plugins_disco.PluginsRegistry) -> None: + """Authenticate & obtain cert, but do not install it. + + This implements the 'certonly' subcommand. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param plugins: List of plugins + :type plugins: plugins_disco.PluginsRegistry + + :returns: `None` + :rtype: None + + :raises errors.Error: If specified plugin could not be used + + """ + # SETUP: Select plugins and construct a client instance + # installers are used in auth mode to determine domain names + installer, auth = plug_sel.choose_configurator_plugins(config, plugins, "certonly") + le_client = _init_le_client(config, auth, installer) + + if config.csr: + cert_path, chain_path, fullchain_path = _csr_get_and_save_cert(config, le_client) + _csr_report_new_cert(config, cert_path, chain_path, fullchain_path) + _report_next_steps(config, None, None, new_or_renewed_cert=not config.dry_run) + _suggest_donation_if_appropriate(config) + eff.handle_subscription(config, le_client.account) + return + + sans, certname = _find_sans_or_certname(config, installer) + should_get_cert, lineage = _find_cert(config, sans, certname) + + if not should_get_cert: + display_util.notification("Certificate not yet due for renewal; no action taken.", + pause=False) + return + + lineage = _get_and_save_cert(le_client, config, sans, certname, lineage) + + # If a new cert was issued and we were passed an installer, we can safely + # run `installer.restart()` to load the newly issued certificate + installer_err: Optional[errors.Error] = None + if lineage and installer and not config.dry_run: + logger.info("Reloading %s server after certificate issuance", config.installer) + try: + installer.restart() + except errors.Error as e: + installer_err = e + + cert_path = lineage.cert_path if lineage else None + fullchain_path = lineage.fullchain_path if lineage else None + key_path = lineage.key_path if lineage else None + _report_new_cert(config, cert_path, fullchain_path, key_path) + _report_next_steps(config, installer_err, lineage, + new_or_renewed_cert=should_get_cert and not config.dry_run) + if installer_err: + raise installer_err + _suggest_donation_if_appropriate(config) + eff.handle_subscription(config, le_client.account) + + +def renew(config: configuration.NamespaceConfig, + unused_plugins: plugins_disco.PluginsRegistry) -> None: + """Renew previously-obtained certificates. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param unused_plugins: List of plugins (deprecated) + :type unused_plugins: plugins_disco.PluginsRegistry + + :returns: `None` + :rtype: None + + """ + renewal.handle_renewal_request(config) + + +def make_or_verify_needed_dirs(config: configuration.NamespaceConfig) -> None: + """Create or verify existence of config, work, and hook directories. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :returns: `None` + :rtype: None + + """ + util.set_up_core_dir(config.config_dir, constants.CONFIG_DIRS_MODE, config.strict_permissions) + + # Ensure the working directory has the expected mode, even under stricter umask settings + with filesystem.temp_umask(0o022): + util.set_up_core_dir(config.work_dir, constants.CONFIG_DIRS_MODE, config.strict_permissions) + + hook_dirs = (config.renewal_pre_hooks_dir, + config.renewal_deploy_hooks_dir, + config.renewal_post_hooks_dir,) + for hook_dir in hook_dirs: + util.make_or_verify_dir(hook_dir, strict=config.strict_permissions) + + +def _report_reconfigure_results(renewal_file: str, orig_renewal_conf: configobj.ConfigObj) -> None: + """Reports the outcome of certificate renewal reconfiguration to the user. + + :param renewal_file: Path to the cert's renewal file + :type renewal_file: str + + :param orig_renewal_conf: Loaded original renewal configuration + :type orig_renewal_conf: configobj.ConfigObj + + :returns: `None` + :rtype: None + + """ + try: + final_renewal_conf = configobj.ConfigObj( + renewal_file, encoding='utf-8', default_encoding='utf-8') + except configobj.ConfigObjError: + raise errors.CertStorageError( + f'error parsing {renewal_file}') + + orig_renewal_params = orig_renewal_conf['renewalparams'] + final_renewal_params = final_renewal_conf['renewalparams'] + + if final_renewal_params == orig_renewal_params: + success_message = '\nNo changes were made to the renewal configuration.' + else: + success_message = '\nSuccessfully updated configuration.' + \ + '\nChanges will apply when the certificate renews.' + + display_util.notify(success_message) + + +def reconfigure(config: configuration.NamespaceConfig, + plugins: plugins_disco.PluginsRegistry) -> None: + """Allow the user to set new configuration options for an existing certificate without + forcing renewal. This can be used for things like authenticator, installer, and hooks, + but not for the domains on the cert, since those are only saved in the cert. + + :param config: Configuration object + :type config: configuration.NamespaceConfig + + :param plugins: List of plugins + :type plugins: plugins_disco.PluginsRegistry + + :raises errors.Error: if the dry run fails + :raises errors.ConfigurationError: if certificate could not be loaded + + """ + + if config.domains: + raise errors.ConfigurationError("You have specified domains, but this function cannot " + "be used to modify the domains in a certificate. If you would like to do so, follow " + "the instructions at https://certbot.org/change-cert-domain. Otherwise, remove the " + "domains from the command to continue reconfiguring. You can specify which certificate " + "you want on the command line with flag --cert-name instead.") + # While we could technically allow domains to be used to specify the certificate in addition to + # --cert-name, there's enough complexity with matching certs to domains that it's not worth it, + # to say nothing of the difficulty in explaining what exactly this subcommand can modify + + + # To make sure that the requested changes work, we're going to do a dry run, and only save + # upon success. First, modify the config as the user requested. + if not config.certname: + certname_question = "Which certificate would you like to reconfigure?" + config.certname = cert_manager.get_certnames( + config, "reconfigure", allow_multiple=False, + custom_prompt=certname_question)[0] + + certname = config.certname + + try: + renewal_file = storage.renewal_file_for_certname(config, certname) + except errors.CertStorageError: + raise errors.ConfigurationError(f"An existing certificate with name {certname} could not " + "be found. Run `certbot certificates` to list available certificates.") + + # figure this out before we modify config + if config.deploy_hook and not config.run_deploy_hooks: + msg = ("You are attempting to set a --deploy-hook. Would you like Certbot to run deploy " + "hooks when it performs a dry run with the new settings? This will run all " + "relevant deploy hooks, including directory hooks, unless --no-directory-hooks " + "is set. This will use the current active certificate, and not the temporary test " + "certificate acquired during the dry run.") + config.run_deploy_hooks = display_util.yesno(msg,"Run deploy hooks", + "Do not run deploy hooks", default=False) + + # cache previous version for later comparison + try: + orig_renewal_conf = configobj.ConfigObj( + renewal_file, encoding='utf-8', default_encoding='utf-8') + except configobj.ConfigObjError: + raise errors.CertStorageError( + f"error parsing {renewal_file}") + + lineage_config = copy.deepcopy(config) + try: + renewal_candidate = renewal.reconstitute(lineage_config, renewal_file) + except Exception as e: # pylint: disable=broad-except + raise errors.ConfigurationError(f"Renewal configuration file {renewal_file} " + f"(cert: {certname}) produced an unexpected error: {e}.") + if not renewal_candidate: + raise errors.ConfigurationError("Could not load certificate. See logs for errors.") + + renewalparams = orig_renewal_conf['renewalparams'] + # If server was set but hasn't changed and no account is loaded, + # load the old account because reconstitute won't have + if lineage_config.set_by_user('server') and lineage_config.server == renewalparams['server']\ + and lineage_config.account is None: + lineage_config.account = renewalparams['account'] + for param in ('account', 'server',): + if getattr(lineage_config, param) != renewalparams.get(param): + msg = ("Using reconfigure to change the ACME account or server is not supported. " + "If you would like to do so, use renew with the --force-renewal flag instead " + "of reconfigure. Note that doing so will count against any rate limits. For " + "more information on this method, see " + "https://certbot.org/renew-reconfiguration") + raise errors.ConfigurationError(msg) + + # this is where lineage_config gets fully filled out (e.g. --apache will set auth and installer) + installer, auth = plug_sel.choose_configurator_plugins(lineage_config, plugins, "certonly") + + # make a deep copy of lineage_config because we're about to modify it for a test dry run + dry_run_lineage_config = copy.deepcopy(lineage_config) + + # we also set noninteractive_mode to more accurately simulate renewal (since `certbot renew` + # implies noninteractive mode) and to avoid prompting the user as changes made to + # dry_run_lineage_config beyond this point will not be applied to the original lineage_config + dry_run_lineage_config.noninteractive_mode = True + dry_run_lineage_config.dry_run = True + cli.set_test_server_options("reconfigure", dry_run_lineage_config) + + le_client = _init_le_client(dry_run_lineage_config, auth, installer) + + # renews cert as dry run to test that the new values are ok + # at this point, renewal_candidate.configuration has the old values, but will use + # the values from lineage_config when doing the dry run + _get_and_save_cert(le_client, dry_run_lineage_config, certname=certname, + lineage=renewal_candidate) + + # this function will update lineage.configuration with the new values, and save it to disk + # use the pre-dry-run version + renewal_candidate.save_new_config_values(lineage_config) + + _report_reconfigure_results(renewal_file, orig_renewal_conf) + + +@contextmanager +def make_displayer(config: configuration.NamespaceConfig + ) -> Generator[Union[display_obj.NoninteractiveDisplay, + display_obj.FileDisplay], None, None]: + """Creates a display object appropriate to the flags in the supplied config. + + :param config: Configuration object + + :returns: Display object + + """ + displayer: Union[None, display_obj.NoninteractiveDisplay, + display_obj.FileDisplay] = None + devnull: Optional[IO] = None + + if config.quiet: + config.noninteractive_mode = True + devnull = open(os.devnull, "w") # pylint: disable=consider-using-with + displayer = display_obj.NoninteractiveDisplay(devnull) + elif config.noninteractive_mode: + displayer = display_obj.NoninteractiveDisplay(sys.stdout) + else: + displayer = display_obj.FileDisplay( + sys.stdout, config.force_interactive) + + try: + yield displayer + finally: + if devnull: + devnull.close() + + +def main(cli_args: Optional[list[str]] = None) -> Optional[Union[str, int]]: + """Run Certbot. + + :param cli_args: command line to Certbot, defaults to ``sys.argv[1:]`` + :type cli_args: `list` of `str` + + :returns: value for `sys.exit` about the exit status of Certbot + :rtype: `str` or `int` or `None` + + """ + if not cli_args: + cli_args = sys.argv[1:] + + log.pre_arg_parse_setup() + + if os.environ.get('CERTBOT_SNAPPED') == 'True': + cli_args = snap_config.prepare_env(cli_args) + + plugins = plugins_disco.PluginsRegistry.find_all() + logger.debug("certbot version: %s", certbot.__version__) + logger.debug("Location of certbot entry point: %s", sys.argv[0]) + # do not log `config`, as it contains sensitive data (e.g. revoke --key)! + logger.debug("Arguments: %r", cli_args) + logger.debug("Discovered plugins: %r", plugins) + + # Some releases of Windows require escape sequences to be enable explicitly + misc.prepare_virtual_console() + + # note: arg parser internally handles --help (and exits afterwards) + config = cli.prepare_and_parse_args(plugins, cli_args) + + # On windows, shell without administrative right cannot create symlinks required by certbot. + # So we check the rights before continuing. + misc.raise_for_non_administrative_windows_rights() + + try: + log.post_arg_parse_setup(config) + make_or_verify_needed_dirs(config) + except errors.Error: + # Let plugins_cmd be run as un-privileged user. + if config.func != plugins_cmd: # pylint: disable=comparison-with-callable + raise + + with make_displayer(config) as displayer: + display_obj.set_display(displayer) + + return config.func(config, plugins) diff --git a/certbot/src/certbot/_internal/ocsp.py b/certbot/src/certbot/_internal/ocsp.py new file mode 100644 index 00000000000..d2ba8175953 --- /dev/null +++ b/certbot/src/certbot/_internal/ocsp.py @@ -0,0 +1,233 @@ +"""Tools for checking certificate revocation.""" +from datetime import datetime +from datetime import timedelta +from datetime import timezone +import logging +from typing import Optional + +from cryptography import x509 +from cryptography.exceptions import InvalidSignature +from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives import serialization +from cryptography.x509 import ocsp +import requests + +from certbot import crypto_util +from certbot import errors +from certbot.interfaces import RenewableCert + +logger = logging.getLogger(__name__) + + +class RevocationChecker: + """This class figures out OCSP checking on this system, and performs it.""" + def ocsp_revoked(self, cert: RenewableCert) -> bool: + """Get revoked status for a particular cert version. + + .. todo:: Make this a non-blocking call + + :param `.interfaces.RenewableCert` cert: Certificate object + :returns: True if revoked; False if valid or the check failed or cert is expired. + :rtype: bool + + """ + return self.ocsp_revoked_by_paths(cert.cert_path, cert.chain_path) + + def ocsp_revoked_by_paths(self, cert_path: str, chain_path: str, timeout: int = 10) -> bool: + """Performs the OCSP revocation check + + :param str cert_path: Certificate filepath + :param str chain_path: Certificate chain + :param int timeout: Timeout (in seconds) for the OCSP query + + :returns: True if revoked; False if valid or the check failed or cert is expired. + :rtype: bool + + """ + # Let's Encrypt doesn't update OCSP for expired certificates, + # so don't check OCSP if the cert is expired. + # https://github.com/certbot/certbot/issues/7152 + now = datetime.now(timezone.utc) + if crypto_util.notAfter(cert_path) <= now: + return False + + url, host = _determine_ocsp_server(cert_path) + if not host or not url: + return False + + return _check_ocsp_cryptography(cert_path, chain_path, url, timeout) + + +def _determine_ocsp_server(cert_path: str) -> tuple[Optional[str], Optional[str]]: + """Extract the OCSP server host from a certificate. + + :param str cert_path: Path to the cert we're checking OCSP for + :rtype tuple: + :returns: (OCSP server URL or None, OCSP server host or None) + + """ + with open(cert_path, 'rb') as file_handler: + cert = x509.load_pem_x509_certificate(file_handler.read(), default_backend()) + try: + extension = cert.extensions.get_extension_for_class(x509.AuthorityInformationAccess) + ocsp_oid = x509.AuthorityInformationAccessOID.OCSP + descriptions = [description for description in extension.value + if description.access_method == ocsp_oid] + + url = descriptions[0].access_location.value + except (x509.ExtensionNotFound, IndexError): + logger.info("Cannot extract OCSP URI from %s", cert_path) + return None, None + + url = url.rstrip() + host = url.partition("://")[2].rstrip("/") + + if host: + return url, host + logger.info("Cannot process OCSP host from URL (%s) in certificate at %s", url, cert_path) + return None, None + + +def _check_ocsp_cryptography(cert_path: str, chain_path: str, url: str, timeout: int) -> bool: + # Retrieve OCSP response + with open(chain_path, 'rb') as file_handler: + issuer = x509.load_pem_x509_certificate(file_handler.read(), default_backend()) + with open(cert_path, 'rb') as file_handler: + cert = x509.load_pem_x509_certificate(file_handler.read(), default_backend()) + builder = ocsp.OCSPRequestBuilder() + builder = builder.add_certificate(cert, issuer, hashes.SHA1()) + request = builder.build() + request_binary = request.public_bytes(serialization.Encoding.DER) + try: + response = requests.post(url, data=request_binary, + headers={'Content-Type': 'application/ocsp-request'}, + timeout=timeout) + except requests.exceptions.RequestException: + logger.info("OCSP check failed for %s (are we offline?)", cert_path, exc_info=True) + return False + if response.status_code != 200: + logger.info("OCSP check failed for %s (HTTP status: %d)", cert_path, response.status_code) + return False + + response_ocsp = ocsp.load_der_ocsp_response(response.content) + + # Check OCSP response validity + if response_ocsp.response_status != ocsp.OCSPResponseStatus.SUCCESSFUL: + logger.warning("Invalid OCSP response status for %s: %s", + cert_path, response_ocsp.response_status) + return False + + # Check OCSP signature + try: + _check_ocsp_response(response_ocsp, request, issuer, cert_path) + except UnsupportedAlgorithm as e: + logger.warning(str(e)) + except errors.Error as e: + logger.warning(str(e)) + except InvalidSignature: + logger.warning('Invalid signature on OCSP response for %s', cert_path) + except AssertionError as error: + logger.warning('Invalid OCSP response for %s: %s.', cert_path, str(error)) + else: + # Check OCSP certificate status + logger.debug("OCSP certificate status for %s is: %s", + cert_path, response_ocsp.certificate_status) + return response_ocsp.certificate_status == ocsp.OCSPCertStatus.REVOKED + + return False + + +def _check_ocsp_response(response_ocsp: 'ocsp.OCSPResponse', request_ocsp: 'ocsp.OCSPRequest', + issuer_cert: x509.Certificate, cert_path: str) -> None: + """Verify that the OCSP is valid for several criteria""" + # Assert OCSP response corresponds to the certificate we are talking about + if response_ocsp.serial_number != request_ocsp.serial_number: + raise AssertionError('the certificate in response does not correspond ' + 'to the certificate in request') + + # Assert signature is valid + _check_ocsp_response_signature(response_ocsp, issuer_cert, cert_path) + + # Assert issuer in response is the expected one + if (not isinstance(response_ocsp.hash_algorithm, type(request_ocsp.hash_algorithm)) + or response_ocsp.issuer_key_hash != request_ocsp.issuer_key_hash + or response_ocsp.issuer_name_hash != request_ocsp.issuer_name_hash): + raise AssertionError('the issuer does not correspond to issuer of the certificate.') + + # In following checks, two situations can occur: + # * nextUpdate is set, and requirement is thisUpdate < now < nextUpdate + # * nextUpdate is not set, and requirement is thisUpdate < now + # NB1: We add a validity period tolerance to handle clock time inconsistencies, + # value is 5 min like for OpenSSL. + # NB2: Another check is to verify that thisUpdate is not too old, it is optional + # for OpenSSL, so we do not do it here. + # See OpenSSL implementation as a reference: + # https://github.com/openssl/openssl/blob/ef45aa14c5af024fcb8bef1c9007f3d1c115bd85/crypto/ocsp/ocsp_cl.c#L338-L391 + # thisUpdate/nextUpdate are expressed in UTC/GMT time zone + now = datetime.now(timezone.utc) + if not response_ocsp.this_update_utc: + raise AssertionError('param thisUpdate is not set.') + if response_ocsp.this_update_utc > now + timedelta(minutes=5): + raise AssertionError('param thisUpdate is in the future.') + if response_ocsp.next_update_utc and response_ocsp.next_update_utc < now - timedelta(minutes=5): + raise AssertionError('param nextUpdate is in the past.') + + +def _check_ocsp_response_signature(response_ocsp: 'ocsp.OCSPResponse', + issuer_cert: x509.Certificate, cert_path: str) -> None: + """Verify an OCSP response signature against certificate issuer or responder""" + def _key_hash(cert: x509.Certificate) -> bytes: + return x509.SubjectKeyIdentifier.from_public_key(cert.public_key()).digest + + if (response_ocsp.responder_name == issuer_cert.subject + or response_ocsp.responder_key_hash == _key_hash(issuer_cert)): + # Case where the OCSP responder is also the certificate issuer + logger.debug('OCSP response for certificate %s is signed by the certificate\'s issuer.', + cert_path) + responder_cert = issuer_cert + else: + # Case where the OCSP responder is not the certificate issuer + logger.debug('OCSP response for certificate %s is delegated to an external responder.', + cert_path) + + responder_certs = [cert for cert in response_ocsp.certificates + if response_ocsp.responder_name == cert.subject or \ + response_ocsp.responder_key_hash == _key_hash(cert)] + if not responder_certs: + raise AssertionError('no matching responder certificate could be found') + + # We suppose here that the ACME server support only one certificate in the OCSP status + # request. This is currently the case for LetsEncrypt servers. + # See https://github.com/letsencrypt/boulder/issues/2331 + responder_cert = responder_certs[0] + + if responder_cert.issuer != issuer_cert.subject: + raise AssertionError('responder certificate is not signed ' + 'by the certificate\'s issuer') + + try: + extension = responder_cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage) + delegate_authorized = x509.oid.ExtendedKeyUsageOID.OCSP_SIGNING in extension.value + except (x509.ExtensionNotFound, IndexError): + delegate_authorized = False + if not delegate_authorized: + raise AssertionError('responder is not authorized by issuer to sign OCSP responses') + + # Following line may raise UnsupportedAlgorithm + chosen_cert_hash = responder_cert.signature_hash_algorithm + assert chosen_cert_hash # always present for RSA and ECDSA certificates. + # For a delegate OCSP responder, we need first check that its certificate is effectively + # signed by the certificate issuer. + crypto_util.verify_signed_payload(issuer_cert.public_key(), responder_cert.signature, + responder_cert.tbs_certificate_bytes, chosen_cert_hash) + + # Following line may raise UnsupportedAlgorithm + chosen_response_hash = response_ocsp.signature_hash_algorithm + # We check that the OSCP response is effectively signed by the responder + # (an authorized delegate one or the certificate issuer itself). + if not chosen_response_hash: + raise AssertionError("no signature hash algorithm defined") + crypto_util.verify_signed_payload(responder_cert.public_key(), response_ocsp.signature, + response_ocsp.tbs_response_bytes, chosen_response_hash) diff --git a/certbot/src/certbot/_internal/plugins/__init__.py b/certbot/src/certbot/_internal/plugins/__init__.py new file mode 100644 index 00000000000..7831eab610e --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/__init__.py @@ -0,0 +1 @@ +"""Certbot plugins.""" diff --git a/certbot/src/certbot/_internal/plugins/apache/__init__.py b/certbot/src/certbot/_internal/plugins/apache/__init__.py new file mode 100644 index 00000000000..9c195ccc7aa --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/__init__.py @@ -0,0 +1 @@ +"""Certbot Apache plugin.""" diff --git a/certbot/src/certbot/_internal/plugins/apache/apache_util.py b/certbot/src/certbot/_internal/plugins/apache/apache_util.py new file mode 100644 index 00000000000..9ad8db84c1b --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/apache_util.py @@ -0,0 +1,254 @@ +""" Utility functions for certbot-apache plugin """ +import atexit +import binascii +import fnmatch +import importlib.resources +import logging +import re +import subprocess +from contextlib import ExitStack +from typing import Iterable +from typing import Optional + +from certbot import errors +from certbot import util +from certbot.compat import os + +logger = logging.getLogger(__name__) + + +def get_mod_deps(mod_name: str) -> list[str]: + """Get known module dependencies. + + .. note:: This does not need to be accurate in order for the client to + run. This simply keeps things clean if the user decides to revert + changes. + .. warning:: If all deps are not included, it may cause incorrect parsing + behavior, due to enable_mod's shortcut for updating the parser's + currently defined modules (`.ApacheParser.add_mod`) + This would only present a major problem in extremely atypical + configs that use ifmod for the missing deps. + + """ + deps = { + "ssl": ["setenvif", "mime"] + } + return deps.get(mod_name, []) + + +def get_file_path(vhost_path: str) -> Optional[str]: + """Get file path from augeas_vhost_path. + + Takes in Augeas path and returns the file name + + :param str vhost_path: Augeas virtual host path + + :returns: filename of vhost + :rtype: str + + """ + if not vhost_path or not vhost_path.startswith("/files/"): + return None + + return _split_aug_path(vhost_path)[0] + + +def get_internal_aug_path(vhost_path: str) -> str: + """Get the Augeas path for a vhost with the file path removed. + + :param str vhost_path: Augeas virtual host path + + :returns: Augeas path to vhost relative to the containing file + :rtype: str + + """ + return _split_aug_path(vhost_path)[1] + + +def _split_aug_path(vhost_path: str) -> tuple[str, str]: + """Splits an Augeas path into a file path and an internal path. + + After removing "/files", this function splits vhost_path into the + file path and the remaining Augeas path. + + :param str vhost_path: Augeas virtual host path + + :returns: file path and internal Augeas path + :rtype: `tuple` of `str` + + """ + # Strip off /files + file_path = vhost_path[6:] + internal_path: list[str] = [] + + # Remove components from the end of file_path until it becomes valid + while not os.path.exists(file_path): + file_path, _, internal_path_part = file_path.rpartition("/") + internal_path.append(internal_path_part) + + return file_path, "/".join(reversed(internal_path)) + + +def parse_define_file(filepath: str, varname: str) -> dict[str, str]: + """ Parses Defines from a variable in configuration file + + :param str filepath: Path of file to parse + :param str varname: Name of the variable + + :returns: Dict of Define:Value pairs + :rtype: `dict` + + """ + return_vars: dict[str, str] = {} + # Get list of words in the variable + a_opts = util.get_var_from_file(varname, filepath).split() + for i, v in enumerate(a_opts): + # Handle Define statements and make sure it has an argument + if v == "-D" and len(a_opts) >= i+2: + var_parts = a_opts[i+1].partition("=") + return_vars[var_parts[0]] = var_parts[2] + elif len(v) > 2 and v.startswith("-D"): + # Found var with no whitespace separator + var_parts = v[2:].partition("=") + return_vars[var_parts[0]] = var_parts[2] + return return_vars + + +def unique_id() -> str: + """ Returns an unique id to be used as a VirtualHost identifier""" + return binascii.hexlify(os.urandom(16)).decode("utf-8") + + +def included_in_paths(filepath: str, paths: Iterable[str]) -> bool: + """ + Returns true if the filepath is included in the list of paths + that may contain full paths or wildcard paths that need to be + expanded. + + :param str filepath: Filepath to check + :param list paths: List of paths to check against + + :returns: True if included + :rtype: bool + """ + return any(fnmatch.fnmatch(filepath, path) for path in paths) + + +def parse_defines(define_cmd: list[str]) -> dict[str, str]: + """ + Gets Defines from httpd process and returns a dictionary of + the defined variables. + + :param list define_cmd: httpd command to dump defines + + :returns: dictionary of defined variables + :rtype: dict + """ + + variables: dict[str, str] = {} + matches = parse_from_subprocess(define_cmd, r"Define: ([^ \n]*)") + try: + matches.remove("DUMP_RUN_CFG") + except ValueError: + return {} + + for match in matches: + # Value could also contain = so split only once + parts = match.split('=', 1) + value = parts[1] if len(parts) == 2 else '' + variables[parts[0]] = value + + return variables + + +def parse_includes(inc_cmd: list[str]) -> list[str]: + """ + Gets Include directives from httpd process and returns a list of + their values. + + :param list inc_cmd: httpd command to dump includes + + :returns: list of found Include directive values + :rtype: list of str + """ + + return parse_from_subprocess(inc_cmd, r"\(.*\) (.*)") + + +def parse_modules(mod_cmd: list[str]) -> list[str]: + """ + Get loaded modules from httpd process, and return the list + of loaded module names. + + :param list mod_cmd: httpd command to dump loaded modules + + :returns: list of found LoadModule module names + :rtype: list of str + """ + + return parse_from_subprocess(mod_cmd, r"(.*)_module") + + +def parse_from_subprocess(command: list[str], regexp: str) -> list[str]: + """Get values from stdout of subprocess command + + :param list command: Command to run + :param str regexp: Regexp for parsing + + :returns: list parsed from command output + :rtype: list + + """ + stdout = _get_runtime_cfg(command) + return re.compile(regexp).findall(stdout) + + +def _get_runtime_cfg(command: list[str]) -> str: + """ + Get runtime configuration info. + + :param command: Command to run + + :returns: stdout from command + + """ + try: + proc = subprocess.run( + command, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + check=False, + env=util.env_no_snap_for_external_calls()) + stdout, stderr = proc.stdout, proc.stderr + + except (OSError, ValueError): + logger.error( + "Error running command %s for runtime parameters!%s", + command, os.linesep) + raise errors.MisconfigurationError( + "Error accessing loaded Apache parameters: {0}".format( + command)) + # Small errors that do not impede + if proc.returncode != 0: + logger.warning("Error in checking parameter list: %s", stderr) + raise errors.MisconfigurationError( + "Apache is unable to check whether or not the module is " + "loaded because Apache is misconfigured.") + + return stdout + + +def find_ssl_apache_conf(prefix: str) -> str: + """ + Find a TLS Apache config file in the dedicated storage. + :param str prefix: prefix of the TLS Apache config file to find + :return: the path the TLS Apache config file + :rtype: str + """ + file_manager = ExitStack() + atexit.register(file_manager.close) + ref = (importlib.resources.files("certbot").joinpath("_internal") + .joinpath("plugins").joinpath("apache").joinpath("tls_configs") + .joinpath("{0}-options-ssl-apache.conf".format(prefix))) + return str(file_manager.enter_context(importlib.resources.as_file(ref))) diff --git a/certbot/src/certbot/_internal/plugins/apache/apacheparser.py b/certbot/src/certbot/_internal/plugins/apache/apacheparser.py new file mode 100644 index 00000000000..4c41d9c4177 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/apacheparser.py @@ -0,0 +1,182 @@ +""" apacheconfig implementation of the ParserNode interfaces """ +from typing import Any +from typing import Iterable +from typing import Optional + +from certbot._internal.plugins.apache import assertions +from certbot._internal.plugins.apache import interfaces +from certbot._internal.plugins.apache import parsernode_util as util +from certbot._internal.plugins.apache.interfaces import ParserNode + + +class ApacheParserNode(interfaces.ParserNode): + """ apacheconfig implementation of ParserNode interface. + + Expects metadata `ac_ast` to be passed in, where `ac_ast` is the AST provided + by parsing the equivalent configuration text using the apacheconfig library. + """ + + def __init__(self, **kwargs: Any) -> None: + # pylint: disable=unused-variable + ancestor, dirty, filepath, metadata = util.parsernode_kwargs(kwargs) + super().__init__(**kwargs) + self.ancestor = ancestor + self.filepath = filepath + self.dirty = dirty + self.metadata = metadata + self._raw: Any = self.metadata["ac_ast"] + + def save(self, msg: str) -> None: + pass # pragma: no cover + + def find_ancestors(self, name: str) -> list["ApacheParserNode"]: # pylint: disable=unused-variable + """Find ancestor BlockNodes with a given name""" + return [ApacheBlockNode(name=assertions.PASS, + parameters=assertions.PASS, + ancestor=self, + filepath=assertions.PASS, + metadata=self.metadata)] + + +class ApacheCommentNode(ApacheParserNode): + """ apacheconfig implementation of CommentNode interface """ + + def __init__(self, **kwargs: Any) -> None: + comment, kwargs = util.commentnode_kwargs(kwargs) + super().__init__(**kwargs) + self.comment = comment + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return (self.comment == other.comment and + self.dirty == other.dirty and + self.ancestor == other.ancestor and + self.metadata == other.metadata and + self.filepath == other.filepath) + return False # pragma: no cover + + +class ApacheDirectiveNode(ApacheParserNode): + """ apacheconfig implementation of DirectiveNode interface """ + + def __init__(self, **kwargs: Any) -> None: + name, parameters, enabled, kwargs = util.directivenode_kwargs(kwargs) + super().__init__(**kwargs) + self.name = name + self.parameters = parameters + self.enabled = enabled + self.include: Optional[str] = None + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return (self.name == other.name and + self.filepath == other.filepath and + self.parameters == other.parameters and + self.enabled == other.enabled and + self.dirty == other.dirty and + self.ancestor == other.ancestor and + self.metadata == other.metadata) + return False # pragma: no cover + + def set_parameters(self, _parameters: Iterable[str]) -> None: + """Sets the parameters for DirectiveNode""" + return # pragma: no cover + + +class ApacheBlockNode(ApacheDirectiveNode): + """ apacheconfig implementation of BlockNode interface """ + + def __init__(self, **kwargs: Any) -> None: + super().__init__(**kwargs) + self.children: tuple[ApacheParserNode, ...] = () + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return (self.name == other.name and + self.filepath == other.filepath and + self.parameters == other.parameters and + self.children == other.children and + self.enabled == other.enabled and + self.dirty == other.dirty and + self.ancestor == other.ancestor and + self.metadata == other.metadata) + return False # pragma: no cover + + # pylint: disable=unused-argument + def add_child_block(self, name: str, parameters: Optional[list[str]] = None, + position: Optional[int] = None) -> "ApacheBlockNode": # pragma: no cover + """Adds a new BlockNode to the sequence of children""" + new_block = ApacheBlockNode(name=assertions.PASS, + parameters=assertions.PASS, + ancestor=self, + filepath=assertions.PASS, + metadata=self.metadata) + self.children += (new_block,) + return new_block + + # pylint: disable=unused-argument + def add_child_directive(self, name: str, parameters: Optional[list[str]] = None, + position: Optional[int] = None + ) -> ApacheDirectiveNode: # pragma: no cover + """Adds a new DirectiveNode to the sequence of children""" + new_dir = ApacheDirectiveNode(name=assertions.PASS, + parameters=assertions.PASS, + ancestor=self, + filepath=assertions.PASS, + metadata=self.metadata) + self.children += (new_dir,) + return new_dir + + # pylint: disable=unused-argument + def add_child_comment( + self, name: str, parameters: Optional[int] = None, position: Optional[int] = None + ) -> ApacheCommentNode: # pragma: no cover + + """Adds a new CommentNode to the sequence of children""" + new_comment = ApacheCommentNode(comment=assertions.PASS, + ancestor=self, + filepath=assertions.PASS, + metadata=self.metadata) + self.children += (new_comment,) + return new_comment + + def find_blocks(self, name: str, exclude: bool = True) -> list["ApacheBlockNode"]: # pylint: disable=unused-argument + """Recursive search of BlockNodes from the sequence of children""" + return [ApacheBlockNode(name=assertions.PASS, + parameters=assertions.PASS, + ancestor=self, + filepath=assertions.PASS, + metadata=self.metadata)] + + def find_directives(self, name: str, exclude: bool = True) -> list[ApacheDirectiveNode]: # pylint: disable=unused-argument + """Recursive search of DirectiveNodes from the sequence of children""" + return [ApacheDirectiveNode(name=assertions.PASS, + parameters=assertions.PASS, + ancestor=self, + filepath=assertions.PASS, + metadata=self.metadata)] + + # pylint: disable=unused-argument + def find_comments(self, comment: str, exact: bool = False) -> list[ApacheCommentNode]: + """Recursive search of DirectiveNodes from the sequence of children""" + return [ApacheCommentNode(comment=assertions.PASS, # pragma: no cover + ancestor=self, + filepath=assertions.PASS, + metadata=self.metadata)] + + def delete_child(self, child: ParserNode) -> None: + """Deletes a ParserNode from the sequence of children""" + return # pragma: no cover + + def unsaved_files(self) -> list[str]: + """Returns a list of unsaved filepaths""" + return [assertions.PASS] # pragma: no cover + + def parsed_paths(self) -> list[str]: + """Returns a list of parsed configuration file paths""" + return [assertions.PASS] + + +interfaces.CommentNode.register(ApacheCommentNode) +interfaces.DirectiveNode.register(ApacheDirectiveNode) +interfaces.BlockNode.register(ApacheBlockNode) diff --git a/certbot/src/certbot/_internal/plugins/apache/assertions.py b/certbot/src/certbot/_internal/plugins/apache/assertions.py new file mode 100644 index 00000000000..32ffa198859 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/assertions.py @@ -0,0 +1,164 @@ +"""Dual parser node assertions""" +import fnmatch +from typing import Any +from typing import Iterable +from typing import Optional +from typing import Union + +from certbot._internal.plugins.apache import interfaces +from certbot._internal.plugins.apache.interfaces import CommentNode +from certbot._internal.plugins.apache.interfaces import DirectiveNode +from certbot._internal.plugins.apache.interfaces import ParserNode +from certbot._internal.plugins.apache.obj import VirtualHost + +PASS = "CERTBOT_PASS_ASSERT" + + +def assertEqual(first: ParserNode, second: ParserNode) -> None: + """ Equality assertion """ + + if isinstance(first, interfaces.CommentNode): + assertEqualComment(first, second) + elif isinstance(first, interfaces.DirectiveNode): + assertEqualDirective(first, second) + + # Do an extra interface implementation assertion, as the contents were + # already checked for BlockNode in the assertEqualDirective + if isinstance(first, interfaces.BlockNode): + assert isinstance(second, interfaces.BlockNode) + + # Skip tests if filepath includes the pass value. This is done + # because filepath is variable of the base ParserNode interface, and + # unless the implementation is actually done, we cannot assume getting + # correct results from boolean assertion for dirty + if not isPass(first.filepath) and not isPass(second.filepath): + assert first.dirty == second.dirty + # We might want to disable this later if testing with two separate + # (but identical) directory structures. + assert first.filepath == second.filepath + + +def assertEqualComment(first: ParserNode, second: ParserNode) -> None: # pragma: no cover + """ Equality assertion for CommentNode """ + + assert isinstance(first, interfaces.CommentNode) + assert isinstance(second, interfaces.CommentNode) + + if not isPass(first.comment) and not isPass(second.comment): + assert first.comment == second.comment + + +def _assertEqualDirectiveComponents(first: ParserNode, # pragma: no cover + second: ParserNode) -> None: + """ Handles assertion for instance variables for DirectiveNode and BlockNode""" + + # Enabled value cannot be asserted, because Augeas implementation + # is unable to figure that out. + # assert first.enabled == second.enabled + assert isinstance(first, DirectiveNode) + assert isinstance(second, DirectiveNode) + + if not isPass(first.name) and not isPass(second.name): + assert first.name == second.name + + if not isPass(first.parameters) and not isPass(second.parameters): + assert first.parameters == second.parameters + + +def assertEqualDirective(first: ParserNode, second: ParserNode) -> None: + """ Equality assertion for DirectiveNode """ + + assert isinstance(first, interfaces.DirectiveNode) + assert isinstance(second, interfaces.DirectiveNode) + _assertEqualDirectiveComponents(first, second) + + +def isPass(value: Any) -> bool: # pragma: no cover + """Checks if the value is set to PASS""" + if isinstance(value, bool): + return True + return PASS in value + + +def isPassDirective(block: DirectiveNode) -> bool: + """ Checks if BlockNode or DirectiveNode should pass the assertion """ + + if isPass(block.name): + return True + if isPass(block.parameters): # pragma: no cover + return True + if isPass(block.filepath): # pragma: no cover + return True + return False + + +def isPassComment(comment: CommentNode) -> bool: + """ Checks if CommentNode should pass the assertion """ + + if isPass(comment.comment): + return True + if isPass(comment.filepath): # pragma: no cover + return True + return False + + +def isPassNodeList(nodelist: list[Union[DirectiveNode, CommentNode]]) -> bool: # pragma: no cover + """ Checks if a ParserNode in the nodelist should pass the assertion, + this function is used for results of find_* methods. Unimplemented find_* + methods should return a sequence containing a single ParserNode instance + with assertion pass string.""" + + node: Optional[Union[DirectiveNode, CommentNode]] + try: + node = nodelist[0] + except IndexError: + node = None + + if not node: # pragma: no cover + return False + + if isinstance(node, interfaces.DirectiveNode): + return isPassDirective(node) + return isPassComment(node) + + +def assertEqualSimple(first: Any, second: Any) -> None: + """ Simple assertion """ + if not isPass(first) and not isPass(second): + assert first == second + + +def isEqualVirtualHost(first: VirtualHost, second: VirtualHost) -> bool: + """ + Checks that two VirtualHost objects are similar. There are some built + in differences with the implementations: VirtualHost created by ParserNode + implementation doesn't have "path" defined, as it was used for Augeas path + and that cannot obviously be used in the future. Similarly the legacy + version lacks "node" variable, that has a reference to the BlockNode for the + VirtualHost. + """ + return ( + first.name == second.name and + first.aliases == second.aliases and + first.filep == second.filep and + first.addrs == second.addrs and + first.ssl == second.ssl and + first.enabled == second.enabled and + first.modmacro == second.modmacro and + first.ancestor == second.ancestor + ) + + +def assertEqualPathsList(first: Iterable[str], second: Iterable[str]) -> None: # pragma: no cover + """ + Checks that the two lists of file paths match. This assertion allows for wildcard + paths. + """ + if any(isPass(path) for path in first): + return + if any(isPass(path) for path in second): + return + for fpath in first: + assert any(fnmatch.fnmatch(fpath, spath) for spath in second) + for spath in second: + assert any(fnmatch.fnmatch(fpath, spath) for fpath in first) diff --git a/certbot-apache/certbot_apache/augeas_lens/README b/certbot/src/certbot/_internal/plugins/apache/augeas_lens/README similarity index 100% rename from certbot-apache/certbot_apache/augeas_lens/README rename to certbot/src/certbot/_internal/plugins/apache/augeas_lens/README diff --git a/certbot-apache/certbot_apache/augeas_lens/httpd.aug b/certbot/src/certbot/_internal/plugins/apache/augeas_lens/httpd.aug similarity index 98% rename from certbot-apache/certbot_apache/augeas_lens/httpd.aug rename to certbot/src/certbot/_internal/plugins/apache/augeas_lens/httpd.aug index 5600088cf7a..6312ffaca58 100644 --- a/certbot-apache/certbot_apache/augeas_lens/httpd.aug +++ b/certbot/src/certbot/_internal/plugins/apache/augeas_lens/httpd.aug @@ -6,7 +6,7 @@ Authors: Raphael Pinson About: Reference - Online Apache configuration manual: http://httpd.apache.org/docs/trunk/ + Online Apache configuration manual: https://httpd.apache.org/docs/trunk/ About: License This file is licensed under the LGPL v2+. @@ -157,7 +157,7 @@ let argv (l:lens) = l . (sep_spc . l)* * not need to have space between them, but bare arguments do. * * Apache apparently is also happy if the last argument starts with a double - * quote, but has no corresponding closing duoble quote, which is what + * quote, but has no corresponding closing double quote, which is what * arg_dir_msg handles *) let dir_args = diff --git a/certbot/src/certbot/_internal/plugins/apache/augeasparser.py b/certbot/src/certbot/_internal/plugins/apache/augeasparser.py new file mode 100644 index 00000000000..48c971765ed --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/augeasparser.py @@ -0,0 +1,563 @@ +""" +Augeas implementation of the ParserNode interfaces. + +Augeas works internally by using XPATH notation. The following is a short example +of how this all works internally, to better understand what's going on under the +hood. + +A configuration file /etc/apache2/apache2.conf with the following content: + + # First comment line + # Second comment line + WhateverDirective whatevervalue + + DirectiveInABlock dirvalue + + SomeDirective somedirectivevalue + + AnotherDirectiveInABlock dirvalue + + # Yet another comment + + +Translates over to Augeas path notation (of immediate children), when calling +for example: aug.match("/files/etc/apache2/apache2.conf/*") + +[ + "/files/etc/apache2/apache2.conf/#comment[1]", + "/files/etc/apache2/apache2.conf/#comment[2]", + "/files/etc/apache2/apache2.conf/directive[1]", + "/files/etc/apache2/apache2.conf/ABlock[1]", + "/files/etc/apache2/apache2.conf/directive[2]", + "/files/etc/apache2/apache2.conf/ABlock[2]", + "/files/etc/apache2/apache2.conf/#comment[3]" +] + +Regardless of directives name, its key in the Augeas tree is always "directive", +with index where needed of course. Comments work similarly, while blocks +have their own key in the Augeas XPATH notation. + +It's important to note that all of the unique keys have their own indices. + +Augeas paths are case sensitive, while Apache configuration is case insensitive. +It looks like this: + + + directive value + + + Directive Value + + + directive value + + + DiReCtiVe VaLuE + + +Translates over to: + +[ + "/files/etc/apache2/apache2.conf/block[1]", + "/files/etc/apache2/apache2.conf/Block[1]", + "/files/etc/apache2/apache2.conf/block[2]", + "/files/etc/apache2/apache2.conf/bLoCk[1]", +] +""" +from typing import Any +from typing import cast +from typing import Iterable +from typing import Optional +from typing import Union + +from certbot import errors +from certbot.compat import os +from certbot._internal.plugins.apache import apache_util +from certbot._internal.plugins.apache import assertions +from certbot._internal.plugins.apache import interfaces +from certbot._internal.plugins.apache import parser +from certbot._internal.plugins.apache import parsernode_util as util + + +class AugeasParserNode(interfaces.ParserNode): + """ Augeas implementation of ParserNode interface """ + + def __init__(self, **kwargs: Any) -> None: + # pylint: disable=unused-variable + ancestor, dirty, filepath, metadata = util.parsernode_kwargs(kwargs) + super().__init__(**kwargs) + self.ancestor = ancestor + self.filepath = filepath + self.dirty = dirty + self.metadata = metadata + self.parser = cast(parser.ApacheParser, + self.metadata.get("augeasparser")) + try: + if self.metadata["augeaspath"].endswith("/"): + raise errors.PluginError( + "Augeas path: {} has a trailing slash".format( + self.metadata["augeaspath"] + ) + ) + except KeyError: + raise errors.PluginError("Augeas path is required") + + def save(self, msg: Iterable[str]) -> None: + self.parser.save(msg) + + def find_ancestors(self, name: str) -> list["AugeasParserNode"]: + """ + Searches for ancestor BlockNodes with a given name. + + :param str name: Name of the BlockNode parent to search for + + :returns: List of matching ancestor nodes. + :rtype: list of AugeasParserNode + """ + + ancestors: list["AugeasParserNode"] = [] + + parent = self.metadata["augeaspath"] + while True: + # Get the path of ancestor node + parent = parent.rpartition("/")[0] + # Root of the tree + if not parent or parent == "/files": + break + anc = self._create_blocknode(parent) + if anc.name is not None and anc.name.lower() == name.lower(): + ancestors.append(anc) + + return ancestors + + def _create_blocknode(self, path: str) -> "AugeasBlockNode": + """ + Helper function to create a BlockNode from Augeas path. This is used by + AugeasParserNode.find_ancestors and AugeasBlockNode. + and AugeasBlockNode.find_blocks + + """ + + name: str = self._aug_get_name(path) + metadata: dict[str, Union[parser.ApacheParser, str]] = { + "augeasparser": self.parser, "augeaspath": path + } + + # Check if the file was included from the root config or initial state + file_path = apache_util.get_file_path(path) + if file_path is None: + raise ValueError(f"No file path found for vhost: {path}.") # pragma: no cover + + enabled = self.parser.parsed_in_original(file_path) + + return AugeasBlockNode(name=name, + enabled=enabled, + ancestor=assertions.PASS, + filepath=file_path, + metadata=metadata) + + def _aug_get_name(self, path: str) -> str: + """ + Helper function to get name of a configuration block or variable from path. + """ + + # Remove the ending slash if any + if path[-1] == "/": # pragma: no cover + path = path[:-1] + + # Get the block name + name = path.split("/")[-1] + + # remove [...], it's not allowed in Apache configuration and is used + # for indexing within Augeas + return name.split("[")[0] + + +class AugeasCommentNode(AugeasParserNode): + """ Augeas implementation of CommentNode interface """ + + def __init__(self, **kwargs: Any) -> None: + comment, kwargs = util.commentnode_kwargs(kwargs) # pylint: disable=unused-variable + super().__init__(**kwargs) + self.comment = comment + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return (self.comment == other.comment and + self.filepath == other.filepath and + self.dirty == other.dirty and + self.ancestor == other.ancestor and + self.metadata == other.metadata) + return False + + +class AugeasDirectiveNode(AugeasParserNode): + """ Augeas implementation of DirectiveNode interface """ + + def __init__(self, **kwargs: Any) -> None: + name, parameters, enabled, kwargs = util.directivenode_kwargs(kwargs) + super().__init__(**kwargs) + self.name = name + self.enabled = enabled + if parameters: + self.set_parameters(parameters) + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return (self.name == other.name and + self.filepath == other.filepath and + self.parameters == other.parameters and + self.enabled == other.enabled and + self.dirty == other.dirty and + self.ancestor == other.ancestor and + self.metadata == other.metadata) + return False + + def set_parameters(self, parameters: Iterable[str]) -> None: + """ + Sets parameters of a DirectiveNode or BlockNode object. + + :param list parameters: List of all parameters for the node to set. + """ + orig_params = self._aug_get_params(self.metadata["augeaspath"]) + + # Clear out old parameters + for _ in orig_params: + # When the first parameter is removed, the indices get updated + param_path = "{}/arg[1]".format(self.metadata["augeaspath"]) + self.parser.aug.remove(param_path) + # Insert new ones + for pi, param in enumerate(parameters): + param_path = "{}/arg[{}]".format(self.metadata["augeaspath"], pi+1) + self.parser.aug.set(param_path, param) + + @property + def parameters(self) -> tuple[str, ...]: + """ + Fetches the parameters from Augeas tree, ensuring that the sequence always + represents the current state + + :returns: Tuple of parameters for this DirectiveNode + :rtype: tuple: + """ + return tuple(self._aug_get_params(self.metadata["augeaspath"])) + + def _aug_get_params(self, path: str) -> list[str]: + """Helper function to get parameters for DirectiveNodes and BlockNodes""" + + arg_paths = self.parser.aug.match(path + "/arg") + args = [self.parser.get_arg(apath) for apath in arg_paths] + return [arg for arg in args if arg is not None] + + +class AugeasBlockNode(AugeasDirectiveNode): + """ Augeas implementation of BlockNode interface """ + + def __init__(self, **kwargs: Any) -> None: + super().__init__(**kwargs) + self.children: tuple["AugeasBlockNode", ...] = () + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return (self.name == other.name and + self.filepath == other.filepath and + self.parameters == other.parameters and + self.children == other.children and + self.enabled == other.enabled and + self.dirty == other.dirty and + self.ancestor == other.ancestor and + self.metadata == other.metadata) + return False + + # pylint: disable=unused-argument + def add_child_block(self, name: str, # pragma: no cover + parameters: Optional[list[str]] = None, + position: Optional[int] = None) -> "AugeasBlockNode": + """Adds a new BlockNode to the sequence of children""" + + insertpath, realpath, before = self._aug_resolve_child_position( + name, + position + ) + new_metadata: dict[str, Any] = {"augeasparser": self.parser, "augeaspath": realpath} + + # Create the new block + self.parser.aug.insert(insertpath, name, before) + # Check if the file was included from the root config or initial state + file_path = apache_util.get_file_path(realpath) + if file_path is None: + raise errors.Error(f"No file path found for vhost: {realpath}") # pragma: no cover + enabled = self.parser.parsed_in_original(file_path) + + # Parameters will be set at the initialization of the new object + return AugeasBlockNode( + name=name, + parameters=parameters, + enabled=enabled, + ancestor=assertions.PASS, + filepath=file_path, + metadata=new_metadata, + ) + + # pylint: disable=unused-argument + def add_child_directive(self, name: str, # pragma: no cover + parameters: Optional[list[str]] = None, + position: Optional[int] = None) -> AugeasDirectiveNode: + """Adds a new DirectiveNode to the sequence of children""" + + if not parameters: + raise errors.PluginError("Directive requires parameters and none were set.") + + insertpath, realpath, before = self._aug_resolve_child_position( + "directive", + position + ) + new_metadata = {"augeasparser": self.parser, "augeaspath": realpath} + + # Create the new directive + self.parser.aug.insert(insertpath, "directive", before) + # Set the directive key + self.parser.aug.set(realpath, name) + # Check if the file was included from the root config or initial state + file_path = apache_util.get_file_path(realpath) + if file_path is None: + raise errors.Error(f"No file path found for vhost: {realpath}") # pragma: no cover + enabled = self.parser.parsed_in_original(file_path) + + return AugeasDirectiveNode( + name=name, + parameters=parameters, + enabled=enabled, + ancestor=assertions.PASS, + filepath=file_path, + metadata=new_metadata, + ) + + def add_child_comment( + self, comment: str = "", position: Optional[int] = None + ) -> "AugeasCommentNode": + """Adds a new CommentNode to the sequence of children""" + + insertpath, realpath, before = self._aug_resolve_child_position( + "#comment", + position + ) + new_metadata: dict[str, Any] = { + "augeasparser": self.parser, "augeaspath": realpath, + } + + # Create the new comment + self.parser.aug.insert(insertpath, "#comment", before) + # Set the comment content + self.parser.aug.set(realpath, comment) + + return AugeasCommentNode( + comment=comment, + ancestor=assertions.PASS, + filepath=apache_util.get_file_path(realpath), + metadata=new_metadata, + ) + + def find_blocks(self, name: str, exclude: bool = True) -> list["AugeasBlockNode"]: + """Recursive search of BlockNodes from the sequence of children""" + + nodes: list["AugeasBlockNode"] = [] + paths: Iterable[str] = self._aug_find_blocks(name) + if exclude: + paths = self.parser.exclude_dirs(paths) + for path in paths: + nodes.append(self._create_blocknode(path)) + + return nodes + + def find_directives(self, name: str, exclude: bool = True) -> list["AugeasDirectiveNode"]: + """Recursive search of DirectiveNodes from the sequence of children""" + + nodes = [] + ownpath = self.metadata.get("augeaspath") + + directives = self.parser.find_dir(name, start=ownpath, exclude=exclude) + already_parsed: set[str] = set() + for directive in directives: + # Remove the /arg part from the Augeas path + directive = directive.partition("/arg")[0] + # find_dir returns an object for each _parameter_ of a directive + # so we need to filter out duplicates. + if directive not in already_parsed: + nodes.append(self._create_directivenode(directive)) + already_parsed.add(directive) + + return nodes + + def find_comments(self, comment: str) -> list["AugeasCommentNode"]: + """ + Recursive search of DirectiveNodes from the sequence of children. + + :param str comment: Comment content to search for. + """ + + nodes: list["AugeasCommentNode"] = [] + ownpath = self.metadata.get("augeaspath") + + comments = self.parser.find_comments(comment, start=ownpath) + for com in comments: + nodes.append(self._create_commentnode(com)) + + return nodes + + def delete_child(self, child: "AugeasParserNode") -> None: + """ + Deletes a ParserNode from the sequence of children, and raises an + exception if it's unable to do so. + :param AugeasParserNode child: A node to delete. + """ + if not self.parser.aug.remove(child.metadata["augeaspath"]): + + raise errors.PluginError( + ("Could not delete child node, the Augeas path: {} doesn't " + + "seem to exist.").format(child.metadata["augeaspath"]) + ) + + def unsaved_files(self) -> set[str]: + """Returns a list of unsaved filepaths""" + return self.parser.unsaved_files() + + def parsed_paths(self) -> list[str]: + """ + Returns a list of file paths that have currently been parsed into the parser + tree. The returned list may include paths with wildcard characters, for + example: ['/etc/apache2/conf.d/*.load'] + + This is typically called on the root node of the ParserNode tree. + + :returns: list of file paths of files that have been parsed + """ + + res_paths: list[str] = [] + + paths = self.parser.existing_paths + for directory in paths: + for filename in paths[directory]: + res_paths.append(os.path.join(directory, filename)) + + return res_paths + + def _create_commentnode(self, path: str) -> "AugeasCommentNode": + """Helper function to create a CommentNode from Augeas path""" + + comment = self.parser.aug.get(path) + metadata = {"augeasparser": self.parser, "augeaspath": path} + + # Because of the dynamic nature of AugeasParser and the fact that we're + # not populating the complete node tree, the ancestor has a dummy value + return AugeasCommentNode(comment=comment, + ancestor=assertions.PASS, + filepath=apache_util.get_file_path(path), + metadata=metadata) + + def _create_directivenode(self, path: str) -> "AugeasDirectiveNode": + """Helper function to create a DirectiveNode from Augeas path""" + + name = self.parser.get_arg(path) + metadata: dict[str, Union[parser.ApacheParser, str]] = { + "augeasparser": self.parser, "augeaspath": path, + } + + # Check if the file was included from the root config or initial state + enabled: bool = self.parser.parsed_in_original( + apache_util.get_file_path(path) + ) + return AugeasDirectiveNode(name=name, + ancestor=assertions.PASS, + enabled=enabled, + filepath=apache_util.get_file_path(path), + metadata=metadata) + + def _aug_find_blocks(self, name: str) -> set[str]: + """Helper function to perform a search to Augeas DOM tree to search + configuration blocks with a given name""" + + # The code here is modified from configurator.get_virtual_hosts() + blk_paths: set[str] = set() + for vhost_path in list(self.parser.parser_paths): + paths = self.parser.aug.match( + ("/files%s//*[label()=~regexp('%s')]" % + (vhost_path, parser.case_i(name)))) + blk_paths.update([path for path in paths if + name.lower() in os.path.basename(path).lower()]) + return blk_paths + + def _aug_resolve_child_position( + self, name: str, position: Optional[int]) -> tuple[str, str, bool]: + """ + Helper function that iterates through the immediate children and figures + out the insertion path for a new AugeasParserNode. + + Augeas also generalizes indices for directives and comments, simply by + using "directive" or "comment" respectively as their names. + + This function iterates over the existing children of the AugeasBlockNode, + returning their insertion path, resulting Augeas path and if the new node + should be inserted before or after the returned insertion path. + + Note: while Apache is case insensitive, Augeas is not, and blocks like + Nameofablock and NameOfABlock have different indices. + + :param str name: Name of the AugeasBlockNode to insert, "directive" for + AugeasDirectiveNode or "comment" for AugeasCommentNode + :param int position: The position to insert the child AugeasParserNode to + + :returns: Tuple of insert path, resulting path and a boolean if the new + node should be inserted before it. + :rtype: tuple of str, str, bool + """ + + # Default to appending + before: bool = False + + all_children: str = self.parser.aug.match("{}/*".format( + self.metadata["augeaspath"]) + ) + + # Calculate resulting_path + # Augeas indices start at 1. We use counter to calculate the index to + # be used in resulting_path. + counter: int = 1 + for i, child in enumerate(all_children): + if position is not None and i >= position: + # We're not going to insert the new node to an index after this + break + childname = self._aug_get_name(child) + if name == childname: + counter += 1 + + resulting_path: str = "{}/{}[{}]".format( + self.metadata["augeaspath"], + name, + counter + ) + + # Form the correct insert_path + # Inserting the only child and appending as the last child work + # similarly in Augeas. + append = not all_children or position is None or position >= len(all_children) + if append: + insert_path = "{}/*[last()]".format( + self.metadata["augeaspath"] + ) + elif position == 0: + # Insert as the first child, before the current first one. + insert_path = all_children[0] + before = True + else: + insert_path = "{}/*[{}]".format( + self.metadata["augeaspath"], + position + ) + + return insert_path, resulting_path, before + + +interfaces.CommentNode.register(AugeasCommentNode) +interfaces.DirectiveNode.register(AugeasDirectiveNode) +interfaces.BlockNode.register(AugeasBlockNode) diff --git a/certbot/src/certbot/_internal/plugins/apache/configurator.py b/certbot/src/certbot/_internal/plugins/apache/configurator.py new file mode 100644 index 00000000000..2e10e9b56f4 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/configurator.py @@ -0,0 +1,2701 @@ +"""Apache Configurator.""" +# pylint: disable=too-many-lines +from collections import defaultdict +import copy +import fnmatch +import logging +import re +import socket +import time +from typing import Any +from typing import Callable +from typing import cast +from typing import Iterable +from typing import Optional +from typing import Sequence +from typing import Union + +from acme import challenges +from certbot import achallenges +from certbot import errors +from certbot import util +from certbot.compat import filesystem +from certbot.compat import os +from certbot.display import util as display_util +from certbot.interfaces import RenewableCert +from certbot.plugins import common +from certbot.plugins.enhancements import AutoHSTSEnhancement +from certbot.plugins.util import path_surgery +from certbot._internal.plugins.apache import apache_util +from certbot._internal.plugins.apache import assertions +from certbot._internal.plugins.apache import constants +from certbot._internal.plugins.apache import display_ops +from certbot._internal.plugins.apache import dualparser +from certbot._internal.plugins.apache import http_01 +from certbot._internal.plugins.apache import obj +from certbot._internal.plugins.apache import parser +from certbot._internal.plugins.apache.apacheparser import ApacheBlockNode + +try: + import apacheconfig + HAS_APACHECONFIG = True +except ImportError: # pragma: no cover + HAS_APACHECONFIG = False + + +logger = logging.getLogger(__name__) + + +class OsOptions: + """ + Dedicated class to describe the OS specificities (eg. paths, binary names) + that the Apache configurator needs to be aware to operate properly. + """ + def __init__(self, + server_root: str = "/etc/apache2", + vhost_root: str = "/etc/apache2/sites-available", + vhost_files: str = "*", + logs_root: str = "/var/log/apache2", + ctl: str = "apache2ctl", + version_cmd: Optional[list[str]] = None, + restart_cmd: Optional[list[str]] = None, + restart_cmd_alt: Optional[list[str]] = None, + conftest_cmd: Optional[list[str]] = None, + enmod: Optional[str] = None, + dismod: Optional[str] = None, + le_vhost_ext: str = "-le-ssl.conf", + handle_modules: bool = False, + handle_sites: bool = False, + challenge_location: str = "/etc/apache2", + apache_bin: Optional[str] = None, + ) -> None: + self.server_root = server_root + self.vhost_root = vhost_root + self.vhost_files = vhost_files + self.logs_root = logs_root + self.ctl = ctl + self.version_cmd = ['apache2ctl', '-v'] if not version_cmd else version_cmd + self.restart_cmd = ['apache2ctl', 'graceful'] if not restart_cmd else restart_cmd + self.restart_cmd_alt = restart_cmd_alt + self.conftest_cmd = ['apache2ctl', 'configtest'] if not conftest_cmd else conftest_cmd + syntax_tests_cmd_base = [ctl, '-t', '-D'] + self.get_defines_cmd = syntax_tests_cmd_base + ['DUMP_RUN_CFG'] + self.get_includes_cmd = syntax_tests_cmd_base + ['DUMP_INCLUDES'] + self.get_modules_cmd = syntax_tests_cmd_base + ['DUMP_MODULES'] + self.enmod = enmod + self.dismod = dismod + self.le_vhost_ext = le_vhost_ext + self.handle_modules = handle_modules + self.handle_sites = handle_sites + self.challenge_location = challenge_location + self.bin = apache_bin + + +# TODO: Augeas sections ie. , beginning and closing +# tags need to be the same case, otherwise Augeas doesn't recognize them. +# This is not able to be completely remedied by regular expressions because +# Augeas views as an error. This will just +# require another check_parsing_errors() after all files are included... +# (after a find_directive search is executed currently). It can be a one +# time check however because all of LE's transactions will ensure +# only properly formed sections are added. + +# Note: This protocol works for filenames with spaces in it, the sites are +# properly set up and directives are changed appropriately, but Apache won't +# recognize names in sites-enabled that have spaces. These are not added to the +# Apache configuration. It may be wise to warn the user if they are trying +# to use vhost filenames that contain spaces and offer to change ' ' to '_' + +# Note: FILEPATHS and changes to files are transactional. They are copied +# over before the updates are made to the existing files. NEW_FILES is +# transactional due to the use of register_file_creation() + + +# TODO: Verify permissions on configuration root... it is easier than +# checking permissions on each of the relative directories and less error +# prone. +# TODO: Write a server protocol finder. Listen or +# Protocol . This can verify partial setups are correct +# TODO: Add directives to sites-enabled... not sites-available. +# sites-available doesn't allow immediate find_dir search even with save() +# and load() +class ApacheConfigurator(common.Configurator): + """Apache configurator. + + :ivar config: Configuration. + :type config: certbot.configuration.NamespaceConfig + + :ivar parser: Handles low level parsing + :type parser: :class:`~certbot._internal.plugins.apache.parser` + + :ivar tup version: version of Apache + :ivar list vhosts: All vhosts found in the configuration + (:class:`list` of :class:`~certbot._internal.plugins.apache.obj.VirtualHost`) + + :ivar dict assoc: Mapping between domains and vhosts + + """ + + description: str = "Apache Web Server plugin" + if os.environ.get("CERTBOT_DOCS") == "1": + description += ( # pragma: no cover + " (Please note that the default values of the Apache plugin options" + " change depending on the operating system Certbot is run on.)" + ) + + OS_DEFAULTS: OsOptions = OsOptions() + + def pick_apache_config(self, warn_on_no_mod_ssl: bool = True) -> str: + """ + Pick the appropriate TLS Apache configuration file for current version of Apache and OS. + + :param bool warn_on_no_mod_ssl: True if we should warn if mod_ssl is not found. + + :return: the path to the TLS Apache configuration file to use + :rtype: str + """ + # Disabling TLS session tickets is supported by Apache 2.4.11+ and OpenSSL 1.0.2l+. + # So for old versions of Apache we pick a configuration without this option. + min_openssl_version = util.parse_loose_version('1.0.2l') + openssl_version = self.openssl_version(warn_on_no_mod_ssl) + if self.version < (2, 4, 11) or not openssl_version or \ + util.parse_loose_version(openssl_version) < min_openssl_version: + # If we're supposed to warn about failing to find mod_ssl, we already did it in the + # openssl_version function and don't need to do it again here. + if openssl_version is not None: + logger.warning('Certbot has detected that apache version < 2.4.11 or compiled ' + 'against openssl < 1.0.2l. Since these are deprecated, the configuration file ' + 'being installed at %s will not receive future updates. To get the latest ' + 'configuration version, update apache.', + self.mod_ssl_conf) + return apache_util.find_ssl_apache_conf("old") + return apache_util.find_ssl_apache_conf("current") + + def _override_cmds(self) -> None: + """ + Set our various command binaries to whatever the user has overridden for apachectl + """ + self.options.version_cmd[0] = self.options.ctl + self.options.restart_cmd[0] = self.options.ctl + self.options.conftest_cmd[0] = self.options.ctl + self.options.get_modules_cmd[0] = self.options.ctl + self.options.get_includes_cmd[0] = self.options.ctl + self.options.get_defines_cmd[0] = self.options.ctl + + def _prepare_options(self) -> None: + """ + Set the values possibly changed by command line parameters to + OS_DEFAULTS constant dictionary + """ + opts = ["enmod", "dismod", "le_vhost_ext", "server_root", "vhost_root", + "logs_root", "challenge_location", "handle_modules", "handle_sites", + "ctl", "bin"] + for o in opts: + # Config options use dashes instead of underscores + if self.conf(o.replace("_", "-")) is not None: + setattr(self.options, o, self.conf(o.replace("_", "-"))) + else: + setattr(self.options, o, getattr(self.OS_DEFAULTS, o)) + + self._override_cmds() + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None]) -> None: + # When adding, modifying or deleting command line arguments, be sure to + # include the changes in the list used in method _prepare_options() to + # ensure consistent behavior. + + # Respect CERTBOT_DOCS environment variable and use default values from + # base class regardless of the underlying distribution (overrides). + if os.environ.get("CERTBOT_DOCS") == "1": + DEFAULTS = ApacheConfigurator.OS_DEFAULTS + else: + # cls.OS_DEFAULTS can be distribution specific, see override classes + DEFAULTS = cls.OS_DEFAULTS + add("enmod", default=DEFAULTS.enmod, + help="Path to the Apache 'a2enmod' binary") + add("dismod", default=DEFAULTS.dismod, + help="Path to the Apache 'a2dismod' binary") + add("le-vhost-ext", default=DEFAULTS.le_vhost_ext, + help="SSL vhost configuration extension") + add("server-root", default=DEFAULTS.server_root, + help="Apache server root directory") + add("vhost-root", default=None, + help="Apache server VirtualHost configuration root") + add("logs-root", default=DEFAULTS.logs_root, + help="Apache server logs directory") + add("challenge-location", + default=DEFAULTS.challenge_location, + help="Directory path for challenge configuration") + add("handle-modules", default=DEFAULTS.handle_modules, + help="Let installer handle enabling required modules for you " + + "(Only Ubuntu/Debian currently)") + add("handle-sites", default=DEFAULTS.handle_sites, + help="Let installer handle enabling sites for you " + + "(Only Ubuntu/Debian currently)") + add("ctl", default=DEFAULTS.ctl, + help="Full path to Apache control script") + add("bin", default=DEFAULTS.bin, + help="Full path to apache2/httpd binary") + + def __init__(self, *args: Any, **kwargs: Any) -> None: + """Initialize an Apache Configurator. + + :param tup version: version of Apache as a tuple (2, 4, 7) + (used mostly for unittesting) + + """ + version = kwargs.pop("version", None) + use_parsernode = kwargs.pop("use_parsernode", False) + openssl_version = kwargs.pop("openssl_version", None) + super().__init__(*args, **kwargs) + + # Add name_server association dict + self.assoc: dict[str, obj.VirtualHost] = {} + # Outstanding challenges + self._chall_out: set[achallenges.AnnotatedChallenge] = set() + # List of vhosts configured per wildcard domain on this run. + # used by deploy_cert() and enhance() + self._wildcard_vhosts: dict[str, list[obj.VirtualHost]] = {} + # Maps enhancements to vhosts we've enabled the enhancement for + self._enhanced_vhosts: defaultdict[str, set[obj.VirtualHost]] = defaultdict(set) + # Temporary state for AutoHSTS enhancement + self._autohsts: dict[str, dict[str, Union[int, float]]] = {} + # Reverter save notes + self.save_notes: str = "" + # Should we use ParserNode implementation instead of the old behavior + self.USE_PARSERNODE = use_parsernode + # Saves the list of file paths that were parsed initially, and + # not added to parser tree by self.conf("vhost-root") for example. + self.parsed_paths: list[str] = [] + # These will be set in the prepare function + self._prepared: bool = False + self.parser: parser.ApacheParser + self.parser_root: Optional[dualparser.DualBlockNode] = None + self.version = version + self._openssl_version: Optional[str] = openssl_version + self.vhosts: list[obj.VirtualHost] + self.options = copy.deepcopy(self.OS_DEFAULTS) + self._enhance_func: dict[str, Callable[[obj.VirtualHost, Any], None]] = { + "redirect": self._enable_redirect, + "ensure-http-header": self._set_http_header, + "staple-ocsp": self._enable_ocsp_stapling, + } + + @property + def mod_ssl_conf(self) -> str: + """Full absolute path to SSL configuration file.""" + return os.path.join(self.config.config_dir, constants.MOD_SSL_CONF_DEST) + + @property + def updated_mod_ssl_conf_digest(self) -> str: + """Full absolute path to digest of updated SSL configuration file.""" + return os.path.join(self.config.config_dir, constants.UPDATED_MOD_SSL_CONF_DIGEST) + + def _open_module_file(self, ssl_module_location: str) -> Optional[bytes]: + """Extract the open lines of openssl_version for testing purposes""" + try: + with open(ssl_module_location, mode="rb") as f: + contents = f.read() + except OSError as error: + logger.debug(str(error), exc_info=True) + return None + return contents + + def openssl_version(self, warn_on_no_mod_ssl: bool = True) -> Optional[str]: + """Lazily retrieve openssl version + + :param bool warn_on_no_mod_ssl: `True` if we should warn if mod_ssl is not found. Set to + `False` when we know we'll try to enable mod_ssl later. This is currently debian/ubuntu, + when called from `prepare`. + + :return: the OpenSSL version as a string, or None. + :rtype: str or None + """ + if self._openssl_version: + return self._openssl_version + # Step 1. Determine the location of ssl_module + try: + ssl_module_location = self.parser.modules['ssl_module'] + except KeyError: + if warn_on_no_mod_ssl: + logger.warning("Could not find ssl_module; not disabling session tickets.") + return None + if ssl_module_location: + # Possibility A: ssl_module is a DSO + ssl_module_location = self.parser.standard_path_from_server_root(ssl_module_location) + else: + # Possibility B: ssl_module is statically linked into Apache + if self.options.bin: + ssl_module_location = self.options.bin + else: + logger.warning("ssl_module is statically linked but --apache-bin is " + "missing; not disabling session tickets.") + return None + # Step 2. Grep in the binary for openssl version + contents = self._open_module_file(ssl_module_location) + if not contents: + logger.warning("Unable to read ssl_module file; not disabling session tickets.") + return None + # looks like: OpenSSL 1.0.2s 28 May 2019 + matches = re.findall(br"OpenSSL ([0-9]\.[^ ]+) ", contents) + if not matches: + logger.warning("Could not find OpenSSL version; not disabling session tickets.") + return None + self._openssl_version = matches[0].decode('UTF-8') + return self._openssl_version + + def prepare(self) -> None: + """Prepare the authenticator/installer. + + :raises .errors.NoInstallationError: If Apache configs cannot be found + :raises .errors.MisconfigurationError: If Apache is misconfigured + :raises .errors.NotSupportedError: If Apache version is not supported + :raises .errors.PluginError: If there is any other error + + """ + self._prepare_options() + + # Verify Apache is installed + self._verify_exe_availability(self.options.ctl) + + # Make sure configuration is valid + self.config_test() + + # Set Version + if self.version is None: + self.version = self.get_version() + logger.debug('Apache version is %s', + '.'.join(str(i) for i in self.version)) + if self.version < (2, 4): + raise errors.NotSupportedError( + "Apache Version {0} not supported.".format(str(self.version))) + + # Recover from previous crash before Augeas initialization to have the + # correct parse tree from the get go. + self.recovery_routine() + # Perform the actual Augeas initialization to be able to react + self.parser = self.get_parser() + + # Set up ParserNode root + pn_meta = {"augeasparser": self.parser, + "augeaspath": self.parser.get_root_augpath(), + "ac_ast": None} + if self.USE_PARSERNODE: + parser_root = self.get_parsernode_root(pn_meta) + self.parser_root = parser_root + self.parsed_paths = parser_root.parsed_paths() + + # Check for errors in parsing files with Augeas + self.parser.check_parsing_errors("httpd.aug") + + # Get all of the available vhosts + self.vhosts = self.get_virtual_hosts() + + # We may try to enable mod_ssl later. If so, we shouldn't warn if we can't find it now. + # This is currently only true for debian/ubuntu. + warn_on_no_mod_ssl = not self.options.handle_modules + self.install_ssl_options_conf(self.mod_ssl_conf, + self.updated_mod_ssl_conf_digest, + warn_on_no_mod_ssl) + + # Prevent two Apache plugins from modifying a config at once + try: + util.lock_dir_until_exit(self.options.server_root) + except (OSError, errors.LockError): + logger.debug("Encountered error:", exc_info=True) + raise errors.PluginError( + "Unable to create a lock file in {0}. Are you running" + " Certbot with sufficient privileges to modify your" + " Apache configuration?".format(self.options.server_root)) + self._prepared = True + + def save(self, title: Optional[str] = None, temporary: bool = False) -> None: + """Saves all changes to the configuration files. + + This function first checks for save errors, if none are found, + all configuration changes made will be saved. According to the + function parameters. If an exception is raised, a new checkpoint + was not created. + + :param str title: The title of the save. If a title is given, the + configuration will be saved as a new checkpoint and put in a + timestamped directory. + + :param bool temporary: Indicates whether the changes made will + be quickly reversed in the future (ie. challenges) + + """ + save_files = self.parser.unsaved_files() + if save_files: + self.add_to_checkpoint(save_files, + self.save_notes, temporary=temporary) + # Handle the parser specific tasks + self.parser.save(save_files) + if title and not temporary: + self.finalize_checkpoint(title) + + def recovery_routine(self) -> None: + """Revert all previously modified files. + + Reverts all modified files that have not been saved as a checkpoint + + :raises .errors.PluginError: If unable to recover the configuration + + """ + super().recovery_routine() + # Reload configuration after these changes take effect if needed + # ie. ApacheParser has been initialized. + if hasattr(self, "parser"): + # TODO: wrap into non-implementation specific parser interface + self.parser.aug.load() + + def revert_challenge_config(self) -> None: + """Used to cleanup challenge configurations. + + :raises .errors.PluginError: If unable to revert the challenge config. + + """ + self.revert_temporary_config() + self.parser.aug.load() + + def rollback_checkpoints(self, rollback: int = 1) -> None: + """Rollback saved checkpoints. + + :param int rollback: Number of checkpoints to revert + + :raises .errors.PluginError: If there is a problem with the input or + the function is unable to correctly revert the configuration + + """ + super().rollback_checkpoints(rollback) + self.parser.aug.load() + + def _verify_exe_availability(self, exe: str) -> None: + """Checks availability of Apache executable""" + if not util.exe_exists(exe): + if not path_surgery(exe): + raise errors.NoInstallationError( + 'Cannot find Apache executable {0}'.format(exe)) + + def get_parser(self) -> parser.ApacheParser: + """Initializes the ApacheParser""" + # If user provided vhost_root value in command line, use it + return parser.ApacheParser( + self.options.server_root, self, self.conf("vhost-root"), version=self.version) + + def get_parsernode_root(self, metadata: dict[str, Any]) -> dualparser.DualBlockNode: + """Initializes the ParserNode parser root instance.""" + + if HAS_APACHECONFIG: + apache_vars = { + "defines": apache_util.parse_defines(self.options.get_defines_cmd), + "includes": apache_util.parse_includes(self.options.get_includes_cmd), + "modules": apache_util.parse_modules(self.options.get_modules_cmd), + } + metadata["apache_vars"] = apache_vars + + with open(self.parser.loc["root"]) as f: + with apacheconfig.make_loader(writable=True, + **apacheconfig.flavors.NATIVE_APACHE) as loader: + metadata["ac_ast"] = loader.loads(f.read()) + + return dualparser.DualBlockNode( + name=assertions.PASS, + ancestor=None, + filepath=self.parser.loc["root"], + metadata=metadata, + ) + + def deploy_cert( + self, domain: str, cert_path: str, key_path: str, + chain_path: Optional[str] = None, fullchain_path: Optional[str] = None + ) -> None: + """Deploys certificate to specified virtual host. + + Currently tries to find the last directives to deploy the certificate + in the VHost associated with the given domain. If it can't find the + directives, it searches the "included" confs. The function verifies + that it has located the three directives and finally modifies them + to point to the correct destination. After the certificate is + installed, the VirtualHost is enabled if it isn't already. + + .. todo:: Might be nice to remove chain directive if none exists + This shouldn't happen within certbot though + + :raises errors.PluginError: When unable to deploy certificate due to + a lack of directives + + """ + vhosts = self.choose_vhosts(domain) + for vhost in vhosts: + self._deploy_cert(vhost, cert_path, key_path, chain_path, fullchain_path) + display_util.notify("Successfully deployed certificate for {} to {}" + .format(domain, vhost.filep)) + + def choose_vhosts(self, domain: str, create_if_no_ssl: bool = True) -> list[obj.VirtualHost]: + """ + Finds VirtualHosts that can be used with the provided domain + + :param str domain: Domain name to match VirtualHosts to + :param bool create_if_no_ssl: If found VirtualHost doesn't have a HTTPS + counterpart, should one get created + + :returns: List of VirtualHosts or None + :rtype: `list` of :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + """ + + if util.is_wildcard_domain(domain): + if domain in self._wildcard_vhosts: + # Vhosts for a wildcard domain were already selected + return self._wildcard_vhosts[domain] + # Ask user which VHosts to support. + # Returned objects are guaranteed to be ssl vhosts + return self._choose_vhosts_wildcard(domain, create_if_no_ssl) + else: + return [self.choose_vhost(domain, create_if_no_ssl)] + + def _vhosts_for_wildcard(self, domain: str) -> list[obj.VirtualHost]: + """ + Get VHost objects for every VirtualHost that the user wants to handle + with the wildcard certificate. + """ + + # Collect all vhosts that match the name + matched: set[obj.VirtualHost] = set() + for vhost in self.vhosts: + for name in vhost.get_names(): + if self._in_wildcard_scope(name, domain): + matched.add(vhost) + + return list(matched) + + def _generate_no_suitable_vhost_error(self, target_name: str) -> errors.PluginError: + """ + Prepare an error to notify the user that Certbot could not find a vhost + to secure. + :param str target_name: The server name that could not be mapped + :return: An instance of PluginError + :rtype: errors.PluginError + """ + return errors.PluginError( + "Certbot could not find a VirtualHost for {0} in the Apache " + "configuration. Please create a VirtualHost with a ServerName " + "matching {0} and try again.".format(target_name) + ) + + def _in_wildcard_scope(self, name: str, domain: str) -> Optional[bool]: + """ + Helper method for _vhosts_for_wildcard() that makes sure that the domain + is in the scope of wildcard domain. + + eg. in scope: domain = *.wild.card, name = 1.wild.card + not in scope: domain = *.wild.card, name = 1.2.wild.card + """ + if len(name.split(".")) == len(domain.split(".")): + return fnmatch.fnmatch(name, domain) + return None + + def _choose_vhosts_wildcard(self, domain: str, create_ssl: bool = True + ) -> list[obj.VirtualHost]: + """Prompts user to choose vhosts to install a wildcard certificate for""" + + # Get all vhosts that are covered by the wildcard domain + vhosts: list[obj.VirtualHost] = self._vhosts_for_wildcard(domain) + + # Go through the vhosts, making sure that we cover all the names + # present, but preferring the SSL vhosts + filtered_vhosts = {} + for vhost in vhosts: + for name in vhost.get_names(): + if vhost.ssl: + # Always prefer SSL vhosts + filtered_vhosts[name] = vhost + elif name not in filtered_vhosts and create_ssl: + # Add if not in list previously + filtered_vhosts[name] = vhost + + # Only unique VHost objects + dialog_input = set(filtered_vhosts.values()) + + # Ask the user which of names to enable, expect list of names back + dialog_output = display_ops.select_vhost_multiple(list(dialog_input)) + + if not dialog_output: + raise self._generate_no_suitable_vhost_error(domain) + + # Make sure we create SSL vhosts for the ones that are HTTP only + # if requested. + return_vhosts = [] + for vhost in dialog_output: + if not vhost.ssl: + return_vhosts.append(self.make_vhost_ssl(vhost)) + else: + return_vhosts.append(vhost) + + self._wildcard_vhosts[domain] = return_vhosts + return return_vhosts + + def _deploy_cert( + self, vhost: obj.VirtualHost, cert_path: str, key_path: str, + chain_path: Optional[str] = None, fullchain_path: Optional[str] = None, + ) -> None: + """ + Helper function for deploy_cert() that handles the actual deployment + this exists because we might want to do multiple deployments per + domain originally passed for deploy_cert(). This is especially true + with wildcard certificates + """ + # This is done first so that ssl module is enabled and cert_path, + # cert_key... can all be parsed appropriately + self.prepare_server_https("443") + + # If we haven't managed to enable mod_ssl by this point, error out + if "ssl_module" not in self.parser.modules: + raise errors.MisconfigurationError("Could not find ssl_module; " + "not installing certificate.") + + # Add directives and remove duplicates + self._add_dummy_ssl_directives(vhost.path) + self._clean_vhost(vhost) + + path: dict[str, Any] = { + "cert_path": self.parser.find_dir("SSLCertificateFile", None, vhost.path), + "cert_key": self.parser.find_dir("SSLCertificateKeyFile", None, vhost.path), + } + + # Only include if a certificate chain is specified + if chain_path is not None: + path["chain_path"] = self.parser.find_dir( + "SSLCertificateChainFile", None, vhost.path) + + logger.info("Deploying Certificate to VirtualHost %s", vhost.filep) + + if self.version < (2, 4, 8) or (chain_path and not fullchain_path): + # install SSLCertificateFile, SSLCertificateKeyFile, + # and SSLCertificateChainFile directives + set_cert_path = cert_path + self.parser.aug.set(path["cert_path"][-1], cert_path) + self.parser.aug.set(path["cert_key"][-1], key_path) + if chain_path is not None: + self.parser.add_dir(vhost.path, + "SSLCertificateChainFile", chain_path) + else: + raise errors.PluginError("--chain-path is required for your " + "version of Apache") + else: + if not fullchain_path: + raise errors.PluginError("Please provide the --fullchain-path " + "option pointing to your full chain file") + set_cert_path = fullchain_path + self.parser.aug.set(path["cert_path"][-1], fullchain_path) + self.parser.aug.set(path["cert_key"][-1], key_path) + + # Enable the new vhost if needed + if not vhost.enabled: + self.enable_site(vhost) + + # Save notes about the transaction that took place + self.save_notes += ("Changed vhost at %s with addresses of %s\n" + "\tSSLCertificateFile %s\n" + "\tSSLCertificateKeyFile %s\n" % + (vhost.filep, + ", ".join(str(addr) for addr in vhost.addrs), + set_cert_path, key_path)) + if chain_path is not None: + self.save_notes += "\tSSLCertificateChainFile %s\n" % chain_path + + def choose_vhost(self, target_name: str, create_if_no_ssl: bool = True) -> obj.VirtualHost: + """Chooses a virtual host based on the given domain name. + + If there is no clear virtual host to be selected, the user is prompted + with all available choices. + + The returned vhost is guaranteed to have TLS enabled unless + create_if_no_ssl is set to False, in which case there is no such guarantee + and the result is not cached. + + :param str target_name: domain name + :param bool create_if_no_ssl: If found VirtualHost doesn't have a HTTPS + counterpart, should one get created + + :returns: vhost associated with name + :rtype: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :raises .errors.PluginError: If no vhost is available or chosen + + """ + # Allows for domain names to be associated with a virtual host + if target_name in self.assoc: + return self.assoc[target_name] + + # Try to find a reasonable vhost + vhost = self._find_best_vhost(target_name) + if vhost is not None: + if not create_if_no_ssl: + return vhost + if not vhost.ssl: + vhost = self.make_vhost_ssl(vhost) + + self._add_servername_alias(target_name, vhost) + self.assoc[target_name] = vhost + return vhost + + # Negate create_if_no_ssl value to indicate if we want a SSL vhost + # to get created if a non-ssl vhost is selected. + return self._choose_vhost_from_list(target_name, temp=not create_if_no_ssl) + + def _choose_vhost_from_list(self, target_name: str, + temp: bool = False) -> obj.VirtualHost: + # Select a vhost from a list + vhost = display_ops.select_vhost(target_name, self.vhosts) + if vhost is None: + raise self._generate_no_suitable_vhost_error(target_name) + if temp: + return vhost + if not vhost.ssl: + addrs = self._get_proposed_addrs(vhost, "443") + # TODO: Conflicts is too conservative + if not any(one_vhost.enabled and one_vhost.conflicts(addrs) for + one_vhost in self.vhosts): + vhost = self.make_vhost_ssl(vhost) + else: + logger.error( + "The selected vhost would conflict with other HTTPS " + "VirtualHosts within Apache. Please select another " + "vhost or add ServerNames to your configuration.") + raise errors.PluginError("VirtualHost not able to be selected.") + + self._add_servername_alias(target_name, vhost) + self.assoc[target_name] = vhost + return vhost + + def domain_in_names(self, names: Iterable[str], target_name: str) -> bool: + """Checks if target domain is covered by one or more of the provided + names. The target name is matched by wildcard as well as exact match. + + :param names: server aliases + :type names: `collections.Iterable` of `str` + :param str target_name: name to compare with wildcards + + :returns: True if target_name is covered by a wildcard, + otherwise, False + :rtype: bool + + """ + # use lowercase strings because fnmatch can be case sensitive + target_name = target_name.lower() + for name in names: + name = name.lower() + # fnmatch treats "[seq]" specially and [ or ] characters aren't + # valid in Apache but Apache doesn't error out if they are present + if "[" not in name and fnmatch.fnmatch(target_name, name): + return True + return False + + def find_best_http_vhost( + self, target: str, filter_defaults: bool, port: str = "80" + ) -> Optional[obj.VirtualHost]: + """Returns non-HTTPS vhost objects found from the Apache config + + :param str target: Domain name of the desired VirtualHost + :param bool filter_defaults: whether _default_ vhosts should be + included if it is the best match + :param str port: port number the vhost should be listening on + + :returns: VirtualHost object that's the best match for target name + :rtype: `obj.VirtualHost` or None + """ + filtered_vhosts = [] + for vhost in self.vhosts: + if any(a.is_wildcard() or a.get_port() == port for a in vhost.addrs) and not vhost.ssl: + filtered_vhosts.append(vhost) + return self._find_best_vhost(target, filtered_vhosts, filter_defaults) + + def _find_best_vhost( + self, target_name: str, vhosts: Optional[list[obj.VirtualHost]] = None, + filter_defaults: bool = True + ) -> Optional[obj.VirtualHost]: + """Finds the best vhost for a target_name. + + This does not upgrade a vhost to HTTPS... it only finds the most + appropriate vhost for the given target_name. + + :param str target_name: domain handled by the desired vhost + :param vhosts: vhosts to consider + :type vhosts: `collections.Iterable` of + :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + :param bool filter_defaults: whether a vhost with a _default_ + addr is acceptable + + :returns: VHost or None + + """ + # Points 6 - Servername SSL + # Points 5 - Wildcard SSL + # Points 4 - Address name with SSL + # Points 3 - Servername no SSL + # Points 2 - Wildcard no SSL + # Points 1 - Address name with no SSL + best_candidate: Optional[obj.VirtualHost] = None + best_points: int = 0 + + if vhosts is None: + vhosts = self.vhosts + + for vhost in vhosts: + if vhost.modmacro is True: + continue + names = vhost.get_names() + if target_name in names: + points = 3 + elif self.domain_in_names(names, target_name): + points = 2 + elif any(addr.get_addr() == target_name for addr in vhost.addrs): + points = 1 + else: + # No points given if names can't be found. + # This gets hit but doesn't register + continue # pragma: no cover + + if vhost.ssl: + points += 3 + + if points > best_points: + best_points = points + best_candidate = vhost + + # No winners here... is there only one reasonable vhost? + if best_candidate is None: + if filter_defaults: + vhosts = self._non_default_vhosts(vhosts) + # remove mod_macro hosts from reasonable vhosts + reasonable_vhosts = [vh for vh + in vhosts if vh.modmacro is False] + if len(reasonable_vhosts) == 1: + best_candidate = reasonable_vhosts[0] + + return best_candidate + + def _non_default_vhosts(self, vhosts: list[obj.VirtualHost]) -> list[obj.VirtualHost]: + """Return all non _default_ only vhosts.""" + return [vh for vh in vhosts if not all( + addr.get_addr() == "_default_" for addr in vh.addrs + )] + + def get_all_names(self) -> set[str]: + """Returns all names found in the Apache Configuration. + + :returns: All ServerNames, ServerAliases, and reverse DNS entries for + virtual host addresses + :rtype: set + + """ + all_names: set[str] = set() + + vhost_macro = [] + + for vhost in self.vhosts: + all_names.update(vhost.get_names()) + if vhost.modmacro: + vhost_macro.append(vhost.filep) + + for addr in vhost.addrs: + if common.hostname_regex.match(addr.get_addr()): + all_names.add(addr.get_addr()) + else: + name = self.get_name_from_ip(addr) + if name: + all_names.add(name) + + if vhost_macro: + display_util.notification( + "Apache mod_macro seems to be in use in file(s):\n{0}" + "\n\nUnfortunately mod_macro is not yet supported".format( + "\n ".join(vhost_macro)), force_interactive=True) + + return util.get_filtered_names(all_names) + + def get_name_from_ip(self, addr: obj.Addr) -> str: + """Returns a reverse dns name if available. + + :param addr: IP Address + :type addr: ~.common.Addr + + :returns: name or empty string if name cannot be determined + :rtype: str + + """ + # If it isn't a private IP, do a reverse DNS lookup + if not common.private_ips_regex.match(addr.get_addr()): + try: + socket.inet_aton(addr.get_addr()) + return socket.gethostbyaddr(addr.get_addr())[0] + except (OSError, socket.herror, socket.timeout): + pass + + return "" + + def _get_vhost_names(self, path: Optional[str]) -> tuple[Optional[str], list[Optional[str]]]: + """Helper method for getting the ServerName and + ServerAlias values from vhost in path + + :param path: Path to read ServerName and ServerAliases from + + :returns: Tuple including ServerName and `list` of ServerAlias strings + """ + + servername_match = self.parser.find_dir( + "ServerName", None, start=path, exclude=False) + serveralias_match = self.parser.find_dir( + "ServerAlias", None, start=path, exclude=False) + + serveraliases = [] + for alias in serveralias_match: + serveralias = self.parser.get_arg(alias) + serveraliases.append(serveralias) + + servername = None + if servername_match: + # Get last ServerName as each overwrites the previous + servername = self.parser.get_arg(servername_match[-1]) + + return servername, serveraliases + + def _add_servernames(self, host: obj.VirtualHost) -> None: + """Helper function for get_virtual_hosts(). + + :param host: In progress vhost whose names will be added + :type host: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + """ + + servername, serveraliases = self._get_vhost_names(host.path) + + for alias in serveraliases: + if not host.modmacro and alias is not None: + host.aliases.add(alias) + + if not host.modmacro: + host.name = servername + + def _create_vhost(self, path: str) -> Optional[obj.VirtualHost]: + """Used by get_virtual_hosts to create vhost objects + + :param str path: Augeas path to virtual host + + :returns: newly created vhost + :rtype: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + """ + addrs: set[obj.Addr] = set() + try: + args = self.parser.aug.match(path + "/arg") + except RuntimeError: + logger.warning("Encountered a problem while parsing file: %s, skipping", path) + return None + for arg in args: + arg_value = self.parser.get_arg(arg) + if arg_value is not None: + addrs.add(obj.Addr.fromstring(arg_value)) + is_ssl = False + + if self.parser.find_dir("SSLEngine", "on", start=path, exclude=False): + is_ssl = True + + # "SSLEngine on" might be set outside of + # Treat vhosts with port 443 as ssl vhosts + for addr in addrs: + if addr.get_port() == "443": + is_ssl = True + + filename = apache_util.get_file_path( + self.parser.aug.get(f"/augeas/files{apache_util.get_file_path(path)}/path")) + if filename is None: + return None + + macro = False + if "/macro/" in path.lower(): + macro = True + + vhost_enabled = self.parser.parsed_in_original(filename) + + vhost = obj.VirtualHost(filename, path, addrs, is_ssl, + vhost_enabled, modmacro=macro) + self._add_servernames(vhost) + return vhost + + def get_virtual_hosts(self) -> list[obj.VirtualHost]: + """ + Temporary wrapper for legacy and ParserNode version for + get_virtual_hosts. This should be replaced with the ParserNode + implementation when ready. + """ + + v1_vhosts = self.get_virtual_hosts_v1() + if self.USE_PARSERNODE and HAS_APACHECONFIG: + v2_vhosts = self.get_virtual_hosts_v2() + + for v1_vh in v1_vhosts: + found = False + for v2_vh in v2_vhosts: + if assertions.isEqualVirtualHost(v1_vh, v2_vh): + found = True + break + if not found: + raise AssertionError("Equivalent for {} was not found".format(v1_vh.path)) + + return v2_vhosts + return v1_vhosts + + def get_virtual_hosts_v1(self) -> list[obj.VirtualHost]: + """Returns list of virtual hosts found in the Apache configuration. + + :returns: List of :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + objects found in configuration + :rtype: list + + """ + # Search base config, and all included paths for VirtualHosts + file_paths: dict[str, str] = {} + internal_paths: defaultdict[str, set[str]] = defaultdict(set) + vhs = [] + # Make a list of parser paths because the parser_paths + # dictionary may be modified during the loop. + for vhost_path in list(self.parser.parser_paths): + paths = self.parser.aug.match( + ("/files%s//*[label()=~regexp('%s')]" % + (vhost_path, parser.case_i("VirtualHost")))) + paths = [path for path in paths if + "virtualhost" in os.path.basename(path).lower()] + for path in paths: + new_vhost = self._create_vhost(path) + if not new_vhost: + continue + internal_path = apache_util.get_internal_aug_path(new_vhost.path) + realpath = filesystem.realpath(new_vhost.filep) + if realpath not in file_paths: + file_paths[realpath] = new_vhost.filep + internal_paths[realpath].add(internal_path) + vhs.append(new_vhost) + elif (realpath == new_vhost.filep and + realpath != file_paths[realpath]): + # Prefer "real" vhost paths instead of symlinked ones + # ex: sites-enabled/vh.conf -> sites-available/vh.conf + + # remove old (most likely) symlinked one + new_vhs = [] + for v in vhs: + if v.filep == file_paths[realpath]: + internal_paths[realpath].remove( + apache_util.get_internal_aug_path(v.path)) + else: + new_vhs.append(v) + vhs = new_vhs + + file_paths[realpath] = realpath + internal_paths[realpath].add(internal_path) + vhs.append(new_vhost) + elif internal_path not in internal_paths[realpath]: + internal_paths[realpath].add(internal_path) + vhs.append(new_vhost) + return vhs + + def get_virtual_hosts_v2(self) -> list[obj.VirtualHost]: + """Returns list of virtual hosts found in the Apache configuration using + ParserNode interface. + :returns: List of :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + objects found in configuration + :rtype: list + """ + + if not self.parser_root: + raise errors.Error("This ApacheConfigurator instance is not" # pragma: no cover + " configured to use a node parser.") + vhs = [] + vhosts = self.parser_root.find_blocks("VirtualHost", exclude=False) + for vhblock in vhosts: + vhs.append(self._create_vhost_v2(cast(ApacheBlockNode, vhblock))) + return vhs + + def _create_vhost_v2(self, node: ApacheBlockNode) -> obj.VirtualHost: + """Used by get_virtual_hosts_v2 to create vhost objects using ParserNode + interfaces. + :param ApacheBlockNode node: The BlockNode object of VirtualHost block + :returns: newly created vhost + :rtype: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + """ + addrs = set() + for param in node.parameters: + addrs.add(obj.Addr.fromstring(param)) + + is_ssl = False + # Exclusion to match the behavior in get_virtual_hosts_v2 + sslengine = node.find_directives("SSLEngine", exclude=False) + if sslengine: + for directive in sslengine: + if directive.parameters[0].lower() == "on": + is_ssl = True + break + + # "SSLEngine on" might be set outside of + # Treat vhosts with port 443 as ssl vhosts + for addr in addrs: + if addr.get_port() == "443": + is_ssl = True + + if node.filepath is None: + raise errors.Error("Node filepath cannot be None.") # pragma: no cover + + enabled = apache_util.included_in_paths(node.filepath, self.parsed_paths) + + macro = False + # Check if the VirtualHost is contained in a mod_macro block + if node.find_ancestors("Macro"): + macro = True + # VirtualHost V2 is part of migration to the pure-Python Apache parser project. It is not + # used on production as of now. + # TODO: Use a meaning full value for augeas path instead of an empty string + vhost = obj.VirtualHost( + node.filepath, "", addrs, is_ssl, enabled, modmacro=macro, node=node + ) + self._populate_vhost_names_v2(vhost) + return vhost + + def _populate_vhost_names_v2(self, vhost: obj.VirtualHost) -> None: + """Helper function that populates the VirtualHost names. + :param host: In progress vhost whose names will be added + :type host: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + """ + if not vhost.node: + raise errors.PluginError("Current VirtualHost has no node.") # pragma: no cover + + servername_match = vhost.node.find_directives("ServerName", exclude=False) + serveralias_match = vhost.node.find_directives("ServerAlias", exclude=False) + + servername = None + if servername_match: + servername = servername_match[-1].parameters[-1] + + if not vhost.modmacro: + for alias in serveralias_match: + for serveralias in alias.parameters: + vhost.aliases.add(serveralias) + vhost.name = servername + + def prepare_server_https(self, port: str, temp: bool = False) -> None: + """Prepare the server for HTTPS. + + Make sure that the ssl_module is loaded and that the server + is appropriately listening on port. + + :param str port: Port to listen on + :param bool temp: If this is just temporary + """ + + self.prepare_https_modules(temp) + self.ensure_listen(port, https=True) + + def ensure_listen(self, port: str, https: bool = False) -> None: + """Make sure that Apache is listening on the port. Checks if the + Listen statement for the port already exists, and adds it to the + configuration if necessary. + + :param str port: Port number to check and add Listen for if not in + place already + :param bool https: If the port will be used for HTTPS + + """ + + # If HTTPS requested for nonstandard port, add service definition + if https and port != "443": + port_service = f"{port} https" + else: + port_service = port + + # Check for Listen + # Note: This could be made to also look for ip:443 combo + directives = [self.parser.get_arg(x) for x in self.parser.find_dir("Listen")] + listens = [directive.split()[0] for directive in directives if directive] + + # Listen already in place + if self._has_port_already(listens, port): + return + + listen_dirs = set(listens) + + if not listens: + listen_dirs.add(port_service) + + for listen in listens: + # For any listen statement, check if the machine also listens on + # the given port. If not, add such a listen statement. + if len(listen.split(":")) == 1: + # Its listening to all interfaces + if port not in listen_dirs and port_service not in listen_dirs: + listen_dirs.add(port_service) + else: + # The Listen statement specifies an ip + _, ip = listen[::-1].split(":", 1) + ip = ip[::-1] + if "%s:%s" % (ip, port_service) not in listen_dirs and ( + "%s:%s" % (ip, port_service) not in listen_dirs): + listen_dirs.add("%s:%s" % (ip, port_service)) + if https: + self._add_listens_https(listen_dirs, listens, port) + else: + self._add_listens_http(listen_dirs, listens, port) + + def _add_listens_http(self, listens: set[str], listens_orig: list[str], port: str) -> None: + """Helper method for ensure_listen to figure out which new + listen statements need adding for listening HTTP on port + + :param set listens: Set of all needed Listen statements + :param list listens_orig: List of existing listen statements + :param string port: Port number we're adding + """ + + new_listens = listens.difference(listens_orig) + + if port in new_listens: + # We have wildcard, skip the rest + self.parser.add_dir(parser.get_aug_path(self.parser.loc["listen"]), + "Listen", port) + self.save_notes += ( + f"Added Listen {port} directive to {self.parser.loc['listen']}\n" + ) + else: + for listen in new_listens: + self.parser.add_dir(parser.get_aug_path( + self.parser.loc["listen"]), "Listen", listen.split(" ")) + self.save_notes += (f"Added Listen {listen} directive to " + f"{self.parser.loc['listen']}\n") + + def _add_listens_https(self, listens: set[str], listens_orig: list[str], port: str) -> None: + """Helper method for ensure_listen to figure out which new + listen statements need adding for listening HTTPS on port + + :param set listens: Set of all needed Listen statements + :param list listens_orig: List of existing listen statements + :param string port: Port number we're adding + """ + + # Add service definition for non-standard ports + if port != "443": + port_service = f"{port} https" + else: + port_service = port + + new_listens = listens.difference(listens_orig) + + if port in new_listens or port_service in new_listens: + # We have wildcard, skip the rest + self.parser.add_dir_to_ifmodssl( + parser.get_aug_path(self.parser.loc["listen"]), + "Listen", port_service.split(" ")) + self.save_notes += ( + f"Added Listen {port_service} directive to {self.parser.loc['listen']}\n" + ) + else: + for listen in new_listens: + self.parser.add_dir_to_ifmodssl( + parser.get_aug_path(self.parser.loc["listen"]), + "Listen", listen.split(" ")) + self.save_notes += (f"Added Listen {listen} directive to " + f"{self.parser.loc['listen']}\n") + + def _has_port_already(self, listens: list[str], port: str) -> Optional[bool]: + """Helper method for prepare_server_https to find out if user + already has an active Listen statement for the port we need + + :param list listens: List of listen variables + :param string port: Port in question + """ + + if port in listens: + return True + # Check if Apache is already listening on a specific IP + for listen in listens: + if len(listen.split(":")) > 1: + # Ugly but takes care of protocol def, eg: 1.1.1.1:443 https + if listen.split(":")[-1].split(" ")[0] == port: + return True + return None + + def prepare_https_modules(self, temp: bool) -> None: + """Helper method for prepare_server_https, taking care of enabling + needed modules + + :param boolean temp: If the change is temporary + """ + + if self.options.handle_modules: + if "socache_shmcb_module" not in self.parser.modules: + self.enable_mod("socache_shmcb", temp=temp) + if "ssl_module" not in self.parser.modules: + self.enable_mod("ssl", temp=temp) + # Make sure we're not throwing away any unwritten changes to the config + self.parser.ensure_augeas_state() + self.parser.aug.load() + self.parser.reset_modules() # Reset to load the new ssl_module path + # Call again because now we can gate on openssl version + self.install_ssl_options_conf(self.mod_ssl_conf, + self.updated_mod_ssl_conf_digest, + warn_on_no_mod_ssl=True) + + def make_vhost_ssl(self, nonssl_vhost: obj.VirtualHost) -> obj.VirtualHost: + """Makes an ssl_vhost version of a nonssl_vhost. + + Duplicates vhost and adds default ssl options + New vhost will reside as (nonssl_vhost.path) + + ``self.options.le_vhost_ext`` + + .. note:: This function saves the configuration + + :param nonssl_vhost: Valid VH that doesn't have SSLEngine on + :type nonssl_vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :returns: SSL vhost + :rtype: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :raises .errors.PluginError: If more than one virtual host is in + the file or if plugin is unable to write/read vhost files. + + """ + avail_fp = nonssl_vhost.filep + ssl_fp = self._get_ssl_vhost_path(avail_fp) + + orig_matches = self.parser.aug.match("/files%s//* [label()=~regexp('%s')]" % + (self._escape(ssl_fp), + parser.case_i("VirtualHost"))) + + self._copy_create_ssl_vhost_skeleton(nonssl_vhost, ssl_fp) + + # Reload augeas to take into account the new vhost + self.parser.aug.load() + # Get Vhost augeas path for new vhost + new_matches = self.parser.aug.match("/files%s//* [label()=~regexp('%s')]" % + (self._escape(ssl_fp), + parser.case_i("VirtualHost"))) + + vh_p = self._get_new_vh_path(orig_matches, new_matches) + + if not vh_p: + # The vhost was not found on the currently parsed paths + # Make Augeas aware of the new vhost + self.parser.parse_file(ssl_fp) + # Try to search again + new_matches = self.parser.aug.match( + "/files%s//* [label()=~regexp('%s')]" % + (self._escape(ssl_fp), + parser.case_i("VirtualHost"))) + vh_p = self._get_new_vh_path(orig_matches, new_matches) + if not vh_p: + raise errors.PluginError( + "Could not reverse map the HTTPS VirtualHost to the original") + + # Update Addresses + self._update_ssl_vhosts_addrs(vh_p) + + # Log actions and create save notes + logger.info("Created an SSL vhost at %s", ssl_fp) + self.save_notes += "Created ssl vhost at %s\n" % ssl_fp + self.save() + + # We know the length is one because of the assertion above + # Create the Vhost object + vhost = self._create_vhost(vh_p) + if not vhost: + raise errors.Error("Could not create a vhost") # pragma: no cover + ssl_vhost: obj.VirtualHost = vhost + ssl_vhost.ancestor = nonssl_vhost + + self.vhosts.append(ssl_vhost) + + # NOTE: Searches through Augeas seem to ruin changes to directives + # The configuration must also be saved before being searched + # for the new directives; For these reasons... this is tacked + # on after fully creating the new vhost + + return ssl_vhost + + def _get_new_vh_path(self, orig_matches: list[str], new_matches: list[str]) -> Optional[str]: + """ Helper method for make_vhost_ssl for matching augeas paths. Returns + VirtualHost path from new_matches that's not present in orig_matches. + + Paths are normalized, because augeas leaves indices out for paths + with only single directive with a similar key """ + + orig_matches = [i.replace("[1]", "") for i in orig_matches] + for match in new_matches: + if match.replace("[1]", "") not in orig_matches: + # Return the unmodified path + return match + return None + + def _get_ssl_vhost_path(self, non_ssl_vh_fp: str) -> str: + """ Get a file path for SSL vhost, uses user defined path as priority, + but if the value is invalid or not defined, will fall back to non-ssl + vhost filepath. + + :param str non_ssl_vh_fp: Filepath of non-SSL vhost + + :returns: Filepath for SSL vhost + :rtype: str + """ + + if self.conf("vhost-root") and os.path.exists(self.conf("vhost-root")): + fp = os.path.join(filesystem.realpath(self.options.vhost_root), + os.path.basename(non_ssl_vh_fp)) + else: + # Use non-ssl filepath + fp = filesystem.realpath(non_ssl_vh_fp) + + if fp.endswith(".conf"): + return fp[:-(len(".conf"))] + self.options.le_vhost_ext + return fp + self.options.le_vhost_ext + + def _sift_rewrite_rule(self, line: str) -> bool: + """Decides whether a line should be copied to a SSL vhost. + + A canonical example of when sifting a line is required: + When the http vhost contains a RewriteRule that unconditionally + redirects any request to the https version of the same site. + e.g: + RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent] + Copying the above line to the ssl vhost would cause a + redirection loop. + + :param str line: a line extracted from the http vhost. + + :returns: True - don't copy line from http vhost to SSL vhost. + :rtype: bool + + """ + if not line.lower().lstrip().startswith("rewriterule"): + return False + + # According to: https://httpd.apache.org/docs/2.4/rewrite/flags.html + # The syntax of a RewriteRule is: + # RewriteRule pattern target [Flag1,Flag2,Flag3] + # i.e. target is required, so it must exist. + target = line.split()[2].strip() + + # target may be surrounded with quotes + if target[0] in ("'", '"') and target[0] == target[-1]: + target = target[1:-1] + + # Sift line if it redirects the request to a HTTPS site + return target.startswith("https://") + + def _copy_create_ssl_vhost_skeleton(self, vhost: obj.VirtualHost, ssl_fp: str) -> None: + """Copies over existing Vhost with IfModule mod_ssl.c> skeleton. + + :param obj.VirtualHost vhost: Original VirtualHost object + :param str ssl_fp: Full path where the new ssl_vhost will reside. + + A new file is created on the filesystem. + + """ + # First register the creation so that it is properly removed if + # configuration is rolled back + if os.path.exists(ssl_fp): + notes = "Appended new VirtualHost directive to file %s" % ssl_fp + files = set() + files.add(ssl_fp) + self.reverter.add_to_checkpoint(files, notes) + else: + self.reverter.register_file_creation(False, ssl_fp) + sift: bool + + try: + orig_contents = self._get_vhost_block(vhost) + ssl_vh_contents, sift = self._sift_rewrite_rules(orig_contents) + + with open(ssl_fp, "a") as new_file: + new_file.write("\n") + new_file.write("\n".join(ssl_vh_contents)) + # The content does not include the closing tag, so add it + new_file.write("\n") + new_file.write("\n") + # Add new file to augeas paths if we're supposed to handle + # activation (it's not included as default) + if not self.parser.parsed_in_current(ssl_fp): + self.parser.parse_file(ssl_fp) + except OSError: + logger.critical("Error writing/reading to file in make_vhost_ssl", exc_info=True) + raise errors.PluginError("Unable to write/read in make_vhost_ssl") + + if sift: + display_util.notify( + f"Some rewrite rules copied from {vhost.filep} were disabled in the " + f"vhost for your HTTPS site located at {ssl_fp} because they have " + "the potential to create redirection loops." + ) + self.parser.aug.set("/augeas/files%s/mtime" % (self._escape(ssl_fp)), "0") + self.parser.aug.set("/augeas/files%s/mtime" % (self._escape(vhost.filep)), "0") + + def _sift_rewrite_rules(self, contents: Iterable[str]) -> tuple[list[str], bool]: + """ Helper function for _copy_create_ssl_vhost_skeleton to prepare the + new HTTPS VirtualHost contents. Currently disabling the rewrites """ + + result: list[str] = [] + sift: bool = False + contents = iter(contents) + + comment = ("# Some rewrite rules in this file were " + "disabled on your HTTPS site,\n" + "# because they have the potential to create " + "redirection loops.\n") + + for line in contents: + A = line.lower().lstrip().startswith("rewritecond") + B = line.lower().lstrip().startswith("rewriterule") + + if not (A or B): + result.append(line) + continue + + # A RewriteRule that doesn't need filtering + if B and not self._sift_rewrite_rule(line): + result.append(line) + continue + + # A RewriteRule that does need filtering + if B and self._sift_rewrite_rule(line): + if not sift: + result.append(comment) + sift = True + result.append("# " + line) + continue + + # We save RewriteCond(s) and their corresponding + # RewriteRule in 'chunk'. + # We then decide whether we comment out the entire + # chunk based on its RewriteRule. + chunk = [] + if A: + chunk.append(line) + line = next(contents) + + # RewriteCond(s) must be followed by one RewriteRule + while not line.lower().lstrip().startswith("rewriterule"): + chunk.append(line) + line = next(contents) + + # Now, current line must start with a RewriteRule + chunk.append(line) + + if self._sift_rewrite_rule(line): + if not sift: + result.append(comment) + sift = True + + result.append('\n'.join('# ' + l for l in chunk)) + else: + result.append('\n'.join(chunk)) + return result, sift + + def _get_vhost_block(self, vhost: obj.VirtualHost) -> list[str]: + """ Helper method to get VirtualHost contents from the original file. + This is done with help of augeas span, which returns the span start and + end positions + + :returns: `list` of VirtualHost block content lines without closing tag + """ + + try: + span_val = self.parser.aug.span(vhost.path) + except ValueError: + logger.critical("Error while reading the VirtualHost %s from " + "file %s", vhost.name, vhost.filep, exc_info=True) + raise errors.PluginError("Unable to read VirtualHost from file") + span_filep = span_val[0] + span_start = span_val[5] + span_end = span_val[6] + with open(span_filep, 'r') as fh: + fh.seek(span_start) + vh_contents = fh.read(span_end-span_start).split("\n") + self._remove_closing_vhost_tag(vh_contents) + return vh_contents + + def _remove_closing_vhost_tag(self, vh_contents: list[str]) -> None: + """Removes the closing VirtualHost tag if it exists. + + This method modifies vh_contents directly to remove the closing + tag. If the closing vhost tag is found, everything on the line + after it is also removed. Whether or not this tag is included + in the result of span depends on the Augeas version. + + :param list vh_contents: VirtualHost block contents to check + + """ + for offset, line in enumerate(reversed(vh_contents)): + if line: + line_index = line.lower().find("") + if line_index != -1: + content_index = len(vh_contents) - offset - 1 + vh_contents[content_index] = line[:line_index] + break + + def _update_ssl_vhosts_addrs(self, vh_path: str) -> set[obj.Addr]: + ssl_addrs: set[obj.Addr] = set() + ssl_addr_p: list[str] = self.parser.aug.match(vh_path + "/arg") + + for addr in ssl_addr_p: + old_addr = obj.Addr.fromstring( + str(self.parser.get_arg(addr))) + ssl_addr = old_addr.get_addr_obj("443") + self.parser.aug.set(addr, str(ssl_addr)) + ssl_addrs.add(ssl_addr) + + return ssl_addrs + + def _clean_vhost(self, vhost: obj.VirtualHost) -> None: + # remove duplicated or conflicting ssl directives + self._deduplicate_directives(vhost.path, + ["SSLCertificateFile", + "SSLCertificateKeyFile"]) + # remove all problematic directives + self._remove_directives(vhost.path, ["SSLCertificateChainFile"]) + + def _deduplicate_directives(self, vh_path: Optional[str], directives: list[str]) -> None: + for directive in directives: + while len(self.parser.find_dir(directive, None, + vh_path, False)) > 1: + directive_path = self.parser.find_dir(directive, None, + vh_path, False) + self.parser.aug.remove(re.sub(r"/\w*$", "", directive_path[0])) + + def _remove_directives(self, vh_path: Optional[str], directives: list[str]) -> None: + for directive in directives: + while self.parser.find_dir(directive, None, vh_path, False): + directive_path = self.parser.find_dir(directive, None, + vh_path, False) + self.parser.aug.remove(re.sub(r"/\w*$", "", directive_path[0])) + + def _add_dummy_ssl_directives(self, vh_path: str) -> None: + self.parser.add_dir(vh_path, "SSLCertificateFile", + "insert_cert_file_path") + self.parser.add_dir(vh_path, "SSLCertificateKeyFile", + "insert_key_file_path") + # Only include the TLS configuration if not already included + existing_inc = self.parser.find_dir("Include", self.mod_ssl_conf, vh_path) + if not existing_inc: + self.parser.add_dir(vh_path, "Include", self.mod_ssl_conf) + + def _add_servername_alias(self, target_name: str, vhost: obj.VirtualHost) -> None: + vh_path = vhost.path + sname, saliases = self._get_vhost_names(vh_path) + if target_name == sname or target_name in saliases: + return + if self._has_matching_wildcard(vh_path, target_name): + return + if not self.parser.find_dir("ServerName", None, + start=vh_path, exclude=False): + self.parser.add_dir(vh_path, "ServerName", target_name) + else: + self.parser.add_dir(vh_path, "ServerAlias", target_name) + self._add_servernames(vhost) + + def _has_matching_wildcard(self, vh_path: str, target_name: str) -> bool: + """Is target_name already included in a wildcard in the vhost? + + :param str vh_path: Augeas path to the vhost + :param str target_name: name to compare with wildcards + + :returns: True if there is a wildcard covering target_name in + the vhost in vhost_path, otherwise, False + :rtype: bool + + """ + matches = self.parser.find_dir( + "ServerAlias", start=vh_path, exclude=False) + aliases = (self.parser.aug.get(match) for match in matches) + return self.domain_in_names(aliases, target_name) + + def find_vhost_by_id(self, id_str: str) -> obj.VirtualHost: + """ + Searches through VirtualHosts and tries to match the id in a comment + + :param str id_str: Id string for matching + + :returns: The matched VirtualHost + :rtype: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :raises .errors.PluginError: If no VirtualHost is found + """ + + for vh in self.vhosts: + if self._find_vhost_id(vh) == id_str: + return vh + msg = "No VirtualHost with ID {} was found.".format(id_str) + logger.warning(msg) + raise errors.PluginError(msg) + + def _find_vhost_id(self, vhost: obj.VirtualHost) -> Optional[str]: + """Tries to find the unique ID from the VirtualHost comments. This is + used for keeping track of VirtualHost directive over time. + + :param vhost: Virtual host to add the id + :type vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :returns: The unique ID or None + :rtype: str or None + """ + + # Strip the {} off from the format string + search_comment = constants.MANAGED_COMMENT_ID.format("") + + id_comment = self.parser.find_comments(search_comment, vhost.path) + if id_comment: + # Use the first value, multiple ones shouldn't exist + comment = self.parser.get_arg(id_comment[0]) + return comment.split(" ")[-1] if comment else None + return None + + def add_vhost_id(self, vhost: obj.VirtualHost) -> Optional[str]: + """Adds an unique ID to the VirtualHost as a comment for mapping back + to it on later invocations, as the config file order might have changed. + If ID already exists, returns that instead. + + :param vhost: Virtual host to add or find the id + :type vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :returns: The unique ID for vhost + :rtype: str or None + """ + + vh_id = self._find_vhost_id(vhost) + if vh_id: + return vh_id + + id_string = apache_util.unique_id() + comment: str = constants.MANAGED_COMMENT_ID.format(id_string) + self.parser.add_comment(vhost.path, comment) + return id_string + + def _escape(self, fp: str) -> str: + fp = fp.replace(",", "\\,") + fp = fp.replace("[", "\\[") + fp = fp.replace("]", "\\]") + fp = fp.replace("|", "\\|") + fp = fp.replace("=", "\\=") + fp = fp.replace("(", "\\(") + fp = fp.replace(")", "\\)") + fp = fp.replace("!", "\\!") + return fp + + ###################################################################### + # Enhancements + ###################################################################### + def supported_enhancements(self) -> list[str]: + """Returns currently supported enhancements.""" + return ["redirect", "ensure-http-header", "staple-ocsp"] + + def enhance(self, domain: str, enhancement: str, + options: Optional[Union[list[str], str]] = None) -> None: + """Enhance configuration. + + :param str domain: domain to enhance + :param str enhancement: enhancement type defined in + :const:`~certbot.plugins.enhancements.ENHANCEMENTS` + :param options: options for the enhancement + See :const:`~certbot.plugins.enhancements.ENHANCEMENTS` + documentation for appropriate parameter. + + :raises .errors.PluginError: If Enhancement is not supported, or if + there is any other problem with the enhancement. + + """ + try: + func = self._enhance_func[enhancement] + except KeyError: + raise errors.PluginError( + "Unsupported enhancement: {0}".format(enhancement)) + + matched_vhosts = self.choose_vhosts(domain, create_if_no_ssl=False) + # We should be handling only SSL vhosts for enhancements + vhosts = [vhost for vhost in matched_vhosts if vhost.ssl] + + if not vhosts: + msg_tmpl = ("Certbot was not able to find SSL VirtualHost for a " + "domain {0} for enabling enhancement \"{1}\". The requested " + "enhancement was not configured.") + msg_enhancement = enhancement + if options: + msg_enhancement += ": " + str(options) + msg = msg_tmpl.format(domain, msg_enhancement) + logger.error(msg) + raise errors.PluginError(msg) + try: + for vhost in vhosts: + func(vhost, options) + except errors.PluginError: + logger.error("Failed %s for %s", enhancement, domain) + raise + + def _autohsts_increase( + self, vhost: obj.VirtualHost, id_str: str, nextstep: int + ) -> None: + """Increase the AutoHSTS max-age value + + :param vhost: Virtual host object to modify + :type vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :param str id_str: The unique ID string of VirtualHost + + :param int nextstep: Next AutoHSTS max-age value index + + """ + nextstep_value = constants.AUTOHSTS_STEPS[nextstep] + self._autohsts_write(vhost, nextstep_value) + self._autohsts[id_str] = {"laststep": nextstep, "timestamp": time.time()} + + def _autohsts_write(self, vhost: obj.VirtualHost, nextstep_value: float) -> None: + """ + Write the new HSTS max-age value to the VirtualHost file + """ + + hsts_dirpath = None + header_path = self.parser.find_dir("Header", None, vhost.path) + if header_path: + pat = '(?:[ "]|^)(strict-transport-security)(?:[ "]|$)' + for match in header_path: + if re.search(pat, self.parser.aug.get(match).lower()): + hsts_dirpath = match + if not hsts_dirpath: + err_msg = ("Certbot was unable to find the existing HSTS header " + "from the VirtualHost at path {0}.").format(vhost.filep) + raise errors.PluginError(err_msg) + + # Prepare the HSTS header value + hsts_maxage = "\"max-age={0}\"".format(nextstep_value) + + # Update the header + # Our match statement was for string strict-transport-security, but + # we need to update the value instead. The next index is for the value + hsts_dirpath = hsts_dirpath.replace("arg[3]", "arg[4]") + self.parser.aug.set(hsts_dirpath, hsts_maxage) + note_msg = ("Increasing HSTS max-age value to {0} for VirtualHost " + "in {1}\n".format(nextstep_value, vhost.filep)) + logger.debug(note_msg) + self.save_notes += note_msg + self.save(note_msg) + + def _autohsts_fetch_state(self) -> None: + """ + Populates the AutoHSTS state from the pluginstorage + """ + try: + self._autohsts = self.storage.fetch("autohsts") + except KeyError: + self._autohsts = {} + + def _autohsts_save_state(self) -> None: + """ + Saves the state of AutoHSTS object to pluginstorage + """ + self.storage.put("autohsts", self._autohsts) + self.storage.save() + + def _autohsts_vhost_in_lineage( + self, vhost: obj.VirtualHost, lineage: RenewableCert + ) -> bool: + """ + Searches AutoHSTS managed VirtualHosts that belong to the lineage. + Matches the private key path. + """ + return bool(self.parser.find_dir("SSLCertificateKeyFile", lineage.key_path, vhost.path)) + + def _enable_ocsp_stapling(self, ssl_vhost: obj.VirtualHost, unused_options: Any) -> None: + """Enables OCSP Stapling + + In OCSP, each client (e.g. browser) would have to query the + OCSP Responder to validate that the site certificate was not revoked. + + Enabling OCSP Stapling, would allow the web-server to query the OCSP + Responder, and staple its response to the offered certificate during + TLS. i.e. clients would not have to query the OCSP responder. + + OCSP Stapling enablement on Apache implicitly depends on + SSLCertificateChainFile being set by other code. + + .. note:: This function saves the configuration + + :param ssl_vhost: Destination of traffic, an ssl enabled vhost + :type ssl_vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :param unused_options: Not currently used + :type unused_options: Not Available + """ + if "socache_shmcb_module" not in self.parser.modules: + self.enable_mod("socache_shmcb") + + # Check if there's an existing SSLUseStapling directive on. + use_stapling_aug_path = self.parser.find_dir("SSLUseStapling", + "on", start=ssl_vhost.path) + if not use_stapling_aug_path: + self.parser.add_dir(ssl_vhost.path, "SSLUseStapling", "on") + + ssl_vhost_aug_path = self._escape(parser.get_aug_path(ssl_vhost.filep)) + + # Check if there's an existing SSLStaplingCache directive. + stapling_cache_aug_path = self.parser.find_dir('SSLStaplingCache', + None, ssl_vhost_aug_path) + + # We'll simply delete the directive, so that we'll have a + # consistent OCSP cache path. + if stapling_cache_aug_path: + self.parser.aug.remove( + re.sub(r"/\w*$", "", stapling_cache_aug_path[0])) + + self.parser.add_dir_to_ifmodssl(ssl_vhost_aug_path, + "SSLStaplingCache", + ["shmcb:/var/run/apache2/stapling_cache(128000)"]) + + msg = "OCSP Stapling was enabled on SSL Vhost: %s.\n"%( + ssl_vhost.filep) + self.save_notes += msg + self.save() + logger.info(msg) + + def _set_http_header(self, ssl_vhost: obj.VirtualHost, header_substring: str) -> None: + """Enables header that is identified by header_substring on ssl_vhost. + + If the header identified by header_substring is not already set, + a new Header directive is placed in ssl_vhost's configuration with + arguments from: constants.HTTP_HEADER[header_substring] + + .. note:: This function saves the configuration + + :param ssl_vhost: Destination of traffic, an ssl enabled vhost + :type ssl_vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :param header_substring: string that uniquely identifies a header. + e.g: Strict-Transport-Security, Upgrade-Insecure-Requests. + :type str + + :raises .errors.PluginError: If no viable HTTP host can be created or + set with header header_substring. + + """ + if "headers_module" not in self.parser.modules: + self.enable_mod("headers") + + # Check if selected header is already set + self._verify_no_matching_http_header(ssl_vhost, header_substring) + + # Add directives to server + self.parser.add_dir(ssl_vhost.path, "Header", + constants.HEADER_ARGS[header_substring]) + + self.save_notes += ("Adding %s header to ssl vhost in %s\n" % + (header_substring, ssl_vhost.filep)) + + self.save() + logger.info("Adding %s header to ssl vhost in %s", header_substring, + ssl_vhost.filep) + + def _verify_no_matching_http_header( + self, ssl_vhost: obj.VirtualHost, header_substring: str + ) -> None: + """Checks to see if there is an existing Header directive that + contains the string header_substring. + + :param ssl_vhost: vhost to check + :type vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :param header_substring: string that uniquely identifies a header. + e.g: Strict-Transport-Security, Upgrade-Insecure-Requests. + :type str + + :returns: boolean + :rtype: (bool) + + :raises errors.PluginEnhancementAlreadyPresent When header + header_substring exists + + """ + header_path = self.parser.find_dir("Header", None, + start=ssl_vhost.path) + if header_path: + # "Existing Header directive for virtualhost" + pat = '(?:[ "]|^)(%s)(?:[ "]|$)' % (header_substring.lower()) + for match in header_path: + if re.search(pat, self.parser.aug.get(match).lower()): + raise errors.PluginEnhancementAlreadyPresent( + "Existing %s header" % header_substring) + + def _enable_redirect(self, ssl_vhost: obj.VirtualHost, unused_options: Any) -> None: + """Redirect all equivalent HTTP traffic to ssl_vhost. + + .. todo:: This enhancement should be rewritten and will + unfortunately require lots of debugging by hand. + + Adds Redirect directive to the port 80 equivalent of ssl_vhost + First the function attempts to find the vhost with equivalent + ip addresses that serves on non-ssl ports + The function then adds the directive + + .. note:: This function saves the configuration + + :param ssl_vhost: Destination of traffic, an ssl enabled vhost + :type ssl_vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :param unused_options: Not currently used + :type unused_options: Not Available + + :raises .errors.PluginError: If no viable HTTP host can be created or + used for the redirect. + + """ + if "rewrite_module" not in self.parser.modules: + self.enable_mod("rewrite") + general_vh = self._get_http_vhost(ssl_vhost) + + if general_vh is None: + # Add virtual_server with redirect + logger.debug("Did not find http version of ssl virtual host " + "attempting to create") + redirect_addrs = self._get_proposed_addrs(ssl_vhost) + for vhost in self.vhosts: + if vhost.enabled and vhost.conflicts(redirect_addrs): + raise errors.PluginError( + "Unable to find corresponding HTTP vhost; " + "Unable to create one as intended addresses conflict; " + "Current configuration does not support automated " + "redirection") + self._create_redirect_vhost(ssl_vhost) + else: + if general_vh in self._enhanced_vhosts["redirect"]: + logger.debug("Already enabled redirect for this vhost") + return + + # Check if Certbot redirection already exists + self._verify_no_certbot_redirect(general_vh) + + # Note: if code flow gets here it means we didn't find the exact + # certbot RewriteRule config for redirection. Finding + # another RewriteRule is likely to be fine in most or all cases, + # but redirect loops are possible in very obscure cases; see #1620 + # for reasoning. + if self._is_rewrite_exists(general_vh): + logger.warning("Added an HTTP->HTTPS rewrite in addition to " + "other RewriteRules; you may wish to check for " + "overall consistency.") + + # Add directives to server + # Note: These are not immediately searchable in sites-enabled + # even with save() and load() + if not self._is_rewrite_engine_on(general_vh): + self.parser.add_dir(general_vh.path, "RewriteEngine", "on") + + names = ssl_vhost.get_names() + for idx, name in enumerate(names): + args = ["%{SERVER_NAME}", "={0}".format(name), "[OR]"] + if idx == len(names) - 1: + args.pop() + self.parser.add_dir(general_vh.path, "RewriteCond", args) + + self._set_https_redirection_rewrite_rule(general_vh) + + self.save_notes += ("Redirecting host in %s to ssl vhost in %s\n" % + (general_vh.filep, ssl_vhost.filep)) + self.save() + + self._enhanced_vhosts["redirect"].add(general_vh) + logger.info("Redirecting vhost in %s to ssl vhost in %s", + general_vh.filep, ssl_vhost.filep) + + def _set_https_redirection_rewrite_rule(self, vhost: obj.VirtualHost) -> None: + self.parser.add_dir(vhost.path, "RewriteRule", constants.REWRITE_HTTPS_ARGS) + + def _verify_no_certbot_redirect(self, vhost: obj.VirtualHost) -> None: + """Checks to see if a redirect was already installed by certbot. + + Checks to see if virtualhost already contains a rewrite rule that is + identical to Certbot's redirection rewrite rule. + + For graceful transition to new rewrite rules for HTTPS redireciton we + delete certbot's old rewrite rules and set the new one instead. + + :param vhost: vhost to check + :type vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :raises errors.PluginEnhancementAlreadyPresent: When the exact + certbot redirection WriteRule exists in virtual host. + """ + rewrite_path = self.parser.find_dir( + "RewriteRule", None, start=vhost.path) + + # There can be other RewriteRule directive lines in vhost config. + # rewrite_args_dict keys are directive ids and the corresponding value + # for each is a list of arguments to that directive. + rewrite_args_dict: defaultdict[str, list[str]] = defaultdict(list) + pat = r'(.*directive\[\d+\]).*' + for match in rewrite_path: + m = re.match(pat, match) + if m: + dir_path = m.group(1) + rewrite_args_dict[dir_path].append(match) + + if rewrite_args_dict: + for dir_path, args_paths in rewrite_args_dict.items(): + arg_vals = [self.parser.aug.get(x) for x in args_paths] + + # Search for past redirection rule, delete it, set the new one + if arg_vals in constants.OLD_REWRITE_HTTPS_ARGS: + self.parser.aug.remove(dir_path) + self._set_https_redirection_rewrite_rule(vhost) + self.save() + raise errors.PluginEnhancementAlreadyPresent( + "Certbot has already enabled redirection") + + if arg_vals == constants.REWRITE_HTTPS_ARGS: + raise errors.PluginEnhancementAlreadyPresent( + "Certbot has already enabled redirection") + + def _is_rewrite_exists(self, vhost: obj.VirtualHost) -> bool: + """Checks if there exists a RewriteRule directive in vhost + + :param vhost: vhost to check + :type vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :returns: True if a RewriteRule directive exists. + :rtype: bool + + """ + rewrite_path = self.parser.find_dir( + "RewriteRule", None, start=vhost.path) + return bool(rewrite_path) + + def _is_rewrite_engine_on(self, vhost: obj.VirtualHost) -> Optional[Union[str, bool]]: + """Checks if a RewriteEngine directive is on + + :param vhost: vhost to check + :type vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + """ + rewrite_engine_path_list = self.parser.find_dir("RewriteEngine", "on", start=vhost.path) + if rewrite_engine_path_list: + for re_path in rewrite_engine_path_list: + # A RewriteEngine directive may also be included in per + # directory .htaccess files. We only care about the VirtualHost. + if 'virtualhost' in re_path.lower(): + return self.parser.get_arg(re_path) + return False + + def _create_redirect_vhost(self, ssl_vhost: obj.VirtualHost) -> None: + """Creates an http_vhost specifically to redirect for the ssl_vhost. + + :param ssl_vhost: ssl vhost + :type ssl_vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + """ + text = self._get_redirect_config_str(ssl_vhost) + + redirect_filepath = self._write_out_redirect(ssl_vhost, text) + + self.parser.aug.load() + # Make a new vhost data structure and add it to the lists + new_vhost = self._create_vhost(parser.get_aug_path( + self._escape(redirect_filepath)) + ) + if new_vhost is None: + raise errors.Error("Could not create a new vhost") # pragma: no cover + self.vhosts.append(new_vhost) + self._enhanced_vhosts["redirect"].add(new_vhost) + + # Finally create documentation for the change + self.save_notes += ("Created a port 80 vhost, %s, for redirection to " + "ssl vhost %s\n" % + (new_vhost.filep, ssl_vhost.filep)) + + def _get_redirect_config_str(self, ssl_vhost: obj.VirtualHost) -> str: + # get servernames and serveraliases + serveralias = "" + servername = "" + + if ssl_vhost.name is not None: + servername = "ServerName " + ssl_vhost.name + if ssl_vhost.aliases: + serveralias = "ServerAlias " + " ".join(ssl_vhost.aliases) + + return ( + f"\n" + f"{servername} \n" + f"{serveralias} \n" + f"ServerSignature Off\n" + f"\n" + f"RewriteEngine On\n" + f"RewriteRule {' '.join(constants.REWRITE_HTTPS_ARGS)}\n" + "\n" + f"ErrorLog {self.options.logs_root}/redirect.error.log\n" + f"LogLevel warn\n" + f"\n" + ) + + def _write_out_redirect(self, ssl_vhost: obj.VirtualHost, text: str) -> str: + # This is the default name + redirect_filename = "le-redirect.conf" + + # See if a more appropriate name can be applied + if ssl_vhost.name is not None: + # make sure servername doesn't exceed filename length restriction + if len(ssl_vhost.name) < (255 - (len(redirect_filename) + 1)): + redirect_filename = "le-redirect-%s.conf" % ssl_vhost.name + + redirect_filepath = os.path.join(self.options.vhost_root, + redirect_filename) + + # Register the new file that will be created + # Note: always register the creation before writing to ensure file will + # be removed in case of unexpected program exit + self.reverter.register_file_creation(False, redirect_filepath) + + # Write out file + with open(redirect_filepath, "w") as redirect_file: + redirect_file.write(text) + + # Add new include to configuration if it doesn't exist yet + if not self.parser.parsed_in_current(redirect_filepath): + self.parser.parse_file(redirect_filepath) + + logger.info("Created redirect file: %s", redirect_filename) + + return redirect_filepath + + def _get_http_vhost(self, ssl_vhost: obj.VirtualHost) -> Optional[obj.VirtualHost]: + """Find appropriate HTTP vhost for ssl_vhost.""" + # First candidate vhosts filter + if ssl_vhost.ancestor: + return ssl_vhost.ancestor + candidate_http_vhs = [ + vhost for vhost in self.vhosts if not vhost.ssl + ] + + # Second filter - check addresses + for http_vh in candidate_http_vhs: + if http_vh.same_server(ssl_vhost): + return http_vh + # Third filter - if none with same names, return generic + for http_vh in candidate_http_vhs: + if http_vh.same_server(ssl_vhost, generic=True): + return http_vh + + return None + + def _get_proposed_addrs(self, vhost: obj.VirtualHost, port: str = "80") -> Iterable[obj.Addr]: + """Return all addrs of vhost with the port replaced with the specified. + + :param obj.VirtualHost ssl_vhost: Original Vhost + :param str port: Desired port for new addresses + + :returns: `set` of :class:`~obj.Addr` + + """ + redirects = set() + for addr in vhost.addrs: + redirects.add(addr.get_addr_obj(port)) + + return redirects + + def enable_site(self, vhost: obj.VirtualHost) -> None: + """Enables an available site, Apache reload required. + + .. note:: Does not make sure that the site correctly works or that all + modules are enabled appropriately. + .. note:: The distribution specific override replaces functionality + of this method where available. + + :param vhost: vhost to enable + :type vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :raises .errors.NotSupportedError: If filesystem layout is not + supported. + + """ + if vhost.enabled: + return + + if not self.parser.parsed_in_original(vhost.filep): + # Add direct include to root conf + logger.info("Enabling site %s by adding Include to root configuration", + vhost.filep) + self.save_notes += "Enabled site %s\n" % vhost.filep + self.parser.add_include(self.parser.loc["default"], vhost.filep) + vhost.enabled = True + return + + # pylint: disable=unused-argument + def enable_mod(self, mod_name: str, temp: bool = False) -> None: + """Enables module in Apache. + + Both enables and reloads Apache so module is active. + + :param str mod_name: Name of the module to enable. (e.g. 'ssl') + :param bool temp: Whether or not this is a temporary action. + + .. note:: The distribution specific override replaces functionality + of this method where available. + + :raises .errors.MisconfigurationError: We cannot enable modules in + generic fashion. + + """ + mod_message = ( + f"Apache needs to have module \"{mod_name}\" active for the " + "requested installation options. Unfortunately Certbot is unable " + "to install or enable it for you. Please install the module, and " + "run Certbot again." + ) + raise errors.MisconfigurationError(mod_message) + + def restart(self) -> None: + """Runs a config test and reloads the Apache server. + + :raises .errors.MisconfigurationError: If either the config test + or reload fails. + + """ + self.config_test() + self._reload() + + def _reload(self) -> None: + """Reloads the Apache server. + + :raises .errors.MisconfigurationError: If reload fails + + """ + try: + util.run_script(self.options.restart_cmd) + except errors.SubprocessError as err: + logger.warning("Unable to restart apache using %s", + self.options.restart_cmd) + if self.options.restart_cmd_alt: + logger.debug("Trying alternative restart command: %s", + self.options.restart_cmd_alt) + # There is an alternative restart command available + # This usually is "restart" verb while original is "graceful" + try: + util.run_script(self.options.restart_cmd_alt) + return + except errors.SubprocessError as secerr: + error = str(secerr) + else: + error = str(err) + raise errors.MisconfigurationError(error) + + def config_test(self) -> None: + """Check the configuration of Apache for errors. + + :raises .errors.MisconfigurationError: If config_test fails + + """ + try: + util.run_script(self.options.conftest_cmd) + except errors.SubprocessError as err: + raise errors.MisconfigurationError(str(err)) + + def get_version(self) -> tuple[int, ...]: + """Return version of Apache Server. + + Version is returned as tuple. (ie. 2.4.7 = (2, 4, 7)) + + :returns: version + :rtype: tuple + + :raises .PluginError: if unable to find Apache version + + """ + try: + stdout, _ = util.run_script(self.options.version_cmd) + except errors.SubprocessError: + raise errors.PluginError( + "Unable to run %s -v" % + self.options.version_cmd) + + regex = re.compile(r"Apache/([0-9\.]*)", re.IGNORECASE) + matches = regex.findall(stdout) + + if len(matches) != 1: + raise errors.PluginError("Unable to find Apache version") + + return tuple(int(i) for i in matches[0].split(".")) + + def more_info(self) -> str: + """Human-readable string to help understand the module""" + return ( + "Configures Apache to authenticate and install HTTPS.{0}" + "Server root: {root}{0}" + "Version: {version}".format( + os.linesep, root=self.parser.loc["root"], + version=".".join(str(i) for i in self.version)) + ) + + def auth_hint( + self, failed_achalls: Iterable[achallenges.AnnotatedChallenge] + ) -> str: # pragma: no cover + return ("The Certificate Authority failed to verify the temporary Apache configuration " + "changes made by Certbot. Ensure that the listed domains point to this Apache " + "server and that it is accessible from the internet.") + + ########################################################################### + # Challenges Section + ########################################################################### + def get_chall_pref(self, unused_identifier: str) -> Sequence[type[challenges.HTTP01]]: + """Return list of challenge preferences.""" + return [challenges.HTTP01] + + def perform(self, achalls: list[achallenges.AnnotatedChallenge] + ) -> list[challenges.ChallengeResponse]: + """Perform the configuration related challenge. + + This function currently assumes all challenges will be fulfilled. + If this turns out not to be the case in the future. Cleanup and + outstanding challenges will have to be designed better. + + """ + self._chall_out.update(achalls) + responses: list[Optional[challenges.ChallengeResponse]] = [None] * len(achalls) + http_doer = http_01.ApacheHttp01(self) + + for i, achall in enumerate(achalls): + if not isinstance(achall, achallenges.KeyAuthorizationAnnotatedChallenge): + raise errors.Error("Challenge should be an instance " # pragma: no cover + "of KeyAuthorizationAnnotatedChallenge") + # Currently also have chall_doer hold associated index of the + # challenge. This helps to put all of the responses back together + # when they are all complete. + http_doer.add_chall(achall, i) + + http_response = http_doer.perform() + if http_response: + # Must reload in order to activate the challenges. + # Handled here because we may be able to load up other challenge + # types + self.restart() + + # TODO: Remove this dirty hack. We need to determine a reliable way + # of identifying when the new configuration is being used. + time.sleep(3) + + self._update_responses(responses, http_response, http_doer) + + return [response for response in responses if response] + + def _update_responses( + self, + responses: list[Optional[challenges.ChallengeResponse]], + chall_response: list[challenges.KeyAuthorizationChallengeResponse], + chall_doer: http_01.ApacheHttp01 + ) -> None: + # Go through all of the challenges and assign them to the proper + # place in the responses return value. All responses must be in the + # same order as the original challenges. + for i, resp in enumerate(chall_response): + responses[chall_doer.indices[i]] = resp + + def cleanup(self, achalls: list[achallenges.AnnotatedChallenge]) -> None: + """Revert all challenges.""" + self._chall_out.difference_update(achalls) + + # If all of the challenges have been finished, clean up everything + if not self._chall_out: + self.revert_challenge_config() + self.restart() + self.parser.reset_modules() + + def install_ssl_options_conf( + self, options_ssl: str, options_ssl_digest: str, warn_on_no_mod_ssl: bool = True + ) -> None: + """Copy Certbot's SSL options file into the system's config dir if required. + + :param bool warn_on_no_mod_ssl: True if we should warn if mod_ssl is not found. + """ + + # XXX if we ever try to enforce a local privilege boundary (eg, running + # certbot for unprivileged users via setuid), this function will need + # to be modified. + apache_config_path = self.pick_apache_config(warn_on_no_mod_ssl) + + return common.install_version_controlled_file( + options_ssl, options_ssl_digest, apache_config_path, constants.ALL_SSL_OPTIONS_HASHES) + + def enable_autohsts(self, _unused_lineage: RenewableCert, domains: list[str]) -> None: + """ + Enable the AutoHSTS enhancement for defined domains + + :param _unused_lineage: Certificate lineage object, unused + :type _unused_lineage: certbot._internal.storage.RenewableCert + + :param domains: List of domains in certificate to enhance + :type domains: `list` of `str` + """ + + self._autohsts_fetch_state() + _enhanced_vhosts: list[obj.VirtualHost] = [] + for d in domains: + matched_vhosts = self.choose_vhosts(d, create_if_no_ssl=False) + # We should be handling only SSL vhosts for AutoHSTS + vhosts = [vhost for vhost in matched_vhosts if vhost.ssl] + + if not vhosts: + msg_tmpl = ("Certbot was not able to find SSL VirtualHost for a " + "domain {0} for enabling AutoHSTS enhancement.") + msg = msg_tmpl.format(d) + logger.error(msg) + raise errors.PluginError(msg) + for vh in vhosts: + try: + self._enable_autohsts_domain(vh) + _enhanced_vhosts.append(vh) + except errors.PluginEnhancementAlreadyPresent: + if vh in _enhanced_vhosts: + continue + msg = ("VirtualHost for domain {0} in file {1} has a " + + "String-Transport-Security header present, exiting.") + raise errors.PluginEnhancementAlreadyPresent( + msg.format(d, vh.filep)) + if _enhanced_vhosts: + note_msg = "Enabling AutoHSTS" + self.save(note_msg) + logger.info(note_msg) + self.restart() + + # Save the current state to pluginstorage + self._autohsts_save_state() + + def _enable_autohsts_domain(self, ssl_vhost: obj.VirtualHost) -> None: + """Do the initial AutoHSTS deployment to a vhost + + :param ssl_vhost: The VirtualHost object to deploy the AutoHSTS + :type ssl_vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` or None + + :raises errors.PluginEnhancementAlreadyPresent: When already enhanced + + """ + # This raises the exception + self._verify_no_matching_http_header(ssl_vhost, "Strict-Transport-Security") + + if "headers_module" not in self.parser.modules: + self.enable_mod("headers") + # Prepare the HSTS header value + hsts_header = constants.HEADER_ARGS["Strict-Transport-Security"][:-1] + initial_maxage = constants.AUTOHSTS_STEPS[0] + hsts_header.append("\"max-age={0}\"".format(initial_maxage)) + + # Add ID to the VirtualHost for mapping back to it later + uniq_id = self.add_vhost_id(ssl_vhost) + if uniq_id is None: + raise errors.Error("Could not generate a unique id") # pragma: no cover + self.save_notes += "Adding unique ID {0} to VirtualHost in {1}\n".format( + uniq_id, ssl_vhost.filep) + # Add the actual HSTS header + self.parser.add_dir(ssl_vhost.path, "Header", hsts_header) + note_msg = ("Adding gradually increasing HSTS header with initial value " + "of {0} to VirtualHost in {1}\n".format( + initial_maxage, ssl_vhost.filep)) + self.save_notes += note_msg + + # Save the current state to pluginstorage + self._autohsts[uniq_id] = {"laststep": 0, "timestamp": time.time()} + + def update_autohsts(self, _unused_domain: str) -> None: + """ + Increase the AutoHSTS values of VirtualHosts that the user has enabled + this enhancement for. + + :param _unused_domain: Not currently used + :type _unused_domain: Not Available + + """ + self._autohsts_fetch_state() + if not self._autohsts: + # No AutoHSTS enabled for any domain + return + curtime = time.time() + save_and_restart = False + for id_str, config in list(self._autohsts.items()): + if config["timestamp"] + constants.AUTOHSTS_FREQ > curtime: + # Skip if last increase was < AUTOHSTS_FREQ ago + continue + nextstep = cast(int, config["laststep"]) + 1 + if nextstep < len(constants.AUTOHSTS_STEPS): + # If installer hasn't been prepared yet, do it now + if not self._prepared: + self.prepare() + # Have not reached the max value yet + try: + vhost = self.find_vhost_by_id(id_str) + except errors.PluginError: + msg = ("Could not find VirtualHost with ID {0}, disabling " + "AutoHSTS for this VirtualHost").format(id_str) + logger.error(msg) + # Remove the orphaned AutoHSTS entry from pluginstorage + self._autohsts.pop(id_str) + continue + self._autohsts_increase(vhost, id_str, nextstep) + msg = ("Increasing HSTS max-age value for VirtualHost with id " + "{0}").format(id_str) + self.save_notes += msg + save_and_restart = True + + if save_and_restart: + self.save("Increased HSTS max-age values") + self.restart() + + self._autohsts_save_state() + + def deploy_autohsts(self, lineage: RenewableCert) -> None: + """ + Checks if autohsts vhost has reached maximum auto-increased value + and changes the HSTS max-age to a high value. + + :param lineage: Certificate lineage object + :type lineage: certbot._internal.storage.RenewableCert + """ + self._autohsts_fetch_state() + if not self._autohsts: + # No autohsts enabled for any vhost + return + + vhosts: list[obj.VirtualHost] = [] + affected_ids = [] + # Copy, as we are removing from the dict inside the loop + for id_str, config in list(self._autohsts.items()): + if config["laststep"]+1 >= len(constants.AUTOHSTS_STEPS): + # max value reached, try to make permanent + try: + vhost: obj.VirtualHost = self.find_vhost_by_id(id_str) + except errors.PluginError: + msg = ("VirtualHost with id {} was not found, unable to " + "make HSTS max-age permanent.").format(id_str) + logger.error(msg) + self._autohsts.pop(id_str) + continue + if self._autohsts_vhost_in_lineage(vhost, lineage): + vhosts.append(vhost) + affected_ids.append(id_str) + + save_and_restart = False + for vhost in vhosts: + self._autohsts_write(vhost, constants.AUTOHSTS_PERMANENT) + msg = ("Strict-Transport-Security max-age value for " + "VirtualHost in {0} was made permanent.").format(vhost.filep) + logger.debug(msg) + self.save_notes += msg+"\n" + save_and_restart = True + + if save_and_restart: + self.save("Made HSTS max-age permanent") + self.restart() + + for id_str in affected_ids: + self._autohsts.pop(id_str) + + # Update AutoHSTS storage (We potentially removed vhosts from managed) + self._autohsts_save_state() + + +AutoHSTSEnhancement.register(ApacheConfigurator) diff --git a/certbot/src/certbot/_internal/plugins/apache/constants.py b/certbot/src/certbot/_internal/plugins/apache/constants.py new file mode 100644 index 00000000000..b37a943b34c --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/constants.py @@ -0,0 +1,85 @@ +"""Apache plugin constants.""" +import atexit +import importlib.resources +from contextlib import ExitStack + + +MOD_SSL_CONF_DEST = "options-ssl-apache.conf" +"""Name of the mod_ssl config file as saved +in `certbot.configuration.NamespaceConfig.config_dir`.""" + + +UPDATED_MOD_SSL_CONF_DIGEST = ".updated-options-ssl-apache-conf-digest.txt" +"""Name of the hash of the updated or informed mod_ssl_conf as saved +in `certbot.configuration.NamespaceConfig.config_dir`.""" + +# NEVER REMOVE A SINGLE HASH FROM THIS LIST UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING! +ALL_SSL_OPTIONS_HASHES: list[str] = [ + '2086bca02db48daf93468332543c60ac6acdb6f0b58c7bfdf578a5d47092f82a', + '4844d36c9a0f587172d9fa10f4f1c9518e3bcfa1947379f155e16a70a728c21a', + '5a922826719981c0a234b1fbcd495f3213e49d2519e845ea0748ba513044b65b', + '4066b90268c03c9ba0201068eaa39abbc02acf9558bb45a788b630eb85dadf27', + 'f175e2e7c673bd88d0aff8220735f385f916142c44aa83b09f1df88dd4767a88', + 'cfdd7c18d2025836ea3307399f509cfb1ebf2612c87dd600a65da2a8e2f2797b', + '80720bd171ccdc2e6b917ded340defae66919e4624962396b992b7218a561791', + 'c0c022ea6b8a51ecc8f1003d0a04af6c3f2bc1c3ce506b3c2dfc1f11ef931082', + '717b0a89f5e4c39b09a42813ac6e747cfbdeb93439499e73f4f70a1fe1473f20', + '0fcdc81280cd179a07ec4d29d3595068b9326b455c488de4b09f585d5dafc137', + '86cc09ad5415cd6d5f09a947fe2501a9344328b1e8a8b458107ea903e80baa6c', + '06675349e457eae856120cdebb564efe546f0b87399f2264baeb41e442c724c7', + '5cc003edd93fb9cd03d40c7686495f8f058f485f75b5e764b789245a386e6daf', + '007cd497a56a3bb8b6a2c1aeb4997789e7e38992f74e44cc5d13a625a738ac73', + '34783b9e2210f5c4a23bced2dfd7ec289834716673354ed7c7abf69fe30192a3', + '61466bc2f98a623c02be8a5ee916ead1655b0ce883bdc936692076ea499ff5ce', + '3fd812e3e87fe5c645d3682a511b2a06c8286f19594f28e280f17cd6af1301b5', + '27155797e160fe43b6951354a0a0ca4d829e9e605b3b41fc223c20bf2f6cb3c6', + '3a6881d0a7e5740b039ec550c916105259f53b577a3d38d0ed11bd675bfeab88', + '0f3d9c62d4274aca0406925dc4ee0919599c397e7463bce792a915b60060d004', + '95f7367d4905a1cd0932a35ce476b4a639e2108dbd1eedf924a5ea9e51fecaf7', +] +"""SHA256 hashes of the contents of previous versions of all versions of MOD_SSL_CONF_SRC""" + +def _generate_augeas_lens_dir_static() -> str: + # This code ensures that the resource is accessible as file for the lifetime of current + # Python process, and will be automatically cleaned up on exit. + file_manager = ExitStack() + atexit.register(file_manager.close) + augeas_lens_dir_ref = importlib.resources.files("certbot") / "_internal" \ + / "plugins" / "apache" / "augeas_lens" + return str(file_manager.enter_context(importlib.resources.as_file(augeas_lens_dir_ref))) + +AUGEAS_LENS_DIR = _generate_augeas_lens_dir_static() +"""Path to the Augeas lens directory""" + +REWRITE_HTTPS_ARGS: list[str] = [ + "^", "https://%{SERVER_NAME}%{REQUEST_URI}", "[END,NE,R=permanent]"] +"""Apache version >= 2.3.9 rewrite rule arguments used for redirections to + https vhost""" + +OLD_REWRITE_HTTPS_ARGS: list[list[str]] = [ + ["^", "https://%{SERVER_NAME}%{REQUEST_URI}", "[L,QSA,R=permanent]"], + ["^", "https://%{SERVER_NAME}%{REQUEST_URI}", "[END,QSA,R=permanent]"], + ["^", "https://%{SERVER_NAME}%{REQUEST_URI}", "[L,NE,R=permanent]"]] + +HSTS_ARGS: list[str] = ["always", "set", "Strict-Transport-Security", + "\"max-age=31536000\""] +"""Apache header arguments for HSTS""" + +UIR_ARGS: list[str] = ["always", "set", "Content-Security-Policy", "upgrade-insecure-requests"] + +HEADER_ARGS: dict[str, list[str]] = { + "Strict-Transport-Security": HSTS_ARGS, "Upgrade-Insecure-Requests": UIR_ARGS, +} + +AUTOHSTS_STEPS: list[int] = [60, 300, 900, 3600, 21600, 43200, 86400] +"""AutoHSTS increase steps: 1min, 5min, 15min, 1h, 6h, 12h, 24h""" + +AUTOHSTS_PERMANENT: int = 31536000 +"""Value for the last max-age of HSTS""" + +AUTOHSTS_FREQ: int = 172800 +"""Minimum time since last increase to perform a new one: 48h""" + +MANAGED_COMMENT: str = "DO NOT REMOVE - Managed by Certbot" +MANAGED_COMMENT_ID: str = MANAGED_COMMENT + ", VirtualHost id: {0}" +"""Managed by Certbot comments and the VirtualHost identification template""" diff --git a/certbot/src/certbot/_internal/plugins/apache/display_ops.py b/certbot/src/certbot/_internal/plugins/apache/display_ops.py new file mode 100644 index 00000000000..1b9c2b126b6 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/display_ops.py @@ -0,0 +1,131 @@ +"""Contains UI methods for Apache operations.""" +import logging +from typing import Iterable +from typing import Optional +from typing import Sequence + +from certbot import errors +from certbot.compat import os +from certbot.display import util as display_util +from certbot._internal.plugins.apache.obj import VirtualHost + +logger = logging.getLogger(__name__) + + +def select_vhost_multiple(vhosts: Optional[list[VirtualHost]]) -> list[VirtualHost]: + """Select multiple Vhosts to install the certificate for + + :param vhosts: Available Apache VirtualHosts + :type vhosts: :class:`list` of type `~VirtualHost` + + :returns: List of VirtualHosts + :rtype: :class:`list`of type `~VirtualHost` + """ + if not vhosts: + return [] + tags_list = [vhost.display_repr()+"\n" for vhost in vhosts] + # Remove the extra newline from the last entry + if tags_list: + tags_list[-1] = tags_list[-1][:-1] + code, names = display_util.checklist( + "Which VirtualHosts would you like to install the wildcard certificate for?", + tags=tags_list, force_interactive=True) + if code == display_util.OK: + return_vhosts = _reversemap_vhosts(names, vhosts) + return return_vhosts + return [] + + +def _reversemap_vhosts(names: Iterable[str], vhosts: Iterable[VirtualHost]) -> list[VirtualHost]: + """Helper function for select_vhost_multiple for mapping string + representations back to actual vhost objects""" + return_vhosts = [] + + for selection in names: + for vhost in vhosts: + if vhost.display_repr().strip() == selection.strip(): + return_vhosts.append(vhost) + return return_vhosts + + +def select_vhost(domain: str, vhosts: Sequence[VirtualHost]) -> Optional[VirtualHost]: + """Select an appropriate Apache Vhost. + + :param str domain: Domain for vhost selection + + :param vhosts: Available Apache VirtualHosts + :type vhosts: :class:`list` of type `~VirtualHost` + + :returns: VirtualHost or `None` + :rtype: `~obj.Vhost` or `None` + + """ + if not vhosts: + return None + code, tag = _vhost_menu(domain, vhosts) + if code == display_util.OK: + return vhosts[tag] + return None + + +def _vhost_menu(domain: str, vhosts: Iterable[VirtualHost]) -> tuple[str, int]: + """Select an appropriate Apache Vhost. + + :param vhosts: Available Apache Virtual Hosts + :type vhosts: :class:`list` of type `~obj.Vhost` + + :returns: Display tuple - ('code', tag') + :rtype: `tuple` + + """ + # Free characters in the line of display text (9 is for ' | ' formatting) + free_chars = display_util.WIDTH - len("HTTPS") - len("Enabled") - 9 + + if free_chars < 2: + logger.debug("Display size is too small for " + "certbot._internal.plugins.apache.display_ops._vhost_menu()") + # This runs the edge off the screen, but it doesn't cause an "error" + filename_size = 1 + disp_name_size = 1 + else: + # Filename is a bit more important and probably longer with 000-* + filename_size = int(free_chars * .6) + disp_name_size = free_chars - filename_size + + choices = [] + for vhost in vhosts: + if len(vhost.get_names()) == 1: + disp_name = next(iter(vhost.get_names())) + elif not vhost.get_names(): + disp_name = "" + else: + disp_name = "Multiple Names" + + choices.append( + "{fn:{fn_size}s} | {name:{name_size}s} | {https:5s} | " + "{active:7s}".format( + fn=os.path.basename(vhost.filep)[:filename_size], + name=disp_name[:disp_name_size], + https="HTTPS" if vhost.ssl else "", + active="Enabled" if vhost.enabled else "", + fn_size=filename_size, + name_size=disp_name_size), + ) + + try: + code, tag = display_util.menu( + f"We were unable to find a vhost with a ServerName " + f"or Address of {domain}.{os.linesep}Which virtual host would you " + f"like to choose?", + choices, force_interactive=True) + except errors.MissingCommandlineFlag: + msg = ( + f"Encountered vhost ambiguity when trying to find a vhost for " + f"{domain} but was unable to ask for user " + f"guidance in non-interactive mode. Certbot may need " + f"vhosts to be explicitly labelled with ServerName or " + f"ServerAlias directives.") + logger.error(msg) + raise errors.MissingCommandlineFlag(msg) + + return code, tag diff --git a/certbot/src/certbot/_internal/plugins/apache/dualparser.py b/certbot/src/certbot/_internal/plugins/apache/dualparser.py new file mode 100644 index 00000000000..65d71bab8a3 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/dualparser.py @@ -0,0 +1,331 @@ +""" Dual ParserNode implementation """ +from typing import Any +from typing import Generic +from typing import Iterable +from typing import Optional +from typing import TYPE_CHECKING +from typing import TypeVar + +from certbot._internal.plugins.apache import apacheparser +from certbot._internal.plugins.apache import assertions +from certbot._internal.plugins.apache import augeasparser +from certbot._internal.plugins.apache import interfaces + +if TYPE_CHECKING: + from certbot._internal.plugins.apache.apacheparser import ApacheParserNode # pragma: no cover + from certbot._internal.plugins.apache.augeasparser import AugeasParserNode # pragma: no cover + +GenericAugeasParserNode = TypeVar("GenericAugeasParserNode", bound="AugeasParserNode") +GenericApacheParserNode = TypeVar("GenericApacheParserNode", bound="ApacheParserNode") +# Circular imports needs Any, see https://github.com/python/mypy/issues/11910 +GenericDualNode = TypeVar("GenericDualNode", bound="DualNodeBase[Any, Any]") + + +class DualNodeBase(Generic[GenericAugeasParserNode, GenericApacheParserNode]): + """ Dual parser interface for in development testing. This is used as the + base class for dual parser interface classes. This class handles runtime + attribute value assertions.""" + + def __init__(self, primary: GenericAugeasParserNode, + secondary: GenericApacheParserNode) -> None: + self.primary = primary + self.secondary = secondary + + def save(self, msg: str) -> None: # pragma: no cover + """ Call save for both parsers """ + self.primary.save(msg) + self.secondary.save(msg) + + def __getattr__(self, aname: str) -> Any: + """ Attribute value assertion """ + firstval = getattr(self.primary, aname) + secondval = getattr(self.secondary, aname) + exclusions = [ + # Metadata will inherently be different, as ApacheParserNode does + # not have Augeas paths and so on. + aname == "metadata", + callable(firstval) + ] + if not any(exclusions): + assertions.assertEqualSimple(firstval, secondval) + return firstval + + def find_ancestors(self, name: str) -> list["DualBlockNode"]: + """ Traverses the ancestor tree and returns ancestors matching name """ + return self._find_helper(DualBlockNode, "find_ancestors", name) + + def _find_helper(self, nodeclass: type[GenericDualNode], findfunc: str, search: str, + **kwargs: Any) -> list[GenericDualNode]: + """A helper for find_* functions. The function specific attributes should + be passed as keyword arguments. + + :param interfaces.ParserNode nodeclass: The node class for results. + :param str findfunc: Name of the find function to call + :param str search: The search term + """ + + primary_res = getattr(self.primary, findfunc)(search, **kwargs) + secondary_res = getattr(self.secondary, findfunc)(search, **kwargs) + + # The order of search results for Augeas implementation cannot be + # assured. + + pass_primary = assertions.isPassNodeList(primary_res) + pass_secondary = assertions.isPassNodeList(secondary_res) + new_nodes = [] + + if pass_primary and pass_secondary: + # Both unimplemented + new_nodes.append(nodeclass(primary=primary_res[0], + secondary=secondary_res[0])) # pragma: no cover + elif pass_primary: + for c in secondary_res: + new_nodes.append(nodeclass(primary=primary_res[0], + secondary=c)) + elif pass_secondary: + for c in primary_res: + new_nodes.append(nodeclass(primary=c, + secondary=secondary_res[0])) + else: + assert len(primary_res) == len(secondary_res) + matches = self._create_matching_list(primary_res, secondary_res) + for p, s in matches: + new_nodes.append(nodeclass(primary=p, secondary=s)) + + return new_nodes + + +class DualCommentNode(DualNodeBase[augeasparser.AugeasCommentNode, + apacheparser.ApacheCommentNode]): + """ Dual parser implementation of CommentNode interface """ + + def __init__(self, **kwargs: Any) -> None: + """ This initialization implementation allows ordinary initialization + of CommentNode objects as well as creating a DualCommentNode object + using precreated or fetched CommentNode objects if provided as optional + arguments primary and secondary. + + Parameters other than the following are from interfaces.CommentNode: + + :param CommentNode primary: Primary pre-created CommentNode, mainly + used when creating new DualParser nodes using add_* methods. + :param CommentNode secondary: Secondary pre-created CommentNode + """ + + kwargs.setdefault("primary", None) + kwargs.setdefault("secondary", None) + primary = kwargs.pop("primary") + secondary = kwargs.pop("secondary") + + if primary or secondary: + assert primary and secondary + super().__init__(primary, secondary) + else: + super().__init__(augeasparser.AugeasCommentNode(**kwargs), + apacheparser.ApacheCommentNode(**kwargs)) + + assertions.assertEqual(self.primary, self.secondary) + + +class DualDirectiveNode(DualNodeBase[augeasparser.AugeasDirectiveNode, + apacheparser.ApacheDirectiveNode]): + """ Dual parser implementation of DirectiveNode interface """ + + parameters: str + + def __init__(self, **kwargs: Any) -> None: + """ This initialization implementation allows ordinary initialization + of DirectiveNode objects as well as creating a DualDirectiveNode object + using precreated or fetched DirectiveNode objects if provided as optional + arguments primary and secondary. + + Parameters other than the following are from interfaces.DirectiveNode: + + :param DirectiveNode primary: Primary pre-created DirectiveNode, mainly + used when creating new DualParser nodes using add_* methods. + :param DirectiveNode secondary: Secondary pre-created DirectiveNode + """ + + kwargs.setdefault("primary", None) + kwargs.setdefault("secondary", None) + primary = kwargs.pop("primary") + secondary = kwargs.pop("secondary") + + if primary or secondary: + assert primary and secondary + super().__init__(primary, secondary) + else: + super().__init__(augeasparser.AugeasDirectiveNode(**kwargs), + apacheparser.ApacheDirectiveNode(**kwargs)) + + assertions.assertEqual(self.primary, self.secondary) + + def set_parameters(self, parameters: Iterable[str]) -> None: + """ Sets parameters and asserts that both implementation successfully + set the parameter sequence """ + + self.primary.set_parameters(parameters) + self.secondary.set_parameters(parameters) + assertions.assertEqual(self.primary, self.secondary) + + +class DualBlockNode(DualNodeBase[augeasparser.AugeasBlockNode, + apacheparser.ApacheBlockNode]): + """ Dual parser implementation of BlockNode interface """ + + def __init__(self, **kwargs: Any) -> None: + """ This initialization implementation allows ordinary initialization + of BlockNode objects as well as creating a DualBlockNode object + using precreated or fetched BlockNode objects if provided as optional + arguments primary and secondary. + + Parameters other than the following are from interfaces.BlockNode: + + :param BlockNode primary: Primary pre-created BlockNode, mainly + used when creating new DualParser nodes using add_* methods. + :param BlockNode secondary: Secondary pre-created BlockNode + """ + + kwargs.setdefault("primary", None) + kwargs.setdefault("secondary", None) + primary: Optional[augeasparser.AugeasBlockNode] = kwargs.pop("primary") + secondary: Optional[apacheparser.ApacheBlockNode] = kwargs.pop("secondary") + + if primary or secondary: + assert primary and secondary + super().__init__(primary, secondary) + else: + super().__init__(augeasparser.AugeasBlockNode(**kwargs), + apacheparser.ApacheBlockNode(**kwargs)) + + assertions.assertEqual(self.primary, self.secondary) + + def add_child_block(self, name: str, parameters: Optional[list[str]] = None, + position: Optional[int] = None) -> "DualBlockNode": + """ Creates a new child BlockNode, asserts that both implementations + did it in a similar way, and returns a newly created DualBlockNode object + encapsulating both of the newly created objects """ + + primary_new = self.primary.add_child_block(name, parameters, position) + secondary_new = self.secondary.add_child_block(name, parameters, position) + assertions.assertEqual(primary_new, secondary_new) + return DualBlockNode(primary=primary_new, secondary=secondary_new) + + def add_child_directive(self, name: str, parameters: Optional[list[str]] = None, + position: Optional[int] = None) -> DualDirectiveNode: + """ Creates a new child DirectiveNode, asserts that both implementations + did it in a similar way, and returns a newly created DualDirectiveNode + object encapsulating both of the newly created objects """ + + primary_new = self.primary.add_child_directive(name, parameters, position) + secondary_new = self.secondary.add_child_directive(name, parameters, position) + assertions.assertEqual(primary_new, secondary_new) + return DualDirectiveNode(primary=primary_new, secondary=secondary_new) + + def add_child_comment(self, comment: str = "", + position: Optional[int] = None) -> DualCommentNode: + """ Creates a new child CommentNode, asserts that both implementations + did it in a similar way, and returns a newly created DualCommentNode + object encapsulating both of the newly created objects """ + + primary_new = self.primary.add_child_comment(comment=comment, position=position) + secondary_new = self.secondary.add_child_comment(name=comment, position=position) + assertions.assertEqual(primary_new, secondary_new) + return DualCommentNode(primary=primary_new, secondary=secondary_new) + + def _create_matching_list(self, primary_list: Iterable[interfaces.ParserNode], + secondary_list: Iterable[interfaces.ParserNode] + ) -> list[tuple[interfaces.ParserNode, interfaces.ParserNode]]: + """ Matches the list of primary_list to a list of secondary_list and + returns a list of tuples. This is used to create results for find_ + methods. + + This helper function exists, because we cannot ensure that the list of + search results returned by primary.find_* and secondary.find_* are ordered + in a same way. The function pairs the same search results from both + implementations to a list of tuples. + """ + + matched = [] + for p in primary_list: + match = None + for s in secondary_list: + try: + assertions.assertEqual(p, s) + match = s + break + except AssertionError: + continue + if match: + matched.append((p, match)) + else: + raise AssertionError("Could not find a matching node.") + return matched + + def find_blocks(self, name: str, exclude: bool = True) -> list["DualBlockNode"]: + """ + Performs a search for BlockNodes using both implementations and does simple + checks for results. This is built upon the assumption that unimplemented + find_* methods return a list with a single assertion passing object. + After the assertion, it creates a list of newly created DualBlockNode + instances that encapsulate the pairs of returned BlockNode objects. + """ + + return self._find_helper(DualBlockNode, "find_blocks", name, + exclude=exclude) + + def find_directives(self, name: str, exclude: bool = True) -> list[DualDirectiveNode]: + """ + Performs a search for DirectiveNodes using both implementations and + checks the results. This is built upon the assumption that unimplemented + find_* methods return a list with a single assertion passing object. + After the assertion, it creates a list of newly created DualDirectiveNode + instances that encapsulate the pairs of returned DirectiveNode objects. + """ + + return self._find_helper(DualDirectiveNode, "find_directives", name, + exclude=exclude) + + def find_comments(self, comment: str) -> list[DualCommentNode]: + """ + Performs a search for CommentNodes using both implementations and + checks the results. This is built upon the assumption that unimplemented + find_* methods return a list with a single assertion passing object. + After the assertion, it creates a list of newly created DualCommentNode + instances that encapsulate the pairs of returned CommentNode objects. + """ + + return self._find_helper(DualCommentNode, "find_comments", comment) + + def delete_child(self, child: "DualBlockNode") -> None: + """Deletes a child from the ParserNode implementations. The actual + ParserNode implementations are used here directly in order to be able + to match a child to the list of children.""" + + self.primary.delete_child(child.primary) + self.secondary.delete_child(child.secondary) + + def unsaved_files(self) -> set[str]: + """ Fetches the list of unsaved file paths and asserts that the lists + match """ + primary_files = self.primary.unsaved_files() + secondary_files = self.secondary.unsaved_files() + assertions.assertEqualSimple(primary_files, secondary_files) + + return primary_files + + def parsed_paths(self) -> list[str]: + """ + Returns a list of file paths that have currently been parsed into the parser + tree. The returned list may include paths with wildcard characters, for + example: ['/etc/apache2/conf.d/*.load'] + + This is typically called on the root node of the ParserNode tree. + + :returns: list of file paths of files that have been parsed + """ + + primary_paths = self.primary.parsed_paths() + secondary_paths = self.secondary.parsed_paths() + assertions.assertEqualPathsList(primary_paths, secondary_paths) + return primary_paths diff --git a/certbot/src/certbot/_internal/plugins/apache/entrypoint.py b/certbot/src/certbot/_internal/plugins/apache/entrypoint.py new file mode 100644 index 00000000000..089b2929832 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/entrypoint.py @@ -0,0 +1,66 @@ +""" Entry point for Apache Plugin """ + +from certbot import util +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache import override_alpine +from certbot._internal.plugins.apache import override_arch +from certbot._internal.plugins.apache import override_centos +from certbot._internal.plugins.apache import override_darwin +from certbot._internal.plugins.apache import override_debian +from certbot._internal.plugins.apache import override_fedora +from certbot._internal.plugins.apache import override_gentoo +from certbot._internal.plugins.apache import override_suse +from certbot._internal.plugins.apache import override_void + +OVERRIDE_CLASSES: dict[str, type[configurator.ApacheConfigurator]] = { + "alpine": override_alpine.AlpineConfigurator, + "arch": override_arch.ArchConfigurator, + "cloudlinux": override_centos.CentOSConfigurator, + "darwin": override_darwin.DarwinConfigurator, + "debian": override_debian.DebianConfigurator, + "ubuntu": override_debian.DebianConfigurator, + "centos": override_centos.CentOSConfigurator, + "centos linux": override_centos.CentOSConfigurator, + "fedora_old": override_centos.CentOSConfigurator, + "fedora": override_fedora.FedoraConfigurator, + "linuxmint": override_debian.DebianConfigurator, + "ol": override_centos.CentOSConfigurator, + "oracle": override_centos.CentOSConfigurator, + "redhatenterpriseserver": override_centos.CentOSConfigurator, + "red hat enterprise linux server": override_centos.CentOSConfigurator, + "rhel": override_centos.CentOSConfigurator, + "amazon": override_centos.CentOSConfigurator, + "gentoo": override_gentoo.GentooConfigurator, + "gentoo base system": override_gentoo.GentooConfigurator, + "opensuse": override_suse.OpenSUSEConfigurator, + "suse": override_suse.OpenSUSEConfigurator, + "sles": override_suse.OpenSUSEConfigurator, + "scientific": override_centos.CentOSConfigurator, + "scientific linux": override_centos.CentOSConfigurator, + "void": override_void.VoidConfigurator, +} + + +def get_configurator() -> type[configurator.ApacheConfigurator]: + """ Get correct configurator class based on the OS fingerprint """ + os_name, os_version = util.get_os_info() + os_name = os_name.lower() + override_class = None + + # Special case for older Fedora versions + min_version = util.parse_loose_version('29') + if os_name == 'fedora' and util.parse_loose_version(os_version) < min_version: + os_name = 'fedora_old' + + try: + override_class = OVERRIDE_CLASSES[os_name] + except KeyError: + # OS not found in the list + os_like = util.get_systemd_os_like() + if os_like: + for os_name in os_like: + override_class = OVERRIDE_CLASSES.get(os_name) + if not override_class: + # No override class found, return the generic configurator + override_class = configurator.ApacheConfigurator + return override_class diff --git a/certbot/src/certbot/_internal/plugins/apache/http_01.py b/certbot/src/certbot/_internal/plugins/apache/http_01.py new file mode 100644 index 00000000000..72220a50987 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/http_01.py @@ -0,0 +1,207 @@ +"""A class that performs HTTP-01 challenges for Apache""" +import errno +import logging +from typing import TYPE_CHECKING + +from acme.challenges import KeyAuthorizationChallengeResponse +from acme import messages +from certbot import errors +from certbot.achallenges import KeyAuthorizationAnnotatedChallenge +from certbot.compat import filesystem +from certbot.compat import os +from certbot.plugins import common +from certbot._internal.plugins.apache.obj import VirtualHost +from certbot._internal.plugins.apache.parser import get_aug_path + +if TYPE_CHECKING: + from certbot._internal.plugins.apache.configurator import ApacheConfigurator # pragma: no cover + +logger = logging.getLogger(__name__) + + +class ApacheHttp01(common.ChallengePerformer): + """Class that performs HTTP-01 challenges within the Apache configurator.""" + + CONFIG_TEMPLATE24_PRE = """\ + RewriteRule ^/\\.well-known/acme-challenge/([A-Za-z0-9-_=]+)$ {0}/$1 [END] + """ + CONFIG_TEMPLATE24_POST = """\ + RewriteEngine on + + Require all granted + + + Require all granted + + """ + + def __init__(self, configurator: "ApacheConfigurator") -> None: + super().__init__(configurator) + self.configurator: "ApacheConfigurator" + self.challenge_conf_pre = os.path.join( + self.configurator.conf("challenge-location"), + "le_http_01_challenge_pre.conf") + self.challenge_conf_post = os.path.join( + self.configurator.conf("challenge-location"), + "le_http_01_challenge_post.conf") + self.challenge_dir = os.path.join( + self.configurator.config.work_dir, + "http_challenges") + self.moded_vhosts: set[VirtualHost] = set() + + def perform(self) -> list[KeyAuthorizationChallengeResponse]: + """Perform all HTTP-01 challenges.""" + if not self.achalls: + return [] + if any(achall.identifier.typ == messages.IDENTIFIER_IP for achall in self.achalls): + raise errors.ConfigurationError( + "Apache authenticator not supported for IP address certificates") + # Save any changes to the configuration as a precaution + # About to make temporary changes to the config + self.configurator.save("Changes before challenge setup", True) + + self.configurator.ensure_listen(str(self.configurator.config.http01_port)) + self.prepare_http01_modules() + + responses = self._set_up_challenges() + + self._mod_config() + # Save reversible changes + self.configurator.save("HTTP Challenge", True) + + return responses + + def prepare_http01_modules(self) -> None: + """Make sure that we have the needed modules available for http01""" + + if self.configurator.conf("handle-modules"): + needed_modules = ["rewrite", "authz_core"] + for mod in needed_modules: + if mod + "_module" not in self.configurator.parser.modules: + self.configurator.enable_mod(mod, temp=True) + + def _mod_config(self) -> None: + selected_vhosts: list[VirtualHost] = [] + http_port = str(self.configurator.config.http01_port) + + # Search for VirtualHosts matching by name + for chall in self.achalls: + selected_vhosts += self._matching_vhosts(chall.identifier.value) + + # Ensure that we have one or more VirtualHosts that we can continue + # with. (one that listens to port configured with --http-01-port) + found = False + for vhost in selected_vhosts: + if any(a.is_wildcard() or a.get_port() == http_port for a in vhost.addrs): + found = True + + # If there's at least one eligible VirtualHost, also add all unnamed VirtualHosts + # because they might match at runtime (#8890) + if found: + selected_vhosts += self._unnamed_vhosts() + # Otherwise, add every Virtualhost which listens on the right port + else: + selected_vhosts += self._relevant_vhosts() + + # Add the challenge configuration + for vh in selected_vhosts: + self._set_up_include_directives(vh) + + self.configurator.reverter.register_file_creation( + True, self.challenge_conf_pre) + self.configurator.reverter.register_file_creation( + True, self.challenge_conf_post) + + config_text_pre = self.CONFIG_TEMPLATE24_PRE.format(self.challenge_dir) + config_text_post = self.CONFIG_TEMPLATE24_POST.format(self.challenge_dir) + + logger.debug("writing a pre config file with text:\n %s", config_text_pre) + with open(self.challenge_conf_pre, "w") as new_conf: + new_conf.write(config_text_pre) + logger.debug("writing a post config file with text:\n %s", config_text_post) + with open(self.challenge_conf_post, "w") as new_conf: + new_conf.write(config_text_post) + + def _matching_vhosts(self, domain: str) -> list[VirtualHost]: + """Return all VirtualHost objects that have the requested domain name or + a wildcard name that would match the domain in ServerName or ServerAlias + directive. + """ + matching_vhosts = [] + for vhost in self.configurator.vhosts: + if self.configurator.domain_in_names(vhost.get_names(), domain): + # domain_in_names also matches the exact names, so no need + # to check "domain in vhost.get_names()" explicitly here + matching_vhosts.append(vhost) + + return matching_vhosts + + def _relevant_vhosts(self) -> list[VirtualHost]: + http01_port = str(self.configurator.config.http01_port) + relevant_vhosts: list[VirtualHost] = [] + for vhost in self.configurator.vhosts: + if any(a.is_wildcard() or a.get_port() == http01_port for a in vhost.addrs): + if not vhost.ssl: + relevant_vhosts.append(vhost) + if not relevant_vhosts: + raise errors.PluginError( + "Unable to find a virtual host listening on port {0} which is" + " currently needed for Certbot to prove to the CA that you" + " control your domain. Please add a virtual host for port" + " {0}.".format(http01_port)) + + return relevant_vhosts + + def _unnamed_vhosts(self) -> list[VirtualHost]: + """Return all VirtualHost objects with no ServerName""" + return [vh for vh in self.configurator.vhosts if vh.name is None] + + def _set_up_challenges(self) -> list[KeyAuthorizationChallengeResponse]: + if not os.path.isdir(self.challenge_dir): + with filesystem.temp_umask(0o022): + try: + filesystem.makedirs(self.challenge_dir, 0o755) + except OSError as exception: + if exception.errno not in (errno.EEXIST, errno.EISDIR): + raise errors.PluginError( + "Couldn't create root for http-01 challenge") + + responses = [] + for achall in self.achalls: + responses.append(self._set_up_challenge(achall)) + + return responses + + def _set_up_challenge(self, achall: KeyAuthorizationAnnotatedChallenge + ) -> KeyAuthorizationChallengeResponse: + response: KeyAuthorizationChallengeResponse + response, validation = achall.response_and_validation() + + name: str = os.path.join(self.challenge_dir, achall.chall.encode("token")) + + self.configurator.reverter.register_file_creation(True, name) + with open(name, 'wb') as f: + f.write(validation.encode()) + filesystem.chmod(name, 0o644) + + return response + + def _set_up_include_directives(self, vhost: VirtualHost) -> None: + """Includes override configuration to the beginning and to the end of + VirtualHost. Note that this include isn't added to Augeas search tree""" + + if vhost not in self.moded_vhosts: + logger.debug( + "Adding a temporary challenge validation Include for name: %s in: %s", + vhost.name, vhost.filep) + self.configurator.parser.add_dir_beginning( + vhost.path, "Include", self.challenge_conf_pre) + self.configurator.parser.add_dir( + vhost.path, "Include", self.challenge_conf_post) + + if not vhost.enabled: + self.configurator.parser.add_dir( + get_aug_path(self.configurator.parser.loc["default"]), + "Include", vhost.filep) + + self.moded_vhosts.add(vhost) diff --git a/certbot/src/certbot/_internal/plugins/apache/interfaces.py b/certbot/src/certbot/_internal/plugins/apache/interfaces.py new file mode 100644 index 00000000000..b2db7690e3f --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/interfaces.py @@ -0,0 +1,527 @@ +"""ParserNode interface for interacting with configuration tree. + +General description +------------------- + +The ParserNode interfaces are designed to be able to contain all the parsing logic, +while allowing their users to interact with the configuration tree in a Pythonic +and well structured manner. + +The structure allows easy traversal of the tree of ParserNodes. Each ParserNode +stores a reference to its ancestor and immediate children, allowing the user to +traverse the tree using built in interface methods as well as accessing the interface +properties directly. + +ParserNode interface implementation should stand between the actual underlying +parser functionality and the business logic within Configurator code, interfacing +with both. The ParserNode tree is a result of configuration parsing action. + +ParserNode tree will be in charge of maintaining the parser state and hence the +abstract syntax tree (AST). Interactions between ParserNode tree and underlying +parser should involve only parsing the configuration files to this structure, and +writing it back to the filesystem - while preserving the format including whitespaces. + +For some implementations (Apache for example) it's important to keep track of and +to use state information while parsing conditional blocks and directives. This +allows the implementation to set a flag to parts of the parsed configuration +structure as not being in effect in a case of unmatched conditional block. It's +important to store these blocks in the tree as well in order to not to conduct +destructive actions (failing to write back parts of the configuration) while writing +the AST back to the filesystem. + +The ParserNode tree is in charge of maintaining the its own structure while every +child node fetched with find - methods or by iterating its list of children can be +changed in place. When making changes the affected nodes should be flagged as "dirty" +in order for the parser implementation to figure out the parts of the configuration +that need to be written back to disk during the save() operation. + + +Metadata +-------- + +The metadata holds all the implementation specific attributes of the ParserNodes - +things like the positional information related to the AST, file paths, whitespacing, +and any other information relevant to the underlying parser engine. + +Access to the metadata should be handled by implementation specific methods, allowing +the Configurator functionality to access the underlying information where needed. + +For some implementations the node can be initialized using the information carried +in metadata alone. This is useful especially when populating the ParserNode tree +while parsing the configuration. + + +Apache implementation +--------------------- + +The Apache implementation of ParserNode interface requires some implementation +specific functionalities that are not described by the interface itself. + +Initialization + +When the user of a ParserNode class is creating these objects, they must specify +the parameters as described in the documentation for the __init__ methods below. +When these objects are created internally, however, some parameters may not be +needed because (possibly more detailed) information is included in the metadata +parameter. In this case, implementations can deviate from the required parameters +from __init__, however, they should still behave the same when metadata is not +provided. + +For consistency internally, if an argument is provided directly in the ParserNode +initialization parameters as well as within metadata it's recommended to establish +clear behavior around this scenario within the implementation. + +Conditional blocks + +Apache configuration can have conditional blocks, for example: , +resulting the directives and subblocks within it being either enabled or disabled. +While find_* interface methods allow including the disabled parts of the configuration +tree in searches a special care needs to be taken while parsing the structure in +order to reflect the active state of configuration. + +Whitespaces + +Each ParserNode object is responsible of storing its prepending whitespace characters +in order to be able to write the AST back to filesystem like it was, preserving the +format, this applies for parameters of BlockNode and DirectiveNode as well. +When parameters of ParserNode are changed, the pre-existing whitespaces in the +parameter sequence are discarded, as the general reason for storing them is to +maintain the ability to write the configuration back to filesystem exactly like +it was. This loses its meaning when we have to change the directives or blocks +parameters for other reasons. + +Searches and matching + +Apache configuration is largely case insensitive, so the Apache implementation of +ParserNode interface needs to provide the user means to match block and directive +names and parameters in case insensitive manner. This does not apply to everything +however, for example the parameters of a conditional statement may be case sensitive. +For this reason the internal representation of data should not ignore the case. +""" +import abc +from typing import Any +from typing import Optional +from typing import TypeVar + +GenericParserNode = TypeVar("GenericParserNode", bound="ParserNode") + + +class ParserNode(metaclass=abc.ABCMeta): + """ + ParserNode is the basic building block of the tree of such nodes, + representing the structure of the configuration. It is largely meant to keep + the structure information intact and idiomatically accessible. + + The root node as well as the child nodes of it should be instances of ParserNode. + Nodes keep track of their differences to on-disk representation of configuration + by marking modified ParserNodes as dirty to enable partial write-to-disk for + different files in the configuration structure. + + While for the most parts the usage and the child types are obvious, "include"- + and similar directives are an exception to this rule. This is because of the + nature of include directives - which unroll the contents of another file or + configuration block to their place. While we could unroll the included nodes + to the parent tree, it remains important to keep the context of include nodes + separate in order to write back the original configuration as it was. + + For parsers that require the implementation to keep track of the whitespacing, + it's responsibility of each ParserNode object itself to store its prepending + whitespaces in order to be able to reconstruct the complete configuration file + as it was when originally read from the disk. + + ParserNode objects should have the following attributes: + + # Reference to ancestor node, or None if the node is the root node of the + # configuration tree. + ancestor: Optional[ParserNode] + + # True if this node has been modified since last save. + dirty: bool + + # Filepath of the file where the configuration element for this ParserNode + # object resides. For root node, the value for filepath is the httpd root + # configuration file. Filepath can be None if a configuration directive is + # defined in for example the httpd command line. + filepath: Optional[str] + + # Metadata dictionary holds all the implementation specific key-value pairs + # for the ParserNode instance. + metadata: Dict[str, Any] + """ + ancestor: Optional["ParserNode"] + dirty: bool + filepath: Optional[str] + metadata: dict[str, Any] + + @abc.abstractmethod + def __init__(self, **kwargs: Any) -> None: + """ + Initializes the ParserNode instance, and sets the ParserNode specific + instance variables. This is not meant to be used directly, but through + specific classes implementing ParserNode interface. + + :param ancestor: BlockNode ancestor for this CommentNode. Required. + :type ancestor: BlockNode or None + + :param filepath: Filesystem path for the file where this CommentNode + does or should exist in the filesystem. Required. + :type filepath: str or None + + :param dirty: Boolean flag for denoting if this CommentNode has been + created or changed after the last save. Default: False. + :type dirty: bool + + :param metadata: Dictionary of metadata values for this ParserNode object. + Metadata information should be used only internally in the implementation. + Default: {} + :type metadata: dict + """ + + @abc.abstractmethod + def save(self, msg: str) -> None: + """ + Save traverses the children, and attempts to write the AST to disk for + all the objects that are marked dirty. The actual operation of course + depends on the underlying implementation. save() shouldn't be called + from the Configurator outside of its designated save() method in order + to ensure that the Reverter checkpoints are created properly. + + Note: this approach of keeping internal structure of the configuration + within the ParserNode tree does not represent the file inclusion structure + of actual configuration files that reside in the filesystem. To handle + file writes properly, the file specific temporary trees should be extracted + from the full ParserNode tree where necessary when writing to disk. + + :param str msg: Message describing the reason for the save. + + """ + + @abc.abstractmethod + def find_ancestors(self: GenericParserNode, name: str) -> list[GenericParserNode]: + """ + Traverses the ancestor tree up, searching for BlockNodes with a specific + name. + + :param str name: Name of the ancestor BlockNode to search for + + :returns: A list of ancestor BlockNodes that match the name + :rtype: list of BlockNode + """ + + +class CommentNode(ParserNode, metaclass=abc.ABCMeta): + """ + CommentNode class is used for representation of comments within the parsed + configuration structure. Because of the nature of comments, it is not able + to have child nodes and hence it is always treated as a leaf node. + + CommentNode stores its contents in class variable 'comment' and does not + have a specific name. + + CommentNode objects should have the following attributes in addition to + the ones described in ParserNode: + + # Contains the contents of the comment without the directive notation + # (typically # or /* ... */). + comment: str + + """ + comment: str + + @abc.abstractmethod + def __init__(self, **kwargs: Any) -> None: + """ + Initializes the CommentNode instance and sets its instance variables. + + :param comment: Contents of the comment. Required. + :type comment: str + + :param ancestor: BlockNode ancestor for this CommentNode. Required. + :type ancestor: BlockNode or None + + :param filepath: Filesystem path for the file where this CommentNode + does or should exist in the filesystem. Required. + :type filepath: str or None + + :param dirty: Boolean flag for denoting if this CommentNode has been + created or changed after the last save. Default: False. + :type dirty: bool + """ + super().__init__( # pragma: no cover + ancestor=kwargs['ancestor'], + dirty=kwargs.get('dirty', False), + filepath=kwargs['filepath'], + metadata=kwargs.get('metadata', {}), + ) + + +class DirectiveNode(ParserNode, metaclass=abc.ABCMeta): + """ + DirectiveNode class represents a configuration directive within the configuration. + It can have zero or more parameters attached to it. Because of the nature of + single directives, it is not able to have child nodes and hence it is always + treated as a leaf node. + + If a this directive was defined on the httpd command line, the ancestor instance + variable for this DirectiveNode should be None, and it should be inserted to the + beginning of root BlockNode children sequence. + + DirectiveNode objects should have the following attributes in addition to + the ones described in ParserNode: + + # True if this DirectiveNode is enabled and False if it is inside of an + # inactive conditional block. + enabled: bool + + # Name, or key of the configuration directive. If BlockNode subclass of + # DirectiveNode is the root configuration node, the name should be None. + name: Optional[str] + + # Tuple of parameters of this ParserNode object, excluding whitespaces. + parameters: Tuple[str, ...] + + """ + enabled: bool + name: Optional[str] + parameters: tuple[str, ...] + + @abc.abstractmethod + def __init__(self, **kwargs: Any) -> None: + """ + Initializes the DirectiveNode instance and sets its instance variables. + + :param name: Name or key of the DirectiveNode object. Required. + :type name: str or None + + :param tuple parameters: Tuple of str parameters for this DirectiveNode. + Default: (). + :type parameters: tuple + + :param ancestor: BlockNode ancestor for this DirectiveNode, or None for + root configuration node. Required. + :type ancestor: BlockNode or None + + :param filepath: Filesystem path for the file where this DirectiveNode + does or should exist in the filesystem, or None for directives introduced + in the httpd command line. Required. + :type filepath: str or None + + :param dirty: Boolean flag for denoting if this DirectiveNode has been + created or changed after the last save. Default: False. + :type dirty: bool + + :param enabled: True if this DirectiveNode object is parsed in the active + configuration of the httpd. False if the DirectiveNode exists within a + unmatched conditional configuration block. Default: True. + :type enabled: bool + + """ + super().__init__( # pragma: no cover + ancestor=kwargs['ancestor'], + dirty=kwargs.get('dirty', False), + filepath=kwargs['filepath'], + metadata=kwargs.get('metadata', {}), + ) + + @abc.abstractmethod + def set_parameters(self, parameters: list[str]) -> None: + """ + Sets the sequence of parameters for this ParserNode object without + whitespaces. While the whitespaces for parameters are discarded when using + this method, the whitespacing preceding the ParserNode itself should be + kept intact. + + :param list parameters: sequence of parameters + """ + + +class BlockNode(DirectiveNode, metaclass=abc.ABCMeta): + """ + BlockNode class represents a block of nested configuration directives, comments + and other blocks as its children. A BlockNode can have zero or more parameters + attached to it. + + Configuration blocks typically consist of one or more child nodes of all possible + types. Because of this, the BlockNode class has various discovery and structure + management methods. + + Lists of parameters used as an optional argument for some of the methods should + be lists of strings that are applicable parameters for each specific BlockNode + or DirectiveNode type. As an example, for a following configuration example: + + + ... + + + The node type would be BlockNode, name would be 'VirtualHost' and its parameters + would be: ['*:80']. + + While for the following example: + + LoadModule alias_module /usr/lib/apache2/modules/mod_alias.so + + The node type would be DirectiveNode, name would be 'LoadModule' and its + parameters would be: ['alias_module', '/usr/lib/apache2/modules/mod_alias.so'] + + The applicable parameters are dependent on the underlying configuration language + and its grammar. + + BlockNode objects should have the following attributes in addition to + the ones described in DirectiveNode: + + # Tuple of direct children of this BlockNode object. The order of children + # in this tuple retain the order of elements in the parsed configuration + # block. + children: Tuple[ParserNode, ...] + + """ + children: tuple[ParserNode, ...] + + @abc.abstractmethod + def add_child_block(self, name: str, parameters: Optional[list[str]] = None, + position: Optional[int] = None) -> "BlockNode": + """ + Adds a new BlockNode child node with provided values and marks the callee + BlockNode dirty. This is used to add new children to the AST. The preceding + whitespaces should not be added based on the ancestor or siblings for the + newly created object. This is to match the current behavior of the legacy + parser implementation. + + :param str name: The name of the child node to add + :param list parameters: list of parameters for the node + :param int position: Position in the list of children to add the new child + node to. Defaults to None, which appends the newly created node to the list. + If an integer is given, the child is inserted before that index in the + list similar to list().insert. + + :returns: BlockNode instance of the created child block + + """ + + @abc.abstractmethod + def add_child_directive(self, name: str, parameters: Optional[list[str]] = None, + position: Optional[int] = None) -> DirectiveNode: + """ + Adds a new DirectiveNode child node with provided values and marks the + callee BlockNode dirty. This is used to add new children to the AST. The + preceding whitespaces should not be added based on the ancestor or siblings + for the newly created object. This is to match the current behavior of the + legacy parser implementation. + + + :param str name: The name of the child node to add + :param list parameters: list of parameters for the node + :param int position: Position in the list of children to add the new child + node to. Defaults to None, which appends the newly created node to the list. + If an integer is given, the child is inserted before that index in the + list similar to list().insert. + + :returns: DirectiveNode instance of the created child directive + + """ + + @abc.abstractmethod + def add_child_comment(self, comment: str = "", position: Optional[int] = None) -> CommentNode: + """ + Adds a new CommentNode child node with provided value and marks the + callee BlockNode dirty. This is used to add new children to the AST. The + preceding whitespaces should not be added based on the ancestor or siblings + for the newly created object. This is to match the current behavior of the + legacy parser implementation. + + + :param str comment: Comment contents + :param int position: Position in the list of children to add the new child + node to. Defaults to None, which appends the newly created node to the list. + If an integer is given, the child is inserted before that index in the + list similar to list().insert. + + :returns: CommentNode instance of the created child comment + + """ + + @abc.abstractmethod + def find_blocks(self, name: str, exclude: bool = True) -> list["BlockNode"]: + """ + Find a configuration block by name. This method walks the child tree of + ParserNodes under the instance it was called from. This way it is possible + to search for the whole configuration tree, when starting from root node or + to do a partial search when starting from a specified branch. The lookup + should be case insensitive. + + :param str name: The name of the directive to search for + :param bool exclude: If the search results should exclude the contents of + ParserNode objects that reside within conditional blocks and because + of current state are not enabled. + + :returns: A list of found BlockNode objects. + """ + + @abc.abstractmethod + def find_directives(self, name: str, exclude: bool = True) -> list[DirectiveNode]: + """ + Find a directive by name. This method walks the child tree of ParserNodes + under the instance it was called from. This way it is possible to search + for the whole configuration tree, when starting from root node, or to do + a partial search when starting from a specified branch. The lookup should + be case insensitive. + + :param str name: The name of the directive to search for + :param bool exclude: If the search results should exclude the contents of + ParserNode objects that reside within conditional blocks and because + of current state are not enabled. + + :returns: A list of found DirectiveNode objects. + + """ + + @abc.abstractmethod + def find_comments(self, comment: str) -> list[CommentNode]: + """ + Find comments with value containing the search term. + + This method walks the child tree of ParserNodes under the instance it was + called from. This way it is possible to search for the whole configuration + tree, when starting from root node, or to do a partial search when starting + from a specified branch. The lookup should be case sensitive. + + :param str comment: The content of comment to search for + + :returns: A list of found CommentNode objects. + + """ + + @abc.abstractmethod + def delete_child(self, child: ParserNode) -> None: + """ + Remove a specified child node from the list of children of the called + BlockNode object. + + :param ParserNode child: Child ParserNode object to remove from the list + of children of the callee. + """ + + @abc.abstractmethod + def unsaved_files(self) -> list[str]: + """ + Returns a list of file paths that have been changed since the last save + (or the initial configuration parse). The intended use for this method + is to tell the Reverter which files need to be included in a checkpoint. + + This is typically called for the root of the ParserNode tree. + + :returns: list of file paths of files that have been changed but not yet + saved to disk. + """ + + @abc.abstractmethod + def parsed_paths(self) -> list[str]: + """ + Returns a list of file paths that have currently been parsed into the parser + tree. The returned list may include paths with wildcard characters, for + example: ['/etc/apache2/conf.d/*.load'] + + This is typically called on the root node of the ParserNode tree. + + :returns: list of file paths of files that have been parsed + """ diff --git a/certbot/src/certbot/_internal/plugins/apache/obj.py b/certbot/src/certbot/_internal/plugins/apache/obj.py new file mode 100644 index 00000000000..6c5a8f0a668 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/obj.py @@ -0,0 +1,262 @@ +"""Module contains classes used by the Apache Configurator.""" +import re +from typing import Any +from typing import Iterable +from typing import Optional +from typing import Union + +from certbot.plugins import common +from certbot._internal.plugins.apache.apacheparser import ApacheBlockNode +from certbot._internal.plugins.apache.augeasparser import AugeasBlockNode +from certbot._internal.plugins.apache.dualparser import DualBlockNode + + +class Addr(common.Addr): + """Represents an Apache address.""" + + def __eq__(self, other: Any) -> bool: + """This is defined as equivalent within Apache. + + ip_addr:* == ip_addr + + """ + if isinstance(other, self.__class__): + return ((self.tup == other.tup) or + (self.tup[0] == other.tup[0] and + self.is_wildcard() and other.is_wildcard())) + return False + + def __repr__(self) -> str: + return f"certbot._internal.plugins.apache.obj.Addr({repr(self.tup)})" + + def __hash__(self) -> int: # pylint: disable=useless-super-delegation + # Python 3 requires explicit overridden for __hash__ if __eq__ or + # __cmp__ is overridden. See https://bugs.python.org/issue2235 + return super().__hash__() + + def _addr_less_specific(self, addr: "Addr") -> bool: + """Returns if addr.get_addr() is more specific than self.get_addr().""" + # pylint: disable=protected-access + return addr._rank_specific_addr() > self._rank_specific_addr() + + def _rank_specific_addr(self) -> int: + """Returns numerical rank for get_addr() + + :returns: 2 - FQ, 1 - wildcard, 0 - _default_ + :rtype: int + + """ + if self.get_addr() == "_default_": + return 0 + elif self.get_addr() == "*": + return 1 + return 2 + + def conflicts(self, addr: "Addr") -> bool: + r"""Returns if address could conflict with correct function of self. + + Could addr take away service provided by self within Apache? + + .. note::IP Address is more important than wildcard. + Connection from 127.0.0.1:80 with choices of *:80 and 127.0.0.1:* + chooses 127.0.0.1:\* + + .. todo:: Handle domain name addrs... + + Examples: + + ========================================= ===== + ``127.0.0.1:\*.conflicts(127.0.0.1:443)`` True + ``127.0.0.1:443.conflicts(127.0.0.1:\*)`` False + ``\*:443.conflicts(\*:80)`` False + ``_default_:443.conflicts(\*:443)`` True + ========================================= ===== + + """ + if self._addr_less_specific(addr): + return True + elif self.get_addr() == addr.get_addr(): + if self.is_wildcard() or self.get_port() == addr.get_port(): + return True + return False + + def is_wildcard(self) -> bool: + """Returns if address has a wildcard port.""" + return self.tup[1] == "*" or not self.tup[1] + + def get_sni_addr(self, port: str) -> common.Addr: + """Returns the least specific address that resolves on the port. + + Examples: + + - ``1.2.3.4:443`` -> ``1.2.3.4:`` + - ``1.2.3.4:*`` -> ``1.2.3.4:*`` + + :param str port: Desired port + + """ + if self.is_wildcard(): + return self + + return self.get_addr_obj(port) + + +class VirtualHost: + """Represents an Apache Virtualhost. + + :ivar str filep: file path of VH + :ivar str path: Augeas path to virtual host + :ivar set addrs: Virtual Host addresses (:class:`set` of + :class:`common.Addr`) + :ivar str name: ServerName of VHost + :ivar list aliases: Server aliases of vhost + (:class:`list` of :class:`str`) + + :ivar bool ssl: SSLEngine on in vhost + :ivar bool enabled: Virtual host is enabled + :ivar bool modmacro: VirtualHost is using mod_macro + :ivar VirtualHost ancestor: A non-SSL VirtualHost this is based on + + https://httpd.apache.org/docs/2.4/vhosts/details.html + + .. todo:: Any vhost that includes the magic _default_ wildcard is given the + same ServerName as the main server. + + """ + # ?: is used for not returning enclosed characters + strip_name: re.Pattern[str] = re.compile(r"^(?:.+://)?([^ :$]*)") + + def __init__(self, filepath: str, path: str, addrs: set["Addr"], ssl: bool, + enabled: bool, name: Optional[str] = None, aliases: Optional[set[str]] = None, + modmacro: bool = False, ancestor: Optional["VirtualHost"] = None, + node: Optional[Union[ApacheBlockNode, AugeasBlockNode, DualBlockNode]] = None + ) -> None: + + """Initialize a VH.""" + self.filep = filepath + self.path = path + self.addrs = addrs + self.name = name + self.aliases = aliases if aliases is not None else set() + self.ssl = ssl + self.enabled = enabled + self.modmacro = modmacro + self.ancestor = ancestor + self.node = node + + def get_names(self) -> set[str]: + """Return a set of all names.""" + all_names: set[str] = set() + all_names.update(self.aliases) + # Strip out any scheme:// and field from servername + if self.name is not None: + all_names.add(VirtualHost.strip_name.findall(self.name)[0]) + + return all_names + + def __str__(self) -> str: + return ( + f"File: {self.filep}\n" + f"Vhost path: {self.path}\n" + f"Addresses: {', '.join(str(addr) for addr in self.addrs)}\n" + f"Name: {self.name if self.name is not None else ''}\n" + f"Aliases: {', '.join(name for name in self.aliases)}\n" + f"TLS Enabled: {'Yes' if self.ssl else 'No'}\n" + f"Site Enabled: {'Yes' if self.enabled else 'No'}\n" + f"mod_macro Vhost: {'Yes' if self.modmacro else 'No'}" + ) + + def display_repr(self) -> str: + """Return a representation of VHost to be used in dialog""" + return ( + f"File: {self.filep}\n" + f"Addresses: {', '.join(str(addr) for addr in self.addrs)}\n" + f"Names: {', '.join(self.get_names())}\n" + f"HTTPS: {'Yes' if self.ssl else 'No'}\n" + ) + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return (self.filep == other.filep and self.path == other.path and + self.addrs == other.addrs and + self.get_names() == other.get_names() and + self.ssl == other.ssl and + self.enabled == other.enabled and + self.modmacro == other.modmacro) + + return False + + def __hash__(self) -> int: + return hash((self.filep, self.path, + tuple(self.addrs), tuple(self.get_names()), + self.ssl, self.enabled, self.modmacro)) + + def conflicts(self, addrs: Iterable[Addr]) -> bool: + """See if vhost conflicts with any of the addrs. + + This determines whether or not these addresses would/could overwrite + the vhost addresses. + + :param addrs: Iterable Addresses + :type addrs: Iterable :class:~obj.Addr + + :returns: If addresses conflicts with vhost + :rtype: bool + + """ + for pot_addr in addrs: + for addr in self.addrs: + if addr.conflicts(pot_addr): + return True + return False + + def same_server(self, vhost: "VirtualHost", generic: bool = False) -> bool: + """Determines if the vhost is the same 'server'. + + Used in redirection - indicates whether or not the two virtual hosts + serve on the exact same IP combinations, but different ports. + The generic flag indicates that that we're trying to match to a + default or generic vhost + + .. todo:: Handle _default_ + + """ + + if not generic: + if vhost.get_names() != self.get_names(): + return False + + # If equal and set is not empty... assume same server + if self.name is not None or self.aliases: + return True + # If we're looking for a generic vhost, + # don't return one with a ServerName + elif self.name: + return False + + # Both sets of names are empty. + + # Make conservative educated guess... this is very restrictive + # Consider adding more safety checks. + if len(vhost.addrs) != len(self.addrs): + return False + + # already_found acts to keep everything very conservative. + # Don't allow multiple ip:ports in same set. + already_found: set[str] = set() + + for addr in vhost.addrs: + for local_addr in self.addrs: + if (local_addr.get_addr() == addr.get_addr() and + local_addr != addr and + local_addr.get_addr() not in already_found): + + # This intends to make sure we aren't double counting... + # e.g. 127.0.0.1:* - We require same number of addrs + # currently + already_found.add(local_addr.get_addr()) + break + else: + return False + + return True diff --git a/certbot/src/certbot/_internal/plugins/apache/override_alpine.py b/certbot/src/certbot/_internal/plugins/apache/override_alpine.py new file mode 100644 index 00000000000..5a184a9daf7 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/override_alpine.py @@ -0,0 +1,19 @@ +""" Distribution specific override class for Alpine Linux """ +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache.configurator import OsOptions + + +class AlpineConfigurator(configurator.ApacheConfigurator): + """Alpine Linux specific ApacheConfigurator override class""" + + OS_DEFAULTS = OsOptions( + server_root="/etc/apache2", + vhost_root="/etc/apache2/conf.d", + vhost_files="*.conf", + logs_root="/var/log/apache2", + ctl="apachectl", + version_cmd=['apachectl', '-v'], + restart_cmd=['apachectl', 'graceful'], + conftest_cmd=['apachectl', 'configtest'], + challenge_location="/etc/apache2/conf.d", + ) diff --git a/certbot/src/certbot/_internal/plugins/apache/override_arch.py b/certbot/src/certbot/_internal/plugins/apache/override_arch.py new file mode 100644 index 00000000000..73f169ff51e --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/override_arch.py @@ -0,0 +1,19 @@ +""" Distribution specific override class for Arch Linux """ +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache.configurator import OsOptions + + +class ArchConfigurator(configurator.ApacheConfigurator): + """Arch Linux specific ApacheConfigurator override class""" + + OS_DEFAULTS = OsOptions( + server_root="/etc/httpd", + vhost_root="/etc/httpd/conf", + vhost_files="*.conf", + logs_root="/var/log/httpd", + ctl="apachectl", + version_cmd=['apachectl', '-v'], + restart_cmd=['apachectl', 'graceful'], + conftest_cmd=['apachectl', 'configtest'], + challenge_location="/etc/httpd/conf", + ) diff --git a/certbot/src/certbot/_internal/plugins/apache/override_centos.py b/certbot/src/certbot/_internal/plugins/apache/override_centos.py new file mode 100644 index 00000000000..e250bfcdfe3 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/override_centos.py @@ -0,0 +1,123 @@ +""" Distribution specific override class for CentOS family (RHEL, Fedora) """ +import logging +from typing import Any + +from certbot import errors +from certbot import util +from certbot._internal.plugins.apache import apache_util +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache import parser +from certbot._internal.plugins.apache.configurator import OsOptions + +logger = logging.getLogger(__name__) + + +class CentOSConfigurator(configurator.ApacheConfigurator): + """CentOS specific ApacheConfigurator override class""" + + OS_DEFAULTS = OsOptions( + server_root="/etc/httpd", + vhost_root="/etc/httpd/conf.d", + vhost_files="*.conf", + logs_root="/var/log/httpd", + ctl="apachectl", + apache_bin="httpd", + version_cmd=['apachectl', '-v'], + restart_cmd=['apachectl', 'graceful'], + restart_cmd_alt=['apachectl', 'restart'], + conftest_cmd=['apachectl', 'configtest'], + challenge_location="/etc/httpd/conf.d", + ) + + def config_test(self) -> None: + """ + Override config_test to mitigate configtest error in vanilla installation + of mod_ssl in Fedora. The error is caused by non-existent self-signed + certificates referenced by the configuration, that would be autogenerated + during the first (re)start of httpd. + """ + + os_info = util.get_os_info() + fedora = os_info[0].lower() == "fedora" + + try: + super().config_test() + except errors.MisconfigurationError: + if fedora: + self._try_restart_fedora() + else: + raise + + def _rhel9_or_newer(self) -> bool: + os_name, os_version = util.get_os_info() + rhel_derived = os_name in [ + "centos", "centos linux", + "cloudlinux", + "ol", "oracle", + "rhel", "redhatenterpriseserver", "red hat enterprise linux server", + "scientific", "scientific linux", + ] + # It is important that the loose version comparison below is not made + # if the OS is not RHEL derived. See + # https://github.com/certbot/certbot/issues/9481. + if not rhel_derived: + return False + at_least_v9 = util.parse_loose_version(os_version) >= util.parse_loose_version('9') + return at_least_v9 + + def _override_cmds(self) -> None: + super()._override_cmds() + + # As of RHEL 9, apachectl can't be passed flags like "-v" or "-t -D", so + # instead use options.bin (i.e. httpd) for version_cmd and the various + # get_X commands + if self._rhel9_or_newer(): + if not self.options.bin: + raise ValueError("OS option apache_bin must be set for CentOS") # pragma: no cover + + self.options.version_cmd[0] = self.options.bin + self.options.get_modules_cmd[0] = self.options.bin + self.options.get_includes_cmd[0] = self.options.bin + self.options.get_defines_cmd[0] = self.options.bin + + if not self.options.restart_cmd_alt: # pragma: no cover + raise ValueError("OS option restart_cmd_alt must be set for CentOS.") + self.options.restart_cmd_alt[0] = self.options.ctl + + def _try_restart_fedora(self) -> None: + """ + Tries to restart httpd using systemctl to generate the self signed key pair. + """ + + try: + util.run_script(['systemctl', 'restart', 'httpd']) + except errors.SubprocessError as err: + raise errors.MisconfigurationError(str(err)) + + # Finish with actual config check to see if systemctl restart helped + super().config_test() + + def get_parser(self) -> "CentOSParser": + """Initializes the ApacheParser""" + return CentOSParser( + self.options.server_root, self, self.options.vhost_root, self.version) + + +class CentOSParser(parser.ApacheParser): + """CentOS specific ApacheParser override class""" + def __init__(self, *args: Any, **kwargs: Any) -> None: + # CentOS specific configuration file for Apache + self.sysconfig_filep: str = "/etc/sysconfig/httpd" + super().__init__(*args, **kwargs) + + def update_runtime_variables(self) -> None: + """ Override for update_runtime_variables for custom parsing """ + # Opportunistic, works if SELinux not enforced + super().update_runtime_variables() + self.parse_sysconfig_var() + + def parse_sysconfig_var(self) -> None: + """ Parses Apache CLI options from CentOS configuration file """ + defines = apache_util.parse_define_file(self.sysconfig_filep, "OPTIONS") + for k, v in defines.items(): + self.variables[k] = v diff --git a/certbot/src/certbot/_internal/plugins/apache/override_darwin.py b/certbot/src/certbot/_internal/plugins/apache/override_darwin.py new file mode 100644 index 00000000000..cc2b65496f6 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/override_darwin.py @@ -0,0 +1,17 @@ +""" Distribution specific override class for macOS """ +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache.configurator import OsOptions + + +class DarwinConfigurator(configurator.ApacheConfigurator): + """macOS specific ApacheConfigurator override class""" + + OS_DEFAULTS = OsOptions( + vhost_root="/etc/apache2/other", + vhost_files="*.conf", + ctl="apachectl", + version_cmd=['apachectl', '-v'], + restart_cmd=['apachectl', 'graceful'], + conftest_cmd=['apachectl', 'configtest'], + challenge_location="/etc/apache2/other", + ) diff --git a/certbot/src/certbot/_internal/plugins/apache/override_debian.py b/certbot/src/certbot/_internal/plugins/apache/override_debian.py new file mode 100644 index 00000000000..417596d33f7 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/override_debian.py @@ -0,0 +1,129 @@ +""" Distribution specific override class for Debian family (Ubuntu/Debian) """ +import logging + +from certbot import errors +from certbot import util +from certbot.compat import filesystem +from certbot.compat import os +from certbot._internal.plugins.apache import apache_util +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache.configurator import OsOptions +from certbot._internal.plugins.apache.obj import VirtualHost + +logger = logging.getLogger(__name__) + + +class DebianConfigurator(configurator.ApacheConfigurator): + """Debian specific ApacheConfigurator override class""" + + OS_DEFAULTS = OsOptions( + enmod="a2enmod", + dismod="a2dismod", + handle_modules=True, + handle_sites=True, + ) + + def enable_site(self, vhost: VirtualHost) -> None: + """Enables an available site, Apache reload required. + + .. note:: Does not make sure that the site correctly works or that all + modules are enabled appropriately. + + :param vhost: vhost to enable + :type vhost: :class:`~certbot._internal.plugins.apache.obj.VirtualHost` + + :raises .errors.NotSupportedError: If filesystem layout is not + supported. + + """ + if vhost.enabled: + return None + + enabled_path = ("%s/sites-enabled/%s" % + (self.parser.root, + os.path.basename(vhost.filep))) + if not os.path.isdir(os.path.dirname(enabled_path)): + # For some reason, sites-enabled / sites-available do not exist + # Call the parent method + return super().enable_site(vhost) + self.reverter.register_file_creation(False, enabled_path) + try: + os.symlink(vhost.filep, enabled_path) + except OSError as err: + if os.path.islink(enabled_path) and filesystem.realpath( + enabled_path) == vhost.filep: + # Already in shape + vhost.enabled = True + return None + logger.error( + "Could not symlink %s to %s, got error: %s", enabled_path, + vhost.filep, err.strerror) + errstring = ("Encountered error while trying to enable a " + + "newly created VirtualHost located at {0} by " + + "linking to it from {1}") + raise errors.NotSupportedError(errstring.format(vhost.filep, + enabled_path)) + vhost.enabled = True + logger.info("Enabling available site: %s", vhost.filep) + self.save_notes += "Enabled site %s\n" % vhost.filep + return None + + def enable_mod(self, mod_name: str, temp: bool = False) -> None: + """Enables module in Apache. + + Both enables and reloads Apache so module is active. + + :param str mod_name: Name of the module to enable. (e.g. 'ssl') + :param bool temp: Whether or not this is a temporary action. + + :raises .errors.NotSupportedError: If the filesystem layout is not + supported. + :raises .errors.MisconfigurationError: If a2enmod or a2dismod cannot be + run. + + """ + avail_path = os.path.join(self.parser.root, "mods-available") + enabled_path = os.path.join(self.parser.root, "mods-enabled") + if not os.path.isdir(avail_path) or not os.path.isdir(enabled_path): + raise errors.NotSupportedError( + "Unsupported directory layout. You may try to enable mod %s " + "and try again." % mod_name) + + deps = apache_util.get_mod_deps(mod_name) + + # Enable all dependencies + for dep in deps: + if (dep + "_module") not in self.parser.modules: + self._enable_mod_debian(dep, temp) + self.parser.add_mod(dep) + note = "Enabled dependency of %s module - %s" % (mod_name, dep) + if not temp: + self.save_notes += note + os.linesep + logger.debug(note) + + # Enable actual module + self._enable_mod_debian(mod_name, temp) + self.parser.add_mod(mod_name) + + if not temp: + self.save_notes += "Enabled %s module in Apache\n" % mod_name + logger.info("Enabled Apache %s module", mod_name) + + # Modules can enable additional config files. Variables may be defined + # within these new configuration sections. + # Reload is not necessary as DUMP_RUN_CFG uses latest config. + self.parser.update_runtime_variables() + + def _enable_mod_debian(self, mod_name: str, temp: bool) -> None: + """Assumes mods-available, mods-enabled layout.""" + # Generate reversal command. + # Try to be safe here... check that we can probably reverse before + # applying enmod command + if (self.options.dismod is None or self.options.enmod is None + or not util.exe_exists(self.options.dismod)): + raise errors.MisconfigurationError( + "Unable to find a2dismod, please make sure a2enmod and " + "a2dismod are configured correctly for certbot.") + + self.reverter.register_undo_command(temp, [self.options.dismod, "-f", mod_name]) + util.run_script([self.options.enmod, mod_name]) diff --git a/certbot/src/certbot/_internal/plugins/apache/override_fedora.py b/certbot/src/certbot/_internal/plugins/apache/override_fedora.py new file mode 100644 index 00000000000..67b3056ccc2 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/override_fedora.py @@ -0,0 +1,88 @@ +""" Distribution specific override class for Fedora 29+ """ +from typing import Any + +from certbot import errors +from certbot import util +from certbot._internal.plugins.apache import apache_util +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache import parser +from certbot._internal.plugins.apache.configurator import OsOptions + + +class FedoraConfigurator(configurator.ApacheConfigurator): + """Fedora 29+ specific ApacheConfigurator override class""" + + OS_DEFAULTS = OsOptions( + server_root="/etc/httpd", + vhost_root="/etc/httpd/conf.d", + vhost_files="*.conf", + logs_root="/var/log/httpd", + ctl="httpd", + version_cmd=['httpd', '-v'], + restart_cmd=['apachectl', 'graceful'], + restart_cmd_alt=['apachectl', 'restart'], + conftest_cmd=['apachectl', 'configtest'], + challenge_location="/etc/httpd/conf.d", + ) + + def config_test(self) -> None: + """ + Override config_test to mitigate configtest error in vanilla installation + of mod_ssl in Fedora. The error is caused by non-existent self-signed + certificates referenced by the configuration, that would be autogenerated + during the first (re)start of httpd. + """ + try: + super().config_test() + except errors.MisconfigurationError: + self._try_restart_fedora() + + def get_parser(self) -> "FedoraParser": + """Initializes the ApacheParser""" + return FedoraParser( + self.options.server_root, self, self.options.vhost_root, self.version) + + def _try_restart_fedora(self) -> None: + """ + Tries to restart httpd using systemctl to generate the self signed key pair. + """ + try: + util.run_script(['systemctl', 'restart', 'httpd']) + except errors.SubprocessError as err: + raise errors.MisconfigurationError(str(err)) + + # Finish with actual config check to see if systemctl restart helped + super().config_test() + + def _prepare_options(self) -> None: + """ + Override the options dictionary initialization to keep using apachectl + instead of httpd and so take advantages of this new bash script in newer versions + of Fedora to restart httpd. + """ + super()._prepare_options() + self.options.restart_cmd[0] = 'apachectl' + if not self.options.restart_cmd_alt: # pragma: no cover + raise ValueError("OS option restart_cmd_alt must be set for Fedora.") + self.options.restart_cmd_alt[0] = 'apachectl' + self.options.conftest_cmd[0] = 'apachectl' + + +class FedoraParser(parser.ApacheParser): + """Fedora 29+ specific ApacheParser override class""" + def __init__(self, *args: Any, **kwargs: Any) -> None: + # Fedora 29+ specific configuration file for Apache + self.sysconfig_filep = "/etc/sysconfig/httpd" + super().__init__(*args, **kwargs) + + def update_runtime_variables(self) -> None: + """ Override for update_runtime_variables for custom parsing """ + # Opportunistic, works if SELinux not enforced + super().update_runtime_variables() + self._parse_sysconfig_var() + + def _parse_sysconfig_var(self) -> None: + """ Parses Apache CLI options from Fedora configuration file """ + defines = apache_util.parse_define_file(self.sysconfig_filep, "OPTIONS") + for k, v in defines.items(): + self.variables[k] = v diff --git a/certbot/src/certbot/_internal/plugins/apache/override_gentoo.py b/certbot/src/certbot/_internal/plugins/apache/override_gentoo.py new file mode 100644 index 00000000000..2955afaf7b4 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/override_gentoo.py @@ -0,0 +1,61 @@ +""" Distribution specific override class for Gentoo Linux """ +from typing import Any + +from certbot._internal.plugins.apache import apache_util +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache import parser +from certbot._internal.plugins.apache.configurator import OsOptions + + +class GentooConfigurator(configurator.ApacheConfigurator): + """Gentoo specific ApacheConfigurator override class""" + + OS_DEFAULTS = OsOptions( + server_root="/etc/apache2", + vhost_root="/etc/apache2/vhosts.d", + vhost_files="*.conf", + restart_cmd_alt=['apache2ctl', 'restart'], + challenge_location="/etc/apache2/vhosts.d", + ) + + def _prepare_options(self) -> None: + """ + Override the options dictionary initialization in order to support + alternative restart cmd used in Gentoo. + """ + super()._prepare_options() + if not self.options.restart_cmd_alt: # pragma: no cover + raise ValueError("OS option restart_cmd_alt must be set for Gentoo.") + self.options.restart_cmd_alt[0] = self.options.ctl + + def get_parser(self) -> "GentooParser": + """Initializes the ApacheParser""" + return GentooParser( + self.options.server_root, self, self.options.vhost_root, self.version) + + +class GentooParser(parser.ApacheParser): + """Gentoo specific ApacheParser override class""" + def __init__(self, *args: Any, **kwargs: Any) -> None: + # Gentoo specific configuration file for Apache2 + self.apacheconfig_filep = "/etc/conf.d/apache2" + super().__init__(*args, **kwargs) + + def update_runtime_variables(self) -> None: + """ Override for update_runtime_variables for custom parsing """ + self.parse_sysconfig_var() + self.update_modules() + + def parse_sysconfig_var(self) -> None: + """ Parses Apache CLI options from Gentoo configuration file """ + defines = apache_util.parse_define_file(self.apacheconfig_filep, + "APACHE2_OPTS") + for k, v in defines.items(): + self.variables[k] = v + + def update_modules(self) -> None: + """Get loaded modules from httpd process, and add them to DOM""" + mod_cmd = [self.configurator.options.ctl, "modules"] + matches = apache_util.parse_from_subprocess(mod_cmd, r"(.*)_module") + for mod in matches: + self.add_mod(mod.strip()) diff --git a/certbot/src/certbot/_internal/plugins/apache/override_suse.py b/certbot/src/certbot/_internal/plugins/apache/override_suse.py new file mode 100644 index 00000000000..1fa49514254 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/override_suse.py @@ -0,0 +1,19 @@ +""" Distribution specific override class for OpenSUSE """ +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache.configurator import OsOptions + + +class OpenSUSEConfigurator(configurator.ApacheConfigurator): + """OpenSUSE specific ApacheConfigurator override class""" + + OS_DEFAULTS = OsOptions( + vhost_root="/etc/apache2/vhosts.d", + vhost_files="*.conf", + ctl="apachectl", + version_cmd=['apachectl', '-v'], + restart_cmd=['apachectl', 'graceful'], + conftest_cmd=['apachectl', 'configtest'], + enmod="a2enmod", + dismod="a2dismod", + challenge_location="/etc/apache2/vhosts.d", + ) diff --git a/certbot/src/certbot/_internal/plugins/apache/override_void.py b/certbot/src/certbot/_internal/plugins/apache/override_void.py new file mode 100644 index 00000000000..0c49ee960d0 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/override_void.py @@ -0,0 +1,19 @@ +""" Distribution specific override class for Void Linux """ +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache.configurator import OsOptions + + +class VoidConfigurator(configurator.ApacheConfigurator): + """Void Linux specific ApacheConfigurator override class""" + + OS_DEFAULTS = OsOptions( + server_root="/etc/apache", + vhost_root="/etc/apache/extra", + vhost_files="*.conf", + logs_root="/var/log/httpd", + ctl="apachectl", + version_cmd=['apachectl', '-v'], + restart_cmd=['apachectl', 'graceful'], + conftest_cmd=['apachectl', 'configtest'], + challenge_location="/etc/apache/extra", + ) diff --git a/certbot/src/certbot/_internal/plugins/apache/parser.py b/certbot/src/certbot/_internal/plugins/apache/parser.py new file mode 100644 index 00000000000..e87f6467c99 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/parser.py @@ -0,0 +1,960 @@ +"""ApacheParser is a member object of the ApacheConfigurator class.""" +import copy +import fnmatch +import logging +import re +from typing import Collection +from typing import Iterable +from typing import Mapping +from typing import Optional +from typing import TYPE_CHECKING +from typing import Union + +from certbot import errors +from certbot.compat import os +from certbot._internal.plugins.apache import apache_util +from certbot._internal.plugins.apache import constants + +if TYPE_CHECKING: + from certbot._internal.plugins.apache.configurator import ApacheConfigurator # pragma: no cover + +try: + from augeas import Augeas +except ImportError: # pragma: no cover + Augeas = None + +logger = logging.getLogger(__name__) + + +class ApacheParser: + """Class handles the fine details of parsing the Apache Configuration. + + .. todo:: Make parsing general... remove sites-available etc... + + :ivar str root: Normalized absolute path to the server root + directory. Without trailing slash. + :ivar set modules: All module names that are currently enabled. + :ivar dict loc: Location to place directives, root - configuration origin, + default - user config file, name - NameVirtualHost, + + """ + arg_var_interpreter: re.Pattern[str] = re.compile(r"\$\{[^ \}]*}") + fnmatch_chars: set[str] = {"*", "?", "\\", "[", "]"} + + # pylint: disable=unused-argument + def __init__(self, root: str, configurator: "ApacheConfigurator", + vhostroot: str, version: tuple[int, ...] = (2, 4)) -> None: + # Note: Order is important here. + + # Needed for calling save() with reverter functionality that resides in + # AugeasConfigurator superclass of ApacheConfigurator. This resolves + # issues with aug.load() after adding new files / defines to parse tree + self.configurator = configurator + + # Initialize augeas + self.aug: Augeas = init_augeas() + + if not self.check_aug_version(): + raise errors.NotSupportedError( + "Apache plugin support requires libaugeas0 and augeas-lenses " + "version 1.2.0 or higher, please make sure you have you have " + "those installed.") + + self.modules: dict[str, Optional[str]] = {} + self.parser_paths: dict[str, list[str]] = {} + self.variables: dict[str, str] = {} + + # Find configuration root and make sure augeas can parse it. + self.root: str = os.path.abspath(root) + self.loc: dict[str, str] = {"root": self._find_config_root()} + self.parse_file(self.loc["root"]) + + # Look up variables from httpd and add to DOM if not already parsed + self.update_runtime_variables() + + # This problem has been fixed in Augeas 1.0 + self.standardize_excl() + + # Parse LoadModule directives from configuration files + self.parse_modules() + + # Set up rest of locations + self.loc.update(self._set_locations()) + + # list of the active include paths, before modifications + self.existing_paths = copy.deepcopy(self.parser_paths) + + # Must also attempt to parse additional virtual host root + if vhostroot: + self.parse_file(os.path.abspath(vhostroot) + "/" + + self.configurator.options.vhost_files) + + def check_parsing_errors(self, lens: str) -> None: + """Verify Augeas can parse all of the lens files. + + :param str lens: lens to check for errors + + :raises .errors.PluginError: If there has been an error in parsing with + the specified lens. + + """ + error_files = self.aug.match("/augeas//error") + + for path in error_files: + # Check to see if it was an error resulting from the use of + # the httpd lens + lens_path = self.aug.get(path + "/lens") + # As aug.get may return null + if lens_path and lens in lens_path: + msg = ( + "There has been an error in parsing the file {0} on line {1}: " + "{2}".format( + # Strip off /augeas/files and /error + path[13:len(path) - 6], + self.aug.get(path + "/line"), + self.aug.get(path + "/message"))) + raise errors.PluginError(msg) + + def check_aug_version(self) -> Union[bool, list[str]]: + """ Checks that we have recent enough version of libaugeas. + If augeas version is recent enough, it will support case insensitive + regexp matching""" + + self.aug.set("/test/path/testing/arg", "aRgUMeNT") + try: + matches: list[str] = self.aug.match( + "/test//*[self::arg=~regexp('argument', 'i')]") + except RuntimeError: + self.aug.remove("/test/path") + return False + self.aug.remove("/test/path") + return matches + + def unsaved_files(self) -> set[str]: + """Lists files that have modified Augeas DOM but the changes have not + been written to the filesystem yet, used by `self.save()` and + ApacheConfigurator to check the file state. + + :raises .errors.PluginError: If there was an error in Augeas, in + an attempt to save the configuration, or an error creating a + checkpoint + + :returns: `set` of unsaved files + """ + save_state = self.aug.get("/augeas/save") + self.aug.set("/augeas/save", "noop") + # Existing Errors + ex_errs = self.aug.match("/augeas//error") + try: + # This is a noop save + self.aug.save() + except (OSError, RuntimeError): + self._log_save_errors(ex_errs) + # Erase Save Notes + self.configurator.save_notes = "" + raise errors.PluginError( + "Error saving files, check logs for more info.") + + # Return the original save method + self.aug.set("/augeas/save", save_state) + + # Retrieve list of modified files + # Note: Noop saves can cause the file to be listed twice, I used a + # set to remove this possibility. This is a known augeas 0.10 error. + save_paths = self.aug.match("/augeas/events/saved") + + save_files = set() + if save_paths: + for path in save_paths: + save_files.add(self.aug.get(path)[6:]) + return save_files + + def ensure_augeas_state(self) -> None: + """Makes sure that all Augeas dom changes are written to files to avoid + loss of configuration directives when doing additional augeas parsing, + causing a possible augeas.load() resulting dom reset + """ + + if self.unsaved_files(): + self.configurator.save_notes += "(autosave)" + self.configurator.save() + + def save(self, save_files: Iterable[str]) -> None: + """Saves all changes to the configuration files. + + save() is called from ApacheConfigurator to handle the parser specific + tasks of saving. + + :param list save_files: list of strings of file paths that we need to save. + + """ + self.configurator.save_notes = "" + + ex_errs = self.aug.match("/augeas//error") + try: + self.aug.save() + except OSError: + self._log_save_errors(ex_errs) + raise + + # Force reload if files were modified + # This is needed to recalculate augeas directive span + if save_files: + for sf in save_files: + self.aug.remove("/files/"+sf) + self.aug.load() + + def _log_save_errors(self, ex_errs: Iterable[str]) -> None: + """Log errors due to bad Augeas save. + + :param list ex_errs: Existing errors before save + + """ + # Check for the root of save problems + new_errs = [e for e in self.aug.match("/augeas//error") if e not in ex_errs] + + for err in new_errs: + logger.debug( + "Error %s saving %s: %s", self.aug.get(err), err[13:len(err) - 6], + self.aug.get(f"{err}/message")) + logger.error( + "Unable to save files: %s.%s", ", ".join(err[13:len(err) - 6] for err in new_errs), + f" Save Notes: {self.configurator.save_notes}" if self.configurator.save_notes else "") + + def add_include(self, main_config: str, inc_path: str) -> None: + """Add Include for a new configuration file if one does not exist + + :param str main_config: file path to main Apache config file + :param str inc_path: path of file to include + + """ + if not self.find_dir(case_i("Include"), inc_path): + logger.debug("Adding Include %s to %s", + inc_path, get_aug_path(main_config)) + self.add_dir( + get_aug_path(main_config), + "Include", inc_path) + + # Add new path to parser paths + new_dir = os.path.dirname(inc_path) + new_file = os.path.basename(inc_path) + self.existing_paths.setdefault(new_dir, []).append(new_file) + + def add_mod(self, mod_name: str) -> None: + """Shortcut for updating parser modules.""" + if mod_name + "_module" not in self.modules: + self.modules[mod_name + "_module"] = None + if "mod_" + mod_name + ".c" not in self.modules: + self.modules["mod_" + mod_name + ".c"] = None + + def reset_modules(self) -> None: + """Reset the loaded modules list. This is called from cleanup to clear + temporarily loaded modules.""" + self.modules = {} + self.update_modules() + self.parse_modules() + + def parse_modules(self) -> None: + """Iterates on the configuration until no new modules are loaded. + + ..todo:: This should be attempted to be done with a binary to avoid + the iteration issue. Else... parse and enable mods at same time. + + """ + mods: dict[str, str] = {} + matches = self.find_dir("LoadModule") + iterator = iter(matches) + # Make sure prev_size != cur_size for do: while: iteration + prev_size = -1 + + while len(mods) != prev_size: + prev_size = len(mods) + + for match_name, match_filename in zip( + iterator, iterator): + mod_name = self.get_arg(match_name) + mod_filename = self.get_arg(match_filename) + if mod_name and mod_filename: + mods[mod_name] = mod_filename + mods[os.path.basename(mod_filename)[:-2] + "c"] = mod_filename + else: + logger.debug("Could not read LoadModule directive from Augeas path: %s", + match_name[6:]) + self.modules.update(mods) + + def update_runtime_variables(self) -> None: + """Update Includes, Defines and Includes from httpd config dump data""" + + self.update_defines() + self.update_includes() + self.update_modules() + + def update_defines(self) -> None: + """Updates the dictionary of known variables in the configuration""" + self.variables = apache_util.parse_defines(self.configurator.options.get_defines_cmd) + + def update_includes(self) -> None: + """Get includes from httpd process, and add them to DOM if needed""" + + # Find_dir iterates over configuration for Include and IncludeOptional + # directives to make sure we see the full include tree present in the + # configuration files + _ = self.find_dir("Include") + + matches = apache_util.parse_includes(self.configurator.options.get_includes_cmd) + if matches: + for i in matches: + if not self.parsed_in_current(i): + self.parse_file(i) + + def update_modules(self) -> None: + """Get loaded modules from httpd process, and add them to DOM""" + + matches = apache_util.parse_modules(self.configurator.options.get_modules_cmd) + for mod in matches: + self.add_mod(mod.strip()) + + def filter_args_num(self, matches: str, args: int) -> list[str]: + """Filter out directives with specific number of arguments. + + This function makes the assumption that all related arguments are given + in order. Thus /files/apache/directive[5]/arg[2] must come immediately + after /files/apache/directive[5]/arg[1]. Runs in 1 linear pass. + + :param str matches: Matches of all directives with arg nodes + :param int args: Number of args you would like to filter + + :returns: List of directives that contain # of arguments. + (arg is stripped off) + + """ + filtered: list[str] = [] + if args == 1: + for i, match in enumerate(matches): + if match.endswith("/arg"): + filtered.append(matches[i][:-4]) + else: + for i, match in enumerate(matches): + if match.endswith("/arg[%d]" % args): + # Make sure we don't cause an IndexError (end of list) + # Check to make sure arg + 1 doesn't exist + if (i == (len(matches) - 1) or + not matches[i + 1].endswith("/arg[%d]" % + (args + 1))): + filtered.append(matches[i][:-len("/arg[%d]" % args)]) + + return filtered + + def add_dir_to_ifmodssl(self, aug_conf_path: str, directive: str, args: list[str]) -> None: + """Adds directive and value to IfMod ssl block. + + Adds given directive and value along configuration path within + an IfMod mod_ssl.c block. If the IfMod block does not exist in + the file, it is created. + + :param str aug_conf_path: Desired Augeas config path to add directive + :param str directive: Directive you would like to add, e.g. Listen + :param args: Values of the directive; list of str (eg. ["443"]) + :type args: list + + """ + # TODO: Add error checking code... does the path given even exist? + # Does it throw exceptions? + if_mod_path = self.get_ifmod(aug_conf_path, "mod_ssl.c") + # IfModule can have only one valid argument, so append after + self.aug.insert(if_mod_path + "arg", "directive", False) + nvh_path = if_mod_path + "directive[1]" + self.aug.set(nvh_path, directive) + if len(args) == 1: + self.aug.set(nvh_path + "/arg", args[0]) + else: + for i, arg in enumerate(args): + self.aug.set("%s/arg[%d]" % (nvh_path, i + 1), arg) + + def get_ifmod(self, aug_conf_path: str, mod: str) -> str: + """Returns the path to and creates one if it doesn't exist. + + :param str aug_conf_path: Augeas configuration path + :param str mod: module ie. mod_ssl.c + :param bool beginning: If the IfModule should be created to the beginning + of augeas path DOM tree. + + :returns: Augeas path of the requested IfModule directive that pre-existed + or was created during the process. The path may be dynamic, + i.e. .../IfModule[last()] + :rtype: str + + """ + if_mods: list[str] = self.aug.match(("%s/IfModule/*[self::arg='%s']" % + (aug_conf_path, mod))) + if not if_mods: + return self.create_ifmod(aug_conf_path, mod) + + # Strip off "arg" at end of first ifmod path + return if_mods[0].rpartition("arg")[0] + + def create_ifmod(self, aug_conf_path: str, mod: str) -> str: + """Creates a new and returns its path. + + :param str aug_conf_path: Augeas configuration path + :param str mod: module ie. mod_ssl.c + + :returns: Augeas path of the newly created IfModule directive. + The path may be dynamic, i.e. .../IfModule[last()] + :rtype: str + + """ + c_path = "{}/IfModule[last() + 1]".format(aug_conf_path) + c_path_arg = "{}/IfModule[last()]/arg".format(aug_conf_path) + self.aug.set(c_path, "") + retpath = "{}/IfModule[last()]/".format(aug_conf_path) + self.aug.set(c_path_arg, mod) + return retpath + + def add_dir( + self, aug_conf_path: Optional[str], directive: Optional[str], args: Union[list[str], str] + ) -> None: + """Appends directive to the end of the file given by aug_conf_path. + + .. note:: Not added to AugeasConfigurator because it may depend + on the lens + + :param str aug_conf_path: Augeas configuration path to add directive + :param str directive: Directive to add + :param args: Value of the directive. ie. Listen 443, 443 is arg + :type args: list or str + + """ + aug_conf_path = aug_conf_path if aug_conf_path else "" + self.aug.set(aug_conf_path + "/directive[last() + 1]", directive) + if isinstance(args, list): + for i, value in enumerate(args, 1): + self.aug.set( + "%s/directive[last()]/arg[%d]" % (aug_conf_path, i), value) + else: + self.aug.set(aug_conf_path + "/directive[last()]/arg", args) + + def add_dir_beginning(self, aug_conf_path: Optional[str], dirname: str, + args: Union[list[str], str]) -> None: + """Adds the directive to the beginning of defined aug_conf_path. + + :param str aug_conf_path: Augeas configuration path to add directive + :param str dirname: Directive to add + :param args: Value of the directive. ie. Listen 443, 443 is arg + :type args: list or str + """ + aug_conf_path = aug_conf_path if aug_conf_path else "" + first_dir = aug_conf_path + "/directive[1]" + if self.aug.get(first_dir): + self.aug.insert(first_dir, "directive", True) + else: + self.aug.set(first_dir, "directive") + + self.aug.set(first_dir, dirname) + if isinstance(args, list): + for i, value in enumerate(args, 1): + self.aug.set(first_dir + "/arg[%d]" % (i), value) + else: + self.aug.set(first_dir + "/arg", args) + + def add_comment(self, aug_conf_path: str, comment: str) -> None: + """Adds the comment to the augeas path + + :param str aug_conf_path: Augeas configuration path to add directive + :param str comment: Comment content + + """ + self.aug.set(aug_conf_path + "/#comment[last() + 1]", comment) + + def find_comments(self, arg: str, start: Optional[str] = None) -> list[str]: + """Finds a comment with specified content from the provided DOM path + + :param str arg: Comment content to search + :param str start: Beginning Augeas path to begin looking + + :returns: List of augeas paths containing the comment content + :rtype: list + + """ + if not start: + start = get_aug_path(self.root) + + comments = self.aug.match("%s//*[label() = '#comment']" % start) + + results = [] + for comment in comments: + c_content = self.aug.get(comment) + if c_content and arg in c_content: + results.append(comment) + return results + + def find_dir(self, directive: str, arg: Optional[str] = None, + start: Optional[str] = None, exclude: bool = True) -> list[str]: + """Finds directive in the configuration. + + Recursively searches through config files to find directives + Directives should be in the form of a case insensitive regex currently + + .. todo:: arg should probably be a list + .. todo:: arg search currently only supports direct matching. It does + not handle the case of variables or quoted arguments. This should + be adapted to use a generic search for the directive and then do a + case-insensitive self.get_arg filter + + Note: Augeas is inherently case sensitive while Apache is case + insensitive. Augeas 1.0 allows case insensitive regexes like + regexp(/Listen/, "i"), however the version currently supported + by Ubuntu 0.10 does not. Thus I have included my own case insensitive + transformation by calling case_i() on everything to maintain + compatibility. + + :param str directive: Directive to look for + :param arg: Specific value directive must have, None if all should + be considered + :type arg: str or None + + :param str start: Beginning Augeas path to begin looking + :param bool exclude: Whether or not to exclude directives based on + variables and enabled modules + + :rtype list + + """ + # Cannot place member variable in the definition of the function so... + if not start: + start = get_aug_path(self.loc["root"]) + + # No regexp code + # if arg is None: + # matches = self.aug.match(start + + # "//*[self::directive='" + directive + "']/arg") + # else: + # matches = self.aug.match(start + + # "//*[self::directive='" + directive + + # "']/* [self::arg='" + arg + "']") + + # includes = self.aug.match(start + + # "//* [self::directive='Include']/* [label()='arg']") + + regex = "(%s)|(%s)|(%s)" % (case_i(directive), + case_i("Include"), + case_i("IncludeOptional")) + matches = self.aug.match( + "%s//*[self::directive=~regexp('%s')]" % (start, regex)) + + if exclude: + matches = self.exclude_dirs(matches) + + if arg is None: + arg_suffix = "/arg" + else: + arg_suffix = "/*[self::arg=~regexp('%s')]" % case_i(arg) + + ordered_matches: list[str] = [] + + # TODO: Wildcards should be included in alphabetical order + # https://httpd.apache.org/docs/2.4/mod/core.html#include + for match in matches: + dir_ = self.aug.get(match).lower() + if dir_ in ("include", "includeoptional"): + ordered_matches.extend(self.find_dir( + directive, arg, + self._get_include_path(self.get_arg(match + "/arg")), + exclude)) + # This additionally allows Include + if dir_ == directive.lower(): + ordered_matches.extend(self.aug.match(match + arg_suffix)) + + return ordered_matches + + def get_arg(self, match: str) -> Optional[str]: + """Uses augeas.get to get argument value and interprets result. + + This also converts all variables and parameters appropriately. + + """ + value: str = self.aug.get(match) + + # No need to strip quotes for variables, as apache2ctl already does + # this, but we do need to strip quotes for all normal arguments. + + # Note: normal argument may be a quoted variable + # e.g. strip now, not later + if not value: + return None + + value = value.strip("'\"") + + variables = ApacheParser.arg_var_interpreter.findall(value) + + for var in variables: + # Strip off ${ and } + try: + value = value.replace(var, self.variables[var[2:-1]]) + except KeyError: + raise errors.PluginError("Error Parsing variable: %s" % var) + + return value + + def get_root_augpath(self) -> str: + """ + Returns the Augeas path of root configuration. + """ + return get_aug_path(self.loc["root"]) + + def exclude_dirs(self, matches: Iterable[str]) -> list[str]: + """Exclude directives that are not loaded into the configuration.""" + filters = [("ifmodule", self.modules.keys()), ("ifdefine", self.variables)] + + valid_matches = [] + + for match in matches: + for filter_ in filters: + if not self._pass_filter(match, filter_): + break + else: + valid_matches.append(match) + return valid_matches + + def _pass_filter(self, match: str, filter_: tuple[str, Collection[str]]) -> bool: + """Determine if directive passes a filter. + + :param str match: Augeas path + :param list filter: list of tuples of form + [("lowercase if directive", set of relevant parameters)] + + """ + match_l = match.lower() + last_match_idx = match_l.find(filter_[0]) + + while last_match_idx != -1: + # Check args + end_of_if = match_l.find("/", last_match_idx) + # This should be aug.get (vars are not used e.g. parser.aug_get) + expression = self.aug.get(match[:end_of_if] + "/arg") + + if expression.startswith("!"): + # Strip off "!" + if expression[1:] in filter_[1]: + return False + else: + if expression not in filter_[1]: + return False + + last_match_idx = match_l.find(filter_[0], end_of_if) + + return True + + def standard_path_from_server_root(self, arg: str) -> str: + """Ensure paths are consistent and absolute + + :param str arg: Argument of directive + + :returns: Standardized argument path + :rtype: str + """ + # Remove beginning and ending quotes + arg = arg.strip("'\"") + + # Standardize the include argument based on server root + if not arg.startswith("/"): + # Normpath will condense ../ + arg = os.path.normpath(os.path.join(self.root, arg)) + else: + arg = os.path.normpath(arg) + return arg + + def _get_include_path(self, arg: Optional[str]) -> Optional[str]: + """Converts an Apache Include directive into Augeas path. + + Converts an Apache Include directive argument into an Augeas + searchable path + + .. todo:: convert to use os.path.join() + + :param str arg: Argument of Include directive + + :returns: Augeas path string + :rtype: str + + """ + # Check to make sure only expected characters are used <- maybe remove + # validChars = re.compile("[a-zA-Z0-9.*?_-/]*") + # matchObj = validChars.match(arg) + # if matchObj.group() != arg: + # logger.error("Error: Invalid regexp characters in %s", arg) + # return [] + if arg is None: + return None # pragma: no cover + arg = self.standard_path_from_server_root(arg) + + # Attempts to add a transform to the file if one does not already exist + if os.path.isdir(arg): + self.parse_file(os.path.join(arg, "*")) + else: + self.parse_file(arg) + + # Argument represents an fnmatch regular expression, convert it + # Split up the path and convert each into an Augeas accepted regex + # then reassemble + split_arg = arg.split("/") + for idx, split in enumerate(split_arg): + if any(char in ApacheParser.fnmatch_chars for char in split): + # Turn it into an augeas regex + # TODO: Can this instead be an augeas glob instead of regex + split_arg[idx] = ("* [label()=~regexp('%s')]" % + self.fnmatch_to_re(split)) + # Reassemble the argument + # Note: This also normalizes the argument /serverroot/ -> /serverroot + arg = "/".join(split_arg) + + return get_aug_path(arg) + + def fnmatch_to_re(self, clean_fn_match: str) -> str: + """Method converts Apache's basic fnmatch to regular expression. + + Assumption - Configs are assumed to be well-formed and only writable by + privileged users. + + https://apr.apache.org/docs/apr/2.0/apr__fnmatch_8h_source.html + + :param str clean_fn_match: Apache style filename match, like globs + + :returns: regex suitable for augeas + :rtype: str + + """ + # Since Python 3.6, it returns a different pattern like (?s:.*\.load)\Z + return fnmatch.translate(clean_fn_match)[4:-3] # pragma: no cover + + def parse_file(self, filepath: str) -> None: + """Parse file with Augeas + + Checks to see if file_path is parsed by Augeas + If filepath isn't parsed, the file is added and Augeas is reloaded + + :param str filepath: Apache config file path + + """ + use_new, remove_old = self._check_path_actions(filepath) + # Ensure that we have the latest Augeas DOM state on disk before + # calling aug.load() which reloads the state from disk + self.ensure_augeas_state() + # Test if augeas included file for Httpd.lens + # Note: This works for augeas globs, ie. *.conf + if use_new: + inc_test = self.aug.match( + "/augeas/load/Httpd['%s' =~ glob(incl)]" % filepath) + if not inc_test: + # Load up files + # This doesn't seem to work on TravisCI + # self.aug.add_transform("Httpd.lns", [filepath]) + if remove_old: + self._remove_httpd_transform(filepath) + self._add_httpd_transform(filepath) + self.aug.load() + + def parsed_in_current(self, filep: Optional[str]) -> bool: + """Checks if the file path is parsed by current Augeas parser config + ie. returns True if the file is found on a path that's found in live + Augeas configuration. + + :param str filep: Path to match + + :returns: True if file is parsed in existing configuration tree + :rtype: bool + """ + if not filep: + return False # pragma: no cover + return self._parsed_by_parser_paths(filep, self.parser_paths) + + def parsed_in_original(self, filep: Optional[str]) -> bool: + """Checks if the file path is parsed by existing Apache config. + ie. returns True if the file is found on a path that matches Include or + IncludeOptional statement in the Apache configuration. + + :param str filep: Path to match + + :returns: True if file is parsed in existing configuration tree + :rtype: bool + """ + if not filep: + return False # pragma: no cover + return self._parsed_by_parser_paths(filep, self.existing_paths) + + def _parsed_by_parser_paths(self, filep: str, paths: Mapping[str, list[str]]) -> bool: + """Helper function that searches through provided paths and returns + True if file path is found in the set""" + for directory in paths: + for filename in paths[directory]: + if fnmatch.fnmatch(filep, os.path.join(directory, filename)): + return True + return False + + def _check_path_actions(self, filepath: str) -> tuple[bool, bool]: + """Determine actions to take with a new augeas path + + This helper function will return a tuple that defines + if we should try to append the new filepath to augeas + parser paths, and / or remove the old one with more + narrow matching. + + :param str filepath: filepath to check the actions for + + """ + + try: + new_file_match = os.path.basename(filepath) + existing_matches = self.parser_paths[os.path.dirname(filepath)] + if "*" in existing_matches: + use_new = False + else: + use_new = True + remove_old = new_file_match == "*" + except KeyError: + use_new = True + remove_old = False + return use_new, remove_old + + def _remove_httpd_transform(self, filepath: str) -> None: + """Remove path from Augeas transform + + :param str filepath: filepath to remove + """ + + remove_basenames = self.parser_paths[os.path.dirname(filepath)] + remove_dirname = os.path.dirname(filepath) + for name in remove_basenames: + remove_path = remove_dirname + "/" + name + remove_inc = self.aug.match( + "/augeas/load/Httpd/incl [. ='%s']" % remove_path) + self.aug.remove(remove_inc[0]) + self.parser_paths.pop(remove_dirname) + + def _add_httpd_transform(self, incl: str) -> None: + """Add a transform to Augeas. + + This function will correctly add a transform to augeas + The existing augeas.add_transform in python doesn't seem to work for + Travis CI as it loads in libaugeas.so.0.10.0 + + :param str incl: filepath to include for transform + + """ + last_include: str = self.aug.match("/augeas/load/Httpd/incl [last()]") + if last_include: + # Insert a new node immediately after the last incl + self.aug.insert(last_include[0], "incl", False) + self.aug.set("/augeas/load/Httpd/incl[last()]", incl) + # On first use... must load lens and add file to incl + else: + # Augeas uses base 1 indexing... insert at beginning... + self.aug.set("/augeas/load/Httpd/lens", "Httpd.lns") + self.aug.set("/augeas/load/Httpd/incl", incl) + # Add included path to paths dictionary + try: + self.parser_paths[os.path.dirname(incl)].append( + os.path.basename(incl)) + except KeyError: + self.parser_paths[os.path.dirname(incl)] = [ + os.path.basename(incl)] + + def standardize_excl(self) -> None: + """Standardize the excl arguments for the Httpd lens in Augeas. + + Note: Hack! + Standardize the excl arguments for the Httpd lens in Augeas + Servers sometimes give incorrect defaults + Note: This problem should be fixed in Augeas 1.0. Unfortunately, + Augeas 0.10 appears to be the most popular version currently. + + """ + # attempt to protect against augeas error in 0.10.0 - ubuntu + # *.augsave -> /*.augsave upon augeas.load() + # Try to avoid bad httpd files + # There has to be a better way... but after a day and a half of testing + # I had no luck + # This is a hack... work around... submit to augeas if still not fixed + + excl = ["*.augnew", "*.augsave", "*.dpkg-dist", "*.dpkg-bak", + "*.dpkg-new", "*.dpkg-old", "*.rpmsave", "*.rpmnew", + "*~", + self.root + "/*.augsave", + self.root + "/*~", + self.root + "/*/*augsave", + self.root + "/*/*~", + self.root + "/*/*/*.augsave", + self.root + "/*/*/*~"] + + for i, excluded in enumerate(excl, 1): + self.aug.set("/augeas/load/Httpd/excl[%d]" % i, excluded) + + self.aug.load() + + def _set_locations(self) -> dict[str, str]: + """Set default location for directives. + + Locations are given as file_paths + .. todo:: Make sure that files are included + + """ + default: str = self.loc["root"] + + temp: str = os.path.join(self.root, "ports.conf") + if os.path.isfile(temp): + listen = temp + name = temp + else: + listen = default + name = default + + return {"default": default, "listen": listen, "name": name} + + def _find_config_root(self) -> str: + """Find the Apache Configuration Root file.""" + location = ["apache2.conf", "httpd.conf", "conf/httpd.conf"] + for name in location: + if os.path.isfile(os.path.join(self.root, name)): + return os.path.join(self.root, name) + raise errors.NoInstallationError("Could not find configuration root") + + +def case_i(string: str) -> str: + """Returns case insensitive regex. + + Returns a sloppy, but necessary version of a case insensitive regex. + Any string should be able to be submitted and the string is + escaped and then made case insensitive. + May be replaced by a more proper /i once augeas 1.0 is widely + supported. + + :param str string: string to make case i regex + + """ + return "".join("[" + c.upper() + c.lower() + "]" + if c.isalpha() else c for c in re.escape(string)) + + +def get_aug_path(file_path: str) -> str: + """Return augeas path for full filepath. + + :param str file_path: Full filepath + + """ + return "/files%s" % file_path + + +def init_augeas() -> Augeas: + """ Initialize the actual Augeas instance """ + + if not Augeas: # pragma: no cover + raise errors.NoInstallationError("Problem in Augeas installation") + + return Augeas( + # specify a directory to load our preferred lens from + loadpath=constants.AUGEAS_LENS_DIR, + # Do not save backup (we do it ourselves), do not load + # anything by default + flags=(Augeas.NONE | + Augeas.NO_MODL_AUTOLOAD | + Augeas.ENABLE_SPAN)) diff --git a/certbot/src/certbot/_internal/plugins/apache/parsernode_util.py b/certbot/src/certbot/_internal/plugins/apache/parsernode_util.py new file mode 100644 index 00000000000..5d475580a33 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/parsernode_util.py @@ -0,0 +1,136 @@ +"""ParserNode utils""" +from typing import Any +from typing import Iterable +from typing import Optional + +from certbot._internal.plugins.apache.interfaces import ParserNode + + +def validate_kwargs(kwargs: dict[str, Any], required_names: Iterable[str]) -> dict[str, Any]: + """ + Ensures that the kwargs dict has all the expected values. This function modifies + the kwargs dictionary, and hence the returned dictionary should be used instead + in the caller function instead of the original kwargs. + + :param dict kwargs: Dictionary of keyword arguments to validate. + :param list required_names: List of required parameter names. + """ + + validated_kwargs: dict[str, Any] = {} + for name in required_names: + try: + validated_kwargs[name] = kwargs.pop(name) + except KeyError: + raise TypeError("Required keyword argument: {} undefined.".format(name)) + + # Raise exception if unknown key word arguments are found. + if kwargs: + unknown = ", ".join(kwargs.keys()) + raise TypeError("Unknown keyword argument(s): {}".format(unknown)) + return validated_kwargs + + +def parsernode_kwargs(kwargs: dict[str, Any] + ) -> tuple[Optional[ParserNode], bool, Optional[str], dict[str, Any]]: + """ + Validates keyword arguments for ParserNode. This function modifies the kwargs + dictionary, and hence the returned dictionary should be used instead in the + caller function instead of the original kwargs. + + If metadata is provided, the otherwise required argument "filepath" may be + omitted if the implementation is able to extract its value from the metadata. + This usecase is handled within this function. Filepath defaults to None. + + :param dict kwargs: Keyword argument dictionary to validate. + + :returns: Tuple of validated and prepared arguments. + """ + + # As many values of ParserNode instances can be derived from the metadata, + # (ancestor being a common exception here) make sure we permit it here as well. + if "metadata" in kwargs: + # Filepath can be derived from the metadata in Augeas implementation. + # Default is None, as in this case the responsibility of populating this + # variable lies on the implementation. + kwargs.setdefault("filepath", None) + + kwargs.setdefault("dirty", False) + kwargs.setdefault("metadata", {}) + + kwargs = validate_kwargs(kwargs, ["ancestor", "dirty", "filepath", "metadata"]) + return kwargs["ancestor"], kwargs["dirty"], kwargs["filepath"], kwargs["metadata"] + + +def commentnode_kwargs(kwargs: dict[str, Any]) -> tuple[Optional[str], dict[str, str]]: + """ + Validates keyword arguments for CommentNode and sets the default values for + optional kwargs. This function modifies the kwargs dictionary, and hence the + returned dictionary should be used instead in the caller function instead of + the original kwargs. + + If metadata is provided, the otherwise required argument "comment" may be + omitted if the implementation is able to extract its value from the metadata. + This usecase is handled within this function. + + :param dict kwargs: Keyword argument dictionary to validate. + + :returns: Tuple of validated and prepared arguments and ParserNode kwargs. + """ + + # As many values of ParserNode instances can be derived from the metadata, + # (ancestor being a common exception here) make sure we permit it here as well. + if "metadata" in kwargs: + kwargs.setdefault("comment", None) + # Filepath can be derived from the metadata in Augeas implementation. + # Default is None, as in this case the responsibility of populating this + # variable lies on the implementation. + kwargs.setdefault("filepath", None) + + kwargs.setdefault("dirty", False) + kwargs.setdefault("metadata", {}) + + kwargs = validate_kwargs(kwargs, ["ancestor", "dirty", "filepath", "comment", + "metadata"]) + + comment = kwargs.pop("comment") + return comment, kwargs + + +def directivenode_kwargs(kwargs: dict[str, Any] + ) -> tuple[Optional[str], tuple[str, ...], bool, dict[str, Any]]: + """ + Validates keyword arguments for DirectiveNode and BlockNode and sets the + default values for optional kwargs. This function modifies the kwargs + dictionary, and hence the returned dictionary should be used instead in the + caller function instead of the original kwargs. + + If metadata is provided, the otherwise required argument "name" may be + omitted if the implementation is able to extract its value from the metadata. + This usecase is handled within this function. + + :param dict kwargs: Keyword argument dictionary to validate. + + :returns: Tuple of validated and prepared arguments and ParserNode kwargs. + """ + + # As many values of ParserNode instances can be derived from the metadata, + # (ancestor being a common exception here) make sure we permit it here as well. + if "metadata" in kwargs: + kwargs.setdefault("name", None) + # Filepath can be derived from the metadata in Augeas implementation. + # Default is None, as in this case the responsibility of populating this + # variable lies on the implementation. + kwargs.setdefault("filepath", None) + + kwargs.setdefault("dirty", False) + kwargs.setdefault("enabled", True) + kwargs.setdefault("parameters", ()) + kwargs.setdefault("metadata", {}) + + kwargs = validate_kwargs(kwargs, ["ancestor", "dirty", "filepath", "name", + "parameters", "enabled", "metadata"]) + + name = kwargs.pop("name") + parameters = kwargs.pop("parameters") + enabled = kwargs.pop("enabled") + return name, parameters, enabled, kwargs diff --git a/certbot/src/certbot/_internal/plugins/apache/tls_configs/current-options-ssl-apache.conf b/certbot/src/certbot/_internal/plugins/apache/tls_configs/current-options-ssl-apache.conf new file mode 100644 index 00000000000..82482b858bc --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/tls_configs/current-options-ssl-apache.conf @@ -0,0 +1,16 @@ +# This file contains important security parameters. If you modify this file +# manually, Certbot will be unable to automatically provide future security +# updates. Instead, Certbot will print and log an error message with a path to +# the up-to-date file that you will need to refer to when manually updating +# this file. Contents are based on https://ssl-config.mozilla.org + +SSLEngine on + +# Intermediate configuration, tweak to your needs +SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 +SSLOpenSSLConfCmd Curves X25519:prime256v1:secp384r1 +SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305 +SSLHonorCipherOrder off +SSLSessionTickets off + +SSLOptions +StrictRequire diff --git a/certbot/src/certbot/_internal/plugins/apache/tls_configs/old-options-ssl-apache.conf b/certbot/src/certbot/_internal/plugins/apache/tls_configs/old-options-ssl-apache.conf new file mode 100644 index 00000000000..b0418496a88 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/apache/tls_configs/old-options-ssl-apache.conf @@ -0,0 +1,18 @@ +# This file contains important security parameters. If you modify this file +# manually, Certbot will be unable to automatically provide future security +# updates. Instead, Certbot will print and log an error message with a path to +# the up-to-date file that you will need to refer to when manually updating +# this file. Contents are based on https://ssl-config.mozilla.org +# +# This file is installed when apache < 2.4.11 or compiled against openssl < 1.0.2l. Since these +# are deprecated, it is no longer receiving updates. To get the latest configuration +# version, update apache. + +SSLEngine on + +# Intermediate configuration, tweak to your needs +SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 +SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384 +SSLHonorCipherOrder off + +SSLOptions +StrictRequire diff --git a/certbot/src/certbot/_internal/plugins/disco.py b/certbot/src/certbot/_internal/plugins/disco.py new file mode 100644 index 00000000000..80d8391690b --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/disco.py @@ -0,0 +1,285 @@ +"""Utilities for plugins discovery and selection.""" +import logging +import sys +from typing import Callable +from typing import cast +from typing import Iterable +from typing import Iterator +from typing import Mapping +from typing import Optional +from typing import Union + +from certbot import configuration +from certbot import errors +from certbot import interfaces +from certbot._internal import constants +from certbot.compat import os +from certbot.errors import Error + +if sys.version_info >= (3, 10): # pragma: no cover + import importlib.metadata as importlib_metadata +else: + import importlib_metadata + +logger = logging.getLogger(__name__) + + +PLUGIN_INTERFACES = [interfaces.Authenticator, interfaces.Installer, interfaces.Plugin] +"""Interfaces that should be listed in `certbot plugins` output""" + + +class PluginEntryPoint: + """Plugin entry point.""" + + # this object is mutable, don't allow it to be hashed! + __hash__ = None # type: ignore + + def __init__(self, entry_point: importlib_metadata.EntryPoint) -> None: + self.name = self.entry_point_to_plugin_name(entry_point) + self.plugin_cls: type[interfaces.Plugin] = entry_point.load() + self.entry_point = entry_point + self.warning_message: Optional[str] = None + self._initialized: Optional[interfaces.Plugin] = None + self._prepared: Optional[Union[bool, Error]] = None + + def check_name(self, name: Optional[str]) -> bool: + """Check if the name refers to this plugin.""" + if name == self.name: + return True + return False + + @classmethod + def entry_point_to_plugin_name(cls, entry_point: importlib_metadata.EntryPoint) -> str: + """Unique plugin name for an ``entry_point``""" + return entry_point.name + + @property + def description(self) -> str: + """Description of the plugin.""" + return self.plugin_cls.description + + @property + def description_with_name(self) -> str: + """Description with name. Handy for UI.""" + return "{0} ({1})".format(self.description, self.name) + + @property + def long_description(self) -> str: + """Long description of the plugin.""" + return getattr(self.plugin_cls, "long_description", self.description) + + @property + def hidden(self) -> bool: + """Should this plugin be hidden from UI?""" + return getattr(self.plugin_cls, "hidden", False) + + def ifaces(self, *ifaces_groups: Iterable[type]) -> bool: + """Does plugin implement specified interface groups?""" + return not ifaces_groups or any( + all(issubclass(self.plugin_cls, iface) + for iface in ifaces) + for ifaces in ifaces_groups) + + @property + def initialized(self) -> bool: + """Has the plugin been initialized already?""" + return self._initialized is not None + + def init(self, config: Optional[configuration.NamespaceConfig] = None) -> interfaces.Plugin: + """Memoized plugin initialization.""" + if not self._initialized: + # For plugins implementing ABCs Plugin, Authenticator or Installer, the following + # line will raise an exception if some implementations of abstract methods are missing. + self._initialized = self.plugin_cls(config, self.name) + return self._initialized + + @property + def prepared(self) -> bool: + """Has the plugin been prepared already?""" + if not self.initialized: + logger.debug(".prepared called on uninitialized %r", self) + return self._prepared is not None + + def prepare(self) -> Union[bool, Error]: + """Memoized plugin preparation.""" + if self._initialized is None: + raise ValueError("Plugin is not initialized.") + if self._prepared is None: + try: + self._initialized.prepare() + except errors.MisconfigurationError as error: + logger.debug("Misconfigured %r: %s", self, error, exc_info=True) + self._prepared = error + except errors.NoInstallationError as error: + logger.debug( + "No installation (%r): %s", self, error, exc_info=True) + self._prepared = error + except errors.PluginError as error: + logger.debug("Other error:(%r): %s", self, error, exc_info=True) + self._prepared = error + else: + self._prepared = True + # Mypy seems to fail to understand the actual type here, let's help it. + return cast(Union[bool, Error], self._prepared) + + @property + def misconfigured(self) -> bool: + """Is plugin misconfigured?""" + return isinstance(self._prepared, errors.MisconfigurationError) + + @property + def problem(self) -> Optional[Exception]: + """Return the Exception raised during plugin setup, or None if all is well""" + if isinstance(self._prepared, Exception): + return self._prepared + return None + + @property + def available(self) -> bool: + """Is plugin available, i.e. prepared or misconfigured?""" + return self._prepared is True or self.misconfigured + + def __repr__(self) -> str: + return "PluginEntryPoint#{0}".format(self.name) + + def __str__(self) -> str: + lines = [ + "* {0}".format(self.name), + "Description: {0}".format(self.plugin_cls.description), + "Interfaces: {0}".format(", ".join( + iface.__name__ for iface in PLUGIN_INTERFACES + if issubclass(self.plugin_cls, iface) + )), + "Entry point: {0}".format(self.entry_point), + ] + + if self.initialized: + lines.append("Initialized: {0}".format(self.init())) + if self.prepared: + lines.append("Prep: {0}".format(self.prepare())) + + return "\n".join(lines) + + +class PluginsRegistry(Mapping): + """Plugins registry.""" + + def __init__(self, plugins: Mapping[str, PluginEntryPoint]) -> None: + # plugins are sorted so the same order is used between runs. + # This prevents deadlock caused by plugins acquiring a lock + # and ensures at least one concurrent Certbot instance will run + # successfully. + self._plugins = dict(sorted(plugins.items())) + + @classmethod + def find_all(cls) -> 'PluginsRegistry': + """Find plugins using Python package entry points. + + See https://packaging.python.org/en/latest/specifications/entry-points/ for more info on + entry points. + + """ + plugins: dict[str, PluginEntryPoint] = {} + plugin_paths_string = os.getenv('CERTBOT_PLUGIN_PATH') + plugin_paths = plugin_paths_string.split(':') if plugin_paths_string else [] + # XXX should ensure this only happens once + sys.path.extend(plugin_paths) + entry_points = list(importlib_metadata.entry_points( # pylint: disable=unexpected-keyword-arg + group=constants.SETUPTOOLS_PLUGINS_ENTRY_POINT)) + old_entry_points = list(importlib_metadata.entry_points( # pylint: disable=unexpected-keyword-arg + group=constants.OLD_SETUPTOOLS_PLUGINS_ENTRY_POINT)) + for entry_point in entry_points + old_entry_points: + try: + cls._load_entry_point(entry_point, plugins) + except Exception as e: + raise errors.PluginError( + f"The '{entry_point.module}' plugin errored while loading: {e}. " + "You may need to remove or update this plugin. The Certbot log will " + "contain the full error details and this should be reported to the " + "plugin developer.") from e + return cls(plugins) + + @classmethod + def _load_entry_point(cls, entry_point: importlib_metadata.EntryPoint, + plugins: dict[str, PluginEntryPoint]) -> None: + plugin_ep = PluginEntryPoint(entry_point) + if plugin_ep.name in plugins: + other_ep = plugins[plugin_ep.name] + plugin1_dist = plugin_ep.entry_point.dist + plugin2_dist = other_ep.entry_point.dist + plugin1 = plugin1_dist.name.lower() if plugin1_dist else "unknown" + plugin2 = plugin2_dist.name.lower() if plugin2_dist else "unknown" + # pylint: disable=broad-exception-raised + raise Exception("Duplicate plugin name {0} from {1} and {2}.".format( + plugin_ep.name, plugin1, plugin2)) + if issubclass(plugin_ep.plugin_cls, interfaces.Plugin): + plugins[plugin_ep.name] = plugin_ep + else: # pragma: no cover + logger.warning( + "%r does not inherit from Plugin, skipping", plugin_ep) + + def __getitem__(self, name: str) -> PluginEntryPoint: + return self._plugins[name] + + def __iter__(self) -> Iterator[str]: + return iter(self._plugins) + + def __len__(self) -> int: + return len(self._plugins) + + def init(self, config: configuration.NamespaceConfig) -> list[interfaces.Plugin]: + """Initialize all plugins in the registry.""" + return [plugin_ep.init(config) for plugin_ep + in self._plugins.values()] + + def filter(self, pred: Callable[[PluginEntryPoint], bool]) -> "PluginsRegistry": + """Filter plugins based on predicate.""" + return type(self)({name: plugin_ep for name, plugin_ep + in self._plugins.items() if pred(plugin_ep)}) + + def visible(self) -> "PluginsRegistry": + """Filter plugins based on visibility.""" + return self.filter(lambda plugin_ep: not plugin_ep.hidden) + + def ifaces(self, *ifaces_groups: Iterable[type]) -> "PluginsRegistry": + """Filter plugins based on interfaces.""" + return self.filter(lambda p_ep: p_ep.ifaces(*ifaces_groups)) + + def prepare(self) -> list[Union[bool, Error]]: + """Prepare all plugins in the registry.""" + return [plugin_ep.prepare() for plugin_ep in self._plugins.values()] + + def available(self) -> "PluginsRegistry": + """Filter plugins based on availability.""" + return self.filter(lambda p_ep: p_ep.available) + # successfully prepared + misconfigured + + def find_init(self, plugin: interfaces.Plugin) -> Optional[PluginEntryPoint]: + """Find an initialized plugin. + + This is particularly useful for finding a name for the plugin:: + + # plugin is an instance providing Plugin, initialized + # somewhere else in the code + plugin_registry.find_init(plugin).name + + Returns ``None`` if ``plugin`` is not found in the registry. + + """ + # use list instead of set because PluginEntryPoint is not hashable + candidates = [plugin_ep for plugin_ep in self._plugins.values() + if plugin_ep.initialized and plugin_ep.init() is plugin] + assert len(candidates) <= 1 + if candidates: + return candidates[0] + return None + + def __repr__(self) -> str: + return "{0}({1})".format( + self.__class__.__name__, ','.join( + repr(p_ep) for p_ep in self._plugins.values())) + + def __str__(self) -> str: + if not self._plugins: + return "No plugins" + return "\n\n".join(str(p_ep) for p_ep in self._plugins.values()) diff --git a/certbot/src/certbot/_internal/plugins/manual.py b/certbot/src/certbot/_internal/plugins/manual.py new file mode 100644 index 00000000000..e73664e5541 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/manual.py @@ -0,0 +1,259 @@ +"""Manual authenticator plugin""" +import logging +from typing import Any +from typing import Callable +from typing import Iterable + +from acme import challenges, messages +from certbot import achallenges +from certbot import errors +from certbot import interfaces +from certbot import reverter +from certbot import util +from certbot._internal import hooks +from certbot._internal.cli import cli_constants +from certbot.compat import misc +from certbot.compat import os +from certbot.display import ops as display_ops +from certbot.display import util as display_util +from certbot.plugins import common + +logger = logging.getLogger(__name__) + + +class Authenticator(common.Plugin, interfaces.Authenticator): + """Manual authenticator + + This plugin allows the user to perform the validation + challenge(s) themselves. This either be done manually by the user or + through shell scripts provided to Certbot. + + """ + + description = 'Manual configuration or run your own shell scripts' + hidden = True + long_description = ( + 'Authenticate through manual configuration or custom shell scripts. ' + 'When using shell scripts, an authenticator script must be provided. ' + 'The environment variables available to this script depend on the ' + 'type of challenge. $CERTBOT_IDENTIFIER will always contain the domain or IP address ' + 'being authenticated. For HTTP-01 and DNS-01, $CERTBOT_VALIDATION ' + 'is the validation string, and $CERTBOT_TOKEN is the filename of the ' + 'resource requested when performing an HTTP-01 challenge. An additional ' + 'cleanup script can also be provided and can use the additional variable ' + '$CERTBOT_AUTH_OUTPUT which contains the stdout output from the auth script. ' + 'For both authenticator and cleanup script, on HTTP-01 and DNS-01 challenges, ' + '$CERTBOT_REMAINING_CHALLENGES will be equal to the number of challenges that ' + 'remain after the current one, and $CERTBOT_ALL_IDENTIFIERS contains a comma-separated ' + 'list of all identifiers that are challenged for the current certificate.') + # Include the full stop at the end of the FQDN in the instructions below for the null + # label of the DNS root, as stated in section 3.1 of RFC 1035. While not necessary + # for most day to day usage of hostnames, when adding FQDNs to a DNS zone editor, this + # full stop is often mandatory. Without a full stop, the entered name is often seen as + # relative to the DNS zone origin, which could lead to entries for, e.g.: + # _acme-challenge.example.com.example.com. For users unaware of this subtle detail, + # including the trailing full stop in the DNS instructions below might avert this issue. + _DNS_INSTRUCTIONS = """\ +Please deploy a DNS TXT record under the name: + +{domain}. + +with the following value: + +{validation} +""" + _DNS_VERIFY_INSTRUCTIONS = """ +Before continuing, verify the TXT record has been deployed. Depending on the DNS +provider, this may take some time, from a few seconds to multiple minutes. You can +check if it has finished deploying with aid of online tools, such as the Google +Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/{domain}. +Look for one or more bolded line(s) below the line ';ANSWER'. It should show the +value(s) you've just added. +""" + _HTTP_INSTRUCTIONS = """\ +Create a file containing just this data: + +{validation} + +And make it available on your web server at this URL: + +{uri} +""" + _SUBSEQUENT_CHALLENGE_INSTRUCTIONS = """ +(This must be set up in addition to the previous challenges; do not remove, +replace, or undo the previous challenge tasks yet.) +""" + _SUBSEQUENT_DNS_CHALLENGE_INSTRUCTIONS = """ +(This must be set up in addition to the previous challenges; do not remove, +replace, or undo the previous challenge tasks yet. Note that you might be +asked to create multiple distinct TXT records with the same name. This is +permitted by DNS standards.) +""" + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.reverter = reverter.Reverter(self.config) + self.reverter.recovery_routine() + self.env: dict[achallenges.AnnotatedChallenge, dict[str, str]] = {} + self.subsequent_dns_challenge = False + self.subsequent_any_challenge = False + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None]) -> None: + add('auth-hook', + help='Path or command to execute for the authentication script') + add('cleanup-hook', + help='Path or command to execute for the cleanup script') + + def prepare(self) -> None: # pylint: disable=missing-function-docstring + if self.config.noninteractive_mode and not self.conf('auth-hook'): + raise errors.PluginError( + 'An authentication script must be provided with --{0} when ' + 'using the manual plugin non-interactively.'.format( + self.option_name('auth-hook'))) + self._validate_hooks() + + def _validate_hooks(self) -> None: + if self.config.validate_hooks: + for name in ('auth-hook', 'cleanup-hook'): + hook = self.conf(name) + if hook is not None: + hook_prefix = self.option_name(name)[:-len('-hook')] + hooks.validate_hook(hook, hook_prefix) + + def more_info(self) -> str: # pylint: disable=missing-function-docstring + return ( + 'This plugin allows the user to customize setup for ' + 'validation challenges either through shell scripts provided by ' + 'the user or by performing the setup manually.') + + def auth_hint(self, failed_achalls: Iterable[achallenges.AnnotatedChallenge]) -> str: + def has_chall(cls: type[challenges.Challenge]) -> bool: + return any(isinstance(achall.chall, cls) for achall in failed_achalls) + + has_dns = has_chall(challenges.DNS01) + resource_names = { + challenges.DNS01: 'DNS TXT records', + challenges.HTTP01: 'challenge files', + } + resources = ' and '.join(sorted([v for k, v in resource_names.items() if has_chall(k)])) + + if self.conf('auth-hook'): + return ( + 'The Certificate Authority failed to verify the {resources} created by the ' + '--manual-auth-hook. Ensure that this hook is functioning correctly{dns_hint}. ' + 'Refer to "{certbot} --help manual" and the Certbot User Guide.' + .format( + certbot=cli_constants.cli_command, + resources=resources, + dns_hint=( + ' and that it waits a sufficient duration of time for DNS propagation' + ) if has_dns else '' + ) + ) + else: + return ( + 'The Certificate Authority failed to verify the manually created {resources}. ' + 'Ensure that you created these in the correct location{dns_hint}.' + .format( + resources=resources, + dns_hint=( + ', or try waiting longer for DNS propagation on the next attempt' + ) if has_dns else '' + ) + ) + + def get_chall_pref(self, identifier: str) -> Iterable[type[challenges.Challenge]]: + # pylint: disable=unused-argument,missing-function-docstring + return [challenges.HTTP01, challenges.DNS01] + + def perform(self, achalls: list[achallenges.AnnotatedChallenge] + ) -> list[challenges.ChallengeResponse]: # pylint: disable=missing-function-docstring + responses = [] + last_dns_achall = 0 + for i, achall in enumerate(achalls): + if isinstance(achall.chall, challenges.DNS01): + last_dns_achall = i + for i, achall in enumerate(achalls): + if self.conf('auth-hook'): + self._perform_achall_with_script(achall, achalls) + else: + self._perform_achall_manually(achall, i == last_dns_achall) + responses.append(achall.response(achall.account_key)) + return responses + + def _perform_achall_with_script(self, achall: achallenges.AnnotatedChallenge, + achalls: list[achallenges.AnnotatedChallenge]) -> None: + identifier_value = achall.identifier.value + env = { + "CERTBOT_DOMAIN": identifier_value, + "CERTBOT_IDENTIFIER": identifier_value, + "CERTBOT_VALIDATION": achall.validation(achall.account_key), + "CERTBOT_ALL_DOMAINS": ','.join(one_achall.identifier.value for one_achall in achalls), + "CERTBOT_ALL_IDENTIFIERS": + ','.join(one_achall.identifier.value for one_achall in achalls), + "CERTBOT_REMAINING_CHALLENGES": str(len(achalls) - achalls.index(achall) - 1), + } + if isinstance(achall.chall, challenges.HTTP01): + env['CERTBOT_TOKEN'] = achall.chall.encode('token') + else: + os.environ.pop('CERTBOT_TOKEN', None) + os.environ.update(env) + _, out = self._execute_hook('auth-hook', identifier_value) + env['CERTBOT_AUTH_OUTPUT'] = out.strip() + self.env[achall] = env + + def _perform_achall_manually(self, achall: achallenges.AnnotatedChallenge, + last_dns_achall: bool = False) -> None: + identifier_value = achall.identifier.value + validation = achall.validation(achall.account_key) + if isinstance(achall.chall, challenges.HTTP01): + msg = self._HTTP_INSTRUCTIONS.format( + achall=achall, encoded_token=achall.chall.encode('token'), + port=self.config.http01_port, + uri=achall.chall.uri(identifier_value), validation=validation) + else: + assert isinstance(achall.chall, challenges.DNS01) + assert achall.identifier.typ == messages.IDENTIFIER_FQDN + msg = self._DNS_INSTRUCTIONS.format( + domain=achall.validation_domain_name(identifier_value), + validation=validation) + if isinstance(achall.chall, challenges.DNS01): + if self.subsequent_dns_challenge: + # 2nd or later dns-01 challenge + msg += self._SUBSEQUENT_DNS_CHALLENGE_INSTRUCTIONS + elif self.subsequent_any_challenge: + # 1st dns-01 challenge, but 2nd or later *any* challenge, so + # instruct user not to remove any previous http-01 challenge + msg += self._SUBSEQUENT_CHALLENGE_INSTRUCTIONS + self.subsequent_dns_challenge = True + if last_dns_achall: + # last dns-01 challenge + msg += self._DNS_VERIFY_INSTRUCTIONS.format( + domain=achall.validation_domain_name(identifier_value)) + elif self.subsequent_any_challenge: + # 2nd or later challenge of another type + msg += self._SUBSEQUENT_CHALLENGE_INSTRUCTIONS + display_util.notification(msg, wrap=False, force_interactive=True) + self.subsequent_any_challenge = True + + def cleanup(self, achalls: Iterable[achallenges.AnnotatedChallenge]) -> None: # pylint: disable=missing-function-docstring + if self.conf('cleanup-hook'): + for achall in achalls: + env = self.env.pop(achall) + if 'CERTBOT_TOKEN' not in env: + os.environ.pop('CERTBOT_TOKEN', None) + os.environ.update(env) + self._execute_hook('cleanup-hook', achall.identifier.value) + self.reverter.recovery_routine() + + def _execute_hook(self, hook_name: str, identifier_value: str) -> tuple[str, str]: + returncode, err, out = misc.execute_command_status( + self.option_name(hook_name), self.conf(hook_name), + env=util.env_no_snap_for_external_calls() + ) + + display_ops.report_executed_command( + f"Hook '--manual-{hook_name}' for {identifier_value}", returncode, out, err) + + return err, out diff --git a/certbot/src/certbot/_internal/plugins/nginx/__init__.py b/certbot/src/certbot/_internal/plugins/nginx/__init__.py new file mode 100644 index 00000000000..71d79b0c27a --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/__init__.py @@ -0,0 +1 @@ +"""Certbot nginx plugin internal implementation.""" diff --git a/certbot/src/certbot/_internal/plugins/nginx/configurator.py b/certbot/src/certbot/_internal/plugins/nginx/configurator.py new file mode 100644 index 00000000000..a53610219b7 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/configurator.py @@ -0,0 +1,1332 @@ +# pylint: disable=too-many-lines +"""Nginx Configuration""" +import atexit +from contextlib import ExitStack +import logging +import importlib.resources +import re +import socket +import subprocess +import tempfile +import time +from typing import Any +from typing import Callable +from typing import Iterable +from typing import Mapping +from typing import Optional +from typing import Sequence +from typing import Union +from typing import cast + +from acme import challenges +from certbot import achallenges +from certbot import errors +from certbot import util +from certbot.compat import os +from certbot.display import util as display_util +from certbot.plugins import common +from certbot._internal.plugins.nginx import constants +from certbot._internal.plugins.nginx import display_ops +from certbot._internal.plugins.nginx import http_01 +from certbot._internal.plugins.nginx import nginxparser +from certbot._internal.plugins.nginx import obj +from certbot._internal.plugins.nginx import parser + +NAME_RANK = 0 +START_WILDCARD_RANK = 1 +END_WILDCARD_RANK = 2 +REGEX_RANK = 3 +NO_SSL_MODIFIER = 4 + + +logger = logging.getLogger(__name__) + + +class NginxConfigurator(common.Configurator): + """Nginx configurator. + + .. todo:: Add proper support for comments in the config. Currently, + config files modified by the configurator will lose all their comments. + + :ivar config: Configuration. + :type config: certbot.configuration.NamespaceConfig + + :ivar parser: Handles low level parsing + :type parser: :class:`~certbot._internal.plugins.nginx.parser` + + :ivar str save_notes: Human-readable config change notes + + :ivar reverter: saves and reverts checkpoints + :type reverter: :class:`certbot.reverter.Reverter` + + :ivar tup version: version of Nginx + + """ + + description = "Nginx Web Server plugin" + + DEFAULT_LISTEN_PORT = '80' + + # SSL directives that Certbot can add when installing a new certificate. + SSL_DIRECTIVES = ['ssl_certificate', 'ssl_certificate_key', 'ssl_dhparam'] + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None]) -> None: + default_server_root = _determine_default_server_root() + add("server-root", default=constants.CLI_DEFAULTS["server_root"], + help="Nginx server root directory. (default: %s)" % default_server_root) + add("ctl", default=constants.CLI_DEFAULTS["ctl"], help="Path to the " + "'nginx' binary, used for 'configtest' and retrieving nginx " + "version number.") + add("sleep-seconds", default=constants.CLI_DEFAULTS["sleep_seconds"], type=int, + help="Number of seconds to wait for nginx configuration changes " + "to apply when reloading.") + + @property + def nginx_conf(self) -> str: + """Nginx config file path.""" + return os.path.join(self.conf("server_root"), "nginx.conf") + + def __init__(self, *args: Any, **kwargs: Any) -> None: + """Initialize an Nginx Configurator. + + :param tup version: version of Nginx as a tuple (1, 4, 7) + (used mostly for unittesting) + + :param tup openssl_version: version of OpenSSL linked to Nginx as a tuple (1, 4, 7) + (used mostly for unittesting) + + """ + version = kwargs.pop("version", None) + openssl_version = kwargs.pop("openssl_version", None) + super().__init__(*args, **kwargs) + + # Files to save + self.save_notes = "" + + # For creating new vhosts if no names match + self.new_vhost: Optional[obj.VirtualHost] = None + + # List of vhosts configured per wildcard domain on this run. + # used by deploy_cert() and enhance() + self._wildcard_vhosts: dict[str, list[obj.VirtualHost]] = {} + self._wildcard_redirect_vhosts: dict[str, list[obj.VirtualHost]] = {} + + # Add number of outstanding challenges + self._chall_out = 0 + + # These will be set in the prepare function + self.version = version + self.openssl_version = openssl_version + self._enhance_func = {"redirect": self._enable_redirect, + "ensure-http-header": self._set_http_header, + "staple-ocsp": self._enable_ocsp_stapling} + + self.reverter.recovery_routine() + self.parser: parser.NginxParser + + @property + def mod_ssl_conf_src(self) -> str: + """Full absolute path to SSL configuration file source.""" + + # Why all this complexity? Well, we want to support Mozilla's intermediate + # recommendations. But TLS1.3 is only supported by newer versions of Nginx. + # And as for session tickets, our ideal is to turn them off across the board. + # But! Turning them off at all is only supported with new enough versions of + # Nginx. And older versions of OpenSSL have a bug that leads to browser errors + # given certain configurations. While we'd prefer to have forward secrecy, we'd + # rather fail open than error out. Unfortunately, Nginx can be compiled against + # many versions of OpenSSL. So we have to check both for the two different features, + # leading to four different combinations of options. + # For a complete history, check out https://github.com/certbot/certbot/issues/7322 + # + # Technically, nginx >= 1.23.2 has session tickets off by default, and therefore + # no longer needs it explicitly set. But since older versions than that are still + # around in the oldest non-deprecated rhel, debian, and ubuntu, we will keep explicitly + # setting it for now to reduce complexity. + + use_tls13 = self.version >= (1, 13, 0) + min_openssl_version = util.parse_loose_version('1.0.2l') + session_tix_off = self.version >= (1, 5, 9) and self.openssl_version and\ + util.parse_loose_version(self.openssl_version) >= min_openssl_version + + deprecated_conf = True + + if use_tls13: + if session_tix_off: + # current version + config_filename = "options-ssl-nginx.conf" + deprecated_conf = False + else: + config_filename = "options-ssl-nginx-tls13-session-tix-on.conf" + else: + if session_tix_off: + config_filename = "options-ssl-nginx-tls12-only.conf" + else: + config_filename = "options-ssl-nginx-old.conf" + + if deprecated_conf: + logger.warning('Certbot has detected that nginx version < 1.13.0 or compiled against ' + 'openssl < 1.0.2l. Since these are deprecated, the configuration file being ' + 'installed at %s will not receive future updates. To get the latest configuration ' + 'version, update nginx.', self.mod_ssl_conf) + + file_manager = ExitStack() + atexit.register(file_manager.close) + ref = (importlib.resources.files("certbot").joinpath("_internal") + .joinpath("plugins").joinpath("nginx").joinpath("tls_configs") + .joinpath(config_filename)) + + return str(file_manager.enter_context(importlib.resources.as_file(ref))) + + @property + def mod_ssl_conf(self) -> str: + """Full absolute path to SSL configuration file.""" + return os.path.join(self.config.config_dir, constants.MOD_SSL_CONF_DEST) + + @property + def updated_mod_ssl_conf_digest(self) -> str: + """Full absolute path to digest of updated SSL configuration file.""" + return os.path.join(self.config.config_dir, constants.UPDATED_MOD_SSL_CONF_DIGEST) + + def install_ssl_options_conf(self, options_ssl: str, options_ssl_digest: str) -> None: + """Copy Certbot's SSL options file into the system's config dir if required.""" + common.install_version_controlled_file( + options_ssl, options_ssl_digest, + self.mod_ssl_conf_src, constants.ALL_SSL_OPTIONS_HASHES) + + # This is called in determine_authenticator and determine_installer + def prepare(self) -> None: + """Prepare the authenticator/installer. + + :raises .errors.NoInstallationError: If Nginx ctl cannot be found + :raises .errors.MisconfigurationError: If Nginx is misconfigured + """ + # Verify Nginx is installed + if not util.exe_exists(self.conf('ctl')): + raise errors.NoInstallationError( + "Could not find a usable 'nginx' binary. Ensure nginx exists, " + "the binary is executable, and your PATH is set correctly.") + + # Make sure configuration is valid + self.config_test() + + self.parser = parser.NginxParser(self.conf('server-root')) + + # Set Version + if self.version is None: + self.version = self.get_version() + + if self.openssl_version is None: + self.openssl_version = self._get_openssl_version() + + self.install_ssl_options_conf(self.mod_ssl_conf, self.updated_mod_ssl_conf_digest) + + self.install_ssl_dhparams() + + # Prevent two Nginx plugins from modifying a config at once + try: + util.lock_dir_until_exit(self.conf('server-root')) + except (OSError, errors.LockError): + logger.debug('Encountered error:', exc_info=True) + raise errors.PluginError('Unable to lock {0}'.format(self.conf('server-root'))) + + # Entry point in main.py for installing cert + def deploy_cert(self, domain: str, cert_path: str, key_path: str, chain_path: str, + fullchain_path: str) -> None: + """Deploys certificate to specified virtual host. + + .. note:: Aborts if the vhost is missing ssl_certificate or + ssl_certificate_key. + + .. note:: This doesn't save the config files! + + :raises errors.PluginError: When unable to deploy certificate due to + a lack of directives or configuration + + """ + if not fullchain_path: + raise errors.PluginError( + "The nginx plugin currently requires --fullchain-path to " + "install a certificate.") + + vhosts = self.choose_or_make_vhosts(domain, key_path, fullchain_path) + for vhost in vhosts: + self._deploy_cert(vhost, cert_path, key_path, chain_path, fullchain_path) + display_util.notify("Successfully deployed certificate for {} to {}" + .format(domain, vhost.filep)) + + def _deploy_cert(self, vhost: obj.VirtualHost, _cert_path: str, key_path: str, + _chain_path: str, fullchain_path: str) -> None: + """ + Helper function for deploy_cert() that handles the actual deployment + this exists because we might want to do multiple deployments per + domain originally passed for deploy_cert(). This is especially true + with wildcard certificates + """ + cert_directives = [['\n ', 'ssl_certificate', ' ', fullchain_path], + ['\n ', 'ssl_certificate_key', ' ', key_path]] + + self.parser.update_or_add_server_directives(vhost, cert_directives) + logger.info("Deploying Certificate to VirtualHost %s", vhost.filep) + + self.save_notes += ("Changed vhost at %s with addresses of %s\n" % + (vhost.filep, + ", ".join(str(addr) for addr in vhost.addrs))) + self.save_notes += "\tssl_certificate %s\n" % fullchain_path + self.save_notes += "\tssl_certificate_key %s\n" % key_path + + def _choose_vhosts_wildcard(self, domain: str, prefer_ssl: bool, + no_ssl_filter_port: Optional[str] = None) -> list[obj.VirtualHost]: + """Prompts user to choose vhosts to install a wildcard certificate for""" + if prefer_ssl: + vhosts_cache = self._wildcard_vhosts + def preference_test(x: obj.VirtualHost) -> bool: + return x.ssl + else: + vhosts_cache = self._wildcard_redirect_vhosts + def preference_test(x: obj.VirtualHost) -> bool: + return not x.ssl + + # Caching! + if domain in vhosts_cache: + # Vhosts for a wildcard domain were already selected + return vhosts_cache[domain] + + # Get all vhosts whether or not they are covered by the wildcard domain + vhosts = self.parser.get_vhosts() + + # Go through the vhosts, making sure that we cover all the names + # present, but preferring the SSL or non-SSL vhosts + filtered_vhosts = {} + for vhost in vhosts: + # Ensure we're listening non-sslishly on no_ssl_filter_port + if no_ssl_filter_port is not None: + if not self._vhost_listening_on_port_no_ssl(vhost, no_ssl_filter_port): + continue + for name in vhost.names: + if preference_test(vhost): + # Prefer either SSL or non-SSL vhosts + filtered_vhosts[name] = vhost + elif name not in filtered_vhosts: + # Add if not in list previously + filtered_vhosts[name] = vhost + + # Only unique VHost objects + dialog_input = set(filtered_vhosts.values()) + + # Ask the user which of names to enable, expect list of names back + return_vhosts = display_ops.select_vhost_multiple(list(dialog_input)) + + for vhost in return_vhosts: + if domain not in vhosts_cache: + vhosts_cache[domain] = [] + vhosts_cache[domain].append(vhost) + + return return_vhosts + + ####################### + # Vhost parsing methods + ####################### + def _choose_vhost_single(self, target_name: str) -> list[obj.VirtualHost]: + matches = self._get_ranked_matches(target_name) + vhosts = [x for x in [self._select_best_name_match(matches)] if x is not None] + return vhosts + + def _choose_vhosts_common(self, target_name: str) -> list[obj.VirtualHost]: + if util.is_wildcard_domain(target_name): + # Ask user which VHosts to support. + return self._choose_vhosts_wildcard(target_name, prefer_ssl=True) + else: + return self._choose_vhost_single(target_name) + + def choose_vhosts(self, target_name: str) -> list[obj.VirtualHost]: + """Chooses SSL virtual hosts based on the given domain name. + + If no matching SSL vhosts are found, none are created and an empty list + is returned. + + :param str target_name: domain name + + :returns: ssl vhosts associated with name + :rtype: list of :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + + """ + return [vhost for vhost in self._choose_vhosts_common(target_name) if vhost.ssl] + + def choose_or_make_vhosts(self, target_name: str, key_path: str, + fullchain_path: str) -> list[obj.VirtualHost]: + """Chooses or creates SSL virtual hosts based on the given domain name. + + If no matching vhost is found, we attempt to create a new one from the + default vhost. If this fails, a MisconfigurationError is raised. + + .. note:: This makes the vhost SSL-enabled if it isn't already. Follows + Nginx's server block selection rules preferring blocks that are + already SSL. + + :param str target_name: domain name + :param str key_path: key to use when creating SSL vhosts + :param str fullchain_path: certificates to use when creating SSL vhosts + + :returns: ssl vhosts associated with name + :rtype: list of :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + + """ + vhosts = self._choose_vhosts_common(target_name) + if not vhosts: + # result will not be [None] because it errors on failure + vhosts = [self._vhost_from_duplicated_default(target_name, True, + str(self.config.https_port))] + for vhost in vhosts: + if not vhost.ssl: + self._make_server_ssl(vhost, key_path, fullchain_path) + + return vhosts + + def ipv6_info(self, host: str, port: str) -> tuple[bool, bool]: + """Returns tuple of booleans (ipv6_active, ipv6only_present) + ipv6_active is true if any server block listens ipv6 address in any port + + ipv6only_present is true if ipv6only=on option exists in any server + block ipv6 listen directive for the specified port. + + :param str host: Host to check ipv6only=on directive for + :param str port: Port to check ipv6only=on directive for + + :returns: Tuple containing information if IPv6 is enabled in the global + configuration, and existence of ipv6only directive for specified port + :rtype: tuple of type (bool, bool) + """ + vhosts = self.parser.get_vhosts() + ipv6_active = False + ipv6only_present = False + for vh in vhosts: + for addr in vh.addrs: + if addr.ipv6: + ipv6_active = True + if addr.ipv6only and addr.get_port() == port and addr.get_addr() == host: + ipv6only_present = True + return ipv6_active, ipv6only_present + + def _vhost_from_duplicated_default(self, domain: str, allow_port_mismatch: bool, + port: str) -> obj.VirtualHost: + """if allow_port_mismatch is False, only server blocks with matching ports will be + used as a default server block template. + """ + assert self.parser is not None # prepare should already have been called here + + if self.new_vhost is None: + default_vhost = self._get_default_vhost(domain, allow_port_mismatch, port) + self.new_vhost = self.parser.duplicate_vhost(default_vhost, + remove_singleton_listen_params=True) + self.new_vhost.names = set() + + self._add_server_name_to_vhost(self.new_vhost, domain) + return self.new_vhost + + def _add_server_name_to_vhost(self, vhost: obj.VirtualHost, domain: str) -> None: + vhost.names.add(domain) + name_block = [['\n ', 'server_name']] + for name in vhost.names: + name_block[0].append(' ') + name_block[0].append(name) + self.parser.update_or_add_server_directives(vhost, name_block) + + def _get_default_vhost(self, domain: str, allow_port_mismatch: bool, + port: str) -> obj.VirtualHost: + """Helper method for _vhost_from_duplicated_default; see argument documentation there""" + vhost_list = self.parser.get_vhosts() + # if one has default_server set, return that one + all_default_vhosts = [] + port_matching_vhosts = [] + for vhost in vhost_list: + for addr in vhost.addrs: + if addr.default: + all_default_vhosts.append(vhost) + if self._port_matches(port, addr.get_port()): + port_matching_vhosts.append(vhost) + break + + if len(port_matching_vhosts) == 1: + return port_matching_vhosts[0] + elif len(all_default_vhosts) == 1 and allow_port_mismatch: + return all_default_vhosts[0] + + # TODO: present a list of vhosts for user to choose from + + raise errors.MisconfigurationError("Could not automatically find a matching server " + f"block for {domain}. Set the `server_name` directive " + "to use the Nginx installer.") + + def _get_ranked_matches(self, target_name: str) -> list[dict[str, Any]]: + """Returns a ranked list of vhosts that match target_name. + The ranking gives preference to SSL vhosts. + + :param str target_name: The name to match + :returns: list of dicts containing the vhost, the matching name, and + the numerical rank + :rtype: list + + """ + vhost_list = self.parser.get_vhosts() + return self._rank_matches_by_name_and_ssl(vhost_list, target_name) + + def _select_best_name_match(self, + matches: Sequence[Mapping[str, Any]]) -> Optional[obj.VirtualHost]: + """Returns the best name match of a ranked list of vhosts. + + :param list matches: list of dicts containing the vhost, the matching name, + and the numerical rank + :returns: the most matching vhost + :rtype: :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + + """ + if not matches: + return None + elif matches[0]['rank'] in [START_WILDCARD_RANK, END_WILDCARD_RANK, + START_WILDCARD_RANK + NO_SSL_MODIFIER, END_WILDCARD_RANK + NO_SSL_MODIFIER]: + # Wildcard match - need to find the longest one + rank = matches[0]['rank'] + wildcards = [x for x in matches if x['rank'] == rank] + return cast(obj.VirtualHost, max(wildcards, key=lambda x: len(x['name']))['vhost']) + # Exact or regex match + return cast(obj.VirtualHost, matches[0]['vhost']) + + def _rank_matches_by_name(self, vhost_list: Iterable[obj.VirtualHost], + target_name: str) -> list[dict[str, Any]]: + """Returns a ranked list of vhosts from vhost_list that match target_name. + This method should always be followed by a call to _select_best_name_match. + + :param list vhost_list: list of vhosts to filter and rank + :param str target_name: The name to match + :returns: list of dicts containing the vhost, the matching name, and + the numerical rank + :rtype: list + + """ + # Nginx chooses a matching server name for a request with precedence: + # 1. exact name match + # 2. longest wildcard name starting with * + # 3. longest wildcard name ending with * + # 4. first matching regex in order of appearance in the file + matches = [] + for vhost in vhost_list: + name_type, name = parser.get_best_match(target_name, vhost.names) + if name_type == 'exact': + matches.append({'vhost': vhost, + 'name': name, + 'rank': NAME_RANK}) + elif name_type == 'wildcard_start': + matches.append({'vhost': vhost, + 'name': name, + 'rank': START_WILDCARD_RANK}) + elif name_type == 'wildcard_end': + matches.append({'vhost': vhost, + 'name': name, + 'rank': END_WILDCARD_RANK}) + elif name_type == 'regex': + matches.append({'vhost': vhost, + 'name': name, + 'rank': REGEX_RANK}) + return sorted(matches, key=lambda x: x['rank']) + + def _rank_matches_by_name_and_ssl(self, vhost_list: Iterable[obj.VirtualHost], + target_name: str) -> list[dict[str, Any]]: + """Returns a ranked list of vhosts from vhost_list that match target_name. + The ranking gives preference to SSLishness before name match level. + + :param list vhost_list: list of vhosts to filter and rank + :param str target_name: The name to match + :returns: list of dicts containing the vhost, the matching name, and + the numerical rank + :rtype: list + + """ + matches = self._rank_matches_by_name(vhost_list, target_name) + for match in matches: + if not match['vhost'].ssl: + match['rank'] += NO_SSL_MODIFIER + return sorted(matches, key=lambda x: x['rank']) + + def choose_redirect_vhosts(self, target_name: str, port: str) -> list[obj.VirtualHost]: + """Chooses a single virtual host for redirect enhancement. + + Chooses the vhost most closely matching target_name that is + listening to port without using ssl. + + .. todo:: This should maybe return list if no obvious answer + is presented. + + .. todo:: The special name "$hostname" corresponds to the machine's + hostname. Currently we just ignore this. + + :param str target_name: domain name + :param str port: port number + + :returns: vhosts associated with name + :rtype: list of :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + + """ + if util.is_wildcard_domain(target_name): + # Ask user which VHosts to enhance. + vhosts = self._choose_vhosts_wildcard(target_name, prefer_ssl=False, + no_ssl_filter_port=port) + else: + matches = self._get_redirect_ranked_matches(target_name, port) + vhosts = [x for x in [self._select_best_name_match(matches)]if x is not None] + return vhosts + + def choose_auth_vhosts(self, target_name: str) -> tuple[list[obj.VirtualHost], + list[obj.VirtualHost]]: + """Returns a list of HTTP and HTTPS vhosts with a server_name matching target_name. + + If no HTTP vhost exists, one will be cloned from the default vhost. If that fails, no HTTP + vhost will be returned. + + :param str target_name: non-wildcard domain name + + :returns: tuple of HTTP and HTTPS virtualhosts + :rtype: tuple of :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + + """ + vhosts = [m['vhost'] for m in self._get_ranked_matches(target_name) if m and 'vhost' in m] + http_vhosts = [vh for vh in vhosts if + self._vhost_listening(vh, str(self.config.http01_port), False)] + https_vhosts = [vh for vh in vhosts if + self._vhost_listening(vh, str(self.config.https_port), True)] + + # If no HTTP vhost matches, try create one from the default_server on http01_port. + if not http_vhosts: + try: + http_vhosts = [self._vhost_from_duplicated_default(target_name, False, + str(self.config.http01_port))] + except errors.MisconfigurationError: + http_vhosts = [] + + return http_vhosts, https_vhosts + + def _port_matches(self, test_port: str, matching_port: str) -> bool: + # test_port is a number, matching is a number or "" or None + if matching_port == "" or matching_port is None: + # if no port is specified, Nginx defaults to listening on port 80. + return test_port == self.DEFAULT_LISTEN_PORT + return test_port == matching_port + + def _vhost_listening(self, vhost: obj.VirtualHost, port: str, ssl: bool) -> bool: + """Tests whether a vhost has an address listening on a port with SSL enabled or disabled. + + :param `obj.VirtualHost` vhost: The vhost whose addresses will be tested + :param port str: The port number as a string that the address should be bound to + :param bool ssl: Whether SSL should be enabled or disabled on the address + + :returns: Whether the vhost has an address listening on the port and protocol. + :rtype: bool + + """ + assert self.parser is not None # prepare should already have been called here + + # if the 'ssl on' directive is present on the vhost, all its addresses have SSL enabled + all_addrs_are_ssl = self.parser.has_ssl_on_directive(vhost) + + # if we want ssl vhosts: either 'ssl on' or 'addr.ssl' should be enabled + # if we want plaintext vhosts: neither 'ssl on' nor 'addr.ssl' should be enabled + def _ssl_matches(addr: obj.Addr) -> bool: + return addr.ssl or all_addrs_are_ssl if ssl else \ + not addr.ssl and not all_addrs_are_ssl + + # if there are no listen directives at all, Nginx defaults to + # listening on port 80. + if not vhost.addrs: + return port == self.DEFAULT_LISTEN_PORT and ssl == all_addrs_are_ssl + + return any(self._port_matches(port, addr.get_port()) and _ssl_matches(addr) + for addr in vhost.addrs) + + def _vhost_listening_on_port_no_ssl(self, vhost: obj.VirtualHost, port: str) -> bool: + return self._vhost_listening(vhost, port, False) + + def _get_redirect_ranked_matches(self, target_name: str, port: str) -> list[dict[str, Any]]: + """Gets a ranked list of plaintextish port-listening vhosts matching target_name + + Filter all hosts for those listening on port without using ssl. + Rank by how well these match target_name. + + :param str target_name: The name to match + :param str port: port number as a string + :returns: list of dicts containing the vhost, the matching name, and + the numerical rank + :rtype: list + + """ + all_vhosts = self.parser.get_vhosts() + + def _vhost_matches(vhost: obj.VirtualHost, port: str) -> bool: + return self._vhost_listening_on_port_no_ssl(vhost, port) + + matching_vhosts = [vhost for vhost in all_vhosts if _vhost_matches(vhost, port)] + + return self._rank_matches_by_name(matching_vhosts, target_name) + + def get_all_names(self) -> set[str]: + """Returns all names found in the Nginx Configuration. + + :returns: All ServerNames, ServerAliases, and reverse DNS entries for + virtual host addresses + :rtype: set + + """ + all_names: set[str] = set() + + for vhost in self.parser.get_vhosts(): + try: + vhost.names.remove("$hostname") + vhost.names.add(socket.gethostname()) + except KeyError: + pass + + all_names.update(vhost.names) + + for addr in vhost.addrs: + host = addr.get_addr() + if common.hostname_regex.match(host): + # If it's a hostname, add it to the names. + all_names.add(host) + elif not common.private_ips_regex.match(host): + # If it isn't a private IP, do a reverse DNS lookup + try: + if addr.ipv6: + host = addr.get_ipv6_exploded() + socket.inet_pton(socket.AF_INET6, host) + else: + socket.inet_pton(socket.AF_INET, host) + all_names.add(socket.gethostbyaddr(host)[0]) + except (OSError, socket.herror, socket.timeout): + continue + + return util.get_filtered_names(all_names) + + def _make_server_ssl(self, vhost: obj.VirtualHost, key_path: str, + fullchain_path: str) -> None: + """Make a server SSL. + + Make a server SSL by adding new listen and SSL directives. + + :param vhost: The vhost to add SSL to. + :type vhost: :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + :param str key_path: key to use for SSL + :param str fullchain_path: certificates to use for SSL + + """ + https_port = self.config.https_port + http_port = self.config.http01_port + + # no addresses should have ssl turned on here + assert not vhost.ssl + + addrs_to_insert: list[obj.Addr] = [ + obj.Addr.fromstring(f'{addr.get_addr()}:{https_port} ssl') + for addr in vhost.addrs + if addr.get_port() == str(http_port) + ] + + # If the vhost was implicitly listening on the default Nginx port, + # have it continue to do so. + if not vhost.addrs: + listen_block = [['\n ', 'listen', ' ', self.DEFAULT_LISTEN_PORT]] + self.parser.add_server_directives(vhost, listen_block) + + if not addrs_to_insert: + # there are no existing addresses listening on 80 + if vhost.ipv6_enabled(): + addrs_to_insert += [obj.Addr.fromstring(f'[::]:{https_port} ssl')] + if vhost.ipv4_enabled(): + addrs_to_insert += [obj.Addr.fromstring(f'{https_port} ssl')] + + addr_blocks: list[list[str]] = [] + ipv6only_set_here: set[tuple[str, str]] = set() + for addr in addrs_to_insert: + host = addr.get_addr() + port = addr.get_port() + if addr.ipv6: + addr_block = ['\n ', + 'listen', + ' ', + f'{host}:{port}', + ' ', + 'ssl'] + ipv6only_exists = self.ipv6_info(host, port)[1] + if not ipv6only_exists and (host, port) not in ipv6only_set_here: + addr.ipv6only = True # bookkeeping in case we switch output implementation + ipv6only_set_here.add((host, port)) + addr_block.append(' ') + addr_block.append('ipv6only=on') + addr_blocks.append(addr_block) + else: + tuple_string = f'{host}:{port}' if host else f'{port}' + addr_block = ['\n ', + 'listen', + ' ', + tuple_string, + ' ', + 'ssl'] + addr_blocks.append(addr_block) + + ssl_block = ([ + *addr_blocks, + ['\n ', 'ssl_certificate', ' ', fullchain_path], + ['\n ', 'ssl_certificate_key', ' ', key_path], + ['\n ', 'include', ' ', self.mod_ssl_conf], + ['\n ', 'ssl_dhparam', ' ', self.ssl_dhparams], + ]) + + self.parser.add_server_directives( + vhost, ssl_block) + + ################################## + # enhancement methods (Installer) + ################################## + def supported_enhancements(self) -> list[str]: + """Returns currently supported enhancements.""" + return ['redirect', 'ensure-http-header', 'staple-ocsp'] + + def enhance(self, domain: str, enhancement: str, + options: Optional[Union[str, list[str]]] = None) -> None: + """Enhance configuration. + + :param str domain: domain to enhance + :param str enhancement: enhancement type defined in + :const:`~certbot.plugins.enhancements.ENHANCEMENTS` + :param options: options for the enhancement + See :const:`~certbot.plugins.enhancements.ENHANCEMENTS` + documentation for appropriate parameter. + + """ + try: + self._enhance_func[enhancement](domain, options) + except (KeyError, ValueError): + raise errors.PluginError( + "Unsupported enhancement: {0}".format(enhancement)) + + def _has_certbot_redirect(self, vhost: obj.VirtualHost, domain: str) -> bool: + test_redirect_block = _test_block_from_block(_redirect_block_for_domain(domain)) + return vhost.contains_list(test_redirect_block) + + def _set_http_header(self, domain: str, header_substring: Union[str, list[str], None]) -> None: + """Enables header identified by header_substring on domain. + + If the vhost is listening plaintextishly, separates out the relevant + directives into a new server block, and only add header directive to + HTTPS block. + + :param str domain: the domain to enable header for. + :param str header_substring: String to uniquely identify a header. + e.g. Strict-Transport-Security, Upgrade-Insecure-Requests + :returns: Success + :raises .errors.PluginError: If no viable HTTPS host can be created or + set with header header_substring. + """ + if not isinstance(header_substring, str): + raise errors.NotSupportedError("Invalid header_substring type " + f"{type(header_substring)}, expected a str.") + if header_substring not in constants.HEADER_ARGS: + raise errors.NotSupportedError( + f"{header_substring} is not supported by the nginx plugin.") + + vhosts = self.choose_vhosts(domain) + if not vhosts: + raise errors.PluginError( + "Unable to find corresponding HTTPS host for enhancement.") + for vhost in vhosts: + if vhost.has_header(header_substring): + raise errors.PluginEnhancementAlreadyPresent( + "Existing %s header" % (header_substring)) + + # if there is no separate SSL block, break the block into two and + # choose the SSL block. + if vhost.ssl and any(not addr.ssl for addr in vhost.addrs): + _, vhost = self._split_block(vhost) + + header_directives = [ + ['\n ', 'add_header', ' ', header_substring, ' '] + + constants.HEADER_ARGS[header_substring], + ['\n']] + self.parser.add_server_directives(vhost, header_directives) + + def _add_redirect_block(self, vhost: obj.VirtualHost, domain: str) -> None: + """Add redirect directive to vhost + """ + redirect_block = _redirect_block_for_domain(domain) + + self.parser.add_server_directives( + vhost, redirect_block, insert_at_top=True) + + def _split_block(self, vhost: obj.VirtualHost, only_directives: Optional[list[str]] = None + ) -> tuple[obj.VirtualHost, obj.VirtualHost]: + """Splits this "virtual host" (i.e. this nginx server block) into + separate HTTP and HTTPS blocks. + + :param vhost: The server block to break up into two. + :param list only_directives: If this exists, only duplicate these directives + when splitting the block. + :type vhost: :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + :returns: tuple (http_vhost, https_vhost) + :rtype: tuple of type :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + """ + http_vhost = self.parser.duplicate_vhost(vhost, only_directives=only_directives) + + def _ssl_match_func(directive: str) -> bool: + return 'ssl' in directive + + def _ssl_config_match_func(directive: str) -> bool: + return self.mod_ssl_conf in directive + + def _no_ssl_match_func(directive: str) -> bool: + return 'ssl' not in directive + + # remove all ssl addresses and related directives from the new block + for directive in self.SSL_DIRECTIVES: + self.parser.remove_server_directives(http_vhost, directive) + self.parser.remove_server_directives(http_vhost, 'listen', match_func=_ssl_match_func) + self.parser.remove_server_directives(http_vhost, 'include', + match_func=_ssl_config_match_func) + + # remove all non-ssl addresses from the existing block + self.parser.remove_server_directives(vhost, 'listen', match_func=_no_ssl_match_func) + return http_vhost, vhost + + def _enable_redirect(self, domain: str, + unused_options: Optional[Union[str, list[str]]]) -> None: + """Redirect all equivalent HTTP traffic to ssl_vhost. + + If the vhost is listening plaintextishly, separate out the + relevant directives into a new server block and add a rewrite directive. + + .. note:: This function saves the configuration + + :param str domain: domain to enable redirect for + :param unused_options: Not currently used + :type unused_options: Not Available + """ + + port = self.DEFAULT_LISTEN_PORT + # If there are blocks listening plaintextishly on self.DEFAULT_LISTEN_PORT, + # choose the most name-matching one. + + vhosts = self.choose_redirect_vhosts(domain, port) + + if not vhosts: + logger.info("No matching insecure server blocks listening on port %s found.", + self.DEFAULT_LISTEN_PORT) + return + + for vhost in vhosts: + self._enable_redirect_single(domain, vhost) + + def _enable_redirect_single(self, domain: str, vhost: obj.VirtualHost) -> None: + """Redirect all equivalent HTTP traffic to ssl_vhost. + + If the vhost is listening plaintextishly, separate out the + relevant directives into a new server block and add a rewrite directive. + + .. note:: This function saves the configuration + + :param str domain: domain to enable redirect for + :param `~obj.Vhost` vhost: vhost to enable redirect for + """ + if vhost.ssl: + http_vhost, _ = self._split_block(vhost, ['listen', 'server_name']) + + # Add this at the bottom to get the right order of directives + return_404_directive = [['\n ', 'return', ' ', '404']] + self.parser.add_server_directives(http_vhost, return_404_directive) + + vhost = http_vhost + + if self._has_certbot_redirect(vhost, domain): + logger.info("Traffic on port %s already redirecting to ssl in %s", + self.DEFAULT_LISTEN_PORT, vhost.filep) + else: + # Redirect plaintextish host to https + self._add_redirect_block(vhost, domain) + logger.info("Redirecting all traffic on port %s to ssl in %s", + self.DEFAULT_LISTEN_PORT, vhost.filep) + + def _enable_ocsp_stapling(self, domain: str, + chain_path: Optional[Union[str, list[str]]]) -> None: + """Include OCSP response in TLS handshake + + :param str domain: domain to enable OCSP response for + :param chain_path: chain file path + :type chain_path: `str` or `None` + + """ + if not isinstance(chain_path, str) and chain_path is not None: + raise errors.NotSupportedError(f"Invalid chain_path type {type(chain_path)}, " + "expected a str or None.") + vhosts = self.choose_vhosts(domain) + if not vhosts: + raise errors.PluginError( + "Unable to find corresponding HTTPS host for OCSP stapling.") + for vhost in vhosts: + self._enable_ocsp_stapling_single(vhost, chain_path) + + def _enable_ocsp_stapling_single(self, vhost: obj.VirtualHost, + chain_path: Optional[str]) -> None: + """Include OCSP response in TLS handshake + + :param str vhost: vhost to enable OCSP response for + :param chain_path: chain file path + :type chain_path: `str` or `None` + + """ + if self.version < (1, 3, 7): + raise errors.PluginError("Version 1.3.7 or greater of nginx " + "is needed to enable OCSP stapling") + + if chain_path is None: + raise errors.PluginError( + "--chain-path is required to enable " + "Online Certificate Status Protocol (OCSP) stapling " + "on nginx >= 1.3.7.") + + stapling_directives = [ + ['\n ', 'ssl_trusted_certificate', ' ', chain_path], + ['\n ', 'ssl_stapling', ' ', 'on'], + ['\n ', 'ssl_stapling_verify', ' ', 'on'], ['\n']] + + try: + self.parser.add_server_directives(vhost, + stapling_directives) + except errors.MisconfigurationError as error: + logger.debug(str(error)) + raise errors.PluginError("An error occurred while enabling OCSP " + "stapling for {0}.".format(vhost.names)) + + self.save_notes += ("OCSP Stapling was enabled " + "on SSL Vhost: {0}.\n".format(vhost.filep)) + self.save_notes += "\tssl_trusted_certificate {0}\n".format(chain_path) + self.save_notes += "\tssl_stapling on\n" + self.save_notes += "\tssl_stapling_verify on\n" + + ###################################### + # Nginx server management (Installer) + ###################################### + def restart(self) -> None: + """Restarts nginx server. + + :raises .errors.MisconfigurationError: If either the reload fails. + + """ + nginx_restart(self.conf('ctl'), self.nginx_conf, self.conf('sleep-seconds')) + + def config_test(self) -> None: + """Check the configuration of Nginx for errors. + + :raises .errors.MisconfigurationError: If config_test fails + + """ + try: + util.run_script([self.conf('ctl'), "-c", self.nginx_conf, "-t"]) + except errors.SubprocessError as err: + raise errors.MisconfigurationError(str(err)) + + def _nginx_version(self) -> str: + """Return results of nginx -V + + :returns: version text + :rtype: str + + :raises .PluginError: + Unable to run Nginx version command + """ + try: + proc = subprocess.run( + [self.conf('ctl'), "-c", self.nginx_conf, "-V"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + check=False, + env=util.env_no_snap_for_external_calls()) + text = proc.stderr # nginx prints output to stderr + except (OSError, ValueError) as error: + logger.debug(str(error), exc_info=True) + raise errors.PluginError( + "Unable to run %s -V" % self.conf('ctl')) + return text + + def get_version(self) -> tuple[int, ...]: + """Return version of Nginx Server. + + Version is returned as tuple. (ie. 2.4.7 = (2, 4, 7)) + + :returns: version + :rtype: tuple + + :raises .PluginError: + Unable to find Nginx version or version is unsupported + + """ + text = self._nginx_version() + + version_regex = re.compile(r"nginx version: ([^/]+)/([0-9\.]*)", re.IGNORECASE) + version_matches = version_regex.findall(text) + + sni_regex = re.compile(r"TLS SNI support enabled", re.IGNORECASE) + sni_matches = sni_regex.findall(text) + + ssl_regex = re.compile(r" --with-http_ssl_module") + ssl_matches = ssl_regex.findall(text) + + if not version_matches: + raise errors.PluginError("Unable to find Nginx version") + if not ssl_matches: + raise errors.PluginError( + "Nginx build is missing SSL module (--with-http_ssl_module).") + if not sni_matches: + raise errors.PluginError("Nginx build doesn't support SNI") + + product_name, product_version = version_matches[0] + if product_name != 'nginx': + logger.warning("NGINX derivative %s is not officially supported by" + " certbot", product_name) + + nginx_version = tuple(int(i) for i in product_version.split(".")) + + # nginx < 0.8.48 uses machine hostname as default server_name instead of + # the empty string + if nginx_version < (0, 8, 48): + raise errors.NotSupportedError("Nginx version must be 0.8.48+") + + return nginx_version + + def _get_openssl_version(self) -> str: + """Return version of OpenSSL linked to Nginx. + + Version is returned as string. If no version can be found, empty string is returned. + + :returns: openssl_version + :rtype: str + + :raises .PluginError: + Unable to run Nginx version command + """ + text = self._nginx_version() + + matches: list[str] = re.findall(r"running with OpenSSL ([^ ]+) ", text) + if not matches: + matches = re.findall(r"built with OpenSSL ([^ ]+) ", text) + if not matches: + logger.warning("NGINX configured with OpenSSL alternatives is not officially" + " supported by Certbot.") + return "" + return matches[0] + + def more_info(self) -> str: + """Human-readable string to help understand the module""" + return ( + "Configures Nginx to authenticate and install HTTPS.{0}" + "Server root: {root}{0}" + "Version: {version}".format( + os.linesep, root=self.parser.config_root, + version=".".join(str(i) for i in self.version)) + ) + + def auth_hint(self, # pragma: no cover + failed_achalls: Iterable[achallenges.AnnotatedChallenge]) -> str: + return ( + "The Certificate Authority failed to verify the temporary nginx configuration changes " + "made by Certbot. Ensure the listed domains point to this nginx server and that it is " + "accessible from the internet." + ) + + ################################################### + # Wrapper functions for Reverter class (Installer) + ################################################### + def save(self, title: Optional[str] = None, temporary: bool = False) -> None: + """Saves all changes to the configuration files. + + :param str title: The title of the save. If a title is given, the + configuration will be saved as a new checkpoint and put in a + timestamped directory. + + :param bool temporary: Indicates whether the changes made will + be quickly reversed in the future (ie. challenges) + + :raises .errors.PluginError: If there was an error in + an attempt to save the configuration, or an error creating a + checkpoint + + """ + save_files = set(self.parser.parsed.keys()) + self.add_to_checkpoint(save_files, self.save_notes, temporary) + self.save_notes = "" + + # Change 'ext' to something else to not override existing conf files + self.parser.filedump(ext='') + if title and not temporary: + self.finalize_checkpoint(title) + + def recovery_routine(self) -> None: + """Revert all previously modified files. + + Reverts all modified files that have not been saved as a checkpoint + + :raises .errors.PluginError: If unable to recover the configuration + + """ + super().recovery_routine() + self.new_vhost = None + self.parser.load() + + def revert_challenge_config(self) -> None: + """Used to cleanup challenge configurations. + + :raises .errors.PluginError: If unable to revert the challenge config. + + """ + self.revert_temporary_config() + self.new_vhost = None + self.parser.load() + + def rollback_checkpoints(self, rollback: int = 1) -> None: + """Rollback saved checkpoints. + + :param int rollback: Number of checkpoints to revert + + :raises .errors.PluginError: If there is a problem with the input or + the function is unable to correctly revert the configuration + + """ + super().rollback_checkpoints(rollback) + self.new_vhost = None + self.parser.load() + + ########################################################################### + # Challenges Section for Authenticator + ########################################################################### + def get_chall_pref(self, unused_identifier: str) -> list[type[challenges.Challenge]]: + """Return list of challenge preferences.""" + return [challenges.HTTP01] + + # Entry point in main.py for performing challenges + def perform(self, achalls: list[achallenges.AnnotatedChallenge] + ) -> list[challenges.ChallengeResponse]: + """Perform the configuration related challenge. + + This function currently assumes all challenges will be fulfilled. + If this turns out not to be the case in the future. Cleanup and + outstanding challenges will have to be designed better. + + """ + self._chall_out += len(achalls) + responses: list[Optional[challenges.ChallengeResponse]] = [None] * len(achalls) + http_doer = http_01.NginxHttp01(self) + + for i, achall in enumerate(achalls): + # Currently also have chall_doer hold associated index of the + # challenge. This helps to put all of the responses back together + # when they are all complete. + if not isinstance(achall, achallenges.KeyAuthorizationAnnotatedChallenge): + raise errors.Error("Challenge should be an instance " + "of KeyAuthorizationAnnotatedChallenge") + http_doer.add_chall(achall, i) + + http_response = http_doer.perform() + # Must restart in order to activate the challenges. + # Handled here because we may be able to load up other challenge types + self.restart() + + # Go through all of the challenges and assign them to the proper place + # in the responses return value. All responses must be in the same order + # as the original challenges. + for i, resp in enumerate(http_response): + responses[http_doer.indices[i]] = resp + + return [response for response in responses if response] + + # called after challenges are performed + def cleanup(self, achalls: list[achallenges.AnnotatedChallenge]) -> None: + """Revert all challenges.""" + self._chall_out -= len(achalls) + + # If all of the challenges have been finished, clean up everything + if self._chall_out <= 0: + self.revert_challenge_config() + self.restart() + + +def _test_block_from_block(block: list[Any]) -> list[Any]: + test_block = nginxparser.UnspacedList(block) + parser.comment_directive(test_block, 0) + return test_block[:-1] + + +def _redirect_block_for_domain(domain: str) -> list[Any]: + updated_domain = domain + match_symbol = '=' + if util.is_wildcard_domain(domain): + match_symbol = '~' + updated_domain = updated_domain.replace('.', r'\.') + updated_domain = updated_domain.replace('*', '[^.]+') + updated_domain = '^' + updated_domain + '$' + redirect_block = [[ + ['\n ', 'if', ' ', '($host', ' ', match_symbol, ' ', '%s)' % updated_domain, ' '], + [['\n ', 'return', ' ', '301', ' ', 'https://$host$request_uri'], + '\n ']], + ['\n']] + return redirect_block + + +def nginx_restart(nginx_ctl: str, nginx_conf: str, sleep_duration: int) -> None: + """Restarts the Nginx Server. + + .. todo:: Nginx restart is fatal if the configuration references + non-existent SSL cert/key files. Remove references to /etc/letsencrypt + before restart. + + :param str nginx_ctl: Path to the Nginx binary. + :param str nginx_conf: Path to the Nginx configuration file. + :param int sleep_duration: How long to sleep after sending the reload signal. + + """ + try: + reload_output: str = "" + with tempfile.TemporaryFile() as out: + proc = subprocess.run([nginx_ctl, "-c", nginx_conf, "-s", "reload"], + env=util.env_no_snap_for_external_calls(), + stdout=out, stderr=out, check=False) + out.seek(0) + reload_output = out.read().decode("utf-8") + + if proc.returncode != 0: + logger.debug("nginx reload failed:\n%s", reload_output) + # Maybe Nginx isn't running - try start it + # Write to temporary files instead of piping because of communication issues on Arch + # https://github.com/certbot/certbot/issues/4324 + with tempfile.TemporaryFile() as out: + nginx_proc = subprocess.run([nginx_ctl, "-c", nginx_conf], + stdout=out, stderr=out, env=util.env_no_snap_for_external_calls(), check=False) + if nginx_proc.returncode != 0: + out.seek(0) + # Enter recovery routine... + raise errors.MisconfigurationError( + "nginx restart failed:\n%s" % out.read().decode("utf-8")) + + except (OSError, ValueError): + raise errors.MisconfigurationError("nginx restart failed") + # Nginx can take a significant duration of time to fully apply a new config, depending + # on size and contents (https://github.com/certbot/certbot/issues/7422). Lacking a way + # to reliably identify when this process is complete, we provide the user with control + # over how long Certbot will sleep after reloading the configuration. + if sleep_duration > 0: + time.sleep(sleep_duration) + + +def _determine_default_server_root() -> str: + if os.environ.get("CERTBOT_DOCS") == "1": + default_server_root = (f"{constants.LINUX_SERVER_ROOT} " + f"or {constants.FREEBSD_DARWIN_SERVER_ROOT}") + else: + default_server_root = constants.CLI_DEFAULTS["server_root"] + return default_server_root diff --git a/certbot/src/certbot/_internal/plugins/nginx/constants.py b/certbot/src/certbot/_internal/plugins/nginx/constants.py new file mode 100644 index 00000000000..6fa3c7270af --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/constants.py @@ -0,0 +1,79 @@ +"""nginx plugin constants.""" +import platform +from typing import Any + +FREEBSD_DARWIN_SERVER_ROOT = "/usr/local/etc/nginx" +LINUX_SERVER_ROOT = "/etc/nginx" +PKGSRC_SERVER_ROOT = "/usr/pkg/etc/nginx" + +if platform.system() in ('FreeBSD', 'Darwin'): + server_root_tmp = FREEBSD_DARWIN_SERVER_ROOT +elif platform.system() in ('NetBSD',): + server_root_tmp = PKGSRC_SERVER_ROOT +else: + server_root_tmp = LINUX_SERVER_ROOT + +CLI_DEFAULTS: dict[str, Any] = { + "server_root": server_root_tmp, + "ctl": "nginx", + "sleep_seconds": 1 +} +"""CLI defaults.""" + + +MOD_SSL_CONF_DEST = "options-ssl-nginx.conf" +"""Name of the mod_ssl config file as saved +in `certbot.configuration.NamespaceConfig.config_dir`.""" + +UPDATED_MOD_SSL_CONF_DIGEST = ".updated-options-ssl-nginx-conf-digest.txt" +"""Name of the hash of the updated or informed mod_ssl_conf as saved +in `certbot.configuration.NamespaceConfig.config_dir`.""" + +ALL_SSL_OPTIONS_HASHES = [ + '0f81093a1465e3d4eaa8b0c14e77b2a2e93568b0fc1351c2b87893a95f0de87c', + '9a7b32c49001fed4cff8ad24353329472a50e86ade1ef9b2b9e43566a619612e', + 'a6d9f1c7d6b36749b52ba061fff1421f9a0a3d2cfdafbd63c05d06f65b990937', + '7f95624dd95cf5afc708b9f967ee83a24b8025dc7c8d9df2b556bbc64256b3ff', + '394732f2bbe3e5e637c3fb5c6e980a1f1b90b01e2e8d6b7cff41dde16e2a756d', + '4b16fec2bcbcd8a2f3296d886f17f9953ffdcc0af54582452ca1e52f5f776f16', + 'c052ffff0ad683f43bffe105f7c606b339536163490930e2632a335c8d191cc4', + '02329eb19930af73c54b3632b3165d84571383b8c8c73361df940cb3894dd426', + '63e2bddebb174a05c9d8a7cf2adf72f7af04349ba59a1a925fe447f73b2f1abf', + '2901debc7ecbc10917edd9084c05464c9c5930b463677571eaf8c94bffd11ae2', + '30baca73ed9a5b0e9a69ea40e30482241d8b1a7343aa79b49dc5d7db0bf53b6c', + '02329eb19930af73c54b3632b3165d84571383b8c8c73361df940cb3894dd426', + '108c4555058a087496a3893aea5d9e1cee0f20a3085d44a52dc1a66522299ac3', + 'd5e021706ecdccc7090111b0ae9a29ef61523e927f020e410caf0a1fd7063981', + 'ef11e3fb17213e74d3e1816cde0ec37b8b95b4167cf21e7b8ff1eaa9c6f918ee', + 'af85f6193808a44789a1d293e6cffa249cad9a21135940800958b8e3c72dbc69', + 'a2a612fd21b02abaa32d9d11ac63d987d6e3054dbfa356de5800eea0d7ce17f3', + '2d9648302e3588a172c318e46bff88ade46fc7a16d6afc85322776a04800d473', + '5e21cc66989f26ec46116d979421e538131cf8ab33ffff3f682fbfe491b0ace8', + 'f5615544105c4eee44f02a604e3e9ae55b3d5bad247160bb18731a0ac531af02', + '05a799c4db12f8e15e68219c98056824cbd5ae7b05863225318ae112f343880b', + 'dc81acfd9670f137d5abbccfe3438d9306d4b6a906439b0fbf6a6756272e7cc7', + '0175f71721dd8e5315a6d0f3efef703ff54e867d1ab2a4e076791b89a0b3511a', + '246b520bedc461fcbd35f4d3efdd75ebf171baccaba5c38f488009566de6d5b3', + 'dd72286f760c90550f34fbeeceb5a1f1351b09b812e65a18569a0f4a4d7f5847', +] +"""SHA256 hashes of the contents of all versions of MOD_SSL_CONF_SRC""" + + +def os_constant(key: str) -> Any: + # XXX TODO: In the future, this could return different constants + # based on what OS we are running under. To see an + # approach to how to handle different OSes, see the + # apache version of this file. Currently, we do not + # actually have any OS-specific constants on Nginx. + """ + Get a constant value for operating system + + :param str key: name of cli constant + :return: value of constant for active os + """ + return CLI_DEFAULTS[key] + + +HSTS_ARGS = ['\"max-age=31536000\"', ' ', 'always'] + +HEADER_ARGS = {'Strict-Transport-Security': HSTS_ARGS} diff --git a/certbot/src/certbot/_internal/plugins/nginx/display_ops.py b/certbot/src/certbot/_internal/plugins/nginx/display_ops.py new file mode 100644 index 00000000000..fbd97d9f1cd --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/display_ops.py @@ -0,0 +1,43 @@ +"""Contains UI methods for Nginx operations.""" +import logging +from typing import Iterable +from typing import Optional + +from certbot.display import util as display_util +from certbot._internal.plugins.nginx.obj import VirtualHost + +logger = logging.getLogger(__name__) + + +def select_vhost_multiple(vhosts: Optional[Iterable[VirtualHost]]) -> list[VirtualHost]: + """Select multiple Vhosts to install the certificate for + :param vhosts: Available Nginx VirtualHosts + :type vhosts: :class:`list` of type `~obj.Vhost` + :returns: List of VirtualHosts + :rtype: :class:`list`of type `~obj.Vhost` + """ + if not vhosts: + return [] + tags_list = [vhost.display_repr()+"\n" for vhost in vhosts] + # Remove the extra newline from the last entry + if tags_list: + tags_list[-1] = tags_list[-1][:-1] + code, names = display_util.checklist( + "Which server blocks would you like to modify?", + tags=tags_list, force_interactive=True) + if code == display_util.OK: + return_vhosts = _reversemap_vhosts(names, vhosts) + return return_vhosts + return [] + + +def _reversemap_vhosts(names: Iterable[str], vhosts: Iterable[VirtualHost]) -> list[VirtualHost]: + """Helper function for select_vhost_multiple for mapping string + representations back to actual vhost objects""" + return_vhosts = [] + + for selection in names: + for vhost in vhosts: + if vhost.display_repr().strip() == selection.strip(): + return_vhosts.append(vhost) + return return_vhosts diff --git a/certbot/src/certbot/_internal/plugins/nginx/http_01.py b/certbot/src/certbot/_internal/plugins/nginx/http_01.py new file mode 100644 index 00000000000..7ff737a1900 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/http_01.py @@ -0,0 +1,244 @@ +"""A class that performs HTTP-01 challenges for Nginx""" + +import logging +from typing import Any +from typing import Optional +from typing import TYPE_CHECKING + +from acme import challenges, messages +from acme.challenges import KeyAuthorizationChallengeResponse +from certbot import errors +from certbot.achallenges import KeyAuthorizationAnnotatedChallenge +from certbot.compat import os +from certbot.plugins import common +from certbot._internal.plugins.nginx import nginxparser +from certbot._internal.plugins.nginx.obj import Addr + +if TYPE_CHECKING: + from certbot._internal.plugins.nginx.configurator import NginxConfigurator + +logger = logging.getLogger(__name__) + + +class NginxHttp01(common.ChallengePerformer): + """HTTP-01 authenticator for Nginx + + :ivar configurator: NginxConfigurator object + :type configurator: :class:`~nginx.configurator.NginxConfigurator` + + :ivar list achalls: Annotated + class:`~certbot.achallenges.KeyAuthorizationAnnotatedChallenge` + challenges + + :param list indices: Meant to hold indices of challenges in a + larger array. NginxHttp01 is capable of solving many challenges + at once which causes an indexing issue within NginxConfigurator + who must return all responses in order. Imagine + NginxConfigurator maintaining state about where all of the + challenges, possibly of different types, belong in the response + array. This is an optional utility. + + """ + + def __init__(self, configurator: "NginxConfigurator") -> None: + super().__init__(configurator) + self.configurator: "NginxConfigurator" + self.challenge_conf = os.path.join( + configurator.config.config_dir, "le_http_01_cert_challenge.conf") + + def perform(self) -> list[KeyAuthorizationChallengeResponse]: + """Perform a challenge on Nginx. + + :returns: list of :class:`acme.challenges.KeyAuthorizationChallengeResponse` + :rtype: list + + """ + if not self.achalls: + return [] + if any(achall.identifier.typ == messages.IDENTIFIER_IP for achall in self.achalls): + raise errors.ConfigurationError( + "nginx authenticator not supported for IP address certificates") + + responses = [x.response(x.account_key) for x in self.achalls] + + # Set up the configuration + self._mod_config() + + # Save reversible changes + self.configurator.save("HTTP Challenge", True) + + return responses + + def _mod_config(self) -> None: + """Modifies Nginx config to include server_names_hash_bucket_size directive + and server challenge blocks. + + :raises .MisconfigurationError: + Unable to find a suitable HTTP block in which to include + authenticator hosts. + """ + included = False + include_directive = ['\n', 'include', ' ', self.challenge_conf] + http_path = self.configurator.parser.http_path + + bucket_directive = ['\n', 'server_names_hash_bucket_size', ' ', '128'] + + main = self.configurator.parser.parsed[http_path] + # insert include directive + for line in main: + if line[0] == ['http']: + body = line[1] + if include_directive not in body: + body.insert(0, include_directive) + included = True + break + + # insert or update the server_names_hash_bucket_size directive + # We have several options here. + # 1) Only check nginx.conf + # 2) Check included files, assuming they've been included inside http already, + # because if they added it outside an http block their config is broken anyway + # 3) Add metadata during parsing to note if an include happened inside the http block + # + # 1 causes bugs; see https://github.com/certbot/certbot/issues/5199 + # 3 would require a more extensive rewrite and probably isn't necessary anyway + # So this code uses option 2. + found_bucket = False + for file_contents in self.configurator.parser.parsed.values(): + body = file_contents # already inside http in an included file + for line in file_contents: + if line[0] == ['http']: + body = line[1] # enter http because this is nginx.conf + break + + for posn, inner_line in enumerate(body): + if inner_line[0] == bucket_directive[1]: + if int(inner_line[1]) < int(bucket_directive[3]): + body[posn] = bucket_directive + found_bucket = True + break + + if found_bucket: + break + + if not found_bucket: + for line in main: + if line[0] == ['http']: + body = line[1] + body.insert(0, bucket_directive) + break + + root = self.configurator.parser.config_root + if not included: + raise errors.MisconfigurationError( + 'Certbot could not find a block to include ' + 'challenges in %s.' % root) + config = [self._make_or_mod_server_block(achall) for achall in self.achalls] + config = [x for x in config if x is not None] + config = nginxparser.UnspacedList(config) + logger.debug("Generated server block:\n%s", str(config)) + + self.configurator.reverter.register_file_creation( + True, self.challenge_conf) + + with open(self.challenge_conf, "w", encoding="utf-8") as new_conf: + nginxparser.dump(config, new_conf) + + def _default_listen_addresses(self) -> list[Addr]: + """Finds addresses for a challenge block to listen on. + :returns: list of :class:`certbot._internal.plugins.nginx.obj.Addr` to apply + :rtype: list + """ + addresses: list[Addr] = [] + default_addr = "%s" % self.configurator.config.http01_port + ipv6_addr = "[::]:{0}".format( + self.configurator.config.http01_port) + port = self.configurator.config.http01_port + + ipv6, ipv6only = self.configurator.ipv6_info("[::]", str(port)) + + if ipv6: + # If IPv6 is active in Nginx configuration + if not ipv6only: + # If ipv6only=on is not already present in the config + ipv6_addr = ipv6_addr + " ipv6only=on" + addresses = [Addr.fromstring(default_addr), + Addr.fromstring(ipv6_addr)] + logger.debug(("Using default addresses %s and %s for authentication."), + default_addr, + ipv6_addr) + else: + addresses = [Addr.fromstring(default_addr)] + logger.debug("Using default address %s for authentication.", + default_addr) + + return addresses + + def _get_validation_path(self, achall: KeyAuthorizationAnnotatedChallenge) -> str: + return os.sep + os.path.join(challenges.HTTP01.URI_ROOT_PATH, achall.chall.encode("token")) + + def _make_server_block(self, achall: KeyAuthorizationAnnotatedChallenge) -> list[Any]: + """Creates a server block for a challenge. + + :param achall: Annotated HTTP-01 challenge + :type achall: :class:`certbot.achallenges.KeyAuthorizationAnnotatedChallenge` + + :returns: server block for the challenge host + :rtype: list + """ + addrs = self._default_listen_addresses() + block = [['listen', ' ', addr.to_string(include_default=False)] for addr in addrs] + + # Ensure we 404 on any other request by setting a root + document_root = os.path.join( + self.configurator.config.work_dir, "http_01_nonexistent") + + block.extend([['server_name', ' ', achall.identifier.value], + ['root', ' ', document_root], + self._location_directive_for_achall(achall) + ]) + # TODO: do we want to return something else if they otherwise access this block? + return [['server'], block] + + def _location_directive_for_achall(self, achall: KeyAuthorizationAnnotatedChallenge + ) -> list[Any]: + validation = achall.validation(achall.account_key) + validation_path = self._get_validation_path(achall) + + location_directive = [['location', ' ', '=', ' ', validation_path], + [['default_type', ' ', 'text/plain'], + ['return', ' ', '200', ' ', validation]]] + return location_directive + + def _make_or_mod_server_block(self, achall: KeyAuthorizationAnnotatedChallenge + ) -> Optional[list[Any]]: + """Modifies server blocks to respond to a challenge. Returns a new HTTP server block + to add to the configuration if an existing one can't be found. + + :param achall: Annotated HTTP-01 challenge + :type achall: :class:`certbot.achallenges.KeyAuthorizationAnnotatedChallenge` + + :returns: new server block to be added, if any + :rtype: list + + """ + http_vhosts, https_vhosts = self.configurator.choose_auth_vhosts(achall.identifier.value) + + new_vhost: Optional[list[Any]] = None + if not http_vhosts: + # Couldn't find either a matching name+port server block + # or a port+default_server block, so create a dummy block + new_vhost = self._make_server_block(achall) + + # Modify any existing server blocks + for vhost in set(http_vhosts + https_vhosts): + location_directive = [self._location_directive_for_achall(achall)] + + self.configurator.parser.add_server_directives(vhost, location_directive) + + rewrite_directive = [['rewrite', ' ', '^(/.well-known/acme-challenge/.*)', + ' ', '$1', ' ', 'break']] + self.configurator.parser.add_server_directives( + vhost, rewrite_directive, insert_at_top=True) + + return new_vhost diff --git a/certbot/src/certbot/_internal/plugins/nginx/nginxparser.py b/certbot/src/certbot/_internal/plugins/nginx/nginxparser.py new file mode 100644 index 00000000000..2d6c721f695 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/nginxparser.py @@ -0,0 +1,324 @@ +"""Very low-level nginx config parser based on pyparsing.""" +# Forked from https://github.com/fatiherikli/nginxparser (MIT Licensed) +import copy +import logging +import operator +import typing +from typing import Any +from typing import IO +from typing import Iterable +from typing import Iterator +from typing import overload +from typing import SupportsIndex +from typing import Union + +from pyparsing import Combine +from pyparsing import FollowedBy +from pyparsing import Forward +from pyparsing import Group +from pyparsing import Literal +from pyparsing import Optional +from pyparsing import ParseResults +from pyparsing import QuotedString +from pyparsing import Regex +from pyparsing import restOfLine +from pyparsing import stringEnd +from pyparsing import White +from pyparsing import ZeroOrMore + +logger = logging.getLogger(__name__) + + +class RawNginxParser: + # pylint: disable=pointless-statement + """A class that parses nginx configuration with pyparsing.""" + + # constants + space = Optional(White(ws=' \t\r\n\u00a0')).leave_whitespace() + required_space = White(ws=' \t\r\n\u00a0').leave_whitespace() + + left_bracket = Literal("{").suppress() + right_bracket = space + Literal("}").suppress() + semicolon = Literal(";").suppress() + dquoted = QuotedString('"', multiline=True, unquote_results=False, esc_char='\\') + squoted = QuotedString("'", multiline=True, unquote_results=False, esc_char='\\') + quoted = dquoted | squoted + head_tokenchars = Regex(r"(\$\{)|[^{};\s'\"]") # if (last_space) + tail_tokenchars = Regex(r"(\$\{)|[^{;\s]") # else + tokenchars = Combine(head_tokenchars + ZeroOrMore(tail_tokenchars)) + paren_quote_extend = Combine(quoted + Literal(')') + ZeroOrMore(tail_tokenchars)) + # note: ')' allows extension, but then we fall into else, not last_space. + + token = paren_quote_extend | tokenchars | quoted + + # An nginx comment runs from '#' to the end of the line. `restOfLine` does + # not consume the trailing newline; the following `required_space` does. + comment = Literal('#') + restOfLine + # A separator between two tokens is whitespace, optionally interleaved with + # one or more comments. The `FollowedBy(token)` lookahead means a comment is + # only treated as inline when a real token follows it — otherwise we leave + # the `#` alone so existing fallback paths (e.g. comment-as-token in a block + # header) still parse. This lets a multi-line directive contain comment + # lines between its tokens (valid nginx), which the old grammar mis-parsed: + # an unbalanced `"` inside such a comment would be sucked up by the + # `multiline=True` quoted strings and run away across the rest of the file + # (issue #10598). + token_separator = required_space + ZeroOrMore( + comment + required_space + FollowedBy(token) + ) + + whitespace_token_group = space + token + ZeroOrMore(token_separator + token) + space + assignment = whitespace_token_group + semicolon + + block = Forward() + + # order matters! see issue 518, and also http { # server { \n} + contents = Group(space + comment) | Group(block) | Group(assignment) + + block_begin = Group(whitespace_token_group) + block_innards = Group(ZeroOrMore(contents) + space).leave_whitespace() + block << block_begin + left_bracket + block_innards + right_bracket + + script = ZeroOrMore(contents) + space + stringEnd + script.parse_with_tabs().leave_whitespace() + + def __init__(self, source: str) -> None: + self.source = source + + def parse(self) -> ParseResults: + """Returns the parsed tree.""" + return self.script.parse_string(self.source) + + def as_list(self) -> list[Any]: + """Returns the parsed tree as a list.""" + return self.parse().asList() + + +class RawNginxDumper: + """A class that dumps nginx configuration from the provided tree.""" + def __init__(self, blocks: list[Any]) -> None: + self.blocks = blocks + + def __iter__(self, blocks: typing.Optional[list[Any]] = None) -> Iterator[str]: + """Iterates the dumped nginx content.""" + blocks = blocks or self.blocks + for b0 in blocks: + if isinstance(b0, str): + yield b0 + continue + item = copy.deepcopy(b0) + if spacey(item[0]): + yield item.pop(0) # indentation + if not item: + continue + + if isinstance(item[0], list): # block + yield "".join(item.pop(0)) + '{' + for parameter in item.pop(0): + yield from self.__iter__([parameter]) # negate "for b0 in blocks" + yield '}' + else: # not a block - list of strings + semicolon = ";" + if isinstance(item[0], str) and item[0].strip() == '#': # comment + semicolon = "" + yield "".join(item) + semicolon + + def __str__(self) -> str: + """Return the parsed block as a string.""" + return ''.join(self) + + +def spacey(x: Any) -> bool: + """Is x an empty string or whitespace?""" + return (isinstance(x, str) and x.isspace()) or x == '' + + +class UnspacedList(list[Any]): + """Wrap a list [of lists], making any whitespace entries magically invisible""" + + def __init__(self, list_source: Iterable[Any]) -> None: + # ensure our argument is not a generator, and duplicate any sublists + self.spaced = copy.deepcopy(list(list_source)) + self.dirty = False + + # Turn self into a version of the source list that has spaces removed + # and all sub-lists also UnspacedList()ed + super().__init__(list_source) + for i, entry in reversed(list(enumerate(self))): + if isinstance(entry, list): + sublist = UnspacedList(entry) + super().__setitem__(i, sublist) + self.spaced[i] = sublist.spaced + elif spacey(entry): + # don't delete comments + if "#" not in self[:i]: + super().__delitem__(i) + + @overload + def _coerce(self, inbound: None) -> tuple[None, None]: ... + + @overload + def _coerce(self, inbound: str) -> tuple[str, str]: ... + + @overload + def _coerce(self, inbound: list[Any]) -> tuple["UnspacedList", list[Any]]: ... + + def _coerce(self, inbound: Any) -> tuple[Any, Any]: + """ + Coerce some inbound object to be appropriately usable in this object + + :param inbound: string or None or list or UnspacedList + :returns: (coerced UnspacedList or string or None, spaced equivalent) + :rtype: tuple + + """ + if not isinstance(inbound, list): # str or None + return inbound, inbound + else: + if not hasattr(inbound, "spaced"): + inbound = UnspacedList(inbound) + return inbound, inbound.spaced + + def insert(self, i: "SupportsIndex", x: Any) -> None: + """Insert object before index.""" + idx = operator.index(i) + item, spaced_item = self._coerce(x) + slicepos = self._spaced_position(idx) if idx < len(self) else len(self.spaced) + self.spaced.insert(slicepos, spaced_item) + if not spacey(item): + super().insert(idx, item) + self.dirty = True + + def append(self, x: Any) -> None: + """Append object to the end of the list.""" + item, spaced_item = self._coerce(x) + self.spaced.append(spaced_item) + if not spacey(item): + super().append(item) + self.dirty = True + + def extend(self, x: Any) -> None: + """Extend list by appending elements from the iterable.""" + item, spaced_item = self._coerce(x) + self.spaced.extend(spaced_item) + super().extend(item) + self.dirty = True + + def __add__(self, other: list[Any]) -> "UnspacedList": + new_list = copy.deepcopy(self) + new_list.extend(other) + new_list.dirty = True + return new_list + + def pop(self, *args: Any, **kwargs: Any) -> None: + """Function pop() is not implemented for UnspacedList""" + raise NotImplementedError("UnspacedList.pop() not yet implemented") + + def remove(self, *args: Any, **kwargs: Any) -> None: + """Function remove() is not implemented for UnspacedList""" + raise NotImplementedError("UnspacedList.remove() not yet implemented") + + def reverse(self) -> None: + """Function reverse() is not implemented for UnspacedList""" + raise NotImplementedError("UnspacedList.reverse() not yet implemented") + + def sort(self, *_args: Any, **_kwargs: Any) -> None: + """Function sort() is not implemented for UnspacedList""" + raise NotImplementedError("UnspacedList.sort() not yet implemented") + + def __setslice__(self, *args: Any, **kwargs: Any) -> None: + raise NotImplementedError("Slice operations on UnspacedLists not yet implemented") + + def __setitem__(self, i: Union["SupportsIndex", slice], value: Any) -> None: + if isinstance(i, slice): + raise NotImplementedError("Slice operations on UnspacedLists not yet implemented") + item, spaced_item = self._coerce(value) + self.spaced.__setitem__(self._spaced_position(i), spaced_item) + if not spacey(item): + super().__setitem__(i, item) + self.dirty = True + + def __delitem__(self, i: Union["SupportsIndex", slice]) -> None: + if isinstance(i, slice): + raise NotImplementedError("Slice operations on UnspacedLists not yet implemented") + self.spaced.__delitem__(self._spaced_position(i)) + super().__delitem__(i) + self.dirty = True + + def __deepcopy__(self, memo: Any) -> "UnspacedList": + new_spaced = copy.deepcopy(self.spaced, memo=memo) + new_list = UnspacedList(new_spaced) + new_list.dirty = self.dirty + return new_list + + def is_dirty(self) -> bool: + """Recurse through the parse tree to figure out if any sublists are dirty""" + if self.dirty: + return True + return any((isinstance(x, UnspacedList) and x.is_dirty() for x in self)) + + def _spaced_position(self, idx: "SupportsIndex") -> int: + """Convert from indexes in the unspaced list to positions in the spaced one""" + int_idx = operator.index(idx) + pos = spaces = 0 + # Normalize indexes like list[-1] etc, and save the result + if int_idx < 0: + int_idx = len(self) + int_idx + if not 0 <= int_idx < len(self): + raise IndexError("list index out of range") + int_idx0 = int_idx + # Count the number of spaces in the spaced list before int_idx in the unspaced one + while int_idx != -1: + if spacey(self.spaced[pos]): + spaces += 1 + else: + int_idx -= 1 + pos += 1 + return int_idx0 + spaces + + +# Shortcut functions to respect Python's serialization interface +# (like pyyaml, picker or json) + +def loads(source: str) -> UnspacedList: + """Parses from a string. + + :param str source: The string to parse + :returns: The parsed tree + :rtype: list + + """ + return UnspacedList(RawNginxParser(source).as_list()) + + +def load(file_: IO[Any]) -> UnspacedList: + """Parses from a file. + + :param file file_: The file to parse + :returns: The parsed tree + :rtype: list + + """ + return loads(file_.read()) + + +def dumps(blocks: UnspacedList) -> str: + """Dump to a Unicode string. + + :param UnspacedList blocks: The parsed tree + :rtype: str + + """ + return str(RawNginxDumper(blocks.spaced)) + + +def dump(blocks: UnspacedList, file_: IO[Any]) -> None: + """Dump to a file. + + :param UnspacedList blocks: The parsed tree + :param IO[Any] file_: The file stream to dump to. It must be opened with + Unicode encoding. + :rtype: None + + """ + file_.write(dumps(blocks)) diff --git a/certbot/src/certbot/_internal/plugins/nginx/obj.py b/certbot/src/certbot/_internal/plugins/nginx/obj.py new file mode 100644 index 00000000000..ee6a8a77630 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/obj.py @@ -0,0 +1,281 @@ +"""Module contains classes used by the Nginx Configurator.""" +import re +from typing import Any +from typing import Optional +from typing import Sequence +from typing import Union + +from certbot.plugins import common + +ADD_HEADER_DIRECTIVE = 'add_header' + + +class SocketAddrError(Exception): + """Raised when a UNIX-domain socket address is encountered.""" + + +class Addr(common.Addr): + r"""Represents an Nginx address, i.e. what comes after the 'listen' + directive. + + According to the `documentation`_, this may be address[:port], port, + or unix:path. The latter is ignored here. + + The default value if no directive is specified is \*:80 (superuser) + or \*:8000 (otherwise). If no port is specified, the default is + 80. If no address is specified, listen on all addresses. + + .. _documentation: + https://nginx.org/en/docs/http/ngx_http_core_module.html#listen + + .. todo:: Old-style nginx configs define SSL vhosts in a separate + block instead of using 'ssl' in the listen directive. + + :param str addr: addr part of vhost address, may be hostname, IPv4, IPv6, + "", or "\*" + :param str port: port number or "\*" or "" + :param bool ssl: Whether the directive includes 'ssl' + :param bool default: Whether the directive includes 'default_server' + :param bool default: Whether this is an IPv6 address + :param bool ipv6only: Whether the directive includes 'ipv6only=on' + + """ + UNSPECIFIED_IPV4_ADDRESSES = ('', '*', '0.0.0.0') + CANONICAL_UNSPECIFIED_ADDRESS = UNSPECIFIED_IPV4_ADDRESSES[0] + + def __init__(self, host: str, port: str, ssl: bool, default: bool, + ipv6: bool, ipv6only: bool) -> None: + super().__init__((host, port)) + self.ssl = ssl + self.default = default + self.ipv6 = ipv6 + self.ipv6only = ipv6only + self.unspecified_address = host in self.UNSPECIFIED_IPV4_ADDRESSES + + @classmethod + def fromstring(cls, str_addr: str) -> "Addr": + """Initialize Addr from string. + + :param str str_addr: nginx address string + :returns: parsed nginx address + :rtype: Addr + :raises SocketAddrError: if a UNIX-domain socket address is given + + """ + parts = str_addr.split(' ') + ssl = False + default = False + ipv6 = False + ipv6only = False + host = '' + port = '' + + # The first part must be the address + addr = parts.pop(0) + + # Raise for UNIX-domain sockets + if addr.startswith('unix:'): + raise SocketAddrError(f'encountered UNIX-domain socket address {str_addr}') + + # IPv6 check + ipv6_match = re.match(r'\[.*\]', addr) + if ipv6_match: + ipv6 = True + # IPv6 handling + host = ipv6_match.group() + # The rest of the addr string will be the port, if any + port = addr[ipv6_match.end()+1:] + else: + # IPv4 handling + tup = addr.partition(':') + if re.match(r'^\d+$', tup[0]): + # This is a bare port, not a hostname. E.g. listen 80 + host = '' + port = tup[0] + else: + # This is a host-port tuple. E.g. listen 127.0.0.1:* + host = tup[0] + port = tup[2] + + # The rest of the parts are options; we only care about ssl and default + while parts: + nextpart = parts.pop() + if nextpart == 'ssl': + ssl = True + elif nextpart == 'default_server': + default = True + elif nextpart == 'default': + default = True + elif nextpart == "ipv6only=on": + ipv6only = True + + return cls(host, port, ssl, default, ipv6, ipv6only) + + def to_string(self, include_default: bool = True) -> str: + """Return string representation of Addr""" + parts = '' + if self.tup[0] and self.tup[1]: + parts = "%s:%s" % self.tup + elif self.tup[0]: + parts = self.tup[0] + else: + parts = self.tup[1] + + if self.default and include_default: + parts += ' default_server' + if self.ssl: + parts += ' ssl' + + return parts + + def __str__(self) -> str: + return self.to_string() + + def __repr__(self) -> str: + return "Addr(" + self.__str__() + ")" + + def __hash__(self) -> int: # pylint: disable=useless-super-delegation + # Python 3 requires explicit overridden for __hash__ + # See certbot-apache/src/certbot._internal.plugins.apache/obj.py for more information + return super().__hash__() + + def super_eq(self, other: "Addr") -> bool: + """Check ip/port equality, with IPv6 support. + """ + # If both addresses got an unspecified address, then make sure the + # host representation in each match when doing the comparison. + if self.unspecified_address and other.unspecified_address: + return common.Addr((self.CANONICAL_UNSPECIFIED_ADDRESS, + self.tup[1]), self.ipv6) == \ + common.Addr((other.CANONICAL_UNSPECIFIED_ADDRESS, + other.tup[1]), other.ipv6) + return super().__eq__(other) + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return (self.super_eq(other) and + self.ssl == other.ssl and + self.default == other.default) + return False + + +class VirtualHost: + """Represents an Nginx Virtualhost. + + :ivar str filep: file path of VH + :ivar set addrs: Virtual Host addresses (:class:`set` of :class:`Addr`) + :ivar set names: Server names/aliases of vhost + (:class:`list` of :class:`str`) + :ivar list raw: The raw form of the parsed server block + + :ivar bool ssl: SSLEngine on in vhost + :ivar bool enabled: Virtual host is enabled + :ivar list path: The indices into the parsed file used to access + the server block defining the vhost + + """ + + def __init__(self, filep: str, addrs: Sequence[Addr], ssl: bool, enabled: bool, + names: set[str], raw: list[Any], path: list[int]) -> None: + """Initialize a VH.""" + self.filep = filep + self.addrs = addrs + self.names = names + self.ssl = ssl + self.enabled = enabled + self.raw = raw + self.path = path + + def __str__(self) -> str: + addr_str = ", ".join(str(addr) for addr in sorted(self.addrs, key=str)) + # names might be a set, and it has different representations in Python + # 2 and 3. Force it to be a list here for consistent outputs + return ("file: %s\n" + "addrs: %s\n" + "names: %s\n" + "ssl: %s\n" + "enabled: %s" % (self.filep, addr_str, + list(self.names), self.ssl, self.enabled)) + + def __repr__(self) -> str: + return "VirtualHost(" + self.__str__().replace("\n", ", ") + ")\n" + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + return (self.filep == other.filep and + sorted(self.addrs, key=str) == sorted(other.addrs, key=str) and + self.names == other.names and + self.ssl == other.ssl and + self.enabled == other.enabled and + self.path == other.path) + + return False + + def __hash__(self) -> int: + return hash((self.filep, tuple(self.path), + tuple(self.addrs), tuple(self.names), + self.ssl, self.enabled)) + + def has_header(self, header_name: str) -> bool: + """Determine if this server block has a particular header set. + :param str header_name: The name of the header to check for, e.g. + 'Strict-Transport-Security' + """ + found = _find_directive(self.raw, ADD_HEADER_DIRECTIVE, header_name) + return found is not None + + def contains_list(self, test: list[Any]) -> bool: + """Determine if raw server block contains test list at top level + """ + for i in range(0, len(self.raw) - len(test) + 1): + if self.raw[i:i + len(test)] == test: + return True + return False + + def ipv6_enabled(self) -> bool: + """Return true if one or more of the listen directives in vhost supports + IPv6""" + for a in self.addrs: + if a.ipv6: + return True + return False + + def ipv4_enabled(self) -> bool: + """Return true if one or more of the listen directives in vhost are IPv4 + only""" + if not self.addrs: + return True + for a in self.addrs: + if not a.ipv6: + return True + return False + + def display_repr(self) -> str: + """Return a representation of VHost to be used in dialog""" + return ( + "File: {filename}\n" + "Addresses: {addrs}\n" + "Names: {names}\n" + "HTTPS: {https}\n".format( + filename=self.filep, + addrs=", ".join(str(addr) for addr in self.addrs), + names=", ".join(self.names), + https="Yes" if self.ssl else "No")) + + +def _find_directive(directives: Optional[Union[str, list[Any]]], directive_name: str, + match_content: Optional[Any] = None) -> Optional[Any]: + """Find a directive of type directive_name in directives. If match_content is given, + Searches for `match_content` in the directive arguments. + """ + if not directives or isinstance(directives, str): + return None + + # If match_content is None, just match on directive type. Otherwise, match on + # both directive type -and- the content! + if directives[0] == directive_name and \ + (match_content is None or match_content in directives): + return directives + + matches = (_find_directive(line, directive_name, match_content) for line in directives) + return next((m for m in matches if m is not None), None) diff --git a/certbot/src/certbot/_internal/plugins/nginx/parser.py b/certbot/src/certbot/_internal/plugins/nginx/parser.py new file mode 100644 index 00000000000..5c9e7e6ac84 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/parser.py @@ -0,0 +1,836 @@ +"""NginxParser is a member object of the NginxConfigurator class.""" +from __future__ import annotations +import copy +import functools +import glob +import logging +import re +from typing import Any +from typing import Callable +from typing import cast +from typing import Iterable +from typing import Mapping +from typing import Optional +from typing import Sequence +from typing import Union + +import pyparsing + +from certbot import errors +from certbot.compat import os +from certbot._internal.plugins.nginx import nginxparser +from certbot._internal.plugins.nginx import obj +from certbot._internal.plugins.nginx.nginxparser import UnspacedList + +logger = logging.getLogger(__name__) + + +class NginxParser: + """Class handles the fine details of parsing the Nginx Configuration. + + :ivar str root: Normalized absolute path to the server root + directory. Without trailing slash. + :ivar dict parsed: Mapping of file paths to parsed trees + + """ + + def __init__(self, root: str) -> None: + self.parsed: dict[str, UnspacedList] = {} + self.root = os.path.abspath(root) + self.config_root = self._find_config_root() + self._http_path: str | None = None + + # Parse nginx.conf and included files. + # TODO: Check sites-available/ as well. For now, the configurator does + # not enable sites from there. + self.load() + + def load(self) -> None: + """Loads Nginx files into a parsed tree. + + """ + self.parsed = {} + self._parse_recursively(self.config_root) + + @property + def http_path(self) -> str: + """ Filepath of file containing nginx http block. Set in self._parse_recursively + """ + if self._http_path is None: + raise errors.MisconfigurationError('No nginx http block found') + return self._http_path + + def _parse_recursively(self, filepath: str) -> None: + """Parses nginx config files recursively by looking at 'include' + directives inside 'http' and 'server' blocks. Note that this only + reads Nginx files that potentially declare a virtual host. + + :param str filepath: The path to the files to parse, as a glob + + """ + # pylint: disable=too-many-nested-blocks + filepath = self.abs_path(filepath) + trees: dict[str, UnspacedList] = self._parse_files(filepath) + for filename, tree in trees.items(): + for entry in tree: + if _is_include_directive(entry): + # Parse the top-level included file + self._parse_recursively(entry[1]) + elif entry[0] == ['http'] or entry[0] == ['server']: + # Note http block location for http_01.py + if entry[0] == ['http']: + self._http_path = filename + # Look for includes in the top-level 'http'/'server' context + for subentry in entry[1]: + if _is_include_directive(subentry): + self._parse_recursively(subentry[1]) + elif entry[0] == ['http'] and subentry[0] == ['server']: + # Look for includes in a 'server' context within + # an 'http' context + for server_entry in subentry[1]: + if _is_include_directive(server_entry): + self._parse_recursively(server_entry[1]) + + def abs_path(self, path: str) -> str: + """Converts a relative path to an absolute path relative to the root. + Does nothing for paths that are already absolute. + + :param str path: The path + :returns: The absolute path + :rtype: str + + """ + if not os.path.isabs(path): + return os.path.normpath(os.path.join(self.root, path)) + return os.path.normpath(path) + + def _build_addr_to_ssl(self) -> dict[tuple[str, str], bool]: + """Builds a map from address to whether it listens on ssl in any server block + """ + servers = self._get_raw_servers() + + addr_to_ssl: dict[tuple[str, str], bool] = {} + for server_list in servers.values(): + for server, _ in server_list: + # Parse the server block to save addr info + parsed_server = _parse_server_raw(server) + for addr in parsed_server['addrs']: + addr_tuple = addr.normalized_tuple() + if addr_tuple not in addr_to_ssl: + addr_to_ssl[addr_tuple] = addr.ssl + addr_to_ssl[addr_tuple] = addr.ssl or addr_to_ssl[addr_tuple] + return addr_to_ssl + + def _get_raw_servers(self) -> dict[str, Union[list[Any], UnspacedList]]: + # pylint: disable=cell-var-from-loop + """Get a map of unparsed all server blocks + """ + servers: dict[str, Union[list[Any], nginxparser.UnspacedList]] = {} + for filename, tree in self.parsed.items(): + servers[filename] = [] + srv = servers[filename] # workaround undefined loop var in lambdas + + # Find all the server blocks + _do_for_subarray(tree, lambda x: len(x) >= 2 and x[0] == ['server'], + lambda x, y: srv.append((x[1], y))) + + # Find 'include' statements in server blocks and append their trees + for i, (server, path) in enumerate(servers[filename]): + new_server = self._get_included_directives(server) + servers[filename][i] = (new_server, path) + return servers + + def get_vhosts(self) -> list[obj.VirtualHost]: + """Gets list of all 'virtual hosts' found in Nginx configuration. + Technically this is a misnomer because Nginx does not have virtual + hosts, it has 'server blocks'. + + :returns: List of :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + objects found in configuration + :rtype: list + + """ + enabled = True # We only look at enabled vhosts for now + servers = self._get_raw_servers() + + vhosts = [] + for filename, server_list in servers.items(): + for server, path in server_list: + # Parse the server block into a VirtualHost object + + parsed_server = _parse_server_raw(server) + vhost = obj.VirtualHost(filename, + parsed_server['addrs'], + parsed_server['ssl'], + enabled, + parsed_server['names'], + server, + path) + vhosts.append(vhost) + + self._update_vhosts_addrs_ssl(vhosts) + + return vhosts + + def _update_vhosts_addrs_ssl(self, vhosts: Iterable[obj.VirtualHost]) -> None: + """Update a list of raw parsed vhosts to include global address sslishness + """ + addr_to_ssl = self._build_addr_to_ssl() + for vhost in vhosts: + for addr in vhost.addrs: + addr.ssl = addr_to_ssl[addr.normalized_tuple()] + if addr.ssl: + vhost.ssl = True + + def _get_included_directives(self, block: UnspacedList) -> UnspacedList: + """Returns array with the "include" directives expanded out by + concatenating the contents of the included file to the block. + + :param list block: + :rtype: list + + """ + result = copy.deepcopy(block) # Copy the list to keep self.parsed idempotent + for directive in block: + if _is_include_directive(directive): + included_files = glob.glob( + self.abs_path(directive[1])) + for incl in included_files: + try: + result.extend(self.parsed[incl]) + except KeyError: + pass + return result + + def _parse_files(self, filepath: str, override: bool = False) -> dict[str, UnspacedList]: + """Parse files from a glob + + :param str filepath: Nginx config file path + :param bool override: Whether to parse a file that has been parsed + :returns: dict of parsed tree structures indexed by filename + :rtype: dict[str, UnspacedList] + + """ + files: list[str] = glob.glob(filepath) # nginx on unix calls glob(3) for this + # XXX Windows nginx uses FindFirstFile, and + # should have a narrower call here + trees: dict[str, UnspacedList] = {} + for filename in files: + if filename in self.parsed and not override: + continue + try: + with open(filename, "r", encoding="utf-8") as _file: + parsed = nginxparser.load(_file) + self.parsed[filename] = parsed + trees[filename] = parsed + except OSError: + logger.warning("Could not open file: %s", filename) + except UnicodeDecodeError: + logger.warning("Could not read file: %s due to invalid " + "character. Only UTF-8 encoding is " + "supported.", filename) + except pyparsing.ParseException: + logger.warning("Could not parse file: %s. This is usually due to a comment that " + "certbot cannot parse, such as between a block's name and definition or " + "within a string literal. Moving the comment to another location in the file " + "or deleting it may resolve the issue.", filename) + return trees + + def _find_config_root(self) -> str: + """Return the Nginx Configuration Root file.""" + location = ['nginx.conf'] + + for name in location: + if os.path.isfile(os.path.join(self.root, name)): + return os.path.join(self.root, name) + + raise errors.NoInstallationError( + "Could not find Nginx root configuration file (nginx.conf)") + + def filedump(self, ext: str = 'tmp', lazy: bool = True) -> None: + """Dumps parsed configurations into files. + + :param str ext: The file extension to use for the dumped files. If + empty, this overrides the existing conf files. + :param bool lazy: Only write files that have been modified + + """ + # Best-effort atomicity is enforced above us by reverter.py + for filename, tree in self.parsed.items(): + if ext: + filename = filename + os.path.extsep + ext + if not isinstance(tree, UnspacedList): + raise ValueError(f"Error tree {tree} is not an UnspacedList") + try: + if lazy and not tree.is_dirty(): + continue + out = nginxparser.dumps(tree) + logger.debug('Writing nginx conf tree to %s:\n%s', filename, out) + with open(filename, 'w', encoding='utf-8') as _file: + _file.write(out) + + except OSError: + logger.error("Could not open file for writing: %s", filename) + + def parse_server(self, server: UnspacedList) -> dict[str, Any]: + """Parses a list of server directives, accounting for global address sslishness. + + :param list server: list of directives in a server block + :rtype: dict + """ + addr_to_ssl = self._build_addr_to_ssl() + parsed_server = _parse_server_raw(server) + _apply_global_addr_ssl(addr_to_ssl, parsed_server) + return parsed_server + + def has_ssl_on_directive(self, vhost: obj.VirtualHost) -> bool: + """Does vhost have ssl on for all ports? + + :param :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + vhost: The vhost in question + + :returns: True if 'ssl on' directive is included + :rtype: bool + + """ + server = vhost.raw + for directive in server: + if not directive: + continue + if _is_ssl_on_directive(directive): + return True + + return False + + def add_server_directives(self, vhost: obj.VirtualHost, directives: list[Any], + insert_at_top: bool = False) -> None: + """Add directives to the server block identified by vhost. + + This method modifies vhost to be fully consistent with the new directives. + + ..note :: It's an error to try and add a nonrepeatable directive that already + exists in the config block with a conflicting value. + + ..todo :: Doesn't match server blocks whose server_name directives are + split across multiple conf files. + + :param :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` vhost: The vhost + whose information we use to match on + :param list directives: The directives to add + :param bool insert_at_top: True if the directives need to be inserted at the top + of the server block instead of the bottom + + """ + self._modify_server_directives(vhost, + functools.partial(_add_directives, directives, insert_at_top)) + + def update_or_add_server_directives(self, vhost: obj.VirtualHost, directives: list[Any], + insert_at_top: bool = False) -> None: + """Add or replace directives in the server block identified by vhost. + + This method modifies vhost to be fully consistent with the new directives. + + ..note :: When a directive with the same name already exists in the + config block, the first instance will be replaced. Otherwise, the directive + will be appended/prepended to the config block as in add_server_directives. + + ..todo :: Doesn't match server blocks whose server_name directives are + split across multiple conf files. + + :param :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` vhost: The vhost + whose information we use to match on + :param list directives: The directives to add + :param bool insert_at_top: True if the directives need to be inserted at the top + of the server block instead of the bottom + + """ + self._modify_server_directives(vhost, + functools.partial(_update_or_add_directives, directives, insert_at_top)) + + def remove_server_directives(self, vhost: obj.VirtualHost, directive_name: str, + match_func: Optional[Callable[[Any], bool]] = None) -> None: + """Remove all directives of type directive_name. + + :param :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` vhost: The vhost + to remove directives from + :param string directive_name: The directive type to remove + :param callable match_func: Function of the directive that returns true for directives + to be deleted. + """ + self._modify_server_directives(vhost, + functools.partial(_remove_directives, directive_name, match_func)) + + def _update_vhost_based_on_new_directives(self, vhost: obj.VirtualHost, + directives_list: UnspacedList) -> None: + new_server = self._get_included_directives(directives_list) + parsed_server = self.parse_server(new_server) + vhost.addrs = parsed_server['addrs'] + vhost.ssl = parsed_server['ssl'] + vhost.names = parsed_server['names'] + vhost.raw = new_server + + def _modify_server_directives(self, vhost: obj.VirtualHost, + block_func: Callable[[list[Any]], None]) -> None: + filename = vhost.filep + try: + result = self.parsed[filename] + for index in vhost.path: + result = result[index] + if not isinstance(result, list) or len(result) != 2: + raise errors.MisconfigurationError("Not a server block.") + result = result[1] + block_func(result) + + self._update_vhost_based_on_new_directives(vhost, result) + except errors.MisconfigurationError as err: + raise errors.MisconfigurationError("Problem in %s: %s" % (filename, str(err))) + + def duplicate_vhost(self, vhost_template: obj.VirtualHost, + remove_singleton_listen_params: bool = False, + only_directives: Optional[list[Any]] = None) -> obj.VirtualHost: + """Duplicate the vhost in the configuration files. + + :param :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` vhost_template: The vhost + whose information we copy + :param bool remove_singleton_listen_params: If we should remove parameters + from listen directives in the block that can only be used once per address + :param list only_directives: If it exists, only duplicate the named directives. Only + looks at first level of depth; does not expand includes. + + :returns: A vhost object for the newly created vhost + :rtype: :class:`~certbot._internal.plugins.nginx.obj.VirtualHost` + """ + # TODO: https://github.com/certbot/certbot/issues/5185 + # put it in the same file as the template, at the same level + new_vhost = copy.deepcopy(vhost_template) + + enclosing_block = self.parsed[vhost_template.filep] + for index in vhost_template.path[:-1]: + enclosing_block = enclosing_block[index] + raw_in_parsed = copy.deepcopy(enclosing_block[vhost_template.path[-1]]) + + if only_directives is not None: + new_directives = nginxparser.UnspacedList([]) + for directive in raw_in_parsed[1]: + if directive and directive[0] in only_directives: + new_directives.append(directive) + raw_in_parsed[1] = new_directives + + self._update_vhost_based_on_new_directives(new_vhost, new_directives) + + enclosing_block.append(raw_in_parsed) + new_vhost.path[-1] = len(enclosing_block) - 1 + if remove_singleton_listen_params: + for addr in new_vhost.addrs: + addr.default = False + addr.ipv6only = False + for directive in enclosing_block[new_vhost.path[-1]][1]: + if directive and directive[0] == 'listen': + # Exclude one-time use parameters which will cause an error if repeated. + # https://nginx.org/en/docs/http/ngx_http_core_module.html#listen + exclude = {'default_server', 'default', 'setfib', 'fastopen', 'backlog', + 'rcvbuf', 'sndbuf', 'accept_filter', 'deferred', 'bind', + 'ipv6only', 'reuseport', 'so_keepalive'} + + for param in exclude: + # See: github.com/certbot/certbot/pull/6223#pullrequestreview-143019225 + keys = [x.split('=')[0] for x in directive] + if param in keys: + del directive[keys.index(param)] + return new_vhost + + +def _parse_ssl_options(ssl_options: Optional[str]) -> list[UnspacedList]: + if ssl_options is not None: + try: + with open(ssl_options, "r", encoding="utf-8") as _file: + return nginxparser.load(_file) + except OSError: + logger.warning("Missing NGINX TLS options file: %s", ssl_options) + except UnicodeDecodeError: + logger.warning("Could not read file: %s due to invalid character. " + "Only UTF-8 encoding is supported.", ssl_options) + except pyparsing.ParseBaseException as err: + logger.warning("Could not parse file: %s due to %s", ssl_options, err) + return UnspacedList([]) + + +def _do_for_subarray(entry: list[Any], condition: Callable[[list[Any]], bool], + func: Callable[[list[Any], list[int]], None], + path: Optional[list[int]] = None) -> None: + """Executes a function for a subarray of a nested array if it matches + the given condition. + + :param list entry: The list to iterate over + :param function condition: Returns true iff func should be executed on item + :param function func: The function to call for each matching item + + """ + if path is None: + path = [] + if isinstance(entry, list): + if condition(entry): + func(entry, path) + else: + for index, item in enumerate(entry): + _do_for_subarray(item, condition, func, path + [index]) + + +def get_best_match(target_name: str, names: Iterable[str]) -> tuple[Optional[str], Optional[str]]: + """Finds the best match for target_name out of names using the Nginx + name-matching rules (exact > longest wildcard starting with * > + longest wildcard ending with * > regex). + + :param str target_name: The name to match + :param set names: The candidate server names + :returns: Tuple of (type of match, the name that matched) + :rtype: tuple + + """ + exact = [] + wildcard_start = [] + wildcard_end = [] + regex = [] + + for name in names: + if _exact_match(target_name, name): + exact.append(name) + elif _wildcard_match(target_name, name, True): + wildcard_start.append(name) + elif _wildcard_match(target_name, name, False): + wildcard_end.append(name) + elif _regex_match(target_name, name): + regex.append(name) + + if exact: + # There can be more than one exact match; e.g. eff.org, .eff.org + match = min(exact, key=len) + return 'exact', match + if wildcard_start: + # Return the longest wildcard + match = max(wildcard_start, key=len) + return 'wildcard_start', match + if wildcard_end: + # Return the longest wildcard + match = max(wildcard_end, key=len) + return 'wildcard_end', match + if regex: + # Just return the first one for now + match = regex[0] + return 'regex', match + + return None, None + + +def _exact_match(target_name: str, name: str) -> bool: + target_lower = target_name.lower() + return name.lower() in (target_lower, '.' + target_lower) + + +def _wildcard_match(target_name: str, name: str, start: bool) -> bool: + # Degenerate case + if name == '*': + return True + + parts = target_name.split('.') + match_parts = name.split('.') + + # If the domain ends in a wildcard, do the match procedure in reverse + if not start: + parts.reverse() + match_parts.reverse() + + # The first part must be a wildcard or blank, e.g. '.eff.org' + first = match_parts.pop(0) + if first not in ('*', ''): + return False + + target_name_lower = '.'.join(parts).lower() + name_lower = '.'.join(match_parts).lower() + + # Ex: www.eff.org matches *.eff.org, eff.org does not match *.eff.org + return target_name_lower.endswith('.' + name_lower) + + +def _regex_match(target_name: str, name: str) -> bool: + # Must start with a tilde + if len(name) < 2 or name[0] != '~': + return False + + # After tilde is a perl-compatible regex + try: + regex = re.compile(name[1:]) + return bool(re.match(regex, target_name)) + except re.error: # pragma: no cover + # perl-compatible regexes are sometimes not recognized by python + return False + + +def _is_include_directive(entry: Any) -> bool: + """Checks if an nginx parsed entry is an 'include' directive. + + :param list entry: the parsed entry + :returns: Whether it's an 'include' directive + :rtype: bool + + """ + return (isinstance(entry, list) and + len(entry) == 2 and entry[0] == 'include' and + isinstance(entry[1], str)) + + +def _is_ssl_on_directive(entry: Any) -> bool: + """Checks if an nginx parsed entry is an 'ssl on' directive. + + :param list entry: the parsed entry + :returns: Whether it's an 'ssl on' directive + :rtype: bool + + """ + return (isinstance(entry, list) and + len(entry) == 2 and entry[0] == 'ssl' and + entry[1] == 'on') + + +def _add_directives(directives: list[Any], insert_at_top: bool, + block: UnspacedList) -> None: + """Adds directives to a config block.""" + for directive in directives: + _add_directive(block, directive, insert_at_top) + if block and '\n' not in block[-1]: # could be " \n " or ["\n"] ! + block.append(nginxparser.UnspacedList('\n')) + + +def _update_or_add_directives(directives: list[Any], insert_at_top: bool, + block: UnspacedList) -> None: + """Adds or replaces directives in a config block.""" + for directive in directives: + _update_or_add_directive(block, directive, insert_at_top) + if block and '\n' not in block[-1]: # could be " \n " or ["\n"] ! + block.append(nginxparser.UnspacedList('\n')) + + +INCLUDE = 'include' +REPEATABLE_DIRECTIVES = {'server_name', 'listen', INCLUDE, 'rewrite', 'add_header'} +COMMENT = ' managed by Certbot' +COMMENT_BLOCK = [' ', '#', COMMENT] + + +def comment_directive(block: UnspacedList, location: int) -> None: + """Add a ``#managed by Certbot`` comment to the end of the line at location. + + :param list block: The block containing the directive to be commented + :param int location: The location within ``block`` of the directive to be commented + """ + next_entry = block[location + 1] if location + 1 < len(block) else None + if isinstance(next_entry, list) and next_entry: + if len(next_entry) >= 2 and next_entry[-2] == "#" and COMMENT in next_entry[-1]: + return + if isinstance(next_entry, nginxparser.UnspacedList): + next_entry = next_entry.spaced[0] + else: + next_entry = next_entry[0] + + block.insert(location + 1, COMMENT_BLOCK[:]) + if next_entry is not None and "\n" not in next_entry: + block.insert(location + 2, '\n') + + +def _comment_out_directive(block: UnspacedList, location: int, include_location: str) -> None: + """Comment out the line at location, with a note of explanation.""" + comment_message = ' duplicated in {0}'.format(include_location) + # add the end comment + # create a dumpable object out of block[location] (so it includes the ;) + directive = block[location] + new_dir_block = nginxparser.UnspacedList([]) # just a wrapper + new_dir_block.append(directive) + dumped = nginxparser.dumps(new_dir_block) + commented = dumped + ' #' + comment_message # add the comment directly to the one-line string + new_dir = nginxparser.loads(commented) # reload into UnspacedList + + # add the beginning comment + insert_location = 0 + if new_dir[0].spaced[0] != new_dir[0][0]: # if there's whitespace at the beginning + insert_location = 1 + new_dir[0].spaced.insert(insert_location, "# ") # comment out the line + new_dir[0].spaced.append(";") # directly add in the ;, because now dumping won't work properly + dumped = nginxparser.dumps(new_dir) + new_dir = nginxparser.loads(dumped) # reload into an UnspacedList + + block[location] = new_dir[0] # set the now-single-line-comment directive back in place + + +def _find_location(block: UnspacedList, directive_name: str, + match_func: Optional[Callable[[Any], bool]] = None) -> Optional[int]: + """Finds the index of the first instance of directive_name in block. + If no line exists, use None.""" + return next((index for index, line in enumerate(block) if ( + line and line[0] == directive_name and (match_func is None or match_func(line)))), None) + + +def _is_whitespace_or_comment(directive: Sequence[Any]) -> bool: + """Is this directive either a whitespace or comment directive?""" + return len(directive) == 0 or directive[0] == '#' + + +def _add_directive(block: UnspacedList, directive: Sequence[Any], insert_at_top: bool) -> None: + if not isinstance(directive, nginxparser.UnspacedList): + directive = nginxparser.UnspacedList(directive) + if _is_whitespace_or_comment(directive): + # whitespace or comment + block.append(directive) + return + + location = _find_location(block, directive[0]) + + # Append or prepend directive. Fail if the name is not a repeatable directive name, + # and there is already a copy of that directive with a different value + # in the config file. + + # handle flat include files + + directive_name = directive[0] + + def can_append(loc: Optional[int], dir_name: str) -> bool: + """ Can we append this directive to the block? """ + return loc is None or (isinstance(dir_name, str) + and dir_name in REPEATABLE_DIRECTIVES) + + err_fmt = 'tried to insert directive "{0}" but found conflicting "{1}".' + + # Give a better error message about the specific directive than Nginx's "fail to restart" + if directive_name == INCLUDE: + # in theory, we might want to do this recursively, but in practice, that's really not + # necessary because we know what file we're talking about (and if we don't recurse, we + # just give a worse error message) + included_directives = _parse_ssl_options(directive[1]) + + for included_directive in included_directives: + included_dir_loc = _find_location(block, included_directive[0]) + included_dir_name = included_directive[0] + if (not _is_whitespace_or_comment(included_directive) + and not can_append(included_dir_loc, included_dir_name)): + + # By construction of can_append(), included_dir_loc cannot be None at that point + resolved_included_dir_loc = cast(int, included_dir_loc) + + if block[resolved_included_dir_loc] != included_directive: + raise errors.MisconfigurationError(err_fmt.format( + included_directive, block[resolved_included_dir_loc])) + _comment_out_directive(block, resolved_included_dir_loc, directive[1]) + + if can_append(location, directive_name): + if insert_at_top: + # Add a newline so the comment doesn't comment + # out existing directives + block.insert(0, nginxparser.UnspacedList('\n')) + block.insert(0, directive) + comment_directive(block, 0) + else: + block.append(directive) + comment_directive(block, len(block) - 1) + return + + # By construction of can_append(), location cannot be None at that point + resolved_location = cast(int, location) + + if block[resolved_location] != directive: + raise errors.MisconfigurationError(err_fmt.format(directive, block[resolved_location])) + + +def _update_directive(block: UnspacedList, directive: Sequence[Any], location: int) -> None: + block[location] = directive + comment_directive(block, location) + + +def _update_or_add_directive(block: UnspacedList, directive: Sequence[Any], + insert_at_top: bool) -> None: + if not isinstance(directive, nginxparser.UnspacedList): + directive = nginxparser.UnspacedList(directive) + if _is_whitespace_or_comment(directive): + # whitespace or comment + block.append(directive) + return + + location = _find_location(block, directive[0]) + + # we can update directive + if location is not None: + _update_directive(block, directive, location) + return + + _add_directive(block, directive, insert_at_top) + + +def _is_certbot_comment(directive: Sequence[Any]) -> bool: + return '#' in directive and COMMENT in directive + + +def _remove_directives(directive_name: str, match_func: Callable[[Any], bool], + block: UnspacedList) -> None: + """Removes directives of name directive_name from a config block if match_func matches. + """ + while True: + location = _find_location(block, directive_name, match_func=match_func) + if location is None: + return + # if the directive was made by us, remove the comment following + if location + 1 < len(block) and _is_certbot_comment(block[location + 1]): + del block[location + 1] + del block[location] + + +def _apply_global_addr_ssl(addr_to_ssl: Mapping[tuple[str, str], bool], + parsed_server: dict[str, Any]) -> None: + """Apply global sslishness information to the parsed server block + """ + for addr in parsed_server['addrs']: + addr.ssl = addr_to_ssl[addr.normalized_tuple()] + if addr.ssl: + parsed_server['ssl'] = True + + +def _parse_server_raw(server: UnspacedList) -> dict[str, Any]: + """Parses a list of server directives. + + :param list server: list of directives in a server block + :rtype: dict + + """ + addrs: set[obj.Addr] = set() + ssl: bool = False + names: set[str] = set() + + apply_ssl_to_all_addrs = False + + for directive in server: + if not directive: + continue + if directive[0] == 'listen': + try: + addr = obj.Addr.fromstring(" ".join(directive[1:])) + except obj.SocketAddrError: + # Ignore UNIX-domain socket addresses + continue + addrs.add(addr) + if addr.ssl: + ssl = True + elif directive[0] == 'server_name': + params = directive[1:] + while '#' in params: + end_index = [i for i, param in enumerate(params) if param.startswith('\n')][0] + params = params[:params.index('#')] + params[end_index+1:] + names.update(x.strip('"\'') for x in params) + elif _is_ssl_on_directive(directive): + ssl = True + apply_ssl_to_all_addrs = True + + if apply_ssl_to_all_addrs: + for addr in addrs: + addr.ssl = True + + return { + 'addrs': addrs, + 'ssl': ssl, + 'names': names + } diff --git a/certbot/src/certbot/_internal/plugins/nginx/parser_obj.py b/certbot/src/certbot/_internal/plugins/nginx/parser_obj.py new file mode 100644 index 00000000000..acd80da58c9 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/parser_obj.py @@ -0,0 +1,408 @@ +# type: ignore +# This module is not used for now, so we just skip type checking for the sake +# of simplicity. +""" This file contains parsing routines and object classes to help derive meaning from +raw lists of tokens from pyparsing. """ + +import abc +import logging +from typing import Any +from typing import Callable +from typing import Iterator +from typing import Optional +from typing import Sequence + +from certbot import errors + +logger = logging.getLogger(__name__) +COMMENT = " managed by Certbot" +COMMENT_BLOCK = ["#", COMMENT] + + +class Parsable: + """ Abstract base class for "Parsable" objects whose underlying representation + is a tree of lists. + + :param .Parsable parent: This object's parsed parent in the tree + """ + + __metaclass__ = abc.ABCMeta + + def __init__(self, parent: Optional["Parsable"] = None): + self._data: list[Any] = [] + self._tabs = None + self.parent = parent + + @classmethod + def parsing_hooks(cls) -> tuple[type["Block"], type["Sentence"], type["Statements"]]: + """Returns object types that this class should be able to `parse` recursively. + The order of the objects indicates the order in which the parser should + try to parse each subitem. + :returns: A list of Parsable classes. + :rtype list: + """ + return Block, Sentence, Statements + + @staticmethod + @abc.abstractmethod + def should_parse(lists: Any) -> bool: + """ Returns whether the contents of `lists` can be parsed into this object. + + :returns: Whether `lists` can be parsed as this object. + :rtype bool: + """ + raise NotImplementedError() + + @abc.abstractmethod + def parse(self, raw_list: list[Any], add_spaces: bool = False) -> None: + """ Loads information into this object from underlying raw_list structure. + Each Parsable object might make different assumptions about the structure of + raw_list. + + :param list raw_list: A list or sublist of tokens from pyparsing, containing whitespace + as separate tokens. + :param bool add_spaces: If set, the method can and should manipulate and insert spacing + between non-whitespace tokens and lists to delimit them. + :raises .errors.MisconfigurationError: when the assumptions about the structure of + raw_list are not met. + """ + raise NotImplementedError() + + @abc.abstractmethod + def iterate(self, expanded: bool = False, + match: Optional[Callable[["Parsable"], bool]] = None) -> Iterator[Any]: + """ Iterates across this object. If this object is a leaf object, only yields + itself. If it contains references other parsing objects, and `expanded` is set, + this function should first yield itself, then recursively iterate across all of them. + :param bool expanded: Whether to recursively iterate on possible children. + :param callable match: If provided, an object is only iterated if this callable + returns True when called on that object. + + :returns: Iterator over desired objects. + """ + raise NotImplementedError() + + @abc.abstractmethod + def get_tabs(self) -> str: + """ Guess at the tabbing style of this parsed object, based on whitespace. + + If this object is a leaf, it deducts the tabbing based on its own contents. + Other objects may guess by calling `get_tabs` recursively on child objects. + + :returns: Guess at tabbing for this object. Should only return whitespace strings + that does not contain newlines. + :rtype str: + """ + raise NotImplementedError() + + @abc.abstractmethod + def set_tabs(self, tabs: str = " ") -> None: + """This tries to set and alter the tabbing of the current object to a desired + whitespace string. Primarily meant for objects that were constructed, so they + can conform to surrounding whitespace. + + :param str tabs: A whitespace string (not containing newlines). + """ + raise NotImplementedError() + + def dump(self, include_spaces: bool = False) -> list[Any]: + """ Dumps back to pyparsing-like list tree. The opposite of `parse`. + + Note: if this object has not been modified, `dump` with `include_spaces=True` + should always return the original input of `parse`. + + :param bool include_spaces: If set to False, magically hides whitespace tokens from + dumped output. + + :returns: Pyparsing-like list tree. + :rtype list: + """ + return [elem.dump(include_spaces) for elem in self._data] + + +class Statements(Parsable): + """ A group or list of "Statements". A Statement is either a Block or a Sentence. + + The underlying representation is simply a list of these Statement objects, with + an extra `_trailing_whitespace` string to keep track of the whitespace that does not + precede any more statements. + """ + def __init__(self, parent: Optional[Parsable] = None): + super().__init__(parent) + self._trailing_whitespace = None + + # ======== Begin overridden functions + + @staticmethod + def should_parse(lists: Any) -> bool: + return isinstance(lists, list) + + def set_tabs(self, tabs: str = " ") -> None: + """ Sets the tabbing for this set of statements. Does this by calling `set_tabs` + on each of the child statements. + + Then, if a parent is present, sets trailing whitespace to parent tabbing. This + is so that the trailing } of any Block that contains Statements lines up + with parent tabbing. + """ + for statement in self._data: + statement.set_tabs(tabs) + if self.parent is not None: + self._trailing_whitespace = "\n" + self.parent.get_tabs() + + def parse(self, raw_list: list[Any], add_spaces: bool = False) -> None: + """ Parses a list of statements. + Expects all elements in `raw_list` to be parseable by `type(self).parsing_hooks`, + with an optional whitespace string at the last index of `raw_list`. + """ + if not isinstance(raw_list, list): + raise errors.MisconfigurationError("Statements parsing expects a list!") + # If there's a trailing whitespace in the list of statements, keep track of it. + if raw_list and isinstance(raw_list[-1], str) and raw_list[-1].isspace(): + self._trailing_whitespace = raw_list[-1] + raw_list = raw_list[:-1] + self._data = [parse_raw(elem, self, add_spaces) for elem in raw_list] + + def get_tabs(self) -> str: + """ Takes a guess at the tabbing of all contained Statements by retrieving the + tabbing of the first Statement.""" + if self._data: + return self._data[0].get_tabs() + return "" + + def dump(self, include_spaces: bool = False) -> list[Any]: + """ Dumps this object by first dumping each statement, then appending its + trailing whitespace (if `include_spaces` is set) """ + data = super().dump(include_spaces) + if include_spaces and self._trailing_whitespace is not None: + return data + [self._trailing_whitespace] + return data + + def iterate(self, expanded: bool = False, + match: Optional[Callable[["Parsable"], bool]] = None) -> Iterator[Any]: + """ Combines each statement's iterator. """ + for elem in self._data: + yield from elem.iterate(expanded, match) + + # ======== End overridden functions + + +def _space_list(list_: Sequence[Any]) -> list[str]: + """ Inserts whitespace between adjacent non-whitespace tokens. """ + spaced_statement: list[str] = [] + for i in reversed(range(len(list_))): + spaced_statement.insert(0, list_[i]) + if i > 0 and not list_[i].isspace() and not list_[i-1].isspace(): + spaced_statement.insert(0, " ") + return spaced_statement + + +class Sentence(Parsable): + """ A list of words. Non-whitespace words are typically separated with whitespace tokens. """ + + # ======== Begin overridden functions + + @staticmethod + def should_parse(lists: Any) -> bool: + """ Returns True if `lists` can be parseable as a `Sentence`-- that is, + every element is a string type. + + :param list lists: The raw unparsed list to check. + + :returns: whether this lists is parseable by `Sentence`. + """ + return (isinstance(lists, list) and len(lists) > 0 and + all(isinstance(elem, str) for elem in lists)) + + def parse(self, raw_list: list[Any], add_spaces: bool = False) -> None: + """ Parses a list of string types into this object. + If add_spaces is set, adds whitespace tokens between adjacent non-whitespace tokens.""" + if add_spaces: + raw_list = _space_list(raw_list) + if (not isinstance(raw_list, list) + or any(not isinstance(elem, str) for elem in raw_list)): + raise errors.MisconfigurationError("Sentence parsing expects a list of string types.") + self._data = raw_list + + def iterate(self, expanded: bool = False, + match: Optional[Callable[[Parsable], bool]] = None) -> Iterator[Any]: + """ Simply yields itself. """ + if match is None or match(self): + yield self + + def set_tabs(self, tabs: str = " ") -> None: + """ Sets the tabbing on this sentence. Inserts a newline and `tabs` at the + beginning of `self._data`. """ + if self._data[0].isspace(): + return + self._data.insert(0, "\n" + tabs) + + def dump(self, include_spaces: bool = False) -> list[Any]: + """ Dumps this sentence. If include_spaces is set, includes whitespace tokens.""" + if not include_spaces: + return self.words + return self._data + + def get_tabs(self) -> str: + """ Guesses at the tabbing of this sentence. If the first element is whitespace, + returns the whitespace after the rightmost newline in the string. """ + first = self._data[0] + if not first.isspace(): + return "" + rindex = first.rfind("\n") + return first[rindex+1:] + + # ======== End overridden functions + + @property + def words(self) -> list[str]: + """ Iterates over words, but without spaces. Like Unspaced List. """ + return [word.strip("\"\'") for word in self._data if not word.isspace()] + + def __getitem__(self, index: int) -> str: + return self.words[index] + + def __contains__(self, word: str) -> bool: + return word in self.words + + +class Block(Parsable): + """ Any sort of block, denoted by a block name and curly braces, like so: + The parsed block: + block name { + content 1; + content 2; + } + might be represented with the list [names, contents], where + names = ["block", " ", "name", " "] + contents = [["\n ", "content", " ", "1"], ["\n ", "content", " ", "2"], "\n"] + """ + def __init__(self, parent: Optional[Parsable] = None) -> None: + super().__init__(parent) + self.names: Optional[Sentence] = None + self.contents: Optional[Block] = None + + @staticmethod + def should_parse(lists: Any) -> bool: + """ Returns True if `lists` can be parseable as a `Block`-- that is, + it's got a length of 2, the first element is a `Sentence` and the second can be + a `Statements`. + + :param list lists: The raw unparsed list to check. + + :returns: whether this lists is parseable by `Block`. """ + return isinstance(lists, list) and len(lists) == 2 and \ + Sentence.should_parse(lists[0]) and isinstance(lists[1], list) + + def set_tabs(self, tabs: str = " ") -> None: + """ Sets tabs by setting equivalent tabbing on names, then adding tabbing + to contents.""" + self.names.set_tabs(tabs) + self.contents.set_tabs(tabs + " ") + + def iterate(self, expanded: bool = False, + match: Optional[Callable[[Parsable], bool]] = None) -> Iterator[Any]: + """ Iterator over self, and if expanded is set, over its contents. """ + if match is None or match(self): + yield self + if expanded: + yield from self.contents.iterate(expanded, match) + + def parse(self, raw_list: list[Any], add_spaces: bool = False) -> None: + """ Parses a list that resembles a block. + + The assumptions that this routine makes are: + 1. the first element of `raw_list` is a valid Sentence. + 2. the second element of `raw_list` is a valid Statement. + If add_spaces is set, we call it recursively on `names` and `contents`, and + add an extra trailing space to `names` (to separate the block's opening bracket + and the block name). + """ + if not Block.should_parse(raw_list): + raise errors.MisconfigurationError("Block parsing expects a list of length 2. " + "First element should be a list of string types (the block names), " + "and second should be another list of statements (the block content).") + self.names = Sentence(self) + if add_spaces: + raw_list[0].append(" ") + self.names.parse(raw_list[0], add_spaces) + self.contents = Statements(self) + self.contents.parse(raw_list[1], add_spaces) + self._data = [self.names, self.contents] + + def get_tabs(self) -> str: + """ Guesses tabbing by retrieving tabbing guess of self.names. """ + return self.names.get_tabs() + + +def _is_comment(parsed_obj: Parsable) -> bool: + """ Checks whether parsed_obj is a comment. + + :param .Parsable parsed_obj: + + :returns: whether parsed_obj represents a comment sentence. + :rtype bool: + """ + if not isinstance(parsed_obj, Sentence): + return False + return parsed_obj.words[0] == "#" + + +def _is_certbot_comment(parsed_obj: Parsable) -> bool: + """ Checks whether parsed_obj is a "managed by Certbot" comment. + + :param .Parsable parsed_obj: + + :returns: whether parsed_obj is a "managed by Certbot" comment. + :rtype bool: + """ + if not _is_comment(parsed_obj): + return False + if len(parsed_obj.words) != len(COMMENT_BLOCK): + return False + for i, word in enumerate(parsed_obj.words): + if word != COMMENT_BLOCK[i]: + return False + return True + + +def _certbot_comment(parent: Parsable, preceding_spaces: int = 4) -> Sentence: + """ A "Managed by Certbot" comment. + :param int preceding_spaces: Number of spaces between the end of the previous + statement and the comment. + :returns: Sentence containing the comment. + :rtype: .Sentence + """ + result = Sentence(parent) + result.parse([" " * preceding_spaces] + COMMENT_BLOCK) + return result + + +def _choose_parser(parent: Parsable, list_: Any) -> Parsable: + """ Choose a parser from type(parent).parsing_hooks, depending on whichever hook + returns True first. """ + hooks = Parsable.parsing_hooks() + if parent: + hooks = type(parent).parsing_hooks() + for type_ in hooks: + if type_.should_parse(list_): + return type_(parent) + raise errors.MisconfigurationError( + "None of the parsing hooks succeeded, so we don't know how to parse this set of lists.") + + +def parse_raw(lists_: Any, parent: Optional[Parsable] = None, add_spaces: bool = False) -> Parsable: + """ Primary parsing factory function. + + :param list lists_: raw lists from pyparsing to parse. + :param .Parent parent: The parent containing this object. + :param bool add_spaces: Whether to pass add_spaces to the parser. + + :returns .Parsable: The parsed object. + + :raises errors.MisconfigurationError: If no parsing hook passes, and we can't + determine which type to parse the raw lists into. + """ + parser = _choose_parser(parent, lists_) + parser.parse(lists_, add_spaces) + return parser diff --git a/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx-old.conf b/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx-old.conf new file mode 100644 index 00000000000..fef6f125230 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx-old.conf @@ -0,0 +1,17 @@ +# This file contains important security parameters. If you modify this file +# manually, Certbot will be unable to automatically provide future security +# updates. Instead, Certbot will print and log an error message with a path to +# the up-to-date file that you will need to refer to when manually updating +# this file. Contents are based on https://ssl-config.mozilla.org +# +# This file is installed when nginx < 1.13.0 or compiled against openssl < 1.0.2l. Since these +# are deprecated, it is no longer receiving updates. To get the latest configuration +# version, update nginx. + +ssl_session_cache shared:le_nginx_SSL:10m; +ssl_session_timeout 1440m; + +ssl_protocols TLSv1.2; +ssl_prefer_server_ciphers off; + +ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384"; diff --git a/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx-tls12-only.conf b/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx-tls12-only.conf new file mode 100644 index 00000000000..a4f3043028f --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx-tls12-only.conf @@ -0,0 +1,18 @@ +# This file contains important security parameters. If you modify this file +# manually, Certbot will be unable to automatically provide future security +# updates. Instead, Certbot will print and log an error message with a path to +# the up-to-date file that you will need to refer to when manually updating +# this file. Contents are based on https://ssl-config.mozilla.org +# +# This file is installed when nginx < 1.13.0 or compiled against openssl < 1.0.2l. Since these +# are deprecated, it is no longer receiving updates. To get the latest configuration +# version, update nginx. + +ssl_session_cache shared:le_nginx_SSL:10m; +ssl_session_timeout 1440m; +ssl_session_tickets off; + +ssl_protocols TLSv1.2; +ssl_prefer_server_ciphers off; + +ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384"; diff --git a/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx-tls13-session-tix-on.conf b/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx-tls13-session-tix-on.conf new file mode 100644 index 00000000000..3c1f3995989 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx-tls13-session-tix-on.conf @@ -0,0 +1,17 @@ +# This file contains important security parameters. If you modify this file +# manually, Certbot will be unable to automatically provide future security +# updates. Instead, Certbot will print and log an error message with a path to +# the up-to-date file that you will need to refer to when manually updating +# this file. Contents are based on https://ssl-config.mozilla.org +# +# This file is installed when nginx < 1.13.0 or compiled against openssl < 1.0.2l. Since these +# are deprecated, it is no longer receiving updates. To get the latest configuration +# version, update nginx. + +ssl_session_cache shared:le_nginx_SSL:10m; +ssl_session_timeout 1440m; + +ssl_protocols TLSv1.2 TLSv1.3; +ssl_prefer_server_ciphers off; + +ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384"; diff --git a/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx.conf b/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx.conf new file mode 100644 index 00000000000..f2aadba3dec --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/nginx/tls_configs/options-ssl-nginx.conf @@ -0,0 +1,14 @@ +# This file contains important security parameters. If you modify this file +# manually, Certbot will be unable to automatically provide future security +# updates. Instead, Certbot will print and log an error message with a path to +# the up-to-date file that you will need to refer to when manually updating +# this file. Contents are based on https://ssl-config.mozilla.org + +ssl_session_cache shared:le_nginx_SSL:10m; +ssl_session_timeout 1440m; +ssl_session_tickets off; + +ssl_protocols TLSv1.2 TLSv1.3; +ssl_prefer_server_ciphers off; + +ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384"; diff --git a/certbot/src/certbot/_internal/plugins/null.py b/certbot/src/certbot/_internal/plugins/null.py new file mode 100644 index 00000000000..b73cd7b96d4 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/null.py @@ -0,0 +1,58 @@ +"""Null plugin.""" +import logging +from typing import Callable +from typing import Optional +from typing import Union + +from certbot import interfaces +from certbot.plugins import common + +logger = logging.getLogger(__name__) + + +class Installer(common.Plugin, interfaces.Installer): + """Null installer.""" + + description = "Null Installer" + hidden = True + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None]) -> None: + pass + + # pylint: disable=missing-function-docstring + + def prepare(self) -> None: + pass # pragma: no cover + + def more_info(self) -> str: + return "Installer that doesn't do anything (for testing)." + + def get_all_names(self) -> list[str]: + return [] + + def deploy_cert(self, domain: str, cert_path: str, key_path: str, + chain_path: str, fullchain_path: str) -> None: + pass # pragma: no cover + + def enhance(self, domain: str, enhancement: str, + options: Optional[Union[list[str], str]] = None) -> None: + pass # pragma: no cover + + def supported_enhancements(self) -> list[str]: + return [] + + def save(self, title: Optional[str] = None, temporary: bool = False) -> None: + pass # pragma: no cover + + def rollback_checkpoints(self, rollback: int = 1) -> None: + pass # pragma: no cover + + def recovery_routine(self) -> None: + pass # pragma: no cover + + def config_test(self) -> None: + pass # pragma: no cover + + def restart(self) -> None: + pass # pragma: no cover diff --git a/certbot/src/certbot/_internal/plugins/selection.py b/certbot/src/certbot/_internal/plugins/selection.py new file mode 100644 index 00000000000..1606634a676 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/selection.py @@ -0,0 +1,371 @@ +"""Decide which plugins to use for authentication & installation""" + +import logging +from typing import cast +from typing import Iterable +from typing import Optional +from typing import TypeVar + +from certbot import configuration +from certbot import errors +from certbot import interfaces +from certbot._internal.plugins import disco +from certbot.compat import os +from certbot.display import util as display_util + +logger = logging.getLogger(__name__) + + +def pick_configurator(config: configuration.NamespaceConfig, default: Optional[str], + plugins: disco.PluginsRegistry, + question: str = "How would you like to authenticate and install " + "certificates?") -> Optional[interfaces.Plugin]: + """Pick configurator plugin.""" + return pick_plugin( + config, default, plugins, question, + (interfaces.Authenticator, interfaces.Installer)) + + +def pick_installer(config: configuration.NamespaceConfig, default: Optional[str], + plugins: disco.PluginsRegistry, + question: str = "How would you like to install certificates?" + ) -> Optional[interfaces.Installer]: + """Pick installer plugin.""" + return pick_plugin(config, default, plugins, question, (interfaces.Installer,)) + + +def pick_authenticator(config: configuration.NamespaceConfig, default: Optional[str], + plugins: disco.PluginsRegistry, + question: str = "How would you " + "like to authenticate with the ACME CA?" + ) -> Optional[interfaces.Authenticator]: + """Pick authentication plugin.""" + return pick_plugin( + config, default, plugins, question, (interfaces.Authenticator,)) + + +def get_unprepared_installer(config: configuration.NamespaceConfig, + plugins: disco.PluginsRegistry) -> Optional[interfaces.Installer]: + """ + Get an unprepared interfaces.Installer object. + + :param certbot.configuration.NamespaceConfig config: Configuration + :param certbot._internal.plugins.disco.PluginsRegistry plugins: + All plugins registered as entry points. + + :returns: Unprepared installer plugin or None + :rtype: Plugin or None + """ + + _, req_inst = cli_plugin_requests(config) + if not req_inst: + return None + installers = plugins.filter(lambda p_ep: p_ep.check_name(req_inst)) + installers.init(config) + if len(installers) > 1: + raise errors.PluginSelectionError( + "Found multiple installers with the name %s, Certbot is unable to " + "determine which one to use. Skipping." % req_inst) + if installers: + inst = list(installers.values())[0] + logger.debug("Selecting plugin: %s", inst) + return inst.init(config) + raise errors.PluginSelectionError( + "Could not select or initialize the requested installer %s." % req_inst) + + +P = TypeVar('P', bound=interfaces.Plugin) + + +def pick_plugin(config: configuration.NamespaceConfig, default: Optional[str], + plugins: disco.PluginsRegistry, question: str, + ifaces: Iterable[type]) -> Optional[P]: + """Pick plugin. + + :param certbot.configuration.NamespaceConfig config: Configuration + :param str default: Plugin name supplied by user or ``None``. + :param certbot._internal.plugins.disco.PluginsRegistry plugins: + All plugins registered as entry points. + :param str question: Question to be presented to the user in case + multiple candidates are found. + :param list ifaces: Interfaces that plugins must provide. + + :returns: Initialized plugin. + :rtype: Plugin + + """ + if default is not None: + # throw more UX-friendly error if default not in plugins + filtered = plugins.filter(lambda p_ep: p_ep.check_name(default)) + else: + if config.noninteractive_mode: + # it's really bad to auto-select the single available plugin in + # non-interactive mode, because an update could later add a second + # available plugin + raise errors.MissingCommandlineFlag( + "Missing command line flags. For non-interactive execution, " + "you will need to specify a plugin on the command line. Run " + "with '--help plugins' to see a list of options, and see " + "https://eff.org/letsencrypt-plugins for more detail on what " + "the plugins do and how to use them.") + + filtered = plugins.visible() + + filtered = filtered.ifaces(ifaces) + filtered.init(config) + filtered.prepare() + prepared = filtered.available() + + if len(prepared) > 1: + logger.debug("Multiple candidate plugins: %s", prepared) + plugin_ep1 = choose_plugin(list(prepared.values()), question) + if plugin_ep1 is None: + return None + return cast(P, plugin_ep1.init()) + elif len(prepared) == 1: + plugin_ep2 = list(prepared.values())[0] + logger.debug("Single candidate plugin: %s", plugin_ep2) + if plugin_ep2.misconfigured: + return None + return plugin_ep2.init() + else: + logger.debug("No candidate plugin") + return None + + +def choose_plugin(prepared: list[disco.PluginEntryPoint], + question: str) -> Optional[disco.PluginEntryPoint]: + """Allow the user to choose their plugin. + + :param list prepared: List of `~.PluginEntryPoint`. + :param str question: Question to be presented to the user. + + :returns: Plugin entry point chosen by the user. + :rtype: `~.PluginEntryPoint` + + """ + opts = [plugin_ep.description_with_name + + (" [Misconfigured]" if plugin_ep.misconfigured else "") + for plugin_ep in prepared] + + while True: + code, index = display_util.menu(question, opts, force_interactive=True) + + if code == display_util.OK: + plugin_ep = prepared[index] + if plugin_ep.misconfigured: + display_util.notification( + "The selected plugin encountered an error while parsing " + "your server configuration and cannot be used. The error " + "was:\n\n{0}".format(plugin_ep.prepare()), pause=False) + else: + return plugin_ep + else: + return None + + +noninstaller_plugins = ["webroot", "manual", "standalone", "dns-cloudflare", + "dns-digitalocean", "dns-dnsimple", "dns-dnsmadeeasy", "dns-gehirn", + "dns-google", "dns-linode", "dns-luadns", "dns-nsone", "dns-ovh", + "dns-rfc2136", "dns-route53", "dns-sakuracloud"] + + +def record_chosen_plugins(config: configuration.NamespaceConfig, plugins: disco.PluginsRegistry, + auth: Optional[interfaces.Authenticator], + inst: Optional[interfaces.Installer]) -> None: + """Update the config entries to reflect the plugins we actually selected.""" + config.authenticator = None + if auth: + auth_ep = plugins.find_init(auth) + if auth_ep: + config.authenticator = auth_ep.name + config.installer = None + if inst: + inst_ep = plugins.find_init(inst) + if inst_ep: + config.installer = inst_ep.name + logger.info("Plugins selected: Authenticator %s, Installer %s", + config.authenticator, config.installer) + + +def choose_configurator_plugins(config: configuration.NamespaceConfig, + plugins: disco.PluginsRegistry, + verb: str) -> tuple[Optional[interfaces.Installer], + Optional[interfaces.Authenticator]]: + """ + Figure out which configurator we're going to use, modifies + config.authenticator and config.installer strings to reflect that choice if + necessary. + + :raises errors.PluginSelectionError if there was a problem + + :returns: tuple of (`Installer` or None, `Authenticator` or None) + :rtype: tuple + """ + + req_auth, req_inst = cli_plugin_requests(config) + installer_question = "" + + if verb == "enhance": + installer_question = ("Which installer would you like to use to " + "configure the selected enhancements?") + + # Which plugins do we need? + if verb == "run": + need_inst = need_auth = True + from certbot._internal.cli import cli_command + if req_auth in noninstaller_plugins and not req_inst: + msg = ('With the {0} plugin, you probably want to use the "certonly" command, eg:{1}' + '{1} {2} certonly --{0}{1}{1}' + '(Alternatively, add a --installer flag. See https://eff.org/letsencrypt-plugins' + '{1} and "--help plugins" for more information.)'.format( + req_auth, os.linesep, cli_command)) + + raise errors.MissingCommandlineFlag(msg) + else: + need_inst = need_auth = False + if verb == "certonly": + need_auth = True + elif verb in ("install", "enhance"): + need_inst = True + if config.authenticator: + logger.warning("Specifying an authenticator doesn't make sense when " + "running Certbot with verb \"%s\"", verb) + # Try to meet the user's request and/or ask them to pick plugins + authenticator: Optional[interfaces.Authenticator] = None + installer: Optional[interfaces.Installer] = None + if verb == "run" and req_auth == req_inst: + # Unless the user has explicitly asked for different auth/install, + # only consider offering a single choice + configurator = pick_configurator(config, req_inst, plugins) + authenticator = cast(Optional[interfaces.Authenticator], configurator) + installer = cast(Optional[interfaces.Installer], configurator) + else: + if need_inst or req_inst: + installer = pick_installer(config, req_inst, plugins, installer_question) + if need_auth: + authenticator = pick_authenticator(config, req_auth, plugins) + + # Report on any failures + if need_inst and not installer: + diagnose_configurator_problem("installer", req_inst, plugins) + if need_auth and not authenticator: + diagnose_configurator_problem("authenticator", req_auth, plugins) + + # As a special case for certonly, if a user selected apache or nginx, set + # the relevant installer (unless the user specifically specified no + # installer or only specified an authenticator on the command line) + if verb == "certonly" and authenticator is not None: + # user specified --nginx or --apache on CLI + selected_configurator = config.nginx or config.apache + # user didn't request an authenticator, and so interactively chose nginx + # or apache + interactively_selected = req_auth is None and authenticator.name in ("nginx", "apache") + + if selected_configurator or interactively_selected: + installer = cast(Optional[interfaces.Installer], authenticator) + logger.debug("Selected authenticator %s and installer %s", authenticator, installer) + + record_chosen_plugins(config, plugins, authenticator, installer) + return installer, authenticator + + +def set_configurator(previously: Optional[str], now: Optional[str]) -> Optional[str]: + """ + Setting configurators multiple ways is okay, as long as they all agree + :param str previously: previously identified request for the installer/authenticator + :param str now: the request currently being processed + """ + if not now: + # we're not actually setting anything + return previously + if previously: + if previously != now: + msg = "Too many flags setting configurators/installers/authenticators {0} -> {1}" + raise errors.PluginSelectionError(msg.format(repr(previously), repr(now))) + return now + + +def cli_plugin_requests(config: configuration.NamespaceConfig + ) -> tuple[Optional[str], Optional[str]]: + """ + Figure out which plugins the user requested with CLI and config options + + :returns: (requested authenticator string or None, requested installer string or None) + :rtype: tuple + """ + req_inst = req_auth = config.configurator + req_inst = set_configurator(req_inst, config.installer) + req_auth = set_configurator(req_auth, config.authenticator) + + if config.nginx: + req_inst = set_configurator(req_inst, "nginx") + req_auth = set_configurator(req_auth, "nginx") + if config.apache: + req_inst = set_configurator(req_inst, "apache") + req_auth = set_configurator(req_auth, "apache") + if config.standalone: + req_auth = set_configurator(req_auth, "standalone") + if config.webroot: + req_auth = set_configurator(req_auth, "webroot") + if config.manual: + req_auth = set_configurator(req_auth, "manual") + if config.dns_cloudflare: + req_auth = set_configurator(req_auth, "dns-cloudflare") + if config.dns_digitalocean: + req_auth = set_configurator(req_auth, "dns-digitalocean") + if config.dns_dnsimple: + req_auth = set_configurator(req_auth, "dns-dnsimple") + if config.dns_dnsmadeeasy: + req_auth = set_configurator(req_auth, "dns-dnsmadeeasy") + if config.dns_gehirn: + req_auth = set_configurator(req_auth, "dns-gehirn") + if config.dns_google: + req_auth = set_configurator(req_auth, "dns-google") + if config.dns_linode: + req_auth = set_configurator(req_auth, "dns-linode") + if config.dns_luadns: + req_auth = set_configurator(req_auth, "dns-luadns") + if config.dns_nsone: + req_auth = set_configurator(req_auth, "dns-nsone") + if config.dns_ovh: + req_auth = set_configurator(req_auth, "dns-ovh") + if config.dns_rfc2136: + req_auth = set_configurator(req_auth, "dns-rfc2136") + if config.dns_route53: + req_auth = set_configurator(req_auth, "dns-route53") + if config.dns_sakuracloud: + req_auth = set_configurator(req_auth, "dns-sakuracloud") + logger.debug("Requested authenticator %s and installer %s", req_auth, req_inst) + return req_auth, req_inst + + +def diagnose_configurator_problem(cfg_type: str, requested: Optional[str], + plugins: disco.PluginsRegistry) -> None: + """ + Raise the most helpful error message about a plugin being unavailable + + :param str cfg_type: either "installer" or "authenticator" + :param str requested: the plugin that was requested + :param .PluginsRegistry plugins: available plugins + + :raises error.PluginSelectionError: if there was a problem + """ + + if requested: + if requested not in plugins: + msg = "The requested {0} plugin does not appear to be installed".format(requested) + else: + msg = ("The {0} plugin is not working; there may be problems with " + "your existing configuration.\nThe error was: {1!r}" + .format(requested, plugins[requested].problem)) + elif cfg_type == "installer": + from certbot._internal.cli import cli_command + msg = ('Certbot doesn\'t know how to automatically configure the web ' + 'server on this system. However, it can still get a certificate for ' + 'you. Please run "{0} certonly" to do so. You\'ll need to ' + 'manually configure your web server to use the resulting ' + 'certificate.').format(cli_command) + else: + msg = "{0} could not be determined or is not installed".format(cfg_type) + raise errors.PluginSelectionError(msg) diff --git a/certbot/src/certbot/_internal/plugins/standalone.py b/certbot/src/certbot/_internal/plugins/standalone.py new file mode 100644 index 00000000000..83baa53ce32 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/standalone.py @@ -0,0 +1,206 @@ +"""Standalone Authenticator.""" +import collections +import errno +import logging +from typing import Any +from typing import Callable +from typing import Iterable +from typing import TYPE_CHECKING + +from acme import challenges +from acme import standalone as acme_standalone +from certbot import achallenges +from certbot import errors +from certbot import interfaces +from certbot.display import util as display_util +from certbot.plugins import common + +logger = logging.getLogger(__name__) + +if TYPE_CHECKING: + ServedType = collections.defaultdict[ + acme_standalone.BaseDualNetworkedServers, + set[achallenges.AnnotatedChallenge] + ] + + +class ServerManager: + """Manager for HTTP-01 standalone server instances.""" + + def __init__(self, + http_01_resources: set[acme_standalone.HTTP01RequestHandler.HTTP01Resource] + ) -> None: + self._instances: dict[int, acme_standalone.HTTP01DualNetworkedServers] = {} + self.http_01_resources = http_01_resources + + def run(self, port: int, challenge_type: type[challenges.Challenge], + listenaddr: str = "") -> acme_standalone.HTTP01DualNetworkedServers: + """Run ACME server on specified ``port``. + + This method is idempotent, i.e. all calls with the same pair of + ``(port, challenge_type)`` will reuse the same server. + + :param int port: Port to run the server on. + :param challenge_type: Subclass of `acme.challenges.Challenge`, + currently only `acme.challenge.HTTP01`. + :param str listenaddr: (optional) The address to listen on. Defaults to all addrs. + + :returns: DualNetworkedServers instance. + :rtype: ACMEServerMixin + + """ + assert challenge_type == challenges.HTTP01 + if port in self._instances: + return self._instances[port] + + address = (listenaddr, port) + try: + servers = acme_standalone.HTTP01DualNetworkedServers( + address, self.http_01_resources) + except OSError as error: + raise errors.StandaloneBindError(error, port) + + servers.serve_forever() + + # if port == 0, then random free port on OS is taken + # both servers, if they exist, have the same port + real_port = servers.getsocknames()[0][1] + self._instances[real_port] = servers + return servers + + def stop(self, port: int) -> None: + """Stop ACME server running on the specified ``port``. + + :param int port: + + """ + instance = self._instances[port] + for sockname in instance.getsocknames(): + logger.debug("Stopping server at %s:%d...", + *sockname[:2]) + instance.shutdown_and_server_close() + del self._instances[port] + + def running(self) -> dict[int, acme_standalone.HTTP01DualNetworkedServers]: + """Return all running instances. + + Once the server is stopped using `stop`, it will not be + returned. + + :returns: Mapping from ``port`` to ``servers``. + :rtype: tuple + + """ + return self._instances.copy() + + +class Authenticator(common.Plugin, interfaces.Authenticator): + """Standalone Authenticator. + + This authenticator creates its own ephemeral TCP listener on the + necessary port in order to respond to incoming http-01 + challenges from the certificate authority. Therefore, it does not + rely on any existing server program. + """ + + description = """Runs an HTTP server locally which serves the necessary validation files \ +under the /.well-known/acme-challenge/ request path. Suitable if there is no HTTP server already \ +running. HTTP challenge only (wildcards not supported).""" + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + + self.served: ServedType = collections.defaultdict(set) + + # Stuff below is shared across threads (i.e. servers read + # values, main thread writes). Due to the nature of CPython's + # GIL, the operations are safe, c.f. + # https://docs.python.org/2/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe + self.http_01_resources: set[acme_standalone.HTTP01RequestHandler.HTTP01Resource] = set() + + self.servers = ServerManager(self.http_01_resources) + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None]) -> None: + pass # No additional argument for the standalone plugin parser + + def more_info(self) -> str: # pylint: disable=missing-function-docstring + return("This authenticator creates its own ephemeral TCP listener " + "on the necessary port in order to respond to incoming " + "http-01 challenges from the certificate authority. Therefore, " + "it does not rely on any existing server program.") + + def prepare(self) -> None: # pylint: disable=missing-function-docstring + pass + + def get_chall_pref(self, identifier: str) -> Iterable[type[challenges.Challenge]]: + # pylint: disable=unused-argument,missing-function-docstring + return [challenges.HTTP01] + + def perform(self, achalls: Iterable[achallenges.AnnotatedChallenge] + ) -> list[challenges.ChallengeResponse]: # pylint: disable=missing-function-docstring + return [self._try_perform_single(achall) for achall in achalls] + + def _try_perform_single(self, + achall: achallenges.AnnotatedChallenge) -> challenges.ChallengeResponse: + while True: + try: + return self._perform_single(achall) + except errors.StandaloneBindError as error: + _handle_perform_error(error) + + def _perform_single(self, + achall: achallenges.AnnotatedChallenge) -> challenges.ChallengeResponse: + servers, response = self._perform_http_01(achall) + self.served[servers].add(achall) + return response + + def _perform_http_01(self, achall: achallenges.AnnotatedChallenge + ) -> tuple[acme_standalone.HTTP01DualNetworkedServers, + challenges.ChallengeResponse]: + port = self.config.http01_port + addr = self.config.http01_address + servers = self.servers.run(port, challenges.HTTP01, listenaddr=addr) + response, validation = achall.response_and_validation() + resource = acme_standalone.HTTP01RequestHandler.HTTP01Resource( + chall=achall.chall, response=response, validation=validation) + self.http_01_resources.add(resource) + return servers, response + + def cleanup(self, achalls: Iterable[achallenges.AnnotatedChallenge]) -> None: # pylint: disable=missing-function-docstring + # reduce self.served and close servers if no challenges are served + for unused_servers, server_achalls in self.served.items(): + for achall in achalls: + if achall in server_achalls: + server_achalls.remove(achall) + for port, servers in self.servers.running().items(): + if not self.served[servers]: + self.servers.stop(port) + + def auth_hint(self, failed_achalls: list[achallenges.AnnotatedChallenge]) -> str: + port, addr = self.config.http01_port, self.config.http01_address + neat_addr = f"{addr}:{port}" if addr else f"port {port}" + return ("The Certificate Authority failed to download the challenge files from " + f"the temporary standalone webserver started by Certbot on {neat_addr}. " + "Ensure that the listed domains point to this machine and that it can " + "accept inbound connections from the internet.") + + +def _handle_perform_error(error: errors.StandaloneBindError) -> None: + if error.socket_error.errno == errno.EACCES: + raise errors.PluginError( + "Could not bind TCP port {0} because you don't have " + "the appropriate permissions (for example, you " + "aren't running this program as " + "root).".format(error.port)) + if error.socket_error.errno == errno.EADDRINUSE: + msg = ( + "Could not bind TCP port {0} because it is already in " + "use by another process on this system (such as a web " + "server). Please stop the program in question and " + "then try again.".format(error.port)) + should_retry = display_util.yesno(msg, "Retry", "Cancel", default=False) + if not should_retry: + raise errors.PluginError(msg) + else: + raise error diff --git a/certbot/src/certbot/_internal/plugins/webroot.py b/certbot/src/certbot/_internal/plugins/webroot.py new file mode 100644 index 00000000000..ab3d2d51246 --- /dev/null +++ b/certbot/src/certbot/_internal/plugins/webroot.py @@ -0,0 +1,354 @@ +"""Webroot plugin.""" +import argparse +import collections +import json +import logging +from typing import Any +from typing import Callable +from typing import Iterable +from typing import Optional +from typing import Sequence +from typing import Union + +from acme import challenges, messages +from certbot import crypto_util +from certbot import errors +from certbot import interfaces +from certbot._internal import cli +from certbot._internal import san +from certbot.achallenges import AnnotatedChallenge +from certbot.compat import filesystem +from certbot.compat import os +from certbot.display import ops +from certbot.display import util as display_util +from certbot.plugins import common +from certbot.plugins import util +from certbot.util import safe_open + +logger = logging.getLogger(__name__) + + +_WEB_CONFIG_CONTENT = """\ + + + + + + + + + + +""" +# This list references the hashes of all versions of the web.config files that Certbot could +# have generated during an HTTP-01 challenge. If you modify _WEB_CONFIG_CONTENT, you MUST add +# the new hash in this list. +_WEB_CONFIG_SHA256SUMS = [ + "20c5ca1bd58fa8ad5f07a2f1be8b7cbb707c20fcb607a8fc8db9393952846a97", + "8d31383d3a079d2098a9d0c0921f4ab87e708b9868dc3f314d54094c2fe70336" +] + + +class Authenticator(common.Plugin, interfaces.Authenticator): + """Webroot Authenticator.""" + + description = """\ +Saves the necessary validation files to a .well-known/acme-challenge/ directory within the \ +nominated webroot path. A separate HTTP server must be running and serving files from the \ +webroot path. HTTP challenge only (wildcards not supported).""" + + MORE_INFO = """\ +Authenticator plugin that performs http-01 challenge by saving +necessary validation resources to appropriate paths on the file +system. It expects that there is some other HTTP server configured +to serve all files under specified web root ({0}).""" + + def more_info(self) -> str: # pylint: disable=missing-function-docstring + return self.MORE_INFO.format(self.conf("path")) + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None]) -> None: + add("path", "-w", default=[], action=_WebrootPathAction, + help="public_html / webroot path. This can be specified multiple " + "times to handle different identifiers; each identifier will have " + "the webroot path that preceded it. For instance: `-w " + "/var/www/example -d example.com -d www.example.com -w " + "/var/www/thing -d thing.net -d m.thing.net` (default: Ask)") + add("map", default={}, action=_WebrootMapAction, + help="JSON dictionary mapping identifiers to webroot paths; this " + "implies -d or --ip-address for each entry. You may need to " + " escape this from your shell. E.g.: --webroot-map " + '\'{"eg1.is,m.eg1.is":"/www/eg1/", "eg2.is":"/www/eg2"}\' ' + "This option is merged with, but takes precedence over, -w / " + "-d entries. At present, if you put webroot-map in a config " + "file, it needs to be on a single line, like: webroot-map = " + '{"example.com":"/var/www"}.') + + def auth_hint(self, failed_achalls: list[AnnotatedChallenge]) -> str: # pragma: no cover + return ("The Certificate Authority failed to download the temporary challenge files " + "created by Certbot. Ensure that the listed identifiers serve their content from " + "the provided --webroot-path/-w and that files created there can be downloaded " + "from the internet.") + + def get_chall_pref(self, identifier: str) -> Iterable[type[challenges.Challenge]]: + # pylint: disable=unused-argument,missing-function-docstring + return [challenges.HTTP01] + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.full_roots: dict[str, str] = {} + self.performed: collections.defaultdict[str, set[AnnotatedChallenge]] = ( + collections.defaultdict(set) + ) + # stack of dirs successfully created by this authenticator + self._created_dirs: list[str] = [] + + def prepare(self) -> None: # pylint: disable=missing-function-docstring + pass + + def perform(self, achalls: list[AnnotatedChallenge]) -> list[challenges.ChallengeResponse]: # pylint: disable=missing-function-docstring + self._set_webroots(achalls) + + self._create_challenge_dirs() + + return [self._perform_single(achall) for achall in achalls] + + def _set_webroots(self, achalls: Iterable[AnnotatedChallenge]) -> None: + if self.conf("path"): + webroot_path = self.conf("path")[-1] + logger.info("Using the webroot path %s for all unmatched identifiers.", + webroot_path) + for achall in achalls: + self.conf("map").setdefault(achall.identifier.value, webroot_path) + else: + known_webroots = list(set(self.conf("map").values())) + for achall in achalls: + if achall.identifier.value not in self.conf("map"): + new_webroot = self._prompt_for_webroot(achall.identifier, + known_webroots) + # Put the most recently input + # webroot first for easy selection + try: + known_webroots.remove(new_webroot) + except ValueError: + pass + known_webroots.insert(0, new_webroot) + self.conf("map")[achall.identifier.value] = new_webroot + + def _prompt_for_webroot(self, identifier: messages.Identifier, + known_webroots: list[str]) -> Optional[str]: + webroot = None + + while webroot is None: + if known_webroots: + # Only show the menu if we have options for it + webroot = self._prompt_with_webroot_list(identifier, known_webroots) + if webroot is None: + webroot = self._prompt_for_new_webroot(identifier) + else: + # Allow prompt to raise PluginError instead of looping forever + webroot = self._prompt_for_new_webroot(identifier, True) + + return webroot + + def _prompt_with_webroot_list(self, identifier: messages.Identifier, + known_webroots: list[str]) -> Optional[str]: + path_flag = "--" + self.option_name("path") + + while True: + code, index = display_util.menu( + "Select the webroot for {0}:".format(identifier.value), + ["Enter a new webroot"] + known_webroots, + cli_flag=path_flag, force_interactive=True) + if code == display_util.CANCEL: + raise errors.PluginError( + "Every requested identifier must have a " + "webroot when using the webroot plugin.") + return None if index == 0 else known_webroots[index - 1] # code == display_util.OK + + def _prompt_for_new_webroot(self, identifier: messages.Identifier, + allowraise: bool = False) -> Optional[str]: + code, webroot = ops.validated_directory( + _validate_webroot, + "Input the webroot for {0}:".format(identifier.value), + force_interactive=True) + if code == display_util.CANCEL: + if not allowraise: + return None + raise errors.PluginError( + "Every requested identifier must have a " + "webroot when using the webroot plugin.") + return _validate_webroot(webroot) # code == display_util.OK + + def _create_challenge_dirs(self) -> None: + path_map = self.conf("map") + if not path_map: + raise errors.PluginError( + "Missing parts of webroot configuration; please set " + "--webroot-path and --domains or --ip-address. " + "Alternatively you may set --webroot-map. " + "Run with --help webroot for examples.") + for name, path in path_map.items(): + self.full_roots[name] = os.path.join(path, os.path.normcase( + challenges.HTTP01.URI_ROOT_PATH)) + logger.debug("Creating root challenges validation dir at %s", + self.full_roots[name]) + + # Change the permissions to be writable (GH #1389) + # Umask is used instead of chmod to ensure the client can also + # run as non-root (GH #1795) + with filesystem.temp_umask(0o022): + # We ignore the last prefix in the next iteration, + # as it does not correspond to a folder path ('/' or 'C:') + for prefix in sorted(util.get_prefixes(self.full_roots[name])[:-1], key=len): + if os.path.isdir(prefix): + # Don't try to create directory if it already exists, as some filesystems + # won't reliably raise EEXIST or EISDIR if directory exists. + continue + try: + # Set owner as parent directory if possible, apply mode for Linux/Windows. + # For Linux, this is coupled with the "umask" call above because + # os.mkdir's "mode" parameter may not always work: + # https://docs.python.org/3/library/os.html#os.mkdir + filesystem.mkdir(prefix, 0o755) + self._created_dirs.append(prefix) + try: + filesystem.copy_ownership_and_apply_mode( + path, prefix, 0o755, copy_user=True, copy_group=True) + except (OSError, AttributeError) as exception: + logger.warning("Unable to change owner and uid of webroot directory") + logger.debug("Error was: %s", exception) + except OSError as exception: + raise errors.PluginError( + "Couldn't create root for {0} http-01 " + "challenge responses: {1}".format(name, exception)) + + # On Windows, generate a local web.config file that allows IIS to serve expose + # challenge files despite the fact they do not have a file extension. + if not filesystem.POSIX_MODE: + web_config_path = os.path.join(self.full_roots[name], "web.config") + if os.path.exists(web_config_path): + logger.info("A web.config file has not been created in " + "%s because another one already exists.", self.full_roots[name]) + continue + logger.info("Creating a web.config file in %s to allow IIS " + "to serve challenge files.", self.full_roots[name]) + with safe_open(web_config_path, mode="w", chmod=0o644) as web_config: + web_config.write(_WEB_CONFIG_CONTENT) + + def _get_validation_path(self, root_path: str, achall: AnnotatedChallenge) -> str: + return os.path.join(root_path, achall.chall.encode("token")) + + def _perform_single(self, achall: AnnotatedChallenge) -> challenges.ChallengeResponse: + response, validation = achall.response_and_validation() + + root_path = self.full_roots[achall.identifier.value] + validation_path = self._get_validation_path(root_path, achall) + logger.debug("Attempting to save validation to %s", validation_path) + + # Change permissions to be world-readable, owner-writable (GH #1795) + with filesystem.temp_umask(0o022): + with safe_open(validation_path, mode="wb", chmod=0o644) as validation_file: + validation_file.write(validation.encode()) + + self.performed[root_path].add(achall) + return response + + def cleanup(self, achalls: list[AnnotatedChallenge]) -> None: # pylint: disable=missing-function-docstring + for achall in achalls: + root_path = self.full_roots.get(achall.identifier.value, None) + if root_path is not None: + validation_path = self._get_validation_path(root_path, achall) + logger.debug("Removing %s", validation_path) + os.remove(validation_path) + self.performed[root_path].remove(achall) + + if not filesystem.POSIX_MODE: + web_config_path = os.path.join(root_path, "web.config") + if os.path.exists(web_config_path): + sha256sum = crypto_util.sha256sum(web_config_path) + if sha256sum in _WEB_CONFIG_SHA256SUMS: + logger.info("Cleaning web.config file generated by Certbot in %s.", + root_path) + os.remove(web_config_path) + else: + logger.info("Not cleaning up the web.config file in %s " + "because it is not generated by Certbot.", root_path) + + not_removed: list[str] = [] + while self._created_dirs: + path = self._created_dirs.pop() + try: + os.rmdir(path) + except OSError as exc: + not_removed.insert(0, path) + logger.info("Challenge directory %s was not empty, didn't remove", path) + logger.debug("Error was: %s", exc) + self._created_dirs = not_removed + logger.debug("All challenges cleaned up") + + +class _WebrootMapAction(argparse.Action): + """Action class for parsing webroot_map.""" + + def __call__(self, parser: argparse.ArgumentParser, namespace: argparse.Namespace, + webroot_map: Union[str, Sequence[Any], None], + option_string: Optional[str] = None) -> None: + if webroot_map is None: + return + for identlist, webroot_path in json.loads(str(webroot_map)).items(): + webroot_path = _validate_webroot(webroot_path) + for s in san.guess(identlist.split(",")): + match s: + case san.IPAddress(): + cli.add_ip_address(namespace, s) + case san.DNSName(): + cli.add_dns_name(namespace, s) + + namespace.webroot_map[str(s)] = webroot_path + + +class _WebrootPathAction(argparse.Action): + """Action class for parsing webroot_path.""" + + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self._ident_before_webroot = False + + def __call__(self, parser: argparse.ArgumentParser, namespace: argparse.Namespace, + webroot_path: Union[str, Sequence[Any], None], + option_string: Optional[str] = None) -> None: + if webroot_path is None: + return + if self._ident_before_webroot: + raise errors.PluginError( + "If you specify multiple webroot paths, " + "one of them must precede all --domain and --ip-address flags") + + if namespace.webroot_path: + # Apply previous webroot to all matched + # domains before setting the new webroot path + prev_webroot = namespace.webroot_path[-1] + for domain in namespace.domains: + namespace.webroot_map.setdefault(domain.dns_name, prev_webroot) + for ip_address in namespace.ip_addresses: + namespace.webroot_map.setdefault(str(ip_address), prev_webroot) + elif namespace.domains or namespace.ip_addresses: + self._ident_before_webroot = True + + namespace.webroot_path.append(_validate_webroot(str(webroot_path))) + + +def _validate_webroot(webroot_path: str) -> str: + """Validates and returns the absolute path of webroot_path. + + :param str webroot_path: path to the webroot directory + + :returns: absolute path of webroot_path + :rtype: str + + """ + if not os.path.isdir(webroot_path): + raise errors.PluginError(webroot_path + " does not exist or is not a directory") + + return os.path.abspath(webroot_path) diff --git a/certbot/src/certbot/_internal/renewal.py b/certbot/src/certbot/_internal/renewal.py new file mode 100644 index 00000000000..bf7c79c635a --- /dev/null +++ b/certbot/src/certbot/_internal/renewal.py @@ -0,0 +1,761 @@ +"""Functionality for autorenewal and associated juggling of configurations""" +import configobj +import copy +import datetime +import itertools +import logging +import random +import sys +import time +import traceback +from typing import Any +from typing import Iterable +from typing import Mapping +from typing import Optional +from typing import Union + +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.hazmat.primitives.asymmetric import rsa +from cryptography.hazmat.primitives.serialization import load_pem_private_key +from cryptography import x509 + +from acme import client as acme_client + +from certbot import configuration +from certbot import crypto_util +from certbot import errors +from certbot import util +from certbot._internal import cli +from certbot._internal import client +from certbot._internal import constants +from certbot._internal import hooks +from certbot._internal import san +from certbot._internal import storage +from certbot._internal import updater +from certbot._internal.display import obj as display_obj +from certbot._internal.plugins import disco as plugins_disco +from certbot.compat import os +from certbot.display import util as display_util + +logger = logging.getLogger(__name__) + +ARI_RETRY_AFTER_CONFIG_ITEM = "ari_retry_after" + +# These are the items which get pulled out of a renewal configuration +# file's renewalparams and actually used in the client configuration +# during the renewal process. We have to record their types here because +# the renewal configuration process loses this information. +STR_CONFIG_ITEMS = ["config_dir", "logs_dir", "work_dir", "user_agent", + "server", "account", "authenticator", "installer", + "deploy_hook", "pre_hook", "post_hook", "http01_address", + "preferred_chain", "key_type", "elliptic_curve", + "preferred_profile", "required_profile"] +INT_CONFIG_ITEMS = ["rsa_key_size", "http01_port"] +BOOL_CONFIG_ITEMS = ["must_staple", "allow_subset_of_names", "reuse_key", + "autorenew"] + +CONFIG_ITEMS = set(itertools.chain( + BOOL_CONFIG_ITEMS, INT_CONFIG_ITEMS, STR_CONFIG_ITEMS, ('pref_challs',))) + +class AriClientPool: + """A cache of ACME clients for using in performing ACME Renewal Info (ARI) requests. + + During `certbot renew` we need to check ARI for many certificates, and usually these + are all issued by the same server. To avoid redundant directory fetches, we create + one acme.ClientV2 per server. + + This takes a command line configuration object so it can set the User-Agent header and + observe the --no-verify-ssl flag. + """ + def __init__(self, cli_config: configuration.NamespaceConfig): + self._verify_ssl = not cli_config.no_verify_ssl + self._user_agent = client.determine_user_agent(cli_config) + self._pool: dict[str, acme_client.ClientV2] = {} + + def get(self, server: str) -> acme_client.ClientV2: + """ + Retrieve or create an ACME client for the specified server. + + Returns: + acme_client.ClientV2: The ACME client associated with the specified server. + """ + ari_client = self._pool.get(server, None) + if ari_client: + return ari_client + + net = acme_client.ClientNetwork(verify_ssl=self._verify_ssl, user_agent=self._user_agent) + directory = acme_client.ClientV2.get_directory(server, net) + ari_client = acme_client.ClientV2(directory, net) + + self._pool[server] = ari_client + return ari_client + + +def reconstitute(config: configuration.NamespaceConfig, + full_path: str) -> Optional[storage.RenewableCert]: + """Try to instantiate a RenewableCert, updating config with relevant items. + + This is specifically for use in renewal and enforces several checks + and policies to ensure that we can try to proceed with the renewal + request. The config argument is modified by including relevant options + read from the renewal configuration file. + + :param configuration.NamespaceConfig config: configuration for the + current lineage + :param str full_path: Absolute path to the configuration file that + defines this lineage + + :returns: the RenewableCert object or None if a fatal error occurred + :rtype: `storage.RenewableCert` or NoneType + + """ + try: + renewal_candidate = storage.RenewableCert(full_path, config) + except (OSError, errors.CertStorageError) as error: + logger.error("Renewal configuration file %s is broken.", full_path) + logger.error("The error was: %s\nSkipping.", str(error)) + logger.debug("Traceback was:\n%s", traceback.format_exc()) + return None + if "renewalparams" not in renewal_candidate.configuration: + logger.error("Renewal configuration file %s lacks " + "renewalparams. Skipping.", full_path) + return None + renewalparams = renewal_candidate.configuration["renewalparams"] + if "authenticator" not in renewalparams: + logger.error("Renewal configuration file %s does not specify " + "an authenticator. Skipping.", full_path) + return None + + # Prior to Certbot v1.25.0, the default value of key_type (rsa) was not persisted to the + # renewal params. If the option is absent, it means the certificate was an RSA key. + # Restoring the option here is necessary to preserve the certificate key_type if + # the user has upgraded directly from Certbot =v2.0.0, where the default + # key_type was changed to ECDSA. See https://github.com/certbot/certbot/issues/9635. + renewalparams["key_type"] = renewalparams.get("key_type", "rsa") + + # Now restore specific values along with their data types, if + # those elements are present. + renewalparams = _remove_deprecated_config_elements(renewalparams) + try: + restore_required_config_elements(config, renewalparams) + _restore_plugin_configs(config, renewalparams) + except (ValueError, errors.Error) as error: + logger.error( + "An error occurred while parsing %s. The error was %s. " + "Skipping the file.", full_path, str(error)) + logger.debug("Traceback was:\n%s", traceback.format_exc()) + return None + + try: + domains, ip_addresses = san.split(renewal_candidate.sans()) + config.domains = domains + config.ip_addresses = ip_addresses + except errors.ConfigurationError as error: + logger.error("Renewal configuration file %s references a certificate " + "that contains an invalid domain name. The problem " + "was: %s. Skipping.", full_path, error) + return None + + return renewal_candidate + + +def _restore_webroot_config(config: configuration.NamespaceConfig, + renewalparams: Mapping[str, Any]) -> None: + """ + webroot_map is, uniquely, a dict, and the general-purpose configuration + restoring logic is not able to correctly parse it from the serialized + form. + """ + if "webroot_map" in renewalparams and not config.set_by_user("webroot_map"): + config.webroot_map = renewalparams["webroot_map"] + # To understand why webroot_path and webroot_map processing are not mutually exclusive, + # see https://github.com/certbot/certbot/pull/7095 + if "webroot_path" in renewalparams and not config.set_by_user("webroot_path"): + wp = renewalparams["webroot_path"] + if isinstance(wp, str): # prior to 0.1.0, webroot_path was a string + wp = [wp] + config.webroot_path = wp + + +def _restore_plugin_configs(config: configuration.NamespaceConfig, + renewalparams: Mapping[str, Any]) -> None: + """Sets plugin specific values in config from renewalparams + + :param configuration.NamespaceConfig config: configuration for the + current lineage + :param configobj.Section renewalparams: Parameters from the renewal + configuration file that defines this lineage + + """ + # Now use parser to get plugin-prefixed items with correct types + # XXX: the current approach of extracting only prefixed items + # related to the actually-used installer and authenticator + # works as long as plugins don't need to read plugin-specific + # variables set by someone else (e.g., assuming Apache + # configurator doesn't need to read webroot_ variables). + # Note: if a parameter that used to be defined in the parser is no + # longer defined, stored copies of that parameter will be + # deserialized as strings by this logic even if they were + # originally meant to be some other type. + plugin_prefixes: list[str] = [] + if renewalparams["authenticator"] == "webroot": + _restore_webroot_config(config, renewalparams) + else: + plugin_prefixes.append(renewalparams["authenticator"]) + + if renewalparams.get("installer") is not None: + plugin_prefixes.append(renewalparams["installer"]) + + for plugin_prefix in set(plugin_prefixes): + plugin_prefix = plugin_prefix.replace('-', '_') + for config_item, config_value in renewalparams.items(): + if config_item.startswith(plugin_prefix + "_") and not config.set_by_user(config_item): + # Values None, True, and False need to be treated specially, + # As their types aren't handled correctly by configobj + if config_value in ("None", "True", "False"): + # bool("False") == True + # pylint: disable=eval-used + setattr(config, config_item, eval(config_value)) + else: + cast = cli.argparse_type(config_item) + setattr(config, config_item, cast(config_value)) + + +def restore_required_config_elements(config: configuration.NamespaceConfig, + renewalparams: Mapping[str, Any]) -> None: + """Sets non-plugin specific values in config from renewalparams + + :param configuration.NamespaceConfig config: configuration for the + current lineage + :param configobj.Section renewalparams: parameters from the renewal + configuration file that defines this lineage + + """ + + updated_values = {} + required_items = itertools.chain( + (("pref_challs", _restore_pref_challs),), + zip(BOOL_CONFIG_ITEMS, itertools.repeat(_restore_bool)), + zip(INT_CONFIG_ITEMS, itertools.repeat(_restore_int)), + zip(STR_CONFIG_ITEMS, itertools.repeat(_restore_str))) + for item_name, restore_func in required_items: + if item_name in renewalparams and not config.set_by_user(item_name): + value = restore_func(item_name, renewalparams[item_name]) + updated_values[item_name] = value + for key, value in updated_values.items(): + setattr(config, key, value) + + +def _remove_deprecated_config_elements(renewalparams: Mapping[str, Any]) -> dict[str, Any]: + """Removes deprecated config options from the parsed renewalparams. + + :param dict renewalparams: list of parsed renewalparams + + :returns: list of renewalparams with deprecated config options removed + :rtype: dict + + """ + return {option_name: v for (option_name, v) in renewalparams.items() + if option_name not in cli.DEPRECATED_OPTIONS} + + +def _restore_pref_challs(unused_name: str, value: Union[list[str], str]) -> list[str]: + """Restores preferred challenges from a renewal config file. + + If value is a `str`, it should be a single challenge type. + + :param str unused_name: option name + :param value: option value + :type value: `list` of `str` or `str` + + :returns: converted option value to be stored in the runtime config + :rtype: `list` of `str` + + :raises errors.Error: if value can't be converted to a bool + + """ + # If pref_challs has only one element, configobj saves the value + # with a trailing comma so it's parsed as a list. If this comma is + # removed by the user, the value is parsed as a str. + value = [value] if isinstance(value, str) else value + return cli.parse_preferred_challenges(value) + + +def _restore_bool(name: str, value: str) -> bool: + """Restores a boolean key-value pair from a renewal config file. + + :param str name: option name + :param str value: option value + + :returns: converted option value to be stored in the runtime config + :rtype: bool + + :raises errors.Error: if value can't be converted to a bool + + """ + lowercase_value = value.lower() + if lowercase_value not in ("true", "false"): + raise errors.Error(f"Expected True or False for {name} but found {value}") + return lowercase_value == "true" + + +def _restore_int(name: str, value: str) -> int: + """Restores an integer key-value pair from a renewal config file. + + :param str name: option name + :param str value: option value + + :returns: converted option value to be stored in the runtime config + :rtype: int + + :raises errors.Error: if value can't be converted to an int + + """ + if name == "http01_port" and value == "None": + logger.info("updating legacy http01_port value") + return cli.flag_default("http01_port") + + try: + return int(value) + except ValueError: + raise errors.Error(f"Expected a numeric value for {name}") + + +def _restore_str(name: str, value: str) -> Optional[str]: + """Restores a string key-value pair from a renewal config file. + + :param str name: option name + :param str value: option value + + :returns: converted option value to be stored in the runtime config + :rtype: str or None + + """ + # To automatically migrate users from Let's Encrypt's old ACMEv1 URL, we replace the it here + # with the default ACME URL. It is still possible to override this choice with the explicit + # `--server` CLI flag. + if name == "server" and value == constants.V1_URI: + logger.info("Using server %s instead of legacy %s", + constants.CLI_DEFAULTS["server"], value) + return constants.CLI_DEFAULTS["server"] + + return None if value == "None" else value + + +def should_renew(config: configuration.NamespaceConfig, + lineage: storage.RenewableCert, + ari_clients: AriClientPool) -> bool: + """Return true if any of the circumstances for automatic renewal apply.""" + if config.renew_by_default: + logger.debug("Auto-renewal forced with --force-renewal...") + return True + if config.dry_run: + logger.info("Certificate not due for renewal, but simulating renewal for dry run") + return True + if should_autorenew(lineage, ari_clients): + logger.info("Certificate is due for renewal, auto-renewing...") + return True + display_util.notify("Certificate not yet due for renewal") + return False + + +def _ari_renewal_time(lineage: storage.RenewableCert, + cert_pem: bytes, + ari_clients: AriClientPool) -> Optional[datetime.datetime]: + """Return the ARI suggested renewal time if it's available.""" + # For ARI requests, we want to use the ACME directory URL from which the + # cert was originally requested. Since `NamespaceConfig.server` can be overridden on + # the command line, we're using the server stored in the cert's renewal + # conf, i.e. `lineage.server` + # + # Fixes https://github.com/certbot/certbot/issues/10339 + if not lineage.server: + logger.warning("Skipping ARI check because %s has no 'server' field. This issue will not " + "prevent certificate renewal", lineage.configfile.filename) + return None + + ari_config_section = lineage.configfile.get("acme_renewal_info", {}) + retry_after = ari_config_section.get(ARI_RETRY_AFTER_CONFIG_ITEM, None) + if retry_after: + retry_after_datetime = datetime.datetime.fromisoformat(retry_after) + now = datetime.datetime.now() + if now < retry_after_datetime: + logger.debug("Skipped ACME Renewal Info check because ari_retry_after %s is in " + "the future", + retry_after) + return None + + renewal_time = None + try: + ari_client = ari_clients.get(lineage.server) + renewal_time, retry_after = ari_client.renewal_time(cert_pem) + except Exception: # pylint: disable=broad-except + # We want to stop errors around ARI preventing renewal so we catch all exceptions here + # with a warning asking users to tell us about any problems they are experiencing + logger.warning("An error occurred requesting ACME Renewal Information (ARI). If this " + "problem persists and you think it's a bug in Certbot, please open an " + "issue at https://github.com/certbot/certbot/issues/new/choose.") + logger.debug("Error while requesting ARI was:", exc_info=True) + retry_after = datetime.datetime.now() + datetime.timedelta(seconds=60 * 60 * 6) + + + config_update = configobj.ConfigObj() + config_update["acme_renewal_info"] = { + # Note: the ACME client returns naive (no timezone) datetimes for retry_after, and that + # is what we serialize here. + ARI_RETRY_AFTER_CONFIG_ITEM: retry_after.isoformat(timespec="seconds"), + } + storage.atomic_rewrite(lineage.configfile.filename, config_update) + return renewal_time + + +def _default_renewal_time(cert_pem: bytes) -> datetime.datetime: + """Return an reasonable default time to attempt renewal of the certificate + based on the certificate lifetime. + + :param bytes cert_pem: cert as pem file + + :returns: Time to attempt renewal + :rtype: `datetime.datetime` + """ + cert = x509.load_pem_x509_certificate(cert_pem) + + not_before = cert.not_valid_before_utc + lifetime = cert.not_valid_after_utc - not_before + if lifetime.total_seconds() < 10 * 86400: + default_rt = not_before + lifetime / 2 + else: + default_rt = not_before + lifetime * 2 / 3 + + return default_rt + +def should_autorenew(lineage: storage.RenewableCert, + ari_clients: AriClientPool) -> bool: + """Should we now try to autorenew the most recent cert version? + + If automatic renewal is disabled for the lineage, this function + immediately returns False. + + Otherwise, if any of ACME Renewal Info (ARI), OCSP, or the + renew_before_expiry config option indicates we should renew, we + return True. If none of those values say we should renew and at + least one of ARI or renew_before_expiry has an available value, we + return False. We otherwise decide whether to renew based on the + certificate's remaining lifetime. + + Note that this examines the numerically most recent cert version, + not the currently deployed version. + + :returns: whether an attempt should now be made to autorenew the + most current cert version in this lineage + :rtype: bool + + """ + if not lineage.autorenewal_is_enabled(): + return False + + cert = lineage.version("cert", lineage.latest_common_version()) + with open(cert, 'rb') as f: + cert_pem = f.read() + + renewal_time = _ari_renewal_time(lineage, cert_pem, ari_clients) + + now = datetime.datetime.now(datetime.timezone.utc) + + if renewal_time and now > renewal_time: + return True + + # Renewals on the basis of revocation + if lineage.ocsp_revoked(lineage.latest_common_version()): + logger.debug("Should renew, certificate is revoked.") + return True + + # If the renew_before_expiry config field is set, check if it says we should renew + config_interval = lineage.configuration.get("renew_before_expiry") + if config_interval is not None: + notAfter = crypto_util.notAfter(cert) + if notAfter < storage.add_time_interval(now, config_interval): + logger.debug("Should renew, less than %s before certificate " + "expiry %s.", config_interval, + notAfter.strftime("%Y-%m-%d %H:%M:%S %Z")) + return True + # Only use the default if we don't have an ARI or renew_before_expiry value + elif renewal_time is None: + default_renewal_time = _default_renewal_time(cert_pem) + if now > default_renewal_time: + return True + + return False + + +def _avoid_invalidating_lineage(config: configuration.NamespaceConfig, + lineage: storage.RenewableCert, original_server: str) -> None: + """Do not renew a valid cert with one from a staging server!""" + if util.is_staging(config.server): + if not util.is_staging(original_server): + if not config.break_my_certs: + names = san.display(lineage.sans()) + raise errors.Error( + "You've asked to renew/replace a seemingly valid certificate with " + f"a test certificate (domains: {names}). We will not do that " + "unless you use the --break-my-certs flag!") + + +def _avoid_reuse_key_conflicts(config: configuration.NamespaceConfig, + lineage: storage.RenewableCert) -> None: + """Don't allow combining --reuse-key with any flags that would conflict + with key reuse (--key-type, --rsa-key-size, --elliptic-curve), unless + --new-key is also set. + """ + # If --no-reuse-key is set, no conflict + if config.set_by_user("reuse_key") and not config.reuse_key: + return + + # If reuse_key is not set on the lineage and --reuse-key is not + # set on the CLI, no conflict. + if not lineage.reuse_key and not config.reuse_key: + return + + # If --new-key is set, no conflict + if config.new_key: + return + + kt = config.key_type.lower() + + # The remaining cases where conflicts are present: + # - --key-type is set on the CLI and doesn't match the stored private key + # - It's an RSA key and --rsa-key-size is set and doesn't match + # - It's an ECDSA key and --eliptic-curve is set and doesn't match + potential_conflicts = [ + ("--key-type", + lambda: kt != lineage.private_key_type.lower()), + ("--rsa-key-size", + lambda: kt == "rsa" and config.rsa_key_size != lineage.rsa_key_size), + ("--elliptic-curve", + lambda: kt == "ecdsa" and lineage.elliptic_curve and \ + config.elliptic_curve.lower() != lineage.elliptic_curve.lower()) + ] + + for conflict in potential_conflicts: + if conflict[1](): + raise errors.Error( + f"Unable to change the {conflict[0]} of this certificate because --reuse-key " + "is set. To stop reusing the private key, specify --no-reuse-key. " + "To change the private key this one time and then reuse it in future, " + "add --new-key.") + + +def renew_cert(config: configuration.NamespaceConfig, sans: Optional[list[san.SAN]], + le_client: client.Client, lineage: storage.RenewableCert) -> None: + """Renew a certificate lineage.""" + renewal_params = lineage.configuration["renewalparams"] + original_server = renewal_params.get("server", cli.flag_default("server")) + _avoid_invalidating_lineage(config, lineage, original_server) + _avoid_reuse_key_conflicts(config, lineage) + if not sans: + sans = lineage.sans() + # The private key is the existing lineage private key if reuse_key is set. + # Otherwise, generate a fresh private key by passing None. + if config.reuse_key and not config.new_key: + new_key = os.path.normpath(lineage.privkey) + _update_renewal_params_from_key(new_key, config) + else: + new_key = None + new_cert, new_chain, new_key, _ = le_client.obtain_certificate(sans, new_key) + if config.dry_run: + logger.debug("Dry run: skipping updating lineage at %s", os.path.dirname(lineage.cert)) + else: + prior_version = lineage.latest_common_version() + # TODO: Check return value of save_successor + lineage.save_successor(prior_version, new_cert, new_key.pem, new_chain, config) + lineage.update_all_links_to(lineage.latest_common_version()) + lineage.truncate() + + hooks.deploy_hook(config, sans, lineage.live_dir) + + +def report(msgs: Iterable[str], category: str) -> str: + """Format a results report for a category of renewal outcomes""" + lines = ("%s (%s)" % (m, category) for m in msgs) + return " " + "\n ".join(lines) + + +def _renew_describe_results(config: configuration.NamespaceConfig, renew_successes: list[str], + renew_failures: list[str], renew_skipped: list[str], + parse_failures: list[str]) -> None: + """ + Print a report to the terminal about the results of the renewal process. + + :param configuration.NamespaceConfiguration config: Configuration + :param list renew_successes: list of fullchain paths which were renewed + :param list renew_failures: list of fullchain paths which failed to be renewed + :param list renew_skipped: list of messages to print about skipped certificates + :param list parse_failures: list of renewal parameter paths which had errors + """ + notify = display_util.notify + notify_error = logger.error + + notify(f'\n{display_obj.SIDE_FRAME}') + + renewal_noun = "simulated renewal" if config.dry_run else "renewal" + + if renew_skipped: + notify("The following certificates are not due for renewal yet:") + notify(report(renew_skipped, "skipped")) + if not renew_successes and not renew_failures: + notify(f"No {renewal_noun}s were attempted.") + if (config.pre_hook is not None or + config.deploy_hook is not None or config.post_hook is not None): + notify("No hooks were run.") + elif renew_successes and not renew_failures: + notify(f"Congratulations, all {renewal_noun}s succeeded: ") + notify(report(renew_successes, "success")) + elif renew_failures and not renew_successes: + notify_error("All %ss failed. The following certificates could " + "not be renewed:", renewal_noun) + notify_error(report(renew_failures, "failure")) + elif renew_failures and renew_successes: + notify(f"The following {renewal_noun}s succeeded:") + notify(report(renew_successes, "success") + "\n") + notify_error("The following %ss failed:", renewal_noun) + notify_error(report(renew_failures, "failure")) + + if parse_failures: + notify("\nAdditionally, the following renewal configurations " + "were invalid: ") + notify(report(parse_failures, "parsefail")) + + notify(display_obj.SIDE_FRAME) + + +def handle_renewal_request(config: configuration.NamespaceConfig) -> None: + """Examine each lineage; renew if due and report results""" + + sans: list[san.SAN] = config.domains + config.ip_addresses + + # This is trivially False if sans is empty + if any(str(san) not in config.webroot_map for san in sans): + # If more plugins start using cli.add_domain / cli.add_ip_address, + # we may want to only log a warning here + raise errors.Error("Currently, the renew verb is capable of either " + "renewing all installed certificates that are due " + "to be renewed or renewing a single certificate specified " + "by its name using the --cert-name option (-d, --domain, and " + "--ip-address are not valid options for the renew subcommand). If you " + "would like to renew specific certificates by their identifiers, use " + "the certonly command instead. The renew verb may provide other options " + "for selecting certificates to renew in the future.") + + if config.certname: + conf_files = [storage.renewal_file_for_certname(config, config.certname)] + else: + conf_files = storage.renewal_conf_files(config) + + renew_successes = [] + renew_failures = [] + renew_skipped = [] + parse_failures = [] + + renewed_domains = [] + failed_domains = [] + + # Noninteractive renewals include a random delay in order to spread + # out the load on the certificate authority servers, even if many + # users all pick the same time for renewals. This delay precedes + # running any hooks, so that side effects of the hooks (such as + # shutting down a web service) aren't prolonged unnecessarily. + apply_random_sleep = not sys.stdin.isatty() and config.random_sleep_on_renew + + # We initialize acme clients on a per-server basis, but most + # lineages use the same server. Memoize clients here so we can + # share the connection pool and reuse a single fetched directory. + ari_clients = AriClientPool(config) + + for renewal_file in conf_files: + display_util.notification("Processing " + renewal_file, pause=False) + lineage_config = copy.deepcopy(config) + assert renewal_file.endswith(".conf") # make sure lineagename_for_filename will not error + lineagename = storage.lineagename_for_filename(renewal_file) + + # Note that this modifies config (to add back the configuration + # elements from within the renewal configuration file). + try: + renewal_candidate = reconstitute(lineage_config, renewal_file) + except Exception as e: # pylint: disable=broad-except + logger.error("Renewal configuration file %s (cert: %s) " + "produced an unexpected error: %s. Skipping.", + renewal_file, lineagename, e) + logger.debug("Traceback was:\n%s", traceback.format_exc()) + parse_failures.append(renewal_file) + continue + + try: + if not renewal_candidate: + parse_failures.append(renewal_file) + else: + renewal_candidate.ensure_deployed() + from certbot._internal import main + plugins = plugins_disco.PluginsRegistry.find_all() + if should_renew(lineage_config, renewal_candidate, ari_clients): + # Apply random sleep upon first renewal if needed + if apply_random_sleep: + sleep_time = random.uniform(1, 60 * 8) + logger.info("Non-interactive renewal: random delay of %s seconds", + sleep_time) + time.sleep(sleep_time) + # We will sleep only once this day, folks. + apply_random_sleep = False + + # domains have been restored into lineage_config by reconstitute + # but they're unnecessary anyway because renew_cert here + # will just grab them from the certificate + # we already know it's time to renew based on should_renew + # and we have a lineage in renewal_candidate + main.renew_cert(lineage_config, plugins, renewal_candidate) + renew_successes.append(renewal_candidate.fullchain) + renewed_domains.extend(renewal_candidate.sans()) + else: + expiry = crypto_util.notAfter(renewal_candidate.version( + "cert", renewal_candidate.latest_common_version())) + renew_skipped.append("%s expires on %s" % (renewal_candidate.fullchain, + expiry.strftime("%Y-%m-%d"))) + # Run updater interface methods + updater.run_generic_updaters(lineage_config, renewal_candidate, + plugins) + + except Exception as e: # pylint: disable=broad-except + # obtain_certificate (presumably) encountered an unanticipated problem. + logger.error( + "Failed to renew certificate %s with error: %s", + lineagename, e + ) + logger.debug("Traceback was:\n%s", traceback.format_exc()) + if renewal_candidate: + renew_failures.append(renewal_candidate.fullchain) + failed_domains.extend(renewal_candidate.sans()) + + # Describe all the results + _renew_describe_results(config, renew_successes, renew_failures, + renew_skipped, parse_failures) + + hooks.run_saved_post_hooks(renewed_domains, failed_domains) + + if renew_failures or parse_failures: + raise errors.Error( + f"{len(renew_failures)} renew failure(s), {len(parse_failures)} parse failure(s)") + + logger.debug("no renewal failures") + + +def _update_renewal_params_from_key(key_path: str, config: configuration.NamespaceConfig) -> None: + with open(key_path, 'rb') as file_h: + key = load_pem_private_key(file_h.read(), password=None, backend=default_backend()) + if isinstance(key, rsa.RSAPrivateKey): + config.key_type = 'rsa' + config.rsa_key_size = key.key_size + elif isinstance(key, ec.EllipticCurvePrivateKey): + config.key_type = 'ecdsa' + config.elliptic_curve = key.curve.name + else: + raise errors.Error(f'Key at {key_path} is of an unsupported type: {type(key)}.') diff --git a/certbot/src/certbot/_internal/san.py b/certbot/src/certbot/_internal/san.py new file mode 100644 index 00000000000..96ba4e2fa83 --- /dev/null +++ b/certbot/src/certbot/_internal/san.py @@ -0,0 +1,130 @@ +"""Types for representing IP addresses and DNS names internal to Certbot.""" +import ipaddress +from abc import abstractmethod +from typing import Any, Iterable + +from acme import crypto_util as acme_crypto_util +from cryptography import x509 + +from certbot.util import enforce_domain_sanity + +class SAN: + """A domain or IP address. + + These are Certbot-internal types, independent of the acme module's messages.Identifier. + """ + @abstractmethod + def is_wildcard(self) -> bool: + """Return True if this is a wildcard DNS name.""" + +class DNSName(SAN): + """An FQDN or wildcard domain name. + + Raises ConfigurationError if the domain name is syntactically invalid. + + Normalizes inputs by converting to lowercase and removing a trailing dot, if present. + """ + def __init__(self, dns_name: str) -> None: + if not isinstance(dns_name, str): + raise TypeError("tried to initialize DNSName with non-str") + self.dns_name = enforce_domain_sanity(dns_name) + + def __str__(self) -> str: + return self.dns_name + + def __hash__(self) -> int: + return hash(self.dns_name) + + def __repr__(self) -> str: + return 'DNS(%s)' % self.dns_name + + def __eq__(self, other: Any) -> bool: + match other: + case DNSName(): + return self.dns_name == other.dns_name + case IPAddress(): + return False + case _: + raise TypeError(f"DNSName SAN compared to non-SAN: {type(other)}") + + def is_wildcard(self) -> bool: + """Return True if this DNS name is a wildcard.""" + return self.dns_name.startswith('*.') + +class IPAddress(SAN): + """An IP address (IPv4 or IPv6). + + Validated upon construction. + """ + def __init__(self, ip_address: str) -> None: + self.ip_address = ipaddress.ip_address(ip_address) + + def __str__(self) -> str: + return str(self.ip_address) + + def __hash__(self) -> int: + return hash(self.ip_address) + + def __repr__(self) -> str: + return 'IP(%s)' % self.ip_address + + def __eq__(self, other: Any) -> bool: + match other: + case IPAddress(): + return self.ip_address == other.ip_address + case DNSName(): + return False + case _: + raise TypeError(f"IPAddress SAN compared to non-SAN: {type(other)}") + + def is_wildcard(self) -> bool: + """Always False.""" + return False + +def guess(names: Iterable[str]) -> list[SAN]: + """Turn a list of strings in to a list of SANs based on how they parse.""" + sans: list[SAN] = [] + for name in names: + try: + sans.append(IPAddress(name)) + except ValueError: + sans.append(DNSName(name)) + return sans + +def split(sans: Iterable[SAN]) -> tuple[list[DNSName], list[IPAddress]]: + """Split a list of SANs into a list of DNSNames and one of IPAddress, in that order.""" + domains = [] + ip_addresses = [] + for s in sans: + match s: + case IPAddress(): + ip_addresses.append(s) + case DNSName(): + domains.append(s) + case _: + raise TypeError(f"SAN of type {type(s)}") + return domains, ip_addresses + +def join(dns_names: Iterable[DNSName], ip_addresses: Iterable[IPAddress]) -> list[SAN]: + """Combine a list of DNS names and a list of IP addresses.""" + return list(dns_names) + list(ip_addresses) + +def display(sans: Iterable[SAN]) -> str: + """Return the list of SANs in string form, separated by comma and space.""" + return ", ".join(map(str, sans)) + +def from_x509(subject: x509.Name, exts: x509.Extensions) -> tuple[list[DNSName], list[IPAddress]]: + """Get all DNS names and IP addresses, plus the first Common Name from subject. + + The CN will be first in the list, if present. It will always be interpreted + as a DNS name. + + :param subject: Name of the x509 object, which may include Common Name + :type subject: `cryptography.x509.Name` + :param exts: Extensions of the x509 object, which may include SANs + :type exts: `cryptography.x509.Extensions` + + :returns: Tuple containing a list of DNSNames and a list of IPAddresses + """ + dns_names, ip_addresses = acme_crypto_util.get_identifiers_from_x509(subject, exts) + return [DNSName(d) for d in dns_names], [IPAddress(i) for i in ip_addresses] diff --git a/certbot/src/certbot/_internal/snap_config.py b/certbot/src/certbot/_internal/snap_config.py new file mode 100644 index 00000000000..072961dde6d --- /dev/null +++ b/certbot/src/certbot/_internal/snap_config.py @@ -0,0 +1,147 @@ +"""Module configuring Certbot in a snap environment""" +from __future__ import annotations +import logging +import socket +from typing import Iterable +from typing import Optional +from typing import Union + +from requests import PreparedRequest, Session +from requests.adapters import HTTPAdapter +from requests.exceptions import HTTPError +from requests.exceptions import RequestException + +from certbot.compat import os +from certbot.errors import Error + +try: + from urllib3.connection import HTTPConnection + from urllib3.connectionpool import HTTPConnectionPool +except ImportError: + # Stub imports for oldest requirements, that will never be used in snaps. + HTTPConnection = object # type: ignore[misc,assignment] + HTTPConnectionPool = object # type: ignore[misc,assignment] + + +_ARCH_TRIPLET_MAP = { + 'arm64': 'aarch64-linux-gnu', + 'armhf': 'arm-linux-gnueabihf', + 'i386': 'i386-linux-gnu', + 'ppc64el': 'powerpc64le-linux-gnu', + 'powerpc': 'powerpc-linux-gnu', + 'amd64': 'x86_64-linux-gnu', + 's390x': 's390x-linux-gnu', +} +CURRENT_PYTHON_VERSION_STRING = 'python3.12' + +LOGGER = logging.getLogger(__name__) + + +def prepare_env(cli_args: list[str]) -> list[str]: + """ + Prepare runtime environment for a certbot execution in snap. + :param list cli_args: List of command line arguments + :return: Update list of command line arguments + :rtype: list + """ + snap_arch = os.environ.get('SNAP_ARCH') + + if snap_arch not in _ARCH_TRIPLET_MAP: + raise Error('Unrecognized value of SNAP_ARCH: {0}'.format(snap_arch)) + + os.environ['CERTBOT_AUGEAS_PATH'] = '{0}/usr/lib/{1}/libaugeas.so.0'.format( + os.environ.get('SNAP'), _ARCH_TRIPLET_MAP[snap_arch]) + + with Session() as session: + session.mount('http://snapd/', _SnapdAdapter()) + + try: + response = session.get('http://snapd/v2/connections?snap=certbot&interface=content', + timeout=30.0) + response.raise_for_status() + except RequestException as e: + if isinstance(e, HTTPError) and e.response.status_code == 404: + LOGGER.error('An error occurred while fetching Certbot snap plugins: ' + 'your version of snapd is outdated.') + LOGGER.error('Please run "sudo snap install core; sudo snap refresh core" ' + 'in your terminal and try again.') + else: + LOGGER.error('An error occurred while fetching Certbot snap plugins: ' + 'make sure the snapd service is running.') + raise e + + data = response.json() + connections = [] + outdated_plugins = [] + for plugin in data.get('result', {}).get('established', []): + plug: str = plugin.get('plug', {}).get('plug') + plug_content: str = plugin.get('plug-attrs', {}).get('content') + if plug == 'plugin' and plug_content == 'certbot-1': + plugin_name: str = plugin['slot']['snap'] + # First, check that the plugin is using our expected python version, + # i.e. its "read" slot is something like + # "$SNAP/lib/python3.12/site-packages". If not, skip it and print an + # error. + slot_read: str = plugin.get('slot-attrs', {}).get('read', []) + if len(slot_read) != 0 and CURRENT_PYTHON_VERSION_STRING not in slot_read[0]: + outdated_plugins.append(plugin_name) + continue + + connections.append('/snap/{0}/current/lib/{1}/site-packages/'.format( + plugin_name, + CURRENT_PYTHON_VERSION_STRING + )) + + if outdated_plugins: + LOGGER.warning('The following plugins are using an outdated python version and must be ' + 'updated to be compatible with Certbot 3.0. Please see ' + 'https://community.letsencrypt.org/t/' + 'certbot-3-0-could-have-potential-third-party-snap-breakages/226940 ' + 'for more information:') + plugin_list = '\n'.join(' * {}'.format(plugin) for plugin in outdated_plugins) + LOGGER.warning(plugin_list) + + os.environ['CERTBOT_PLUGIN_PATH'] = ':'.join(connections) + + cli_args.append('--preconfigured-renewal') + + return cli_args + + +class _SnapdConnection(HTTPConnection): + def __init__(self) -> None: + super().__init__("localhost") + self.sock: Optional[socket.socket] = None + + def connect(self) -> None: + self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.sock.connect("/run/snapd.socket") + + +class _SnapdConnectionPool(HTTPConnectionPool): + def __init__(self) -> None: + super().__init__("localhost") + + def _new_conn(self) -> _SnapdConnection: + return _SnapdConnection() + + +class _SnapdAdapter(HTTPAdapter): + # get_connection is used with versions of requests before 2.32.2 and + # get_connection_with_tls_context is used instead in versions after that. as of + # writing this, Certbot in EPEL 9 is still seeing updates and they have requests 2.25.1 so to + # help out those packagers while ensuring this code works reliably, we offer custom versions of + # both functions for now. when certbot does declare a dependency on requests>=2.32.2 in its + # setup.py files, get_connection can be deleted + def get_connection(self, url: str | bytes, + proxies: Optional[Iterable[str]] = None) -> _SnapdConnectionPool: + return _SnapdConnectionPool() + + def get_connection_with_tls_context(self, request: PreparedRequest, + verify: bool | str | None, + proxies: Optional[Iterable[str]] = None, + cert: Optional[Union[str, tuple[str,str]]] = None + ) -> _SnapdConnectionPool: + """Required method for creating a new connection pool. Simply return our + shim that forces a UNIX socket connection to snapd.""" + return _SnapdConnectionPool() diff --git a/certbot/src/certbot/_internal/storage.py b/certbot/src/certbot/_internal/storage.py new file mode 100644 index 00000000000..a7f19d0dc69 --- /dev/null +++ b/certbot/src/certbot/_internal/storage.py @@ -0,0 +1,1256 @@ +"""Renewable certificates storage.""" +# pylint: disable=too-many-lines +import datetime +import glob +import logging +import re +import shutil +import stat +from typing import Any +from typing import cast +from typing import Iterable +from typing import Mapping +from typing import Optional +from typing import Union + +import configobj +from cryptography import x509 +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePrivateKey +from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey +from cryptography.hazmat.primitives.serialization import load_pem_private_key +import parsedatetime + +import certbot +from certbot import configuration +from certbot import crypto_util +from certbot import errors +from certbot import interfaces +from certbot import util +from certbot._internal import error_handler +from certbot._internal import ocsp +from certbot._internal import san +from certbot._internal.plugins import disco as plugins_disco +from certbot.compat import filesystem +from certbot.compat import os +from certbot.plugins import common as plugins_common +from certbot.util import parse_loose_version + +logger = logging.getLogger(__name__) + +ALL_FOUR = ("cert", "privkey", "chain", "fullchain") +README = "README" +CURRENT_VERSION = parse_loose_version(certbot.__version__) +BASE_PRIVKEY_MODE = 0o600 + +# pylint: disable=too-many-lines + + +def renewal_conf_files(config: configuration.NamespaceConfig) -> list[str]: + """Build a list of all renewal configuration files. + + :param configuration.NamespaceConfig config: Configuration object + + :returns: list of renewal configuration files + :rtype: `list` of `str` + + """ + result = glob.glob(os.path.join(config.renewal_configs_dir, "*.conf")) + result.sort() + return result + + +def renewal_file_for_certname(config: configuration.NamespaceConfig, certname: str) -> str: + """Return /path/to/certname.conf in the renewal conf directory""" + path = os.path.join(config.renewal_configs_dir, f"{certname}.conf") + if not os.path.exists(path): + raise errors.CertStorageError( + f"No certificate found with name {certname} (expected {path}).") + return path + + +def cert_path_for_cert_name(config: configuration.NamespaceConfig, cert_name: str) -> str: + """ If `--cert-name` was specified, but you need a value for `--cert-path`. + + :param configuration.NamespaceConfig config: parsed command line arguments + :param str cert_name: cert name. + + """ + cert_name_implied_conf = renewal_file_for_certname(config, cert_name) + return configobj.ConfigObj( + cert_name_implied_conf, encoding='utf-8', default_encoding='utf-8')["fullchain"] + + +def add_time_interval(base_time: datetime.datetime, interval: str, + textparser: parsedatetime.Calendar = parsedatetime.Calendar() + ) -> datetime.datetime: + """Parse the time specified time interval, and add it to the base_time + + The interval can be in the English-language format understood by + parsedatetime, e.g., '10 days', '3 weeks', '6 months', '9 hours', or + a sequence of such intervals like '6 months 1 week' or '3 days 12 + hours'. If an integer is found with no associated unit, it is + interpreted by default as a number of days. + + :param datetime.datetime base_time: The time to be added with the interval. + :param str interval: The time interval to parse. + + :returns: The base_time plus the interpretation of the time interval. + :rtype: :class:`datetime.datetime`""" + + if interval.strip().isdigit(): + interval += " days" + + # try to use the same timezone, but fallback to UTC + tzinfo = base_time.tzinfo or datetime.timezone.utc + + return textparser.parseDT(interval, base_time, tzinfo=tzinfo)[0] + + +def create_renewal_config_file(filename: str, archive_dir: str, + target: Mapping[str, str], + cli_config: configuration.NamespaceConfig) -> None: + """Creates a renewal config file with the specified name and values. + + :param str filename: Absolute path to the new destination of config file + :param str archive_dir: Absolute path to the archive directory + :param dict target: Maps ALL_FOUR to their symlink paths + :param .NamespaceConfig cli_config: parsed command line + arguments + """ + config = make_renewal_configobj(archive_dir, target, cli_config) + with open(filename, "wb") as f: + config.write(outfile=f) + + +def atomic_rewrite(config_filename: str, new_config: configobj.ConfigObj) -> None: + """Update a config file on disk with the provided values. + + The update will be atomic: if writing fails, the original file will not be modified. The + updated file will preserve comments from the original and will have the same permissions. + + In general, fields that exist on disk but not in new_config will be preserved. As a special + case, fields in the 'renewalparams' section will be deleted unless they exist in new_config. + This deletion clears out old fields from when we used to dump _all_ flags (as opposed to + relevant ones). + + :param str config_filename: Absolute path to the configuration file + :param configobj.ConfigObj new_config: New configuration object + """ + # Read the existing values from `config_filename`, or error (file_error=True) + merged_config = configobj.ConfigObj(config_filename, encoding='utf-8', default_encoding='utf-8', + file_error=True) + merged_config.merge(new_config) + + # When updating the "renewalparams" section, only carry through fields that actually exist + # in the new config's "renewalparams" section. We could achieve this straightforwardly by + # assigning the new config's "renewalparams" section into the merged config. But then we would + # lose comments. Merging and then deleting allows us to preserve comments. + # + # As a concrete example, if a renewal params config has elliptic_curve=secp384r1, and the + # user executes a command to issue with `--key-type=rsa` instead, the old elliptic_curve value + # should disappear because it's not specified in the current command line. + # + # This only applies when "renewalparams" is actually being updated, because sometimes we + # update other sections independently (like "acme_renewal_info"). + if "renewalparams" in new_config: + for k in merged_config["renewalparams"]: + if k not in new_config["renewalparams"]: + del merged_config["renewalparams"][k] + + current_permissions = stat.S_IMODE(os.lstat(config_filename).st_mode) + + temp_filename = config_filename + ".new" + + # If an existing tempfile exists, delete it + if os.path.exists(temp_filename): + os.unlink(temp_filename) + + with util.safe_open(temp_filename, "wb", current_permissions) as f: + merged_config.write(outfile=f) + + filesystem.replace(temp_filename, config_filename) + + +def update_configuration(lineagename: str, archive_dir: str, target: Mapping[str, str], + cli_config: configuration.NamespaceConfig) -> configobj.ConfigObj: + """Modifies lineagename's config to contain the specified values. + + :param str lineagename: Name of the lineage being modified + :param str archive_dir: Absolute path to the archive directory + :param dict target: Maps ALL_FOUR to their symlink paths + :param .NamespaceConfig cli_config: parsed command line arguments + + :returns: Configuration object for the updated config file + :rtype: configobj.ConfigObj + + """ + config = make_renewal_configobj(archive_dir, target, cli_config) + + config_filename = renewal_filename_for_lineagename(cli_config, lineagename) + + atomic_rewrite(config_filename, config) + + return configobj.ConfigObj(config_filename, encoding='utf-8', default_encoding='utf-8') + + +def make_renewal_configobj(archive_dir: str, target: Mapping[str, str], + cli_config: configuration.NamespaceConfig) -> configobj.ConfigObj: + """Create a configobj.ConfigObj representing the provided values. + + :param str archive_dir: Absolute path to the archive directory + :param dict target: Maps ALL_FOUR to their symlink paths + :param .NamespaceConfig cli_config: parsed command line arguments + + :returns: Configuration object representing the inputs + :rtype: configobj.ConfigObj + """ + config = configobj.ConfigObj(encoding='utf-8', default_encoding='utf-8') + config["version"] = certbot.__version__ + config["archive_dir"] = archive_dir + for kind in ALL_FOUR: + config[kind] = target[kind] + config['renewalparams'] = {} + config['renewalparams'].update(relevant_values(cli_config)) + + # MAGIC CODE ALERT + # In keeping with the code in RenewableCert.__init__, we use deploy_hook internally, + # but write out the value as renew_hook to allow downgrade compatibility. + # So, if there's a deploy_hook (the internal name), change it to renew_hook (the renewal + # config file name). + if "deploy_hook" in config["renewalparams"]: + config["renewalparams"]["renew_hook"] = config["renewalparams"]["deploy_hook"] + del config["renewalparams"]["deploy_hook"] + + return config + + +def get_link_target(link: str) -> str: + """Get an absolute path to the target of link. + + :param str link: Path to a symbolic link + + :returns: Absolute path to the target of link + :rtype: str + + :raises .CertStorageError: If link does not exists. + + """ + try: + target = filesystem.readlink(link) + except OSError: + raise errors.CertStorageError( + "Expected {0} to be a symlink".format(link)) + + if not os.path.isabs(target): + target = os.path.join(os.path.dirname(link), target) + return os.path.abspath(target) + + +def _write_live_readme_to(readme_path: str, is_base_dir: bool = False) -> None: + prefix = "" + if is_base_dir: + prefix = "[cert name]/" + with open(readme_path, "w") as f: + logger.debug("Writing README to %s.", readme_path) + f.write("This directory contains your keys and certificates.\n\n" + "`{prefix}privkey.pem` : the private key for your certificate.\n" + "`{prefix}fullchain.pem`: the certificate file used in most server software.\n" + "`{prefix}chain.pem` : used for OCSP stapling in Nginx >=1.3.7.\n" + "`{prefix}cert.pem` : will break many server configurations, and " + "should not be used\n" + " without reading further documentation (see link below).\n\n" + "WARNING: DO NOT MOVE OR RENAME THESE FILES!\n" + " Certbot expects these files to remain in this location in order\n" + " to function properly!\n\n" + "We recommend not moving these files. For more information, see the Certbot\n" + "User Guide at https://certbot.eff.org/docs/using.html#where-are-my-" + "certificates.\n".format(prefix=prefix)) + + +def _relevant(namespaces: Iterable[str], option: str) -> bool: + """ + Is this option one that could be restored for future renewal purposes? + + :param namespaces: plugin namespaces for configuration options + :type namespaces: `list` of `str` + :param str option: the name of the option + + :rtype: bool + """ + from certbot._internal import renewal + + return (option in renewal.CONFIG_ITEMS or + any(option.startswith(namespace) for namespace in namespaces)) + + +def relevant_values(config: configuration.NamespaceConfig) -> dict[str, Any]: + """Return a new dict containing only items relevant for renewal. + + :param .NamespaceConfig config: parsed command line + + :returns: A new dictionary containing items that can be used in renewal. + :rtype dict: + + """ + all_values = config.to_dict() + plugins = plugins_disco.PluginsRegistry.find_all() + namespaces = [plugins_common.dest_namespace(plugin) for plugin in plugins] + + rv = { + option: value + for option, value in all_values.items() + if _relevant(namespaces, option) and config.set_by_user(option) + } + # We always save the server value to help with forward compatibility + # and behavioral consistency when versions of Certbot with different + # server defaults are used. + rv["server"] = all_values["server"] + + # Save key type to help with forward compatibility on Certbot's transition + # from RSA to ECDSA certificates by default. + rv["key_type"] = all_values["key_type"] + + return rv + + +def lineagename_for_filename(config_filename: str) -> str: + """Returns the lineagename for a configuration filename. + """ + if not config_filename.endswith(".conf"): + raise errors.CertStorageError( + "renewal config file name must end in .conf") + return os.path.basename(config_filename[:-len(".conf")]) + + +def renewal_filename_for_lineagename(config: configuration.NamespaceConfig, + lineagename: str) -> str: + """Returns the lineagename for a configuration filename. + """ + return os.path.join(config.renewal_configs_dir, lineagename) + ".conf" + + +def _relpath_from_file(archive_dir: str, from_file: str) -> str: + """Path to a directory from a file""" + return os.path.relpath(archive_dir, os.path.dirname(from_file)) + + +def full_archive_path(config_obj: configobj.ConfigObj, cli_config: configuration.NamespaceConfig, + lineagename: str) -> str: + """Returns the full archive path for a lineagename + + Uses cli_config to determine archive path if not available from config_obj. + + :param configobj.ConfigObj config_obj: Renewal conf file contents (can be None) + :param configuration.NamespaceConfig cli_config: Main config file + :param str lineagename: Certificate name + """ + if config_obj and "archive_dir" in config_obj: + return config_obj["archive_dir"] + return os.path.join(cli_config.default_archive_dir, lineagename) + + +def _full_live_path(cli_config: configuration.NamespaceConfig, lineagename: str) -> str: + """Returns the full default live path for a lineagename""" + return os.path.join(cli_config.live_dir, lineagename) + + +def delete_files(config: configuration.NamespaceConfig, certname: str) -> None: + """Delete all files related to the certificate. + + If some files are not found, ignore them and continue. + """ + renewal_filename = renewal_file_for_certname(config, certname) + # file exists + full_default_archive_dir = full_archive_path(None, config, certname) + full_default_live_dir = _full_live_path(config, certname) + try: + renewal_config = configobj.ConfigObj( + renewal_filename, encoding='utf-8', default_encoding='utf-8') + except configobj.ConfigObjError: + # config is corrupted + logger.error("Could not parse %s. You may wish to manually " + "delete the contents of %s and %s.", renewal_filename, + full_default_live_dir, full_default_archive_dir) + raise errors.CertStorageError( + "error parsing {0}".format(renewal_filename)) + finally: + # we couldn't read it, but let's at least delete it + # if this was going to fail, it already would have. + os.remove(renewal_filename) + logger.info("Removed %s", renewal_filename) + + # cert files and (hopefully) live directory + # it's not guaranteed that the files are in our default storage + # structure. so, first delete the cert files. + directory_names = set() + for kind in ALL_FOUR: + link = renewal_config.get(kind) + try: + os.remove(link) + logger.debug("Removed %s", link) + except OSError: + logger.debug("Unable to delete %s", link) + directory = os.path.dirname(link) + directory_names.add(directory) + + # if all four were in the same directory, and the only thing left + # is the README file (or nothing), delete that directory. + # this will be wrong in very few but some cases. + if len(directory_names) == 1: + # delete the README file + directory = directory_names.pop() + readme_path = os.path.join(directory, README) + try: + os.remove(readme_path) + logger.debug("Removed %s", readme_path) + except OSError: + logger.debug("Unable to delete %s", readme_path) + # if it's now empty, delete the directory + try: + os.rmdir(directory) # only removes empty directories + logger.debug("Removed %s", directory) + except OSError: + logger.debug("Unable to remove %s; may not be empty.", directory) + + # archive directory + try: + archive_path = full_archive_path(renewal_config, config, certname) + shutil.rmtree(archive_path) + logger.debug("Removed %s", archive_path) + except OSError: + logger.debug("Unable to remove %s", archive_path) + + +class RenewableCert(interfaces.RenewableCert): + """Renewable certificate. + + Represents a lineage of certificates that is under the management of + Certbot, indicated by the existence of an associated renewal + configuration file. + + Note that the notion of "current version" for a lineage is + maintained on disk in the structure of symbolic links, and is not + explicitly stored in any instance variable in this object. The + RenewableCert object is able to determine information about the + current (or other) version by accessing data on disk, but does not + inherently know any of this information except by examining the + symbolic links as needed. The instance variables mentioned below + point to symlinks that reflect the notion of "current version" of + each managed object, and it is these paths that should be used when + configuring servers to use the certificate managed in a lineage. + These paths are normally within the "live" directory, and their + symlink targets -- the actual cert files -- are normally found + within the "archive" directory. + + :ivar str cert: The path to the symlink representing the current + version of the certificate managed by this lineage. + :ivar str privkey: The path to the symlink representing the current + version of the private key managed by this lineage. + :ivar str chain: The path to the symlink representing the current version + of the chain managed by this lineage. + :ivar str fullchain: The path to the symlink representing the + current version of the fullchain (combined chain and cert) + managed by this lineage. + :ivar configobj.ConfigObj configuration: The renewal configuration + options associated with this lineage, obtained from parsing the + renewal configuration file and/or systemwide defaults. + + """ + def __init__(self, config_filename: str, cli_config: configuration.NamespaceConfig) -> None: + """Instantiate a RenewableCert object from an existing lineage. + + :param str config_filename: the path to the renewal config file + that defines this lineage. + :param .NamespaceConfig: parsed command line arguments + + :raises .CertStorageError: if the configuration file's name didn't end + in ".conf", or the file is missing or broken. + + """ + self.cli_config = cli_config + self._lineagename = lineagename_for_filename(config_filename) + + try: + self.configfile = configobj.ConfigObj( + config_filename, encoding='utf-8', default_encoding='utf-8') + except configobj.ConfigObjError: + raise errors.CertStorageError( + "error parsing {0}".format(config_filename)) + + # These are equivalent. Previously we were adding the unused default + # value of renew_before_expiry. Keeping both names because cleaning + # out the variables from callers is annoying. Ideally new code should + # use self.configfile so we can remove self.configuration at some point, + # but either should work currently. + self.configuration = self.configfile + + if not all(x in self.configuration for x in ALL_FOUR): + raise errors.CertStorageError( + "renewal config file {0} is missing a required " + "file reference".format(self.configfile)) + + conf_version = self.configuration.get("version") + if (conf_version is not None and + parse_loose_version(conf_version) > CURRENT_VERSION): + logger.info( + "Attempting to parse the version %s renewal configuration " + "file found at %s with version %s of Certbot. This might not " + "work.", conf_version, config_filename, certbot.__version__) + + self.cert = self.configuration["cert"] + self.privkey = self.configuration["privkey"] + self.chain = self.configuration["chain"] + self.fullchain = self.configuration["fullchain"] + self.live_dir = os.path.dirname(self.cert) + + # MAGIC CODE ALERT + # We changed the name of the internal property from deploy hook to renew hook + # There are already configs out there with renew_hook saved, so we're going to keep saving + # it out as renew_hook to allow downgrade compatibility. Load it in as deploy + # hook. Then, renewal.py's STR_CONFIG_ITEMS will check against the new internal name. + if "renewalparams" in self.configuration: + if "renew_hook" in self.configuration["renewalparams"]: + self.configuration["renewalparams"]["deploy_hook"] = \ + self.configuration["renewalparams"]["renew_hook"] + del self.configuration["renewalparams"]["renew_hook"] + + self._fix_symlinks() + self._check_symlinks() + + @property + def key_path(self) -> str: + """Duck type for self.privkey""" + return self.privkey + + @property + def cert_path(self) -> str: + """Duck type for self.cert""" + return self.cert + + @property + def chain_path(self) -> str: + """Duck type for self.chain""" + return self.chain + + @property + def fullchain_path(self) -> str: + """Duck type for self.fullchain""" + return self.fullchain + + @property + def lineagename(self) -> str: + """Name given to the certificate lineage. + + :rtype: str + + """ + return self._lineagename + + @property + def target_expiry(self) -> datetime.datetime: + """The current target certificate's expiration datetime + + :returns: Expiration datetime of the current target certificate + :rtype: :class:`datetime.datetime` + """ + cert_path = self.current_target("cert") + if not cert_path: + raise errors.Error("Target certificate does not exist.") + return crypto_util.notAfter(cert_path) + + @property + def archive_dir(self) -> str: + """Returns the default or specified archive directory""" + return full_archive_path(self.configuration, + self.cli_config, self.lineagename) + + def relative_archive_dir(self, from_file: str) -> str: + """Returns the default or specified archive directory as a relative path + + Used for creating symbolic links. + """ + return _relpath_from_file(self.archive_dir, from_file) + + @property + def server(self) -> Optional[str]: + """Returns the ACME server associated with this certificate""" + return self.configuration["renewalparams"].get("server", None) + + @property + def is_test_cert(self) -> bool: + """Returns true if this is a test cert from a staging server.""" + if self.server: + return util.is_staging(self.server) + return False + + @property + def reuse_key(self) -> bool: + """Returns whether this certificate is configured to reuse its private key""" + return "reuse_key" in self.configuration["renewalparams"] and \ + self.configuration["renewalparams"].as_bool("reuse_key") + + def _check_symlinks(self) -> None: + """Raises an exception if a symlink doesn't exist""" + for kind in ALL_FOUR: + link = getattr(self, kind) + if not os.path.islink(link): + raise errors.CertStorageError( + "expected {0} to be a symlink".format(link)) + target = get_link_target(link) + if not os.path.exists(target): + raise errors.CertStorageError("target {0} of symlink {1} does " + "not exist".format(target, link)) + + def _consistent(self) -> bool: + """Are the files associated with this lineage self-consistent? + + :returns: Whether the files stored in connection with this + lineage appear to be correct and consistent with one + another. + :rtype: bool + + """ + # Each element must be referenced with an absolute path + for x in (self.cert, self.privkey, self.chain, self.fullchain): + if not os.path.isabs(x): + logger.debug("Element %s is not referenced with an " + "absolute path.", x) + return False + + # Each element must exist and be a symbolic link + for x in (self.cert, self.privkey, self.chain, self.fullchain): + if not os.path.islink(x): + logger.debug("Element %s is not a symbolic link.", x) + return False + for kind in ALL_FOUR: + link = getattr(self, kind) + target = get_link_target(link) + + # Each element's link must point within the cert lineage's + # directory within the official archive directory + if not os.path.samefile(os.path.dirname(target), self.archive_dir): + logger.debug("Element's link does not point within the " + "cert lineage's directory within the " + "official archive directory. Link: %s, " + "target directory: %s, " + "archive directory: %s.", + link, os.path.dirname(target), self.archive_dir) + return False + + # The link must point to a file that exists + if not os.path.exists(target): + logger.debug("Link %s points to file %s that does not exist.", + link, target) + return False + + # The link must point to a file that follows the archive + # naming convention + pattern = re.compile(r"^{0}([0-9]+)\.pem$".format(kind)) + if not pattern.match(os.path.basename(target)): + logger.debug("%s does not follow the archive naming " + "convention.", target) + return False + + # It is NOT required that the link's target be a regular + # file (it may itself be a symlink). But we should probably + # do a recursive check that ultimately the target does + # exist? + # XXX: Additional possible consistency checks (e.g. + # cryptographic validation of the chain being a chain, + # the chain matching the cert, and the cert matching + # the subject key) + # XXX: All four of the targets are in the same directory + # (This check is redundant with the check that they + # are all in the desired directory!) + # len(set(os.path.basename(self.current_target(x) + # for x in ALL_FOUR))) == 1 + return True + + def _fix(self) -> None: + """Attempt to fix defects or inconsistencies in this lineage. + + .. todo:: Currently unimplemented. + + """ + # TODO: Figure out what kinds of fixes are possible. For + # example, checking if there is a valid version that + # we can update the symlinks to. (Maybe involve + # parsing keys and certs to see if they exist and + # if a key corresponds to the subject key of a cert?) + + # TODO: In general, the symlink-reading functions below are not + # cautious enough about the possibility that links or their + # targets may not exist. (This shouldn't happen, but might + # happen as a result of random tampering by a sysadmin, or + # filesystem errors, or crashes.) + + def _previous_symlinks(self) -> list[tuple[str, str]]: + """Returns the kind and path of all symlinks used in recovery. + + :returns: list of (kind, symlink) tuples + :rtype: list + + """ + previous_symlinks = [] + for kind in ALL_FOUR: + link_dir = os.path.dirname(getattr(self, kind)) + link_base = "previous_{0}.pem".format(kind) + previous_symlinks.append((kind, os.path.join(link_dir, link_base))) + + return previous_symlinks + + def _fix_symlinks(self) -> None: + """Fixes symlinks in the event of an incomplete version update. + + If there is no problem with the current symlinks, this function + has no effect. + + """ + previous_symlinks = self._previous_symlinks() + if all(os.path.exists(link[1]) for link in previous_symlinks): + for kind, previous_link in previous_symlinks: + current_link = getattr(self, kind) + if os.path.lexists(current_link): + os.unlink(current_link) + os.symlink(filesystem.readlink(previous_link), current_link) + + for _, link in previous_symlinks: + if os.path.exists(link): + os.unlink(link) + + def current_target(self, kind: str) -> Optional[str]: + """Returns full path to which the specified item currently points. + + :param str kind: the lineage member item ("cert", "privkey", + "chain", or "fullchain") + + :returns: The path to the current version of the specified + member. + :rtype: str or None + + """ + if kind not in ALL_FOUR: + raise errors.CertStorageError("unknown kind of item") + link = getattr(self, kind) + if not os.path.exists(link): + logger.debug("Expected symlink %s for %s does not exist.", + link, kind) + return None + return get_link_target(link) + + def current_version(self, kind: str) -> Optional[int]: + """Returns numerical version of the specified item. + + For example, if kind is "chain" and the current chain link + points to a file named "chain7.pem", returns the integer 7. + + :param str kind: the lineage member item ("cert", "privkey", + "chain", or "fullchain") + + :returns: the current version of the specified member. + :rtype: int + + """ + if kind not in ALL_FOUR: + raise errors.CertStorageError("unknown kind of item") + pattern = re.compile(r"^{0}([0-9]+)\.pem$".format(kind)) + target = self.current_target(kind) + if target is None or not os.path.exists(target): + logger.debug("Current-version target for %s " + "does not exist at %s.", kind, target) + target = "" + matches = pattern.match(os.path.basename(target)) + if matches: + return int(matches.groups()[0]) + logger.debug("No matches for target %s.", kind) + return None + + def version(self, kind: str, version: int) -> str: + """The filename that corresponds to the specified version and kind. + + .. warning:: The specified version may not exist in this + lineage. There is no guarantee that the file path returned + by this method actually exists. + + :param str kind: the lineage member item ("cert", "privkey", + "chain", or "fullchain") + :param int version: the desired version + + :returns: The path to the specified version of the specified member. + :rtype: str + + """ + if kind not in ALL_FOUR: + raise errors.CertStorageError("unknown kind of item") + link = self.current_target(kind) + if not link: + raise errors.Error(f"Target {kind} does not exist!") + where = os.path.dirname(link) + return os.path.join(where, "{0}{1}.pem".format(kind, version)) + + def available_versions(self, kind: str) -> list[int]: + """Which alternative versions of the specified kind of item exist? + + The archive directory where the current version is stored is + consulted to obtain the list of alternatives. + + :param str kind: the lineage member item ( + ``cert``, ``privkey``, ``chain``, or ``fullchain``) + + :returns: all of the version numbers that currently exist + :rtype: `list` of `int` + + """ + if kind not in ALL_FOUR: + raise errors.CertStorageError("unknown kind of item") + link = self.current_target(kind) + if not link: + raise errors.Error(f"Target {kind} does not exist!") + where = os.path.dirname(link) + files = os.listdir(where) + pattern = re.compile(r"^{0}([0-9]+)\.pem$".format(kind)) + matches = [pattern.match(f) for f in files] + return sorted([int(m.groups()[0]) for m in matches if m]) + + def newest_available_version(self, kind: str) -> int: + """Newest available version of the specified kind of item? + + :param str kind: the lineage member item (``cert``, + ``privkey``, ``chain``, or ``fullchain``) + + :returns: the newest available version of this member + :rtype: int + + """ + return max(self.available_versions(kind)) + + def latest_common_version(self) -> int: + """Newest version for which all items are available? + + :returns: the newest available version for which all members + (``cert, ``privkey``, ``chain``, and ``fullchain``) exist + :rtype: int + + """ + # TODO: this can raise CertStorageError if there is no version overlap + # (it should probably return None instead) + # TODO: this can raise a spurious AttributeError if the current + # link for any kind is missing (it should probably return None) + versions = [self.available_versions(x) for x in ALL_FOUR] + return max(n for n in versions[0] if all(n in v for v in versions[1:])) + + def next_free_version(self) -> int: + """Smallest version newer than all full or partial versions? + + :returns: the smallest version number that is larger than any + version of any item currently stored in this lineage + :rtype: int + + """ + # TODO: consider locking/mutual exclusion between updating processes + # This isn't self.latest_common_version() + 1 because we don't want + # collide with a version that might exist for one file type but not + # for the others. + return max(self.newest_available_version(x) for x in ALL_FOUR) + 1 + + def ensure_deployed(self) -> bool: + """Make sure we've deployed the latest version. + + :returns: False if a change was needed, True otherwise + :rtype: bool + + May need to recover from rare interrupted / crashed states.""" + + if self.has_pending_deployment(): + logger.warning("Found a new certificate /archive/ that was not " + "linked to in /live/; fixing...") + self.update_all_links_to(self.latest_common_version()) + return False + return True + + def has_pending_deployment(self) -> bool: + """Is there a later version of all of the managed items? + + :returns: ``True`` if there is a complete version of this + lineage with a larger version number than the current + version, and ``False`` otherwise + :rtype: bool + + """ + all_versions: list[int] = [] + for item in ALL_FOUR: + version = self.current_version(item) + if version is None: + raise errors.Error(f"{item} is required but missing for this certificate.") + all_versions.append(version) + # TODO: consider whether to assume consistency or treat + # inconsistent/consistent versions differently + smallest_current = min(all_versions) + return smallest_current < self.latest_common_version() + + def _update_link_to(self, kind: str, version: int) -> None: + """Make the specified item point at the specified version. + + (Note that this method doesn't verify that the specified version + exists.) + + :param str kind: the lineage member item ("cert", "privkey", + "chain", or "fullchain") + :param int version: the desired version + + """ + if kind not in ALL_FOUR: + raise errors.CertStorageError("unknown kind of item") + link = getattr(self, kind) + filename = "{0}{1}.pem".format(kind, version) + # Relative rather than absolute target directory + target_directory = os.path.dirname(filesystem.readlink(link)) + # TODO: it could be safer to make the link first under a temporary + # filename, then unlink the old link, then rename the new link + # to the old link; this ensures that this process is able to + # create symlinks. + # TODO: we might also want to check consistency of related links + # for the other corresponding items + os.unlink(link) + os.symlink(os.path.join(target_directory, filename), link) + + def update_all_links_to(self, version: int) -> None: + """Change all member objects to point to the specified version. + + :param int version: the desired version + + """ + with error_handler.ErrorHandler(self._fix_symlinks): + previous_links = self._previous_symlinks() + for kind, link in previous_links: + target = self.current_target(kind) + if not target: + raise errors.Error(f"Target {kind} does not exist!") + os.symlink(target, link) + + for kind in ALL_FOUR: + self._update_link_to(kind, version) + + for _, link in previous_links: + os.unlink(link) + + def names(self) -> list[str]: + """Return the DNS names and IP addresses from this certificate as strings. + + :returns: the subject names + :rtype: `list` of `str` + :raises .CertStorageError: if could not find cert file. + """ + return list(map(str, self.sans())) + + def sans(self) -> list[san.SAN]: + """Return the DNS names and IP addresses from this certificate as SAN objects. + + :returns: the subject names + :rtype: `list` of `san.SAN` + :raises .CertStorageError: if could not find cert file. + + """ + target = self.current_target("cert") + if target is None: + raise errors.CertStorageError("could not find the certificate file") + with open(target, "rb") as f: + cert_bytes = f.read() + x509_cert = x509.load_pem_x509_certificate(cert_bytes) + dns_names, ip_addrs = san.from_x509(x509_cert.subject, x509_cert.extensions) + return cast(list[san.SAN], dns_names + ip_addrs) + + def ocsp_revoked(self, version: int) -> bool: + """Is the specified cert version revoked according to OCSP? + + Also returns True if the cert version is declared as revoked + according to OCSP. If OCSP status could not be determined, False + is returned. + + :param int version: the desired version number + + :returns: True if the certificate is revoked, otherwise, False + :rtype: bool + + """ + cert_path = self.version("cert", version) + chain_path = self.version("chain", version) + # While the RevocationChecker should return False if it failed to + # determine the OCSP status, let's ensure we don't crash Certbot by + # catching all exceptions here. + try: + return ocsp.RevocationChecker().ocsp_revoked_by_paths(cert_path, + chain_path) + except Exception as e: # pylint: disable=broad-except + logger.warning( + "An error occurred determining the OCSP status of %s.", + cert_path) + logger.debug(str(e)) + return False + + def autorenewal_is_enabled(self) -> bool: + """Is automatic renewal enabled for this cert? + + If autorenew is not specified, defaults to True. + + :returns: True if automatic renewal is enabled + :rtype: bool + + """ + return ("autorenew" not in self.configuration["renewalparams"] or + self.configuration["renewalparams"].as_bool("autorenew")) + + @classmethod + def new_lineage(cls, lineagename: str, cert: bytes, privkey: bytes, chain: bytes, + cli_config: configuration.NamespaceConfig) -> "RenewableCert": + """Create a new certificate lineage. + + Attempts to create a certificate lineage -- enrolled for + potential future renewal -- with the (suggested) lineage name + lineagename, and the associated cert, privkey, and chain (the + associated fullchain will be created automatically). Optional + configurator and renewalparams record the configuration that was + originally used to obtain this cert, so that it can be reused + later during automated renewal. + + Returns a new RenewableCert object referring to the created + lineage. (The actual lineage name, as well as all the relevant + file paths, will be available within this object.) + + :param str lineagename: the suggested name for this lineage + (normally the current cert's first subject DNS name) + :param str cert: the initial certificate version in PEM format + :param str privkey: the private key in PEM format + :param str chain: the certificate chain in PEM format + :param .NamespaceConfig cli_config: parsed command line + arguments + + :returns: the newly-created RenewalCert object + :rtype: :class:`storage.renewableCert` + + """ + + # Examine the configuration and find the new lineage's name + for i in (cli_config.renewal_configs_dir, cli_config.default_archive_dir, + cli_config.live_dir): + if not os.path.exists(i): + filesystem.makedirs(i, 0o700) + logger.debug("Creating directory %s.", i) + config_file, config_filename = util.unique_lineage_name( + cli_config.renewal_configs_dir, lineagename) + base_readme_path = os.path.join(cli_config.live_dir, README) + if not os.path.exists(base_readme_path): + _write_live_readme_to(base_readme_path, is_base_dir=True) + + # Determine where on disk everything will go + # lineagename will now potentially be modified based on which + # renewal configuration file could actually be created + lineagename = lineagename_for_filename(config_filename) + archive = full_archive_path(None, cli_config, lineagename) + live_dir = _full_live_path(cli_config, lineagename) + if os.path.exists(archive) and (not os.path.isdir(archive) or os.listdir(archive)): + config_file.close() + raise errors.CertStorageError( + "archive directory exists for " + lineagename) + if os.path.exists(live_dir) and (not os.path.isdir(live_dir) or os.listdir(live_dir)): + config_file.close() + raise errors.CertStorageError( + "live directory exists for " + lineagename) + for i in (archive, live_dir): + if not os.path.exists(i): + filesystem.makedirs(i) + logger.debug("Creating directory %s.", i) + + # Put the data into the appropriate files on disk + target = {kind: os.path.join(live_dir, kind + ".pem") for kind in ALL_FOUR} + archive_target = {kind: os.path.join(archive, kind + "1.pem") for kind in ALL_FOUR} + for kind in ALL_FOUR: + os.symlink(_relpath_from_file(archive_target[kind], target[kind]), target[kind]) + with open(target["cert"], "wb") as f_b: + logger.debug("Writing certificate to %s.", target["cert"]) + f_b.write(cert) + with util.safe_open(archive_target["privkey"], "wb", chmod=BASE_PRIVKEY_MODE) as f_a: + logger.debug("Writing private key to %s.", target["privkey"]) + f_a.write(privkey) + # XXX: Let's make sure to get the file permissions right here + with open(target["chain"], "wb") as f_b: + logger.debug("Writing chain to %s.", target["chain"]) + f_b.write(chain) + with open(target["fullchain"], "wb") as f_b: + # assumes the cert includes ending newline character + logger.debug("Writing full chain to %s.", target["fullchain"]) + f_b.write(cert + chain) + + # Write a README file to the live directory + readme_path = os.path.join(live_dir, README) + _write_live_readme_to(readme_path) + + # Document what we've done in a new renewal config file + config_file.close() + + create_renewal_config_file(config_filename, archive, target, cli_config) + return cls(config_filename, cli_config) + + def _private_key(self) -> Union[RSAPrivateKey, EllipticCurvePrivateKey]: + with open(self.configuration["privkey"], "rb") as priv_key_file: + key = load_pem_private_key( + data=priv_key_file.read(), + password=None, + backend=default_backend() + ) + return cast(Union[RSAPrivateKey, EllipticCurvePrivateKey], key) + + @property + def private_key_type(self) -> str: + """ + :returns: The type of algorithm for the private, RSA or ECDSA + :rtype: str + """ + key = self._private_key() + if isinstance(key, RSAPrivateKey): + return "RSA" + return "ECDSA" + + @property + def rsa_key_size(self) -> Optional[int]: + """ + :returns: If the private key is an RSA key, its size. + :rtype: int + """ + key = self._private_key() + if isinstance(key, RSAPrivateKey): + return key.key_size + return None + + @property + def elliptic_curve(self) -> Optional[str]: + """ + :returns: If the private key is an elliptic key, the name of its curve. + :rtype: str + """ + key = self._private_key() + if isinstance(key, EllipticCurvePrivateKey): + return key.curve.name + return None + + def save_successor(self, prior_version: int, new_cert: bytes, new_privkey: bytes, + new_chain: bytes, cli_config: configuration.NamespaceConfig) -> int: + """Save new cert and chain as a successor of a prior version. + + Returns the new version number that was created. + + .. note:: this function does NOT update links to deploy this + version + + :param int prior_version: the old version to which this version + is regarded as a successor (used to choose a privkey, if the + key has not changed, but otherwise this information is not + permanently recorded anywhere) + :param bytes new_cert: the new certificate, in PEM format + :param bytes new_privkey: the new private key, in PEM format, + or ``None``, if the private key has not changed + :param bytes new_chain: the new chain, in PEM format + :param .NamespaceConfig cli_config: parsed command line + arguments + + :returns: the new version number that was created + :rtype: int + + """ + # XXX: assumes official archive location rather than examining links + # XXX: consider using os.open for availability of os.O_EXCL + # XXX: ensure file permissions are correct; also create directories + # if needed (ensuring their permissions are correct) + # Figure out what the new version is and hence where to save things + + self.cli_config = cli_config + target_version = self.next_free_version() + target = {kind: os.path.join(self.archive_dir, "{0}{1}.pem".format(kind, target_version)) + for kind in ALL_FOUR} + + old_privkey = os.path.join( + self.archive_dir, "privkey{0}.pem".format(prior_version)) + + # Distinguish the cases where the privkey has changed and where it + # has not changed (in the latter case, making an appropriate symlink + # to an earlier privkey version) + if new_privkey is None: + # The behavior below keeps the prior key by creating a new + # symlink to the old key or the target of the old key symlink. + if os.path.islink(old_privkey): + old_privkey = filesystem.readlink(old_privkey) + else: + old_privkey = f"privkey{prior_version}.pem" + logger.debug("Writing symlink to old private key, %s.", old_privkey) + os.symlink(old_privkey, target["privkey"]) + else: + with util.safe_open(target["privkey"], "wb", chmod=BASE_PRIVKEY_MODE) as f: + logger.debug("Writing new private key to %s.", target["privkey"]) + f.write(new_privkey) + # Preserve gid and (mode & MASK_FOR_PRIVATE_KEY_PERMISSIONS) + # from previous privkey in this lineage. + mode = filesystem.compute_private_key_mode(old_privkey, BASE_PRIVKEY_MODE) + filesystem.copy_ownership_and_apply_mode( + old_privkey, target["privkey"], mode, copy_user=False, copy_group=True) + + # Save everything else + with open(target["cert"], "wb") as f: + logger.debug("Writing certificate to %s.", target["cert"]) + f.write(new_cert) + with open(target["chain"], "wb") as f: + logger.debug("Writing chain to %s.", target["chain"]) + f.write(new_chain) + with open(target["fullchain"], "wb") as f: + logger.debug("Writing full chain to %s.", target["fullchain"]) + f.write(new_cert + new_chain) + + symlinks = {kind: self.configuration[kind] for kind in ALL_FOUR} + # Update renewal config file + self.configfile = update_configuration( + self.lineagename, self.archive_dir, symlinks, cli_config) + self.configuration = self.configfile + + return target_version + + def save_new_config_values(self, cli_config: configuration.NamespaceConfig) -> None: + """Save only the config information without writing the new cert. + + :param .NamespaceConfig cli_config: parsed command line + arguments + """ + self.cli_config = cli_config + symlinks = {kind: self.configuration[kind] for kind in ALL_FOUR} + # Update renewal config file + self.configfile = update_configuration( + self.lineagename, self.archive_dir, symlinks, cli_config) + self.configuration = self.configfile + + def truncate(self, num_prior_certs_to_keep: int = 5) -> None: + """Delete unused historical certificate, chain and key items from the lineage. + + A certificate version will be deleted if it is: + 1. not the current target, and + 2. not a previous version within num_prior_certs_to_keep. + + :param num_prior_certs_to_keep: How many prior certificate versions to keep. + + """ + # Do not want to delete the current or the previous num_prior_certs_to_keep certs + current_version = self.latest_common_version() + versions_to_delete = set(self.available_versions("cert")) + versions_to_delete -= set(range(current_version, + current_version - 1 - num_prior_certs_to_keep, -1)) + archive = self.archive_dir + + # Delete the remaining lineage items kinds for those certificate versions. + for ver in versions_to_delete: + logger.debug("Deleting %s/cert%d.pem and related items during clean up", + archive, ver) + for kind in ALL_FOUR: + item_path = os.path.join(archive, f"{kind}{ver}.pem") + try: + if os.path.exists(item_path): + os.unlink(item_path) + except OSError: + logger.debug("Failed to clean up %s", item_path, exc_info=True) diff --git a/certbot/src/certbot/_internal/tests/__init__.py b/certbot/src/certbot/_internal/tests/__init__.py new file mode 100644 index 00000000000..5b3aa04ab2b --- /dev/null +++ b/certbot/src/certbot/_internal/tests/__init__.py @@ -0,0 +1 @@ +"""certbot tests""" diff --git a/certbot/src/certbot/_internal/tests/account_test.py b/certbot/src/certbot/_internal/tests/account_test.py new file mode 100644 index 00000000000..f856779bb55 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/account_test.py @@ -0,0 +1,336 @@ +"""Tests for certbot._internal.account.""" +import datetime +import sys +import unittest +from unittest import mock + +import josepy as jose +import pytest + +from acme import messages +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import misc +from certbot.compat import os +import certbot.tests.util as test_util + +KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) + + +class AccountTest(unittest.TestCase): + """Tests for certbot._internal.account.Account.""" + + def setUp(self): + from certbot._internal.account import Account + self.regr = mock.MagicMock() + self.meta = Account.Meta( + creation_host="test.certbot.org", + creation_dt=datetime.datetime( + 2015, 7, 4, 14, 4, 10, tzinfo=datetime.timezone.utc)) + self.acc = Account(self.regr, KEY, self.meta) + self.regr.__repr__ = mock.MagicMock(return_value="i_am_a_regr") + + with mock.patch("certbot._internal.account.socket") as mock_socket: + mock_socket.gethostname.return_value = "test.certbot.org" + with mock.patch("certbot._internal.account.datetime") as mock_dt: + mock_dt.datetime.now.return_value = self.meta.creation_dt + self.acc_no_meta = Account(self.regr, KEY) + + def test_init(self): + assert self.regr == self.acc.regr + assert KEY == self.acc.key + assert self.meta == self.acc_no_meta.meta + + def test_id(self): + assert self.acc.id == "7adac10320f585ddf118429c0c4af2cd" + + def test_slug(self): + assert self.acc.slug == "test.certbot.org@2015-07-04T14:04:10Z (7ada)" + + def test_repr(self): + assert repr(self.acc).startswith( + " 3 + + assert self.mock_auth.cleanup.call_count == 1 + # Test if list first element is http-01, use typ because it is an achall + assert self.mock_auth.cleanup.call_args[0][0][0].typ == "http-01" + + assert len(authzr) == 1 + + def test_name1_http_01_1_acme_2(self): + self._test_name1_http_01_1_common() + + def test_name1_http_01_1_dns_1_acme_2(self): + self.mock_net.poll.side_effect = _gen_mock_on_poll() + self.mock_auth.get_chall_pref.return_value.append(challenges.DNS01) + + authzr = gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES) + mock_order = mock.MagicMock(authorizations=[authzr]) + authzr = self.handler.handle_authorizations(mock_order, self.mock_config) + + assert self.mock_net.answer_challenge.call_count == 1 + + assert self.mock_net.poll.call_count == 1 + + assert self.mock_auth.cleanup.call_count == 1 + cleaned_up_achalls = self.mock_auth.cleanup.call_args[0][0] + assert len(cleaned_up_achalls) == 1 + assert cleaned_up_achalls[0].typ == "http-01" + + # Length of authorizations list + assert len(authzr) == 1 + + def test_name3_http_01_3_common_acme_2(self): + authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES), + gen_dom_authzr(domain="1", challs=acme_util.CHALLENGES), + gen_dom_authzr(domain="2", challs=acme_util.CHALLENGES)] + mock_order = mock.MagicMock(authorizations=authzrs) + + self.mock_net.poll.side_effect = _gen_mock_on_poll() + authzr = self.handler.handle_authorizations(mock_order, self.mock_config) + + assert self.mock_net.answer_challenge.call_count == 3 + + # Check poll call + assert self.mock_net.poll.call_count == 3 + + assert self.mock_auth.cleanup.call_count == 1 + + assert len(authzr) == 3 + + def test_debug_challenges(self): + config = mock.Mock(debug_challenges=True, verbose_count=0) + authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)] + mock_order = mock.MagicMock(authorizations=authzrs) + + account_key_thumbprint = b"foobarbaz" + self.mock_account.key.thumbprint.return_value = account_key_thumbprint + + self.mock_net.poll.side_effect = _gen_mock_on_poll() + + self.handler.handle_authorizations(mock_order, config) + + assert self.mock_net.answer_challenge.call_count == 1 + assert self.mock_display.notification.call_count == 1 + assert 'Pass "-v" for more info' in \ + self.mock_display.notification.call_args[0][0] + assert f"http://{authzrs[0].body.identifier.value}/.well-known/acme-challenge/" + \ + b64encode(authzrs[0].body.challenges[0].chall.token).decode() not in \ + self.mock_display.notification.call_args[0][0] + assert b64encode(account_key_thumbprint).decode() not in \ + self.mock_display.notification.call_args[0][0] + + def test_debug_challenges_verbose(self): + config = mock.Mock(debug_challenges=True, verbose_count=1) + authzrs = [gen_dom_authzr(domain="0", challs=[acme_util.HTTP01]), + gen_dom_authzr(domain="1", challs=[acme_util.DNS01])] + mock_order = mock.MagicMock(authorizations=authzrs) + + account_key_thumbprint = b"foobarbaz" + self.mock_account.key.thumbprint.return_value = account_key_thumbprint + + self.mock_net.poll.side_effect = _gen_mock_on_poll() + + self.mock_auth.get_chall_pref.return_value = [challenges.HTTP01, + challenges.DNS01] + + self.handler.handle_authorizations(mock_order, config) + + assert self.mock_net.answer_challenge.call_count == 2 + assert self.mock_display.notification.call_count == 1 + assert 'Pass "-v" for more info' not in \ + self.mock_display.notification.call_args[0][0] + assert f"http://{authzrs[0].body.identifier.value}/.well-known/acme-challenge/" + \ + b64encode(authzrs[0].body.challenges[0].chall.token).decode() in \ + self.mock_display.notification.call_args[0][0] + assert b64encode(account_key_thumbprint).decode() in \ + self.mock_display.notification.call_args[0][0] + assert f"_acme-challenge.{authzrs[1].body.identifier.value}" in \ + self.mock_display.notification.call_args[0][0] + assert authzrs[1].body.challenges[0].validation(self.mock_account.key) in \ + self.mock_display.notification.call_args[0][0] + + def test_perform_failure(self): + authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)] + mock_order = mock.MagicMock(authorizations=authzrs) + + self.mock_auth.perform.side_effect = errors.AuthorizationError + + with pytest.raises(errors.AuthorizationError): + self.handler.handle_authorizations(mock_order, self.mock_config) + + def test_max_retries_exceeded(self): + authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)] + mock_order = mock.MagicMock(authorizations=authzrs) + + # We will return STATUS_PENDING twice before returning STATUS_VALID. + self.mock_net.poll.side_effect = _gen_mock_on_poll(retry=2) + + with pytest.raises(errors.AuthorizationError, + match='All authorizations were not finalized by the CA.'): + # We retry only once, so retries will be exhausted before STATUS_VALID is returned. + self.handler.handle_authorizations(mock_order, self.mock_config, False, 1) + + @mock.patch('certbot._internal.auth_handler.time.sleep') + def test_deadline_exceeded(self, mock_sleep): + authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)] + mock_order = mock.MagicMock(authorizations=authzrs) + + orig_now = datetime.datetime.now + state = {'time_slept': 0} + + def mock_sleep_effect(secs): + state['time_slept'] += secs + mock_sleep.side_effect = mock_sleep_effect + + def mock_now_effect(): + return orig_now() + datetime.timedelta(seconds=state["time_slept"]) + + # We will return STATUS_PENDING and ask Certbot to sleep for 20 minutes at a time. + interval = datetime.timedelta(minutes=20).seconds + self.mock_net.poll.side_effect = _gen_mock_on_poll(status=messages.STATUS_PENDING, + wait_value=interval) + + with pytest.raises(errors.AuthorizationError, + match='All authorizations were not finalized by the CA.'): + with mock.patch('certbot._internal.auth_handler.datetime.datetime') as mock_dt: + mock_dt.now.side_effect = mock_now_effect + # Polling will only proceed for 30 minutes at most, so the second 20 minute sleep + # should be truncated and the polling should be aborted. + self.handler.handle_authorizations(mock_order, self.mock_config, False) + + assert mock_sleep.call_count == 3 # 1s, 20m and 10m sleep + assert mock_sleep.call_args_list[0][0][0] == 1 + assert abs(mock_sleep.call_args_list[1][0][0] - (interval - 1)) <= 1 + assert abs(mock_sleep.call_args_list[2][0][0] - (interval/2 - 1)) <= 1 + + def test_no_domains(self): + mock_order = mock.MagicMock(authorizations=[]) + with pytest.raises(errors.AuthorizationError): + self.handler.handle_authorizations(mock_order, self.mock_config) + + def test_preferred_challenge_choice_common_acme_2(self): + authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)] + mock_order = mock.MagicMock(authorizations=authzrs) + + self.mock_auth.get_chall_pref.return_value.append(challenges.HTTP01) + + self.handler.pref_challs.extend((challenges.HTTP01.typ, + challenges.DNS01.typ,)) + + self.mock_net.poll.side_effect = _gen_mock_on_poll() + self.handler.handle_authorizations(mock_order, self.mock_config) + + assert self.mock_auth.cleanup.call_count == 1 + assert self.mock_auth.cleanup.call_args[0][0][0].typ == "http-01" + + def test_preferred_challenges_not_supported_acme_2(self): + authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)] + mock_order = mock.MagicMock(authorizations=authzrs) + self.handler.pref_challs.append(challenges.DNS01.typ) + with pytest.raises(errors.AuthorizationError): + self.handler.handle_authorizations(mock_order, self.mock_config) + + def test_dns_only_challenge_not_supported(self): + authzrs = [gen_dom_authzr(domain="0", challs=[acme_util.DNS01])] + mock_order = mock.MagicMock(authorizations=authzrs) + with pytest.raises(errors.AuthorizationError): + self.handler.handle_authorizations(mock_order, self.mock_config) + + def test_perform_error(self): + self.mock_auth.perform.side_effect = errors.AuthorizationError + + authzr = gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES) + mock_order = mock.MagicMock(authorizations=[authzr]) + with pytest.raises(errors.AuthorizationError): + self.handler.handle_authorizations(mock_order, self.mock_config) + + assert self.mock_auth.cleanup.call_count == 1 + assert self.mock_auth.cleanup.call_args[0][0][0].typ == "http-01" + + def test_answer_error(self): + self.mock_net.answer_challenge.side_effect = errors.AuthorizationError + + authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)] + mock_order = mock.MagicMock(authorizations=authzrs) + + with pytest.raises(errors.AuthorizationError): + self.handler.handle_authorizations(mock_order, self.mock_config) + assert self.mock_auth.cleanup.call_count == 1 + assert self.mock_auth.cleanup.call_args[0][0][0].typ == "http-01" + + def test_incomplete_authzr_error(self): + authzrs = [gen_dom_authzr(domain="0", challs=acme_util.CHALLENGES)] + mock_order = mock.MagicMock(authorizations=authzrs) + self.mock_net.poll.side_effect = _gen_mock_on_poll(status=messages.STATUS_INVALID) + + with test_util.patch_display_util(): + with pytest.raises(errors.AuthorizationError, match='Some challenges have failed.'): + self.handler.handle_authorizations(mock_order, self.mock_config, False) + assert self.mock_auth.cleanup.call_count == 1 + assert self.mock_auth.cleanup.call_args[0][0][0].typ == "http-01" + + def test_best_effort(self): + def _conditional_mock_on_poll(authzr): + """This mock will invalidate one authzr, and invalidate the other one""" + valid_mock = _gen_mock_on_poll(messages.STATUS_VALID) + invalid_mock = _gen_mock_on_poll(messages.STATUS_INVALID) + + if authzr.body.identifier.value == 'will-be-invalid': + return invalid_mock(authzr) + return valid_mock(authzr) + + # Two authzrs. Only one will be valid. + authzrs = [gen_dom_authzr(domain="will-be-valid", challs=acme_util.CHALLENGES), + gen_dom_authzr(domain="will-be-invalid", challs=acme_util.CHALLENGES)] + self.mock_net.poll.side_effect = _conditional_mock_on_poll + + mock_order = mock.MagicMock(authorizations=authzrs) + + with mock.patch('certbot._internal.auth_handler.AuthHandler._report_failed_authzrs') \ + as mock_report: + valid_authzr = self.handler.handle_authorizations(mock_order, self.mock_config, True) + + # Because best_effort=True, we did not blow up. Instead ... + assert len(valid_authzr) == 1 # ... the valid authzr has been processed + assert mock_report.call_count == 1 # ... the invalid authzr has been reported + + self.mock_net.poll.side_effect = _gen_mock_on_poll(status=messages.STATUS_INVALID) + + with test_util.patch_display_util(): + with pytest.raises(errors.AuthorizationError, match='All challenges have failed.'): + # Despite best_effort=True, process will fail because no authzr is valid. + self.handler.handle_authorizations(mock_order, self.mock_config, True) + + def test_validated_challenge_not_rerun(self): + # With a pending challenge that is not supported by the plugin, we + # expect an exception to be raised. + authzr = acme_util.gen_authzr( + messages.STATUS_PENDING, "0", + [acme_util.DNS01], + [messages.STATUS_PENDING]) + mock_order = mock.MagicMock(authorizations=[authzr]) + with pytest.raises(errors.AuthorizationError): + self.handler.handle_authorizations(mock_order, self.mock_config) + + # With a validated challenge that is not supported by the plugin, we + # expect the challenge to not be solved again and + # handle_authorizations() to succeed. + authzr = acme_util.gen_authzr( + messages.STATUS_VALID, "0", + [acme_util.DNS01], + [messages.STATUS_VALID]) + mock_order = mock.MagicMock(authorizations=[authzr]) + self.handler.handle_authorizations(mock_order, self.mock_config) + + def test_valid_authzrs_deactivated(self): + """When we deactivate valid authzrs in an orderr, we expect them to become deactivated + and to receive a list of deactivated authzrs in return.""" + def _mock_deactivate(authzr): + if authzr.body.status == messages.STATUS_VALID: + if authzr.body.identifier.value == "is_valid_but_will_fail": + raise acme_errors.Error("Mock deactivation ACME error") + authzb = authzr.body.update(status=messages.STATUS_DEACTIVATED) + authzr = messages.AuthorizationResource(body=authzb) + else: # pragma: no cover + raise errors.Error("Can't deactivate non-valid authz") + return authzr + + to_deactivate = [("is_valid", messages.STATUS_VALID), + ("is_pending", messages.STATUS_PENDING), + ("is_valid_but_will_fail", messages.STATUS_VALID)] + + to_deactivate = [acme_util.gen_authzr(a[1], a[0], [acme_util.HTTP01], + [a[1]]) for a in to_deactivate] + orderr = mock.MagicMock(authorizations=to_deactivate) + + self.mock_net.deactivate_authorization.side_effect = _mock_deactivate + + authzrs, failed = self.handler.deactivate_valid_authorizations(orderr) + + assert self.mock_net.deactivate_authorization.call_count == 2 + assert len(authzrs) == 1 + assert len(failed) == 1 + assert authzrs[0].body.identifier.value == "is_valid" + assert authzrs[0].body.status == messages.STATUS_DEACTIVATED + assert failed[0].body.identifier.value == "is_valid_but_will_fail" + assert failed[0].body.status == messages.STATUS_VALID + + +def _gen_mock_on_poll(status=messages.STATUS_VALID, retry=0, wait_value=1): + state = {'count': retry} + + def _mock(authzr): + state['count'] = state['count'] - 1 + effective_status = status if state['count'] < 0 else messages.STATUS_PENDING + updated_azr = acme_util.gen_authzr( + effective_status, + authzr.body.identifier.value, + [challb.chall for challb in authzr.body.challenges], + [effective_status] * len(authzr.body.challenges)) + return updated_azr, mock.MagicMock(headers={'Retry-After': str(wait_value)}) + return _mock + + +class ChallbToAchallTest(unittest.TestCase): + """Tests for certbot._internal.auth_handler.challb_to_achall.""" + + def _call(self, challb): + from certbot._internal.auth_handler import challb_to_achall + ident = messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="domain") + return challb_to_achall(challb, "account_key", ident) + + def test_it(self): + assert self._call(acme_util.HTTP01_P) == \ + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.HTTP01_P, account_key="account_key", + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="domain")) + + +class GenChallengePathTest(unittest.TestCase): + """Tests for certbot._internal.auth_handler.gen_challenge_path. + + """ + def setUp(self): + logging.disable(logging.FATAL) + + def tearDown(self): + logging.disable(logging.NOTSET) + + @classmethod + def _call(cls, challbs, preferences): + from certbot._internal.auth_handler import gen_challenge_path + return gen_challenge_path(challbs, preferences) + + def test_common_case(self): + """Given DNS01 and HTTP01 with appropriate combos.""" + challbs = (acme_util.DNS01_P, acme_util.HTTP01_P) + prefs = [challenges.DNS01, challenges.HTTP01] + + assert self._call(challbs, prefs) == (0,) + assert self._call(challbs[::-1], prefs) == (1,) + + def test_not_supported(self): + challbs = (acme_util.DNS01_P,) + prefs = [challenges.HTTP01] + + # smart path fails because no challs in prefs satisfies combos + with pytest.raises(errors.AuthorizationError): + self._call(challbs, prefs) + + +class ReportFailedAuthzrsTest(unittest.TestCase): + """Tests for certbot._internal.auth_handler.AuthHandler._report_failed_authzrs.""" + # pylint: disable=protected-access + + + def setUp(self): + from certbot._internal.auth_handler import AuthHandler + + self.mock_auth = mock.MagicMock(spec=plugin_common.Plugin, name="buzz") + self.mock_auth.name = "buzz" + self.mock_auth.auth_hint.return_value = "the buzz hint" + self.handler = AuthHandler(self.mock_auth, mock.MagicMock(), mock.MagicMock(), []) + + kwargs = { + "chall": acme_util.HTTP01, + "uri": "uri", + "status": messages.STATUS_INVALID, + "error": messages.Error.with_code("tls", detail="detail"), + } + + # Prevent future regressions if the error type changes + assert kwargs["error"].description is not None + + http_01 = messages.ChallengeBody(**kwargs) + + kwargs["chall"] = acme_util.HTTP01 + http_01 = messages.ChallengeBody(**kwargs) + + self.authzr1 = mock.MagicMock() + self.authzr1.body.identifier.value = 'example.com' + self.authzr1.body.challenges = [http_01, http_01] + + kwargs["error"] = messages.Error.with_code("dnssec", detail="detail") + http_01_diff = messages.ChallengeBody(**kwargs) + + self.authzr2 = mock.MagicMock() + self.authzr2.body.identifier.value = 'foo.bar' + self.authzr2.body.challenges = [http_01_diff] + + @mock.patch('certbot._internal.auth_handler.display_util.notify') + def test_same_error_and_domain(self, mock_notify): + self.handler._report_failed_authzrs([self.authzr1]) + mock_notify.assert_called_with( + '\n' + 'Certbot failed to authenticate some domains (authenticator: buzz). ' + 'The Certificate Authority reported these problems:\n' + ' Identifier: example.com\n' + ' Type: tls\n' + ' Detail: detail\n' + '\n' + ' Identifier: example.com\n' + ' Type: tls\n' + ' Detail: detail\n' + '\nHint: the buzz hint\n' + ) + + @mock.patch('certbot._internal.auth_handler.display_util.notify') + def test_different_errors_and_domains(self, mock_notify): + self.mock_auth.name = "quux" + self.mock_auth.auth_hint.return_value = "quuuuuux" + self.handler._report_failed_authzrs([self.authzr1, self.authzr2]) + mock_notify.assert_called_with( + '\n' + 'Certbot failed to authenticate some domains (authenticator: quux). ' + 'The Certificate Authority reported these problems:\n' + ' Identifier: foo.bar\n' + ' Type: dnssec\n' + ' Detail: detail\n' + '\n' + ' Identifier: example.com\n' + ' Type: tls\n' + ' Detail: detail\n' + '\n' + ' Identifier: example.com\n' + ' Type: tls\n' + ' Detail: detail\n' + '\nHint: quuuuuux\n' + ) + + @mock.patch('certbot._internal.auth_handler.display_util.notify') + def test_non_subclassed_authenticator(self, mock_notify): + """If authenticator not derived from common.Plugin, we shouldn't call .auth_hint""" + from certbot._internal.auth_handler import AuthHandler + + self.mock_auth = mock.MagicMock(name="quuz") + self.mock_auth.name = "quuz" + self.mock_auth.auth_hint.side_effect = Exception + self.handler = AuthHandler(self.mock_auth, mock.MagicMock(), mock.MagicMock(), []) + self.handler._report_failed_authzrs([self.authzr1]) + assert mock_notify.call_count == 1 + + +def gen_auth_resp(chall_list): + """Generate a dummy authorization response.""" + return ["%s%s" % (chall.__class__.__name__, chall.identifier.value) + for chall in chall_list] + + +def gen_dom_authzr(domain, challs): + """Generates new authzr for domains.""" + return acme_util.gen_authzr( + messages.STATUS_PENDING, domain, challs, + [messages.STATUS_PENDING] * len(challs)) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/cert_manager_test.py b/certbot/src/certbot/_internal/tests/cert_manager_test.py new file mode 100644 index 00000000000..7e14eb85887 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/cert_manager_test.py @@ -0,0 +1,586 @@ + +"""Tests for certbot._internal.cert_manager.""" +# pylint: disable=protected-access +import re +import shutil +import sys +import tempfile +import unittest +from unittest import mock + +import configobj +import pytest + +from certbot import configuration +from certbot import errors +from certbot._internal import san +from certbot._internal.storage import ALL_FOUR +from certbot._internal.tests import storage_test +from certbot.compat import filesystem +from certbot.compat import os +from certbot.display import util as display_util +from certbot.tests import util as test_util + + +class BaseCertManagerTest(test_util.ConfigTestCase): + """Base class for setting up Cert Manager tests. + """ + def setUp(self): + super().setUp() + + self.config.quiet = False + filesystem.makedirs(self.config.renewal_configs_dir) + + self.domains = { + "example.org": None, + "other.com": os.path.join(self.config.config_dir, "specialarchive") + } + self.config_files = {domain: self._set_up_config(domain, self.domains[domain]) + for domain in self.domains} + + # We also create a file that isn't a renewal config in the same + # location to test that logic that reads in all-and-only renewal + # configs will ignore it and NOT attempt to parse it. + with open(os.path.join(self.config.renewal_configs_dir, "IGNORE.THIS"), "w") as junk: + junk.write("This file should be ignored!") + + def _set_up_config(self, domain, custom_archive): + # TODO: maybe provide NamespaceConfig.make_dirs? + # TODO: main() should create those dirs, c.f. #902 + filesystem.makedirs(os.path.join(self.config.live_dir, domain)) + config_file = configobj.ConfigObj() + + if custom_archive is not None: + filesystem.makedirs(custom_archive) + config_file["archive_dir"] = custom_archive + else: + filesystem.makedirs(os.path.join(self.config.default_archive_dir, domain)) + + for kind in ALL_FOUR: + config_file[kind] = os.path.join(self.config.live_dir, domain, + kind + ".pem") + + config_file.filename = os.path.join(self.config.renewal_configs_dir, + domain + ".conf") + config_file.write() + return config_file + + +class DeleteTest(storage_test.BaseRenewableCertTest): + """Tests for certbot._internal.cert_manager.delete + """ + + def _call(self): + from certbot._internal import cert_manager + cert_manager.delete(self.config) + + @test_util.patch_display_util() + @mock.patch('certbot.display.util.notify') + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.storage.delete_files') + def test_delete_from_config_yes(self, mock_delete_files, mock_lineage_for_certname, + mock_notify, mock_util): + """Test delete""" + mock_lineage_for_certname.return_value = self.test_rc + mock_util().yesno.return_value = True + self.config.certname = "example.org" + self._call() + mock_delete_files.assert_called_once_with(self.config, "example.org") + mock_notify.assert_called_once_with( + "Deleted all files relating to certificate example.org." + ) + + @test_util.patch_display_util() + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.storage.delete_files') + def test_delete_from_config_no(self, mock_delete_files, mock_lineage_for_certname, + mock_util): + """Test delete""" + mock_lineage_for_certname.return_value = self.test_rc + mock_util().yesno.return_value = False + self.config.certname = "example.org" + self._call() + assert mock_delete_files.call_count == 0 + + @test_util.patch_display_util() + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.storage.delete_files') + def test_delete_interactive_single_yes(self, mock_delete_files, mock_lineage_for_certname, + mock_util): + """Test delete""" + mock_lineage_for_certname.return_value = self.test_rc + mock_util().checklist.return_value = (display_util.OK, ["example.org"]) + mock_util().yesno.return_value = True + self._call() + mock_delete_files.assert_called_once_with(self.config, "example.org") + + @test_util.patch_display_util() + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.storage.delete_files') + def test_delete_interactive_single_no(self, mock_delete_files, mock_lineage_for_certname, + mock_util): + """Test delete""" + mock_lineage_for_certname.return_value = self.test_rc + mock_util().checklist.return_value = (display_util.OK, ["example.org"]) + mock_util().yesno.return_value = False + self._call() + assert mock_delete_files.call_count == 0 + + @test_util.patch_display_util() + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.storage.delete_files') + def test_delete_interactive_multiple_yes(self, mock_delete_files, mock_lineage_for_certname, + mock_util): + """Test delete""" + mock_lineage_for_certname.return_value = self.test_rc + mock_util().checklist.return_value = (display_util.OK, ["example.org", "other.org"]) + mock_util().yesno.return_value = True + self._call() + mock_delete_files.assert_any_call(self.config, "example.org") + mock_delete_files.assert_any_call(self.config, "other.org") + assert mock_delete_files.call_count == 2 + + @test_util.patch_display_util() + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.storage.delete_files') + def test_delete_interactive_multiple_no(self, mock_delete_files, mock_lineage_for_certname, + mock_util): + """Test delete""" + mock_lineage_for_certname.return_value = self.test_rc + mock_util().checklist.return_value = (display_util.OK, ["example.org", "other.org"]) + mock_util().yesno.return_value = False + self._call() + assert mock_delete_files.call_count == 0 + + +class CertificatesTest(BaseCertManagerTest): + """Tests for certbot._internal.cert_manager.certificates + """ + def _certificates(self, *args, **kwargs): + from certbot._internal.cert_manager import certificates + return certificates(*args, **kwargs) + + @mock.patch('certbot._internal.cert_manager.logger') + @test_util.patch_display_util() + def test_certificates_parse_fail(self, mock_utility, mock_logger): + self._certificates(self.config) + assert mock_logger.warning.called #pylint: disable=no-member + assert mock_utility.called + + @mock.patch('certbot._internal.cert_manager.logger') + @test_util.patch_display_util() + def test_certificates_quiet(self, mock_utility, mock_logger): + self.config.quiet = True + self._certificates(self.config) + assert mock_utility.notification.called is False + assert mock_logger.warning.called #pylint: disable=no-member + + @mock.patch('certbot.crypto_util.verify_renewable_cert') + @mock.patch('certbot._internal.cert_manager.logger') + @test_util.patch_display_util() + @mock.patch("certbot._internal.storage.RenewableCert") + @mock.patch('certbot._internal.cert_manager._report_human_readable') + def test_certificates_parse_success(self, mock_report, mock_renewable_cert, + mock_utility, mock_logger, mock_verifier): + mock_verifier.return_value = None + mock_report.return_value = "" + self._certificates(self.config) + assert mock_logger.warning.called is False + assert mock_report.called + assert mock_utility.called + assert mock_renewable_cert.called + + @mock.patch('certbot._internal.cert_manager.logger') + @test_util.patch_display_util() + def test_certificates_no_files(self, mock_utility, mock_logger): + empty_tempdir = tempfile.mkdtemp() + empty_config = configuration.NamespaceConfig(mock.MagicMock( + config_dir=os.path.join(empty_tempdir, "config"), + work_dir=os.path.join(empty_tempdir, "work"), + logs_dir=os.path.join(empty_tempdir, "logs"), + quiet=False + )) + + filesystem.makedirs(empty_config.renewal_configs_dir) + self._certificates(empty_config) + assert mock_logger.warning.called is False + assert mock_utility.called + shutil.rmtree(empty_tempdir) + + @mock.patch('certbot.crypto_util.get_serial_from_cert') + @mock.patch('certbot._internal.cert_manager.ocsp.RevocationChecker.ocsp_revoked') + def test_report_human_readable(self, mock_revoked, mock_serial): + mock_revoked.return_value = None + mock_serial.return_value = 1234567890 + import datetime + + from certbot._internal import cert_manager + expiry = datetime.datetime.now(datetime.timezone.utc) + + cert = mock.MagicMock(lineagename="nameone") + cert.target_expiry = expiry + cert.sans.return_value = [san.DNSName("nameone"), san.DNSName("nametwo")] + cert.is_test_cert = False + parsed_certs = [cert] + + mock_config = mock.MagicMock(certname=None, lineagename=None) + mock_config.domains = [] + mock_config.ip_addresses = [] + # pylint: disable=protected-access + + # pylint: disable=protected-access + def get_report(): + return cert_manager._report_human_readable(mock_config, parsed_certs) + + out = get_report() + assert "INVALID: EXPIRED" in out + + cert.target_expiry += datetime.timedelta(hours=2) + # pylint: disable=protected-access + out = get_report() + assert ('1 hour' in out or '2 hour(s)' in out) is True + assert 'VALID' in out + assert 'INVALID' not in out + + cert.target_expiry += datetime.timedelta(days=1) + # pylint: disable=protected-access + out = get_report() + assert '1 day' in out + assert 'under' not in out + assert 'VALID' in out + assert 'INVALID' not in out + + cert.target_expiry += datetime.timedelta(days=2) + # pylint: disable=protected-access + out = get_report() + assert '3 days' in out + assert 'VALID' in out + assert 'INVALID' not in out + + cert.is_test_cert = True + mock_revoked.return_value = True + out = get_report() + assert 'INVALID: TEST_CERT, REVOKED' in out + + cert = mock.MagicMock(lineagename="indescribable") + cert.target_expiry = expiry + cert.sans.return_value = [san.DNSName("nameone"), san.DNSName("thrice.named")] + cert.is_test_cert = True + parsed_certs.append(cert) + + out = get_report() + assert len(re.findall("INVALID:", out)) == 2 + mock_config.domains = [san.DNSName("thrice.named")] + out = get_report() + assert len(re.findall("INVALID:", out)) == 1 + mock_config.domains = [san.DNSName("nameone")] + out = get_report() + assert len(re.findall("INVALID:", out)) == 2 + mock_config.certname = "indescribable" + out = get_report() + assert len(re.findall("INVALID:", out)) == 1 + mock_config.certname = "horror" + out = get_report() + assert len(re.findall("INVALID:", out)) == 0 + + +class SearchLineagesTest(BaseCertManagerTest): + """Tests for certbot._internal.cert_manager._search_lineages.""" + + @mock.patch('certbot.util.make_or_verify_dir') + @mock.patch('certbot._internal.storage.renewal_conf_files') + @mock.patch('certbot._internal.storage.RenewableCert') + def test_cert_storage_error(self, mock_renewable_cert, mock_renewal_conf_files, + mock_make_or_verify_dir): + mock_renewal_conf_files.return_value = ["badfile"] + mock_renewable_cert.side_effect = errors.CertStorageError + from certbot._internal import cert_manager + + # pylint: disable=protected-access + assert cert_manager._search_lineages(self.config, lambda x: x, "check") == "check" + assert mock_make_or_verify_dir.called + + +class LineageForCertnameTest(BaseCertManagerTest): + """Tests for certbot._internal.cert_manager.lineage_for_certname""" + + @mock.patch('certbot.util.make_or_verify_dir') + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + @mock.patch('certbot._internal.storage.RenewableCert') + def test_found_match(self, mock_renewable_cert, mock_renewal_conf_file, + mock_make_or_verify_dir): + mock_renewal_conf_file.return_value = "somefile.conf" + mock_match = mock.Mock(lineagename="example.com") + mock_renewable_cert.return_value = mock_match + from certbot._internal import cert_manager + assert cert_manager.lineage_for_certname(self.config, "example.com") == mock_match + assert mock_make_or_verify_dir.called + + @mock.patch('certbot.util.make_or_verify_dir') + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + def test_no_match(self, mock_renewal_conf_file, mock_make_or_verify_dir): + mock_renewal_conf_file.return_value = "other.com.conf" + from certbot._internal import cert_manager + assert cert_manager.lineage_for_certname(self.config, "example.com") is None + assert mock_make_or_verify_dir.called + + @mock.patch('certbot.util.make_or_verify_dir') + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + def test_no_renewal_file(self, mock_renewal_conf_file, mock_make_or_verify_dir): + mock_renewal_conf_file.side_effect = errors.CertStorageError() + from certbot._internal import cert_manager + assert cert_manager.lineage_for_certname(self.config, "example.com") is None + assert mock_make_or_verify_dir.called + + +class DomainsForCertnameTest(BaseCertManagerTest): + """Tests for certbot._internal.cert_manager.sans_for_certname""" + + @mock.patch('certbot.util.make_or_verify_dir') + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + @mock.patch('certbot._internal.storage.RenewableCert') + def test_found_match(self, mock_renewable_cert, mock_renewal_conf_file, + mock_make_or_verify_dir): + mock_renewal_conf_file.return_value = "somefile.conf" + mock_match = mock.Mock(lineagename="example.com") + domains = [san.DNSName("example.com"), san.DNSName("example.org")] + mock_match.sans.return_value = domains + mock_renewable_cert.return_value = mock_match + from certbot._internal import cert_manager + assert cert_manager.sans_for_certname(self.config, "example.com") == \ + domains + assert mock_make_or_verify_dir.called + + @mock.patch('certbot.util.make_or_verify_dir') + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + def test_no_match(self, mock_renewal_conf_file, mock_make_or_verify_dir): + mock_renewal_conf_file.return_value = "somefile.conf" + from certbot._internal import cert_manager + assert cert_manager.sans_for_certname(self.config, "other.com") is None + assert mock_make_or_verify_dir.called + + +class DuplicativeCertsTest(storage_test.BaseRenewableCertTest): + """Test to avoid duplicate lineages.""" + + def setUp(self): + super().setUp() + self.config_file.write() + self._write_out_ex_kinds() + + @mock.patch('certbot.util.make_or_verify_dir') + def test_find_duplicative_names(self, unused_makedir): + from certbot._internal.cert_manager import find_duplicative_certs + test_cert = test_util.load_vector('cert-san_512.pem') + with open(self.test_rc.cert, 'wb') as f: + f.write(test_cert) + + # No overlap at all + result = find_duplicative_certs( + self.config, [san.DNSName('wow.net'), san.DNSName('hooray.org')]) + assert result == (None, None) + + # Totally identical + result = find_duplicative_certs( + self.config, [san.DNSName('example.com'), san.DNSName('www.example.com')]) + assert result[0].configfile.filename.endswith('example.org.conf') + assert result[1] is None + + # Superset + result = find_duplicative_certs( + self.config, [san.DNSName('example.com'), san.DNSName('www.example.com'), + san.DNSName('something.new')]) + assert result[0] is None + assert result[1].configfile.filename.endswith('example.org.conf') + + # Partial overlap doesn't count + result = find_duplicative_certs( + self.config, [san.DNSName('example.com'), san.DNSName('something.new')]) + assert result == (None, None) + + +class CertPathToLineageTest(storage_test.BaseRenewableCertTest): + """Tests for certbot._internal.cert_manager.cert_path_to_lineage""" + + def setUp(self): + super().setUp() + self.config_file.write() + self._write_out_ex_kinds() + self.fullchain = os.path.join(self.config.config_dir, 'live', 'example.org', + 'fullchain.pem') + self.config.cert_path = self.fullchain + + def _call(self, cli_config): + from certbot._internal.cert_manager import cert_path_to_lineage + return cert_path_to_lineage(cli_config) + + def _archive_files(self, cli_config, filetype): + from certbot._internal.cert_manager import _archive_files + return _archive_files(cli_config, filetype) + + def test_basic_match(self): + assert 'example.org' == self._call(self.config) + + def test_no_match_exists(self): + bad_test_config = self.config + bad_test_config.cert_path = os.path.join(self.config.config_dir, 'live', + 'SailorMoon', 'fullchain.pem') + with pytest.raises(errors.Error): + self._call(bad_test_config) + + @mock.patch('certbot._internal.cert_manager._acceptable_matches') + def test_options_fullchain(self, mock_acceptable_matches): + mock_acceptable_matches.return_value = [lambda x: x.fullchain_path] + self.config.fullchain_path = self.fullchain + assert 'example.org' == self._call(self.config) + + @mock.patch('certbot._internal.cert_manager._acceptable_matches') + def test_options_cert_path(self, mock_acceptable_matches): + mock_acceptable_matches.return_value = [lambda x: x.cert_path] + test_cert_path = os.path.join(self.config.config_dir, 'live', 'example.org', + 'cert.pem') + self.config.cert_path = test_cert_path + assert 'example.org' == self._call(self.config) + + @mock.patch('certbot._internal.cert_manager._acceptable_matches') + def test_options_archive_cert(self, mock_acceptable_matches): + # Also this and the next test check that the regex of _archive_files is working. + self.config.cert_path = os.path.join(self.config.config_dir, 'archive', 'example.org', + 'cert11.pem') + mock_acceptable_matches.return_value = [lambda x: self._archive_files(x, 'cert')] + assert 'example.org' == self._call(self.config) + + @mock.patch('certbot._internal.cert_manager._acceptable_matches') + def test_options_archive_fullchain(self, mock_acceptable_matches): + self.config.cert_path = os.path.join(self.config.config_dir, 'archive', + 'example.org', 'fullchain11.pem') + mock_acceptable_matches.return_value = [lambda x: + self._archive_files(x, 'fullchain')] + assert 'example.org' == self._call(self.config) + + def test_only_path(self): + self.config.cert_path = self.fullchain + assert 'example.org' == self._call(self.config) + + +class MatchAndCheckOverlaps(storage_test.BaseRenewableCertTest): + """Tests for certbot._internal.cert_manager.match_and_check_overlaps w/o overlapping + archive dirs.""" + # A test with real overlapping archive dirs can be found in tests/boulder_integration.sh + def setUp(self): + super().setUp() + self.config_file.write() + self._write_out_ex_kinds() + self.fullchain = os.path.join(self.config.config_dir, 'live', 'example.org', + 'fullchain.pem') + self.config.cert_path = self.fullchain + + def _call(self, cli_config, acceptable_matches, match_func, rv_func): + from certbot._internal.cert_manager import match_and_check_overlaps + return match_and_check_overlaps(cli_config, acceptable_matches, match_func, rv_func) + + def test_basic_match(self): + from certbot._internal.cert_manager import _acceptable_matches + assert ['example.org'] == self._call(self.config, _acceptable_matches(), + lambda x: self.config.cert_path, lambda x: x.lineagename) + + @mock.patch('certbot._internal.cert_manager._search_lineages') + def test_no_matches(self, mock_search_lineages): + mock_search_lineages.return_value = [] + with pytest.raises(errors.Error): + self._call(self.config, None, None, None) + + @mock.patch('certbot._internal.cert_manager._search_lineages') + def test_too_many_matches(self, mock_search_lineages): + mock_search_lineages.return_value = ['spider', 'dance'] + with pytest.raises(errors.OverlappingMatchFound): + self._call(self.config, None, None, None) + + +class GetCertnameTest(unittest.TestCase): + """Tests for certbot._internal.cert_manager.""" + + def setUp(self): + get_utility_patch = test_util.patch_display_util() + self.mock_get_utility = get_utility_patch.start() + self.addCleanup(get_utility_patch.stop) + self.config = mock.MagicMock() + self.config.certname = None + + @mock.patch('certbot._internal.storage.renewal_conf_files') + @mock.patch('certbot._internal.storage.lineagename_for_filename') + def test_get_certnames(self, mock_name, mock_files): + mock_files.return_value = ['example.com.conf'] + mock_name.return_value = 'example.com' + from certbot._internal import cert_manager + prompt = "Which certificate would you" + self.mock_get_utility().menu.return_value = (display_util.OK, 0) + assert cert_manager.get_certnames( + self.config, "verb", allow_multiple=False) == ['example.com'] + assert prompt in self.mock_get_utility().menu.call_args[0][0] + + @mock.patch('certbot._internal.storage.renewal_conf_files') + @mock.patch('certbot._internal.storage.lineagename_for_filename') + def test_get_certnames_custom_prompt(self, mock_name, mock_files): + mock_files.return_value = ['example.com.conf'] + mock_name.return_value = 'example.com' + from certbot._internal import cert_manager + prompt = "custom prompt" + self.mock_get_utility().menu.return_value = (display_util.OK, 0) + assert cert_manager.get_certnames( + self.config, "verb", allow_multiple=False, custom_prompt=prompt) == \ + ['example.com'] + assert self.mock_get_utility().menu.call_args[0][0] == \ + prompt + + @mock.patch('certbot._internal.storage.renewal_conf_files') + @mock.patch('certbot._internal.storage.lineagename_for_filename') + def test_get_certnames_user_abort(self, mock_name, mock_files): + mock_files.return_value = ['example.com.conf'] + mock_name.return_value = 'example.com' + from certbot._internal import cert_manager + self.mock_get_utility().menu.return_value = (display_util.CANCEL, 0) + with pytest.raises(errors.Error): + cert_manager.get_certnames(self.config, "erroring_anyway", allow_multiple=False) + + @mock.patch('certbot._internal.storage.renewal_conf_files') + @mock.patch('certbot._internal.storage.lineagename_for_filename') + def test_get_certnames_allow_multiple(self, mock_name, mock_files): + mock_files.return_value = ['example.com.conf'] + mock_name.return_value = 'example.com' + from certbot._internal import cert_manager + prompt = "Which certificate(s) would you" + self.mock_get_utility().checklist.return_value = (display_util.OK, + ['example.com']) + assert cert_manager.get_certnames( + self.config, "verb", allow_multiple=True) == ['example.com'] + assert prompt in self.mock_get_utility().checklist.call_args[0][0] + + @mock.patch('certbot._internal.storage.renewal_conf_files') + @mock.patch('certbot._internal.storage.lineagename_for_filename') + def test_get_certnames_allow_multiple_custom_prompt(self, mock_name, mock_files): + mock_files.return_value = ['example.com.conf'] + mock_name.return_value = 'example.com' + from certbot._internal import cert_manager + prompt = "custom prompt" + self.mock_get_utility().checklist.return_value = (display_util.OK, + ['example.com']) + assert cert_manager.get_certnames( + self.config, "verb", allow_multiple=True, custom_prompt=prompt) == \ + ['example.com'] + assert self.mock_get_utility().checklist.call_args[0][0] == \ + prompt + + @mock.patch('certbot._internal.storage.renewal_conf_files') + @mock.patch('certbot._internal.storage.lineagename_for_filename') + def test_get_certnames_allow_multiple_user_abort(self, mock_name, mock_files): + mock_files.return_value = ['example.com.conf'] + mock_name.return_value = 'example.com' + from certbot._internal import cert_manager + self.mock_get_utility().checklist.return_value = (display_util.CANCEL, []) + with pytest.raises(errors.Error): + cert_manager.get_certnames(self.config, "erroring_anyway", allow_multiple=True) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/cli_test.py b/certbot/src/certbot/_internal/tests/cli_test.py new file mode 100644 index 00000000000..83a4355506d --- /dev/null +++ b/certbot/src/certbot/_internal/tests/cli_test.py @@ -0,0 +1,611 @@ +"""Tests for certbot._internal.cli.""" +import argparse +import copy +import io +import sys +import tempfile +from typing import Any +import unittest +from unittest import mock + +import pytest + +from acme import challenges +from certbot import errors +from certbot.configuration import ArgumentSource, NamespaceConfig +from certbot._internal import cli +from certbot._internal import constants +from certbot._internal import san +from certbot._internal.cli.cli_utils import flag_default +from certbot._internal.plugins import disco +from certbot.compat import filesystem +from certbot.compat import os +import certbot.tests.util as test_util +from certbot.tests.util import TempDirTestCase + +PLUGINS = disco.PluginsRegistry.find_all() + + +class TestReadFile(TempDirTestCase): + """Test cli.read_file""" + def test_read_file(self): + curr_dir = os.getcwd() + try: + # On Windows current directory may be on a different drive than self.tempdir. + # However a relative path between two different drives is invalid. So we move to + # self.tempdir to ensure that we stay on the same drive. + os.chdir(self.tempdir) + # The read-only filesystem introduced with macOS Catalina can break + # code using relative paths below. See + # https://bugs.python.org/issue38295 for another example of this. + # Eliminating any possible symlinks in self.tempdir before passing + # it to os.path.relpath solves the problem. This is done by calling + # filesystem.realpath which removes any symlinks in the path on + # POSIX systems. + real_path = filesystem.realpath(os.path.join(self.tempdir, 'foo')) + relative_path = os.path.relpath(real_path) + with pytest.raises(argparse.ArgumentTypeError): + cli.read_file(relative_path) + + test_contents = b'bar\n' + with open(relative_path, 'wb') as f: + f.write(test_contents) + + path, contents = cli.read_file(relative_path) + assert path == os.path.abspath(path) + assert contents == test_contents + finally: + os.chdir(curr_dir) + + +class FlagDefaultTest(unittest.TestCase): + """Tests cli.flag_default""" + + def test_default_directories(self): + if os.name != 'nt': + assert cli.flag_default('config_dir') == '/etc/letsencrypt' + assert cli.flag_default('work_dir') == '/var/lib/letsencrypt' + assert cli.flag_default('logs_dir') == '/var/log/letsencrypt' + else: + assert cli.flag_default('config_dir') == 'C:\\Certbot' + assert cli.flag_default('work_dir') == 'C:\\Certbot\\lib' + assert cli.flag_default('logs_dir') == 'C:\\Certbot\\log' + + +def assert_set_by_user_with_value(namespace, attr: str, value: Any): + assert getattr(namespace, attr) == value + assert namespace.set_by_user(attr) + + +def assert_value_and_source(namespace, attr: str, value: Any, source: ArgumentSource): + assert getattr(namespace, attr) == value + assert namespace.argument_sources[attr] == source + + +class ParseTest(unittest.TestCase): + '''Test the cli args entrypoint''' + + @staticmethod + def _unmocked_parse(args: list[str]) -> NamespaceConfig: + """Get result of cli.prepare_and_parse_args.""" + return cli.prepare_and_parse_args(PLUGINS, args) + + @staticmethod + def parse(args: list[str]) -> NamespaceConfig: + """Mocks certbot._internal.display.obj.get_display and calls _unmocked_parse.""" + with test_util.patch_display_util(): + return ParseTest._unmocked_parse(args) + + def _help_output(self, args: list[str]): + "Run a command, and return the output string for scrutiny" + + output = io.StringIO() + + def write_msg(message, *args, **kwargs): # pylint: disable=missing-docstring,unused-argument + output.write(message) + + with mock.patch('certbot._internal.main.sys.stdout', new=output): + with test_util.patch_display_util() as mock_get_utility: + mock_get_utility().notification.side_effect = write_msg + with mock.patch('certbot._internal.main.sys.stderr'): + with pytest.raises(SystemExit): + self._unmocked_parse(args) + + return output.getvalue() + + @mock.patch("certbot._internal.cli.helpful.flag_default") + def test_cli_ini_domains(self, mock_flag_default): + with tempfile.NamedTemporaryFile() as tmp_config: + tmp_config.close() # close now because of compatibility issues on Windows + # use a shim to get ConfigArgParse to pick up tmp_config + def shim(v): + return (copy.deepcopy(constants.CLI_DEFAULTS[v]) + if v != "config_files" + else [tmp_config.name]) + mock_flag_default.side_effect = shim + + namespace = self.parse(["certonly"]) + assert_value_and_source(namespace, 'domains', [], ArgumentSource.DEFAULT) + with open(tmp_config.name, 'w') as file_h: + file_h.write("domains = example.com") + namespace = self.parse(["certonly"]) + assert_value_and_source(namespace, 'domains', [san.DNSName("example.com")], ArgumentSource.CONFIG_FILE) + namespace = self.parse(["renew"]) + assert_value_and_source(namespace, 'domains', [], ArgumentSource.RUNTIME) + + def test_no_args(self): + namespace = self.parse([]) + for d in ('config_dir', 'logs_dir', 'work_dir'): + assert getattr(namespace, d) == cli.flag_default(d) + assert not namespace.set_by_user(d) + + def test_install_abspath(self): + cert = 'cert' + key = 'key' + chain = 'chain' + fullchain = 'fullchain' + + with mock.patch('certbot._internal.main.install'): + namespace = self.parse(['install', '--cert-path', cert, + '--key-path', 'key', '--chain-path', + 'chain', '--fullchain-path', 'fullchain']) + + assert namespace.cert_path == os.path.abspath(cert) + assert namespace.key_path == os.path.abspath(key) + assert namespace.chain_path == os.path.abspath(chain) + assert namespace.fullchain_path == os.path.abspath(fullchain) + + def test_help(self): + self._help_output(['--help']) # assert SystemExit is raised here + out = self._help_output(['--help', 'all']) + assert "--configurator" in out + assert "how a certificate is deployed" in out + assert "--webroot-path" in out + assert "--text" not in out + assert "%s" not in out + assert "{0}" not in out + assert "--renew-hook" not in out + + out = self._help_output(['-h', 'nginx']) + if "nginx" in PLUGINS: + # may be false while building distributions without plugins + assert "--nginx-ctl" in out + assert "--webroot-path" not in out + assert "--checkpoints" not in out + + out = self._help_output(['-h']) + if "nginx" in PLUGINS: + assert "Use the Nginx plugin" in out + else: + assert "(the certbot nginx plugin is not" in out + + out = self._help_output(['--help', 'plugins']) + assert "--webroot-path" not in out + assert "--prepare" in out + assert '"plugins" subcommand' in out + + # test multiple topics + out = self._help_output(['-h', 'renew']) + assert "--keep" in out + out = self._help_output(['-h', 'automation']) + assert "--keep" in out + out = self._help_output(['-h', 'revoke']) + assert "--keep" not in out + + out = self._help_output(['--help', 'install']) + assert "--cert-path" in out + assert "--key-path" in out + + out = self._help_output(['--help', 'revoke']) + assert "--cert-path" in out + assert "--key-path" in out + assert "--reason" in out + assert "--delete-after-revoke" in out + assert "--no-delete-after-revoke" in out + + out = self._help_output(['-h', 'register']) + assert "--cert-path" not in out + assert "--key-path" not in out + + out = self._help_output(['-h']) + assert cli.SHORT_USAGE in out + assert cli.COMMAND_OVERVIEW[:100] in out + assert "%s" not in out + assert "{0}" not in out + + def test_help_no_dashes(self): + self._help_output(['help']) # assert SystemExit is raised here + + out = self._help_output(['help', 'all']) + assert "--configurator" in out + assert "how a certificate is deployed" in out + assert "--webroot-path" in out + assert "--text" not in out + assert "%s" not in out + assert "{0}" not in out + + out = self._help_output(['help', 'install']) + assert "--cert-path" in out + assert "--key-path" in out + + out = self._help_output(['help', 'revoke']) + assert "--cert-path" in out + assert "--key-path" in out + + def test_parse_domains(self): + short_args = ['-d', 'example.com'] + namespace = self.parse(short_args) + assert_set_by_user_with_value(namespace, 'domains', [san.DNSName('example.com')]) + + short_args = ['-d', 'trailing.period.com.'] + namespace = self.parse(short_args) + assert_set_by_user_with_value(namespace, 'domains', [san.DNSName('trailing.period.com')]) + + short_args = ['-d', 'example.com,another.net,third.org,example.com'] + namespace = self.parse(short_args) + assert_set_by_user_with_value(namespace, 'domains', + [san.DNSName('example.com'), san.DNSName('another.net'), san.DNSName('third.org')]) + + long_args = ['--domains', 'example.com'] + namespace = self.parse(long_args) + assert_set_by_user_with_value(namespace, 'domains', [san.DNSName('example.com')]) + + long_args = ['--domains', 'trailing.period.com.'] + namespace = self.parse(long_args) + assert_set_by_user_with_value(namespace, 'domains', [san.DNSName('trailing.period.com')]) + + long_args = ['--domains', 'example.com,another.net,example.com'] + namespace = self.parse(long_args) + assert_set_by_user_with_value(namespace, 'domains', [san.DNSName('example.com'), san.DNSName('another.net')]) + + def test_preferred_challenges(self): + short_args = ['--preferred-challenges', 'http, dns'] + namespace = self.parse(short_args) + + expected = [challenges.HTTP01.typ, challenges.DNS01.typ] + assert_set_by_user_with_value(namespace, 'pref_challs', expected) + + short_args = ['--preferred-challenges', 'jumping-over-the-moon'] + # argparse.ArgumentError makes argparse print more information + # to stderr and call sys.exit() + with mock.patch('sys.stderr'): + with pytest.raises(SystemExit): + self.parse(short_args) + + def test_server_flag(self): + namespace = self.parse('--server example.com'.split()) + assert_set_by_user_with_value(namespace, 'server', 'example.com') + + def test_must_staple_flag(self): + namespace = self.parse(['--must-staple']) + assert_set_by_user_with_value(namespace, 'must_staple', True) + assert_value_and_source(namespace, 'staple', True, ArgumentSource.RUNTIME) + + def test_must_staple_and_staple_ocsp_flags(self): + namespace = self.parse(['--must-staple', '--staple-ocsp']) + assert_set_by_user_with_value(namespace, 'must_staple', True) + assert_set_by_user_with_value(namespace, 'staple', True) + + def _check_server_conflict_message(self, parser_args, conflicting_args): + try: + self.parse(parser_args) + self.fail( # pragma: no cover + "The following flags didn't conflict with " + '--server: {0}'.format(', '.join(conflicting_args))) + except errors.Error as error: + assert '--server' in str(error) + for arg in conflicting_args: + assert arg in str(error) + + def test_staging_flag(self): + short_args = ['--staging'] + namespace = self.parse(short_args) + assert_set_by_user_with_value(namespace, 'staging', True) + assert_set_by_user_with_value(namespace, 'server', constants.STAGING_URI) + + short_args += '--server example.com'.split() + self._check_server_conflict_message(short_args, '--staging') + + def _assert_dry_run_flag_worked(self, namespace, existing_account): + assert_set_by_user_with_value(namespace, 'dry_run', True) + assert_value_and_source(namespace, 'break_my_certs', True, ArgumentSource.RUNTIME) + assert_value_and_source(namespace, 'staging', True, ArgumentSource.RUNTIME) + assert_value_and_source(namespace, 'server', constants.STAGING_URI, ArgumentSource.RUNTIME) + + if existing_account: + assert_value_and_source(namespace, 'tos', True, ArgumentSource.RUNTIME) + assert_value_and_source(namespace, 'register_unsafely_without_email', True, ArgumentSource.RUNTIME) + else: + assert_value_and_source(namespace, 'tos', False, ArgumentSource.DEFAULT) + assert_value_and_source(namespace, 'register_unsafely_without_email', False, ArgumentSource.DEFAULT) + + def test_dry_run_flag(self): + config_dir = tempfile.mkdtemp() + short_args = '--dry-run --config-dir {0}'.format(config_dir).split() + with pytest.raises(errors.Error): + self.parse(short_args) + + self._assert_dry_run_flag_worked( + self.parse(short_args + ['auth']), False) + self._assert_dry_run_flag_worked( + self.parse(short_args + ['certonly']), False) + self._assert_dry_run_flag_worked( + self.parse(short_args + ['renew']), False) + + account_dir = os.path.join(config_dir, constants.ACCOUNTS_DIR) + filesystem.mkdir(account_dir) + filesystem.mkdir(os.path.join(account_dir, 'fake_account_dir')) + + self._assert_dry_run_flag_worked(self.parse(short_args + ['auth']), True) + self._assert_dry_run_flag_worked(self.parse(short_args + ['renew']), True) + self._assert_dry_run_flag_worked(self.parse(short_args + ['certonly']), True) + + short_args += ['certonly'] + + # `--dry-run --server example.com` should emit example.com + config = self.parse(short_args + ['--server', 'example.com']) + assert_set_by_user_with_value(config, 'server', 'example.com') + + # `--dry-run --server STAGING_URI` should emit STAGING_URI + config = self.parse(short_args + ['--server', constants.STAGING_URI]) + assert_set_by_user_with_value(config, 'server', constants.STAGING_URI) + + # `--dry-run --server LIVE` should emit STAGING_URI + config = self.parse(short_args + ['--server', cli.flag_default("server")]) + assert_value_and_source(config, 'server', constants.STAGING_URI, ArgumentSource.RUNTIME) + + # `--dry-run --server example.com --staging` should emit an error + conflicts = ['--staging'] + self._check_server_conflict_message(short_args + ['--server', 'example.com', '--staging'], + conflicts) + + def test_user_set_rsa_key_size(self): + key_size_option = 'rsa_key_size' + key_size_value = cli.flag_default(key_size_option) + config = self.parse('--rsa-key-size {0}'.format(key_size_value).split()) + + assert config.set_by_user(key_size_option) + + config_dir_option = 'config_dir' + assert not config.set_by_user( + config_dir_option) + assert not config.set_by_user('authenticator') + + def test_user_set_installer_and_authenticator(self): + config = self.parse('--apache') + assert config.set_by_user('installer') + assert config.set_by_user('authenticator') + + config = self.parse('--installer webroot') + assert config.set_by_user('installer') + assert not config.set_by_user('authenticator') + + def test_user_set_ecdsa_key_option(self): + elliptic_curve_option = 'elliptic_curve' + elliptic_curve_option_value = cli.flag_default(elliptic_curve_option) + config = self.parse('--elliptic-curve {0}'.format(elliptic_curve_option_value).split()) + assert config.set_by_user(elliptic_curve_option) + + def test_user_set_invalid_key_type(self): + key_type_option = 'key_type' + key_type_value = cli.flag_default(key_type_option) + config = self.parse('--key-type {0}'.format(key_type_value).split()) + assert config.set_by_user(key_type_option) + + with pytest.raises(SystemExit): + self.parse("--key-type foo") + + @mock.patch('certbot._internal.hooks.validate_hooks') + def test_user_set_deploy_hook(self, unused_mock_validate_hooks): + args = 'renew --deploy-hook foo'.split() + plugins = disco.PluginsRegistry.find_all() + config = cli.prepare_and_parse_args(plugins, args) + assert config.set_by_user('deploy_hook') + + @mock.patch('certbot._internal.plugins.webroot._validate_webroot') + def test_user_set_webroot_map(self, mock_validate_webroot): + args = 'renew -w /var/www/html -d example.com'.split() + mock_validate_webroot.return_value = '/var/www/html' + plugins = disco.PluginsRegistry.find_all() + config = cli.prepare_and_parse_args(plugins, args) + assert config.set_by_user('webroot_map') + + def test_encode_revocation_reason(self): + for reason, code in constants.REVOCATION_REASONS.items(): + namespace = self.parse(['--reason', reason]) + assert namespace.reason == code + for reason, code in constants.REVOCATION_REASONS.items(): + namespace = self.parse(['--reason', reason.upper()]) + assert namespace.reason == code + + def test_force_interactive(self): + with pytest.raises(errors.Error): + self.parse("renew --force-interactive".split()) + with pytest.raises(errors.Error): + self.parse("-n --force-interactive".split()) + + def test_deploy_hook_conflict(self): + namespace = self.parse(["--renew-hook", "foo", + "--deploy-hook", "bar", + "--disable-hook-validation"]) + assert_set_by_user_with_value(namespace, 'deploy_hook', "bar") + + def test_renew_hook_conflict(self): + namespace = self.parse(["--deploy-hook", "foo", + "--renew-hook", "bar", + "--disable-hook-validation"]) + assert_set_by_user_with_value(namespace, 'deploy_hook', "bar") + + def test_deploy_hook_matches_renew_hook(self): + value = "foo" + namespace = self.parse(["--renew-hook", value, + "--deploy-hook", value, + "--disable-hook-validation"]) + assert_set_by_user_with_value(namespace, 'deploy_hook', value) + + def test_renew_hook_sets_deploy_hook(self): + value = "foo" + namespace = self.parse( + ["--renew-hook", value, "--disable-hook-validation"]) + assert_set_by_user_with_value(namespace, 'deploy_hook', value) + + def test_renew_hook_matches_deploy_hook(self): + value = "foo" + namespace = self.parse(["--deploy-hook", value, + "--renew-hook", value, + "--disable-hook-validation"]) + assert_set_by_user_with_value(namespace, 'deploy_hook', value) + + def test_renew_hook_does_not_set_renew_hook(self): + value = "foo" + namespace = self.parse( + ["--renew-hook", value, "--disable-hook-validation"]) + assert not hasattr(namespace, "renew_hook") + assert_set_by_user_with_value(namespace, 'deploy_hook', value) + + def test_deploy_hook_does_not_set_renew_hook(self): + value = "foo" + namespace = self.parse( + ["--deploy-hook", value, "--disable-hook-validation"]) + assert not hasattr(namespace, "renew_hook") + assert_set_by_user_with_value(namespace, 'deploy_hook', value) + + def test_max_log_backups_error(self): + with mock.patch('certbot._internal.cli.sys.stderr'): + with pytest.raises(SystemExit): + self.parse("--max-log-backups foo".split()) + with pytest.raises(SystemExit): + self.parse("--max-log-backups -42".split()) + + def test_max_log_backups_success(self): + value = "42" + namespace = self.parse(["--max-log-backups", value]) + assert_set_by_user_with_value(namespace, 'max_log_backups', int(value)) + + def test_unchanging_defaults(self): + namespace = self.parse([]) + assert_value_and_source(namespace, 'domains', [], ArgumentSource.DEFAULT) + assert_value_and_source(namespace, 'pref_challs', [], ArgumentSource.DEFAULT) + + namespace.pref_challs = [challenges.HTTP01.typ] + namespace.domains = [san.DNSName('example.com')] + + namespace = self.parse([]) + assert_value_and_source(namespace, 'domains', [], ArgumentSource.DEFAULT) + assert_value_and_source(namespace, 'pref_challs', [], ArgumentSource.DEFAULT) + + def test_no_directory_hooks_set(self): + namespace = self.parse(["--no-directory-hooks"]) + assert_set_by_user_with_value(namespace, 'directory_hooks', False) + + def test_no_directory_hooks_unset(self): + namespace = self.parse([]) + assert_value_and_source(namespace, 'directory_hooks', True, ArgumentSource.DEFAULT) + + def test_delete_after_revoke(self): + namespace = self.parse(["--delete-after-revoke"]) + assert_set_by_user_with_value(namespace, 'delete_after_revoke', True) + + def test_delete_after_revoke_default(self): + namespace = self.parse([]) + assert namespace.delete_after_revoke is None + assert not namespace.set_by_user('delete_after_revoke') + + def test_no_delete_after_revoke(self): + namespace = self.parse(["--no-delete-after-revoke"]) + assert_set_by_user_with_value(namespace, 'delete_after_revoke', False) + + def test_allow_subset_with_wildcard(self): + with pytest.raises(errors.Error): + self.parse("--allow-subset-of-names -d *.example.org".split()) + + def test_route53_no_revert(self): + for help_flag in ['-h', '--help']: + for topic in ['all', 'plugins', 'dns-route53']: + assert 'certbot-route53:auth' not in self._help_output([help_flag, topic]) + + def test_parse_args_hosts_and_auto_hosts(self): + with pytest.raises(errors.Error): + self.parse(['--hsts', '--auto-hsts']) + + def test_parse_with_multiple_argument_sources(self): + DEFAULT_VALUE = flag_default('server') + CONFIG_FILE_VALUE = 'configfile.biz' + COMMAND_LINE_VALUE = 'commandline.edu' + + # check that the default is set + namespace = self.parse(['certonly']) + assert_value_and_source(namespace, 'server', DEFAULT_VALUE, ArgumentSource.DEFAULT) + + with tempfile.NamedTemporaryFile() as tmp_config: + tmp_config.close() # close now because of compatibility issues on Windows + with open(tmp_config.name, 'w') as file_h: + file_h.write(f'server = {CONFIG_FILE_VALUE}') + + # first, just provide a value from a config file + namespace = self.parse([ + 'certonly', + '-c', tmp_config.name, + ]) + assert_value_and_source(namespace, 'server', CONFIG_FILE_VALUE, ArgumentSource.CONFIG_FILE) + + # now provide config file + command line values + namespace = self.parse([ + 'certonly', + '-c', tmp_config.name, + '--server', COMMAND_LINE_VALUE, + ]) + assert_value_and_source(namespace, 'server', COMMAND_LINE_VALUE, ArgumentSource.COMMAND_LINE) + + def test_abbreviated_arguments(self): + # Argparse's "allow_abbrev" option (which is True by default) allows + # for unambiguous partial arguments (e.g. "--preferred-chal dns" will be + # interpreted the same as "--preferred-challenges dns") + namespace = self.parse('--preferred-chal dns --no-dir') + assert_set_by_user_with_value(namespace, 'pref_challs', ['dns-01']) + assert_set_by_user_with_value(namespace, 'directory_hooks', False) + + with tempfile.NamedTemporaryFile() as tmp_config: + tmp_config.close() # close now because of compatibility issues on Windows + with open(tmp_config.name, 'w') as file_h: + file_h.write('preferred-chal = dns') + + namespace = self.parse([ + 'certonly', + '--config', tmp_config.name, + ]) + assert_set_by_user_with_value(namespace, 'pref_challs', ['dns-01']) + + @mock.patch('certbot._internal.hooks.validate_hooks') + def test_argument_with_equals(self, unsused_mock_validate_hooks): + namespace = self.parse('-d=example.com') + assert_set_by_user_with_value(namespace, 'domains', [san.DNSName('example.com')]) + + # make sure it doesn't choke on equals signs being present in the argument value + plugins = disco.PluginsRegistry.find_all() + namespace = cli.prepare_and_parse_args(plugins, ['run', '--pre-hook="foo=bar"']) + assert_set_by_user_with_value(namespace, 'pre_hook', '"foo=bar"') + + def test_adjacent_short_args(self): + namespace = self.parse('-tv') + assert_set_by_user_with_value(namespace, 'text_mode', True) + assert_set_by_user_with_value(namespace, 'verbose_count', 1) + + namespace = self.parse('-tvvv') + assert_set_by_user_with_value(namespace, 'text_mode', True) + assert_set_by_user_with_value(namespace, 'verbose_count', 3) + + namespace = self.parse('-tvm foo@example.com') + assert_set_by_user_with_value(namespace, 'text_mode', True) + assert_set_by_user_with_value(namespace, 'verbose_count', 1) + assert_set_by_user_with_value(namespace, 'email', 'foo@example.com') + + def test_arg_with_contained_spaces(self): + # This can happen if a user specifies an arg like "-d foo.com" enclosed + # in double quotes, or as its own line in a docker-compose.yml file (as + # in #9811) + namespace = self.parse(['certonly', '-d foo.com']) + assert_set_by_user_with_value(namespace, 'domains', [san.DNSName('foo.com')]) + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/client_test.py b/certbot/src/certbot/_internal/tests/client_test.py new file mode 100644 index 00000000000..45877a3ffdb --- /dev/null +++ b/certbot/src/certbot/_internal/tests/client_test.py @@ -0,0 +1,1122 @@ +"""Tests for certbot._internal.client.""" +import contextlib +import datetime +import platform +import shutil +import sys +import tempfile +import unittest +from unittest import mock +from unittest.mock import MagicMock + +from josepy import interfaces +import pytest + +from certbot import errors +from certbot import util +from certbot._internal import account +from certbot._internal import constants +from certbot._internal import san +from certbot._internal.display import obj as display_obj +from certbot.compat import os +import certbot.tests.util as test_util + +KEY = test_util.load_vector("rsa512_key.pem") +CSR_SAN = test_util.load_vector("csr-san_512.pem") + +# pylint: disable=line-too-long + + +class DetermineUserAgentTest(test_util.ConfigTestCase): + """Tests for certbot._internal.client.determine_user_agent.""" + + def _call(self): + from certbot._internal.client import determine_user_agent + return determine_user_agent(self.config) + + @mock.patch.dict(os.environ, {"CERTBOT_DOCS": "1"}) + def test_docs_value(self): + self._test(expect_doc_values=True) + + @mock.patch.dict(os.environ, {}) + def test_real_values(self): + self._test(expect_doc_values=False) + + def _test(self, expect_doc_values): + ua = self._call() + + if expect_doc_values: + doc_value_check = self.assertIn + real_value_check = self.assertNotIn + else: + doc_value_check = self.assertNotIn + real_value_check = self.assertIn + + doc_value_check("OS_NAME OS_VERSION", ua) + doc_value_check("major.minor.patchlevel", ua) + real_value_check(util.get_os_info_ua(), ua) + real_value_check(platform.python_version(), ua) + + +class RegisterTest(test_util.ConfigTestCase): + """Tests for certbot._internal.client.register.""" + + def setUp(self): + super().setUp() + self.config.rsa_key_size = 1024 + self.config.register_unsafely_without_email = False + self.config.email = "alias@example.com" + self.account_storage = account.AccountMemoryStorage() + self.tos_cb = mock.MagicMock() + display_obj.set_display(MagicMock()) + + def _call(self): + from certbot._internal.client import register + return register(self.config, self.account_storage, self.tos_cb) + + @staticmethod + def _public_key_mock(): + m = mock.Mock(__class__=interfaces.JSONDeSerializable) + m.to_partial_json.return_value = '{"a": 1}' + return m + + @staticmethod + def _new_acct_dir_mock(): + return "/acme/new-account" + + @staticmethod + def _true_mock(): + return True + + @staticmethod + def _false_mock(): + return False + + @staticmethod + @contextlib.contextmanager + def _patched_acme_client(): + with mock.patch('certbot._internal.client.acme_client') as mock_acme_client: + yield mock_acme_client.ClientV2 + + def test_no_tos(self): + with self._patched_acme_client() as mock_client: + mock_client.new_account().terms_of_service = "http://tos" + mock_client().external_account_required.side_effect = self._false_mock + with mock.patch("certbot._internal.eff.prepare_subscription") as mock_prepare: + mock_client().new_account.side_effect = errors.Error + with pytest.raises(errors.Error): + self._call() + assert mock_prepare.called is False + + mock_client().new_account.side_effect = None + self._call() + assert mock_prepare.called is True + + @mock.patch('certbot._internal.eff.prepare_subscription') + def test_empty_meta(self, unused_mock_prepare): + # Test that we can handle an ACME server which does not implement the 'meta' + # directory object (for terms-of-service handling). + with self._patched_acme_client() as mock_client: + from acme.messages import Directory + mock_client().directory = Directory.from_json({}) + + mock_client().external_account_required.side_effect = self._false_mock + + self._call() + assert self.tos_cb.called is False + + @test_util.patch_display_util() + def test_it(self, unused_mock_get_utility): + with self._patched_acme_client() as mock_client: + mock_client().external_account_required.side_effect = self._false_mock + with mock.patch("certbot._internal.eff.handle_subscription"): + self._call() + assert self.tos_cb.called is True + + @mock.patch("certbot._internal.client.display_ops.get_email") + def test_email_retry(self, mock_get_email): + from acme import messages + self.config.noninteractive_mode = False + msg = "DNS problem: NXDOMAIN looking up MX for example.com" + mx_err = messages.Error.with_code('invalidContact', detail=msg) + with self._patched_acme_client() as mock_client: + mock_client().external_account_required.side_effect = self._false_mock + with mock.patch("certbot._internal.eff.prepare_subscription") as mock_prepare: + mock_client().new_account.side_effect = [mx_err, mock.MagicMock()] + self._call() + assert mock_get_email.call_count == 1 + assert mock_prepare.called is True + + def test_email_invalid_noninteractive(self): + from acme import messages + self.config.noninteractive_mode = True + msg = "DNS problem: NXDOMAIN looking up MX for example.com" + mx_err = messages.Error.with_code('invalidContact', detail=msg) + with self._patched_acme_client() as mock_client: + mock_client().external_account_required.side_effect = self._false_mock + with mock.patch("certbot._internal.eff.handle_subscription"): + mock_client().new_account.side_effect = [mx_err, mock.MagicMock()] + with pytest.raises(errors.Error): + self._call() + + def test_no_email_is_chill(self): + self.config.email = None + with self._patched_acme_client() as mock_client: + mock_client().external_account_required.side_effect = self._false_mock + self._call() + + @mock.patch("certbot._internal.client.logger") + def test_without_email(self, mock_logger): + with mock.patch("certbot._internal.eff.prepare_subscription") as mock_prepare: + with self._patched_acme_client() as mock_client: + mock_client().external_account_required.side_effect = self._false_mock + self.config.email = None + self.config.register_unsafely_without_email = True + self.config.dry_run = False + self._call() + assert mock_prepare.called is True + + @mock.patch("certbot._internal.client.display_ops.get_email") + def test_dry_run_no_staging_account(self, mock_get_email): + """Tests dry-run for no staging account, expect account created with no email""" + with self._patched_acme_client() as mock_client: + mock_client().external_account_required.side_effect = self._false_mock + with mock.patch("certbot._internal.eff.handle_subscription"): + self.config.dry_run = True + self._call() + # check Certbot did not ask the user to provide an email + assert mock_get_email.called is False + # check Certbot created an account with no email. Contact should return empty + assert not mock_client().new_account.call_args[0][0].contact + + @test_util.patch_display_util() + def test_with_eab_arguments(self, unused_mock_get_utility): + with self._patched_acme_client() as mock_client: + mock_client().client.directory.__getitem__ = mock.Mock( + side_effect=self._new_acct_dir_mock + ) + mock_client().external_account_required.side_effect = self._false_mock + with mock.patch("certbot._internal.eff.handle_subscription"): + target = "certbot._internal.client.messages.ExternalAccountBinding.from_data" + with mock.patch(target) as mock_eab_from_data: + self.config.eab_kid = "test-kid" + self.config.eab_hmac_key = "J2OAqW4MHXsrHVa_PVg0Y-L_R4SYw0_aL1le6mfblbE" + self._call() + + assert mock_eab_from_data.called is True + + @test_util.patch_display_util() + def test_without_eab_arguments(self, unused_mock_get_utility): + with self._patched_acme_client() as mock_client: + mock_client().external_account_required.side_effect = self._false_mock + with mock.patch("certbot._internal.eff.handle_subscription"): + target = "certbot._internal.client.messages.ExternalAccountBinding.from_data" + with mock.patch(target) as mock_eab_from_data: + self.config.eab_kid = None + self.config.eab_hmac_key = None + self._call() + + assert mock_eab_from_data.called is False + + def test_external_account_required_without_eab_arguments(self): + with self._patched_acme_client() as mock_client: + mock_client().client.net.key.public_key = mock.Mock(side_effect=self._public_key_mock) + mock_client().external_account_required.side_effect = self._true_mock + with mock.patch("certbot._internal.eff.handle_subscription"): + with mock.patch("certbot._internal.client.messages.ExternalAccountBinding.from_data"): + self.config.eab_kid = None + self.config.eab_hmac_key = None + + with pytest.raises(errors.Error): + self._call() + + def test_unsupported_error(self): + from acme import messages + msg = "Test" + mx_err = messages.Error.with_code("malformed", detail=msg, title="title") + with self._patched_acme_client() as mock_client: + mock_client().client.directory.__getitem__ = mock.Mock( + side_effect=self._new_acct_dir_mock + ) + mock_client().external_account_required.side_effect = self._false_mock + with mock.patch("certbot._internal.eff.handle_subscription") as mock_handle: + mock_client().new_account.side_effect = [mx_err, mock.MagicMock()] + with pytest.raises(messages.Error): + self._call() + assert mock_handle.called is False + + +class ClientTestCommon(test_util.ConfigTestCase): + """Common base class for certbot._internal.client.Client tests.""" + + def setUp(self): + super().setUp() + self.config.no_verify_ssl = False + self.config.allow_subset_of_names = False + + self.account = mock.MagicMock(**{"key.pem": KEY}) + + from certbot._internal.client import Client + with mock.patch("certbot._internal.client.acme_client") as acme: + self.acme_client = acme.ClientV2 + self.acme = self.acme_client.return_value = mock.MagicMock() + self.client_network = acme.ClientNetwork + self.client = Client( + config=self.config, account_=self.account, + auth=None, installer=None) + + +class ClientTest(ClientTestCommon): + """Tests for certbot._internal.client.Client.""" + + def setUp(self): + super().setUp() + + self.config.allow_subset_of_names = False + self.config.dry_run = False + self.config.strict_permissions = True + self.eg_domains = ["example.com", "www.example.com"] + self.eg_sans = list(map(san.DNSName, self.eg_domains)) + self.eg_order = mock.MagicMock( + authorizations=[None], + csr_pem=mock.sentinel.csr_pem) + + def test_init_acme_verify_ssl(self): + assert self.client_network.call_args[1]['verify_ssl'] is True + + def _mock_obtain_certificate(self): + self.client.auth_handler = mock.MagicMock() + self.client.auth_handler.handle_authorizations.return_value = [None] + self.client.auth_handler.deactivate_valid_authorizations.return_value = ([], []) + self.acme.finalize_order.return_value = self.eg_order + self.acme.new_order.return_value = self.eg_order + self.eg_order.update.return_value = self.eg_order + + def _check_obtain_certificate(self, auth_count=1): + if auth_count == 1: + self.client.auth_handler.handle_authorizations.assert_called_once_with( + self.eg_order, + self.config, + self.config.allow_subset_of_names) + else: + assert self.client.auth_handler.handle_authorizations.call_count == auth_count + + self.acme.finalize_order.assert_called_once_with( + self.eg_order, mock.ANY, + fetch_alternative_chains=self.config.preferred_chain is not None) + + @mock.patch("certbot._internal.client.crypto_util") + @mock.patch("certbot._internal.client.logger") + def test_obtain_certificate_from_csr(self, mock_logger, mock_crypto_util): + self._mock_obtain_certificate() + test_csr = util.CSR(form="pem", file=None, data=CSR_SAN) + auth_handler = self.client.auth_handler + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + orderr = self.acme.new_order(test_csr.data) + auth_handler.handle_authorizations(orderr, self.config, False) + assert (mock.sentinel.cert, mock.sentinel.chain) == \ + self.client.obtain_certificate_from_csr( + test_csr, + orderr=orderr) + mock_crypto_util.find_chain_with_issuer.assert_not_called() + # and that the cert was obtained correctly + self._check_obtain_certificate() + + # Test that --preferred-chain results in chain selection + self.config.preferred_chain = "some issuer" + assert (mock.sentinel.cert, mock.sentinel.chain) == \ + self.client.obtain_certificate_from_csr( + test_csr, + orderr=orderr) + mock_crypto_util.find_chain_with_issuer.assert_called_once_with( + [orderr.fullchain_pem] + orderr.alternative_fullchains_pem, + "some issuer", True) + self.config.preferred_chain = None + + # Test for default issuance_timeout + expected_deadline = \ + datetime.datetime.now() + datetime.timedelta( + seconds=constants.CLI_DEFAULTS["issuance_timeout"]) + self.client.obtain_certificate_from_csr(test_csr, orderr=orderr) + ((_, deadline), _) = self.client.acme.finalize_order.call_args + assert abs(expected_deadline - deadline) <= datetime.timedelta(seconds=1) + + # Test for specific issuance_timeout (300 seconds) + expected_deadline = \ + datetime.datetime.now() + datetime.timedelta(seconds=300) + self.config.issuance_timeout = 300 + self.client.obtain_certificate_from_csr(test_csr, orderr=orderr) + ((_, deadline), _) = self.client.acme.finalize_order.call_args + assert abs(expected_deadline - deadline) <= datetime.timedelta(seconds=1) + + # Test for orderr=None + assert (mock.sentinel.cert, mock.sentinel.chain) == \ + self.client.obtain_certificate_from_csr( + test_csr, + orderr=None) + auth_handler.handle_authorizations.assert_called_with(self.eg_order, self.config, False) + + # Test for no auth_handler + self.client.auth_handler = None + with pytest.raises(errors.Error): + self.client.obtain_certificate_from_csr(test_csr) + mock_logger.error.assert_called_once_with(mock.ANY) + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate(self, mock_crypto_util): + csr = util.CSR(form="pem", file=None, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = mock.sentinel.key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self._test_obtain_certificate_common(mock.sentinel.key, csr) + + mock_crypto_util.generate_key.assert_called_once_with( + key_size=self.config.rsa_key_size, + key_dir=None, + key_type=self.config.key_type, + elliptic_curve="secp256r1", + strict_permissions=True, + ) + mock_crypto_util.generate_csr.assert_called_once_with( + mock.sentinel.key, self.eg_domains, None, False, True, ipaddrs=[]) + mock_crypto_util.cert_and_chain_from_fullchain.assert_called_once_with( + self.eg_order.fullchain_pem) + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_no_profile_preference(self, mock_crypto_util): + csr = util.CSR(form="pem", file=None, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = mock.sentinel.key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self.client.config.required_profile = None + self.client.config.preferred_profile = None + + self._test_obtain_certificate_common(mock.sentinel.key, csr) + self.acme.new_order.assert_called_once_with(mock.ANY, profile=None) + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_required_profile(self, mock_crypto_util): + csr = util.CSR(form="pem", file=None, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = mock.sentinel.key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self.client.config.required_profile = "exampleProfile" + self.client.config.preferred_profile = None + + self._test_obtain_certificate_common(mock.sentinel.key, csr) + self.acme.new_order.assert_called_once_with(mock.ANY, profile="exampleProfile") + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_preferred_profile_exists(self, mock_crypto_util): + csr = util.CSR(form="pem", file=None, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = mock.sentinel.key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self.client.config.required_profile = None + self.client.config.preferred_profile = "exampleProfile" + + from acme.messages import Directory + self.acme.directory = Directory.from_json({ + 'meta': { + 'profiles': { + 'exampleProfile': 'here is some descriptive text, very informative', + } + } + }) + + self._test_obtain_certificate_common(mock.sentinel.key, csr) + self.acme.new_order.assert_called_once_with(mock.ANY, profile="exampleProfile") + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_preferred_profile_does_not_exist(self, mock_crypto_util): + csr = util.CSR(form="pem", file=None, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = mock.sentinel.key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self.client.config.required_profile = None + self.client.config.preferred_profile = "thisProfileDoesNotExist" + + from acme.messages import Directory + self.acme.directory = Directory.from_json({ + 'meta': { + 'profiles': { + 'example': 'profiles!', + } + } + }) + + self._test_obtain_certificate_common(mock.sentinel.key, csr) + self.acme.new_order.assert_called_once_with(mock.ANY, profile=None) + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_preferred_profile_no_profiles_exist(self, mock_crypto_util): + csr = util.CSR(form="pem", file=None, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = mock.sentinel.key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self.client.config.required_profile = None + self.client.config.preferred_profile = "thisProfileDoesNotExist" + + from acme.messages import Directory + self.acme.directory = Directory.from_json({}) + + self._test_obtain_certificate_common(mock.sentinel.key, csr) + self.acme.new_order.assert_called_once_with(mock.ANY, profile=None) + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_partial_success(self, mock_crypto_util): + csr = util.CSR(form="pem", file=mock.sentinel.csr_file, data=CSR_SAN) + key = util.CSR(form="pem", file=mock.sentinel.key_file, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + authzr = (self._authzr_from_sans([san.DNSName("example.com")])) + self.config.allow_subset_of_names = True + self._test_obtain_certificate_common(key, csr, authzr_ret=authzr, auth_count=2) + + assert mock_crypto_util.generate_key.call_count == 2 + assert mock_crypto_util.generate_csr.call_count == 2 + assert mock_crypto_util.cert_and_chain_from_fullchain.call_count == 1 + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_finalize_order_partial_success(self, mock_crypto_util): + from acme import messages + csr = util.CSR(form="pem", file=mock.sentinel.csr_file, data=CSR_SAN) + key = util.CSR(form="pem", file=mock.sentinel.key_file, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self._mock_obtain_certificate() + authzr = self._authzr_from_sans(self.eg_sans) + self.eg_order.authorizations = authzr + self.client.auth_handler.handle_authorizations.return_value = authzr + + identifier = messages.Identifier(typ=messages.IDENTIFIER_FQDN, value='example.com') + subproblem = messages.Error.with_code('caa', detail='bar', title='title', identifier=identifier) + error_with_subproblems = messages.Error.with_code('malformed', detail='foo', title='title', subproblems=[subproblem]) + self.client.acme.finalize_order.side_effect = [error_with_subproblems, mock.DEFAULT] + + self.config.allow_subset_of_names = True + + with test_util.patch_display_util(): + result = self.client.obtain_certificate(self.eg_sans) + + assert result == \ + (mock.sentinel.cert, mock.sentinel.chain, key, csr) + assert self.client.auth_handler.handle_authorizations.call_count == 2 + assert self.acme.finalize_order.call_count == 2 + + assert mock_crypto_util.generate_key.call_count == 2 + successful_domains = [d for d in self.eg_domains if d != 'example.com'] + mock_crypto_util.generate_csr.assert_has_calls([ + mock.call(key, self.eg_domains, None, self.config.must_staple, self.config.strict_permissions, ipaddrs=[]), + mock.call(key, successful_domains, None, self.config.must_staple, self.config.strict_permissions, ipaddrs=[])]) + assert mock_crypto_util.cert_and_chain_from_fullchain.call_count == 1 + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_finalize_order_no_retryable_domains(self, mock_crypto_util): + from acme import messages + csr = util.CSR(form="pem", file=mock.sentinel.csr_file, data=CSR_SAN) + key = util.CSR(form="pem", file=mock.sentinel.key_file, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self._mock_obtain_certificate() + authzr = self._authzr_from_sans(self.eg_sans) + self.eg_order.authorizations = authzr + self.client.auth_handler.handle_authorizations.return_value = authzr + + identifier1 = messages.Identifier(typ=messages.IDENTIFIER_FQDN, value='example.com') + identifier2 = messages.Identifier(typ=messages.IDENTIFIER_FQDN, value='www.example.com') + subproblem1 = messages.Error.with_code('caa', detail='bar', title='title', identifier=identifier1) + subproblem2 = messages.Error.with_code('caa', detail='bar', title='title', identifier=identifier2) + error_with_subproblems = messages.Error.with_code('malformed', detail='foo', title='title', subproblems=[subproblem1, subproblem2]) + self.client.acme.finalize_order.side_effect = error_with_subproblems + + self.config.allow_subset_of_names = True + + with pytest.raises(messages.Error): + self.client.obtain_certificate(self.eg_sans) + assert self.client.auth_handler.handle_authorizations.call_count == 1 + assert self.acme.finalize_order.call_count == 1 + assert mock_crypto_util.generate_key.call_count == 1 + assert mock_crypto_util.cert_and_chain_from_fullchain.call_count == 0 + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_finalize_order_rejected_identifier_no_subproblems(self, mock_crypto_util): + from acme import messages + csr = util.CSR(form="pem", file=mock.sentinel.csr_file, data=CSR_SAN) + key = util.CSR(form="pem", file=mock.sentinel.key_file, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self._mock_obtain_certificate() + authzr = self._authzr_from_sans(self.eg_sans) + self.eg_order.authorizations = authzr + self.client.auth_handler.handle_authorizations.return_value = authzr + + error = messages.Error.with_code('caa', detail='foo', title='title') + self.client.acme.finalize_order.side_effect = error + + self.config.allow_subset_of_names = True + + with pytest.raises(messages.Error): + self.client.obtain_certificate(self.eg_sans) + assert self.client.auth_handler.handle_authorizations.call_count == 1 + assert self.acme.finalize_order.call_count == 1 + assert mock_crypto_util.generate_key.call_count == 1 + assert mock_crypto_util.cert_and_chain_from_fullchain.call_count == 0 + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_get_order_partial_success(self, mock_crypto_util): + from acme import messages + csr = util.CSR(form="pem", file=mock.sentinel.csr_file, data=CSR_SAN) + key = util.CSR(form="pem", file=mock.sentinel.key_file, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self._mock_obtain_certificate() + authzr = self._authzr_from_sans(self.eg_sans) + self.eg_order.authorizations = authzr + self.client.auth_handler.handle_authorizations.return_value = authzr + + identifier = messages.Identifier(typ=messages.IDENTIFIER_FQDN, value='example.com') + subproblem = messages.Error.with_code('caa', detail='bar', title='title', identifier=identifier) + error_with_subproblems = messages.Error.with_code('malformed', detail='foo', title='title', subproblems=[subproblem]) + self.client.acme.new_order.side_effect = [error_with_subproblems, mock.DEFAULT] + + self.config.allow_subset_of_names = True + + with test_util.patch_display_util(): + result = self.client.obtain_certificate(self.eg_sans) + + assert result == \ + (mock.sentinel.cert, mock.sentinel.chain, key, csr) + assert self.client.auth_handler.handle_authorizations.call_count == 1 + assert self.acme.new_order.call_count == 2 + + successful_domains = [d for d in self.eg_domains if d != 'example.com'] + assert mock_crypto_util.generate_key.call_count == 2 + mock_crypto_util.generate_csr.assert_has_calls([ + mock.call(key, self.eg_domains, None, self.config.must_staple, self.config.strict_permissions, ipaddrs=[]), + mock.call(key, successful_domains, None, self.config.must_staple, self.config.strict_permissions, ipaddrs=[])]) + assert mock_crypto_util.cert_and_chain_from_fullchain.call_count == 1 + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_get_order_no_retryable_domains(self, mock_crypto_util): + from acme import messages + csr = util.CSR(form="pem", file=mock.sentinel.csr_file, data=CSR_SAN) + key = util.CSR(form="pem", file=mock.sentinel.key_file, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self._mock_obtain_certificate() + authzr = self._authzr_from_sans(self.eg_sans) + self.eg_order.authorizations = authzr + self.client.auth_handler.handle_authorizations.return_value = authzr + + identifier1 = messages.Identifier(typ=messages.IDENTIFIER_FQDN, value='example.com') + identifier2 = messages.Identifier(typ=messages.IDENTIFIER_FQDN, value='www.example.com') + subproblem1 = messages.Error.with_code('caa', detail='bar', title='title', identifier=identifier1) + subproblem2 = messages.Error.with_code('caa', detail='bar', title='title', identifier=identifier2) + error_with_subproblems = messages.Error.with_code('malformed', detail='foo', title='title', subproblems=[subproblem1, subproblem2]) + self.client.acme.new_order.side_effect = error_with_subproblems + + self.config.allow_subset_of_names = True + + with pytest.raises(messages.Error): + self.client.obtain_certificate(self.eg_sans) + assert self.client.auth_handler.handle_authorizations.call_count == 0 + assert self.acme.new_order.call_count == 1 + assert mock_crypto_util.generate_key.call_count == 1 + assert mock_crypto_util.cert_and_chain_from_fullchain.call_count == 0 + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_get_order_rejected_identifier_no_subproblems(self, mock_crypto_util): + from acme import messages + csr = util.CSR(form="pem", file=mock.sentinel.csr_file, data=CSR_SAN) + key = util.CSR(form="pem", file=mock.sentinel.key_file, data=CSR_SAN) + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + self._mock_obtain_certificate() + authzr = self._authzr_from_sans(self.eg_sans) + self.eg_order.authorizations = authzr + self.client.auth_handler.handle_authorizations.return_value = authzr + + error = messages.Error.with_code('caa', detail='foo', title='title') + self.client.acme.new_order.side_effect = error + + self.config.allow_subset_of_names = True + + with pytest.raises(messages.Error): + self.client.obtain_certificate(self.eg_sans) + assert self.client.auth_handler.handle_authorizations.call_count == 0 + assert self.acme.new_order.call_count == 1 + assert mock_crypto_util.generate_key.call_count == 1 + assert mock_crypto_util.cert_and_chain_from_fullchain.call_count == 0 + + @mock.patch("certbot._internal.client.crypto_util") + @mock.patch("certbot._internal.client.acme_crypto_util") + def test_obtain_certificate_dry_run(self, mock_acme_crypto, mock_crypto): + csr = util.CSR(form="pem", file=None, data=CSR_SAN) + mock_acme_crypto.make_csr.return_value = CSR_SAN + mock_crypto.make_key.return_value = mock.sentinel.key_pem + key = util.Key(file=None, pem=mock.sentinel.key_pem) + self._set_mock_from_fullchain(mock_crypto.cert_and_chain_from_fullchain) + + self.client.config.dry_run = True + self._test_obtain_certificate_common(key, csr) + + mock_crypto.make_key.assert_called_once_with( + bits=self.config.rsa_key_size, + elliptic_curve="secp256r1", + key_type=self.config.key_type, + ) + # Assumes all of eg_sans are DNSNames. + eg_domains = list(map(str, self.eg_sans)) + mock_acme_crypto.make_csr.assert_called_once_with( + mock.sentinel.key_pem, eg_domains, self.config.must_staple, ipaddrs=[]) + mock_crypto.generate_key.assert_not_called() + mock_crypto.generate_csr.assert_not_called() + assert mock_crypto.cert_and_chain_from_fullchain.call_count == 1 + + @mock.patch("certbot._internal.client.logger") + @mock.patch("certbot._internal.client.crypto_util") + @mock.patch("certbot._internal.client.acme_crypto_util") + def test_obtain_certificate_dry_run_authz_deactivations_failed(self, mock_acme_crypto, + mock_crypto, mock_log): + from acme import messages + csr = util.CSR(form="pem", file=None, data=CSR_SAN) + mock_acme_crypto.make_csr.return_value = CSR_SAN + mock_crypto.make_key.return_value = mock.sentinel.key_pem + key = util.Key(file=None, pem=mock.sentinel.key_pem) + self._set_mock_from_fullchain(mock_crypto.cert_and_chain_from_fullchain) + + self._mock_obtain_certificate() + self.client.config.dry_run = True + + # Two authzs that are already valid and should get deactivated (dry run) + authzrs = self._authzr_from_sans([san.DNSName("example.com"), san.DNSName("www.example.com")]) + for authzr in authzrs: + authzr.body.status = messages.STATUS_VALID + + # One deactivation succeeds, one fails + auth_handler = self.client.auth_handler + auth_handler.deactivate_valid_authorizations.return_value = ([authzrs[0]], [authzrs[1]]) + + # Certificate should get issued despite one failed deactivation + self.eg_order.authorizations = authzrs + self.client.auth_handler.handle_authorizations.return_value = authzrs + with test_util.patch_display_util(): + result = self.client.obtain_certificate(self.eg_sans) + assert result == (mock.sentinel.cert, mock.sentinel.chain, key, csr) + self._check_obtain_certificate(1) + + # Deactivation success/failure should have been handled properly + assert auth_handler.deactivate_valid_authorizations.call_count == 1, \ + "Deactivate authorizations should be called" + assert self.acme.new_order.call_count == 2, \ + "Order should be recreated due to successfully deactivated authorizations" + mock_log.warning.assert_called_with("Certbot was unable to obtain fresh authorizations for" + " every domain. The dry run will continue, but results" + " may not be accurate.") + + @mock.patch("certbot._internal.client.crypto_util") + def test_obtain_certificate_reuse_key_with_allow_subset_of_names(self, mock_crypto_util): + csr = util.CSR(form="pem", file=mock.sentinel.csr_file, data=CSR_SAN) + old_key = util.Key(file="old_key_file", pem="old_key_pem") + new_key = util.Key(file="new_key_file", pem="new_key_pem") + mock_crypto_util.generate_csr.return_value = csr + mock_crypto_util.generate_key.return_value = new_key + self._set_mock_from_fullchain(mock_crypto_util.cert_and_chain_from_fullchain) + + authzr = self._authzr_from_sans([san.DNSName("example.com")]) + self.config.allow_subset_of_names = True + self.config.reuse_key = True + + self._mock_obtain_certificate() + + self.eg_order.authorizations = authzr + self.client.auth_handler.handle_authorizations.return_value = authzr + + with test_util.patch_display_util(): + mocked_open = mock.mock_open(read_data="old_key_pem") + with mock.patch('builtins.open', mocked_open): + result = self.client.obtain_certificate(self.eg_sans, "old_key_file") + + assert result == \ + (mock.sentinel.cert, mock.sentinel.chain, old_key, csr) + self._check_obtain_certificate(2) + + assert mock_crypto_util.generate_key.call_count == 0 + + def _set_mock_from_fullchain(self, mock_from_fullchain): + mock_cert = mock.Mock() + mock_cert.encode.return_value = mock.sentinel.cert + mock_chain = mock.Mock() + mock_chain.encode.return_value = mock.sentinel.chain + mock_from_fullchain.return_value = (mock_cert, mock_chain) + + def _authzr_from_sans(self, sans): + authzr = [] + + # domain ordering should not be affected by authorization order + for s in reversed(sans): + if type(s) is not san.DNSName: + raise TypeError(f"expected DNSName but got {type(s)}") + authzr.append( + mock.MagicMock( + body=mock.MagicMock( + identifier=mock.MagicMock( + value=str(s), + typ="dns")))) + return authzr + + def _test_obtain_certificate_common(self, key, csr, authzr_ret=None, auth_count=1): + self._mock_obtain_certificate() + + # return_value is essentially set to (None, None) in + # _mock_obtain_certificate(), which breaks this test. + # Thus fixed by the next line. + authzr = authzr_ret or self._authzr_from_sans(self.eg_sans) + + self.eg_order.authorizations = authzr + self.client.auth_handler.handle_authorizations.return_value = authzr + + with test_util.patch_display_util(): + result = self.client.obtain_certificate(self.eg_sans) + + assert result == \ + (mock.sentinel.cert, mock.sentinel.chain, key, csr) + self._check_obtain_certificate(auth_count) + + @mock.patch('certbot._internal.client.Client.obtain_certificate') + @mock.patch('certbot._internal.storage.RenewableCert.new_lineage') + def test_obtain_and_enroll_certificate(self, + mock_storage, mock_obtain_certificate): + sans = [san.DNSName("*.example.com"), san.DNSName("example.com")] + mock_obtain_certificate.return_value = (mock.MagicMock(), + mock.MagicMock(), mock.MagicMock(), None) + + self.client.config.dry_run = False + assert self.client.obtain_and_enroll_certificate(sans, "example_cert") + + assert self.client.obtain_and_enroll_certificate(sans, None) + assert self.client.obtain_and_enroll_certificate(sans[1:], None) + + self.client.config.dry_run = True + + assert not self.client.obtain_and_enroll_certificate(sans, None) + + names = [call[0][0] for call in mock_storage.call_args_list] + assert names == ["example_cert", "example.com", "example.com"] + + @mock.patch("certbot._internal.cli.helpful_parser") + def test_save_certificate(self, mock_parser): + certs = ["cert_512.pem", "cert-san_512.pem"] + tmp_path = tempfile.mkdtemp() + + cert_pem = test_util.load_vector(certs[0]) + chain_pem = (test_util.load_vector(certs[0]) + test_util.load_vector(certs[1])) + candidate_cert_path = os.path.join(tmp_path, "certs", "cert_512.pem") + candidate_chain_path = os.path.join(tmp_path, "chains", "chain.pem") + candidate_fullchain_path = os.path.join(tmp_path, "chains", "fullchain.pem") + mock_parser.verb = "certonly" + mock_parser.args = ["--cert-path", candidate_cert_path, + "--chain-path", candidate_chain_path, + "--fullchain-path", candidate_fullchain_path] + + cert_path, chain_path, fullchain_path = self.client.save_certificate( + cert_pem, chain_pem, candidate_cert_path, candidate_chain_path, + candidate_fullchain_path) + + assert os.path.dirname(cert_path) == \ + os.path.dirname(candidate_cert_path) + assert os.path.dirname(chain_path) == \ + os.path.dirname(candidate_chain_path) + assert os.path.dirname(fullchain_path) == \ + os.path.dirname(candidate_fullchain_path) + + with open(cert_path, "rb") as cert_file: + cert_contents = cert_file.read() + assert cert_contents == test_util.load_vector(certs[0]) + + with open(chain_path, "rb") as chain_file: + chain_contents = chain_file.read() + assert chain_contents == test_util.load_vector(certs[0]) + \ + test_util.load_vector(certs[1]) + + shutil.rmtree(tmp_path) + + @test_util.patch_display_util() + def test_deploy_certificate_success(self, mock_util): + with pytest.raises(errors.Error): + self.client.deploy_certificate([san.DNSName("foo.bar")], "key", "cert", "chain", "fullchain") + + installer = mock.MagicMock() + self.client.installer = installer + + self.client.deploy_certificate([san.DNSName("foo.bar")], "key", "cert", "chain", "fullchain") + installer.deploy_cert.assert_called_once_with( + cert_path=os.path.abspath("cert"), + chain_path=os.path.abspath("chain"), + domain='foo.bar', + fullchain_path='fullchain', + key_path=os.path.abspath("key")) + assert installer.save.call_count == 2 + installer.restart.assert_called_once_with() + + @mock.patch('certbot._internal.client.display_util.notify') + @test_util.patch_display_util() + def test_deploy_certificate_failure(self, mock_util, mock_notify): + installer = mock.MagicMock() + self.client.installer = installer + self.config.installer = "foobar" + + installer.deploy_cert.side_effect = errors.PluginError + with pytest.raises(errors.PluginError): + self.client.deploy_certificate([san.DNSName("foo.bar")], "key", "cert", "chain", "fullchain") + installer.recovery_routine.assert_called_once_with() + + mock_notify.assert_any_call('Deploying certificate') + + + @test_util.patch_display_util() + def test_deploy_certificate_save_failure(self, mock_util): + installer = mock.MagicMock() + self.client.installer = installer + + installer.save.side_effect = errors.PluginError + with pytest.raises(errors.PluginError): + self.client.deploy_certificate([san.DNSName("foo.bar")], "key", "cert", "chain", "fullchain") + installer.recovery_routine.assert_called_once_with() + + @mock.patch('certbot._internal.client.display_util.notify') + @test_util.patch_display_util() + def test_deploy_certificate_restart_failure(self, mock_get_utility, mock_notify): + installer = mock.MagicMock() + installer.restart.side_effect = [errors.PluginError, None] + self.client.installer = installer + + with pytest.raises(errors.PluginError): + self.client.deploy_certificate([san.DNSName("foo.bar")], "key", "cert", "chain", "fullchain") + mock_notify.assert_called_with( + 'We were unable to install your certificate, however, we successfully restored ' + 'your server to its prior configuration.') + installer.rollback_checkpoints.assert_called_once_with() + assert installer.restart.call_count == 2 + + @mock.patch('certbot._internal.client.logger') + @test_util.patch_display_util() + def test_deploy_certificate_restart_failure2(self, mock_get_utility, mock_logger): + installer = mock.MagicMock() + installer.restart.side_effect = errors.PluginError + installer.rollback_checkpoints.side_effect = errors.ReverterError + self.client.installer = installer + + with pytest.raises(errors.PluginError): + self.client.deploy_certificate([san.DNSName("foo.bar")], "key", "cert", "chain", "fullchain") + assert mock_logger.error.call_count == 1 + assert 'An error occurred and we failed to restore your config' in \ + mock_logger.error.call_args[0][0] + installer.rollback_checkpoints.assert_called_once_with() + assert installer.restart.call_count == 1 + + def test_choose_lineage_name(self): + sep = os.path.sep + invalid_domains = [san.DNSName(f"exam{sep}ple.com")] + valid_domains = [san.DNSName("example.com")] + invalid_certname = f"foo{sep}.bar" + valid_certname = "foo.bar" + invalid_wildcard_domain = [san.DNSName(f"*.exam{sep}ple.com")] + # Verify errors are raised when invalid lineagename is chosen. + with pytest.raises(errors.Error): + self.client._choose_lineagename(invalid_domains, None) + with pytest.raises(errors.Error): + self.client._choose_lineagename(invalid_domains, invalid_certname) + with pytest.raises(errors.Error): + self.client._choose_lineagename(valid_domains, invalid_certname) + with pytest.raises(errors.Error): + self.client._choose_lineagename(invalid_wildcard_domain, None) + # Verify no error is raised when invalid domain is overridden by valid certname. + self.client._choose_lineagename(invalid_domains, valid_certname) + + +class EnhanceConfigTest(ClientTestCommon): + """Tests for certbot._internal.client.Client.enhance_config.""" + + def setUp(self): + super().setUp() + + self.config.hsts = False + self.config.redirect = False + self.config.staple = False + self.config.uir = False + self.domain = "example.org" + + def test_no_installer(self): + with pytest.raises(errors.Error): + self.client.enhance_config([san.DNSName(self.domain)], None) + + def test_unsupported(self): + self.client.installer = mock.MagicMock() + self.client.installer.supported_enhancements.return_value = [] + + self.config.redirect = None + self.config.hsts = True + with mock.patch("certbot._internal.client.logger") as mock_logger: + self.client.enhance_config([self.domain], None) + assert mock_logger.error.call_count == 1 + self.client.installer.enhance.assert_not_called() + + @mock.patch("certbot._internal.client.logger") + def test_already_exists_header(self, mock_log): + self.config.hsts = True + self._test_with_already_existing() + assert mock_log.info.called is True + assert mock_log.info.call_args[0][1] == \ + 'Strict-Transport-Security' + + @mock.patch("certbot._internal.client.logger") + def test_already_exists_redirect(self, mock_log): + self.config.redirect = True + self._test_with_already_existing() + assert mock_log.info.called is True + assert mock_log.info.call_args[0][1] == \ + 'redirect' + + @mock.patch("certbot._internal.client.logger") + def test_config_set_no_warning_redirect(self, mock_log): + self.config.redirect = False + self._test_with_already_existing() + assert mock_log.warning.called is False + + @mock.patch("certbot._internal.client.logger") + def test_no_warn_redirect(self, mock_log): + self.config.redirect = None + self._test_with_all_supported() + assert mock_log.warning.called is False + + def test_no_ask_hsts(self): + self.config.hsts = True + self._test_with_all_supported() + self.client.installer.enhance.assert_called_with( + self.domain, "ensure-http-header", "Strict-Transport-Security") + + def test_no_ask_redirect(self): + self.config.redirect = True + self._test_with_all_supported() + self.client.installer.enhance.assert_called_with( + self.domain, "redirect", None) + + def test_no_ask_staple(self): + self.config.staple = True + self._test_with_all_supported() + self.client.installer.enhance.assert_called_with( + self.domain, "staple-ocsp", None) + + def test_no_ask_uir(self): + self.config.uir = True + self._test_with_all_supported() + self.client.installer.enhance.assert_called_with( + self.domain, "ensure-http-header", "Upgrade-Insecure-Requests") + + def test_enhance_failure(self): + self.client.installer = mock.MagicMock() + self.client.installer.enhance.side_effect = errors.PluginError + self._test_error(enhance_error=True) + self.client.installer.recovery_routine.assert_called_once_with() + + def test_save_failure(self): + self.client.installer = mock.MagicMock() + self.client.installer.save.side_effect = errors.PluginError + self._test_error() + self.client.installer.recovery_routine.assert_called_once_with() + self.client.installer.save.assert_called_once_with(mock.ANY) + + def test_restart_failure(self): + self.client.installer = mock.MagicMock() + self.client.installer.restart.side_effect = [errors.PluginError, None] + self._test_error_with_rollback() + + def test_restart_failure2(self): + installer = mock.MagicMock() + installer.restart.side_effect = errors.PluginError + installer.rollback_checkpoints.side_effect = errors.ReverterError + self.client.installer = installer + self._test_error_with_rollback() + + def _test_error_with_rollback(self): + self._test_error() + assert self.client.installer.restart.called is True + + def _test_error(self, enhance_error=False, restart_error=False): + self.config.redirect = True + with mock.patch('certbot._internal.client.logger') as mock_logger, \ + test_util.patch_display_util(): + with pytest.raises(errors.PluginError): + self._test_with_all_supported() + + if enhance_error: + assert mock_logger.error.call_count == 1 + assert 'Unable to set the %s enhancement for %s.' == mock_logger.error.call_args_list[0][0][0] + if restart_error: + mock_logger.critical.assert_called_with( + 'Rolling back to previous server configuration...') + + def _test_with_all_supported(self): + if self.client.installer is None: + self.client.installer = mock.MagicMock() + self.client.installer.supported_enhancements.return_value = [ + "ensure-http-header", "redirect", "staple-ocsp"] + self.client.enhance_config([san.DNSName(self.domain)], None) + assert self.client.installer.save.call_count == 1 + assert self.client.installer.restart.call_count == 1 + + def _test_with_already_existing(self): + self.client.installer = mock.MagicMock() + self.client.installer.supported_enhancements.return_value = [ + "ensure-http-header", "redirect", "staple-ocsp"] + self.client.installer.enhance.side_effect = errors.PluginEnhancementAlreadyPresent() + self.client.enhance_config([san.DNSName(self.domain)], None) + + +class RollbackTest(unittest.TestCase): + """Tests for certbot._internal.client.rollback.""" + + def setUp(self): + self.m_install = mock.MagicMock() + + @classmethod + def _call(cls, checkpoints, side_effect): + from certbot._internal.client import rollback + with mock.patch("certbot._internal.client.plugin_selection.pick_installer") as mpi: + mpi.side_effect = side_effect + rollback(None, checkpoints, {}, mock.MagicMock()) + + def test_no_problems(self): + self._call(1, self.m_install) + assert self.m_install().rollback_checkpoints.call_count == 1 + assert self.m_install().restart.call_count == 1 + + def test_no_installer(self): + self._call(1, None) # Just make sure no exceptions are raised + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/compat/__init__.py b/certbot/src/certbot/_internal/tests/compat/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot/src/certbot/_internal/tests/compat/filesystem_test.py b/certbot/src/certbot/_internal/tests/compat/filesystem_test.py new file mode 100644 index 00000000000..565f17087e4 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/compat/filesystem_test.py @@ -0,0 +1,684 @@ +"""Tests for certbot.compat.filesystem""" +import contextlib +import errno +import sys +import unittest +from unittest import mock + +import pytest + +from certbot import util +from certbot._internal import lock +from certbot.compat import filesystem +from certbot.compat import os +import certbot.tests.util as test_util +from certbot.tests.util import TempDirTestCase + +try: + import ntsecuritycon + import win32api + import win32security + POSIX_MODE = False +except ImportError: + POSIX_MODE = True + + + +EVERYBODY_SID = 'S-1-1-0' +SYSTEM_SID = 'S-1-5-18' +ADMINS_SID = 'S-1-5-32-544' + + +@unittest.skipIf(POSIX_MODE, reason='Tests specific to Windows security') +class WindowsChmodTests(TempDirTestCase): + """Unit tests for Windows chmod function in filesystem module""" + def setUp(self): + super().setUp() + self.probe_path = _create_probe(self.tempdir) + + def test_symlink_resolution(self): + link_path = os.path.join(self.tempdir, 'link') + os.symlink(self.probe_path, link_path) + + ref_dacl_probe = _get_security_dacl(self.probe_path).GetSecurityDescriptorDacl() + ref_dacl_link = _get_security_dacl(link_path).GetSecurityDescriptorDacl() + + filesystem.chmod(link_path, 0o700) + + # Assert the real file is impacted, not the link. + cur_dacl_probe = _get_security_dacl(self.probe_path).GetSecurityDescriptorDacl() + cur_dacl_link = _get_security_dacl(link_path).GetSecurityDescriptorDacl() + assert not filesystem._compare_dacls(ref_dacl_probe, cur_dacl_probe) # pylint: disable=protected-access + assert filesystem._compare_dacls(ref_dacl_link, cur_dacl_link) # pylint: disable=protected-access + + def test_world_permission(self): + everybody = win32security.ConvertStringSidToSid(EVERYBODY_SID) + + filesystem.chmod(self.probe_path, 0o700) + dacl = _get_security_dacl(self.probe_path).GetSecurityDescriptorDacl() + + assert not [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == everybody] + + filesystem.chmod(self.probe_path, 0o704) + dacl = _get_security_dacl(self.probe_path).GetSecurityDescriptorDacl() + + assert [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == everybody] + + def test_group_permissions_noop(self): + filesystem.chmod(self.probe_path, 0o700) + ref_dacl_probe = _get_security_dacl(self.probe_path).GetSecurityDescriptorDacl() + + filesystem.chmod(self.probe_path, 0o740) + cur_dacl_probe = _get_security_dacl(self.probe_path).GetSecurityDescriptorDacl() + + assert filesystem._compare_dacls(ref_dacl_probe, cur_dacl_probe) # pylint: disable=protected-access + + def test_admin_permissions(self): + system = win32security.ConvertStringSidToSid(SYSTEM_SID) + admins = win32security.ConvertStringSidToSid(ADMINS_SID) + + filesystem.chmod(self.probe_path, 0o400) + dacl = _get_security_dacl(self.probe_path).GetSecurityDescriptorDacl() + + system_aces = [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == system] + admin_aces = [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == admins] + + assert len(system_aces) == 1 + assert len(admin_aces) == 1 + + assert system_aces[0][1] == ntsecuritycon.FILE_ALL_ACCESS + assert admin_aces[0][1] == ntsecuritycon.FILE_ALL_ACCESS + + def test_read_flag(self): + self._test_flag(4, ntsecuritycon.FILE_GENERIC_READ) + + def test_execute_flag(self): + self._test_flag(1, ntsecuritycon.FILE_GENERIC_EXECUTE) + + def test_write_flag(self): + self._test_flag(2, (ntsecuritycon.FILE_ALL_ACCESS + ^ ntsecuritycon.FILE_GENERIC_READ + ^ ntsecuritycon.FILE_GENERIC_EXECUTE)) + + def test_full_flag(self): + self._test_flag(7, ntsecuritycon.FILE_ALL_ACCESS) + + def _test_flag(self, everyone_mode, windows_flag): + # Note that flag is tested against `everyone`, not `user`, because practically these unit + # tests are executed with admin privilege, so current user is effectively the admins group, + # and so will always have all rights. + filesystem.chmod(self.probe_path, 0o700 | everyone_mode) + dacl = _get_security_dacl(self.probe_path).GetSecurityDescriptorDacl() + everybody = win32security.ConvertStringSidToSid(EVERYBODY_SID) + + acls_everybody = [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == everybody] + + assert len(acls_everybody) == 1 + + acls_everybody = acls_everybody[0] + + assert acls_everybody[1] == windows_flag + + def test_user_admin_dacl_consistency(self): + # Set ownership of target to authenticated user + authenticated_user, _, _ = win32security.LookupAccountName("", win32api.GetUserName()) + security_owner = _get_security_owner(self.probe_path) + _set_owner(self.probe_path, security_owner, authenticated_user) + + filesystem.chmod(self.probe_path, 0o700) + + security_dacl = _get_security_dacl(self.probe_path) + # We expect three ACE: one for admins, one for system, and one for the user + assert security_dacl.GetSecurityDescriptorDacl().GetAceCount() == 3 + + # Set ownership of target to Administrators user group + admin_user = win32security.ConvertStringSidToSid(ADMINS_SID) + security_owner = _get_security_owner(self.probe_path) + _set_owner(self.probe_path, security_owner, admin_user) + + filesystem.chmod(self.probe_path, 0o700) + + security_dacl = _get_security_dacl(self.probe_path) + # We expect only two ACE: one for admins, one for system, + # since the user is also the admins group + assert security_dacl.GetSecurityDescriptorDacl().GetAceCount() == 2 + + +class UmaskTest(TempDirTestCase): + def test_umask_on_dir(self): + previous_umask = filesystem.umask(0o022) + + try: + dir1 = os.path.join(self.tempdir, 'probe1') + filesystem.mkdir(dir1) + assert filesystem.check_mode(dir1, 0o755) is True + + filesystem.umask(0o077) + + dir2 = os.path.join(self.tempdir, 'dir2') + filesystem.mkdir(dir2) + assert filesystem.check_mode(dir2, 0o700) is True + + dir3 = os.path.join(self.tempdir, 'dir3') + filesystem.mkdir(dir3, mode=0o777) + assert filesystem.check_mode(dir3, 0o700) is True + finally: + filesystem.umask(previous_umask) + + def test_umask_on_file(self): + previous_umask = filesystem.umask(0o022) + + try: + file1 = os.path.join(self.tempdir, 'probe1') + UmaskTest._create_file(file1) + assert filesystem.check_mode(file1, 0o755) is True + + filesystem.umask(0o077) + + file2 = os.path.join(self.tempdir, 'probe2') + UmaskTest._create_file(file2) + assert filesystem.check_mode(file2, 0o700) is True + + file3 = os.path.join(self.tempdir, 'probe3') + UmaskTest._create_file(file3) + assert filesystem.check_mode(file3, 0o700) is True + finally: + filesystem.umask(previous_umask) + + @staticmethod + def _create_file(path, mode=0o777): + file_desc = None + try: + file_desc = filesystem.open(path, flags=os.O_CREAT, mode=mode) + finally: + if file_desc: + os.close(file_desc) + + +class ComputePrivateKeyModeTest(TempDirTestCase): + def setUp(self): + super().setUp() + self.probe_path = _create_probe(self.tempdir) + + def test_compute_private_key_mode(self): + filesystem.chmod(self.probe_path, 0o777) + new_mode = filesystem.compute_private_key_mode(self.probe_path, 0o600) + + if POSIX_MODE: + # On Linux RWX permissions for group and R permission for world + # are persisted from the existing moe + assert new_mode == 0o674 + else: + # On Windows no permission is persisted + assert new_mode == 0o600 + + +@unittest.skipIf(POSIX_MODE, reason='Tests specific to Windows security') +class WindowsOpenTest(TempDirTestCase): + def test_new_file_correct_permissions(self): + path = os.path.join(self.tempdir, 'file') + + desc = filesystem.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o700) + os.close(desc) + + dacl = _get_security_dacl(path).GetSecurityDescriptorDacl() + everybody = win32security.ConvertStringSidToSid(EVERYBODY_SID) + + assert not [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == everybody] + + def test_existing_file_correct_permissions(self): + path = os.path.join(self.tempdir, 'file') + open(path, 'w').close() + + desc = filesystem.open(path, os.O_EXCL | os.O_RDWR, 0o700) + os.close(desc) + + dacl = _get_security_dacl(path).GetSecurityDescriptorDacl() + everybody = win32security.ConvertStringSidToSid(EVERYBODY_SID) + + assert not [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == everybody] + + def test_create_file_on_open(self): + # os.O_CREAT | os.O_EXCL + file not exists = OK + self._test_one_creation(1, file_exist=False, flags=(os.O_CREAT | os.O_EXCL)) + + # os.O_CREAT | os.O_EXCL + file exists = EEXIST OS exception + with pytest.raises(OSError) as exc_info: + self._test_one_creation(2, file_exist=True, flags=(os.O_CREAT | os.O_EXCL)) + assert exc_info.value.errno == errno.EEXIST + + # os.O_CREAT + file not exists = OK + self._test_one_creation(3, file_exist=False, flags=os.O_CREAT) + + # os.O_CREAT + file exists = OK + self._test_one_creation(4, file_exist=True, flags=os.O_CREAT) + + # os.O_CREAT + file exists (locked) = EACCES OS exception + path = os.path.join(self.tempdir, '5') + open(path, 'w').close() + filelock = lock.LockFile(path) + try: + with pytest.raises(OSError) as exc_info: + self._test_one_creation(5, file_exist=True, flags=os.O_CREAT) + assert exc_info.value.errno == errno.EACCES + finally: + filelock.release() + + # os.O_CREAT not set + file not exists = OS exception + with pytest.raises(OSError): + self._test_one_creation(6, file_exist=False, flags=os.O_RDONLY) + + def _test_one_creation(self, num, file_exist, flags): + one_file = os.path.join(self.tempdir, str(num)) + if file_exist and not os.path.exists(one_file): + with open(one_file, 'w'): + pass + + handler = None + try: + handler = filesystem.open(one_file, flags) + finally: + if handler: + os.close(handler) + + +class TempUmaskTests(test_util.TempDirTestCase): + """Tests for using the TempUmask class in `with` statements""" + def _check_umask(self): + old_umask = filesystem.umask(0) + filesystem.umask(old_umask) + return old_umask + + def test_works_normally(self): + filesystem.umask(0o0022) + assert self._check_umask() == 0o0022 + with filesystem.temp_umask(0o0077): + assert self._check_umask() == 0o0077 + assert self._check_umask() == 0o0022 + + def test_resets_umask_after_exception(self): + filesystem.umask(0o0022) + assert self._check_umask() == 0o0022 + try: + with filesystem.temp_umask(0o0077): + assert self._check_umask() == 0o0077 + raise Exception() + except: + assert self._check_umask() == 0o0022 + + +@unittest.skipIf(POSIX_MODE, reason='Test specific to Windows security') +class WindowsMkdirTests(test_util.TempDirTestCase): + """Unit tests for Windows mkdir + makedirs functions in filesystem module""" + def test_mkdir_correct_permissions(self): + path = os.path.join(self.tempdir, 'dir') + + filesystem.mkdir(path, 0o700) + + everybody = win32security.ConvertStringSidToSid(EVERYBODY_SID) + + dacl = _get_security_dacl(path).GetSecurityDescriptorDacl() + assert not [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == everybody] + + def test_makedirs_correct_permissions(self): + path = os.path.join(self.tempdir, 'dir') + subpath = os.path.join(path, 'subpath') + + filesystem.makedirs(subpath, 0o700) + + everybody = win32security.ConvertStringSidToSid(EVERYBODY_SID) + + dacl = _get_security_dacl(subpath).GetSecurityDescriptorDacl() + assert not [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == everybody] + + def test_makedirs_switch_os_mkdir(self): + path = os.path.join(self.tempdir, 'dir') + import os as std_os # pylint: disable=os-module-forbidden + original_mkdir = std_os.mkdir + + filesystem.makedirs(path) + assert original_mkdir == std_os.mkdir + + try: + filesystem.makedirs(path) # Will fail because path already exists + except OSError: + pass + assert original_mkdir == std_os.mkdir + + +class MakedirsTests(test_util.TempDirTestCase): + """Unit tests for makedirs function in filesystem module""" + def test_makedirs_correct_permissions(self): + path = os.path.join(self.tempdir, 'dir') + subpath = os.path.join(path, 'subpath') + + previous_umask = filesystem.umask(0o022) + + try: + filesystem.makedirs(subpath, 0o700) + + assert filesystem.check_mode(path, 0o700) + assert filesystem.check_mode(subpath, 0o700) + finally: + filesystem.umask(previous_umask) + + +class CopyOwnershipAndModeTest(test_util.TempDirTestCase): + """Tests about copy_ownership_and_apply_mode, copy_ownership_and_mode and has_same_ownership""" + def setUp(self): + super().setUp() + self.probe_path = _create_probe(self.tempdir) + + @unittest.skipIf(POSIX_MODE, reason='Test specific to Windows security') + def test_copy_ownership_and_apply_mode_windows(self): + system = win32security.ConvertStringSidToSid(SYSTEM_SID) + security = win32security.SECURITY_ATTRIBUTES().SECURITY_DESCRIPTOR + security.SetSecurityDescriptorOwner(system, False) + + with mock.patch('win32security.GetFileSecurity') as mock_get: + with mock.patch('win32security.SetFileSecurity') as mock_set: + mock_get.return_value = security + filesystem.copy_ownership_and_apply_mode( + 'dummy', self.probe_path, 0o700, copy_user=True, copy_group=False) + + assert mock_set.call_count == 2 + + first_call = mock_set.call_args_list[0] + security = first_call[0][2] + assert system == security.GetSecurityDescriptorOwner() + + second_call = mock_set.call_args_list[1] + security = second_call[0][2] + dacl = security.GetSecurityDescriptorDacl() + everybody = win32security.ConvertStringSidToSid(EVERYBODY_SID) + assert dacl.GetAceCount() + assert not [dacl.GetAce(index) for index in range(0, dacl.GetAceCount()) + if dacl.GetAce(index)[2] == everybody] + + @unittest.skipUnless(POSIX_MODE, reason='Test specific to Linux security') + def test_copy_ownership_and_apply_mode_linux(self): + with mock.patch('os.chown') as mock_chown: + with mock.patch('os.chmod') as mock_chmod: + with mock.patch('os.stat') as mock_stat: + mock_stat.return_value.st_uid = 50 + mock_stat.return_value.st_gid = 51 + filesystem.copy_ownership_and_apply_mode( + 'dummy', self.probe_path, 0o700, copy_user=True, copy_group=True) + + mock_chown.assert_called_once_with(self.probe_path, 50, 51) + mock_chmod.assert_called_once_with(self.probe_path, 0o700) + + def test_has_same_ownership(self): + path1 = os.path.join(self.tempdir, 'test1') + path2 = os.path.join(self.tempdir, 'test2') + + util.safe_open(path1, 'w').close() + util.safe_open(path2, 'w').close() + + assert filesystem.has_same_ownership(path1, path2) is True + + @unittest.skipIf(POSIX_MODE, reason='Test specific to Windows security') + def test_copy_ownership_and_mode_windows(self): + src = self.probe_path + dst = _create_probe(self.tempdir, name='dst') + + filesystem.chmod(src, 0o700) + assert filesystem.check_mode(src, 0o700) is True + assert filesystem.check_mode(dst, 0o744) is True + + # Checking an actual change of owner is tricky during a unit test, since we do not know + # if any user exists beside the current one. So we mock _copy_win_ownership. It's behavior + # have been checked theoretically with test_copy_ownership_and_apply_mode_windows. + with mock.patch('certbot.compat.filesystem._copy_win_ownership') as mock_copy_owner: + filesystem.copy_ownership_and_mode(src, dst) + + mock_copy_owner.assert_called_once_with(src, dst) + assert filesystem.check_mode(dst, 0o700) is True + + +class CheckPermissionsTest(test_util.TempDirTestCase): + """Tests relative to functions that check modes.""" + def setUp(self): + super().setUp() + self.probe_path = _create_probe(self.tempdir) + + def test_check_mode(self): + assert filesystem.check_mode(self.probe_path, 0o744) is True + + filesystem.chmod(self.probe_path, 0o700) + assert not filesystem.check_mode(self.probe_path, 0o744) + + @unittest.skipIf(POSIX_MODE, reason='Test specific to Windows security') + def test_check_owner_windows(self): + assert filesystem.check_owner(self.probe_path) is True + + system = win32security.ConvertStringSidToSid(SYSTEM_SID) + security = win32security.SECURITY_ATTRIBUTES().SECURITY_DESCRIPTOR + security.SetSecurityDescriptorOwner(system, False) + + with mock.patch('win32security.GetFileSecurity') as mock_get: + mock_get.return_value = security + assert not filesystem.check_owner(self.probe_path) + + @unittest.skipUnless(POSIX_MODE, reason='Test specific to Linux security') + def test_check_owner_linux(self): + assert filesystem.check_owner(self.probe_path) is True + + import os as std_os # pylint: disable=os-module-forbidden + + # See related inline comment in certbot.compat.filesystem.check_owner method + # that explains why MyPy/PyLint check disable is needed here. + uid = std_os.getuid() + + with mock.patch('os.getuid') as mock_uid: + mock_uid.return_value = uid + 1 + assert not filesystem.check_owner(self.probe_path) + + def test_check_permissions(self): + assert filesystem.check_permissions(self.probe_path, 0o744) is True + + with mock.patch('certbot.compat.filesystem.check_mode') as mock_mode: + mock_mode.return_value = False + assert not filesystem.check_permissions(self.probe_path, 0o744) + + with mock.patch('certbot.compat.filesystem.check_owner') as mock_owner: + mock_owner.return_value = False + assert not filesystem.check_permissions(self.probe_path, 0o744) + + def test_check_min_permissions(self): + filesystem.chmod(self.probe_path, 0o744) + assert filesystem.has_min_permissions(self.probe_path, 0o744) is True + + filesystem.chmod(self.probe_path, 0o700) + assert not filesystem.has_min_permissions(self.probe_path, 0o744) + + filesystem.chmod(self.probe_path, 0o741) + assert not filesystem.has_min_permissions(self.probe_path, 0o744) + + def test_is_world_reachable(self): + filesystem.chmod(self.probe_path, 0o744) + assert filesystem.has_world_permissions(self.probe_path) is True + + filesystem.chmod(self.probe_path, 0o700) + assert not filesystem.has_world_permissions(self.probe_path) + + +class OsReplaceTest(test_util.TempDirTestCase): + """Test to ensure consistent behavior of rename method""" + def test_os_replace_to_existing_file(self): + """Ensure that replace will effectively rename src into dst for all platforms.""" + src = os.path.join(self.tempdir, 'src') + dst = os.path.join(self.tempdir, 'dst') + open(src, 'w').close() + open(dst, 'w').close() + + # On Windows, a direct call to os.rename would fail because dst already exists. + filesystem.replace(src, dst) + + assert not os.path.exists(src) + assert os.path.exists(dst) is True + + +class RealpathTest(test_util.TempDirTestCase): + """Tests for realpath method""" + def setUp(self): + super().setUp() + self.probe_path = _create_probe(self.tempdir) + + def test_symlink_resolution(self): + # Remove any symlinks already in probe_path + self.probe_path = filesystem.realpath(self.probe_path) + # Absolute resolution + link_path = os.path.join(self.tempdir, 'link_abs') + os.symlink(self.probe_path, link_path) + + assert self.probe_path == filesystem.realpath(self.probe_path) + assert self.probe_path == filesystem.realpath(link_path) + + # Relative resolution + curdir = os.getcwd() + link_path = os.path.join(self.tempdir, 'link_rel') + probe_name = os.path.basename(self.probe_path) + try: + os.chdir(os.path.dirname(self.probe_path)) + os.symlink(probe_name, link_path) + + assert self.probe_path == filesystem.realpath(probe_name) + assert self.probe_path == filesystem.realpath(link_path) + finally: + os.chdir(curdir) + + def test_symlink_loop_mitigation(self): + link1_path = os.path.join(self.tempdir, 'link1') + link2_path = os.path.join(self.tempdir, 'link2') + link3_path = os.path.join(self.tempdir, 'link3') + os.symlink(link1_path, link2_path) + os.symlink(link2_path, link3_path) + os.symlink(link3_path, link1_path) + + with pytest.raises(RuntimeError, match='link1 is a loop!'): + filesystem.realpath(link1_path) + + +class IsExecutableTest(test_util.TempDirTestCase): + """Tests for is_executable method""" + def test_not_executable(self): + file_path = os.path.join(self.tempdir, "foo") + + # On Windows a file created within Certbot will always have all permissions to the + # Administrators group set. Since the unit tests are typically executed under elevated + # privileges, it means that current user will always have effective execute rights on the + # hook script, and so the test will fail. To prevent that and represent a file created + # outside Certbot as typically a hook file is, we mock the _generate_dacl function in + # certbot.compat.filesystem to give rights only to the current user. This implies removing + # all ACEs except the first one from the DACL created by original _generate_dacl function. + + from certbot.compat.filesystem import _generate_dacl + + def _execute_mock(user_sid, mode, mask=None): + dacl = _generate_dacl(user_sid, mode, mask) + for _ in range(1, dacl.GetAceCount()): + dacl.DeleteAce(1) # DeleteAce dynamically updates the internal index mapping. + return dacl + + # create a non-executable file + with mock.patch("certbot.compat.filesystem._generate_dacl", side_effect=_execute_mock): + os.close(filesystem.open(file_path, os.O_CREAT | os.O_WRONLY, 0o666)) + + assert not filesystem.is_executable(file_path) + + @mock.patch("certbot.compat.filesystem.os.path.isfile") + @mock.patch("certbot.compat.filesystem.os.access") + def test_full_path(self, mock_access, mock_isfile): + with _fix_windows_runtime(): + mock_access.return_value = True + mock_isfile.return_value = True + assert filesystem.is_executable("/path/to/exe") is True + + @mock.patch("certbot.compat.filesystem.os.path.isfile") + @mock.patch("certbot.compat.filesystem.os.access") + def test_rel_path(self, mock_access, mock_isfile): + with _fix_windows_runtime(): + mock_access.return_value = True + mock_isfile.return_value = True + assert filesystem.is_executable("exe") is True + + @mock.patch("certbot.compat.filesystem.os.path.isfile") + @mock.patch("certbot.compat.filesystem.os.access") + def test_not_found(self, mock_access, mock_isfile): + with _fix_windows_runtime(): + mock_access.return_value = True + mock_isfile.return_value = False + assert not filesystem.is_executable("exe") + + +class ReadlinkTest(unittest.TestCase): + @unittest.skipUnless(POSIX_MODE, reason='Tests specific to Linux') + @mock.patch("certbot.compat.filesystem.os.readlink") + def test_path_posix(self, mock_readlink): + mock_readlink.return_value = "/normal/path" + assert filesystem.readlink("dummy") == "/normal/path" + + @unittest.skipIf(POSIX_MODE, reason='Tests specific to Windows') + @mock.patch("certbot.compat.filesystem.os.readlink") + def test_normal_path_windows(self, mock_readlink): + # test the standard path format + mock_readlink.return_value = "C:\\short\\path" + assert filesystem.readlink("dummy") == "C:\\short\\path" + + # test the extended form + mock_readlink.return_value = "\\\\?\\C:\\short\\path" + assert filesystem.readlink("dummy") == "C:\\short\\path" + + @unittest.skipIf(POSIX_MODE, reason='Tests specific to Windows') + @mock.patch("certbot.compat.filesystem.os.readlink") + def test_extended_path_windows(self, mock_readlink): + # Following path is largely over the 260 characters permitted in the normal form. + mock_readlink.return_value = "\\\\?\\C:\\long" + 1000 * "\\path" + with pytest.raises(ValueError): + filesystem.readlink("dummy") + +@contextlib.contextmanager +def _fix_windows_runtime(): + if os.name != 'nt': + yield + else: + with mock.patch('win32security.GetFileSecurity') as mock_get: + dacl_mock = mock_get.return_value.GetSecurityDescriptorDacl + mode_mock = dacl_mock.return_value.GetEffectiveRightsFromAcl + mode_mock.return_value = ntsecuritycon.FILE_GENERIC_EXECUTE + yield + + +def _get_security_dacl(target): + return win32security.GetFileSecurity(target, win32security.DACL_SECURITY_INFORMATION) + + +def _get_security_owner(target): + return win32security.GetFileSecurity(target, win32security.OWNER_SECURITY_INFORMATION) + + +def _set_owner(target, security_owner, user): + security_owner.SetSecurityDescriptorOwner(user, False) + win32security.SetFileSecurity( + target, win32security.OWNER_SECURITY_INFORMATION, security_owner) + + +def _create_probe(tempdir, name='probe'): + filesystem.chmod(tempdir, 0o744) + probe_path = os.path.join(tempdir, name) + util.safe_open(probe_path, 'w', chmod=0o744).close() + return probe_path + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/compat/misc_test.py b/certbot/src/certbot/_internal/tests/compat/misc_test.py new file mode 100644 index 00000000000..cf76f36cb6d --- /dev/null +++ b/certbot/src/certbot/_internal/tests/compat/misc_test.py @@ -0,0 +1,48 @@ +"""Tests for certbot.compat.misc""" +import sys +from unittest import mock + +import pytest + +from certbot.compat import os + + +class ExecuteStatusTest: + """Tests for certbot.compat.misc.execute_command_status.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.compat.misc import execute_command_status + return execute_command_status(*args, **kwargs) + + def _test_common(self, returncode, stdout, stderr): + given_command = "foo" + given_name = "foo-hook" + with mock.patch("certbot.compat.misc.subprocess.run") as mock_run: + mock_run.return_value.stdout = stdout + mock_run.return_value.stderr = stderr + mock_run.return_value.returncode = returncode + with mock.patch("certbot.compat.misc.logger") as mock_logger: + assert self._call(given_name, given_command) == (returncode, stderr, stdout) + + executed_command = mock_run.call_args[1].get( + "args", mock_run.call_args[0][0]) + if os.name == 'nt': + expected_command = ['powershell.exe', '-Command', given_command] + else: + expected_command = given_command + assert executed_command == expected_command + assert executed_command == expected_command + + mock_logger.info.assert_any_call("Running %s command: %s", + given_name, given_command) + + def test_it(self): + for returncode in range(0, 2): + for stdout in ("", "Hello World!",): + for stderr in ("", "Goodbye Cruel World!"): + self._test_common(returncode, stdout, stderr) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/compat/os_test.py b/certbot/src/certbot/_internal/tests/compat/os_test.py new file mode 100644 index 00000000000..5bbc70fedef --- /dev/null +++ b/certbot/src/certbot/_internal/tests/compat/os_test.py @@ -0,0 +1,22 @@ +"""Unit test for os module.""" +import sys + +import pytest + +from certbot.compat import os + + +def test_forbidden_methods(): + # Checks for os module + for method in ['chmod', 'chown', 'open', 'mkdir', 'makedirs', 'rename', + 'replace', 'access', 'stat', 'fstat']: + with pytest.raises(RuntimeError): + getattr(os, method)() + # Checks for os.path module + for method in ['realpath']: + with pytest.raises(RuntimeError): + getattr(os.path, method)() + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/configuration_test.py b/certbot/src/certbot/_internal/tests/configuration_test.py new file mode 100644 index 00000000000..61c310b5505 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/configuration_test.py @@ -0,0 +1,171 @@ +"""Tests for certbot.configuration.""" +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot._internal import constants +from certbot.compat import misc +from certbot.compat import os +from certbot.tests import util as test_util + + +class NamespaceConfigTest(test_util.ConfigTestCase): + """Tests for certbot.configuration.NamespaceConfig.""" + + def setUp(self): + super().setUp() + self.config.foo = 'bar' # pylint: disable=blacklisted-name + self.config.server = 'https://acme-server.org:443/new' + self.config.https_port = 1234 + self.config.http01_port = 4321 + + def test_init_same_ports(self): + self.config.https_port = 4321 + from certbot.configuration import NamespaceConfig + with pytest.raises(errors.Error): + NamespaceConfig(self.config.namespace) + + def test_proxy_getattr(self): + assert self.config.foo == 'bar' + assert self.config.work_dir == os.path.join(self.tempdir, 'work') + + def test_server_path(self): + assert ['acme-server.org:443', 'new'] == \ + self.config.server_path.split(os.path.sep) + + self.config.server = ('http://user:pass@acme.server:443' + '/p/a/t/h;parameters?query#fragment') + assert ['user:pass@acme.server:443', 'p', 'a', 't', 'h'] == \ + self.config.server_path.split(os.path.sep) + + @mock.patch('certbot.configuration.constants') + def test_dynamic_dirs(self, mock_constants): + mock_constants.ACCOUNTS_DIR = 'acc' + mock_constants.BACKUP_DIR = 'backups' + + mock_constants.IN_PROGRESS_DIR = '../p' + mock_constants.KEY_DIR = 'keys' + mock_constants.TEMP_CHECKPOINT_DIR = 't' + + ref_path = misc.underscores_for_unsupported_characters_in_path( + 'acc/acme-server.org:443/new') + assert os.path.normpath(self.config.accounts_dir) == \ + os.path.normpath(os.path.join(self.config.config_dir, ref_path)) + assert os.path.normpath(self.config.backup_dir) == \ + os.path.normpath(os.path.join(self.config.work_dir, 'backups')) + assert os.path.normpath(self.config.in_progress_dir) == \ + os.path.normpath(os.path.join(self.config.work_dir, '../p')) + assert os.path.normpath(self.config.temp_checkpoint_dir) == \ + os.path.normpath(os.path.join(self.config.work_dir, 't')) + + def test_absolute_paths(self): + from certbot.configuration import NamespaceConfig + + config_base = "foo" + work_base = "bar" + logs_base = "baz" + server = "mock.server" + + mock_namespace = mock.MagicMock(spec=['config_dir', 'work_dir', + 'logs_dir', 'http01_port', + 'https_port', + 'domains', 'server']) + mock_namespace.config_dir = config_base + mock_namespace.work_dir = work_base + mock_namespace.logs_dir = logs_base + mock_namespace.server = server + config = NamespaceConfig(mock_namespace) + + assert os.path.isabs(config.config_dir) + assert config.config_dir == \ + os.path.join(os.getcwd(), config_base) + assert os.path.isabs(config.work_dir) + assert config.work_dir == \ + os.path.join(os.getcwd(), work_base) + assert os.path.isabs(config.logs_dir) + assert config.logs_dir == \ + os.path.join(os.getcwd(), logs_base) + assert os.path.isabs(config.accounts_dir) + assert os.path.isabs(config.backup_dir) + assert os.path.isabs(config.in_progress_dir) + assert os.path.isabs(config.temp_checkpoint_dir) + + @mock.patch('certbot.configuration.constants') + def test_renewal_dynamic_dirs(self, mock_constants): + mock_constants.ARCHIVE_DIR = 'a' + mock_constants.LIVE_DIR = 'l' + mock_constants.RENEWAL_CONFIGS_DIR = 'renewal_configs' + + assert self.config.default_archive_dir == os.path.join(self.config.config_dir, 'a') + assert self.config.live_dir == os.path.join(self.config.config_dir, 'l') + assert self.config.renewal_configs_dir == os.path.join( + self.config.config_dir, 'renewal_configs') + + def test_renewal_absolute_paths(self): + from certbot.configuration import NamespaceConfig + + config_base = "foo" + work_base = "bar" + logs_base = "baz" + + mock_namespace = mock.MagicMock(spec=['config_dir', 'work_dir', + 'logs_dir', 'http01_port', + 'https_port', + 'domains', 'server']) + mock_namespace.config_dir = config_base + mock_namespace.work_dir = work_base + mock_namespace.logs_dir = logs_base + config = NamespaceConfig(mock_namespace) + + assert os.path.isabs(config.default_archive_dir) + assert os.path.isabs(config.live_dir) + assert os.path.isabs(config.renewal_configs_dir) + + def test_get_and_set_attr(self): + self.config.foo = 42 + assert self.config.namespace.foo == 42 + self.config.namespace.bar = 1337 + assert self.config.bar == 1337 + + def test_hook_directories(self): + assert self.config.renewal_hooks_dir == \ + os.path.join(self.config.config_dir, + constants.RENEWAL_HOOKS_DIR) + assert self.config.renewal_pre_hooks_dir == \ + os.path.join(self.config.renewal_hooks_dir, + constants.RENEWAL_PRE_HOOKS_DIR) + assert self.config.renewal_deploy_hooks_dir == \ + os.path.join(self.config.renewal_hooks_dir, + constants.RENEWAL_DEPLOY_HOOKS_DIR) + assert self.config.renewal_post_hooks_dir == \ + os.path.join(self.config.renewal_hooks_dir, + constants.RENEWAL_POST_HOOKS_DIR) + + def test_set_by_user_runtime_overrides(self): + assert not self.config.set_by_user('something') + self.config.something = 'a value' + assert self.config.set_by_user('something') + + def test_set_by_user_exception(self): + from certbot.configuration import NamespaceConfig + + # a newly created NamespaceConfig has no argument sources dict, so an + # exception is raised + config = NamespaceConfig(self.config.namespace) + with pytest.raises(RuntimeError): + config.set_by_user('whatever') + + # now set an argument sources dict + config.set_argument_sources({}) + assert not config.set_by_user('whatever') + + def test_set_by_user_mutables(self): + assert not self.config.set_by_user('domains') + self.config.domains.append('example.org') + assert self.config.set_by_user('domains') + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/conftest.py b/certbot/src/certbot/_internal/tests/conftest.py new file mode 100644 index 00000000000..6e929364d5b --- /dev/null +++ b/certbot/src/certbot/_internal/tests/conftest.py @@ -0,0 +1,16 @@ +from unittest import mock + +import pytest + +# This avoids a bug on mac where getfqdn errors after a long timeout. +# See https://bugs.python.org/issue35164 +# and discussion at https://github.com/certbot/certbot/pull/10408 +@pytest.fixture(autouse=True) +def mock_getfqdn(): + with mock.patch("socket.getfqdn", return_value="server_name") as mocked: + yield mocked + +@pytest.fixture(autouse=True) +def mock_sleep(): + with mock.patch("time.sleep") as mocked: + yield mocked diff --git a/certbot/src/certbot/_internal/tests/crypto_util_test.py b/certbot/src/certbot/_internal/tests/crypto_util_test.py new file mode 100644 index 00000000000..e767076c503 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/crypto_util_test.py @@ -0,0 +1,549 @@ +"""Tests for certbot.crypto_util.""" +import logging +import re +import sys +import unittest +from unittest import mock + +import pytest +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.hazmat.primitives.serialization import Encoding + +from acme import crypto_util as acme_crypto_util +from certbot import errors +from certbot import util +from certbot.compat import filesystem +from certbot.compat import os +import certbot.tests.util as test_util + +RSA512_KEY = test_util.load_vector('rsa512_key.pem') +RSA512_KEY_PATH = test_util.vector_path('rsa512_key.pem') +RSA2048_KEY = test_util.load_vector('rsa2048_key.pem') +RSA2048_KEY_PATH = test_util.vector_path('rsa2048_key.pem') +CERT_PATH = test_util.vector_path('cert_512.pem') +CERT = test_util.load_vector('cert_512.pem') +SS_CERT_PATH = test_util.vector_path('cert_2048.pem') +SS_CERT = test_util.load_vector('cert_2048.pem') +P256_KEY = test_util.load_vector('nistp256_key.pem') +P256_KEY_PATH = test_util.vector_path('nistp256_key.pem') +P256_CERT_PATH = test_util.vector_path('cert-nosans_nistp256.pem') +P256_CERT = test_util.load_vector('cert-nosans_nistp256.pem') +# CERT_LEAF is signed by CERT_ISSUER. CERT_ALT_ISSUER is a cross-sign of CERT_ISSUER. +CERT_LEAF = test_util.load_vector('cert_leaf.pem') +CERT_ISSUER = test_util.load_vector('cert_intermediate_1.pem') +CERT_ALT_ISSUER = test_util.load_vector('cert_intermediate_2.pem') + + +class GenerateKeyTest(test_util.TempDirTestCase): + """Tests for certbot.crypto_util.generate_key.""" + def setUp(self): + super().setUp() + + self.workdir = os.path.join(self.tempdir, 'workdir') + filesystem.mkdir(self.workdir, mode=0o700) + + logging.disable(logging.CRITICAL) + + def tearDown(self): + super().tearDown() + + logging.disable(logging.NOTSET) + + @classmethod + def _call(cls, key_size, key_dir): + from certbot.crypto_util import generate_key + return generate_key(key_size, key_dir, 'key-certbot.pem', strict_permissions=True) + + @mock.patch('certbot.crypto_util.make_key') + def test_success(self, mock_make): + mock_make.return_value = b'key_pem' + key = self._call(1024, self.workdir) + assert key.pem == b'key_pem' + assert 'key-certbot.pem' in key.file + assert os.path.exists(os.path.join(self.workdir, key.file)) + + @mock.patch('certbot.crypto_util.make_key') + def test_key_failure(self, mock_make): + mock_make.side_effect = ValueError + with pytest.raises(ValueError): + self._call(431, self.workdir) + + +class GenerateCSRTest(test_util.TempDirTestCase): + """Tests for certbot.crypto_util.generate_csr.""" + @mock.patch('acme.crypto_util.make_csr') + @mock.patch('certbot.crypto_util.util.make_or_verify_dir') + def test_it(self, unused_mock_verify, mock_csr): + from certbot.crypto_util import generate_csr + + mock_csr.return_value = b'csr_pem' + + csr = generate_csr( + mock.Mock(pem='dummy_key'), 'example.com', self.tempdir, strict_permissions=True) + + assert csr.data == b'csr_pem' + assert 'csr-certbot.pem' in csr.file + + +class ValidCSRTest(unittest.TestCase): + """Tests for certbot.crypto_util.valid_csr.""" + + @classmethod + def _call(cls, csr): + from certbot.crypto_util import valid_csr + return valid_csr(csr) + + def test_valid_pem_true(self): + assert self._call(test_util.load_vector('csr_512.pem')) + + def test_valid_pem_san_true(self): + assert self._call(test_util.load_vector('csr-san_512.pem')) + + def test_valid_der_false(self): + assert not self._call(test_util.load_vector('csr_512.der')) + + def test_empty_false(self): + assert not self._call('') + + def test_random_false(self): + assert not self._call('foo bar') + + +class CSRMatchesPubkeyTest(unittest.TestCase): + """Tests for certbot.crypto_util.csr_matches_pubkey.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.crypto_util import csr_matches_pubkey + return csr_matches_pubkey(*args, **kwargs) + + def test_valid_true(self): + assert self._call( + test_util.load_vector('csr_512.pem'), RSA512_KEY) + + def test_invalid_false(self): + assert not self._call( + test_util.load_vector('csr_512.pem'), P256_KEY) + + +class ReadCSRFileTest(unittest.TestCase): + """Tests for certbot.certbot_util.read_csr_file.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.crypto_util import read_csr_file + return read_csr_file(*args, **kwargs) + + def test_der_csr(self): + csrfile = test_util.vector_path('csr_512.der') + data = test_util.load_vector('csr_512.der') + data_pem = test_util.load_vector('csr_512.pem') + + assert util.CSR(file=csrfile, + data=data_pem, + form="pem") == \ + self._call(csrfile, data) + + def test_pem_csr(self): + csrfile = test_util.vector_path('csr_512.pem') + data = test_util.load_vector('csr_512.pem') + + assert util.CSR(file=csrfile, + data=data, + form="pem") == \ + self._call(csrfile, data) + + def test_bad_csr(self): + with pytest.raises(errors.Error): + self._call(test_util.vector_path('cert_512.pem'), + test_util.load_vector('cert_512.pem')) + + +class ImportCSRFileTest(unittest.TestCase): + """Tests for certbot.certbot_util.import_csr_file.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.crypto_util import import_csr_file + with pytest.warns(DeprecationWarning, match='import_csr_file is deprecated'): + return import_csr_file(*args, **kwargs) + + def test_der_csr(self): + csrfile = test_util.vector_path('csr_512.der') + data = test_util.load_vector('csr_512.der') + data_pem = test_util.load_vector('csr_512.pem') + + with pytest.warns(DeprecationWarning, match='Format is deprecated'): + assert (acme_crypto_util.Format.PEM, + util.CSR(file=csrfile, + data=data_pem, + form="pem"), + ["Example.com"]) == \ + self._call(csrfile, data) + + def test_pem_csr(self): + csrfile = test_util.vector_path('csr_512.pem') + data = test_util.load_vector('csr_512.pem') + + with pytest.warns(DeprecationWarning, match='Format is deprecated'): + assert (acme_crypto_util.Format.PEM, + util.CSR(file=csrfile, + data=data, + form="pem"), + ["Example.com"],) == \ + self._call(csrfile, data) + + def test_bad_csr(self): + with pytest.raises(errors.Error): + self._call(test_util.vector_path('cert_512.pem'), + test_util.load_vector('cert_512.pem')) + + +class MakeKeyTest(unittest.TestCase): + """Tests for certbot.crypto_util.make_key.""" + + def test_rsa(self): # pylint: disable=no-self-use + # RSA Key Type Test + from certbot.crypto_util import make_key + + # Do not test larger keys as it takes too long. + serialization.load_pem_private_key(make_key(2048), password=None) + + def test_ec(self): # pylint: disable=no-self-use + # ECDSA Key Type Tests + from certbot.crypto_util import make_key + + for (name, bits) in [('secp256r1', 256), ('secp384r1', 384), ('secp521r1', 521)]: + pkey = serialization.load_pem_private_key( + make_key(elliptic_curve=name, key_type='ecdsa'), + password=None + ) + assert isinstance(pkey, ec.EllipticCurvePrivateKey) + assert pkey.curve.key_size == bits + + def test_bad_key_sizes(self): + from certbot.crypto_util import make_key + + # Try a bad key size for RSA and ECDSA + with pytest.raises(errors.Error, match='Unsupported RSA key length: 1024'): + make_key(bits=1024, key_type='rsa') + + def test_bad_elliptic_curve_name(self): + from certbot.crypto_util import make_key + with pytest.raises(errors.Error, match='Unsupported elliptic curve: nothere'): + make_key(elliptic_curve="nothere", key_type='ecdsa') + + def test_bad_key_type(self): + from certbot.crypto_util import make_key + + # Try a bad --key-type + with pytest.raises(errors.Error, + match=re.escape('Invalid key_type specified: unf. Use [rsa|ecdsa]')): + make_key(2048, key_type='unf') + + def test_for_pkcs8_format(self): + from certbot.crypto_util import make_key + + # PKCS#1 format will instead have text like "BEGIN RSA PRIVATE KEY" or "BEGIN EC PRIVATE + # KEY" + assert b"BEGIN PRIVATE KEY" in make_key(2048) + assert b"BEGIN PRIVATE KEY" in make_key(elliptic_curve='secp256r1', key_type='ecdsa') + + +class VerifyCertSetup(unittest.TestCase): + """Refactoring for verification tests.""" + + def setUp(self): + self.renewable_cert = mock.MagicMock() + self.renewable_cert.cert_path = SS_CERT_PATH + self.renewable_cert.chain_path = SS_CERT_PATH + self.renewable_cert.key_path = RSA2048_KEY_PATH + self.renewable_cert.fullchain_path = test_util.vector_path('cert_fullchain_2048.pem') + + self.bad_renewable_cert = mock.MagicMock() + self.bad_renewable_cert.chain_path = SS_CERT_PATH + self.bad_renewable_cert.cert_path = SS_CERT_PATH + self.bad_renewable_cert.fullchain_path = SS_CERT_PATH + + +class VerifyRenewableCertTest(VerifyCertSetup): + """Tests for certbot.crypto_util.verify_renewable_cert.""" + + def _call(self, renewable_cert): + from certbot.crypto_util import verify_renewable_cert + return verify_renewable_cert(renewable_cert) + + def test_verify_renewable_cert(self): + assert self._call(self.renewable_cert) is None + + @mock.patch('certbot.crypto_util.verify_renewable_cert_sig', side_effect=errors.Error("")) + def test_verify_renewable_cert_failure(self, unused_verify_renewable_cert_sign): + with pytest.raises(errors.Error): + self._call(self.bad_renewable_cert) + + +class VerifyRenewableCertSigTest(VerifyCertSetup): + """Tests for certbot.crypto_util.verify_renewable_cert.""" + + def _call(self, renewable_cert): + from certbot.crypto_util import verify_renewable_cert_sig + return verify_renewable_cert_sig(renewable_cert) + + def test_cert_sig_match(self): + assert self._call(self.renewable_cert) is None + + def test_cert_sig_match_ec(self): + renewable_cert = mock.MagicMock() + renewable_cert.cert_path = P256_CERT_PATH + renewable_cert.chain_path = P256_CERT_PATH + renewable_cert.key_path = P256_KEY + assert self._call(renewable_cert) is None + + def test_cert_sig_mismatch(self): + self.bad_renewable_cert.cert_path = test_util.vector_path('cert_512_bad.pem') + with pytest.raises(errors.Error): + self._call(self.bad_renewable_cert) + + +class VerifyFullchainTest(VerifyCertSetup): + """Tests for certbot.crypto_util.verify_fullchain.""" + + def _call(self, renewable_cert): + from certbot.crypto_util import verify_fullchain + return verify_fullchain(renewable_cert) + + def test_fullchain_matches(self): + assert self._call(self.renewable_cert) is None + + def test_fullchain_mismatch(self): + with pytest.raises(errors.Error): + self._call(self.bad_renewable_cert) + + def test_fullchain_ioerror(self): + self.bad_renewable_cert.chain = "dog" + with pytest.raises(errors.Error): + self._call(self.bad_renewable_cert) + + +class VerifyCertMatchesPrivKeyTest(VerifyCertSetup): + """Tests for certbot.crypto_util.verify_cert_matches_priv_key.""" + + def _call(self, renewable_cert): + from certbot.crypto_util import verify_cert_matches_priv_key + return verify_cert_matches_priv_key(renewable_cert.cert, renewable_cert.privkey) + + def test_cert_priv_key_match(self): + self.renewable_cert.cert = SS_CERT_PATH + self.renewable_cert.privkey = RSA2048_KEY_PATH + assert self._call(self.renewable_cert) is None + + def test_cert_priv_key_mismatch(self): + self.bad_renewable_cert.privkey = P256_KEY_PATH + self.bad_renewable_cert.cert = SS_CERT_PATH + + with pytest.raises(errors.Error): + self._call(self.bad_renewable_cert) + + +class ValidPrivkeyTest(unittest.TestCase): + """Tests for certbot.crypto_util.valid_privkey.""" + + @classmethod + def _call(cls, privkey): + from certbot.crypto_util import valid_privkey + return valid_privkey(privkey) + + def test_valid_true(self): + assert self._call(RSA512_KEY) + + def test_empty_false(self): + assert not self._call('') + + def test_random_false(self): + assert not self._call('foo bar') + + +class GetSANsFromCertTest(unittest.TestCase): + """Tests for certbot.crypto_util.get_sans_from_cert.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.crypto_util import get_sans_from_cert + with pytest.warns(DeprecationWarning, match='get_sans_from_cert is deprecated'): + return get_sans_from_cert(*args, **kwargs) + + def test_single(self): + assert [] == self._call(test_util.load_vector('cert_512.pem')) + + def test_san(self): + assert ['example.com', 'www.example.com'] == \ + self._call(test_util.load_vector('cert-san_512.pem')) + + +class GetNamesFromCertTest(unittest.TestCase): + """Tests for certbot.crypto_util.get_names_from_cert.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.crypto_util import get_names_from_cert + with pytest.warns(DeprecationWarning, match='get_names_from_cert is deprecated'): + return get_names_from_cert(*args, **kwargs) + + def test_single(self): + assert ['example.com'] == \ + self._call(test_util.load_vector('cert_512.pem')) + + def test_san(self): + assert ['example.com', 'www.example.com'] == \ + self._call(test_util.load_vector('cert-san_512.pem')) + + def test_common_name_sans_order(self): + # Tests that the common name comes first + # followed by the SANS in alphabetical order + assert ['example.com'] + ['{0}.example.com'.format(c) for c in 'abcd'] == \ + self._call(test_util.load_vector('cert-5sans_512.pem')) + + def test_parse_non_cert(self): + with pytest.raises(ValueError): + self._call(b"hello there") + + +class GetNamesFromReqTest(unittest.TestCase): + """Tests for certbot.crypto_util.get_names_from_req.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.crypto_util import get_names_from_req + with pytest.warns(DeprecationWarning, match='get_names_from_req is deprecated'): + return get_names_from_req(*args, **kwargs) + + def test_nonames(self): + assert [] == \ + self._call(test_util.load_vector('csr-nonames_512.pem')) + + def test_nosans(self): + assert ['example.com'] == \ + self._call(test_util.load_vector('csr-nosans_512.pem')) + + def test_sans(self): + assert ['example.com', 'example.org', 'example.net', 'example.info', + 'subdomain.example.com', 'other.subdomain.example.com'] == \ + self._call(test_util.load_vector('csr-6sans_512.pem')) + + def test_der(self): + with pytest.warns(DeprecationWarning, match='Format is deprecated'): + assert ['Example.com'] == \ + self._call(test_util.load_vector('csr_512.der'), typ=acme_crypto_util.Format.DER) + + +class NotBeforeTest(unittest.TestCase): + """Tests for certbot.crypto_util.notBefore""" + + def test_notBefore(self): + from certbot.crypto_util import notBefore + assert notBefore(CERT_PATH).isoformat() == \ + '2014-12-11T22:34:45+00:00' + + +class NotAfterTest(unittest.TestCase): + """Tests for certbot.crypto_util.notAfter""" + + def test_notAfter(self): + from certbot.crypto_util import notAfter + assert notAfter(CERT_PATH).isoformat() == \ + '2014-12-18T22:34:45+00:00' + + +class Sha256sumTest(unittest.TestCase): + """Tests for certbot.crypto_util.notAfter""" + def test_sha256sum(self): + from certbot.crypto_util import sha256sum + assert sha256sum(CERT_PATH) == \ + '914ffed8daf9e2c99d90ac95c77d54f32cbd556672facac380f0c063498df84e' + + +class CertAndChainFromFullchainTest(unittest.TestCase): + """Tests for certbot.crypto_util.cert_and_chain_from_fullchain""" + + def _parse_and_reencode_pem(self, cert_pem:str)->str: + cert = x509.load_pem_x509_certificate(cert_pem.encode()) + return cert.public_bytes(Encoding.PEM).decode() + + def test_cert_and_chain_from_fullchain(self): + cert_pem: str = CERT.decode() + chain_pem: str = cert_pem + SS_CERT.decode() + fullchain_pem: str = cert_pem + chain_pem + spacey_fullchain_pem: str = cert_pem + u'\n' + chain_pem + crlf_fullchain_pem: str = fullchain_pem.replace(u'\n', u'\r\n') + + # In the ACME v1 code path, the fullchain is constructed by loading cert+chain DERs + # and using OpenSSL to dump them, so here we confirm that cryptography is producing certs + # that will be parseable by cert_and_chain_from_fullchain. + acmev1_fullchain_pem = self._parse_and_reencode_pem(cert_pem) + \ + self._parse_and_reencode_pem(cert_pem) + self._parse_and_reencode_pem(SS_CERT.decode()) + + from certbot.crypto_util import cert_and_chain_from_fullchain + for fullchain in (fullchain_pem, spacey_fullchain_pem, crlf_fullchain_pem, + acmev1_fullchain_pem): + cert_out, chain_out = cert_and_chain_from_fullchain(fullchain) + assert cert_out == cert_pem + assert chain_out == chain_pem + + with pytest.raises(errors.Error): + cert_and_chain_from_fullchain(cert_pem) + + +class FindChainWithIssuerTest(unittest.TestCase): + """Tests for certbot.crypto_util.find_chain_with_issuer""" + + @classmethod + def _call(cls, fullchains, issuer_cn, **kwargs): + from certbot.crypto_util import find_chain_with_issuer + return find_chain_with_issuer(fullchains, issuer_cn, kwargs) + + def _all_fullchains(self): + return [CERT_LEAF.decode() + CERT_ISSUER.decode(), + CERT_LEAF.decode() + CERT_ALT_ISSUER.decode()] + + def test_positive_match(self): + """Correctly pick the chain based on the root's CN""" + fullchains = self._all_fullchains() + matched = self._call(fullchains, "Pebble Root CA 0cc6f0") + assert matched == fullchains[1] + + @mock.patch('certbot.crypto_util.logger.info') + def test_intermediate_match(self, mock_info): + """Don't pick a chain where only an intermediate matches""" + fullchains = self._all_fullchains() + # Make the second chain actually only contain "Pebble Root CA 0cc6f0" + # as an intermediate, not as the root. This wouldn't be a valid chain + # (the CERT_ISSUER cert didn't issue the CERT_ALT_ISSUER cert), but the + # function under test here doesn't care about that. + fullchains[1] = fullchains[1] + CERT_ISSUER.decode() + matched = self._call(fullchains, "Pebble Root CA 0cc6f0") + assert matched == fullchains[0] + mock_info.assert_not_called() + + @mock.patch('certbot.crypto_util.logger.info') + def test_no_match(self, mock_info): + fullchains = self._all_fullchains() + matched = self._call(fullchains, "non-existent issuer") + assert matched == fullchains[0] + mock_info.assert_not_called() + + @mock.patch('certbot.crypto_util.logger.warning') + def test_warning_on_no_match(self, mock_warning): + fullchains = self._all_fullchains() + matched = self._call(fullchains, "non-existent issuer", + warn_on_no_match=True) + assert matched == fullchains[0] + mock_warning.assert_called_once_with("Certbot has been configured to prefer " + "certificate chains with issuer '%s', but no chain from the CA matched " + "this issuer. Using the default certificate chain instead.", + "non-existent issuer") + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/tests/display/__init__.py b/certbot/src/certbot/_internal/tests/display/__init__.py similarity index 100% rename from certbot/tests/display/__init__.py rename to certbot/src/certbot/_internal/tests/display/__init__.py diff --git a/certbot/src/certbot/_internal/tests/display/completer_test.py b/certbot/src/certbot/_internal/tests/display/completer_test.py new file mode 100644 index 00000000000..0a5353f26a5 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/display/completer_test.py @@ -0,0 +1,109 @@ +"""Test certbot._internal.display.completer.""" +from importlib import reload as reload_module +import string +import sys +import unittest +from unittest import mock + +import pytest + +from certbot.compat import filesystem +from certbot.compat import os +import certbot.tests.util as test_util + +try: + import readline # pylint: disable=import-error +except ImportError: + import certbot._internal.display.dummy_readline as readline # type: ignore + + + +class CompleterTest(test_util.TempDirTestCase): + """Test certbot._internal.display.completer.Completer.""" + + def setUp(self): + super().setUp() + + # directories must end with os.sep for completer to + # search inside the directory for possible completions + if self.tempdir[-1] != os.sep: + self.tempdir += os.sep + + self.paths: list[str] = [] + # create some files and directories in temp_dir + for c in string.ascii_lowercase: + path = os.path.join(self.tempdir, c) + self.paths.append(path) + if ord(c) % 2: + filesystem.mkdir(path) + else: + with open(path, 'w'): + pass + + def test_complete(self): + from certbot._internal.display import completer + my_completer = completer.Completer() + num_paths = len(self.paths) + + for i in range(num_paths): + completion = my_completer.complete(self.tempdir, i) + assert completion in self.paths + self.paths.remove(completion) + + assert len(self.paths) == 0 + completion = my_completer.complete(self.tempdir, num_paths) + assert completion is None + + @unittest.skipIf('readline' not in sys.modules, + reason='Not relevant if readline is not available.') + def test_import_error(self): + original_readline = sys.modules['readline'] + sys.modules['readline'] = None + + self.test_context_manager_with_unmocked_readline() + + sys.modules['readline'] = original_readline + + def test_context_manager_with_unmocked_readline(self): + from certbot._internal.display import completer + reload_module(completer) + + original_completer = readline.get_completer() + original_delims = readline.get_completer_delims() + + with completer.Completer(): + pass + + assert readline.get_completer() == original_completer + assert readline.get_completer_delims() == original_delims + + @mock.patch('certbot._internal.display.completer.readline', autospec=True) + def test_context_manager_libedit(self, mock_readline): + mock_readline.__doc__ = 'libedit' + self._test_context_manager_with_mock_readline(mock_readline) + + @mock.patch('certbot._internal.display.completer.readline', autospec=True) + def test_context_manager_readline(self, mock_readline): + mock_readline.__doc__ = 'GNU readline' + self._test_context_manager_with_mock_readline(mock_readline) + + def _test_context_manager_with_mock_readline(self, mock_readline): + from certbot._internal.display import completer + + mock_readline.parse_and_bind.side_effect = enable_tab_completion + + with completer.Completer(): + pass + + assert mock_readline.parse_and_bind.called is True + + +def enable_tab_completion(unused_command): + """Enables readline tab completion using the system specific syntax.""" + libedit = readline.__doc__ is not None and 'libedit' in readline.__doc__ + command = 'bind ^I rl_complete' if libedit else 'tab: complete' + readline.parse_and_bind(command) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/display/internal_util_test.py b/certbot/src/certbot/_internal/tests/display/internal_util_test.py new file mode 100644 index 00000000000..688592a202b --- /dev/null +++ b/certbot/src/certbot/_internal/tests/display/internal_util_test.py @@ -0,0 +1,153 @@ +"""Test :mod:`certbot._internal.display.util`.""" +import io +import socket +import sys +import tempfile +import unittest +from unittest import mock + +import pytest + +from acme import messages as acme_messages +from certbot import errors + + +class WrapLinesTest(unittest.TestCase): + def test_wrap_lines(self): + from certbot._internal.display.util import wrap_lines + msg = ("This is just a weak test{0}" + "This function is only meant to be for easy viewing{0}" + "Test a really really really really really really really really " + "really really really really long line...".format('\n')) + text = wrap_lines(msg) + + assert text.count('\n') == 3 + + +class PlaceParensTest(unittest.TestCase): + @classmethod + def _call(cls, label): + from certbot._internal.display.util import parens_around_char + return parens_around_char(label) + + def test_single_letter(self): + assert "(a)" == self._call("a") + + def test_multiple(self): + assert "(L)abel" == self._call("Label") + assert "(y)es please" == self._call("yes please") + + +class InputWithTimeoutTest(unittest.TestCase): + """Tests for certbot._internal.display.util.input_with_timeout.""" + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.display.util import input_with_timeout + return input_with_timeout(*args, **kwargs) + + def test_eof(self): + with tempfile.TemporaryFile("r+") as f: + with mock.patch("certbot._internal.display.util.sys.stdin", new=f): + with pytest.raises(EOFError): + self._call() + + def test_input(self, prompt=None): + expected = "foo bar" + stdin = io.StringIO(expected + "\n") + with mock.patch("certbot.compat.misc.select.select") as mock_select: + mock_select.return_value = ([stdin], [], [],) + assert self._call(prompt) == expected + + @mock.patch("certbot._internal.display.util.sys.stdout") + def test_input_with_prompt(self, mock_stdout): + prompt = "test prompt: " + self.test_input(prompt) + mock_stdout.write.assert_called_once_with(prompt) + mock_stdout.flush.assert_called_once_with() + + def test_timeout(self): + stdin = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + stdin.bind(('', 0)) + stdin.listen(1) + with mock.patch("certbot._internal.display.util.sys.stdin", stdin): + with pytest.raises(errors.Error): + self._call(timeout=0.001) + stdin.close() + + +class SeparateListInputTest(unittest.TestCase): + """Test Module functions.""" + def setUp(self): + self.exp = ["a", "b", "c", "test"] + + @classmethod + def _call(cls, input_): + from certbot._internal.display.util import separate_list_input + return separate_list_input(input_) + + def test_commas(self): + assert self._call("a,b,c,test") == self.exp + + def test_spaces(self): + assert self._call("a b c test") == self.exp + + def test_both(self): + assert self._call("a, b, c, test") == self.exp + + def test_mess(self): + actual = [ + self._call(" a , b c \t test"), + self._call(",a, ,, , b c test "), + self._call(",,,,, , a b,,, , c,test"), + ] + + for act in actual: + assert act == self.exp + + +class SummarizeSANsTest(unittest.TestCase): + @classmethod + def _call(cls, domains): + from certbot._internal.display.util import summarize_sans + return summarize_sans(domains) + + def test_single_domain(self): + assert "example.com" == self._call(["example.com"]) + + def test_two_domains(self): + assert "example.com and example.org" == \ + self._call(["example.com", "example.org"]) + + def test_many_domains(self): + assert "example.com and 2 more" == \ + self._call(["example.com", "example.org", "a.example.com"]) + + def test_empty_domains(self): + assert "" == self._call([]) + + +class DescribeACMEErrorTest(unittest.TestCase): + @classmethod + def _call(cls, typ: str = "urn:ietf:params:acme:error:badCSR", + title: str = "Unacceptable CSR", + detail: str = "CSR contained unknown extensions"): + from certbot._internal.display.util import describe_acme_error + return describe_acme_error( + acme_messages.Error(typ=typ, title=title, detail=detail)) + + def test_title_and_detail(self): + assert "Unacceptable CSR :: CSR contained unknown extensions" == self._call() + + def test_detail(self): + assert "CSR contained unknown extensions" == self._call(title=None) + + def test_description(self): + assert acme_messages.ERROR_CODES["badCSR"] == self._call(title=None, detail=None) + + def test_unknown_type(self): + assert "urn:ietf:params:acme:error:unknownErrorType" == \ + self._call(typ="urn:ietf:params:acme:error:unknownErrorType", title=None, detail=None) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/display/obj_test.py b/certbot/src/certbot/_internal/tests/display/obj_test.py new file mode 100644 index 00000000000..fa157e6456e --- /dev/null +++ b/certbot/src/certbot/_internal/tests/display/obj_test.py @@ -0,0 +1,353 @@ +"""Test :mod:`certbot._internal.display.obj`.""" +import sys +import unittest +from unittest import mock + +import pytest + +from certbot import errors +from certbot._internal.display import obj as display_obj +from certbot.display import util as display_util + +CHOICES = [("First", "Description1"), ("Second", "Description2")] +TAGS = ["tag1", "tag2", "tag3"] + + +class FileOutputDisplayTest(unittest.TestCase): + """Test stdout display. + + Most of this class has to deal with visual output. In order to test how the + functions look to a user, uncomment the test_visual function. + + """ + def setUp(self): + super().setUp() + self.mock_stdout = mock.MagicMock() + self.displayer = display_obj.FileDisplay(self.mock_stdout, False) + + @mock.patch("certbot._internal.display.obj.logger") + def test_notification_no_pause(self, mock_logger): + self.displayer.notification("message", False) + string = self.mock_stdout.write.call_args[0][0] + + assert "message" in string + mock_logger.debug.assert_called_with("Notifying user: %s", "message") + + def test_notification_pause(self): + input_with_timeout = "certbot._internal.display.util.input_with_timeout" + with mock.patch(input_with_timeout, return_value="enter"): + self.displayer.notification("message", force_interactive=True) + + assert "message" in self.mock_stdout.write.call_args[0][0] + + def test_notification_noninteractive(self): + self._force_noninteractive(self.displayer.notification, "message") + string = self.mock_stdout.write.call_args[0][0] + assert "message" in string + + def test_notification_noninteractive2(self): + # The main purpose of this test is to make sure we only call + # logger.warning once which _force_noninteractive checks internally + self._force_noninteractive(self.displayer.notification, "message") + string = self.mock_stdout.write.call_args[0][0] + assert "message" in string + + assert self.displayer.skipped_interaction + + self._force_noninteractive(self.displayer.notification, "message2") + string = self.mock_stdout.write.call_args[0][0] + assert "message2" in string + + def test_notification_decoration(self): + from certbot.compat import os + self.displayer.notification("message", pause=False, decorate=False) + string = self.mock_stdout.write.call_args[0][0] + assert string == "message" + os.linesep + + self.displayer.notification("message2", pause=False) + string = self.mock_stdout.write.call_args[0][0] + assert "- - - " in string + assert "message2" + os.linesep in string + + @mock.patch("certbot._internal.display.obj." + "FileDisplay._get_valid_int_ans") + def test_menu(self, mock_ans): + mock_ans.return_value = (display_util.OK, 1) + ret = self.displayer.menu("message", CHOICES, force_interactive=True) + assert ret == (display_util.OK, 0) + + def test_menu_noninteractive(self): + default = 0 + result = self._force_noninteractive( + self.displayer.menu, "msg", CHOICES, default=default) + assert result == (display_util.OK, default) + + def test_input_cancel(self): + input_with_timeout = "certbot._internal.display.util.input_with_timeout" + with mock.patch(input_with_timeout, return_value="c"): + code, _ = self.displayer.input("message", force_interactive=True) + + assert code, display_util.CANCEL + + def test_input_normal(self): + input_with_timeout = "certbot._internal.display.util.input_with_timeout" + with mock.patch(input_with_timeout, return_value="domain.com"): + code, input_ = self.displayer.input("message", force_interactive=True) + + assert code == display_util.OK + assert input_ == "domain.com" + + def test_input_noninteractive(self): + default = "foo" + code, input_ = self._force_noninteractive( + self.displayer.input, "message", default=default) + + assert code == display_util.OK + assert input_ == default + + def test_input_assertion_fail(self): + # If the call to util.assert_valid_call is commented out, an + # error.Error is raised, otherwise, an AssertionError is raised. + with pytest.raises(Exception): + self._force_noninteractive(self.displayer.input, "message", cli_flag="--flag") + + def test_input_assertion_fail2(self): + with mock.patch("certbot.display.util.assert_valid_call"): + with pytest.raises(errors.Error): + self._force_noninteractive(self.displayer.input, "msg", cli_flag="--flag") + + def test_yesno(self): + input_with_timeout = "certbot._internal.display.util.input_with_timeout" + with mock.patch(input_with_timeout, return_value="Yes"): + assert self.displayer.yesno( + "message", force_interactive=True) + with mock.patch(input_with_timeout, return_value="y"): + assert self.displayer.yesno( + "message", force_interactive=True) + with mock.patch(input_with_timeout, side_effect=["maybe", "y"]): + assert self.displayer.yesno( + "message", force_interactive=True) + with mock.patch(input_with_timeout, return_value="No"): + assert not self.displayer.yesno( + "message", force_interactive=True) + with mock.patch(input_with_timeout, side_effect=["cancel", "n"]): + assert not self.displayer.yesno( + "message", force_interactive=True) + + with mock.patch(input_with_timeout, return_value="a"): + assert self.displayer.yesno( + "msg", yes_label="Agree", force_interactive=True) + + def test_yesno_noninteractive(self): + assert self._force_noninteractive( + self.displayer.yesno, "message", default=True) + + @mock.patch("certbot._internal.display.util.input_with_timeout") + def test_checklist_valid(self, mock_input): + mock_input.return_value = "2 1" + code, tag_list = self.displayer.checklist( + "msg", TAGS, force_interactive=True) + assert (code, set(tag_list)) == (display_util.OK, {"tag1", "tag2"}) + + @mock.patch("certbot._internal.display.util.input_with_timeout") + def test_checklist_empty(self, mock_input): + mock_input.return_value = "" + code, tag_list = self.displayer.checklist("msg", TAGS, force_interactive=True) + assert (code, set(tag_list)) == (display_util.OK, {"tag1", "tag2", "tag3"}) + + @mock.patch("certbot._internal.display.util.input_with_timeout") + def test_checklist_miss_valid(self, mock_input): + mock_input.side_effect = ["10", "tag1 please", "1"] + + ret = self.displayer.checklist("msg", TAGS, force_interactive=True) + assert ret == (display_util.OK, ["tag1"]) + + @mock.patch("certbot._internal.display.util.input_with_timeout") + def test_checklist_miss_quit(self, mock_input): + mock_input.side_effect = ["10", "c"] + + ret = self.displayer.checklist("msg", TAGS, force_interactive=True) + assert ret == (display_util.CANCEL, []) + + def test_checklist_noninteractive(self): + default = TAGS + code, input_ = self._force_noninteractive( + self.displayer.checklist, "msg", TAGS, default=default) + + assert code == display_util.OK + assert input_ == default + + def test_scrub_checklist_input_valid(self): + # pylint: disable=protected-access + indices = [ + ["1"], + ["1", "2", "1"], + ["2", "3"], + ] + exp = [ + {"tag1"}, + {"tag1", "tag2"}, + {"tag2", "tag3"}, + ] + for i, list_ in enumerate(indices): + set_tags = set( + self.displayer._scrub_checklist_input(list_, TAGS)) + assert set_tags == exp[i] + + def test_scrub_checklist_maintain_indices_order(self): + # pylint: disable=protected-access + source_tags = ["T1", "T2", "T3", "T4", "T5", "T6", "T7", "T8", "T9"] + indices = [ + ["4", "9"], + ["9", "4"], + ["4", "9", "4"], + ["9", "4", "9"], + ] + exp = [ + ["T4", "T9"], + ["T9", "T4"], + ["T4", "T9"], + ["T9", "T4"], + ] + for i, list_ in enumerate(indices): + tags = self.displayer._scrub_checklist_input(list_, source_tags) + assert tags == exp[i] + + @mock.patch("certbot._internal.display.util.input_with_timeout") + def test_directory_select(self, mock_input): + args = ["msg", "/var/www/html", "--flag", True] + user_input = "/var/www/html" + mock_input.return_value = user_input + + returned = self.displayer.directory_select(*args) + assert returned == (display_util.OK, user_input) + + def test_directory_select_noninteractive(self): + default = "/var/www/html" + code, input_ = self._force_noninteractive( + self.displayer.directory_select, "msg", default=default) + + assert code == display_util.OK + assert input_ == default + + def _force_noninteractive(self, func, *args, **kwargs): + skipped_interaction = self.displayer.skipped_interaction + + with mock.patch("certbot._internal.display.obj.sys.stdin") as mock_stdin: + mock_stdin.isatty.return_value = False + with mock.patch("certbot._internal.display.obj.logger") as mock_logger: + result = func(*args, **kwargs) + + if skipped_interaction: + assert mock_logger.warning.called is False + else: + assert mock_logger.warning.call_count == 1 + + return result + + def test_scrub_checklist_input_invalid(self): + # pylint: disable=protected-access + indices = [ + ["0"], + ["4"], + ["tag1"], + ["1", "tag1"], + ["2", "o"] + ] + for list_ in indices: + assert self.displayer._scrub_checklist_input(list_, TAGS) == [] + + def test_print_menu(self): + # pylint: disable=protected-access + # This is purely cosmetic... just make sure there aren't any exceptions + self.displayer._print_menu("msg", CHOICES) + self.displayer._print_menu("msg", TAGS) + + def test_get_valid_int_ans_valid(self): + # pylint: disable=protected-access + input_with_timeout = "certbot._internal.display.util.input_with_timeout" + with mock.patch(input_with_timeout, return_value="1"): + assert self.displayer._get_valid_int_ans(1) == (display_util.OK, 1) + ans = "2" + with mock.patch(input_with_timeout, return_value=ans): + assert self.displayer._get_valid_int_ans(3) == \ + (display_util.OK, int(ans)) + + def test_get_valid_int_ans_invalid(self): + # pylint: disable=protected-access + answers = [ + ["0", "c"], + ["4", "one", "C"], + ["c"], + ] + input_with_timeout = "certbot._internal.display.util.input_with_timeout" + for ans in answers: + with mock.patch(input_with_timeout, side_effect=ans): + assert self.displayer._get_valid_int_ans(3) == \ + (display_util.CANCEL, -1) + + +class NoninteractiveDisplayTest(unittest.TestCase): + """Test non-interactive display. These tests are pretty easy!""" + def setUp(self): + self.mock_stdout = mock.MagicMock() + self.displayer = display_obj.NoninteractiveDisplay(self.mock_stdout) + + @mock.patch("certbot._internal.display.obj.logger") + def test_notification_no_pause(self, mock_logger): + self.displayer.notification("message", 10) + string = self.mock_stdout.write.call_args[0][0] + + assert "message" in string + mock_logger.debug.assert_called_with("Notifying user: %s", "message") + + def test_notification_decoration(self): + from certbot.compat import os + self.displayer.notification("message", pause=False, decorate=False) + string = self.mock_stdout.write.call_args[0][0] + assert string == "message" + os.linesep + + self.displayer.notification("message2", pause=False) + string = self.mock_stdout.write.call_args[0][0] + assert "- - - " in string + assert "message2" + os.linesep in string + + def test_input(self): + d = "an incomputable value" + ret = self.displayer.input("message", default=d) + assert ret == (display_util.OK, d) + with pytest.raises(errors.MissingCommandlineFlag): + self.displayer.input("message") + + def test_menu(self): + ret = self.displayer.menu("message", CHOICES, default=1) + assert ret == (display_util.OK, 1) + with pytest.raises(errors.MissingCommandlineFlag): + self.displayer.menu("message", CHOICES) + + def test_yesno(self): + d = False + ret = self.displayer.yesno("message", default=d) + assert ret == d + with pytest.raises(errors.MissingCommandlineFlag): + self.displayer.yesno("message") + + def test_checklist(self): + d = [1, 3] + ret = self.displayer.checklist("message", TAGS, default=d) + assert ret == (display_util.OK, d) + with pytest.raises(errors.MissingCommandlineFlag): + self.displayer.checklist("message", TAGS) + + def test_directory_select(self): + default = "/var/www/html" + expected = (display_util.OK, default) + actual = self.displayer.directory_select("msg", default) + assert expected == actual + + with pytest.raises(errors.MissingCommandlineFlag): + self.displayer.directory_select("msg") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/display/ops_test.py b/certbot/src/certbot/_internal/tests/display/ops_test.py new file mode 100644 index 00000000000..b971f780c46 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/display/ops_test.py @@ -0,0 +1,509 @@ +# coding=utf-8 +"""Test certbot.display.ops.""" +import sys +import unittest +from unittest import mock + +import josepy as jose +import pytest + +from acme import messages +from certbot import errors +from certbot._internal import account +from certbot._internal.display import obj as display_obj +from certbot.compat import filesystem +from certbot.compat import os +from certbot.display import ops +from certbot.display import util as display_util +import certbot.tests.util as test_util + +KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) + + +class GetEmailTest(unittest.TestCase): + """Tests for certbot.display.ops.get_email.""" + + @classmethod + def _call(cls, **kwargs): + from certbot.display.ops import get_email + return get_email(**kwargs) + + @test_util.patch_display_util() + def test_cancel_none(self, mock_get_utility): + mock_input = mock_get_utility().input + mock_input.return_value = (display_util.CANCEL, "foo@bar.baz") + with pytest.raises(errors.Error): + self._call() + with pytest.raises(errors.Error): + self._call() + + @test_util.patch_display_util() + def test_ok_safe(self, mock_get_utility): + mock_input = mock_get_utility().input + mock_input.return_value = (display_util.OK, "foo@bar.baz") + with mock.patch("certbot.display.ops.util.safe_email") as mock_safe_email: + mock_safe_email.return_value = True + assert self._call() == "foo@bar.baz" + + @test_util.patch_display_util() + def test_ok_not_safe(self, mock_get_utility): + mock_input = mock_get_utility().input + mock_input.return_value = (display_util.OK, "foo@bar.baz") + with mock.patch("certbot.display.ops.util.safe_email") as mock_safe_email: + mock_safe_email.side_effect = [False, True] + assert self._call() == "foo@bar.baz" + + @test_util.patch_display_util() + def test_invalid_flag(self, mock_get_utility): + invalid_txt = "The server reported a problem" + mock_input = mock_get_utility().input + mock_input.return_value = (display_util.OK, "foo@bar.baz") + with mock.patch("certbot.display.ops.util.safe_email") as mock_safe_email: + mock_safe_email.return_value = True + self._call() + assert invalid_txt not in mock_input.call_args[0][0] + self._call(invalid=True) + assert invalid_txt in mock_input.call_args[0][0] + + @test_util.patch_display_util() + def test_optional_invalid_unsafe(self, mock_get_utility): + invalid_txt = "There is a problem" + mock_input = mock_get_utility().input + mock_input.return_value = (display_util.OK, "foo@bar.baz") + with mock.patch("certbot.display.ops.util.safe_email") as mock_safe_email: + mock_safe_email.side_effect = [False, True] + self._call(invalid=True) + assert invalid_txt in mock_input.call_args[0][0] + + +class ChooseAccountTest(test_util.TempDirTestCase): + """Tests for certbot.display.ops.choose_account.""" + def setUp(self): + super().setUp() + + display_obj.set_display(display_obj.FileDisplay(sys.stdout, False)) + + self.account_keys_dir = os.path.join(self.tempdir, "keys") + filesystem.makedirs(self.account_keys_dir, 0o700) + + self.config = mock.MagicMock( + accounts_dir=self.tempdir, + account_keys_dir=self.account_keys_dir, + server="certbot-demo.org") + self.key = KEY + + self.acc1 = account.Account(messages.RegistrationResource( + uri=None, body=messages.Registration.from_data( + email="email1@g.com")), self.key) + self.acc2 = account.Account(messages.RegistrationResource( + uri=None, body=messages.Registration.from_data( + email="email2@g.com", phone="phone")), self.key) + + @classmethod + def _call(cls, accounts): + return ops.choose_account(accounts) + + @test_util.patch_display_util() + def test_one(self, mock_util): + mock_util().menu.return_value = (display_util.OK, 0) + assert self._call([self.acc1]) == self.acc1 + + @test_util.patch_display_util() + def test_two(self, mock_util): + mock_util().menu.return_value = (display_util.OK, 1) + assert self._call([self.acc1, self.acc2]) == self.acc2 + + @test_util.patch_display_util() + def test_cancel(self, mock_util): + mock_util().menu.return_value = (display_util.CANCEL, 1) + assert self._call([self.acc1, self.acc2]) is None + + +class GenHttpsNamesTest(unittest.TestCase): + """Test _gen_https_names.""" + def setUp(self): + display_obj.set_display(display_obj.FileDisplay(sys.stdout, False)) + + @classmethod + def _call(cls, domains): + from certbot.display.ops import _gen_https_names + return _gen_https_names(domains) + + def test_zero(self): + assert self._call([]) == "" + + def test_one(self): + doms = [ + "example.com", + "asllkjsadfljasdf.c", + ] + for dom in doms: + assert self._call([dom]) == "https://%s" % dom + + def test_two(self): + domains_list = [ + ["foo.bar.org", "bar.org"], + ["paypal.google.facebook.live.com", "*.zombo.example.com"], + ] + for doms in domains_list: + assert self._call(doms) == \ + "https://{dom[0]} and https://{dom[1]}".format(dom=doms) + + def test_three(self): + doms = ["a.org", "b.org", "c.org"] + # We use an oxford comma + assert self._call(doms) == \ + "https://{dom[0]}, https://{dom[1]}, and https://{dom[2]}".format( + dom=doms) + + def test_four(self): + doms = ["a.org", "b.org", "c.org", "d.org"] + exp = ("https://{dom[0]}, https://{dom[1]}, https://{dom[2]}, " + "and https://{dom[3]}".format(dom=doms)) + + assert self._call(doms) == exp + + +class ChooseNamesTest(unittest.TestCase): + """Test choose names.""" + def setUp(self): + display_obj.set_display(display_obj.FileDisplay(sys.stdout, False)) + self.mock_install = mock.MagicMock() + + @classmethod + def _call(cls, installer, question=None): + from certbot.display.ops import choose_names + return choose_names(installer, question) + + @mock.patch("certbot.display.ops._choose_names_manually") + def test_no_installer(self, mock_manual): + self._call(None) + assert mock_manual.call_count == 1 + + @test_util.patch_display_util() + def test_no_installer_cancel(self, mock_util): + mock_util().input.return_value = (display_util.CANCEL, []) + assert self._call(None) == [] + + @test_util.patch_display_util() + def test_no_names_choose(self, mock_util): + self.mock_install().get_all_names.return_value = set() + domain = "example.com" + mock_util().input.return_value = (display_util.OK, domain) + + actual_doms = self._call(self.mock_install) + assert mock_util().input.call_count == 1 + assert actual_doms == [domain] + + def test_sort_names_trivial(self): + from certbot.display.ops import _sort_names + + #sort an empty list + assert _sort_names([]) == [] + + #sort simple domains + some_domains = ["ex.com", "zx.com", "ax.com"] + assert _sort_names(some_domains) == ["ax.com", "ex.com", "zx.com"] + + #Sort subdomains of a single domain + domain = ".ex.com" + unsorted_short = ["e", "a", "z", "y"] + unsorted_long = [us + domain for us in unsorted_short] + + sorted_short = sorted(unsorted_short) + sorted_long = [us + domain for us in sorted_short] + + assert _sort_names(unsorted_long) == sorted_long + + def test_sort_names_many(self): + from certbot.display.ops import _sort_names + + unsorted_domains = [".cx.com", ".bx.com", ".ax.com", ".dx.com"] + unsorted_short = ["www", "bnother.long.subdomain", "a", "a.long.subdomain", "z", "b"] + #Of course sorted doesn't work here ;-) + sorted_short = ["a", "b", "a.long.subdomain", "bnother.long.subdomain", "www", "z"] + + to_sort = [] + for short in unsorted_short: + for domain in unsorted_domains: + to_sort.append(short+domain) + sortd = [] + for domain in sorted(unsorted_domains): + for short in sorted_short: + sortd.append(short+domain) + assert _sort_names(to_sort) == sortd + + + @test_util.patch_display_util() + def test_filter_names_valid_return(self, mock_util): + self.mock_install.get_all_names.return_value = {"example.com"} + mock_util().checklist.return_value = (display_util.OK, ["example.com"]) + + names = self._call(self.mock_install) + assert names == ["example.com"] + assert mock_util().checklist.call_count == 1 + + @test_util.patch_display_util() + def test_filter_namees_override_question(self, mock_util): + self.mock_install.get_all_names.return_value = {"example.com"} + mock_util().checklist.return_value = (display_util.OK, ["example.com"]) + names = self._call(self.mock_install, "Custom") + assert names == ["example.com"] + assert mock_util().checklist.call_count == 1 + assert mock_util().checklist.call_args[0][0] == "Custom" + + @test_util.patch_display_util() + def test_filter_names_nothing_selected(self, mock_util): + self.mock_install.get_all_names.return_value = {"example.com"} + mock_util().checklist.return_value = (display_util.OK, []) + + assert self._call(self.mock_install) == [] + + @test_util.patch_display_util() + def test_filter_names_cancel(self, mock_util): + self.mock_install.get_all_names.return_value = {"example.com"} + mock_util().checklist.return_value = ( + display_util.CANCEL, ["example.com"]) + + assert self._call(self.mock_install) == [] + + def test_get_valid_domains(self): + from certbot.display.ops import get_valid_domains + all_valid = ["example.com", "second.example.com", + "also.example.com", "under_score.example.com", + "justtld", "*.wildcard.com"] + all_invalid = ["öóòps.net", "uniçodé.com"] + two_valid = ["example.com", "úniçøde.com", "also.example.com"] + assert get_valid_domains(all_valid) == all_valid + assert get_valid_domains(all_invalid) == [] + assert len(get_valid_domains(two_valid)) == 2 + + @test_util.patch_display_util() + def test_choose_manually(self, mock_util): + from certbot.display.ops import _choose_names_manually + utility_mock = mock_util() + # No retry + utility_mock.yesno.return_value = False + # IDN and no retry + utility_mock.input.return_value = (display_util.OK, + "uniçodé.com") + assert _choose_names_manually() == [] + # IDN exception with previous mocks + with mock.patch( + "certbot.display.ops.internal_display_util.separate_list_input" + ) as mock_sli: + unicode_error = UnicodeEncodeError('mock', u'', 0, 1, 'mock') + mock_sli.side_effect = unicode_error + assert _choose_names_manually() == [] + # Valid domains + utility_mock.input.return_value = (display_util.OK, + ("example.com," + "under_score.example.com," + "justtld," + "valid.example.com")) + assert _choose_names_manually() == \ + ["example.com", "under_score.example.com", + "justtld", "valid.example.com"] + + @test_util.patch_display_util() + def test_choose_manually_retry(self, mock_util): + from certbot.display.ops import _choose_names_manually + utility_mock = mock_util() + # Three iterations + utility_mock.input.return_value = (display_util.OK, + "uniçodé.com") + utility_mock.yesno.side_effect = [True, True, False] + _choose_names_manually() + assert utility_mock.yesno.call_count == 3 + + +class SuccessInstallationTest(unittest.TestCase): + """Test the success installation message.""" + @classmethod + def _call(cls, names): + from certbot.display.ops import success_installation + success_installation(names) + + @test_util.patch_display_util() + @mock.patch("certbot.display.util.notify") + def test_success_installation(self, mock_notify, mock_display): + mock_display().notification.return_value = None + names = ["example.com", "abc.com"] + + self._call(names) + + assert mock_notify.call_count == 1 + arg = mock_notify.call_args_list[0][0][0] + + for name in names: + assert name in arg + + +class SuccessRenewalTest(unittest.TestCase): + """Test the success renewal message.""" + @classmethod + def _call(cls, names): + from certbot.display.ops import success_renewal + success_renewal(names) + + @test_util.patch_display_util() + @mock.patch("certbot.display.util.notify") + def test_success_renewal(self, mock_notify, mock_display): + mock_display().notification.return_value = None + names = ["example.com", "abc.com"] + + self._call(names) + + assert mock_notify.call_count == 1 + + +class SuccessRevocationTest(unittest.TestCase): + """Test the success revocation message.""" + @classmethod + def _call(cls, path): + from certbot.display.ops import success_revocation + success_revocation(path) + + @test_util.patch_display_util() + @mock.patch("certbot.display.util.notify") + def test_success_revocation(self, mock_notify, unused_mock_display): + path = "/path/to/cert.pem" + self._call(path) + mock_notify.assert_called_once_with( + "Congratulations! You have successfully revoked the certificate " + "that was located at {0}.".format(path) + ) + + +class ValidatorTests(unittest.TestCase): + """Tests for `validated_input` and `validated_directory`.""" + + __ERROR = "Must be non-empty" + + valid_input = "asdf" + valid_directory = "/var/www/html" + + @staticmethod + def __validator(m): + if m == "": + raise errors.PluginError(ValidatorTests.__ERROR) + + @test_util.patch_display_util() + def test_input_blank_with_validator(self, mock_util): + mock_util().input.side_effect = [(display_util.OK, ""), + (display_util.OK, ""), + (display_util.OK, ""), + (display_util.OK, self.valid_input)] + + returned = ops.validated_input(self.__validator, "message", force_interactive=True) + assert ValidatorTests.__ERROR == mock_util().notification.call_args[0][0] + assert returned == (display_util.OK, self.valid_input) + + @test_util.patch_display_util() + def test_input_validation_with_default(self, mock_util): + mock_util().input.side_effect = [(display_util.OK, self.valid_input)] + + returned = ops.validated_input(self.__validator, "msg", default="other") + assert returned == (display_util.OK, self.valid_input) + + @test_util.patch_display_util() + def test_input_validation_with_bad_default(self, mock_util): + mock_util().input.side_effect = [(display_util.OK, self.valid_input)] + + with pytest.raises(AssertionError): + ops.validated_input(self.__validator, "msg", default="") + + @test_util.patch_display_util() + def test_input_cancel_with_validator(self, mock_util): + mock_util().input.side_effect = [(display_util.CANCEL, "")] + + code, unused_raw = ops.validated_input(self.__validator, "message", force_interactive=True) + assert code == display_util.CANCEL + + @test_util.patch_display_util() + def test_directory_select_validation(self, mock_util): + mock_util().directory_select.side_effect = [(display_util.OK, ""), + (display_util.OK, self.valid_directory)] + + returned = ops.validated_directory(self.__validator, "msg", force_interactive=True) + assert ValidatorTests.__ERROR == mock_util().notification.call_args[0][0] + assert returned == (display_util.OK, self.valid_directory) + + @test_util.patch_display_util() + def test_directory_select_validation_with_default(self, mock_util): + mock_util().directory_select.side_effect = [(display_util.OK, self.valid_directory)] + + returned = ops.validated_directory(self.__validator, "msg", default="other") + assert returned == (display_util.OK, self.valid_directory) + + @test_util.patch_display_util() + def test_directory_select_validation_with_bad_default(self, mock_util): + mock_util().directory_select.side_effect = [(display_util.OK, self.valid_directory)] + + with pytest.raises(AssertionError): + ops.validated_directory(self.__validator, "msg", default="") + + +class ChooseValuesTest(unittest.TestCase): + """Test choose_values.""" + @classmethod + def _call(cls, values, question): + from certbot.display.ops import choose_values + return choose_values(values, question) + + @test_util.patch_display_util() + def test_choose_names_success(self, mock_util): + items = ["first", "second", "third"] + mock_util().checklist.return_value = (display_util.OK, [items[2]]) + result = self._call(items, None) + assert result == [items[2]] + assert mock_util().checklist.called is True + assert mock_util().checklist.call_args[0][0] == "" + + @test_util.patch_display_util() + def test_choose_names_success_question(self, mock_util): + items = ["first", "second", "third"] + question = "Which one?" + mock_util().checklist.return_value = (display_util.OK, [items[1]]) + result = self._call(items, question) + assert result == [items[1]] + assert mock_util().checklist.called is True + assert mock_util().checklist.call_args[0][0] == question + + @test_util.patch_display_util() + def test_choose_names_user_cancel(self, mock_util): + items = ["first", "second", "third"] + question = "Want to cancel?" + mock_util().checklist.return_value = (display_util.CANCEL, []) + result = self._call(items, question) + assert result == [] + assert mock_util().checklist.called is True + assert mock_util().checklist.call_args[0][0] == question + + +@mock.patch('certbot.display.ops.logger') +@mock.patch('certbot.display.util.notify') +class ReportExecutedCommand(unittest.TestCase): + """Test report_executed_command""" + @classmethod + def _call(cls, cmd_name: str, rc: int, out: str, err: str): + from certbot.display.ops import report_executed_command + report_executed_command(cmd_name, rc, out, err) + + def test_mixed_success(self, mock_notify, mock_logger): + self._call("some-hook", 0, "Did a thing", "Some warning") + assert mock_logger.warning.call_count == 1 + assert mock_notify.call_count == 1 + + def test_mixed_error(self, mock_notify, mock_logger): + self._call("some-hook", -127, "Did a thing", "Some warning") + assert mock_logger.warning.call_count == 2 + assert mock_notify.call_count == 1 + + def test_empty_success(self, mock_notify, mock_logger): + self._call("some-hook", 0, "\n", " ") + assert mock_logger.warning.call_count == 0 + assert mock_notify.call_count == 0 + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/display/util_test.py b/certbot/src/certbot/_internal/tests/display/util_test.py new file mode 100644 index 00000000000..3e507cd4ca7 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/display/util_test.py @@ -0,0 +1,74 @@ +"""Test :mod:`certbot.display.util`.""" +import sys + +import pytest + +import certbot.tests.util as test_util + + +@test_util.patch_display_util() +def test_notify(mock_util): + from certbot.display.util import notify + notify("Hello World") + mock_util().notification.assert_called_with( + "Hello World", pause=False, decorate=False, wrap=False + ) + + +@test_util.patch_display_util() +def test_notification(mock_util): + from certbot.display.util import notification + notification("Hello World") + mock_util().notification.assert_called_with( + "Hello World", pause=True, decorate=True, wrap=True, force_interactive=False + ) + + +@test_util.patch_display_util() +def test_menu(mock_util): + from certbot.display.util import menu + menu("Hello World", ["one", "two"], default=0) + mock_util().menu.assert_called_with( + "Hello World", ["one", "two"], default=0, cli_flag=None, force_interactive=False + ) + + +@test_util.patch_display_util() +def test_input_text(mock_util): + from certbot.display.util import input_text + input_text("Hello World", default="something") + mock_util().input.assert_called_with( + "Hello World", default='something', cli_flag=None, force_interactive=False + ) + + +@test_util.patch_display_util() +def test_yesno(mock_util): + from certbot.display.util import yesno + yesno("Hello World", default=True) + mock_util().yesno.assert_called_with( + "Hello World", yes_label='Yes', no_label='No', default=True, cli_flag=None, + force_interactive=False + ) + + +@test_util.patch_display_util() +def test_checklist(mock_util): + from certbot.display.util import checklist + checklist("Hello World", ["one", "two"], default="one") + mock_util().checklist.assert_called_with( + "Hello World", ['one', 'two'], default='one', cli_flag=None, force_interactive=False + ) + + +@test_util.patch_display_util() +def test_directory_select(mock_util): + from certbot.display.util import directory_select + directory_select("Hello World", default="something") + mock_util().directory_select.assert_called_with( + "Hello World", default='something', cli_flag=None, force_interactive=False + ) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/eff_test.py b/certbot/src/certbot/_internal/tests/eff_test.py new file mode 100644 index 00000000000..4402be80327 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/eff_test.py @@ -0,0 +1,183 @@ +"""Tests for certbot._internal.eff.""" +import datetime +import sys +import unittest +from unittest import mock + +import josepy +import pytest +import requests + +from acme import messages +from certbot._internal import account +from certbot._internal import constants +import certbot.tests.util as test_util + +_KEY = josepy.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) + + +class SubscriptionTest(test_util.ConfigTestCase): + """Abstract class for subscription tests.""" + def setUp(self): + super().setUp() + self.account = account.Account( + regr=messages.RegistrationResource( + uri=None, body=messages.Registration(), + new_authzr_uri='hi'), + key=_KEY, + meta=account.Account.Meta( + creation_host='test.certbot.org', + creation_dt=datetime.datetime( + 2015, 7, 4, 14, 4, 10, tzinfo=datetime.timezone.utc))) + self.config.email = 'certbot@example.org' + self.config.eff_email = None + + +class PrepareSubscriptionTest(SubscriptionTest): + """Tests for certbot._internal.eff.prepare_subscription.""" + def _call(self): + from certbot._internal.eff import prepare_subscription + prepare_subscription(self.config, self.account) + + @test_util.patch_display_util() + @mock.patch("certbot._internal.eff.display_util.notify") + def test_failure(self, mock_notify, mock_get_utility): + self.config.email = None + self.config.eff_email = True + self._call() + actual = mock_notify.call_args[0][0] + expected_part = "because you didn't provide an e-mail address" + assert expected_part in actual + assert self.account.meta.register_to_eff is None + + @test_util.patch_display_util() + def test_will_not_subscribe_with_no_prompt(self, mock_get_utility): + self.config.eff_email = False + self._call() + self._assert_no_get_utility_calls(mock_get_utility) + assert self.account.meta.register_to_eff is None + + @test_util.patch_display_util() + def test_will_subscribe_with_no_prompt(self, mock_get_utility): + self.config.eff_email = True + self._call() + self._assert_no_get_utility_calls(mock_get_utility) + assert self.account.meta.register_to_eff == self.config.email + + @test_util.patch_display_util() + def test_will_not_subscribe_with_prompt(self, mock_get_utility): + mock_get_utility().yesno.return_value = False + self._call() + assert not mock_get_utility().add_message.called + self._assert_correct_yesno_call(mock_get_utility) + assert self.account.meta.register_to_eff is None + + @test_util.patch_display_util() + def test_will_subscribe_with_prompt(self, mock_get_utility): + mock_get_utility().yesno.return_value = True + self._call() + assert not mock_get_utility().add_message.called + self._assert_correct_yesno_call(mock_get_utility) + assert self.account.meta.register_to_eff == self.config.email + + def _assert_no_get_utility_calls(self, mock_get_utility): + assert not mock_get_utility().yesno.called + assert not mock_get_utility().add_message.called + + def _assert_correct_yesno_call(self, mock_get_utility): + assert mock_get_utility().yesno.called + call_args, call_kwargs = mock_get_utility().yesno.call_args + actual = call_args[0] + expected_part = 'Electronic Frontier Foundation' + assert expected_part in actual + assert not call_kwargs.get('default', True) + + +class HandleSubscriptionTest(SubscriptionTest): + """Tests for certbot._internal.eff.handle_subscription.""" + def _call(self): + from certbot._internal.eff import handle_subscription + handle_subscription(self.config, self.account) + + @mock.patch('certbot._internal.eff.subscribe') + def test_no_subscribe(self, mock_subscribe): + self._call() + assert mock_subscribe.called is False + + @mock.patch('certbot._internal.eff.subscribe') + def test_subscribe(self, mock_subscribe): + self.account.meta = self.account.meta.update(register_to_eff=self.config.email) + self._call() + assert mock_subscribe.called + assert mock_subscribe.call_args[0][0] == self.config.email + + +class SubscribeTest(unittest.TestCase): + """Tests for certbot._internal.eff.subscribe.""" + def setUp(self): + self.email = 'certbot@example.org' + self.json = {'status': True} + self.response = mock.Mock(ok=True) + self.response.json.return_value = self.json + patcher = mock.patch("certbot._internal.eff.display_util.notify") + self.mock_notify = patcher.start() + self.addCleanup(patcher.stop) + + @mock.patch('certbot._internal.eff.requests.post') + def _call(self, mock_post): + mock_post.return_value = self.response + + from certbot._internal.eff import subscribe + subscribe(self.email) + self._check_post_call(mock_post) + + def _check_post_call(self, mock_post): + assert mock_post.call_count == 1 + call_args, call_kwargs = mock_post.call_args + assert call_args[0] == constants.EFF_SUBSCRIBE_URI + + data = call_kwargs.get('data') + assert data is not None + assert data.get('email') == self.email + + def test_bad_status(self): + self.json['status'] = False + self._call() + actual = self._get_reported_message() + expected_part = 'because your e-mail address appears to be invalid.' + assert expected_part in actual + + def test_not_ok(self): + self.response.ok = False + self.response.raise_for_status.side_effect = requests.exceptions.HTTPError + self._call() + actual = self._get_reported_message() + unexpected_part = 'because' + assert unexpected_part not in actual + + def test_response_not_json(self): + self.response.json.side_effect = ValueError() + self._call() + actual = self._get_reported_message() + expected_part = 'problem' + assert expected_part in actual + + def test_response_json_missing_status_element(self): + self.json.clear() + self._call() + actual = self._get_reported_message() + expected_part = 'problem' + assert expected_part in actual + + def _get_reported_message(self): + assert self.mock_notify.called + return self.mock_notify.call_args[0][0] + + @test_util.patch_display_util() + def test_subscribe(self, mock_get_utility): + self._call() + assert mock_get_utility.called is False + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/error_handler_test.py b/certbot/src/certbot/_internal/tests/error_handler_test.py new file mode 100644 index 00000000000..be001cf87ec --- /dev/null +++ b/certbot/src/certbot/_internal/tests/error_handler_test.py @@ -0,0 +1,154 @@ +"""Tests for certbot._internal.error_handler.""" +import contextlib +import signal +import sys +from typing import Callable +from typing import Union +import unittest +from unittest import mock + +import pytest + +from certbot.compat import os + + +def get_signals(signums): + """Get the handlers for an iterable of signums.""" + return {s: signal.getsignal(s) for s in signums} + + +def set_signals(sig_handler_dict): + """Set the signal (keys) with the handler (values) from the input dict.""" + for s, h in sig_handler_dict.items(): + signal.signal(s, h) + + +@contextlib.contextmanager +def signal_receiver(signums): + """Context manager to catch signals""" + signals = [] + prev_handlers: dict[int, Union[int, None, Callable]] = get_signals(signums) + set_signals({s: lambda s, _: signals.append(s) for s in signums}) + yield signals + set_signals(prev_handlers) + + +def send_signal(signum): + """Send the given signal""" + os.kill(os.getpid(), signum) + + +class ErrorHandlerTest(unittest.TestCase): + """Tests for certbot._internal.error_handler.ErrorHandler.""" + + def setUp(self): + from certbot._internal import error_handler + + self.init_func = mock.MagicMock() + self.init_args = {42,} + self.init_kwargs = {'foo': 'bar'} + self.handler = error_handler.ErrorHandler(self.init_func, + *self.init_args, + **self.init_kwargs) + + # pylint: disable=protected-access + self.signals = error_handler._SIGNALS + + def test_context_manager(self): + exception_raised = False + try: + with self.handler: + raise ValueError + except ValueError: + exception_raised = True + + assert exception_raised + self.init_func.assert_called_once_with(*self.init_args, + **self.init_kwargs) + + def test_context_manager_with_signal(self): + if not self.signals: + self.skipTest(reason='Signals cannot be handled on Windows.') + init_signals = get_signals(self.signals) + with signal_receiver(self.signals) as signals_received: + with self.handler: + should_be_42 = 42 + send_signal(self.signals[0]) + should_be_42 *= 10 + + # check execution stopped when the signal was sent + assert 42 == should_be_42 + # assert signals were caught + assert [self.signals[0]] == signals_received + # assert the error handling function was just called once + self.init_func.assert_called_once_with(*self.init_args, + **self.init_kwargs) + for signum in self.signals: + assert init_signals[signum] == signal.getsignal(signum) + + def test_bad_recovery(self): + bad_func = mock.MagicMock(side_effect=[ValueError]) + self.handler.register(bad_func) + try: + with self.handler: + raise ValueError + except ValueError: + pass + self.init_func.assert_called_once_with(*self.init_args, + **self.init_kwargs) + bad_func.assert_called_once_with() + + def test_bad_recovery_with_signal(self): + if not self.signals: + self.skipTest(reason='Signals cannot be handled on Windows.') + sig1 = self.signals[0] + sig2 = self.signals[-1] + bad_func = mock.MagicMock(side_effect=lambda: send_signal(sig1)) + self.handler.register(bad_func) + with signal_receiver(self.signals) as signals_received: + with self.handler: + send_signal(sig2) + assert [sig2, sig1] == signals_received + self.init_func.assert_called_once_with(*self.init_args, + **self.init_kwargs) + bad_func.assert_called_once_with() + + def test_sysexit_ignored(self): + try: + with self.handler: + sys.exit(0) + except SystemExit: + pass + assert self.init_func.called is False + + def test_regular_exit(self): + func = mock.MagicMock() + self.handler.register(func) + with self.handler: + pass + self.init_func.assert_not_called() + func.assert_not_called() + + +class ExitHandlerTest(ErrorHandlerTest): + """Tests for certbot._internal.error_handler.ExitHandler.""" + + def setUp(self): + from certbot._internal import error_handler + super().setUp() + self.handler = error_handler.ExitHandler(self.init_func, + *self.init_args, + **self.init_kwargs) + + def test_regular_exit(self): + func = mock.MagicMock() + self.handler.register(func) + with self.handler: + pass + self.init_func.assert_called_once_with(*self.init_args, + **self.init_kwargs) + func.assert_called_once_with() + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/errors_test.py b/certbot/src/certbot/_internal/tests/errors_test.py new file mode 100644 index 00000000000..576443c81b1 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/errors_test.py @@ -0,0 +1,60 @@ +"""Tests for certbot.errors.""" +import sys +import unittest +from unittest import mock + +import pytest + +from acme import messages +from certbot import achallenges +from certbot.tests import acme_util + + +class FailedChallengesTest(unittest.TestCase): + """Tests for certbot.errors.FailedChallenges.""" + + def setUp(self): + from certbot.errors import FailedChallenges + self.error = FailedChallenges({achallenges.DNS( + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="example.com"), + challb=messages.ChallengeBody( + chall=acme_util.DNS01, uri=None, + error=messages.Error.with_code("tls", detail="detail")))}) + + def test_str(self): + assert str(self.error).startswith( + "Failed authorization procedure. example.com (dns-01): " + "urn:ietf:params:acme:error:tls") + + def test_unicode(self): + from certbot.errors import FailedChallenges + arabic_detail = u'\u0639\u062f\u0627\u0644\u0629' + arabic_error = FailedChallenges({achallenges.DNS( + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="example.com"), + challb=messages.ChallengeBody( + chall=acme_util.DNS01, uri=None, + error=messages.Error.with_code("tls", detail=arabic_detail)))}) + + assert str(arabic_error).startswith( + "Failed authorization procedure. example.com (dns-01): " + "urn:ietf:params:acme:error:tls") + + +class StandaloneBindErrorTest(unittest.TestCase): + """Tests for certbot.errors.StandaloneBindError.""" + + def setUp(self): + from certbot.errors import StandaloneBindError + self.error = StandaloneBindError(mock.sentinel.error, 1234) + + def test_instance_args(self): + assert mock.sentinel.error == self.error.socket_error + assert 1234 == self.error.port + + def test_str(self): + assert str(self.error).startswith( + "Problem binding to port 1234: ") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/helpful_test.py b/certbot/src/certbot/_internal/tests/helpful_test.py new file mode 100644 index 00000000000..2da2d6cd363 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/helpful_test.py @@ -0,0 +1,117 @@ +"""Tests for certbot.helpful_parser""" +import sys + +import pytest + +from certbot._internal.cli import HelpfulArgumentParser + + +class TestScanningFlags: + '''Test the prescan_for_flag method of HelpfulArgumentParser''' + def test_prescan_no_help_flag(self): + arg_parser = HelpfulArgumentParser(['run'], {}) + detected_flag = arg_parser.prescan_for_flag('--help', + ['all', 'certonly']) + assert detected_flag is False + detected_flag = arg_parser.prescan_for_flag('-h', + ['all, certonly']) + assert detected_flag is False + + def test_prescan_unvalid_topic(self): + arg_parser = HelpfulArgumentParser(['--help', 'all'], {}) + detected_flag = arg_parser.prescan_for_flag('--help', + ['potato']) + assert detected_flag is True + detected_flag = arg_parser.prescan_for_flag('-h', + arg_parser.help_topics) + assert detected_flag is False + + def test_prescan_valid_topic(self): + arg_parser = HelpfulArgumentParser(['-h', 'all'], {}) + detected_flag = arg_parser.prescan_for_flag('-h', + arg_parser.help_topics) + assert detected_flag == 'all' + detected_flag = arg_parser.prescan_for_flag('--help', + arg_parser.help_topics) + assert detected_flag is False + +class TestDetermineVerbs: + '''Tests for determine_verb methods of HelpfulArgumentParser''' + def test_determine_verb_wrong_verb(self): + arg_parser = HelpfulArgumentParser(['potato'], {}) + assert arg_parser.verb == "run" + assert arg_parser.args == ["potato"] + + def test_determine_verb_help(self): + arg_parser = HelpfulArgumentParser(['--help', 'everything'], {}) + assert arg_parser.verb == "help" + assert arg_parser.args == ["--help", "everything"] + arg_parser = HelpfulArgumentParser(['-d', 'some_domain', '--help', + 'all'], {}) + assert arg_parser.verb == "help" + assert arg_parser.args == ['-d', 'some_domain', '--help', + 'all'] + + def test_determine_verb(self): + arg_parser = HelpfulArgumentParser(['certonly'], {}) + assert arg_parser.verb == 'certonly' + assert arg_parser.args == [] + + arg_parser = HelpfulArgumentParser(['auth'], {}) + assert arg_parser.verb == 'certonly' + assert arg_parser.args == [] + + arg_parser = HelpfulArgumentParser(['everything'], {}) + assert arg_parser.verb == 'run' + assert arg_parser.args == [] + + +class TestAdd: + '''Tests for add method in HelpfulArgumentParser''' + def test_add_trivial_argument(self): + arg_parser = HelpfulArgumentParser(['run'], {}) + arg_parser.add(None, "--hello-world") + parsed_args = arg_parser.parser.parse_args(['--hello-world', + 'Hello World!']) + assert parsed_args.hello_world == 'Hello World!' + assert not hasattr(parsed_args, 'potato') + + def test_add_expected_argument(self): + arg_parser = HelpfulArgumentParser(['--help', 'run'], {}) + arg_parser.add( + [None, "run", "certonly", "register"], + "--eab-kid", dest="eab_kid", action="store", + metavar="EAB_KID", + help="Key Identifier for External Account Binding") + parsed_args = arg_parser.parser.parse_args(["--eab-kid", None]) + assert parsed_args.eab_kid is None + assert hasattr(parsed_args, 'eab_kid') + + +class TestAddGroup: + '''Test add_group method of HelpfulArgumentParser''' + def test_add_group_no_input(self): + arg_parser = HelpfulArgumentParser(['run'], {}) + with pytest.raises(TypeError): + arg_parser.add_group() + + def test_add_group_topic_not_visible(self): + # The user request help on run. A topic that given somewhere in the + # args won't be added to the groups in the parser. + arg_parser = HelpfulArgumentParser(['--help', 'run'], {}) + arg_parser.add_group("auth", + description="description of auth") + assert arg_parser.groups == {} + + def test_add_group_topic_requested_help(self): + arg_parser = HelpfulArgumentParser(['--help', 'run'], {}) + arg_parser.add_group("run", + description="description of run") + assert arg_parser.groups["run"] + arg_parser.add_group("certonly", description="description of certonly") + with pytest.raises(KeyError): + assert arg_parser.groups["certonly"] is False + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/hook_test.py b/certbot/src/certbot/_internal/tests/hook_test.py new file mode 100644 index 00000000000..c09461b57da --- /dev/null +++ b/certbot/src/certbot/_internal/tests/hook_test.py @@ -0,0 +1,468 @@ +"""Tests for certbot._internal.hooks.""" +import sys +import unittest +from platform import python_version_tuple +from unittest import mock + +import pytest + +from certbot import errors +from certbot import util +from certbot.compat import filesystem +from certbot.compat import os +from certbot.tests import util as test_util + + +def pyver_lt(major: int, minor: int): + pymajor = int(python_version_tuple()[0]) + pyminor = int(python_version_tuple()[1]) + if pymajor < major: + return True + elif pymajor > major: + return False + else: + return pyminor < minor + + +class ValidateHooksTest(unittest.TestCase): + """Tests for certbot._internal.hooks.validate_hooks.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.hooks import validate_hooks + return validate_hooks(*args, **kwargs) + + @mock.patch("certbot._internal.hooks.validate_hook") + def test_it(self, mock_validate_hook): + config = mock.MagicMock() + self._call(config) + + types = [call[0][1] for call in mock_validate_hook.call_args_list] + assert {"pre", "post", "deploy",} == set(types) + + +class ValidateHookTest(test_util.TempDirTestCase): + """Tests for certbot._internal.hooks.validate_hook.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.hooks import validate_hook + return validate_hook(*args, **kwargs) + + def test_hook_not_executable(self): + # prevent unnecessary modifications to PATH + with mock.patch("certbot._internal.hooks.plug_util.path_surgery"): + # We just mock out filesystem.is_executable since on Windows, it is difficult + # to get a fully working test around executable permissions. See + # certbot.tests.compat.filesystem::NotExecutableTest for more in-depth tests. + with mock.patch("certbot._internal.hooks.filesystem.is_executable", return_value=False): + with pytest.raises(errors.HookCommandNotFound): + self._call('dummy', "foo") + + @mock.patch("certbot._internal.hooks.util.exe_exists") + def test_not_found(self, mock_exe_exists): + mock_exe_exists.return_value = False + with mock.patch("certbot._internal.hooks.plug_util.path_surgery") as mock_ps: + with pytest.raises(errors.HookCommandNotFound): + self._call("foo", "bar") + assert mock_ps.called + + @mock.patch("certbot._internal.hooks._prog") + def test_unset(self, mock_prog): + self._call(None, "foo") + assert mock_prog.called is False + + +class HookTest(test_util.ConfigTestCase): + """Common base class for hook tests.""" + + @classmethod + def _call(cls, *args, **kwargs): # pragma: no cover + """Calls the method being tested with the given arguments.""" + raise NotImplementedError + + @classmethod + def _call_with_mock_execute(cls, *args, **kwargs): + """Calls self._call after mocking out certbot.compat.misc.execute_command_status. + + The mock execute object is returned rather than the return value + of self._call. + + """ + with mock.patch("certbot.compat.misc.execute_command_status") as mock_execute: + mock_execute.return_value = (0, "", "") + cls._call(*args, **kwargs) + return mock_execute + + +class PreHookTest(HookTest): + """Tests for certbot._internal.hooks.pre_hook.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.hooks import pre_hook + return pre_hook(*args, **kwargs) + + def setUp(self): + super().setUp() + self.config.pre_hook = "foo" + + filesystem.makedirs(self.config.renewal_pre_hooks_dir) + self.dir_hook = os.path.join(self.config.renewal_pre_hooks_dir, "bar") + create_hook(self.dir_hook) + + # Reset this value as it may have been modified by past tests + self._reset_pre_hook_already() + + def tearDown(self): + # Reset this value so it's unmodified for future tests + self._reset_pre_hook_already() + super().tearDown() + + def _reset_pre_hook_already(self): + from certbot._internal.hooks import executed_pre_hooks + executed_pre_hooks.clear() + + def test_certonly(self): + self.config.verb = "certonly" + self._test_nonrenew_common() + + def test_run(self): + self.config.verb = "run" + self._test_nonrenew_common() + + def _test_nonrenew_common(self): + mock_execute = self._call_with_mock_execute(self.config) + mock_execute.assert_any_call("pre-hook", self.dir_hook, env=mock.ANY) + mock_execute.assert_called_with("pre-hook", self.config.pre_hook, env=mock.ANY) + self._test_no_executions_common() + + def test_no_hooks(self): + self.config.pre_hook = None + self.config.verb = "renew" + os.remove(self.dir_hook) + + with mock.patch("certbot._internal.hooks.logger") as mock_logger: + mock_execute = self._call_with_mock_execute(self.config) + assert mock_execute.called is False + assert mock_logger.info.called is False + + def test_renew_disabled_dir_hooks(self): + self.config.directory_hooks = False + mock_execute = self._call_with_mock_execute(self.config) + mock_execute.assert_called_once_with("pre-hook", self.config.pre_hook, env=mock.ANY) + self._test_no_executions_common() + + def test_renew_no_overlap(self): + self.config.verb = "renew" + mock_execute = self._call_with_mock_execute(self.config) + mock_execute.assert_any_call("pre-hook", self.dir_hook, env=mock.ANY) + mock_execute.assert_called_with("pre-hook", self.config.pre_hook, env=mock.ANY) + self._test_no_executions_common() + + def test_renew_with_overlap(self): + self.config.pre_hook = self.dir_hook + self.config.verb = "renew" + mock_execute = self._call_with_mock_execute(self.config) + mock_execute.assert_called_once_with("pre-hook", self.dir_hook, env=mock.ANY) + self._test_no_executions_common() + + def _test_no_executions_common(self): + with mock.patch("certbot._internal.hooks.logger") as mock_logger: + mock_execute = self._call_with_mock_execute(self.config) + assert mock_execute.called is False + assert mock_logger.info.called + + +class PostHookTest(HookTest): + """Tests for certbot._internal.hooks.post_hook.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.hooks import post_hook + return post_hook(*args, **kwargs) + + def setUp(self): + super().setUp() + + self.config.post_hook = "bar" + filesystem.makedirs(self.config.renewal_post_hooks_dir) + self.dir_hook = os.path.join(self.config.renewal_post_hooks_dir, "foo") + create_hook(self.dir_hook) + + # Reset this value as it may have been modified by past tests + self._reset_post_hook_eventually() + + def tearDown(self): + # Reset this value so it's unmodified for future tests + self._reset_post_hook_eventually() + super().tearDown() + + def _reset_post_hook_eventually(self): + from certbot._internal.hooks import post_hooks + del post_hooks[:] + + def test_certonly_and_run_with_hook(self): + for verb in ("certonly", "run",): + self.config.verb = verb + mock_execute = self._call_with_mock_execute(self.config, []) + mock_execute.assert_any_call("post-hook", self.dir_hook, env=mock.ANY) + mock_execute.assert_called_with("post-hook", self.config.post_hook, env=mock.ANY) + assert not self._get_eventually() + + def test_certonly_and_run_without_cli_hook(self): + self.config.post_hook = None + for verb in ("certonly", "run",): + self.config.verb = verb + mock_execute = self._call_with_mock_execute(self.config, []) + mock_execute.assert_called_once_with("post-hook", self.dir_hook, env=mock.ANY) + assert not self._get_eventually() + + def test_renew_env(self): + self.config.verb = "certonly" + args = self._call_with_mock_execute(self.config, ["success.org"]).call_args + assert args.kwargs['env']["RENEWED_DOMAINS"] == "success.org" + + def test_renew_disabled_dir_hooks(self): + self.config.directory_hooks = False + self._test_renew_common([self.config.post_hook]) + + def test_renew_no_config_hook(self): + self.config.post_hook = None + self._test_renew_common([self.dir_hook]) + + def test_renew_no_dir_hook(self): + os.remove(self.dir_hook) + self._test_renew_common([self.config.post_hook]) + + def test_renew_no_hooks(self): + self.config.post_hook = None + os.remove(self.dir_hook) + self._test_renew_common([]) + + def test_renew_no_overlap(self): + expected = [self.dir_hook, self.config.post_hook] + self._test_renew_common(expected) + + self.config.post_hook = "baz" + expected.append(self.config.post_hook) + self._test_renew_common(expected) + + def test_renew_with_overlap(self): + self.config.post_hook = self.dir_hook + self._test_renew_common([self.dir_hook]) + + def _test_renew_common(self, expected): + self.config.verb = "renew" + + for _ in range(2): + self._call(self.config, []) + assert self._get_eventually() == expected + + def _get_eventually(self): + from certbot._internal.hooks import post_hooks + return post_hooks + + +class RunSavedPostHooksTest(HookTest): + """Tests for certbot._internal.hooks.run_saved_post_hooks.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.hooks import run_saved_post_hooks + renewed_domains = kwargs["renewed_domains"] if "renewed_domains" in kwargs else args[0] + failed_domains = kwargs["failed_domains"] if "failed_domains" in kwargs else args[1] + + return run_saved_post_hooks(renewed_domains, failed_domains) + + def _call_with_mock_execute_and_eventually(self, *args, **kwargs): + """Call run_saved_post_hooks but mock out execute and eventually + + certbot._internal.hooks.post_hooks is replaced with + self.eventually. The mock execute object is returned rather than + the return value of run_saved_post_hooks. + + """ + eventually_path = "certbot._internal.hooks.post_hooks" + with mock.patch(eventually_path, new=self.eventually): + return self._call_with_mock_execute(*args, **kwargs) + + def setUp(self): + super().setUp() + self.eventually: list[str] = [] + + def test_empty(self): + assert not self._call_with_mock_execute_and_eventually([], []).called + + def test_multiple(self): + self.eventually = ["foo", "bar", "baz", "qux"] + mock_execute = self._call_with_mock_execute_and_eventually([], []) + + calls = mock_execute.call_args_list + for actual_call, expected_arg in zip(calls, self.eventually): + assert actual_call[0][1] == expected_arg + + def test_single(self): + self.eventually = ["foo"] + mock_execute = self._call_with_mock_execute_and_eventually([], []) + mock_execute.assert_called_once_with("post-hook", self.eventually[0], env=mock.ANY) + + def test_env(self): + self.eventually = ["foo"] + mock_execute = self._call_with_mock_execute_and_eventually(["success.org"], ["failed.org"]) + assert mock_execute.call_args.kwargs['env']["RENEWED_DOMAINS"] == "success.org" + assert mock_execute.call_args.kwargs['env']["FAILED_DOMAINS"] == "failed.org" + + +class RenewalHookTest(HookTest): + """Common base class for testing deploy/renew hooks.""" + # Needed for https://github.com/PyCQA/pylint/issues/179 + # pylint: disable=abstract-method + + def _call_with_mock_execute(self, *args, **kwargs): + """Calls self._call after mocking out certbot.compat.misc.execute_command_status. + + The mock execute object is returned rather than the return value + of self._call. The mock execute object asserts that environment + variables were properly set. + + """ + domains = kwargs["domains"] if "domains" in kwargs else args[1] + lineage = kwargs["lineage"] if "lineage" in kwargs else args[2] + + def execute_side_effect(*unused_args, **unused_kwargs): + """Assert environment variables are properly set. + + :returns: two strings imitating no output from the hook + :rtype: `tuple` of `str` + + """ + assert os.environ["RENEWED_DOMAINS"] == " ".join(domains) + assert os.environ["RENEWED_LINEAGE"] == lineage + return (0, "", "") + + with mock.patch("certbot.compat.misc.execute_command_status") as mock_execute: + mock_execute.side_effect = execute_side_effect + self._call(*args, **kwargs) + return mock_execute + + def setUp(self): + super().setUp() + self.vars_to_clear = { + var for var in ("RENEWED_DOMAINS", "RENEWED_LINEAGE",) + if var not in os.environ + } + + def tearDown(self): + for var in self.vars_to_clear: + os.environ.pop(var, None) + super().tearDown() + + +class DeployHookTest(RenewalHookTest): + """Tests for certbot._internal.hooks.deploy_hook.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.hooks import deploy_hook + return deploy_hook(*args, **kwargs) + + def setUp(self): + super().setUp() + self.config.deploy_hook = "foo" + + filesystem.makedirs(self.config.renewal_deploy_hooks_dir) + self.dir_hook = os.path.join(self.config.renewal_deploy_hooks_dir, + "bar") + create_hook(self.dir_hook) + + def test_no_hooks(self): + self.config.deploy_hook = None + os.remove(self.dir_hook) + + with mock.patch("certbot._internal.hooks.logger") as mock_logger: + mock_execute = self._call_with_mock_execute( + self.config, ["example.org"], "/foo/bar") + assert mock_execute.called is False + assert mock_logger.info.called is False + + def test_success(self): + domains = ["example.org", "example.net"] + lineage = "/foo/bar" + self.config.deploy_hook = "foo" + mock_execute = self._call_with_mock_execute( + self.config, domains, lineage) + assert mock_execute.call_count == 2 + + def test_disabled_dir_hooks(self): + self.config.directory_hooks = False + mock_execute = self._call_with_mock_execute( + self.config, ["example.org"], "/foo/bar") + mock_execute.assert_called_once_with("deploy-hook", self.config.deploy_hook, env=mock.ANY) + + @mock.patch("certbot._internal.hooks.logger") + def test_dry_run(self, mock_logger): + self.config.dry_run = True + mock_execute = self._call_with_mock_execute( + self.config, ["example.org"], "/foo/bar") + assert mock_execute.called is False + assert mock_logger.info.call_count == 2 + + def test_overlap(self): + self.config.deploy_hook = self.dir_hook + mock_execute = self._call_with_mock_execute( + self.config, ["example.net", "example.org"], "/foo/bar") + mock_execute.assert_called_once_with("deploy-hook", self.dir_hook, env=mock.ANY) + + def test_no_overlap(self): + mock_execute = self._call_with_mock_execute( + self.config, ["example.org"], "/foo/bar") + mock_execute.assert_any_call("deploy-hook", self.dir_hook, env=mock.ANY) + mock_execute.assert_called_with("deploy-hook", self.config.deploy_hook, env=mock.ANY) + + +class ListHooksTest(test_util.TempDirTestCase): + """Tests for certbot._internal.hooks.list_hooks.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.hooks import list_hooks + return list_hooks(*args, **kwargs) + + def test_empty(self): + assert not self._call(self.tempdir) + + def test_multiple(self): + names = sorted( + os.path.join(self.tempdir, basename) + for basename in ("foo", "bar", "baz", "qux") + ) + for name in names: + create_hook(name) + + assert self._call(self.tempdir) == names + + def test_single(self): + name = os.path.join(self.tempdir, "foo") + create_hook(name) + + assert self._call(self.tempdir) == [name] + + def test_ignore_tilde(self): + name = os.path.join(self.tempdir, "foo~") + create_hook(name) + + assert self._call(self.tempdir) == [] + + +def create_hook(file_path): + """Creates an executable file at the specified path. + + :param str file_path: path to create the file at + + """ + util.safe_open(file_path, mode="w", chmod=0o744).close() + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/lock_test.py b/certbot/src/certbot/_internal/tests/lock_test.py new file mode 100644 index 00000000000..9a56e31a138 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/lock_test.py @@ -0,0 +1,141 @@ +"""Tests for certbot._internal.lock.""" +import functools +import importlib +import multiprocessing +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot.compat import os +from certbot.tests import util as test_util + +if importlib.util.find_spec('fcntl'): + POSIX_MODE = True +else: + POSIX_MODE = False + + + +class LockDirTest(test_util.TempDirTestCase): + """Tests for certbot._internal.lock.lock_dir.""" + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.lock import lock_dir + return lock_dir(*args, **kwargs) + + def test_it(self): + assert_raises = functools.partial( + self.assertRaises, errors.LockError, self._call, self.tempdir) + lock_path = os.path.join(self.tempdir, '.certbot.lock') + test_util.lock_and_call(assert_raises, lock_path) + + +class LockFileTest(test_util.TempDirTestCase): + """Tests for certbot._internal.lock.LockFile.""" + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.lock import LockFile + return LockFile(*args, **kwargs) + + def setUp(self): + super().setUp() + self.lock_path = os.path.join(self.tempdir, 'test.lock') + + def test_acquire_without_deletion(self): + # acquire the lock in another process but don't delete the file + child = multiprocessing.Process(target=self._call, + args=(self.lock_path,)) + child.start() + child.join() + assert child.exitcode == 0 + assert os.path.exists(self.lock_path) + + # Test we're still able to properly acquire and release the lock + self.test_removed() + + def test_contention(self): + assert_raises = functools.partial( + self.assertRaises, errors.LockError, self._call, self.lock_path) + test_util.lock_and_call(assert_raises, self.lock_path) + + def test_locked_repr(self): + lock_file = self._call(self.lock_path) + try: + locked_repr = repr(lock_file) + self._test_repr_common(lock_file, locked_repr) + assert 'acquired' in locked_repr + finally: + lock_file.release() + + def test_released_repr(self): + lock_file = self._call(self.lock_path) + lock_file.release() + released_repr = repr(lock_file) + self._test_repr_common(lock_file, released_repr) + assert 'released' in released_repr + + def _test_repr_common(self, lock_file, lock_repr): + assert lock_file.__class__.__name__ in lock_repr + assert self.lock_path in lock_repr + + @test_util.skip_on_windows( + 'Race conditions on lock are specific to the non-blocking file access approach on Linux.') + def test_race(self): + should_delete = [True, False] + # Normally os module should not be imported in certbot codebase except in certbot.compat + # for the sake of compatibility over Windows and Linux. + # We make an exception here, since test_race is a test function called only on Linux. + from os import stat # pylint: disable=os-module-forbidden + + def delete_and_stat(path): + """Wrap os.stat and maybe delete the file first.""" + if path == self.lock_path and should_delete.pop(0): + os.remove(path) + return stat(path) + + with mock.patch('certbot._internal.lock.filesystem.os.stat') as mock_stat: + mock_stat.side_effect = delete_and_stat + self._call(self.lock_path) + assert len(should_delete) == 0 + + def test_removed(self): + lock_file = self._call(self.lock_path) + lock_file.release() + assert not os.path.exists(self.lock_path) + + def test_unexpected_lockf_or_locking_err(self): + if POSIX_MODE: + mocked_function = 'certbot._internal.lock.fcntl.lockf' + else: + mocked_function = 'certbot._internal.lock.msvcrt.locking' + msg = 'hi there' + with mock.patch(mocked_function) as mock_lock: + mock_lock.side_effect = OSError(msg) + try: + self._call(self.lock_path) + except OSError as err: + assert msg in str(err) + else: # pragma: no cover + self.fail('IOError not raised') + + def test_unexpected_os_err(self): + if POSIX_MODE: + mock_function = 'certbot._internal.lock.filesystem.os.stat' + else: + mock_function = 'certbot._internal.lock.msvcrt.locking' + # The only expected errno are ENOENT and EACCES in lock module. + msg = 'hi there' + with mock.patch(mock_function) as mock_os: + mock_os.side_effect = OSError(msg) + try: + self._call(self.lock_path) + except OSError as err: + assert msg in str(err) + else: # pragma: no cover + self.fail('OSError not raised') + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/log_test.py b/certbot/src/certbot/_internal/tests/log_test.py new file mode 100644 index 00000000000..338425505f8 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/log_test.py @@ -0,0 +1,438 @@ +"""Tests for certbot._internal.log.""" +import io +import logging +import logging.handlers +import sys +import time +from typing import Optional +import unittest +from unittest import mock + +import pytest + +from acme import messages +from certbot import errors +from certbot import util +from certbot._internal import constants +from certbot.compat import filesystem +from certbot.compat import os +from certbot.tests import util as test_util + + +class PreArgParseSetupTest(unittest.TestCase): + """Tests for certbot._internal.log.pre_arg_parse_setup.""" + + @classmethod + def _call(cls, *args, **kwargs): # pylint: disable=unused-argument + from certbot._internal.log import pre_arg_parse_setup + return pre_arg_parse_setup() + + def tearDown(self): + # We need to call logging.shutdown() at the end of this test to + # properly clean up any resources created by pre_arg_parse_setup. + logging.shutdown() + super().tearDown() + + @mock.patch('certbot._internal.log.sys') + @mock.patch('certbot._internal.log.pre_arg_parse_except_hook') + @mock.patch('certbot._internal.log.logging.getLogger') + @mock.patch('certbot._internal.log.util.atexit_register') + def test_it(self, mock_register, mock_get, mock_except_hook, mock_sys): + mock_sys.argv = ['--debug'] + mock_sys.version_info = sys.version_info + self._call() + + mock_root_logger = mock_get() + mock_root_logger.setLevel.assert_called_once_with(logging.DEBUG) + assert mock_root_logger.addHandler.call_count == 2 + + memory_handler: Optional[logging.handlers.MemoryHandler] = None + for call in mock_root_logger.addHandler.call_args_list: + handler = call[0][0] + if memory_handler is None and isinstance(handler, logging.handlers.MemoryHandler): + memory_handler = handler + target = memory_handler.target + else: + assert isinstance(handler, logging.StreamHandler) + assert isinstance(target, logging.StreamHandler) + + mock_register.assert_called_once_with(logging.shutdown) + mock_sys.excepthook(1, 2, 3) + mock_except_hook.assert_called_once_with( + memory_handler, 1, 2, 3, debug=True, quiet=False, log_path=mock.ANY) + + +class PostArgParseSetupTest(test_util.ConfigTestCase): + """Tests for certbot._internal.log.post_arg_parse_setup.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.log import post_arg_parse_setup + return post_arg_parse_setup(*args, **kwargs) + + def setUp(self): + super().setUp() + self.config.debug = False + self.config.max_log_backups = 1000 + self.config.quiet = False + self.config.verbose_count = constants.CLI_DEFAULTS['verbose_count'] + self.devnull = open(os.devnull, 'w') + + from certbot._internal.log import ColoredStreamHandler + self.stream_handler = ColoredStreamHandler(io.StringIO()) + from certbot._internal.log import MemoryHandler + from certbot._internal.log import TempHandler + self.temp_handler = TempHandler() + self.temp_path = self.temp_handler.path + self.memory_handler = MemoryHandler(self.temp_handler) + self.root_logger = mock.MagicMock( + handlers=[self.memory_handler, self.stream_handler]) + + def tearDown(self): + self.memory_handler.close() + self.stream_handler.close() + self.temp_handler.close() + self.devnull.close() + super().tearDown() + + def test_common(self): + with mock.patch('certbot._internal.log.logging.getLogger') as mock_get_logger: + mock_get_logger.return_value = self.root_logger + except_hook_path = 'certbot._internal.log.post_arg_parse_except_hook' + with mock.patch(except_hook_path) as mock_except_hook: + with mock.patch('certbot._internal.log.sys') as mock_sys: + mock_sys.version_info = sys.version_info + self._call(self.config) + + log_path = os.path.join(self.config.logs_dir, 'letsencrypt.log') + + self.root_logger.removeHandler.assert_called_once_with( + self.memory_handler) + assert self.root_logger.addHandler.called + assert os.path.exists(log_path) + assert not os.path.exists(self.temp_path) + mock_sys.excepthook(1, 2, 3) + mock_except_hook.assert_called_once_with( + 1, 2, 3, debug=self.config.debug, + quiet=self.config.quiet, log_path=log_path) + + level = self.stream_handler.level + if self.config.quiet: + assert level == constants.QUIET_LOGGING_LEVEL + else: + assert level == constants.DEFAULT_LOGGING_LEVEL + + def test_debug(self): + self.config.debug = True + self.test_common() + + def test_quiet(self): + self.config.quiet = True + self.test_common() + + +class SetupLogFileHandlerTest(test_util.ConfigTestCase): + """Tests for certbot._internal.log.setup_log_file_handler.""" + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.log import setup_log_file_handler + return setup_log_file_handler(*args, **kwargs) + + def setUp(self): + super().setUp() + self.config.max_log_backups = 42 + + @mock.patch('certbot._internal.main.logging.handlers.RotatingFileHandler') + def test_failure(self, mock_handler): + mock_handler.side_effect = IOError + + try: + self._call(self.config, 'test.log', '%(message)s') + except errors.Error as err: + assert '--logs-dir' in str(err) + else: # pragma: no cover + self.fail('Error not raised.') + + def test_success_with_rollover(self): + self._test_success_common(should_rollover=True) + + def test_success_without_rollover(self): + self.config.max_log_backups = 0 + self._test_success_common(should_rollover=False) + + def _test_success_common(self, should_rollover): + log_file = 'test.log' + handler, log_path = self._call(self.config, log_file, '%(message)s') + handler.close() + + assert handler.level == logging.DEBUG + assert handler.formatter.converter == time.localtime + + expected_path = os.path.join(self.config.logs_dir, log_file) + assert log_path == expected_path + + backup_path = os.path.join(self.config.logs_dir, log_file + '.1') + assert os.path.exists(backup_path) == should_rollover + + @mock.patch('certbot._internal.log.logging.handlers.RotatingFileHandler') + def test_max_log_backups_used(self, mock_handler): + self._call(self.config, 'test.log', '%(message)s') + backup_count = mock_handler.call_args[1]['backupCount'] + assert self.config.max_log_backups == backup_count + + +class ColoredStreamHandlerTest(unittest.TestCase): + """Tests for certbot._internal.log.ColoredStreamHandler""" + + def setUp(self): + self.stream = io.StringIO() + self.stream.isatty = lambda: True + self.logger = logging.getLogger() + self.logger.setLevel(logging.DEBUG) + + from certbot._internal.log import ColoredStreamHandler + self.handler = ColoredStreamHandler(self.stream) + self.logger.addHandler(self.handler) + + def tearDown(self): + self.handler.close() + + def test_format(self): + msg = 'I did a thing' + self.logger.debug(msg) + assert self.stream.getvalue() == '{0}\n'.format(msg) + + def test_format_and_red_level(self): + msg = 'I did another thing' + self.handler.red_level = logging.DEBUG + self.logger.debug(msg) + + assert self.stream.getvalue() == \ + '{0}{1}{2}\n'.format(util.ANSI_SGR_RED, + msg, + util.ANSI_SGR_RESET) + + +class MemoryHandlerTest(unittest.TestCase): + """Tests for certbot._internal.log.MemoryHandler""" + def setUp(self): + self.logger = logging.getLogger(__name__) + self.logger.setLevel(logging.DEBUG) + self.msg = 'hi there' + self.stream = io.StringIO() + + self.stream_handler = logging.StreamHandler(self.stream) + from certbot._internal.log import MemoryHandler + self.handler = MemoryHandler(self.stream_handler) + self.logger.addHandler(self.handler) + + def tearDown(self): + self.handler.close() + self.stream_handler.close() + + def test_flush(self): + self._test_log_debug() + self.handler.flush(force=True) + assert self.stream.getvalue() == self.msg + '\n' + + def test_not_flushed(self): + # By default, logging.ERROR messages and higher are flushed + self.logger.critical(self.msg) + self.handler.flush() + assert self.stream.getvalue() == '' + + def test_target_reset(self): + self._test_log_debug() + + new_stream = io.StringIO() + new_stream_handler = logging.StreamHandler(new_stream) + self.handler.setTarget(new_stream_handler) + self.handler.flush(force=True) + assert self.stream.getvalue() == '' + assert new_stream.getvalue() == self.msg + '\n' + new_stream_handler.close() + + def _test_log_debug(self): + self.logger.debug(self.msg) + + +class TempHandlerTest(unittest.TestCase): + """Tests for certbot._internal.log.TempHandler.""" + def setUp(self): + self.closed = False + from certbot._internal.log import TempHandler + self.handler = TempHandler() + + def tearDown(self): + self.handler.close() + + def test_permissions(self): + assert filesystem.check_permissions(self.handler.path, 0o600) + + def test_delete(self): + self.handler.close() + assert not os.path.exists(self.handler.path) + + def test_no_delete(self): + self.handler.emit(mock.MagicMock()) + self.handler.close() + assert os.path.exists(self.handler.path) + os.remove(self.handler.path) + + +class PreArgParseExceptHookTest(unittest.TestCase): + """Tests for certbot._internal.log.pre_arg_parse_except_hook.""" + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.log import pre_arg_parse_except_hook + return pre_arg_parse_except_hook(*args, **kwargs) + + @mock.patch('certbot._internal.log.post_arg_parse_except_hook') + def test_it(self, mock_post_arg_parse_except_hook): + memory_handler = mock.MagicMock() + args = ('some', 'args',) + kwargs = {'some': 'kwargs'} + + self._call(memory_handler, *args, **kwargs) + + mock_post_arg_parse_except_hook.assert_called_once_with( + *args, **kwargs) + memory_handler.flush.assert_called_once_with(force=True) + + +class PostArgParseExceptHookTest(unittest.TestCase): + """Tests for certbot._internal.log.post_arg_parse_except_hook.""" + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.log import post_arg_parse_except_hook + return post_arg_parse_except_hook(*args, **kwargs) + + def setUp(self): + self.error_msg = 'test error message' + self.log_path = 'foo.log' + + def test_base_exception(self): + exc_type = BaseException + mock_logger, output = self._test_common(exc_type, debug=False) + self._assert_exception_logged(mock_logger.error, exc_type) + self._assert_logfile_output(output) + + def test_debug(self): + exc_type = ValueError + mock_logger, output = self._test_common(exc_type, debug=True) + self._assert_exception_logged(mock_logger.error, exc_type) + self._assert_logfile_output(output) + + def test_quiet(self): + exc_type = ValueError + mock_logger, output = self._test_common(exc_type, debug=True, quiet=True) + self._assert_exception_logged(mock_logger.error, exc_type) + assert 'See the logfile' not in output + + def test_custom_error(self): + exc_type = errors.PluginError + mock_logger, output = self._test_common(exc_type, debug=False) + self._assert_exception_logged(mock_logger.debug, exc_type) + self._assert_quiet_output(mock_logger, output) + + def test_acme_error(self): + # Get an arbitrary error code + acme_code = next(iter(messages.ERROR_CODES)) + + def get_acme_error(msg): + """Wraps ACME errors so the constructor takes only a msg.""" + return messages.Error.with_code(acme_code, detail=msg) + + mock_logger, output = self._test_common(get_acme_error, debug=False) + self._assert_exception_logged(mock_logger.debug, messages.Error) + self._assert_quiet_output(mock_logger, output) + assert messages.ERROR_PREFIX not in output + + def test_other_error(self): + exc_type = ValueError + mock_logger, output = self._test_common(exc_type, debug=False) + self._assert_exception_logged(mock_logger.debug, exc_type) + self._assert_quiet_output(mock_logger, output) + + def test_keyboardinterrupt(self): + exc_type = KeyboardInterrupt + mock_logger, output = self._test_common(exc_type, debug=False) + mock_logger.error.assert_called_once_with('Exiting due to user request.') + + def _test_common(self, error_type, debug, quiet=False): + """Returns the mocked logger and stderr output.""" + mock_err = io.StringIO() + + def write_err(*args, **unused_kwargs): + """Write error to mock_err.""" + mock_err.write(args[0]) + + try: + raise error_type(self.error_msg) + except BaseException: + exc_info = sys.exc_info() + with mock.patch('certbot._internal.log.logger') as mock_logger: + mock_logger.error.side_effect = write_err + with mock.patch('certbot._internal.log.sys.stderr', mock_err): + try: + self._call( + *exc_info, debug=debug, quiet=quiet, log_path=self.log_path) + except SystemExit as exit_err: + mock_err.write(str(exit_err)) + else: # pragma: no cover + self.fail('SystemExit not raised.') + + output = mock_err.getvalue() + return mock_logger, output + + def _assert_exception_logged(self, log_func, exc_type): + assert log_func.called + call_kwargs = log_func.call_args[1] + assert 'exc_info' in call_kwargs + + actual_exc_info = call_kwargs['exc_info'] + expected_exc_info = (exc_type, mock.ANY, mock.ANY) + assert actual_exc_info == expected_exc_info + + def _assert_logfile_output(self, output): + assert 'See the logfile' in output + assert self.log_path in output + + def _assert_quiet_output(self, mock_logger, output): + assert mock_logger.exception.called is False + assert mock_logger.debug.called + assert self.error_msg in output + + +class ExitWithAdviceTest(test_util.TempDirTestCase): + """Tests for certbot._internal.log.exit_with_advice.""" + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.log import exit_with_advice + return exit_with_advice(*args, **kwargs) + + def test_log_file(self): + log_file = os.path.join(self.tempdir, 'test.log') + open(log_file, 'w').close() + + err_str = self._test_common(log_file) + assert 'logfiles' not in err_str + assert log_file in err_str + + def test_log_dir(self): + err_str = self._test_common(self.tempdir) + assert 'logfiles' in err_str + assert self.tempdir in err_str + + # pylint: disable=inconsistent-return-statements + def _test_common(self, *args, **kwargs): + try: + self._call(*args, **kwargs) + except SystemExit as err: + return str(err) + self.fail('SystemExit was not raised.') # pragma: no cover + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/main_test.py b/certbot/src/certbot/_internal/tests/main_test.py new file mode 100644 index 00000000000..2bb8e859bc8 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/main_test.py @@ -0,0 +1,2569 @@ +# coding=utf-8 +"""Tests for certbot._internal.main.""" +# pylint: disable=too-many-lines +import contextlib +import datetime +from importlib import reload as reload_module +import io +import itertools +import json +import shutil +import sys +import tempfile +import traceback +import unittest +from unittest import mock + +import configobj +from cryptography import x509 +import josepy as jose +import pytest + +from acme.messages import Error as acme_error +from certbot import errors +from certbot import interfaces +from certbot import util +from certbot._internal import account +from certbot._internal import cli +from certbot._internal import constants +from certbot._internal import main +from certbot._internal import updater +from certbot._internal import san +from certbot._internal.plugins import disco +from certbot._internal.plugins import manual +from certbot._internal.plugins import null +from certbot._internal.plugins import standalone +from certbot.compat import filesystem +from certbot.compat import os +from certbot.plugins import enhancements +import certbot.tests.util as test_util + +CERT_PATH = test_util.vector_path('cert_512.pem') +CERT = test_util.vector_path('cert_512.pem') +CSR = test_util.vector_path('csr_512.der') +JWK = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem')) +RSA2048_KEY_PATH = test_util.vector_path('rsa2048_key.pem') +P256_KEY_PATH = test_util.vector_path('p256_key.pem') +SS_CERT_PATH = test_util.vector_path('cert_2048.pem') + + +class TestHandleCerts(unittest.TestCase): + """Test for certbot._internal.main._handle_* methods""" + @mock.patch("certbot._internal.main._handle_unexpected_key_type_migration") + def test_handle_identical_cert_request_pending(self, mock_handle_migration): + mock_lineage = mock.Mock() + mock_lineage.ensure_deployed.return_value = False + # pylint: disable=protected-access + ret = main._handle_identical_cert_request(mock.Mock(), mock_lineage) + assert ret == ("reinstall", mock_lineage) + assert mock_handle_migration.called + + @mock.patch('certbot._internal.renewal.should_renew') + @mock.patch("certbot.display.util.menu") + @mock.patch("certbot._internal.main._handle_unexpected_key_type_migration") + def test_handle_identical_cert_key_type_change(self, mock_handle_migration, mock_menu, + mock_should_renew): + mock_handle_migration.return_value = True + mock_lineage = mock.Mock() + mock_lineage.ensure_deployed.return_value = True + mock_should_renew.return_value = False + ret = main._handle_identical_cert_request(mock.MagicMock(verb="run", reinstall=False), + mock_lineage) + assert mock_handle_migration.called + assert not mock_menu.called + assert ret == ("renew", mock_lineage) + + @mock.patch("certbot._internal.main._handle_unexpected_key_type_migration") + def test_handle_subset_cert_request(self, mock_handle_migration): + mock_config = mock.Mock() + mock_config.expand = True + mock_lineage = mock.Mock() + mock_lineage.sans.return_value = [san.DNSName("dummy1"), san.DNSName("dummy2")] + ret = main._handle_subset_cert_request(mock_config, ["dummy1"], mock_lineage) + assert ret == ("renew", mock_lineage) + assert mock_handle_migration.called + + @mock.patch("certbot._internal.main.display_util.yesno") + def test_handle_unexpected_key_type_migration(self, mock_yesno): + config = mock.Mock() + mock_set = mock.Mock() + config.set_by_user = mock_set + cert = mock.Mock() + + # If the key types do not differ, it should be a no-op. + config.key_type = "rsa" + cert.private_key_type = "rsa" + main._handle_unexpected_key_type_migration(config, cert) + mock_yesno.assert_not_called() + assert config.key_type == cert.private_key_type + + # If the user confirms the change interactively, the key change should proceed silently. + cert.private_key_type = "ecdsa" + mock_yesno.return_value = True + main._handle_unexpected_key_type_migration(config, cert) + assert mock_set.call_count == 2 + assert config.key_type == "rsa" + + # User does not interactively confirm the key type change. + mock_yesno.return_value = False + + # If --key-type and --cert-name are both set, the key type change should proceed silently. + mock_set.return_value = True + main._handle_unexpected_key_type_migration(config, cert) + assert config.key_type == "rsa" + + # If neither --key-type nor --cert-name are set, Certbot should keep the old key type. + mock_set.return_value = False + main._handle_unexpected_key_type_migration(config, cert) + assert config.key_type == "ecdsa" + + # If --key-type is set and --cert-name isn't, Certbot should error. + config.key_type = "rsa" + mock_set.side_effect = lambda var: var != "certname" + with pytest.raises(errors.Error, match="Please provide both --cert-name and --key-type"): + main._handle_unexpected_key_type_migration(config, cert) + + # If --key-type is not set, Certbot should keep the old key type. + mock_set.side_effect = lambda var: var != "key_type" + main._handle_unexpected_key_type_migration(config, cert) + assert config.key_type == "ecdsa" + + +class RunTest(test_util.ConfigTestCase): + """Tests for certbot._internal.main.run.""" + + def setUp(self): + super().setUp() + self.domain = 'example.org' + patches = [ + mock.patch('certbot._internal.main._get_and_save_cert'), + mock.patch('certbot._internal.main.display_ops.success_installation'), + mock.patch('certbot._internal.main.display_ops.success_renewal'), + mock.patch('certbot._internal.main._init_le_client'), + mock.patch('certbot._internal.main._suggest_donation_if_appropriate'), + mock.patch('certbot._internal.main._report_new_cert'), + mock.patch('certbot._internal.main._find_cert'), + mock.patch('certbot._internal.eff.handle_subscription'), + mock.patch('certbot._internal.main._report_next_steps') + ] + + self.mock_auth = patches[0].start() + self.mock_success_installation = patches[1].start() + self.mock_success_renewal = patches[2].start() + self.mock_init = patches[3].start() + self.mock_suggest_donation = patches[4].start() + self.mock_report_cert = patches[5].start() + self.mock_find_cert = patches[6].start() + self.mock_subscription = patches[7].start() + self.mock_report_next_steps = patches[8].start() + for patch in patches: + self.addCleanup(patch.stop) + + def _call(self): + args = '-a webroot -i null -d {0}'.format(self.domain).split() + plugins = disco.PluginsRegistry.find_all() + config = cli.prepare_and_parse_args(plugins, args) + + from certbot._internal.main import run + run(config, plugins) + + def test_newcert_success(self): + self.mock_auth.return_value = mock.Mock() + self.mock_find_cert.return_value = True, None + self._call() + self.mock_success_installation.assert_called_once_with([self.domain]) + self.mock_report_next_steps.assert_called_once_with(mock.ANY, None, mock.ANY, + new_or_renewed_cert=True) + + def test_reinstall_success(self): + self.mock_auth.return_value = mock.Mock() + self.mock_find_cert.return_value = False, mock.Mock() + self._call() + self.mock_success_installation.assert_called_once_with([self.domain]) + + def test_renewal_success(self): + self.mock_auth.return_value = mock.Mock() + self.mock_find_cert.return_value = True, mock.Mock() + self._call() + self.mock_success_renewal.assert_called_once_with([self.domain]) + + @mock.patch('certbot._internal.main.plug_sel.choose_configurator_plugins') + def test_run_enhancement_not_supported(self, mock_choose): + mock_choose.return_value = (null.Installer(self.config, "null"), None) + plugins = disco.PluginsRegistry.find_all() + self.config.auto_hsts = True + with pytest.raises(errors.NotSupportedError): + main.run(self.config, plugins) + + @mock.patch('certbot._internal.main._install_cert') + def test_cert_success_install_error(self, mock_install_cert): + mock_install_cert.side_effect = errors.PluginError("Fake installation error") + self.mock_auth.return_value = mock.Mock() + self.mock_find_cert.return_value = True, None + with pytest.raises(errors.PluginError): + self._call() + + # Next steps should contain both renewal advice and installation error + self.mock_report_next_steps.assert_called_once_with( + mock.ANY, mock_install_cert.side_effect, mock.ANY, new_or_renewed_cert=True) + # The final success message shouldn't be shown + self.mock_success_installation.assert_not_called() + + @mock.patch('certbot._internal.main.plug_sel.choose_configurator_plugins') + def test_run_must_staple_not_supported(self, mock_choose): + mock_choose.return_value = (null.Installer(self.config, "null"), None) + plugins = disco.PluginsRegistry.find_all() + self.config.must_staple = True + with pytest.raises(errors.NotSupportedError): + main.run(self.config, plugins) + + +class CertonlyTest(unittest.TestCase): + """Tests for certbot._internal.main.certonly.""" + + def setUp(self): + self.get_utility_patch = test_util.patch_display_util() + self.mock_get_utility = self.get_utility_patch.start() + + def tearDown(self): + self.get_utility_patch.stop() + + def _call(self, args): + plugins = disco.PluginsRegistry.find_all() + config = cli.prepare_and_parse_args(plugins, args) + + with mock.patch('certbot._internal.main._init_le_client') as mock_init: + with mock.patch('certbot._internal.main._suggest_donation_if_appropriate'): + with mock.patch('certbot._internal.eff.handle_subscription'): + main.certonly(config, plugins) + + return mock_init() # returns the client + + @mock.patch('certbot._internal.main._find_cert') + @mock.patch('certbot._internal.main._get_and_save_cert') + @mock.patch('certbot._internal.main._report_new_cert') + def test_no_reinstall_text_pause(self, unused_report, mock_auth, mock_find_cert): + mock_notification = self.mock_get_utility().notification + mock_notification.side_effect = self._assert_no_pause + mock_auth.return_value = mock.Mock() + mock_find_cert.return_value = False, None + self._call('certonly --webroot -d example.com'.split()) + + def _assert_no_pause(self, *args, **kwargs): # pylint: disable=unused-argument + assert kwargs.get("pause") is False + + @mock.patch('certbot._internal.main._report_next_steps') + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.cert_manager.sans_for_certname') + @mock.patch('certbot._internal.renewal.renew_cert') + @mock.patch('certbot._internal.main._handle_unexpected_key_type_migration') + @mock.patch('certbot._internal.main._report_new_cert') + def test_find_lineage_for_sans_and_certname(self, mock_report_cert, + mock_handle_type, mock_renew_cert, mock_sans_for_certname, mock_lineage, mock_report_next_steps): + domains = [san.DNSName('example.com'), san.DNSName('test.org')] + mock_sans_for_certname.return_value = domains + mock_lineage.sans.return_value = domains + self._call(('certonly --webroot -d example.com -d test.org ' + '--cert-name example.com --no-directory-hooks').split()) + + assert mock_lineage.call_count == 1 + assert mock_sans_for_certname.call_count == 1 + assert mock_renew_cert.call_count == 1 + assert mock_report_cert.call_count == 1 + assert mock_handle_type.call_count == 1 + mock_report_next_steps.assert_called_once_with( + mock.ANY, None, mock.ANY, new_or_renewed_cert=True) + + # user confirms updating lineage with new domains + self._call(('certonly --webroot -d example.com -d test.com ' + '--cert-name example.com --no-directory-hooks').split()) + assert mock_lineage.call_count == 2 + assert mock_sans_for_certname.call_count == 2 + assert mock_renew_cert.call_count == 2 + assert mock_report_cert.call_count == 2 + assert mock_handle_type.call_count == 2 + + # error in _ask_user_to_confirm_new_names + self.mock_get_utility().yesno.return_value = False + with pytest.raises(errors.ConfigurationError): + self._call('certonly --webroot -d example.com -d test.com --cert-name example.com' + ' --no-directory-hooks'.split()) + + @mock.patch('certbot._internal.main._report_next_steps') + @mock.patch('certbot._internal.cert_manager.sans_for_certname') + @mock.patch('certbot.display.ops.choose_names') + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.main._report_new_cert') + def test_find_lineage_for_sans_new_certname(self, mock_report_cert, + mock_lineage, mock_choose_names, mock_sans_for_certname, unused_mock_report_next_steps): + mock_lineage.return_value = None + + # no lineage with this name but we specified domains so create a new cert + self._call(('certonly --webroot -d example.com -d test.com ' + '--cert-name example.com --no-directory-hooks').split()) + assert mock_lineage.call_count == 1 + assert mock_report_cert.call_count == 1 + + # no lineage with this name and we didn't give domains + mock_choose_names.return_value = ["somename"] + mock_sans_for_certname.return_value = None + self._call(('certonly --webroot --cert-name example.com --no-directory-hooks').split()) + assert mock_choose_names.called is True + + @mock.patch('certbot._internal.main._report_next_steps') + @mock.patch('certbot._internal.main._get_and_save_cert') + @mock.patch('certbot._internal.main._csr_get_and_save_cert') + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + def test_dryrun_next_steps_no_cert_saved(self, mock_lineage, mock_csr_get_cert, + unused_mock_get_cert, mock_report_next_steps): + """certonly --dry-run shouldn't report creation of a certificate in NEXT STEPS.""" + mock_lineage.return_value = None + mock_csr_get_cert.return_value = ("/cert", "/chain", "/fullchain") + for flag in (f"--csr {CSR}", "-d example.com"): + self._call(f"certonly {flag} --webroot --cert-name example.com --dry-run".split()) + mock_report_next_steps.assert_called_once_with( + mock.ANY, mock.ANY, mock.ANY, new_or_renewed_cert=False) + mock_report_next_steps.reset_mock() + + @mock.patch('certbot._internal.main._report_next_steps') + @mock.patch('certbot._internal.main._report_new_cert') + @mock.patch('certbot._internal.main._find_cert') + @mock.patch('certbot._internal.main._get_and_save_cert') + @mock.patch('certbot._internal.plugins.selection.choose_configurator_plugins') + def test_installer_runs_restart(self, mock_sel, mock_get_cert, mock_find_cert, + unused_report_new, unused_report_next): + mock_installer = mock.MagicMock() + mock_sel.return_value = (mock_installer, None) + mock_get_cert.return_value = mock.MagicMock() + mock_find_cert.return_value = (True, None) + + self._call('certonly --nginx -d example.com'.split()) + mock_installer.restart.assert_called_once() + + @mock.patch('certbot._internal.main._report_next_steps') + @mock.patch('certbot._internal.main._report_new_cert') + @mock.patch('certbot._internal.main._find_cert') + @mock.patch('certbot._internal.main._get_and_save_cert') + @mock.patch('certbot._internal.plugins.selection.choose_configurator_plugins') + def test_dryrun_installer_doesnt_restart(self, mock_sel, mock_get_cert, mock_find_cert, + unused_report_new, unused_report_next): + mock_installer = mock.MagicMock() + mock_sel.return_value = (mock_installer, None) + mock_get_cert.return_value = mock.MagicMock() + mock_find_cert.return_value = (True, None) + + self._call('certonly --nginx -d example.com --dry-run'.split()) + mock_installer.restart.assert_not_called() + + @mock.patch('certbot._internal.main._report_next_steps') + @mock.patch('certbot._internal.main._report_new_cert') + @mock.patch('certbot._internal.main._find_cert') + @mock.patch('certbot._internal.main._get_and_save_cert') + def test_invalid_installer(self, mock_get_cert, mock_find_cert, + unused_report_new, unused_report_next): + mock_get_cert.return_value = mock.MagicMock() + mock_find_cert.return_value = (True, None) + self._call((f'certonly --webroot -w {tempfile.gettempdir()} ' + + '-i standalone -d example.com').split()) + + +class FindSansOrCertnameTest(unittest.TestCase): + """Tests for certbot._internal.main._find_sans_or_certname.""" + + @mock.patch('certbot.display.ops.choose_names') + def test_display_ops(self, mock_choose_names): + mock_config = mock.Mock(domains=None, ip_addresses=None, certname=None) + mock_choose_names.return_value = ["example.com"] + # pylint: disable=protected-access + assert main._find_sans_or_certname(mock_config, None) == ([san.DNSName("example.com")], None) + + @mock.patch('certbot.display.ops.choose_names') + def test_no_results(self, mock_choose_names): + mock_config = mock.Mock(domains=None, ip_addresses=None, certname=None) + mock_choose_names.return_value = [] + # pylint: disable=protected-access + with pytest.raises(errors.Error): + main._find_sans_or_certname(mock_config, None) + + @mock.patch('certbot._internal.cert_manager.sans_for_certname') + def test_grab_domains(self, mock_domains): + mock_config = mock.Mock(domains=None, ip_addresses=None, certname="one.com") + mock_domains.return_value = ["one.com", "two.com"] + # pylint: disable=protected-access + assert main._find_sans_or_certname(mock_config, None) == \ + (["one.com", "two.com"], "one.com") + + +class RevokeTest(test_util.TempDirTestCase): + """Tests for certbot._internal.main.revoke.""" + + def setUp(self): + super().setUp() + + shutil.copy(CERT_PATH, self.tempdir) + self.tmp_cert_path = os.path.abspath(os.path.join(self.tempdir, 'cert_512.pem')) + + patches = [ + mock.patch('certbot._internal.client.acme_client'), + mock.patch('certbot._internal.client.Client'), + mock.patch('certbot._internal.main._determine_account'), + mock.patch('certbot._internal.main.display_ops.success_revocation') + ] + self.mock_acme_client = patches[0].start().ClientV2 + patches[1].start() + self.mock_determine_account = patches[2].start() + self.mock_success_revoke = patches[3].start() + for patch in patches: + self.addCleanup(patch.stop) + + from certbot._internal.account import Account + + self.regr = mock.MagicMock() + self.meta = Account.Meta( + creation_host="test.certbot.org", + creation_dt=datetime.datetime( + 2015, 7, 4, 14, 4, 10, tzinfo=datetime.timezone.utc)) + self.acc = Account(self.regr, JWK, self.meta) + + self.mock_determine_account.return_value = (self.acc, None) + + def _call(self, args=None): + if not args: + args = 'revoke --cert-path={0} ' + args = args.format(self.tmp_cert_path).split() + plugins = disco.PluginsRegistry.find_all() + config = cli.prepare_and_parse_args(plugins, args) + + from certbot._internal.main import revoke + revoke(config, plugins) + + @mock.patch('certbot._internal.main._delete_if_appropriate') + @mock.patch('certbot._internal.main.client.acme_client') + def test_revoke_with_reason(self, mock_acme_client, + mock_delete_if_appropriate): + mock_delete_if_appropriate.return_value = False + mock_revoke = mock_acme_client.ClientV2().revoke + expected = [] + for reason, code in constants.REVOCATION_REASONS.items(): + args = 'revoke --cert-path={0} --reason {1}'.format(self.tmp_cert_path, reason).split() + self._call(args) + expected.append(mock.call(mock.ANY, code)) + args = 'revoke --cert-path={0} --reason {1}'.format(self.tmp_cert_path, + reason.upper()).split() + self._call(args) + expected.append(mock.call(mock.ANY, code)) + assert expected == mock_revoke.call_args_list + + @mock.patch('certbot._internal.main._delete_if_appropriate') + @mock.patch('certbot._internal.storage.RenewableCert') + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + @mock.patch('certbot._internal.client.create_acme_client') + def test_revoke_by_certname(self, mock_create_acme, + unused_mock_renewal_file_for_certname, mock_cert, + mock_delete_if_appropriate): + mock_create_acme.return_value = self.mock_acme_client + mock_cert.return_value = mock.MagicMock(cert_path=self.tmp_cert_path, + server="https://acme.example") + args = 'revoke --cert-name=example.com'.split() + mock_delete_if_appropriate.return_value = False + self._call(args) + assert mock_create_acme.call_args_list[0][0][0].server == \ + 'https://acme.example' + self.mock_success_revoke.assert_called_once_with(self.tmp_cert_path) + + @mock.patch('certbot._internal.main._delete_if_appropriate') + @mock.patch('certbot._internal.storage.RenewableCert') + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + @mock.patch('certbot._internal.client.create_acme_client') + def test_revoke_by_certname_and_server(self, mock_create_acme, + unused_mock_renewal_file_for_certname, mock_cert, + mock_delete_if_appropriate): + """Revoking with --server should use the server from the CLI""" + mock_cert.return_value = mock.MagicMock(cert_path=self.tmp_cert_path, + server="https://acme.example") + args = 'revoke --cert-name=example.com --server https://other.example'.split() + mock_delete_if_appropriate.return_value = False + self._call(args) + assert mock_create_acme.call_args_list[0][0][0].server == \ + 'https://other.example' + self.mock_success_revoke.assert_called_once_with(self.tmp_cert_path) + + @mock.patch('certbot._internal.main._delete_if_appropriate') + @mock.patch('certbot._internal.storage.RenewableCert') + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + @mock.patch('certbot._internal.client.create_acme_client') + def test_revoke_by_certname_empty_server(self, mock_create_acme, + unused_mock_renewal_file_for_certname, + mock_cert, mock_delete_if_appropriate): + """Revoking with --cert-name where the lineage server is empty shouldn't crash """ + mock_cert.return_value = mock.MagicMock(cert_path=self.tmp_cert_path, server=None) + args = 'revoke --cert-name=example.com'.split() + mock_delete_if_appropriate.return_value = False + self._call(args) + assert mock_create_acme.call_args_list[0][0][0].server == \ + constants.CLI_DEFAULTS['server'] + self.mock_success_revoke.assert_called_once_with(self.tmp_cert_path) + + @mock.patch('certbot._internal.main._delete_if_appropriate') + def test_revocation_success(self, mock_delete_if_appropriate): + self._call() + mock_delete_if_appropriate.return_value = False + self.mock_success_revoke.assert_called_once_with(self.tmp_cert_path) + + def test_revocation_error(self): + from acme import errors as acme_errors + self.mock_acme_client.side_effect = acme_errors.ClientError() + with pytest.raises(acme_errors.ClientError): + self._call() + self.mock_success_revoke.assert_not_called() + + @mock.patch('certbot._internal.main._delete_if_appropriate') + @mock.patch('certbot._internal.cert_manager.delete') + @test_util.patch_display_util() + def test_revocation_with_prompt(self, mock_get_utility, + mock_delete, mock_delete_if_appropriate): + mock_get_utility().yesno.return_value = False + mock_delete_if_appropriate.return_value = False + self._call() + assert mock_delete.called is False + + +class ReconfigureTest(test_util.TempDirTestCase): + """Tests for certbot._internal.main.reconfigure""" + + def setUp(self): + super().setUp() + self.get_utility_patch = test_util.patch_display_util() + self.mock_get_utility = self.get_utility_patch.start() + self.patchers = { + 'check_symlinks': mock.patch('certbot._internal.storage.RenewableCert._check_symlinks'), + 'cert_sans': mock.patch('certbot._internal.storage.RenewableCert.sans'), + 'pick_installer': mock.patch('certbot._internal.plugins.selection.pick_installer'), + 'pick_auth': mock.patch('certbot._internal.plugins.selection.pick_authenticator'), + 'find_init': mock.patch('certbot._internal.plugins.disco.PluginsRegistry.find_init'), + '_get_and_save_cert': mock.patch('certbot._internal.main._get_and_save_cert'), + '_init_le_client': mock.patch('certbot._internal.main._init_le_client'), + 'list_hooks': mock.patch('certbot._internal.hooks.list_hooks'), + } + self.mocks = {k: v.start() for k, v in self.patchers.items()} + self.mocks['cert_sans'].return_value = [san.DNSName('example.com')] + + self.config_dir = os.path.join(self.tempdir, 'config') + renewal_configs_dir = os.path.join(self.config_dir, 'renewal') + if not os.path.exists(renewal_configs_dir): + filesystem.makedirs(renewal_configs_dir) + self.renewal_file = os.path.join(renewal_configs_dir, 'example.com.conf') + original_config = """ + version = 1.32.0 + archive_dir = /etc/letsencrypt/archive/example.com + cert = /etc/letsencrypt/live/example.com/cert.pem + privkey = /etc/letsencrypt/live/example.com/privkey.pem + chain = /etc/letsencrypt/live/example.com/chain.pem + fullchain = /etc/letsencrypt/live/example.com/fullchain.pem + + # Options used in the renewal process + [renewalparams] + account = ee43634db0aa4e6804f152be39990e6a + server = https://acme-v02.api.letsencrypt.org/directory + authenticator = nginx + installer = nginx + key_type = rsa + """ + with open(self.renewal_file, 'w') as f: + f.write(original_config) + with open(self.renewal_file, 'r') as f: + self.original_config = configobj.ConfigObj(f, + encoding='utf-8', default_encoding='utf-8') + + + def tearDown(self): + super().tearDown() + self.get_utility_patch.stop() + for patch in self.patchers.values(): + patch.stop() + + def _call(self, passed_args): + full_args = passed_args + ['--config-dir', self.config_dir] + plugins = disco.PluginsRegistry.find_all() + config = cli.prepare_and_parse_args(plugins, full_args) + + from certbot._internal.main import reconfigure + reconfigure(config, plugins) + + with open(self.renewal_file, 'r') as f: + updated_conf = configobj.ConfigObj(f, encoding='utf-8', default_encoding='utf-8') + + return updated_conf + + def test_domains_set(self): + with pytest.raises(errors.ConfigurationError): + self._call('--cert-name cert1 -d one.cert.com'.split()) + + @mock.patch('certbot._internal.cert_manager.get_certnames') + def test_asks_for_certname(self, mock_cert_manager): + named_mock = mock.Mock() + named_mock.name = 'nginx' + + self.mocks['pick_installer'].return_value = named_mock + self.mocks['pick_auth'].return_value = named_mock + self.mocks['find_init'].return_value = named_mock + mock_cert_manager.return_value = ['example.com'] + self._call('--nginx'.split()) + assert mock_cert_manager.call_count == 1 + + def test_update_configurator(self): + named_mock = mock.Mock() + named_mock.name = 'apache' + + self.mocks['pick_installer'].return_value = named_mock + self.mocks['pick_auth'].return_value = named_mock + self.mocks['find_init'].return_value = named_mock + + new_config = self._call('--cert-name example.com --apache'.split()) + assert new_config['renewalparams']['authenticator'] == 'apache' + + def test_only_intended_changes(self): + """ Check that we don't accidentally modify anything that we didn't mean to """ + named_mock = mock.Mock() + named_mock.name = 'apache' + + self.mocks['pick_installer'].return_value = named_mock + self.mocks['pick_auth'].return_value = named_mock + self.mocks['find_init'].return_value = named_mock + + new_config = self._call('--cert-name example.com --apache'.split()) + # Undo the changes we made in calling and in testing + new_config['renewalparams']['authenticator'] = 'nginx' + new_config['renewalparams']['installer'] = 'nginx' + del new_config['renewalparams']['config_dir'] + new_config['version'] = self.original_config['version'] + + assert new_config == self.original_config + + @mock.patch('certbot._internal.hooks.validate_hooks') + def test_staging_used(self, unused_validate_hooks): + """ Check that we use the staging server for the dry run """ + assert self.original_config['renewalparams']['server'] == \ + 'https://acme-v02.api.letsencrypt.org/directory' + + self._call('--cert-name example.com --pre-hook'.split() + ['echo pre']) + + assert 'staging' in self.mocks['_init_le_client'].call_args.args[0].server + assert 'staging' in self.mocks['_get_and_save_cert'].call_args.args[1].server + + def test_new_account_or_server_errors(self): + """ Check that we error when attempting to change the account id or server, + but not when it's the same + """ + orig_account_id = self.original_config['renewalparams']['account'] + orig_server = self.original_config['renewalparams']['server'] + + # new account + try: + self._call('--cert-name example.com --account newaccountid'.split()) + except errors.ConfigurationError as err: + assert "Using reconfigure to change the ACME account" in str(err) + + # check that config isn't modified + with open(self.renewal_file, 'r') as f: + new_config = configobj.ConfigObj(f, encoding='utf-8', default_encoding='utf-8') + assert new_config['renewalparams']['account'] == orig_account_id + + # same account + new_config = self._call(f'--cert-name example.com --account {orig_account_id}'.split()) + assert new_config['renewalparams']['account'] == orig_account_id + + # new server + try: + self._call('--cert-name example.com --server x.com'.split()) + except errors.ConfigurationError as err: + assert "Using reconfigure to change the ACME account" in str(err) + + # check that config isn't modified + with open(self.renewal_file, 'r') as f: + new_config = configobj.ConfigObj(f, encoding='utf-8', default_encoding='utf-8') + assert new_config['renewalparams']['server'] == orig_server + + # same server + new_config = self._call(f'--cert-name example.com --server {orig_server}'.split()) + assert new_config['renewalparams']['server'] == orig_server + + @mock.patch('certbot._internal.hooks.validate_hooks') + def test_update_hooks(self, unused_validate_hooks): + assert 'pre_hook' not in self.original_config + # test set + new_config = self._call('--cert-name example.com --pre-hook'.split() + ['echo pre']) + assert new_config['renewalparams']['pre_hook'] == 'echo pre' + # test update + new_config = self._call('--cert-name example.com --pre-hook'.split() + ['echo pre2']) + assert new_config['renewalparams']['pre_hook'] == 'echo pre2' + + # test deploy hook is set even though we did a dry run + assert 'renew_hook' not in self.original_config + new_config = self._call('--cert-name example.com --deploy-hook'.split() + ['echo deploy']) + assert new_config['renewalparams']['renew_hook'] == 'echo deploy' + + def test_dry_run_fails(self): + # set side effect of raising error + self.mocks['_get_and_save_cert'].side_effect = errors.Error + + try: + self._call('--cert-name example.com --apache'.split()) + except errors.Error: + pass + + # check that config isn't modified + with open(self.renewal_file, 'r') as f: + new_config = configobj.ConfigObj(f, encoding='utf-8', default_encoding='utf-8') + assert new_config['renewalparams']['authenticator'] == 'nginx' + + @mock.patch('certbot._internal.main.display_util.notify') + def test_report_results(self, mock_notify): + # make sure report results works when config has a webroot map + original_config = """ + version = 2.0.0 + archive_dir = /etc/letsencrypt/archive/example.com + cert = /etc/letsencrypt/live/example.com/cert.pem + privkey = /etc/letsencrypt/live/example.com/privkey.pem + chain = /etc/letsencrypt/live/example.com/chain.pem + fullchain = /etc/letsencrypt/live/example.com/fullchain.pem + + # Options used in the renewal process + [renewalparams] + account = ee43634db0aa4e6804f152be39990e6a + server = https://acme-staging-v02.api.letsencrypt.org/directory + authenticator = webroot + installer = nginx + key_type = ecdsa + webroot_path = /var/www/html, + [[webroot_map]] + example.com = /var/www/html + """ + with open(self.renewal_file, 'w') as f: + f.write(original_config) + with open(self.renewal_file, 'r') as f: + self.original_config = configobj.ConfigObj(f, + encoding='utf-8', default_encoding='utf-8') + + named_mock = mock.Mock() + named_mock.name = 'nginx' + + self.mocks['pick_auth'].return_value = named_mock + self.mocks['find_init'].return_value = named_mock + + new_config = self._call('--cert-name example.com --nginx'.split()) + assert new_config['renewalparams']['authenticator'] == 'nginx' + mock_notify.assert_called_with( + '\nSuccessfully updated configuration.'+ + '\nChanges will apply when the certificate renews.') + + +class DeleteIfAppropriateTest(test_util.ConfigTestCase): + """Tests for certbot._internal.main._delete_if_appropriate """ + + def _call(self, mock_config): + from certbot._internal.main import _delete_if_appropriate + _delete_if_appropriate(mock_config) + + def _test_delete_opt_out_common(self): + with mock.patch('certbot._internal.cert_manager.delete') as mock_delete: + self._call(self.config) + mock_delete.assert_not_called() + + @test_util.patch_display_util() + def test_delete_flag_opt_out(self, unused_mock_get_utility): + self.config.delete_after_revoke = False + self._test_delete_opt_out_common() + + @test_util.patch_display_util() + def test_delete_prompt_opt_out(self, mock_get_utility): + util_mock = mock_get_utility() + util_mock.yesno.return_value = False + self._test_delete_opt_out_common() + + @mock.patch("certbot._internal.main.logger.warning") + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + @mock.patch('certbot._internal.cert_manager.delete') + @mock.patch('certbot._internal.cert_manager.match_and_check_overlaps') + @mock.patch('certbot._internal.storage.full_archive_path') + @mock.patch('certbot._internal.cert_manager.cert_path_to_lineage') + @test_util.patch_display_util() + def test_overlapping_archive_dirs(self, mock_get_utility, + mock_cert_path_to_lineage, mock_archive, + mock_match_and_check_overlaps, mock_delete, + mock_renewal_file_for_certname, mock_warning): + # pylint: disable = unused-argument + config = self.config + config.cert_path = "/some/reasonable/path" + config.certname = "" + mock_cert_path_to_lineage.return_value = "example.com" + mock_match_and_check_overlaps.side_effect = errors.OverlappingMatchFound() + self._call(config) + mock_delete.assert_not_called() + assert mock_warning.call_count == 1 + + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + @mock.patch('certbot._internal.cert_manager.match_and_check_overlaps') + @mock.patch('certbot._internal.storage.full_archive_path') + @mock.patch('certbot._internal.cert_manager.delete') + @mock.patch('certbot._internal.cert_manager.cert_path_to_lineage') + @test_util.patch_display_util() + def test_cert_path_only(self, mock_get_utility, + mock_cert_path_to_lineage, mock_delete, mock_archive, + mock_overlapping_archive_dirs, mock_renewal_file_for_certname): + # pylint: disable = unused-argument + config = self.config + config.cert_path = "/some/reasonable/path" + config.certname = "" + mock_cert_path_to_lineage.return_value = "example.com" + mock_overlapping_archive_dirs.return_value = False + self._call(config) + assert mock_delete.call_count == 1 + + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + @mock.patch('certbot._internal.cert_manager.match_and_check_overlaps') + @mock.patch('certbot._internal.storage.full_archive_path') + @mock.patch('certbot._internal.cert_manager.cert_path_to_lineage') + @mock.patch('certbot._internal.cert_manager.delete') + @test_util.patch_display_util() + def test_noninteractive_deletion(self, mock_get_utility, mock_delete, + mock_cert_path_to_lineage, mock_full_archive_dir, + mock_match_and_check_overlaps, mock_renewal_file_for_certname): + # pylint: disable = unused-argument + config = self.config + config.noninteractive_mode = True + config.cert_path = "/some/reasonable/path" + config.certname = "" + mock_cert_path_to_lineage.return_value = "example.com" + mock_full_archive_dir.return_value = "" + mock_match_and_check_overlaps.return_value = "" + self._call(config) + assert mock_delete.call_count == 1 + + @mock.patch('certbot._internal.storage.renewal_file_for_certname') + @mock.patch('certbot._internal.cert_manager.match_and_check_overlaps') + @mock.patch('certbot._internal.storage.full_archive_path') + @mock.patch('certbot._internal.cert_manager.cert_path_to_lineage') + @mock.patch('certbot._internal.cert_manager.delete') + @test_util.patch_display_util() + def test_opt_in_deletion(self, mock_get_utility, mock_delete, + mock_cert_path_to_lineage, mock_full_archive_dir, + mock_match_and_check_overlaps, mock_renewal_file_for_certname): + config = self.config + config.delete_after_revoke = True + config.cert_path = "/some/reasonable/path" + config.certname = "" + mock_cert_path_to_lineage.return_value = "example.com" + mock_full_archive_dir.return_value = "" + mock_match_and_check_overlaps.return_value = "" + self._call(config) + assert mock_delete.call_count == 1 + assert not mock_get_utility().yesno.called + + +class DetermineAccountTest(test_util.ConfigTestCase): + """Tests for certbot._internal.main._determine_account.""" + + def setUp(self): + super().setUp() + self.config.account = None + self.config.email = None + self.config.register_unsafely_without_email = False + self.accs = [mock.MagicMock(id='x'), mock.MagicMock(id='y')] + self.account_storage = account.AccountMemoryStorage() + # For use in saving accounts: fake out the new_authz URL. + self.mock_client = mock.MagicMock() + self.mock_client.directory.new_authz = "hi" + + + def _call(self): + # pylint: disable=protected-access + from certbot._internal.main import _determine_account + with mock.patch('certbot._internal.main.account.AccountFileStorage') as mock_storage, \ + test_util.patch_display_util(): + mock_storage.return_value = self.account_storage + return _determine_account(self.config) + + @mock.patch('certbot._internal.client.register') + @mock.patch('certbot._internal.client.display_ops.get_email') + def _register_error_common(self, err_msg, exception, mock_get_email, mock_register): + mock_get_email.return_value = 'foo@bar.baz' + mock_register.side_effect = exception + try: + self._call() + except errors.Error as err: + assert f"Unable to register an account with ACME server. {err_msg}" == \ + str(err) + + def test_args_account_set(self): + self.account_storage.save(self.accs[1], self.mock_client) + self.config.account = self.accs[1].id + assert (self.accs[1], None) == self._call() + assert self.accs[1].id == self.config.account + assert self.config.email is None + + def test_single_account(self): + self.account_storage.save(self.accs[0], self.mock_client) + assert (self.accs[0], None) == self._call() + assert self.accs[0].id == self.config.account + assert self.config.email is None + + @mock.patch('certbot._internal.client.display_ops.choose_account') + def test_multiple_accounts(self, mock_choose_accounts): + for acc in self.accs: + self.account_storage.save(acc, self.mock_client) + mock_choose_accounts.return_value = self.accs[1] + assert (self.accs[1], None) == self._call() + assert set(mock_choose_accounts.call_args[0][0]) == set(self.accs) + assert self.accs[1].id == self.config.account + assert self.config.email is None + + @mock.patch('certbot._internal.client.display_ops.choose_account') + def test_multiple_accounts_canceled(self, mock_choose_accounts): + for acc in self.accs: + self.account_storage.save(acc, self.mock_client) + mock_choose_accounts.return_value = None + try: + self._call() + except errors.Error as err: + assert "No account has been chosen" in str(err) + + @mock.patch('certbot._internal.client.display_ops.get_email') + @mock.patch('certbot._internal.main.display_util.notify') + def test_no_accounts_no_email(self, mock_notify, mock_get_email): + mock_get_email.return_value = 'foo@bar.baz' + + with mock.patch('certbot._internal.main.client') as client: + client.register.return_value = ( + self.accs[0], mock.sentinel.acme) + assert (self.accs[0], mock.sentinel.acme) == self._call() + client.register.assert_called_once_with( + self.config, self.account_storage, tos_cb=mock.ANY) + + assert self.accs[0].id == self.config.account + assert 'foo@bar.baz' == self.config.email + mock_notify.assert_called_once_with('Account registered.') + + def test_no_accounts_email(self): + self.config.email = 'other email' + with mock.patch('certbot._internal.main.client') as client: + client.register.return_value = (self.accs[1], mock.sentinel.acme) + self._call() + assert self.accs[1].id == self.config.account + assert 'other email' == self.config.email + + def test_register_error_certbot(self): + err_msg = "Some error message raised by Certbot" + self._register_error_common(err_msg, errors.Error(err_msg)) + + def test_register_error_acme_type_and_detail(self): + err_msg = ("Error returned by the ACME server: must agree to terms of service") + exception = acme_error(typ = "urn:ietf:params:acme:error:malformed", + detail = "must agree to terms of service") + self._register_error_common(err_msg, exception) + + def test_register_error_acme_type_only(self): + err_msg = ("Error returned by the ACME server: The server experienced an internal error") + exception = acme_error(typ = "urn:ietf:params:acme:error:serverInternal") + self._register_error_common(err_msg, exception) + + +class MainTest(test_util.ConfigTestCase): + """Tests for different commands.""" + + def setUp(self): + super().setUp() + + filesystem.mkdir(self.config.logs_dir) + self.standard_args = ['--config-dir', self.config.config_dir, + '--work-dir', self.config.work_dir, + '--logs-dir', self.config.logs_dir, '--text'] + + self.mock_sleep = mock.patch('time.sleep').start() + + def tearDown(self): + # Reset globals in cli + reload_module(cli) + + super().tearDown() + + def _call(self, args, stdout=None, mockisfile=False): + """Run the cli with output streams, actual client and optionally + os.path.isfile() mocked out""" + + if mockisfile: + orig_open = os.path.isfile + + def mock_isfile(fn, *args, **kwargs): # pylint: disable=unused-argument + """Mock os.path.isfile()""" + if (fn.endswith("cert") or + fn.endswith("chain") or + fn.endswith("privkey")): + return True + return orig_open(fn) + + with mock.patch("certbot.compat.os.path.isfile") as mock_if: + mock_if.side_effect = mock_isfile + with mock.patch('certbot._internal.main.client') as client: + ret, stdout, stderr = self._call_no_clientmock(args, stdout) + return ret, stdout, stderr, client + else: + with mock.patch('certbot._internal.main.client') as client: + ret, stdout, stderr = self._call_no_clientmock(args, stdout) + return ret, stdout, stderr, client + + def _call_no_clientmock(self, args, stdout=None): + """Run the client with output streams mocked out""" + args = self.standard_args + args + + toy_stdout = stdout if stdout else io.StringIO() + with mock.patch('certbot._internal.main.sys.stdout', new=toy_stdout): + with mock.patch('certbot._internal.main.sys.stderr') as stderr: + with mock.patch("certbot.util.atexit"): + ret = main.main(args[:]) # NOTE: parser can alter its args! + return ret, toy_stdout, stderr + + def test_no_flags(self): + with mock.patch('certbot._internal.main.run') as mock_run: + self._call([]) + assert 1 == mock_run.call_count + + def test_version_string_program_name(self): + toy_out = io.StringIO() + toy_err = io.StringIO() + with mock.patch('certbot._internal.main.sys.stdout', new=toy_out): + with mock.patch('certbot._internal.main.sys.stderr', new=toy_err): + try: + main.main(["--version"]) + except SystemExit: + pass + finally: + output = toy_out.getvalue() or toy_err.getvalue() + assert "certbot" in output, "Output is {0}".format(output) + + def _cli_missing_flag(self, args, message): + "Ensure that a particular error raises a missing cli flag error containing message" + exc = None + try: + with mock.patch('certbot._internal.main.sys.stderr'): + main.main(self.standard_args + args[:]) # NOTE: parser can alter its args! + except errors.MissingCommandlineFlag as exc_: + exc = exc_ + assert message in str(exc) + assert exc is not None + + @mock.patch('certbot._internal.log.post_arg_parse_setup') + def test_noninteractive(self, _): + args = ['-n', 'certonly'] + self._cli_missing_flag(args, "specify a plugin") + + @mock.patch('certbot._internal.eff.handle_subscription') + @mock.patch('certbot._internal.log.post_arg_parse_setup') + @mock.patch('certbot._internal.main._report_new_cert') + @mock.patch('certbot._internal.main._determine_account') + @mock.patch('certbot._internal.main.client.Client.obtain_and_enroll_certificate') + @mock.patch('certbot._internal.main._get_and_save_cert') + def test_user_agent(self, gsc, _obt, det, _, __, ___): + # Normally the client is totally mocked out, but here we need more + # arguments to automate it... + args = ["--standalone", "certonly", "-m", "none@none.com", + "-d", "example.com", '--agree-tos'] + self.standard_args + det.return_value = mock.MagicMock(), None + gsc.return_value = mock.MagicMock() + + with mock.patch('certbot._internal.main.client.acme_client') as acme_client: + acme_net = acme_client.ClientNetwork + self._call_no_clientmock(args) + os_ver = util.get_os_info_ua() + ua = acme_net.call_args[1]["user_agent"] + assert os_ver in ua + import platform + plat = platform.platform() + if "linux" in plat.lower(): + assert util.get_os_info_ua() in ua + + with mock.patch('certbot._internal.main.client.acme_client') as acme_client: + acme_net = acme_client.ClientNetwork + ua = "bandersnatch" + args += ["--user-agent", ua] + self._call_no_clientmock(args) + acme_net.assert_called_once_with(mock.ANY, account=mock.ANY, verify_ssl=True, + user_agent=ua, alg=jose.RS256) + + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @mock.patch('certbot._internal.main.plug_sel.pick_installer') + def test_installer_selection(self, mock_pick_installer, _rec): + self._call(['install', '--domains', 'foo.bar', '--cert-path', 'cert', + '--key-path', 'privkey', '--chain-path', 'chain'], mockisfile=True) + assert mock_pick_installer.call_count == 1 + + @mock.patch('certbot._internal.main._install_cert') + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @mock.patch('certbot._internal.main.plug_sel.pick_installer') + def test_installer_certname(self, _inst, _rec, mock_install): + mock_lineage = mock.MagicMock(cert_path=test_util.temp_join('cert'), + chain_path=test_util.temp_join('chain'), + fullchain_path=test_util.temp_join('chain'), + key_path=test_util.temp_join('privkey')) + + with mock.patch("certbot._internal.cert_manager.lineage_for_certname") as mock_getlin: + mock_getlin.return_value = mock_lineage + self._call(['install', '--cert-name', 'whatever'], mockisfile=True) + call_config = mock_install.call_args[0][0] + assert call_config.cert_path == test_util.temp_join('cert') + assert call_config.fullchain_path == test_util.temp_join('chain') + assert call_config.key_path == test_util.temp_join('privkey') + + @mock.patch('certbot._internal.log.post_arg_parse_setup') + @mock.patch('certbot._internal.main._install_cert') + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @mock.patch('certbot._internal.main.plug_sel.pick_installer') + def test_installer_param_override(self, _inst, _rec, mock_install, _): + mock_lineage = mock.MagicMock(cert_path=test_util.temp_join('cert'), + chain_path=test_util.temp_join('chain'), + fullchain_path=test_util.temp_join('chain'), + key_path=test_util.temp_join('privkey')) + with mock.patch("certbot._internal.cert_manager.lineage_for_certname") as mock_getlin: + mock_getlin.return_value = mock_lineage + self._call(['install', '--cert-name', 'whatever', + '--key-path', test_util.temp_join('overriding_privkey')], mockisfile=True) + call_config = mock_install.call_args[0][0] + assert call_config.cert_path == test_util.temp_join('cert') + assert call_config.fullchain_path == test_util.temp_join('chain') + assert call_config.chain_path == test_util.temp_join('chain') + assert call_config.key_path == test_util.temp_join('overriding_privkey') + + mock_install.reset() + + self._call(['install', '--cert-name', 'whatever', + '--cert-path', test_util.temp_join('overriding_cert')], mockisfile=True) + call_config = mock_install.call_args[0][0] + assert call_config.cert_path == test_util.temp_join('overriding_cert') + assert call_config.fullchain_path == test_util.temp_join('chain') + assert call_config.key_path == test_util.temp_join('privkey') + + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @mock.patch('certbot._internal.main.plug_sel.pick_installer') + def test_installer_param_error(self, _inst, _rec): + with pytest.raises(errors.ConfigurationError): + self._call(['install', '--cert-name', 'notfound', + '--key-path', 'invalid']) + + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @mock.patch('certbot._internal.main.plug_sel.pick_installer') + @mock.patch('certbot._internal.cert_manager.get_certnames') + @mock.patch('certbot._internal.main._install_cert') + def test_installer_select_cert(self, mock_inst, mock_getcert, _inst, _rec): + mock_lineage = mock.MagicMock(cert_path=test_util.temp_join('cert'), + chain_path=test_util.temp_join('chain'), + fullchain_path=test_util.temp_join('chain'), + key_path=test_util.temp_join('privkey')) + with mock.patch("certbot._internal.cert_manager.lineage_for_certname") as mock_getlin: + mock_getlin.return_value = mock_lineage + self._call(['install'], mockisfile=True) + assert mock_getcert.called + assert mock_inst.called + + @mock.patch('certbot._internal.eff.handle_subscription') + @mock.patch('certbot._internal.log.post_arg_parse_setup') + @mock.patch('certbot._internal.main._report_new_cert') + @mock.patch('certbot.util.exe_exists') + def test_configurator_selection(self, mock_exe_exists, _, __, ___): + mock_exe_exists.return_value = True + real_plugins = disco.PluginsRegistry.find_all() + args = ['--apache', '--authenticator', 'standalone'] + + # This needed two calls to find_all(), which we're avoiding for now + # because of possible side effects: + # https://github.com/certbot/certbot/commit/51ed2b681f87b1eb29088dd48718a54f401e4855 + # with mock.patch('certbot._internal.cli.plugins_testable') as plugins: + # plugins.return_value = {"apache": True, "nginx": True} + # ret, _, _, _ = self._call(args) + # self.assertTrue("Too many flags setting" in ret) + + args = ["install", "--nginx", "--cert-path", + test_util.temp_join('blah'), "--key-path", test_util.temp_join('blah'), + "--nginx-server-root", "/nonexistent/thing", "-d", + "example.com", "--debug"] + if "nginx" in real_plugins: + # Sending nginx a non-existent conf dir will simulate misconfiguration + # (we can only do that if certbot-nginx is actually present) + ret, _, _, _ = self._call(args) + assert "The nginx plugin is not working" in ret + assert "MisconfigurationError" in ret + + self._cli_missing_flag(["--standalone"], "With the standalone plugin, you probably") + + with mock.patch("certbot._internal.main._init_le_client") as mock_init: + with mock.patch("certbot._internal.main._get_and_save_cert") as mock_gsc: + mock_gsc.return_value = mock.MagicMock() + self._call(["certonly", "--manual", "-d", "foo.bar"]) + unused_config, auth, unused_installer = mock_init.call_args[0] + assert isinstance(auth, manual.Authenticator) + + with mock.patch('certbot._internal.main.certonly') as mock_certonly: + self._call(["auth", "--standalone"]) + assert 1 == mock_certonly.call_count + + @mock.patch('certbot._internal.log.post_arg_parse_setup') + def test_rollback(self, _): + _, _, _, client = self._call(['rollback']) + assert 1 == client.rollback.call_count + + _, _, _, client = self._call(['rollback', '--checkpoints', '123']) + client.rollback.assert_called_once_with( + mock.ANY, 123, mock.ANY, mock.ANY) + + @mock.patch('certbot._internal.cert_manager.certificates') + def test_certificates(self, mock_cert_manager): + self._call_no_clientmock(['certificates']) + assert 1 == mock_cert_manager.call_count + + @mock.patch('certbot._internal.cert_manager.delete') + def test_delete(self, mock_cert_manager): + self._call_no_clientmock(['delete']) + assert 1 == mock_cert_manager.call_count + + @mock.patch('certbot._internal.main.plugins_disco') + @mock.patch('certbot._internal.main.cli.HelpfulArgumentParser.determine_help_topics') + @mock.patch('certbot._internal.log.post_arg_parse_setup') + def test_plugins(self, _, _det, mock_disco): + flags = ['--init', '--prepare', '--authenticators', '--installers'] + for args in itertools.chain( + *(itertools.combinations(flags, r) + for r in range(len(flags)))): + self._call(['plugins'] + list(args)) + + @mock.patch('certbot._internal.main.plugins_disco') + @mock.patch('certbot._internal.main.cli.HelpfulArgumentParser.determine_help_topics') + def test_plugins_no_args(self, _det, mock_disco): + ifaces: list[interfaces.Plugin] = [] + plugins = mock_disco.PluginsRegistry.find_all() + + stdout = io.StringIO() + with test_util.patch_display_util_with_stdout(stdout=stdout): + _, stdout, _, _ = self._call(['plugins'], stdout) + + plugins.visible.assert_called_once_with() + plugins.visible().ifaces.assert_called_once_with(ifaces) + filtered = plugins.visible().ifaces() + assert stdout.getvalue().strip() == str(filtered) + + @mock.patch('certbot._internal.main.plugins_disco') + @mock.patch('certbot._internal.main.cli.HelpfulArgumentParser.determine_help_topics') + def test_plugins_no_args_unprivileged(self, _det, mock_disco): + ifaces: list[interfaces.Plugin] = [] + plugins = mock_disco.PluginsRegistry.find_all() + + def throw_error(directory, mode, strict): + """Raises error.Error.""" + _, _, _ = directory, mode, strict + raise errors.Error() + + stdout = io.StringIO() + with mock.patch('certbot.util.set_up_core_dir') as mock_set_up_core_dir: + with test_util.patch_display_util_with_stdout(stdout=stdout): + mock_set_up_core_dir.side_effect = throw_error + _, stdout, _, _ = self._call(['plugins'], stdout) + + plugins.visible.assert_called_once_with() + plugins.visible().ifaces.assert_called_once_with(ifaces) + filtered = plugins.visible().ifaces() + assert stdout.getvalue().strip() == str(filtered) + + @mock.patch('certbot._internal.main.plugins_disco') + @mock.patch('certbot._internal.main.cli.HelpfulArgumentParser.determine_help_topics') + def test_plugins_init(self, _det, mock_disco): + ifaces: list[interfaces.Plugin] = [] + plugins = mock_disco.PluginsRegistry.find_all() + + stdout = io.StringIO() + with test_util.patch_display_util_with_stdout(stdout=stdout): + _, stdout, _, _ = self._call(['plugins', '--init'], stdout) + + plugins.visible.assert_called_once_with() + plugins.visible().ifaces.assert_called_once_with(ifaces) + filtered = plugins.visible().ifaces() + assert filtered.init.call_count == 1 + assert stdout.getvalue().strip() == str(filtered) + + @mock.patch('certbot._internal.main.plugins_disco') + @mock.patch('certbot._internal.main.cli.HelpfulArgumentParser.determine_help_topics') + def test_plugins_prepare(self, _det, mock_disco): + ifaces: list[interfaces.Plugin] = [] + plugins = mock_disco.PluginsRegistry.find_all() + + stdout = io.StringIO() + with test_util.patch_display_util_with_stdout(stdout=stdout): + _, stdout, _, _ = self._call(['plugins', '--init', '--prepare'], stdout) + + plugins.visible.assert_called_once_with() + plugins.visible().ifaces.assert_called_once_with(ifaces) + filtered = plugins.visible().ifaces() + assert filtered.init.call_count == 1 + filtered.prepare.assert_called_once_with() + filtered.available.assert_called_once_with() + available = filtered.available() + assert stdout.getvalue().strip() == str(available) + + def test_certonly_abspath(self): + cert = 'cert' + key = 'key' + chain = 'chain' + fullchain = 'fullchain' + + with mock.patch('certbot._internal.main.certonly') as mock_certonly: + self._call(['certonly', '--cert-path', cert, '--key-path', 'key', + '--chain-path', 'chain', + '--fullchain-path', 'fullchain']) + + config, unused_plugins = mock_certonly.call_args[0] + assert config.cert_path == os.path.abspath(cert) + assert config.key_path == os.path.abspath(key) + assert config.chain_path == os.path.abspath(chain) + assert config.fullchain_path == os.path.abspath(fullchain) + + def test_certonly_bad_args(self): + try: + self._call(['-a', 'bad_auth', 'certonly']) + pytest.fail("Exception should have been raised") + except errors.PluginSelectionError as e: + assert 'The requested bad_auth plugin does not appear' in str(e) + + def test_check_config_sanity_domain(self): + # FQDN + with pytest.raises(errors.ConfigurationError): + self._call(['-d', 'a' * 64]) + # FQDN 2 + with pytest.raises(errors.ConfigurationError): + self._call(['-d', (('a' * 50) + '.') * 10]) + # Bare IP address (this is actually a different error message now) + with pytest.raises(errors.ConfigurationError): + self._call(['-d', '204.11.231.35']) + # Bare IPv6 address + with pytest.raises(errors.ConfigurationError): + self._call(['-d', '2001:db8:ac69:3ff:b1cb:c8c6:5a84:a31b']) + + def test_csr_with_besteffort(self): + with pytest.raises(errors.Error): + self._call('certonly --csr {0} --allow-subset-of-names'.format(CSR).split()) + + def test_run_with_csr(self): + # This is an error because you can only use --csr with certonly + try: + self._call(['--csr', CSR]) + except errors.Error as e: + assert "Please try the certonly" in repr(e) + return + pytest.fail("Expected supplying --csr to fail with default verb") + + def test_csr_with_no_domains(self): + with pytest.raises(errors.Error): + self._call('certonly --csr {0}'.format( + test_util.vector_path('csr-nonames_512.pem')).split()) + + def test_csr_with_inconsistent_domains(self): + with pytest.raises(errors.Error): + self._call('certonly -d example.org --csr {0}'.format(CSR).split()) + + def _certonly_new_request_common(self, mock_client, args=None): + with mock.patch('certbot._internal.main._find_lineage_for_sans_and_certname') \ + as mock_renewal: + mock_renewal.return_value = ("newcert", None) + with mock.patch('certbot._internal.main._init_le_client') as mock_init: + mock_init.return_value = mock_client + if args is None: + args = [] + args += '-d foo.bar -a standalone certonly'.split() + self._call(args) + + @mock.patch('certbot._internal.main._report_new_cert') + def test_certonly_dry_run_new_request_success(self, mock_report): + mock_client = mock.MagicMock() + mock_client.obtain_and_enroll_certificate.return_value = None + self._certonly_new_request_common(mock_client, ['--dry-run']) + assert mock_client.obtain_and_enroll_certificate.call_count == 1 + assert mock_report.call_count == 1 + assert mock_report.call_args[0][0].dry_run is True + + @mock.patch('certbot._internal.main._report_new_cert') + @mock.patch('certbot._internal.main.util.atexit_register') + @mock.patch('certbot._internal.eff.handle_subscription') + @mock.patch('certbot.crypto_util.notAfter') + def test_certonly_new_request_success(self, mock_notAfter, + mock_subscription, mock_register, mock_report): + cert_path = os.path.normpath(os.path.join(self.config.config_dir, 'live/foo.bar')) + key_path = os.path.normpath(os.path.join(self.config.config_dir, 'live/baz.qux')) + date = '1970-01-01' + mock_notAfter().date.return_value = date + + mock_lineage = mock.MagicMock(cert=cert_path, fullchain=cert_path, + fullchain_path=cert_path, key_path=key_path) + mock_client = mock.MagicMock() + mock_client.obtain_and_enroll_certificate.return_value = mock_lineage + self._certonly_new_request_common(mock_client) + assert mock_client.obtain_and_enroll_certificate.call_count == 1 + assert mock_report.call_count == 1 + assert cert_path in mock_report.call_args[0][2] + assert key_path in mock_report.call_args[0][3] + assert 'donate' in mock_register.call_args[0][1] + assert mock_subscription.called is True + + def _test_renewal_common(self, due_for_renewal, extra_args, log_out=None, + args=None, should_renew=True, error_expected=False, + quiet_mode=False, expiry_date=datetime.datetime.now(), + reuse_key=False, new_key=False): + cert_path = test_util.vector_path('cert_512.pem') + chain_path = os.path.normpath(os.path.join(self.config.config_dir, + 'live/foo.bar/fullchain.pem')) + mock_lineage = mock.MagicMock(cert=cert_path, fullchain=chain_path, + cert_path=cert_path, fullchain_path=chain_path) + mock_lineage.has_pending_deployment.return_value = False + mock_lineage.sans.return_value = [san.DNSName('isnot.org')] + mock_lineage.private_key_type = 'ecdsa' + mock_lineage.elliptic_curve = 'secp256r1' + mock_lineage.reuse_key = reuse_key + mock_certr = mock.MagicMock() + mock_key = mock.MagicMock(pem='pem_key') + mock_client = mock.MagicMock() + stdout = io.StringIO() + mock_client.obtain_certificate.return_value = (mock_certr, 'chain', + mock_key, 'csr') + + def write_msg(message, *args, **kwargs): # pylint: disable=unused-argument + """Write message to stdout.""" + stdout.write(message) + + try: + with mock.patch('certbot._internal.cert_manager.find_duplicative_certs') as mock_fdc: + mock_fdc.return_value = (mock_lineage, None) + with mock.patch('certbot._internal.main._init_le_client') as mock_init: + mock_init.return_value = mock_client + with mock.patch('certbot._internal.display.obj.get_display') as mock_display: + if not quiet_mode: + mock_display().notification.side_effect = write_msg + with mock.patch('certbot._internal.main.renewal.crypto_util') \ + as mock_crypto_util: + mock_crypto_util.notAfter.return_value = expiry_date + with mock.patch('certbot._internal.eff.handle_subscription'): + with mock.patch('certbot._internal.renewal.should_autorenew') as should_autorenew: + should_autorenew.return_value = due_for_renewal + if not args: + args = ['-d', 'isnot.org', '-a', 'standalone', 'certonly'] + if extra_args: + args += extra_args + try: + ret, stdout, _, _ = self._call(args, stdout) + if ret: + print("Returned", ret) + raise AssertionError(ret) + assert not error_expected, "renewal should have errored" + except: # pylint: disable=bare-except + if not error_expected: + raise AssertionError( + "Unexpected renewal error:\n" + + traceback.format_exc()) + + if should_renew: + if reuse_key and not new_key: + # The location of the previous live privkey.pem is passed + # to obtain_certificate + mock_client.obtain_certificate.assert_called_once_with([mock.ANY], + os.path.normpath(os.path.join( + self.config.config_dir, "live/sample-renewal/privkey.pem"))) + else: + mock_client.obtain_certificate.assert_called_once_with([mock.ANY], None) + else: + assert mock_client.obtain_certificate.call_count == 0 + except: + self._dump_log() + raise + finally: + if log_out: + with open(os.path.join(self.config.logs_dir, "letsencrypt.log")) as lf: + assert log_out in lf.read() + + return mock_lineage, mock_display, stdout + + @mock.patch('certbot._internal.main._report_new_cert') + @mock.patch('certbot._internal.main.util.atexit_register') + @mock.patch('certbot.crypto_util.notAfter') + def test_certonly_renewal(self, _, mock_register, mock_report): + lineage, _, _ = self._test_renewal_common(True, []) + assert lineage.save_successor.call_count == 1 + lineage.update_all_links_to.assert_called_once_with( + lineage.latest_common_version()) + assert mock_report.call_count == 1 + assert 'fullchain.pem' in mock_report.call_args[0][2] + assert 'donate' in mock_register.call_args[0][1] + + @mock.patch('certbot._internal.main.display_util.notify') + @mock.patch('certbot._internal.log.logging.handlers.RotatingFileHandler.doRollover') + @mock.patch('certbot.crypto_util.notAfter') + def test_certonly_renewal_triggers(self, _, __, mock_notify): + # --dry-run should force renewal + _, _, _ = self._test_renewal_common(False, ['--dry-run', '--keep'], + log_out="simulating renewal") + mock_notify.assert_any_call('The dry run was successful.') + + self._test_renewal_common(False, ['--renew-by-default', '-tvv', '--debug'], + log_out="Auto-renewal forced") + + _, mock_displayer, _ = self._test_renewal_common(False, ['-tvv', '--debug', '--keep'], + should_renew=False) + assert 'not yet due' in mock_displayer().notification.call_args[0][0] + + def _dump_log(self): + print("Logs:") + log_path = os.path.join(self.config.logs_dir, "letsencrypt.log") + if os.path.exists(log_path): + with open(log_path) as lf: + print(lf.read()) + + def test_renew_verb(self): + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + args = ["renew", "--dry-run", "-tvv"] + self._test_renewal_common(True, [], args=args, should_renew=True) + + def test_reuse_key(self): + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf', ec=False) + args = ["renew", "--dry-run", "--reuse-key"] + self._test_renewal_common(True, [], args=args, should_renew=True, reuse_key=True) + + @mock.patch('certbot._internal.storage.RenewableCert.save_successor') + def test_reuse_key_no_dry_run(self, unused_save_successor): + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf', ec=False) + args = ["renew", "--reuse-key"] + self._test_renewal_common(True, [], args=args, should_renew=True, reuse_key=True) + + @mock.patch('certbot._internal.storage.RenewableCert.save_successor') + def test_new_key(self, unused_save_successor): + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + args = ["renew", "--reuse-key", "--new-key"] + self._test_renewal_common(True, [], args=args, should_renew=True, reuse_key=True, + new_key=True) + + @mock.patch('sys.stdin') + def test_noninteractive_renewal_delay(self, stdin): + stdin.isatty.return_value = False + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + args = ["renew", "--dry-run", "-tvv"] + self._test_renewal_common(True, [], args=args, should_renew=True) + assert self.mock_sleep.call_count == 1 + # in main.py: + # sleep_time = random.randint(1, 60*8) + sleep_call_arg = self.mock_sleep.call_args[0][0] + assert 1 <= sleep_call_arg <= 60*8 + + @mock.patch('sys.stdin') + def test_interactive_no_renewal_delay(self, stdin): + stdin.isatty.return_value = True + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + args = ["renew", "--dry-run", "-tvv"] + self._test_renewal_common(True, [], args=args, should_renew=True) + assert self.mock_sleep.call_count == 0 + + @mock.patch('certbot._internal.renewal.should_renew') + def test_renew_skips_recent_certs(self, should_renew): + should_renew.return_value = False + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + expiry = datetime.datetime.now() + datetime.timedelta(days=90) + _, _, stdout = self._test_renewal_common(False, extra_args=None, should_renew=False, + args=['renew'], expiry_date=expiry) + assert 'No renewals were attempted.' in stdout.getvalue() + assert 'The following certificates are not due for renewal yet:' in stdout.getvalue() + + @mock.patch('certbot._internal.log.post_arg_parse_setup') + def test_quiet_renew(self, _): + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + args = ["renew", "--dry-run"] + _, _, stdout = self._test_renewal_common(True, [], args=args, should_renew=True) + out = stdout.getvalue() + assert "renew" in out + + args = ["renew", "--dry-run", "-q"] + _, _, stdout = self._test_renewal_common(True, [], args=args, + should_renew=True, quiet_mode=True) + out = stdout.getvalue() + assert "" == out + + def test_renew_hook_validation(self): + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + args = ["renew", "--dry-run", "--post-hook=no-such-command"] + self._test_renewal_common(True, [], args=args, should_renew=False, + error_expected=True) + + def test_renew_no_hook_validation(self): + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + args = ["renew", "--dry-run", "--post-hook=no-such-command", + "--disable-hook-validation"] + with mock.patch("certbot._internal.hooks.post_hook"): + self._test_renewal_common(True, [], args=args, should_renew=True, + error_expected=False) + + def test_renew_verb_empty_config(self): + rd = os.path.join(self.config.config_dir, 'renewal') + if not os.path.exists(rd): + filesystem.makedirs(rd) + with open(os.path.join(rd, 'empty.conf'), 'w'): + pass # leave the file empty + args = ["renew", "--dry-run", "-tvv"] + self._test_renewal_common(False, [], args=args, should_renew=False, error_expected=True) + + def test_renew_with_certname(self): + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + self._test_renewal_common(True, [], should_renew=True, + args=['renew', '--dry-run', '--cert-name', 'sample-renewal']) + + def test_renew_with_bad_certname(self): + self._test_renewal_common(True, [], should_renew=False, + args=['renew', '--dry-run', '--cert-name', 'sample-renewal'], + error_expected=True) + + def _make_dummy_renewal_config(self): + renewer_configs_dir = os.path.join(self.config.config_dir, 'renewal') + filesystem.makedirs(renewer_configs_dir) + with open(os.path.join(renewer_configs_dir, 'test.conf'), 'w') as f: + f.write("My contents don't matter") + + def _test_renew_common(self, renewalparams=None, names=None, + assert_oc_called=None, **kwargs): + self._make_dummy_renewal_config() + with mock.patch('certbot._internal.storage.RenewableCert') as mock_rc: + mock_lineage = mock.MagicMock() + mock_lineage.fullchain = "somepath/fullchain.pem" + if renewalparams is not None: + mock_lineage.configuration = {'renewalparams': renewalparams} + if names is not None: + mock_lineage.sans.return_value = [san.DNSName(n) for n in names] + mock_rc.return_value = mock_lineage + with mock.patch('certbot._internal.main.renew_cert') as mock_renew_cert: + kwargs.setdefault('args', ['renew']) + self._test_renewal_common(True, None, should_renew=False, **kwargs) + + if assert_oc_called is not None: + if assert_oc_called: + assert mock_renew_cert.called + else: + assert mock_renew_cert.called is False + + def test_renew_no_renewalparams(self): + self._test_renew_common(assert_oc_called=False, error_expected=True) + + def test_renew_no_authenticator(self): + self._test_renew_common(renewalparams={}, assert_oc_called=False, + error_expected=True) + + def test_renew_with_bad_int(self): + renewalparams = {'authenticator': 'webroot', + 'rsa_key_size': 'over 9000'} + self._test_renew_common(renewalparams=renewalparams, error_expected=True, + assert_oc_called=False) + + def test_renew_with_nonetype_http01(self): + renewalparams = {'authenticator': 'webroot', + 'http01_port': 'None'} + self._test_renew_common(renewalparams=renewalparams, + assert_oc_called=True) + + @mock.patch('certbot._internal.plugins.selection.choose_configurator_plugins') + def test_renew_with_configurator(self, mock_sel): + mock_sel.return_value = (mock.MagicMock(), mock.MagicMock()) + renewalparams = {'authenticator': 'webroot'} + self._test_renew_common( + renewalparams=renewalparams, assert_oc_called=True, + args='renew --configurator apache'.split()) + + def test_renew_plugin_config_restoration(self): + renewalparams = {'authenticator': 'webroot', + 'webroot_path': 'None', + 'webroot_imaginary_flag': '42'} + self._test_renew_common(renewalparams=renewalparams, + assert_oc_called=True) + + def test_renew_with_webroot_map(self): + renewalparams = {'authenticator': 'webroot'} + self._test_renew_common( + renewalparams=renewalparams, assert_oc_called=True, + args=['renew', '--webroot-map', json.dumps({'example.com': tempfile.gettempdir()})]) + + def test_renew_reconstitute_error(self): + # pylint: disable=protected-access + with mock.patch('certbot._internal.main.renewal.reconstitute') as mock_reconstitute: + mock_reconstitute.side_effect = Exception + self._test_renew_common(assert_oc_called=False, error_expected=True) + + def test_renew_obtain_cert_error(self): + self._make_dummy_renewal_config() + with mock.patch('certbot._internal.storage.RenewableCert') as mock_rc: + mock_lineage = mock.MagicMock() + mock_lineage.fullchain = "somewhere/fullchain.pem" + mock_rc.return_value = mock_lineage + mock_lineage.configuration = { + 'renewalparams': {'authenticator': 'webroot'}} + with mock.patch('certbot._internal.main.renew_cert') as mock_renew_cert: + mock_renew_cert.side_effect = Exception + self._test_renewal_common(True, None, error_expected=True, + args=['renew'], should_renew=False) + + def test_renew_with_bad_cli_args(self): + self._test_renewal_common(True, None, args='renew -d example.com'.split(), + should_renew=False, error_expected=True) + self._test_renewal_common(True, None, args='renew --csr {0}'.format(CSR).split(), + should_renew=False, error_expected=True) + + def test_no_renewal_with_hooks(self): + _, _, stdout = self._test_renewal_common( + due_for_renewal=False, extra_args=None, should_renew=False, + args=['renew', '--post-hook', + '{0} -c "print(\'hello world\');"' + .format(sys.executable)]) + assert 'No hooks were run.' in stdout.getvalue() + + @test_util.patch_display_util() + @mock.patch('certbot._internal.main._find_lineage_for_sans_and_certname') + @mock.patch('certbot._internal.main._init_le_client') + @mock.patch('certbot._internal.main._report_new_cert') + def test_certonly_reinstall(self, mock_report_new_cert, mock_init, + mock_renewal, mock_get_utility): + mock_renewal.return_value = ('reinstall', mock.MagicMock()) + mock_init.return_value = mock_client = mock.MagicMock() + self._call(['-d', 'foo.bar', '-a', 'standalone', 'certonly']) + assert mock_client.obtain_certificate.called is False + assert mock_client.obtain_and_enroll_certificate.called is False + assert mock_get_utility().add_message.call_count == 0 + mock_report_new_cert.assert_not_called() + #self.assertTrue('donate' not in mock_get_utility().add_message.call_args[0][0]) + + def _test_certonly_csr_common(self, extra_args=None): + certr = 'certr' + chain = 'chain' + mock_client = mock.MagicMock() + mock_client.obtain_certificate_from_csr.return_value = (certr, chain) + cert_path = os.path.normpath(os.path.join( + self.config.config_dir, + 'live/example.com/cert_512.pem')) + full_path = os.path.normpath(os.path.join( + self.config.config_dir, + 'live/example.com/fullchain.pem')) + mock_client.save_certificate.return_value = cert_path, None, full_path + with mock.patch('certbot._internal.main._init_le_client') as mock_init: + mock_init.return_value = mock_client + chain_path = os.path.normpath(os.path.join( + self.config.config_dir, + 'live/example.com/chain.pem')) + args = ('-a standalone certonly --csr {0} --cert-path {1} ' + '--chain-path {2} --fullchain-path {3}').format( + CSR, cert_path, chain_path, full_path).split() + if extra_args: + args += extra_args + with mock.patch('certbot._internal.main.crypto_util'): + self._call(args) + + if '--dry-run' in args: + assert mock_client.save_certificate.called is False + else: + mock_client.save_certificate.assert_called_once_with( + certr, chain, cert_path, chain_path, full_path) + + @mock.patch('certbot._internal.main._csr_report_new_cert') + @mock.patch('certbot._internal.main.util.atexit_register') + @mock.patch('certbot._internal.eff.handle_subscription') + def test_certonly_csr(self, mock_subscription, mock_register, mock_csr_report): + self._test_certonly_csr_common() + assert mock_csr_report.call_count == 1 + assert 'cert_512.pem' in mock_csr_report.call_args[0][1] + assert mock_csr_report.call_args[0][2] is None + assert 'fullchain.pem' in mock_csr_report.call_args[0][3] + assert 'donate' in mock_register.call_args[0][1] + assert mock_subscription.called is True + + @mock.patch('certbot._internal.main._csr_report_new_cert') + def test_certonly_csr_dry_run(self, mock_csr_report): + self._test_certonly_csr_common(['--dry-run']) + assert mock_csr_report.call_count == 1 + assert mock_csr_report.call_args[0][0].dry_run is True + + @mock.patch('certbot._internal.main._delete_if_appropriate') + @mock.patch('certbot._internal.main.client.acme_client') + def test_revoke_with_key(self, mock_acme_client, + mock_delete_if_appropriate): + mock_delete_if_appropriate.return_value = False + server = 'foo.bar' + self._call_no_clientmock(['--cert-path', SS_CERT_PATH, '--key-path', RSA2048_KEY_PATH, + '--server', server, 'revoke']) + with open(RSA2048_KEY_PATH, 'rb') as f: + assert mock_acme_client.ClientV2.call_count == 1 + assert mock_acme_client.ClientNetwork.call_args[0][0] == \ + jose.JWK.load(f.read()) + with open(SS_CERT_PATH, 'rb') as f: + cert = x509.load_pem_x509_certificate(f.read()) + mock_revoke = mock_acme_client.ClientV2().revoke + mock_revoke.assert_called_once_with(cert, mock.ANY) + + def test_revoke_with_key_mismatch(self): + server = 'foo.bar' + with pytest.raises(errors.Error): + self._call_no_clientmock(['--cert-path', CERT, '--key-path', P256_KEY_PATH, + '--server', server, 'revoke']) + + @mock.patch('certbot._internal.main._delete_if_appropriate') + @mock.patch('certbot._internal.main._determine_account') + def test_revoke_without_key(self, mock_determine_account, + mock_delete_if_appropriate): + mock_delete_if_appropriate.return_value = False + mock_determine_account.return_value = (mock.MagicMock(), None) + _, _, _, client = self._call(['--cert-path', CERT, 'revoke']) + with open(CERT, 'rb') as f: + cert = x509.load_pem_x509_certificate(f.read()) + mock_revoke = client.create_acme_client().revoke + mock_revoke.assert_called_once_with(cert, mock.ANY) + + @mock.patch('certbot._internal.log.post_arg_parse_setup') + def test_register(self, _): + with mock.patch('certbot._internal.main.client') as mocked_client: + acc = mock.MagicMock() + acc.id = "imaginary_account" + mocked_client.register.return_value = (acc, "worked") + self._call_no_clientmock(["register", "--email", "user@example.org"]) + # TODO: It would be more correct to explicitly check that + # _determine_account() gets called in the above case, + # but coverage statistics should also show that it did. + with mock.patch('certbot._internal.main.account') as mocked_account: + mocked_storage = mock.MagicMock() + mocked_account.AccountFileStorage.return_value = mocked_storage + mocked_storage.find_all.return_value = ["an account"] + x = self._call_no_clientmock(["register", "--email", "user@example.org"]) + assert "There is an existing account" in x[0] + + @mock.patch('certbot._internal.plugins.selection.choose_configurator_plugins') + @mock.patch('certbot._internal.updater._run_updaters') + def test_plugin_selection_error(self, mock_run, mock_choose): + mock_choose.side_effect = errors.PluginSelectionError + with pytest.raises(errors.PluginSelectionError): + main.renew_cert(None, None, None) + + self.config.dry_run = False + updater.run_generic_updaters(self.config, None, None) + # Make sure we're returning None, and hence not trying to run the + # without installer + assert mock_run.called is False + + @mock.patch('certbot._internal.main.updater.run_renewal_deployer') + @mock.patch('certbot._internal.plugins.selection.choose_configurator_plugins') + @mock.patch('certbot._internal.main._init_le_client') + @mock.patch('certbot._internal.main._get_and_save_cert') + def test_renew_doesnt_restart_on_dryrun(self, mock_get_cert, mock_init, mock_choose, + mock_run_renewal_deployer): + """A dry-run renewal shouldn't try to restart the installer""" + self.config.dry_run = True + installer = mock.MagicMock() + mock_choose.return_value = (installer, mock.MagicMock()) + + main.renew_cert(self.config, None, None) + + assert mock_init.call_count == 1 + assert mock_get_cert.call_count == 1 + installer.restart.assert_not_called() + mock_run_renewal_deployer.assert_not_called() + + +class UnregisterTest(unittest.TestCase): + def setUp(self): + self.patchers = { + '_determine_account': mock.patch('certbot._internal.main._determine_account'), + 'account': mock.patch('certbot._internal.main.account'), + 'client': mock.patch('certbot._internal.main.client'), + 'get_utility': test_util.patch_display_util()} + self.mocks = {k: v.start() for k, v in self.patchers.items()} + + def tearDown(self): + for patch in self.patchers.values(): + patch.stop() + + def test_abort_unregister(self): + self.mocks['account'].AccountFileStorage.return_value = mock.Mock() + + util_mock = self.mocks['get_utility']() + util_mock.yesno.return_value = False + + config = mock.Mock() + unused_plugins = mock.Mock() + + res = main.unregister(config, unused_plugins) + assert res == "Deactivation aborted." + + @mock.patch("certbot._internal.main.display_util.notify") + def test_unregister(self, mock_notify): + mocked_storage = mock.MagicMock() + mocked_storage.find_all.return_value = ["an account"] + + self.mocks['account'].AccountFileStorage.return_value = mocked_storage + self.mocks['_determine_account'].return_value = (mock.MagicMock(), "foo") + + cb_client = mock.MagicMock() + self.mocks['client'].Client.return_value = cb_client + + config = mock.MagicMock() + unused_plugins = mock.MagicMock() + + res = main.unregister(config, unused_plugins) + + assert res is None + mock_notify.assert_called_once_with("Account deactivated.") + + def test_unregister_no_account(self): + mocked_storage = mock.MagicMock() + mocked_storage.find_all.return_value = [] + self.mocks['account'].AccountFileStorage.return_value = mocked_storage + + cb_client = mock.MagicMock() + self.mocks['client'].Client.return_value = cb_client + + config = mock.MagicMock() + config.server = "https://acme.example.com/directory" + unused_plugins = mock.MagicMock() + + res = main.unregister(config, unused_plugins) + m = "Could not find existing account for server https://acme.example.com/directory." + assert res == m + assert cb_client.acme.deactivate_registration.called is False + + +class MakeOrVerifyNeededDirs(test_util.ConfigTestCase): + """Tests for certbot._internal.main.make_or_verify_needed_dirs.""" + + @mock.patch("certbot._internal.main.util") + def test_it(self, mock_util): + main.make_or_verify_needed_dirs(self.config) + for core_dir in (self.config.config_dir, self.config.work_dir,): + mock_util.set_up_core_dir.assert_any_call( + core_dir, constants.CONFIG_DIRS_MODE, + self.config.strict_permissions + ) + + hook_dirs = (self.config.renewal_pre_hooks_dir, + self.config.renewal_deploy_hooks_dir, + self.config.renewal_post_hooks_dir,) + for hook_dir in hook_dirs: + # default mode of 755 is used + mock_util.make_or_verify_dir.assert_any_call( + hook_dir, strict=self.config.strict_permissions) + + +class EnhanceTest(test_util.ConfigTestCase): + """Tests for certbot._internal.main.enhance.""" + + def setUp(self): + super().setUp() + self.get_utility_patch = test_util.patch_display_util() + self.mock_get_utility = self.get_utility_patch.start() + self.mockinstaller = mock.MagicMock(spec=enhancements.AutoHSTSEnhancement) + + def tearDown(self): + self.get_utility_patch.stop() + + def _call(self, args): + plugins = disco.PluginsRegistry.find_all() + config = cli.prepare_and_parse_args(plugins, args) + + with mock.patch('certbot._internal.cert_manager.get_certnames') as mock_certs: + mock_certs.return_value = ['example.com'] + with mock.patch('certbot._internal.cert_manager.sans_for_certname') as mock_dom: + mock_dom.return_value = [san.DNSName('example.com')] + with mock.patch('certbot._internal.main._init_le_client') as mock_init: + mock_client = mock.MagicMock() + mock_client.config = config + mock_init.return_value = mock_client + main.enhance(config, plugins) + return mock_client # returns the client + + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.main.display_ops.choose_values') + @mock.patch('certbot._internal.main._find_sans_or_certname') + def test_selection_question(self, mock_find, mock_choose, mock_lineage, _rec): + mock_lineage.return_value = mock.MagicMock(chain_path="/tmp/nonexistent") + mock_choose.return_value = ['example.com'] + mock_find.return_value = (None, None) + with mock.patch('certbot._internal.main.plug_sel.pick_installer') as mock_pick: + self._call(['enhance', '--redirect']) + assert mock_pick.called + # Check that the message includes "enhancements" + assert "enhancements" in mock_pick.call_args[0][3] + + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.main.display_ops.choose_values') + @mock.patch('certbot._internal.main._find_sans_or_certname') + def test_selection_auth_warning(self, mock_find, mock_choose, mock_lineage, _rec): + mock_lineage.return_value = mock.MagicMock(chain_path="/tmp/nonexistent") + mock_choose.return_value = ["example.com"] + mock_find.return_value = (None, None) + with mock.patch('certbot._internal.main.plug_sel.pick_installer'): + with mock.patch('certbot._internal.main.plug_sel.logger.warning') as mock_log: + mock_client = self._call(['enhance', '-a', 'webroot', '--redirect']) + assert mock_log.called + assert "make sense" in mock_log.call_args[0][0] + assert mock_client.enhance_config.called + + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.main.display_ops.choose_values') + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + def test_enhance_config_call(self, _rec, mock_choose, mock_lineage): + mock_lineage.return_value = mock.MagicMock(chain_path="/tmp/nonexistent") + mock_choose.return_value = ["example.com"] + with mock.patch('certbot._internal.main.plug_sel.pick_installer'): + mock_client = self._call(['enhance', '--redirect', '--hsts']) + req_enh = ["redirect", "hsts"] + not_req_enh = ["uir"] + assert mock_client.enhance_config.called + assert all(getattr(mock_client.config, e) for e in req_enh) + assert not any(getattr(mock_client.config, e) for e in not_req_enh) + assert san.DNSName("example.com") in mock_client.enhance_config.call_args[0][0] + + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.main.display_ops.choose_values') + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + def test_enhance_noninteractive(self, _rec, mock_choose, mock_lineage): + mock_lineage.return_value = mock.MagicMock( + chain_path="/tmp/nonexistent") + mock_choose.return_value = ["example.com"] + with mock.patch('certbot._internal.main.plug_sel.pick_installer'): + mock_client = self._call(['enhance', '--redirect', + '--hsts', '--non-interactive']) + assert mock_client.enhance_config.called + assert mock_choose.called is False + + @mock.patch('certbot._internal.main.display_ops.choose_values') + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + def test_user_abort_domains(self, _rec, mock_choose): + mock_choose.return_value = [] + with mock.patch('certbot._internal.main.plug_sel.pick_installer'): + with pytest.raises(errors.Error): + self._call(['enhance', '--redirect', '--hsts']) + + def test_no_enhancements_defined(self): + with pytest.raises(errors.MisconfigurationError): + self._call(['enhance', '-a', 'null']) + + @mock.patch('certbot._internal.main.plug_sel.choose_configurator_plugins') + @mock.patch('certbot._internal.main.display_ops.choose_values') + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + def test_plugin_selection_error(self, _rec, mock_choose, mock_pick): + mock_choose.return_value = ["example.com"] + mock_pick.return_value = (None, None) + mock_pick.side_effect = errors.PluginSelectionError() + mock_client = self._call(['enhance', '--hsts']) + assert mock_client.enhance_config.called is False + + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.main.display_ops.choose_values') + @mock.patch('certbot._internal.main.plug_sel.pick_installer') + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @test_util.patch_display_util() + def test_enhancement_enable(self, _, _rec, mock_inst, mock_choose, mock_lineage): + mock_inst.return_value = self.mockinstaller + mock_choose.return_value = ["example.com", "another.tld"] + mock_lineage.return_value = mock.MagicMock(chain_path="/tmp/nonexistent") + self._call(['enhance', '--auto-hsts']) + assert self.mockinstaller.enable_autohsts.called + assert self.mockinstaller.enable_autohsts.call_args[0][1] == \ + ["example.com", "another.tld"] + + @mock.patch('certbot._internal.cert_manager.lineage_for_certname') + @mock.patch('certbot._internal.main.display_ops.choose_values') + @mock.patch('certbot._internal.main.plug_sel.pick_installer') + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @test_util.patch_display_util() + def test_enhancement_enable_not_supported(self, _, _rec, mock_inst, mock_choose, mock_lineage): + mock_inst.return_value = null.Installer(self.config, "null") + mock_choose.return_value = ["example.com", "another.tld"] + mock_lineage.return_value = mock.MagicMock(chain_path="/tmp/nonexistent") + with pytest.raises(errors.NotSupportedError): + self._call(['enhance', '--auto-hsts']) + + def test_enhancement_enable_conflict(self): + with pytest.raises(errors.Error): + self._call(['enhance', '--auto-hsts', '--hsts']) + + +class InstallTest(test_util.ConfigTestCase): + """Tests for certbot._internal.main.install.""" + + def setUp(self): + super().setUp() + self.mockinstaller = mock.MagicMock(spec=enhancements.AutoHSTSEnhancement) + + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @mock.patch('certbot._internal.main.plug_sel.pick_installer') + def test_install_enhancement_not_supported(self, mock_inst, _rec): + mock_inst.return_value = null.Installer(self.config, "null") + plugins = disco.PluginsRegistry.find_all() + self.config.auto_hsts = True + self.config.certname = "nonexistent" + with pytest.raises(errors.NotSupportedError): + main.install(self.config, plugins) + + @mock.patch('certbot._internal.main.plug_sel.record_chosen_plugins') + @mock.patch('certbot._internal.main.plug_sel.pick_installer') + def test_install_enhancement_no_certname(self, mock_inst, _rec): + mock_inst.return_value = self.mockinstaller + plugins = disco.PluginsRegistry.find_all() + self.config.auto_hsts = True + self.config.certname = None + self.config.key_path = "/tmp/nonexistent" + self.config.cert_path = "/tmp/nonexistent" + with pytest.raises(errors.ConfigurationError): + main.install(self.config, plugins) + + +class ReportNewCertTest(unittest.TestCase): + """Tests for certbot._internal.main._report_new_cert and + certbot._internal.main._csr_report_new_cert. + """ + + def setUp(self): + self.notify_patch = mock.patch('certbot._internal.main.display_util.notify') + self.mock_notify = self.notify_patch.start() + + self.notafter_patch = mock.patch('certbot._internal.main.crypto_util.notAfter') + self.mock_notafter = self.notafter_patch.start() + self.mock_notafter.return_value = datetime.datetime(1970, 1, 1, 0, 0) + + def tearDown(self): + self.notify_patch.stop() + self.notafter_patch.stop() + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.main import _report_new_cert + return _report_new_cert(*args, **kwargs) + + @classmethod + def _call_csr(cls, *args, **kwargs): + from certbot._internal.main import _csr_report_new_cert + return _csr_report_new_cert(*args, **kwargs) + + def test_report_dry_run(self): + self._call(mock.Mock(dry_run=True), None, None, None) + self.mock_notify.assert_called_with("The dry run was successful.") + + def test_csr_report_dry_run(self): + self._call_csr(mock.Mock(dry_run=True), None, None, None) + self.mock_notify.assert_called_with("The dry run was successful.") + + def test_report_no_paths(self): + with pytest.raises(AssertionError): + self._call(mock.Mock(dry_run=False), None, None, None) + + with pytest.raises(AssertionError): + self._call_csr(mock.Mock(dry_run=False), None, None, None) + + def test_report(self): + self._call(mock.Mock(dry_run=False), + '/path/to/cert.pem', '/path/to/fullchain.pem', + '/path/to/privkey.pem') + + self.mock_notify.assert_called_with( + '\nSuccessfully received certificate.\n' + 'Certificate is saved at: /path/to/fullchain.pem\n' + 'Key is saved at: /path/to/privkey.pem\n' + 'This certificate expires on 1970-01-01.\n' + 'These files will be updated when the certificate renews.\n' + 'Certbot has set up a scheduled task to automatically renew this ' + 'certificate in the background.' + ) + + def test_report_no_key(self): + self._call(mock.Mock(dry_run=False), + '/path/to/cert.pem', '/path/to/fullchain.pem', + None) + + self.mock_notify.assert_called_with( + '\nSuccessfully received certificate.\n' + 'Certificate is saved at: /path/to/fullchain.pem\n' + 'This certificate expires on 1970-01-01.\n' + 'These files will be updated when the certificate renews.\n' + 'Certbot has set up a scheduled task to automatically renew this ' + 'certificate in the background.' + ) + + def test_report_no_preconfigured_renewal(self): + self._call(mock.Mock(dry_run=False, preconfigured_renewal=False), + '/path/to/cert.pem', '/path/to/fullchain.pem', + '/path/to/privkey.pem') + + self.mock_notify.assert_called_with( + '\nSuccessfully received certificate.\n' + 'Certificate is saved at: /path/to/fullchain.pem\n' + 'Key is saved at: /path/to/privkey.pem\n' + 'This certificate expires on 1970-01-01.\n' + 'These files will be updated when the certificate renews.' + ) + + def test_csr_report(self): + self._call_csr(mock.Mock(dry_run=False), '/path/to/cert.pem', + '/path/to/chain.pem', '/path/to/fullchain.pem') + + self.mock_notify.assert_called_with( + '\nSuccessfully received certificate.\n' + 'Certificate is saved at: /path/to/cert.pem\n' + 'Intermediate CA chain is saved at: /path/to/chain.pem\n' + 'Full certificate chain is saved at: /path/to/fullchain.pem\n' + 'This certificate expires on 1970-01-01.' + ) + + def test_manual_no_hooks_report(self): + """Shouldn't get a message about autorenewal if no --manual-auth-hook""" + self._call(mock.Mock(dry_run=False, authenticator='manual', manual_auth_hook=None), + '/path/to/cert.pem', '/path/to/fullchain.pem', + '/path/to/privkey.pem') + + self.mock_notify.assert_called_with( + '\nSuccessfully received certificate.\n' + 'Certificate is saved at: /path/to/fullchain.pem\n' + 'Key is saved at: /path/to/privkey.pem\n' + 'This certificate expires on 1970-01-01.\n' + 'These files will be updated when the certificate renews.' + ) + + +class ReportNextStepsTest(unittest.TestCase): + """Tests for certbot._internal.main._report_next_steps""" + + def setUp(self): + self.config = mock.MagicMock( + cert_name="example.com", preconfigured_renewal=True, + csr=None, authenticator="nginx", manual_auth_hook=None) + notify_patch = mock.patch('certbot._internal.main.display_util.notify') + self.mock_notify = notify_patch.start() + self.addCleanup(notify_patch.stop) + self.old_stdout = sys.stdout + sys.stdout = io.StringIO() + + def tearDown(self): + sys.stdout = self.old_stdout + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.main import _report_next_steps + _report_next_steps(*args, **kwargs) + + def _output(self) -> str: + assert self.mock_notify.call_count == 2 + assert self.mock_notify.call_args_list[0][0][0] == 'NEXT STEPS:' + return self.mock_notify.call_args_list[1][0][0] + + def test_report(self): + """No steps for a normal renewal""" + self.config.authenticator = "manual" + self.config.manual_auth_hook = "/bin/true" + self._call(self.config, None, None) + self.mock_notify.assert_not_called() + + def test_csr_report(self): + """--csr requires manual renewal""" + self.config.csr = "foo.csr" + self._call(self.config, None, None) + assert "--csr will not be renewed" in self._output() + + def test_manual_no_hook_renewal(self): + """--manual without a hook requires manual renewal""" + self.config.authenticator = "manual" + self._call(self.config, None, None) + assert "--manual certificates requires" in self._output() + + def test_no_preconfigured_renewal(self): + """No --preconfigured-renewal needs manual cron setup""" + self.config.preconfigured_renewal = False + self._call(self.config, None, None) + assert "https://certbot.org/renewal-setup" in self._output() + + +class UpdateAccountTest(test_util.ConfigTestCase): + """Tests for certbot._internal.main.update_account""" + + def setUp(self): + patches = { + 'account': mock.patch('certbot._internal.main.account'), + 'atexit': mock.patch('certbot.util.atexit'), + 'client': mock.patch('certbot._internal.main.client'), + 'determine_account': mock.patch('certbot._internal.main._determine_account'), + 'notify': mock.patch('certbot._internal.main.display_util.notify'), + 'prepare_sub': mock.patch('certbot._internal.eff.prepare_subscription'), + 'util': test_util.patch_display_util() + } + self.mocks = { k: patches[k].start() for k in patches } + for patch in patches.values(): + self.addCleanup(patch.stop) + + return super().setUp() + + def _call(self, args): + with mock.patch('certbot._internal.main.sys.stdout'), \ + mock.patch('certbot._internal.main.sys.stderr'): + args = ['--config-dir', self.config.config_dir, + '--work-dir', self.config.work_dir, + '--logs-dir', self.config.logs_dir, '--text'] + args + return main.main(args[:]) # NOTE: parser can alter its args! + + def _prepare_mock_account(self): + mock_storage = mock.MagicMock() + mock_account = mock.MagicMock() + mock_regr = mock.MagicMock() + mock_storage.find_all.return_value = [mock_account] + self.mocks['account'].AccountFileStorage.return_value = mock_storage + mock_account.regr.body = mock_regr.body + self.mocks['determine_account'].return_value = (mock_account, mock.MagicMock()) + return (mock_account, mock_storage, mock_regr) + + def _test_update_no_contact(self, args): + """Utility to assert that email removal is handled correctly""" + (_, mock_storage, mock_regr) = self._prepare_mock_account() + result = self._call(args) + # When update succeeds, the return value of update_account() is None + assert result is None + # We submitted a registration to the server + assert self.mocks['client'].Client().acme.update_registration.call_count == 1 + mock_regr.body.update.assert_called_with(contact=()) + # We got an update from the server and persisted it + assert mock_storage.update_regr.call_count == 1 + # We should have notified the user + self.mocks['notify'].assert_called_with( + 'Any contact information associated with this account has been removed.' + ) + # We should not have called subscription because there's no email + self.mocks['prepare_sub'].assert_not_called() + + def test_no_existing_accounts(self): + """Test that no existing account is handled correctly""" + mock_storage = mock.MagicMock() + mock_storage.find_all.return_value = [] + self.mocks['account'].AccountFileStorage.return_value = mock_storage + assert self._call(['update_account', '--email', 'user@example.org']) == \ + 'Could not find an existing account for server' \ + ' https://acme-v02.api.letsencrypt.org/directory.' + + def test_update_account_remove_email(self): + """Test that --register-unsafely-without-email is handled as no email""" + self._test_update_no_contact(['update_account', '--register-unsafely-without-email']) + + def test_update_account_empty_email(self): + """Test that providing an empty email is handled as no email""" + self._test_update_no_contact(['update_account', '-m', '']) + + @mock.patch('certbot._internal.main.display_ops.get_email') + def test_update_account_with_email(self, mock_email): + """Test that updating with a singular email is handled correctly""" + mock_email.return_value = 'user@example.com' + (_, mock_storage, _) = self._prepare_mock_account() + mock_client = mock.MagicMock() + self.mocks['client'].Client.return_value = mock_client + + result = self._call(['update_account']) + # None if registration succeeds + assert result is None + # We should have updated the server + assert mock_client.acme.update_registration.call_count == 1 + # We should have updated the account on disk + assert mock_storage.update_regr.call_count == 1 + # Subscription should have been prompted + assert self.mocks['prepare_sub'].call_count == 1 + # Should have printed the email + self.mocks['notify'].assert_called_with( + 'Your e-mail address was updated to user@example.com.') + + def test_update_account_with_multiple_emails(self): + """Test that multiple email addresses are handled correctly""" + (_, mock_storage, mock_regr) = self._prepare_mock_account() + assert self._call(['update_account', '-m', 'user@example.com,user@example.org']) is None + mock_regr.body.update.assert_called_with( + contact=['mailto:user@example.com', 'mailto:user@example.org'] + ) + assert mock_storage.update_regr.call_count == 1 + self.mocks['notify'].assert_called_with( + 'Your e-mail address was updated to user@example.com,user@example.org.') + + +class ShowAccountTest(test_util.ConfigTestCase): + """Tests for certbot._internal.main.show_account""" + + def setUp(self): + patches = { + 'account': mock.patch('certbot._internal.main.account'), + 'atexit': mock.patch('certbot.util.atexit'), + 'client': mock.patch('certbot._internal.main.client'), + 'determine_account': mock.patch('certbot._internal.main._determine_account'), + 'notify': mock.patch('certbot._internal.main.display_util.notify'), + 'util': test_util.patch_display_util() + } + self.mocks = { k: patches[k].start() for k in patches } + for patch in patches.values(): + self.addCleanup(patch.stop) + + return super().setUp() + + def _call(self, args): + with mock.patch('certbot._internal.main.sys.stdout'), \ + mock.patch('certbot._internal.main.sys.stderr'): + args = ['--config-dir', self.config.config_dir, + '--work-dir', self.config.work_dir, + '--logs-dir', self.config.logs_dir, '--text'] + args + return main.main(args[:]) # NOTE: parser can alter its args! + + def _prepare_mock_account(self): + mock_storage = mock.MagicMock() + mock_account = mock.MagicMock() + mock_regr = mock.MagicMock() + mock_storage.find_all.return_value = [mock_account] + self.mocks['account'].AccountFileStorage.return_value = mock_storage + mock_account.regr.body = mock_regr.body + mock_account.key.thumbprint.return_value = b'foobarbaz' + self.mocks['determine_account'].return_value = (mock_account, mock.MagicMock()) + + def _test_show_account(self, contact): + self._prepare_mock_account() + mock_client = mock.MagicMock() + mock_regr = mock.MagicMock() + mock_regr.body.contact = contact + mock_regr.uri = 'https://www.letsencrypt-demo.org/acme/reg/1' + mock_client.acme.query_registration.return_value = mock_regr + self.mocks['client'].Client.return_value = mock_client + + args = ['show_account'] + + self._call(args) + + assert mock_client.acme.query_registration.call_count == 1 + + def test_no_existing_accounts(self): + """Test that no existing account is handled correctly""" + mock_storage = mock.MagicMock() + mock_storage.find_all.return_value = [] + self.mocks['account'].AccountFileStorage.return_value = mock_storage + assert self._call(['show_account']) == \ + 'Could not find an existing account for server' \ + ' https://acme-v02.api.letsencrypt.org/directory.' + + def test_no_existing_client(self): + """Test that issues with the ACME client are handled correctly""" + self._prepare_mock_account() + mock_client = mock.MagicMock() + mock_client.acme = None + self.mocks['client'].Client.return_value = mock_client + try: + self._call(['show_account']) + except errors.Error as e: + assert 'ACME client is not set.' == str(e) + + def test_no_contacts(self): + self._test_show_account(()) + + assert self.mocks['notify'].call_count == 1 + self.mocks['notify'].assert_has_calls([ + mock.call('Account details for server https://acme-v02.api.letsencr' + 'ypt.org/directory:\n Account URL: https://www.letsencry' + 'pt-demo.org/acme/reg/1\n Account Thumbprint: Zm9vYmFyYmF6\n' + ' Email contact: none')]) + + def test_single_email(self): + contact = ('mailto:foo@example.com',) + self._test_show_account(contact) + + assert self.mocks['notify'].call_count == 1 + self.mocks['notify'].assert_has_calls([ + mock.call('Account details for server https://acme-v02.api.letsencr' + 'ypt.org/directory:\n Account URL: https://www.letsencry' + 'pt-demo.org/acme/reg/1\n Account Thumbprint: Zm9vYmFyYmF6' + '\n Email contact: foo@example.com')]) + + def test_double_email(self): + contact = ('mailto:foo@example.com', 'mailto:bar@example.com') + self._test_show_account(contact) + + assert self.mocks['notify'].call_count == 1 + self.mocks['notify'].assert_has_calls([ + mock.call('Account details for server https://acme-v02.api.letsencr' + 'ypt.org/directory:\n Account URL: https://www.letsencry' + 'pt-demo.org/acme/reg/1\n Account Thumbprint: Zm9vYmFyYmF6\n' + ' Email contacts: foo@example.com, bar@example.com')]) + + +class TestLockOrder: + """Tests that Certbot's directory locks were acquired in the right order.""" + EXPECTED_ERROR_TYPE = errors.Error + EXPECTED_ERROR_STR = 'Expected TestLockOrder error' + # This regex is needed because certbot renew captures raised errors and + # raises its own. + EXPECTED_ERROR_STR_REGEX = f'{EXPECTED_ERROR_STR}|1 renew failure' + + @pytest.fixture + def mock_lock_dir(self): + with mock.patch('certbot._internal.lock.lock_dir') as mock_lock_dir: + yield mock_lock_dir + + @contextlib.contextmanager + def mock_plugin_prepare(self, authenticator_dir, installer_dir, mock_lock_dir, subcommand): + """Patches plugin prepare to call mock_lock_dir and raise the expected error.""" + def authenticator_lock(unused_self): + mock_lock_dir(authenticator_dir) + raise self.EXPECTED_ERROR_TYPE(self.EXPECTED_ERROR_STR) + + def installer_lock(unused_self): + mock_lock_dir(installer_dir) + # Unless an installer isn't needed (e.g. certbot install), we + # expect the authenticator to raise the expected error because it + # is prepared last. See + # https://github.com/certbot/certbot/blob/7a6752a68ed77e73c2b29ab20d3ca8927f4fa7b0/certbot/certbot/_internal/plugins/selection.py#L246-L249 + if subcommand == 'install': + raise self.EXPECTED_ERROR_TYPE(self.EXPECTED_ERROR_STR) + + with mock.patch.object(standalone.Authenticator, 'prepare', authenticator_lock): + with mock.patch.object(null.Installer, 'prepare', installer_lock): + yield + + @pytest.fixture(params='certonly install renew run'.split()) + def args_and_lock_order(self, mock_lock_dir, request, tmp_path): + """Sets up Certbot with args and mocks to error after acquiring the last lock. + + This fixture yields the CLI arguments that should be given to Certbot + and the expected order of directories to be locked. An error is raised + after acquiring the last lock just as a means of stopping Certbot's + execution. + + """ + # select directories + authenticator_dir = str(tmp_path / 'authenticator') + config_dir = str(tmp_path / 'config') + installer_dir = str(tmp_path / 'installer') + logs_dir = str(tmp_path / 'logs') + work_dir = str(tmp_path / 'work') + + # prepare args and lineage + subcommand = request.param + args = [subcommand, '-a', 'standalone', '-i', 'null', '--no-random-sleep-on-renew', + '--config-dir', config_dir, '--logs-dir', logs_dir, '--work-dir', + work_dir] + test_util.make_lineage(config_dir, 'sample-renewal.conf') + + with self.mock_plugin_prepare(authenticator_dir, installer_dir, mock_lock_dir, subcommand): + lock_order = [logs_dir, config_dir, work_dir, installer_dir] + if subcommand == 'install': + yield args, lock_order + else: + # We expect the installer to be prepared even for certonly + # because an installer was requested on the command line. + yield args, lock_order + [authenticator_dir] + + def test_lock_order(self, args_and_lock_order, mock_lock_dir): + args, lock_order = args_and_lock_order + with pytest.raises(self.EXPECTED_ERROR_TYPE, match=self.EXPECTED_ERROR_STR_REGEX): + main.main(args) + assert mock_lock_dir.call_count == len(lock_order) + for call, locked_dir in zip(mock_lock_dir.call_args_list, lock_order): + assert call[0][0] == locked_dir + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/ocsp_test.py b/certbot/src/certbot/_internal/tests/ocsp_test.py new file mode 100644 index 00000000000..7beb1ff0694 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/ocsp_test.py @@ -0,0 +1,249 @@ +"""Tests for ocsp.py""" +# pylint: disable=protected-access +import contextlib +from datetime import datetime +from datetime import timedelta +from datetime import timezone +import sys +import unittest +from unittest import mock + +from cryptography import x509 +from cryptography.exceptions import InvalidSignature +from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import hashes +from cryptography.x509 import ocsp as ocsp_lib +import pytest + +from certbot import crypto_util +from certbot.tests import util as test_util + + +class OSCPTestCryptography(unittest.TestCase): + """ + OCSP revokation tests using Cryptography >= 2.4.0 + """ + + def setUp(self): + from certbot._internal import ocsp + self.checker = ocsp.RevocationChecker() + self.cert_path = test_util.vector_path('ocsp_certificate.pem') + self.chain_path = test_util.vector_path('ocsp_issuer_certificate.pem') + self.cert_obj = mock.MagicMock() + self.cert_obj.cert_path = self.cert_path + self.cert_obj.chain_path = self.chain_path + now = datetime.now(timezone.utc) + self.mock_notAfter = mock.patch('certbot._internal.ocsp.crypto_util.notAfter', + return_value=now + timedelta(hours=2)) + self.mock_notAfter.start() + # Ensure the mock.patch is stopped even if test raises an exception + self.addCleanup(self.mock_notAfter.stop) + + @mock.patch('certbot._internal.ocsp._determine_ocsp_server') + @mock.patch('certbot._internal.ocsp._check_ocsp_cryptography') + def test_ensure_cryptography_toggled(self, mock_check, mock_determine): + mock_determine.return_value = ('http://example.com', 'example.com') + self.checker.ocsp_revoked(self.cert_obj) + + mock_check.assert_called_once_with(self.cert_path, self.chain_path, 'http://example.com', 10) + + def test_revoke(self): + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, ocsp_lib.OCSPResponseStatus.SUCCESSFUL): + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked + + def test_responder_is_issuer(self): + issuer = x509.load_pem_x509_certificate( + test_util.load_vector('ocsp_issuer_certificate.pem'), default_backend()) + + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, + ocsp_lib.OCSPResponseStatus.SUCCESSFUL) as mocks: + # OCSP response with ResponseID as Name + mocks['mock_response'].return_value.responder_name = issuer.subject + mocks['mock_response'].return_value.responder_key_hash = None + self.checker.ocsp_revoked(self.cert_obj) + # OCSP response with ResponseID as KeyHash + key_hash = x509.SubjectKeyIdentifier.from_public_key(issuer.public_key()).digest + mocks['mock_response'].return_value.responder_name = None + mocks['mock_response'].return_value.responder_key_hash = key_hash + self.checker.ocsp_revoked(self.cert_obj) + + # Here responder and issuer are the same. So only the signature of the OCSP + # response is checked (using the issuer/responder public key). + assert mocks['mock_check'].call_count == 2 + assert mocks['mock_check'].call_args_list[0][0][0].public_numbers() == \ + issuer.public_key().public_numbers() + assert mocks['mock_check'].call_args_list[1][0][0].public_numbers() == \ + issuer.public_key().public_numbers() + + def test_responder_is_authorized_delegate(self): + issuer = x509.load_pem_x509_certificate( + test_util.load_vector('ocsp_issuer_certificate.pem'), default_backend()) + responder = x509.load_pem_x509_certificate( + test_util.load_vector('ocsp_responder_certificate.pem'), default_backend()) + + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, + ocsp_lib.OCSPResponseStatus.SUCCESSFUL) as mocks: + # OCSP response with ResponseID as Name + mocks['mock_response'].return_value.responder_name = responder.subject + mocks['mock_response'].return_value.responder_key_hash = None + self.checker.ocsp_revoked(self.cert_obj) + # OCSP response with ResponseID as KeyHash + key_hash = x509.SubjectKeyIdentifier.from_public_key(responder.public_key()).digest + mocks['mock_response'].return_value.responder_name = None + mocks['mock_response'].return_value.responder_key_hash = key_hash + self.checker.ocsp_revoked(self.cert_obj) + + # Here responder and issuer are not the same. Two signatures will be checked then, + # first to verify the responder cert (using the issuer public key), second to + # to verify the OCSP response itself (using the responder public key). + assert mocks['mock_check'].call_count == 4 + assert mocks['mock_check'].call_args_list[0][0][0].public_numbers() == \ + issuer.public_key().public_numbers() + assert mocks['mock_check'].call_args_list[1][0][0].public_numbers() == \ + responder.public_key().public_numbers() + assert mocks['mock_check'].call_args_list[2][0][0].public_numbers() == \ + issuer.public_key().public_numbers() + assert mocks['mock_check'].call_args_list[3][0][0].public_numbers() == \ + responder.public_key().public_numbers() + + def test_revoke_resiliency(self): + # Server return an invalid HTTP response + with _ocsp_mock(ocsp_lib.OCSPCertStatus.UNKNOWN, ocsp_lib.OCSPResponseStatus.SUCCESSFUL, + http_status_code=400): + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + # OCSP response in invalid + with _ocsp_mock(ocsp_lib.OCSPCertStatus.UNKNOWN, ocsp_lib.OCSPResponseStatus.UNAUTHORIZED): + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + # OCSP response is valid, but certificate status is unknown + with _ocsp_mock(ocsp_lib.OCSPCertStatus.UNKNOWN, ocsp_lib.OCSPResponseStatus.SUCCESSFUL): + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + # The OCSP response says that the certificate is revoked, but certificate + # does not contain the OCSP extension. + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, ocsp_lib.OCSPResponseStatus.SUCCESSFUL): + with mock.patch('cryptography.x509.Extensions.get_extension_for_class', + side_effect=x509.ExtensionNotFound( + 'Not found', x509.AuthorityInformationAccessOID.OCSP)): + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + # OCSP response uses an unsupported signature. + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, ocsp_lib.OCSPResponseStatus.SUCCESSFUL, + check_signature_side_effect=UnsupportedAlgorithm('foo')): + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + # OSCP signature response is invalid. + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, ocsp_lib.OCSPResponseStatus.SUCCESSFUL, + check_signature_side_effect=InvalidSignature('foo')): + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + # Assertion error on OCSP response validity + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, ocsp_lib.OCSPResponseStatus.SUCCESSFUL, + check_signature_side_effect=AssertionError('foo')): + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + # No responder cert in OCSP response + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, + ocsp_lib.OCSPResponseStatus.SUCCESSFUL) as mocks: + mocks['mock_response'].return_value.certificates = [] + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + # Responder cert is not signed by certificate issuer + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, + ocsp_lib.OCSPResponseStatus.SUCCESSFUL) as mocks: + cert = mocks['mock_response'].return_value.certificates[0] + mocks['mock_response'].return_value.certificates[0] = mock.Mock( + issuer='fake', subject=cert.subject) + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + with _ocsp_mock(ocsp_lib.OCSPCertStatus.REVOKED, ocsp_lib.OCSPResponseStatus.SUCCESSFUL): + # This mock is necessary to avoid the first call contained in _determine_ocsp_server + # of the method cryptography.x509.Extensions.get_extension_for_class. + with mock.patch('certbot._internal.ocsp._determine_ocsp_server') as mock_server: + mock_server.return_value = ('https://example.com', 'example.com') + with mock.patch('cryptography.x509.Extensions.get_extension_for_class', + side_effect=x509.ExtensionNotFound( + 'Not found', x509.AuthorityInformationAccessOID.OCSP)): + revoked = self.checker.ocsp_revoked(self.cert_obj) + assert revoked is False + + +class TestDeprecation: + """Tests related to the deprecation of certbot.ocsp. + + These tests can be deleted after this module is removed from Certbot. + + """ + def test_deprecation_warning(self): + with pytest.warns(DeprecationWarning, match='certbot.ocsp is deprecated'): + import certbot.ocsp # noqa: F401 + + def test_no_changes(self): + from certbot._internal import ocsp + expected_hash = '4f595b3c6e63749af1f71b5b4890b94e04734bb75f8bff95cf7d7a7e4752d5c1' + failure_message = ('Despite being prefixed by _internal, certbot._internal.ocsp is still ' + 'part of our public API while certbot.ocsp exists. You are free to make changes to ' + 'this file and update the hash in this test however, please be sure your changes do ' + 'not affect the API of the certbot.ocsp module.') + assert crypto_util.sha256sum(ocsp.__file__) == expected_hash, failure_message + + +@contextlib.contextmanager +def _ocsp_mock(certificate_status, response_status, + http_status_code=200, check_signature_side_effect=None): + with mock.patch('certbot._internal.ocsp.ocsp.load_der_ocsp_response') as mock_response: + mock_response.return_value = _construct_mock_ocsp_response( + certificate_status, response_status) + with mock.patch('certbot._internal.ocsp.requests.post') as mock_post: + mock_post.return_value = mock.Mock(status_code=http_status_code) + with mock.patch('certbot._internal.ocsp.crypto_util.verify_signed_payload') \ + as mock_check: + if check_signature_side_effect: + mock_check.side_effect = check_signature_side_effect + yield { + 'mock_response': mock_response, + 'mock_post': mock_post, + 'mock_check': mock_check, + } + + +def _construct_mock_ocsp_response(certificate_status, response_status): + cert = x509.load_pem_x509_certificate( + test_util.load_vector('ocsp_certificate.pem'), default_backend()) + issuer = x509.load_pem_x509_certificate( + test_util.load_vector('ocsp_issuer_certificate.pem'), default_backend()) + responder = x509.load_pem_x509_certificate( + test_util.load_vector('ocsp_responder_certificate.pem'), default_backend()) + builder = ocsp_lib.OCSPRequestBuilder() + builder = builder.add_certificate(cert, issuer, hashes.SHA1()) + request = builder.build() + + return mock.Mock( + response_status=response_status, + certificate_status=certificate_status, + serial_number=request.serial_number, + issuer_key_hash=request.issuer_key_hash, + issuer_name_hash=request.issuer_name_hash, + responder_name=responder.subject, + certificates=[responder], + hash_algorithm=hashes.SHA1(), + next_update_utc=datetime.now(timezone.utc) + timedelta(days=1), + this_update_utc=datetime.now(timezone.utc) - timedelta(days=1), + signature_algorithm_oid=x509.oid.SignatureAlgorithmOID.RSA_WITH_SHA1, + ) + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/__init__.py b/certbot/src/certbot/_internal/tests/plugins/__init__.py new file mode 100644 index 00000000000..3cfcb5008d7 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/__init__.py @@ -0,0 +1 @@ +"""Certbot Plugins Tests""" diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/__init__.py b/certbot/src/certbot/_internal/tests/plugins/apache/__init__.py new file mode 100644 index 00000000000..97c96aaa3c8 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/__init__.py @@ -0,0 +1,7 @@ +"""certbot-apache tests""" + +import pytest + + +# Make sure we're only running these tests if our apache plugin dependencies are installed +pytest.importorskip("augeas") diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/NEEDED.txt b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/NEEDED.txt similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/NEEDED.txt rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/NEEDED.txt diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/apache-conf-test b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test similarity index 90% rename from certbot-apache/certbot_apache/tests/apache-conf-files/apache-conf-test rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test index 4838a6eeee4..3fccab04307 100755 --- a/certbot-apache/certbot_apache/tests/apache-conf-files/apache-conf-test +++ b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test @@ -51,9 +51,9 @@ function Cleanup() { # if our environment asks us to enable modules, do our best! if [ "$1" = --debian-modules ] ; then - sudo apt-get install -y apache2 - sudo apt-get install -y libapache2-mod-wsgi - sudo apt-get install -y libapache2-mod-macro + sudo DEBIAN_FRONTEND=noninteractive apt-get install -y apache2 + sudo DEBIAN_FRONTEND=noninteractive apt-get install -y libapache2-mod-wsgi-py3 + sudo DEBIAN_FRONTEND=noninteractive apt-get install -y libapache2-mod-macro for mod in ssl rewrite macro wsgi deflate userdir version mime setenvif ; do echo -n enabling $mod diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test-pebble.py b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test-pebble.py new file mode 100755 index 00000000000..837c67b9ec1 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test-pebble.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +""" +This executable script wraps the apache-conf-test bash script, in order to setup a pebble instance +before its execution. Directory URL is passed through the SERVER environment variable. +""" +import os +import subprocess +import sys + +from certbot_integration_tests.utils import acme_server + +SCRIPT_DIRNAME = os.path.dirname(__file__) + + +def main() -> int: + args = sys.argv[1:] + with acme_server.ACMEServer([], False) as acme_xdist: + environ = os.environ.copy() + environ['SERVER'] = acme_xdist['directory_url'] + command = [os.path.join(SCRIPT_DIRNAME, 'apache-conf-test')] + command.extend(args) + return subprocess.call(command, env=environ) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/failing/missing-double-quote-1724.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/failing/missing-double-quote-1724.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/failing/missing-double-quote-1724.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/failing/missing-double-quote-1724.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/failing/multivhost-1093.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/failing/multivhost-1093.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/failing/multivhost-1093.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/failing/multivhost-1093.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/failing/multivhost-1093b.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/failing/multivhost-1093b.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/failing/multivhost-1093b.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/failing/multivhost-1093b.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/1626-1531.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/1626-1531.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/1626-1531.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/1626-1531.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/README.modules b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/README.modules similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/README.modules rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/README.modules diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/anarcat-1531.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/anarcat-1531.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/anarcat-1531.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/anarcat-1531.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/comment-continuations-2050.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/comment-continuations-2050.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/comment-continuations-2050.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/comment-continuations-2050.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/drupal-errordocument-arg-1724.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/drupal-errordocument-arg-1724.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/drupal-errordocument-arg-1724.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/drupal-errordocument-arg-1724.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/drupal-htaccess-1531.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/drupal-htaccess-1531.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/drupal-htaccess-1531.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/drupal-htaccess-1531.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/escaped-space-arguments-2735.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/escaped-space-arguments-2735.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/escaped-space-arguments-2735.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/escaped-space-arguments-2735.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/example-1755.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/example-1755.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/example-1755.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/example-1755.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/example-ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/example-ssl.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/example-ssl.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/example-ssl.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/example.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/example.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/example.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/example.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/finalize-1243.apache2.conf.txt b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/finalize-1243.apache2.conf.txt similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/finalize-1243.apache2.conf.txt rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/finalize-1243.apache2.conf.txt diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/finalize-1243.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/finalize-1243.conf similarity index 98% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/finalize-1243.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/finalize-1243.conf index 0918e5669ca..dbfae3765f9 100644 --- a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/finalize-1243.conf +++ b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/finalize-1243.conf @@ -1,7 +1,7 @@ #LoadModule ssl_module modules/mod_ssl.so -Listen 443 - +Listen 4443 + # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/graphite-quote-1934.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/graphite-quote-1934.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/graphite-quote-1934.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/graphite-quote-1934.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/ipv6-1143.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/ipv6-1143.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/ipv6-1143.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/ipv6-1143.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/ipv6-1143b.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/ipv6-1143b.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/ipv6-1143b.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/ipv6-1143b.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/ipv6-1143c.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/ipv6-1143c.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/ipv6-1143c.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/ipv6-1143c.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/ipv6-1143d.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/ipv6-1143d.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/ipv6-1143d.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/ipv6-1143d.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/missing-quote-1724.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/missing-quote-1724.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/missing-quote-1724.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/missing-quote-1724.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/modmacro-1385.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/modmacro-1385.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/modmacro-1385.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/modmacro-1385.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/owncloud-1264.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/owncloud-1264.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/owncloud-1264.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/owncloud-1264.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/rewrite-quote-1960.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/rewrite-quote-1960.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/rewrite-quote-1960.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/rewrite-quote-1960.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/roundcube-1222.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/roundcube-1222.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/roundcube-1222.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/roundcube-1222.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/section-continuations-2525.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/section-continuations-2525.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/section-continuations-2525.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/section-continuations-2525.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/section-empty-continuations-2731.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/section-empty-continuations-2731.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/section-empty-continuations-2731.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/section-empty-continuations-2731.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/semacode-1598.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/semacode-1598.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/semacode-1598.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/semacode-1598.conf diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/sslrequire-wordlist-1827.htaccess b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/sslrequire-wordlist-1827.htaccess similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/sslrequire-wordlist-1827.htaccess rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/sslrequire-wordlist-1827.htaccess diff --git a/certbot-apache/certbot_apache/tests/apache-conf-files/passing/two-blocks-one-line-1693.conf b/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/two-blocks-one-line-1693.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/apache-conf-files/passing/two-blocks-one-line-1693.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/passing/two-blocks-one-line-1693.conf diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/augeasnode_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/augeasnode_test.py new file mode 100644 index 00000000000..c4600d99555 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/augeasnode_test.py @@ -0,0 +1,320 @@ +"""Tests for AugeasParserNode classes""" +import os +from unittest import mock + +import pytest + +from certbot import errors +from certbot._internal.plugins.apache import assertions +from certbot._internal.plugins.apache import augeasparser +from certbot._internal.tests.plugins.apache import util + + +def _get_augeasnode_mock(filepath): + """ Helper function for mocking out DualNode instance with an AugeasNode """ + def augeasnode_mock(metadata): + return augeasparser.AugeasBlockNode( + name=assertions.PASS, + ancestor=None, + filepath=filepath, + metadata=metadata) + return augeasnode_mock + + +class AugeasParserNodeTest(util.ApacheTest): # pylint: disable=too-many-public-methods + """Test AugeasParserNode using available test configurations""" + + def setUp(self): # pylint: disable=arguments-differ + super().setUp() + + with mock.patch( + "certbot._internal.plugins.apache.configurator.ApacheConfigurator.get_parsernode_root" + ) as mock_parsernode: + mock_parsernode.side_effect = _get_augeasnode_mock( + os.path.join(self.config_path, "apache2.conf")) + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir, + use_parsernode=True, + ) + self.vh_truth = util.get_vh_truth( + self.temp_dir, "debian_apache_2_4/multiple_vhosts") + + def test_save(self): + with mock.patch('certbot._internal.plugins.apache.parser.ApacheParser.save') as mock_save: + self.config.parser_root.save("A save message") + assert mock_save.called is True + assert mock_save.call_args[0][0] == "A save message" + + def test_unsaved_files(self): + with mock.patch('certbot._internal.plugins.apache.parser.ApacheParser.unsaved_files') as mock_uf: + mock_uf.return_value = ["first", "second"] + files = self.config.parser_root.unsaved_files() + assert files == ["first", "second"] + + def test_get_block_node_name(self): + from certbot._internal.plugins.apache.augeasparser import AugeasBlockNode + block = AugeasBlockNode( + name=assertions.PASS, + ancestor=None, + filepath=assertions.PASS, + metadata={"augeasparser": mock.Mock(), "augeaspath": "/files/anything"} + ) + testcases = { + "/some/path/FirstNode/SecondNode": "SecondNode", + "/some/path/FirstNode/SecondNode/": "SecondNode", + "OnlyPathItem": "OnlyPathItem", + "/files/etc/apache2/apache2.conf/VirtualHost": "VirtualHost", + "/Anything": "Anything", + } + for test in testcases: + # pylint: disable=protected-access + assert block._aug_get_name(test) == testcases[test] + + def test_find_blocks(self): + blocks = self.config.parser_root.find_blocks("VirtualHost", exclude=False) + assert len(blocks) == 12 + + def test_find_blocks_case_insensitive(self): + vhs = self.config.parser_root.find_blocks("VirtualHost") + vhs2 = self.config.parser_root.find_blocks("viRtuAlHoST") + assert len(vhs) == len(vhs2) + + def test_find_directive_found(self): + directives = self.config.parser_root.find_directives("Listen") + assert len(directives) == 1 + assert directives[0].filepath.endswith("/apache2/ports.conf") is True + assert directives[0].parameters == (u'80',) + + def test_find_directive_notfound(self): + directives = self.config.parser_root.find_directives("Nonexistent") + assert len(directives) == 0 + + def test_find_directive_from_block(self): + blocks = self.config.parser_root.find_blocks("virtualhost") + found = False + for vh in blocks: + if vh.filepath.endswith("sites-enabled/certbot.conf"): + servername = vh.find_directives("servername") + assert servername[0].parameters[0] == "certbot.demo" + found = True + assert found is True + + def test_find_comments(self): + rootcomment = self.config.parser_root.find_comments( + "This is the main Apache server configuration file. " + ) + assert len(rootcomment) == 1 + assert rootcomment[0].filepath.endswith( + "debian_apache_2_4/multiple_vhosts/apache2/apache2.conf" + ) is True + + def test_set_parameters(self): + servernames = self.config.parser_root.find_directives("servername") + names: list[str] = [] + for servername in servernames: + names += servername.parameters + assert "going_to_set_this" not in names + servernames[0].set_parameters(["something", "going_to_set_this"]) + servernames = self.config.parser_root.find_directives("servername") + names = [] + for servername in servernames: + names += servername.parameters + assert "going_to_set_this" in names + + def test_set_parameters_atinit(self): + from certbot._internal.plugins.apache.augeasparser import AugeasDirectiveNode + servernames = self.config.parser_root.find_directives("servername") + setparam = "certbot._internal.plugins.apache.augeasparser.AugeasDirectiveNode.set_parameters" + with mock.patch(setparam) as mock_set: + AugeasDirectiveNode( + name=servernames[0].name, + parameters=["test", "setting", "these"], + ancestor=assertions.PASS, + metadata=servernames[0].metadata + ) + assert mock_set.called is True + assert mock_set.call_args_list[0][0][0] == \ + ["test", "setting", "these"] + + def test_set_parameters_delete(self): + # Set params + servername = self.config.parser_root.find_directives("servername")[0] + servername.set_parameters(["thisshouldnotexistpreviously", "another", + "third"]) + + # Delete params + servernames = self.config.parser_root.find_directives("servername") + found = False + for servername in servernames: + if "thisshouldnotexistpreviously" in servername.parameters: + assert len(servername.parameters) == 3 + servername.set_parameters(["thisshouldnotexistpreviously"]) + found = True + assert found is True + + # Verify params + servernames = self.config.parser_root.find_directives("servername") + found = False + for servername in servernames: + if "thisshouldnotexistpreviously" in servername.parameters: + assert len(servername.parameters) == 1 + servername.set_parameters(["thisshouldnotexistpreviously"]) + found = True + assert found is True + + def test_add_child_comment(self): + newc = self.config.parser_root.add_child_comment("The content") + comments = self.config.parser_root.find_comments("The content") + assert len(comments) == 1 + assert newc.metadata["augeaspath"] == \ + comments[0].metadata["augeaspath"] + assert newc.comment == comments[0].comment + + def test_delete_child(self): + listens = self.config.parser_root.find_directives("Listen") + assert len(listens) == 1 + self.config.parser_root.delete_child(listens[0]) + + listens = self.config.parser_root.find_directives("Listen") + assert len(listens) == 0 + + def test_delete_child_not_found(self): + listen = self.config.parser_root.find_directives("Listen")[0] + listen.metadata["augeaspath"] = "/files/something/nonexistent" + + with pytest.raises(errors.PluginError): + self.config.parser_root.delete_child(listen) + + def test_add_child_block(self): + nb = self.config.parser_root.add_child_block( + "NewBlock", + ["first", "second"] + ) + rpath, _, directive = nb.metadata["augeaspath"].rpartition("/") + assert rpath == \ + self.config.parser_root.metadata["augeaspath"] + assert directive.startswith("NewBlock") is True + + def test_add_child_block_beginning(self): + self.config.parser_root.add_child_block( + "Beginning", + position=0 + ) + parser = self.config.parser_root.parser + root_path = self.config.parser_root.metadata["augeaspath"] + # Get first child + first = parser.aug.match("{}/*[1]".format(root_path)) + assert first[0].endswith("Beginning") is True + + def test_add_child_block_append(self): + self.config.parser_root.add_child_block( + "VeryLast", + ) + parser = self.config.parser_root.parser + root_path = self.config.parser_root.metadata["augeaspath"] + # Get last child + last = parser.aug.match("{}/*[last()]".format(root_path)) + assert last[0].endswith("VeryLast") is True + + def test_add_child_block_append_alt(self): + self.config.parser_root.add_child_block( + "VeryLastAlt", + position=99999 + ) + parser = self.config.parser_root.parser + root_path = self.config.parser_root.metadata["augeaspath"] + # Get last child + last = parser.aug.match("{}/*[last()]".format(root_path)) + assert last[0].endswith("VeryLastAlt") is True + + def test_add_child_block_middle(self): + self.config.parser_root.add_child_block( + "Middle", + position=5 + ) + parser = self.config.parser_root.parser + root_path = self.config.parser_root.metadata["augeaspath"] + # Augeas indices start at 1 :( + middle = parser.aug.match("{}/*[6]".format(root_path)) + assert middle[0].endswith("Middle") is True + + def test_add_child_block_existing_name(self): + parser = self.config.parser_root.parser + root_path = self.config.parser_root.metadata["augeaspath"] + # There already exists a single VirtualHost in the base config + new_block = parser.aug.match("{}/VirtualHost[2]".format(root_path)) + assert len(new_block) == 0 + vh = self.config.parser_root.add_child_block( + "VirtualHost", + ) + new_block = parser.aug.match("{}/VirtualHost[2]".format(root_path)) + assert len(new_block) == 1 + assert vh.metadata["augeaspath"].endswith("VirtualHost[2]") is True + + def test_node_init_error_bad_augeaspath(self): + from certbot._internal.plugins.apache.augeasparser import AugeasBlockNode + parameters = { + "name": assertions.PASS, + "ancestor": None, + "filepath": assertions.PASS, + "metadata": { + "augeasparser": mock.Mock(), + "augeaspath": "/files/path/endswith/slash/" + } + } + with pytest.raises(errors.PluginError): + AugeasBlockNode(**parameters) + + def test_node_init_error_missing_augeaspath(self): + from certbot._internal.plugins.apache.augeasparser import AugeasBlockNode + parameters = { + "name": assertions.PASS, + "ancestor": None, + "filepath": assertions.PASS, + "metadata": { + "augeasparser": mock.Mock(), + } + } + with pytest.raises(errors.PluginError): + AugeasBlockNode(**parameters) + + def test_add_child_directive(self): + self.config.parser_root.add_child_directive( + "ThisWasAdded", + ["with", "parameters"], + position=0 + ) + dirs = self.config.parser_root.find_directives("ThisWasAdded") + assert len(dirs) == 1 + assert dirs[0].parameters == ("with", "parameters") + # The new directive was added to the very first line of the config + assert dirs[0].metadata["augeaspath"].endswith("[1]") is True + + def test_add_child_directive_exception(self): + with pytest.raises(errors.PluginError): + self.config.parser_root.add_child_directive("ThisRaisesErrorBecauseMissingParameters") + + def test_parsed_paths(self): + paths = self.config.parser_root.parsed_paths() + assert len(paths) == 6 + + def test_find_ancestors(self): + vhsblocks = self.config.parser_root.find_blocks("VirtualHost") + macro_test = False + nonmacro_test = False + for vh in vhsblocks: + if "/macro/" in vh.metadata["augeaspath"].lower(): + ancs = vh.find_ancestors("Macro") + assert len(ancs) == 1 + macro_test = True + else: + ancs = vh.find_ancestors("Macro") + assert len(ancs) == 0 + nonmacro_test = True + assert macro_test is True + assert nonmacro_test is True + + def test_find_ancestors_bad_path(self): + self.config.parser_root.metadata["augeaspath"] = "" + ancs = self.config.parser_root.find_ancestors("Anything") + assert len(ancs) == 0 diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/autohsts_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/autohsts_test.py new file mode 100644 index 00000000000..acb84d092b0 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/autohsts_test.py @@ -0,0 +1,182 @@ +# pylint: disable=too-many-lines +"""Test for certbot._internal.plugins.apache.configurator AutoHSTS functionality""" +import re +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot._internal.plugins.apache import constants +from certbot._internal.tests.plugins.apache import util + + +class AutoHSTSTest(util.ApacheTest): + """Tests for AutoHSTS feature""" + # pylint: disable=protected-access + + def setUp(self): # pylint: disable=arguments-differ + super().setUp() + + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir) + self.config.parser.modules["headers_module"] = None + self.config.parser.modules["mod_headers.c"] = None + self.config.parser.modules["ssl_module"] = None + self.config.parser.modules["mod_ssl.c"] = None + + self.vh_truth = util.get_vh_truth( + self.temp_dir, "debian_apache_2_4/multiple_vhosts") + + def get_autohsts_value(self, vh_path): + """ Get value from Strict-Transport-Security header """ + header_path = self.config.parser.find_dir("Header", None, vh_path) + if header_path: + pat = '(?:[ "]|^)(strict-transport-security)(?:[ "]|$)' + for head in header_path: + if re.search(pat, self.config.parser.aug.get(head).lower()): + return self.config.parser.aug.get( + head.replace("arg[3]", "arg[4]")) + return None # pragma: no cover + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.enable_mod") + def test_autohsts_enable_headers_mod(self, mock_enable, _restart): + self.config.parser.modules.pop("headers_module", None) + self.config.parser.modules.pop("mod_header.c", None) + self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) + assert mock_enable.called is True + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + def test_autohsts_deploy_already_exists(self, _restart): + self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) + with pytest.raises(errors.PluginEnhancementAlreadyPresent): + self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) + + @mock.patch("certbot._internal.plugins.apache.constants.AUTOHSTS_FREQ", 0) + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.prepare") + def test_autohsts_increase(self, mock_prepare, _mock_restart): + self.config._prepared = False + maxage = "\"max-age={0}\"" + initial_val = maxage.format(constants.AUTOHSTS_STEPS[0]) + inc_val = maxage.format(constants.AUTOHSTS_STEPS[1]) + + self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) + # Verify initial value + assert self.get_autohsts_value(self.vh_truth[7].path) == \ + initial_val + # Increase + self.config.update_autohsts(mock.MagicMock()) + # Verify increased value + assert self.get_autohsts_value(self.vh_truth[7].path) == \ + inc_val + assert mock_prepare.called is True + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator._autohsts_increase") + def test_autohsts_increase_noop(self, mock_increase, _restart): + maxage = "\"max-age={0}\"" + initial_val = maxage.format(constants.AUTOHSTS_STEPS[0]) + self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) + # Verify initial value + assert self.get_autohsts_value(self.vh_truth[7].path) == \ + initial_val + + self.config.update_autohsts(mock.MagicMock()) + # Freq not patched, so value shouldn't increase + assert mock_increase.called is False + + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + @mock.patch("certbot._internal.plugins.apache.constants.AUTOHSTS_FREQ", 0) + def test_autohsts_increase_no_header(self, _restart): + self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) + # Remove the header + dir_locs = self.config.parser.find_dir("Header", None, + self.vh_truth[7].path) + dir_loc = "/".join(dir_locs[0].split("/")[:-1]) + self.config.parser.aug.remove(dir_loc) + with pytest.raises(errors.PluginError): + self.config.update_autohsts(mock.MagicMock()) + + @mock.patch("certbot._internal.plugins.apache.constants.AUTOHSTS_FREQ", 0) + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + def test_autohsts_increase_and_make_permanent(self, _mock_restart): + maxage = "\"max-age={0}\"" + max_val = maxage.format(constants.AUTOHSTS_PERMANENT) + mock_lineage = mock.MagicMock() + mock_lineage.key_path = "/etc/apache2/ssl/key-certbot_15.pem" + self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com"]) + for i in range(len(constants.AUTOHSTS_STEPS)-1): + # Ensure that value is not made permanent prematurely + self.config.deploy_autohsts(mock_lineage) + assert self.get_autohsts_value(self.vh_truth[7].path) != \ + max_val + self.config.update_autohsts(mock.MagicMock()) + # Value should match pre-permanent increment step + cur_val = maxage.format(constants.AUTOHSTS_STEPS[i+1]) + assert self.get_autohsts_value(self.vh_truth[7].path) == \ + cur_val + # Ensure that the value is raised to max + assert self.get_autohsts_value(self.vh_truth[7].path) == \ + maxage.format(constants.AUTOHSTS_STEPS[-1]) + # Make permanent + self.config.deploy_autohsts(mock_lineage) + assert self.get_autohsts_value(self.vh_truth[7].path) == \ + max_val + + def test_autohsts_update_noop(self): + with mock.patch("certbot._internal.plugins.apache.configurator.time") as mock_time_module: + # Time mock is used to make sure that the execution does not + # continue when no autohsts entries exist in pluginstorage + self.config.update_autohsts(mock.MagicMock()) + assert not mock_time_module.time.called + + def test_autohsts_make_permanent_noop(self): + self.config.storage.put = mock.MagicMock() + self.config.deploy_autohsts(mock.MagicMock()) + # Make sure that the execution does not continue when no entries in store + assert self.config.storage.put.called is False + + @mock.patch("certbot._internal.plugins.apache.display_ops.select_vhost") + def test_autohsts_no_ssl_vhost(self, mock_select): + mock_select.return_value = self.vh_truth[0] + with mock.patch("certbot._internal.plugins.apache.configurator.logger.error") as mock_log: + with pytest.raises(errors.PluginError): + self.config.enable_autohsts(mock.MagicMock(), "invalid.example.com") + assert "Certbot was not able to find SSL" in mock_log.call_args[0][0] + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.add_vhost_id") + def test_autohsts_dont_enhance_twice(self, mock_id, _restart): + mock_id.return_value = "1234567" + self.config.enable_autohsts(mock.MagicMock(), ["ocspvhost.com", "ocspvhost.com"]) + assert mock_id.call_count == 1 + + def test_autohsts_remove_orphaned(self): + # pylint: disable=protected-access + self.config._autohsts_fetch_state() + self.config._autohsts["orphan_id"] = {"laststep": 0, "timestamp": 0} + + self.config._autohsts_save_state() + self.config.update_autohsts(mock.MagicMock()) + assert "orphan_id" not in self.config._autohsts + # Make sure it's removed from the pluginstorage file as well + self.config._autohsts = None + self.config._autohsts_fetch_state() + assert not self.config._autohsts + + def test_autohsts_make_permanent_vhost_not_found(self): + # pylint: disable=protected-access + self.config._autohsts_fetch_state() + self.config._autohsts["orphan_id"] = {"laststep": 999, "timestamp": 0} + self.config._autohsts_save_state() + with mock.patch("certbot._internal.plugins.apache.configurator.logger.error") as mock_log: + self.config.deploy_autohsts(mock.MagicMock()) + assert mock_log.called is True + assert "VirtualHost with id orphan_id was not" in mock_log.call_args[0][0] + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/centos_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/centos_test.py new file mode 100644 index 00000000000..87fc09fefc0 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/centos_test.py @@ -0,0 +1,242 @@ +"""Test for certbot._internal.plugins.apache.configurator for Centos overrides""" +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +from certbot._internal.plugins.apache import obj +from certbot._internal.plugins.apache import override_centos +from certbot._internal.tests.plugins.apache import util + + +def get_vh_truth(temp_dir, config_name): + """Return the ground truth for the specified directory.""" + prefix = os.path.join( + temp_dir, config_name, "httpd/conf.d") + + aug_pre = "/files" + prefix + vh_truth = [ + obj.VirtualHost( + os.path.join(prefix, "centos.example.com.conf"), + os.path.join(aug_pre, "centos.example.com.conf/VirtualHost"), + {obj.Addr.fromstring("*:80")}, + False, True, "centos.example.com"), + obj.VirtualHost( + os.path.join(prefix, "ssl.conf"), + os.path.join(aug_pre, "ssl.conf/VirtualHost"), + {obj.Addr.fromstring("_default_:443")}, + True, True, None) + ] + return vh_truth + + +class FedoraRestartTest(util.ApacheTest): + """Tests for Fedora specific self-signed certificate override""" + + def setUp(self): # pylint: disable=arguments-differ + test_dir = "centos7_apache/apache" + config_root = "centos7_apache/apache/httpd" + vhost_root = "centos7_apache/apache/httpd/conf.d" + super().setUp(test_dir=test_dir, + config_root=config_root, + vhost_root=vhost_root) + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir, + os_info="fedora_old") + self.vh_truth = get_vh_truth( + self.temp_dir, "centos7_apache/apache") + + def _run_fedora_test(self): + assert isinstance(self.config, override_centos.CentOSConfigurator) + with mock.patch("certbot.util.get_os_info") as mock_info: + mock_info.return_value = ["fedora", "28"] + self.config.config_test() + + def test_non_fedora_error(self): + c_test = "certbot._internal.plugins.apache.configurator.ApacheConfigurator.config_test" + with mock.patch(c_test) as mock_test: + mock_test.side_effect = errors.MisconfigurationError + with mock.patch("certbot.util.get_os_info") as mock_info: + mock_info.return_value = ["not_fedora"] + with pytest.raises(errors.MisconfigurationError): + self.config.config_test() + + def test_fedora_restart_error(self): + c_test = "certbot._internal.plugins.apache.configurator.ApacheConfigurator.config_test" + with mock.patch(c_test) as mock_test: + # First call raises error, second doesn't + mock_test.side_effect = [errors.MisconfigurationError, ''] + with mock.patch("certbot.util.run_script") as mock_run: + mock_run.side_effect = errors.SubprocessError + with pytest.raises(errors.MisconfigurationError): + self._run_fedora_test() + + def test_fedora_restart(self): + c_test = "certbot._internal.plugins.apache.configurator.ApacheConfigurator.config_test" + with mock.patch(c_test) as mock_test: + with mock.patch("certbot.util.run_script") as mock_run: + # First call raises error, second doesn't + mock_test.side_effect = [errors.MisconfigurationError, ''] + self._run_fedora_test() + assert mock_test.call_count == 2 + assert mock_run.call_args[0][0] == \ + ['systemctl', 'restart', 'httpd'] + + +class UseCorrectApacheExecutableTest(util.ApacheTest): + """Make sure the various CentOS/RHEL versions use the right httpd executable""" + + def setUp(self): # pylint: disable=arguments-differ + test_dir = "centos7_apache/apache" + config_root = "centos7_apache/apache/httpd" + vhost_root = "centos7_apache/apache/httpd/conf.d" + super().setUp(test_dir=test_dir, + config_root=config_root, + vhost_root=vhost_root) + + @mock.patch("certbot.util.get_os_info") + def test_old_centos_rhel_and_fedora(self, mock_get_os_info): + for os_info in [("centos", "7"), ("rhel", "7"), ("fedora", "28"), ("scientific", "6")]: + mock_get_os_info.return_value = os_info + config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir, + os_info="centos") + assert config.options.ctl == "apachectl" + assert config.options.bin == "httpd" + assert config.options.version_cmd == ["apachectl", "-v"] + assert config.options.restart_cmd == ["apachectl", "graceful"] + assert config.options.restart_cmd_alt == ["apachectl", "restart"] + assert config.options.conftest_cmd == ["apachectl", "configtest"] + assert config.options.get_defines_cmd == ["apachectl", "-t", "-D", "DUMP_RUN_CFG"] + assert config.options.get_includes_cmd == ["apachectl", "-t", "-D", "DUMP_INCLUDES"] + assert config.options.get_modules_cmd == ["apachectl", "-t", "-D", "DUMP_MODULES"] + + @mock.patch("certbot.util.get_os_info") + def test_new_rhel_derived(self, mock_get_os_info): + for os_info in [("centos", "9"), ("rhel", "9"), ("oracle", "9")]: + mock_get_os_info.return_value = os_info + config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir, + os_info=os_info[0]) + assert config.options.ctl == "apachectl" + assert config.options.bin == "httpd" + assert config.options.version_cmd == ["httpd", "-v"] + assert config.options.restart_cmd == ["apachectl", "graceful"] + assert config.options.restart_cmd_alt == ["apachectl", "restart"] + assert config.options.conftest_cmd == ["apachectl", "configtest"] + assert config.options.get_defines_cmd == ["httpd", "-t", "-D", "DUMP_RUN_CFG"] + assert config.options.get_includes_cmd == ["httpd", "-t", "-D", "DUMP_INCLUDES"] + assert config.options.get_modules_cmd == ["httpd", "-t", "-D", "DUMP_MODULES"] + + +class MultipleVhostsTestCentOS(util.ApacheTest): + """Multiple vhost tests for CentOS / RHEL family of distros""" + + @mock.patch("certbot.util.get_os_info") + def setUp(self, mock_get_os_info): # pylint: disable=arguments-differ + test_dir = "centos7_apache/apache" + config_root = "centos7_apache/apache/httpd" + vhost_root = "centos7_apache/apache/httpd/conf.d" + super().setUp(test_dir=test_dir, + config_root=config_root, + vhost_root=vhost_root) + mock_get_os_info.return_value = ("centos", "9") + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir, + os_info="centos") + self.vh_truth = get_vh_truth( + self.temp_dir, "centos7_apache/apache") + + def test_get_parser(self): + assert isinstance(self.config.parser, override_centos.CentOSParser) + + @mock.patch("certbot._internal.plugins.apache.apache_util._get_runtime_cfg") + def test_opportunistic_httpd_runtime_parsing(self, mock_get): + define_val = ( + 'Define: TEST1\n' + 'Define: TEST2\n' + 'Define: DUMP_RUN_CFG\n' + ) + mod_val = ( + 'Loaded Modules:\n' + ' mock_module (static)\n' + ' another_module (static)\n' + ) + def mock_get_cfg(command): + """Mock httpd process stdout""" + if command == ['httpd', '-t', '-D', 'DUMP_RUN_CFG']: + return define_val + elif command == ['httpd', '-t', '-D', 'DUMP_MODULES']: + return mod_val + return "" + mock_get.side_effect = mock_get_cfg + self.config.parser.modules = {} + self.config.parser.variables = {} + + with mock.patch("certbot.util.get_os_info") as mock_osi: + # Make sure we have the have the CentOS httpd constants + mock_osi.return_value = ("centos", "9") + self.config.parser.update_runtime_variables() + + assert mock_get.call_count == 3 + assert len(self.config.parser.modules) == 4 + assert len(self.config.parser.variables) == 2 + assert "TEST2" in self.config.parser.variables + assert "mod_another.c" in self.config.parser.modules + + def test_get_virtual_hosts(self): + """Make sure all vhosts are being properly found.""" + vhs = self.config.get_virtual_hosts() + assert len(vhs) == 2 + found = 0 + + for vhost in vhs: + for centos_truth in self.vh_truth: + if vhost == centos_truth: + found += 1 + break + else: + raise Exception("Missed: %s" % vhost) # pragma: no cover + assert found == 2 + + @mock.patch("certbot._internal.plugins.apache.apache_util._get_runtime_cfg") + def test_get_sysconfig_vars(self, mock_cfg): + """Make sure we read the sysconfig OPTIONS variable correctly""" + # Return nothing for the process calls + mock_cfg.return_value = "" + self.config.parser.sysconfig_filep = filesystem.realpath( + os.path.join(self.config.parser.root, "../sysconfig/httpd")) + self.config.parser.variables = {} + + with mock.patch("certbot.util.get_os_info") as mock_osi: + # Make sure we have the have the CentOS httpd constants + mock_osi.return_value = ("centos", "9") + self.config.parser.update_runtime_variables() + + assert "mock_define" in self.config.parser.variables + assert "mock_define_too" in self.config.parser.variables + assert "mock_value" in self.config.parser.variables + assert "TRUE" == self.config.parser.variables["mock_value"] + assert "MOCK_NOSEP" in self.config.parser.variables + assert "NOSEP_VAL" == self.config.parser.variables["NOSEP_TWO"] + + @mock.patch("certbot._internal.plugins.apache.configurator.util.run_script") + def test_alt_restart_works(self, mock_run_script): + mock_run_script.side_effect = [None, errors.SubprocessError, None] + self.config.restart() + assert mock_run_script.call_count == 3 + + @mock.patch("certbot._internal.plugins.apache.configurator.util.run_script") + def test_alt_restart_errors(self, mock_run_script): + mock_run_script.side_effect = [None, + errors.SubprocessError, + errors.SubprocessError] + with pytest.raises(errors.MisconfigurationError): + self.config.restart() + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/complex_parsing_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/complex_parsing_test.py new file mode 100644 index 00000000000..1e796ed0e86 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/complex_parsing_test.py @@ -0,0 +1,121 @@ +"""Tests for certbot._internal.plugins.apache.parser.""" +import sys + +import pytest + +from certbot import errors +from certbot.compat import os +from certbot._internal.tests.plugins.apache import util + + +class ComplexParserTest(util.ParserTest): + """Apache Parser Test.""" + + def setUp(self): # pylint: disable=arguments-differ + super().setUp("complex_parsing", "complex_parsing") + + self.setup_variables() + # This needs to happen after due to setup_variables not being run + # until after + self.parser.parse_modules() # pylint: disable=protected-access + + def setup_variables(self): + """Set up variables for parser.""" + self.parser.variables.update( + { + "COMPLEX": "", + "tls_port": "1234", + "fnmatch_filename": "test_fnmatch.conf", + "tls_port_str": "1234" + } + ) + + def test_filter_args_num(self): + """Note: This may also fail do to Include conf-enabled/ syntax.""" + matches = self.parser.find_dir("TestArgsDirective") + + assert len(self.parser.filter_args_num(matches, 1)) == 3 + assert len(self.parser.filter_args_num(matches, 2)) == 2 + assert len(self.parser.filter_args_num(matches, 3)) == 1 + + def test_basic_variable_parsing(self): + matches = self.parser.find_dir("TestVariablePort") + + assert len(matches) == 1 + assert self.parser.get_arg(matches[0]) == "1234" + + def test_basic_variable_parsing_quotes(self): + matches = self.parser.find_dir("TestVariablePortStr") + + assert len(matches) == 1 + assert self.parser.get_arg(matches[0]) == "1234" + + def test_invalid_variable_parsing(self): + del self.parser.variables["tls_port"] + + matches = self.parser.find_dir("TestVariablePort") + with pytest.raises(errors.PluginError): + self.parser.get_arg(matches[0]) + + def test_basic_ifdefine(self): + assert len(self.parser.find_dir("VAR_DIRECTIVE")) == 2 + assert len(self.parser.find_dir("INVALID_VAR_DIRECTIVE")) == 0 + + def test_basic_ifmodule(self): + assert len(self.parser.find_dir("MOD_DIRECTIVE")) == 2 + assert len(self.parser.find_dir("INVALID_MOD_DIRECTIVE")) == 0 + + def test_nested(self): + assert len(self.parser.find_dir("NESTED_DIRECTIVE")) == 3 + assert len(self.parser.find_dir("INVALID_NESTED_DIRECTIVE")) == 0 + + def test_load_modules(self): + """If only first is found, there is bad variable parsing.""" + assert "status_module" in self.parser.modules + assert "mod_status.c" in self.parser.modules + + # This is in an IfDefine + assert "ssl_module" in self.parser.modules + assert "mod_ssl.c" in self.parser.modules + + def verify_fnmatch(self, arg, hit=True): + """Test if Include was correctly parsed.""" + from certbot._internal.plugins.apache import parser + self.parser.add_dir(parser.get_aug_path(self.parser.loc["default"]), + "Include", [arg]) + if hit: + assert self.parser.find_dir("FNMATCH_DIRECTIVE") + else: + assert not self.parser.find_dir("FNMATCH_DIRECTIVE") + + # NOTE: Only run one test per function otherwise you will have + # inf recursion + def test_include(self): + self.verify_fnmatch("test_fnmatch.?onf") + + def test_include_complex(self): + self.verify_fnmatch("../complex_parsing/[te][te]st_*.?onf") + + def test_include_fullpath(self): + self.verify_fnmatch(os.path.join(self.config_path, + "test_fnmatch.conf")) + + def test_include_fullpath_trailing_slash(self): + self.verify_fnmatch(self.config_path + "//") + + def test_include_single_quotes(self): + self.verify_fnmatch("'" + self.config_path + "'") + + def test_include_double_quotes(self): + self.verify_fnmatch('"' + self.config_path + '"') + + def test_include_variable(self): + self.verify_fnmatch("../complex_parsing/${fnmatch_filename}") + + def test_include_missing(self): + # This should miss + self.verify_fnmatch("test_*.onf", False) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/configurator_reverter_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/configurator_reverter_test.py new file mode 100644 index 00000000000..96e980d2fdf --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/configurator_reverter_test.py @@ -0,0 +1,75 @@ +"""Test for certbot._internal.plugins.apache.configurator implementations of reverter""" +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot._internal.tests.plugins.apache import util + + +class ConfiguratorReverterTest(util.ApacheTest): + """Test for ApacheConfigurator reverter methods""" + + def setUp(self): # pylint: disable=arguments-differ + super().setUp() + + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir) + + self.vh_truth = util.get_vh_truth(self.temp_dir, "debian_apache_2_4/multiple_vhosts") + + def test_bad_save_checkpoint(self): + self.config.reverter.add_to_checkpoint = mock.Mock(side_effect=errors.ReverterError) + self.config.parser.add_dir(self.vh_truth[0].path, "Test", "bad_save_ckpt") + with pytest.raises(errors.PluginError): + self.config.save() + + def test_bad_save_finalize_checkpoint(self): + self.config.reverter.finalize_checkpoint = mock.Mock(side_effect=errors.ReverterError) + self.config.parser.add_dir(self.vh_truth[0].path, "Test", "bad_save_ckpt") + with pytest.raises(errors.PluginError): + self.config.save("Title") + + def test_finalize_save(self): + mock_finalize = mock.Mock() + self.config.reverter = mock_finalize + self.config.save("Example Title") + + assert mock_finalize.is_called + + def test_revert_challenge_config(self): + mock_load = mock.Mock() + self.config.parser.aug.load = mock_load + + self.config.revert_challenge_config() + assert mock_load.call_count == 1 + + def test_revert_challenge_config_error(self): + self.config.reverter.revert_temporary_config = mock.Mock( + side_effect=errors.ReverterError) + + with pytest.raises(errors.PluginError): + self.config.revert_challenge_config() + + def test_rollback_checkpoints(self): + mock_load = mock.Mock() + self.config.parser.aug.load = mock_load + + self.config.rollback_checkpoints() + assert mock_load.call_count == 1 + + def test_rollback_error(self): + self.config.reverter.rollback_checkpoints = mock.Mock(side_effect=errors.ReverterError) + with pytest.raises(errors.PluginError): + self.config.rollback_checkpoints() + + def test_recovery_routine_reload(self): + mock_load = mock.Mock() + self.config.parser.aug.load = mock_load + self.config.recovery_routine() + assert mock_load.call_count == 1 + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/configurator_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/configurator_test.py new file mode 100644 index 00000000000..07888dbe7bf --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/configurator_test.py @@ -0,0 +1,1792 @@ +# pylint: disable=too-many-lines +"""Test for certbot._internal.plugins.apache.configurator.""" +import copy +import shutil +import socket +import sys +import tempfile +from unittest import mock + +import pytest + +from acme import challenges, messages +from certbot import achallenges +from certbot import crypto_util +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +from certbot.tests import acme_util +from certbot.tests import util as certbot_util +from certbot._internal.plugins.apache import apache_util +from certbot._internal.plugins.apache import constants +from certbot._internal.plugins.apache import obj +from certbot._internal.plugins.apache import parser +from certbot._internal.tests.plugins.apache import util + + +class MultipleVhostsTest(util.ApacheTest): + """Test two standard well-configured HTTP vhosts.""" + + def setUp(self): # pylint: disable=arguments-differ + super().setUp() + + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir) + self.config = self.mock_deploy_cert(self.config) + self.vh_truth = util.get_vh_truth( + self.temp_dir, "debian_apache_2_4/multiple_vhosts") + + def mock_deploy_cert(self, config): + """A test for a mock deploy cert""" + config.real_deploy_cert = self.config.deploy_cert + + def mocked_deploy_cert(*args, **kwargs): + """a helper to mock a deployed cert""" + g_mod = "certbot._internal.plugins.apache.configurator.ApacheConfigurator.enable_mod" + with mock.patch(g_mod): + config.real_deploy_cert(*args, **kwargs) + self.config.deploy_cert = mocked_deploy_cert + return self.config + + @mock.patch("certbot._internal.plugins.apache.configurator.path_surgery") + def test_prepare_no_install(self, mock_surgery): + silly_path = {"PATH": "/tmp/nothingness2342"} + mock_surgery.return_value = False + with mock.patch.dict('os.environ', silly_path): + with pytest.raises(errors.NoInstallationError): + self.config.prepare() + assert mock_surgery.call_count == 1 + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser") + @mock.patch("certbot._internal.plugins.apache.configurator.util.exe_exists") + def test_prepare_version(self, mock_exe_exists, _): + mock_exe_exists.return_value = True + self.config.version = None + self.config.config_test = mock.Mock() + self.config.get_version = mock.Mock(return_value=(1, 1)) + + with pytest.raises(errors.NotSupportedError): + self.config.prepare() + + def test_prepare_locked(self): + # It is important to test that server_root is locked during the call to + # prepare (as opposed to somewhere else during plugin execution) to + # ensure that this lock will be acquired after the Certbot package + # acquires all of its locks. (Tests that Certbot calls prepare after + # acquiring its locks are part of the Certbot package's tests.) Not + # doing this could result in deadlock from two versions of Certbot that + # acquire its locks in a different order. + server_root = self.config.conf("server-root") + self.config.config_test = mock.Mock() + os.remove(os.path.join(server_root, ".certbot.lock")) + certbot_util.lock_and_call(self._test_prepare_locked, server_root) + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser") + @mock.patch("certbot._internal.plugins.apache.configurator.util.exe_exists") + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.get_parsernode_root") + def _test_prepare_locked(self, _node, _exists, _parser): + try: + self.config.prepare() + except errors.PluginError as err: + err_msg = str(err) + assert "lock" in err_msg + assert self.config.conf("server-root") in err_msg + else: # pragma: no cover + self.fail("Exception wasn't raised!") + + def test_add_parser_arguments(self): # pylint: disable=no-self-use + from certbot._internal.plugins.apache.configurator import ApacheConfigurator + + # Weak test.. + ApacheConfigurator.add_parser_arguments(mock.MagicMock()) + + def test_docs_parser_arguments(self): + os.environ["CERTBOT_DOCS"] = "1" + from certbot._internal.plugins.apache.configurator import ApacheConfigurator + mock_add = mock.MagicMock() + ApacheConfigurator.add_parser_arguments(mock_add) + parserargs = ["server_root", "enmod", "dismod", "le_vhost_ext", + "vhost_root", "logs_root", "challenge_location", + "handle_modules", "handle_sites", "ctl"] + exp = {} + + for k in ApacheConfigurator.OS_DEFAULTS.__dict__.keys(): + if k in parserargs: + exp[k.replace("_", "-")] = getattr(ApacheConfigurator.OS_DEFAULTS, k) + # Special cases + exp["vhost-root"] = None + + found = set() + for call in mock_add.call_args_list: + found.add(call[0][0]) + + # Make sure that all (and only) the expected values exist + assert len(mock_add.call_args_list) == len(found) + for e in exp: + with self.subTest(e=e): + assert e in found + + del os.environ["CERTBOT_DOCS"] + + def test_add_parser_arguments_all_configurators(self): # pylint: disable=no-self-use + from certbot._internal.plugins.apache.entrypoint import OVERRIDE_CLASSES + for cls in OVERRIDE_CLASSES.values(): + cls.add_parser_arguments(mock.MagicMock()) + + def test_all_configurators_defaults_defined(self): + from certbot._internal.plugins.apache.configurator import ApacheConfigurator + from certbot._internal.plugins.apache.entrypoint import OVERRIDE_CLASSES + parameters = set(ApacheConfigurator.OS_DEFAULTS.__dict__.keys()) + for cls in OVERRIDE_CLASSES.values(): + assert parameters.issubset(set(cls.OS_DEFAULTS.__dict__.keys())) is True + + def test_constant(self): + assert "debian_apache_2_4/multiple_vhosts/apache" in self.config.options.server_root + + @certbot_util.patch_display_util() + def test_get_all_names(self, mock_getutility): + mock_utility = mock_getutility() + mock_utility.notification = mock.MagicMock(return_value=True) + names = self.config.get_all_names() + assert names == {"certbot.demo", "ocspvhost.com", "encryption-example.demo", + "nonsym.link", "vhost.in.rootconf", "www.certbot.demo", + "duplicate.example.com"} + + @certbot_util.patch_display_util() + @mock.patch("certbot._internal.plugins.apache.configurator.socket.gethostbyaddr") + def test_get_all_names_addrs(self, mock_gethost, mock_getutility): + mock_gethost.side_effect = [("google.com", "", ""), socket.error] + mock_utility = mock_getutility() + mock_utility.notification.return_value = True + vhost = obj.VirtualHost( + "fp", "ap", + {obj.Addr(("8.8.8.8", "443")), + obj.Addr(("zombo.com",)), + obj.Addr(("192.168.1.2"))}, + True, False) + + self.config.vhosts.append(vhost) + + names = self.config.get_all_names() + assert len(names) == 9 + assert "zombo.com" in names + assert "google.com" in names + assert "certbot.demo" in names + + def test_get_bad_path(self): + assert apache_util.get_file_path(None) is None + assert apache_util.get_file_path("nonexistent") is None + assert self.config._create_vhost("nonexistent") is None # pylint: disable=protected-access + + def test_get_aug_internal_path(self): + from certbot._internal.plugins.apache.apache_util import get_internal_aug_path + internal_paths = [ + "Virtualhost", "IfModule/VirtualHost", "VirtualHost", "VirtualHost", + "Macro/VirtualHost", "IfModule/VirtualHost", "VirtualHost", + "IfModule/VirtualHost"] + + for i, internal_path in enumerate(internal_paths): + assert get_internal_aug_path(self.vh_truth[i].path) == internal_path + + def test_bad_servername_alias(self): + ssl_vh1 = obj.VirtualHost( + "fp1", "ap1", {obj.Addr(("*", "443"))}, + True, False) + # pylint: disable=protected-access + self.config._add_servernames(ssl_vh1) + assert self.config._add_servername_alias("oy_vey", ssl_vh1) is None + + def test_add_servernames_alias(self): + self.config.parser.add_dir( + self.vh_truth[2].path, "ServerAlias", ["*.le.co"]) + # pylint: disable=protected-access + self.config._add_servernames(self.vh_truth[2]) + assert self.vh_truth[2].get_names() == {"*.le.co", "ip-172-30-0-17"} + + def test_get_virtual_hosts(self): + """Make sure all vhosts are being properly found.""" + vhs = self.config.get_virtual_hosts() + assert len(vhs) == 12 + found = 0 + + for vhost in vhs: + for truth in self.vh_truth: + if vhost == truth: + found += 1 + break + else: + raise Exception("Missed: %s" % vhost) # pragma: no cover + + assert found == 12 + + # Handle case of non-debian layout get_virtual_hosts + with mock.patch( + "certbot._internal.plugins.apache.configurator.ApacheConfigurator.conf" + ) as mock_conf: + mock_conf.return_value = False + vhs = self.config.get_virtual_hosts() + assert len(vhs) == 12 + + @mock.patch("certbot._internal.plugins.apache.display_ops.select_vhost") + def test_choose_vhost_none_avail(self, mock_select): + mock_select.return_value = None + with pytest.raises(errors.PluginError): + self.config.choose_vhost("none.com") + + @mock.patch("certbot._internal.plugins.apache.display_ops.select_vhost") + def test_choose_vhost_select_vhost_ssl(self, mock_select): + mock_select.return_value = self.vh_truth[1] + assert self.vh_truth[1] == self.config.choose_vhost("none.com") + + @mock.patch("certbot._internal.plugins.apache.display_ops.select_vhost") + @mock.patch("certbot._internal.plugins.apache.obj.VirtualHost.conflicts") + def test_choose_vhost_select_vhost_non_ssl(self, mock_conf, mock_select): + mock_select.return_value = self.vh_truth[0] + mock_conf.return_value = False + chosen_vhost = self.config.choose_vhost("none.com") + self.vh_truth[0].aliases.add("none.com") + assert self.vh_truth[0].get_names() == chosen_vhost.get_names() + + # Make sure we go from HTTP -> HTTPS + assert self.vh_truth[0].ssl is False + assert chosen_vhost.ssl is True + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator._find_best_vhost") + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.add_dir") + def test_choose_vhost_and_servername_addition(self, mock_add, mock_find): + ret_vh = self.vh_truth[8] + ret_vh.enabled = False + mock_find.return_value = self.vh_truth[8] + self.config.choose_vhost("whatever.com") + assert mock_add.called is True + + @mock.patch("certbot._internal.plugins.apache.display_ops.select_vhost") + def test_choose_vhost_select_vhost_with_temp(self, mock_select): + mock_select.return_value = self.vh_truth[0] + chosen_vhost = self.config.choose_vhost("none.com", create_if_no_ssl=False) + assert self.vh_truth[0] == chosen_vhost + + @mock.patch("certbot._internal.plugins.apache.display_ops.select_vhost") + def test_choose_vhost_select_vhost_conflicting_non_ssl(self, mock_select): + mock_select.return_value = self.vh_truth[3] + conflicting_vhost = obj.VirtualHost( + "path", "aug_path", {obj.Addr.fromstring("*:443")}, + True, True) + self.config.vhosts.append(conflicting_vhost) + + with pytest.raises(errors.PluginError): + self.config.choose_vhost("none.com") + + def test_find_best_http_vhost_default(self): + vh = obj.VirtualHost( + "fp", "ap", {obj.Addr.fromstring("_default_:80")}, False, True) + self.config.vhosts = [vh] + assert self.config.find_best_http_vhost("foo.bar", False) == vh + + def test_find_best_http_vhost_port(self): + port = "8080" + vh = obj.VirtualHost( + "fp", "ap", {obj.Addr.fromstring("*:" + port)}, + False, True, "encryption-example.demo") + self.config.vhosts.append(vh) + assert self.config.find_best_http_vhost("foo.bar", False, port) == vh + + def test_findbest_continues_on_short_domain(self): + # pylint: disable=protected-access + assert self.config._find_best_vhost("purple.com") is None + + def test_findbest_continues_on_long_domain(self): + # pylint: disable=protected-access + assert self.config._find_best_vhost("green.red.purple.com") is None + + def test_find_best_vhost(self): + # pylint: disable=protected-access + assert self.vh_truth[3] == self.config._find_best_vhost("certbot.demo") + assert self.vh_truth[0] == self.config._find_best_vhost("encryption-example.demo") + assert self.config._find_best_vhost("does-not-exist.com") is None + + def test_find_best_vhost_variety(self): + # pylint: disable=protected-access + ssl_vh = obj.VirtualHost( + "fp", "ap", {obj.Addr(("*", "443")), + obj.Addr(("zombo.com",))}, + True, False) + self.config.vhosts.append(ssl_vh) + assert self.config._find_best_vhost("zombo.com") == ssl_vh + + def test_find_best_vhost_default(self): + # pylint: disable=protected-access + # Assume only the two default vhosts. + self.config.vhosts = [ + vh for vh in self.config.vhosts + if vh.name not in ["certbot.demo", "nonsym.link", + "encryption-example.demo", "duplicate.example.com", + "ocspvhost.com", "vhost.in.rootconf"] + and "*.blue.purple.com" not in vh.aliases + ] + assert self.config._find_best_vhost("encryption-example.demo") == \ + self.vh_truth[2] + + def test_non_default_vhosts(self): + # pylint: disable=protected-access + vhosts = self.config._non_default_vhosts(self.config.vhosts) + assert len(vhosts) == 10 + + @mock.patch('certbot._internal.plugins.apache.configurator.display_util.notify') + def test_deploy_cert_enable_new_vhost(self, unused_mock_notify): + # Create + ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[0]) + self.config.parser.modules["ssl_module"] = None + self.config.parser.modules["mod_ssl.c"] = None + self.config.parser.modules["socache_shmcb_module"] = None + + assert ssl_vhost.enabled is False + self.config.deploy_cert( + "encryption-example.demo", "example/cert.pem", "example/key.pem", + "example/cert_chain.pem", "example/fullchain.pem") + assert ssl_vhost.enabled is True + + def test_no_duplicate_include(self): + def mock_find_dir(directive, argument, _): + """Mock method for parser.find_dir""" + if directive == "Include" and argument.endswith("options-ssl-apache.conf"): + return ["/path/to/whatever"] + return None # pragma: no cover + + mock_add = mock.MagicMock() + self.config.parser.add_dir = mock_add + self.config._add_dummy_ssl_directives(self.vh_truth[0]) # pylint: disable=protected-access + tried_to_add = False + for a in mock_add.call_args_list: + if a[0][1] == "Include" and a[0][2] == self.config.mod_ssl_conf: + tried_to_add = True + # Include should be added, find_dir is not patched, and returns falsy + assert tried_to_add is True + + self.config.parser.find_dir = mock_find_dir + mock_add.reset_mock() + self.config._add_dummy_ssl_directives(self.vh_truth[0]) # pylint: disable=protected-access + for a in mock_add.call_args_list: + if a[0][1] == "Include" and a[0][2] == self.config.mod_ssl_conf: + self.fail("Include shouldn't be added, as patched find_dir 'finds' existing one") \ + # pragma: no cover + + @mock.patch('certbot._internal.plugins.apache.configurator.display_util.notify') + def test_deploy_cert(self, unused_mock_notify): + self.config.parser.modules["ssl_module"] = None + self.config.parser.modules["mod_ssl.c"] = None + self.config.parser.modules["socache_shmcb_module"] = None + # Patch _add_dummy_ssl_directives to make sure we write them correctly + # pylint: disable=protected-access + orig_add_dummy = self.config._add_dummy_ssl_directives + def mock_add_dummy_ssl(vhostpath): + """Mock method for _add_dummy_ssl_directives""" + def find_args(path, directive): + """Return list of arguments in requested directive at path""" + f_args = [] + dirs = self.config.parser.find_dir(directive, None, + path) + for d in dirs: + f_args.append(self.config.parser.get_arg(d)) + return f_args + # Verify that the dummy directives do not exist + assert "insert_cert_file_path" not in find_args(vhostpath, "SSLCertificateFile") + assert "insert_key_file_path" not in find_args(vhostpath, "SSLCertificateKeyFile") + orig_add_dummy(vhostpath) + # Verify that the dummy directives exist + assert "insert_cert_file_path" in find_args(vhostpath, "SSLCertificateFile") + assert "insert_key_file_path" in find_args(vhostpath, "SSLCertificateKeyFile") + # pylint: disable=protected-access + self.config._add_dummy_ssl_directives = mock_add_dummy_ssl + + # Get the default 443 vhost + self.config.assoc["random.demo"] = self.vh_truth[1] + self.config.deploy_cert( + "random.demo", + "example/cert.pem", "example/key.pem", "example/cert_chain.pem") + self.config.save() + + # Verify ssl_module was enabled. + assert self.vh_truth[1].enabled is True + assert "ssl_module" in self.config.parser.modules + + loc_cert = self.config.parser.find_dir( + "sslcertificatefile", "example/cert.pem", self.vh_truth[1].path) + loc_key = self.config.parser.find_dir( + "sslcertificateKeyfile", "example/key.pem", self.vh_truth[1].path) + loc_chain = self.config.parser.find_dir( + "SSLCertificateChainFile", "example/cert_chain.pem", + self.vh_truth[1].path) + + # Verify one directive was found in the correct file + assert len(loc_cert) == 1 + assert apache_util.get_file_path(loc_cert[0]) == \ + self.vh_truth[1].filep + + assert len(loc_key) == 1 + assert apache_util.get_file_path(loc_key[0]) == \ + self.vh_truth[1].filep + + assert len(loc_chain) == 1 + assert apache_util.get_file_path(loc_chain[0]) == \ + self.vh_truth[1].filep + + # One more time for chain directive setting + self.config.deploy_cert( + "random.demo", + "two/cert.pem", "two/key.pem", "two/cert_chain.pem") + assert self.config.parser.find_dir( + "SSLCertificateChainFile", "two/cert_chain.pem", + self.vh_truth[1].path) + + def test_add_listen_80(self): + mock_find = mock.Mock() + mock_add_dir = mock.Mock() + mock_find.return_value = [] + self.config.parser.find_dir = mock_find + self.config.parser.add_dir = mock_add_dir + self.config.ensure_listen("80") + assert mock_add_dir.called is True + assert mock_find.called is True + assert mock_add_dir.call_args[0][1] == "Listen" + assert mock_add_dir.call_args[0][2] == "80" + + def test_add_listen_80_named(self): + mock_find = mock.Mock() + mock_find.return_value = ["test1", "test2", "test3"] + mock_get = mock.Mock() + mock_get.side_effect = ["1.2.3.4:80", "[::1]:80", "1.1.1.1:443"] + mock_add_dir = mock.Mock() + + self.config.parser.find_dir = mock_find + self.config.parser.get_arg = mock_get + self.config.parser.add_dir = mock_add_dir + + self.config.ensure_listen("80") + assert mock_add_dir.call_count == 0 + + # Reset return lists and inputs + mock_add_dir.reset_mock() + mock_get.side_effect = ["1.2.3.4:80", "[::1]:80", "1.1.1.1:443"] + + # Test + self.config.ensure_listen("8080") + assert mock_add_dir.call_count == 3 + assert mock_add_dir.called is True + assert mock_add_dir.call_args[0][1] == "Listen" + call_found = False + for mock_call in mock_add_dir.mock_calls: + if mock_call[1][2] == ['1.2.3.4:8080']: + call_found = True + assert call_found is True + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.reset_modules") + def test_prepare_server_https(self, mock_reset): + mock_enable = mock.Mock() + self.config.enable_mod = mock_enable + + mock_find = mock.Mock() + mock_add_dir = mock.Mock() + mock_find.return_value = [] + + # This will test the Add listen + self.config.parser.find_dir = mock_find + self.config.parser.add_dir_to_ifmodssl = mock_add_dir + self.config.prepare_server_https("443") + # Changing the order these modules are enabled breaks the reverter + assert mock_enable.call_args_list[0][0][0] == "socache_shmcb" + assert mock_enable.call_args[0][0] == "ssl" + assert mock_enable.call_args[1] == {"temp": False} + + self.config.prepare_server_https("8080", temp=True) + # Changing the order these modules are enabled breaks the reverter + assert mock_enable.call_args_list[2][0][0] == "socache_shmcb" + assert mock_enable.call_args[0][0] == "ssl" + # Enable mod is temporary + assert mock_enable.call_args[1] == {"temp": True} + + assert mock_add_dir.call_count == 2 + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.reset_modules") + def test_prepare_server_https_named_listen(self, mock_reset): + mock_find = mock.Mock() + mock_find.return_value = ["test1", "test2", "test3"] + mock_get = mock.Mock() + mock_get.side_effect = ["1.2.3.4:80", "[::1]:80", "1.1.1.1:443"] + mock_add_dir = mock.Mock() + mock_enable = mock.Mock() + + self.config.parser.find_dir = mock_find + self.config.parser.get_arg = mock_get + self.config.parser.add_dir_to_ifmodssl = mock_add_dir + self.config.enable_mod = mock_enable + + # Test Listen statements with specific ip listeed + self.config.prepare_server_https("443") + # Should be 0 as one interface already listens to 443 + assert mock_add_dir.call_count == 0 + + # Reset return lists and inputs + mock_add_dir.reset_mock() + mock_get.side_effect = ["1.2.3.4:80", "[::1]:80", "1.1.1.1:443"] + + # Test + self.config.prepare_server_https("8080", temp=True) + assert mock_add_dir.call_count == 3 + call_args_list = [mock_add_dir.call_args_list[i][0][2] for i in range(3)] + assert sorted(call_args_list) == \ + sorted([["1.2.3.4:8080", "https"], + ["[::1]:8080", "https"], + ["1.1.1.1:8080", "https"]]) + + # mock_get.side_effect = ["1.2.3.4:80", "[::1]:80"] + # mock_find.return_value = ["test1", "test2", "test3"] + # self.config.parser.get_arg = mock_get + # self.config.prepare_server_https("8080", temp=True) + # self.assertEqual(self.listens, 0) + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.reset_modules") + def test_prepare_server_https_needed_listen(self, mock_reset): + mock_find = mock.Mock() + mock_find.return_value = ["test1", "test2"] + mock_get = mock.Mock() + mock_get.side_effect = ["1.2.3.4:8080", "80"] + mock_add_dir = mock.Mock() + mock_enable = mock.Mock() + + self.config.parser.find_dir = mock_find + self.config.parser.get_arg = mock_get + self.config.parser.add_dir_to_ifmodssl = mock_add_dir + self.config.enable_mod = mock_enable + + self.config.prepare_server_https("443") + assert mock_add_dir.call_count == 1 + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.reset_modules") + def test_prepare_server_https_mixed_listen(self, mock_reset): + mock_find = mock.Mock() + mock_find.return_value = ["test1", "test2"] + mock_get = mock.Mock() + mock_get.side_effect = ["1.2.3.4:8080", "443"] + mock_add_dir = mock.Mock() + mock_enable = mock.Mock() + + self.config.parser.find_dir = mock_find + self.config.parser.get_arg = mock_get + self.config.parser.add_dir_to_ifmodssl = mock_add_dir + self.config.enable_mod = mock_enable + + # Test Listen statements with specific ip listeed + self.config.prepare_server_https("443") + # Should only be 2 here, as the third interface + # already listens to the correct port + assert mock_add_dir.call_count == 0 + + def test_make_vhost_ssl_with_mock_span(self): + # span excludes the closing tag in older versions + # of Augeas + return_value = [self.vh_truth[0].filep, 1, 12, 0, 0, 0, 1142] + with mock.patch.object(self.config.parser.aug, 'span') as mock_span: + mock_span.return_value = return_value + self.test_make_vhost_ssl() + + def test_make_vhost_ssl_with_mock_span2(self): + # span includes the closing tag in newer versions + # of Augeas + return_value = [self.vh_truth[0].filep, 1, 12, 0, 0, 0, 1157] + with mock.patch.object(self.config.parser.aug, 'span') as mock_span: + mock_span.return_value = return_value + self.test_make_vhost_ssl() + + def test_make_vhost_ssl_nonsymlink(self): + ssl_vhost_slink = self.config.make_vhost_ssl(self.vh_truth[8]) + assert ssl_vhost_slink.ssl is True + assert ssl_vhost_slink.enabled is True + assert ssl_vhost_slink.name == "nonsym.link" + + def test_make_vhost_ssl_nonexistent_vhost_path(self): + ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[1]) + assert os.path.dirname(ssl_vhost.filep) == \ + os.path.dirname(filesystem.realpath(self.vh_truth[1].filep)) + + def test_make_vhost_ssl(self): + ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[0]) + + assert ssl_vhost.filep == \ + os.path.join(self.config_path, "sites-available", + "encryption-example-le-ssl.conf") + + assert ssl_vhost.path == \ + "/files" + ssl_vhost.filep + "/IfModule/Virtualhost" + assert len(ssl_vhost.addrs) == 1 + assert {obj.Addr.fromstring("*:443")} == ssl_vhost.addrs + assert ssl_vhost.name == "encryption-example.demo" + assert ssl_vhost.ssl is True + assert ssl_vhost.enabled is False + + assert len(self.config.vhosts) == 13 + + def test_clean_vhost_ssl(self): + # pylint: disable=protected-access + for directive in ["SSLCertificateFile", "SSLCertificateKeyFile", + "SSLCertificateChainFile", "SSLCACertificatePath"]: + for _ in range(10): + self.config.parser.add_dir(self.vh_truth[1].path, + directive, ["bogus"]) + self.config.save() + + self.config._clean_vhost(self.vh_truth[1]) + self.config.save() + + loc_cert = self.config.parser.find_dir( + 'SSLCertificateFile', None, self.vh_truth[1].path, False) + loc_key = self.config.parser.find_dir( + 'SSLCertificateKeyFile', None, self.vh_truth[1].path, False) + loc_chain = self.config.parser.find_dir( + 'SSLCertificateChainFile', None, self.vh_truth[1].path, False) + loc_cacert = self.config.parser.find_dir( + 'SSLCACertificatePath', None, self.vh_truth[1].path, False) + + assert len(loc_cert) == 1 + assert len(loc_key) == 1 + + assert len(loc_chain) == 0 + + assert len(loc_cacert) == 10 + + def test_deduplicate_directives(self): + # pylint: disable=protected-access + DIRECTIVE = "Foo" + for _ in range(10): + self.config.parser.add_dir(self.vh_truth[1].path, + DIRECTIVE, ["bar"]) + self.config.save() + + self.config._deduplicate_directives(self.vh_truth[1].path, [DIRECTIVE]) + self.config.save() + + assert len(self.config.parser.find_dir( + DIRECTIVE, None, self.vh_truth[1].path, False)) == 1 + + def test_remove_directives(self): + # pylint: disable=protected-access + DIRECTIVES = ["Foo", "Bar"] + for directive in DIRECTIVES: + for _ in range(10): + self.config.parser.add_dir(self.vh_truth[2].path, + directive, ["baz"]) + self.config.save() + + self.config._remove_directives(self.vh_truth[2].path, DIRECTIVES) + self.config.save() + + for directive in DIRECTIVES: + assert len(self.config.parser.find_dir( + directive, None, self.vh_truth[2].path, False)) == 0 + + def test_make_vhost_ssl_bad_write(self): + mock_open = mock.mock_open() + # This calls open + self.config.reverter.register_file_creation = mock.Mock() + mock_open.side_effect = IOError + with mock.patch("builtins.open", mock_open): + with pytest.raises(errors.PluginError): + self.config.make_vhost_ssl(self.vh_truth[0]) + + def test_get_ssl_vhost_path(self): + # pylint: disable=protected-access + assert self.config._get_ssl_vhost_path("example_path").endswith(".conf") is True + + @mock.patch("certbot._internal.plugins.apache.configurator.http_01.ApacheHttp01.perform") + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + def test_perform(self, mock_restart, mock_http_perform): + # Only tests functionality specific to configurator.perform + # Note: As more challenges are offered this will have to be expanded + account_key, achalls = self.get_key_and_achalls() + + expected = [achall.response(account_key) for achall in achalls] + mock_http_perform.return_value = expected + + responses = self.config.perform(achalls) + + assert mock_http_perform.call_count == 1 + assert responses == expected + + assert mock_restart.call_count == 1 + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + @mock.patch("certbot._internal.plugins.apache.apache_util._get_runtime_cfg") + def test_cleanup(self, mock_cfg, mock_restart): + mock_cfg.return_value = "" + _, achalls = self.get_key_and_achalls() + + for achall in achalls: + self.config._chall_out.add(achall) # pylint: disable=protected-access + + for i, achall in enumerate(achalls): + self.config.cleanup([achall]) + if i == len(achalls) - 1: + assert mock_restart.called is True + else: + assert mock_restart.called is False + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.restart") + @mock.patch("certbot._internal.plugins.apache.apache_util._get_runtime_cfg") + def test_cleanup_no_errors(self, mock_cfg, mock_restart): + mock_cfg.return_value = "" + _, achalls = self.get_key_and_achalls() + self.config.http_doer = mock.MagicMock() + + for achall in achalls: + self.config._chall_out.add(achall) # pylint: disable=protected-access + + self.config.cleanup([achalls[-1]]) + assert mock_restart.called is False + + self.config.cleanup(achalls) + assert mock_restart.called is True + + @mock.patch("certbot.util.run_script") + def test_get_version(self, mock_script): + mock_script.return_value = ( + "Server Version: Apache/2.4.2 (Debian)", "") + assert self.config.get_version() == (2, 4, 2) + + mock_script.return_value = ( + "Server Version: Apache/2 (Linux)", "") + assert self.config.get_version() == (2,) + + mock_script.return_value = ( + "Server Version: Apache (Debian)", "") + with pytest.raises(errors.PluginError): + self.config.get_version() + + mock_script.return_value = ( + "Server Version: Apache/2.3{0} Apache/2.4.7".format( + os.linesep), "") + with pytest.raises(errors.PluginError): + self.config.get_version() + + mock_script.side_effect = errors.SubprocessError("Can't find program") + with pytest.raises(errors.PluginError): + self.config.get_version() + + @mock.patch("certbot._internal.plugins.apache.configurator.util.run_script") + def test_restart(self, _): + self.config.restart() + + @mock.patch("certbot._internal.plugins.apache.configurator.util.run_script") + def test_restart_bad_process(self, mock_run_script): + mock_run_script.side_effect = [None, errors.SubprocessError] + + with pytest.raises(errors.MisconfigurationError): + self.config.restart() + + @mock.patch("certbot.util.run_script") + def test_config_test(self, _): + self.config.config_test() + + @mock.patch("certbot.util.run_script") + def test_config_test_bad_process(self, mock_run_script): + mock_run_script.side_effect = errors.SubprocessError + + with pytest.raises(errors.MisconfigurationError): + self.config.config_test() + + def test_more_info(self): + assert self.config.more_info() + + def test_get_chall_pref(self): + assert isinstance(self.config.get_chall_pref(""), list) + + def test_install_ssl_options_conf(self): + path = os.path.join(self.work_dir, "test_it") + other_path = os.path.join(self.work_dir, "other_test_it") + self.config.install_ssl_options_conf(path, other_path) + assert os.path.isfile(path) is True + assert os.path.isfile(other_path) is True + + # TEST ENHANCEMENTS + def test_supported_enhancements(self): + assert isinstance(self.config.supported_enhancements(), list) + + def test_find_http_vhost_without_ancestor(self): + # pylint: disable=protected-access + vhost = self.vh_truth[0] + vhost.ssl = True + vhost.ancestor = None + res = self.config._get_http_vhost(vhost) + assert self.vh_truth[0].name == res.name + assert self.vh_truth[0].aliases == res.aliases + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator._get_http_vhost") + @mock.patch("certbot._internal.plugins.apache.display_ops.select_vhost") + @mock.patch("certbot.util.exe_exists") + def test_enhance_unknown_vhost(self, mock_exe, mock_sel_vhost, mock_get): + self.config.parser.modules["rewrite_module"] = None + mock_exe.return_value = True + ssl_vh1 = obj.VirtualHost( + "fp1", "ap1", {obj.Addr(("*", "443"))}, + True, False) + ssl_vh1.name = "satoshi.com" + self.config.vhosts.append(ssl_vh1) + mock_sel_vhost.return_value = None + mock_get.return_value = None + + with pytest.raises(errors.PluginError): + self.config.enhance("satoshi.com", "redirect") + + def test_enhance_unknown_enhancement(self): + with pytest.raises(errors.PluginError): + self.config.enhance("certbot.demo", "unknown_enhancement") + + def test_enhance_no_ssl_vhost(self): + with mock.patch("certbot._internal.plugins.apache.configurator.logger.error") as mock_log: + with pytest.raises(errors.PluginError): + self.config.enhance("certbot.demo", "redirect") + # Check that correct logger.warning was printed + assert "not able to find" in mock_log.call_args[0][0] + assert "\"redirect\"" in mock_log.call_args[0][0] + + mock_log.reset_mock() + + with pytest.raises(errors.PluginError): + self.config.enhance("certbot.demo", "ensure-http-header", "Test") + # Check that correct logger.warning was printed + assert "not able to find" in mock_log.call_args[0][0] + assert "Test" in mock_log.call_args[0][0] + + @mock.patch("certbot.util.exe_exists") + def test_ocsp_stapling(self, mock_exe): + self.config.parser.update_runtime_variables = mock.Mock() + self.config.parser.modules["mod_ssl.c"] = None + self.config.parser.modules["socache_shmcb_module"] = None + self.config.get_version = mock.Mock(return_value=(2, 4, 7)) + mock_exe.return_value = True + + # This will create an ssl vhost for certbot.demo + self.config.choose_vhost("certbot.demo") + self.config.enhance("certbot.demo", "staple-ocsp") + + # Get the ssl vhost for certbot.demo + ssl_vhost = self.config.assoc["certbot.demo"] + + ssl_use_stapling_aug_path = self.config.parser.find_dir( + "SSLUseStapling", "on", ssl_vhost.path) + + assert len(ssl_use_stapling_aug_path) == 1 + + ssl_vhost_aug_path = parser.get_aug_path(ssl_vhost.filep) + stapling_cache_aug_path = self.config.parser.find_dir('SSLStaplingCache', + "shmcb:/var/run/apache2/stapling_cache(128000)", + ssl_vhost_aug_path) + + assert len(stapling_cache_aug_path) == 1 + + @mock.patch("certbot.util.exe_exists") + def test_ocsp_stapling_twice(self, mock_exe): + self.config.parser.update_runtime_variables = mock.Mock() + self.config.parser.modules["mod_ssl.c"] = None + self.config.parser.modules["socache_shmcb_module"] = None + self.config.get_version = mock.Mock(return_value=(2, 4, 7)) + mock_exe.return_value = True + + # Checking the case with already enabled ocsp stapling configuration + self.config.choose_vhost("ocspvhost.com") + self.config.enhance("ocspvhost.com", "staple-ocsp") + + # Get the ssl vhost for letsencrypt.demo + ssl_vhost = self.config.assoc["ocspvhost.com"] + + ssl_use_stapling_aug_path = self.config.parser.find_dir( + "SSLUseStapling", "on", ssl_vhost.path) + + assert len(ssl_use_stapling_aug_path) == 1 + ssl_vhost_aug_path = parser.get_aug_path(ssl_vhost.filep) + stapling_cache_aug_path = self.config.parser.find_dir('SSLStaplingCache', + "shmcb:/var/run/apache2/stapling_cache(128000)", + ssl_vhost_aug_path) + + assert len(stapling_cache_aug_path) == 1 + + def test_get_http_vhost_third_filter(self): + ssl_vh = obj.VirtualHost( + "fp", "ap", {obj.Addr(("*", "443"))}, + True, False) + ssl_vh.name = "satoshi.com" + self.config.vhosts.append(ssl_vh) + + # pylint: disable=protected-access + http_vh = self.config._get_http_vhost(ssl_vh) + assert http_vh.ssl is False + + @mock.patch("certbot.util.run_script") + @mock.patch("certbot.util.exe_exists") + def test_http_header_hsts(self, mock_exe, _): + self.config.parser.update_runtime_variables = mock.Mock() + self.config.parser.modules["mod_ssl.c"] = None + self.config.parser.modules["headers_module"] = None + mock_exe.return_value = True + + # This will create an ssl vhost for certbot.demo + self.config.choose_vhost("certbot.demo") + self.config.enhance("certbot.demo", "ensure-http-header", + "Strict-Transport-Security") + + # Get the ssl vhost for certbot.demo + ssl_vhost = self.config.assoc["certbot.demo"] + + # These are not immediately available in find_dir even with save() and + # load(). They must be found in sites-available + hsts_header = self.config.parser.find_dir( + "Header", None, ssl_vhost.path) + + # four args to HSTS header + assert len(hsts_header) == 4 + + def test_http_header_hsts_twice(self): + self.config.parser.modules["mod_ssl.c"] = None + # skip the enable mod + self.config.parser.modules["headers_module"] = None + + # This will create an ssl vhost for encryption-example.demo + self.config.choose_vhost("encryption-example.demo") + self.config.enhance("encryption-example.demo", "ensure-http-header", + "Strict-Transport-Security") + + with pytest.raises(errors.PluginEnhancementAlreadyPresent): + self.config.enhance("encryption-example.demo", + "ensure-http-header", "Strict-Transport-Security") + + @mock.patch("certbot.util.run_script") + @mock.patch("certbot.util.exe_exists") + def test_http_header_uir(self, mock_exe, _): + self.config.parser.update_runtime_variables = mock.Mock() + self.config.parser.modules["mod_ssl.c"] = None + self.config.parser.modules["headers_module"] = None + + mock_exe.return_value = True + + # This will create an ssl vhost for certbot.demo + self.config.choose_vhost("certbot.demo") + self.config.enhance("certbot.demo", "ensure-http-header", + "Upgrade-Insecure-Requests") + + assert "headers_module" in self.config.parser.modules + + # Get the ssl vhost for certbot.demo + ssl_vhost = self.config.assoc["certbot.demo"] + + # These are not immediately available in find_dir even with save() and + # load(). They must be found in sites-available + uir_header = self.config.parser.find_dir( + "Header", None, ssl_vhost.path) + + # four args to HSTS header + assert len(uir_header) == 4 + + def test_http_header_uir_twice(self): + self.config.parser.modules["mod_ssl.c"] = None + # skip the enable mod + self.config.parser.modules["headers_module"] = None + + # This will create an ssl vhost for encryption-example.demo + self.config.choose_vhost("encryption-example.demo") + self.config.enhance("encryption-example.demo", "ensure-http-header", + "Upgrade-Insecure-Requests") + + with pytest.raises(errors.PluginEnhancementAlreadyPresent): + self.config.enhance("encryption-example.demo", + "ensure-http-header", "Upgrade-Insecure-Requests") + + @mock.patch("certbot.util.run_script") + @mock.patch("certbot.util.exe_exists") + def test_redirect_well_formed_http(self, mock_exe, _): + self.config.parser.modules["rewrite_module"] = None + self.config.parser.update_runtime_variables = mock.Mock() + mock_exe.return_value = True + self.config.get_version = mock.Mock(return_value=(2, 2)) + + # This will create an ssl vhost for certbot.demo + self.config.choose_vhost("certbot.demo") + self.config.enhance("certbot.demo", "redirect") + + # These are not immediately available in find_dir even with save() and + # load(). They must be found in sites-available + rw_engine = self.config.parser.find_dir( + "RewriteEngine", "on", self.vh_truth[3].path) + rw_rule = self.config.parser.find_dir( + "RewriteRule", None, self.vh_truth[3].path) + + assert len(rw_engine) == 1 + # three args to rw_rule + assert len(rw_rule) == 3 + + # [:-3] to remove the vhost index number + assert rw_engine[0].startswith(self.vh_truth[3].path[:-3]) is True + assert rw_rule[0].startswith(self.vh_truth[3].path[:-3]) is True + + def test_rewrite_rule_exists(self): + # Skip the enable mod + self.config.parser.modules["rewrite_module"] = None + self.config.get_version = mock.Mock(return_value=(2, 3, 9)) + self.config.parser.add_dir( + self.vh_truth[3].path, "RewriteRule", ["Unknown"]) + # pylint: disable=protected-access + assert self.config._is_rewrite_exists(self.vh_truth[3]) is True + + def test_rewrite_engine_exists(self): + # Skip the enable mod + self.config.parser.modules["rewrite_module"] = None + self.config.get_version = mock.Mock(return_value=(2, 3, 9)) + self.config.parser.add_dir( + self.vh_truth[3].path, "RewriteEngine", "on") + # pylint: disable=protected-access + assert self.config._is_rewrite_engine_on(self.vh_truth[3]) + + @mock.patch("certbot.util.run_script") + @mock.patch("certbot.util.exe_exists") + def test_redirect_with_existing_rewrite(self, mock_exe, _): + self.config.parser.modules["rewrite_module"] = None + self.config.parser.update_runtime_variables = mock.Mock() + mock_exe.return_value = True + self.config.get_version = mock.Mock(return_value=(2, 2, 0)) + + # Create a preexisting rewrite rule + self.config.parser.add_dir( + self.vh_truth[3].path, "RewriteRule", ["UnknownPattern", + "UnknownTarget"]) + self.config.save() + + # This will create an ssl vhost for certbot.demo + self.config.choose_vhost("certbot.demo") + self.config.enhance("certbot.demo", "redirect") + + # These are not immediately available in find_dir even with save() and + # load(). They must be found in sites-available + rw_engine = self.config.parser.find_dir( + "RewriteEngine", "on", self.vh_truth[3].path) + rw_rule = self.config.parser.find_dir( + "RewriteRule", None, self.vh_truth[3].path) + + assert len(rw_engine) == 1 + # three args to rw_rule + 1 arg for the pre existing rewrite + assert len(rw_rule) == 5 + # [:-3] to remove the vhost index number + assert rw_engine[0].startswith(self.vh_truth[3].path[:-3]) is True + assert rw_rule[0].startswith(self.vh_truth[3].path[:-3]) is True + + assert "rewrite_module" in self.config.parser.modules + + @mock.patch("certbot.util.run_script") + @mock.patch("certbot.util.exe_exists") + def test_redirect_with_old_https_redirection(self, mock_exe, _): + self.config.parser.modules["rewrite_module"] = None + self.config.parser.update_runtime_variables = mock.Mock() + mock_exe.return_value = True + self.config.get_version = mock.Mock(return_value=(2, 4, 0)) + + ssl_vhost = self.config.choose_vhost("certbot.demo") + + # pylint: disable=protected-access + http_vhost = self.config._get_http_vhost(ssl_vhost) + + # Create an old (previously supported) https redirectoin rewrite rule + self.config.parser.add_dir( + http_vhost.path, "RewriteRule", + ["^", + "https://%{SERVER_NAME}%{REQUEST_URI}", + "[L,QSA,R=permanent]"]) + + self.config.save() + + try: + self.config.enhance("certbot.demo", "redirect") + except errors.PluginEnhancementAlreadyPresent: + args_paths = self.config.parser.find_dir( + "RewriteRule", None, http_vhost.path, False) + arg_vals = [self.config.parser.aug.get(x) for x in args_paths] + assert arg_vals == constants.REWRITE_HTTPS_ARGS + + + def test_redirect_with_conflict(self): + self.config.parser.modules["rewrite_module"] = None + ssl_vh = obj.VirtualHost( + "fp", "ap", {obj.Addr(("*", "443")), + obj.Addr(("zombo.com",))}, + True, False) + # No names ^ this guy should conflict. + + # pylint: disable=protected-access + with pytest.raises(errors.PluginError): + self.config._enable_redirect(ssl_vh, "") + + def test_redirect_two_domains_one_vhost(self): + # Skip the enable mod + self.config.parser.modules["rewrite_module"] = None + self.config.get_version = mock.Mock(return_value=(2, 3, 9)) + + # Creates ssl vhost for the domain + self.config.choose_vhost("red.blue.purple.com") + + self.config.enhance("red.blue.purple.com", "redirect") + verify_no_redirect = ("certbot._internal.plugins.apache.configurator." + "ApacheConfigurator._verify_no_certbot_redirect") + with mock.patch(verify_no_redirect) as mock_verify: + self.config.enhance("green.blue.purple.com", "redirect") + assert mock_verify.called is False + + def test_redirect_from_previous_run(self): + # Skip the enable mod + self.config.parser.modules["rewrite_module"] = None + self.config.get_version = mock.Mock(return_value=(2, 3, 9)) + self.config.choose_vhost("red.blue.purple.com") + self.config.enhance("red.blue.purple.com", "redirect") + # Clear state about enabling redirect on this run + # pylint: disable=protected-access + self.config._enhanced_vhosts["redirect"].clear() + + with pytest.raises(errors.PluginEnhancementAlreadyPresent): + self.config.enhance("green.blue.purple.com", "redirect") + + def test_create_own_redirect(self): + self.config.parser.modules["rewrite_module"] = None + self.config.get_version = mock.Mock(return_value=(2, 3, 9)) + # For full testing... give names... + self.vh_truth[1].name = "default.com" + self.vh_truth[1].aliases = {"yes.default.com"} + + # pylint: disable=protected-access + self.config._enable_redirect(self.vh_truth[1], "") + assert len(self.config.vhosts) == 13 + + def test_create_own_redirect_for_old_apache_version(self): + self.config.parser.modules["rewrite_module"] = None + self.config.get_version = mock.Mock(return_value=(2, 2)) + # For full testing... give names... + self.vh_truth[1].name = "default.com" + self.vh_truth[1].aliases = {"yes.default.com"} + + # pylint: disable=protected-access + self.config._enable_redirect(self.vh_truth[1], "") + assert len(self.config.vhosts) == 13 + + def test_sift_rewrite_rule(self): + # pylint: disable=protected-access + small_quoted_target = "RewriteRule ^ \"http://\"" + assert self.config._sift_rewrite_rule(small_quoted_target) is False + + https_target = "RewriteRule ^ https://satoshi" + assert self.config._sift_rewrite_rule(https_target) is True + + normal_target = "RewriteRule ^/(.*) http://www.a.com:1234/$1 [L,R]" + assert self.config._sift_rewrite_rule(normal_target) is False + + not_rewriterule = "NotRewriteRule ^ ..." + assert self.config._sift_rewrite_rule(not_rewriterule) is False + + def get_key_and_achalls(self): + """Return testing achallenges.""" + account_key = self.rsa512jwk + achall1 = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01( + token=b"jIq_Xy1mXGN37tb4L6Xj_es58fW571ZNyXekdZzhh7Q"), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="encryption-example.demo"), + account_key=account_key) + achall2 = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01( + token=b"uqnaPzxtrndteOqtrXb0Asl5gOJfWAnnx6QJyvcmlDU"), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="certbot.demo"), + account_key=account_key) + achall3 = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=(b'x' * 16)), "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="example.org"), + account_key=account_key) + + return account_key, (achall1, achall2, achall3) + + def test_enable_site_nondebian(self): + inc_path = "/path/to/wherever" + vhost = self.vh_truth[0] + vhost.enabled = False + vhost.filep = inc_path + assert self.config.parser.find_dir("Include", inc_path) == [] + assert os.path.dirname(inc_path) not in self.config.parser.existing_paths + self.config.enable_site(vhost) + assert len(self.config.parser.find_dir("Include", inc_path)) >= 1 + assert os.path.dirname(inc_path) in self.config.parser.existing_paths + assert os.path.basename(inc_path) in self.config.parser.existing_paths[ + os.path.dirname(inc_path)] + + @mock.patch('certbot._internal.plugins.apache.configurator.display_util.notify') + def test_deploy_cert_not_parsed_path(self, unused_mock_notify): + # Make sure that we add include to root config for vhosts when + # handle-sites is false + self.config.parser.modules["ssl_module"] = None + self.config.parser.modules["mod_ssl.c"] = None + self.config.parser.modules["socache_shmcb_module"] = None + tmp_path = filesystem.realpath(tempfile.mkdtemp("vhostroot")) + filesystem.chmod(tmp_path, 0o755) + mock_p = "certbot._internal.plugins.apache.configurator.ApacheConfigurator._get_ssl_vhost_path" + mock_a = "certbot._internal.plugins.apache.parser.ApacheParser.add_include" + + with mock.patch(mock_p) as mock_path: + mock_path.return_value = os.path.join(tmp_path, "whatever.conf") + with mock.patch(mock_a) as mock_add: + self.config.deploy_cert( + "encryption-example.demo", + "example/cert.pem", "example/key.pem", + "example/cert_chain.pem") + # Test that we actually called add_include + assert mock_add.called is True + shutil.rmtree(tmp_path) + + def test_deploy_cert_no_mod_ssl(self): + # Create + self.config.make_vhost_ssl(self.vh_truth[0]) + self.config.parser.modules["socache_shmcb_module"] = None + self.config.prepare_server_https = mock.Mock() + + with pytest.raises(errors.MisconfigurationError): + self.config.deploy_cert("encryption-example.demo", "example/cert.pem", "example/key.pem", + "example/cert_chain.pem", "example/fullchain.pem") + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.parsed_in_original") + def test_choose_vhost_and_servername_addition_parsed(self, mock_parsed): + ret_vh = self.vh_truth[8] + ret_vh.enabled = True + self.config.enable_site(ret_vh) + # Make sure that we return early + assert mock_parsed.called is False + + def test_enable_mod_unsupported(self): + with pytest.raises(errors.MisconfigurationError): + self.config.enable_mod("whatever") + + def test_choose_vhosts_wildcard(self): + # pylint: disable=protected-access + mock_path = "certbot._internal.plugins.apache.display_ops.select_vhost_multiple" + with mock.patch(mock_path) as mock_select_vhs: + mock_select_vhs.return_value = [self.vh_truth[3]] + vhs = self.config._choose_vhosts_wildcard("*.certbot.demo", + create_ssl=True) + # Check that the dialog was called with one vh: certbot.demo + assert mock_select_vhs.call_args[0][0][0] == self.vh_truth[3] + assert len(mock_select_vhs.call_args_list) == 1 + + # And the actual returned values + assert len(vhs) == 1 + assert vhs[0].name == "certbot.demo" + assert vhs[0].ssl is True + + assert vhs[0] != self.vh_truth[3] + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.make_vhost_ssl") + def test_choose_vhosts_wildcard_no_ssl(self, mock_makessl): + # pylint: disable=protected-access + mock_path = "certbot._internal.plugins.apache.display_ops.select_vhost_multiple" + with mock.patch(mock_path) as mock_select_vhs: + mock_select_vhs.return_value = [self.vh_truth[1]] + vhs = self.config._choose_vhosts_wildcard("*.certbot.demo", + create_ssl=False) + assert mock_makessl.called is False + assert vhs[0] == self.vh_truth[1] + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator._vhosts_for_wildcard") + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.make_vhost_ssl") + def test_choose_vhosts_wildcard_already_ssl(self, mock_makessl, mock_vh_for_w): + # pylint: disable=protected-access + # Already SSL vhost + mock_vh_for_w.return_value = [self.vh_truth[7]] + mock_path = "certbot._internal.plugins.apache.display_ops.select_vhost_multiple" + with mock.patch(mock_path) as mock_select_vhs: + mock_select_vhs.return_value = [self.vh_truth[7]] + vhs = self.config._choose_vhosts_wildcard("whatever", + create_ssl=True) + assert mock_select_vhs.call_args[0][0][0] == self.vh_truth[7] + assert len(mock_select_vhs.call_args_list) == 1 + # Ensure that make_vhost_ssl was not called, vhost.ssl == true + assert mock_makessl.called is False + + # And the actual returned values + assert len(vhs) == 1 + assert vhs[0].ssl is True + assert vhs[0] == self.vh_truth[7] + + @mock.patch('certbot._internal.plugins.apache.configurator.display_util.notify') + def test_deploy_cert_wildcard(self, unused_mock_notify): + # pylint: disable=protected-access + mock_choose_vhosts = mock.MagicMock() + mock_choose_vhosts.return_value = [self.vh_truth[7]] + self.config._choose_vhosts_wildcard = mock_choose_vhosts + mock_d = "certbot._internal.plugins.apache.configurator.ApacheConfigurator._deploy_cert" + with mock.patch(mock_d) as mock_dep: + self.config.deploy_cert("*.wildcard.example.org", "/tmp/path", + "/tmp/path", "/tmp/path", "/tmp/path") + assert mock_dep.called is True + assert len(mock_dep.call_args_list) == 1 + assert self.vh_truth[7] == mock_dep.call_args_list[0][0][0] + + @mock.patch("certbot._internal.plugins.apache.display_ops.select_vhost_multiple") + def test_deploy_cert_wildcard_no_vhosts(self, mock_dialog): + # pylint: disable=protected-access + mock_dialog.return_value = [] + with pytest.raises(errors.PluginError): + self.config.deploy_cert("*.wild.cat", "/tmp/path", "/tmp/path", + "/tmp/path", "/tmp/path") + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator._choose_vhosts_wildcard") + def test_enhance_wildcard_after_install(self, mock_choose): + # pylint: disable=protected-access + self.config.parser.modules["mod_ssl.c"] = None + self.config.parser.modules["headers_module"] = None + self.vh_truth[3].ssl = True + self.config._wildcard_vhosts["*.certbot.demo"] = [self.vh_truth[3]] + self.config.enhance("*.certbot.demo", "ensure-http-header", + "Upgrade-Insecure-Requests") + assert mock_choose.called is False + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator._choose_vhosts_wildcard") + def test_enhance_wildcard_no_install(self, mock_choose): + self.vh_truth[3].ssl = True + mock_choose.return_value = [self.vh_truth[3]] + self.config.parser.modules["mod_ssl.c"] = None + self.config.parser.modules["headers_module"] = None + self.config.enhance("*.certbot.demo", "ensure-http-header", + "Upgrade-Insecure-Requests") + assert mock_choose.called is True + + def test_add_vhost_id(self): + for vh in [self.vh_truth[0], self.vh_truth[1], self.vh_truth[2]]: + vh_id = self.config.add_vhost_id(vh) + assert vh == self.config.find_vhost_by_id(vh_id) + + def test_find_vhost_by_id_404(self): + with pytest.raises(errors.PluginError): + self.config.find_vhost_by_id("nonexistent") + + def test_add_vhost_id_already_exists(self): + first_id = self.config.add_vhost_id(self.vh_truth[0]) + second_id = self.config.add_vhost_id(self.vh_truth[0]) + assert first_id == second_id + + def test_realpath_replaces_symlink(self): + orig_match = self.config.parser.aug.match + mock_vhost = copy.deepcopy(self.vh_truth[0]) + mock_vhost.filep = mock_vhost.filep.replace('sites-enabled', u'sites-available') + mock_vhost.path = mock_vhost.path.replace('sites-enabled', 'sites-available') + mock_vhost.enabled = False + self.config.parser.parse_file(mock_vhost.filep) + + def mock_match(aug_expr): + """Return a mocked match list of VirtualHosts""" + if "/mocked/path" in aug_expr: + return [self.vh_truth[1].path, self.vh_truth[0].path, mock_vhost.path] + return orig_match(aug_expr) + + self.config.parser.parser_paths = ["/mocked/path"] + self.config.parser.aug.match = mock_match + vhs = self.config.get_virtual_hosts() + assert len(vhs) == 2 + assert vhs[0] == self.vh_truth[1] + # mock_vhost should have replaced the vh_truth[0], because its filepath + # isn't a symlink + assert vhs[1] == mock_vhost + + +class AugeasVhostsTest(util.ApacheTest): + """Test vhosts with illegal names dependent on augeas version.""" + # pylint: disable=protected-access + + def setUp(self): # pylint: disable=arguments-differ + td = "debian_apache_2_4/augeas_vhosts" + cr = "debian_apache_2_4/augeas_vhosts/apache2" + vr = "debian_apache_2_4/augeas_vhosts/apache2/sites-available" + super().setUp(test_dir=td, + config_root=cr, + vhost_root=vr) + + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, + self.work_dir) + + def test_choosevhost_with_illegal_name(self): + self.config.parser.aug = mock.MagicMock() + self.config.parser.aug.match.side_effect = RuntimeError + path = "debian_apache_2_4/augeas_vhosts/apache2/sites-available/old-and-default.conf" + chosen_vhost = self.config._create_vhost(path) + assert chosen_vhost is None + + def test_choosevhost_works(self): + path = "debian_apache_2_4/augeas_vhosts/apache2/sites-available/old-and-default.conf" + chosen_vhost = self.config._create_vhost(path) + assert chosen_vhost is None or chosen_vhost.path == path + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator._create_vhost") + def test_get_vhost_continue(self, mock_vhost): + mock_vhost.return_value = None + vhs = self.config.get_virtual_hosts() + assert [] == vhs + + def test_choose_vhost_with_matching_wildcard(self): + names = ( + "an.example.net", "another.example.net", "an.other.example.net") + for name in names: + with self.subTest(name=name): + assert name not in self.config.choose_vhost(name).aliases + + @mock.patch("certbot._internal.plugins.apache.obj.VirtualHost.conflicts") + def test_choose_vhost_without_matching_wildcard(self, mock_conflicts): + mock_conflicts.return_value = False + mock_path = "certbot._internal.plugins.apache.display_ops.select_vhost" + with mock.patch(mock_path, lambda _, vhosts: vhosts[0]): + for name in ("a.example.net", "other.example.net"): + assert name in self.config.choose_vhost(name).aliases + + @mock.patch("certbot._internal.plugins.apache.obj.VirtualHost.conflicts") + def test_choose_vhost_wildcard_not_found(self, mock_conflicts): + mock_conflicts.return_value = False + mock_path = "certbot._internal.plugins.apache.display_ops.select_vhost" + names = ( + "abc.example.net", "not.there.tld", "aa.wildcard.tld" + ) + with mock.patch(mock_path) as mock_select: + mock_select.return_value = self.config.vhosts[0] + for name in names: + orig_cc = mock_select.call_count + self.config.choose_vhost(name) + assert mock_select.call_count - orig_cc == 1 + + def test_choose_vhost_wildcard_found(self): + mock_path = "certbot._internal.plugins.apache.display_ops.select_vhost" + names = ( + "ab.example.net", "a.wildcard.tld", "yetanother.example.net" + ) + with mock.patch(mock_path) as mock_select: + mock_select.return_value = self.config.vhosts[0] + for name in names: + self.config.choose_vhost(name) + assert mock_select.call_count == 0 + + def test_augeas_span_error(self): + broken_vhost = self.config.vhosts[0] + broken_vhost.path = broken_vhost.path + "/nonexistent" + with pytest.raises(errors.PluginError): + self.config.make_vhost_ssl(broken_vhost) + + +class MultiVhostsTest(util.ApacheTest): + """Test configuration with multiple virtualhosts in a single file.""" + # pylint: disable=protected-access + + def setUp(self): # pylint: disable=arguments-differ + td = "debian_apache_2_4/multi_vhosts" + cr = "debian_apache_2_4/multi_vhosts/apache2" + vr = "debian_apache_2_4/multi_vhosts/apache2/sites-available" + super().setUp(test_dir=td, config_root=cr, vhost_root=vr) + + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, + self.config_dir, self.work_dir, conf_vhost_path=self.vhost_path) + self.vh_truth = util.get_vh_truth( + self.temp_dir, "debian_apache_2_4/multi_vhosts") + + def test_make_vhost_ssl(self): + ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[1]) + + assert ssl_vhost.filep == \ + os.path.join(self.config_path, "sites-available", + "default-le-ssl.conf") + + assert ssl_vhost.path == \ + "/files" + ssl_vhost.filep + "/IfModule/VirtualHost" + assert len(ssl_vhost.addrs) == 1 + assert {obj.Addr.fromstring("*:443")} == ssl_vhost.addrs + assert ssl_vhost.name == "banana.vomit.com" + assert ssl_vhost.ssl is True + assert ssl_vhost.enabled is False + + mock_path = "certbot._internal.plugins.apache.configurator.ApacheConfigurator._get_new_vh_path" + with mock.patch(mock_path) as mock_getpath: + mock_getpath.return_value = None + with pytest.raises(errors.PluginError): + self.config.make_vhost_ssl(self.vh_truth[1]) + + def test_get_new_path(self): + with_index_1 = ["/path[1]/section[1]"] + without_index = ["/path/section"] + with_index_2 = ["/path[2]/section[2]"] + assert self.config._get_new_vh_path(without_index, with_index_1) is None + assert self.config._get_new_vh_path(without_index, with_index_2) == with_index_2[0] + + both = with_index_1 + with_index_2 + assert self.config._get_new_vh_path(without_index, both) == with_index_2[0] + + @mock.patch("certbot._internal.plugins.apache.configurator.display_util.notify") + def test_make_vhost_ssl_with_existing_rewrite_rule(self, mock_notify): + self.config.parser.modules["rewrite_module"] = None + + ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[4]) + + assert self.config.parser.find_dir("RewriteEngine", "on", ssl_vhost.path, False) + + with open(ssl_vhost.filep) as the_file: + conf_text = the_file.read() + commented_rewrite_rule = ("# RewriteRule \"^/secrets/(.+)\" " + "\"https://new.example.com/docs/$1\" [R,L]") + uncommented_rewrite_rule = ("RewriteRule \"^/docs/(.+)\" " + "\"http://new.example.com/docs/$1\" [R,L]") + assert commented_rewrite_rule in conf_text + assert uncommented_rewrite_rule in conf_text + assert mock_notify.call_count == 1 + assert "Some rewrite rules" in mock_notify.call_args[0][0] + + @mock.patch("certbot._internal.plugins.apache.configurator.display_util.notify") + def test_make_vhost_ssl_with_existing_rewrite_conds(self, mock_notify): + self.config.parser.modules["rewrite_module"] = None + + ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[3]) + + with open(ssl_vhost.filep) as the_file: + conf_lines = the_file.readlines() + conf_line_set = [l.strip() for l in conf_lines] + not_commented_cond1 = ("RewriteCond " + "%{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f") + not_commented_rewrite_rule = ("RewriteRule " + "^(.*)$ b://u%{REQUEST_URI} [P,NE,L]") + + commented_cond1 = "# RewriteCond %{HTTPS} !=on" + commented_cond2 = "# RewriteCond %{HTTPS} !^$" + commented_rewrite_rule = ("# RewriteRule ^ " + "https://%{SERVER_NAME}%{REQUEST_URI} " + "[L,NE,R=permanent]") + + assert not_commented_cond1 in conf_line_set + assert not_commented_rewrite_rule in conf_line_set + + assert commented_cond1 in conf_line_set + assert commented_cond2 in conf_line_set + assert commented_rewrite_rule in conf_line_set + assert mock_notify.call_count == 1 + assert "Some rewrite rules" in mock_notify.call_args[0][0] + + +class InstallSslOptionsConfTest(util.ApacheTest): + """Test that the options-ssl-nginx.conf file is installed and updated properly.""" + + def setUp(self): # pylint: disable=arguments-differ + super().setUp() + + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir) + + def _call(self): + self.config.install_ssl_options_conf(self.config.mod_ssl_conf, + self.config.updated_mod_ssl_conf_digest) + + def _current_ssl_options_hash(self): + return crypto_util.sha256sum(self.config.pick_apache_config()) + + def _assert_current_file(self): + assert os.path.isfile(self.config.mod_ssl_conf) is True + assert crypto_util.sha256sum(self.config.mod_ssl_conf) == \ + self._current_ssl_options_hash() + + def test_no_file(self): + # prepare should have placed a file there + self._assert_current_file() + os.remove(self.config.mod_ssl_conf) + assert os.path.isfile(self.config.mod_ssl_conf) is False + self._call() + self._assert_current_file() + + def test_current_file(self): + self._assert_current_file() + self._call() + self._assert_current_file() + + def test_prev_file_updates_to_current(self): + from certbot._internal.plugins.apache.constants import ALL_SSL_OPTIONS_HASHES + ALL_SSL_OPTIONS_HASHES.insert(0, "test_hash_does_not_match") + with mock.patch('certbot.crypto_util.sha256sum') as mock_sha256: + mock_sha256.return_value = ALL_SSL_OPTIONS_HASHES[0] + self._call() + self._assert_current_file() + + def test_manually_modified_current_file_does_not_update(self): + with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf: + mod_ssl_conf.write("a new line for the wrong hash\n") + with mock.patch("certbot.plugins.common.logger") as mock_logger: + self._call() + assert mock_logger.warning.called is False + assert os.path.isfile(self.config.mod_ssl_conf) is True + assert crypto_util.sha256sum( + self.config.pick_apache_config()) == \ + self._current_ssl_options_hash() + assert crypto_util.sha256sum(self.config.mod_ssl_conf) != \ + self._current_ssl_options_hash() + + def test_manually_modified_past_file_warns(self): + with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf: + mod_ssl_conf.write("a new line for the wrong hash\n") + with open(self.config.updated_mod_ssl_conf_digest, "w") as f: + f.write("hashofanoldversion") + with mock.patch("certbot.plugins.common.logger") as mock_logger: + self._call() + assert mock_logger.warning.call_args[0][0] == \ + "%s has been manually modified; updated file " \ + "saved to %s. We recommend updating %s for security purposes." + assert crypto_util.sha256sum( + self.config.pick_apache_config()) == \ + self._current_ssl_options_hash() + # only print warning once + with mock.patch("certbot.plugins.common.logger") as mock_logger: + self._call() + assert mock_logger.warning.called is False + + @mock.patch('certbot._internal.plugins.apache.configurator.logger.warning') + @mock.patch('certbot._internal.plugins.apache.configurator.ApacheConfigurator.openssl_version') + def test_pick_apache_config_versions_and_warnings(self, mock_openssl_version, mock_warning): + def has_logged_warning(): + """Returns True if a warning was logged about updating Apache.""" + return any('update apache' in call.args[0] for call in mock_warning.call_args_list) + + # old apache, no openssl + self.config.version = (2, 4, 10) + mock_openssl_version.return_value = None + assert 'old' in self.config.pick_apache_config() + assert not has_logged_warning() + + # old apache, old openssl + mock_warning.reset_mock() + mock_openssl_version.return_value = '1.0.2a' + assert 'old' in self.config.pick_apache_config() + assert has_logged_warning() + + # old apache, new openssl + mock_warning.reset_mock() + mock_openssl_version.return_value = '1.0.2m' + assert 'old' in self.config.pick_apache_config() + assert has_logged_warning() + + # new apache, no openssl + mock_warning.reset_mock() + self.config.version = (2, 4, 11) + mock_openssl_version.return_value = None + assert 'old' in self.config.pick_apache_config() + assert not has_logged_warning() + + # new apache, old openssl + mock_warning.reset_mock() + mock_openssl_version.return_value = '1.0.2a' + assert 'old' in self.config.pick_apache_config() + assert has_logged_warning() + + # new apache, new openssl + mock_warning.reset_mock() + mock_openssl_version.return_value = '1.0.2m' + assert 'current' in self.config.pick_apache_config() + assert not has_logged_warning() + + def test_ssl_config_files_hash_in_all_hashes(self): + """ + It is really critical that all TLS Apache config files have their SHA256 hash registered in + constants.ALL_SSL_OPTIONS_HASHES. Otherwise Certbot will mistakenly assume that the config + file has been manually edited by the user, and will refuse to update it. + This test ensures that all necessary hashes are present. + """ + import importlib.resources + + from certbot._internal.plugins.apache.constants import ALL_SSL_OPTIONS_HASHES + + ref = importlib.resources.files("certbot") / "_internal" / "plugins" /"apache" / "tls_configs" + with importlib.resources.as_file(ref) as tls_configs_dir: + all_files = [os.path.join(tls_configs_dir, name) for name in os.listdir(tls_configs_dir) + if name.endswith('options-ssl-apache.conf')] + assert len(all_files) >= 1 + for one_file in all_files: + file_hash = crypto_util.sha256sum(one_file) + assert file_hash in ALL_SSL_OPTIONS_HASHES, \ + f"Constants.ALL_SSL_OPTIONS_HASHES must be appended with the sha256 " \ + f"hash of {one_file} when it is updated." + + def test_openssl_version(self): + self.config._openssl_version = None + some_string_contents = b""" + SSLOpenSSLConfCmd + OpenSSL configuration command + SSLv3 not supported by this version of OpenSSL + '%s': invalid OpenSSL configuration command + OpenSSL 1.0.2g 1 Mar 2016 + OpenSSL + AH02407: "SSLOpenSSLConfCmd %s %s" failed for %s + AH02556: "SSLOpenSSLConfCmd %s %s" applied to %s + OpenSSL 1.0.2g 1 Mar 2016 + """ + # ssl_module as a DSO + self.config.parser.modules['ssl_module'] = '/fake/path' + with mock.patch("certbot._internal.plugins.apache.configurator." + "ApacheConfigurator._open_module_file") as mock_omf: + mock_omf.return_value = some_string_contents + assert self.config.openssl_version() == "1.0.2g" + + # ssl_module statically linked + self.config._openssl_version = None + self.config.parser.modules['ssl_module'] = None + self.config.options.bin = '/fake/path/to/httpd' + with mock.patch("certbot._internal.plugins.apache.configurator." + "ApacheConfigurator._open_module_file") as mock_omf: + mock_omf.return_value = some_string_contents + assert self.config.openssl_version() == "1.0.2g" + + def test_openssl_version_warns(self): + self.config._openssl_version = '1.0.2a' + assert self.config.openssl_version() == '1.0.2a' + + self.config._openssl_version = None + with mock.patch("certbot._internal.plugins.apache.configurator.logger.warning") as mock_log: + assert self.config.openssl_version() is None + assert "Could not find ssl_module" in mock_log.call_args[0][0] + + # When no ssl_module is present at all + self.config._openssl_version = None + assert "ssl_module" not in self.config.parser.modules + with mock.patch("certbot._internal.plugins.apache.configurator.logger.warning") as mock_log: + assert self.config.openssl_version() is None + assert "Could not find ssl_module" in mock_log.call_args[0][0] + + # When ssl_module is statically linked but --apache-bin not provided + self.config._openssl_version = None + self.config.options.bin = None + self.config.parser.modules['ssl_module'] = None + with mock.patch("certbot._internal.plugins.apache.configurator.logger.warning") as mock_log: + assert self.config.openssl_version() is None + assert "ssl_module is statically linked but" in mock_log.call_args[0][0] + + self.config.parser.modules['ssl_module'] = "/fake/path" + with mock.patch("certbot._internal.plugins.apache.configurator.logger.warning") as mock_log: + # Check that correct logger.warning was printed + assert self.config.openssl_version() is None + assert "Unable to read" in mock_log.call_args[0][0] + + contents_missing_openssl = b"these contents won't match the regex" + with mock.patch("certbot._internal.plugins.apache.configurator." + "ApacheConfigurator._open_module_file") as mock_omf: + mock_omf.return_value = contents_missing_openssl + with mock.patch("certbot._internal.plugins.apache.configurator.logger.warning") as mock_log: + # Check that correct logger.warning was printed + assert self.config.openssl_version() is None + assert "Could not find OpenSSL" in mock_log.call_args[0][0] + + def test_open_module_file(self): + mock_open = mock.mock_open(read_data="testing 12 3") + with mock.patch("builtins.open", mock_open): + assert self.config._open_module_file("/nonsense/") == "testing 12 3" + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/conftest.py b/certbot/src/certbot/_internal/tests/plugins/apache/conftest.py new file mode 100644 index 00000000000..1cddad61316 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/conftest.py @@ -0,0 +1,8 @@ +from unittest import mock + +import pytest + +@pytest.fixture(autouse=True) +def mock_sleep(): + with mock.patch("time.sleep") as mocked: + yield mocked diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/constants_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/constants_test.py new file mode 100644 index 00000000000..d155b85be7d --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/constants_test.py @@ -0,0 +1,15 @@ +"""Tests for certbot._internal.plugins.apache.constants""" +import sys +import os.path + +import pytest + +from certbot._internal.plugins.apache import constants + + +def test_augeas_lens_dir_exists(): + assert os.path.exists(constants.AUGEAS_LENS_DIR) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/debian_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/debian_test.py new file mode 100644 index 00000000000..06563b9deb7 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/debian_test.py @@ -0,0 +1,211 @@ +"""Test for certbot._internal.plugins.apache.configurator for Debian overrides""" +import shutil +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot.compat import os +from certbot.tests import util as certbot_util +from certbot._internal.plugins.apache import apache_util +from certbot._internal.plugins.apache import obj +from certbot._internal.tests.plugins.apache import util + + +class MultipleVhostsTestDebian(util.ApacheTest): + """Multiple vhost tests for Debian family of distros""" + + def setUp(self): # pylint: disable=arguments-differ + super().setUp() + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir, + os_info="debian") + self.config = self.mock_deploy_cert(self.config) + self.vh_truth = util.get_vh_truth(self.temp_dir, + "debian_apache_2_4/multiple_vhosts") + + def mock_deploy_cert(self, config): + """A test for a mock deploy cert""" + config.real_deploy_cert = self.config.deploy_cert + + def mocked_deploy_cert(*args, **kwargs): + """a helper to mock a deployed cert""" + g_mod = "certbot._internal.plugins.apache.configurator.ApacheConfigurator.enable_mod" + d_mod = "certbot._internal.plugins.apache.override_debian.DebianConfigurator.enable_mod" + with mock.patch(g_mod): + with mock.patch(d_mod): + config.real_deploy_cert(*args, **kwargs) + self.config.deploy_cert = mocked_deploy_cert + return self.config + + def test_enable_mod_unsupported_dirs(self): + shutil.rmtree(os.path.join(self.config.parser.root, "mods-enabled")) + with pytest.raises(errors.NotSupportedError): + self.config.enable_mod("ssl") + + @mock.patch("certbot.util.run_script") + @mock.patch("certbot.util.exe_exists") + @mock.patch("certbot._internal.plugins.apache.apache_util.subprocess.run") + def test_enable_mod(self, mock_run, mock_exe_exists, mock_run_script): + mock_run.return_value.stdout = "Define: DUMP_RUN_CFG" + mock_run.return_value.stderr = "" + mock_run.return_value.returncode = 0 + mock_exe_exists.return_value = True + + self.config.enable_mod("ssl") + assert "ssl_module" in self.config.parser.modules + assert "mod_ssl.c" in self.config.parser.modules + + assert mock_run_script.called is True + + def test_deploy_cert_enable_new_vhost(self): + # Create + ssl_vhost = self.config.make_vhost_ssl(self.vh_truth[0]) + self.config.parser.modules["ssl_module"] = None + self.config.parser.modules["mod_ssl.c"] = None + assert ssl_vhost.enabled is False + with certbot_util.patch_display_util(): + self.config.deploy_cert( + "encryption-example.demo", "example/cert.pem", "example/key.pem", + "example/cert_chain.pem", "example/fullchain.pem") + assert ssl_vhost.enabled is True + # Make sure that we don't error out if symlink already exists + ssl_vhost.enabled = False + assert ssl_vhost.enabled is False + self.config.deploy_cert( + "encryption-example.demo", "example/cert.pem", "example/key.pem", + "example/cert_chain.pem", "example/fullchain.pem") + assert ssl_vhost.enabled is True + + def test_enable_site_failure(self): + self.config.parser.root = "/tmp/nonexistent" + with mock.patch("certbot.compat.os.path.isdir") as mock_dir: + mock_dir.return_value = True + with mock.patch("certbot.compat.os.path.islink") as mock_link: + mock_link.return_value = False + with pytest.raises(errors.NotSupportedError): + self.config.enable_site(obj.VirtualHost("asdf", "afsaf", set(), False, False)) + + def test_deploy_cert_newssl(self): + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, + self.work_dir, version=(2, 4, 16)) + self.config = self.mock_deploy_cert(self.config) + self.config.parser.modules["ssl_module"] = None + self.config.parser.modules["mod_ssl.c"] = None + + # Get the default 443 vhost + self.config.assoc["random.demo"] = self.vh_truth[1] + with certbot_util.patch_display_util(): + self.config.deploy_cert( + "random.demo", "example/cert.pem", "example/key.pem", + "example/cert_chain.pem", "example/fullchain.pem") + self.config.save() + + # Verify ssl_module was enabled. + assert self.vh_truth[1].enabled is True + assert "ssl_module" in self.config.parser.modules + + loc_cert = self.config.parser.find_dir( + "sslcertificatefile", "example/fullchain.pem", + self.vh_truth[1].path) + loc_key = self.config.parser.find_dir( + "sslcertificateKeyfile", "example/key.pem", self.vh_truth[1].path) + + # Verify one directive was found in the correct file + assert len(loc_cert) == 1 + assert apache_util.get_file_path(loc_cert[0]) == \ + self.vh_truth[1].filep + + assert len(loc_key) == 1 + assert apache_util.get_file_path(loc_key[0]) == \ + self.vh_truth[1].filep + + def test_deploy_cert_newssl_no_fullchain(self): + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, + self.work_dir, version=(2, 4, 16)) + self.config = self.mock_deploy_cert(self.config) + self.config.parser.modules["ssl_module"] = None + self.config.parser.modules["mod_ssl.c"] = None + + # Get the default 443 vhost + self.config.assoc["random.demo"] = self.vh_truth[1] + with pytest.raises(errors.PluginError): + self.config.deploy_cert( + "random.demo", "example/cert.pem", + "example/key.pem") + + def test_deploy_cert_old_apache_no_chain(self): + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, + self.work_dir, version=(2, 4, 7)) + self.config = self.mock_deploy_cert(self.config) + self.config.parser.modules["ssl_module"] = None + self.config.parser.modules["mod_ssl.c"] = None + + # Get the default 443 vhost + self.config.assoc["random.demo"] = self.vh_truth[1] + with pytest.raises(errors.PluginError): + self.config.deploy_cert( + "random.demo", "example/cert.pem", + "example/key.pem") + + @mock.patch("certbot.util.run_script") + @mock.patch("certbot.util.exe_exists") + def test_ocsp_stapling_enable_mod(self, mock_exe, _): + self.config.parser.update_runtime_variables = mock.Mock() + self.config.parser.modules["mod_ssl.c"] = None + self.config.get_version = mock.Mock(return_value=(2, 4, 7)) + mock_exe.return_value = True + # This will create an ssl vhost for certbot.demo + self.config.choose_vhost("certbot.demo") + self.config.enhance("certbot.demo", "staple-ocsp") + assert "socache_shmcb_module" in self.config.parser.modules + + @mock.patch("certbot.util.run_script") + @mock.patch("certbot.util.exe_exists") + def test_ensure_http_header_enable_mod(self, mock_exe, _): + self.config.parser.update_runtime_variables = mock.Mock() + self.config.parser.modules["mod_ssl.c"] = None + mock_exe.return_value = True + + # This will create an ssl vhost for certbot.demo + self.config.choose_vhost("certbot.demo") + self.config.enhance("certbot.demo", "ensure-http-header", + "Strict-Transport-Security") + assert "headers_module" in self.config.parser.modules + + @mock.patch("certbot.util.run_script") + @mock.patch("certbot.util.exe_exists") + def test_redirect_enable_mod(self, mock_exe, _): + self.config.parser.update_runtime_variables = mock.Mock() + mock_exe.return_value = True + self.config.get_version = mock.Mock(return_value=(2, 2)) + # This will create an ssl vhost for certbot.demo + self.config.choose_vhost("certbot.demo") + self.config.enhance("certbot.demo", "redirect") + assert "rewrite_module" in self.config.parser.modules + + def test_enable_site_already_enabled(self): + assert self.vh_truth[1].enabled is True + self.config.enable_site(self.vh_truth[1]) + + def test_enable_site_call_parent(self): + with mock.patch( + "certbot._internal.plugins.apache.configurator.ApacheConfigurator.enable_site") as e_s: + self.config.parser.root = "/tmp/nonexistent" + vh = self.vh_truth[0] + vh.enabled = False + self.config.enable_site(vh) + assert e_s.called is True + + @mock.patch("certbot.util.exe_exists") + def test_enable_mod_no_disable(self, mock_exe_exists): + mock_exe_exists.return_value = False + with pytest.raises(errors.MisconfigurationError): + self.config.enable_mod("ssl") + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/display_ops_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/display_ops_test.py new file mode 100644 index 00000000000..925b86968f7 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/display_ops_test.py @@ -0,0 +1,106 @@ +"""Test certbot._internal.plugins.apache.display_ops.""" +import sys +import unittest +from unittest import mock + +import pytest + +from certbot import errors +from certbot.display import util as display_util +from certbot.tests import util as certbot_util +from certbot._internal.plugins.apache import obj +from certbot._internal.plugins.apache.display_ops import select_vhost_multiple +from certbot._internal.tests.plugins.apache import util + + +class SelectVhostMultiTest(unittest.TestCase): + """Tests for certbot._internal.plugins.apache.display_ops.select_vhost_multiple.""" + + def setUp(self): + self.base_dir = "/example_path" + self.vhosts = util.get_vh_truth( + self.base_dir, "debian_apache_2_4/multiple_vhosts") + + def test_select_no_input(self): + assert len(select_vhost_multiple([])) == 0 + + @certbot_util.patch_display_util() + def test_select_correct(self, mock_util): + mock_util().checklist.return_value = ( + display_util.OK, [self.vhosts[3].display_repr(), + self.vhosts[2].display_repr()]) + vhs = select_vhost_multiple([self.vhosts[3], + self.vhosts[2], + self.vhosts[1]]) + assert self.vhosts[2] in vhs + assert self.vhosts[3] in vhs + assert self.vhosts[1] not in vhs + + @certbot_util.patch_display_util() + def test_select_cancel(self, mock_util): + mock_util().checklist.return_value = (display_util.CANCEL, "whatever") + vhs = select_vhost_multiple([self.vhosts[2], self.vhosts[3]]) + assert vhs == [] + + +class SelectVhostTest(unittest.TestCase): + """Tests for certbot._internal.plugins.apache.display_ops.select_vhost.""" + + def setUp(self): + self.base_dir = "/example_path" + self.vhosts = util.get_vh_truth( + self.base_dir, "debian_apache_2_4/multiple_vhosts") + + @classmethod + def _call(cls, vhosts): + from certbot._internal.plugins.apache.display_ops import select_vhost + return select_vhost("example.com", vhosts) + + @certbot_util.patch_display_util() + def test_successful_choice(self, mock_util): + mock_util().menu.return_value = (display_util.OK, 3) + assert self.vhosts[3] == self._call(self.vhosts) + + @certbot_util.patch_display_util() + def test_noninteractive(self, mock_util): + mock_util().menu.side_effect = errors.MissingCommandlineFlag("no vhost default") + try: + self._call(self.vhosts) + except errors.MissingCommandlineFlag as e: + assert "vhost ambiguity" in str(e) + + @certbot_util.patch_display_util() + def test_more_info_cancel(self, mock_util): + mock_util().menu.side_effect = [ + (display_util.CANCEL, -1), + ] + + assert self._call(self.vhosts) is None + + def test_no_vhosts(self): + assert self._call([]) is None + + @mock.patch("certbot._internal.plugins.apache.display_ops.display_util") + @mock.patch("certbot._internal.plugins.apache.display_ops.logger") + def test_small_display(self, mock_logger, mock_display_util): + mock_display_util.WIDTH = 20 + mock_display_util.menu.return_value = (display_util.OK, 0) + self._call(self.vhosts) + + assert mock_logger.debug.call_count == 1 + + @certbot_util.patch_display_util() + def test_multiple_names(self, mock_util): + mock_util().menu.return_value = (display_util.OK, 5) + + self.vhosts.append( + obj.VirtualHost( + "path", "aug_path", {obj.Addr.fromstring("*:80")}, + False, False, + "wildcard.com", {"*.wildcard.com"})) + + assert self.vhosts[5] == self._call(self.vhosts) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/dualnode_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/dualnode_test.py new file mode 100644 index 00000000000..4778a324d56 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/dualnode_test.py @@ -0,0 +1,445 @@ +"""Tests for DualParserNode implementation""" +import unittest +from unittest import mock + +from certbot._internal.plugins.apache import assertions +from certbot._internal.plugins.apache import augeasparser +from certbot._internal.plugins.apache import dualparser +import pytest + + +class DualParserNodeTest(unittest.TestCase): # pylint: disable=too-many-public-methods + """DualParserNode tests""" + + def setUp(self): # pylint: disable=arguments-differ + parser_mock = mock.MagicMock() + parser_mock.aug.match.return_value = [] + parser_mock.get_arg.return_value = [] + self.metadata = {"augeasparser": parser_mock, "augeaspath": "/invalid", "ac_ast": None} + self.block = dualparser.DualBlockNode(name="block", + ancestor=None, + filepath="/tmp/something", + metadata=self.metadata) + self.block_two = dualparser.DualBlockNode(name="block", + ancestor=self.block, + filepath="/tmp/something", + metadata=self.metadata) + self.directive = dualparser.DualDirectiveNode(name="directive", + ancestor=self.block, + filepath="/tmp/something", + metadata=self.metadata) + self.comment = dualparser.DualCommentNode(comment="comment", + ancestor=self.block, + filepath="/tmp/something", + metadata=self.metadata) + + def test_create_with_precreated(self): + cnode = dualparser.DualCommentNode(comment="comment", + ancestor=self.block, + filepath="/tmp/something", + primary=self.comment.secondary, + secondary=self.comment.primary) + dnode = dualparser.DualDirectiveNode(name="directive", + ancestor=self.block, + filepath="/tmp/something", + primary=self.directive.secondary, + secondary=self.directive.primary) + bnode = dualparser.DualBlockNode(name="block", + ancestor=self.block, + filepath="/tmp/something", + primary=self.block.secondary, + secondary=self.block.primary) + # Switched around + assert cnode.primary == self.comment.secondary + assert cnode.secondary == self.comment.primary + assert dnode.primary == self.directive.secondary + assert dnode.secondary == self.directive.primary + assert bnode.primary == self.block.secondary + assert bnode.secondary == self.block.primary + + def test_set_params(self): + params = ("first", "second") + self.directive.primary.set_parameters = mock.Mock() + self.directive.secondary.set_parameters = mock.Mock() + self.directive.set_parameters(params) + assert self.directive.primary.set_parameters.called is True + assert self.directive.secondary.set_parameters.called is True + + def test_set_parameters(self): + pparams = mock.MagicMock() + sparams = mock.MagicMock() + pparams.parameters = ("a", "b") + sparams.parameters = ("a", "b") + self.directive.primary.set_parameters = pparams + self.directive.secondary.set_parameters = sparams + self.directive.set_parameters(("param", "seq")) + assert pparams.called is True + assert sparams.called is True + + def test_delete_child(self): + pdel = mock.MagicMock() + sdel = mock.MagicMock() + self.block.primary.delete_child = pdel + self.block.secondary.delete_child = sdel + self.block.delete_child(self.comment) + assert pdel.called is True + assert sdel.called is True + + def test_unsaved_files(self): + puns = mock.MagicMock() + suns = mock.MagicMock() + puns.return_value = assertions.PASS + suns.return_value = assertions.PASS + self.block.primary.unsaved_files = puns + self.block.secondary.unsaved_files = suns + self.block.unsaved_files() + assert puns.called is True + assert suns.called is True + + def test_getattr_equality(self): + self.directive.primary.variableexception = "value" + self.directive.secondary.variableexception = "not_value" + with pytest.raises(AssertionError): + _ = self.directive.variableexception + + self.directive.primary.variable = "value" + self.directive.secondary.variable = "value" + try: + self.directive.variable + except AssertionError: # pragma: no cover + self.fail("getattr check raised an AssertionError where it shouldn't have") + + def test_parsernode_dirty_assert(self): + # disable assertion pass + self.comment.primary.comment = "value" + self.comment.secondary.comment = "value" + self.comment.primary.filepath = "x" + self.comment.secondary.filepath = "x" + + self.comment.primary.dirty = False + self.comment.secondary.dirty = True + with pytest.raises(AssertionError): + assertions.assertEqual(self.comment.primary, self.comment.secondary) + + def test_parsernode_filepath_assert(self): + # disable assertion pass + self.comment.primary.comment = "value" + self.comment.secondary.comment = "value" + + self.comment.primary.filepath = "first" + self.comment.secondary.filepath = "second" + with pytest.raises(AssertionError): + assertions.assertEqual(self.comment.primary, self.comment.secondary) + + def test_add_child_block(self): + mock_first = mock.MagicMock(return_value=self.block.primary) + mock_second = mock.MagicMock(return_value=self.block.secondary) + self.block.primary.add_child_block = mock_first + self.block.secondary.add_child_block = mock_second + self.block.add_child_block("Block") + assert mock_first.called is True + assert mock_second.called is True + + def test_add_child_directive(self): + mock_first = mock.MagicMock(return_value=self.directive.primary) + mock_second = mock.MagicMock(return_value=self.directive.secondary) + self.block.primary.add_child_directive = mock_first + self.block.secondary.add_child_directive = mock_second + self.block.add_child_directive("Directive") + assert mock_first.called is True + assert mock_second.called is True + + def test_add_child_comment(self): + mock_first = mock.MagicMock(return_value=self.comment.primary) + mock_second = mock.MagicMock(return_value=self.comment.secondary) + self.block.primary.add_child_comment = mock_first + self.block.secondary.add_child_comment = mock_second + self.block.add_child_comment("Comment") + assert mock_first.called is True + assert mock_second.called is True + + def test_find_comments(self): + pri_comments = [augeasparser.AugeasCommentNode(comment="some comment", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + sec_comments = [augeasparser.AugeasCommentNode(comment=assertions.PASS, + ancestor=self.block, + filepath=assertions.PASS, + metadata=self.metadata)] + find_coms_primary = mock.MagicMock(return_value=pri_comments) + find_coms_secondary = mock.MagicMock(return_value=sec_comments) + self.block.primary.find_comments = find_coms_primary + self.block.secondary.find_comments = find_coms_secondary + + dcoms = self.block.find_comments("comment") + p_dcoms = [d.primary for d in dcoms] + s_dcoms = [d.secondary for d in dcoms] + p_coms = self.block.primary.find_comments("comment") + s_coms = self.block.secondary.find_comments("comment") + # Check that every comment response is represented in the list of + # DualParserNode instances. + for p in p_dcoms: + assert p in p_coms + for s in s_dcoms: + assert s in s_coms + + def test_find_blocks_first_passing(self): + youshallnotpass = [augeasparser.AugeasBlockNode(name="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + youshallpass = [augeasparser.AugeasBlockNode(name=assertions.PASS, + ancestor=self.block, + filepath=assertions.PASS, + metadata=self.metadata)] + find_blocks_primary = mock.MagicMock(return_value=youshallpass) + find_blocks_secondary = mock.MagicMock(return_value=youshallnotpass) + self.block.primary.find_blocks = find_blocks_primary + self.block.secondary.find_blocks = find_blocks_secondary + + blocks = self.block.find_blocks("something") + for block in blocks: + try: + assertions.assertEqual(block.primary, block.secondary) + except AssertionError: # pragma: no cover + self.fail("Assertion should have passed") + assert assertions.isPassDirective(block.primary) is True + assert assertions.isPassDirective(block.secondary) is False + + def test_find_blocks_second_passing(self): + youshallnotpass = [augeasparser.AugeasBlockNode(name="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + youshallpass = [augeasparser.AugeasBlockNode(name=assertions.PASS, + ancestor=self.block, + filepath=assertions.PASS, + metadata=self.metadata)] + find_blocks_primary = mock.MagicMock(return_value=youshallnotpass) + find_blocks_secondary = mock.MagicMock(return_value=youshallpass) + self.block.primary.find_blocks = find_blocks_primary + self.block.secondary.find_blocks = find_blocks_secondary + + blocks = self.block.find_blocks("something") + for block in blocks: + try: + assertions.assertEqual(block.primary, block.secondary) + except AssertionError: # pragma: no cover + self.fail("Assertion should have passed") + assert assertions.isPassDirective(block.primary) is False + assert assertions.isPassDirective(block.secondary) is True + + def test_find_dirs_first_passing(self): + notpassing = [augeasparser.AugeasDirectiveNode(name="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + passing = [augeasparser.AugeasDirectiveNode(name=assertions.PASS, + ancestor=self.block, + filepath=assertions.PASS, + metadata=self.metadata)] + find_dirs_primary = mock.MagicMock(return_value=passing) + find_dirs_secondary = mock.MagicMock(return_value=notpassing) + self.block.primary.find_directives = find_dirs_primary + self.block.secondary.find_directives = find_dirs_secondary + + directives = self.block.find_directives("something") + for directive in directives: + try: + assertions.assertEqual(directive.primary, directive.secondary) + except AssertionError: # pragma: no cover + self.fail("Assertion should have passed") + assert assertions.isPassDirective(directive.primary) is True + assert assertions.isPassDirective(directive.secondary) is False + + def test_find_dirs_second_passing(self): + notpassing = [augeasparser.AugeasDirectiveNode(name="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + passing = [augeasparser.AugeasDirectiveNode(name=assertions.PASS, + ancestor=self.block, + filepath=assertions.PASS, + metadata=self.metadata)] + find_dirs_primary = mock.MagicMock(return_value=notpassing) + find_dirs_secondary = mock.MagicMock(return_value=passing) + self.block.primary.find_directives = find_dirs_primary + self.block.secondary.find_directives = find_dirs_secondary + + directives = self.block.find_directives("something") + for directive in directives: + try: + assertions.assertEqual(directive.primary, directive.secondary) + except AssertionError: # pragma: no cover + self.fail("Assertion should have passed") + assert assertions.isPassDirective(directive.primary) is False + assert assertions.isPassDirective(directive.secondary) is True + + def test_find_coms_first_passing(self): + notpassing = [augeasparser.AugeasCommentNode(comment="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + passing = [augeasparser.AugeasCommentNode(comment=assertions.PASS, + ancestor=self.block, + filepath=assertions.PASS, + metadata=self.metadata)] + find_coms_primary = mock.MagicMock(return_value=passing) + find_coms_secondary = mock.MagicMock(return_value=notpassing) + self.block.primary.find_comments = find_coms_primary + self.block.secondary.find_comments = find_coms_secondary + + comments = self.block.find_comments("something") + for comment in comments: + try: + assertions.assertEqual(comment.primary, comment.secondary) + except AssertionError: # pragma: no cover + self.fail("Assertion should have passed") + assert assertions.isPassComment(comment.primary) is True + assert assertions.isPassComment(comment.secondary) is False + + def test_find_coms_second_passing(self): + notpassing = [augeasparser.AugeasCommentNode(comment="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + passing = [augeasparser.AugeasCommentNode(comment=assertions.PASS, + ancestor=self.block, + filepath=assertions.PASS, + metadata=self.metadata)] + find_coms_primary = mock.MagicMock(return_value=notpassing) + find_coms_secondary = mock.MagicMock(return_value=passing) + self.block.primary.find_comments = find_coms_primary + self.block.secondary.find_comments = find_coms_secondary + + comments = self.block.find_comments("something") + for comment in comments: + try: + assertions.assertEqual(comment.primary, comment.secondary) + except AssertionError: # pragma: no cover + self.fail("Assertion should have passed") + assert assertions.isPassComment(comment.primary) is False + assert assertions.isPassComment(comment.secondary) is True + + def test_find_blocks_no_pass_equal(self): + notpassing1 = [augeasparser.AugeasBlockNode(name="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + notpassing2 = [augeasparser.AugeasBlockNode(name="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + find_blocks_primary = mock.MagicMock(return_value=notpassing1) + find_blocks_secondary = mock.MagicMock(return_value=notpassing2) + self.block.primary.find_blocks = find_blocks_primary + self.block.secondary.find_blocks = find_blocks_secondary + + blocks = self.block.find_blocks("anything") + assert len(blocks) == 1 + for block in blocks: + assert block.primary == block.secondary + assert block.primary is not block.secondary + + def test_find_dirs_no_pass_equal(self): + notpassing1 = [augeasparser.AugeasDirectiveNode(name="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + notpassing2 = [augeasparser.AugeasDirectiveNode(name="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + find_dirs_primary = mock.MagicMock(return_value=notpassing1) + find_dirs_secondary = mock.MagicMock(return_value=notpassing2) + self.block.primary.find_directives = find_dirs_primary + self.block.secondary.find_directives = find_dirs_secondary + + directives = self.block.find_directives("anything") + assert len(directives) == 1 + for directive in directives: + assert directive.primary == directive.secondary + assert directive.primary is not directive.secondary + + def test_find_comments_no_pass_equal(self): + notpassing1 = [augeasparser.AugeasCommentNode(comment="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + notpassing2 = [augeasparser.AugeasCommentNode(comment="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + find_coms_primary = mock.MagicMock(return_value=notpassing1) + find_coms_secondary = mock.MagicMock(return_value=notpassing2) + self.block.primary.find_comments = find_coms_primary + self.block.secondary.find_comments = find_coms_secondary + + comments = self.block.find_comments("anything") + assert len(comments) == 1 + for comment in comments: + assert comment.primary == comment.secondary + assert comment.primary is not comment.secondary + + def test_find_blocks_no_pass_notequal(self): + notpassing1 = [augeasparser.AugeasBlockNode(name="notpassing", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + notpassing2 = [augeasparser.AugeasBlockNode(name="different", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata)] + find_blocks_primary = mock.MagicMock(return_value=notpassing1) + find_blocks_secondary = mock.MagicMock(return_value=notpassing2) + self.block.primary.find_blocks = find_blocks_primary + self.block.secondary.find_blocks = find_blocks_secondary + + with pytest.raises(AssertionError): + _ = self.block.find_blocks("anything") + + def test_parsernode_notequal(self): + ne_block = augeasparser.AugeasBlockNode(name="different", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata) + ne_directive = augeasparser.AugeasDirectiveNode(name="different", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata) + ne_comment = augeasparser.AugeasCommentNode(comment="different", + ancestor=self.block, + filepath="/path/to/whatever", + metadata=self.metadata) + assert self.block != ne_block + assert self.directive != ne_directive + assert self.comment != ne_comment + + def test_parsed_paths(self): + mock_p = mock.MagicMock(return_value=['/path/file.conf', + '/another/path', + '/path/other.conf']) + mock_s = mock.MagicMock(return_value=['/path/*.conf', '/another/path']) + self.block.primary.parsed_paths = mock_p + self.block.secondary.parsed_paths = mock_s + self.block.parsed_paths() + assert mock_p.called is True + assert mock_s.called is True + + def test_parsed_paths_error(self): + mock_p = mock.MagicMock(return_value=['/path/file.conf']) + mock_s = mock.MagicMock(return_value=['/path/*.conf', '/another/path']) + self.block.primary.parsed_paths = mock_p + self.block.secondary.parsed_paths = mock_s + with pytest.raises(AssertionError): + self.block.parsed_paths() + + def test_find_ancestors(self): + primarymock = mock.MagicMock(return_value=[]) + secondarymock = mock.MagicMock(return_value=[]) + self.block.primary.find_ancestors = primarymock + self.block.secondary.find_ancestors = secondarymock + self.block.find_ancestors("anything") + assert primarymock.called is True + assert secondarymock.called is True diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/entrypoint_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/entrypoint_test.py new file mode 100644 index 00000000000..23d7acd550e --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/entrypoint_test.py @@ -0,0 +1,42 @@ +"""Test for certbot._internal.plugins.apache.entrypoint for override class resolution""" +import sys +from unittest import mock + +import pytest + +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache import entrypoint + + +def test_get_configurator(): + with mock.patch("certbot.util.get_os_info") as mock_info: + for distro in entrypoint.OVERRIDE_CLASSES: + return_value = (distro, "whatever") + if distro == 'fedora_old': + return_value = ('fedora', '28') + elif distro == 'fedora': + return_value = ('fedora', '29') + mock_info.return_value = return_value + assert entrypoint.get_configurator() == \ + entrypoint.OVERRIDE_CLASSES[distro] + +def test_nonexistent_like(): + with mock.patch("certbot.util.get_os_info") as mock_info: + mock_info.return_value = ("nonexistent", "irrelevant") + with mock.patch("certbot.util.get_systemd_os_like") as mock_like: + for like in entrypoint.OVERRIDE_CLASSES: + mock_like.return_value = [like] + assert entrypoint.get_configurator() == \ + entrypoint.OVERRIDE_CLASSES[like] + +def test_nonexistent_generic(): + with mock.patch("certbot.util.get_os_info") as mock_info: + mock_info.return_value = ("nonexistent", "irrelevant") + with mock.patch("certbot.util.get_systemd_os_like") as mock_like: + mock_like.return_value = ["unknown"] + assert entrypoint.get_configurator() == \ + configurator.ApacheConfigurator + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/fedora_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/fedora_test.py new file mode 100644 index 00000000000..8e515a02dc3 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/fedora_test.py @@ -0,0 +1,195 @@ +"""Test for certbot._internal.plugins.apache.configurator for Fedora 29+ overrides""" +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +from certbot._internal.plugins.apache import obj +from certbot._internal.plugins.apache import override_fedora +from certbot._internal.tests.plugins.apache import util + + +def get_vh_truth(temp_dir, config_name): + """Return the ground truth for the specified directory.""" + prefix = os.path.join( + temp_dir, config_name, "httpd/conf.d") + + aug_pre = "/files" + prefix + # TODO: eventually, these tests should have a dedicated configuration instead + # of reusing the ones from centos_test + vh_truth = [ + obj.VirtualHost( + os.path.join(prefix, "centos.example.com.conf"), + os.path.join(aug_pre, "centos.example.com.conf/VirtualHost"), + {obj.Addr.fromstring("*:80")}, + False, True, "centos.example.com"), + obj.VirtualHost( + os.path.join(prefix, "ssl.conf"), + os.path.join(aug_pre, "ssl.conf/VirtualHost"), + {obj.Addr.fromstring("_default_:443")}, + True, True, None) + ] + return vh_truth + + +class FedoraRestartTest(util.ApacheTest): + """Tests for Fedora specific self-signed certificate override""" + + # TODO: eventually, these tests should have a dedicated configuration instead + # of reusing the ones from centos_test + def setUp(self): # pylint: disable=arguments-differ + test_dir = "centos7_apache/apache" + config_root = "centos7_apache/apache/httpd" + vhost_root = "centos7_apache/apache/httpd/conf.d" + super().setUp(test_dir=test_dir, + config_root=config_root, + vhost_root=vhost_root) + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir, + os_info="fedora") + self.vh_truth = get_vh_truth( + self.temp_dir, "centos7_apache/apache") + + def _run_fedora_test(self): + assert isinstance(self.config, override_fedora.FedoraConfigurator) + self.config.config_test() + + def test_fedora_restart_error(self): + c_test = "certbot._internal.plugins.apache.configurator.ApacheConfigurator.config_test" + with mock.patch(c_test) as mock_test: + # First call raises error, second doesn't + mock_test.side_effect = [errors.MisconfigurationError, ''] + with mock.patch("certbot.util.run_script") as mock_run: + mock_run.side_effect = errors.SubprocessError + with pytest.raises(errors.MisconfigurationError): + self._run_fedora_test() + + def test_fedora_restart(self): + c_test = "certbot._internal.plugins.apache.configurator.ApacheConfigurator.config_test" + with mock.patch(c_test) as mock_test: + with mock.patch("certbot.util.run_script") as mock_run: + # First call raises error, second doesn't + mock_test.side_effect = [errors.MisconfigurationError, ''] + self._run_fedora_test() + assert mock_test.call_count == 2 + assert mock_run.call_args[0][0] == \ + ['systemctl', 'restart', 'httpd'] + + +class MultipleVhostsTestFedora(util.ApacheTest): + """Multiple vhost tests for CentOS / RHEL family of distros""" + + def setUp(self): # pylint: disable=arguments-differ + test_dir = "centos7_apache/apache" + config_root = "centos7_apache/apache/httpd" + vhost_root = "centos7_apache/apache/httpd/conf.d" + super().setUp(test_dir=test_dir, + config_root=config_root, + vhost_root=vhost_root) + + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir, + os_info="fedora") + self.vh_truth = get_vh_truth( + self.temp_dir, "centos7_apache/apache") + + def test_get_parser(self): + assert isinstance(self.config.parser, override_fedora.FedoraParser) + + @mock.patch("certbot._internal.plugins.apache.apache_util._get_runtime_cfg") + def test_opportunistic_httpd_runtime_parsing(self, mock_get): + define_val = ( + 'Define: TEST1\n' + 'Define: TEST2\n' + 'Define: DUMP_RUN_CFG\n' + ) + mod_val = ( + 'Loaded Modules:\n' + ' mock_module (static)\n' + ' another_module (static)\n' + ) + def mock_get_cfg(command): + """Mock httpd process stdout""" + if command == ['httpd', '-t', '-D', 'DUMP_RUN_CFG']: + return define_val + elif command == ['httpd', '-t', '-D', 'DUMP_MODULES']: + return mod_val + return "" + mock_get.side_effect = mock_get_cfg + self.config.parser.modules = {} + self.config.parser.variables = {} + + with mock.patch("certbot.util.get_os_info") as mock_osi: + # Make sure we have the have the CentOS httpd constants + mock_osi.return_value = ("fedora", "29") + self.config.parser.update_runtime_variables() + + assert mock_get.call_count == 3 + assert len(self.config.parser.modules) == 4 + assert len(self.config.parser.variables) == 2 + assert "TEST2" in self.config.parser.variables + assert "mod_another.c" in self.config.parser.modules + + @mock.patch("certbot._internal.plugins.apache.configurator.util.run_script") + def test_get_version(self, mock_run_script): + mock_run_script.return_value = ('', None) + with pytest.raises(errors.PluginError): + self.config.get_version() + assert mock_run_script.call_args[0][0][0] == 'httpd' + + def test_get_virtual_hosts(self): + """Make sure all vhosts are being properly found.""" + vhs = self.config.get_virtual_hosts() + assert len(vhs) == 2 + found = 0 + + for vhost in vhs: + for centos_truth in self.vh_truth: + if vhost == centos_truth: + found += 1 + break + else: + raise Exception("Missed: %s" % vhost) # pragma: no cover + assert found == 2 + + @mock.patch("certbot._internal.plugins.apache.apache_util._get_runtime_cfg") + def test_get_sysconfig_vars(self, mock_cfg): + """Make sure we read the sysconfig OPTIONS variable correctly""" + # Return nothing for the process calls + mock_cfg.return_value = "" + self.config.parser.sysconfig_filep = filesystem.realpath( + os.path.join(self.config.parser.root, "../sysconfig/httpd")) + self.config.parser.variables = {} + + with mock.patch("certbot.util.get_os_info") as mock_osi: + # Make sure we have the have the CentOS httpd constants + mock_osi.return_value = ("fedora", "29") + self.config.parser.update_runtime_variables() + + assert "mock_define" in self.config.parser.variables + assert "mock_define_too" in self.config.parser.variables + assert "mock_value" in self.config.parser.variables + assert "TRUE" == self.config.parser.variables["mock_value"] + assert "MOCK_NOSEP" in self.config.parser.variables + assert "NOSEP_VAL" == self.config.parser.variables["NOSEP_TWO"] + + @mock.patch("certbot._internal.plugins.apache.configurator.util.run_script") + def test_alt_restart_works(self, mock_run_script): + mock_run_script.side_effect = [None, errors.SubprocessError, None] + self.config.restart() + assert mock_run_script.call_count == 3 + + @mock.patch("certbot._internal.plugins.apache.configurator.util.run_script") + def test_alt_restart_errors(self, mock_run_script): + mock_run_script.side_effect = [None, + errors.SubprocessError, + errors.SubprocessError] + with pytest.raises(errors.MisconfigurationError): + self.config.restart() + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/gentoo_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/gentoo_test.py new file mode 100644 index 00000000000..5baa13443fb --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/gentoo_test.py @@ -0,0 +1,136 @@ +"""Test for certbot._internal.plugins.apache.configurator for Gentoo overrides""" +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +from certbot._internal.plugins.apache import obj +from certbot._internal.plugins.apache import override_gentoo +from certbot._internal.tests.plugins.apache import util + + +def get_vh_truth(temp_dir, config_name): + """Return the ground truth for the specified directory.""" + prefix = os.path.join( + temp_dir, config_name, "apache2/vhosts.d") + + aug_pre = "/files" + prefix + vh_truth = [ + obj.VirtualHost( + os.path.join(prefix, "gentoo.example.com.conf"), + os.path.join(aug_pre, "gentoo.example.com.conf/VirtualHost"), + {obj.Addr.fromstring("*:80")}, + False, True, "gentoo.example.com"), + obj.VirtualHost( + os.path.join(prefix, "00_default_vhost.conf"), + os.path.join(aug_pre, "00_default_vhost.conf/IfDefine/VirtualHost"), + {obj.Addr.fromstring("*:80")}, + False, True, "localhost"), + obj.VirtualHost( + os.path.join(prefix, "00_default_ssl_vhost.conf"), + os.path.join(aug_pre, + "00_default_ssl_vhost.conf" + + "/IfDefine/IfDefine/IfModule/VirtualHost"), + {obj.Addr.fromstring("_default_:443")}, + True, True, "localhost") + ] + return vh_truth + +class MultipleVhostsTestGentoo(util.ApacheTest): + """Multiple vhost tests for non-debian distro""" + + def setUp(self): # pylint: disable=arguments-differ + test_dir = "gentoo_apache/apache" + config_root = "gentoo_apache/apache/apache2" + vhost_root = "gentoo_apache/apache/apache2/vhosts.d" + super().setUp(test_dir=test_dir, + config_root=config_root, + vhost_root=vhost_root) + + # pylint: disable=line-too-long + with mock.patch("certbot._internal.plugins.apache.override_gentoo.GentooParser.update_runtime_variables"): + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, self.work_dir, + os_info="gentoo") + self.vh_truth = get_vh_truth( + self.temp_dir, "gentoo_apache/apache") + + def test_get_parser(self): + assert isinstance(self.config.parser, override_gentoo.GentooParser) + + def test_get_virtual_hosts(self): + """Make sure all vhosts are being properly found.""" + vhs = self.config.get_virtual_hosts() + assert len(vhs) == 3 + found = 0 + + for vhost in vhs: + for gentoo_truth in self.vh_truth: + if vhost == gentoo_truth: + found += 1 + break + else: + raise Exception("Missed: %s" % vhost) # pragma: no cover + assert found == 3 + + def test_get_sysconfig_vars(self): + """Make sure we read the Gentoo APACHE2_OPTS variable correctly""" + defines = ['DEFAULT_VHOST', 'INFO', + 'SSL', 'SSL_DEFAULT_VHOST', 'LANGUAGE'] + self.config.parser.apacheconfig_filep = filesystem.realpath( + os.path.join(self.config.parser.root, "../conf.d/apache2")) + self.config.parser.variables = {} + with mock.patch("certbot._internal.plugins.apache.override_gentoo.GentooParser.update_modules"): + self.config.parser.update_runtime_variables() + for define in defines: + assert define in self.config.parser.variables + + @mock.patch("certbot._internal.plugins.apache.apache_util.parse_from_subprocess") + def test_no_binary_configdump(self, mock_subprocess): + """Make sure we don't call binary dumps other than modules from Apache + as this is not supported in Gentoo currently""" + + with mock.patch("certbot._internal.plugins.apache.override_gentoo.GentooParser.update_modules"): + self.config.parser.update_runtime_variables() + self.config.parser.reset_modules() + assert mock_subprocess.called is False + + self.config.parser.update_runtime_variables() + self.config.parser.reset_modules() + assert mock_subprocess.called is True + + @mock.patch("certbot._internal.plugins.apache.apache_util._get_runtime_cfg") + def test_opportunistic_httpd_runtime_parsing(self, mock_get): + mod_val = ( + 'Loaded Modules:\n' + ' mock_module (static)\n' + ' another_module (static)\n' + ) + def mock_get_cfg(command): + """Mock httpd process stdout""" + if command == ['apache2ctl', 'modules']: + return mod_val + return None # pragma: no cover + mock_get.side_effect = mock_get_cfg + self.config.parser.modules = {} + + with mock.patch("certbot.util.get_os_info") as mock_osi: + # Make sure we have the have the Gentoo httpd constants + mock_osi.return_value = ("gentoo", "123") + self.config.parser.update_runtime_variables() + + assert mock_get.call_count == 1 + assert len(self.config.parser.modules) == 4 + assert "mod_another.c" in self.config.parser.modules + + @mock.patch("certbot._internal.plugins.apache.configurator.util.run_script") + def test_alt_restart_works(self, mock_run_script): + mock_run_script.side_effect = [None, errors.SubprocessError, None] + self.config.restart() + assert mock_run_script.call_count == 3 + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/http_01_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/http_01_test.py new file mode 100644 index 00000000000..25f835fccc5 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/http_01_test.py @@ -0,0 +1,243 @@ +"""Test for certbot._internal.plugins.apache.http_01.""" +import errno +import sys +from unittest import mock + +import pytest + +from acme import challenges, messages +from certbot import achallenges +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +from certbot.tests import acme_util +from certbot._internal.plugins.apache.parser import get_aug_path +from certbot._internal.tests.plugins.apache import util + +NUM_ACHALLS = 3 + + +class ApacheHttp01Test(util.ApacheTest): + """Test for certbot._internal.plugins.apache.http_01.ApacheHttp01.""" + + def setUp(self, *args, **kwargs): # pylint: disable=arguments-differ + super().setUp(*args, **kwargs) + + self.account_key = self.rsa512jwk + self.achalls: list[achallenges.KeyAuthorizationAnnotatedChallenge] = [] + vh_truth = util.get_vh_truth( + self.temp_dir, "debian_apache_2_4/multiple_vhosts") + # Takes the vhosts for encryption-example.demo, certbot.demo + # and vhost.in.rootconf + self.vhosts = [vh_truth[0], vh_truth[3], vh_truth[10]] + + for i in range(NUM_ACHALLS): + self.achalls.append( + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=((chr(ord('a') + i).encode() * 16))), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value=self.vhosts[i].name), + account_key=self.account_key)) + + modules = ["ssl", "rewrite", "authz_core", "authz_host"] + for mod in modules: + self.config.parser.modules["mod_{0}.c".format(mod)] = None + self.config.parser.modules[mod + "_module"] = None + + from certbot._internal.plugins.apache.http_01 import ApacheHttp01 + self.http = ApacheHttp01(self.config) + + def test_empty_perform(self): + assert len(self.http.perform()) == 0 + + def test_ip_address_perform(self): + self.http.achalls = [achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=((b'a' * 16))), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_IP, value="127.0.0.1"), + account_key=self.account_key)] + with pytest.raises(errors.ConfigurationError): + self.http.perform() + + @mock.patch("certbot._internal.plugins.apache.configurator.ApacheConfigurator.enable_mod") + def test_enable_modules_apache_2_4(self, mock_enmod): + del self.config.parser.modules["authz_core_module"] + del self.config.parser.modules["mod_authz_host.c"] + + enmod_calls = self.common_enable_modules_test(mock_enmod) + assert enmod_calls[0][0][0] == "authz_core" + + def common_enable_modules_test(self, mock_enmod): + """Tests enabling mod_rewrite and other modules.""" + del self.config.parser.modules["rewrite_module"] + del self.config.parser.modules["mod_rewrite.c"] + + self.http.prepare_http01_modules() + + assert mock_enmod.called is True + calls = mock_enmod.call_args_list + other_calls = [] + for call in calls: + if call[0][0] != "rewrite": + other_calls.append(call) + + # If these lists are equal, we never enabled mod_rewrite + assert calls != other_calls + return other_calls + + def test_same_vhost(self): + vhost = next(v for v in self.config.vhosts if v.name == "certbot.demo") + achalls = [ + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=((b'a' * 16))), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value=vhost.name), + account_key=self.account_key), + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=((b'b' * 16))), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value=next(iter(vhost.aliases))), + account_key=self.account_key) + ] + self.common_perform_test(achalls, [vhost]) + + def test_anonymous_vhost(self): + vhosts = [v for v in self.config.vhosts if not v.ssl] + achalls = [ + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=((b'a' * 16))), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="something.nonexistent"), + account_key=self.account_key)] + self.common_perform_test(achalls, vhosts) + + def test_configure_multiple_vhosts(self): + vhosts = [v for v in self.config.vhosts if "duplicate.example.com" in v.get_names()] + assert len(vhosts) == 2 + achalls = [ + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=((b'a' * 16))), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="duplicate.example.com"), + account_key=self.account_key)] + self.common_perform_test(achalls, vhosts) + + def test_configure_name_and_blank(self): + domain = "certbot.demo" + vhosts = [v for v in self.config.vhosts if v.name == domain or v.name is None] + achalls = [ + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=((b'a' * 16))), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value=domain), + account_key=self.account_key), + ] + self.common_perform_test(achalls, vhosts) + + def test_no_vhost(self): + for achall in self.achalls: + self.http.add_chall(achall) + self.config.config.http01_port = 12345 + with pytest.raises(errors.PluginError): + self.http.perform() + + def test_perform_1_achall_apache_2_4(self): + self.combinations_perform_test(num_achalls=1, minor_version=4) + + def test_perform_2_achall_apache_2_4(self): + self.combinations_perform_test(num_achalls=2, minor_version=4) + + def test_perform_3_achall_apache_2_4(self): + self.combinations_perform_test(num_achalls=3, minor_version=4) + + def test_activate_disabled_vhost(self): + vhosts = [v for v in self.config.vhosts if v.name == "certbot.demo"] + achalls = [ + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=((b'a' * 16))), + "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="certbot.demo"), + account_key=self.account_key)] + vhosts[0].enabled = False + self.common_perform_test(achalls, vhosts) + matches = self.config.parser.find_dir( + "Include", vhosts[0].filep, + get_aug_path(self.config.parser.loc["default"])) + assert len(matches) == 1 + + def combinations_perform_test(self, num_achalls, minor_version): + """Test perform with the given achall count and Apache version.""" + achalls = self.achalls[:num_achalls] + vhosts = self.vhosts[:num_achalls] + self.config.version = (2, minor_version) + self.common_perform_test(achalls, vhosts) + + def common_perform_test(self, achalls, vhosts): + """Tests perform with the given achalls.""" + challenge_dir = self.http.challenge_dir + assert os.path.exists(challenge_dir) is False + for achall in achalls: + self.http.add_chall(achall) + + expected_response = [ + achall.response(self.account_key) for achall in achalls] + assert self.http.perform() == expected_response + + assert os.path.isdir(self.http.challenge_dir) is True + assert filesystem.has_min_permissions(self.http.challenge_dir, 0o755) is True + self._test_challenge_conf() + + for achall in achalls: + self._test_challenge_file(achall) + + for vhost in vhosts: + matches = self.config.parser.find_dir("Include", + self.http.challenge_conf_pre, + vhost.path) + assert len(matches) == 1 + matches = self.config.parser.find_dir("Include", + self.http.challenge_conf_post, + vhost.path) + assert len(matches) == 1 + + assert os.path.exists(challenge_dir) is True + + @mock.patch("certbot._internal.plugins.apache.http_01.filesystem.makedirs") + def test_failed_makedirs(self, mock_makedirs): + mock_makedirs.side_effect = OSError(errno.EACCES, "msg") + self.http.add_chall(self.achalls[0]) + with pytest.raises(errors.PluginError): + self.http.perform() + + def _test_challenge_conf(self): + with open(self.http.challenge_conf_pre) as f: + pre_conf_contents = f.read() + + with open(self.http.challenge_conf_post) as f: + post_conf_contents = f.read() + + assert "RewriteEngine on" in post_conf_contents + assert "RewriteRule" in pre_conf_contents + + assert self.http.challenge_dir in post_conf_contents + assert "Require all granted" in post_conf_contents + + def _test_challenge_file(self, achall): + name = os.path.join(self.http.challenge_dir, achall.chall.encode("token")) + validation = achall.validation(self.account_key) + + assert filesystem.has_min_permissions(name, 0o644) is True + with open(name, 'rb') as f: + assert f.read() == validation.encode() + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/obj_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/obj_test.py new file mode 100644 index 00000000000..8ca2b6ce099 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/obj_test.py @@ -0,0 +1,140 @@ +"""Tests for certbot._internal.plugins.apache.obj.""" +import sys +import unittest + +import pytest + + +class VirtualHostTest(unittest.TestCase): + """Test the VirtualHost class.""" + + def setUp(self): + from certbot._internal.plugins.apache.obj import Addr + from certbot._internal.plugins.apache.obj import VirtualHost + + self.addr1 = Addr.fromstring("127.0.0.1") + self.addr2 = Addr.fromstring("127.0.0.1:443") + self.addr_default = Addr.fromstring("_default_:443") + + self.vhost1 = VirtualHost( + "filep", "vh_path", {self.addr1}, False, False, "localhost") + + self.vhost1b = VirtualHost( + "filep", "vh_path", {self.addr1}, False, False, "localhost") + + self.vhost2 = VirtualHost( + "fp", "vhp", {self.addr2}, False, False, "localhost") + + def test_repr(self): + assert repr(self.addr2) == \ + "certbot._internal.plugins.apache.obj.Addr(('127.0.0.1', '443'))" + + def test_eq(self): + assert self.vhost1b == self.vhost1 + assert self.vhost1 != self.vhost2 + assert str(self.vhost1b) == str(self.vhost1) + assert self.vhost1b != 1234 + + def test_ne(self): + assert self.vhost1 != self.vhost2 + assert self.vhost1 == self.vhost1b + + def test_conflicts(self): + from certbot._internal.plugins.apache.obj import Addr + from certbot._internal.plugins.apache.obj import VirtualHost + + complex_vh = VirtualHost( + "fp", "vhp", + {Addr.fromstring("*:443"), Addr.fromstring("1.2.3.4:443")}, + False, False) + assert complex_vh.conflicts([self.addr1]) is True + assert complex_vh.conflicts([self.addr2]) is True + assert complex_vh.conflicts([self.addr_default]) is False + + assert self.vhost1.conflicts([self.addr2]) is True + assert self.vhost1.conflicts([self.addr_default]) is False + + assert self.vhost2.conflicts([self.addr1, self.addr_default]) is False + + def test_same_server(self): + from certbot._internal.plugins.apache.obj import VirtualHost + no_name1 = VirtualHost( + "fp", "vhp", {self.addr1}, False, False, None) + no_name2 = VirtualHost( + "fp", "vhp", {self.addr2}, False, False, None) + no_name3 = VirtualHost( + "fp", "vhp", {self.addr_default}, + False, False, None) + no_name4 = VirtualHost( + "fp", "vhp", {self.addr2, self.addr_default}, + False, False, None) + + assert self.vhost1.same_server(self.vhost2) is True + assert no_name1.same_server(no_name2) is True + + assert self.vhost1.same_server(no_name1) is False + assert no_name1.same_server(no_name3) is False + assert no_name1.same_server(no_name4) is False + + +class AddrTest(unittest.TestCase): + """Test obj.Addr.""" + def setUp(self): + from certbot._internal.plugins.apache.obj import Addr + self.addr = Addr.fromstring("*:443") + + self.addr1 = Addr.fromstring("127.0.0.1") + self.addr2 = Addr.fromstring("127.0.0.1:*") + + self.addr_defined = Addr.fromstring("127.0.0.1:443") + self.addr_default = Addr.fromstring("_default_:443") + + def test_wildcard(self): + assert self.addr.is_wildcard() is False + assert self.addr1.is_wildcard() is True + assert self.addr2.is_wildcard() is True + + def test_get_sni_addr(self): + from certbot._internal.plugins.apache.obj import Addr + assert self.addr.get_sni_addr("443") == Addr.fromstring("*:443") + assert self.addr.get_sni_addr("225") == Addr.fromstring("*:225") + assert self.addr1.get_sni_addr("443") == Addr.fromstring("127.0.0.1") + + def test_conflicts(self): + # Note: Defined IP is more important than defined port in match + assert self.addr.conflicts(self.addr1) is True + assert self.addr.conflicts(self.addr2) is True + assert self.addr.conflicts(self.addr_defined) is True + assert self.addr.conflicts(self.addr_default) is False + + assert self.addr1.conflicts(self.addr) is False + assert self.addr1.conflicts(self.addr_defined) is True + assert self.addr1.conflicts(self.addr_default) is False + + assert self.addr_defined.conflicts(self.addr1) is False + assert self.addr_defined.conflicts(self.addr2) is False + assert self.addr_defined.conflicts(self.addr) is False + assert self.addr_defined.conflicts(self.addr_default) is False + + assert self.addr_default.conflicts(self.addr) is True + assert self.addr_default.conflicts(self.addr1) is True + assert self.addr_default.conflicts(self.addr_defined) is True + + # Self test + assert self.addr.conflicts(self.addr) is True + assert self.addr1.conflicts(self.addr1) is True + # This is a tricky one... + assert self.addr1.conflicts(self.addr2) is True + + def test_equal(self): + assert self.addr1 == self.addr2 + assert self.addr != self.addr1 + assert self.addr != 123 + + def test_not_equal(self): + assert self.addr1 == self.addr2 + assert self.addr != self.addr1 + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/parser_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/parser_test.py new file mode 100644 index 00000000000..327bc864f86 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/parser_test.py @@ -0,0 +1,385 @@ +"""Tests for certbot._internal.plugins.apache.parser.""" +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot.compat import os +from certbot._internal.tests.plugins.apache import util + + +class BasicParserTest(util.ParserTest): + """Apache Parser Test.""" + + def test_bad_parse(self): + self.parser.parse_file(os.path.join(self.parser.root, + "conf-available", "bad_conf_file.conf")) + with pytest.raises(errors.PluginError): + self.parser.check_parsing_errors("httpd.aug") + + def test_bad_save(self): + mock_save = mock.Mock() + mock_save.side_effect = IOError + self.parser.aug.save = mock_save + with pytest.raises(errors.PluginError): + self.parser.unsaved_files() + + @mock.patch("certbot._internal.plugins.apache.parser.logger") + def test_bad_save_errors(self, mock_logger): + nx_path = "/non/existent/path.conf" + self.parser.aug.set("/augeas/load/Httpd/incl[last()]", nx_path) + self.parser.add_dir(f"/files{nx_path}", "AddDirective", "test") + + with pytest.raises(IOError): + self.parser.save({}) + mock_logger.error.assert_called_with( + 'Unable to save files: %s.%s', '/non/existent/path.conf', mock.ANY) + mock_logger.debug.assert_called_with( + "Error %s saving %s: %s", "mk_augtemp", + "/non/existent/path.conf", "No such file or directory") + + def test_aug_version(self): + mock_match = mock.Mock(return_value=["something"]) + self.parser.aug.match = mock_match + # pylint: disable=protected-access + assert self.parser.check_aug_version() == \ + ["something"] + self.parser.aug.match.side_effect = RuntimeError + assert self.parser.check_aug_version() is False + + def test_find_config_root_no_root(self): + # pylint: disable=protected-access + os.remove(self.parser.loc["root"]) + with pytest.raises(errors.NoInstallationError): + self.parser._find_config_root() + + def test_parse_file(self): + """Test parse_file. + + certbot.conf is chosen as the test file as it will not be + included during the normal course of execution. + + """ + file_path = os.path.join( + self.config_path, "not-parsed-by-default", "certbot.conf") + + self.parser.parse_file(file_path) # pylint: disable=protected-access + + # search for the httpd incl + matches = self.parser.aug.match( + "/augeas/load/Httpd/incl [. ='%s']" % file_path) + + assert matches + + def test_find_dir(self): + test = self.parser.find_dir("Listen", "80") + # This will only look in enabled hosts + test2 = self.parser.find_dir("documentroot") + + assert len(test) == 1 + assert len(test2) == 8 + + def test_add_dir(self): + aug_default = "/files" + self.parser.loc["default"] + self.parser.add_dir(aug_default, "AddDirective", "test") + + assert self.parser.find_dir("AddDirective", "test", aug_default) + + self.parser.add_dir(aug_default, "AddList", ["1", "2", "3", "4"]) + matches = self.parser.find_dir("AddList", None, aug_default) + for i, match in enumerate(matches): + assert self.parser.aug.get(match) == str(i + 1) + + def test_add_dir_beginning(self): + aug_default = "/files" + self.parser.loc["default"] + self.parser.add_dir_beginning(aug_default, + "AddDirectiveBeginning", + "testBegin") + + assert self.parser.find_dir("AddDirectiveBeginning", "testBegin", aug_default) + + assert self.parser.aug.get(aug_default+"/directive[1]") == "AddDirectiveBeginning" + self.parser.add_dir_beginning(aug_default, "AddList", ["1", "2", "3", "4"]) + matches = self.parser.find_dir("AddList", None, aug_default) + for i, match in enumerate(matches): + assert self.parser.aug.get(match) == str(i + 1) + + for name in ("empty.conf", "no-directives.conf"): + conf = "/files" + os.path.join(self.parser.root, "sites-available", name) + self.parser.add_dir_beginning(conf, "AddDirectiveBeginning", "testBegin") + assert len(self.parser.find_dir("AddDirectiveBeginning", "testBegin", conf)) > \ + 0 + + def test_empty_arg(self): + assert self.parser.get_arg("/files/whatever/nonexistent") is None + + def test_add_dir_to_ifmodssl(self): + """test add_dir_to_ifmodssl. + + Path must be valid before attempting to add to augeas + + """ + from certbot._internal.plugins.apache.parser import get_aug_path + + # This makes sure that find_dir will work + self.parser.modules["mod_ssl.c"] = "/fake/path" + + self.parser.add_dir_to_ifmodssl( + get_aug_path(self.parser.loc["default"]), + "FakeDirective", ["123"]) + + matches = self.parser.find_dir("FakeDirective", "123") + + assert len(matches) == 1 + assert "IfModule" in matches[0] + + def test_add_dir_to_ifmodssl_multiple(self): + from certbot._internal.plugins.apache.parser import get_aug_path + + # This makes sure that find_dir will work + self.parser.modules["mod_ssl.c"] = "/fake/path" + + self.parser.add_dir_to_ifmodssl( + get_aug_path(self.parser.loc["default"]), + "FakeDirective", ["123", "456", "789"]) + + matches = self.parser.find_dir("FakeDirective") + + assert len(matches) == 3 + assert "IfModule" in matches[0] + + def test_get_aug_path(self): + from certbot._internal.plugins.apache.parser import get_aug_path + assert "/files/etc/apache" == get_aug_path("/etc/apache") + + def test_set_locations(self): + with mock.patch("certbot._internal.plugins.apache.parser.os.path") as mock_path: + + mock_path.isfile.side_effect = [False, False] + + # pylint: disable=protected-access + results = self.parser._set_locations() + + assert results["default"] == results["listen"] + assert results["default"] == results["name"] + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.find_dir") + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.get_arg") + def test_parse_modules_bad_syntax(self, mock_arg, mock_find): + mock_find.return_value = ["1", "2", "3", "4", "5", "6", "7", "8"] + mock_arg.return_value = None + with mock.patch("certbot._internal.plugins.apache.parser.logger") as mock_logger: + self.parser.parse_modules() + # Make sure that we got None return value and logged the file + assert mock_logger.debug.called is True + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.find_dir") + @mock.patch("certbot._internal.plugins.apache.apache_util._get_runtime_cfg") + def test_update_runtime_variables(self, mock_cfg, _): + define_val = ( + 'ServerRoot: "/etc/apache2"\n' + 'Main DocumentRoot: "/var/www"\n' + 'Main ErrorLog: "/var/log/apache2/error.log"\n' + 'Mutex ssl-stapling: using_defaults\n' + 'Mutex ssl-cache: using_defaults\n' + 'Mutex default: dir="/var/lock/apache2" mechanism=fcntl\n' + 'Mutex watchdog-callback: using_defaults\n' + 'PidFile: "/var/run/apache2/apache2.pid"\n' + 'Define: TEST\n' + 'Define: DUMP_RUN_CFG\n' + 'Define: U_MICH\n' + 'Define: TLS=443\n' + 'Define: WITH_ASSIGNMENT=URL=http://example.com\n' + 'Define: EMPTY=\n' + 'Define: example_path=Documents/path\n' + 'User: name="www-data" id=33 not_used\n' + 'Group: name="www-data" id=33 not_used\n' + ) + inc_val = ( + 'Included configuration files:\n' + ' (*) /etc/apache2/apache2.conf\n' + ' (146) /etc/apache2/mods-enabled/access_compat.load\n' + ' (146) /etc/apache2/mods-enabled/alias.load\n' + ' (146) /etc/apache2/mods-enabled/auth_basic.load\n' + ' (146) /etc/apache2/mods-enabled/authn_core.load\n' + ' (146) /etc/apache2/mods-enabled/authn_file.load\n' + ' (146) /etc/apache2/mods-enabled/authz_core.load\n' + ' (146) /etc/apache2/mods-enabled/authz_host.load\n' + ' (146) /etc/apache2/mods-enabled/authz_user.load\n' + ' (146) /etc/apache2/mods-enabled/autoindex.load\n' + ' (146) /etc/apache2/mods-enabled/deflate.load\n' + ' (146) /etc/apache2/mods-enabled/dir.load\n' + ' (146) /etc/apache2/mods-enabled/env.load\n' + ' (146) /etc/apache2/mods-enabled/filter.load\n' + ' (146) /etc/apache2/mods-enabled/mime.load\n' + ' (146) /etc/apache2/mods-enabled/mpm_event.load\n' + ' (146) /etc/apache2/mods-enabled/negotiation.load\n' + ' (146) /etc/apache2/mods-enabled/reqtimeout.load\n' + ' (146) /etc/apache2/mods-enabled/setenvif.load\n' + ' (146) /etc/apache2/mods-enabled/socache_shmcb.load\n' + ' (146) /etc/apache2/mods-enabled/ssl.load\n' + ' (146) /etc/apache2/mods-enabled/status.load\n' + ' (147) /etc/apache2/mods-enabled/alias.conf\n' + ' (147) /etc/apache2/mods-enabled/autoindex.conf\n' + ' (147) /etc/apache2/mods-enabled/deflate.conf\n' + ) + mod_val = ( + 'Loaded Modules:\n' + ' core_module (static)\n' + ' so_module (static)\n' + ' watchdog_module (static)\n' + ' http_module (static)\n' + ' log_config_module (static)\n' + ' logio_module (static)\n' + ' version_module (static)\n' + ' unixd_module (static)\n' + ' access_compat_module (shared)\n' + ' alias_module (shared)\n' + ' auth_basic_module (shared)\n' + ' authn_core_module (shared)\n' + ' authn_file_module (shared)\n' + ' authz_core_module (shared)\n' + ' authz_host_module (shared)\n' + ' authz_user_module (shared)\n' + ' autoindex_module (shared)\n' + ' deflate_module (shared)\n' + ' dir_module (shared)\n' + ' env_module (shared)\n' + ' filter_module (shared)\n' + ' mime_module (shared)\n' + ' mpm_event_module (shared)\n' + ' negotiation_module (shared)\n' + ' reqtimeout_module (shared)\n' + ' setenvif_module (shared)\n' + ' socache_shmcb_module (shared)\n' + ' ssl_module (shared)\n' + ' status_module (shared)\n' + ) + + def mock_get_vars(cmd): + """Mock command output""" + if cmd[-1] == "DUMP_RUN_CFG": + return define_val + elif cmd[-1] == "DUMP_INCLUDES": + return inc_val + elif cmd[-1] == "DUMP_MODULES": + return mod_val + return None # pragma: no cover + + mock_cfg.side_effect = mock_get_vars + + expected_vars = {"TEST": "", "U_MICH": "", "TLS": "443", + "example_path": "Documents/path", + "WITH_ASSIGNMENT": "URL=http://example.com", + "EMPTY": "", + } + + self.parser.modules = {} + with mock.patch( + "certbot._internal.plugins.apache.parser.ApacheParser.parse_file") as mock_parse: + self.parser.update_runtime_variables() + assert self.parser.variables == expected_vars + assert len(self.parser.modules) == 58 + # None of the includes in inc_val should be in parsed paths. + # Make sure we tried to include them all. + assert mock_parse.call_count == 25 + + @mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.find_dir") + @mock.patch("certbot._internal.plugins.apache.apache_util._get_runtime_cfg") + def test_update_runtime_variables_alt_values(self, mock_cfg, _): + inc_val = ( + 'Included configuration files:\n' + ' (*) {0}\n' + ' (146) /etc/apache2/mods-enabled/access_compat.load\n' + ' (146) {1}/mods-enabled/alias.load\n' + ).format(self.parser.loc["root"], + os.path.dirname(self.parser.loc["root"])) + + mock_cfg.return_value = inc_val + self.parser.modules = {} + + with mock.patch( + "certbot._internal.plugins.apache.parser.ApacheParser.parse_file") as mock_parse: + self.parser.update_runtime_variables() + # No matching modules should have been found + assert len(self.parser.modules) == 0 + # Only one of the three includes do not exist in already parsed + # path derived from root configuration Include statements + assert mock_parse.call_count == 1 + + @mock.patch("certbot._internal.plugins.apache.apache_util.subprocess.run") + def test_update_runtime_vars_bad_ctl(self, mock_run): + mock_run.side_effect = OSError + with pytest.raises(errors.MisconfigurationError): + self.parser.update_runtime_variables() + + @mock.patch("certbot._internal.plugins.apache.apache_util.subprocess.run") + def test_update_runtime_vars_bad_exit(self, mock_run): + mock_proc = mock_run.return_value + mock_proc.stdout = "" + mock_proc.stderr = "" + mock_proc.returncode = -1 + with pytest.raises(errors.MisconfigurationError): + self.parser.update_runtime_variables() + + def test_add_comment(self): + from certbot._internal.plugins.apache.parser import get_aug_path + self.parser.add_comment(get_aug_path(self.parser.loc["name"]), "123456") + comm = self.parser.find_comments("123456") + assert len(comm) == 1 + assert self.parser.loc["name"] in comm[0] + + +class ParserInitTest(util.ApacheTest): + + @mock.patch("certbot._internal.plugins.apache.parser.init_augeas") + def test_prepare_no_augeas(self, mock_init_augeas): + from certbot._internal.plugins.apache.parser import ApacheParser + mock_init_augeas.side_effect = errors.NoInstallationError + self.config.config_test = mock.Mock() + with pytest.raises(errors.NoInstallationError): + ApacheParser(os.path.relpath(self.config_path), self.config, + "/dummy/vhostpath", version=(2, 4, 22)) + + def test_init_old_aug(self): + from certbot._internal.plugins.apache.parser import ApacheParser + with mock.patch("certbot._internal.plugins.apache.parser.ApacheParser.check_aug_version") as mock_c: + mock_c.return_value = False + with pytest.raises(errors.NotSupportedError): + ApacheParser(os.path.relpath(self.config_path), self.config, + "/dummy/vhostpath", version=(2, 4, 22)) + + def test_root_normalized(self): + from certbot._internal.plugins.apache.parser import ApacheParser + + with mock.patch("certbot._internal.plugins.apache.parser.ApacheParser." + "update_runtime_variables"): + path = os.path.join( + self.temp_dir, + "debian_apache_2_4/////multiple_vhosts/../multiple_vhosts/apache2") + + parser = ApacheParser(path, self.config, "/dummy/vhostpath") + + assert parser.root == self.config_path + + def test_root_absolute(self): + from certbot._internal.plugins.apache.parser import ApacheParser + with mock.patch("certbot._internal.plugins.apache.parser.ApacheParser." + "update_runtime_variables"): + parser = ApacheParser( + os.path.relpath(self.config_path), self.config, "/dummy/vhostpath") + + assert parser.root == self.config_path + + def test_root_no_trailing_slash(self): + from certbot._internal.plugins.apache.parser import ApacheParser + with mock.patch("certbot._internal.plugins.apache.parser.ApacheParser." + "update_runtime_variables"): + parser = ApacheParser( + self.config_path + os.path.sep, self.config, "/dummy/vhostpath") + assert parser.root == self.config_path + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/parsernode_configurator_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/parsernode_configurator_test.py new file mode 100644 index 00000000000..162d2b08d79 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/parsernode_configurator_test.py @@ -0,0 +1,47 @@ +"""Tests for ApacheConfigurator for AugeasParserNode classes""" +import importlib +import sys +import unittest +from unittest import mock + +import pytest + +from certbot._internal.tests.plugins.apache import util + + +if importlib.util.find_spec('apacheconfig'): + HAS_APACHECONFIG = True +else: # pragma: no cover + HAS_APACHECONFIG = False + + +@unittest.skipIf(not HAS_APACHECONFIG, reason='Tests require apacheconfig dependency') +class ConfiguratorParserNodeTest(util.ApacheTest): # pylint: disable=too-many-public-methods + """Test AugeasParserNode using available test configurations""" + + def setUp(self): # pylint: disable=arguments-differ + super().setUp() + + self.config = util.get_apache_configurator( + self.config_path, self.vhost_path, self.config_dir, + self.work_dir, use_parsernode=True) + self.vh_truth = util.get_vh_truth( + self.temp_dir, "debian_apache_2_4/multiple_vhosts") + + def test_parsernode_get_vhosts(self): + self.config.USE_PARSERNODE = True + vhosts = self.config.get_virtual_hosts() + # Legacy get_virtual_hosts() do not set the node + assert vhosts[0].node is not None + + def test_parsernode_get_vhosts_mismatch(self): + vhosts = self.config.get_virtual_hosts_v2() + # One of the returned VirtualHost objects differs + vhosts[0].name = "IdidntExpectThat" + self.config.get_virtual_hosts_v2 = mock.MagicMock(return_value=vhosts) + with pytest.raises(AssertionError): + _ = self.config.get_virtual_hosts() + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/parsernode_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/parsernode_test.py new file mode 100644 index 00000000000..7d441673f1f --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/parsernode_test.py @@ -0,0 +1,128 @@ +""" Tests for ParserNode interface """ + +import sys + +import pytest + +from certbot._internal.plugins.apache import interfaces +from certbot._internal.plugins.apache import parsernode_util as util + + +class DummyParserNode(interfaces.ParserNode): + """ A dummy class implementing ParserNode interface """ + + def __init__(self, **kwargs): + """ + Initializes the ParserNode instance. + """ + ancestor, dirty, filepath, metadata = util.parsernode_kwargs(kwargs) + self.ancestor = ancestor + self.dirty = dirty + self.filepath = filepath + self.metadata = metadata + super().__init__(**kwargs) + + def save(self, msg): # pragma: no cover + """Save""" + pass + + def find_ancestors(self, name): # pragma: no cover + """ Find ancestors """ + return [] + + +class DummyCommentNode(DummyParserNode): + """ A dummy class implementing CommentNode interface """ + + def __init__(self, **kwargs): + """ + Initializes the CommentNode instance and sets its instance variables. + """ + comment, kwargs = util.commentnode_kwargs(kwargs) + self.comment = comment + super().__init__(**kwargs) + + +class DummyDirectiveNode(DummyParserNode): + """ A dummy class implementing DirectiveNode interface """ + + # pylint: disable=too-many-arguments + def __init__(self, **kwargs): + """ + Initializes the DirectiveNode instance and sets its instance variables. + """ + name, parameters, enabled, kwargs = util.directivenode_kwargs(kwargs) + self.name = name + self.parameters = parameters + self.enabled = enabled + + super().__init__(**kwargs) + + def set_parameters(self, parameters): # pragma: no cover + """Set parameters""" + pass + + +class DummyBlockNode(DummyDirectiveNode): + """ A dummy class implementing BlockNode interface """ + + def add_child_block(self, name, parameters=None, position=None): # pragma: no cover + """Add child block""" + pass + + def add_child_directive(self, name, parameters=None, position=None): # pragma: no cover + """Add child directive""" + pass + + def add_child_comment(self, comment="", position=None): # pragma: no cover + """Add child comment""" + pass + + def find_blocks(self, name, exclude=True): # pragma: no cover + """Find blocks""" + pass + + def find_directives(self, name, exclude=True): # pragma: no cover + """Find directives""" + pass + + def find_comments(self, comment, exact=False): # pragma: no cover + """Find comments""" + pass + + def delete_child(self, child): # pragma: no cover + """Delete child""" + pass + + def unsaved_files(self): # pragma: no cover + """Unsaved files""" + pass + + +interfaces.CommentNode.register(DummyCommentNode) +interfaces.DirectiveNode.register(DummyDirectiveNode) +interfaces.BlockNode.register(DummyBlockNode) + +def test_dummy(): + """Dummy placeholder test case for ParserNode interfaces""" + dummyblock = DummyBlockNode( + name="None", + parameters=(), + ancestor=None, + dirty=False, + filepath="/some/random/path" + ) + dummydirective = DummyDirectiveNode( # noqa: F841 + name="Name", + ancestor=None, + filepath="/another/path" + ) + dummycomment = DummyCommentNode( # noqa: F841 + comment="Comment", + ancestor=dummyblock, + filepath="/some/file" + ) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/parsernode_util_test.py b/certbot/src/certbot/_internal/tests/plugins/apache/parsernode_util_test.py new file mode 100644 index 00000000000..e228b20a3c8 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/parsernode_util_test.py @@ -0,0 +1,120 @@ +""" Tests for ParserNode utils """ +import sys + +import pytest + +from certbot._internal.plugins.apache import parsernode_util as util + + +def _setup_parsernode(): + """ Sets up kwargs dict for ParserNode """ + return { + "ancestor": None, + "dirty": False, + "filepath": "/tmp", + } + +def _setup_commentnode(): + """ Sets up kwargs dict for CommentNode """ + + pn = _setup_parsernode() + pn["comment"] = "x" + return pn + +def _setup_directivenode(): + """ Sets up kwargs dict for DirectiveNode """ + + pn = _setup_parsernode() + pn["name"] = "Name" + pn["parameters"] = ("first",) + pn["enabled"] = True + return pn + +def test_unknown_parameter(): + params = _setup_parsernode() + params["unknown"] = "unknown" + with pytest.raises(TypeError): + util.parsernode_kwargs(params) + + params = _setup_commentnode() + params["unknown"] = "unknown" + with pytest.raises(TypeError): + util.commentnode_kwargs(params) + + params = _setup_directivenode() + params["unknown"] = "unknown" + with pytest.raises(TypeError): + util.directivenode_kwargs(params) + +def test_parsernode(): + params = _setup_parsernode() + ctrl = _setup_parsernode() + + ancestor, dirty, filepath, metadata = util.parsernode_kwargs(params) + assert ancestor == ctrl["ancestor"] + assert dirty == ctrl["dirty"] + assert filepath == ctrl["filepath"] + assert metadata == {} + +def test_parsernode_from_metadata(): + params = _setup_parsernode() + params.pop("filepath") + md = {"some": "value"} + params["metadata"] = md + + # Just testing that error from missing required parameters is not raised + _, _, _, metadata = util.parsernode_kwargs(params) + assert metadata == md + +def test_commentnode(): + params = _setup_commentnode() + ctrl = _setup_commentnode() + + comment, _ = util.commentnode_kwargs(params) + assert comment == ctrl["comment"] + +def test_commentnode_from_metadata(): + params = _setup_commentnode() + params.pop("comment") + params["metadata"] = {} + + # Just testing that error from missing required parameters is not raised + util.commentnode_kwargs(params) + +def test_directivenode(): + params = _setup_directivenode() + ctrl = _setup_directivenode() + + name, parameters, enabled, _ = util.directivenode_kwargs(params) + assert name == ctrl["name"] + assert parameters == ctrl["parameters"] + assert enabled == ctrl["enabled"] + +def test_directivenode_from_metadata(): + params = _setup_directivenode() + params.pop("filepath") + params.pop("name") + params["metadata"] = {"irrelevant": "value"} + + # Just testing that error from missing required parameters is not raised + util.directivenode_kwargs(params) + +def test_missing_required(): + c_params = _setup_commentnode() + c_params.pop("comment") + with pytest.raises(TypeError): + util.commentnode_kwargs(c_params) + + d_params = _setup_directivenode() + d_params.pop("ancestor") + with pytest.raises(TypeError): + util.directivenode_kwargs(d_params) + + p_params = _setup_parsernode() + p_params.pop("filepath") + with pytest.raises(TypeError): + util.parsernode_kwargs(p_params) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/README b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/README similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/README rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/README diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/autoindex.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/autoindex.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/autoindex.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/autoindex.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/centos.example.com.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/centos.example.com.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/centos.example.com.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/centos.example.com.conf diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/ssl.conf new file mode 100644 index 00000000000..a88033dabfe --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/ssl.conf @@ -0,0 +1,211 @@ +# +# When we also provide SSL we have to listen to the +# the HTTPS port in addition. +# +Listen 443 https + +## +## SSL Global Context +## +## All SSL configuration in this context applies both to +## the main server and all SSL-enabled virtual hosts. +## + +# Pass Phrase Dialog: +# Configure the pass phrase gathering process. +# The filtering dialog program (`builtin' is an internal +# terminal dialog) has to provide the pass phrase on stdout. +SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog + +# Inter-Process Session Cache: +# Configure the SSL Session Cache: First the mechanism +# to use and second the expiring timeout (in seconds). +SSLSessionCache shmcb:/run/httpd/sslcache(512000) +SSLSessionCacheTimeout 300 + +# Pseudo Random Number Generator (PRNG): +# Configure one or more sources to seed the PRNG of the +# SSL library. The seed data should be of good random quality. +# WARNING! On some platforms /dev/random blocks if not enough entropy +# is available. This means you then cannot use the /dev/random device +# because it would lead to very long connection times (as long as +# it requires to make more entropy available). But usually those +# platforms additionally provide a /dev/urandom device which doesn't +# block. So, if available, use this one instead. Read the mod_ssl User +# Manual for more details. +SSLRandomSeed startup file:/dev/urandom 256 +SSLRandomSeed connect builtin +#SSLRandomSeed startup file:/dev/random 512 +#SSLRandomSeed connect file:/dev/random 512 +#SSLRandomSeed connect file:/dev/urandom 512 + +# +# Use "SSLCryptoDevice" to enable any supported hardware +# accelerators. Use "openssl engine -v" to list supported +# engine names. NOTE: If you enable an accelerator and the +# server does not start, consult the error logs and ensure +# your accelerator is functioning properly. +# +SSLCryptoDevice builtin +#SSLCryptoDevice ubsec + +## +## SSL Virtual Host Context +## + + + +# General setup for the virtual host, inherited from global configuration +#DocumentRoot "/var/www/html" +#ServerName www.example.com:443 + +# Use separate log files for the SSL virtual host; note that LogLevel +# is not inherited from httpd.conf. +ErrorLog logs/ssl_error_log +TransferLog logs/ssl_access_log +LogLevel warn + +# SSL Engine Switch: +# Enable/Disable SSL for this virtual host. +SSLEngine on + +# SSL Protocol support: +# List the enable protocol levels with which clients will be able to +# connect. Disable SSLv2 access by default: +SSLProtocol all -SSLv2 + +# SSL Cipher Suite: +# List the ciphers that the client is permitted to negotiate. +# See the mod_ssl documentation for a complete list. +SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5:!SEED:!IDEA + +# Speed-optimized SSL Cipher configuration: +# If speed is your main concern (on busy HTTPS servers e.g.), +# you might want to force clients to specific, performance +# optimized ciphers. In this case, prepend those ciphers +# to the SSLCipherSuite list, and enable SSLHonorCipherOrder. +# Caveat: by giving precedence to RC4-SHA and AES128-SHA +# (as in the example below), most connections will no longer +# have perfect forward secrecy - if the server's key is +# compromised, captures of past or future traffic must be +# considered compromised, too. +#SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5 +#SSLHonorCipherOrder on + +# Server Certificate: +# Point SSLCertificateFile at a PEM encoded certificate. If +# the certificate is encrypted, then you will be prompted for a +# pass phrase. Note that a kill -HUP will prompt again. A new +# certificate can be generated using the genkey(1) command. + +# Server Private Key: +# If the key is not combined with the certificate, use this +# directive to point at the key file. Keep in mind that if +# you've both a RSA and a DSA private key you can configure +# both in parallel (to also allow the use of DSA ciphers, etc.) + +# Server Certificate Chain: +# Point SSLCertificateChainFile at a file containing the +# concatenation of PEM encoded CA certificates which form the +# certificate chain for the server certificate. Alternatively +# the referenced file can be the same as SSLCertificateFile +# when the CA certificates are directly appended to the server +# certificate for convenience. +#SSLCertificateChainFile /etc/pki/tls/certs/server-chain.crt + +# Certificate Authority (CA): +# Set the CA certificate verification path where to find CA +# certificates for client authentication or alternatively one +# huge file containing all of them (file must be PEM encoded) +#SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt + +# Client Authentication (Type): +# Client certificate verification type and depth. Types are +# none, optional, require and optional_no_ca. Depth is a +# number which specifies how deeply to verify the certificate +# issuer chain before deciding the certificate is not valid. +#SSLVerifyClient require +#SSLVerifyDepth 10 + +# Access Control: +# With SSLRequire you can do per-directory access control based +# on arbitrary complex boolean expressions containing server +# variable checks and other lookup directives. The syntax is a +# mixture between C and Perl. See the mod_ssl documentation +# for more details. +# +#SSLRequire ( %{SSL_CIPHER} !~ m/^(EXP|NULL)/ \ +# and %{SSL_CLIENT_S_DN_O} eq "Snake Oil, Ltd." \ +# and %{SSL_CLIENT_S_DN_OU} in {"Staff", "CA", "Dev"} \ +# and %{TIME_WDAY} >= 1 and %{TIME_WDAY} <= 5 \ +# and %{TIME_HOUR} >= 8 and %{TIME_HOUR} <= 20 ) \ +# or %{REMOTE_ADDR} =~ m/^192\.76\.162\.[0-9]+$/ +# + +# SSL Engine Options: +# Set various options for the SSL engine. +# o FakeBasicAuth: +# Translate the client X.509 into a Basic Authorisation. This means that +# the standard Auth/DBMAuth methods can be used for access control. The +# user name is the `one line' version of the client's X.509 certificate. +# Note that no password is obtained from the user. Every entry in the user +# file needs this password: `xxj31ZMTZzkVA'. +# o ExportCertData: +# This exports two additional environment variables: SSL_CLIENT_CERT and +# SSL_SERVER_CERT. These contain the PEM-encoded certificates of the +# server (always existing) and the client (only existing when client +# authentication is used). This can be used to import the certificates +# into CGI scripts. +# o StdEnvVars: +# This exports the standard SSL/TLS related `SSL_*' environment variables. +# Per default this exportation is switched off for performance reasons, +# because the extraction step is an expensive operation and is usually +# useless for serving static content. So one usually enables the +# exportation for CGI and SSI requests only. +# o StrictRequire: +# This denies access when "SSLRequireSSL" or "SSLRequire" applied even +# under a "Satisfy any" situation, i.e. when it applies access is denied +# and no other module can change it. +# o OptRenegotiate: +# This enables optimized SSL connection renegotiation handling when SSL +# directives are used in per-directory context. +#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire + + SSLOptions +StdEnvVars + + + SSLOptions +StdEnvVars + + +# SSL Protocol Adjustments: +# The safe and default but still SSL/TLS standard compliant shutdown +# approach is that mod_ssl sends the close notify alert but doesn't wait for +# the close notify alert from client. When you need a different shutdown +# approach you can use one of the following variables: +# o ssl-unclean-shutdown: +# This forces an unclean shutdown when the connection is closed, i.e. no +# SSL close notify alert is send or allowed to received. This violates +# the SSL/TLS standard but is needed for some brain-dead browsers. Use +# this when you receive I/O errors because of the standard approach where +# mod_ssl sends the close notify alert. +# o ssl-accurate-shutdown: +# This forces an accurate shutdown when the connection is closed, i.e. a +# SSL close notify alert is send and mod_ssl waits for the close notify +# alert of the client. This is 100% SSL/TLS standard compliant, but in +# practice often causes hanging connections with brain-dead browsers. Use +# this only for browsers where you know that their SSL implementation +# works correctly. +# Notice: Most problems of broken clients are also related to the HTTP +# keep-alive facility, so you usually additionally want to disable +# keep-alive for those clients, too. Use variable "nokeepalive" for this. +# Similarly, one has to force some clients to use HTTP/1.0 to workaround +# their broken HTTP/1.1 implementation. Use variables "downgrade-1.0" and +# "force-response-1.0" for this. +BrowserMatch "MSIE [2-5]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0 + +# Per-Server Logging: +# The home of a custom SSL log file. Use this when you want a +# compact non-error SSL logfile on a virtual host basis. +CustomLog logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" + + diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/userdir.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/userdir.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/userdir.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/userdir.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/welcome.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/welcome.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.d/welcome.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.d/welcome.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-base.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-base.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-base.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-base.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-dav.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-dav.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-dav.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-dav.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-lua.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-lua.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-lua.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-lua.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-mpm.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-mpm.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-mpm.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-mpm.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-proxy.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-proxy.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-proxy.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-proxy.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-ssl.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-ssl.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-ssl.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-systemd.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-systemd.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/00-systemd.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/00-systemd.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/01-cgi.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/01-cgi.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf.modules.d/01-cgi.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf.modules.d/01-cgi.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf/httpd.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf/httpd.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf/httpd.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf/httpd.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf/magic b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf/magic similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/httpd/conf/magic rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/httpd/conf/magic diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/sites b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/sites similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/sites rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/sites diff --git a/certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/sysconfig/httpd b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/sysconfig/httpd similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/centos7_apache/apache/sysconfig/httpd rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/centos7_apache/apache/sysconfig/httpd diff --git a/certbot-apache/certbot_apache/tests/testdata/complex_parsing/apache2.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/complex_parsing/apache2.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/complex_parsing/apache2.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/complex_parsing/apache2.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/complex_parsing/conf-enabled/dummy.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/complex_parsing/conf-enabled/dummy.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/complex_parsing/conf-enabled/dummy.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/complex_parsing/conf-enabled/dummy.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/complex_parsing/test_fnmatch.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/complex_parsing/test_fnmatch.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/complex_parsing/test_fnmatch.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/complex_parsing/test_fnmatch.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/complex_parsing/test_variables.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/complex_parsing/test_variables.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/complex_parsing/test_variables.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/complex_parsing/test_variables.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/apache2.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/apache2.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/apache2.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/apache2.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/bad_conf_file.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/bad_conf_file.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/bad_conf_file.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/bad_conf_file.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/other-vhosts-access-log.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/other-vhosts-access-log.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/other-vhosts-access-log.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/other-vhosts-access-log.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/security.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/security.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/security.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/security.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/serve-cgi-bin.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/serve-cgi-bin.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/serve-cgi-bin.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-available/serve-cgi-bin.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/other-vhosts-access-log.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/other-vhosts-access-log.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/other-vhosts-access-log.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/other-vhosts-access-log.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/security.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/security.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/security.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/security.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/serve-cgi-bin.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/serve-cgi-bin.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/serve-cgi-bin.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/conf-enabled/serve-cgi-bin.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/envvars b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/envvars similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/envvars rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/envvars diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/authz_svn.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/authz_svn.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/authz_svn.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/authz_svn.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav_svn.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav_svn.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav_svn.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav_svn.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav_svn.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav_svn.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav_svn.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/dav_svn.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/rewrite.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/rewrite.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/rewrite.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/rewrite.load diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.conf new file mode 100644 index 00000000000..65baec87405 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.conf @@ -0,0 +1,89 @@ + + + # Pseudo Random Number Generator (PRNG): + # Configure one or more sources to seed the PRNG of the SSL library. + # The seed data should be of good random quality. + # WARNING! On some platforms /dev/random blocks if not enough entropy + # is available. This means you then cannot use the /dev/random device + # because it would lead to very long connection times (as long as + # it requires to make more entropy available). But usually those + # platforms additionally provide a /dev/urandom device which doesn't + # block. So, if available, use this one instead. Read the mod_ssl User + # Manual for more details. + # + SSLRandomSeed startup builtin + SSLRandomSeed startup file:/dev/urandom 512 + SSLRandomSeed connect builtin + SSLRandomSeed connect file:/dev/urandom 512 + + ## + ## SSL Global Context + ## + ## All SSL configuration in this context applies both to + ## the main server and all SSL-enabled virtual hosts. + ## + + # + # Some MIME-types for downloading Certificates and CRLs + # + AddType application/x-x509-ca-cert .crt + AddType application/x-pkcs7-crl .crl + + # Pass Phrase Dialog: + # Configure the pass phrase gathering process. + # The filtering dialog program (`builtin' is an internal + # terminal dialog) has to provide the pass phrase on stdout. + SSLPassPhraseDialog exec:/usr/share/apache2/ask-for-passphrase + + # Inter-Process Session Cache: + # Configure the SSL Session Cache: First the mechanism + # to use and second the expiring timeout (in seconds). + # (The mechanism dbm has known memory leaks and should not be used). + #SSLSessionCache dbm:${APACHE_RUN_DIR}/ssl_scache + SSLSessionCache shmcb:${APACHE_RUN_DIR}/ssl_scache(512000) + SSLSessionCacheTimeout 300 + + # Semaphore: + # Configure the path to the mutual exclusion semaphore the + # SSL engine uses internally for inter-process synchronization. + # (Disabled by default, the global Mutex directive consolidates by default + # this) + #Mutex file:${APACHE_LOCK_DIR}/ssl_mutex ssl-cache + + + # SSL Cipher Suite: + # List the ciphers that the client is permitted to negotiate. See the + # ciphers(1) man page from the openssl package for list of all available + # options. + # Enable only secure ciphers: + SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5 + + # Speed-optimized SSL Cipher configuration: + # If speed is your main concern (on busy HTTPS servers e.g.), + # you might want to force clients to specific, performance + # optimized ciphers. In this case, prepend those ciphers + # to the SSLCipherSuite list, and enable SSLHonorCipherOrder. + # Caveat: by giving precedence to RC4-SHA and AES128-SHA + # (as in the example below), most connections will no longer + # have perfect forward secrecy - if the server's key is + # compromised, captures of past or future traffic must be + # considered compromised, too. + #SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5 + #SSLHonorCipherOrder on + + # The protocols to enable. + # Available values: all, SSLv3, TLSv1, TLSv1.1, TLSv1.2 + # SSL v2 is no longer supported + SSLProtocol all + + # Allow insecure renegotiation with clients which do not yet support the + # secure renegotiation protocol. Default: Off + #SSLInsecureRenegotiation on + + # Whether to forbid non-SNI clients to access name based virtual hosts. + # Default: Off + #SSLStrictSNIVHostCheck On + + + +# vim: syntax=apache ts=4 sw=4 sts=4 sr noet diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-available/ssl.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/authz_svn.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/authz_svn.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/authz_svn.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/authz_svn.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav_svn.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav_svn.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav_svn.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav_svn.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav_svn.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav_svn.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav_svn.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/mods-enabled/dav_svn.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/ports.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/ports.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/ports.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/ports.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/another_wildcard.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/another_wildcard.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/another_wildcard.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/another_wildcard.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/old,default.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/old-and-default.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/old,default.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/old-and-default.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/wildcard.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/wildcard.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/wildcard.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-available/wildcard.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/another_wildcard.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/another_wildcard.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/another_wildcard.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/another_wildcard.conf diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/old-and-default.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/old-and-default.conf new file mode 120000 index 00000000000..f7fdf1bbe44 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/old-and-default.conf @@ -0,0 +1 @@ +../sites-available/old-and-default.conf \ No newline at end of file diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/wildcard.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/wildcard.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/wildcard.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/apache2/sites-enabled/wildcard.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/sites b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/sites similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/augeas_vhosts/sites rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/augeas_vhosts/sites diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/apache2.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/apache2.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/apache2.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/apache2.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/other-vhosts-access-log.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/other-vhosts-access-log.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/other-vhosts-access-log.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/other-vhosts-access-log.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/security.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/security.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/security.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/security.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/serve-cgi-bin.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/serve-cgi-bin.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/serve-cgi-bin.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-available/serve-cgi-bin.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/other-vhosts-access-log.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/other-vhosts-access-log.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/other-vhosts-access-log.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/other-vhosts-access-log.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/security.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/security.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/security.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/security.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/serve-cgi-bin.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/serve-cgi-bin.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/serve-cgi-bin.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/conf-enabled/serve-cgi-bin.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/envvars b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/envvars similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/envvars rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/envvars diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.conf new file mode 100644 index 00000000000..65baec87405 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.conf @@ -0,0 +1,89 @@ + + + # Pseudo Random Number Generator (PRNG): + # Configure one or more sources to seed the PRNG of the SSL library. + # The seed data should be of good random quality. + # WARNING! On some platforms /dev/random blocks if not enough entropy + # is available. This means you then cannot use the /dev/random device + # because it would lead to very long connection times (as long as + # it requires to make more entropy available). But usually those + # platforms additionally provide a /dev/urandom device which doesn't + # block. So, if available, use this one instead. Read the mod_ssl User + # Manual for more details. + # + SSLRandomSeed startup builtin + SSLRandomSeed startup file:/dev/urandom 512 + SSLRandomSeed connect builtin + SSLRandomSeed connect file:/dev/urandom 512 + + ## + ## SSL Global Context + ## + ## All SSL configuration in this context applies both to + ## the main server and all SSL-enabled virtual hosts. + ## + + # + # Some MIME-types for downloading Certificates and CRLs + # + AddType application/x-x509-ca-cert .crt + AddType application/x-pkcs7-crl .crl + + # Pass Phrase Dialog: + # Configure the pass phrase gathering process. + # The filtering dialog program (`builtin' is an internal + # terminal dialog) has to provide the pass phrase on stdout. + SSLPassPhraseDialog exec:/usr/share/apache2/ask-for-passphrase + + # Inter-Process Session Cache: + # Configure the SSL Session Cache: First the mechanism + # to use and second the expiring timeout (in seconds). + # (The mechanism dbm has known memory leaks and should not be used). + #SSLSessionCache dbm:${APACHE_RUN_DIR}/ssl_scache + SSLSessionCache shmcb:${APACHE_RUN_DIR}/ssl_scache(512000) + SSLSessionCacheTimeout 300 + + # Semaphore: + # Configure the path to the mutual exclusion semaphore the + # SSL engine uses internally for inter-process synchronization. + # (Disabled by default, the global Mutex directive consolidates by default + # this) + #Mutex file:${APACHE_LOCK_DIR}/ssl_mutex ssl-cache + + + # SSL Cipher Suite: + # List the ciphers that the client is permitted to negotiate. See the + # ciphers(1) man page from the openssl package for list of all available + # options. + # Enable only secure ciphers: + SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5 + + # Speed-optimized SSL Cipher configuration: + # If speed is your main concern (on busy HTTPS servers e.g.), + # you might want to force clients to specific, performance + # optimized ciphers. In this case, prepend those ciphers + # to the SSLCipherSuite list, and enable SSLHonorCipherOrder. + # Caveat: by giving precedence to RC4-SHA and AES128-SHA + # (as in the example below), most connections will no longer + # have perfect forward secrecy - if the server's key is + # compromised, captures of past or future traffic must be + # considered compromised, too. + #SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5 + #SSLHonorCipherOrder on + + # The protocols to enable. + # Available values: all, SSLv3, TLSv1, TLSv1.1, TLSv1.2 + # SSL v2 is no longer supported + SSLProtocol all + + # Allow insecure renegotiation with clients which do not yet support the + # secure renegotiation protocol. Default: Off + #SSLInsecureRenegotiation on + + # Whether to forbid non-SNI clients to access name based virtual hosts. + # Default: Off + #SSLStrictSNIVHostCheck On + + + +# vim: syntax=apache ts=4 sw=4 sts=4 sr noet diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/mods-available/ssl.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/ports.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/ports.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/ports.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/ports.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/sites-available/000-default.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/sites-available/000-default.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/sites-available/000-default.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/sites-available/000-default.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/sites-available/default-ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/sites-available/default-ssl.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/sites-available/default-ssl.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/sites-available/default-ssl.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/sites-enabled/000-default.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/sites-enabled/000-default.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/apache2/sites-enabled/000-default.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/apache2/sites-enabled/000-default.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/sites b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/sites similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/default_vhost/sites rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/default_vhost/sites diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/apache2.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/apache2.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/apache2.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/apache2.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/envvars b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/envvars similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/envvars rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/envvars diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/ports.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/ports.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/ports.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/ports.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-available/default.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-available/default.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-available/default.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-available/default.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-available/multi-vhost.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-available/multi-vhost.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-available/multi-vhost.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-available/multi-vhost.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-enabled/default.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-enabled/default.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-enabled/default.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-enabled/default.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-enabled/multi-vhost.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-enabled/multi-vhost.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-enabled/multi-vhost.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multi_vhosts/apache2/sites-enabled/multi-vhost.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/apache2.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/apache2.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/apache2.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/apache2.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/bad_conf_file.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/bad_conf_file.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/bad_conf_file.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/bad_conf_file.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/other-vhosts-access-log.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/other-vhosts-access-log.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/other-vhosts-access-log.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/other-vhosts-access-log.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/security.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/security.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/security.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/security.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/serve-cgi-bin.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/serve-cgi-bin.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/serve-cgi-bin.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-available/serve-cgi-bin.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/other-vhosts-access-log.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/other-vhosts-access-log.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/other-vhosts-access-log.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/other-vhosts-access-log.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/security.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/security.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/security.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/security.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/serve-cgi-bin.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/serve-cgi-bin.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/serve-cgi-bin.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/conf-enabled/serve-cgi-bin.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/envvars b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/envvars similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/envvars rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/envvars diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/authz_svn.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/authz_svn.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/authz_svn.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/authz_svn.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav_svn.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav_svn.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav_svn.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav_svn.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav_svn.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav_svn.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav_svn.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/dav_svn.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/rewrite.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/rewrite.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/rewrite.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/rewrite.load diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.conf new file mode 100644 index 00000000000..65baec87405 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.conf @@ -0,0 +1,89 @@ + + + # Pseudo Random Number Generator (PRNG): + # Configure one or more sources to seed the PRNG of the SSL library. + # The seed data should be of good random quality. + # WARNING! On some platforms /dev/random blocks if not enough entropy + # is available. This means you then cannot use the /dev/random device + # because it would lead to very long connection times (as long as + # it requires to make more entropy available). But usually those + # platforms additionally provide a /dev/urandom device which doesn't + # block. So, if available, use this one instead. Read the mod_ssl User + # Manual for more details. + # + SSLRandomSeed startup builtin + SSLRandomSeed startup file:/dev/urandom 512 + SSLRandomSeed connect builtin + SSLRandomSeed connect file:/dev/urandom 512 + + ## + ## SSL Global Context + ## + ## All SSL configuration in this context applies both to + ## the main server and all SSL-enabled virtual hosts. + ## + + # + # Some MIME-types for downloading Certificates and CRLs + # + AddType application/x-x509-ca-cert .crt + AddType application/x-pkcs7-crl .crl + + # Pass Phrase Dialog: + # Configure the pass phrase gathering process. + # The filtering dialog program (`builtin' is an internal + # terminal dialog) has to provide the pass phrase on stdout. + SSLPassPhraseDialog exec:/usr/share/apache2/ask-for-passphrase + + # Inter-Process Session Cache: + # Configure the SSL Session Cache: First the mechanism + # to use and second the expiring timeout (in seconds). + # (The mechanism dbm has known memory leaks and should not be used). + #SSLSessionCache dbm:${APACHE_RUN_DIR}/ssl_scache + SSLSessionCache shmcb:${APACHE_RUN_DIR}/ssl_scache(512000) + SSLSessionCacheTimeout 300 + + # Semaphore: + # Configure the path to the mutual exclusion semaphore the + # SSL engine uses internally for inter-process synchronization. + # (Disabled by default, the global Mutex directive consolidates by default + # this) + #Mutex file:${APACHE_LOCK_DIR}/ssl_mutex ssl-cache + + + # SSL Cipher Suite: + # List the ciphers that the client is permitted to negotiate. See the + # ciphers(1) man page from the openssl package for list of all available + # options. + # Enable only secure ciphers: + SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5 + + # Speed-optimized SSL Cipher configuration: + # If speed is your main concern (on busy HTTPS servers e.g.), + # you might want to force clients to specific, performance + # optimized ciphers. In this case, prepend those ciphers + # to the SSLCipherSuite list, and enable SSLHonorCipherOrder. + # Caveat: by giving precedence to RC4-SHA and AES128-SHA + # (as in the example below), most connections will no longer + # have perfect forward secrecy - if the server's key is + # compromised, captures of past or future traffic must be + # considered compromised, too. + #SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5 + #SSLHonorCipherOrder on + + # The protocols to enable. + # Available values: all, SSLv3, TLSv1, TLSv1.1, TLSv1.2 + # SSL v2 is no longer supported + SSLProtocol all + + # Allow insecure renegotiation with clients which do not yet support the + # secure renegotiation protocol. Default: Off + #SSLInsecureRenegotiation on + + # Whether to forbid non-SNI clients to access name based virtual hosts. + # Default: Off + #SSLStrictSNIVHostCheck On + + + +# vim: syntax=apache ts=4 sw=4 sts=4 sr noet diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-available/ssl.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/authz_svn.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/authz_svn.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/authz_svn.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/authz_svn.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav_svn.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav_svn.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav_svn.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav_svn.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav_svn.load b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav_svn.load similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav_svn.load rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/mods-enabled/dav_svn.load diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/ports.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/ports.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/ports.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/ports.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/000-default.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/000-default.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/000-default.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/000-default.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/certbot.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/certbot.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/certbot.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/certbot.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/default-ssl-port-only.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/default-ssl-port-only.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/default-ssl-port-only.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/default-ssl-port-only.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/default-ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/default-ssl.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/default-ssl.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/default-ssl.conf diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/duplicatehttp.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/duplicatehttp.conf new file mode 100644 index 00000000000..5684651fbb8 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/duplicatehttp.conf @@ -0,0 +1,9 @@ + + ServerName duplicate.example.com + + ServerAdmin webmaster@certbot.demo + DocumentRoot /var/www/html + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/duplicatehttps.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/duplicatehttps.conf new file mode 100644 index 00000000000..e3ac21fac80 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/duplicatehttps.conf @@ -0,0 +1,14 @@ + + + ServerName duplicate.example.com + + ServerAdmin webmaster@certbot.demo + DocumentRoot /var/www/html + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + +SSLCertificateFile /etc/apache2/certs/certbot-cert_5.pem +SSLCertificateKeyFile /etc/apache2/ssl/key-certbot_15.pem + + diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/empty.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/empty.conf new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/encryption-example.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/encryption-example.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/encryption-example.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/encryption-example.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/mod_macro-example.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/mod_macro-example.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/mod_macro-example.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/mod_macro-example.conf diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/no-directives.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/no-directives.conf new file mode 100644 index 00000000000..e7ceab441db --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/no-directives.conf @@ -0,0 +1,5 @@ + + + Require all denied + + diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/ocsp-ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/ocsp-ssl.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/ocsp-ssl.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/ocsp-ssl.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/wildcard.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/wildcard.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/wildcard.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-available/wildcard.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/000-default.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/000-default.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/000-default.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/000-default.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/certbot.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/certbot.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/certbot.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/certbot.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/default-ssl-port-only.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/default-ssl-port-only.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/default-ssl-port-only.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/default-ssl-port-only.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/default-ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/default-ssl.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/default-ssl.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/default-ssl.conf diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/duplicatehttp.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/duplicatehttp.conf new file mode 120000 index 00000000000..a69ee3c1dca --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/duplicatehttp.conf @@ -0,0 +1 @@ +../sites-available/duplicatehttp.conf \ No newline at end of file diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/duplicatehttps.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/duplicatehttps.conf new file mode 120000 index 00000000000..a52ee1ccb97 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/duplicatehttps.conf @@ -0,0 +1 @@ +../sites-available/duplicatehttps.conf \ No newline at end of file diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/encryption-example.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/encryption-example.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/encryption-example.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/encryption-example.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/mod_macro-example.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/mod_macro-example.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/mod_macro-example.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/mod_macro-example.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/non-symlink.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/non-symlink.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/non-symlink.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/non-symlink.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/ocsp-ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/ocsp-ssl.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/ocsp-ssl.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/ocsp-ssl.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/wildcard.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/wildcard.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/wildcard.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/apache2/sites-enabled/wildcard.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/sites b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/sites similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/debian_apache_2_4/multiple_vhosts/sites rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/debian_apache_2_4/multiple_vhosts/sites diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/httpd.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/httpd.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/httpd.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/httpd.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/magic b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/magic similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/magic rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/magic diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_default_settings.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_default_settings.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_default_settings.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_default_settings.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_error_documents.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_error_documents.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_error_documents.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_error_documents.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_languages.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_languages.conf similarity index 99% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_languages.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_languages.conf index c429bf94c83..10cf3fb54e3 100644 --- a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_languages.conf +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_languages.conf @@ -33,7 +33,7 @@ # English (en) - Esperanto (eo) - Estonian (et) - French (fr) - German (de) # Greek-Modern (el) - Hebrew (he) - Italian (it) - Japanese (ja) # Korean (ko) - Luxembourgeois* (ltz) - Norwegian Nynorsk (nn) -# Norwegian (no) - Polish (pl) - Portugese (pt) +# Norwegian (no) - Polish (pl) - Portuguese (pt) # Brazilian Portuguese (pt-BR) - Russian (ru) - Swedish (sv) # Simplified Chinese (zh-CN) - Spanish (es) - Traditional Chinese (zh-TW) AddLanguage ca .ca diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_autoindex.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_autoindex.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_autoindex.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_autoindex.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_info.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_info.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_info.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_info.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_log_config.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_log_config.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_log_config.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_log_config.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_mime.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_mime.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_mime.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_mime.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_status.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_status.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_status.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_status.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_userdir.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_userdir.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_userdir.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mod_userdir.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mpm.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mpm.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/00_mpm.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/00_mpm.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/10_mod_mem_cache.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/10_mod_mem_cache.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/10_mod_mem_cache.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/10_mod_mem_cache.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/40_mod_ssl.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/40_mod_ssl.conf similarity index 97% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/40_mod_ssl.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/40_mod_ssl.conf index f51de464178..7f3cef423c9 100644 --- a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/40_mod_ssl.conf +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/40_mod_ssl.conf @@ -43,7 +43,7 @@ SSLRandomSeed connect builtin ## Pass Phrase Dialog: # Configure the pass phrase gathering process. The filtering dialog program -# (`builtin' is a internal terminal dialog) has to provide the pass phrase on +# (`builtin' is an internal terminal dialog) has to provide the pass phrase on # stdout. SSLPassPhraseDialog builtin diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/41_mod_http2.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/41_mod_http2.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/41_mod_http2.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/41_mod_http2.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/45_mod_dav.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/45_mod_dav.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/45_mod_dav.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/45_mod_dav.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/46_mod_ldap.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/46_mod_ldap.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/modules.d/46_mod_ldap.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/modules.d/46_mod_ldap.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_ssl_vhost.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_ssl_vhost.conf similarity index 99% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_ssl_vhost.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_ssl_vhost.conf index bb395473c9a..b2aa28da274 100644 --- a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_ssl_vhost.conf +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_ssl_vhost.conf @@ -58,7 +58,7 @@ Listen 443 # PEM encoded CA certificates which form the certificate chain for the # server certificate. Alternatively the referenced file can be the same as # SSLCertificateFile when the CA certificates are directly appended to the - # server certificate for convinience. + # server certificate for convenience. #SSLCertificateChainFile /etc/ssl/apache2/ca.crt ## Certificate Authority (CA): diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_vhost.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_vhost.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_vhost.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/vhosts.d/00_default_vhost.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/vhosts.d/default_vhost.include b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/vhosts.d/default_vhost.include similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/vhosts.d/default_vhost.include rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/vhosts.d/default_vhost.include diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/vhosts.d/gentoo.example.com.conf b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/vhosts.d/gentoo.example.com.conf similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/apache2/vhosts.d/gentoo.example.com.conf rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/apache2/vhosts.d/gentoo.example.com.conf diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/conf.d/apache2 b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/conf.d/apache2 similarity index 97% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/conf.d/apache2 rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/conf.d/apache2 index b7ecb4f2a83..89323ea8f22 100644 --- a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/conf.d/apache2 +++ b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/conf.d/apache2 @@ -26,7 +26,7 @@ # # The following two options provide the default virtual host for the HTTP and # HTTPS protocol. YOU NEED TO ENABLE AT LEAST ONE OF THEM, otherwise apache -# will not listen for incomming connections on the approriate port. +# will not listen for incoming connections on the appropriate port. # # DEFAULT_VHOST Enables name-based virtual hosts, with the default # virtual host being in /var/www/localhost/htdocs diff --git a/certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/sites b/certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/sites similarity index 100% rename from certbot-apache/certbot_apache/tests/testdata/gentoo_apache/apache/sites rename to certbot/src/certbot/_internal/tests/plugins/apache/testdata/gentoo_apache/apache/sites diff --git a/certbot/src/certbot/_internal/tests/plugins/apache/util.py b/certbot/src/certbot/_internal/tests/plugins/apache/util.py new file mode 100644 index 00000000000..33c598fffd9 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/apache/util.py @@ -0,0 +1,241 @@ +"""Common test utilities for the apache plugin.""" +import shutil +from typing import Optional +import unittest +from unittest import mock + +import augeas +import josepy as jose + +from certbot import util +from certbot.compat import os +from certbot.plugins import common +from certbot.tests import util as test_util +from certbot._internal.plugins.apache import configurator +from certbot._internal.plugins.apache import entrypoint +from certbot._internal.plugins.apache import obj + + +class ApacheTest(unittest.TestCase): + + def setUp(self, test_dir: str = "debian_apache_2_4/multiple_vhosts", + config_root: str = "debian_apache_2_4/multiple_vhosts/apache2", + vhost_root: str = "debian_apache_2_4/multiple_vhosts/apache2/sites-available" + ) -> None: + # pylint: disable=arguments-differ + self.temp_dir, self.config_dir, self.work_dir = common.dir_setup( + test_dir=test_dir, + pkg=__package__) + + self.config_path = os.path.join(self.temp_dir, config_root) + self.vhost_path = os.path.join(self.temp_dir, vhost_root) + + self.rsa512jwk = jose.jwk.JWKRSA.load(test_util.load_vector( + "rsa512_key.pem")) + + self.config = get_apache_configurator(self.config_path, vhost_root, + self.config_dir, self.work_dir) + + # Make sure all vhosts in sites-enabled are symlinks (Python packaging + # does not preserve symlinks) + sites_enabled = os.path.join(self.config_path, "sites-enabled") + if not os.path.exists(sites_enabled): + return + + for vhost_basename in os.listdir(sites_enabled): + # Keep the one non-symlink test vhost in place + if vhost_basename == "non-symlink.conf": + continue + vhost = os.path.join(sites_enabled, vhost_basename) + if not os.path.islink(vhost): # pragma: no cover + os.remove(vhost) + target = os.path.join( + os.path.pardir, "sites-available", vhost_basename) + os.symlink(target, vhost) + + def tearDown(self) -> None: + util._release_locks() + shutil.rmtree(self.temp_dir) + shutil.rmtree(self.config_dir) + shutil.rmtree(self.work_dir) + + +class ParserTest(ApacheTest): + + def setUp(self, test_dir: str = "debian_apache_2_4/multiple_vhosts", + config_root: str = "debian_apache_2_4/multiple_vhosts/apache2", + vhost_root: str = "debian_apache_2_4/multiple_vhosts/apache2/sites-available" + ) -> None: + super().setUp(test_dir, config_root, vhost_root) + + from certbot._internal.plugins.apache.parser import ApacheParser + self.aug = augeas.Augeas( + flags=augeas.Augeas.NONE | augeas.Augeas.NO_MODL_AUTOLOAD) + with mock.patch("certbot._internal.plugins.apache.parser.ApacheParser." + "update_runtime_variables"): + self.parser = ApacheParser( + self.config_path, self.config, self.vhost_path) + + +def get_apache_configurator( + config_path: str, vhost_path: str, + config_dir: str, work_dir: str, version: tuple[int, int, int] = (2, 4, 7), + os_info: str = "generic", + conf_vhost_path: Optional[str] = None, + use_parsernode: bool = False, + openssl_version: str = "1.1.1a") -> configurator.ApacheConfigurator: + """Create an Apache Configurator with the specified options. + + :param conf: Function that returns binary paths. self.conf in Configurator + + """ + backups = os.path.join(work_dir, "backups") + mock_le_config = mock.MagicMock( + apache_server_root=config_path, + apache_vhost_root=None, + apache_le_vhost_ext="-le-ssl.conf", + apache_challenge_location=config_path, + apache_enmod=None, + backup_dir=backups, + config_dir=config_dir, + http01_port=80, + temp_checkpoint_dir=os.path.join(work_dir, "temp_checkpoints"), + in_progress_dir=os.path.join(backups, "IN_PROGRESS"), + work_dir=work_dir) + + with mock.patch("certbot._internal.plugins.apache.configurator.util.run_script"): + with mock.patch("certbot._internal.plugins.apache.configurator.util." + "exe_exists") as mock_exe_exists: + mock_exe_exists.return_value = True + with mock.patch("certbot._internal.plugins.apache.parser.ApacheParser." + "update_runtime_variables"): + with mock.patch("certbot._internal.plugins.apache.apache_util.parse_from_subprocess") as mock_sp: + mock_sp.return_value = [] + try: + config_class = entrypoint.OVERRIDE_CLASSES[os_info] + except KeyError: + config_class = configurator.ApacheConfigurator + config = config_class(config=mock_le_config, name="apache", + version=version, use_parsernode=use_parsernode, + openssl_version=openssl_version) + if not conf_vhost_path: + config_class.OS_DEFAULTS.vhost_root = vhost_path + else: + # Custom virtualhost path was requested + config.config.apache_vhost_root = conf_vhost_path + config.config.apache_ctl = config_class.OS_DEFAULTS.ctl + config.config.apache_bin = config_class.OS_DEFAULTS.bin + config.prepare() + return config + + +def get_vh_truth(temp_dir: str, config_name: str) -> Optional[list[obj.VirtualHost]]: + """Return the ground truth for the specified directory.""" + if config_name == "debian_apache_2_4/multiple_vhosts": + prefix = os.path.join( + temp_dir, config_name, "apache2/sites-enabled") + + aug_pre = "/files" + prefix + vh_truth = [ + obj.VirtualHost( + os.path.join(prefix, "encryption-example.conf"), + os.path.join(aug_pre, "encryption-example.conf/Virtualhost"), + {obj.Addr.fromstring("*:80")}, + False, True, "encryption-example.demo"), + obj.VirtualHost( + os.path.join(prefix, "default-ssl.conf"), + os.path.join(aug_pre, + "default-ssl.conf/IfModule/VirtualHost"), + {obj.Addr.fromstring("_default_:443")}, True, True), + obj.VirtualHost( + os.path.join(prefix, "000-default.conf"), + os.path.join(aug_pre, "000-default.conf/VirtualHost"), + {obj.Addr.fromstring("*:80"), + obj.Addr.fromstring("[::]:80")}, + False, True, "ip-172-30-0-17"), + obj.VirtualHost( + os.path.join(prefix, "certbot.conf"), + os.path.join(aug_pre, "certbot.conf/VirtualHost"), + {obj.Addr.fromstring("*:80")}, False, True, + "certbot.demo", aliases={"www.certbot.demo"}), + obj.VirtualHost( + os.path.join(prefix, "mod_macro-example.conf"), + os.path.join(aug_pre, + "mod_macro-example.conf/Macro/VirtualHost"), + {obj.Addr.fromstring("*:80")}, False, True, + modmacro=True), + obj.VirtualHost( + os.path.join(prefix, "default-ssl-port-only.conf"), + os.path.join(aug_pre, ("default-ssl-port-only.conf/" + "IfModule/VirtualHost")), + {obj.Addr.fromstring("_default_:443")}, True, True), + obj.VirtualHost( + os.path.join(prefix, "wildcard.conf"), + os.path.join(aug_pre, "wildcard.conf/VirtualHost"), + {obj.Addr.fromstring("*:80")}, False, True, + "ip-172-30-0-17", aliases={"*.blue.purple.com"}), + obj.VirtualHost( + os.path.join(prefix, "ocsp-ssl.conf"), + os.path.join(aug_pre, "ocsp-ssl.conf/IfModule/VirtualHost"), + {obj.Addr.fromstring("10.2.3.4:443")}, True, True, + "ocspvhost.com"), + obj.VirtualHost( + os.path.join(prefix, "non-symlink.conf"), + os.path.join(aug_pre, "non-symlink.conf/VirtualHost"), + {obj.Addr.fromstring("*:80")}, False, True, + "nonsym.link"), + obj.VirtualHost( + os.path.join(prefix, "default-ssl-port-only.conf"), + os.path.join(aug_pre, + "default-ssl-port-only.conf/VirtualHost"), + {obj.Addr.fromstring("*:80")}, True, True, ""), + obj.VirtualHost( + os.path.join(temp_dir, config_name, + "apache2/apache2.conf"), + "/files" + os.path.join(temp_dir, config_name, + "apache2/apache2.conf/VirtualHost"), + {obj.Addr.fromstring("*:80")}, False, True, + "vhost.in.rootconf"), + obj.VirtualHost( + os.path.join(prefix, "duplicatehttp.conf"), + os.path.join(aug_pre, "duplicatehttp.conf/VirtualHost"), + {obj.Addr.fromstring("10.2.3.4:80")}, False, True, + "duplicate.example.com"), + obj.VirtualHost( + os.path.join(prefix, "duplicatehttps.conf"), + os.path.join(aug_pre, "duplicatehttps.conf/IfModule/VirtualHost"), + {obj.Addr.fromstring("10.2.3.4:443")}, True, True, + "duplicate.example.com")] + return vh_truth + if config_name == "debian_apache_2_4/multi_vhosts": + prefix = os.path.join( + temp_dir, config_name, "apache2/sites-available") + aug_pre = "/files" + prefix + vh_truth = [ + obj.VirtualHost( + os.path.join(prefix, "default.conf"), + os.path.join(aug_pre, "default.conf/VirtualHost[1]"), + {obj.Addr.fromstring("*:80")}, + False, True, "ip-172-30-0-17"), + obj.VirtualHost( + os.path.join(prefix, "default.conf"), + os.path.join(aug_pre, "default.conf/VirtualHost[2]"), + {obj.Addr.fromstring("*:80")}, + False, True, "banana.vomit.com"), + obj.VirtualHost( + os.path.join(prefix, "multi-vhost.conf"), + os.path.join(aug_pre, "multi-vhost.conf/VirtualHost[1]"), + {obj.Addr.fromstring("*:80")}, + False, True, "1.multi.vhost.tld"), + obj.VirtualHost( + os.path.join(prefix, "multi-vhost.conf"), + os.path.join(aug_pre, "multi-vhost.conf/IfModule/VirtualHost"), + {obj.Addr.fromstring("*:80")}, + False, True, "2.multi.vhost.tld"), + obj.VirtualHost( + os.path.join(prefix, "multi-vhost.conf"), + os.path.join(aug_pre, "multi-vhost.conf/VirtualHost[2]"), + {obj.Addr.fromstring("*:80")}, + False, True, "3.multi.vhost.tld")] + return vh_truth + return None # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/common_test.py b/certbot/src/certbot/_internal/tests/plugins/common_test.py new file mode 100644 index 00000000000..2d1201ecf6f --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/common_test.py @@ -0,0 +1,374 @@ +"""Tests for certbot.plugins.common.""" +import functools +import shutil +import sys +import unittest +from unittest import mock + +import josepy as jose +import pytest + +from acme import challenges +from acme import messages +from certbot import achallenges +from certbot import crypto_util +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +from certbot.tests import acme_util +from certbot.tests import util as test_util + +AUTH_KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) +ACHALL = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb(challenges.HTTP01(token=b'token1'), + messages.STATUS_PENDING), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="encryption-example.demo"), + account_key=AUTH_KEY) + + +class NamespaceFunctionsTest(unittest.TestCase): + """Tests for certbot.plugins.common.*_namespace functions.""" + + def test_option_namespace(self): + from certbot.plugins.common import option_namespace + assert "foo-" == option_namespace("foo") + + def test_dest_namespace(self): + from certbot.plugins.common import dest_namespace + assert "foo_" == dest_namespace("foo") + + def test_dest_namespace_with_dashes(self): + from certbot.plugins.common import dest_namespace + assert "foo_bar_" == dest_namespace("foo-bar") + + +class PluginTest(unittest.TestCase): + """Test for certbot.plugins.common.Plugin.""" + + def setUp(self): + from certbot.plugins.common import Plugin + + class MockPlugin(Plugin): # pylint: disable=missing-docstring + def prepare(self) -> None: + pass + + def more_info(self) -> str: + return "info" + + @classmethod + def add_parser_arguments(cls, add): + add("foo-bar", dest="different_to_foo_bar", x=1, y=None) + + self.plugin_cls = MockPlugin + self.config = mock.MagicMock() + self.plugin = MockPlugin(config=self.config, name="mock") + + def test_init(self): + assert "mock" == self.plugin.name + assert self.config == self.plugin.config + + def test_option_namespace(self): + assert "mock-" == self.plugin.option_namespace + + def test_option_name(self): + assert "mock-foo_bar" == self.plugin.option_name("foo_bar") + + def test_dest_namespace(self): + assert "mock_" == self.plugin.dest_namespace + + def test_dest(self): + assert "mock_foo_bar" == self.plugin.dest("foo-bar") + assert "mock_foo_bar" == self.plugin.dest("foo_bar") + + def test_conf(self): + assert self.config.mock_foo_bar == self.plugin.conf("foo-bar") + + def test_inject_parser_options(self): + parser = mock.MagicMock() + self.plugin_cls.inject_parser_options(parser, "mock") + # note that inject_parser_options doesn't check if dest has + # correct prefix + parser.add_argument.assert_called_once_with( + "--mock-foo-bar", dest="different_to_foo_bar", x=1, y=None) + + def test_fallback_auth_hint(self): + assert "the mock plugin completed the required dns-01 challenges" in \ + self.plugin.auth_hint([acme_util.DNS01_A, acme_util.DNS01_A]) + assert "the mock plugin completed the required dns-01 and http-01 challenges" in \ + self.plugin.auth_hint([acme_util.DNS01_A, acme_util.HTTP01_A, + acme_util.DNS01_A]) + + +class InstallerTest(test_util.ConfigTestCase): + """Tests for certbot.plugins.common.Installer.""" + + def setUp(self): + super().setUp() + filesystem.mkdir(self.config.config_dir) + from certbot.tests.util import DummyInstaller + + self.installer = DummyInstaller(config=self.config, + name="Installer") + self.reverter = self.installer.reverter + + def test_add_to_real_checkpoint(self): + files = {"foo.bar", "baz.qux",} + save_notes = "foo bar baz qux" + self._test_wrapped_method("add_to_checkpoint", files, save_notes) + + def test_add_to_real_checkpoint2(self): + self._test_add_to_checkpoint_common(False) + + def test_add_to_temporary_checkpoint(self): + self._test_add_to_checkpoint_common(True) + + def _test_add_to_checkpoint_common(self, temporary): + files = {"foo.bar", "baz.qux",} + save_notes = "foo bar baz qux" + + installer_func = functools.partial(self.installer.add_to_checkpoint, + temporary=temporary) + + if temporary: + reverter_func_name = "add_to_temp_checkpoint" + else: + reverter_func_name = "add_to_checkpoint" + + self._test_adapted_method(installer_func, reverter_func_name, files, save_notes) + + def test_finalize_checkpoint(self): + self._test_wrapped_method("finalize_checkpoint", "foo") + + def test_recovery_routine(self): + self._test_wrapped_method("recovery_routine") + + def test_revert_temporary_config(self): + self._test_wrapped_method("revert_temporary_config") + + def test_rollback_checkpoints(self): + self._test_wrapped_method("rollback_checkpoints", 42) + + def _test_wrapped_method(self, name, *args, **kwargs): + """Test a wrapped reverter method. + + :param str name: name of the method to test + :param tuple args: position arguments to method + :param dict kwargs: keyword arguments to method + + """ + installer_func = getattr(self.installer, name) + self._test_adapted_method(installer_func, name, *args, **kwargs) + + def _test_adapted_method(self, installer_func, + reverter_func_name, *passed_args, **passed_kwargs): + """Test an adapted reverter method + + :param callable installer_func: installer method to test + :param str reverter_func_name: name of the method on the + reverter that should be called + :param tuple passed_args: positional arguments passed from + installer method to the reverter method + :param dict passed_kargs: keyword arguments passed from + installer method to the reverter method + + """ + with mock.patch.object(self.reverter, reverter_func_name) as reverter_func: + installer_func(*passed_args, **passed_kwargs) + reverter_func.assert_called_once_with(*passed_args, **passed_kwargs) + reverter_func.side_effect = errors.ReverterError + with pytest.raises(errors.PluginError): + installer_func(*passed_args, **passed_kwargs) + + def test_install_ssl_dhparams(self): + self.installer.install_ssl_dhparams() + assert os.path.isfile(self.installer.ssl_dhparams) + + def _current_ssl_dhparams_hash(self): + from certbot._internal.constants import SSL_DHPARAMS_SRC + return crypto_util.sha256sum(SSL_DHPARAMS_SRC) + + def test_current_file_hash_in_all_hashes(self): + from certbot._internal.constants import ALL_SSL_DHPARAMS_HASHES + assert self._current_ssl_dhparams_hash() in ALL_SSL_DHPARAMS_HASHES, \ + "Constants.ALL_SSL_DHPARAMS_HASHES must be appended" \ + " with the sha256 hash of self.config.ssl_dhparams when it is updated." + + +class AddrTest(unittest.TestCase): + """Tests for certbot.plugins.common.Addr.""" + + def setUp(self): + from certbot.plugins.common import Addr + self.addr1 = Addr.fromstring("192.168.1.1") + self.addr2 = Addr.fromstring("192.168.1.1:*") + self.addr3 = Addr.fromstring("192.168.1.1:80") + self.addr4 = Addr.fromstring("[fe00::1]") + self.addr5 = Addr.fromstring("[fe00::1]:*") + self.addr6 = Addr.fromstring("[fe00::1]:80") + self.addr7 = Addr.fromstring("[fe00::1]:5") + self.addr8 = Addr.fromstring("[fe00:1:2:3:4:5:6:7:8:9]:8080") + + def test_fromstring(self): + assert self.addr1.get_addr() == "192.168.1.1" + assert self.addr1.get_port() == "" + assert self.addr2.get_addr() == "192.168.1.1" + assert self.addr2.get_port() == "*" + assert self.addr3.get_addr() == "192.168.1.1" + assert self.addr3.get_port() == "80" + assert self.addr4.get_addr() == "[fe00::1]" + assert self.addr4.get_port() == "" + assert self.addr5.get_addr() == "[fe00::1]" + assert self.addr5.get_port() == "*" + assert self.addr6.get_addr() == "[fe00::1]" + assert self.addr6.get_port() == "80" + assert self.addr6.get_ipv6_exploded() == \ + "fe00:0:0:0:0:0:0:1" + assert self.addr1.get_ipv6_exploded() == \ + "" + assert self.addr7.get_port() == "5" + assert self.addr8.get_ipv6_exploded() == \ + "fe00:1:2:3:4:5:6:7" + + def test_str(self): + assert str(self.addr1) == "192.168.1.1" + assert str(self.addr2) == "192.168.1.1:*" + assert str(self.addr3) == "192.168.1.1:80" + assert str(self.addr4) == "[fe00::1]" + assert str(self.addr5) == "[fe00::1]:*" + assert str(self.addr6) == "[fe00::1]:80" + + def test_get_addr_obj(self): + assert str(self.addr1.get_addr_obj("443")) == "192.168.1.1:443" + assert str(self.addr2.get_addr_obj("")) == "192.168.1.1" + assert str(self.addr1.get_addr_obj("*")) == "192.168.1.1:*" + assert str(self.addr4.get_addr_obj("443")) == "[fe00::1]:443" + assert str(self.addr5.get_addr_obj("")) == "[fe00::1]" + assert str(self.addr4.get_addr_obj("*")) == "[fe00::1]:*" + + def test_eq(self): + assert self.addr1 == self.addr2.get_addr_obj("") + assert self.addr1 != self.addr2 + assert self.addr1 != 3333 + + assert self.addr4 == self.addr4.get_addr_obj("") + assert self.addr4 != self.addr5 + assert self.addr4 != 3333 + from certbot.plugins.common import Addr + assert self.addr4 == Addr.fromstring("[fe00:0:0::1]") + assert self.addr4 == Addr.fromstring("[fe00:0::0:0:1]") + + + def test_set_inclusion(self): + from certbot.plugins.common import Addr + set_a = {self.addr1, self.addr2} + addr1b = Addr.fromstring("192.168.1.1") + addr2b = Addr.fromstring("192.168.1.1:*") + set_b = {addr1b, addr2b} + + assert set_a == set_b + + set_c = {self.addr4, self.addr5} + addr4b = Addr.fromstring("[fe00::1]") + addr5b = Addr.fromstring("[fe00::1]:*") + set_d = {addr4b, addr5b} + + assert set_c == set_d + + +class ChallengePerformerTest(unittest.TestCase): + """Tests for certbot.plugins.common.ChallengePerformer.""" + + def setUp(self): + configurator = mock.MagicMock() + + from certbot.plugins.common import ChallengePerformer + self.performer = ChallengePerformer(configurator) + + def test_add_chall(self): + self.performer.add_chall(ACHALL, 0) + assert 1 == len(self.performer.achalls) + assert [0] == self.performer.indices + + def test_perform(self): + with pytest.raises(NotImplementedError): + self.performer.perform() + + +class InstallVersionControlledFileTest(test_util.TempDirTestCase): + """Tests for certbot.plugins.common.install_version_controlled_file.""" + + def setUp(self): + super().setUp() + self.hashes = ["someotherhash"] + self.dest_path = os.path.join(self.tempdir, "options-ssl-dest.conf") + self.hash_path = os.path.join(self.tempdir, ".options-ssl-conf.txt") + self.old_path = os.path.join(self.tempdir, "options-ssl-old.conf") + self.source_path = os.path.join(self.tempdir, "options-ssl-src.conf") + for path in (self.source_path, self.old_path,): + with open(path, "w") as f: + f.write(path) + self.hashes.append(crypto_util.sha256sum(path)) + + def _call(self): + from certbot.plugins.common import install_version_controlled_file + install_version_controlled_file(self.dest_path, + self.hash_path, + self.source_path, + self.hashes) + + def _current_file_hash(self): + return crypto_util.sha256sum(self.source_path) + + def _assert_current_file(self): + assert os.path.isfile(self.dest_path) + assert crypto_util.sha256sum(self.dest_path) == \ + self._current_file_hash() + + def test_no_file(self): + assert not os.path.isfile(self.dest_path) + self._call() + self._assert_current_file() + + def test_current_file(self): + # 1st iteration installs the file, the 2nd checks if it needs updating + for _ in range(2): + self._call() + self._assert_current_file() + + def test_prev_file_updates_to_current(self): + shutil.copyfile(self.old_path, self.dest_path) + self._call() + self._assert_current_file() + + def test_manually_modified_current_file_does_not_update(self): + self._call() + with open(self.dest_path, "a") as mod_ssl_conf: + mod_ssl_conf.write("a new line for the wrong hash\n") + with mock.patch("certbot.plugins.common.logger") as mock_logger: + self._call() + assert mock_logger.warning.called is False + assert os.path.isfile(self.dest_path) + assert crypto_util.sha256sum(self.source_path) == \ + self._current_file_hash() + assert crypto_util.sha256sum(self.dest_path) != \ + self._current_file_hash() + + def test_manually_modified_past_file_warns(self): + with open(self.dest_path, "a") as mod_ssl_conf: + mod_ssl_conf.write("a new line for the wrong hash\n") + with open(self.hash_path, "w") as f: + f.write("hashofanoldversion") + with mock.patch("certbot.plugins.common.logger") as mock_logger: + self._call() + assert mock_logger.warning.call_args[0][0] == \ + "%s has been manually modified; updated file " \ + "saved to %s. We recommend updating %s for security purposes." + assert crypto_util.sha256sum(self.source_path) == \ + self._current_file_hash() + # only print warning once + with mock.patch("certbot.plugins.common.logger") as mock_logger: + self._call() + assert mock_logger.warning.called is False + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/disco_test.py b/certbot/src/certbot/_internal/tests/plugins/disco_test.py new file mode 100644 index 00000000000..2bef8cf0706 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/disco_test.py @@ -0,0 +1,304 @@ +"""Tests for certbot._internal.plugins.disco.""" +import functools +import string +import sys +import unittest +from unittest import mock + +import pytest + +from certbot import errors +from certbot import interfaces +from certbot._internal.plugins import null +from certbot._internal.plugins import standalone +from certbot._internal.plugins import webroot + +if sys.version_info >= (3, 10): # pragma: no cover + import importlib.metadata as importlib_metadata +else: + import importlib_metadata + + +class _EntryPointLoadFail(importlib_metadata.EntryPoint): + def load(self): + raise RuntimeError("Loading failure") + + +EP_SA = importlib_metadata.EntryPoint( + name="sa", + value="certbot._internal.plugins.standalone:Authenticator", + group="certbot.plugins") + +EP_WR = importlib_metadata.EntryPoint( + name="wr", + value="certbot._internal.plugins.webroot:Authenticator", + group="certbot.plugins") + +EP_SA_LOADFAIL = _EntryPointLoadFail( + name="sa", + value="certbot._internal.plugins.standalone:Authenticator", + group="certbot.plugins") + + +class PluginEntryPointTest(unittest.TestCase): + """Tests for certbot._internal.plugins.disco.PluginEntryPoint.""" + + def setUp(self): + self.ep1 = importlib_metadata.EntryPoint( + name="ep1", + value="p1.ep1:Authenticator", + group="certbot.plugins") + self.ep1prim = importlib_metadata.EntryPoint( + name="ep1", + value="p2.pe2:Authenticator", + group="certbot.plugins") + # nested + self.ep2 = importlib_metadata.EntryPoint( + name="ep2", + value="p2.foo.ep2:Authenticator", + group="certbot.plugins") + # project name != top-level package name + self.ep3 = importlib_metadata.EntryPoint( + name="ep3", + value="a.ep3:Authenticator", + group="certbot.plugins") + + from certbot._internal.plugins.disco import PluginEntryPoint + self.plugin_ep = PluginEntryPoint(EP_SA) + + def test_entry_point_to_plugin_name_not_prefixed(self): + from certbot._internal.plugins.disco import PluginEntryPoint + + names = { + self.ep1: "ep1", + self.ep1prim: "ep1", + self.ep2: "ep2", + self.ep3: "ep3", + EP_SA: "sa", + } + + for entry_point, name in names.items(): + assert name == PluginEntryPoint.entry_point_to_plugin_name(entry_point) + + def test_description(self): + assert "server locally" in self.plugin_ep.description + + def test_description_with_name(self): + self.plugin_ep.plugin_cls = mock.MagicMock(description="Desc") + assert "Desc (sa)" == self.plugin_ep.description_with_name + + def test_long_description(self): + self.plugin_ep.plugin_cls = mock.MagicMock( + long_description="Long desc") + assert "Long desc" == self.plugin_ep.long_description + + def test_long_description_nonexistent(self): + self.plugin_ep.plugin_cls = mock.MagicMock( + description="Long desc not found", spec=["description"]) + assert "Long desc not found" == self.plugin_ep.long_description + + def test_ifaces(self): + assert self.plugin_ep.ifaces((interfaces.Authenticator,)) + assert not self.plugin_ep.ifaces((interfaces.Installer,)) + assert not self.plugin_ep.ifaces(( + interfaces.Installer, interfaces.Authenticator)) + + def test__init__(self): + assert self.plugin_ep.initialized is False + assert self.plugin_ep.prepared is False + assert self.plugin_ep.misconfigured is False + assert self.plugin_ep.available is False + assert self.plugin_ep.problem is None + assert self.plugin_ep.entry_point is EP_SA + assert "sa" == self.plugin_ep.name + + assert self.plugin_ep.plugin_cls is standalone.Authenticator + + def test_init(self): + config = mock.MagicMock() + plugin = self.plugin_ep.init(config=config) + assert self.plugin_ep.initialized is True + assert plugin.config is config + # memoize! + assert self.plugin_ep.init() is plugin + assert plugin.config is config + # try to give different config + assert self.plugin_ep.init(123) is plugin + assert plugin.config is config + + assert self.plugin_ep.prepared is False + assert self.plugin_ep.misconfigured is False + assert self.plugin_ep.available is False + + def test_prepare(self): + config = mock.MagicMock() + self.plugin_ep.init(config=config) + self.plugin_ep.prepare() + assert self.plugin_ep.prepared + assert self.plugin_ep.misconfigured is False + + # output doesn't matter that much, just test if it runs + str(self.plugin_ep) + + def test_prepare_misconfigured(self): + plugin = mock.MagicMock() + plugin.prepare.side_effect = errors.MisconfigurationError + # pylint: disable=protected-access + self.plugin_ep._initialized = plugin + assert isinstance(self.plugin_ep.prepare(), errors.MisconfigurationError) + assert self.plugin_ep.prepared + assert self.plugin_ep.misconfigured + assert isinstance(self.plugin_ep.problem, errors.MisconfigurationError) + assert self.plugin_ep.available + + def test_prepare_no_installation(self): + plugin = mock.MagicMock() + plugin.prepare.side_effect = errors.NoInstallationError + # pylint: disable=protected-access + self.plugin_ep._initialized = plugin + assert isinstance(self.plugin_ep.prepare(), errors.NoInstallationError) + assert self.plugin_ep.prepared is True + assert self.plugin_ep.misconfigured is False + assert self.plugin_ep.available is False + + def test_prepare_generic_plugin_error(self): + plugin = mock.MagicMock() + plugin.prepare.side_effect = errors.PluginError + # pylint: disable=protected-access + self.plugin_ep._initialized = plugin + assert isinstance(self.plugin_ep.prepare(), errors.PluginError) + assert self.plugin_ep.prepared + assert self.plugin_ep.misconfigured is False + assert self.plugin_ep.available is False + + def test_str(self): + output = str(self.plugin_ep) + assert "Authenticator" in output + assert "Installer" not in output + assert "Plugin" in output + + def test_repr(self): + assert "PluginEntryPoint#sa" == repr(self.plugin_ep) + + +class PluginsRegistryTest(unittest.TestCase): + """Tests for certbot._internal.plugins.disco.PluginsRegistry.""" + + @classmethod + def _create_new_registry(cls, plugins): + from certbot._internal.plugins.disco import PluginsRegistry + return PluginsRegistry(plugins) + + def setUp(self): + self.plugin_ep = mock.MagicMock() + self.plugin_ep.name = "mock" + self.plugin_ep.__hash__.side_effect = TypeError + self.plugins = {self.plugin_ep.name: self.plugin_ep} + self.reg = self._create_new_registry(self.plugins) + self.ep1 = importlib_metadata.EntryPoint( + name="ep1", + value="p1.ep1", + group="certbot.plugins") + + def test_find_all(self): + from certbot._internal.plugins.disco import PluginsRegistry + with mock.patch("certbot._internal.plugins.disco.importlib_metadata") as mock_meta: + mock_meta.entry_points.side_effect = [ + [EP_SA], [EP_WR, self.ep1], + ] + with mock.patch.object(importlib_metadata.EntryPoint, 'load') as mock_load: + mock_load.side_effect = [ + standalone.Authenticator, webroot.Authenticator, + null.Installer, null.Installer] + plugins = PluginsRegistry.find_all() + assert plugins["sa"].plugin_cls is standalone.Authenticator + assert plugins["sa"].entry_point is EP_SA + assert plugins["wr"].plugin_cls is webroot.Authenticator + assert plugins["wr"].entry_point is EP_WR + assert plugins["ep1"].plugin_cls is null.Installer + assert plugins["ep1"].entry_point is self.ep1 + assert "p1:ep1" not in plugins + + def test_find_all_error_message(self): + from certbot._internal.plugins.disco import PluginsRegistry + with mock.patch("certbot._internal.plugins.disco.importlib_metadata") as mock_meta: + #EP_SA.load = None # This triggers a TypeError when the entrypoint loads + mock_meta.entry_points.side_effect = [ + [EP_SA_LOADFAIL], [EP_WR, self.ep1], + ] + with self.assertRaises(errors.PluginError) as cm: + PluginsRegistry.find_all() + assert "standalone' plugin errored" in str(cm.exception) + + def test_getitem(self): + assert self.plugin_ep == self.reg["mock"] + + def test_iter(self): + assert ["mock"] == list(self.reg) + + def test_len(self): + assert 0 == len(self._create_new_registry({})) + assert 1 == len(self.reg) + + def test_init(self): + self.plugin_ep.init.return_value = "baz" + assert ["baz"] == self.reg.init("bar") + self.plugin_ep.init.assert_called_once_with("bar") + + def test_filter(self): + assert self.plugins == \ + self.reg.filter(lambda p_ep: p_ep.name.startswith("m")) + assert {} == self.reg.filter(lambda p_ep: p_ep.name.startswith("b")) + + def test_ifaces(self): + self.plugin_ep.ifaces.return_value = True + # pylint: disable=protected-access + assert self.plugins == self.reg.ifaces()._plugins + self.plugin_ep.ifaces.return_value = False + assert {} == self.reg.ifaces()._plugins + + def test_prepare(self): + self.plugin_ep.prepare.return_value = "baz" + assert ["baz"] == self.reg.prepare() + self.plugin_ep.prepare.assert_called_once_with() + + def test_prepare_order(self): + order: list[str] = [] + plugins = { + c: mock.MagicMock(prepare=functools.partial(order.append, c)) + for c in string.ascii_letters + } + reg = self._create_new_registry(plugins) + reg.prepare() + # order of prepare calls must be sorted to prevent deadlock + # caused by plugins acquiring locks during prepare + assert order == sorted(string.ascii_letters) + + def test_available(self): + self.plugin_ep.available = True + # pylint: disable=protected-access + assert self.plugins == self.reg.available()._plugins + self.plugin_ep.available = False + assert {} == self.reg.available()._plugins + + def test_find_init(self): + assert self.reg.find_init(mock.Mock()) is None + self.plugin_ep.initialized = True + assert self.reg.find_init(self.plugin_ep.init()) is self.plugin_ep + + def test_repr(self): + self.plugin_ep.__repr__ = lambda _: "PluginEntryPoint#mock" + assert "PluginsRegistry(PluginEntryPoint#mock)" == \ + repr(self.reg) + + def test_str(self): + assert "No plugins" == str(self._create_new_registry({})) + self.plugin_ep.__str__ = lambda _: "Mock" + assert "Mock" == str(self.reg) + plugins = {self.plugin_ep.name: self.plugin_ep, "foo": "Bar"} + reg = self._create_new_registry(plugins) + assert "Bar\n\nMock" == str(reg) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/dns_common_test.py b/certbot/src/certbot/_internal/tests/plugins/dns_common_test.py new file mode 100644 index 00000000000..c74910d40a9 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/dns_common_test.py @@ -0,0 +1,237 @@ +"""Tests for certbot.plugins.dns_common.""" + +import collections +import logging +import sys +import unittest +from unittest import mock + +import pytest + +from certbot import errors +from certbot import util +from certbot.compat import os +from certbot.display import util as display_util +from certbot.plugins import dns_common +from certbot.plugins import dns_test_common +from certbot.tests import util as test_util + + +class DNSAuthenticatorTest(test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest): + # pylint: disable=protected-access + + class _FakeDNSAuthenticator(dns_common.DNSAuthenticator): + _setup_credentials = mock.MagicMock() + _perform = mock.MagicMock() + _cleanup = mock.MagicMock() + + def more_info(self): # pylint: disable=missing-docstring,no-self-use + return 'A fake authenticator for testing.' + + class _FakeConfig: + fake_propagation_seconds = 0 + fake_config_key = 1 + fake_other_key = None + fake_file_path = None + + def setUp(self): + super().setUp() + + self.config = DNSAuthenticatorTest._FakeConfig() + + self.auth = DNSAuthenticatorTest._FakeDNSAuthenticator(self.config, "fake") + + @test_util.patch_display_util() + def test_perform(self, unused_mock_get_utility): + self.auth.perform([self.achall]) + + self.auth._perform.assert_called_once_with(dns_test_common.DOMAIN, mock.ANY, mock.ANY) + + def test_cleanup(self): + self.auth._attempt_cleanup = True + + self.auth.cleanup([self.achall]) + + self.auth._cleanup.assert_called_once_with(dns_test_common.DOMAIN, mock.ANY, mock.ANY) + + @test_util.patch_display_util() + def test_prompt(self, mock_get_utility): + mock_display = mock_get_utility() + mock_display.input.side_effect = ((display_util.OK, "",), + (display_util.OK, "value",)) + + self.auth._configure("other_key", "") + assert self.auth.config.fake_other_key == "value" + + @test_util.patch_display_util() + def test_prompt_canceled(self, mock_get_utility): + mock_display = mock_get_utility() + mock_display.input.side_effect = ((display_util.CANCEL, "c",),) + + with pytest.raises(errors.PluginError): + self.auth._configure("other_key", "") + + @test_util.patch_display_util() + def test_prompt_file(self, mock_get_utility): + path = os.path.join(self.tempdir, 'file.ini') + open(path, "wb").close() + + mock_display = mock_get_utility() + mock_display.directory_select.side_effect = ((display_util.OK, "",), + (display_util.OK, "not-a-file.ini",), + (display_util.OK, self.tempdir), + (display_util.OK, path,)) + + self.auth._configure_file("file_path", "") + assert self.auth.config.fake_file_path == path + + @test_util.patch_display_util() + def test_prompt_file_canceled(self, mock_get_utility): + mock_display = mock_get_utility() + mock_display.directory_select.side_effect = ((display_util.CANCEL, "c",),) + + with pytest.raises(errors.PluginError): + self.auth._configure_file("file_path", "") + + def test_configure_credentials(self): + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write({"fake_test": "value"}, path) + setattr(self.config, "fake_credentials", path) + + credentials = self.auth._configure_credentials("credentials", "", {"test": ""}) + + assert credentials.conf("test") == "value" + + @test_util.patch_display_util() + def test_prompt_credentials(self, mock_get_utility): + bad_path = os.path.join(self.tempdir, 'bad-file.ini') + dns_test_common.write({"fake_other": "other_value"}, bad_path) + + path = os.path.join(self.tempdir, 'file.ini') + dns_test_common.write({"fake_test": "value"}, path) + setattr(self.config, "fake_credentials", "") + + mock_display = mock_get_utility() + mock_display.directory_select.side_effect = ((display_util.OK, "",), + (display_util.OK, "not-a-file.ini",), + (display_util.OK, self.tempdir), + (display_util.OK, bad_path), + (display_util.OK, path,)) + + credentials = self.auth._configure_credentials("credentials", "", {"test": ""}) + assert credentials.conf("test") == "value" + + def test_auth_hint(self): + assert 'try increasing --fake-propagation-seconds (currently 0 seconds).' in \ + self.auth.auth_hint([mock.MagicMock()]) + + +class CredentialsConfigurationTest(test_util.TempDirTestCase): + class _MockLoggingHandler(logging.Handler): + messages = None + + def __init__(self, *args, **kwargs): + self.reset() + super().__init__(*args, **kwargs) + + def emit(self, record): + self.messages[record.levelname.lower()].append(record.getMessage()) + + def reset(self): + """Allows the handler to be reset between tests.""" + self.messages = collections.defaultdict(list) + + def test_valid_file(self): + path = os.path.join(self.tempdir, 'too-permissive-file.ini') + + dns_test_common.write({"test": "value", "other": 1}, path) + + credentials_configuration = dns_common.CredentialsConfiguration(path) + assert "value" == credentials_configuration.conf("test") + assert "1" == credentials_configuration.conf("other") + + def test_nonexistent_file(self): + path = os.path.join(self.tempdir, 'not-a-file.ini') + + with pytest.raises(errors.PluginError): + dns_common.CredentialsConfiguration(path) + + def test_valid_file_with_unsafe_permissions(self): + log = self._MockLoggingHandler() + dns_common.logger.addHandler(log) + + path = os.path.join(self.tempdir, 'too-permissive-file.ini') + util.safe_open(path, "wb", 0o744).close() + + dns_common.CredentialsConfiguration(path) + + assert 1 == len([_ for _ in log.messages['warning'] if _.startswith("Unsafe")]) + + +class CredentialsConfigurationRequireTest(test_util.TempDirTestCase): + + def setUp(self): + super().setUp() + + self.path = os.path.join(self.tempdir, 'file.ini') + + def _write(self, values): + dns_test_common.write(values, self.path) + + def test_valid(self): + self._write({"test": "value", "other": 1}) + + credentials_configuration = dns_common.CredentialsConfiguration(self.path) + credentials_configuration.require({"test": "", "other": ""}) + + def test_valid_but_extra(self): + self._write({"test": "value", "other": 1}) + + credentials_configuration = dns_common.CredentialsConfiguration(self.path) + credentials_configuration.require({"test": ""}) + + def test_valid_empty(self): + self._write({}) + + credentials_configuration = dns_common.CredentialsConfiguration(self.path) + credentials_configuration.require({}) + + def test_missing(self): + self._write({}) + + credentials_configuration = dns_common.CredentialsConfiguration(self.path) + with pytest.raises(errors.PluginError): + credentials_configuration.require({"test": ""}) + + def test_blank(self): + self._write({"test": ""}) + + credentials_configuration = dns_common.CredentialsConfiguration(self.path) + with pytest.raises(errors.PluginError): + credentials_configuration.require({"test": ""}) + + def test_typo(self): + self._write({"tets": "typo!"}) + + credentials_configuration = dns_common.CredentialsConfiguration(self.path) + with pytest.raises(errors.PluginError): + credentials_configuration.require({"test": ""}) + + +class DomainNameGuessTest(unittest.TestCase): + + def test_simple_case(self): + assert 'example.com' in \ + dns_common.base_domain_name_guesses("example.com") + + def test_sub_domain(self): + assert 'example.com' in \ + dns_common.base_domain_name_guesses("foo.bar.baz.example.com") + + def test_second_level_domain(self): + assert 'example.co.uk' in \ + dns_common.base_domain_name_guesses("foo.bar.baz.example.co.uk") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/enhancements_test.py b/certbot/src/certbot/_internal/tests/plugins/enhancements_test.py new file mode 100644 index 00000000000..39bf3adae6a --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/enhancements_test.py @@ -0,0 +1,62 @@ +"""Tests for new style enhancements""" +import sys +from unittest import mock + +import pytest + +from certbot._internal.plugins import null +from certbot.plugins import enhancements +import certbot.tests.util as test_util + + +class EnhancementTest(test_util.ConfigTestCase): + """Tests for new style enhancements in certbot.plugins.enhancements""" + + def setUp(self): + super().setUp() + self.mockinstaller = mock.MagicMock(spec=enhancements.AutoHSTSEnhancement) + + @test_util.patch_display_util() + def test_enhancement_enabled_enhancements(self, _): + FAKEINDEX = [ + { + "name": "autohsts", + "cli_dest": "auto_hsts", + }, + { + "name": "somethingelse", + "cli_dest": "something", + } + ] + with mock.patch("certbot.plugins.enhancements._INDEX", FAKEINDEX): + self.config.auto_hsts = True + self.config.something = True + enabled = list(enhancements.enabled_enhancements(self.config)) + assert len(enabled) == 2 + assert [i for i in enabled if i["name"] == "autohsts"] + assert [i for i in enabled if i["name"] == "somethingelse"] + + def test_are_requested(self): + assert len(list(enhancements.enabled_enhancements(self.config))) == 0 + assert not enhancements.are_requested(self.config) + self.config.auto_hsts = True + assert len(list(enhancements.enabled_enhancements(self.config))) == 1 + assert enhancements.are_requested(self.config) + + def test_are_supported(self): + self.config.auto_hsts = True + unsupported = null.Installer(self.config, "null") + assert enhancements.are_supported(self.config, self.mockinstaller) + assert not enhancements.are_supported(self.config, unsupported) + + def test_enable(self): + self.config.auto_hsts = True + domains = ["example.com", "www.example.com"] + lineage = "lineage" + enhancements.enable(lineage, domains, self.mockinstaller, self.config) + assert self.mockinstaller.enable_autohsts.called + assert self.mockinstaller.enable_autohsts.call_args[0] == (lineage, domains) + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/manual_test.py b/certbot/src/certbot/_internal/tests/plugins/manual_test.py new file mode 100644 index 00000000000..7b42719e7bc --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/manual_test.py @@ -0,0 +1,160 @@ +"""Tests for certbot._internal.plugins.manual""" +import sys +import textwrap +from unittest import mock + +import pytest + +from acme import challenges +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +from certbot.tests import acme_util +from certbot.tests import util as test_util + + +class AuthenticatorTest(test_util.TempDirTestCase): + """Tests for certbot._internal.plugins.manual.Authenticator.""" + + def setUp(self): + super().setUp() + get_display_patch = test_util.patch_display_util() + self.mock_get_display = get_display_patch.start() + self.addCleanup(get_display_patch.stop) + + self.http_achall = acme_util.HTTP01_A + self.dns_achall = acme_util.DNS01_A + self.dns_achall_2 = acme_util.DNS01_A_2 + self.achalls = [self.http_achall, self.dns_achall, self.dns_achall_2] + for d in ["config_dir", "work_dir", "in_progress"]: + filesystem.mkdir(os.path.join(self.tempdir, d)) + # "backup_dir" and "temp_checkpoint_dir" get created in + # certbot.util.make_or_verify_dir() during the Reverter + # initialization. + self.config = mock.MagicMock( + http01_port=0, manual_auth_hook=None, manual_cleanup_hook=None, + noninteractive_mode=False, validate_hooks=False, + config_dir=os.path.join(self.tempdir, "config_dir"), + work_dir=os.path.join(self.tempdir, "work_dir"), + backup_dir=os.path.join(self.tempdir, "backup_dir"), + temp_checkpoint_dir=os.path.join( + self.tempdir, "temp_checkpoint_dir"), + in_progress_dir=os.path.join(self.tempdir, "in_progess")) + + from certbot._internal.plugins.manual import Authenticator + self.auth = Authenticator(self.config, name='manual') + + def test_prepare_no_hook_noninteractive(self): + self.config.noninteractive_mode = True + with pytest.raises(errors.PluginError): + self.auth.prepare() + + def test_prepare_bad_hook(self): + self.config.manual_auth_hook = os.path.abspath(os.sep) # is / on UNIX + self.config.validate_hooks = True + with pytest.raises(errors.HookCommandNotFound): + self.auth.prepare() + + def test_more_info(self): + assert isinstance(self.auth.more_info(), str) + + def test_get_chall_pref(self): + assert self.auth.get_chall_pref('example.org') == \ + [challenges.HTTP01, challenges.DNS01] + + def test_script_perform(self): + self.config.manual_auth_hook = ( + '{0} -c "' + 'from certbot.compat import os;' + 'print(os.environ.get(\'CERTBOT_DOMAIN\'));' + 'print(os.environ.get(\'CERTBOT_IDENTIFIER\'));' + 'print(os.environ.get(\'CERTBOT_TOKEN\', \'notoken\'));' + 'print(os.environ.get(\'CERTBOT_VALIDATION\', \'novalidation\'));' + 'print(os.environ.get(\'CERTBOT_ALL_DOMAINS\'));' + 'print(os.environ.get(\'CERTBOT_ALL_IDENTIFIERS\'));' + 'print(os.environ.get(\'CERTBOT_REMAINING_CHALLENGES\'));"' + .format(sys.executable)) + dns_expected = '{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}'.format( + self.dns_achall.identifier.value, + self.dns_achall.identifier.value, + 'notoken', + self.dns_achall.validation(self.dns_achall.account_key), + ','.join(achall.identifier.value for achall in self.achalls), + ','.join(achall.identifier.value for achall in self.achalls), + len(self.achalls) - self.achalls.index(self.dns_achall) - 1) + http_expected = '{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}'.format( + self.http_achall.identifier.value, + self.http_achall.identifier.value, + self.http_achall.chall.encode('token'), + self.http_achall.validation(self.http_achall.account_key), + ','.join(achall.identifier.value for achall in self.achalls), + ','.join(achall.identifier.value for achall in self.achalls), + len(self.achalls) - self.achalls.index(self.http_achall) - 1) + + assert self.auth.perform(self.achalls) == \ + [achall.response(achall.account_key) for achall in self.achalls] + assert self.auth.env[self.dns_achall]['CERTBOT_AUTH_OUTPUT'] == \ + dns_expected + assert self.auth.env[self.http_achall]['CERTBOT_AUTH_OUTPUT'] == \ + http_expected + + # Successful hook output should be sent to notify + assert self.mock_get_display().notification.call_count == len(self.achalls) + for i, (args, _) in enumerate(self.mock_get_display().notification.call_args_list): + needle = textwrap.indent(self.auth.env[self.achalls[i]]['CERTBOT_AUTH_OUTPUT'], ' ') + assert needle in args[0] + + def test_manual_perform(self): + assert self.auth.perform(self.achalls) == \ + [achall.response(achall.account_key) for achall in self.achalls] + + assert self.mock_get_display().notification.call_count == len(self.achalls) + for i, (args, kwargs) in enumerate(self.mock_get_display().notification.call_args_list): + achall = self.achalls[i] + assert achall.validation(achall.account_key) in args[0] + assert kwargs['wrap'] is False + + def test_cleanup(self): + self.config.manual_auth_hook = ('{0} -c "import sys; sys.stdout.write(\'foo\')"' + .format(sys.executable)) + self.config.manual_cleanup_hook = '# cleanup' + self.auth.perform(self.achalls) + + for achall in self.achalls: + self.auth.cleanup([achall]) + assert os.environ['CERTBOT_AUTH_OUTPUT'] == 'foo' + assert os.environ['CERTBOT_DOMAIN'] == achall.identifier.value + assert os.environ['CERTBOT_IDENTIFIER'] == achall.identifier.value + if isinstance(achall.chall, (challenges.HTTP01, challenges.DNS01)): + assert os.environ['CERTBOT_VALIDATION'] == \ + achall.validation(achall.account_key) + if isinstance(achall.chall, challenges.HTTP01): + assert os.environ['CERTBOT_TOKEN'] == \ + achall.chall.encode('token') + else: + assert 'CERTBOT_TOKEN' not in os.environ + + def test_auth_hint_hook(self): + self.config.manual_auth_hook = '/bin/true' + assert self.auth.auth_hint([acme_util.DNS01_A, acme_util.HTTP01_A]) == \ + 'The Certificate Authority failed to verify the DNS TXT records and challenge ' \ + 'files created by the --manual-auth-hook. Ensure that this hook is functioning ' \ + 'correctly and that it waits a sufficient duration of time for DNS propagation. ' \ + 'Refer to "certbot --help manual" and the Certbot User Guide.' + assert self.auth.auth_hint([acme_util.HTTP01_A]) == \ + 'The Certificate Authority failed to verify the challenge files created by the ' \ + '--manual-auth-hook. Ensure that this hook is functioning correctly. Refer to ' \ + '"certbot --help manual" and the Certbot User Guide.' + + def test_auth_hint_no_hook(self): + assert self.auth.auth_hint([acme_util.DNS01_A, acme_util.HTTP01_A]) == \ + 'The Certificate Authority failed to verify the manually created DNS TXT records ' \ + 'and challenge files. Ensure that you created these in the correct location, or ' \ + 'try waiting longer for DNS propagation on the next attempt.' + assert self.auth.auth_hint([acme_util.HTTP01_A, acme_util.HTTP01_A, acme_util.HTTP01_A]) == \ + 'The Certificate Authority failed to verify the manually created challenge files. ' \ + 'Ensure that you created these in the correct location.' + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/__init__.py b/certbot/src/certbot/_internal/tests/plugins/nginx/__init__.py new file mode 100644 index 00000000000..60c94e90096 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/__init__.py @@ -0,0 +1,6 @@ +"""certbot-nginx tests""" +import pytest + + +# Make sure we're only running these tests if our nginx plugin dependencies are installed +pytest.importorskip("pyparsing") diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/configurator_test.py b/certbot/src/certbot/_internal/tests/plugins/nginx/configurator_test.py new file mode 100644 index 00000000000..95ffbd35be4 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/configurator_test.py @@ -0,0 +1,1177 @@ +"""Test for certbot._internal.plugins.nginx.configurator.""" +import sys +from unittest import mock + +import pytest + +from acme import challenges +from acme import messages +from certbot import achallenges +from certbot import crypto_util +from certbot import errors +from certbot.compat import os +from certbot.tests import util as certbot_test_util +from certbot._internal.plugins.nginx import obj +from certbot._internal.plugins.nginx import parser +from certbot._internal.plugins.nginx.configurator import _redirect_block_for_domain +from certbot._internal.plugins.nginx.nginxparser import UnspacedList +from certbot._internal.tests.plugins.nginx import test_util as util + + +class NginxConfiguratorTest(util.NginxTest): + """Test a semi complex vhost configuration.""" + + def setUp(self): + super().setUp() + + self.config = self.get_nginx_configurator( + self.config_path, self.config_dir, self.work_dir, self.logs_dir) + + patch = mock.patch('certbot._internal.plugins.nginx.configurator.display_util.notify') + self.mock_notify = patch.start() + self.addCleanup(patch.stop) + + @mock.patch("certbot._internal.plugins.nginx.configurator.util.exe_exists") + def test_prepare_no_install(self, mock_exe_exists): + mock_exe_exists.return_value = False + with pytest.raises(errors.NoInstallationError): + self.config.prepare() + + def test_prepare(self): + assert (1, 6, 2) == self.config.version + assert 16 == len(self.config.parser.parsed) + + @mock.patch("certbot._internal.plugins.nginx.configurator.util.exe_exists") + @mock.patch("certbot._internal.plugins.nginx.configurator.subprocess.run") + def test_prepare_initializes_version(self, mock_run, mock_exe_exists): + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = "\n".join( + ["nginx version: nginx/1.6.2", + "built by clang 6.0 (clang-600.0.56)" + " (based on LLVM 3.5svn)", + "TLS SNI support enabled", + "configure arguments: --prefix=/usr/local/Cellar/" + "nginx/1.6.2 --with-http_ssl_module"]) + + mock_exe_exists.return_value = True + + self.config.version = None + self.config.config_test = mock.Mock() + self.config.prepare() + assert (1, 6, 2) == self.config.version + + def test_prepare_locked(self): + # It is important to test that server_root is locked during the call to + # prepare (as opposed to somewhere else during plugin execution) to + # ensure that this lock will be acquired after the Certbot package + # acquires all of its locks. (Tests that Certbot calls prepare after + # acquiring its locks are part of the Certbot package's tests.) Not + # doing this could result in deadlock from two versions of Certbot that + # acquire its locks in a different order. + server_root = self.config.conf("server-root") + + from certbot import util as certbot_util + certbot_util._LOCKS[server_root].release() # pylint: disable=protected-access + + self.config.config_test = mock.Mock() + certbot_test_util.lock_and_call(self._test_prepare_locked, server_root) + + @mock.patch("certbot._internal.plugins.nginx.configurator.util.exe_exists") + def _test_prepare_locked(self, unused_exe_exists): + try: + self.config.prepare() + except errors.PluginError as err: + err_msg = str(err) + assert "lock" in err_msg + assert self.config.conf("server-root") in err_msg + else: # pragma: no cover + self.fail("Exception wasn't raised!") + + @mock.patch("certbot._internal.plugins.nginx.configurator.socket.gethostname") + @mock.patch("certbot._internal.plugins.nginx.configurator.socket.gethostbyaddr") + def test_get_all_names(self, mock_gethostbyaddr, mock_gethostname): + mock_gethostbyaddr.return_value = ('155.225.50.69.nephoscale.net', [], []) + mock_gethostname.return_value = ('example.net') + names = self.config.get_all_names() + assert names == { + "155.225.50.69.nephoscale.net", "www.example.org", "another.alias", + "migration.com", "summer.com", "geese.com", "sslon.com", + "globalssl.com", "globalsslsetssl.com", "ipv6.com", "ipv6ssl.com", + "headers.com", "example.net", "ssl.both.com", "no-listens.com", + "addr-80.com"} + + def test_supported_enhancements(self): + assert ['redirect', 'ensure-http-header', 'staple-ocsp'] == \ + self.config.supported_enhancements() + + def test_enhance(self): + with pytest.raises(errors.PluginError): + self.config.enhance('myhost', 'unknown_enhancement') + + def test_get_chall_pref(self): + assert [challenges.HTTP01] == \ + self.config.get_chall_pref('myhost') + + def test_save(self): + filep = self.config.parser.abs_path('sites-enabled/example.com') + mock_vhost = obj.VirtualHost(filep, + None, None, None, + {'.example.com', 'example.*'}, + None, [0]) + self.config.parser.add_server_directives( + mock_vhost, + [['listen', ' ', '5001', ' ', 'ssl']]) + self.config.save() + + # pylint: disable=protected-access + parsed = self.config.parser._parse_files(filep, override=True) + assert [[['server'], + [['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', '.example.com'], + ['server_name', 'example.*'], + ['listen', '5001', 'ssl'], + ['#', parser.COMMENT]]]] == \ + parsed[filep] + + def test_choose_or_make_vhosts_alias(self): + self._test_choose_or_make_vhosts_common('alias', 'server_conf') + + def test_choose_or_make_vhosts_example_com(self): + self._test_choose_or_make_vhosts_common('example.com', 'example_conf') + + def test_choose_or_make_vhosts_localhost(self): + self._test_choose_or_make_vhosts_common('localhost', 'localhost_conf') + + def test_choose_or_make_vhosts_example_com_uk_test(self): + self._test_choose_or_make_vhosts_common('example.com.uk.test', 'example_conf') + + def test_choose_or_make_vhosts_www_example_com(self): + self._test_choose_or_make_vhosts_common('www.example.com', 'example_conf') + + def test_choose_or_make_vhosts_test_www_example_com(self): + self._test_choose_or_make_vhosts_common('test.www.example.com', 'foo_conf') + + def test_choose_or_make_vhosts_abc_www_foo_com(self): + self._test_choose_or_make_vhosts_common('abc.www.foo.com', 'foo_conf') + + def test_choose_or_make_vhosts_www_bar_co_uk(self): + self._test_choose_or_make_vhosts_common('www.bar.co.uk', 'localhost_conf') + + def test_choose_or_make_vhosts_ipv6_com(self): + self._test_choose_or_make_vhosts_common('ipv6.com', 'ipv6_conf') + + def _test_choose_or_make_vhosts_common(self, name, conf): + conf_names = {'localhost_conf': {'localhost', r'~^(www\.)?(example|bar)\.'}, + 'server_conf': {'somename', 'another.alias', 'alias'}, + 'example_conf': {'.example.com', 'example.*'}, + 'foo_conf': {'*.www.foo.com', '*.www.example.com'}, + 'ipv6_conf': {'ipv6.com'}} + + conf_path = {'localhost': "etc_nginx/nginx.conf", + 'alias': "etc_nginx/nginx.conf", + 'example.com': "etc_nginx/sites-enabled/example.com", + 'example.com.uk.test': "etc_nginx/sites-enabled/example.com", + 'www.example.com': "etc_nginx/sites-enabled/example.com", + 'test.www.example.com': "etc_nginx/foo.conf", + 'abc.www.foo.com': "etc_nginx/foo.conf", + 'www.bar.co.uk': "etc_nginx/nginx.conf", + 'ipv6.com': "etc_nginx/sites-enabled/ipv6.com"} + conf_path = {key: os.path.normpath(value) for key, value in conf_path.items()} + + vhost = self.config.choose_or_make_vhosts(name, 'key.pem', 'fullchain.pem')[0] + path = os.path.relpath(vhost.filep, self.temp_dir) + + assert conf_names[conf] == vhost.names + assert conf_path[name] == path + # IPv6 specific checks + if name == "ipv6.com": + assert vhost.ipv6_enabled() + # Make sure that we have SSL enabled also for IPv6 addr + assert any(True for x in vhost.addrs if x.ssl and x.ipv6) + + def test_choose_or_make_vhosts_bad(self): + bad_results = ['www.foo.com', 'example', 't.www.bar.co', + '69.255.225.155'] + + for name in bad_results: + with self.subTest(name=name): + with pytest.raises(errors.MisconfigurationError): + self.config.choose_or_make_vhosts(name, 'key.pem', 'fullchain.pem') + + def test_choose_or_make_vhosts_keep_ip_address(self): + # let's use a simple helper function to set key and fullchain values + def choose_or_make_vhosts(domain): + return self.config.choose_or_make_vhosts(domain, 'key.pem', 'fullchain.pem') + + # no listen on port 80 + # listen 69.50.225.155:9000; + # listen 127.0.0.1; + vhost = choose_or_make_vhosts('example.com')[0] + assert obj.Addr.fromstring("5001 ssl") in vhost.addrs + + # no listens at all + vhost = choose_or_make_vhosts('no-listens.com')[0] + assert obj.Addr.fromstring("5001 ssl") in vhost.addrs + assert obj.Addr.fromstring("80") in vhost.addrs + + # blank addr listen on 80 should result in blank addr ssl + # listen 80; + # listen [::]:80; + vhost = choose_or_make_vhosts('ipv6.com')[0] + assert obj.Addr.fromstring("5001 ssl") in vhost.addrs + assert obj.Addr.fromstring("[::]:5001 ssl") in vhost.addrs + + # listen on 80 with ip address should result in copied addr + # listen 1.2.3.4:80; + # listen [1:20::300]:80; + vhost = choose_or_make_vhosts('addr-80.com')[0] + assert obj.Addr.fromstring("1.2.3.4:5001 ssl") in vhost.addrs + assert obj.Addr.fromstring("[1:20::300]:5001 ssl ipv6only=on") in vhost.addrs + + def test_choose_or_make_vhost_ssl_directives(self): + conf_path = self.config.parser.abs_path('sites-enabled/example.com') + self.config.choose_or_make_vhosts('example.com', 'my-key.pem', 'my-fullchain.pem') + self.config.save() + self.config.parser.load() + parsed_conf = util.filter_comments(self.config.parser.parsed[conf_path]) + + expected_directives = [ + ['ssl_certificate', 'my-fullchain.pem'], + ['ssl_certificate_key', 'my-key.pem'], + ] + for directive in expected_directives: + assert util.contains_at_depth(parsed_conf, directive, 2) + + def test_ipv6only(self): + # ipv6_info: (ipv6_active, ipv6only_present) + assert (True, False) == self.config.ipv6_info("[::]", "80") + # Port 443 has ipv6only=on because of ipv6ssl.com vhost + assert (True, True) == self.config.ipv6_info("[::]", "443") + + def test_ipv6only_detection(self): + self.config.version = (1, 3, 1) + + vhost = self.config.choose_or_make_vhosts("ipv6.com", "key.pem", "fullchain.pem")[0] + for addr in vhost.addrs: + assert not addr.ipv6only + + def test_more_info(self): + assert 'nginx.conf' in self.config.more_info() + + def test_deploy_cert_requires_fullchain_path(self): + self.config.version = (1, 3, 1) + with pytest.raises(errors.PluginError): + self.config.deploy_cert("www.example.com", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + None) + + @mock.patch('certbot._internal.plugins.nginx.parser.NginxParser.update_or_add_server_directives') + def test_deploy_cert_raise_on_add_error(self, mock_update_or_add_server_directives): + mock_update_or_add_server_directives.side_effect = errors.MisconfigurationError() + with pytest.raises(errors.PluginError): + self.config.deploy_cert("migration.com", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + "example/fullchain.pem") + + def test_deploy_cert(self): + server_conf = self.config.parser.abs_path('server.conf') + nginx_conf = self.config.parser.abs_path('nginx.conf') + example_conf = self.config.parser.abs_path('sites-enabled/example.com') + self.config.version = (1, 3, 1) + + # Get the default SSL vhost + self.config.deploy_cert( + "www.example.com", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + "example/fullchain.pem") + self.config.deploy_cert( + "another.alias", + "/etc/nginx/cert.pem", + "/etc/nginx/key.pem", + "/etc/nginx/chain.pem", + "/etc/nginx/fullchain.pem") + self.config.save() + + self.config.parser.load() + + parsed_example_conf = util.filter_comments(self.config.parser.parsed[example_conf]) + parsed_server_conf = util.filter_comments(self.config.parser.parsed[server_conf]) + parsed_nginx_conf = util.filter_comments(self.config.parser.parsed[nginx_conf]) + + assert [[['server'], + [ + ['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', '.example.com'], + ['server_name', 'example.*'], + + ['listen', '5001', 'ssl'], + ['ssl_certificate', 'example/fullchain.pem'], + ['ssl_certificate_key', 'example/key.pem'], + ['include', self.config.mod_ssl_conf], + ['ssl_dhparam', self.config.ssl_dhparams], + ]]] == \ + parsed_example_conf + assert [['server_name', 'somename', 'alias', 'another.alias']] == \ + parsed_server_conf + assert util.contains_at_depth( + parsed_nginx_conf, + [['server'], + [ + ['listen', '8000'], + ['listen', 'somename:8080'], + ['include', 'server.conf'], + [['location', '/'], + [['root', 'html'], + ['index', 'index.html', 'index.htm']]], + ['listen', '5001', 'ssl'], + ['ssl_certificate', '/etc/nginx/fullchain.pem'], + ['ssl_certificate_key', '/etc/nginx/key.pem'], + ['include', self.config.mod_ssl_conf], + ['ssl_dhparam', self.config.ssl_dhparams], + ]], + 2) + + def test_deploy_cert_add_explicit_listen(self): + migration_conf = self.config.parser.abs_path('sites-enabled/migration.com') + self.config.deploy_cert( + "summer.com", + "summer/cert.pem", + "summer/key.pem", + "summer/chain.pem", + "summer/fullchain.pem") + self.config.save() + self.config.parser.load() + parsed_migration_conf = util.filter_comments(self.config.parser.parsed[migration_conf]) + assert [['server'], + [ + ['server_name', 'migration.com'], + ['server_name', 'summer.com'], + + ['listen', '80'], + ['listen', '5001', 'ssl'], + ['ssl_certificate', 'summer/fullchain.pem'], + ['ssl_certificate_key', 'summer/key.pem'], + ['include', self.config.mod_ssl_conf], + ['ssl_dhparam', self.config.ssl_dhparams], + ]] == \ + parsed_migration_conf[0] + + @mock.patch("certbot._internal.plugins.nginx.configurator.http_01.NginxHttp01.perform") + @mock.patch("certbot._internal.plugins.nginx.configurator.NginxConfigurator.restart") + @mock.patch("certbot._internal.plugins.nginx.configurator.NginxConfigurator.revert_challenge_config") + def test_perform_and_cleanup(self, mock_revert, mock_restart, mock_http_perform): + # Only tests functionality specific to configurator.perform + # Note: As more challenges are offered this will have to be expanded + achall = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=messages.ChallengeBody( + chall=challenges.HTTP01(token=b"m8TdO1qik4JVFtgPPurJmg"), + uri="https://ca.org/chall1_uri", + status=messages.Status("pending"), + ), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="example.com"), + account_key=self.rsa512jwk) + + expected = [ + achall.response(self.rsa512jwk), + ] + + mock_http_perform.return_value = expected[:] + responses = self.config.perform([achall]) + + assert mock_http_perform.call_count == 1 + assert responses == expected + + self.config.cleanup([achall]) + assert 0 == self.config._chall_out # pylint: disable=protected-access + assert mock_revert.call_count == 1 + assert mock_restart.call_count == 2 + + @mock.patch("certbot._internal.plugins.nginx.configurator.subprocess.run") + def test_get_version(self, mock_run): + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = "\n".join( + ["nginx version: nginx/1.4.2", + "built by clang 6.0 (clang-600.0.56)" + " (based on LLVM 3.5svn)", + "TLS SNI support enabled", + "configure arguments: --prefix=/usr/local/Cellar/" + "nginx/1.6.2 --with-http_ssl_module"]) + assert self.config.get_version() == (1, 4, 2) + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = "\n".join( + ["nginx version: nginx/0.9", + "built by clang 6.0 (clang-600.0.56)" + " (based on LLVM 3.5svn)", + "TLS SNI support enabled", + "configure arguments: --with-http_ssl_module"]) + assert self.config.get_version() == (0, 9) + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = "\n".join( + ["blah 0.0.1", + "built by clang 6.0 (clang-600.0.56)" + " (based on LLVM 3.5svn)", + "TLS SNI support enabled", + "configure arguments: --with-http_ssl_module"]) + with pytest.raises(errors.PluginError): + self.config.get_version() + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = "\n".join( + ["nginx version: nginx/1.4.2", + "TLS SNI support enabled"]) + with pytest.raises(errors.PluginError): + self.config.get_version() + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = "\n".join( + ["nginx version: nginx/1.4.2", + "built by clang 6.0 (clang-600.0.56)" + " (based on LLVM 3.5svn)", + "configure arguments: --with-http_ssl_module"]) + with pytest.raises(errors.PluginError): + self.config.get_version() + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = "\n".join( + ["nginx version: nginx/0.8.1", + "built by clang 6.0 (clang-600.0.56)" + " (based on LLVM 3.5svn)", + "TLS SNI support enabled", + "configure arguments: --with-http_ssl_module"]) + with pytest.raises(errors.NotSupportedError): + self.config.get_version() + + mock_run.side_effect = OSError("Can't find program") + with pytest.raises(errors.PluginError): + self.config.get_version() + + @mock.patch("certbot._internal.plugins.nginx.configurator.subprocess.run") + def test_get_openssl_version(self, mock_run): + # pylint: disable=protected-access + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = """ + nginx version: nginx/1.15.5 + built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) + built with OpenSSL 1.0.2g 1 Mar 2016 + TLS SNI support enabled + configure arguments: + """ + assert self.config._get_openssl_version() == "1.0.2g" + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = """ + nginx version: nginx/1.15.5 + built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) + built with OpenSSL 1.0.2-beta1 1 Mar 2016 + TLS SNI support enabled + configure arguments: + """ + assert self.config._get_openssl_version() == "1.0.2-beta1" + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = """ + nginx version: nginx/1.15.5 + built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) + built with OpenSSL 1.0.2 1 Mar 2016 + TLS SNI support enabled + configure arguments: + """ + assert self.config._get_openssl_version() == "1.0.2" + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = """ + nginx version: nginx/1.15.5 + built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) + built with OpenSSL 1.0.2g 1 Mar 2016 (running with OpenSSL 1.0.2a 1 Mar 2016) + TLS SNI support enabled + configure arguments: + """ + assert self.config._get_openssl_version() == "1.0.2a" + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = """ + nginx version: nginx/1.15.5 + built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) + built with LibreSSL 2.2.2 + TLS SNI support enabled + configure arguments: + """ + assert self.config._get_openssl_version() == "" + + mock_run.return_value.stdout = "" + mock_run.return_value.stderr = """ + nginx version: nginx/1.15.5 + built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) + TLS SNI support enabled + configure arguments: + """ + assert self.config._get_openssl_version() == "" + + @mock.patch("certbot._internal.plugins.nginx.configurator.subprocess.run") + @mock.patch("certbot._internal.plugins.nginx.configurator.time") + def test_nginx_restart(self, mock_time, mock_run): + mocked = mock_run.return_value + mocked.stdout = '' + mocked.stderr = '' + mocked.returncode = 0 + self.config.restart() + assert mock_run.call_count == 1 + mock_time.sleep.assert_called_once_with(0.1234) + + @mock.patch("certbot._internal.plugins.nginx.configurator.subprocess.run") + @mock.patch("certbot._internal.plugins.nginx.configurator.logger.debug") + def test_nginx_restart_fail(self, mock_log_debug, mock_run): + mocked = mock_run.return_value + mocked.stdout = '' + mocked.stderr = '' + mocked.returncode = 1 + with pytest.raises(errors.MisconfigurationError): + self.config.restart() + assert mock_run.call_count == 2 + mock_log_debug.assert_called_once_with("nginx reload failed:\n%s", "") + + @mock.patch("certbot._internal.plugins.nginx.configurator.subprocess.run") + def test_no_nginx_start(self, mock_run): + mock_run.side_effect = OSError("Can't find program") + with pytest.raises(errors.MisconfigurationError): + self.config.restart() + + @mock.patch("certbot.util.run_script") + def test_config_test_bad_process(self, mock_run_script): + mock_run_script.side_effect = errors.SubprocessError + with pytest.raises(errors.MisconfigurationError): + self.config.config_test() + + @mock.patch("certbot.util.run_script") + def test_config_test(self, _): + self.config.config_test() + + @mock.patch("certbot.reverter.Reverter.recovery_routine") + def test_recovery_routine_throws_error_from_reverter(self, mock_recovery_routine): + mock_recovery_routine.side_effect = errors.ReverterError("foo") + with pytest.raises(errors.PluginError): + self.config.recovery_routine() + + @mock.patch("certbot.reverter.Reverter.rollback_checkpoints") + def test_rollback_checkpoints_throws_error_from_reverter(self, mock_rollback_checkpoints): + mock_rollback_checkpoints.side_effect = errors.ReverterError("foo") + with pytest.raises(errors.PluginError): + self.config.rollback_checkpoints() + + @mock.patch("certbot.reverter.Reverter.revert_temporary_config") + def test_revert_challenge_config_throws_error_from_reverter(self, mock_revert_temporary_config): + mock_revert_temporary_config.side_effect = errors.ReverterError("foo") + with pytest.raises(errors.PluginError): + self.config.revert_challenge_config() + + @mock.patch("certbot.reverter.Reverter.add_to_checkpoint") + def test_save_throws_error_from_reverter(self, mock_add_to_checkpoint): + mock_add_to_checkpoint.side_effect = errors.ReverterError("foo") + with pytest.raises(errors.PluginError): + self.config.save() + + def test_redirect_enhance(self): + # Test that we successfully add a redirect when there is + # a listen directive + expected = UnspacedList(_redirect_block_for_domain("www.example.com"))[0] + + example_conf = self.config.parser.abs_path('sites-enabled/example.com') + self.config.enhance("www.example.com", "redirect") + + generated_conf = self.config.parser.parsed[example_conf] + assert util.contains_at_depth(generated_conf, expected, 2) is True + + # Test that we successfully add a redirect when there is + # no listen directive + migration_conf = self.config.parser.abs_path('sites-enabled/migration.com') + self.config.enhance("migration.com", "redirect") + + expected = UnspacedList(_redirect_block_for_domain("migration.com"))[0] + + generated_conf = self.config.parser.parsed[migration_conf] + assert util.contains_at_depth(generated_conf, expected, 2) is True + + def test_split_for_redirect(self): + example_conf = self.config.parser.abs_path('sites-enabled/example.com') + self.config.deploy_cert( + "example.org", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + "example/fullchain.pem") + self.config.enhance("www.example.com", "redirect") + generated_conf = self.config.parser.parsed[example_conf] + assert [[['server'], [ + ['server_name', '.example.com'], + ['server_name', 'example.*'], + ['listen', '5001', 'ssl'], ['#', ' managed by Certbot'], + ['ssl_certificate', 'example/fullchain.pem'], ['#', ' managed by Certbot'], + ['ssl_certificate_key', 'example/key.pem'], ['#', ' managed by Certbot'], + ['include', self.config.mod_ssl_conf], ['#', ' managed by Certbot'], + ['ssl_dhparam', self.config.ssl_dhparams], ['#', ' managed by Certbot'], + [], []]], + [['server'], [ + [['if', '($host', '=', 'www.example.com)'], [ + ['return', '301', 'https://$host$request_uri']]], + ['#', ' managed by Certbot'], [], + ['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', '.example.com'], + ['server_name', 'example.*'], + ['return', '404'], ['#', ' managed by Certbot'], [], [], []]]] == \ + generated_conf + + def test_split_for_headers(self): + example_conf = self.config.parser.abs_path('sites-enabled/example.com') + self.config.deploy_cert( + "example.org", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + "example/fullchain.pem") + self.config.enhance("www.example.com", "ensure-http-header", "Strict-Transport-Security") + generated_conf = self.config.parser.parsed[example_conf] + assert [[['server'], [ + ['server_name', '.example.com'], + ['server_name', 'example.*'], + ['listen', '5001', 'ssl'], ['#', ' managed by Certbot'], + ['ssl_certificate', 'example/fullchain.pem'], ['#', ' managed by Certbot'], + ['ssl_certificate_key', 'example/key.pem'], ['#', ' managed by Certbot'], + ['include', self.config.mod_ssl_conf], ['#', ' managed by Certbot'], + ['ssl_dhparam', self.config.ssl_dhparams], ['#', ' managed by Certbot'], + [], [], + ['add_header', 'Strict-Transport-Security', '"max-age=31536000"', 'always'], + ['#', ' managed by Certbot'], + [], []]], + [['server'], [ + ['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', '.example.com'], + ['server_name', 'example.*'], + [], []]]] == \ + generated_conf + + def test_http_header_hsts(self): + # we need to select an SSL enabled vhost here for the HSTS enhancement + example_conf = self.config.parser.abs_path('sites-enabled/migration.com') + self.config.enhance("migration.com", "ensure-http-header", + "Strict-Transport-Security") + expected = ['add_header', 'Strict-Transport-Security', '"max-age=31536000"', 'always'] + generated_conf = self.config.parser.parsed[example_conf] + assert util.contains_at_depth(generated_conf, expected, 2) is True + + def test_multiple_headers_hsts(self): + # we need to select an SSL enabled vhost here for the HSTS enhancement + headers_conf = self.config.parser.abs_path('sites-enabled/headers.com') + self.config.enhance("headers.com", "ensure-http-header", + "Strict-Transport-Security") + expected = ['add_header', 'Strict-Transport-Security', '"max-age=31536000"', 'always'] + generated_conf = self.config.parser.parsed[headers_conf] + assert util.contains_at_depth(generated_conf, expected, 2) is True + + def test_http_header_hsts_twice(self): + # we need to select an SSL enabled vhost here for the HSTS enhancement + self.config.enhance("migration.com", "ensure-http-header", + "Strict-Transport-Security") + with pytest.raises(errors.PluginEnhancementAlreadyPresent): + self.config.enhance("migration.com", + "ensure-http-header", "Strict-Transport-Security") + + @mock.patch('certbot._internal.plugins.nginx.obj.VirtualHost.contains_list') + def test_certbot_redirect_exists(self, mock_contains_list): + # Test that we add no redirect statement if there is already a + # redirect in the block that is managed by certbot + # Has a certbot redirect + mock_contains_list.return_value = True + with mock.patch("certbot._internal.plugins.nginx.configurator.logger") as mock_logger: + self.config.enhance("www.example.com", "redirect") + assert mock_logger.info.call_args[0][0] == \ + "Traffic on port %s already redirecting to ssl in %s" + + def test_redirect_dont_enhance(self): + # Test that we don't accidentally add redirect to ssl-only block + with mock.patch("certbot._internal.plugins.nginx.configurator.logger") as mock_logger: + self.config.enhance("geese.com", "redirect") + assert mock_logger.info.call_args[0][0] == \ + 'No matching insecure server blocks listening on port %s found.' + + def test_double_redirect(self): + # Test that we add one redirect for each domain + example_conf = self.config.parser.abs_path('sites-enabled/example.com') + self.config.enhance("example.com", "redirect") + self.config.enhance("example.org", "redirect") + + expected1 = UnspacedList(_redirect_block_for_domain("example.com"))[0] + expected2 = UnspacedList(_redirect_block_for_domain("example.org"))[0] + + generated_conf = self.config.parser.parsed[example_conf] + assert util.contains_at_depth(generated_conf, expected1, 2) + assert util.contains_at_depth(generated_conf, expected2, 2) + + def test_staple_ocsp_bad_version(self): + self.config.version = (1, 3, 1) + with pytest.raises(errors.PluginError): + self.config.enhance("www.example.com", "staple-ocsp", "chain_path") + + def test_staple_ocsp_no_chain_path(self): + with pytest.raises(errors.PluginError): + self.config.enhance("www.example.com", "staple-ocsp", None) + + def test_staple_ocsp_internal_error(self): + # we need to select an SSL enabled vhost here for the OCSP stapling + # enhancement + self.config.enhance("sslon.com", "staple-ocsp", "chain_path") + # error is raised because the server block has conflicting directives + with pytest.raises(errors.PluginError): + self.config.enhance("sslon.com", "staple-ocsp", "different_path") + + def test_staple_ocsp(self): + chain_path = "example/chain.pem" + # we need to select an SSL enabled vhost here for the OCSP stapling + # enhancement + self.config.enhance("sslon.com", "staple-ocsp", chain_path) + + example_conf = self.config.parser.abs_path('sites-enabled/sslon.com') + generated_conf = self.config.parser.parsed[example_conf] + + assert util.contains_at_depth( + generated_conf, + ['ssl_trusted_certificate', 'example/chain.pem'], 2) + assert util.contains_at_depth( + generated_conf, ['ssl_stapling', 'on'], 2) + assert util.contains_at_depth( + generated_conf, ['ssl_stapling_verify', 'on'], 2) + + def test_deploy_no_match_default_set(self): + default_conf = self.config.parser.abs_path('sites-enabled/default') + foo_conf = self.config.parser.abs_path('foo.conf') + del self.config.parser.parsed[foo_conf][2][1][0][1][0] # remove default_server + self.config.version = (1, 3, 1) + + self.config.deploy_cert( + "www.nomatch.com", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + "example/fullchain.pem") + self.config.save() + + self.config.parser.load() + + parsed_default_conf = util.filter_comments(self.config.parser.parsed[default_conf]) + + assert [[['server'], + [['listen', 'myhost', 'default_server'], + ['listen', 'otherhost', 'default_server'], + ['server_name', '"www.example.org"'], + [['location', '/'], + [['root', 'html'], + ['index', 'index.html', 'index.htm']]]]], + [['server'], + [['listen', 'myhost'], + ['listen', 'otherhost'], + ['server_name', 'www.nomatch.com'], + [['location', '/'], + [['root', 'html'], + ['index', 'index.html', 'index.htm']]], + ['listen', '5001', 'ssl'], + ['ssl_certificate', 'example/fullchain.pem'], + ['ssl_certificate_key', 'example/key.pem'], + ['include', self.config.mod_ssl_conf], + ['ssl_dhparam', self.config.ssl_dhparams]]]] == \ + parsed_default_conf + + self.config.deploy_cert( + "nomatch.com", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + "example/fullchain.pem") + self.config.save() + + self.config.parser.load() + + parsed_default_conf = util.filter_comments(self.config.parser.parsed[default_conf]) + + assert util.contains_at_depth(parsed_default_conf, "nomatch.com", 3) + + def test_deploy_no_match_default_set_multi_level_path(self): + default_conf = self.config.parser.abs_path('sites-enabled/default') + foo_conf = self.config.parser.abs_path('foo.conf') + del self.config.parser.parsed[default_conf][0][1][0] + del self.config.parser.parsed[default_conf][0][1][0] + self.config.version = (1, 3, 1) + + self.config.deploy_cert( + "www.nomatch.com", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + "example/fullchain.pem") + self.config.save() + + self.config.parser.load() + + parsed_foo_conf = util.filter_comments(self.config.parser.parsed[foo_conf]) + + assert [['server'], + [['listen', '*:80', 'ssl'], + ['server_name', 'www.nomatch.com'], + ['root', '/home/ubuntu/sites/foo/'], + [['location', '/status'], [[['types'], [['image/jpeg', 'jpg']]]]], + [['location', '~', 'case_sensitive\\.php$'], [['index', 'index.php'], + ['root', '/var/root']]], + [['location', '~*', 'case_insensitive\\.php$'], []], + [['location', '=', 'exact_match\\.php$'], []], + [['location', '^~', 'ignore_regex\\.php$'], []], + ['ssl_certificate', 'example/fullchain.pem'], + ['ssl_certificate_key', 'example/key.pem']]] == \ + parsed_foo_conf[1][1][1] + + def test_deploy_no_match_no_default_set(self): + default_conf = self.config.parser.abs_path('sites-enabled/default') + foo_conf = self.config.parser.abs_path('foo.conf') + del self.config.parser.parsed[default_conf][0][1][0] + del self.config.parser.parsed[default_conf][0][1][0] + del self.config.parser.parsed[foo_conf][2][1][0][1][0] + self.config.version = (1, 3, 1) + + with pytest.raises(errors.MisconfigurationError): + self.config.deploy_cert("www.nomatch.com", "example/cert.pem", "example/key.pem", + "example/chain.pem", "example/fullchain.pem") + + def test_deploy_no_match_fail_multiple_defaults(self): + self.config.version = (1, 3, 1) + with pytest.raises(errors.MisconfigurationError): + self.config.deploy_cert("www.nomatch.com", "example/cert.pem", "example/key.pem", + "example/chain.pem", "example/fullchain.pem") + + def test_deploy_no_match_multiple_defaults_ok(self): + foo_conf = self.config.parser.abs_path('foo.conf') + self.config.parser.parsed[foo_conf][2][1][0][1][0][1] = '*:5001' + self.config.version = (1, 3, 1) + self.config.deploy_cert("www.nomatch.com", "example/cert.pem", "example/key.pem", + "example/chain.pem", "example/fullchain.pem") + + def test_deploy_no_match_add_redirect(self): + default_conf = self.config.parser.abs_path('sites-enabled/default') + foo_conf = self.config.parser.abs_path('foo.conf') + del self.config.parser.parsed[foo_conf][2][1][0][1][0] # remove default_server + self.config.version = (1, 3, 1) + + self.config.deploy_cert( + "www.nomatch.com", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + "example/fullchain.pem") + + self.config.deploy_cert( + "nomatch.com", + "example/cert.pem", + "example/key.pem", + "example/chain.pem", + "example/fullchain.pem") + + self.config.enhance("www.nomatch.com", "redirect") + + self.config.save() + + self.config.parser.load() + + expected = UnspacedList(_redirect_block_for_domain("www.nomatch.com"))[0] + + generated_conf = self.config.parser.parsed[default_conf] + assert util.contains_at_depth(generated_conf, expected, 2) + + @mock.patch('certbot.reverter.logger') + @mock.patch('certbot._internal.plugins.nginx.parser.NginxParser.load') + def test_parser_reload_after_config_changes(self, mock_parser_load, unused_mock_logger): + self.config.recovery_routine() + self.config.revert_challenge_config() + self.config.rollback_checkpoints() + assert mock_parser_load.call_count == 3 + + def test_choose_vhosts_wildcard(self): + # pylint: disable=protected-access + mock_path = "certbot._internal.plugins.nginx.display_ops.select_vhost_multiple" + with mock.patch(mock_path) as mock_select_vhs: + vhost = [x for x in self.config.parser.get_vhosts() + if 'summer.com' in x.names][0] + mock_select_vhs.return_value = [vhost] + vhs = self.config._choose_vhosts_wildcard("*.com", + prefer_ssl=True) + # Check that the dialog was called with migration.com + assert vhost in mock_select_vhs.call_args[0][0] + + # And the actual returned values + assert len(vhs) == 1 + assert vhs[0] == vhost + + def test_choose_vhosts_wildcard_redirect(self): + # pylint: disable=protected-access + mock_path = "certbot._internal.plugins.nginx.display_ops.select_vhost_multiple" + with mock.patch(mock_path) as mock_select_vhs: + vhost = [x for x in self.config.parser.get_vhosts() + if 'summer.com' in x.names][0] + mock_select_vhs.return_value = [vhost] + vhs = self.config._choose_vhosts_wildcard("*.com", + prefer_ssl=False) + # Check that the dialog was called with migration.com + assert vhost in mock_select_vhs.call_args[0][0] + + # And the actual returned values + assert len(vhs) == 1 + assert vhs[0] == vhost + + def test_deploy_cert_wildcard(self): + # pylint: disable=protected-access + mock_choose_vhosts = mock.MagicMock() + vhost = [x for x in self.config.parser.get_vhosts() + if 'geese.com' in x.names][0] + mock_choose_vhosts.return_value = [vhost] + self.config._choose_vhosts_wildcard = mock_choose_vhosts + mock_d = "certbot._internal.plugins.nginx.configurator.NginxConfigurator._deploy_cert" + with mock.patch(mock_d) as mock_dep: + self.config.deploy_cert("*.com", "/tmp/path", + "/tmp/path", "/tmp/path", "/tmp/path") + assert mock_dep.called + assert len(mock_dep.call_args_list) == 1 + assert vhost == mock_dep.call_args_list[0][0][0] + + @mock.patch("certbot._internal.plugins.nginx.display_ops.select_vhost_multiple") + def test_deploy_cert_wildcard_no_vhosts(self, mock_dialog): + # pylint: disable=protected-access + mock_dialog.return_value = [] + with pytest.raises(errors.PluginError): + self.config.deploy_cert("*.wild.cat", "/tmp/path", "/tmp/path", + "/tmp/path", "/tmp/path") + + @mock.patch("certbot._internal.plugins.nginx.display_ops.select_vhost_multiple") + def test_enhance_wildcard_ocsp_after_install(self, mock_dialog): + # pylint: disable=protected-access + vhost = [x for x in self.config.parser.get_vhosts() + if 'geese.com' in x.names][0] + self.config._wildcard_vhosts["*.com"] = [vhost] + self.config.enhance("*.com", "staple-ocsp", "example/chain.pem") + assert not mock_dialog.called + + @mock.patch("certbot._internal.plugins.nginx.display_ops.select_vhost_multiple") + def test_enhance_wildcard_redirect_or_ocsp_no_install(self, mock_dialog): + # we need to select an SSL enabled vhost here for the OCSP stapling + # enhancement + vhost = [x for x in self.config.parser.get_vhosts() + if 'geese.com' in x.names][0] + mock_dialog.return_value = [vhost] + self.config.enhance("*.com", "staple-ocsp", "example/chain.pem") + assert mock_dialog.called is True + + @mock.patch("certbot._internal.plugins.nginx.display_ops.select_vhost_multiple") + def test_enhance_wildcard_double_redirect(self, mock_dialog): + # pylint: disable=protected-access + vhost = [x for x in self.config.parser.get_vhosts() + if 'summer.com' in x.names][0] + self.config._wildcard_redirect_vhosts["*.com"] = [vhost] + self.config.enhance("*.com", "redirect") + assert not mock_dialog.called + + def test_choose_vhosts_wildcard_no_ssl_filter_port(self): + # pylint: disable=protected-access + mock_path = "certbot._internal.plugins.nginx.display_ops.select_vhost_multiple" + with mock.patch(mock_path) as mock_select_vhs: + mock_select_vhs.return_value = [] + self.config._choose_vhosts_wildcard("*.com", + prefer_ssl=False, + no_ssl_filter_port='80') + # Check that the dialog was called with only port 80 vhosts + assert len(mock_select_vhs.call_args[0][0]) == 9 + + def test_choose_auth_vhosts(self): + """choose_auth_vhosts correctly selects duplicative and HTTP/HTTPS vhosts""" + http, https = self.config.choose_auth_vhosts('ssl.both.com') + assert len(http) == 4 + assert len(https) == 2 + assert http[0].names == {'ssl.both.com'} + assert http[1].names == {'ssl.both.com'} + assert http[2].names == {'ssl.both.com'} + assert http[3].names == {'*.both.com'} + assert https[0].names == {'ssl.both.com'} + assert https[1].names == {'*.both.com'} + + +class InstallSslOptionsConfTest(util.NginxTest): + """Test that the options-ssl-nginx.conf file is installed and updated properly.""" + + def setUp(self): + super().setUp() + + self.config = self.get_nginx_configurator( + self.config_path, self.config_dir, self.work_dir, self.logs_dir) + + def _call(self): + self.config.install_ssl_options_conf(self.config.mod_ssl_conf, + self.config.updated_mod_ssl_conf_digest) + + def _current_ssl_options_hash(self): + return crypto_util.sha256sum(self.config.mod_ssl_conf_src) + + def _assert_current_file(self): + assert os.path.isfile(self.config.mod_ssl_conf) + assert crypto_util.sha256sum(self.config.mod_ssl_conf) == \ + self._current_ssl_options_hash() + + def test_no_file(self): + # prepare should have placed a file there + self._assert_current_file() + os.remove(self.config.mod_ssl_conf) + assert not os.path.isfile(self.config.mod_ssl_conf) + self._call() + self._assert_current_file() + + def test_current_file(self): + self._assert_current_file() + self._call() + self._assert_current_file() + + def _mock_hash_except_ssl_conf_src(self, fake_hash): + # Write a bad file in place so that update tests fail if no update occurs. + # We're going to pretend this file (the currently installed conf file) + # actually hashes to `fake_hash` for the update tests. + with open(self.config.mod_ssl_conf, "w") as f: + f.write("bogus") + sha256 = crypto_util.sha256sum + def _hash(filename): + return sha256(filename) if filename == self.config.mod_ssl_conf_src else fake_hash + return _hash + + def test_prev_file_updates_to_current(self): + from certbot._internal.plugins.nginx.constants import ALL_SSL_OPTIONS_HASHES + with mock.patch('certbot.crypto_util.sha256sum', + new=self._mock_hash_except_ssl_conf_src(ALL_SSL_OPTIONS_HASHES[0])): + self._call() + self._assert_current_file() + + def test_prev_file_updates_to_current_old_nginx(self): + from certbot._internal.plugins.nginx.constants import ALL_SSL_OPTIONS_HASHES + self.config.version = (1, 5, 8) + with mock.patch('certbot.crypto_util.sha256sum', + new=self._mock_hash_except_ssl_conf_src(ALL_SSL_OPTIONS_HASHES[0])): + self._call() + self._assert_current_file() + + def test_manually_modified_current_file_does_not_update(self): + with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf: + mod_ssl_conf.write("a new line for the wrong hash\n") + with mock.patch("certbot.plugins.common.logger") as mock_logger: + self._call() + assert not mock_logger.warning.called + assert os.path.isfile(self.config.mod_ssl_conf) + assert crypto_util.sha256sum(self.config.mod_ssl_conf_src) == \ + self._current_ssl_options_hash() + assert crypto_util.sha256sum(self.config.mod_ssl_conf) != \ + self._current_ssl_options_hash() + + def test_manually_modified_past_file_warns(self): + with open(self.config.mod_ssl_conf, "a") as mod_ssl_conf: + mod_ssl_conf.write("a new line for the wrong hash\n") + with open(self.config.updated_mod_ssl_conf_digest, "w") as f: + f.write("hashofanoldversion") + with mock.patch("certbot.plugins.common.logger") as mock_logger: + self._call() + assert mock_logger.warning.call_args[0][0] == \ + "%s has been manually modified; updated file " \ + "saved to %s. We recommend updating %s for security purposes." + assert crypto_util.sha256sum(self.config.mod_ssl_conf_src) == \ + self._current_ssl_options_hash() + # only print warning once + with mock.patch("certbot.plugins.common.logger") as mock_logger: + self._call() + assert not mock_logger.warning.called + + def test_current_file_hash_in_all_hashes(self): + from certbot._internal.plugins.nginx.constants import ALL_SSL_OPTIONS_HASHES + assert self._current_ssl_options_hash() in ALL_SSL_OPTIONS_HASHES, \ + "Constants.ALL_SSL_OPTIONS_HASHES must be appended" \ + " with the sha256 hash of self.config.mod_ssl_conf when it is updated." + + def test_ssl_config_files_hash_in_all_hashes(self): + """ + It is really critical that all TLS Nginx config files have their SHA256 hash registered in + constants.ALL_SSL_OPTIONS_HASHES. Otherwise Certbot will mistakenly assume that the config + file has been manually edited by the user, and will refuse to update it. + This test ensures that all necessary hashes are present. + """ + import importlib.resources + + from certbot._internal.plugins.nginx.constants import ALL_SSL_OPTIONS_HASHES + + tls_configs_ref = importlib.resources.files("certbot").joinpath( + "_internal", "plugins", "nginx", "tls_configs") + with importlib.resources.as_file(tls_configs_ref) as tls_configs_dir: + for tls_config_file in os.listdir(tls_configs_dir): + file_hash = crypto_util.sha256sum(os.path.join(tls_configs_dir, tls_config_file)) + assert file_hash in ALL_SSL_OPTIONS_HASHES, \ + f"Constants.ALL_SSL_OPTIONS_HASHES must be appended with the sha256 " \ + f"hash of {tls_config_file} when it is updated." + + def test_nginx_version_uses_correct_config(self): + self.config.version = (1, 5, 8) + self.config.openssl_version = "1.0.2g" # shouldn't matter + assert os.path.basename(self.config.mod_ssl_conf_src) == \ + "options-ssl-nginx-old.conf" + self._call() + self._assert_current_file() + self.config.version = (1, 5, 9) + self.config.openssl_version = "1.0.2l" + assert os.path.basename(self.config.mod_ssl_conf_src) == \ + "options-ssl-nginx-tls12-only.conf" + self._call() + self._assert_current_file() + self.config.version = (1, 13, 0) + assert os.path.basename(self.config.mod_ssl_conf_src) == \ + "options-ssl-nginx.conf" + self._call() + self._assert_current_file() + self.config.version = (1, 13, 0) + self.config.openssl_version = "1.0.2k" + assert os.path.basename(self.config.mod_ssl_conf_src) == \ + "options-ssl-nginx-tls13-session-tix-on.conf" + + +class DetermineDefaultServerRootTest(certbot_test_util.ConfigTestCase): + """Tests for certbot._internal.plugins.nginx.configurator._determine_default_server_root.""" + + def _call(self): + from certbot._internal.plugins.nginx.configurator import _determine_default_server_root + return _determine_default_server_root() + + @mock.patch.dict(os.environ, {"CERTBOT_DOCS": "1"}) + def test_docs_value(self): + self._test(expect_both_values=True) + + @mock.patch.dict(os.environ, {}) + def test_real_values(self): + self._test(expect_both_values=False) + + def _test(self, expect_both_values): + server_root = self._call() + + if expect_both_values: + assert "/usr/local/etc/nginx" in server_root + assert "/etc/nginx" in server_root + else: + assert server_root in ("/etc/nginx", "/usr/local/etc/nginx") + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/display_ops_test.py b/certbot/src/certbot/_internal/tests/plugins/nginx/display_ops_test.py new file mode 100644 index 00000000000..96f21495dd7 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/display_ops_test.py @@ -0,0 +1,45 @@ +"""Test certbot._internal.plugins.nginx.display_ops.""" +import sys + +import pytest + +from certbot.display import util as display_util +from certbot.tests import util as certbot_util +from certbot._internal.plugins.nginx import parser +from certbot._internal.plugins.nginx.display_ops import select_vhost_multiple +from certbot._internal.tests.plugins.nginx import test_util as util + + +class SelectVhostMultiTest(util.NginxTest): + """Tests for certbot._internal.plugins.nginx.display_ops.select_vhost_multiple.""" + + def setUp(self): + super().setUp() + nparser = parser.NginxParser(self.config_path) + self.vhosts = nparser.get_vhosts() + + def test_select_no_input(self): + assert not select_vhost_multiple([]) + + @certbot_util.patch_display_util() + def test_select_correct(self, mock_util): + mock_util().checklist.return_value = ( + display_util.OK, [self.vhosts[3].display_repr(), + self.vhosts[2].display_repr()]) + vhs = select_vhost_multiple([self.vhosts[3], + self.vhosts[2], + self.vhosts[1]]) + assert self.vhosts[2] in vhs + assert self.vhosts[3] in vhs + assert self.vhosts[1] not in vhs + + @certbot_util.patch_display_util() + def test_select_cancel(self, mock_util): + mock_util().checklist.return_value = (display_util.CANCEL, "whatever") + vhs = select_vhost_multiple([self.vhosts[2], self.vhosts[3]]) + assert len(vhs) == 0 + assert vhs == [] + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/http_01_test.py b/certbot/src/certbot/_internal/tests/plugins/nginx/http_01_test.py new file mode 100644 index 00000000000..6c62033f165 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/http_01_test.py @@ -0,0 +1,238 @@ +"""Tests for certbot._internal.plugins.nginx.http_01""" +import sys +from unittest import mock + +import josepy as jose +import pytest + +from acme import challenges, messages +from certbot import achallenges +from certbot.tests import acme_util +from certbot.tests import util as test_util +from certbot._internal.plugins.nginx.obj import Addr +from certbot._internal.tests.plugins.nginx import test_util as util + +AUTH_KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) + + +class HttpPerformTest(util.NginxTest): + """Test the NginxHttp01 challenge.""" + + account_key = AUTH_KEY + achalls = [ + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=b"kNdwjwOeX0I_A8DXt9Msmg"), messages.STATUS_PENDING), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="www.example.com"), + account_key=account_key), + achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01( + token=b"\xba\xa9\xda? 1 + assert len(nparser.parsed[nparser.config_root]) == 4 + assert os.path.join(self.config_path, "nginx.conf") in nparser.parsed + + def test_abs_path(self): + nparser = parser.NginxParser(self.config_path) + if os.name != 'nt': + assert '/etc/nginx/*' == nparser.abs_path('/etc/nginx/*') + assert os.path.join(self.config_path, 'foo/bar') == \ + nparser.abs_path('foo/bar') + else: + assert 'C:\\etc\\nginx\\*' == nparser.abs_path('C:\\etc\\nginx\\*') + assert os.path.join(self.config_path, 'foo\\bar') == \ + nparser.abs_path('foo\\bar') + + + def test_filedump(self): + nparser = parser.NginxParser(self.config_path) + nparser.filedump('test', lazy=False) + # pylint: disable=protected-access + parsed = nparser._parse_files(nparser.abs_path( + 'sites-enabled/example.com.test')) + assert 4 == len(glob.glob(nparser.abs_path('*.test'))) + assert 12 == len( + glob.glob(nparser.abs_path('sites-enabled/*.test'))) + assert [[['server'], [['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', '.example.com'], + ['server_name', 'example.*']]]] == \ + parsed[nparser.abs_path('sites-enabled/example.com.test')] + + def test__do_for_subarray(self): + # pylint: disable=protected-access + mylists = [([[2], [3], [2]], [[0], [2]]), + ([[2], [3], [4]], [[0]]), + ([[4], [3], [2]], [[2]]), + ([], []), + (2, []), + ([[[2], [3], [2]], [[2], [3], [2]]], + [[0, 0], [0, 2], [1, 0], [1, 2]]), + ([[[0], [3], [2]], [[2], [3], [2]]], [[0, 2], [1, 0], [1, 2]]), + ([[[0], [3], [4]], [[2], [3], [2]]], [[1, 0], [1, 2]]), + ([[[0], [3], [4]], [[5], [3], [2]]], [[1, 2]]), + ([[[0], [3], [4]], [[5], [3], [0]]], [])] + + for mylist, result in mylists: + paths: list[list[int]] = [] + parser._do_for_subarray(mylist, + lambda x: isinstance(x, list) and + len(x) >= 1 and + x[0] == 2, + lambda x, y, pts=paths: pts.append(y)) + assert paths == result + + def test_get_vhosts_global_ssl(self): + nparser = parser.NginxParser(self.config_path) + vhosts = nparser.get_vhosts() + + vhost = obj.VirtualHost(nparser.abs_path('sites-enabled/globalssl.com'), + [obj.Addr('4.8.2.6', '57', True, False, + False, False)], + True, True, {'globalssl.com'}, [], [0]) + + globalssl_com = [x for x in vhosts if 'globalssl.com' in x.filep][0] + assert vhost == globalssl_com + + def test_get_vhosts(self): + nparser = parser.NginxParser(self.config_path) + vhosts = nparser.get_vhosts() + + vhost1 = obj.VirtualHost(nparser.abs_path('nginx.conf'), + [obj.Addr('', '8080', False, False, + False, False)], + False, True, + {'localhost', + r'~^(www\.)?(example|bar)\.'}, + [], [10, 1, 9]) + vhost2 = obj.VirtualHost(nparser.abs_path('nginx.conf'), + [obj.Addr('somename', '8080', False, False, + False, False), + obj.Addr('', '8000', False, False, + False, False)], + False, True, + {'somename', 'another.alias', 'alias'}, + [], [10, 1, 12]) + vhost3 = obj.VirtualHost(nparser.abs_path('sites-enabled/example.com'), + [obj.Addr('69.50.225.155', '9000', + False, False, False, False), + obj.Addr('127.0.0.1', '', False, False, + False, False)], + False, True, + {'.example.com', 'example.*'}, [], [0]) + vhost4 = obj.VirtualHost(nparser.abs_path('sites-enabled/default'), + [obj.Addr('myhost', '', False, True, + False, False), + obj.Addr('otherhost', '', False, True, + False, False)], + False, True, {'www.example.org'}, + [], [0]) + vhost5 = obj.VirtualHost(nparser.abs_path('foo.conf'), + [obj.Addr('*', '80', True, True, + False, False)], + True, True, {'*.www.foo.com', + '*.www.example.com'}, + [], [2, 1, 0]) + + assert 21 == len(vhosts) + example_com = [x for x in vhosts if 'example.com' in x.filep][0] + assert vhost3 == example_com + default = [x for x in vhosts if 'default' in x.filep][0] + assert vhost4 == default + fooconf = [x for x in vhosts if 'foo.conf' in x.filep][0] + assert vhost5 == fooconf + localhost = [x for x in vhosts if 'localhost' in x.names][0] + assert vhost1 == localhost + somename = [x for x in vhosts if 'somename' in x.names][0] + assert vhost2 == somename + + def test_has_ssl_on_directive(self): + nparser = parser.NginxParser(self.config_path) + mock_vhost = obj.VirtualHost(None, None, None, None, None, + [['listen', 'myhost default_server'], + ['server_name', 'www.example.org'], + [['location', '/'], [['root', 'html'], ['index', 'index.html index.htm']]] + ], None) + assert not nparser.has_ssl_on_directive(mock_vhost) + mock_vhost.raw = [['listen', '*:80', 'default_server', 'ssl'], + ['server_name', '*.www.foo.com', '*.www.example.com'], + ['root', '/home/ubuntu/sites/foo/']] + assert not nparser.has_ssl_on_directive(mock_vhost) + mock_vhost.raw = [['listen', '80 ssl'], + ['server_name', '*.www.foo.com', '*.www.example.com']] + assert not nparser.has_ssl_on_directive(mock_vhost) + mock_vhost.raw = [['listen', '80'], + ['ssl', 'on'], + ['server_name', '*.www.foo.com', '*.www.example.com']] + assert nparser.has_ssl_on_directive(mock_vhost) is True + + def test_remove_server_directives(self): + nparser = parser.NginxParser(self.config_path) + mock_vhost = obj.VirtualHost(nparser.abs_path('nginx.conf'), + None, None, None, + {'localhost', + r'~^(www\.)?(example|bar)\.'}, + None, [10, 1, 9]) + example_com = nparser.abs_path('sites-enabled/example.com') + names = {'.example.com', 'example.*'} + mock_vhost.filep = example_com + mock_vhost.names = names + mock_vhost.path = [0] + nparser.add_server_directives(mock_vhost, + [['foo', 'bar'], ['ssl_certificate', + '/etc/ssl/cert2.pem']]) + nparser.remove_server_directives(mock_vhost, 'foo') + nparser.remove_server_directives(mock_vhost, 'ssl_certificate') + assert nparser.parsed[example_com] == \ + [[['server'], [['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', '.example.com'], + ['server_name', 'example.*'], + []]]] + + def test_add_server_directives(self): + nparser = parser.NginxParser(self.config_path) + mock_vhost = obj.VirtualHost(nparser.abs_path('nginx.conf'), + None, None, None, + {'localhost', + r'~^(www\.)?(example|bar)\.'}, + None, [10, 1, 9]) + nparser.add_server_directives(mock_vhost, + [['foo', 'bar'], ['\n ', 'ssl_certificate', ' ', + '/etc/ssl/cert.pem']]) + ssl_re = re.compile(r'\n\s+ssl_certificate /etc/ssl/cert.pem') + dump = nginxparser.dumps(nparser.parsed[nparser.abs_path('nginx.conf')]) + assert 1 == len(re.findall(ssl_re, dump)) + + example_com = nparser.abs_path('sites-enabled/example.com') + names = {'.example.com', 'example.*'} + mock_vhost.filep = example_com + mock_vhost.names = names + mock_vhost.path = [0] + nparser.add_server_directives(mock_vhost, + [['foo', 'bar'], ['ssl_certificate', + '/etc/ssl/cert2.pem']]) + nparser.add_server_directives(mock_vhost, [['foo', 'bar']]) + from certbot._internal.plugins.nginx.parser import COMMENT + assert nparser.parsed[example_com] == \ + [[['server'], [['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', '.example.com'], + ['server_name', 'example.*'], + ['foo', 'bar'], + ['#', COMMENT], + ['ssl_certificate', '/etc/ssl/cert2.pem'], + ['#', COMMENT], [], [] + ]]] + + server_conf = nparser.abs_path('server.conf') + names = {'alias', 'another.alias', 'somename'} + mock_vhost.filep = server_conf + mock_vhost.names = names + mock_vhost.path = [] + with pytest.raises(errors.MisconfigurationError): + nparser.add_server_directives(mock_vhost, + [['foo', 'bar'], + ['ssl_certificate', '/etc/ssl/cert2.pem']]) + + def test_comment_is_repeatable(self): + nparser = parser.NginxParser(self.config_path) + example_com = nparser.abs_path('sites-enabled/example.com') + mock_vhost = obj.VirtualHost(example_com, + None, None, None, + {'.example.com', 'example.*'}, + None, [0]) + nparser.add_server_directives(mock_vhost, + [['\n ', '#', ' ', 'what a nice comment']]) + nparser.add_server_directives(mock_vhost, + [['\n ', 'include', ' ', + nparser.abs_path('comment_in_file.conf')]]) + from certbot._internal.plugins.nginx.parser import COMMENT + assert nparser.parsed[example_com] == \ + [[['server'], [['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', '.example.com'], + ['server_name', 'example.*'], + ['#', ' ', 'what a nice comment'], + [], + ['include', nparser.abs_path('comment_in_file.conf')], + ['#', COMMENT], + []]]] + + def test_replace_server_directives(self): + nparser = parser.NginxParser(self.config_path) + target = {'.example.com', 'example.*'} + filep = nparser.abs_path('sites-enabled/example.com') + mock_vhost = obj.VirtualHost(filep, None, None, None, target, None, [0]) + nparser.update_or_add_server_directives( + mock_vhost, [['server_name', 'foobar.com']]) + from certbot._internal.plugins.nginx.parser import COMMENT + assert nparser.parsed[filep] == \ + [[['server'], [['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', 'foobar.com'], ['#', COMMENT], + ['server_name', 'example.*'], [] + ]]] + mock_vhost.names = {'foobar.com', 'example.*'} + nparser.update_or_add_server_directives( + mock_vhost, [['ssl_certificate', 'cert.pem']]) + assert nparser.parsed[filep] == \ + [[['server'], [['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', 'foobar.com'], ['#', COMMENT], + ['server_name', 'example.*'], [], + ['ssl_certificate', 'cert.pem'], ['#', COMMENT], [], + ]]] + + def test_get_best_match(self): + target_name = 'www.eff.org' + names = [{'www.eff.org', 'irrelevant.long.name.eff.org', '*.org'}, + {'eff.org', 'ww2.eff.org', 'test.www.eff.org'}, + {'*.eff.org', '.www.eff.org'}, + {'.eff.org', '*.org'}, + {'www.eff.', 'www.eff.*', '*.www.eff.org'}, + {'example.com', r'~^(www\.)?(eff.+)', '*.eff.*'}, + {'*', r'~^(www\.)?(eff.+)'}, + {'www.*', r'~^(www\.)?(eff.+)', '.test.eff.org'}, + {'*.org', r'*.eff.org', 'www.eff.*'}, + {'*.www.eff.org', 'www.*'}, + {'*.org'}, + set(), + {'example.com'}, + {'www.Eff.org'}, + {'.efF.org'}] + winners = [('exact', 'www.eff.org'), + (None, None), + ('exact', '.www.eff.org'), + ('wildcard_start', '.eff.org'), + ('wildcard_end', 'www.eff.*'), + ('regex', r'~^(www\.)?(eff.+)'), + ('wildcard_start', '*'), + ('wildcard_end', 'www.*'), + ('wildcard_start', '*.eff.org'), + ('wildcard_end', 'www.*'), + ('wildcard_start', '*.org'), + (None, None), + (None, None), + ('exact', 'www.Eff.org'), + ('wildcard_start', '.efF.org')] + + for i, winner in enumerate(winners): + assert winner == \ + parser.get_best_match(target_name, names[i]) + + def test_comment_directive(self): + # pylint: disable=protected-access + block = nginxparser.UnspacedList([ + ["\n", "a", " ", "b", "\n"], + ["c", " ", "d"], + ["\n", "e", " ", "f"]]) + from certbot._internal.plugins.nginx.parser import COMMENT_BLOCK + from certbot._internal.plugins.nginx.parser import comment_directive + comment_directive(block, 1) + comment_directive(block, 0) + assert block.spaced == [ + ["\n", "a", " ", "b", "\n"], + COMMENT_BLOCK, + "\n", + ["c", " ", "d"], + COMMENT_BLOCK, + ["\n", "e", " ", "f"]] + + def test_comment_out_directive(self): + server_block = nginxparser.loads(""" + server { + listen 80; + root /var/www/html; + index star.html; + + server_name *.functorkitten.xyz; + ssl_session_timeout 1440m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; + + ssl_prefer_server_ciphers on; + }""") + block = server_block[0][1] + from certbot._internal.plugins.nginx.parser import _comment_out_directive + _comment_out_directive(block, 4, "blah1") + _comment_out_directive(block, 5, "blah2") + _comment_out_directive(block, 6, "blah3") + assert block.spaced == [ + ['\n ', 'listen', ' ', '80'], + ['\n ', 'root', ' ', '/var/www/html'], + ['\n ', 'index', ' ', 'star.html'], + ['\n\n ', 'server_name', ' ', '*.functorkitten.xyz'], + ['\n ', '#', ' ssl_session_timeout 1440m; # duplicated in blah1'], + [' ', '#', ' ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # duplicated in blah2'], + ['\n\n ', '#', ' ssl_prefer_server_ciphers on; # duplicated in blah3'], + '\n '] + + def test_parse_server_raw_ssl(self): + server = parser._parse_server_raw([ #pylint: disable=protected-access + ['listen', '443'] + ]) + assert not server['ssl'] + + server = parser._parse_server_raw([ #pylint: disable=protected-access + ['listen', '443', 'ssl'] + ]) + assert server['ssl'] + + server = parser._parse_server_raw([ #pylint: disable=protected-access + ['listen', '443'], ['ssl', 'off'] + ]) + assert not server['ssl'] + + server = parser._parse_server_raw([ #pylint: disable=protected-access + ['listen', '443'], ['ssl', 'on'] + ]) + assert server['ssl'] + + def test_parse_server_raw_comment(self): + testdata = """ + server_name *.goo.far + # commented + baz.com; + """ + loaded = nginxparser.loads(testdata) + server = parser._parse_server_raw(loaded) #pylint: disable=protected-access + assert server['names'] == {'*.goo.far', 'baz.com'} + + testdata = """ + server_name *.goo.far # commented + baz.com; + """ + loaded = nginxparser.loads(testdata) + server = parser._parse_server_raw(loaded) #pylint: disable=protected-access + assert server['names'] == {'*.goo.far', 'baz.com'} + + testdata = """ + server_name *.goo.far # commented + ; + """ + loaded = nginxparser.loads(testdata) + server = parser._parse_server_raw(loaded) #pylint: disable=protected-access + assert server['names'] == {'*.goo.far'} + + # known bug; see https://github.com/certbot/certbot/issues/9942 + testdata = """ + server_name *.goo.far + #commented + ; + """ + loaded = nginxparser.loads(testdata) + server = parser._parse_server_raw(loaded) #pylint: disable=protected-access + assert server['names'] == {'*.goo.far', '#commented'} + + # same bug; # isn't actually allowed in domains + testdata = """ + server_name *.go#o.far + ; + """ + loaded = nginxparser.loads(testdata) + server = parser._parse_server_raw(loaded) #pylint: disable=protected-access + assert server['names'] == {'*.go#o.far'} + + testdata = """ + listen 443 + # commented + ssl; + """ + loaded = nginxparser.loads(testdata) + server = parser._parse_server_raw(loaded) #pylint: disable=protected-access + assert server['addrs'] == {obj.Addr.fromstring('443 ssl')} + + def test_parse_server_raw_unix(self): + server = parser._parse_server_raw([ #pylint: disable=protected-access + ['listen', 'unix:/var/run/nginx.sock'] + ]) + assert len(server['addrs']) == 0 + + def test_parse_server_global_ssl_applied(self): + nparser = parser.NginxParser(self.config_path) + server = nparser.parse_server([ + ['listen', '443'] + ]) + assert server['ssl'] + + def test_duplicate_vhost(self): + nparser = parser.NginxParser(self.config_path) + + vhosts = nparser.get_vhosts() + default = [x for x in vhosts if 'default' in x.filep][0] + new_vhost = nparser.duplicate_vhost(default, remove_singleton_listen_params=True) + nparser.filedump(ext='') + + # check properties of new vhost + assert next(iter(new_vhost.addrs)).default is False + assert new_vhost.path != default.path + + # check that things are written to file correctly + new_nparser = parser.NginxParser(self.config_path) + new_vhosts = new_nparser.get_vhosts() + new_defaults = [x for x in new_vhosts if 'default' in x.filep] + assert len(new_defaults) == 2 + new_vhost_parsed = new_defaults[1] + assert next(iter(new_vhost_parsed.addrs)).default is False + assert next(iter(default.names)) == next(iter(new_vhost_parsed.names)) + assert len(default.raw) == len(new_vhost_parsed.raw) + assert next(iter(default.addrs)).super_eq(next(iter(new_vhost_parsed.addrs))) + + def test_duplicate_vhost_remove_ipv6only(self): + nparser = parser.NginxParser(self.config_path) + + vhosts = nparser.get_vhosts() + ipv6ssl = [x for x in vhosts if 'ipv6ssl' in x.filep][0] + new_vhost = nparser.duplicate_vhost(ipv6ssl, remove_singleton_listen_params=True) + nparser.filedump(ext='') + + for addr in new_vhost.addrs: + assert not addr.ipv6only + + identical_vhost = nparser.duplicate_vhost(ipv6ssl, remove_singleton_listen_params=False) + nparser.filedump(ext='') + + called = False + for addr in identical_vhost.addrs: + if addr.ipv6: + assert addr.ipv6only + called = True + assert called + + def test_valid_unicode_characters(self): + nparser = parser.NginxParser(self.config_path) + path = nparser.abs_path('valid_unicode_comments.conf') + parsed = nparser._parse_files(path) # pylint: disable=protected-access + assert ['server'] == parsed[path][2][0] + assert ['listen', '80'] == parsed[path][2][1][3] + + def test_valid_unicode_roundtrip(self): + """This tests the parser's ability to load and save a config containing Unicode""" + nparser = parser.NginxParser(self.config_path) + nparser._parse_files( + nparser.abs_path('valid_unicode_comments.conf') + ) # pylint: disable=protected-access + nparser.filedump(lazy=False) + + def test_invalid_unicode_characters(self): + with self.assertLogs() as log: + nparser = parser.NginxParser(self.config_path) + path = nparser.abs_path('invalid_unicode_comments.conf') + parsed = nparser._parse_files(path) # pylint: disable=protected-access + + assert {} == parsed + assert any( + ('invalid character' in output) and ('UTF-8' in output) + for output in log.output + ) + + def test_valid_unicode_characters_in_ssl_options(self): + nparser = parser.NginxParser(self.config_path) + path = nparser.abs_path('valid_unicode_comments.conf') + parsed = parser._parse_ssl_options(path) # pylint: disable=protected-access + assert ['server'] == parsed[2][0] + assert ['listen', '80'] == parsed[2][1][3] + + def test_invalid_unicode_characters_in_ssl_options(self): + with self.assertLogs() as log: + nparser = parser.NginxParser(self.config_path) + path = nparser.abs_path('invalid_unicode_comments.conf') + parsed = parser._parse_ssl_options(path) # pylint: disable=protected-access + + assert [] == parsed + assert any( + ('invalid character' in output) and ('UTF-8' in output) + for output in log.output + ) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/test_util.py b/certbot/src/certbot/_internal/tests/plugins/nginx/test_util.py new file mode 100644 index 00000000000..ddb2b0ea1d6 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/test_util.py @@ -0,0 +1,128 @@ +"""Common test utilities for the nginx plugin.""" +import copy +import importlib.resources +import shutil +import tempfile +from contextlib import contextmanager +from unittest import mock + +import josepy as jose + +from certbot import util +from certbot.compat import os +from certbot.plugins import common +from certbot.tests import util as test_util +from certbot._internal.plugins.nginx import configurator +from certbot._internal.plugins.nginx import nginxparser + +class NginxTest(test_util.ConfigTestCase): + + def setUp(self): + super().setUp() + + self.configuration = self.config + self.config = None + + self.temp_dir, self.config_dir, self.work_dir = common.dir_setup( + "etc_nginx", __package__) + self.logs_dir = tempfile.mkdtemp('logs') + + self.config_path = os.path.join(self.temp_dir, "etc_nginx") + + self.rsa512jwk = jose.JWKRSA.load(test_util.load_vector( + "rsa512_key.pem")) + + def tearDown(self): + # Cleanup opened resources after a test. This is usually done through atexit handlers in + # Certbot, but during tests, atexit will not run registered functions before tearDown is + # called and instead will run them right before the entire test process exits. + # It is a problem on Windows, that does not accept to clean resources before closing them. + util._release_locks() # pylint: disable=protected-access + + shutil.rmtree(self.temp_dir) + shutil.rmtree(self.config_dir) + shutil.rmtree(self.work_dir) + shutil.rmtree(self.logs_dir) + + def get_nginx_configurator(self, config_path, config_dir, work_dir, logs_dir, + version=(1, 6, 2), openssl_version="1.0.2g"): + """Create an Nginx Configurator with the specified options.""" + + backups = os.path.join(work_dir, "backups") + + self.configuration.nginx_server_root = config_path + self.configuration.nginx_sleep_seconds = 0.1234 + self.configuration.le_vhost_ext = "-le-ssl.conf" + self.configuration.config_dir = config_dir + self.configuration.work_dir = work_dir + self.configuration.logs_dir = logs_dir + self.configuration.backup_dir = backups + self.configuration.temp_checkpoint_dir = os.path.join(work_dir, "temp_checkpoints") + self.configuration.in_progress_dir = os.path.join(backups, "IN_PROGRESS") + self.configuration.server = "https://acme-server.org:443/new" + self.configuration.http01_port = 80 + self.configuration.https_port = 5001 + + with mock.patch("certbot._internal.plugins.nginx.configurator.NginxConfigurator." + "config_test"): + with mock.patch("certbot._internal.plugins.nginx.configurator.util." + "exe_exists") as mock_exe_exists: + mock_exe_exists.return_value = True + config = configurator.NginxConfigurator( + self.configuration, + name="nginx", + version=version, + openssl_version=openssl_version) + config.prepare() + + return config + + +@contextmanager +def get_data_filename(filename): + """Gets the filename of a test data file.""" + ref = importlib.resources.files(__package__) / "testdata" / "etc_nginx"/ filename + with importlib.resources.as_file(ref) as path: + yield path + + +def filter_comments(tree): + """Filter comment nodes from parsed configurations.""" + + def traverse(tree): + """Generator dropping comment nodes""" + for entry in tree: + # key, values = entry + spaceless = [e for e in entry if not nginxparser.spacey(e)] + if spaceless: + key = spaceless[0] + values = spaceless[1] if len(spaceless) > 1 else None + else: + key = values = "" + if isinstance(key, list): + new = copy.deepcopy(entry) + new[1] = filter_comments(values) + yield new + else: + if key != '#' and spaceless: + yield spaceless + + return list(traverse(tree)) + + +def contains_at_depth(haystack, needle, n): + """Is the needle in haystack at depth n? + + Return true if the needle is present in one of the sub-iterables in haystack + at depth n. Haystack must be an iterable. + """ + # Specifically use hasattr rather than isinstance(..., collections.Iterable) + # because we want to include lists but reject strings. + if not hasattr(haystack, '__iter__') or hasattr(haystack, 'strip'): + return False + if n == 0: + return needle in haystack + for item in haystack: + if contains_at_depth(item, needle, n - 1): + return True + return False diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/broken.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/broken.conf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/broken.conf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/broken.conf diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/comment_in_file.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/comment_in_file.conf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/comment_in_file.conf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/comment_in_file.conf diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/edge_cases.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/edge_cases.conf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/edge_cases.conf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/edge_cases.conf diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/foo.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/foo.conf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/foo.conf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/foo.conf diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/invalid_unicode_comments.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/invalid_unicode_comments.conf new file mode 100644 index 00000000000..4d638453559 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/invalid_unicode_comments.conf @@ -0,0 +1,7 @@ +# This configuration file is saved with EUC-KR (a.k.a. cp949) encoding, +# including some Korean letters. + +server { + # ¾È³çÇϼ¼¿ä. 80¹ø Æ÷Æ®¿¡¼­ ¿äûÀ» ±â´Ù¸°´Ù. + listen 80; +} diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/mime.types b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/mime.types new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/minimalistic_comments.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/minimalistic_comments.conf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/minimalistic_comments.conf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/minimalistic_comments.conf diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/multiline_quotes.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/multiline_quotes.conf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/multiline_quotes.conf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/multiline_quotes.conf diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/nginx-include.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/nginx-include.conf new file mode 100644 index 00000000000..e4da3cb5f88 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/nginx-include.conf @@ -0,0 +1,4 @@ +# just a comment on top +# so we're not at the very top +include nginx.conf; +# and another comment for fun diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/nginx.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/nginx.conf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/nginx.conf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/nginx.conf diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/server.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/server.conf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/server.conf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/server.conf diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/addr-80.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/addr-80.com new file mode 100644 index 00000000000..aeef7abdf92 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/addr-80.com @@ -0,0 +1,5 @@ +server { + listen 1.2.3.4:80; + listen [1:20::300]:80; + server_name addr-80.com; +} diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/both.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/both.com new file mode 100644 index 00000000000..23a660df391 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/both.com @@ -0,0 +1,32 @@ +server { + server_name ssl.both.com; +} + +# a duplicate vhost +server { + server_name ssl.both.com; +} + +# a duplicate by means of wildcard +server { + server_name *.both.com; +} + +# combined HTTP and HTTPS +server { + server_name ssl.both.com; + listen 80; + listen 5001 ssl; + + ssl_certificate cert.pem; + ssl_certificate_key cert.key; +} + +# HTTPS, duplicate by means of wildcard +server { + server_name *.both.com; + listen 5001 ssl; + + ssl_certificate cert.pem; + ssl_certificate_key cert.key; +} diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/default b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/default similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/default rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/default diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/example.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/example.com similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/example.com rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/example.com diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/example.net b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/example.net new file mode 100644 index 00000000000..67d566fe647 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/example.net @@ -0,0 +1,5 @@ +server { + listen 80; + listen [::]:80; + server_name $hostname; +} diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/globalssl.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/globalssl.com similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/globalssl.com rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/globalssl.com diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/headers.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/headers.com new file mode 100644 index 00000000000..1489a37be18 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/headers.com @@ -0,0 +1,7 @@ +server { + server_name headers.com; + add_header X-Content-Type-Options nosniff; + ssl on; + ssl_certificate snakeoil.cert; + ssl_certificate_key snakeoil.key; +} diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/ipv6.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/ipv6.com similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/ipv6.com rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/ipv6.com diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/ipv6ssl.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/ipv6ssl.com similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/ipv6ssl.com rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/ipv6ssl.com diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/migration.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/migration.com similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/migration.com rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/migration.com diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/no-listens.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/no-listens.com new file mode 100644 index 00000000000..5081455bde9 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/no-listens.com @@ -0,0 +1,3 @@ +server { + server_name no-listens.com; +} diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/sslon.com b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/sslon.com similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/sites-enabled/sslon.com rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/sites-enabled/sslon.com diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/fastcgi_params b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/fastcgi_params similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/fastcgi_params rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/fastcgi_params diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-utf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-utf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-utf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-utf diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-win b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-win similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-win rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/koi-win diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/mime.types b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/mime.types similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/mime.types rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/mime.types diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi-ui.conf.1.4.1 b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi-ui.conf.1.4.1 similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi-ui.conf.1.4.1 rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi-ui.conf.1.4.1 diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi.rules b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi.rules similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi.rules rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi.rules diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi_core.rules b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi_core.rules similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi_core.rules rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/naxsi_core.rules diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/nginx.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/nginx.conf similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/nginx.conf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/nginx.conf diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/proxy_params b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/proxy_params similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/proxy_params rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/proxy_params diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/scgi_params b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/scgi_params similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/scgi_params rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/scgi_params diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-available/default b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-available/default similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-available/default rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-available/default diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-enabled/default b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-enabled/default similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-enabled/default rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/sites-enabled/default diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/uwsgi_params b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/uwsgi_params similarity index 100% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/uwsgi_params rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/uwsgi_params diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/win-utf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/win-utf similarity index 99% rename from certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/win-utf rename to certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/win-utf index cd288529284..60e224c2168 100644 --- a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/win-utf +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/ubuntu_nginx_1_4_6/default_vhost/nginx/win-utf @@ -29,7 +29,7 @@ charset_map windows-1251 utf-8 { A4 C2A4; # currency sign A5 D290; # capital Ukrainian soft G - A6 C2A6; # borken bar + A6 C2A6; # broken bar A7 C2A7; # section sign A8 D081; # capital YO A9 C2A9; # (C) diff --git a/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/valid_unicode_comments.conf b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/valid_unicode_comments.conf new file mode 100644 index 00000000000..89c978b2e37 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/nginx/testdata/etc_nginx/valid_unicode_comments.conf @@ -0,0 +1,9 @@ +# This configuration file is saved with valid UTF-8 encoding, +# including some CJK alphabets. + +server { + # 안녕하세요. 80번 í¬íЏì—서 ìš”ì²­ì„ ê¸°ë‹¤ë¦°ë‹¤. + # ã“ã‚“ã«ã¡ã¯ã€‚8ï¼ç•ªãƒãƒ¼ãƒˆã‹ã‚‰ãƒªã‚¯ã‚¨ã‚¹ãƒˆã‚’å¾…ã¤ã€‚ + # 你好。等待端å£80上的请求。 + listen 80; +} diff --git a/certbot/src/certbot/_internal/tests/plugins/null_test.py b/certbot/src/certbot/_internal/tests/plugins/null_test.py new file mode 100644 index 00000000000..4740b7d878e --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/null_test.py @@ -0,0 +1,23 @@ +"""Tests for certbot._internal.plugins.null.""" +import sys +import unittest +from unittest import mock + +import pytest + + +class InstallerTest(unittest.TestCase): + """Tests for certbot._internal.plugins.null.Installer.""" + + def setUp(self): + from certbot._internal.plugins.null import Installer + self.installer = Installer(config=mock.MagicMock(), name="null") + + def test_it(self): + assert isinstance(self.installer.more_info(), str) + assert [] == self.installer.get_all_names() + assert [] == self.installer.supported_enhancements() + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/selection_test.py b/certbot/src/certbot/_internal/tests/plugins/selection_test.py new file mode 100644 index 00000000000..3a7e12926a2 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/selection_test.py @@ -0,0 +1,274 @@ +"""Tests for letsencrypt.plugins.selection""" +import sys +import unittest +from unittest import mock + +import pytest + +from certbot import errors +from certbot import interfaces +from certbot._internal.display import obj as display_obj +from certbot._internal.plugins.disco import PluginsRegistry +from certbot.display import util as display_util +from certbot.tests import util as test_util + + +class ConveniencePickPluginTest(unittest.TestCase): + """Tests for certbot._internal.plugins.selection.pick_*.""" + + def _test(self, fun, ifaces): + config = mock.Mock() + default = mock.Mock() + plugins = mock.Mock() + + with mock.patch("certbot._internal.plugins.selection.pick_plugin") as mock_p: + mock_p.return_value = "foo" + assert "foo" == fun(config, default, plugins, "Question?") + mock_p.assert_called_once_with( + config, default, plugins, "Question?", ifaces) + + def test_authenticator(self): + from certbot._internal.plugins.selection import pick_authenticator + self._test(pick_authenticator, (interfaces.Authenticator,)) + + def test_installer(self): + from certbot._internal.plugins.selection import pick_installer + self._test(pick_installer, (interfaces.Installer,)) + + def test_configurator(self): + from certbot._internal.plugins.selection import pick_configurator + self._test(pick_configurator, + (interfaces.Authenticator, interfaces.Installer)) + + +class PickPluginTest(unittest.TestCase): + """Tests for certbot._internal.plugins.selection.pick_plugin.""" + + def setUp(self): + self.config = mock.Mock(noninteractive_mode=False) + self.default = None + self.reg = mock.MagicMock() + self.question = "Question?" + self.ifaces: list[interfaces.Plugin] = [] + + def _call(self): + from certbot._internal.plugins.selection import pick_plugin + return pick_plugin(self.config, self.default, self.reg, + self.question, self.ifaces) + + def test_default_provided(self): + self.default = "foo" + self._call() + assert 1 == self.reg.filter.call_count + + def test_no_default(self): + self._call() + assert 1 == self.reg.visible().ifaces.call_count + + def test_no_candidate(self): + assert self._call() is None + + def test_single(self): + plugin_ep = mock.MagicMock() + plugin_ep.init.return_value = "foo" + plugin_ep.misconfigured = False + + self.reg.visible().ifaces().available.return_value = { + "bar": plugin_ep} + assert "foo" == self._call() + + def test_single_misconfigured(self): + plugin_ep = mock.MagicMock() + plugin_ep.init.return_value = "foo" + plugin_ep.misconfigured = True + + self.reg.visible().ifaces().available.return_value = { + "bar": plugin_ep} + assert self._call() is None + + def test_multiple(self): + plugin_ep = mock.MagicMock() + plugin_ep.init.return_value = "foo" + self.reg.visible().ifaces().available.return_value = { + "bar": plugin_ep, + "baz": plugin_ep, + } + with mock.patch("certbot._internal.plugins.selection.choose_plugin") as mock_choose: + mock_choose.return_value = plugin_ep + assert "foo" == self._call() + mock_choose.assert_called_once_with( + [plugin_ep, plugin_ep], self.question) + + def test_choose_plugin_none(self): + self.reg.visible().ifaces().available.return_value = { + "bar": None, + "baz": None, + } + + with mock.patch("certbot._internal.plugins.selection.choose_plugin") as mock_choose: + mock_choose.return_value = None + assert self._call() is None + + def test_default_must_be_filtered(self): + # https://github.com/certbot/certbot/issues/9664 + self.default = "foo" + filtered = mock.MagicMock() + self.reg.filter.return_value = filtered + self._call() + assert filtered.ifaces.call_count == 1 + + +class ChoosePluginTest(unittest.TestCase): + """Tests for certbot._internal.plugins.selection.choose_plugin.""" + + def setUp(self): + display_obj.set_display(display_obj.FileDisplay(sys.stdout, False)) + + self.mock_apache = mock.Mock( + description_with_name="a", misconfigured=True) + self.mock_apache.name = "apache" + self.mock_stand = mock.Mock( + description_with_name="s", misconfigured=False) + self.mock_stand.init().more_info.return_value = "standalone" + self.plugins = [ + self.mock_apache, + self.mock_stand, + ] + + def _call(self): + from certbot._internal.plugins.selection import choose_plugin + return choose_plugin(self.plugins, "Question?") + + @test_util.patch_display_util() + def test_selection(self, mock_util): + mock_util().menu.side_effect = [(display_util.OK, 0), + (display_util.OK, 1)] + assert self.mock_stand == self._call() + assert mock_util().notification.call_count == 1 + + @test_util.patch_display_util() + def test_more_info(self, mock_util): + mock_util().menu.side_effect = [ + (display_util.OK, 1), + ] + + assert self.mock_stand == self._call() + + @test_util.patch_display_util() + def test_no_choice(self, mock_util): + mock_util().menu.return_value = (display_util.CANCEL, 0) + assert self._call() is None + + +class GetUnpreparedInstallerTest(test_util.ConfigTestCase): + """Tests for certbot._internal.plugins.selection.get_unprepared_installer.""" + + def setUp(self): + super().setUp() + self.mock_apache_fail_ep = mock.Mock( + description_with_name="afail") + self.mock_apache_fail_ep.check_name = lambda name: name == "afail" + self.mock_apache_ep = mock.Mock( + description_with_name="apache") + self.mock_apache_ep.check_name = lambda name: name == "apache" + self.mock_apache_plugin = mock.MagicMock() + self.mock_apache_ep.init.return_value = self.mock_apache_plugin + self.plugins = PluginsRegistry({ + "afail": self.mock_apache_fail_ep, + "apache": self.mock_apache_ep, + }) + + def _call(self): + from certbot._internal.plugins.selection import get_unprepared_installer + return get_unprepared_installer(self.config, self.plugins) + + def test_no_installer_defined(self): + self.config.configurator = None + assert self._call() is None + + def test_no_available_installers(self): + self.config.configurator = "apache" + self.plugins = PluginsRegistry({}) + with pytest.raises(errors.PluginSelectionError): + self._call() + + def test_get_plugin(self): + self.config.configurator = "apache" + installer = self._call() + assert installer is self.mock_apache_plugin + + def test_multiple_installers_returned(self): + self.config.configurator = "apache" + # Two plugins with the same name + self.mock_apache_fail_ep.check_name = lambda name: name == "apache" + with pytest.raises(errors.PluginSelectionError): + self._call() + + +class TestChooseConfiguratorPlugins(unittest.TestCase): + """Tests for certbot._internal.plugins.selection.choose_configurator_plugins.""" + + def _setupMockPlugin(self, name): + mock_ep = mock.Mock( + description_with_name=name) + mock_ep.check_name = lambda n: n == name + mock_plugin = mock.MagicMock() + mock_plugin.name = name + mock_ep.init.return_value = mock_plugin + mock_ep.misconfigured = False + return mock_ep + + def _parseArgs(self, args): + from certbot._internal import cli + return cli.prepare_and_parse_args(self.plugins, args.split()) + + def setUp(self): + self.plugins = PluginsRegistry({ + "nginx": self._setupMockPlugin("nginx"), + "apache": self._setupMockPlugin("apache"), + "manual": self._setupMockPlugin("manual"), + }) + + def _runWithArgs(self, args): + from certbot._internal.plugins.selection import choose_configurator_plugins + return choose_configurator_plugins(self._parseArgs(args), self.plugins, "certonly") + + def test_noninteractive_configurator(self): + # For certonly, setting either the nginx or apache configurators should + # return both an installer and authenticator + inst, auth = self._runWithArgs("certonly --nginx") + assert inst.name == "nginx" + assert auth.name == "nginx" + + inst, auth = self._runWithArgs("certonly --apache") + assert inst.name == "apache" + assert auth.name == "apache" + + def test_noninteractive_inst_arg(self): + # For certonly, if an installer arg is set, it should be returned as expected + inst, auth = self._runWithArgs("certonly -a nginx -i nginx") + assert inst.name == "nginx" + assert auth.name == "nginx" + + inst, auth = self._runWithArgs("certonly -a apache -i apache") + assert inst.name == "apache" + assert auth.name == "apache" + + # if no installer arg is set (or it's set to none), one shouldn't be returned + inst, auth = self._runWithArgs("certonly -a nginx") + assert inst is None + assert auth.name == "nginx" + inst, auth = self._runWithArgs("certonly -a nginx -i none") + assert inst is None + assert auth.name == "nginx" + + inst, auth = self._runWithArgs("certonly -a apache") + assert inst is None + assert auth.name == "apache" + inst, auth = self._runWithArgs("certonly -a apache -i none") + assert inst is None + assert auth.name == "apache" + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/standalone_test.py b/certbot/src/certbot/_internal/tests/plugins/standalone_test.py new file mode 100644 index 00000000000..ab3cd2768de --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/standalone_test.py @@ -0,0 +1,188 @@ +"""Tests for certbot._internal.plugins.standalone.""" +import errno +import socket +import sys +import unittest +from unittest import mock + +import josepy as jose +import pytest + +from acme import challenges, messages +from acme import standalone as acme_standalone +from certbot import achallenges +from certbot import errors +from certbot.tests import acme_util +from certbot.tests import util as test_util + + +class ServerManagerTest(unittest.TestCase): + """Tests for certbot._internal.plugins.standalone.ServerManager.""" + + def setUp(self): + from certbot._internal.plugins.standalone import ServerManager + self.http_01_resources: set[acme_standalone.HTTP01RequestHandler.HTTP01Resource] = {} + self.mgr = ServerManager(self.http_01_resources) + + def test_init(self): + assert self.mgr.http_01_resources is self.http_01_resources + + def _test_run_stop(self, challenge_type): + server = self.mgr.run(port=0, challenge_type=challenge_type) + port = server.getsocknames()[0][1] + assert self.mgr.running() == {port: server} + self.mgr.stop(port=port) + assert self.mgr.running() == {} + + def test_run_stop_http_01(self): + self._test_run_stop(challenges.HTTP01) + + def test_run_idempotent(self): + server = self.mgr.run(port=0, challenge_type=challenges.HTTP01) + port = server.getsocknames()[0][1] + server2 = self.mgr.run(port=port, challenge_type=challenges.HTTP01) + assert self.mgr.running() == {port: server} + assert server is server2 + self.mgr.stop(port) + assert self.mgr.running() == {} + + def test_run_bind_error(self): + some_server = socket.socket(socket.AF_INET6) + some_server.bind(("", 0)) + port = some_server.getsockname()[1] + maybe_another_server = socket.socket() + try: + maybe_another_server.bind(("", port)) + except OSError: + pass + with pytest.raises(errors.StandaloneBindError): + self.mgr.run(port, + challenge_type=challenges.HTTP01) + assert self.mgr.running() == {} + some_server.close() + maybe_another_server.close() + + +def get_open_port(): + """Gets an open port number from the OS.""" + open_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) + open_socket.bind(("", 0)) + port = open_socket.getsockname()[1] + open_socket.close() + return port + + +class AuthenticatorTest(unittest.TestCase): + """Tests for certbot._internal.plugins.standalone.Authenticator.""" + + def setUp(self): + from certbot._internal.plugins.standalone import Authenticator + + self.config = mock.MagicMock(http01_port=get_open_port()) + self.auth = Authenticator(self.config, name="standalone") + self.auth.servers = mock.MagicMock() + + def test_more_info(self): + assert isinstance(self.auth.more_info(), str) + + def test_get_chall_pref(self): + assert self.auth.get_chall_pref(identifier=None) == \ + [challenges.HTTP01] + + def test_perform(self): + achalls = self._get_achalls() + response = self.auth.perform(achalls) + + expected = [achall.response(achall.account_key) for achall in achalls] + assert response == expected + + @test_util.patch_display_util() + def test_perform_eaddrinuse_retry(self, mock_get_utility): + mock_utility = mock_get_utility() + encountered_errno = errno.EADDRINUSE + error = errors.StandaloneBindError(mock.MagicMock(errno=encountered_errno), -1) + self.auth.servers.run.side_effect = [error] + 2 * [mock.MagicMock()] + mock_yesno = mock_utility.yesno + mock_yesno.return_value = True + + self.test_perform() + self._assert_correct_yesno_call(mock_yesno) + + @test_util.patch_display_util() + def test_perform_eaddrinuse_no_retry(self, mock_get_utility): + mock_utility = mock_get_utility() + mock_yesno = mock_utility.yesno + mock_yesno.return_value = False + + encountered_errno = errno.EADDRINUSE + with pytest.raises(errors.PluginError): + self._fail_perform(encountered_errno) + self._assert_correct_yesno_call(mock_yesno) + + def _assert_correct_yesno_call(self, mock_yesno): + yesno_args, yesno_kwargs = mock_yesno.call_args + assert "in use" in yesno_args[0] + assert not yesno_kwargs.get("default", True) + + def test_perform_eacces(self): + encountered_errno = errno.EACCES + with pytest.raises(errors.PluginError): + self._fail_perform(encountered_errno) + + def test_perform_unexpected_socket_error(self): + encountered_errno = errno.ENOTCONN + with pytest.raises(errors.StandaloneBindError): + self._fail_perform(encountered_errno) + + def _fail_perform(self, encountered_errno): + error = errors.StandaloneBindError(mock.MagicMock(errno=encountered_errno), -1) + self.auth.servers.run.side_effect = error + self.auth.perform(self._get_achalls()) + + @classmethod + def _get_achalls(cls): + domain = b'localhost' + key = jose.JWK.load(test_util.load_vector('rsa512_key.pem')) + http_01 = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.HTTP01_P, + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value=domain), + account_key=key) + + return [http_01] + + def test_cleanup(self): + self.auth.servers.running.return_value = { + 1: "server1", + 2: "server2", + } + self.auth.served["server1"].add("chall1") + self.auth.served["server2"].update(["chall2", "chall3"]) + + self.auth.cleanup(["chall1"]) + assert self.auth.served == { + "server1": set(), "server2": {"chall2", "chall3"}} + self.auth.servers.stop.assert_called_once_with(1) + + self.auth.servers.running.return_value = { + 2: "server2", + } + self.auth.cleanup(["chall2"]) + assert self.auth.served == { + "server1": set(), "server2": {"chall3"}} + assert 1 == self.auth.servers.stop.call_count + + self.auth.cleanup(["chall3"]) + assert self.auth.served == { + "server1": set(), "server2": set()} + self.auth.servers.stop.assert_called_with(2) + + def test_auth_hint(self): + self.config.http01_port = "80" + self.config.http01_address = None + assert "on port 80" in self.auth.auth_hint([]) + self.config.http01_address = "127.0.0.1" + assert "on 127.0.0.1:80" in self.auth.auth_hint([]) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/storage_test.py b/certbot/src/certbot/_internal/tests/plugins/storage_test.py new file mode 100644 index 00000000000..ddc830014d4 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/storage_test.py @@ -0,0 +1,118 @@ +"""Tests for certbot.plugins.storage.PluginStorage""" +import json +import sys +from unittest import mock + +import pytest + +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +from certbot.tests import util as test_util + + +class PluginStorageTest(test_util.ConfigTestCase): + """Test for certbot.plugins.storage.PluginStorage""" + + def setUp(self): + super().setUp() + self.plugin_cls = test_util.DummyInstaller + filesystem.mkdir(self.config.config_dir) + with mock.patch("certbot.reverter.util"): + self.plugin = self.plugin_cls(config=self.config, name="mockplugin") + + def test_load_errors_cant_read(self): + with open(os.path.join(self.config.config_dir, + ".pluginstorage.json"), "w") as fh: + fh.write("dummy") + # When unable to read file that exists + mock_open = mock.mock_open() + mock_open.side_effect = IOError + self.plugin.storage._storagepath = os.path.join(self.config.config_dir, + ".pluginstorage.json") + with mock.patch("builtins.open", mock_open): + with mock.patch('certbot.compat.os.path.isfile', return_value=True): + with mock.patch("certbot.reverter.util"): + with pytest.raises(errors.PluginStorageError): + self.plugin.storage._load() # pylint: disable=protected-access + + def test_load_errors_empty(self): + with open(os.path.join(self.config.config_dir, ".pluginstorage.json"), "w") as fh: + fh.write('') + with mock.patch("certbot.plugins.storage.logger.debug") as mock_log: + # Should not error out but write a debug log line instead + with mock.patch("certbot.reverter.util"): + nocontent = self.plugin_cls(self.config, "mockplugin") + with pytest.raises(KeyError): + nocontent.storage.fetch("value") + assert mock_log.called + assert "no values loaded" in mock_log.call_args[0][0] + + def test_load_errors_corrupted(self): + with open(os.path.join(self.config.config_dir, + ".pluginstorage.json"), "w") as fh: + fh.write('invalid json') + with mock.patch("certbot.plugins.storage.logger.error") as mock_log: + with mock.patch("certbot.reverter.util"): + corrupted = self.plugin_cls(self.config, "mockplugin") + with pytest.raises(errors.PluginError): + corrupted.storage.fetch("value") + assert "is corrupted" in mock_log.call_args[0][0] + + def test_save_errors_cant_serialize(self): + with mock.patch("certbot.plugins.storage.logger.error") as mock_log: + # Set data as something that can't be serialized + self.plugin.storage._initialized = True # pylint: disable=protected-access + self.plugin.storage._storagepath = "/tmp/whatever" + self.plugin.storage._data = self.plugin_cls # pylint: disable=protected-access + with pytest.raises(errors.PluginStorageError): + self.plugin.storage.save() + assert "Could not serialize" in mock_log.call_args[0][0] + + def test_save_errors_unable_to_write_file(self): + mock_open = mock.mock_open() + mock_open.side_effect = IOError + with mock.patch("certbot.compat.filesystem.open", mock_open): + with mock.patch("certbot.plugins.storage.logger.error") as mock_log: + self.plugin.storage._data = {"valid": "data"} # pylint: disable=protected-access + self.plugin.storage._initialized = True # pylint: disable=protected-access + self.plugin.storage._storagepath = "/tmp/whatever" + with pytest.raises(errors.PluginStorageError): + self.plugin.storage.save() + assert "Could not write" in mock_log.call_args[0][0] + + def test_save_uninitialized(self): + with mock.patch("certbot.reverter.util"): + with pytest.raises(errors.PluginStorageError): + self.plugin_cls(self.config, "x").storage.save() + + def test_namespace_isolation(self): + with mock.patch("certbot.reverter.util"): + plugin1 = self.plugin_cls(self.config, "first") + plugin2 = self.plugin_cls(self.config, "second") + plugin1.storage.put("first_key", "first_value") + with pytest.raises(KeyError): + plugin2.storage.fetch("first_key") + with pytest.raises(KeyError): + plugin2.storage.fetch("first") + assert plugin1.storage.fetch("first_key") == "first_value" + + def test_saved_state(self): + self.plugin.storage.put("testkey", "testvalue") + # Write to disk + self.plugin.storage.save() + with mock.patch("certbot.reverter.util"): + another = self.plugin_cls(self.config, "mockplugin") + assert another.storage.fetch("testkey") == "testvalue" + + with open(os.path.join(self.config.config_dir, + ".pluginstorage.json"), 'r') as fh: + psdata = fh.read() + psjson = json.loads(psdata) + assert "mockplugin" in psjson.keys() + assert len(psjson) == 1 + assert psjson["mockplugin"]["testkey"] == "testvalue" + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/util_test.py b/certbot/src/certbot/_internal/tests/plugins/util_test.py new file mode 100644 index 00000000000..c3d76f8018e --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/util_test.py @@ -0,0 +1,40 @@ +"""Tests for certbot.plugins.util.""" +import sys +from unittest import mock + +import pytest + +from certbot.compat import os + + +def test_get_prefix(): + from certbot.plugins.util import get_prefixes + assert get_prefixes('/a/b/c') == \ + [os.path.normpath(path) for path in ['/a/b/c', '/a/b', '/a', '/']] + assert get_prefixes('/') == [os.path.normpath('/')] + assert get_prefixes('a') == ['a'] + + +@mock.patch("certbot.plugins.util.logger.debug") +def test_path_surgery(mock_debug): + from certbot.plugins.util import path_surgery + all_path = {"PATH": "/usr/local/bin:/bin/:/usr/sbin/:/usr/local/sbin/"} + with mock.patch.dict('os.environ', all_path): + with mock.patch('certbot.util.exe_exists') as mock_exists: + mock_exists.return_value = True + assert path_surgery("eg") is True + assert mock_debug.call_count == 0 + assert os.environ["PATH"] == all_path["PATH"] + if os.name != 'nt': + # This part is specific to Linux since on Windows no PATH surgery is ever done. + no_path = {"PATH": "/tmp/"} + with mock.patch.dict('os.environ', no_path): + path_surgery("thingy") + assert mock_debug.call_count == (2 if os.name != 'nt' else 1) + assert "Failed to find" in mock_debug.call_args[0][0] + assert "/usr/local/bin" in os.environ["PATH"] + assert "/tmp" in os.environ["PATH"] + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/plugins/webroot_test.py b/certbot/src/certbot/_internal/tests/plugins/webroot_test.py new file mode 100644 index 00000000000..ec2c382662f --- /dev/null +++ b/certbot/src/certbot/_internal/tests/plugins/webroot_test.py @@ -0,0 +1,414 @@ +"""Tests for certbot._internal.plugins.webroot.""" + +from __future__ import print_function + +import argparse +import errno +import json +import shutil +import sys +import tempfile +import unittest +from unittest import mock + +import josepy as jose +import pytest + +from acme import challenges, messages +from certbot import achallenges +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +from certbot.display import util as display_util +from certbot._internal.cli import cli_utils +from certbot.tests import acme_util +from certbot.tests import util as test_util + +KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) + + +class AuthenticatorTest(unittest.TestCase): + """Tests for certbot._internal.plugins.webroot.Authenticator.""" + + achall = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.HTTP01_P, + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="thing.com"), + account_key=KEY) + + def setUp(self): + from certbot._internal.plugins.webroot import Authenticator + + # On Linux directories created by tempfile.mkdtemp inherit their permissions from their + # parent directory. So the actual permissions are inconsistent over various tests env. + # To circumvent this, a dedicated sub-workspace is created under the workspace, using + # filesystem.mkdir to get consistent permissions. + self.workspace = tempfile.mkdtemp() + self.path = os.path.join(self.workspace, 'webroot') + filesystem.mkdir(self.path) + self.partial_root_challenge_path = os.path.join( + self.path, ".well-known") + self.root_challenge_path = os.path.join( + self.path, ".well-known", "acme-challenge") + self.validation_path = os.path.join( + self.root_challenge_path, + "ZXZhR3hmQURzNnBTUmIyTEF2OUlaZjE3RHQzanV4R0orUEN0OTJ3citvQQ") + self.config = mock.MagicMock(webroot_path=self.path, + webroot_map={"thing.com": self.path}) + self.auth = Authenticator(self.config, "webroot") + + def tearDown(self): + shutil.rmtree(self.path) + + def test_more_info(self): + more_info = self.auth.more_info() + assert isinstance(more_info, str) + assert self.path in more_info + + def test_add_parser_arguments(self): + add = mock.MagicMock() + self.auth.add_parser_arguments(add) + assert 2 == add.call_count + + def test_prepare(self): + self.auth.prepare() # shouldn't raise any exceptions + + @test_util.patch_display_util() + def test_webroot_from_list(self, mock_get_utility): + self.config.webroot_path = [] + self.config.webroot_map = {"otherthing.com": self.path} + mock_display = mock_get_utility() + mock_display.menu.return_value = (display_util.OK, 1,) + + self.auth.perform([self.achall]) + assert mock_display.menu.called + for call in mock_display.menu.call_args_list: + assert self.achall.identifier.value in call[0][0] + assert all( + webroot in call[0][1] + for webroot in self.config.webroot_map.values()) + assert self.config.webroot_map[self.achall.identifier.value] == \ + self.path + + @unittest.skipIf(filesystem.POSIX_MODE, reason='Test specific to Windows') + @test_util.patch_display_util() + def test_webconfig_file_generate_and_cleanup(self, mock_get_utility): + mock_display = mock_get_utility() + mock_display.menu.return_value = (display_util.OK, 1,) + + self.auth.perform([self.achall]) + assert os.path.exists(os.path.join(self.root_challenge_path, "web.config")) + self.auth.cleanup([self.achall]) + assert not os.path.exists(os.path.join(self.root_challenge_path, "web.config")) + + @unittest.skipIf(filesystem.POSIX_MODE, reason='Test specific to Windows') + @test_util.patch_display_util() + def test_foreign_webconfig_file_handling(self, mock_get_utility): + mock_display = mock_get_utility() + mock_display.menu.return_value = (display_util.OK, 1,) + + challenge_path = os.path.join(self.path, ".well-known", "acme-challenge") + filesystem.makedirs(challenge_path) + + webconfig_path = os.path.join(challenge_path, "web.config") + with open(webconfig_path, "w") as file: + file.write("something") + self.auth.perform([self.achall]) + from certbot import crypto_util + webconfig_hash = crypto_util.sha256sum(webconfig_path) + from certbot._internal.plugins.webroot import _WEB_CONFIG_SHA256SUMS + assert webconfig_hash not in _WEB_CONFIG_SHA256SUMS + + @unittest.skipIf(filesystem.POSIX_MODE, reason='Test specific to Windows') + def test_foreign_webconfig_multiple_domains(self): + # Covers bug https://github.com/certbot/certbot/issues/9091 + achall_2 = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb(challenges.HTTP01(token=b"bingo"), "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="second-thing.com"), + account_key=KEY) + self.config.webroot_map["second-thing.com"] = self.path + + challenge_path = os.path.join(self.path, ".well-known", "acme-challenge") + filesystem.makedirs(challenge_path) + + webconfig_path = os.path.join(challenge_path, "web.config") + with open(webconfig_path, "w") as file: + file.write("something") + self.auth.perform([self.achall, achall_2]) + + @test_util.patch_display_util() + def test_webroot_from_list_help_and_cancel(self, mock_get_utility): + self.config.webroot_path = [] + self.config.webroot_map = {"otherthing.com": self.path} + + mock_display = mock_get_utility() + mock_display.menu.side_effect = ((display_util.CANCEL, -1),) + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + assert mock_display.menu.called + for call in mock_display.menu.call_args_list: + assert self.achall.identifier.value in call[0][0] + assert all( + webroot in call[0][1] + for webroot in self.config.webroot_map.values()) + + @test_util.patch_display_util() + def test_new_webroot(self, mock_get_utility): + self.config.webroot_path = [] + self.config.webroot_map = {"something.com": self.path} + + mock_display = mock_get_utility() + mock_display.menu.return_value = (display_util.OK, 0,) + with mock.patch('certbot.display.ops.validated_directory') as m: + m.side_effect = ((display_util.CANCEL, -1), + (display_util.OK, self.path,)) + + self.auth.perform([self.achall]) + + assert self.config.webroot_map[self.achall.identifier.value] == self.path + + @test_util.patch_display_util() + def test_new_webroot_empty_map_cancel(self, mock_get_utility): + self.config.webroot_path = [] + self.config.webroot_map = {} + + mock_display = mock_get_utility() + mock_display.menu.return_value = (display_util.OK, 0,) + with mock.patch('certbot.display.ops.validated_directory') as m: + m.return_value = (display_util.CANCEL, -1) + with pytest.raises(errors.PluginError): + self.auth.perform([self.achall]) + + def test_perform_missing_root(self): + self.config.webroot_path = None + self.config.webroot_map = {} + with pytest.raises(errors.PluginError): + self.auth.perform([]) + + def test_perform_reraises_other_errors(self): + self.auth.full_path = os.path.join(self.path, "null") + permission_canary = os.path.join(self.path, "rnd") + with open(permission_canary, "w") as f: + f.write("thingimy") + filesystem.chmod(self.path, 0o000) + try: + with open(permission_canary, "r"): + pass + print("Warning, running tests as root skips permissions tests...") + except OSError: + # ok, permissions work, test away... + with pytest.raises(errors.PluginError): + self.auth.perform([]) + filesystem.chmod(self.path, 0o700) + + @mock.patch("certbot._internal.plugins.webroot.filesystem.copy_ownership_and_apply_mode") + def test_failed_chown(self, mock_ownership): + mock_ownership.side_effect = OSError(errno.EACCES, "msg") + self.auth.perform([self.achall]) # exception caught and logged + + @test_util.patch_display_util() + def test_perform_new_webroot_not_in_map(self, mock_get_utility): + new_webroot = tempfile.mkdtemp() + self.config.webroot_path = [] + self.config.webroot_map = {"whatever.com": self.path} + mock_display = mock_get_utility() + mock_display.menu.side_effect = ((display_util.OK, 0), + (display_util.OK, new_webroot)) + achall = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.HTTP01_P, + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="something.com"), + account_key=KEY) + with mock.patch('certbot.display.ops.validated_directory') as m: + m.return_value = (display_util.OK, new_webroot,) + self.auth.perform([achall]) + assert self.config.webroot_map[achall.identifier.value] == new_webroot + + def test_perform_permissions(self): + self.auth.prepare() + + # Remove exec bit from permission check, so that it + # matches the file + self.auth.perform([self.achall]) + assert filesystem.check_mode(self.validation_path, 0o644) + + # Check permissions of the directories + for dirpath, dirnames, _ in os.walk(self.path): + for directory in dirnames: + full_path = os.path.join(dirpath, directory) + assert filesystem.check_mode(full_path, 0o755) + + assert filesystem.has_same_ownership(self.validation_path, self.path) + + def test_perform_cleanup(self): + self.auth.prepare() + responses = self.auth.perform([self.achall]) + assert 1 == len(responses) + assert os.path.exists(self.validation_path) + with open(self.validation_path) as validation_f: + validation = validation_f.read() + assert challenges.KeyAuthorizationChallengeResponse( + key_authorization=validation).verify( + self.achall.chall, KEY.public_key()) + + self.auth.cleanup([self.achall]) + assert not os.path.exists(self.validation_path) + assert not os.path.exists(self.root_challenge_path) + assert not os.path.exists(self.partial_root_challenge_path) + + def test_perform_cleanup_existing_dirs(self): + filesystem.mkdir(self.partial_root_challenge_path) + self.auth.prepare() + self.auth.perform([self.achall]) + self.auth.cleanup([self.achall]) + + # Ensure we don't "clean up" directories that previously existed + assert not os.path.exists(self.validation_path) + assert not os.path.exists(self.root_challenge_path) + + def test_perform_cleanup_multiple_challenges(self): + bingo_achall = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=b"bingo"), "pending"), + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="thing.com"), + account_key=KEY) + + bingo_validation_path = "YmluZ28" + filesystem.mkdir(self.partial_root_challenge_path) + self.auth.prepare() + self.auth.perform([bingo_achall, self.achall]) + + self.auth.cleanup([self.achall]) + assert not os.path.exists(bingo_validation_path) + assert os.path.exists(self.root_challenge_path) + self.auth.cleanup([bingo_achall]) + assert not os.path.exists(self.validation_path) + assert not os.path.exists(self.root_challenge_path) + + def test_cleanup_leftovers(self): + self.auth.prepare() + self.auth.perform([self.achall]) + + leftover_path = os.path.join(self.root_challenge_path, 'leftover') + filesystem.mkdir(leftover_path) + + self.auth.cleanup([self.achall]) + assert not os.path.exists(self.validation_path) + assert os.path.exists(self.root_challenge_path) + + os.rmdir(leftover_path) + + @mock.patch('certbot.compat.os.rmdir') + def test_cleanup_failure(self, mock_rmdir): + self.auth.prepare() + self.auth.perform([self.achall]) + + os_error = OSError() + os_error.errno = errno.EACCES + mock_rmdir.side_effect = os_error + + self.auth.cleanup([self.achall]) + assert not os.path.exists(self.validation_path) + assert os.path.exists(self.root_challenge_path) + +class WebrootActionTest(unittest.TestCase): + """Tests for webroot argparse actions.""" + + achall = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.HTTP01_P, + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value="thing.com"), + account_key=KEY) + + ipchall = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.chall_to_challb( + challenges.HTTP01(token=((b'a' * 16))), + messages.STATUS_PENDING), + identifier=messages.Identifier(typ=messages.IDENTIFIER_IP, value="1.2.3.4"), + account_key=KEY) + + def setUp(self): + from certbot._internal.plugins.webroot import Authenticator + self.path = tempfile.mkdtemp() + self.parser = argparse.ArgumentParser() + self.parser.ip_addresses = [] + self.parser.add_argument("-d", "--domains", + action=cli_utils.DomainsAction, default=[]) + self.parser.add_argument("--ip-address", + action=cli_utils.IPAddressAction, + dest="ip_addresses", + default=[]) + Authenticator.inject_parser_options(self.parser, "webroot") + + def test_webroot_map_action(self): + other_path = tempfile.mkdtemp() + args = self.parser.parse_args( + ["--webroot-map", json.dumps({'thing.com,thunk.com,9.8.7.6': self.path,'thunk.com': other_path})]) + assert args.webroot_map["thing.com"] == self.path + assert args.webroot_map["9.8.7.6"] == self.path + assert args.webroot_map["thunk.com"] == other_path + + def test_domain_before_webroot(self): + args = self.parser.parse_args( + "-d {0} -w {1}".format(self.achall.identifier.value, self.path).split()) + config = self._get_config_after_perform(args) + assert config.webroot_map[self.achall.identifier.value] == self.path + + def test_multi_identifier(self): + args = self.parser.parse_args( + "-w {0} -d {1} --ip-address {2}".format( + self.path, self.achall.identifier.value, self.ipchall.identifier.value).split()) + + config = self._get_config_after_perform(args, challs=[self.achall, self.ipchall]) + assert config.webroot_map[self.achall.identifier.value] == self.path + assert config.webroot_map[self.ipchall.identifier.value] == self.path + + def test_domain_before_webroot_error(self): + with pytest.raises(errors.PluginError): + self.parser.parse_args("-d foo -w bar -w baz".split()) + with pytest.raises(errors.PluginError): + self.parser.parse_args("-d foo -w bar -d baz -w qux".split()) + + def test_multiwebroot(self): + ip = self.ipchall.identifier.value + dns_name = self.achall.identifier.value + + ip_path = tempfile.mkdtemp() + dns_path = tempfile.mkdtemp() + args = self.parser.parse_args(f"-w {dns_path} -d {dns_name} -w {ip_path} --ip-address {ip}".split()) + config = self._get_config_after_perform(args, challs=[self.achall, self.ipchall]) + assert config.webroot_map[dns_name] == dns_path + assert config.webroot_map[ip] == ip_path + + def test_multiwebroot_ip_first(self): + ip = self.ipchall.identifier.value + dns_name = self.achall.identifier.value + + ip_path = tempfile.mkdtemp() + dns_path = tempfile.mkdtemp() + args = self.parser.parse_args(f"-w {ip_path} --ip-address {ip} -w {dns_path} -d {dns_name}".split()) + config = self._get_config_after_perform(args, challs=[self.achall, self.ipchall]) + assert config.webroot_map[dns_name] == dns_path + assert config.webroot_map[ip] == ip_path + + def test_webroot_map_partial_without_perform(self): + # This test acknowledges the fact that webroot_map content will be partial if webroot + # plugin perform method is not invoked (corner case when all auths are already valid). + # To not be a problem, the webroot_path must always been conserved during renew. + # This condition is challenged by: + # certbot.tests.renewal_tests::RenewalTest::test_webroot_params_conservation + # See https://github.com/certbot/certbot/pull/7095 for details. + other_webroot_path = tempfile.mkdtemp() + args = self.parser.parse_args("-w {0} -d {1} -w {2} -d bar".format( + self.path, self.achall.identifier.value, other_webroot_path).split()) + assert args.webroot_map == {self.achall.identifier.value: self.path} + assert args.webroot_path == [self.path, other_webroot_path] + + def _get_config_after_perform(self, config, challs=None): + if not challs: + challs = [self.achall] + from certbot._internal.plugins.webroot import Authenticator + auth = Authenticator(config, "webroot") + auth.perform(challs) + return auth.config + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/renewal_test.py b/certbot/src/certbot/_internal/tests/renewal_test.py new file mode 100644 index 00000000000..c78099b48b3 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/renewal_test.py @@ -0,0 +1,698 @@ +"""Tests for certbot._internal.renewal""" +import copy +import datetime +import sys +import tempfile +import unittest +from unittest import mock + +import configobj +import pytest + +from acme import challenges, errors as acme_errors +from certbot import configuration +from certbot import errors +from certbot._internal import storage +import certbot.tests.util as test_util + +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.hazmat.primitives import serialization, hashes +from cryptography import x509 + +def make_cert_with_lifetime(not_before: datetime.datetime, lifetime_days: int) -> bytes: + """Return PEM of a self-signed certificate with the given notBefore and lifetime.""" + key = ec.generate_private_key(ec.SECP256R1()) + not_after=not_before + datetime.timedelta(days=lifetime_days) + cert = x509.CertificateBuilder( + issuer_name=x509.Name([]), + subject_name=x509.Name([]), + public_key=key.public_key(), + serial_number=x509.random_serial_number(), + not_valid_before=not_before, + not_valid_after=not_after, + ).add_extension( + x509.SubjectAlternativeName([x509.DNSName("example.com")]), + critical=False, + ).sign( + private_key=key, + algorithm=hashes.SHA256(), + ) + return cert.public_bytes(serialization.Encoding.PEM) + +class RenewalTest(test_util.ConfigTestCase): + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_ancient_webroot_renewal_conf(self, mock_set_by_user): + mock_set_by_user.return_value = False + rc_path = test_util.make_lineage( + self.config.config_dir, 'sample-renewal-ancient.conf') + self.config.account = None + self.config.email = None + self.config.webroot_path = None + config = configuration.NamespaceConfig(self.config) + lineage = storage.RenewableCert(rc_path, config) + renewalparams = lineage.configuration['renewalparams'] + # pylint: disable=protected-access + from certbot._internal import renewal + renewal._restore_webroot_config(config, renewalparams) + assert config.webroot_path == ['/var/www/'] + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_webroot_params_conservation(self, mock_set_by_user): + # For more details about why this test is important, see: + # certbot._internal.plugins.webroot_test:: + # WebrootActionTest::test_webroot_map_partial_without_perform + from certbot._internal import renewal + mock_set_by_user.return_value = False + + renewalparams = { + 'webroot_map': {'test.example.com': '/var/www/test'}, + 'webroot_path': ['/var/www/test', '/var/www/other'], + } + renewal._restore_webroot_config(self.config, renewalparams) # pylint: disable=protected-access + assert self.config.webroot_map == {'test.example.com': '/var/www/test'} + assert self.config.webroot_path == ['/var/www/test', '/var/www/other'] + + renewalparams = { + 'webroot_map': {}, + 'webroot_path': '/var/www/test', + } + renewal._restore_webroot_config(self.config, renewalparams) # pylint: disable=protected-access + assert self.config.webroot_map == {} + assert self.config.webroot_path == ['/var/www/test'] + + @mock.patch('certbot._internal.renewal._avoid_reuse_key_conflicts') + def test_reuse_key_renewal_params(self, unused_mock_avoid_reuse_conflicts): + self.config.elliptic_curve = 'INVALID_VALUE' + self.config.reuse_key = True + self.config.dry_run = True + config = configuration.NamespaceConfig(self.config) + + rc_path = test_util.make_lineage( + self.config.config_dir, 'sample-renewal.conf') + lineage = storage.RenewableCert(rc_path, config) + + le_client = mock.MagicMock() + le_client.obtain_certificate.return_value = (None, None, None, None) + + from certbot._internal import renewal + + with mock.patch('certbot._internal.renewal.hooks.deploy_hook'): + renewal.renew_cert(self.config, None, le_client, lineage) + + assert self.config.elliptic_curve == 'secp256r1' + + @mock.patch('certbot._internal.renewal._avoid_reuse_key_conflicts') + def test_reuse_ec_key_renewal_params(self, unused_mock_avoid_reuse_conflicts): + self.config.elliptic_curve = 'INVALID_CURVE' + self.config.reuse_key = True + self.config.dry_run = True + self.config.key_type = 'ecdsa' + config = configuration.NamespaceConfig(self.config) + + rc_path = test_util.make_lineage( + self.config.config_dir, + 'sample-renewal-ec.conf', + ec=True, + ) + lineage = storage.RenewableCert(rc_path, config) + + le_client = mock.MagicMock() + le_client.obtain_certificate.return_value = (None, None, None, None) + + from certbot._internal import renewal + + with mock.patch('certbot._internal.renewal.hooks.deploy_hook'): + renewal.renew_cert(self.config, None, le_client, lineage) + + assert self.config.elliptic_curve == 'secp256r1' + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_new_key(self, mock_set_by_user): + mock_set_by_user.return_value = False + # When renewing with both reuse_key and new_key, the key should be regenerated, + # the key type, key parameters and reuse_key should be kept. + self.config.reuse_key = True + self.config.new_key = True + self.config.dry_run = True + config = configuration.NamespaceConfig(self.config) + + rc_path = test_util.make_lineage( + self.config.config_dir, 'sample-renewal.conf') + lineage = storage.RenewableCert(rc_path, config) + + le_client = mock.MagicMock() + le_client.obtain_certificate.return_value = (None, None, None, None) + + from certbot._internal import renewal + + with mock.patch('certbot._internal.renewal.hooks.deploy_hook'): + renewal.renew_cert(self.config, None, le_client, lineage) + + assert self.config.elliptic_curve == 'secp256r1' + assert self.config.key_type == 'ecdsa' + assert self.config.reuse_key + # None is passed as the existing key, i.e. the key is not actually being reused. + le_client.obtain_certificate.assert_called_with(mock.ANY, None) + + @mock.patch('certbot._internal.renewal.hooks.deploy_hook') + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_reuse_key_conflicts(self, mock_set_by_user, unused_mock_deploy_hook): + mock_set_by_user.return_value = False + + # When renewing with reuse_key and a conflicting key parameter (size, curve) + # an error should be raised ... + self.config.reuse_key = True + self.config.key_type = "rsa" + self.config.rsa_key_size = 4096 + self.config.dry_run = True + + config = configuration.NamespaceConfig(self.config) + + rc_path = test_util.make_lineage( + self.config.config_dir, 'sample-renewal.conf') + lineage = storage.RenewableCert(rc_path, config) + lineage.configuration["renewalparams"]["reuse_key"] = True + + le_client = mock.MagicMock() + le_client.obtain_certificate.return_value = (None, None, None, None) + + from certbot._internal import renewal + + with pytest.raises(errors.Error, match="Unable to change the --key-type"): + renewal.renew_cert(self.config, None, le_client, lineage) + + # ... unless --no-reuse-key is set + mock_set_by_user.side_effect = lambda var: var == "reuse_key" + self.config.reuse_key = False + renewal.renew_cert(self.config, None, le_client, lineage) + + @test_util.patch_display_util() + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_remove_deprecated_config_elements(self, mock_set_by_user, unused_mock_get_utility): + mock_set_by_user.return_value = False + config = configuration.NamespaceConfig(self.config) + config.certname = "sample-renewal-deprecated-option" + + rc_path = test_util.make_lineage( + self.config.config_dir, 'sample-renewal-deprecated-option.conf') + + from certbot._internal import renewal + lineage_config = copy.deepcopy(self.config) + renewal.reconstitute(lineage_config, rc_path) + # This means that manual_public_ip_logging_ok was not modified in the config based on its + # value in the renewal conf file + assert isinstance(lineage_config.manual_public_ip_logging_ok, mock.MagicMock) + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_absent_key_type_restored(self, mock_set_by_user): + mock_set_by_user.return_value = False + + rc_path = test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf', ec=False) + + from certbot._internal import renewal + lineage_config = copy.deepcopy(self.config) + renewal.reconstitute(lineage_config, rc_path) + assert lineage_config.key_type == 'rsa' + + @test_util.patch_display_util() + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + @mock.patch('certbot._internal.renewal.AriClientPool.get') + @mock.patch('certbot._internal.main.renew_cert') + @mock.patch("certbot._internal.renewal.datetime") + def test_renewal_via_ari(self, mock_datetime, mock_renew_cert, mock_ari_client_get, mock_set_by_user, unused_mock_display): + mock_set_by_user.return_value = False + from certbot._internal import renewal + acme_client = mock.MagicMock() + mock_ari_client_get.return_value = acme_client + past = datetime.datetime(2025, 3, 19, 0, 0, 0, tzinfo=datetime.timezone.utc) + now = datetime.datetime(2025, 4, 19, 0, 0, 0, tzinfo=datetime.timezone.utc) + future = datetime.datetime(2025, 4, 19, 12, 0, 0, tzinfo=datetime.timezone.utc) + mock_datetime.datetime.now.return_value = now + acme_client.renewal_time.return_value = past, future + + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf', ec=False) + config = configuration.NamespaceConfig(self.config) + + with mock.patch('time.sleep'): + renewal.handle_renewal_request(config) + + mock_renew_cert.assert_called_once() + # This value comes from `sample-renewal.conf` and is different than + # the global default. + expected_server = "https://acme-staging-v02.api.letsencrypt.org/directory" + assert expected_server != config.server + mock_ari_client_get.assert_called_once() + assert mock_ari_client_get.call_args[0][0] == expected_server + + @test_util.patch_display_util() + @mock.patch('acme.client.ClientNetwork.get') + @mock.patch('certbot._internal.storage.RenewableCert.autorenewal_is_enabled') + def test_no_network_if_no_autorenew(self, mock_autorenewal_enabled, + mock_client_network_get, unused_mock_display): + from certbot._internal import renewal + mock_autorenewal_enabled.return_value = False + + test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf', ec=False) + + with mock.patch('time.sleep'): + renewal.handle_renewal_request(self.config) + + assert mock_client_network_get.call_count == 0 + + @mock.patch('acme.client.ClientV2') + def test_dry_run_no_ari_call(self, mock_acme): + from certbot._internal import renewal + self.config.dry_run = True + ari_client_pool = mock.MagicMock() + ari_client_pool.get.return_value = mock_acme + with mock.patch('time.sleep'): + renewal.should_renew(self.config, mock.Mock(), ari_client_pool) + assert mock_acme.renewal_time.call_count == 0 + + def test_default_renewal_time(self): + from certbot._internal import renewal + cert_pem = make_cert_with_lifetime(datetime.datetime(2025, 3, 12, 00, 00, 00), 8) + t = renewal._default_renewal_time(cert_pem) + assert t == datetime.datetime(2025, 3, 16, 00, 00, 00, tzinfo=datetime.timezone.utc) + + cert_pem = make_cert_with_lifetime(datetime.datetime(2025, 3, 12, 00, 00, 00), 18) + t = renewal._default_renewal_time(cert_pem) + assert t == datetime.datetime(2025, 3, 24, 00, 00, 00, tzinfo=datetime.timezone.utc) + + @mock.patch("certbot._internal.storage.atomic_rewrite") + @mock.patch("certbot._internal.renewal.datetime") + def test_renew_before_expiry(self, mock_datetime, unused_mock_atomic_rewrite): + """When neither OCSP nor the ACME client indicate it's time to renew, + obey the renew_before_expiry config. + """ + from certbot._internal import renewal + + # This certificate has a lifetime of 7 days, and the tests below + # that use a "None" interval (i.e. choose a default) rely on that fact. + # + # Not Before: Dec 11 22:34:45 2014 GMT + # Not After : Dec 18 22:34:45 2014 GMT + not_before = datetime.datetime(2014, 12, 11, 22, 34, 45) + short_cert = make_cert_with_lifetime(not_before, 7) + + ari_server = "http://ari" + future = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=100000) + ari_client_pool = MockAriClientPool(future, future) + + mock_renewable_cert = mock.MagicMock() + mock_renewable_cert.server = ari_server + mock_renewable_cert.autorenewal_is_enabled.return_value = True + mock_renewable_cert.version.return_value = "/tmp/abc" + mock_renewable_cert.ocsp_revoked.return_value = False + mock_renewable_cert.configfile = configobj.ConfigObj() + + mock_datetime.timedelta = datetime.timedelta + + with tempfile.NamedTemporaryFile() as tmp_cert: + tmp_cert.close() # close now because of compatibility issues on Windows + with open(tmp_cert.name, 'wb') as c: + c.write(short_cert) + + mock_renewable_cert.version.return_value = tmp_cert.name + + # First, test cases where ARI returns a renewal_time far in the future + for (current_time, interval, result) in [ + # 2014-12-13 12:00 (about 5 days prior to expiry) + # Times that should result in autorenewal/autodeployment + (1418472000, "2 months", True), (1418472000, "1 week", True), + # With the "default" logic, this 7-day certificate should autorenew + # at 3.5 days prior to expiry. We haven't reached that yet, + # so don't renew. + (1418472000, None, False), + # Times that should not renew + (1418472000, "4 days", False), (1418472000, "2 days", False), + # 2014-12-16 20:00 (after the default renewal time but before expiry) + # Times that should not renew + (1418760000, None, False), + (1418760000, "1 day", False), + # 2009-05-01 12:00:00+00:00 (about 5 years prior to expiry) + # Times that should result in autorenewal/autodeployment + (1241179200, "7 years", True), + (1241179200, "11 years 2 months", True), + # Times that should not renew + (1241179200, "8 hours", False), (1241179200, "2 days", False), + (1241179200, "40 days", False), (1241179200, "9 months", False), + # 2015-01-01 (after expiry has already happened, so all + # intervals should cause autorenewal/autodeployment) + (1420070400, "0 seconds", True), + (1420070400, "10 seconds", True), + (1420070400, "10 minutes", True), + (1420070400, "10 weeks", True), (1420070400, "10 months", True), + (1420070400, "10 years", True), (1420070400, "99 months", True), + ]: + sometime = datetime.datetime.fromtimestamp(current_time, datetime.timezone.utc) + mock_datetime.datetime.now.return_value = sometime + mock_renewable_cert.configuration = {"renew_before_expiry": interval} + assert renewal.should_autorenew(mock_renewable_cert, ari_client_pool) == result, f"at {current_time}, with config '{interval}', ari response in future, expected {result}" + + # Now, test cases where ARI either fails (returns `(None, _)`) or + # the cert has no `server` value and ARI is skipped + ari_client_pool = MockAriClientPool(None, future) + for (current_time, interval, result) in [ + # 2014-12-13 12:00 (about 5 days prior to expiry) + # Times that should result in autorenewal/autodeployment + (1418472000, "2 months", True), (1418472000, "1 week", True), + # With the "default" logic, this 7-day certificate should autorenew + # at 3.5 days prior to expiry. We haven't reached that yet, + # so don't renew. + (1418472000, None, False), + # Times that should not renew + (1418472000, "4 days", False), (1418472000, "2 days", False), + # 2014-12-16 20:00 (after the default renewal time but before expiry) + # Times that should result in autorenewal/autodeployment + (1418760000, None, True), # Note that this result is different from the above + # Times that should not renew + (1418760000, "1 day", False), + # 2009-05-01 12:00:00+00:00 (about 5 years prior to expiry) + # Times that should result in autorenewal/autodeployment + (1241179200, "7 years", True), + (1241179200, "11 years 2 months", True), + # Times that should not renew + (1241179200, "8 hours", False), (1241179200, "2 days", False), + (1241179200, "40 days", False), (1241179200, "9 months", False), + # 2015-01-01 (after expiry has already happened, so all + # intervals should cause autorenewal/autodeployment) + (1420070400, "0 seconds", True), + (1420070400, "10 seconds", True), + (1420070400, "10 minutes", True), + (1420070400, "10 weeks", True), (1420070400, "10 months", True), + (1420070400, "10 years", True), (1420070400, "99 months", True), + ]: + sometime = datetime.datetime.fromtimestamp(current_time, datetime.timezone.utc) + mock_datetime.datetime.now.return_value = sometime + mock_renewable_cert.configuration = {"renew_before_expiry": interval} + mock_renewable_cert.server = ari_server + assert renewal.should_autorenew(mock_renewable_cert, ari_client_pool) == result, f"at {current_time}, with config '{interval}', no ari response, expected {result}" + mock_renewable_cert.server = None + assert renewal.should_autorenew(mock_renewable_cert, ari_client_pool) == result, f"at {current_time}, with config '{interval}', skipped ari, expected {result}" + + @mock.patch("certbot._internal.storage.RenewableCert.ocsp_revoked") + def test_should_autorenew(self, mock_ocsp): + from certbot._internal import renewal + + mock_acme = mock.MagicMock() + ari_server = "http://ari" + future = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=1000) + mock_acme.renewal_time.return_value = (future, future) + ari_client_pool = mock.MagicMock() + ari_client_pool.get.return_value = mock_acme + mock_rc = mock.MagicMock() + + with mock.patch('certbot._internal.renewal.open', mock.mock_open(read_data=b'')): + # Autorenewal turned off + mock_rc.autorenewal_is_enabled.return_value = False + mock_rc.server = ari_server + assert not renewal.should_autorenew(mock_rc, ari_client_pool) + mock_rc.server = None + assert not renewal.should_autorenew(mock_rc, ari_client_pool) + + # Autorenewal turned on, mandatory renewal on the basis of OCSP + # revocation + mock_rc.autorenewal_is_enabled.return_value = True + mock_ocsp.return_value = True + assert renewal.should_autorenew(mock_rc, ari_client_pool) + mock_rc.server = None + with mock.patch('certbot._internal.renewal.logger.warning') as mock_warning: + assert renewal.should_autorenew(mock_rc, ari_client_pool) + # Ensure we warned about skipping ARI checks when server is None + assert any(call.args[0].startswith('Skipping ARI check') for call in + mock_warning.call_args_list) + + @mock.patch("certbot._internal.storage.atomic_rewrite") + @mock.patch('certbot._internal.storage.RenewableCert.ocsp_revoked') + @mock.patch('acme.client.ClientV2.renewal_time') + def test_resilient_ari_directory_fetches(self, mock_renewal_time, mock_ocsp, + unused_mock_atomic_rewrite): + from certbot._internal import renewal + from acme import messages + + ari_server = 'http://ari' + ari_client_pool = mock.MagicMock() + ari_client_pool.get.side_effect = messages.Error() + mock_rc = mock.MagicMock() + mock_rc.server = ari_server + mock_rc.configfile = configobj.ConfigObj() + mock_rc.autorenewal_is_enabled.return_value = True + mock_ocsp.return_value = True + + with mock.patch('certbot._internal.renewal.open', mock.mock_open(read_data=b'')): + with mock.patch('certbot._internal.renewal.logger') as mock_logger: + assert renewal.should_autorenew(mock_rc, ari_client_pool) + + assert mock_renewal_time.call_count == 0 + # Ensure we logged about skipping the ARI check and the underlying exception + assert any('ARI' in call.args[0] for call in mock_logger.warning.call_args_list) + assert any(call.kwargs.get('exc_info') for call in mock_logger.debug.call_args_list) + + @mock.patch('certbot._internal.storage.RenewableCert.ocsp_revoked') + def test_resilient_ari_check(self, mock_ocsp): + from certbot._internal import renewal + + rc_path = test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + renewable_cert = storage.RenewableCert(rc_path, self.config) + + ari_error = acme_errors.ARIError('some error', datetime.datetime.now()) + ari_client_pool = MockAriClientPool(None, None) + ari_client_pool.mock_acme.renewal_time.side_effect = ari_error + + mock_ocsp.return_value = True + + with mock.patch('certbot._internal.renewal.open', mock.mock_open(read_data=b'')): + with mock.patch('certbot._internal.renewal.logger') as mock_logger: + assert renewal.should_autorenew(renewable_cert, ari_client_pool) + # Ensure we logged about skipping the ARI check and the underlying exception + assert any('ARI' in call.args[0] for call in mock_logger.warning.call_args_list) + assert any(call.kwargs.get('exc_info') for call in mock_logger.debug.call_args_list) + + def test_stores_ari_retry_after(self): + from certbot._internal import renewal + + rc_path = test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + renewable_cert = storage.RenewableCert(rc_path, self.config) + + renewal_time = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta( + seconds=1000) + retry_after = datetime.datetime.now() + datetime.timedelta(seconds=1000) + mock_ari_client_pool = MockAriClientPool(renewal_time, retry_after) + + # Check for renewal. As a side effect, this should cause the lineage config to be + # updated with 'ari_retry_after' in the renewalparams section. + renewal.should_autorenew(renewable_cert, mock_ari_client_pool) + + with open(renewable_cert.configfile.filename, 'r') as c: + renewable_cert_config = configobj.ConfigObj(c) + + assert renewable_cert_config['acme_renewal_info']['ari_retry_after'] == retry_after.isoformat( + timespec='seconds') + + def test_skips_ari_when_retry_after_future(self): + from certbot._internal import renewal + + rc_path = test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + renewable_cert = storage.RenewableCert(rc_path, self.config) + + future = datetime.datetime.now() + datetime.timedelta(seconds=1000) + storage.atomic_rewrite(rc_path, + {"acme_renewal_info": {"ari_retry_after": future.isoformat(timespec="seconds")}}) + + # ARI shouldn't be checked at all because retry after is in the future. + mock_ari_client_pool = MockAriClientPool(None, None) + mock_ari_client_pool.mock_acme.renewal_time.side_effect = errors.Error("Shouldn't be called") + + # Check for renewal. All we care about here is that renewal_time is not called; if it were, + # an exception would be raised. + renewal.should_autorenew(renewable_cert, mock_ari_client_pool) + + def test_checks_ari_when_retry_after_absent(self): + from certbot._internal import renewal + + rc_path = test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + renewable_cert = storage.RenewableCert(rc_path, self.config) + + renewal_time = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=1000) + retry_after = datetime.datetime.now() + datetime.timedelta(seconds=1000) + mock_ari_client_pool = MockAriClientPool(renewal_time, retry_after) + + # The 'ari_retry_after' field is absent, so renewal_time _should_ be called. + # We don't care about the return value of should_autorenew. + renewal.should_autorenew(renewable_cert, mock_ari_client_pool) + + mock_ari_client_pool.mock_acme.renewal_time.assert_called_once() + + def test_checks_ari_when_retry_after_in_past(self): + from certbot._internal import renewal + + rc_path = test_util.make_lineage(self.config.config_dir, 'sample-renewal.conf') + renewable_cert = storage.RenewableCert(rc_path, self.config) + + past = datetime.datetime.now() - datetime.timedelta(seconds=1000) + storage.atomic_rewrite(rc_path, + {"acme_renewal_info": {"ari_retry_after": past.isoformat(timespec="seconds")}}) + + renewal_time = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=1000) + retry_after = datetime.datetime.now() + datetime.timedelta(seconds=1000) + mock_ari_client_pool = MockAriClientPool(renewal_time, retry_after) + + # The 'ari_retry_after' field is in the past, so renewal_time _should_ be called. + # We don't care about the return value of should_autorenew. + renewal.should_autorenew(renewable_cert, mock_ari_client_pool) + mock_ari_client_pool.mock_acme.renewal_time.assert_called_once() + +class MockAriClientPool: + def __init__(self, renewal_time, retry_after): + self.mock_acme = mock.MagicMock() + self.mock_acme.renewal_time.return_value = (renewal_time, retry_after) + + def get(self, server): + return self.mock_acme + + +class RestoreRequiredConfigElementsTest(test_util.ConfigTestCase): + """Tests for certbot._internal.renewal.restore_required_config_elements.""" + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.renewal import restore_required_config_elements + return restore_required_config_elements(*args, **kwargs) + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_allow_subset_of_names_success(self, mock_set_by_user): + mock_set_by_user.return_value = False + self._call(self.config, {'allow_subset_of_names': 'True'}) + assert self.config.allow_subset_of_names is True + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_allow_subset_of_names_failure(self, mock_set_by_user): + mock_set_by_user.return_value = False + renewalparams = {'allow_subset_of_names': 'maybe'} + with pytest.raises(errors.Error): + self._call(self.config, renewalparams) + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_pref_challs_list(self, mock_set_by_user): + mock_set_by_user.return_value = False + renewalparams = {'pref_challs': 'http-01, dns'.split(',')} + self._call(self.config, renewalparams) + expected = [challenges.HTTP01.typ, challenges.DNS01.typ] + assert self.config.pref_challs == expected + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_pref_challs_str(self, mock_set_by_user): + mock_set_by_user.return_value = False + renewalparams = {'pref_challs': 'dns'} + self._call(self.config, renewalparams) + expected = [challenges.DNS01.typ] + assert self.config.pref_challs == expected + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_pref_challs_failure(self, mock_set_by_user): + mock_set_by_user.return_value = False + renewalparams = {'pref_challs': 'finding-a-shrubbery'} + with pytest.raises(errors.Error): + self._call(self.config, renewalparams) + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_must_staple_success(self, mock_set_by_user): + mock_set_by_user.return_value = False + self._call(self.config, {'must_staple': 'True'}) + assert self.config.must_staple is True + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_must_staple_failure(self, mock_set_by_user): + mock_set_by_user.return_value = False + renewalparams = {'must_staple': 'maybe'} + with pytest.raises(errors.Error): + self._call(self.config, renewalparams) + + @mock.patch.object(configuration.NamespaceConfig, 'set_by_user') + def test_ancient_server_renewal_conf(self, mock_set_by_user): + from certbot._internal import constants + self.config.server = None + mock_set_by_user.return_value = False + self._call(self.config, {'server': constants.V1_URI}) + assert self.config.server == constants.CLI_DEFAULTS['server'] + + def test_related_values(self): + # certbot.configuration.NamespaceConfig.set_by_user considers some values as related to each + # other and considers both set by the user if either is. This test ensures all renewal + # parameters are restored regardless of their restoration order or relation between values. + # See https://github.com/certbot/certbot/issues/9805 for more info. + renewalparams = { + 'server': 'https://example.org', + 'account': 'somehash', + } + self._call(self.config, renewalparams) + self.assertEqual(self.config.account, renewalparams['account']) + + +class DescribeResultsTest(unittest.TestCase): + """Tests for certbot._internal.renewal._renew_describe_results.""" + def setUp(self): + self.patchers = { + 'log_error': mock.patch('certbot._internal.renewal.logger.error'), + 'notify': mock.patch('certbot._internal.renewal.display_util.notify')} + self.mock_notify = self.patchers['notify'].start() + self.mock_error = self.patchers['log_error'].start() + + def tearDown(self): + for patch in self.patchers.values(): + patch.stop() + + @classmethod + def _call(cls, *args, **kwargs): + from certbot._internal.renewal import _renew_describe_results + _renew_describe_results(*args, **kwargs) + + def _assert_success_output(self, lines): + self.mock_notify.assert_has_calls([mock.call(l) for l in lines]) + + def test_no_renewal_attempts(self): + self._call(mock.MagicMock(dry_run=True), [], [], [], []) + self._assert_success_output(['No simulated renewals were attempted.']) + + def test_successful_renewal(self): + self._call(mock.MagicMock(dry_run=False), ['good.pem'], None, None, None) + self._assert_success_output([ + '\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -', + 'Congratulations, all renewals succeeded: ', + ' good.pem (success)', + '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -', + ]) + + def test_failed_renewal(self): + self._call(mock.MagicMock(dry_run=False), [], ['bad.pem'], [], []) + self._assert_success_output([ + '\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -', + '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -', + ]) + self.mock_error.assert_has_calls([ + mock.call('All %ss failed. The following certificates could not be renewed:', 'renewal'), + mock.call(' bad.pem (failure)'), + ]) + + def test_all_renewal(self): + self._call(mock.MagicMock(dry_run=True), + ['good.pem', 'good2.pem'], ['bad.pem', 'bad2.pem'], + ['foo.pem expires on 123'], ['errored.conf']) + self._assert_success_output([ + '\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -', + 'The following certificates are not due for renewal yet:', + ' foo.pem expires on 123 (skipped)', + 'The following simulated renewals succeeded:', + ' good.pem (success)\n good2.pem (success)\n', + '\nAdditionally, the following renewal configurations were invalid: ', + ' errored.conf (parsefail)', + '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -', + ]) + self.mock_error.assert_has_calls([ + mock.call('The following %ss failed:', 'simulated renewal'), + mock.call(' bad.pem (failure)\n bad2.pem (failure)'), + ]) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/renewupdater_test.py b/certbot/src/certbot/_internal/tests/renewupdater_test.py new file mode 100644 index 00000000000..31154c13e7b --- /dev/null +++ b/certbot/src/certbot/_internal/tests/renewupdater_test.py @@ -0,0 +1,125 @@ +"""Tests for renewal updater interfaces""" +import sys +from unittest import mock + +import pytest + +from certbot import interfaces +from certbot._internal import main +from certbot._internal import updater +from certbot.plugins import enhancements +import certbot.tests.util as test_util + + +class RenewUpdaterTest(test_util.ConfigTestCase): + """Tests for interfaces.RenewDeployer and interfaces.GenericUpdater""" + + def setUp(self): + super().setUp() + self.generic_updater = mock.MagicMock(spec=interfaces.GenericUpdater) + self.generic_updater.restart = mock.MagicMock() + self.renew_deployer = mock.MagicMock(spec=interfaces.RenewDeployer) + self.mockinstaller = mock.MagicMock(spec=enhancements.AutoHSTSEnhancement) + + @mock.patch('certbot._internal.main._get_and_save_cert') + @mock.patch('certbot._internal.plugins.selection.choose_configurator_plugins') + @mock.patch('certbot._internal.plugins.selection.get_unprepared_installer') + @test_util.patch_display_util() + def test_server_updates(self, _, mock_geti, mock_select, mock_getsave): + mock_getsave.return_value = mock.MagicMock() + mock_generic_updater = self.generic_updater + + # Generic Updater + mock_select.return_value = (mock_generic_updater, None) + mock_geti.return_value = mock_generic_updater + with mock.patch('certbot._internal.main._init_le_client'): + main.renew_cert(self.config, None, mock.MagicMock()) + assert mock_generic_updater.restart.called + + mock_generic_updater.restart.reset_mock() + mock_generic_updater.generic_updates.reset_mock() + updater.run_generic_updaters(self.config, mock.MagicMock(), None) + assert mock_generic_updater.generic_updates.call_count == 1 + assert mock_generic_updater.restart.called is False + + def test_renew_deployer(self): + lineage = mock.MagicMock() + mock_deployer = self.renew_deployer + updater.run_renewal_deployer(self.config, lineage, mock_deployer) + mock_deployer.renew_deploy.assert_called_with(lineage) + + @mock.patch("certbot._internal.updater.logger.debug") + def test_updater_skip_dry_run(self, mock_log): + self.config.dry_run = True + updater.run_generic_updaters(self.config, None, None) + assert mock_log.called + assert mock_log.call_args[0][0] == \ + "Skipping updaters in dry-run mode." + + @mock.patch("certbot._internal.updater.logger.debug") + def test_deployer_skip_dry_run(self, mock_log): + self.config.dry_run = True + updater.run_renewal_deployer(self.config, None, None) + assert mock_log.called + assert mock_log.call_args[0][0] == \ + "Skipping renewal deployer in dry-run mode." + + @mock.patch('certbot._internal.plugins.selection.get_unprepared_installer') + def test_enhancement_updates(self, mock_geti): + mock_geti.return_value = self.mockinstaller + updater.run_generic_updaters(self.config, mock.MagicMock(), None) + assert self.mockinstaller.update_autohsts.called + assert self.mockinstaller.update_autohsts.call_count == 1 + + def test_enhancement_deployer(self): + updater.run_renewal_deployer(self.config, mock.MagicMock(), + self.mockinstaller) + assert self.mockinstaller.deploy_autohsts.called + + @mock.patch('certbot._internal.plugins.selection.get_unprepared_installer') + def test_enhancement_updates_not_called(self, mock_geti): + self.config.disable_renew_updates = True + mock_geti.return_value = self.mockinstaller + updater.run_generic_updaters(self.config, mock.MagicMock(), None) + assert self.mockinstaller.update_autohsts.called is False + + def test_enhancement_deployer_not_called(self): + self.config.disable_renew_updates = True + updater.run_renewal_deployer(self.config, mock.MagicMock(), + self.mockinstaller) + assert self.mockinstaller.deploy_autohsts.called is False + + @mock.patch('certbot._internal.plugins.selection.get_unprepared_installer') + def test_enhancement_no_updater(self, mock_geti): + FAKEINDEX = [ + { + "name": "Test", + "class": enhancements.AutoHSTSEnhancement, + "updater_function": None, + "deployer_function": "deploy_autohsts", + "enable_function": "enable_autohsts" + } + ] + mock_geti.return_value = self.mockinstaller + with mock.patch("certbot.plugins.enhancements._INDEX", FAKEINDEX): + updater.run_generic_updaters(self.config, mock.MagicMock(), None) + assert self.mockinstaller.update_autohsts.called is False + + def test_enhancement_no_deployer(self): + FAKEINDEX = [ + { + "name": "Test", + "class": enhancements.AutoHSTSEnhancement, + "updater_function": "deploy_autohsts", + "deployer_function": None, + "enable_function": "enable_autohsts" + } + ] + with mock.patch("certbot.plugins.enhancements._INDEX", FAKEINDEX): + updater.run_renewal_deployer(self.config, mock.MagicMock(), + self.mockinstaller) + assert self.mockinstaller.deploy_autohsts.called is False + + +if __name__ == '__main__': + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/reverter_test.py b/certbot/src/certbot/_internal/tests/reverter_test.py new file mode 100644 index 00000000000..50cdd68c996 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/reverter_test.py @@ -0,0 +1,454 @@ +"""Test certbot.reverter.""" +import csv +import logging +import shutil +import sys +import tempfile +from unittest import mock + +import pytest + +from certbot import errors +from certbot.compat import os +from certbot.tests import util as test_util + + +class ReverterCheckpointLocalTest(test_util.ConfigTestCase): + """Test the Reverter Class.""" + def setUp(self): + super().setUp() + from certbot.reverter import Reverter + + # Disable spurious errors... we are trying to test for them + logging.disable(logging.CRITICAL) + + self.reverter = Reverter(self.config) + + tup = setup_test_files() + self.config1, self.config2, self.dir1, self.dir2, self.sets = tup + + def tearDown(self): + shutil.rmtree(self.config.work_dir) + shutil.rmtree(self.dir1) + shutil.rmtree(self.dir2) + + logging.disable(logging.NOTSET) + + @mock.patch("certbot.reverter.Reverter._read_and_append") + def test_no_change(self, mock_read): + mock_read.side_effect = OSError("cannot even") + try: + self.reverter.add_to_checkpoint(self.sets[0], "save1") + except OSError: + pass + self.reverter.finalize_checkpoint("blah") + path = os.listdir(self.reverter.config.backup_dir)[0] + no_change = os.path.join(self.reverter.config.backup_dir, path, "CHANGES_SINCE") + with open(no_change, "r") as f: + x = f.read() + assert "No changes" in x + + def test_basic_add_to_temp_checkpoint(self): + # These shouldn't conflict even though they are both named config.txt + self.reverter.add_to_temp_checkpoint(self.sets[0], "save1") + self.reverter.add_to_temp_checkpoint(self.sets[1], "save2") + + assert os.path.isdir(self.config.temp_checkpoint_dir) + assert get_save_notes( + self.config.temp_checkpoint_dir) == "save1save2" + assert not os.path.isfile( + os.path.join(self.config.temp_checkpoint_dir, "NEW_FILES")) + + assert get_filepaths(self.config.temp_checkpoint_dir) == \ + "{0}\n{1}\n".format(self.config1, self.config2) + + def test_add_to_checkpoint_copy_failure(self): + with mock.patch("certbot.reverter.shutil.copy2") as mock_copy2: + mock_copy2.side_effect = OSError("bad copy") + with pytest.raises(errors.ReverterError): + self.reverter.add_to_checkpoint(self.sets[0], "save1") + + def test_checkpoint_conflict(self): + """Make sure that checkpoint errors are thrown appropriately.""" + config3 = os.path.join(self.dir1, "config3.txt") + self.reverter.register_file_creation(True, config3) + update_file(config3, "This is a new file!") + + self.reverter.add_to_checkpoint(self.sets[2], "save1") + # This shouldn't throw an error + self.reverter.add_to_temp_checkpoint(self.sets[0], "save2") + # Raise error + with pytest.raises(errors.ReverterError): + self.reverter.add_to_checkpoint(self.sets[2], "save3") + # Should not cause an error + self.reverter.add_to_checkpoint(self.sets[1], "save4") + + # Check to make sure new files are also checked... + with pytest.raises(errors.ReverterError): + self.reverter.add_to_checkpoint({config3}, "invalid save") + + def test_multiple_saves_and_temp_revert(self): + self.reverter.add_to_temp_checkpoint(self.sets[0], "save1") + update_file(self.config1, "updated-directive") + self.reverter.add_to_temp_checkpoint(self.sets[0], "save2-updated dir") + update_file(self.config1, "new directive change that we won't keep") + + self.reverter.revert_temporary_config() + assert read_in(self.config1) == "directive-dir1" + + def test_multiple_registration_fail_and_revert(self): + + config3 = os.path.join(self.dir1, "config3.txt") + update_file(config3, "Config3") + config4 = os.path.join(self.dir2, "config4.txt") + update_file(config4, "Config4") + + # Test multiple registrations and two registrations at once + self.reverter.register_file_creation(True, self.config1) + self.reverter.register_file_creation(True, self.config2) + self.reverter.register_file_creation(True, config3, config4) + + # Simulate Certbot crash... recovery routine is run + self.reverter.recovery_routine() + + assert not os.path.isfile(self.config1) + assert not os.path.isfile(self.config2) + assert not os.path.isfile(config3) + assert not os.path.isfile(config4) + + def test_multiple_registration_same_file(self): + self.reverter.register_file_creation(True, self.config1) + self.reverter.register_file_creation(True, self.config1) + self.reverter.register_file_creation(True, self.config1) + self.reverter.register_file_creation(True, self.config1) + + files = get_new_files(self.config.temp_checkpoint_dir) + + assert len(files) == 1 + + def test_register_file_creation_write_error(self): + m_open = mock.mock_open() + with mock.patch("certbot.reverter.open", m_open, create=True): + m_open.side_effect = OSError("bad open") + with pytest.raises(errors.ReverterError): + self.reverter.register_file_creation(True, self.config1) + + def test_bad_registration(self): + # Made this mistake and want to make sure it doesn't happen again... + with pytest.raises(errors.ReverterError): + self.reverter.register_file_creation("filepath") + + def test_register_undo_command(self): + coms = [ + ["a2dismod", "ssl"], + ["a2dismod", "rewrite"], + ["cleanslate"] + ] + for com in coms: + self.reverter.register_undo_command(True, com) + + act_coms = get_undo_commands(self.config.temp_checkpoint_dir) + + for a_com, com in zip(act_coms, coms): + assert a_com == com + + def test_bad_register_undo_command(self): + m_open = mock.mock_open() + with mock.patch("certbot.reverter.open", m_open, create=True): + m_open.side_effect = OSError("bad open") + with pytest.raises(errors.ReverterError): + self.reverter.register_undo_command(True, ["command"]) + + @mock.patch("certbot.util.run_script") + def test_run_undo_commands(self, mock_run): + mock_run.side_effect = ["", errors.SubprocessError] + coms = [ + ["invalid_command"], + ["a2dismod", "ssl"], + ] + for com in coms: + self.reverter.register_undo_command(True, com) + + self.reverter.revert_temporary_config() + + assert mock_run.call_count == 2 + + def test_recovery_routine_in_progress_failure(self): + self.reverter.add_to_checkpoint(self.sets[0], "perm save") + + # pylint: disable=protected-access + self.reverter._recover_checkpoint = mock.MagicMock( + side_effect=errors.ReverterError) + with pytest.raises(errors.ReverterError): + self.reverter.recovery_routine() + + def test_recover_checkpoint_revert_temp_failures(self): + + mock_recover = mock.MagicMock( + side_effect=errors.ReverterError("e")) + + # pylint: disable=protected-access + self.reverter._recover_checkpoint = mock_recover + + self.reverter.add_to_temp_checkpoint(self.sets[0], "config1 save") + + with pytest.raises(errors.ReverterError): + self.reverter.revert_temporary_config() + + def test_recover_checkpoint_rollback_failure(self): + mock_recover = mock.MagicMock( + side_effect=errors.ReverterError("e")) + # pylint: disable=protected-access + self.reverter._recover_checkpoint = mock_recover + + self.reverter.add_to_checkpoint(self.sets[0], "config1 save") + self.reverter.finalize_checkpoint("Title") + + with pytest.raises(errors.ReverterError): + self.reverter.rollback_checkpoints(1) + + def test_recover_checkpoint_copy_failure(self): + self.reverter.add_to_temp_checkpoint(self.sets[0], "save1") + + with mock.patch("certbot.reverter.shutil.copy2") as mock_copy2: + mock_copy2.side_effect = OSError("bad copy") + with pytest.raises(errors.ReverterError): + self.reverter.revert_temporary_config() + + def test_recover_checkpoint_rm_failure(self): + self.reverter.add_to_temp_checkpoint(self.sets[0], "temp save") + + with mock.patch("certbot.reverter.shutil.rmtree") as mock_rmtree: + mock_rmtree.side_effect = OSError("Cannot remove tree") + with pytest.raises(errors.ReverterError): + self.reverter.revert_temporary_config() + + @mock.patch("certbot.reverter.logger.warning") + def test_recover_checkpoint_missing_new_files(self, mock_warn): + self.reverter.register_file_creation( + True, os.path.join(self.dir1, "missing_file.txt")) + self.reverter.revert_temporary_config() + assert mock_warn.call_count == 1 + + @mock.patch("certbot.reverter.os.remove") + def test_recover_checkpoint_remove_failure(self, mock_remove): + self.reverter.register_file_creation(True, self.config1) + mock_remove.side_effect = OSError("Can't remove") + with pytest.raises(errors.ReverterError): + self.reverter.revert_temporary_config() + + def test_recovery_routine_temp_and_perm(self): + # Register a new perm checkpoint file + config3 = os.path.join(self.dir1, "config3.txt") + self.reverter.register_file_creation(False, config3) + update_file(config3, "This is a new perm file!") + + # Add changes to perm checkpoint + self.reverter.add_to_checkpoint(self.sets[0], "perm save1") + update_file(self.config1, "updated perm config1") + self.reverter.add_to_checkpoint(self.sets[1], "perm save2") + update_file(self.config2, "updated perm config2") + + # Add changes to a temporary checkpoint + self.reverter.add_to_temp_checkpoint(self.sets[0], "temp save1") + update_file(self.config1, "second update now temp config1") + + # Register a new temp checkpoint file + config4 = os.path.join(self.dir2, "config4.txt") + self.reverter.register_file_creation(True, config4) + update_file(config4, "New temporary file!") + + # Now erase everything + self.reverter.recovery_routine() + + # Now Run tests + # These were new files.. they should be removed + assert not os.path.isfile(config3) + assert not os.path.isfile(config4) + + # Check to make sure everything got rolled back appropriately + assert read_in(self.config1) == "directive-dir1" + assert read_in(self.config2) == "directive-dir2" + + +class TestFullCheckpointsReverter(test_util.ConfigTestCase): + """Tests functions having to deal with full checkpoints.""" + def setUp(self): + super().setUp() + from certbot.reverter import Reverter + + # Disable spurious errors... + logging.disable(logging.CRITICAL) + + self.reverter = Reverter(self.config) + + tup = setup_test_files() + self.config1, self.config2, self.dir1, self.dir2, self.sets = tup + + def tearDown(self): + shutil.rmtree(self.config.work_dir) + shutil.rmtree(self.dir1) + shutil.rmtree(self.dir2) + + logging.disable(logging.NOTSET) + + def test_rollback_improper_inputs(self): + with pytest.raises(errors.ReverterError): + self.reverter.rollback_checkpoints("-1") + with pytest.raises(errors.ReverterError): + self.reverter.rollback_checkpoints(-1000) + with pytest.raises(errors.ReverterError): + self.reverter.rollback_checkpoints("one") + + def test_rollback_finalize_checkpoint_valid_inputs(self): + + config3 = self._setup_three_checkpoints() + + # Check resulting backup directory + assert len(os.listdir(self.config.backup_dir)) == 3 + # Check rollbacks + # First rollback + self.reverter.rollback_checkpoints(1) + assert read_in(self.config1) == "update config1" + assert read_in(self.config2) == "update config2" + # config3 was not included in checkpoint + assert read_in(config3) == "Final form config3" + + # Second rollback + self.reverter.rollback_checkpoints(1) + assert read_in(self.config1) == "update config1" + assert read_in(self.config2) == "directive-dir2" + assert not os.path.isfile(config3) + + # One dir left... check title + all_dirs = os.listdir(self.config.backup_dir) + assert len(all_dirs) == 1 + assert "First Checkpoint" in get_save_notes( + os.path.join(self.config.backup_dir, all_dirs[0])) + # Final rollback + self.reverter.rollback_checkpoints(1) + assert read_in(self.config1) == "directive-dir1" + + def test_finalize_checkpoint_no_in_progress(self): + # No need to warn for this... just make sure there are no errors. + self.reverter.finalize_checkpoint("No checkpoint...") + + @mock.patch("certbot.reverter.shutil.move") + def test_finalize_checkpoint_cannot_title(self, mock_move): + self.reverter.add_to_checkpoint(self.sets[0], "perm save") + mock_move.side_effect = OSError("cannot move") + + with pytest.raises(errors.ReverterError): + self.reverter.finalize_checkpoint("Title") + + @mock.patch("certbot.reverter.filesystem.replace") + def test_finalize_checkpoint_no_rename_directory(self, mock_replace): + + self.reverter.add_to_checkpoint(self.sets[0], "perm save") + mock_replace.side_effect = OSError + + with pytest.raises(errors.ReverterError): + self.reverter.finalize_checkpoint("Title") + + @mock.patch("certbot.reverter.logger") + def test_rollback_too_many(self, mock_logger): + # Test no exist warning... + self.reverter.rollback_checkpoints(1) + assert mock_logger.warning.call_count == 1 + + # Test Generic warning + self._setup_three_checkpoints() + mock_logger.warning.reset_mock() + self.reverter.rollback_checkpoints(4) + assert mock_logger.warning.call_count == 1 + + def test_multi_rollback(self): + config3 = self._setup_three_checkpoints() + self.reverter.rollback_checkpoints(3) + + assert read_in(self.config1) == "directive-dir1" + assert read_in(self.config2) == "directive-dir2" + assert not os.path.isfile(config3) + + def _setup_three_checkpoints(self): + """Generate some finalized checkpoints.""" + # Checkpoint1 - config1 + self.reverter.add_to_checkpoint(self.sets[0], "first save") + self.reverter.finalize_checkpoint("First Checkpoint") + + update_file(self.config1, "update config1") + + # Checkpoint2 - new file config3, update config2 + config3 = os.path.join(self.dir1, "config3.txt") + self.reverter.register_file_creation(False, config3) + update_file(config3, "directive-config3") + self.reverter.add_to_checkpoint(self.sets[1], "second save") + self.reverter.finalize_checkpoint("Second Checkpoint") + + update_file(self.config2, "update config2") + update_file(config3, "update config3") + + # Checkpoint3 - update config1, config2 + self.reverter.add_to_checkpoint(self.sets[2], "third save") + self.reverter.finalize_checkpoint("Third Checkpoint - Save both") + + update_file(self.config1, "Final form config1") + update_file(self.config2, "Final form config2") + update_file(config3, "Final form config3") + + return config3 + + +def setup_test_files(): + """Setup sample configuration files.""" + dir1 = tempfile.mkdtemp("dir1") + dir2 = tempfile.mkdtemp("dir2") + config1 = os.path.join(dir1, "config.txt") + config2 = os.path.join(dir2, "config.txt") + with open(config1, "w") as file_fd: + file_fd.write("directive-dir1") + with open(config2, "w") as file_fd: + file_fd.write("directive-dir2") + + sets = [{config1}, + {config2}, + {config1, config2}] + + return config1, config2, dir1, dir2, sets + + +def get_save_notes(dire): + """Read save notes""" + return read_in(os.path.join(dire, "CHANGES_SINCE")) + + +def get_filepaths(dire): + """Get Filepaths""" + return read_in(os.path.join(dire, "FILEPATHS")) + + +def get_new_files(dire): + """Get new files.""" + return read_in(os.path.join(dire, "NEW_FILES")).splitlines() + + +def get_undo_commands(dire): + """Get new files.""" + with open(os.path.join(dire, "COMMANDS")) as csvfile: + return list(csv.reader(csvfile)) + + +def read_in(path): + """Read in a file, return the str""" + with open(path, "r") as file_fd: + return file_fd.read() + + +def update_file(filename, string): + """Update a file with a new value.""" + with open(filename, "w") as file_fd: + file_fd.write(string) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/san_test.py b/certbot/src/certbot/_internal/tests/san_test.py new file mode 100644 index 00000000000..83190d55427 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/san_test.py @@ -0,0 +1,152 @@ +"""Tests for the san module""" +import ipaddress +from datetime import datetime + +import pytest +import unittest + +from cryptography import x509 +from cryptography.x509.oid import NameOID +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives.asymmetric import ec + +from certbot._internal import san + +class SanTest(unittest.TestCase): + def test_str(self) -> None: + assert str(san.DNSName("example.com")) == "example.com" + assert str(san.IPAddress("192.168.1.1")) == "192.168.1.1" + + def test_hash(self) -> None: + assert hash(san.DNSName("example.com")) == hash(san.DNSName("example.com")) + assert hash(san.DNSName("EXAMPLE.COM.")) == hash(san.DNSName("example.com")) + assert hash(san.IPAddress("192.168.1.1")) == hash(san.IPAddress("192.168.1.1")) + + def test_repr(self) -> None: + assert repr(san.DNSName("example.com")) == "DNS(example.com)" + assert repr(san.IPAddress("192.168.1.1")) == "IP(192.168.1.1)" + + def test_eq(self) -> None: + with pytest.raises(TypeError): + san.DNSName("example.com") == "example.com" # pylint: disable=expression-not-assigned + with pytest.raises(TypeError): + "example.com" == san.DNSName("example.com") # pylint: disable=expression-not-assigned + with pytest.raises(TypeError): + san.IPAddress("192.168.1.1") == "192.168.1.1" # pylint: disable=expression-not-assigned + with pytest.raises(TypeError): + "192.168.1.1" == san.IPAddress("192.168.1.1") # pylint: disable=expression-not-assigned + assert san.DNSName("example.com") == san.DNSName("example.com") + assert san.DNSName("Example.com") == san.DNSName("example.com") + assert san.DNSName("example.com") == san.DNSName("Example.com") + assert san.DNSName("EXAMPLE.COM") == san.DNSName("Example.com") + assert san.DNSName("EXAMPLE.COM.") == san.DNSName("example.com") + + def test_is_wildcard(self) -> None: + assert not san.DNSName("example.com").is_wildcard() + assert not san.DNSName("example.*.com").is_wildcard() + assert san.DNSName("*.example.com").is_wildcard() + assert not san.IPAddress("192.168.1.1").is_wildcard() + + def test_split(self) -> None: + assert san.split([]) == ([], []) + assert san.split([san.IPAddress("192.168.1.1")]) == ([], [san.IPAddress("192.168.1.1")]) + assert san.split([san.DNSName("example.com")]) == ([san.DNSName("example.com")], []) + assert san.split([san.DNSName("example.com"), san.IPAddress("192.168.1.1"), + san.DNSName("example.org"), san.IPAddress("192.168.1.2")]) == ( + [san.DNSName("example.com"), san.DNSName("example.org")], + [san.IPAddress("192.168.1.1"), san.IPAddress("192.168.1.2")]) + + def test_display(self) -> None: + assert san.display([san.DNSName("example.com")]) == "example.com" + assert san.display([san.DNSName("example.com"), san.IPAddress("192.168.1.1")]) \ + == "example.com, 192.168.1.1" + + def test_ip_address(self) -> None: + with pytest.raises(ValueError): + san.IPAddress("example.com") + +class FromX509Test(unittest.TestCase): + def test_csr(self) -> None: + key = ec.generate_private_key(ec.SECP256R1()) + csr = ( + x509.CertificateSigningRequestBuilder() + .subject_name(x509.Name([])) + .add_extension( + x509.SubjectAlternativeName( + [x509.DNSName("example.com"), + x509.IPAddress(ipaddress.ip_address("192.168.1.1"))] + ), + critical=False, + ) + ).sign(key, hashes.SHA256()) + result = san.from_x509(csr.subject, csr.extensions) + assert result == ( + [san.DNSName("example.com")], + [san.IPAddress("192.168.1.1")], + ) + + def test_cert(self) -> None: + key = ec.generate_private_key(ec.SECP256R1()) + cert = ( + x509.CertificateBuilder() + .serial_number(x509.random_serial_number()) + .not_valid_before(datetime.now()) + .not_valid_after(datetime.now()) + .subject_name(x509.Name([])) + .issuer_name(x509.Name([])) + .public_key(key.public_key()) + .add_extension( + x509.SubjectAlternativeName( + [x509.DNSName("example.com"), + x509.IPAddress(ipaddress.ip_address("192.168.1.1"))] + ), + critical=False, + ) + ).sign(key, hashes.SHA256()) + result = san.from_x509(cert.subject, cert.extensions) + assert result == ( + [san.DNSName("example.com")], + [san.IPAddress("192.168.1.1")], + ) + + def test_cn(self) -> None: + key = ec.generate_private_key(ec.SECP256R1()) + csr = ( + x509.CertificateSigningRequestBuilder() + .subject_name(x509.Name([ + x509.NameAttribute(NameOID.COMMON_NAME, "common.example"), + ])) + .add_extension( + x509.SubjectAlternativeName( + [x509.DNSName("example.com"), + x509.IPAddress(ipaddress.ip_address("192.168.1.1"))] + ), + critical=False, + ) + ).sign(key, hashes.SHA256()) + result = san.from_x509(csr.subject, csr.extensions) + assert result == ( + [san.DNSName("common.example"), san.DNSName("example.com")], + [san.IPAddress("192.168.1.1")], + ) + + def test_cn_duplicate(self) -> None: + key = ec.generate_private_key(ec.SECP256R1()) + csr = ( + x509.CertificateSigningRequestBuilder() + .subject_name(x509.Name([ + x509.NameAttribute(NameOID.COMMON_NAME, "example.com"), + ])) + .add_extension( + x509.SubjectAlternativeName( + [x509.DNSName("example.com"), + x509.IPAddress(ipaddress.ip_address("192.168.1.1"))] + ), + critical=False, + ) + ).sign(key, hashes.SHA256()) + result = san.from_x509(csr.subject, csr.extensions) + assert result == ( + [san.DNSName("example.com")], + [san.IPAddress("192.168.1.1")], + ) diff --git a/certbot/src/certbot/_internal/tests/storage_test.py b/certbot/src/certbot/_internal/tests/storage_test.py new file mode 100644 index 00000000000..da3bd25ce6b --- /dev/null +++ b/certbot/src/certbot/_internal/tests/storage_test.py @@ -0,0 +1,913 @@ +"""Tests for certbot._internal.storage.""" +# pylint disable=protected-access +import datetime +import shutil +import stat +import sys +import unittest +from unittest import mock +import zoneinfo + +import configobj +import pytest + +import certbot +from certbot import errors +from certbot._internal.storage import ALL_FOUR +from certbot._internal import san +from certbot.compat import filesystem +from certbot.compat import os +import certbot.tests.util as test_util + + +def unlink_all(rc_object): + """Unlink all four items associated with this RenewableCert.""" + for kind in ALL_FOUR: + os.unlink(getattr(rc_object, kind)) + + +def fill_with_sample_data(rc_object): + """Put dummy data into all four files of this RenewableCert.""" + for kind in ALL_FOUR: + with open(getattr(rc_object, kind), "w") as f: + f.write(kind) + + +class RelevantValuesTest(unittest.TestCase): + """Tests for certbot._internal.storage.relevant_values.""" + + def setUp(self): + self.values = {"server": "example.org", "key_type": "rsa"} + self.mock_config = mock.MagicMock() + self.mock_config.set_by_user = mock.MagicMock() + + def _call(self, values): + from certbot._internal.storage import relevant_values + self.mock_config.to_dict.return_value = values + return relevant_values(self.mock_config) + + @mock.patch("certbot._internal.plugins.disco.PluginsRegistry.find_all") + def test_namespace(self, mock_find_all): + mock_find_all.return_value = ["certbot-foo:bar"] + self.mock_config.set_by_user.return_value = True + + self.values["certbot_foo:bar_baz"] = 42 + assert self._call(self.values.copy()) == self.values + + def test_option_set(self): + self.mock_config.set_by_user.return_value = True + + self.values["allow_subset_of_names"] = True + self.values["authenticator"] = "apache" + self.values["rsa_key_size"] = 1337 + expected_relevant_values = self.values.copy() + self.values["hello"] = "there" + + assert self._call(self.values) == expected_relevant_values + + def test_option_unset(self): + self.mock_config.set_by_user.return_value = False + + expected_relevant_values = self.values.copy() + self.values["rsa_key_size"] = 2048 + + assert self._call(self.values) == expected_relevant_values + + def test_deprecated_item(self): + deprected_option = 'manual_public_ip_logging_ok' + self.mock_config.set_by_user = lambda v: False if v == deprected_option else True + # deprecated items should never be relevant to store + expected_relevant_values = self.values.copy() + self.values[deprected_option] = None + assert self._call(self.values) == expected_relevant_values + self.values[deprected_option] = True + assert self._call(self.values) == expected_relevant_values + self.values[deprected_option] = False + assert self._call(self.values) == expected_relevant_values + + def test_with_real_parser(self): + from certbot._internal.storage import relevant_values + from certbot._internal.plugins import disco + from certbot._internal import cli + from certbot._internal import constants + + PLUGINS = disco.PluginsRegistry.find_all() + namespace = cli.prepare_and_parse_args(PLUGINS, [ + '--allow-subset-of-names', + '--authenticator', 'apache', + '--preferred-profile', 'fancyprofile', + ]) + expected_relevant_values = { + 'server': constants.CLI_DEFAULTS['server'], + 'key_type': 'ecdsa', + 'allow_subset_of_names': True, + 'authenticator': 'apache', + 'preferred_profile': 'fancyprofile', + } + + assert relevant_values(namespace) == expected_relevant_values + + def test_with_required_profile(self): + self.values["required_profile"] = "shortlived" + expected_relevant_values = self.values.copy() + assert self._call(self.values) == expected_relevant_values + +class BaseRenewableCertTest(test_util.ConfigTestCase): + """Base class for setting up Renewable Cert tests. + + .. note:: It may be required to write out self.config for + your test. Check :class:`.cli_test.DuplicateCertTest` for an example. + + """ + + def setUp(self): + from certbot._internal import storage + + super().setUp() + + # TODO: maybe provide NamespaceConfig.make_dirs? + # TODO: main() should create those dirs, c.f. #902 + filesystem.makedirs(os.path.join(self.config.config_dir, "live", "example.org")) + archive_path = os.path.join(self.config.config_dir, "archive", "example.org") + filesystem.makedirs(archive_path) + filesystem.makedirs(os.path.join(self.config.config_dir, "renewal")) + + config_file = configobj.ConfigObj() + for kind in ALL_FOUR: + kind_path = os.path.join(self.config.config_dir, "live", "example.org", + kind + ".pem") + config_file[kind] = kind_path + with open(os.path.join(self.config.config_dir, "live", "example.org", + "README"), 'a'): + pass + config_file["archive"] = archive_path + config_file.filename = os.path.join(self.config.config_dir, "renewal", + "example.org.conf") + config_file.write() + self.config_file = config_file + + # We also create a file that isn't a renewal config in the same + # location to test that logic that reads in all-and-only renewal + # configs will ignore it and NOT attempt to parse it. + with open(os.path.join(self.config.config_dir, "renewal", "IGNORE.THIS"), "w") as junk: + junk.write("This file should be ignored!") + + self.defaults = configobj.ConfigObj() + + with mock.patch("certbot._internal.storage.RenewableCert._check_symlinks") as check: + check.return_value = True + self.test_rc = storage.RenewableCert(config_file.filename, self.config) + + def _write_out_kind(self, kind, ver, value=None): + link = getattr(self.test_rc, kind) + if os.path.lexists(link): + os.unlink(link) + os.symlink(os.path.join(os.path.pardir, os.path.pardir, "archive", + "example.org", "{0}{1}.pem".format(kind, ver)), + link) + with open(link, "wb") as f: + f.write(kind.encode('ascii') if value is None else value) + if kind == "privkey": + filesystem.chmod(link, 0o600) + + def _write_out_ex_kinds(self): + for kind in ALL_FOUR: + self._write_out_kind(kind, 12) + self._write_out_kind(kind, 11) + + +class RenewableCertTests(BaseRenewableCertTest): + """Tests for certbot._internal.storage.""" + + def test_initialization(self): + assert self.test_rc.lineagename == "example.org" + for kind in ALL_FOUR: + assert getattr(self.test_rc, kind) == os.path.join( + self.config.config_dir, "live", "example.org", kind + ".pem") + + def test_renewal_bad_config(self): + """Test that the RenewableCert constructor will complain if + the renewal configuration file doesn't end in ".conf" + + """ + from certbot._internal import storage + broken = os.path.join(self.config.config_dir, "broken.conf") + with open(broken, "w") as f: + f.write("[No closing bracket for you!") + with pytest.raises(errors.CertStorageError): + storage.RenewableCert(broken, self.config) + os.unlink(broken) + with pytest.raises(errors.CertStorageError): + storage.RenewableCert("fun", self.config) + + def test_renewal_incomplete_config(self): + """Test that the RenewableCert constructor will complain if + the renewal configuration file is missing a required file element.""" + from certbot._internal import storage + config = configobj.ConfigObj() + config["cert"] = "imaginary_cert.pem" + # Here the required privkey is missing. + config["chain"] = "imaginary_chain.pem" + config["fullchain"] = "imaginary_fullchain.pem" + config.filename = os.path.join(self.config.config_dir, "imaginary_config.conf") + config.write() + with pytest.raises(errors.CertStorageError): + storage.RenewableCert(config.filename, self.config) + + def test_no_renewal_version(self): + from certbot._internal import storage + + self._write_out_ex_kinds() + assert "version" not in self.config_file + + with mock.patch("certbot._internal.storage.logger") as mock_logger: + storage.RenewableCert(self.config_file.filename, self.config) + assert mock_logger.warning.called is False + + def test_renewal_newer_version(self): + from certbot._internal import storage + + self._write_out_ex_kinds() + self.config_file["version"] = "99.99.99" + self.config_file.write() + + with mock.patch("certbot._internal.storage.logger") as mock_logger: + storage.RenewableCert(self.config_file.filename, self.config) + assert mock_logger.info.called + assert "version" in mock_logger.info.call_args[0][0] + + def test_consistent(self): + # pylint: disable=protected-access + oldcert = self.test_rc.cert + self.test_rc.cert = "relative/path" + # Absolute path for item requirement + assert not self.test_rc._consistent() + self.test_rc.cert = oldcert + # Items must exist requirement + assert not self.test_rc._consistent() + # Items must be symlinks requirements + fill_with_sample_data(self.test_rc) + assert not self.test_rc._consistent() + unlink_all(self.test_rc) + # Items must point to desired place if they are relative + for kind in ALL_FOUR: + os.symlink(os.path.join("..", kind + "17.pem"), + getattr(self.test_rc, kind)) + assert not self.test_rc._consistent() + unlink_all(self.test_rc) + # Items must point to desired place if they are absolute + for kind in ALL_FOUR: + os.symlink(os.path.join(self.config.config_dir, kind + "17.pem"), + getattr(self.test_rc, kind)) + assert not self.test_rc._consistent() + unlink_all(self.test_rc) + # Items must point to things that exist + for kind in ALL_FOUR: + os.symlink(os.path.join("..", "..", "archive", "example.org", + kind + "17.pem"), + getattr(self.test_rc, kind)) + assert not self.test_rc._consistent() + # This version should work + fill_with_sample_data(self.test_rc) + assert self.test_rc._consistent() + # Items must point to things that follow the naming convention + os.unlink(self.test_rc.fullchain) + os.symlink(os.path.join("..", "..", "archive", "example.org", + "fullchain_17.pem"), self.test_rc.fullchain) + with open(self.test_rc.fullchain, "w") as f: + f.write("wrongly-named fullchain") + assert not self.test_rc._consistent() + + def test_current_target(self): + # Relative path logic + self._write_out_kind("cert", 17) + assert os.path.samefile(self.test_rc.current_target("cert"), + os.path.join(self.config.config_dir, "archive", + "example.org", + "cert17.pem")) + # Absolute path logic + os.unlink(self.test_rc.cert) + os.symlink(os.path.join(self.config.config_dir, "archive", "example.org", + "cert17.pem"), self.test_rc.cert) + with open(self.test_rc.cert, "w") as f: + f.write("cert") + assert os.path.samefile(self.test_rc.current_target("cert"), + os.path.join(self.config.config_dir, "archive", + "example.org", + "cert17.pem")) + + def test_current_version(self): + for ver in (1, 5, 10, 20): + self._write_out_kind("cert", ver) + os.unlink(self.test_rc.cert) + os.symlink(os.path.join("..", "..", "archive", "example.org", + "cert10.pem"), self.test_rc.cert) + assert self.test_rc.current_version("cert") == 10 + + def test_no_current_version(self): + assert self.test_rc.current_version("cert") is None + + def test_latest_and_next_versions(self): + for ver in range(1, 6): + for kind in ALL_FOUR: + self._write_out_kind(kind, ver) + assert self.test_rc.latest_common_version() == 5 + assert self.test_rc.next_free_version() == 6 + # Having one kind of file of a later version doesn't change the + # result + self._write_out_kind("privkey", 7) + assert self.test_rc.latest_common_version() == 5 + # ... although it does change the next free version + assert self.test_rc.next_free_version() == 8 + # Nor does having three out of four change the result + self._write_out_kind("cert", 7) + self._write_out_kind("fullchain", 7) + assert self.test_rc.latest_common_version() == 5 + # If we have everything from a much later version, it does change + # the result + for kind in ALL_FOUR: + self._write_out_kind(kind, 17) + assert self.test_rc.latest_common_version() == 17 + assert self.test_rc.next_free_version() == 18 + + @mock.patch("certbot._internal.storage.logger") + def test_ensure_deployed(self, mock_logger): + mock_update = self.test_rc.update_all_links_to = mock.Mock() + mock_has_pending = self.test_rc.has_pending_deployment = mock.Mock() + self.test_rc.latest_common_version = mock.Mock() + + mock_has_pending.return_value = False + assert self.test_rc.ensure_deployed() is True + assert mock_update.call_count == 0 + assert mock_logger.warning.call_count == 0 + + mock_has_pending.return_value = True + assert self.test_rc.ensure_deployed() is False + assert mock_update.call_count == 1 + assert mock_logger.warning.call_count == 1 + + + def test_update_link_to(self): + for ver in range(1, 6): + for kind in ALL_FOUR: + self._write_out_kind(kind, ver) + assert ver == self.test_rc.current_version(kind) + # pylint: disable=protected-access + self.test_rc._update_link_to("cert", 3) + self.test_rc._update_link_to("privkey", 2) + assert 3 == self.test_rc.current_version("cert") + assert 2 == self.test_rc.current_version("privkey") + assert 5 == self.test_rc.current_version("chain") + assert 5 == self.test_rc.current_version("fullchain") + # Currently we are allowed to update to a version that doesn't exist + self.test_rc._update_link_to("chain", 3000) + # However, current_version doesn't allow querying the resulting + # version (because it's a broken link). + assert os.path.basename(filesystem.readlink(self.test_rc.chain)) == \ + "chain3000.pem" + + def test_version(self): + self._write_out_kind("cert", 12) + # TODO: We should probably test that the directory is still the + # same, but it's tricky because we can get an absolute + # path out when we put a relative path in. + assert "cert8.pem" == \ + os.path.basename(self.test_rc.version("cert", 8)) + + def test_update_all_links_to_success(self): + for ver in range(1, 6): + for kind in ALL_FOUR: + self._write_out_kind(kind, ver) + assert ver == self.test_rc.current_version(kind) + assert self.test_rc.latest_common_version() == 5 + for ver in range(1, 6): + self.test_rc.update_all_links_to(ver) + for kind in ALL_FOUR: + assert ver == self.test_rc.current_version(kind) + assert self.test_rc.latest_common_version() == 5 + + def test_update_all_links_to_partial_failure(self): + def unlink_or_raise(path, real_unlink=os.unlink): + # pylint: disable=missing-docstring + basename = os.path.basename(path) + if "fullchain" in basename and basename.startswith("prev"): + raise ValueError + real_unlink(path) + + self._write_out_ex_kinds() + with mock.patch("certbot._internal.storage.os.unlink") as mock_unlink: + mock_unlink.side_effect = unlink_or_raise + with pytest.raises(ValueError): + self.test_rc.update_all_links_to(12) + + for kind in ALL_FOUR: + assert self.test_rc.current_version(kind) == 12 + + def test_update_all_links_to_full_failure(self): + def unlink_or_raise(path, real_unlink=os.unlink): + # pylint: disable=missing-docstring + if "fullchain" in os.path.basename(path): + raise ValueError + real_unlink(path) + + self._write_out_ex_kinds() + with mock.patch("certbot._internal.storage.os.unlink") as mock_unlink: + mock_unlink.side_effect = unlink_or_raise + with pytest.raises(ValueError): + self.test_rc.update_all_links_to(12) + + for kind in ALL_FOUR: + assert self.test_rc.current_version(kind) == 11 + + def test_has_pending_deployment(self): + for ver in range(1, 6): + for kind in ALL_FOUR: + self._write_out_kind(kind, ver) + assert ver == self.test_rc.current_version(kind) + for ver in range(1, 6): + self.test_rc.update_all_links_to(ver) + for kind in ALL_FOUR: + assert ver == self.test_rc.current_version(kind) + if ver < 5: + assert self.test_rc.has_pending_deployment() + else: + assert not self.test_rc.has_pending_deployment() + + def test_sans(self): + # Trying the current version + self._write_out_kind("cert", 12, test_util.load_vector("cert-san_512.pem")) + + assert self.test_rc.sans() == \ + [san.DNSName("example.com"), san.DNSName("www.example.com")] + + # Trying missing cert + os.unlink(self.test_rc.cert) + with pytest.raises(errors.CertStorageError): + self.test_rc.sans() + + def test_autorenewal_is_enabled(self): + self.test_rc.configuration["renewalparams"] = {} + assert self.test_rc.autorenewal_is_enabled() + self.test_rc.configuration["renewalparams"]["autorenew"] = "True" + assert self.test_rc.autorenewal_is_enabled() + + self.test_rc.configuration["renewalparams"]["autorenew"] = "False" + assert not self.test_rc.autorenewal_is_enabled() + + @mock.patch("certbot._internal.storage.relevant_values") + def test_save_successor(self, mock_rv): + # Mock relevant_values() to claim that all values are relevant here + # (to avoid instantiating parser) + mock_rv.side_effect = lambda x: x.to_dict() + + for ver in range(1, 6): + for kind in ALL_FOUR: + self._write_out_kind(kind, ver) + self.test_rc.update_all_links_to(3) + assert 6 == self.test_rc.save_successor(3, b'new cert', None, + b'new chain', self.config) + with open(self.test_rc.version("cert", 6)) as f: + assert f.read() == "new cert" + with open(self.test_rc.version("chain", 6)) as f: + assert f.read() == "new chain" + with open(self.test_rc.version("fullchain", 6)) as f: + assert f.read() == "new cert" + "new chain" + # version 6 of the key should be a link back to version 3 + assert not os.path.islink(self.test_rc.version("privkey", 3)) + assert os.path.islink(self.test_rc.version("privkey", 6)) + # Let's try two more updates + assert 7 == self.test_rc.save_successor(6, b'again', None, + b'newer chain', self.config) + assert 8 == self.test_rc.save_successor(7, b'hello', None, + b'other chain', self.config) + # All of the subsequent versions should link directly to the original + # privkey. + for i in (6, 7, 8): + assert os.path.islink(self.test_rc.version("privkey", i)) + assert "privkey3.pem" == os.path.basename(filesystem.readlink( + self.test_rc.version("privkey", i))) + + for kind in ALL_FOUR: + assert self.test_rc.available_versions(kind) == list(range(1, 9)) + assert self.test_rc.current_version(kind) == 3 + # Test updating from latest version rather than old version + self.test_rc.update_all_links_to(8) + assert 9 == self.test_rc.save_successor(8, b'last', None, + b'attempt', self.config) + for kind in ALL_FOUR: + assert self.test_rc.available_versions(kind) == \ + list(range(1, 10)) + assert self.test_rc.current_version(kind) == 8 + with open(self.test_rc.version("fullchain", 9)) as f: + assert f.read() == "last" + "attempt" + temp_config_file = os.path.join(self.config.renewal_configs_dir, + self.test_rc.lineagename) + ".conf.new" + with open(temp_config_file, "w") as f: + f.write("We previously crashed while writing me :(") + # Test updating when providing a new privkey. The key should + # be saved in a new file rather than creating a new symlink. + assert 10 == self.test_rc.save_successor(9, b'with', b'a', + b'key', self.config) + assert os.path.exists(self.test_rc.version("privkey", 10)) + assert not os.path.islink(self.test_rc.version("privkey", 10)) + assert not os.path.exists(temp_config_file) + + @test_util.skip_on_windows('Group/everybody permissions are not maintained on Windows.') + @mock.patch("certbot._internal.storage.relevant_values") + def test_save_successor_maintains_group_mode(self, mock_rv): + # Mock relevant_values() to claim that all values are relevant here + # (to avoid instantiating parser) + mock_rv.side_effect = lambda x: x.to_dict() + for kind in ALL_FOUR: + self._write_out_kind(kind, 1) + self.test_rc.update_all_links_to(1) + assert filesystem.check_mode(self.test_rc.version("privkey", 1), 0o600) + filesystem.chmod(self.test_rc.version("privkey", 1), 0o444) + # If no new key, permissions should be the same (we didn't write any keys) + self.test_rc.save_successor(1, b"newcert", None, b"new chain", self.config) + assert filesystem.check_mode(self.test_rc.version("privkey", 2), 0o444) + # If new key, permissions should be kept as 644 + self.test_rc.save_successor(2, b"newcert", b"new_privkey", b"new chain", self.config) + assert filesystem.check_mode(self.test_rc.version("privkey", 3), 0o644) + # If permissions reverted, next renewal will also revert permissions of new key + filesystem.chmod(self.test_rc.version("privkey", 3), 0o400) + self.test_rc.save_successor(3, b"newcert", b"new_privkey", b"new chain", self.config) + assert filesystem.check_mode(self.test_rc.version("privkey", 4), 0o600) + + @mock.patch("certbot._internal.storage.relevant_values") + @mock.patch("certbot._internal.storage.filesystem.copy_ownership_and_apply_mode") + def test_save_successor_maintains_gid(self, mock_ownership, mock_rv): + # Mock relevant_values() to claim that all values are relevant here + # (to avoid instantiating parser) + mock_rv.side_effect = lambda x: x.to_dict() + for kind in ALL_FOUR: + self._write_out_kind(kind, 1) + self.test_rc.update_all_links_to(1) + self.test_rc.save_successor(1, b"newcert", None, b"new chain", self.config) + assert mock_ownership.called is False + self.test_rc.save_successor(2, b"newcert", b"new_privkey", b"new chain", self.config) + assert mock_ownership.called + + @mock.patch("certbot._internal.storage.relevant_values") + def test_new_lineage(self, mock_rv): + """Test for new_lineage() class method.""" + # Mock relevant_values to say everything is relevant here (so we + # don't have to mock the parser to help it decide!) + mock_rv.side_effect = lambda x: x.to_dict() + + from certbot._internal import storage + result = storage.RenewableCert.new_lineage( + "the-lineage.com", b"cert", b"privkey", b"chain", self.config) + # This consistency check tests most relevant properties about the + # newly created cert lineage. + # pylint: disable=protected-access + assert result._consistent() + assert os.path.exists(os.path.join( + self.config.renewal_configs_dir, "the-lineage.com.conf")) + assert os.path.exists(os.path.join( + self.config.live_dir, "README")) + assert os.path.exists(os.path.join( + self.config.live_dir, "the-lineage.com", "README")) + assert filesystem.check_mode(result.key_path, 0o600) + with open(result.fullchain, "rb") as f: + assert f.read() == b"cert" + b"chain" + # Let's do it again and make sure it makes a different lineage + result = storage.RenewableCert.new_lineage( + "the-lineage.com", b"cert2", b"privkey2", b"chain2", self.config) + assert os.path.exists(os.path.join( + self.config.renewal_configs_dir, "the-lineage.com-0001.conf")) + assert os.path.exists(os.path.join( + self.config.live_dir, "the-lineage.com-0001", "README")) + # Allow write to existing but empty dir + filesystem.mkdir(os.path.join(self.config.default_archive_dir, "the-lineage.com-0002")) + result = storage.RenewableCert.new_lineage( + "the-lineage.com", b"cert3", b"privkey3", b"chain3", self.config) + assert os.path.exists(os.path.join( + self.config.live_dir, "the-lineage.com-0002", "README")) + assert filesystem.check_mode(result.key_path, 0o600) + # Now trigger the detection of already existing files + shutil.copytree(os.path.join(self.config.live_dir, "the-lineage.com"), + os.path.join(self.config.live_dir, "the-lineage.com-0003")) + with pytest.raises(errors.CertStorageError): + storage.RenewableCert.new_lineage("the-lineage.com", + b"cert4", b"privkey4", b"chain4", self.config) + shutil.copytree(os.path.join(self.config.live_dir, "the-lineage.com"), + os.path.join(self.config.live_dir, "other-example.com")) + with pytest.raises(errors.CertStorageError): + storage.RenewableCert.new_lineage("other-example.com", b"cert5", + b"privkey5", b"chain5", self.config) + # Make sure it can accept renewal parameters + result = storage.RenewableCert.new_lineage( + "the-lineage.com", b"cert2", b"privkey2", b"chain2", self.config) + # TODO: Conceivably we could test that the renewal parameters actually + # got saved + + @mock.patch("certbot._internal.storage.relevant_values") + def test_new_lineage_nonexistent_dirs(self, mock_rv): + """Test that directories can be created if they don't exist.""" + # Mock relevant_values to say everything is relevant here (so we + # don't have to mock the parser to help it decide!) + mock_rv.side_effect = lambda x: x.to_dict() + + from certbot._internal import storage + shutil.rmtree(self.config.renewal_configs_dir) + shutil.rmtree(self.config.default_archive_dir) + shutil.rmtree(self.config.live_dir) + + storage.RenewableCert.new_lineage( + "the-lineage.com", b"cert2", b"privkey2", b"chain2", self.config) + assert os.path.exists( + os.path.join( + self.config.renewal_configs_dir, "the-lineage.com.conf")) + assert os.path.exists(os.path.join( + self.config.live_dir, "the-lineage.com", "privkey.pem")) + assert os.path.exists(os.path.join( + self.config.default_archive_dir, "the-lineage.com", "privkey1.pem")) + + @mock.patch("certbot._internal.storage.util.unique_lineage_name") + def test_invalid_config_filename(self, mock_uln): + from certbot._internal import storage + mock_uln.return_value = "this_does_not_end_with_dot_conf", "yikes" + with pytest.raises(errors.CertStorageError): + storage.RenewableCert.new_lineage("example.com", + "cert", "privkey", "chain", self.config) + + def test_bad_kind(self): + with pytest.raises(errors.CertStorageError): + self.test_rc.current_target("elephant") + with pytest.raises(errors.CertStorageError): + self.test_rc.current_version("elephant") + with pytest.raises(errors.CertStorageError): + self.test_rc.version("elephant", 17) + with pytest.raises(errors.CertStorageError): + self.test_rc.available_versions("elephant") + with pytest.raises(errors.CertStorageError): + self.test_rc.newest_available_version("elephant") + # pylint: disable=protected-access + with pytest.raises(errors.CertStorageError): + self.test_rc._update_link_to("elephant", 17) + + @mock.patch("certbot._internal.ocsp.RevocationChecker.ocsp_revoked_by_paths") + def test_ocsp_revoked(self, mock_checker): + # Write out test files + for kind in ALL_FOUR: + self._write_out_kind(kind, 1) + version = self.test_rc.latest_common_version() + expected_cert_path = self.test_rc.version("cert", version) + expected_chain_path = self.test_rc.version("chain", version) + + # Test with cert revoked + mock_checker.return_value = True + assert self.test_rc.ocsp_revoked(version) + assert mock_checker.call_args[0][0] == expected_cert_path + assert mock_checker.call_args[0][1] == expected_chain_path + + # Test with cert not revoked + mock_checker.return_value = False + assert not self.test_rc.ocsp_revoked(version) + assert mock_checker.call_args[0][0] == expected_cert_path + assert mock_checker.call_args[0][1] == expected_chain_path + + # Test with error + mock_checker.side_effect = ValueError + with mock.patch("certbot._internal.storage.logger.warning") as logger: + assert not self.test_rc.ocsp_revoked(version) + assert mock_checker.call_args[0][0] == expected_cert_path + assert mock_checker.call_args[0][1] == expected_chain_path + log_msg = logger.call_args[0][0] + assert "An error occurred determining the OCSP status" in log_msg + + def test_add_time_interval(self): + from certbot._internal import storage + + # this month has 30 days, and the next year is a leap year + time_1 = datetime.datetime(2003, 11, 20, 11, 59, 21, tzinfo=datetime.timezone.utc) + + # this month has 31 days, and the next year is not a leap year + time_2 = datetime.datetime(2012, 10, 18, 21, 31, 16, tzinfo=datetime.timezone.utc) + + # in different time zone (GMT+8) + time_3 = datetime.datetime(2015, 10, 26, 22, 25, 41, tzinfo=zoneinfo.ZoneInfo('Asia/Shanghai')) + + intended = { + (time_1, ""): time_1, + (time_2, ""): time_2, + (time_3, ""): time_3, + (time_1, "17 days"): time_1 + datetime.timedelta(17), + (time_2, "17 days"): time_2 + datetime.timedelta(17), + (time_1, "30"): time_1 + datetime.timedelta(30), + (time_2, "30"): time_2 + datetime.timedelta(30), + (time_1, "7 weeks"): time_1 + datetime.timedelta(49), + (time_2, "7 weeks"): time_2 + datetime.timedelta(49), + # 1 month is always 30 days, no matter which month it is + (time_1, "1 month"): time_1 + datetime.timedelta(30), + (time_2, "1 month"): time_2 + datetime.timedelta(31), + # 1 year could be 365 or 366 days, depends on the year + (time_1, "1 year"): time_1 + datetime.timedelta(366), + (time_2, "1 year"): time_2 + datetime.timedelta(365), + (time_1, "1 year 1 day"): time_1 + datetime.timedelta(367), + (time_2, "1 year 1 day"): time_2 + datetime.timedelta(366), + (time_1, "1 year-1 day"): time_1 + datetime.timedelta(365), + (time_2, "1 year-1 day"): time_2 + datetime.timedelta(364), + (time_1, "4 years"): time_1 + datetime.timedelta(1461), + (time_2, "4 years"): time_2 + datetime.timedelta(1461), + } + + for parameters, excepted in intended.items(): + base_time, interval = parameters + assert storage.add_time_interval(base_time, interval) == \ + excepted + + def test_server(self): + self.test_rc.configuration["renewalparams"] = {} + assert self.test_rc.server is None + rp = self.test_rc.configuration["renewalparams"] + rp["server"] = "https://acme.example/dir" + assert self.test_rc.server == "https://acme.example/dir" + + def test_is_test_cert(self): + self.test_rc.configuration["renewalparams"] = {} + rp = self.test_rc.configuration["renewalparams"] + assert self.test_rc.is_test_cert is False + rp["server"] = "https://acme-staging-v02.api.letsencrypt.org/directory" + assert self.test_rc.is_test_cert is True + rp["server"] = "https://staging.someotherca.com/directory" + assert self.test_rc.is_test_cert is True + rp["server"] = "https://acme-v01.api.letsencrypt.org/directory" + assert self.test_rc.is_test_cert is False + rp["server"] = "https://acme-v02.api.letsencrypt.org/directory" + assert self.test_rc.is_test_cert is False + + def test_missing_cert(self): + from certbot._internal import storage + with pytest.raises(errors.CertStorageError): + storage.RenewableCert(self.config_file.filename, self.config) + os.symlink("missing", self.config_file[ALL_FOUR[0]]) + with pytest.raises(errors.CertStorageError): + storage.RenewableCert(self.config_file.filename, self.config) + + def test_atomic_rewrite(self): + # Mostly tested by the process of creating and updating lineages, + # but we can test that this successfully creates files, removes + # unneeded items, preserves permissions, and preserves comments. + temp = os.path.join(self.config.config_dir, "sample-file") + with open(temp, "w") as f: + f.write("[renewalparams]\nuseful = value # A useful value\n" + "useless = value # Not needed\n") + filesystem.chmod(temp, 0o640) + perms = stat.S_IMODE(os.lstat(temp).st_mode) + config = configobj.ConfigObj() + for x in ALL_FOUR: + config[x] = "somewhere" + config["version"] = certbot.__version__ + config["archive_dir"] = "the_archive" + config["renewalparams"] = {"useful": "new_value"} + + from certbot._internal import storage + storage.atomic_rewrite(temp, config) + + with open(temp, "r") as f: + content = f.read() + # useful value was updated + assert "useful = new_value" in content + # associated comment was preserved + assert "A useful value" in content + # useless value was deleted + assert "useless" not in content + # check version was stored + assert "version = {0}".format(certbot.__version__) in content + # ensure permissions are copied + assert stat.S_IMODE(os.lstat(temp).st_mode) == perms + + def test_truncate(self): + # It should not do anything when there's less than 5 cert history + for kind in ALL_FOUR: + self._write_out_kind(kind, 1) + with mock.patch('certbot.compat.os.unlink') as mock_unlink: + self.test_rc.truncate() + mock_unlink.assert_not_called() + + # It should truncate the excess when there's more than 5 cert history + for kind in ALL_FOUR: + for i in range(2, 8): + self._write_out_kind(kind, i) + with mock.patch('certbot.compat.os.unlink') as mock_unlink: + self.test_rc.truncate() + assert mock_unlink.call_count == 1 * len(ALL_FOUR) + assert "1.pem" in mock_unlink.call_args_list[0][0][0] + +class DeleteFilesTest(BaseRenewableCertTest): + """Tests for certbot._internal.storage.delete_files""" + def setUp(self): + super().setUp() + + for kind in ALL_FOUR: + kind_path = os.path.join(self.config.config_dir, "live", "example.org", + kind + ".pem") + with open(kind_path, 'a'): + pass + self.config_file.write() + assert os.path.exists(os.path.join( + self.config.renewal_configs_dir, "example.org.conf")) + assert os.path.exists(os.path.join( + self.config.live_dir, "example.org")) + assert os.path.exists(os.path.join( + self.config.config_dir, "archive", "example.org")) + + def _call(self): + from certbot._internal import storage + with mock.patch("certbot._internal.storage.logger"): + storage.delete_files(self.config, "example.org") + + def test_delete_all_files(self): + self._call() + + assert not os.path.exists(os.path.join( + self.config.renewal_configs_dir, "example.org.conf")) + assert not os.path.exists(os.path.join( + self.config.live_dir, "example.org")) + assert not os.path.exists(os.path.join( + self.config.config_dir, "archive", "example.org")) + + def test_bad_renewal_config(self): + with open(self.config_file.filename, 'a') as config_file: + config_file.write("asdfasfasdfasdf") + + with pytest.raises(errors.CertStorageError): + self._call() + assert os.path.exists(os.path.join( + self.config.live_dir, "example.org")) + assert not os.path.exists(os.path.join( + self.config.renewal_configs_dir, "example.org.conf")) + + def test_no_renewal_config(self): + os.remove(self.config_file.filename) + with pytest.raises(errors.CertStorageError): + self._call() + assert os.path.exists(os.path.join( + self.config.live_dir, "example.org")) + assert not os.path.exists(self.config_file.filename) + + def test_no_cert_file(self): + os.remove(os.path.join( + self.config.live_dir, "example.org", "cert.pem")) + self._call() + assert not os.path.exists(self.config_file.filename) + assert not os.path.exists(os.path.join( + self.config.live_dir, "example.org")) + assert not os.path.exists(os.path.join( + self.config.config_dir, "archive", "example.org")) + + def test_no_readme_file(self): + os.remove(os.path.join( + self.config.live_dir, "example.org", "README")) + self._call() + assert not os.path.exists(self.config_file.filename) + assert not os.path.exists(os.path.join( + self.config.live_dir, "example.org")) + assert not os.path.exists(os.path.join( + self.config.config_dir, "archive", "example.org")) + + def test_livedir_not_empty(self): + with open(os.path.join( + self.config.live_dir, "example.org", "other_file"), 'a'): + pass + self._call() + assert not os.path.exists(self.config_file.filename) + assert os.path.exists(os.path.join( + self.config.live_dir, "example.org")) + assert not os.path.exists(os.path.join( + self.config.config_dir, "archive", "example.org")) + + def test_no_archive(self): + archive_dir = os.path.join(self.config.config_dir, "archive", "example.org") + os.rmdir(archive_dir) + self._call() + assert not os.path.exists(self.config_file.filename) + assert not os.path.exists(os.path.join( + self.config.live_dir, "example.org")) + assert not os.path.exists(archive_dir) + +class CertPathForCertNameTest(BaseRenewableCertTest): + """Test for certbot._internal.storage.cert_path_for_cert_name""" + def setUp(self): + super().setUp() + self.config_file.write() + self._write_out_ex_kinds() + self.fullchain = os.path.join(self.config.config_dir, 'live', 'example.org', + 'fullchain.pem') + self.config.cert_path = self.fullchain + + def _call(self, cli_config, certname): + from certbot._internal.storage import cert_path_for_cert_name + return cert_path_for_cert_name(cli_config, certname) + + def test_simple_cert_name(self): + assert self._call(self.config, 'example.org') == self.fullchain + + def test_no_such_cert_name(self): + with pytest.raises(errors.CertStorageError): + self._call(self.config, 'fake-example.org') + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/tests/util_test.py b/certbot/src/certbot/_internal/tests/util_test.py new file mode 100644 index 00000000000..7ab451a39d3 --- /dev/null +++ b/certbot/src/certbot/_internal/tests/util_test.py @@ -0,0 +1,709 @@ +"""Tests for certbot.util.""" +import argparse +import errno +from importlib import reload as reload_module +import io +import sys +import unittest +from unittest import mock + +import pytest + +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os +import certbot.tests.util as test_util + + +class EnvNoSnapForExternalCallsTest(unittest.TestCase): + """Tests for certbot.util.env_no_snap_for_external_calls.""" + @classmethod + def _call(cls): + from certbot.util import env_no_snap_for_external_calls + return env_no_snap_for_external_calls() + + def test_removed(self): + original_path = os.environ['PATH'] + env_copy_dict = os.environ.copy() + env_copy_dict['PATH'] = 'RANDOM_NONSENSE_GARBAGE/blah/blah:' + original_path + env_copy_dict['SNAP'] = 'RANDOM_NONSENSE_GARBAGE' + env_copy_dict['CERTBOT_SNAPPED'] = 'True' + with mock.patch('certbot.compat.os.environ.copy', return_value=env_copy_dict): + assert self._call()['PATH'] == original_path + + def test_noop(self): + env_copy_dict_unmodified = os.environ.copy() + env_copy_dict_unmodified['PATH'] = 'RANDOM_NONSENSE_GARBAGE/blah/blah:' \ + + env_copy_dict_unmodified['PATH'] + env_copy_dict = env_copy_dict_unmodified.copy() + with mock.patch('certbot.compat.os.environ.copy', return_value=env_copy_dict): + # contains neither necessary key + env_copy_dict.pop('SNAP', None) + env_copy_dict.pop('CERTBOT_SNAPPED', None) + assert self._call()['PATH'] == env_copy_dict_unmodified['PATH'] + # contains only one necessary key + env_copy_dict['SNAP'] = 'RANDOM_NONSENSE_GARBAGE' + assert self._call()['PATH'] == env_copy_dict_unmodified['PATH'] + del env_copy_dict['SNAP'] + env_copy_dict['CERTBOT_SNAPPED'] = 'True' + assert self._call()['PATH'] == env_copy_dict_unmodified['PATH'] + + +class RunScriptTest(unittest.TestCase): + """Tests for certbot.util.run_script.""" + @classmethod + def _call(cls, params): + from certbot.util import run_script + return run_script(params) + + @mock.patch("certbot.util.subprocess.run") + def test_default(self, mock_run): + """These will be changed soon enough with reload.""" + mock_run().returncode = 0 + mock_run().stdout = "stdout" + mock_run().stderr = "stderr" + + out, err = self._call(["test"]) + assert out == "stdout" + assert err == "stderr" + + @mock.patch("certbot.util.subprocess.run") + def test_bad_process(self, mock_run): + mock_run.side_effect = OSError + + with pytest.raises(errors.SubprocessError): + self._call(["test"]) + + @mock.patch("certbot.util.subprocess.run") + def test_failure(self, mock_run): + mock_run().returncode = 1 + + with pytest.raises(errors.SubprocessError): + self._call(["test"]) + + +class ExeExistsTest(unittest.TestCase): + """Tests for certbot.util.exe_exists.""" + + @classmethod + def _call(cls, exe): + from certbot.util import exe_exists + return exe_exists(exe) + + def test_exe_exists(self): + with mock.patch("certbot.util.filesystem.is_executable", return_value=True): + assert self._call("/path/to/exe") + + def test_exe_not_exists(self): + with mock.patch("certbot.util.filesystem.is_executable", return_value=False): + assert not self._call("/path/to/exe") + + +class LockDirUntilExit(test_util.TempDirTestCase): + """Tests for certbot.util.lock_dir_until_exit.""" + @classmethod + def _call(cls, *args, **kwargs): + from certbot.util import lock_dir_until_exit + return lock_dir_until_exit(*args, **kwargs) + + def setUp(self): + super().setUp() + # reset global state from other tests + import certbot.util + reload_module(certbot.util) + + @mock.patch('certbot.util.logger') + @mock.patch('certbot.util.atexit_register') + def test_it(self, mock_register, mock_logger): + subdir = os.path.join(self.tempdir, 'subdir') + filesystem.mkdir(subdir) + self._call(self.tempdir) + self._call(subdir) + self._call(subdir) + + assert mock_register.call_count == 1 + registered_func = mock_register.call_args[0][0] + + from certbot import util + + # Despite lock_dir_until_exit has been called twice to subdir, its lock should have been + # added only once. So we expect to have two lock references: for self.tempdir and subdir + assert len(util._LOCKS) == 2 # pylint: disable=protected-access + registered_func() # Exception should not be raised + # Logically, logger.debug, that would be invoked in case of unlock failure, + # should never been called. + assert mock_logger.debug.call_count == 0 + + +class SetUpCoreDirTest(test_util.TempDirTestCase): + """Tests for certbot.util.make_or_verify_core_dir.""" + + def _call(self, *args, **kwargs): + from certbot.util import set_up_core_dir + return set_up_core_dir(*args, **kwargs) + + @mock.patch('certbot.util.lock_dir_until_exit') + def test_success(self, mock_lock): + new_dir = os.path.join(self.tempdir, 'new') + self._call(new_dir, 0o700, False) + assert os.path.exists(new_dir) + assert mock_lock.call_count == 1 + + @mock.patch('certbot.util.make_or_verify_dir') + def test_failure(self, mock_make_or_verify): + mock_make_or_verify.side_effect = OSError + with pytest.raises(errors.Error): + self._call(self.tempdir, 0o700, False) + + +class MakeOrVerifyDirTest(test_util.TempDirTestCase): + """Tests for certbot.util.make_or_verify_dir. + + Note that it is not possible to test for a wrong directory owner, + as this testing script would have to be run as root. + + """ + + def setUp(self): + super().setUp() + + self.path = os.path.join(self.tempdir, "foo") + filesystem.mkdir(self.path, 0o600) + + def _call(self, directory, mode): + from certbot.util import make_or_verify_dir + return make_or_verify_dir(directory, mode, strict=True) + + def test_creates_dir_when_missing(self): + path = os.path.join(self.tempdir, "bar") + self._call(path, 0o650) + assert os.path.isdir(path) + assert filesystem.check_mode(path, 0o650) + + def test_existing_correct_mode_does_not_fail(self): + self._call(self.path, 0o600) + assert filesystem.check_mode(self.path, 0o600) + + def test_existing_wrong_mode_fails(self): + with pytest.raises(errors.Error): + self._call(self.path, 0o400) + + def test_reraises_os_error(self): + with mock.patch.object(filesystem, "makedirs") as makedirs: + makedirs.side_effect = OSError() + with pytest.raises(OSError): + self._call("bar", 12312312) + + +class UniqueFileTest(test_util.TempDirTestCase): + """Tests for certbot.util.unique_file.""" + + def setUp(self): + super().setUp() + + self.default_name = os.path.join(self.tempdir, "foo.txt") + + def _call(self, mode=0o600): + from certbot.util import unique_file + return unique_file(self.default_name, mode) + + def test_returns_fd_for_writing(self): + fd, name = self._call() + fd.write("bar") + fd.close() + with open(name) as f: + assert f.read() == "bar" + + def test_right_mode(self): + fd1, name1 = self._call(0o700) + fd2, name2 = self._call(0o600) + assert filesystem.check_mode(name1, 0o700) + assert filesystem.check_mode(name2, 0o600) + fd1.close() + fd2.close() + + def test_default_exists(self): + fd1, name1 = self._call() # create 0000_foo.txt + fd2, name2 = self._call() + fd3, name3 = self._call() + + assert name1 != name2 + assert name1 != name3 + assert name2 != name3 + + assert os.path.dirname(name1) == self.tempdir + assert os.path.dirname(name2) == self.tempdir + assert os.path.dirname(name3) == self.tempdir + + basename1 = os.path.basename(name2) + assert basename1.endswith("foo.txt") + basename2 = os.path.basename(name2) + assert basename2.endswith("foo.txt") + basename3 = os.path.basename(name3) + assert basename3.endswith("foo.txt") + + fd1.close() + fd2.close() + fd3.close() + + +class UniqueLineageNameTest(test_util.TempDirTestCase): + """Tests for certbot.util.unique_lineage_name.""" + + def _call(self, filename, mode=0o777): + from certbot.util import unique_lineage_name + return unique_lineage_name(self.tempdir, filename, mode) + + def test_basic(self): + f, path = self._call("wow") + assert isinstance(f, io.TextIOWrapper) + assert os.path.join(self.tempdir, "wow.conf") == path + f.close() + + def test_multiple(self): + items = [] + for _ in range(10): + items.append(self._call("wow")) + f, name = items[-1] + assert isinstance(f, io.TextIOWrapper) + assert isinstance(name, str) + assert "wow-0009.conf" in name + for f, _ in items: + f.close() + + def test_failure(self): + with mock.patch("certbot.compat.filesystem.open", side_effect=OSError(errno.EIO)): + with pytest.raises(OSError): + self._call("wow") + + +class SafelyRemoveTest(test_util.TempDirTestCase): + """Tests for certbot.util.safely_remove.""" + + def setUp(self): + super().setUp() + + self.path = os.path.join(self.tempdir, "foo") + + def _call(self): + from certbot.util import safely_remove + return safely_remove(self.path) + + def test_exists(self): + with open(self.path, "w"): + pass # just create the file + self._call() + assert not os.path.exists(self.path) + + def test_missing(self): + self._call() + # no error, yay! + assert not os.path.exists(self.path) + + def test_other_error_passthrough(self): + with mock.patch("certbot.util.os.remove") as mock_remove: + mock_remove.side_effect = OSError + with pytest.raises(OSError): + self._call() + + +class SafeEmailTest(unittest.TestCase): + """Test safe_email.""" + @classmethod + def _call(cls, addr): + from certbot.util import safe_email + return safe_email(addr) + + def test_valid_emails(self): + addrs = [ + "certbot@certbot.org", + "tbd.ade@gmail.com", + "abc_def.jdk@hotmail.museum", + ] + for addr in addrs: + assert self._call(addr), "%s failed." % addr + + def test_invalid_emails(self): + addrs = [ + "certbot@certbot..org", + ".tbd.ade@gmail.com", + "~/abc_def.jdk@hotmail.museum", + ] + for addr in addrs: + assert not self._call(addr), "%s failed." % addr + + +class AddDeprecatedArgumentTest(unittest.TestCase): + """Test add_deprecated_argument.""" + def setUp(self): + self.parser = argparse.ArgumentParser() + + def _call(self, argument_name, nargs): + from certbot.util import add_deprecated_argument + add_deprecated_argument(self.parser.add_argument, argument_name, nargs) + + def test_warning_no_arg(self): + self._call("--old-option", 0) + with mock.patch("certbot.util.logger.warning") as mock_warn: + self.parser.parse_args(["--old-option"]) + assert mock_warn.call_count == 1 + assert "is deprecated" in mock_warn.call_args[0][0] + assert "--old-option" in mock_warn.call_args[0][1] + + def test_warning_with_arg(self): + self._call("--old-option", 1) + with mock.patch("certbot.util.logger.warning") as mock_warn: + self.parser.parse_args(["--old-option", "42"]) + assert mock_warn.call_count == 1 + assert "is deprecated" in mock_warn.call_args[0][0] + assert "--old-option" in mock_warn.call_args[0][1] + + def test_help(self): + self._call("--old-option", 2) + stdout = io.StringIO() + with mock.patch("sys.stdout", new=stdout): + try: + self.parser.parse_args(["-h"]) + except SystemExit: + pass + assert "--old-option" not in stdout.getvalue() + + def test_set_constant(self): + """Test when ACTION_TYPES_THAT_DONT_NEED_A_VALUE is a set. + + This variable is a set in configargparse versions < 0.12.0. + + """ + self._test_constant_common(set) + + def test_tuple_constant(self): + """Test when ACTION_TYPES_THAT_DONT_NEED_A_VALUE is a tuple. + + This variable is a tuple in configargparse versions >= 0.12.0. + + """ + self._test_constant_common(tuple) + + def _test_constant_common(self, typ): + with mock.patch("certbot.util.configargparse") as mock_configargparse: + mock_configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE = typ() + self._call("--old-option", 1) + self._call("--old-option2", 2) + assert len(mock_configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE) == 1 + + +class EnforceLeValidity(unittest.TestCase): + """Test enforce_le_validity.""" + def _call(self, domain): + from certbot.util import enforce_le_validity + return enforce_le_validity(domain) + + def test_sanity(self): + with pytest.raises(errors.ConfigurationError): + self._call(u"..") + + def test_invalid_chars(self): + with pytest.raises(errors.ConfigurationError): + self._call(u"hello_world.example.com") + + def test_leading_hyphen(self): + with pytest.raises(errors.ConfigurationError): + self._call(u"-a.example.com") + + def test_trailing_hyphen(self): + with pytest.raises(errors.ConfigurationError): + self._call(u"a-.example.com") + + def test_one_label(self): + with pytest.raises(errors.ConfigurationError): + self._call(u"com") + + def test_valid_domain(self): + assert self._call(u"example.com") == u"example.com" + + def test_input_with_scheme(self): + with pytest.raises(errors.ConfigurationError): + self._call(u"http://example.com") + with pytest.raises(errors.ConfigurationError): + self._call(u"https://example.com") + + def test_valid_input_with_scheme_name(self): + assert self._call(u"http.example.com") == u"http.example.com" + + +class EnforceDomainSanityTest(unittest.TestCase): + """Test enforce_domain_sanity.""" + + def _call(self, domain): + from certbot.util import enforce_domain_sanity + return enforce_domain_sanity(domain) + + def test_nonascii_str(self): + with pytest.raises(errors.ConfigurationError): + self._call(u"eichh\u00f6rnchen.example.com".encode("utf-8")) + + def test_nonascii_unicode(self): + with pytest.raises(errors.ConfigurationError): + self._call(u"eichh\u00f6rnchen.example.com") + + def test_too_long(self): + long_domain = u"a"*256 + with pytest.raises(errors.ConfigurationError): + self._call(long_domain) + + def test_not_too_long(self): + not_too_long_domain = u"{0}.{1}.{2}.{3}".format("a"*63, "b"*63, "c"*63, "d"*63) + self._call(not_too_long_domain) + + def test_empty_label(self): + empty_label_domain = u"fizz..example.com" + with pytest.raises(errors.ConfigurationError): + self._call(empty_label_domain) + + def test_empty_trailing_label(self): + empty_trailing_label_domain = u"example.com.." + with pytest.raises(errors.ConfigurationError): + self._call(empty_trailing_label_domain) + + def test_long_label_1(self): + long_label_domain = u"a"*64 + with pytest.raises(errors.ConfigurationError): + self._call(long_label_domain) + + def test_long_label_2(self): + long_label_domain = u"{0}.{1}.com".format(u"a"*64, u"b"*63) + with pytest.raises(errors.ConfigurationError): + self._call(long_label_domain) + + def test_not_long_label(self): + not_too_long_label_domain = u"{0}.{1}.com".format(u"a"*63, u"b"*63) + self._call(not_too_long_label_domain) + + def test_empty_domain(self): + empty_domain = u"" + with pytest.raises(errors.ConfigurationError): + self._call(empty_domain) + + def test_punycode_ok(self): + # Punycode is now legal, so no longer an error; instead check + # that it's _not_ an error (at the initial sanity check stage) + self._call('this.is.xn--ls8h.tld') + + +class IsWildcardDomainTest(unittest.TestCase): + """Tests for is_wildcard_domain.""" + + def setUp(self): + self.wildcard = u"*.example.org" + self.no_wildcard = u"example.org" + + def _call(self, domain): + from certbot.util import is_wildcard_domain + return is_wildcard_domain(domain) + + def test_no_wildcard(self): + assert not self._call(self.no_wildcard) + assert not self._call(self.no_wildcard.encode()) + + def test_wildcard(self): + assert self._call(self.wildcard) + assert self._call(self.wildcard.encode()) + + +class OsInfoTest(unittest.TestCase): + """Test OS / distribution detection""" + + @mock.patch("certbot.util.distro") + @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") + def test_systemd_os_release_like(self, m_distro): + import certbot.util as cbutil + m_distro.like.return_value = "first debian third" + id_likes = cbutil.get_systemd_os_like() + assert len(id_likes) == 3 + assert "debian" in id_likes + + @mock.patch("certbot.util.distro") + @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") + def test_get_os_info_ua(self, m_distro): + import certbot.util as cbutil + with mock.patch('platform.system_alias', + return_value=('linux', '42', '42')): + m_distro.version.return_value = "1.0" + # empty value on first call for fallback to "get_python_os_info" in get_os_info_ua + m_distro.name.side_effect = ["", "something", "something"] + assert cbutil.get_os_info_ua() == \ + " ".join(cbutil.get_python_os_info(pretty=True)) + + m_distro.name.side_effect = ["whatever"] + assert cbutil.get_os_info_ua() == "whatever" + + @mock.patch("certbot.util.distro") + @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") + def test_get_os_info(self, m_distro): + import certbot.util as cbutil + with mock.patch("platform.system") as mock_platform: + m_distro.id.return_value = "name" + m_distro.version.return_value = "version" + mock_platform.return_value = "linux" + assert cbutil.get_os_info() == ("name", "version") + + m_distro.id.return_value = "something" + m_distro.version.return_value = "else" + assert cbutil.get_os_info() == ("something", "else") + + def test_non_systemd_os_info(self): + import certbot.util as cbutil + with mock.patch('certbot.util._USE_DISTRO', False): + with mock.patch('platform.system_alias', + return_value=('NonSystemD', '42', '42')): + assert cbutil.get_python_os_info()[0] == 'nonsystemd' + + with mock.patch('platform.system_alias', + return_value=('darwin', '', '')): + with mock.patch("subprocess.run") as run_mock: + run_mock().stdout = '42.42.42' + assert cbutil.get_python_os_info()[0] == 'darwin' + assert cbutil.get_python_os_info()[1] == '42.42.42' + + with mock.patch('platform.system_alias', + return_value=('freebsd', '9.3-RC3-p1', '')): + assert cbutil.get_python_os_info() == ("freebsd", "9") + + with mock.patch('platform.system_alias', + return_value=('windows', '', '')): + with mock.patch('platform.win32_ver', + return_value=('4242', '95', '2', '')): + assert cbutil.get_python_os_info() == \ + ("windows", "95") + + @mock.patch("certbot.util.distro") + @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") + def test_python_os_info_notfound(self, m_distro): + import certbot.util as cbutil + m_distro.id.return_value = "" + m_distro.version.return_value = "" + assert cbutil.get_python_os_info()[0] == "linux" + + @mock.patch("certbot.util.distro") + @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") + def test_python_os_info_custom(self, m_distro): + import certbot.util as cbutil + m_distro.id.return_value = "testdist" + m_distro.version.return_value = "42" + assert cbutil.get_python_os_info() == ("testdist", "42") + + +class AtexitRegisterTest(unittest.TestCase): + """Tests for certbot.util.atexit_register.""" + def setUp(self): + self.func = mock.MagicMock() + self.args = ('hi',) + self.kwargs = {'answer': 42} + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.util import atexit_register + return atexit_register(*args, **kwargs) + + def test_called(self): + self._test_common(os.getpid()) + self.func.assert_called_with(*self.args, **self.kwargs) + + def test_not_called(self): + self._test_common(initial_pid=-1) + assert self.func.called is False + + def _test_common(self, initial_pid): + with mock.patch('certbot.util._INITIAL_PID', initial_pid): + with mock.patch('certbot.util.atexit') as mock_atexit: + self._call(self.func, *self.args, **self.kwargs) + + # _INITIAL_PID must be mocked when calling atexit_func + assert mock_atexit.register.called + args, kwargs = mock_atexit.register.call_args + atexit_func = args[0] + atexit_func(*args[1:], **kwargs) + + +class LooseVersionTest(unittest.TestCase): + """Test for certbot.util.LooseVersion. + + These tests are based on the original tests for + distutils.version.LooseVersion at + https://github.com/python/cpython/blob/v3.10.0/Lib/distutils/tests/test_version.py#L58-L81. + + """ + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.util import LooseVersion + return LooseVersion(*args, **kwargs) + + def test_less_than(self): + comparisons = (('1.5.1', '1.5.2b2'), + ('3.4j', '1996.07.12'), + ('2g6', '11g'), + ('0.960923', '2.2beta29'), + ('1.13++', '5.5.kw'), + ('2.0', '2.0.1'), + ('a', 'b')) + for v1, v2 in comparisons: + assert self._call(v1).try_risky_comparison(self._call(v2)) == -1 + + def test_equal(self): + comparisons = (('8.02', '8.02'), + ('1a', '1a'), + ('2', '2.0.0'), + ('2.0', '2.0.0')) + for v1, v2 in comparisons: + assert self._call(v1).try_risky_comparison(self._call(v2)) == 0 + + def test_greater_than(self): + comparisons = (('161', '3.10a'), + ('3.2.pl0', '3.1.1.6')) + for v1, v2 in comparisons: + assert self._call(v1).try_risky_comparison(self._call(v2)) == 1 + + def test_incomparible(self): + comparisons = (('bookworm/sid', '9'), + ('1a', '1.0')) + for v1, v2 in comparisons: + with pytest.raises(ValueError): + assert self._call(v1).try_risky_comparison(self._call(v2)) + + +class ParseLooseVersionTest(unittest.TestCase): + """Test for certbot.util.parse_loose_version. + + These tests are based on the original tests for + distutils.version.LooseVersion at + https://github.com/python/cpython/blob/v3.10.0/Lib/distutils/tests/test_version.py#L58-L81. + + """ + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.util import parse_loose_version + return parse_loose_version(*args, **kwargs) + + def test_less_than(self): + comparisons = (('1.5.1', '1.5.2b2'), + ('3.4j', '1996.07.12'), + ('2g6', '11g'), + ('0.960923', '2.2beta29'), + ('1.13++', '5.5.kw')) + for v1, v2 in comparisons: + assert self._call(v1) < self._call(v2) + + def test_equal(self): + assert self._call('8.02') == self._call('8.02') + + def test_greater_than(self): + comparisons = (('161', '3.10a'), + ('3.2.pl0', '3.1.1.6')) + for v1, v2 in comparisons: + assert self._call(v1) > self._call(v2) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover diff --git a/certbot/src/certbot/_internal/updater.py b/certbot/src/certbot/_internal/updater.py new file mode 100644 index 00000000000..21408755087 --- /dev/null +++ b/certbot/src/certbot/_internal/updater.py @@ -0,0 +1,133 @@ +"""Updaters run at renewal""" +import logging + +from certbot import configuration +from certbot import errors +from certbot import interfaces +from certbot._internal import storage +from certbot._internal.plugins import disco as plugin_disco +from certbot._internal.plugins import selection as plug_sel +from certbot.plugins import enhancements + +logger = logging.getLogger(__name__) + + +def run_generic_updaters(config: configuration.NamespaceConfig, lineage: storage.RenewableCert, + plugins: plugin_disco.PluginsRegistry) -> None: + """Run updaters that the plugin supports + + :param config: Configuration object + :type config: certbot.configuration.NamespaceConfig + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :param plugins: List of plugins + :type plugins: certbot._internal.plugins.disco.PluginsRegistry + + :returns: `None` + :rtype: None + """ + if config.dry_run: + logger.debug("Skipping updaters in dry-run mode.") + return + try: + installer = plug_sel.get_unprepared_installer(config, plugins) + except errors.Error as e: + logger.error("Could not choose appropriate plugin for updaters: %s", e) + return + if installer: + _run_updaters(lineage, installer, config) + _run_enhancement_updaters(lineage, installer, config) + + +def run_renewal_deployer(config: configuration.NamespaceConfig, lineage: storage.RenewableCert, + installer: interfaces.Installer) -> None: + """Helper function to run deployer interface method if supported by the used + installer plugin. + + :param config: Configuration object + :type config: certbot.configuration.NamespaceConfig + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :param installer: Installer object + :type installer: interfaces.Installer + + :returns: `None` + :rtype: None + """ + if config.dry_run: + logger.debug("Skipping renewal deployer in dry-run mode.") + return + + if not config.disable_renew_updates and isinstance(installer, + interfaces.RenewDeployer): + installer.renew_deploy(lineage) + _run_enhancement_deployers(lineage, installer, config) + + +def _run_updaters(lineage: storage.RenewableCert, installer: interfaces.Installer, + config: configuration.NamespaceConfig) -> None: + """Helper function to run the updater interface methods if supported by the + used installer plugin. + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :param installer: Installer object + :type installer: interfaces.Installer + + :returns: `None` + :rtype: None + """ + if not config.disable_renew_updates: + if isinstance(installer, interfaces.GenericUpdater): + installer.generic_updates(lineage) + + +def _run_enhancement_updaters(lineage: storage.RenewableCert, installer: interfaces.Installer, + config: configuration.NamespaceConfig) -> None: + """Iterates through known enhancement interfaces. If the installer implements + an enhancement interface and the enhance interface has an updater method, the + updater method gets run. + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :param installer: Installer object + :type installer: interfaces.Installer + + :param config: Configuration object + :type config: certbot.configuration.NamespaceConfig + """ + + if config.disable_renew_updates: + return + for enh in enhancements._INDEX: # pylint: disable=protected-access + if isinstance(installer, enh["class"]) and enh["updater_function"]: + getattr(installer, enh["updater_function"])(lineage) + + +def _run_enhancement_deployers(lineage: storage.RenewableCert, installer: interfaces.Installer, + config: configuration.NamespaceConfig) -> None: + """Iterates through known enhancement interfaces. If the installer implements + an enhancement interface and the enhance interface has an deployer method, the + deployer method gets run. + + :param lineage: Certificate lineage object + :type lineage: storage.RenewableCert + + :param installer: Installer object + :type installer: interfaces.Installer + + :param config: Configuration object + :type config: certbot.configuration.NamespaceConfig + """ + + if config.disable_renew_updates: + return + for enh in enhancements._INDEX: # pylint: disable=protected-access + if isinstance(installer, enh["class"]) and enh["deployer_function"]: + getattr(installer, enh["deployer_function"])(lineage) diff --git a/certbot/src/certbot/achallenges.py b/certbot/src/certbot/achallenges.py new file mode 100644 index 00000000000..9ce7dcd1a49 --- /dev/null +++ b/certbot/src/certbot/achallenges.py @@ -0,0 +1,106 @@ +"""Client annotated ACME challenges. + +Please use names such as ``achall`` to distinguish from variables "of type" +:class:`acme.challenges.Challenge` (denoted by ``chall``) +and :class:`.ChallengeBody` (denoted by ``challb``):: + + from acme import challenges + from acme import messages + from certbot import achallenges + + chall = challenges.DNS(token='foo') + challb = messages.ChallengeBody(chall=chall) + achall = achallenges.DNS(chall=challb, domain='example.com') + +Note, that all annotated challenges act as a proxy objects:: + + achall.token == challb.token + +""" +import logging +from typing import Any +import warnings + +import josepy as jose + +from acme import challenges, messages +from acme.challenges import Challenge + +from certbot import errors + +logger = logging.getLogger(__name__) + + +class AnnotatedChallenge(jose.ImmutableMap): + """Client annotated challenge. + + Wraps around server provided challenge and annotates with data + useful for the client. + + :ivar ~.challb: Wrapped `~.ChallengeBody`. + + """ + __slots__ = ('challb',) + _acme_type: type[Challenge] = NotImplemented + + def __getattr__(self, name: str) -> Any: + return getattr(self.challb, name) + + def __getattribute__(self, name: str) -> Any: + if name == 'domain': + warnings.warn("The domain attribute is deprecated and will be removed in " + "an upcoming release. Access the AnnotatedChallenge.identifier.value " + "attribute instead", + DeprecationWarning) + return super().__getattribute__(name) + + def __hash__(self) -> int: + with warnings.catch_warnings(): + warnings.filterwarnings('ignore', 'the domain attribute is deprecated') + return super().__hash__() + + def __eq__(self, other: Any) -> bool: + with warnings.catch_warnings(): + warnings.filterwarnings('ignore', 'the domain attribute is deprecated') + return super().__eq__(other) + + def __init__(self, **kwargs: Any) -> None: + if 'domain' in kwargs: + if 'identifier' in kwargs: + raise errors.Error("AnnotatedChallenge takes either domain or identifier, not both") + warnings.warn("The domain attribute is deprecated and will be removed in " + "an upcoming release. Replace domain= with " + "identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, " + "value=)", + DeprecationWarning) + if 'identifier' not in kwargs: + kwargs['identifier'] = messages.Identifier( + typ=messages.IDENTIFIER_FQDN, value=kwargs['domain']) + if 'domain' not in kwargs: + if kwargs['identifier'].typ == messages.IDENTIFIER_FQDN: + kwargs['domain'] = kwargs['identifier'].value + else: + kwargs['domain'] = None + super().__init__(**kwargs) + + +class KeyAuthorizationAnnotatedChallenge(AnnotatedChallenge): + """Client annotated `KeyAuthorizationChallenge` challenge.""" + __slots__ = ('challb', 'domain', 'account_key', 'identifier') # pylint: disable=redefined-slots-in-subclass + + def response_and_validation(self, *args: Any, **kwargs: Any + ) -> tuple['challenges.KeyAuthorizationChallengeResponse', Any]: + """Generate response and validation.""" + return self.challb.chall.response_and_validation( + self.account_key, *args, **kwargs) + + +class DNS(AnnotatedChallenge): + """Client annotated "dns" ACME challenge.""" + __slots__ = ('challb', 'domain', 'identifier') # pylint: disable=redefined-slots-in-subclass + acme_type = challenges.DNS + +class Other(AnnotatedChallenge): + """Client annotated ACME challenge of an unknown type.""" + __slots__ = ('challb', 'domain', 'identifier') # pylint: disable=redefined-slots-in-subclass + acme_type = challenges.Challenge diff --git a/certbot/src/certbot/compat/__init__.py b/certbot/src/certbot/compat/__init__.py new file mode 100644 index 00000000000..74451131a93 --- /dev/null +++ b/certbot/src/certbot/compat/__init__.py @@ -0,0 +1,6 @@ +""" +Compatibility layer to run certbot both on Linux and Windows. + +This package contains all logic that needs to be implemented specifically for Linux and for Windows. +Then the rest of certbot code relies on this module to be platform agnostic. +""" diff --git a/certbot/src/certbot/compat/_path.py b/certbot/src/certbot/compat/_path.py new file mode 100644 index 00000000000..bc37833af4c --- /dev/null +++ b/certbot/src/certbot/compat/_path.py @@ -0,0 +1,39 @@ +""" +This compat module wraps os.path to forbid some functions. + +isort:skip_file +""" + +# NB: Each function defined in compat._path is marked with "type: ignore" to avoid mypy +# to complain that a function is redefined (because we imported if first from os.path). + +# pylint: disable=function-redefined +from __future__ import absolute_import + +# First round of wrapping: we import statically all public attributes exposed by the os.path +# module. This allows in particular to have pylint, mypy, IDEs be aware that most of os.path +# members are available in certbot.compat.path. +from os.path import * # pylint: disable=wildcard-import,unused-wildcard-import,os-module-forbidden # noqa: F403 + +# Second round of wrapping: we import dynamically all attributes from the os.path module that have +# not yet been imported by the first round (static star import). +import os.path as std_os_path # pylint: disable=os-module-forbidden +import sys as std_sys + +ourselves = std_sys.modules[__name__] +for attribute in dir(std_os_path): + # Check if the attribute does not already exist in our module. It could be internal attributes + # of the module (__name__, __doc__), or attributes from standard os.path already imported with + # `from os.path import *`. + if not hasattr(ourselves, attribute): + setattr(ourselves, attribute, getattr(std_os_path, attribute)) + +# Clean all remaining importables that are not from the core os.path module. +del ourselves, std_os_path, std_sys + + +# Function os.path.realpath is broken on some versions of Python for Windows. +def realpath(*unused_args, **unused_kwargs): # type: ignore + """Method os.path.realpath() is forbidden""" + raise RuntimeError('Usage of os.path.realpath() is forbidden. ' + 'Use certbot.compat.filesystem.realpath() instead.') diff --git a/certbot/src/certbot/compat/filesystem.py b/certbot/src/certbot/compat/filesystem.py new file mode 100644 index 00000000000..b9224a432a9 --- /dev/null +++ b/certbot/src/certbot/compat/filesystem.py @@ -0,0 +1,731 @@ +"""Compat module to handle files security on Windows and Linux""" +from __future__ import absolute_import + +from contextlib import contextmanager +import errno +import os # pylint: disable=os-module-forbidden +import stat +from typing import Any +from typing import Generator +from typing import Optional + +try: + import ntsecuritycon + import pywintypes + import win32api + import win32con + import win32file + import win32security + import winerror +except ImportError: + POSIX_MODE = True +else: + POSIX_MODE = False + + +# Windows umask implementation, since Windows does not have a concept of umask by default. +# We choose 022 as initial value since it is the default one on most Linux distributions, and +# it is a decent choice to not have write permissions for group owner and everybody by default. +# We use a class here to avoid needing to define a global variable, and the potential mistakes +# that could happen with this kind of pattern. +class _WindowsUmask: + """Store the current umask to apply on Windows""" + def __init__(self) -> None: + self.mask = 0o022 + + +_WINDOWS_UMASK = _WindowsUmask() + + +def chmod(file_path: str, mode: int) -> None: + """ + Apply a POSIX mode on given file_path: + + - for Linux, the POSIX mode will be directly applied using chmod, + - for Windows, the POSIX mode will be translated into a Windows DACL that make sense for + Certbot context, and applied to the file using kernel calls. + + The definition of the Windows DACL that correspond to a POSIX mode, in the context of Certbot, + is explained at https://github.com/certbot/certbot/issues/6356 and is implemented by the + method `_generate_windows_flags()`. + + :param str file_path: Path of the file + :param int mode: POSIX mode to apply + """ + if POSIX_MODE: + os.chmod(file_path, mode) + else: + _apply_win_mode(file_path, mode) + + +def umask(mask: int) -> int: + """ + Set the current numeric umask and return the previous umask. On Linux, the built-in umask + method is used. On Windows, our Certbot-side implementation is used. + + :param int mask: The user file-creation mode mask to apply. + :rtype: int + :return: The previous umask value. + """ + if POSIX_MODE: + return os.umask(mask) + + previous_umask = _WINDOWS_UMASK.mask + _WINDOWS_UMASK.mask = mask + return previous_umask + + +@contextmanager +def temp_umask(mask: int) -> Generator[None, None, None]: + """ + Apply a umask temporarily, meant to be used in a `with` block. Uses the Certbot + implementation of umask. + + :param int mask: The user file-creation mode mask to apply temporarily + """ + old_umask: Optional[int] = None + try: + old_umask = umask(mask) + yield None + finally: + if old_umask is not None: + umask(old_umask) + + +# One could ask why there is no copy_ownership() function, or even a reimplementation +# of os.chown() that would modify the ownership of file without touching the mode itself. +# This is because on Windows, it would require recalculating the existing DACL against +# the new owner, since the DACL is composed of ACEs that targets a specific user, not dynamically +# the current owner of a file. This action would be necessary to keep consistency between +# the POSIX mode applied to the file and the current owner of this file. +# Since copying and editing arbitrary DACL is very difficult, and since we actually know +# the mode to apply at the time the owner of a file should change, it is easier to just +# change the owner, then reapply the known mode, as copy_ownership_and_apply_mode() does. +def copy_ownership_and_apply_mode(src: str, dst: str, mode: int, + copy_user: bool, copy_group: bool) -> None: + """ + Copy ownership (user and optionally group on Linux) from the source to the + destination, then apply given mode in compatible way for Linux and Windows. + This replaces the os.chown command. + + :param str src: Path of the source file + :param str dst: Path of the destination file + :param int mode: Permission mode to apply on the destination file + :param bool copy_user: Copy user if `True` + :param bool copy_group: Copy group if `True` on Linux (has no effect on Windows) + """ + if POSIX_MODE: + stats = os.stat(src) + user_id = stats.st_uid if copy_user else -1 + group_id = stats.st_gid if copy_group else -1 + # On Windows, os.chown does not exist. This is checked through POSIX_MODE value, + # but MyPy/PyLint does not know it and raises an error here on Windows. + # We disable specifically the check to fix the issue. + os.chown(dst, user_id, group_id) + elif copy_user: + # There is no group handling in Windows + _copy_win_ownership(src, dst) + + chmod(dst, mode) + + +# Quite similar to copy_ownership_and_apply_mode, but this time the DACL is copied from +# the source file on Windows. The DACL stays consistent with the dynamic rights of the +# equivalent POSIX mode, because ownership and mode are copied altogether on the destination +# file, so no recomputing of the DACL against the new owner is needed, as it would be +# for a copy_ownership alone method. +def copy_ownership_and_mode(src: str, dst: str, + copy_user: bool = True, copy_group: bool = True) -> None: + """ + Copy ownership (user and optionally group on Linux) and mode/DACL + from the source to the destination. + + :param str src: Path of the source file + :param str dst: Path of the destination file + :param bool copy_user: Copy user if `True` + :param bool copy_group: Copy group if `True` on Linux (has no effect on Windows) + """ + if POSIX_MODE: + # On Linux, we just delegate to chown and chmod. + stats = os.stat(src) + user_id = stats.st_uid if copy_user else -1 + group_id = stats.st_gid if copy_group else -1 + os.chown(dst, user_id, group_id) + chmod(dst, stats.st_mode) + else: + if copy_user: + # There is no group handling in Windows + _copy_win_ownership(src, dst) + _copy_win_mode(src, dst) + + +def check_mode(file_path: str, mode: int) -> bool: + """ + Check if the given mode matches the permissions of the given file. + On Linux, will make a direct comparison, on Windows, mode will be compared against + the security model. + + :param str file_path: Path of the file + :param int mode: POSIX mode to test + :rtype: bool + :return: True if the POSIX mode matches the file permissions + """ + if POSIX_MODE: + return stat.S_IMODE(os.stat(file_path).st_mode) == mode + + return _check_win_mode(file_path, mode) + + +def check_owner(file_path: str) -> bool: + """ + Check if given file is owned by current user. + + :param str file_path: File path to check + :rtype: bool + :return: True if given file is owned by current user, False otherwise. + """ + if POSIX_MODE: + return os.stat(file_path).st_uid == os.getuid() + + # Get owner sid of the file + security = win32security.GetFileSecurity(file_path, win32security.OWNER_SECURITY_INFORMATION) + user = security.GetSecurityDescriptorOwner() + + # Compare sids + return _get_current_user() == user + + +def check_permissions(file_path: str, mode: int) -> bool: + """ + Check if given file has the given mode and is owned by current user. + + :param str file_path: File path to check + :param int mode: POSIX mode to check + :rtype: bool + :return: True if file has correct mode and owner, False otherwise. + """ + return check_owner(file_path) and check_mode(file_path, mode) + + +def open(file_path: str, flags: int, mode: int = 0o777) -> int: # pylint: disable=redefined-builtin + """ + Wrapper of original os.open function, that will ensure on Windows that given mode + is correctly applied. + + :param str file_path: The file path to open + :param int flags: Flags to apply on file while opened + :param int mode: POSIX mode to apply on file when opened, + Python defaults will be applied if ``None`` + :returns: the file descriptor to the opened file + :rtype: int + :raise: OSError(errno.EEXIST) if the file already exists and os.O_CREAT & os.O_EXCL are set, + OSError(errno.EACCES) on Windows if the file already exists and is a directory, and + os.O_CREAT is set. + """ + if POSIX_MODE: + # On Linux, invoke os.open directly. + return os.open(file_path, flags, mode) + + # Windows: handle creation of the file atomically with proper permissions. + if flags & os.O_CREAT: + # If os.O_EXCL is set, we will use the "CREATE_NEW", that will raise an exception if + # file exists, matching the API contract of this bit flag. Otherwise, we use + # "CREATE_ALWAYS" that will always create the file whether it exists or not. + disposition = win32con.CREATE_NEW if flags & os.O_EXCL else win32con.CREATE_ALWAYS + + attributes = win32security.SECURITY_ATTRIBUTES() + security = attributes.SECURITY_DESCRIPTOR + user = _get_current_user() + dacl = _generate_dacl(user, mode, _WINDOWS_UMASK.mask) + # We set second parameter to 0 (`False`) to say that this security descriptor is + # NOT constructed from a default mechanism, but is explicitly set by the user. + # See https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-setsecuritydescriptorowner # pylint: disable=line-too-long + security.SetSecurityDescriptorOwner(user, 0) + # We set first parameter to 1 (`True`) to say that this security descriptor contains + # a DACL. Otherwise second and third parameters are ignored. + # We set third parameter to 0 (`False`) to say that this security descriptor is + # NOT constructed from a default mechanism, but is explicitly set by the user. + # See https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-setsecuritydescriptordacl # pylint: disable=line-too-long + security.SetSecurityDescriptorDacl(1, dacl, 0) + + handle = None + try: + handle = win32file.CreateFile(file_path, win32file.GENERIC_READ, + win32file.FILE_SHARE_READ & win32file.FILE_SHARE_WRITE, + attributes, disposition, 0, None) + except pywintypes.error as err: + # Handle native windows errors into python errors to be consistent with the API + # of os.open in the situation of a file already existing or locked. + if err.winerror == winerror.ERROR_FILE_EXISTS: + raise OSError(errno.EEXIST, err.strerror) + if err.winerror == winerror.ERROR_SHARING_VIOLATION: + raise OSError(errno.EACCES, err.strerror) + raise err + finally: + if handle: + handle.Close() + + # At this point, the file that did not exist has been created with proper permissions, + # so os.O_CREAT and os.O_EXCL are not needed anymore. We remove them from the flags to + # avoid a FileExists exception before calling os.open. + return os.open(file_path, flags ^ os.O_CREAT ^ os.O_EXCL) + + # Windows: general case, we call os.open, let exceptions be thrown, then chmod if all is fine. + fd = os.open(file_path, flags) + chmod(file_path, mode) + return fd + + +def makedirs(file_path: str, mode: int = 0o777) -> None: + """ + Rewrite of original os.makedirs function, that will ensure on Windows that given mode + is correctly applied. + + :param str file_path: The file path to open + :param int mode: POSIX mode to apply on leaf directory when created, Python defaults + will be applied if ``None`` + """ + current_umask = umask(0) + try: + # Since Python 3.7, os.makedirs does not set the given mode to the intermediate + # directories that could be created in the process. To keep things safe and consistent + # on all Python versions, we set the umask accordingly to have all directories + # (intermediate and leaf) created with the given mode. + umask(current_umask | 0o777 ^ mode) + + if POSIX_MODE: + return os.makedirs(file_path, mode) + + orig_mkdir_fn = os.mkdir + try: + # As we know that os.mkdir is called internally by os.makedirs, we will swap the + # function in os module for the time of makedirs execution on Windows. + os.mkdir = mkdir # type: ignore + return os.makedirs(file_path, mode) + finally: + os.mkdir = orig_mkdir_fn + finally: + umask(current_umask) + + +def mkdir(file_path: str, mode: int = 0o777) -> None: + """ + Rewrite of original os.mkdir function, that will ensure on Windows that given mode + is correctly applied. + + :param str file_path: The file path to open + :param int mode: POSIX mode to apply on directory when created, Python defaults + will be applied if ``None`` + """ + if POSIX_MODE: + return os.mkdir(file_path, mode) + + attributes = win32security.SECURITY_ATTRIBUTES() + security = attributes.SECURITY_DESCRIPTOR + user = _get_current_user() + dacl = _generate_dacl(user, mode, _WINDOWS_UMASK.mask) + security.SetSecurityDescriptorOwner(user, False) + security.SetSecurityDescriptorDacl(1, dacl, 0) + + try: + win32file.CreateDirectory(file_path, attributes) + except pywintypes.error as err: + # Handle native windows error into python error to be consistent with the API + # of os.mkdir in the situation of a directory already existing. + if err.winerror == winerror.ERROR_ALREADY_EXISTS: + raise OSError(errno.EEXIST, err.strerror, file_path, err.winerror) + raise err + + return None + + +def replace(src: str, dst: str) -> None: + """ + Rename a file to a destination path and handles situations where the destination exists. + + :param str src: The current file path. + :param str dst: The new file path. + """ + if hasattr(os, 'replace'): + # Use replace if possible. Since we don't support Python 2 on Windows + # and os.replace() was added in Python 3.3, we can assume that + # os.replace() is always available on Windows. + getattr(os, 'replace')(src, dst) + else: + # Otherwise, use os.rename() that behaves like os.replace() on Linux. + os.rename(src, dst) + + +def realpath(file_path: str) -> str: + """ + Find the real path for the given path. This method resolves symlinks, including + recursive symlinks, and is protected against symlinks that creates an infinite loop. + + :param str file_path: The path to resolve + :returns: The real path for the given path + :rtype: str + """ + original_path = file_path + + # os.path.realpath also resolves symlinks + path = os.path.realpath(file_path) + if os.path.islink(path): + # If path returned by realpath is still a link, it means that it failed to + # resolve the symlink because of a loop. + # See realpath code: https://github.com/python/cpython/blob/master/Lib/posixpath.py + raise RuntimeError('Error, link {0} is a loop!'.format(original_path)) + return path + + +def readlink(link_path: str) -> str: + """ + Return a string representing the path to which the symbolic link points. + + :param str link_path: The symlink path to resolve + :return: The path the symlink points to + :returns: str + :raise: ValueError if a long path (260> characters) is encountered on Windows + """ + path = os.readlink(link_path) + + if POSIX_MODE or not path.startswith('\\\\?\\'): + return path + + # At this point, we know we are on Windows and that the path returned uses + # the extended form which begins with the prefix \\?\ + + # Max length of a normal path is 260 characters on Windows, including the non printable + # termination character "". The termination character is not included in Python + # strings, giving a max length of 259 characters, + 4 characters for the extended form + # prefix, to an effective max length 263 characters on a string representing a normal path. + if len(path) < 264: + return path[4:] + + raise ValueError("Long paths are not supported by Certbot on Windows.") + + +# On Windows is_executable run from an unprivileged shell may claim that a path is +# executable when it is executable only if run from a privileged shell. This result +# is due to the fact that GetEffectiveRightsFromAcl calculate effective rights +# without taking into consideration if the target user has currently required the +# elevated privileges or not. However this is not a problem since certbot always +# requires to be run under a privileged shell, so the user will always benefit +# from the highest (privileged one) set of permissions on a given file. +def is_executable(path: str) -> bool: + """ + Is path an executable file? + + :param str path: path to test + :return: True if path is an executable file + :rtype: bool + """ + if POSIX_MODE: + return os.path.isfile(path) and os.access(path, os.X_OK) + + return _win_is_executable(path) + + +def has_world_permissions(path: str) -> bool: + """ + Check if everybody/world has any right (read/write/execute) on a file given its path. + + :param str path: path to test + :return: True if everybody/world has any right to the file + :rtype: bool + """ + if POSIX_MODE: + return bool(stat.S_IMODE(os.stat(path).st_mode) & stat.S_IRWXO) + + security = win32security.GetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION) + dacl = security.GetSecurityDescriptorDacl() + + return bool(dacl.GetEffectiveRightsFromAcl({ + 'TrusteeForm': win32security.TRUSTEE_IS_SID, + 'TrusteeType': win32security.TRUSTEE_IS_USER, + 'Identifier': win32security.ConvertStringSidToSid('S-1-1-0'), + })) + + +def compute_private_key_mode(old_key: str, base_mode: int) -> int: + """ + Calculate the POSIX mode to apply to a private key given the previous private key. + + :param str old_key: path to the previous private key + :param int base_mode: the minimum modes to apply to a private key + :return: the POSIX mode to apply + :rtype: int + """ + if POSIX_MODE: + # On Linux, we keep read/write/execute permissions + # for group and read permissions for everybody. + old_mode = (stat.S_IMODE(os.stat(old_key).st_mode) & + (stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH)) + return base_mode | old_mode + + # On Windows, the mode returned by os.stat is not reliable, + # so we do not keep any permission from the previous private key. + return base_mode + + +def has_same_ownership(path1: str, path2: str) -> bool: + """ + Return True if the ownership of two files given their respective path is the same. + On Windows, ownership is checked against owner only, since files do not have a group owner. + + :param str path1: path to the first file + :param str path2: path to the second file + :return: True if both files have the same ownership, False otherwise + :rtype: bool + + """ + if POSIX_MODE: + stats1 = os.stat(path1) + stats2 = os.stat(path2) + return (stats1.st_uid, stats1.st_gid) == (stats2.st_uid, stats2.st_gid) + + security1 = win32security.GetFileSecurity(path1, win32security.OWNER_SECURITY_INFORMATION) + user1 = security1.GetSecurityDescriptorOwner() + + security2 = win32security.GetFileSecurity(path2, win32security.OWNER_SECURITY_INFORMATION) + user2 = security2.GetSecurityDescriptorOwner() + + return user1 == user2 + + +def has_min_permissions(path: str, min_mode: int) -> bool: + """ + Check if a file given its path has at least the permissions defined by the given minimal mode. + On Windows, group permissions are ignored since files do not have a group owner. + + :param str path: path to the file to check + :param int min_mode: the minimal permissions expected + :return: True if the file matches the minimal permissions expectations, False otherwise + :rtype: bool + """ + if POSIX_MODE: + st_mode = os.stat(path).st_mode + return st_mode == st_mode | min_mode + + # Resolve symlinks, to get a consistent result with os.stat on Linux, + # that follows symlinks by default. + path = realpath(path) + + # Get owner sid of the file + security = win32security.GetFileSecurity( + path, win32security.OWNER_SECURITY_INFORMATION | win32security.DACL_SECURITY_INFORMATION) + user = security.GetSecurityDescriptorOwner() + dacl = security.GetSecurityDescriptorDacl() + min_dacl = _generate_dacl(user, min_mode) + + for index in range(min_dacl.GetAceCount()): + min_ace = min_dacl.GetAce(index) + + # On a given ACE, index 0 is the ACE type, 1 is the permission mask, and 2 is the SID. + # See: http://timgolden.me.uk/pywin32-docs/PyACL__GetAce_meth.html + mask = min_ace[1] + user = min_ace[2] + + effective_mask = dacl.GetEffectiveRightsFromAcl({ + 'TrusteeForm': win32security.TRUSTEE_IS_SID, + 'TrusteeType': win32security.TRUSTEE_IS_USER, + 'Identifier': user, + }) + + if effective_mask != effective_mask | mask: + return False + + return True + + +def _win_is_executable(path: str) -> bool: + if not os.path.isfile(path): + return False + + security = win32security.GetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION) + dacl = security.GetSecurityDescriptorDacl() + + mode = dacl.GetEffectiveRightsFromAcl({ + 'TrusteeForm': win32security.TRUSTEE_IS_SID, + 'TrusteeType': win32security.TRUSTEE_IS_USER, + 'Identifier': _get_current_user(), + }) + + return mode & ntsecuritycon.FILE_GENERIC_EXECUTE == ntsecuritycon.FILE_GENERIC_EXECUTE + + +def _apply_win_mode(file_path: str, mode: int) -> None: + """ + This function converts the given POSIX mode into a Windows ACL list, and applies it to the + file given its path. If the given path is a symbolic link, it will resolved to apply the + mode on the targeted file. + """ + file_path = realpath(file_path) + # Get owner sid of the file + security = win32security.GetFileSecurity(file_path, win32security.OWNER_SECURITY_INFORMATION) + user = security.GetSecurityDescriptorOwner() + + # New DACL, that will overwrite existing one (including inherited permissions) + dacl = _generate_dacl(user, mode) + + # Apply the new DACL + security.SetSecurityDescriptorDacl(1, dacl, 0) + win32security.SetFileSecurity(file_path, win32security.DACL_SECURITY_INFORMATION, security) + + +def _generate_dacl(user_sid: Any, mode: int, mask: Optional[int] = None) -> Any: + if mask: + mode = mode & (0o777 - mask) + analysis = _analyze_mode(mode) + + # Get standard accounts from "well-known" sid + # See the list here: + # https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems + system = win32security.ConvertStringSidToSid('S-1-5-18') + admins = win32security.ConvertStringSidToSid('S-1-5-32-544') + everyone = win32security.ConvertStringSidToSid('S-1-1-0') + + # New dacl, without inherited permissions + dacl = win32security.ACL() + + # If user is already system or admins, any ACE defined here would be superseded by + # the full control ACE that will be added after. + if user_sid not in [system, admins]: + # Handle user rights + user_flags = _generate_windows_flags(analysis['user']) + if user_flags: + dacl.AddAccessAllowedAce(win32security.ACL_REVISION, user_flags, user_sid) + + # Handle everybody rights + everybody_flags = _generate_windows_flags(analysis['all']) + if everybody_flags: + dacl.AddAccessAllowedAce(win32security.ACL_REVISION, everybody_flags, everyone) + + # Handle administrator rights + full_permissions = _generate_windows_flags({'read': True, 'write': True, 'execute': True}) + dacl.AddAccessAllowedAce(win32security.ACL_REVISION, full_permissions, system) + dacl.AddAccessAllowedAce(win32security.ACL_REVISION, full_permissions, admins) + + return dacl + + +def _analyze_mode(mode: int) -> dict[str, dict[str, int]]: + return { + 'user': { + 'read': mode & stat.S_IRUSR, + 'write': mode & stat.S_IWUSR, + 'execute': mode & stat.S_IXUSR, + }, + 'all': { + 'read': mode & stat.S_IROTH, + 'write': mode & stat.S_IWOTH, + 'execute': mode & stat.S_IXOTH, + }, + } + + +def _copy_win_ownership(src: str, dst: str) -> None: + # Resolve symbolic links + src = realpath(src) + + security_src = win32security.GetFileSecurity(src, win32security.OWNER_SECURITY_INFORMATION) + user_src = security_src.GetSecurityDescriptorOwner() + + security_dst = win32security.GetFileSecurity(dst, win32security.OWNER_SECURITY_INFORMATION) + # Second parameter indicates, if `False`, that the owner of the file is not provided by some + # default mechanism, but is explicitly set instead. This is obviously what we are doing here. + security_dst.SetSecurityDescriptorOwner(user_src, False) + + win32security.SetFileSecurity(dst, win32security.OWNER_SECURITY_INFORMATION, security_dst) + + +def _copy_win_mode(src: str, dst: str) -> None: + # Resolve symbolic links + src = realpath(src) + + # Copy the DACL from src to dst. + security_src = win32security.GetFileSecurity(src, win32security.DACL_SECURITY_INFORMATION) + dacl = security_src.GetSecurityDescriptorDacl() + + security_dst = win32security.GetFileSecurity(dst, win32security.DACL_SECURITY_INFORMATION) + security_dst.SetSecurityDescriptorDacl(1, dacl, 0) + win32security.SetFileSecurity(dst, win32security.DACL_SECURITY_INFORMATION, security_dst) + + +def _generate_windows_flags(rights_desc: dict[str, int]) -> int: + # Some notes about how each POSIX right is interpreted. + # + # For the rights read and execute, we have a pretty bijective relation between + # POSIX flags and their generic counterparts on Windows, so we use them directly + # (respectively ntsecuritycon.FILE_GENERIC_READ and ntsecuritycon.FILE_GENERIC_EXECUTE). + # + # But ntsecuritycon.FILE_GENERIC_WRITE does not correspond to what one could expect from a + # write access on Linux: for Windows, FILE_GENERIC_WRITE does not include delete, move or + # rename. This is something that requires ntsecuritycon.FILE_ALL_ACCESS. + # So to reproduce the write right as POSIX, we will apply ntsecuritycon.FILE_ALL_ACCESS + # subtracted of the rights corresponding to POSIX read and POSIX execute. + # + # Finally, having read + write + execute gives a ntsecuritycon.FILE_ALL_ACCESS, + # so a "Full Control" on the file. + # + # A complete list of the rights defined on NTFS can be found here: + # https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2003/cc783530(v=ws.10)#permissions-for-files-and-folders + flag = 0 + if rights_desc['read']: + flag = flag | ntsecuritycon.FILE_GENERIC_READ + if rights_desc['write']: + flag = flag | (ntsecuritycon.FILE_ALL_ACCESS + ^ ntsecuritycon.FILE_GENERIC_READ + ^ ntsecuritycon.FILE_GENERIC_EXECUTE) + if rights_desc['execute']: + flag = flag | ntsecuritycon.FILE_GENERIC_EXECUTE + + return flag + + +def _check_win_mode(file_path: str, mode: int) -> bool: + # Resolve symbolic links + file_path = realpath(file_path) + # Get current dacl file + security = win32security.GetFileSecurity(file_path, win32security.OWNER_SECURITY_INFORMATION + | win32security.DACL_SECURITY_INFORMATION) + dacl = security.GetSecurityDescriptorDacl() + + # Get current file owner sid + user = security.GetSecurityDescriptorOwner() + + if not dacl: + # No DACL means full control to everyone + # This is not a deterministic permissions set. + return False + + # Calculate the target dacl + ref_dacl = _generate_dacl(user, mode) + + return _compare_dacls(dacl, ref_dacl) + + +def _compare_dacls(dacl1: Any, dacl2: Any) -> bool: + """ + This method compare the two given DACLs to check if they are identical. + Identical means here that they contains the same set of ACEs in the same order. + """ + return ([dacl1.GetAce(index) for index in range(dacl1.GetAceCount())] == + [dacl2.GetAce(index) for index in range(dacl2.GetAceCount())]) + + +def _get_current_user() -> Any: + """ + Return the pySID corresponding to the current user. + """ + # We craft the account_name ourselves instead of calling for instance win32api.GetUserNameEx, + # because this function returns nonsense values when Certbot is run under NT AUTHORITY\SYSTEM. + # To run Certbot under NT AUTHORITY\SYSTEM, you can open a shell using the instructions here: + # https://blogs.technet.microsoft.com/ben_parker/2010/10/27/how-do-i-run-powershell-execommand-prompt-as-the-localsystem-account-on-windows-7/ + account_name = r"{0}\{1}".format(win32api.GetDomainName(), win32api.GetUserName()) + # LookupAccountName() expects the system name as first parameter. By passing None to it, + # we instruct Windows to first search the matching account in the machine local accounts, + # then into the primary domain accounts, if the machine has joined a domain, then finally + # into the trusted domains accounts. This is the preferred lookup mechanism to use in Windows + # if there is no reason to use a specific lookup mechanism. + # See https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-lookupaccountnamea + return win32security.LookupAccountName(None, account_name)[0] diff --git a/certbot/src/certbot/compat/misc.py b/certbot/src/certbot/compat/misc.py new file mode 100644 index 00000000000..96e6890b5a0 --- /dev/null +++ b/certbot/src/certbot/compat/misc.py @@ -0,0 +1,168 @@ +""" +This compat module handles various platform specific calls that do not fall into one +particular category. +""" +from __future__ import absolute_import + +import logging +import select +import subprocess +import sys +from typing import Optional + +from certbot import errors +from certbot.compat import os + +try: + from pywintypes import error as pywinerror + from win32com.shell import shell as shellwin32 + from win32console import GetStdHandle + from win32console import STD_OUTPUT_HANDLE + POSIX_MODE = False +except ImportError: # pragma: no cover + POSIX_MODE = True + + +logger = logging.getLogger(__name__) + +# For Linux: define OS specific standard binary directories +STANDARD_BINARY_DIRS = ["/usr/sbin", "/usr/local/bin", "/usr/local/sbin"] if POSIX_MODE else [] + + +def raise_for_non_administrative_windows_rights() -> None: + """ + On Windows, raise if current shell does not have the administrative rights. + Do nothing on Linux. + + :raises .errors.Error: If the current shell does not have administrative rights on Windows. + """ + if not POSIX_MODE and shellwin32.IsUserAnAdmin() == 0: # pragma: no cover + raise errors.Error('Error, certbot must be run on a shell with administrative rights.') + + +def prepare_virtual_console() -> None: + """ + On Windows, ensure that Console Virtual Terminal Sequences are enabled. + + """ + if POSIX_MODE: + return + + # https://docs.microsoft.com/en-us/windows/console/setconsolemode + ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 + + # stdout/stderr will be the same console screen buffer, but this could return None or raise + try: + h = GetStdHandle(STD_OUTPUT_HANDLE) + if h: + h.SetConsoleMode(h.GetConsoleMode() | ENABLE_VIRTUAL_TERMINAL_PROCESSING) + except pywinerror: + logger.debug("Failed to set console mode", exc_info=True) + + +def readline_with_timeout(timeout: float, prompt: Optional[str]) -> str: + """ + Read user input to return the first line entered, or raise after specified timeout. + + :param float timeout: The timeout in seconds given to the user. + :param str prompt: The prompt message to display to the user. + + :returns: The first line entered by the user. + :rtype: str + + """ + try: + # Linux specific + # + # Call to select can only be done like this on UNIX + rlist, _, _ = select.select([sys.stdin], [], [], timeout) + if not rlist: + raise errors.Error( + "Timed out waiting for answer to prompt '{0}'".format(prompt if prompt else "")) + return rlist[0].readline() + except OSError: + # Windows specific + # + # No way with select to make a timeout to the user input on Windows, + # as select only supports socket in this case. + # So no timeout on Windows for now. + return sys.stdin.readline() + + +WINDOWS_DEFAULT_FOLDERS = { + 'config': 'C:\\Certbot', + 'work': 'C:\\Certbot\\lib', + 'logs': 'C:\\Certbot\\log', +} +LINUX_DEFAULT_FOLDERS = { + 'config': '/etc/letsencrypt', + 'work': '/var/lib/letsencrypt', + 'logs': '/var/log/letsencrypt', +} + + +def get_default_folder(folder_type: str) -> str: + """ + Return the relevant default folder for the current OS + + :param str folder_type: The type of folder to retrieve (config, work or logs) + + :returns: The relevant default folder. + :rtype: str + + """ + if os.name != 'nt': + # Linux specific + return LINUX_DEFAULT_FOLDERS[folder_type] + # Windows specific + return WINDOWS_DEFAULT_FOLDERS[folder_type] + + +def underscores_for_unsupported_characters_in_path(path: str) -> str: + """ + Replace unsupported characters in path for current OS by underscores. + :param str path: the path to normalize + :return: the normalized path + :rtype: str + """ + if os.name != 'nt': + # Linux specific + return path + + # Windows specific + drive, tail = os.path.splitdrive(path) + return drive + tail.replace(':', '_') + + +def execute_command_status(cmd_name: str, shell_cmd: str, + env: Optional[dict] = None) -> tuple[int, str, str]: + """ + Run a command: + - on Linux command will be run by the standard shell selected with + subprocess.run(shell=True) + - on Windows command will be run in a Powershell shell + + This function returns the exit code, and does not log the result and output + of the command. + + :param str cmd_name: the user facing name of the hook being run + :param str shell_cmd: shell command to execute + :param dict env: environ to pass into subprocess.run + + :returns: `tuple` (`int` returncode, `str` stderr, `str` stdout) + """ + logger.info("Running %s command: %s", cmd_name, shell_cmd) + + if POSIX_MODE: + proc = subprocess.run(shell_cmd, shell=True, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, universal_newlines=True, + check=False, env=env) + else: + line = ['powershell.exe', '-Command', shell_cmd] + proc = subprocess.run(line, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + universal_newlines=True, check=False, env=env) + + # universal_newlines causes stdout and stderr to be str objects instead of + # bytes in Python 3 + out, err = proc.stdout, proc.stderr + return proc.returncode, err, out diff --git a/certbot/src/certbot/compat/os.py b/certbot/src/certbot/compat/os.py new file mode 100644 index 00000000000..82f5704591a --- /dev/null +++ b/certbot/src/certbot/compat/os.py @@ -0,0 +1,172 @@ +""" +This compat modules is a wrapper of the core os module that forbids usage of specific operations +(e.g. chown, chmod, getuid) that would be harmful to the Windows file security model of Certbot. +This module is intended to replace standard os module throughout certbot projects (except acme). + +This module has the same API as the os module in the Python standard library +except for the functions defined below. + +isort:skip_file +""" + +# NB1: If adding a new documented function to compat.os, ensure that it is added to the +# ':members:' list in certbot/docs/api/certbot.compat.os.rst. + +# NB2: Each function defined in compat.os is marked with "type: ignore" to avoid mypy +# to complain that a function is redefined (because we imported if first from os). + +# pylint: disable=function-redefined +from __future__ import absolute_import + +# First round of wrapping: we import statically all public attributes exposed by the os module +# This allows in particular to have pylint, mypy, IDEs be aware that most of os members are +# available in certbot.compat.os. +from os import * # pylint: disable=wildcard-import,unused-wildcard-import,redefined-builtin,os-module-forbidden # noqa: F403 + +# Second round of wrapping: we import dynamically all attributes from the os module that have not +# yet been imported by the first round (static import). This covers in particular the case of +# specific python 3.x versions where not all public attributes are in the special __all__ of os, +# and so not in `from os import *`. +import os as std_os # pylint: disable=os-module-forbidden +import sys as std_sys + +ourselves = std_sys.modules[__name__] +# Adding all of stdlib os to this module confuses Sphinx so we skip this when +# building the documentation. +if not std_os.environ.get("CERTBOT_DOCS") == "1": + for attribute in dir(std_os): + # Check if the attribute does not already exist in our module. It could + # be internal attributes of the module (__name__, __doc__), or + # attributes from standard os already imported with `from os import *`. + if not hasattr(ourselves, attribute): + setattr(ourselves, attribute, getattr(std_os, attribute)) + +# Import our internal path module, then allow certbot.compat.os.path +# to behave as a module (similarly to os.path). +from certbot.compat import _path as path # type: ignore # pylint: disable=wrong-import-position # noqa: E402 +std_sys.modules[__name__ + '.path'] = path + +# Clean all remaining importables that are not from the core os module. +del ourselves, std_os, std_sys + + +# Chmod is the root of all evil for our security model on Windows. With the default implementation +# of os.chmod on Windows, almost all bits on mode will be ignored, and only a general RO or RW will +# be applied. The DACL, the inner mechanism to control file access on Windows, will stay on its +# default definition, giving effectively at least read permissions to any one, as the default +# permissions on root path will be inherit by the file (as NTFS state), and root path can be read +# by anyone. So the given mode needs to be translated into a secured and not inherited DACL that +# will be applied to this file using filesystem.chmod, calling internally the win32security +# module to construct and apply the DACL. Complete security model to translate a POSIX mode into +# a suitable DACL on Windows for Certbot can be found here: +# https://github.com/certbot/certbot/issues/6356 +# Basically, it states that appropriate permissions will be set for the owner, nothing for the +# group, appropriate permissions for the "Everyone" group, and all permissions to the +# "Administrators" group + "System" user, as they can do everything anyway. +def chmod(*unused_args, **unused_kwargs): # type: ignore + """Method os.chmod() is forbidden""" + raise RuntimeError('Usage of os.chmod() is forbidden. ' + 'Use certbot.compat.filesystem.chmod() instead.') + + +# Since there is no mode on Windows, there is no umask either, and so this method is a noop for +# this platform. In order to have a consistent behavior between Linux and Windows on Certbot files +# and directories, the filesystem umask method must be used instead, since it implements umask for +# Windows. +def umask(*unused_args, **unused_kwargs): # type: ignore + """Method os.chmod() is forbidden""" + raise RuntimeError('Usage of os.umask() is forbidden. ' + 'Use certbot.compat.filesystem.umask() instead.') + + +# Because uid is not a concept on Windows, chown is useless. In fact, it is not even available +# on Python for Windows. So to be consistent on both platforms for Certbot, this method is +# always forbidden. +def chown(*unused_args, **unused_kwargs): # type: ignore + """Method os.chown() is forbidden""" + raise RuntimeError('Usage of os.chown() is forbidden.' + 'Use certbot.compat.filesystem.copy_ownership_and_apply_mode() instead.') + + +# The os.open function on Windows has the same effect as a call to os.chown concerning the file +# modes: these modes lack the correct control over the permissions given to the file. Instead, +# filesystem.open invokes the Windows native API `CreateFile` to ensure that permissions are +# atomically set in case of file creation, or invokes filesystem.chmod to properly set the +# permissions for the other cases. +def open(*unused_args, **unused_kwargs): # type: ignore + """Method os.open() is forbidden""" + raise RuntimeError('Usage of os.open() is forbidden. ' + 'Use certbot.compat.filesystem.open() instead.') + + +# Very similarly to os.open, os.mkdir has the same effects on Windows and creates an unsecured +# folder. So a similar mitigation to security.chmod is provided on this platform. +def mkdir(*unused_args, **unused_kwargs): # type: ignore + """Method os.mkdir() is forbidden""" + raise RuntimeError('Usage of os.mkdir() is forbidden. ' + 'Use certbot.compat.filesystem.mkdir() instead.') + + +# As said above, os.makedirs would call the original os.mkdir function recursively on Windows, +# creating the same flaws for every actual folder created. This method is modified to ensure +# that our modified os.mkdir is called on Windows, by monkey patching temporarily the mkdir method +# on the original os module, executing the modified logic to correctly protect newly created +# folders, then restoring original mkdir method in the os module. +def makedirs(*unused_args, **unused_kwargs): # type: ignore + """Method os.makedirs() is forbidden""" + raise RuntimeError('Usage of os.makedirs() is forbidden. ' + 'Use certbot.compat.filesystem.makedirs() instead.') + + +# Because of the blocking strategy on file handlers on Windows, rename does not behave as expected +# with POSIX systems: an exception will be raised if dst already exists. +def rename(*unused_args, **unused_kwargs): # type: ignore + """Method os.rename() is forbidden""" + raise RuntimeError('Usage of os.rename() is forbidden. ' + 'Use certbot.compat.filesystem.replace() instead.') + + +# Behavior of os.replace is consistent between Windows and Linux. However, it is not supported on +# Python 2.x. So, as for os.rename, we forbid it in favor of filesystem.replace. +def replace(*unused_args, **unused_kwargs): # type: ignore + """Method os.replace() is forbidden""" + raise RuntimeError('Usage of os.replace() is forbidden. ' + 'Use certbot.compat.filesystem.replace() instead.') + + +# Results given by os.access are inconsistent or partial on Windows, because this platform is not +# following the POSIX approach. +def access(*unused_args, **unused_kwargs): # type: ignore + """Method os.access() is forbidden""" + raise RuntimeError('Usage of os.access() is forbidden. ' + 'Use certbot.compat.filesystem.check_mode() or ' + 'certbot.compat.filesystem.is_executable() instead.') + + +# On Windows os.stat call result is inconsistent, with a lot of flags that are not set or +# meaningless. We need to use specialized functions from the certbot.compat.filesystem module. +def stat(*unused_args, **unused_kwargs): # type: ignore + """Method os.stat() is forbidden""" + raise RuntimeError('Usage of os.stat() is forbidden. ' + 'Use certbot.compat.filesystem functions instead ' + '(eg. has_min_permissions, has_same_ownership).') + + +# Method os.fstat has the same problem than os.stat, since it is the same function, +# but accepting a file descriptor instead of a path. +def fstat(*unused_args, **unused_kwargs): # type: ignore + """Method os.stat() is forbidden""" + raise RuntimeError('Usage of os.fstat() is forbidden. ' + 'Use certbot.compat.filesystem functions instead ' + '(eg. has_min_permissions, has_same_ownership).') + + +# On Windows, os.readlink "typically" returns the resolved path in its "extended-style" form which +# allows use of more than 259 characters and its string representation is prepended with "\\?\". See +# https://docs.python.org/3/library/os.html#os.readlink. This causes problems for us because the +# behavior is not consistent (see https://github.com/certbot/certbot/pull/10136) and paths that have +# this prefix won't match those that do not in simple equality comparisons. +def readlink(*unused_args, **unused_kwargs): # type: ignore + """Method os.readlink() is forbidden""" + raise RuntimeError('Usage of os.readlink() is forbidden. ' + 'Use certbot.compat.filesystem.realpath() instead.') diff --git a/certbot/src/certbot/configuration.py b/certbot/src/certbot/configuration.py new file mode 100644 index 00000000000..2640c01f31e --- /dev/null +++ b/certbot/src/certbot/configuration.py @@ -0,0 +1,501 @@ +"""Certbot user-supplied configuration.""" +import argparse +import copy +import enum +import logging +from typing import Any +from typing import Optional +from urllib import parse + +from certbot import errors +from certbot._internal import constants +from certbot.compat import misc +from certbot.compat import os + + +logger = logging.getLogger(__name__) + + +class ArgumentSource(enum.Enum): + """Enum for describing where a configuration argument was set.""" + + COMMAND_LINE = enum.auto() + """Argument was specified on the command line""" + CONFIG_FILE = enum.auto() + """Argument was specified in a .ini config file""" + DEFAULT = enum.auto() + """Argument was not set by the user, and was assigned its default value""" + ENV_VAR = enum.auto() + """Argument was specified in an environment variable""" + RUNTIME = enum.auto() + """Argument was set at runtime by certbot""" + + +class NamespaceConfig: + """Configuration wrapper around :class:`argparse.Namespace`. + + Please note that the following attributes are dynamically resolved using + :attr:`~certbot.configuration.NamespaceConfig.work_dir` and relative + paths defined in :py:mod:`certbot._internal.constants`: + + - `accounts_dir` + - `in_progress_dir` + - `temp_checkpoint_dir` + + And the following paths are dynamically resolved using + :attr:`~certbot.configuration.NamespaceConfig.config_dir` and relative + paths defined in :py:mod:`certbot._internal.constants`: + + - `default_archive_dir` + - `live_dir` + - `renewal_configs_dir` + + :ivar namespace: Namespace typically produced by + :meth:`argparse.ArgumentParser.parse_args`. + :type namespace: :class:`argparse.Namespace` + + """ + + def __init__(self, namespace: argparse.Namespace) -> None: + self.namespace: argparse.Namespace + # Avoid recursion loop because of the delegation defined in __setattr__ + object.__setattr__(self, 'namespace', namespace) + object.__setattr__(self, '_argument_sources', None) + object.__setattr__(self, '_previously_accessed_mutables', {}) + + self.namespace.config_dir = os.path.abspath(self.namespace.config_dir) + self.namespace.work_dir = os.path.abspath(self.namespace.work_dir) + self.namespace.logs_dir = os.path.abspath(self.namespace.logs_dir) + + # Check command line parameters sanity, and error out in case of problem. + _check_config_sanity(self) + + def set_argument_sources(self, argument_sources: dict[str, ArgumentSource]) -> None: + """ + Associate the NamespaceConfig with a dictionary describing where each of + its arguments came from, e.g. `{ 'email': ArgumentSource.CONFIG_FILE }`. + This is necessary for making runtime evaluations on whether an argument + was specified by the user or not (see `set_by_user`). + + For an example of how to build such a dictionary, see + `certbot._internal.cli.helpful.HelpfulArgumentParser._build_sources_dict` + + :ivar argument_sources: dictionary of argument names to their :class:`ArgumentSource` + :type argument_sources: :class:`Dict[str, ArgumentSource]` + """ + + # Avoid recursion loop because of the delegation defined in __setattr__ + object.__setattr__(self, '_argument_sources', argument_sources) + + + def set_by_user(self, var: str) -> bool: + """ + Return True if a particular config variable has been set by the user + (via CLI or config file) including if the user explicitly set it to the + default, or if it was dynamically set at runtime. Returns False if the + variable was assigned a default value. + + Raises an exception if `argument_sources` is not set. + """ + from certbot._internal.cli.cli_constants import DEPRECATED_OPTIONS + from certbot._internal.cli.cli_constants import VAR_MODIFIERS + from certbot._internal.plugins import selection + + if self.argument_sources is None: + raise RuntimeError( + "NamespaceConfig.set_by_user called without an ArgumentSources dict. " + "See NamespaceConfig.set_argument_sources().") + + # We should probably never actually hit this code. But if we do, + # a deprecated option has logically never been set by the CLI. + if var in DEPRECATED_OPTIONS: + return False + + if var in ['authenticator', 'installer']: + auth, inst = selection.cli_plugin_requests(self) + if var == 'authenticator': + return auth is not None + if var == 'installer': + return inst is not None + + if var in self.argument_sources and self.argument_sources[var] != ArgumentSource.DEFAULT: + logger.debug("Var %s=%s (set by user).", var, getattr(self, var)) + return True + + for modifier in VAR_MODIFIERS.get(var, []): + if self.set_by_user(modifier): + logger.debug("Var %s=%s (set by user).", + var, VAR_MODIFIERS.get(var, [])) + return True + + return False + + def to_dict(self) -> dict[str, Any]: + """ + Returns a dictionary mapping all argument names to their values + """ + return vars(self.namespace) + + def _mark_runtime_override(self, name: str) -> None: + """ + If an argument_sources dict was set, overwrites an argument's source to + be ArgumentSource.RUNTIME. Used when certbot sets an argument's values + at runtime. This also clears the modified value from + _previously_accessed_mutables since it is no longer needed. + """ + if self._argument_sources is not None: + self._argument_sources[name] = ArgumentSource.RUNTIME + if name in self._previously_accessed_mutables: + del self._previously_accessed_mutables[name] + + @property + def argument_sources(self) -> Optional[dict[str, ArgumentSource]]: + """Returns _argument_sources after handling any changes to accessed mutable values.""" + # We keep values in _previously_accessed_mutables until we've detected a modification to try + # to provide up-to-date information when argument_sources is accessed. Once a mutable object + # has been accessed, it can be modified at any time if a reference to it was kept somewhere + # else. + + # We copy _previously_accessed_mutables because _mark_runtime_override modifies it. + for name, prev_value in self._previously_accessed_mutables.copy().items(): + current_value = getattr(self.namespace, name) + if current_value != prev_value: + self._mark_runtime_override(name) + return self._argument_sources + + # Delegate any attribute not explicitly defined to the underlying namespace object. + # + # If any mutable namespace attributes are explicitly defined in the future, you'll probably want + # to take an approach like the one used in __getattr__ and the argument_sources property. + + def __getattr__(self, name: str) -> Any: + arg_sources = self.argument_sources + value = getattr(self.namespace, name) + if arg_sources is not None: + # If the requested attribute was already modified at runtime, we don't need to track any + # future changes. + if name not in arg_sources or arg_sources[name] != ArgumentSource.RUNTIME: + # If name is already in _previously_accessed_mutables, we don't need to make a copy + # of it again. If its value was changed, this would have been caught while preparing + # the return value of the property self.argument_sources accessed earlier in this + # function. + if name not in self._previously_accessed_mutables and not _is_immutable(value): + self._previously_accessed_mutables[name] = copy.deepcopy(value) + return value + + def __setattr__(self, name: str, value: Any) -> None: + self._mark_runtime_override(name) + setattr(self.namespace, name, value) + + @property + def server(self) -> str: + """ACME Directory Resource URI.""" + return self.namespace.server + + @server.setter + def server(self, server_: str) -> None: + self._mark_runtime_override('server') + self.namespace.server = server_ + + @property + def email(self) -> Optional[str]: + """Email used for registration and recovery contact. + + Use comma to register multiple emails, + ex: u1@example.com,u2@example.com. (default: Ask). + """ + return self.namespace.email + + @email.setter + def email(self, mail: str) -> None: + self._mark_runtime_override('email') + self.namespace.email = mail + + @property + def rsa_key_size(self) -> int: + """Size of the RSA key.""" + return self.namespace.rsa_key_size + + @rsa_key_size.setter + def rsa_key_size(self, ksize: int) -> None: + """Set the rsa_key_size property""" + self._mark_runtime_override('rsa_key_size') + self.namespace.rsa_key_size = ksize + + @property + def elliptic_curve(self) -> str: + """The SECG elliptic curve name to use. + + Please see RFC 8446 for supported values. + """ + return self.namespace.elliptic_curve + + @elliptic_curve.setter + def elliptic_curve(self, ecurve: str) -> None: + """Set the elliptic_curve property""" + self._mark_runtime_override('elliptic_curve') + self.namespace.elliptic_curve = ecurve + + @property + def key_type(self) -> str: + """Type of generated private key. + + Only *ONE* per invocation can be provided at this time. + """ + return self.namespace.key_type + + @key_type.setter + def key_type(self, ktype: str) -> None: + """Set the key_type property""" + self._mark_runtime_override('key_type') + self.namespace.key_type = ktype + + @property + def must_staple(self) -> bool: + """Adds the OCSP Must-Staple extension to the certificate. + + Autoconfigures OCSP Stapling for supported setups + (Apache version >= 2.3.3 ). + """ + return self.namespace.must_staple + + @property + def config_dir(self) -> str: + """Configuration directory.""" + return self.namespace.config_dir + + @property + def work_dir(self) -> str: + """Working directory.""" + return self.namespace.work_dir + + @property + def accounts_dir(self) -> str: + """Directory where all account information is stored.""" + return self.accounts_dir_for_server_path(self.server_path) + + @property + def backup_dir(self) -> str: + """Configuration backups directory.""" + return os.path.join(self.namespace.work_dir, constants.BACKUP_DIR) + + @property + def in_progress_dir(self) -> str: + """Directory used before a permanent checkpoint is finalized.""" + return os.path.join(self.namespace.work_dir, constants.IN_PROGRESS_DIR) + + @property + def temp_checkpoint_dir(self) -> str: + """Temporary checkpoint directory.""" + return os.path.join( + self.namespace.work_dir, constants.TEMP_CHECKPOINT_DIR) + + @property + def no_verify_ssl(self) -> bool: + """Disable verification of the ACME server's certificate. + + The root certificates trusted by Certbot can be overridden by setting the + REQUESTS_CA_BUNDLE environment variable. + """ + return self.namespace.no_verify_ssl + + @property + def http01_port(self) -> int: + """Port used in the http-01 challenge. + + This only affects the port Certbot listens on. + A conforming ACME server will still attempt to connect on port 80. + """ + return self.namespace.http01_port + + @property + def http01_address(self) -> str: + """The address the server listens to during http-01 challenge.""" + return self.namespace.http01_address + + @property + def https_port(self) -> int: + """Port used to serve HTTPS. + + This affects which port Nginx will listen on after a LE certificate + is installed. + """ + return self.namespace.https_port + + @property + def pref_challs(self) -> list[str]: + """List of user specified preferred challenges. + + Sorted with the most preferred challenge listed first. + """ + return self.namespace.pref_challs + + @property + def allow_subset_of_names(self) -> bool: + """Allow only a subset of names to be authorized to perform validations. + + When performing domain validation, do not consider it a failure + if authorizations can not be obtained for a strict subset of + the requested domains. This may be useful for allowing renewals for + multiple domains to succeed even if some domains no longer point + at this system. + """ + return self.namespace.allow_subset_of_names + + @property + def strict_permissions(self) -> bool: + """Enable strict permissions checks. + + Require that all configuration files are owned by the current + user; only needed if your config is somewhere unsafe like /tmp/. + """ + return self.namespace.strict_permissions + + @property + def disable_renew_updates(self) -> bool: + """Disable renewal updates. + + If updates provided by installer enhancements when Certbot is being run + with \"renew\" verb should be disabled. + """ + return self.namespace.disable_renew_updates + + @property + def required_profile(self) -> Optional[str]: + """Request the given profile name from the ACME server. + + If the ACME server returns an error, issuance (or renewal) will fail. + For long-term reliability, setting preferred_profile instead may be + preferable because it allows fallback to a default. Use this setting + when renewal failure is preferable to fallback. + """ + return self.namespace.required_profile + + @property + def preferred_profile(self) -> Optional[str]: + """Request the given profile name from the ACME server, or fallback to default. + + If the given profile name exists in the ACME directory, use it to request a + a certificate. Otherwise, fall back to requesting a certificate without a profile + (which means the CA will use its default profile). This allows renewals to + succeed even if the CA deprecates and removes a given profile. + """ + return self.namespace.preferred_profile + + @property + def preferred_chain(self) -> Optional[str]: + """Set the preferred certificate chain. + + If the CA offers multiple certificate chains, prefer the chain whose + topmost certificate was issued from this Subject Common Name. + If no match, the default offered chain will be used. + """ + return self.namespace.preferred_chain + + @property + def server_path(self) -> str: + """File path based on ``server``.""" + parsed = parse.urlparse(self.namespace.server) + return (parsed.netloc + parsed.path).replace('/', os.path.sep) + + def accounts_dir_for_server_path(self, server_path: str) -> str: + """Path to accounts directory based on server_path""" + server_path = misc.underscores_for_unsupported_characters_in_path(server_path) + return os.path.join( + self.namespace.config_dir, constants.ACCOUNTS_DIR, server_path) + + @property + def default_archive_dir(self) -> str: # pylint: disable=missing-function-docstring + return os.path.join(self.namespace.config_dir, constants.ARCHIVE_DIR) + + @property + def live_dir(self) -> str: # pylint: disable=missing-function-docstring + return os.path.join(self.namespace.config_dir, constants.LIVE_DIR) + + @property + def renewal_configs_dir(self) -> str: # pylint: disable=missing-function-docstring + return os.path.join( + self.namespace.config_dir, constants.RENEWAL_CONFIGS_DIR) + + @property + def renewal_hooks_dir(self) -> str: + """Path to directory with hooks to run when getting or renewing certificates.""" + return os.path.join(self.namespace.config_dir, + constants.RENEWAL_HOOKS_DIR) + + @property + def renewal_pre_hooks_dir(self) -> str: + """Path to the pre-hook directory for hooks to run + before attempting to get or renew certs. + """ + return os.path.join(self.renewal_hooks_dir, + constants.RENEWAL_PRE_HOOKS_DIR) + + @property + def renewal_deploy_hooks_dir(self) -> str: + """Path to the deploy-hook directory for hooks to run + upon successfully getting or renewing certs. + """ + return os.path.join(self.renewal_hooks_dir, + constants.RENEWAL_DEPLOY_HOOKS_DIR) + + @property + def renewal_post_hooks_dir(self) -> str: + """Path to the post-hook directory for hooks to run + after attempting to get or renew certs. + """ + return os.path.join(self.renewal_hooks_dir, + constants.RENEWAL_POST_HOOKS_DIR) + + @property + def issuance_timeout(self) -> int: + """This option specifies how long (in seconds) Certbot will wait + for the server to issue a certificate. + """ + return self.namespace.issuance_timeout + + @property + def new_key(self) -> bool: + """This option specifies whether Certbot should generate a new private + key when replacing a certificate, even if reuse_key is set. + """ + return self.namespace.new_key + + # Magic methods + + def __deepcopy__(self, _memo: Any) -> 'NamespaceConfig': + # Work around https://bugs.python.org/issue1515 for py26 tests :( :( + new_ns = copy.deepcopy(self.namespace) + new_config = type(self)(new_ns) + # Avoid recursion loop because of the delegation defined in __setattr__ + object.__setattr__(new_config, '_argument_sources', copy.deepcopy(self.argument_sources)) + object.__setattr__(new_config, '_previously_accessed_mutables', + copy.deepcopy(self._previously_accessed_mutables)) + return new_config + + +def _check_config_sanity(config: NamespaceConfig) -> None: + """Validate command line options and display error message if + requirements are not met. + + :param config: NamespaceConfig instance holding user configuration + :type args: :class:`certbot.configuration.NamespaceConfig` + + """ + # Port check + if config.http01_port == config.https_port: + raise errors.ConfigurationError( + "Trying to run http-01 and https-port " + "on the same port ({0})".format(config.https_port)) + + +def _is_immutable(value: Any) -> bool: + """Is value of an immutable type?""" + if isinstance(value, tuple): + # tuples are only immutable if all contained values are immutable. + return all(_is_immutable(subvalue) for subvalue in value) + for immutable_type in (int, float, complex, str, bytes, bool, frozenset,): + if isinstance(value, immutable_type): + return True + # The last case we consider here is None which is also immutable. + return value is None diff --git a/certbot/src/certbot/crypto_util.py b/certbot/src/certbot/crypto_util.py new file mode 100644 index 00000000000..95fc33bb538 --- /dev/null +++ b/certbot/src/certbot/crypto_util.py @@ -0,0 +1,649 @@ +"""Certbot client crypto utility functions. + +.. todo:: Make the transition to use PSS rather than PKCS1_v1_5 when the server + is capable of handling the signatures. + +""" +import datetime +import hashlib +import ipaddress +import logging +import re +from typing import Optional +from typing import TYPE_CHECKING +from typing import Union +import warnings + +from cryptography import x509 +from cryptography.exceptions import InvalidSignature +from cryptography.exceptions import UnsupportedAlgorithm +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.hazmat.primitives.asymmetric import rsa +from cryptography.hazmat.primitives.asymmetric.dsa import DSAPublicKey +from cryptography.hazmat.primitives.asymmetric.ec import ECDSA +from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey +from cryptography.hazmat.primitives.asymmetric.mldsa import ( + MLDSA44PublicKey, + MLDSA65PublicKey, + MLDSA87PublicKey, +) +from cryptography.hazmat.primitives.asymmetric.mlkem import MLKEM768PublicKey, MLKEM1024PublicKey +from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 +from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey +from cryptography.hazmat.primitives.serialization import Encoding +from cryptography.hazmat.primitives.serialization import NoEncryption +from cryptography.hazmat.primitives.serialization import PrivateFormat +from OpenSSL import SSL + +from acme import crypto_util as acme_crypto_util +from certbot import errors +from certbot import interfaces +from certbot import util +from certbot.compat import os + +# Cryptography ed448 and ed25519 modules do not exist on oldest tests +if TYPE_CHECKING: + from cryptography.hazmat.primitives.asymmetric.ed448 import Ed448PublicKey + from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey + from cryptography.hazmat.primitives.asymmetric.x448 import X448PublicKey + from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PublicKey + +logger = logging.getLogger(__name__) + + +# High level functions + +def generate_key(key_size: int, key_dir: Optional[str], key_type: str = "rsa", + elliptic_curve: str = "secp256r1", keyname: str = "key-certbot.pem", + strict_permissions: bool = True) -> util.Key: + """Initializes and saves a privkey. + + Inits key and saves it in PEM format on the filesystem. + + .. note:: keyname is the attempted filename, it may be different if a file + already exists at the path. + + :param int key_size: key size in bits if key size is rsa. + :param str key_dir: Optional key save directory. + :param str key_type: Key Type [rsa, ecdsa] + :param str elliptic_curve: Name of the elliptic curve if key type is ecdsa. + :param str keyname: Filename of key + :param bool strict_permissions: If true and key_dir exists, an exception is raised if + the directory doesn't have 0700 permissions or isn't owned by the current user. + + :returns: Key + :rtype: :class:`certbot.util.Key` + + :raises ValueError: If unable to generate the key given key_size. + + """ + try: + key_pem = make_key( + bits=key_size, elliptic_curve=elliptic_curve or "secp256r1", key_type=key_type, + ) + except ValueError as err: + logger.debug("", exc_info=True) + logger.error("Encountered error while making key: %s", str(err)) + raise err + + # Save file + key_path = None + if key_dir: + util.make_or_verify_dir(key_dir, 0o700, strict_permissions) + key_f, key_path = util.unique_file( + os.path.join(key_dir, keyname), 0o600, "wb") + with key_f: + key_f.write(key_pem) + if key_type == 'rsa': + logger.debug("Generating RSA key (%d bits): %s", key_size, key_path) + else: + logger.debug("Generating ECDSA key (%d bits): %s", key_size, key_path) + + return util.Key(key_path, key_pem) + + +def generate_csr(privkey: util.Key, names: Union[list[str], set[str]], path: Optional[str], + must_staple: bool = False, strict_permissions: bool = True, + ipaddrs: list[ipaddress.IPv4Address | ipaddress.IPv6Address] | None = None, + ) -> util.CSR: + """Initialize a CSR with the given private key. + + :param privkey: Key to include in the CSR + :type privkey: :class:`certbot.util.Key` + :param set names: `str` names to include in the CSR + :param str path: Optional certificate save directory. + :param bool must_staple: If true, include the TLS Feature extension "OCSP Must-Staple" + :param bool strict_permissions: If true and path exists, an exception is raised if + the directory doesn't have 0755 permissions or isn't owned by the current user. + + :returns: CSR + :rtype: :class:`certbot.util.CSR` + + """ + csr_pem = acme_crypto_util.make_csr( + privkey.pem, names, must_staple=must_staple, ipaddrs=ipaddrs) + + # Save CSR, if requested + csr_filename = None + if path: + util.make_or_verify_dir(path, 0o755, strict_permissions) + csr_f, csr_filename = util.unique_file( + os.path.join(path, "csr-certbot.pem"), 0o644, "wb") + with csr_f: + csr_f.write(csr_pem) + logger.debug("Creating CSR: %s", csr_filename) + + return util.CSR(csr_filename, csr_pem, "pem") + + +# WARNING: the csr and private key file are possible attack vectors for TOCTOU +# We should either... +# A. Do more checks to verify that the CSR is trusted/valid +# B. Audit the parsing code for vulnerabilities + +def valid_csr(csr: bytes) -> bool: + """Validate CSR. + + Check if `csr` is a valid CSR with a correct self-signed signature. + + :param bytes csr: CSR in PEM. + + :returns: Validity of CSR. + :rtype: bool + + """ + try: + req = x509.load_pem_x509_csr(csr) + return req.is_signature_valid + except (ValueError, TypeError): + logger.debug("", exc_info=True) + return False + + +def csr_matches_pubkey(csr: bytes, privkey: bytes) -> bool: + """Does private key correspond to the subject public key in the CSR? + + :param bytes csr: CSR in PEM. + :param bytes privkey: Private key file contents (PEM) + + :returns: Correspondence of private key to CSR subject public key. + :rtype: bool + + """ + req = x509.load_pem_x509_csr(csr) + pkey = serialization.load_pem_private_key(privkey, password=None) + return req.is_signature_valid and req.public_key() == pkey.public_key() + + +def read_csr_file( + csrfile: str, data: bytes +) -> util.CSR: + """Reads a CSR file, which can be either PEM or DER, and returns a + `certbot.util.CSR` object. + + :param str csrfile: CSR filename + :param bytes data: contents of the CSR file + + :returns: object representing the CSR + :rtype: util.CSR + + """ + try: + # Try to parse as DER first, then fall back to PEM. + csr = x509.load_der_x509_csr(data) + except ValueError: + try: + csr = x509.load_pem_x509_csr(data) + except ValueError: + raise errors.Error("Failed to parse CSR file: {0}".format(csrfile)) + + # Internally we always use PEM, so re-encode as PEM before returning. + data_pem = csr.public_bytes(serialization.Encoding.PEM) + return util.CSR(file=csrfile, data=data_pem, form="pem") + + +def import_csr_file( + csrfile: str, data: bytes +) -> tuple['acme_crypto_util.Format', util.CSR, list[str]]: + """Import a CSR file, which can be either PEM or DER. + + :param str csrfile: CSR filename + :param bytes data: contents of the CSR file + + :returns: (`acme_crypto_util.Format.PEM`, + util.CSR object representing the CSR, + list of domains requested in the CSR) + :rtype: tuple + + """ + warnings.warn("certbot.crypto_util.import_csr_file is deprecated and " + "will be removed in the next major release. Please use " + "certbot.crypto_util.read_csr_file instead.", DeprecationWarning) + try: + # Try to parse as DER first, then fall back to PEM. + csr = x509.load_der_x509_csr(data) + except ValueError: + try: + csr = x509.load_pem_x509_csr(data) + except ValueError: + raise errors.Error("Failed to parse CSR file: {0}".format(csrfile)) + + domains = acme_crypto_util.get_names_from_subject_and_extensions(csr.subject, csr.extensions) + # Internally we always use PEM, so re-encode as PEM before returning. + data_pem = csr.public_bytes(serialization.Encoding.PEM) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", "acme.crypto_util.Format is deprecated") + return ( + acme_crypto_util.Format.PEM, + util.CSR(file=csrfile, data=data_pem, form="pem"), + domains, + ) + + +def make_key(bits: int = 2048, key_type: str = "rsa", + elliptic_curve: Optional[str] = None) -> bytes: + """Generate PEM encoded RSA|EC key. + + :param int bits: Number of bits if key_type=rsa. At least 2048 for RSA. + :param str key_type: The type of key to generate, but be rsa or ecdsa + :param str elliptic_curve: The elliptic curve to use. + + :returns: new RSA or ECDSA key in PEM form with specified number of bits + or of type ec_curve when key_type ecdsa is used. + :rtype: bytes + + """ + key: Union[rsa.RSAPrivateKey, ec.EllipticCurvePrivateKey] + if key_type == 'rsa': + if bits < 2048: + raise errors.Error("Unsupported RSA key length: {}".format(bits)) + + key = rsa.generate_private_key(public_exponent=65537, key_size=bits) + elif key_type == 'ecdsa': + if not elliptic_curve: + raise errors.Error("When key_type == ecdsa, elliptic_curve must be set.") + try: + name = elliptic_curve.upper() + if name in ('SECP256R1', 'SECP384R1', 'SECP521R1'): + curve = getattr(ec, elliptic_curve.upper()) + if not curve: + raise errors.Error(f"Invalid curve type: {elliptic_curve}") + key = ec.generate_private_key( + curve=curve(), + backend=default_backend() + ) + else: + raise errors.Error("Unsupported elliptic curve: {}".format(elliptic_curve)) + except TypeError: + raise errors.Error("Unsupported elliptic curve: {}".format(elliptic_curve)) + except UnsupportedAlgorithm as e: + raise e from errors.Error(str(e)) + else: + raise errors.Error("Invalid key_type specified: {}. Use [rsa|ecdsa]".format(key_type)) + return key.private_bytes( + encoding=Encoding.PEM, + format=PrivateFormat.PKCS8, + encryption_algorithm=NoEncryption() + ) + + +def valid_privkey(privkey: Union[str, bytes]) -> bool: + """Is valid RSA private key? + + :param privkey: Private key file contents in PEM + + :returns: Validity of private key. + :rtype: bool + + """ + if isinstance(privkey, str): + privkey = privkey.encode() + try: + serialization.load_pem_private_key(privkey, password=None) + except ValueError: + return False + else: + return True + + +def verify_renewable_cert(renewable_cert: interfaces.RenewableCert) -> None: + """For checking that your certs were not corrupted on disk. + + Several things are checked: + 1. Signature verification for the cert. + 2. That fullchain matches cert and chain when concatenated. + 3. Check that the private key matches the certificate. + + :param renewable_cert: cert to verify + :type renewable_cert: certbot.interfaces.RenewableCert + + :raises errors.Error: If verification fails. + """ + verify_renewable_cert_sig(renewable_cert) + verify_fullchain(renewable_cert) + verify_cert_matches_priv_key(renewable_cert.cert_path, renewable_cert.key_path) + + +def verify_renewable_cert_sig(renewable_cert: interfaces.RenewableCert) -> None: + """Verifies the signature of a RenewableCert object. + + :param renewable_cert: cert to verify + :type renewable_cert: certbot.interfaces.RenewableCert + + :raises errors.Error: If signature verification fails. + """ + try: + with open(renewable_cert.chain_path, 'rb') as chain_file: + chain = x509.load_pem_x509_certificate(chain_file.read(), default_backend()) + with open(renewable_cert.cert_path, 'rb') as cert_file: + cert = x509.load_pem_x509_certificate(cert_file.read(), default_backend()) + pk = chain.public_key() + assert cert.signature_hash_algorithm # always present for RSA and ECDSA + verify_signed_payload(pk, cert.signature, cert.tbs_certificate_bytes, + cert.signature_hash_algorithm) + except (OSError, ValueError, InvalidSignature) as e: + error_str = "verifying the signature of the certificate located at {0} has failed. \ + Details: {1}".format(renewable_cert.cert_path, e) + logger.exception(error_str) + raise errors.Error(error_str) + + +def verify_signed_payload(public_key: Union[DSAPublicKey, 'Ed25519PublicKey', 'Ed448PublicKey', + EllipticCurvePublicKey, MLDSA44PublicKey, + MLDSA65PublicKey, MLDSA87PublicKey, MLKEM768PublicKey, + MLKEM1024PublicKey, RSAPublicKey, + 'X25519PublicKey', 'X448PublicKey'], + signature: bytes, payload: bytes, + signature_hash_algorithm: hashes.HashAlgorithm) -> None: + """Check the signature of a payload. + + :param RSAPublicKey/EllipticCurvePublicKey public_key: the public_key to check signature + :param bytes signature: the signature bytes + :param bytes payload: the payload bytes + :param hashes.HashAlgorithm signature_hash_algorithm: algorithm used to hash the payload + + :raises InvalidSignature: If signature verification fails. + :raises errors.Error: If public key type is not supported + """ + if isinstance(public_key, RSAPublicKey): + public_key.verify( + signature, payload, PKCS1v15(), signature_hash_algorithm + ) + elif isinstance(public_key, EllipticCurvePublicKey): + public_key.verify( + signature, payload, ECDSA(signature_hash_algorithm) + ) + else: + raise errors.Error("Unsupported public key type.") + + +def verify_cert_matches_priv_key(cert_path: str, key_path: str) -> None: + """ Verifies that the private key and cert match. + + :param str cert_path: path to a cert in PEM format + :param str key_path: path to a private key file + + :raises errors.Error: If they don't match. + """ + try: + context = SSL.Context(SSL.TLS_METHOD) + context.use_certificate_file(cert_path) + context.use_privatekey_file(key_path) + context.check_privatekey() + except (OSError, SSL.Error) as e: + error_str = "verifying the certificate located at {0} matches the \ + private key located at {1} has failed. \ + Details: {2}".format(cert_path, + key_path, e) + logger.exception(error_str) + raise errors.Error(error_str) + + +def verify_fullchain(renewable_cert: interfaces.RenewableCert) -> None: + """ Verifies that fullchain is indeed cert concatenated with chain. + + :param renewable_cert: cert to verify + :type renewable_cert: certbot.interfaces.RenewableCert + + :raises errors.Error: If cert and chain do not combine to fullchain. + """ + try: + with open(renewable_cert.chain_path) as chain_file: + chain = chain_file.read() + with open(renewable_cert.cert_path) as cert_file: + cert = cert_file.read() + with open(renewable_cert.fullchain_path) as fullchain_file: + fullchain = fullchain_file.read() + if (cert + chain) != fullchain: + error_str = "fullchain does not match cert + chain for {0}!" + error_str = error_str.format(renewable_cert.lineagename) + raise errors.Error(error_str) + except OSError as e: + error_str = "reading one of cert, chain, or fullchain has failed: {0}".format(e) + logger.exception(error_str) + raise errors.Error(error_str) + except errors.Error as e: + raise e + + +def get_sans_from_cert( + cert: bytes, typ: 'acme_crypto_util.Format | int | None' = None +) -> list[str]: + """Get a list of Subject Alternative Names from a certificate. + + :param str cert: Certificate (encoded). + :param Format typ: Which format the `cert` bytes are in. + + :returns: A list of Subject Alternative Names. + :rtype: list + + """ + warnings.warn("get_sans_from_cert is deprecated and will be removed in the next " + "major release.", DeprecationWarning) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", "acme.crypto_util.Format is deprecated") + if typ is None: + typ = acme_crypto_util.Format.PEM + typ = acme_crypto_util.Format(typ) + if typ == acme_crypto_util.Format.PEM: + x509_cert = x509.load_pem_x509_certificate(cert) + else: + assert typ == acme_crypto_util.Format.DER + x509_cert = x509.load_der_x509_certificate(cert) + + try: + san_ext = x509_cert.extensions.get_extension_for_class( + x509.SubjectAlternativeName + ) + except x509.ExtensionNotFound: + return [] + + return san_ext.value.get_values_for_type(x509.DNSName) + + +def get_names_from_cert( + cert: bytes, typ: 'acme_crypto_util.Format | int | None' = None +) -> list[str]: + """Get a list of domains from a cert, including the CN if it is set. + + :param str cert: Certificate (encoded). + :param Format typ: Which format the `cert` bytes are in. + + :returns: A list of domain names. + :rtype: list + + """ + warnings.warn("get_names_from_cert is deprecated and will be removed in the next " + "major release.", DeprecationWarning) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", "acme.crypto_util.Format is deprecated") + if typ is None: + typ = acme_crypto_util.Format.PEM + typ = acme_crypto_util.Format(typ) + if typ == acme_crypto_util.Format.PEM: + x509_cert = x509.load_pem_x509_certificate(cert) + else: + assert typ == acme_crypto_util.Format.DER + x509_cert = x509.load_der_x509_certificate(cert) + return acme_crypto_util.get_names_from_subject_and_extensions( + x509_cert.subject, x509_cert.extensions + ) + + +def get_names_from_req( + csr: bytes, typ: 'acme_crypto_util.Format | int | None' = None +) -> list[str]: + """Get a list of domains from a CSR, including the CN if it is set. + + :param str csr: CSR (encoded). + :param acme_crypto_util.Format typ: Which format the `csr` bytes are in. + :returns: A list of domain names. + :rtype: list + + """ + warnings.warn("get_names_from_req is deprecated and will be removed in the next " + "major release.", DeprecationWarning) + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", "acme.crypto_util.Format is deprecated") + if typ is None: + typ = acme_crypto_util.Format.PEM + typ = acme_crypto_util.Format(typ) + if typ == acme_crypto_util.Format.PEM: + x509_req = x509.load_pem_x509_csr(csr) + else: + assert typ == acme_crypto_util.Format.DER + x509_req = x509.load_der_x509_csr(csr) + return acme_crypto_util.get_names_from_subject_and_extensions( + x509_req.subject, x509_req.extensions + ) + + +def notBefore(cert_path: str) -> datetime.datetime: + """When does the cert at cert_path start being valid? + + :param str cert_path: path to a cert in PEM format + + :returns: the notBefore value from the cert at cert_path + :rtype: :class:`datetime.datetime` + + """ + with open(cert_path, "rb") as f: + cert = x509.load_pem_x509_certificate(f.read()) + return cert.not_valid_before_utc + + +def notAfter(cert_path: str) -> datetime.datetime: + """When does the cert at cert_path stop being valid? + + :param str cert_path: path to a cert in PEM format + + :returns: the notAfter value from the cert at cert_path + :rtype: :class:`datetime.datetime` + + """ + with open(cert_path, "rb") as f: + cert = x509.load_pem_x509_certificate(f.read()) + return cert.not_valid_after_utc + + +def sha256sum(filename: str) -> str: + """Compute a sha256sum of a file. + + NB: In given file, platform specific newlines characters will be converted + into their equivalent unicode counterparts before calculating the hash. + + :param str filename: path to the file whose hash will be computed + + :returns: sha256 digest of the file in hexadecimal + :rtype: str + """ + sha256 = hashlib.sha256() + with open(filename, 'r') as file_d: + sha256.update(file_d.read().encode('UTF-8')) + return sha256.hexdigest() + +# Finds one CERTIFICATE stricttextualmsg according to rfc7468#section-3. +# Does not validate the base64text - use crypto.load_certificate. +CERT_PEM_REGEX = re.compile( + b"""-----BEGIN CERTIFICATE-----\r? +.+?\r? +-----END CERTIFICATE-----\r? +""", + re.DOTALL # DOTALL (/s) because the base64text may include newlines +) + + +def cert_and_chain_from_fullchain(fullchain_pem: str) -> tuple[str, str]: + """Split fullchain_pem into cert_pem and chain_pem + + :param str fullchain_pem: concatenated cert + chain + + :returns: tuple of string cert_pem and chain_pem + :rtype: tuple + + :raises errors.Error: If there are less than 2 certificates in the chain. + + """ + # First pass: find the boundary of each certificate in the chain. + # TODO: This will silently skip over any "explanatory text" in between boundaries, + # which is prohibited by RFC8555. + certs = CERT_PEM_REGEX.findall(fullchain_pem.encode()) + if len(certs) < 2: + raise errors.Error("failed to parse fullchain into cert and chain: " + + "less than 2 certificates in chain") + + # Second pass: for each certificate found, parse it using cryptography and re-encode it, + # with the effect of normalizing any encoding variations (e.g. CRLF, whitespace). + certs_normalized: list[str] = [] + for cert_pem in certs: + cert = x509.load_pem_x509_certificate(cert_pem) + cert_pem = cert.public_bytes(Encoding.PEM) + certs_normalized.append(cert_pem.decode()) + + # Since each normalized cert has a newline suffix, no extra newlines are required. + return (certs_normalized[0], "".join(certs_normalized[1:])) + + +def get_serial_from_cert(cert_path: str) -> int: + """Retrieve the serial number of a certificate from certificate path + + :param str cert_path: path to a cert in PEM format + + :returns: serial number of the certificate + :rtype: int + """ + with open(cert_path, "rb") as f: + cert = x509.load_pem_x509_certificate(f.read()) + return cert.serial_number + + +def find_chain_with_issuer(fullchains: list[str], issuer_cn: str, + warn_on_no_match: bool = False) -> str: + """Chooses the first certificate chain from fullchains whose topmost + intermediate has an Issuer Common Name matching issuer_cn (in other words + the first chain which chains to a root whose name matches issuer_cn). + + :param fullchains: The list of fullchains in PEM chain format. + :type fullchains: `list` of `str` + :param `str` issuer_cn: The exact Subject Common Name to match against any + issuer in the certificate chain. + + :returns: The best-matching fullchain, PEM-encoded, or the first if none match. + :rtype: `str` + """ + for chain in fullchains: + certs = CERT_PEM_REGEX.findall(chain.encode()) + top_cert = x509.load_pem_x509_certificate(certs[-1], default_backend()) + top_issuer_cn = top_cert.issuer.get_attributes_for_oid(x509.NameOID.COMMON_NAME) + if top_issuer_cn and top_issuer_cn[0].value == issuer_cn: + return chain + + # Nothing matched, return whatever was first in the list. + if warn_on_no_match: + logger.warning("Certbot has been configured to prefer certificate chains with " + "issuer '%s', but no chain from the CA matched this issuer. Using " + "the default certificate chain instead.", issuer_cn) + return fullchains[0] diff --git a/certbot/src/certbot/display/__init__.py b/certbot/src/certbot/display/__init__.py new file mode 100644 index 00000000000..9d39dce9203 --- /dev/null +++ b/certbot/src/certbot/display/__init__.py @@ -0,0 +1 @@ +"""Certbot display utilities.""" diff --git a/certbot/src/certbot/display/ops.py b/certbot/src/certbot/display/ops.py new file mode 100644 index 00000000000..2034358cdf0 --- /dev/null +++ b/certbot/src/certbot/display/ops.py @@ -0,0 +1,352 @@ +"""Contains UI methods for LE user operations.""" +import logging +from textwrap import indent +from typing import Any +from typing import Callable +from typing import Iterable +from typing import Optional + +from certbot import errors +from certbot import interfaces +from certbot import util +from certbot._internal import account +from certbot._internal.display import util as internal_display_util +from certbot.compat import os +from certbot.display import util as display_util + +logger = logging.getLogger(__name__) + + +def get_email(invalid: bool = False, **kwargs: Any) -> str: + """Prompt for valid email address. + + :param bool invalid: True if an invalid address was provided by the user + + :returns: e-mail address + :rtype: str + + :raises errors.Error: if the user cancels + + """ + # pylint: disable=unused-argument + invalid_prefix = "" + if invalid: + invalid_prefix = "The server reported a problem with your email address. " + msg = "Enter email address or hit Enter to skip.\n" + + while True: + code, email = display_util.input_text(invalid_prefix + msg, default="") + + if code != display_util.OK: + raise errors.Error("Error getting email address.") + if email == "": + return "" + if util.safe_email(email): + return email + invalid_prefix = "There is a problem with your email address. " + + +def choose_account(accounts: list[account.Account]) -> Optional[account.Account]: + """Choose an account. + + :param list accounts: Containing at least one + :class:`~certbot._internal.account.Account` + + """ + # Note this will get more complicated once we start recording authorizations + labels = [acc.slug for acc in accounts] + + code, index = display_util.menu("Please choose an account", labels, force_interactive=True) + if code == display_util.OK: + return accounts[index] + return None + + +def choose_values(values: list[str], question: Optional[str] = None) -> list[str]: + """Display screen to let user pick one or multiple values from the provided + list. + + :param list values: Values to select from + :param str question: Question to ask to user while choosing values + + :returns: List of selected values + :rtype: list + """ + code, items = display_util.checklist(question if question else "", tags=values, + force_interactive=True) + if code == display_util.OK and items: + return items + return [] + + +def choose_names(installer: Optional[interfaces.Installer], + question: Optional[str] = None) -> list[str]: + """Display screen to select domains to validate. + + :param installer: An installer object + :type installer: :class:`certbot.interfaces.Installer` + + :param `str` question: Overriding default question to ask the user if asked + to choose from domain names. + + :returns: List of selected names + :rtype: `list` of `str` + + """ + if installer is None: + logger.debug("No installer, picking names manually") + return _choose_names_manually() + + domains = list(installer.get_all_names()) + names = get_valid_domains(domains) + + if not names: + return _choose_names_manually() + + code, names = _filter_names(names, question) + if code == display_util.OK and names: + return names + return [] + + +def get_valid_domains(domains: Iterable[str]) -> list[str]: + """Helper method for choose_names that implements basic checks + on domain names + + :param list domains: Domain names to validate + :return: List of valid domains + :rtype: list + """ + valid_domains: list[str] = [] + for domain in domains: + try: + valid_domains.append(util.enforce_domain_sanity(domain)) + except errors.ConfigurationError: + continue + return valid_domains + + +def _sort_names(FQDNs: Iterable[str]) -> list[str]: + """Sort FQDNs by SLD (and if many, by their subdomains) + + :param list FQDNs: list of domain names + + :returns: Sorted list of domain names + :rtype: list + """ + return sorted(FQDNs, key=lambda fqdn: fqdn.split('.')[::-1][1:]) + + +def _filter_names(names: Iterable[str], + override_question: Optional[str] = None) -> tuple[str, list[str]]: + """Determine which names the user would like to select from a list. + + :param list names: domain names + + :returns: tuple of the form (`code`, `names`) where + `code` - str display exit code + `names` - list of names selected + :rtype: tuple + + """ + # Sort by domain first, and then by subdomain + sorted_names = _sort_names(names) + if override_question: + question = override_question + else: + question = ( + "Which names would you like to activate HTTPS for?\n" + "We recommend selecting either all domains, or all domains in a VirtualHost/server " + "block.") + code, names = display_util.checklist( + question, tags=sorted_names, cli_flag="--domains", force_interactive=True) + return code, [str(s) for s in names] + + +def _choose_names_manually(prompt_prefix: str = "") -> list[str]: + """Manually input names for those without an installer. + + :param str prompt_prefix: string to prepend to prompt for domains + + :returns: list of provided names + :rtype: `list` of `str` + + """ + code, input_ = display_util.input_text( + prompt_prefix + + "Please enter the domain name(s) you would like on your certificate " + "(comma and/or space separated)", + cli_flag="--domains", force_interactive=True) + + if code == display_util.OK: + invalid_domains = {} + retry_message = "" + try: + domain_list = internal_display_util.separate_list_input(input_) + except UnicodeEncodeError: + domain_list = [] + retry_message = ( + "Internationalized domain names are not presently " + "supported.{0}{0}Would you like to re-enter the " + "names?{0}").format(os.linesep) + + for i, domain in enumerate(domain_list): + try: + domain_list[i] = util.enforce_domain_sanity(domain) + except errors.ConfigurationError as e: + invalid_domains[domain] = str(e) + + if invalid_domains: + retry_message = ( + "One or more of the entered domain names was not valid:" + "{0}{0}").format(os.linesep) + for invalid_domain, err in invalid_domains.items(): + retry_message = retry_message + "{1}: {2}{0}".format( + os.linesep, invalid_domain, err) + retry_message = retry_message + ( + "{0}Would you like to re-enter the names?{0}").format( + os.linesep) + + if retry_message: + # We had error in input + retry = display_util.yesno(retry_message, force_interactive=True) + if retry: + return _choose_names_manually() + else: + return domain_list + return [] + + +def success_installation(domains: list[str]) -> None: + """Display a box confirming the installation of HTTPS. + + :param list domains: domain names which were enabled + + """ + display_util.notify( + "Congratulations! You have successfully enabled HTTPS on {0}" + .format(_gen_https_names(domains)) + ) + + +def success_renewal(unused_domains: list[str]) -> None: + """Display a box confirming the renewal of an existing certificate. + + :param list domains: domain names which were renewed + + """ + display_util.notify( + "Your existing certificate has been successfully renewed, and the " + "new certificate has been installed." + ) + + +def success_revocation(cert_path: str) -> None: + """Display a message confirming a certificate has been revoked. + + :param list cert_path: path to certificate which was revoked. + + """ + display_util.notify( + "Congratulations! You have successfully revoked the certificate " + "that was located at {0}.".format(cert_path) + ) + + +def report_executed_command(command_name: str, returncode: int, stdout: str, stderr: str) -> None: + """Display a message describing the success or failure of an executed process (e.g. hook). + + :param str command_name: Human-readable description of the executed command + :param int returncode: The exit code of the executed command + :param str stdout: The stdout output of the executed command + :param str stderr: The stderr output of the executed command + + """ + out_s, err_s = stdout.strip(), stderr.strip() + if returncode != 0: + logger.warning("%s reported error code %d", command_name, returncode) + if out_s: + display_util.notify(f"{command_name} ran with output:\n{indent(out_s, ' ')}") + if err_s: + logger.warning("%s ran with error output:\n%s", command_name, indent(err_s, ' ')) + + +def _gen_https_names(domains: list[str]) -> str: + """Returns a string of the https domains. + + Domains are formatted nicely with ``https://`` prepended to each. + + :param list domains: Each domain is a 'str' + + """ + if len(domains) == 1: + return "https://{0}".format(domains[0]) + elif len(domains) == 2: + return "https://{dom[0]} and https://{dom[1]}".format(dom=domains) + elif len(domains) > 2: + return "{0}{1}{2}".format( + ", ".join("https://%s" % dom for dom in domains[:-1]), + ", and https://", + domains[-1]) + + return "" + + +def _get_validated(method: Callable[..., tuple[str, str]], + validator: Callable[[str], Any], message: str, + default: Optional[str] = None, **kwargs: Any) -> tuple[str, str]: + if default is not None: + try: + validator(default) + except errors.Error: + logger.debug('Encountered invalid default value "%s" when prompting for "%s"', + default, + message, + exc_info=True) + raise AssertionError('Invalid default "{0}"'.format(default)) + + while True: + code, raw = method(message, default=default, **kwargs) + if code == display_util.OK: + try: + validator(raw) + return code, raw + except errors.Error as error: + logger.debug('Validator rejected "%s" when prompting for "%s"', + raw, + message, + exc_info=True) + display_util.notification(str(error), pause=False) + else: + return code, raw + + +def validated_input(validator: Callable[[str], Any], + *args: Any, **kwargs: Any) -> tuple[str, str]: + """Like `~certbot.display.util.input_text`, but with validation. + + :param callable validator: A method which will be called on the + supplied input. If the method raises an `errors.Error`, its + text will be displayed and the user will be re-prompted. + :param list `*args`: Arguments to be passed to `~certbot.display.util.input_text`. + :param dict `**kwargs`: Arguments to be passed to `~certbot.display.util.input_text`. + :return: as `~certbot.display.util.input_text` + :rtype: tuple + """ + return _get_validated(display_util.input_text, validator, *args, **kwargs) + + +def validated_directory(validator: Callable[[str], Any], + *args: Any, **kwargs: Any) -> tuple[str, str]: + """Like `~certbot.display.util.directory_select`, but with validation. + + :param callable validator: A method which will be called on the + supplied input. If the method raises an `errors.Error`, its + text will be displayed and the user will be re-prompted. + :param list `*args`: Arguments to be passed to `~certbot.display.util.directory_select`. + :param dict `**kwargs`: Arguments to be passed to + `~certbot.display.util.directory_select`. + :return: as `~certbot.display.util.directory_select` + :rtype: tuple + """ + return _get_validated(display_util.directory_select, validator, *args, **kwargs) diff --git a/certbot/src/certbot/display/util.py b/certbot/src/certbot/display/util.py new file mode 100644 index 00000000000..4f9bc47577a --- /dev/null +++ b/certbot/src/certbot/display/util.py @@ -0,0 +1,181 @@ +"""Certbot display. + +This module (`certbot.display.util`) or its companion `certbot.display.ops` +should be used whenever: + +- Displaying status information to the user on the terminal +- Collecting information from the user via prompts + +Other messages can use the `logging` module. See `log.py`. + +""" +from typing import Optional +from typing import Union + +from certbot._internal.display import obj + +# These constants are defined this way to make them easier to document with +# Sphinx and to not couple our public docstrings to our internal ones. +OK = obj.OK +"""Display exit code indicating user acceptance.""" + +CANCEL = obj.CANCEL +"""Display exit code for a user canceling the display.""" + +WIDTH = 72 + +def notify(msg: str) -> None: + """Display a basic status message. + + :param str msg: message to display + + """ + obj.get_display().notification(msg, pause=False, decorate=False, wrap=False) + + +def notification(message: str, pause: bool = True, wrap: bool = True, + force_interactive: bool = False, decorate: bool = True) -> None: + """Displays a notification and waits for user acceptance. + + :param str message: Message to display + :param bool pause: Whether or not the program should pause for the + user's confirmation + :param bool wrap: Whether or not the application should wrap text + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + :param bool decorate: Whether to surround the message with a + decorated frame + + """ + obj.get_display().notification(message, pause=pause, wrap=wrap, + force_interactive=force_interactive, decorate=decorate) + + +def menu(message: str, choices: Union[list[str], list[tuple[str, str]]], + default: Optional[int] = None, cli_flag: Optional[str] = None, + force_interactive: bool = False) -> tuple[str, int]: + """Display a menu. + + .. todo:: This doesn't enable the help label/button (I wasn't sold on + any interface I came up with for this). It would be a nice feature. + + :param str message: title of menu + :param choices: Menu lines, len must be > 0 + :type choices: list of tuples (tag, item) or + list of descriptions (tags will be enumerated) + :param default: default value to return, if interaction is not possible + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: tuple of (`code`, `index`) where + `code` - str display exit code + `index` - int index of the user's selection + + :rtype: tuple + + """ + return obj.get_display().menu(message, choices, default=default, cli_flag=cli_flag, + force_interactive=force_interactive) + + +def input_text(message: str, default: Optional[str] = None, cli_flag: Optional[str] = None, + force_interactive: bool = False) -> tuple[str, str]: + """Accept input from the user. + + :param str message: message to display to the user + :param default: default value to return, if interaction is not possible + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: tuple of (`code`, `input`) where + `code` - str display exit code + `input` - str of the user's input + :rtype: tuple + + """ + return obj.get_display().input(message, default=default, cli_flag=cli_flag, + force_interactive=force_interactive) + + +def yesno(message: str, yes_label: str = "Yes", no_label: str = "No", + default: Optional[bool] = None, cli_flag: Optional[str] = None, + force_interactive: bool = False) -> bool: + """Query the user with a yes/no question. + + Yes and No label must begin with different letters, and must contain at + least one letter each. + + :param str message: question for the user + :param str yes_label: Label of the "Yes" parameter + :param str no_label: Label of the "No" parameter + :param default: default value to return, if interaction is not possible + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: True for "Yes", False for "No" + :rtype: bool + + """ + return obj.get_display().yesno(message, yes_label=yes_label, no_label=no_label, default=default, + cli_flag=cli_flag, force_interactive=force_interactive) + + +def checklist(message: str, tags: list[str], default: Optional[list[str]] = None, + cli_flag: Optional[str] = None, + force_interactive: bool = False) -> tuple[str, list[str]]: + """Display a checklist. + + :param str message: Message to display to user + :param list tags: `str` tags to select, len(tags) > 0 + :param default: default value to return, if interaction is not possible + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: tuple of (`code`, `tags`) where + `code` - str display exit code + `tags` - list of selected tags + :rtype: tuple + + """ + return obj.get_display().checklist(message, tags, default=default, cli_flag=cli_flag, + force_interactive=force_interactive) + + +def directory_select(message: str, default: Optional[str] = None, cli_flag: Optional[str] = None, + force_interactive: bool = False) -> tuple[str, str]: + """Display a directory selection screen. + + :param str message: prompt to give the user + :param default: default value to return, if interaction is not possible + :param str cli_flag: option used to set this value with the CLI + :param bool force_interactive: True if it's safe to prompt the user + because it won't cause any workflow regressions + + :returns: tuple of the form (`code`, `string`) where + `code` - display exit code + `string` - input entered by the user + + """ + return obj.get_display().directory_select(message, default=default, cli_flag=cli_flag, + force_interactive=force_interactive) + + +def assert_valid_call(prompt: str, default: str, cli_flag: str, force_interactive: bool) -> None: + """Verify that provided arguments is a valid display call. + + :param str prompt: prompt for the user + :param default: default answer to prompt + :param str cli_flag: command line option for setting an answer + to this question + :param bool force_interactive: if interactivity is forced + + """ + msg = "Invalid display call for this prompt:\n{0}".format(prompt) + if cli_flag: + msg += ("\nYou can set an answer to " + "this prompt with the {0} flag".format(cli_flag)) + assert default is not None or force_interactive, msg diff --git a/certbot/src/certbot/errors.py b/certbot/src/certbot/errors.py new file mode 100644 index 00000000000..448b25b9856 --- /dev/null +++ b/certbot/src/certbot/errors.py @@ -0,0 +1,114 @@ +"""Certbot client errors.""" +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from certbot.achallenges import AnnotatedChallenge + + +class Error(Exception): + """Generic Certbot client error.""" + + +class AccountStorageError(Error): + """Generic `.AccountStorage` error.""" + + +class AccountNotFound(AccountStorageError): + """Account not found error.""" + + +class ReverterError(Error): + """Certbot Reverter error.""" + + +class SubprocessError(Error): + """Subprocess handling error.""" + + +class CertStorageError(Error): + """Generic `.CertStorage` error.""" + + +class HookCommandNotFound(Error): + """Failed to find a hook command in the PATH.""" + + +class SignalExit(Error): + """A Unix signal was received while in the ErrorHandler context manager.""" + +class OverlappingMatchFound(Error): + """Multiple lineages matched what should have been a unique result.""" + +class LockError(Error): + """File locking error.""" + + +# Auth Handler Errors +class AuthorizationError(Error): + """Authorization error.""" + + +class FailedChallenges(AuthorizationError): + """Failed challenges error. + + :ivar set failed_achalls: Failed `.AnnotatedChallenge` instances. + + """ + def __init__(self, failed_achalls: set['AnnotatedChallenge']) -> None: + assert failed_achalls + self.failed_achalls = failed_achalls + super().__init__() + + def __str__(self) -> str: + return "Failed authorization procedure. {0}".format( + ", ".join( + "{0} ({1}): {2}".format(achall.identifier.value, achall.typ, achall.error) + for achall in self.failed_achalls if achall.error is not None)) + + +# Plugin Errors +class PluginError(Error): + """Certbot Plugin error.""" + + +class PluginEnhancementAlreadyPresent(Error): + """ Enhancement was already set """ + + +class PluginSelectionError(Error): + """A problem with plugin/configurator selection or setup""" + + +class NoInstallationError(PluginError): + """Certbot No Installation error.""" + + +class MisconfigurationError(PluginError): + """Certbot Misconfiguration error.""" + + +class NotSupportedError(PluginError): + """Certbot Plugin function not supported error.""" + + +class PluginStorageError(PluginError): + """Certbot Plugin Storage error.""" + + +class StandaloneBindError(Error): + """Standalone plugin bind error.""" + + def __init__(self, socket_error: OSError, port: int) -> None: + super().__init__( + "Problem binding to port {0}: {1}".format(port, socket_error)) + self.socket_error = socket_error + self.port = port + + +class ConfigurationError(Error): + """Configuration sanity error.""" + +# NoninteractiveDisplay error: + +class MissingCommandlineFlag(Error): + """A command line argument was missing in noninteractive usage""" diff --git a/certbot/src/certbot/interfaces.py b/certbot/src/certbot/interfaces.py new file mode 100644 index 00000000000..511b3c33ffc --- /dev/null +++ b/certbot/src/certbot/interfaces.py @@ -0,0 +1,483 @@ +"""Certbot client interfaces.""" +from abc import ABCMeta +from abc import abstractmethod +from argparse import ArgumentParser +from typing import Any +from typing import Iterable +from typing import Optional +from typing import TYPE_CHECKING +from typing import Union + +from acme.challenges import Challenge +from acme.challenges import ChallengeResponse +from acme.client import ClientV2 +from certbot import configuration +from certbot.achallenges import AnnotatedChallenge + +try: + from zope.interface import Interface as ZopeInterface +except ImportError: + ZopeInterface = object + +if TYPE_CHECKING: + from certbot._internal.account import Account + + +class AccountStorage(metaclass=ABCMeta): + """Accounts storage interface.""" + + @abstractmethod + def find_all(self) -> list['Account']: # pragma: no cover + """Find all accounts. + + :returns: All found accounts. + :rtype: list + + """ + raise NotImplementedError() + + @abstractmethod + def load(self, account_id: str) -> 'Account': # pragma: no cover + """Load an account by its id. + + :raises .AccountNotFound: if account could not be found + :raises .AccountStorageError: if account could not be loaded + + :returns: The account loaded + :rtype: .Account + + """ + raise NotImplementedError() + + @abstractmethod + def save(self, account: 'Account', client: ClientV2) -> None: # pragma: no cover + """Save account. + + :raises .AccountStorageError: if account could not be saved + + """ + raise NotImplementedError() + + +class Plugin(metaclass=ABCMeta): + """Certbot plugin. + + Objects providing this interface will be called without satisfying + any entry point "extras" (extra dependencies) you might have defined + for your plugin, e.g (excerpt from ``setup.py`` script):: + + setup( + ... + entry_points={ + 'certbot.plugins': [ + 'name=example_project.plugin[plugin_deps]', + ], + }, + extras_require={ + 'plugin_deps': ['dep1', 'dep2'], + } + ) + + Therefore, make sure such objects are importable and usable without + extras. This is necessary, because CLI does the following operations + (in order): + + - loads an entry point, + - calls `inject_parser_options`, + - requires an entry point, + - creates plugin instance (`__call__`). + + """ + + description: str = NotImplemented + """Short plugin description""" + + name: str = NotImplemented + """Unique name of the plugin""" + + @abstractmethod + def __init__(self, config: Optional[configuration.NamespaceConfig], name: str) -> None: + """Create a new `Plugin`. + + :param configuration.NamespaceConfig config: Configuration. + :param str name: Unique plugin name. + + """ + super().__init__() + + @abstractmethod + def prepare(self) -> None: + """Prepare the plugin. + + Finish up any additional initialization. + + :raises .PluginError: + when full initialization cannot be completed. + :raises .MisconfigurationError: + when full initialization cannot be completed. Plugin will + be displayed on a list of available plugins. + :raises .NoInstallationError: + when the necessary programs/files cannot be located. Plugin + will NOT be displayed on a list of available plugins. + :raises .NotSupportedError: + when the installation is recognized, but the version is not + currently supported. + + """ + + @abstractmethod + def more_info(self) -> str: + """Human-readable string to help the user. + + Should describe the steps taken and any relevant info to help the user + decide which plugin to use. + + :rtype str: + + """ + + @classmethod + @abstractmethod + def inject_parser_options(cls, parser: ArgumentParser, name: str) -> None: + """Inject argument parser options (flags). + + 1. Be nice and prepend all options and destinations with + `~.common.option_namespace` and `~common.dest_namespace`. + + 2. Inject options (flags) only. Positional arguments are not + allowed, as this would break the CLI. + + :param ArgumentParser parser: (Almost) top-level CLI parser. + :param str name: Unique plugin name. + + """ + + +class Authenticator(Plugin): + """Generic Certbot Authenticator. + + Class represents all possible tools processes that have the + ability to perform challenges and attain a certificate. + + """ + + @abstractmethod + def get_chall_pref(self, identifier: str) -> Iterable[type[Challenge]]: + """Return `collections.Iterable` of challenge preferences. + + :param str identifier: Domain or IP address for which challenge preferences are sought. + + :returns: `collections.Iterable` of challenge types (subclasses of + :class:`acme.challenges.Challenge`) with the most + preferred challenges first. If a type is not specified, it means the + Authenticator cannot perform the challenge. + :rtype: `collections.Iterable` + + """ + + @abstractmethod + def perform(self, achalls: list[AnnotatedChallenge]) -> list[ChallengeResponse]: + """Perform the given challenge. + + :param list achalls: Non-empty (guaranteed) list of + :class:`~certbot.achallenges.AnnotatedChallenge` + instances, such that it contains types found within + :func:`get_chall_pref` only. + + :returns: list of ACME + :class:`~acme.challenges.ChallengeResponse` instances corresponding to each provided + :class:`~acme.challenges.Challenge`. + :rtype: :class:`collections.List` of + :class:`acme.challenges.ChallengeResponse`, + where responses are required to be returned in + the same order as corresponding input challenges + + :raises .PluginError: If some or all challenges cannot be performed + + """ + + @abstractmethod + def cleanup(self, achalls: list[AnnotatedChallenge]) -> None: + """Revert changes and shutdown after challenges complete. + + This method should be able to revert all changes made by + perform, even if perform exited abnormally. + + :param list achalls: Non-empty (guaranteed) list of + :class:`~certbot.achallenges.AnnotatedChallenge` + instances, a subset of those previously passed to :func:`perform`. + + :raises PluginError: if original configuration cannot be restored + + """ + + +class Installer(Plugin): + """Generic Certbot Installer Interface. + + Represents any server that an X509 certificate can be placed. + + It is assumed that :func:`save` is the only method that finalizes a + checkpoint. This is important to ensure that checkpoints are + restored in a consistent manner if requested by the user or in case + of an error. + + Using :class:`certbot.reverter.Reverter` to implement checkpoints, + rollback, and recovery can dramatically simplify plugin development. + + """ + + @abstractmethod + def get_all_names(self) -> Iterable[str]: + """Returns all names that may be authenticated. + + :rtype: `collections.Iterable` of `str` + + """ + + @abstractmethod + def deploy_cert(self, domain: str, cert_path: str, key_path: str, + chain_path: str, fullchain_path: str) -> None: + """Deploy certificate. + + :param str domain: domain to deploy certificate file + :param str cert_path: absolute path to the certificate file + :param str key_path: absolute path to the private key file + :param str chain_path: absolute path to the certificate chain file + :param str fullchain_path: absolute path to the certificate fullchain + file (cert plus chain) + + :raises .PluginError: when cert cannot be deployed + + """ + + @abstractmethod + def enhance(self, domain: str, enhancement: str, + options: Optional[Union[list[str], str]] = None) -> None: + """Perform a configuration enhancement. + + :param str domain: domain for which to provide enhancement + :param str enhancement: An enhancement as defined in + :const:`~certbot.plugins.enhancements.ENHANCEMENTS` + :param options: Flexible options parameter for enhancement. + Check documentation of + :const:`~certbot.plugins.enhancements.ENHANCEMENTS` + for expected options for each enhancement. + + :raises .PluginError: If Enhancement is not supported, or if + an error occurs during the enhancement. + + """ + + @abstractmethod + def supported_enhancements(self) -> list[str]: + """Returns a `collections.Iterable` of supported enhancements. + + :returns: supported enhancements which should be a subset of + :const:`~certbot.plugins.enhancements.ENHANCEMENTS` + :rtype: :class:`collections.Iterable` of :class:`str` + + """ + + @abstractmethod + def save(self, title: Optional[str] = None, temporary: bool = False) -> None: + """Saves all changes to the configuration files. + + Both title and temporary are needed because a save may be + intended to be permanent, but the save is not ready to be a full + checkpoint. + + It is assumed that at most one checkpoint is finalized by this + method. Additionally, if an exception is raised, it is assumed a + new checkpoint was not finalized. + + :param str title: The title of the save. If a title is given, the + configuration will be saved as a new checkpoint and put in a + timestamped directory. `title` has no effect if temporary is true. + + :param bool temporary: Indicates whether the changes made will + be quickly reversed in the future (challenges) + + :raises .PluginError: when save is unsuccessful + + """ + + @abstractmethod + def rollback_checkpoints(self, rollback: int = 1) -> None: + """Revert `rollback` number of configuration checkpoints. + + :raises .PluginError: when configuration cannot be fully reverted + + """ + + @abstractmethod + def recovery_routine(self) -> None: + """Revert configuration to most recent finalized checkpoint. + + Remove all changes (temporary and permanent) that have not been + finalized. This is useful to protect against crashes and other + execution interruptions. + + :raises .errors.PluginError: If unable to recover the configuration + + """ + + @abstractmethod + def config_test(self) -> None: + """Make sure the configuration is valid. + + :raises .MisconfigurationError: when the config is not in a usable state + + """ + + @abstractmethod + def restart(self) -> None: + """Restart or refresh the server content. + + :raises .PluginError: when server cannot be restarted + + """ + + +class RenewableCert(metaclass=ABCMeta): + """Interface to a certificate lineage.""" + + @property + @abstractmethod + def cert_path(self) -> str: + """Path to the certificate file. + + :rtype: str + + """ + + @property + @abstractmethod + def key_path(self) -> str: + """Path to the private key file. + + :rtype: str + + """ + + @property + @abstractmethod + def chain_path(self) -> str: + """Path to the certificate chain file. + + :rtype: str + + """ + + @property + @abstractmethod + def fullchain_path(self) -> str: + """Path to the full chain file. + + The full chain is the certificate file plus the chain file. + + :rtype: str + + """ + + @property + @abstractmethod + def lineagename(self) -> str: + """Name given to the certificate lineage. + + :rtype: str + + """ + + @abstractmethod + def names(self) -> list[str]: + """What are the subject names of this certificate? + + :returns: the subject names + :rtype: `list` of `str` + :raises .CertStorageError: if could not find cert file. + + """ + +# Updater interfaces +# +# When "certbot renew" is run, Certbot will iterate over each lineage and check +# if the selected installer for that lineage is a subclass of each updater +# class. If it is and the update of that type is configured to be run for that +# lineage, the relevant update function will be called for it. These functions +# are never called for other subcommands, so if an installer wants to perform +# an update during the run or install subcommand, it should do so when +# :func:`IInstaller.deploy_cert` is called. + + +class GenericUpdater(metaclass=ABCMeta): + """Interface for update types not currently specified by Certbot. + + This class allows plugins to perform types of updates that Certbot hasn't + defined (yet). + + To make use of this interface, the installer should implement the interface + methods, and interfaces.GenericUpdater.register(InstallerClass) should + be called from the installer code. + + The plugins implementing this enhancement are responsible of handling + the saving of configuration checkpoints as well as other calls to + interface methods of `interfaces.Installer` such as prepare() and restart() + """ + + @abstractmethod + def generic_updates(self, lineage: RenewableCert, *args: Any, **kwargs: Any) -> None: + """Perform any update types defined by the installer. + + If an installer is a subclass of the class containing this method, this + function will always be called when "certbot renew" is run. If the + update defined by the installer should be run conditionally, the + installer needs to handle checking the conditions itself. + + This method is called once for each lineage. + + :param lineage: Certificate lineage object + :type lineage: RenewableCert + + """ + + +class RenewDeployer(metaclass=ABCMeta): + """Interface for update types run when a lineage is renewed + + This class allows plugins to perform types of updates that need to run at + lineage renewal that Certbot hasn't defined (yet). + + To make use of this interface, the installer should implement the interface + methods, and interfaces.RenewDeployer.register(InstallerClass) should + be called from the installer code. + """ + + @abstractmethod + def renew_deploy(self, lineage: RenewableCert, *args: Any, **kwargs: Any) -> None: + """Perform updates defined by installer when a certificate has been renewed + + If an installer is a subclass of the class containing this method, this + function will always be called when a certificate has been renewed by + running "certbot renew". For example if a plugin needs to copy a + certificate over, or change configuration based on the new certificate. + + This method is called once for each lineage renewed + + :param lineage: Certificate lineage object + :type lineage: RenewableCert + + """ + + +class IPluginFactory(ZopeInterface): + """Compatibility shim for plugins that still use Certbot's old zope.interface classes.""" + +class IPlugin(ZopeInterface): + """Compatibility shim for plugins that still use Certbot's old zope.interface classes.""" + +class IAuthenticator(IPlugin): + """Compatibility shim for plugins that still use Certbot's old zope.interface classes.""" + +class IInstaller(IPlugin): + """Compatibility shim for plugins that still use Certbot's old zope.interface classes.""" diff --git a/certbot/src/certbot/main.py b/certbot/src/certbot/main.py new file mode 100644 index 00000000000..bd71fd50277 --- /dev/null +++ b/certbot/src/certbot/main.py @@ -0,0 +1,18 @@ +"""Certbot main public entry point.""" +from typing import Optional +from typing import Union + +from certbot._internal import main as internal_main + + +def main(cli_args: Optional[list[str]] = None) -> Optional[Union[str, int]]: + """Run Certbot. + + :param cli_args: command line to Certbot, defaults to ``sys.argv[1:]`` + :type cli_args: `list` of `str` + + :returns: value for `sys.exit` about the exit status of Certbot + :rtype: `str` or `int` or `None` + + """ + return internal_main.main(cli_args) diff --git a/certbot/src/certbot/ocsp.py b/certbot/src/certbot/ocsp.py new file mode 100644 index 00000000000..b0c76a58fd7 --- /dev/null +++ b/certbot/src/certbot/ocsp.py @@ -0,0 +1,9 @@ +"""Deprecated tools for checking certificate revocation.""" +import warnings + +# ruff: disable[F403] +from certbot._internal.ocsp import * # pylint: disable=wildcard-import,unused-wildcard-import +# ruff: enable[F403] + +warnings.warn("certbot.ocsp is deprecated and will be removed in the next major" + " release", DeprecationWarning, stacklevel=2) diff --git a/certbot/src/certbot/plugins/__init__.py b/certbot/src/certbot/plugins/__init__.py new file mode 100644 index 00000000000..7831eab610e --- /dev/null +++ b/certbot/src/certbot/plugins/__init__.py @@ -0,0 +1 @@ +"""Certbot plugins.""" diff --git a/certbot/src/certbot/plugins/apache.py b/certbot/src/certbot/plugins/apache.py new file mode 100644 index 00000000000..13a4c831aad --- /dev/null +++ b/certbot/src/certbot/plugins/apache.py @@ -0,0 +1,7 @@ +"""Despite being public API, this is only meant for use in our certbot-apache plugin, and isn't +intended for public use.""" + +from certbot._internal.plugins.apache import entrypoint + + +ENTRYPOINT = entrypoint.get_configurator() diff --git a/certbot/src/certbot/plugins/common.py b/certbot/src/certbot/plugins/common.py new file mode 100644 index 00000000000..a29c7104bf9 --- /dev/null +++ b/certbot/src/certbot/plugins/common.py @@ -0,0 +1,464 @@ +"""Plugin common functions.""" +from abc import ABCMeta +from abc import abstractmethod +import argparse +import importlib.resources +import logging +import re +import shutil +import tempfile +from typing import Any +from typing import Callable +from typing import Iterable +from typing import Optional +from typing import TypeVar + +from acme import challenges +from certbot import achallenges +from certbot import configuration +from certbot import crypto_util +from certbot import errors +from certbot import interfaces +from certbot import reverter +from certbot._internal import constants +from certbot.compat import filesystem +from certbot.compat import os +from certbot.interfaces import Installer as AbstractInstaller +from certbot.interfaces import Plugin as AbstractPlugin +from certbot.plugins.storage import PluginStorage + +logger = logging.getLogger(__name__) + + +def option_namespace(name: str) -> str: + """ArgumentParser options namespace (prefix of all options).""" + return name + "-" + + +def dest_namespace(name: str) -> str: + """ArgumentParser dest namespace (prefix of all destinations).""" + return name.replace("-", "_") + "_" + + +private_ips_regex = re.compile( + r"(^127\.0\.0\.1)|(^10\.)|(^172\.1[6-9]\.)|" + r"(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)") +hostname_regex = re.compile( + r"^(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*[a-z]+$", re.IGNORECASE) + + +class Plugin(AbstractPlugin, metaclass=ABCMeta): + """Generic plugin.""" + + def __init__(self, config: configuration.NamespaceConfig, name: str) -> None: + super().__init__(config, name) + self.config = config + self.name = name + + @classmethod + @abstractmethod + def add_parser_arguments(cls, add: Callable[..., None]) -> None: + """Add plugin arguments to the CLI argument parser. + + :param callable add: Function that proxies calls to + `argparse.ArgumentParser.add_argument` prepending options + with unique plugin name prefix. + + """ + + @classmethod + def inject_parser_options(cls, parser: argparse.ArgumentParser, name: str) -> None: + """Inject parser options. + + See `~.certbot.interfaces.Plugin.inject_parser_options` for docs. + + """ + # dummy function, doesn't check if dest.startswith(self.dest_namespace) + def add(arg_name_no_prefix: str, *args: Any, **kwargs: Any) -> None: + parser.add_argument( + "--{0}{1}".format(option_namespace(name), arg_name_no_prefix), + *args, **kwargs) + return cls.add_parser_arguments(add) + + @property + def option_namespace(self) -> str: + """ArgumentParser options namespace (prefix of all options).""" + return option_namespace(self.name) + + def option_name(self, name: str) -> str: + """Option name (include plugin namespace).""" + return self.option_namespace + name + + @property + def dest_namespace(self) -> str: + """ArgumentParser dest namespace (prefix of all destinations).""" + return dest_namespace(self.name) + + def dest(self, var: str) -> str: + """Find a destination for given variable ``var``.""" + # this should do exactly the same what ArgumentParser(arg), + # does to "arg" to compute "dest" + return self.dest_namespace + var.replace("-", "_") + + def conf(self, var: str) -> Any: + """Find a configuration value for variable ``var``.""" + return getattr(self.config, self.dest(var)) + + def auth_hint(self, failed_achalls: list[achallenges.AnnotatedChallenge]) -> str: + """Human-readable string to help the user troubleshoot the authenticator. + + Shown to the user if one or more of the attempted challenges were not a success. + + Should describe, in simple language, what the authenticator tried to do, what went + wrong and what the user should try as their "next steps". + + TODO: auth_hint belongs in Authenticator but can't be added until the next major + version of Certbot. For now, it lives in .Plugin and auth_handler will only call it + on authenticators that subclass .Plugin. For now, inherit from `.Plugin` to implement + and/or override the method. + + :param list failed_achalls: List of one or more failed challenges + (:class:`achallenges.AnnotatedChallenge` subclasses). + + :rtype str: + """ + # This is a fallback hint. Authenticators should implement their own auth_hint that + # addresses the specific mechanics of that authenticator. + challs = " and ".join(sorted({achall.typ for achall in failed_achalls})) + return ("The Certificate Authority couldn't externally verify that the {name} plugin " + "completed the required {challs} challenges. Ensure the plugin is configured " + "correctly and that the changes it makes are accessible from the internet." + .format(name=self.name, challs=challs)) + + +class Installer(AbstractInstaller, Plugin, metaclass=ABCMeta): + """An installer base class with reverter and ssl_dhparam methods defined. + + Installer plugins do not have to inherit from this class. + + """ + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.storage = PluginStorage(self.config, self.name) + self.reverter = reverter.Reverter(self.config) + + def add_to_checkpoint(self, save_files: set[str], save_notes: str, + temporary: bool = False) -> None: + """Add files to a checkpoint. + + :param set save_files: set of filepaths to save + :param str save_notes: notes about changes during the save + :param bool temporary: True if the files should be added to a + temporary checkpoint rather than a permanent one. This is + usually used for changes that will soon be reverted. + + :raises .errors.PluginError: when unable to add to checkpoint + + """ + if temporary: + checkpoint_func = self.reverter.add_to_temp_checkpoint + else: + checkpoint_func = self.reverter.add_to_checkpoint + + try: + checkpoint_func(save_files, save_notes) + except errors.ReverterError as err: + raise errors.PluginError(str(err)) + + def finalize_checkpoint(self, title: str) -> None: + """Timestamp and save changes made through the reverter. + + :param str title: Title describing checkpoint + + :raises .errors.PluginError: when an error occurs + + """ + try: + self.reverter.finalize_checkpoint(title) + except errors.ReverterError as err: + raise errors.PluginError(str(err)) + + def recovery_routine(self) -> None: + """Revert all previously modified files. + + Reverts all modified files that have not been saved as a checkpoint + + :raises .errors.PluginError: If unable to recover the configuration + + """ + try: + self.reverter.recovery_routine() + except errors.ReverterError as err: + raise errors.PluginError(str(err)) + + def revert_temporary_config(self) -> None: + """Rollback temporary checkpoint. + + :raises .errors.PluginError: when unable to revert config + + """ + try: + self.reverter.revert_temporary_config() + except errors.ReverterError as err: + raise errors.PluginError(str(err)) + + def rollback_checkpoints(self, rollback: int = 1) -> None: + """Rollback saved checkpoints. + + :param int rollback: Number of checkpoints to revert + + :raises .errors.PluginError: If there is a problem with the input or + the function is unable to correctly revert the configuration + + """ + try: + self.reverter.rollback_checkpoints(rollback) + except errors.ReverterError as err: + raise errors.PluginError(str(err)) + + @property + def ssl_dhparams(self) -> str: + """Full absolute path to ssl_dhparams file.""" + return os.path.join(self.config.config_dir, constants.SSL_DHPARAMS_DEST) + + @property + def updated_ssl_dhparams_digest(self) -> str: + """Full absolute path to digest of updated ssl_dhparams file.""" + return os.path.join(self.config.config_dir, constants.UPDATED_SSL_DHPARAMS_DIGEST) + + def install_ssl_dhparams(self) -> None: + """Copy Certbot's ssl_dhparams file into the system's config dir if required.""" + install_version_controlled_file( + self.ssl_dhparams, + self.updated_ssl_dhparams_digest, + constants.SSL_DHPARAMS_SRC, + constants.ALL_SSL_DHPARAMS_HASHES) + + +class Configurator(Installer, interfaces.Authenticator, metaclass=ABCMeta): + """ + A plugin that extends certbot.plugins.common.Installer + and implements certbot.interfaces.Authenticator + """ + + +GenericAddr = TypeVar("GenericAddr", bound="Addr") + + +class Addr: + r"""Represents an virtual host address. + + :param str addr: addr part of vhost address + :param str port: port number or \*, or "" + + """ + def __init__(self, tup: tuple[str, str], ipv6: bool = False): + self.tup = tup + self.ipv6 = ipv6 + + @classmethod + def fromstring(cls: type[GenericAddr], str_addr: str) -> GenericAddr: + """Initialize Addr from string.""" + if str_addr.startswith('['): + # ipv6 addresses starts with [ + endIndex = str_addr.rfind(']') + host = str_addr[:endIndex + 1] + port = '' + if len(str_addr) > endIndex + 2 and str_addr[endIndex + 1] == ':': + port = str_addr[endIndex + 2:] + return cls((host, port), ipv6=True) + else: + tup = str_addr.partition(':') + return cls((tup[0], tup[2])) + + def __str__(self) -> str: + if self.tup[1]: + return "%s:%s" % self.tup + return self.tup[0] + + def normalized_tuple(self) -> tuple[str, str]: + """Normalized representation of addr/port tuple + """ + if self.ipv6: + return self.get_ipv6_exploded(), self.tup[1] + return self.tup + + def __eq__(self, other: Any) -> bool: + if isinstance(other, self.__class__): + # compare normalized to take different + # styles of representation into account + return self.normalized_tuple() == other.normalized_tuple() + + return False + + def __hash__(self) -> int: + return hash(self.tup) + + def get_addr(self) -> str: + """Return addr part of Addr object.""" + return self.tup[0] + + def get_port(self) -> str: + """Return port.""" + return self.tup[1] + + def get_addr_obj(self: GenericAddr, port: str) -> GenericAddr: + """Return new address object with same addr and new port.""" + return self.__class__((self.tup[0], port), self.ipv6) + + def _normalize_ipv6(self, addr: str) -> list[str]: + """Return IPv6 address in normalized form, helper function""" + addr = addr.lstrip("[") + addr = addr.rstrip("]") + return self._explode_ipv6(addr) + + def get_ipv6_exploded(self) -> str: + """Return IPv6 in normalized form""" + if self.ipv6: + return ":".join(self._normalize_ipv6(self.tup[0])) + return "" + + def _explode_ipv6(self, addr: str) -> list[str]: + """Explode IPv6 address for comparison""" + result = ['0', '0', '0', '0', '0', '0', '0', '0'] + addr_list = addr.split(":") + if len(addr_list) > len(result): + # too long, truncate + addr_list = addr_list[0:len(result)] + append_to_end = False + for i, block in enumerate(addr_list): + if not block: + # encountered ::, so rest of the blocks should be + # appended to the end + append_to_end = True + continue + if len(block) > 1: + # remove leading zeros + block = block.lstrip("0") + if not append_to_end: + result[i] = str(block) + else: + # count the location from the end using negative indices + result[i-len(addr_list)] = str(block) + return result + + +class ChallengePerformer: + """Abstract base for challenge performers. + + :ivar configurator: Authenticator and installer plugin + :ivar achalls: Annotated challenges + :vartype achalls: `list` of `.KeyAuthorizationAnnotatedChallenge` + :ivar indices: Holds the indices of challenges from a larger array + so the user of the class doesn't have to. + :vartype indices: `list` of `int` + + """ + + def __init__(self, configurator: Configurator): + self.configurator = configurator + self.achalls: list[achallenges.KeyAuthorizationAnnotatedChallenge] = [] + self.indices: list[int] = [] + + def add_chall(self, achall: achallenges.KeyAuthorizationAnnotatedChallenge, + idx: Optional[int] = None) -> None: + """Store challenge to be performed when perform() is called. + + :param .KeyAuthorizationAnnotatedChallenge achall: Annotated + challenge. + :param int idx: index to challenge in a larger array + + """ + self.achalls.append(achall) + if idx is not None: + self.indices.append(idx) + + def perform(self) -> list[challenges.KeyAuthorizationChallengeResponse]: + """Perform all added challenges. + + :returns: challenge responses + :rtype: `list` of `acme.challenges.KeyAuthorizationChallengeResponse` + + + """ + raise NotImplementedError() + + +def install_version_controlled_file(dest_path: str, digest_path: str, src_path: str, + all_hashes: Iterable[str]) -> None: + """Copy a file into an active location (likely the system's config dir) if required. + + :param str dest_path: destination path for version controlled file + :param str digest_path: path to save a digest of the file in + :param str src_path: path to version controlled file found in distribution + :param list all_hashes: hashes of every released version of the file + """ + current_hash = crypto_util.sha256sum(src_path) + + def _write_current_hash() -> None: + with open(digest_path, "w") as file_h: + file_h.write(current_hash) + + def _install_current_file() -> None: + shutil.copyfile(src_path, dest_path) + _write_current_hash() + + # Check to make sure options-ssl.conf is installed + if not os.path.isfile(dest_path): + _install_current_file() + return + # there's already a file there. if it's up to date, do nothing. if it's not but + # it matches a known file hash, we can update it. + # otherwise, print a warning once per new version. + active_file_digest = crypto_util.sha256sum(dest_path) + if active_file_digest == current_hash: # already up to date + return + if active_file_digest in all_hashes: # safe to update + _install_current_file() + else: # has been manually modified, not safe to update + # did they modify the current version or an old version? + if os.path.isfile(digest_path): + with open(digest_path, "r") as f: + saved_digest = f.read() + # they modified it after we either installed or told them about this version, so return + if saved_digest == current_hash: + return + # there's a new version but we couldn't update the file, or they deleted the digest. + # save the current digest so we only print this once, and print a warning + _write_current_hash() + logger.warning("%s has been manually modified; updated file " + "saved to %s. We recommend updating %s for security purposes.", + dest_path, src_path, dest_path) + + +# test utils used by the apache/nginx plugins (hence +# "pragma: no cover") TODO: this might quickly lead to dead code (also +# c.f. #383) + +def dir_setup(test_dir: str, pkg: str) -> tuple[str, str, str]: # pragma: no cover + """Setup the directories necessary for the configurator.""" + def expanded_tempdir(prefix: str) -> str: + """Return the real path of a temp directory with the specified prefix + + Some plugins rely on real paths of symlinks for working correctly. For + example, certbot-apache uses real paths of configuration files to tell + a virtual host from another. On systems where TMP itself is a symbolic + link, (ex: OS X) such plugins will be confused. This function prevents + such a case. + """ + return filesystem.realpath(tempfile.mkdtemp(prefix)) + + temp_dir = expanded_tempdir("temp") + config_dir = expanded_tempdir("config") + work_dir = expanded_tempdir("work") + + filesystem.chmod(temp_dir, constants.CONFIG_DIRS_MODE) + filesystem.chmod(config_dir, constants.CONFIG_DIRS_MODE) + filesystem.chmod(work_dir, constants.CONFIG_DIRS_MODE) + + test_dir_ref = importlib.resources.files(pkg).joinpath("testdata").joinpath(test_dir) + with importlib.resources.as_file(test_dir_ref) as path: + shutil.copytree( + path, os.path.join(temp_dir, test_dir), symlinks=True) + + return temp_dir, config_dir, work_dir diff --git a/certbot/src/certbot/plugins/dns_common.py b/certbot/src/certbot/plugins/dns_common.py new file mode 100644 index 00000000000..60bd4137955 --- /dev/null +++ b/certbot/src/certbot/plugins/dns_common.py @@ -0,0 +1,369 @@ +"""Common code for DNS Authenticator Plugins.""" +import abc +import logging +from time import sleep +from typing import Callable +from typing import Iterable +from typing import Mapping +from typing import Optional + +import configobj + +from acme import challenges +from certbot import achallenges +from certbot import configuration +from certbot import errors +from certbot import interfaces +from certbot.compat import filesystem +from certbot.compat import os +from certbot.display import ops +from certbot.display import util as display_util +from certbot.plugins import common + +logger = logging.getLogger(__name__) + + +# As of writing this, the only one of our plugins that does not inherit from this class (either +# directly or indirectly through certbot.plugins.dns_common_lexicon.LexiconDNSAuthenticator) is +# certbot-dns-route53. If you are attempting to make changes to all of our DNS plugins, please keep +# this difference in mind. +class DNSAuthenticator(common.Plugin, interfaces.Authenticator, metaclass=abc.ABCMeta): + """Base class for DNS Authenticators""" + + def __init__(self, config: configuration.NamespaceConfig, name: str) -> None: + super().__init__(config, name) + + self._attempt_cleanup = False + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None], # pylint: disable=arguments-differ + default_propagation_seconds: int = 10) -> None: + add('propagation-seconds', + default=default_propagation_seconds, + type=int, + help='The number of seconds to wait for DNS to propagate before asking the ACME server ' + 'to verify the DNS record.') + + def auth_hint(self, failed_achalls: list[achallenges.AnnotatedChallenge]) -> str: + """See certbot.plugins.common.Plugin.auth_hint.""" + delay = self.conf('propagation-seconds') + return ( + 'The Certificate Authority failed to verify the DNS TXT records created by --{name}. ' + 'Ensure the above domains are hosted by this DNS provider, or try increasing ' + '--{name}-propagation-seconds (currently {secs} second{suffix}).' + .format(name=self.name, secs=delay, suffix='s' if delay != 1 else '') + ) + + def get_chall_pref(self, unused_identifier: str) -> Iterable[type[challenges.Challenge]]: # pylint: disable=missing-function-docstring + return [challenges.DNS01] + + def prepare(self) -> None: # pylint: disable=missing-function-docstring + pass + + def more_info(self) -> str: # pylint: disable=missing-function-docstring + raise NotImplementedError() + + def perform(self, achalls: list[achallenges.AnnotatedChallenge] + ) -> list[challenges.ChallengeResponse]: # pylint: disable=missing-function-docstring + self._setup_credentials() + + self._attempt_cleanup = True + + responses = [] + for achall in achalls: + domain = achall.identifier.value + validation_domain_name = achall.validation_domain_name(domain) + validation = achall.validation(achall.account_key) + + self._perform(domain, validation_domain_name, validation) + responses.append(achall.response(achall.account_key)) + + # DNS updates take time to propagate and checking to see if the update has occurred is not + # reliable (the machine this code is running on might be able to see an update before + # the ACME server). So: we sleep for a short amount of time we believe to be long enough. + display_util.notify("Waiting %d seconds for DNS changes to propagate" % + self.conf('propagation-seconds')) + sleep(self.conf('propagation-seconds')) + + return responses + + def cleanup(self, achalls: list[achallenges.AnnotatedChallenge]) -> None: # pylint: disable=missing-function-docstring + if self._attempt_cleanup: + for achall in achalls: + domain = achall.identifier.value + validation_domain_name = achall.validation_domain_name(domain) + validation = achall.validation(achall.account_key) + + self._cleanup(domain, validation_domain_name, validation) + + @abc.abstractmethod + def _setup_credentials(self) -> None: # pragma: no cover + """ + Establish credentials, prompting if necessary. + """ + raise NotImplementedError() + + @abc.abstractmethod + def _perform(self, domain: str, validation_name: str, + validation: str) -> None: # pragma: no cover + """ + Performs a dns-01 challenge by creating a DNS TXT record. + + :param str domain: The domain being validated. + :param str validation_domain_name: The validation record domain name. + :param str validation: The validation record content. + :raises errors.PluginError: If the challenge cannot be performed + """ + raise NotImplementedError() + + @abc.abstractmethod + def _cleanup(self, domain: str, validation_name: str, + validation: str) -> None: # pragma: no cover + """ + Deletes the DNS TXT record which would have been created by `_perform_achall`. + + Fails gracefully if no such record exists. + + :param str domain: The domain being validated. + :param str validation_domain_name: The validation record domain name. + :param str validation: The validation record content. + """ + raise NotImplementedError() + + def _configure(self, key: str, label: str) -> None: + """ + Ensure that a configuration value is available. + + If necessary, prompts the user and stores the result. + + :param str key: The configuration key. + :param str label: The user-friendly label for this piece of information. + """ + + configured_value = self.conf(key) + if not configured_value: + new_value = self._prompt_for_data(label) + + setattr(self.config, self.dest(key), new_value) + + def _configure_file(self, key: str, label: str, + validator: Optional[Callable[[str], None]] = None) -> None: + """ + Ensure that a configuration value is available for a path. + + If necessary, prompts the user and stores the result. + + :param str key: The configuration key. + :param str label: The user-friendly label for this piece of information. + """ + + configured_value = self.conf(key) + if not configured_value: + new_value = self._prompt_for_file(label, validator) + + setattr(self.config, self.dest(key), os.path.abspath(os.path.expanduser(new_value))) + + def _configure_credentials( + self, key: str, label: str, required_variables: Optional[Mapping[str, str]] = None, + validator: Optional[Callable[['CredentialsConfiguration'], None]] = None + ) -> 'CredentialsConfiguration': + """ + As `_configure_file`, but for a credential configuration file. + + If necessary, prompts the user and stores the result. + + Always stores absolute paths to avoid issues during renewal. + + :param str key: The configuration key. + :param str label: The user-friendly label for this piece of information. + :param dict required_variables: Map of variable which must be present to error to display. + :param callable validator: A method which will be called to validate the + `CredentialsConfiguration` resulting from the supplied input after it has been validated + to contain the `required_variables`. Should throw a `~certbot.errors.PluginError` to + indicate any issue. + """ + + def __validator(filename: str) -> None: # pylint: disable=unused-private-member + applied_configuration = CredentialsConfiguration(filename, self.dest) + + if required_variables: + applied_configuration.require(required_variables) + + if validator: + validator(applied_configuration) + + self._configure_file(key, label, __validator) + + credentials_configuration = CredentialsConfiguration(self.conf(key), self.dest) + if required_variables: + credentials_configuration.require(required_variables) + + if validator: + validator(credentials_configuration) + + return credentials_configuration + + @staticmethod + def _prompt_for_data(label: str) -> str: + """ + Prompt the user for a piece of information. + + :param str label: The user-friendly label for this piece of information. + :returns: The user's response (guaranteed non-empty). + :rtype: str + """ + + def __validator(i: str) -> None: # pylint: disable=unused-private-member + if not i: + raise errors.PluginError('Please enter your {0}.'.format(label)) + + code, response = ops.validated_input( + __validator, + 'Input your {0}'.format(label), + force_interactive=True) + + if code == display_util.OK: + return response + raise errors.PluginError('{0} required to proceed.'.format(label)) + + @staticmethod + def _prompt_for_file(label: str, validator: Optional[Callable[[str], None]] = None) -> str: + """ + Prompt the user for a path. + + :param str label: The user-friendly label for the file. + :param callable validator: A method which will be called to validate the supplied input + after it has been validated to be a non-empty path to an existing file. Should throw a + `~certbot.errors.PluginError` to indicate any issue. + :returns: The user's response (guaranteed to exist). + :rtype: str + """ + + def __validator(filename: str) -> None: # pylint: disable=unused-private-member + if not filename: + raise errors.PluginError('Please enter a valid path to your {0}.'.format(label)) + + filename = os.path.expanduser(filename) + + validate_file(filename) + + if validator: + validator(filename) + + code, response = ops.validated_directory( + __validator, + 'Input the path to your {0}'.format(label), + force_interactive=True) + + if code == display_util.OK: + return response + raise errors.PluginError('{0} required to proceed.'.format(label)) + + +class CredentialsConfiguration: + """Represents a user-supplied filed which stores API credentials.""" + + def __init__(self, filename: str, mapper: Callable[[str], str] = lambda x: x) -> None: + """ + :param str filename: A path to the configuration file. + :param callable mapper: A transformation to apply to configuration key names + :raises errors.PluginError: If the file does not exist or is not a valid format. + """ + validate_file_permissions(filename) + + try: + self.confobj = configobj.ConfigObj(filename) + except configobj.ConfigObjError as e: + logger.debug( + "Error parsing credentials configuration '%s': %s", + filename, + e, + exc_info=True + ) + raise errors.PluginError( + "Error parsing credentials configuration '{}': {}".format( + filename, + e + ) + ) + + self.mapper = mapper + + def require(self, required_variables: Mapping[str, str]) -> None: + """Ensures that the supplied set of variables are all present in the file. + + :param dict required_variables: Map of variable which must be present to error to display. + :raises errors.PluginError: If one or more are missing. + """ + messages = [] + + for var in required_variables: + if not self._has(var): + messages.append('Property "{0}" not found (should be {1}).' + .format(self.mapper(var), required_variables[var])) + elif not self._get(var): + messages.append('Property "{0}" not set (should be {1}).' + .format(self.mapper(var), required_variables[var])) + + if messages: + raise errors.PluginError( + 'Missing {0} in credentials configuration file {1}:\n * {2}'.format( + 'property' if len(messages) == 1 else 'properties', + self.confobj.filename, + '\n * '.join(messages) + ) + ) + + def conf(self, var: str) -> Optional[str]: + """Find a configuration value for variable `var`, as transformed by `mapper`. + + :param str var: The variable to get. + :returns: The value of the variable, if it exists. + :rtype: str or None + """ + + return self._get(var) + + def _has(self, var: str) -> bool: + return self.mapper(var) in self.confobj + + def _get(self, var: str) -> Optional[str]: + return self.confobj.get(self.mapper(var)) + + +def validate_file(filename: str) -> None: + """Ensure that the specified file exists.""" + + if not os.path.exists(filename): + raise errors.PluginError('File not found: {0}'.format(filename)) + + if os.path.isdir(filename): + raise errors.PluginError('Path is a directory: {0}'.format(filename)) + + +def validate_file_permissions(filename: str) -> None: + """Ensure that the specified file exists and warn about unsafe permissions.""" + + validate_file(filename) + + if filesystem.has_world_permissions(filename): + logger.warning('Unsafe permissions on credentials configuration file: %s', filename) + + +def base_domain_name_guesses(domain: str) -> list[str]: + """Return a list of progressively less-specific domain names. + + One of these will probably be the domain name known to the DNS provider. + + :Example: + + >>> base_domain_name_guesses('foo.bar.baz.example.com') + ['foo.bar.baz.example.com', 'bar.baz.example.com', 'baz.example.com', 'example.com', 'com'] + + :param str domain: The domain for which to return guesses. + :returns: The a list of less specific domain names. + :rtype: list + """ + + fragments = domain.split('.') + return ['.'.join(fragments[i:]) for i in range(0, len(fragments))] diff --git a/certbot/src/certbot/plugins/dns_common_lexicon.py b/certbot/src/certbot/plugins/dns_common_lexicon.py new file mode 100644 index 00000000000..09891f43433 --- /dev/null +++ b/certbot/src/certbot/plugins/dns_common_lexicon.py @@ -0,0 +1,303 @@ +"""Common code for DNS Authenticator Plugins built on Lexicon.""" +import abc +import logging +import sys +from types import ModuleType +from typing import Any +from typing import cast +from typing import Mapping +from typing import Optional +from typing import Union +import warnings + +from requests.exceptions import HTTPError +from requests.exceptions import RequestException + +from certbot import configuration +from certbot import errors +from certbot.plugins import dns_common + +# Lexicon is not declared as a dependency in Certbot itself, +# but in the Certbot plugins backed by Lexicon. +# So we catch import error here to allow this module to be +# always importable, even if it does not make sense to use it +# if Lexicon is not available, obviously. +try: + from lexicon.client import Client + from lexicon.config import ConfigResolver + from lexicon.interfaces import Provider +except ImportError: # pragma: no cover + Client = None # type: ignore + ConfigResolver = None # type: ignore + Provider = None # type: ignore + +logger = logging.getLogger(__name__) + + +class LexiconClient: # pragma: no cover + """ + Encapsulates all communication with a DNS provider via Lexicon. + + .. deprecated:: 2.7.0 + Please use certbot.plugins.dns_common_lexicon.LexiconDNSAuthenticator instead. + """ + + def __init__(self) -> None: + self.provider: Provider + + def add_txt_record(self, domain: str, record_name: str, record_content: str) -> None: + """ + Add a TXT record using the supplied information. + + :param str domain: The domain to use to look up the managed zone. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + :raises errors.PluginError: if an error occurs communicating with the DNS Provider API + """ + self._find_domain_id(domain) + + try: + self.provider.create_record(rtype='TXT', name=record_name, content=record_content) + except RequestException as e: + logger.debug('Encountered error adding TXT record: %s', e, exc_info=True) + raise errors.PluginError('Error adding TXT record: {0}'.format(e)) + + def del_txt_record(self, domain: str, record_name: str, record_content: str) -> None: + """ + Delete a TXT record using the supplied information. + + :param str domain: The domain to use to look up the managed zone. + :param str record_name: The record name (typically beginning with '_acme-challenge.'). + :param str record_content: The record content (typically the challenge validation). + :raises errors.PluginError: if an error occurs communicating with the DNS Provider API + """ + try: + self._find_domain_id(domain) + except errors.PluginError as e: + logger.debug('Encountered error finding domain_id during deletion: %s', e, + exc_info=True) + return + + try: + self.provider.delete_record(rtype='TXT', name=record_name, content=record_content) + except RequestException as e: + logger.debug('Encountered error deleting TXT record: %s', e, exc_info=True) + + def _find_domain_id(self, domain: str) -> None: + """ + Find the domain_id for a given domain. + + :param str domain: The domain for which to find the domain_id. + :raises errors.PluginError: if the domain_id cannot be found. + """ + + domain_name_guesses = dns_common.base_domain_name_guesses(domain) + + for domain_name in domain_name_guesses: + try: + if hasattr(self.provider, 'options'): + # For Lexicon 2.x + self.provider.options['domain'] = domain_name + else: + # For Lexicon 3.x + self.provider.domain = domain_name + + self.provider.authenticate() + + return # If `authenticate` doesn't throw an exception, we've found the right name + except HTTPError as e: + result1 = self._handle_http_error(e, domain_name) + + if result1: + raise result1 + except Exception as e: # pylint: disable=broad-except + result2 = self._handle_general_error(e, domain_name) + + if result2: + raise result2 # pylint: disable=raising-bad-type + + raise errors.PluginError('Unable to determine zone identifier for {0} using zone names: {1}' + .format(domain, domain_name_guesses)) + + def _handle_http_error(self, e: HTTPError, domain_name: str) -> Optional[errors.PluginError]: + return errors.PluginError('Error determining zone identifier for {0}: {1}.' + .format(domain_name, e)) + + def _handle_general_error(self, e: Exception, domain_name: str) -> Optional[errors.PluginError]: + if not str(e).startswith('No domain found'): + return errors.PluginError('Unexpected error determining zone identifier for {0}: {1}' + .format(domain_name, e)) + return None + + +def build_lexicon_config(lexicon_provider_name: str, + lexicon_options: Mapping[str, Any], provider_options: Mapping[str, Any] + ) -> Union[ConfigResolver, dict[str, Any]]: # pragma: no cover + """ + Convenient function to build a Lexicon 2.x/3.x config object. + + :param str lexicon_provider_name: the name of the lexicon provider to use + :param dict lexicon_options: options specific to lexicon + :param dict provider_options: options specific to provider + :return: configuration to apply to the provider + :rtype: ConfigurationResolver or dict + + .. deprecated:: 2.7.0 + Please use certbot.plugins.dns_common_lexicon.LexiconDNSAuthenticator instead. + """ + config_dict: dict[str, Any] = {'provider_name': lexicon_provider_name} + config_dict.update(lexicon_options) + if ConfigResolver is None: + # Lexicon 2.x + config_dict.update(provider_options) + return config_dict + else: + # Lexicon 3.x + provider_config: dict[str, Any] = {} + provider_config.update(provider_options) + config_dict[lexicon_provider_name] = provider_config + return ConfigResolver().with_dict(config_dict).with_env() + + +class LexiconDNSAuthenticator(dns_common.DNSAuthenticator): + """ + Base class for a DNS authenticator that uses Lexicon client + as backend to execute DNS record updates + """ + + def __init__(self, config: configuration.NamespaceConfig, name: str): + super().__init__(config, name) + self._provider_options: list[tuple[str, str, str]] = [] + self._credentials: dns_common.CredentialsConfiguration + + @property + @abc.abstractmethod + def _provider_name(self) -> str: + """ + The name of the Lexicon provider to use + """ + + @property + def _ttl(self) -> int: + """ + Time to live to apply to the DNS records created by this Authenticator + """ + return 60 + + def _add_provider_option(self, creds_var_name: str, creds_var_label: str, + lexicon_provider_option_name: str) -> None: + self._provider_options.append( + (creds_var_name, creds_var_label, lexicon_provider_option_name)) + + def _build_lexicon_config(self, domain: str) -> ConfigResolver: + if not hasattr(self, '_credentials'): # pragma: no cover + self._setup_credentials() + + dict_config = { + 'domain': domain, + # We bypass Lexicon subdomain resolution by setting the 'delegated' field in the config + # to the value of the 'domain' field itself. Here we consider that the domain passed to + # _build_lexicon_config() is already the exact subdomain of the actual DNS zone to use. + 'delegated': domain, + 'provider_name': self._provider_name, + 'ttl': self._ttl, + self._provider_name: {item[2]: self._credentials.conf(item[0]) + for item in self._provider_options} + } + return ConfigResolver().with_dict(dict_config).with_env() + + def _setup_credentials(self) -> None: + self._credentials = self._configure_credentials( + key='credentials', + label=f'Credentials INI file for {self._provider_name} DNS authenticator', + required_variables={item[0]: item[1] for item in self._provider_options}, + ) + + def _perform(self, domain: str, validation_name: str, validation: str) -> None: + resolved_domain = self._resolve_domain(domain) + + try: + with Client(self._build_lexicon_config(resolved_domain)) as operations: + operations.create_record(rtype='TXT', name=validation_name, content=validation) + except RequestException as e: + logger.debug('Encountered error adding TXT record: %s', e, exc_info=True) + raise errors.PluginError('Error adding TXT record: {0}'.format(e)) + + def _cleanup(self, domain: str, validation_name: str, validation: str) -> None: + try: + resolved_domain = self._resolve_domain(domain) + except errors.PluginError as e: + logger.debug('Encountered error finding domain_id during deletion: %s', e, + exc_info=True) + return + + try: + with Client(self._build_lexicon_config(resolved_domain)) as operations: + operations.delete_record(rtype='TXT', name=validation_name, content=validation) + except RequestException as e: + logger.debug('Encountered error deleting TXT record: %s', e, exc_info=True) + + def _resolve_domain(self, domain: str) -> str: + domain_name_guesses = dns_common.base_domain_name_guesses(domain) + + for domain_name in domain_name_guesses: + try: + # Using client as a context manager requires `dns-lexicon>=3.14` and we may want to + # provide better checks and error handling around this in the future. + with Client(self._build_lexicon_config(domain_name)): + return domain_name + except HTTPError as e: + result1 = self._handle_http_error(e, domain_name) + + if result1: + raise result1 + except Exception as e: # pylint: disable=broad-except + result2 = self._handle_general_error(e, domain_name) + + if result2: + raise result2 # pylint: disable=raising-bad-type + + raise errors.PluginError('Unable to determine zone identifier for {0} using zone names: {1}' + .format(domain, domain_name_guesses)) + + def _handle_http_error(self, e: HTTPError, domain_name: str) -> Optional[errors.PluginError]: + return errors.PluginError('Error determining zone identifier for {0}: {1}.' + .format(domain_name, e)) + + def _handle_general_error(self, e: Exception, domain_name: str) -> Optional[errors.PluginError]: + if not str(e).startswith('No domain found'): + return errors.PluginError('Unexpected error determining zone identifier for {0}: {1}' + .format(domain_name, e)) + return None + + +# This class takes a similar approach to the cryptography project to deprecate attributes +# in public modules. See the _ModuleWithDeprecation class here: +# https://github.com/pyca/cryptography/blob/91105952739442a74582d3e62b3d2111365b0dc7/src/cryptography/utils.py#L129 +class _DeprecationModule: + """ + Internal class delegating to a module, and displaying warnings when attributes + related to deprecated attributes in the current module. + """ + def __init__(self, module: ModuleType): + self.__dict__['_module'] = module + + def __getattr__(self, attr: str) -> Any: + if attr in ('LexiconClient', 'build_lexicon_config'): + warnings.warn(f'{attr} attribute in {__name__} module is deprecated ' + 'and will be removed soon.', + DeprecationWarning, stacklevel=2) + return getattr(self._module, attr) + + def __setattr__(self, attr: str, value: Any) -> None: # pragma: no cover + setattr(self._module, attr, value) + + def __delattr__(self, attr: str) -> Any: # pragma: no cover + delattr(self._module, attr) + + def __dir__(self) -> list[str]: # pragma: no cover + return ['_module'] + dir(self._module) + + +# Patching ourselves to warn about deprecation and planned removal of some elements in the module. +sys.modules[__name__] = cast(ModuleType, _DeprecationModule(sys.modules[__name__])) diff --git a/certbot/src/certbot/plugins/dns_test_common.py b/certbot/src/certbot/plugins/dns_test_common.py new file mode 100644 index 00000000000..b76fd61dc7b --- /dev/null +++ b/certbot/src/certbot/plugins/dns_test_common.py @@ -0,0 +1,86 @@ +"""Base test class for DNS authenticators.""" +from typing import Any +from typing import Mapping +from typing import Protocol +from unittest import mock + +import configobj +import josepy as jose + +from acme import challenges, messages +from certbot import achallenges +from certbot.compat import filesystem +from certbot.plugins.dns_common import DNSAuthenticator +from certbot.tests import acme_util +from certbot.tests import util as test_util + +DOMAIN = 'example.com' +KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) + + +class _AuthenticatorCallableTestCase(Protocol): + """Protocol describing a TestCase able to call a real DNSAuthenticator instance.""" + auth: DNSAuthenticator + + def assertTrue(self, *unused_args: Any) -> None: + """ + See + https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertTrue + """ + + def assertEqual(self, *unused_args: Any) -> None: + """ + See + https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertEqual + """ + + def assertRaises(self, *unused_args: Any) -> None: + """ + See + https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises + """ + + +class BaseAuthenticatorTest: + """ + A base test class to reduce duplication between test code for DNS Authenticator Plugins. + + Assumes: + * That subclasses also subclass unittest.TestCase + * That the authenticator is stored as self.auth + """ + + achall = achallenges.KeyAuthorizationAnnotatedChallenge( + challb=acme_util.DNS01, + identifier=messages.Identifier(typ=messages.IDENTIFIER_FQDN, value=DOMAIN), + account_key=KEY) + + def test_more_info(self: _AuthenticatorCallableTestCase) -> None: + self.assertTrue(isinstance(self.auth.more_info(), str)) # pylint: disable=no-member + + def test_get_chall_pref(self: _AuthenticatorCallableTestCase) -> None: + self.assertEqual(self.auth.get_chall_pref("example.org"), [challenges.DNS01]) # pylint: disable=no-member + + def test_parser_arguments(self: _AuthenticatorCallableTestCase) -> None: + m = mock.MagicMock() + self.auth.add_parser_arguments(m) # pylint: disable=no-member + + m.assert_any_call('propagation-seconds', type=int, default=mock.ANY, help=mock.ANY) + + +def write(values: Mapping[str, Any], path: str) -> None: + """Write the specified values to a config file. + + :param dict values: A map of values to write. + :param str path: Where to write the values. + """ + + config = configobj.ConfigObj() + + for key in values: + config[key] = values[key] + + with open(path, "wb") as f: + config.write(outfile=f) + + filesystem.chmod(path, 0o600) diff --git a/certbot/src/certbot/plugins/dns_test_common_lexicon.py b/certbot/src/certbot/plugins/dns_test_common_lexicon.py new file mode 100644 index 00000000000..3e21e9967d6 --- /dev/null +++ b/certbot/src/certbot/plugins/dns_test_common_lexicon.py @@ -0,0 +1,354 @@ +"""Base test class for DNS authenticators built on Lexicon.""" +import contextlib +import sys +from types import ModuleType +from typing import Any +from typing import cast +from typing import Generator +from typing import Protocol +from unittest import mock +from unittest.mock import MagicMock +import warnings + +import josepy as jose +from requests import Response +from requests.exceptions import HTTPError +from requests.exceptions import RequestException + +from certbot import errors +from certbot.achallenges import AnnotatedChallenge +from certbot.plugins import dns_test_common + +with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=DeprecationWarning) + from certbot.plugins.dns_common_lexicon import LexiconClient + +from certbot.plugins.dns_test_common import _AuthenticatorCallableTestCase +from certbot.tests import util as test_util + +DOMAIN = 'example.com' +KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) + +DOMAIN_NOT_FOUND = Exception('No domain found') +GENERIC_ERROR = RequestException +LOGIN_ERROR = HTTPError('400 Client Error: ...', response=Response()) +UNKNOWN_LOGIN_ERROR = HTTPError('500 Surprise! Error: ...', response=Response()) + + +class _AuthenticatorCallableLexiconTestCase(_AuthenticatorCallableTestCase, Protocol): + """ + Protocol describing a TestCase suitable to test challenges against + a mocked LexiconClient instance. + """ + mock_client: MagicMock + achall: AnnotatedChallenge + + +class _LexiconAwareTestCase(Protocol): + """ + Protocol describing a TestCase suitable to test a real LexiconClient instance. + """ + client: LexiconClient + provider_mock: MagicMock + + record_prefix: str + record_name: str + record_content: str + + DOMAIN_NOT_FOUND: Exception + GENERIC_ERROR: Exception + LOGIN_ERROR: Exception + UNKNOWN_LOGIN_ERROR: Exception + + def assertRaises(self, *unused_args: Any) -> None: + """ + See + https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertRaises + """ + + +# These classes are intended to be subclassed/mixed in, so not all members are defined. +# pylint: disable=no-member + +class BaseLexiconAuthenticatorTest(dns_test_common.BaseAuthenticatorTest): # pragma: no cover + + @test_util.patch_display_util() + def test_perform(self: _AuthenticatorCallableLexiconTestCase, + unused_mock_get_utility: Any) -> None: + self.auth.perform([self.achall]) + + expected = [mock.call.add_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)] + self.assertEqual(expected, self.mock_client.mock_calls) + + def test_cleanup(self: _AuthenticatorCallableLexiconTestCase) -> None: + self.auth._attempt_cleanup = True # _attempt_cleanup | pylint: disable=protected-access + self.auth.cleanup([self.achall]) + + expected = [mock.call.del_txt_record(DOMAIN, '_acme-challenge.'+DOMAIN, mock.ANY)] + self.assertEqual(expected, self.mock_client.mock_calls) + + +class BaseLexiconClientTest: # pragma: no cover + DOMAIN_NOT_FOUND = DOMAIN_NOT_FOUND + GENERIC_ERROR = GENERIC_ERROR + LOGIN_ERROR = LOGIN_ERROR + UNKNOWN_LOGIN_ERROR = UNKNOWN_LOGIN_ERROR + + record_prefix = "_acme-challenge" + record_name = record_prefix + "." + DOMAIN + record_content = "bar" + + def test_add_txt_record(self: _LexiconAwareTestCase) -> None: + self.client.add_txt_record(DOMAIN, self.record_name, self.record_content) + + self.provider_mock.create_record.assert_called_with(rtype='TXT', + name=self.record_name, + content=self.record_content) + + def test_add_txt_record_try_twice_to_find_domain(self: _LexiconAwareTestCase) -> None: + self.provider_mock.authenticate.side_effect = [self.DOMAIN_NOT_FOUND, ''] + + self.client.add_txt_record(DOMAIN, self.record_name, self.record_content) + + self.provider_mock.create_record.assert_called_with(rtype='TXT', + name=self.record_name, + content=self.record_content) + + def test_add_txt_record_fail_to_find_domain(self: _LexiconAwareTestCase) -> None: + self.provider_mock.authenticate.side_effect = [self.DOMAIN_NOT_FOUND, + self.DOMAIN_NOT_FOUND, + self.DOMAIN_NOT_FOUND,] + + self.assertRaises(errors.PluginError, + self.client.add_txt_record, + DOMAIN, self.record_name, self.record_content) + + def test_add_txt_record_fail_to_authenticate(self: _LexiconAwareTestCase) -> None: + self.provider_mock.authenticate.side_effect = self.LOGIN_ERROR + + self.assertRaises(errors.PluginError, + self.client.add_txt_record, + DOMAIN, self.record_name, self.record_content) + + def test_add_txt_record_fail_to_authenticate_with_unknown_error( + self: _LexiconAwareTestCase) -> None: + self.provider_mock.authenticate.side_effect = self.UNKNOWN_LOGIN_ERROR + + self.assertRaises(errors.PluginError, + self.client.add_txt_record, + DOMAIN, self.record_name, self.record_content) + + def test_add_txt_record_error_finding_domain(self: _LexiconAwareTestCase) -> None: + self.provider_mock.authenticate.side_effect = self.GENERIC_ERROR + + self.assertRaises(errors.PluginError, + self.client.add_txt_record, + DOMAIN, self.record_name, self.record_content) + + def test_add_txt_record_error_adding_record(self: _LexiconAwareTestCase) -> None: + self.provider_mock.create_record.side_effect = self.GENERIC_ERROR + + self.assertRaises(errors.PluginError, + self.client.add_txt_record, + DOMAIN, self.record_name, self.record_content) + + def test_del_txt_record(self: _LexiconAwareTestCase) -> None: + self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + self.provider_mock.delete_record.assert_called_with(rtype='TXT', + name=self.record_name, + content=self.record_content) + + def test_del_txt_record_fail_to_find_domain(self: _LexiconAwareTestCase) -> None: + self.provider_mock.authenticate.side_effect = [self.DOMAIN_NOT_FOUND, + self.DOMAIN_NOT_FOUND, + self.DOMAIN_NOT_FOUND, ] + + self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + def test_del_txt_record_fail_to_authenticate(self: _LexiconAwareTestCase) -> None: + self.provider_mock.authenticate.side_effect = self.LOGIN_ERROR + + self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + def test_del_txt_record_fail_to_authenticate_with_unknown_error( + self: _LexiconAwareTestCase) -> None: + self.provider_mock.authenticate.side_effect = self.UNKNOWN_LOGIN_ERROR + + self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + def test_del_txt_record_error_finding_domain(self: _LexiconAwareTestCase) -> None: + self.provider_mock.authenticate.side_effect = self.GENERIC_ERROR + + self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + def test_del_txt_record_error_deleting_record(self: _LexiconAwareTestCase) -> None: + self.provider_mock.delete_record.side_effect = self.GENERIC_ERROR + + self.client.del_txt_record(DOMAIN, self.record_name, self.record_content) + + +class _BaseLexiconDNSAuthenticatorTestProto(_AuthenticatorCallableTestCase, Protocol): + """Protocol for BaseLexiconDNSAuthenticatorTest instances""" + DOMAIN_NOT_FOUND: Exception + GENERIC_ERROR: Exception + LOGIN_ERROR: Exception + UNKNOWN_LOGIN_ERROR: Exception + + achall: AnnotatedChallenge + + +class BaseLexiconDNSAuthenticatorTest(dns_test_common.BaseAuthenticatorTest): + + DOMAIN_NOT_FOUND = DOMAIN_NOT_FOUND + GENERIC_ERROR = GENERIC_ERROR + LOGIN_ERROR = LOGIN_ERROR + UNKNOWN_LOGIN_ERROR = UNKNOWN_LOGIN_ERROR + + def test_perform_succeed(self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with test_util.patch_display_util(): + with _patch_lexicon_client() as (mock_client, mock_operations): + self.auth.perform([self.achall]) + + mock_client.assert_called() + config = mock_client.call_args[0][0] + self.assertEqual(DOMAIN, config.resolve('lexicon:domain')) + + mock_operations.create_record.assert_called_with( + rtype='TXT', name=f'_acme-challenge.{DOMAIN}', content=mock.ANY) + + def test_perform_with_one_domain_resolution_failure_succeed( + self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with test_util.patch_display_util(): + with _patch_lexicon_client() as (mock_client, mock_operations): + mock_client.return_value.__enter__.side_effect = [ + self.DOMAIN_NOT_FOUND, # First resolution domain attempt + mock_operations, # Second resolution domain attempt + mock_operations, # Create record operation + ] + self.auth.perform([self.achall]) + + def test_perform_with_two_domain_resolution_failures_raise( + self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with test_util.patch_display_util(): + with _patch_lexicon_client() as (mock_client, _): + mock_client.return_value.__enter__.side_effect = self.DOMAIN_NOT_FOUND + self.assertRaises(errors.PluginError, + self.auth.perform, + [self.achall]) + + def test_perform_with_domain_resolution_general_failure_raise( + self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with test_util.patch_display_util(): + with _patch_lexicon_client() as (mock_client, _): + mock_client.return_value.__enter__.side_effect = self.GENERIC_ERROR + self.assertRaises(errors.PluginError, + self.auth.perform, + [self.achall]) + + def test_perform_with_auth_failure_raise(self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with test_util.patch_display_util(): + with _patch_lexicon_client() as (mock_client, _): + mock_client.side_effect = self.LOGIN_ERROR + self.assertRaises(errors.PluginError, + self.auth.perform, + [self.achall]) + + def test_perform_with_unknown_auth_failure_raise( + self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with test_util.patch_display_util(): + with _patch_lexicon_client() as (mock_client, _): + mock_client.side_effect = self.UNKNOWN_LOGIN_ERROR + self.assertRaises(errors.PluginError, + self.auth.perform, + [self.achall]) + + def test_perform_with_create_record_failure_raise( + self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with test_util.patch_display_util(): + with _patch_lexicon_client() as (_, mock_operations): + mock_operations.create_record.side_effect = self.GENERIC_ERROR + self.assertRaises(errors.PluginError, + self.auth.perform, + [self.achall]) + + def test_cleanup_success(self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + self.auth._attempt_cleanup = True # _attempt_cleanup | pylint: disable=protected-access + with _patch_lexicon_client() as (mock_client, mock_operations): + self.auth.cleanup([self.achall]) + + mock_client.assert_called() + config = mock_client.call_args[0][0] + self.assertEqual(DOMAIN, config.resolve('lexicon:domain')) + + mock_operations.delete_record.assert_called_with( + rtype='TXT', name=f'_acme-challenge.{DOMAIN}', content=mock.ANY) + + def test_cleanup_with_auth_failure_ignore(self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with _patch_lexicon_client() as (mock_client, _): + mock_client.side_effect = self.LOGIN_ERROR + self.auth.cleanup([self.achall]) + + def test_cleanup_with_unknown_auth_failure_ignore( + self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with _patch_lexicon_client() as (mock_client, _): + mock_client.side_effect = self.LOGIN_ERROR + self.auth.cleanup([self.achall]) + + def test_cleanup_with_domain_resolution_failure_ignore( + self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with _patch_lexicon_client() as (mock_client, _): + mock_client.return_value.__enter__.side_effect = self.DOMAIN_NOT_FOUND + self.auth.cleanup([self.achall]) + + def test_cleanup_with_domain_resolution_general_failure_ignore( + self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with _patch_lexicon_client() as (mock_client, _): + mock_client.return_value.__enter__.side_effect = self.GENERIC_ERROR + self.auth.cleanup([self.achall]) + + def test_cleanup_with_delete_record_failure_ignore( + self: _BaseLexiconDNSAuthenticatorTestProto) -> None: + with _patch_lexicon_client() as (_, mock_operations): + mock_operations.create_record.side_effect = self.GENERIC_ERROR + self.auth.cleanup([self.achall]) + + +@contextlib.contextmanager +def _patch_lexicon_client() -> Generator[tuple[MagicMock, MagicMock], None, None]: + with mock.patch('certbot.plugins.dns_common_lexicon.Client') as mock_client: + mock_operations = MagicMock() + mock_client.return_value.__enter__.return_value = mock_operations + yield mock_client, mock_operations + + +# This class takes a similar approach to the cryptography project to deprecate attributes +# in public modules. See the _ModuleWithDeprecation class here: +# https://github.com/pyca/cryptography/blob/91105952739442a74582d3e62b3d2111365b0dc7/src/cryptography/utils.py#L129 +class _DeprecationModule: + """ + Internal class delegating to a module, and displaying warnings when attributes + related to deprecated attributes in the current module. + """ + def __init__(self, module: ModuleType): + self.__dict__['_module'] = module + + def __getattr__(self, attr: str) -> Any: + if attr in ('BaseLexiconAuthenticatorTest', 'BaseLexiconClientTest'): + warnings.warn(f'{attr} attribute in {__name__} module is deprecated ' + 'and will be removed soon.', + DeprecationWarning, stacklevel=2) + return getattr(self._module, attr) + + def __setattr__(self, attr: str, value: Any) -> None: # pragma: no cover + setattr(self._module, attr, value) + + def __delattr__(self, attr: str) -> Any: # pragma: no cover + delattr(self._module, attr) + + def __dir__(self) -> list[str]: # pragma: no cover + return ['_module'] + dir(self._module) + + +# Patching ourselves to warn about deprecation and planned removal of some elements in the module. +sys.modules[__name__] = cast(ModuleType, _DeprecationModule(sys.modules[__name__])) diff --git a/certbot/src/certbot/plugins/enhancements.py b/certbot/src/certbot/plugins/enhancements.py new file mode 100644 index 00000000000..d7ec07dcf85 --- /dev/null +++ b/certbot/src/certbot/plugins/enhancements.py @@ -0,0 +1,190 @@ +"""New interface style Certbot enhancements""" +import abc +from typing import Any +from typing import Callable +from typing import Generator +from typing import Iterable +from typing import Optional + +from certbot import configuration +from certbot import interfaces +from certbot._internal import constants + +ENHANCEMENTS = ["redirect", "ensure-http-header", "ocsp-stapling"] +"""List of possible :class:`certbot.interfaces.Installer` +enhancements. + +List of expected options parameters: +- redirect: None +- ensure-http-header: name of header (i.e. Strict-Transport-Security) +- ocsp-stapling: certificate chain file path + +""" + + +def enabled_enhancements( + config: configuration.NamespaceConfig) -> Generator[dict[str, Any], None, None]: + """ + Generator to yield the enabled new style enhancements. + + :param config: Configuration. + :type config: certbot.configuration.NamespaceConfig + """ + for enh in _INDEX: + if getattr(config, enh["cli_dest"]): + yield enh + + +def are_requested(config: configuration.NamespaceConfig) -> bool: + """ + Checks if one or more of the requested enhancements are those of the new + enhancement interfaces. + + :param config: Configuration. + :type config: certbot.configuration.NamespaceConfig + """ + return any(enabled_enhancements(config)) + + +def are_supported(config: configuration.NamespaceConfig, + installer: Optional[interfaces.Installer]) -> bool: + """ + Checks that all of the requested enhancements are supported by the + installer. + + :param config: Configuration. + :type config: certbot.configuration.NamespaceConfig + + :param installer: Installer object + :type installer: interfaces.Installer + + :returns: If all the requested enhancements are supported by the installer + :rtype: bool + """ + for enh in enabled_enhancements(config): + if not isinstance(installer, enh["class"]): + return False + return True + + +def enable(lineage: Optional[interfaces.RenewableCert], domains: Iterable[str], + installer: Optional[interfaces.Installer], + config: configuration.NamespaceConfig) -> None: + """ + Run enable method for each requested enhancement that is supported. + + :param lineage: Certificate lineage object + :type lineage: certbot.interfaces.RenewableCert + + :param domains: List of domains in certificate to enhance + :type domains: str + + :param installer: Installer object + :type installer: interfaces.Installer + + :param config: Configuration. + :type config: certbot.configuration.NamespaceConfig + """ + if installer: + for enh in enabled_enhancements(config): + getattr(installer, enh["enable_function"])(lineage, domains) + + +def populate_cli(add: Callable[..., None]) -> None: + """ + Populates the command line flags for certbot._internal.cli.HelpfulParser + + :param add: Add function of certbot._internal.cli.HelpfulParser + :type add: func + """ + for enh in _INDEX: + add(enh["cli_groups"], enh["cli_flag"], action=enh["cli_action"], + dest=enh["cli_dest"], default=enh["cli_flag_default"], + help=enh["cli_help"]) + + +class AutoHSTSEnhancement(object, metaclass=abc.ABCMeta): + """ + Enhancement interface that installer plugins can implement in order to + provide functionality that configures the software to have a + 'Strict-Transport-Security' with initially low max-age value that will + increase over time. + + The plugins implementing new style enhancements are responsible of handling + the saving of configuration checkpoints as well as calling possible restarts + of managed software themselves. For update_autohsts method, the installer may + have to call prepare() to finalize the plugin initialization. + + Methods: + enable_autohsts is called when the header is initially installed using a + low max-age value. + + update_autohsts is called every time when Certbot is run using 'renew' + verb. The max-age value should be increased over time using this method. + + deploy_autohsts is called for every lineage that has had its certificate + renewed. A long HSTS max-age value should be set here, as we should be + confident that the user is able to automatically renew their certificates. + + + """ + + @abc.abstractmethod + def update_autohsts(self, lineage: interfaces.RenewableCert, *args: Any, **kwargs: Any) -> None: + """ + Gets called for each lineage every time Certbot is run with 'renew' verb. + Implementation of this method should increase the max-age value. + + :param lineage: Certificate lineage object + :type lineage: certbot.interfaces.RenewableCert + + .. note:: prepare() method inherited from `interfaces.Plugin` might need + to be called manually within implementation of this interface method + to finalize the plugin initialization. + """ + + @abc.abstractmethod + def deploy_autohsts(self, lineage: interfaces.RenewableCert, *args: Any, **kwargs: Any) -> None: + """ + Gets called for a lineage when its certificate is successfully renewed. + Long max-age value should be set in implementation of this method. + + :param lineage: Certificate lineage object + :type lineage: certbot.interfaces.RenewableCert + """ + + @abc.abstractmethod + def enable_autohsts(self, lineage: Optional[interfaces.RenewableCert], domains: Iterable[str], + *args: Any, **kwargs: Any) -> None: + """ + Enables the AutoHSTS enhancement, installing + Strict-Transport-Security header with a low initial value to be increased + over the subsequent runs of Certbot renew. + + :param lineage: Certificate lineage object + :type lineage: certbot.interfaces.RenewableCert + + :param domains: List of domains in certificate to enhance + :type domains: `list` of `str` + """ + + +# This is used to configure internal new style enhancements in Certbot. These +# enhancement interfaces need to be defined in this file. Please do not modify +# this list from plugin code. +_INDEX: list[dict[str, Any]] = [ + { + "name": "AutoHSTS", + "cli_help": "Gradually increasing max-age value for HTTP Strict Transport "+ + "Security security header", + "cli_flag": "--auto-hsts", + "cli_flag_default": constants.CLI_DEFAULTS["auto_hsts"], + "cli_groups": ["security", "enhance"], + "cli_dest": "auto_hsts", + "cli_action": "store_true", + "class": AutoHSTSEnhancement, + "updater_function": "update_autohsts", + "deployer_function": "deploy_autohsts", + "enable_function": "enable_autohsts" + } +] diff --git a/certbot/src/certbot/plugins/nginx.py b/certbot/src/certbot/plugins/nginx.py new file mode 100644 index 00000000000..41fd96aef3b --- /dev/null +++ b/certbot/src/certbot/plugins/nginx.py @@ -0,0 +1,7 @@ +"""Despite being public API, this is only meant for use in our certbot-nginx plugin, and isn't +intended for public use.""" + +from certbot._internal.plugins.nginx import configurator + + +ENTRYPOINT = configurator.NginxConfigurator diff --git a/certbot/src/certbot/plugins/storage.py b/certbot/src/certbot/plugins/storage.py new file mode 100644 index 00000000000..dbe6f16eef1 --- /dev/null +++ b/certbot/src/certbot/plugins/storage.py @@ -0,0 +1,123 @@ +"""Plugin storage class.""" +import json +import logging +from typing import Any + +from certbot import configuration +from certbot import errors +from certbot.compat import filesystem +from certbot.compat import os + +logger = logging.getLogger(__name__) + + +class PluginStorage: + """Class implementing storage functionality for plugins""" + + def __init__(self, config: configuration.NamespaceConfig, classkey: str) -> None: + """Initializes PluginStorage object storing required configuration + options. + + :param .configuration.NamespaceConfig config: Configuration object + :param str classkey: class name to use as root key in storage file + + """ + + self._config = config + self._classkey = classkey + self._initialized = False + self._data: dict + self._storagepath: str + + def _initialize_storage(self) -> None: + """Initializes PluginStorage data and reads current state from the disk + if the storage json exists.""" + + self._storagepath = os.path.join(self._config.config_dir, ".pluginstorage.json") + self._load() + self._initialized = True + + def _load(self) -> None: + """Reads PluginStorage content from the disk to a dict structure + + :raises .errors.PluginStorageError: when unable to open or read the file + """ + data: dict[str, Any] = {} + filedata = "" + try: + with open(self._storagepath, 'r') as fh: + filedata = fh.read() + except OSError as e: + errmsg = "Could not read PluginStorage data file: {0} : {1}".format( + self._storagepath, str(e)) + if os.path.isfile(self._storagepath): + # Only error out if file exists, but cannot be read + logger.error(errmsg) + raise errors.PluginStorageError(errmsg) + try: + data = json.loads(filedata) + except ValueError: + if not filedata: + logger.debug("Plugin storage file %s was empty, no values loaded", + self._storagepath) + else: + errmsg = "PluginStorage file {0} is corrupted.".format( + self._storagepath) + logger.error(errmsg) + raise errors.PluginStorageError(errmsg) + self._data = data + + def save(self) -> None: + """Saves PluginStorage content to disk + + :raises .errors.PluginStorageError: when unable to serialize the data + or write it to the filesystem + """ + if not self._initialized: + errmsg = "Unable to save, no values have been added to PluginStorage." + logger.error(errmsg) + raise errors.PluginStorageError(errmsg) + + try: + serialized = json.dumps(self._data) + except TypeError as e: + errmsg = "Could not serialize PluginStorage data: {0}".format( + str(e)) + logger.error(errmsg) + raise errors.PluginStorageError(errmsg) + try: + with os.fdopen(filesystem.open( + self._storagepath, + os.O_WRONLY | os.O_CREAT | os.O_TRUNC, + 0o600), 'w') as fh: + fh.write(serialized) + except OSError as e: + errmsg = "Could not write PluginStorage data to file {0} : {1}".format( + self._storagepath, str(e)) + logger.error(errmsg) + raise errors.PluginStorageError(errmsg) + + def put(self, key: str, value: Any) -> None: + """Put configuration value to PluginStorage + + :param str key: Key to store the value to + :param value: Data to store + """ + if not self._initialized: + self._initialize_storage() + + if self._classkey not in self._data: + self._data[self._classkey] = {} + self._data[self._classkey][key] = value + + def fetch(self, key: str) -> Any: + """Get configuration value from PluginStorage + + :param str key: Key to get value from the storage + + :raises KeyError: If the key doesn't exist in the storage + """ + if not self._initialized: + self._initialize_storage() + + return self._data[self._classkey][key] diff --git a/certbot/src/certbot/plugins/util.py b/certbot/src/certbot/plugins/util.py new file mode 100644 index 00000000000..107b7f8cdfe --- /dev/null +++ b/certbot/src/certbot/plugins/util.py @@ -0,0 +1,60 @@ +"""Plugin utilities.""" +import logging + +from certbot import util +from certbot.compat import os +from certbot.compat.misc import STANDARD_BINARY_DIRS + +logger = logging.getLogger(__name__) + + +def get_prefixes(path: str) -> list[str]: + """Retrieves all possible path prefixes of a path, in descending order + of length. For instance: + + * (Linux) `/a/b/c` returns `['/a/b/c', '/a/b', '/a', '/']` + * (Windows) `C:\\a\\b\\c` returns `['C:\\a\\b\\c', 'C:\\a\\b', 'C:\\a', 'C:']` + + :param str path: the path to break into prefixes + + :returns: all possible path prefixes of given path in descending order + :rtype: `list` of `str` + """ + prefix = os.path.normpath(path) + prefixes: list[str] = [] + while prefix: + prefixes.append(prefix) + prefix, _ = os.path.split(prefix) + # break once we hit the root path + if prefix == prefixes[-1]: + break + return prefixes + + +def path_surgery(cmd: str) -> bool: + """Attempt to perform PATH surgery to find cmd + + Mitigates https://github.com/certbot/certbot/issues/1833 + + :param str cmd: the command that is being searched for in the PATH + + :returns: True if the operation succeeded, False otherwise + """ + path = os.environ["PATH"] + added = [] + for d in STANDARD_BINARY_DIRS: + if d not in path: + path += os.pathsep + d + added.append(d) + + if any(added): + logger.debug("Can't find %s, attempting PATH mitigation by adding %s", + cmd, os.pathsep.join(added)) + os.environ["PATH"] = path + + if util.exe_exists(cmd): + return True + expanded = " expanded" if any(added) else "" + logger.debug("Failed to find executable %s in%s PATH: %s", cmd, + expanded, path) + return False diff --git a/certbot/src/certbot/py.typed b/certbot/src/certbot/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/certbot/reverter.py b/certbot/src/certbot/reverter.py similarity index 77% rename from certbot/reverter.py rename to certbot/src/certbot/reverter.py index 91903735807..62be16eab47 100644 --- a/certbot/reverter.py +++ b/certbot/src/certbot/reverter.py @@ -2,25 +2,23 @@ import csv import glob import logging -import os import shutil import time import traceback +from typing import Iterable +from typing import TextIO -import six -import zope.component - -from certbot import compat -from certbot import constants +from certbot import configuration from certbot import errors -from certbot import interfaces from certbot import util - +from certbot._internal import constants +from certbot.compat import filesystem +from certbot.compat import os logger = logging.getLogger(__name__) -class Reverter(object): +class Reverter: """Reverter Class - save and revert configuration checkpoints. This class can be used by the plugins, especially Installers, to @@ -59,17 +57,16 @@ class Reverter(object): .. note:: Consider moving everything over to CSV format. :param config: Configuration. - :type config: :class:`certbot.interfaces.IConfig` + :type config: :class:`certbot.configuration.NamespaceConfig` """ - def __init__(self, config): + def __init__(self, config: configuration.NamespaceConfig) -> None: self.config = config util.make_or_verify_dir( - config.backup_dir, constants.CONFIG_DIRS_MODE, compat.os_geteuid(), - self.config.strict_permissions) + config.backup_dir, constants.CONFIG_DIRS_MODE, self.config.strict_permissions) - def revert_temporary_config(self): + def revert_temporary_config(self) -> None: """Reload users original configuration files after a temporary save. This function should reinstall the users original configuration files @@ -89,7 +86,7 @@ def revert_temporary_config(self): ) raise errors.ReverterError("Unable to revert temporary config") - def rollback_checkpoints(self, rollback=1): + def rollback_checkpoints(self, rollback: int = 1) -> None: """Revert 'rollback' number of configuration checkpoints. :param int rollback: Number of checkpoints to reverse. A str num will be @@ -131,62 +128,7 @@ def rollback_checkpoints(self, rollback=1): "Unable to load checkpoint during rollback") rollback -= 1 - def view_config_changes(self, for_logging=False, num=None): - """Displays all saved checkpoints. - - All checkpoints are printed by - :meth:`certbot.interfaces.IDisplay.notification`. - - .. todo:: Decide on a policy for error handling, OSError IOError... - - :raises .errors.ReverterError: If invalid directory structure. - - """ - backups = os.listdir(self.config.backup_dir) - backups.sort(reverse=True) - if num: - backups = backups[:num] - if not backups: - logger.info("Certbot has not saved backups of your configuration") - - return - # Make sure there isn't anything unexpected in the backup folder - # There should only be timestamped (float) directories - try: - for bkup in backups: - float(bkup) - except ValueError: - raise errors.ReverterError( - "Invalid directories in {0}".format(self.config.backup_dir)) - - output = [] - for bkup in backups: - output.append(time.ctime(float(bkup))) - cur_dir = os.path.join(self.config.backup_dir, bkup) - with open(os.path.join(cur_dir, "CHANGES_SINCE")) as changes_fd: - output.append(changes_fd.read()) - - output.append("Affected files:") - with open(os.path.join(cur_dir, "FILEPATHS")) as paths_fd: - filepaths = paths_fd.read().splitlines() - for path in filepaths: - output.append(" {0}".format(path)) - - if os.path.isfile(os.path.join(cur_dir, "NEW_FILES")): - with open(os.path.join(cur_dir, "NEW_FILES")) as new_fd: - output.append("New Configuration Files:") - filepaths = new_fd.read().splitlines() - for path in filepaths: - output.append(" {0}".format(path)) - - output.append(os.linesep) - - if for_logging: - return os.linesep.join(output) - zope.component.getUtility(interfaces.IDisplay).notification( - os.linesep.join(output), force_interactive=True, pause=False) - - def add_to_temp_checkpoint(self, save_files, save_notes): + def add_to_temp_checkpoint(self, save_files: set[str], save_notes: str) -> None: """Add files to temporary checkpoint. :param set save_files: set of filepaths to save @@ -196,7 +138,7 @@ def add_to_temp_checkpoint(self, save_files, save_notes): self._add_to_checkpoint_dir( self.config.temp_checkpoint_dir, save_files, save_notes) - def add_to_checkpoint(self, save_files, save_notes): + def add_to_checkpoint(self, save_files: set[str], save_notes: str) -> None: """Add files to a permanent checkpoint. :param set save_files: set of filepaths to save @@ -208,7 +150,7 @@ def add_to_checkpoint(self, save_files, save_notes): self._add_to_checkpoint_dir( self.config.in_progress_dir, save_files, save_notes) - def _add_to_checkpoint_dir(self, cp_dir, save_files, save_notes): + def _add_to_checkpoint_dir(self, cp_dir: str, save_files: set[str], save_notes: str) -> None: """Add save files to checkpoint directory. :param str cp_dir: Checkpoint directory filepath @@ -220,8 +162,7 @@ def _add_to_checkpoint_dir(self, cp_dir, save_files, save_notes): """ util.make_or_verify_dir( - cp_dir, constants.CONFIG_DIRS_MODE, compat.os_geteuid(), - self.config.strict_permissions) + cp_dir, constants.CONFIG_DIRS_MODE, self.config.strict_permissions) op_fd, existing_filepaths = self._read_and_append( os.path.join(cp_dir, "FILEPATHS")) @@ -238,9 +179,9 @@ def _add_to_checkpoint_dir(self, cp_dir, save_files, save_notes): try: shutil.copy2(filename, os.path.join( cp_dir, os.path.basename(filename) + "_" + str(idx))) - op_fd.write(filename + os.linesep) - # http://stackoverflow.com/questions/4726260/effective-use-of-python-shutil-copy2 - except IOError: + op_fd.write('{0}\n'.format(filename)) + # https://stackoverflow.com/questions/4726260/effective-use-of-python-shutil-copy2 + except OSError: op_fd.close() logger.error( "Unable to add file %s to checkpoint %s", @@ -254,12 +195,13 @@ def _add_to_checkpoint_dir(self, cp_dir, save_files, save_notes): with open(os.path.join(cp_dir, "CHANGES_SINCE"), "a") as notes_fd: notes_fd.write(save_notes) - def _read_and_append(self, filepath): # pylint: disable=no-self-use + def _read_and_append(self, filepath: str) -> tuple[TextIO, list[str]]: """Reads the file lines and returns a file obj. Read the file returning the lines, and a pointer to the end of the file. """ + # pylint: disable=consider-using-with # Open up filepath differently depending on if it already exists if os.path.isfile(filepath): op_fd = open(filepath, "r+") @@ -270,7 +212,7 @@ def _read_and_append(self, filepath): # pylint: disable=no-self-use return op_fd, lines - def _recover_checkpoint(self, cp_dir): + def _recover_checkpoint(self, cp_dir: str) -> None: """Recover a specific checkpoint. Recover a specific checkpoint provided by cp_dir @@ -293,11 +235,10 @@ def _recover_checkpoint(self, cp_dir): shutil.copy2(os.path.join( cp_dir, os.path.basename(path) + "_" + str(idx)), path) - except (IOError, OSError): + except OSError: # This file is required in all checkpoints. logger.error("Unable to recover files from %s", cp_dir) - raise errors.ReverterError( - "Unable to recover files from %s" % cp_dir) + raise errors.ReverterError(f"Unable to recover files from {cp_dir}") # Remove any newly added files if they exist self._remove_contained_files(os.path.join(cp_dir, "NEW_FILES")) @@ -309,11 +250,13 @@ def _recover_checkpoint(self, cp_dir): raise errors.ReverterError( "Unable to remove directory: %s" % cp_dir) - def _run_undo_commands(self, filepath): # pylint: disable=no-self-use + def _run_undo_commands(self, filepath: str) -> None: """Run all commands in a file.""" - # NOTE: csv module uses native strings. That is, bytes on Python 2 and - # unicode on Python 3 - with open(filepath, 'r') as csvfile: + # NOTE: csv module uses native strings. That is unicode on Python 3 + # It is strongly advised to set newline = '' on Python 3 with CSV, + # and it fixes problems on Windows. + kwargs = {'newline': ''} + with open(filepath, 'r', **kwargs) as csvfile: # type: ignore csvreader = csv.reader(csvfile) for command in reversed(list(csvreader)): try: @@ -322,7 +265,7 @@ def _run_undo_commands(self, filepath): # pylint: disable=no-self-use logger.error( "Unable to run undo command: %s", " ".join(command)) - def _check_tempfile_saves(self, save_files): + def _check_tempfile_saves(self, save_files: set[str]) -> None: """Verify save isn't overwriting any temporary files. :param set save_files: Set of files about to be saved. @@ -348,11 +291,9 @@ def _check_tempfile_saves(self, save_files): # Verify no save_file is in protected_files for filename in protected_files: if filename in save_files: - raise errors.ReverterError( - "Attempting to overwrite challenge " - "file - %s" % filename) + raise errors.ReverterError(f"Attempting to overwrite challenge file - {filename}") - def register_file_creation(self, temporary, *files): + def register_file_creation(self, temporary: bool, *files: str) -> None: r"""Register the creation of all files during certbot execution. Call this method before writing to the file to make sure that the @@ -382,8 +323,8 @@ def register_file_creation(self, temporary, *files): for path in files: if path not in ex_files: - new_fd.write("{0}{1}".format(path, os.linesep)) - except (IOError, OSError): + new_fd.write("{0}\n".format(path)) + except OSError: logger.error("Unable to register file creation(s) - %s", files) raise errors.ReverterError( "Unable to register file creation(s) - {0}".format(files)) @@ -391,7 +332,7 @@ def register_file_creation(self, temporary, *files): if new_fd is not None: new_fd.close() - def register_undo_command(self, temporary, command): + def register_undo_command(self, temporary: bool, command: Iterable[str]) -> None: """Register a command to be run to undo actions taken. .. warning:: This function does not enforce order of operations in terms @@ -408,25 +349,20 @@ def register_undo_command(self, temporary, command): """ commands_fp = os.path.join(self._get_cp_dir(temporary), "COMMANDS") - command_file = None + # It is strongly advised to set newline = '' on Python 3 with CSV, + # and it fixes problems on Windows. + kwargs = {'newline': ''} try: - if os.path.isfile(commands_fp): - command_file = open(commands_fp, "a") - else: - command_file = open(commands_fp, "w") - - csvwriter = csv.writer(command_file) - csvwriter.writerow(command) - - except (IOError, OSError): + mode = "a" if os.path.isfile(commands_fp) else "w" + with open(commands_fp, mode, **kwargs) as f: # type: ignore + csvwriter = csv.writer(f) + csvwriter.writerow(command) + except OSError: logger.error("Unable to register undo command") raise errors.ReverterError( "Unable to register undo command.") - finally: - if command_file is not None: - command_file.close() - def _get_cp_dir(self, temporary): + def _get_cp_dir(self, temporary: bool) -> str: """Return the proper reverter directory.""" if temporary: cp_dir = self.config.temp_checkpoint_dir @@ -434,12 +370,11 @@ def _get_cp_dir(self, temporary): cp_dir = self.config.in_progress_dir util.make_or_verify_dir( - cp_dir, constants.CONFIG_DIRS_MODE, compat.os_geteuid(), - self.config.strict_permissions) + cp_dir, constants.CONFIG_DIRS_MODE, self.config.strict_permissions) return cp_dir - def recovery_routine(self): + def recovery_routine(self) -> None: """Revert configuration to most recent finalized checkpoint. Remove all changes (temporary and permanent) that have not been @@ -449,7 +384,7 @@ def recovery_routine(self): :raises .errors.ReverterError: If unable to recover the configuration """ - # First, any changes found in IConfig.temp_checkpoint_dir are removed, + # First, any changes found in NamespaceConfig.temp_checkpoint_dir are removed, # then IN_PROGRESS changes are removed The order is important. # IN_PROGRESS is unable to add files that are already added by a TEMP # change. Thus TEMP must be rolled back first because that will be the @@ -467,7 +402,7 @@ def recovery_routine(self): "Incomplete or failed recovery for IN_PROGRESS checkpoint " "- %s" % self.config.in_progress_dir) - def _remove_contained_files(self, file_list): # pylint: disable=no-self-use + def _remove_contained_files(self, file_list: str) -> bool: """Erase all files contained within file_list. :param str file_list: file containing list of file paths to be deleted @@ -493,10 +428,10 @@ def _remove_contained_files(self, file_list): # pylint: disable=no-self-use os.remove(path) else: logger.warning( - "File: %s - Could not be found to be deleted %s - " - "Certbot probably shut down unexpectedly", - os.linesep, path) - except (IOError, OSError): + "File: %s - Could not be found to be deleted\n" + " - Certbot probably shut down unexpectedly", + path) + except OSError: logger.critical( "Unable to remove filepaths contained within %s", file_list) raise errors.ReverterError( @@ -505,7 +440,7 @@ def _remove_contained_files(self, file_list): # pylint: disable=no-self-use return True - def finalize_checkpoint(self, title): + def finalize_checkpoint(self, title: str) -> None: """Finalize the checkpoint. Timestamps and permanently saves all changes made through the use @@ -538,7 +473,7 @@ def finalize_checkpoint(self, title): # Move self.config.in_progress_dir to Backups directory shutil.move(changes_since_tmp_path, changes_since_path) - except (IOError, OSError): + except OSError: logger.error("Unable to finalize checkpoint - adding title") logger.debug("Exception was:\n%s", traceback.format_exc()) raise errors.ReverterError("Unable to add title") @@ -546,7 +481,7 @@ def finalize_checkpoint(self, title): # rename the directory as a timestamp self._timestamp_progress_dir() - def _checkpoint_timestamp(self): + def _checkpoint_timestamp(self) -> str: "Determine the timestamp of the checkpoint, enforcing monotonicity." timestamp = str(time.time()) others = glob.glob(os.path.join(self.config.backup_dir, "[0-9]*")) @@ -567,19 +502,19 @@ def _checkpoint_timestamp(self): timestamp = timetravel return timestamp - def _timestamp_progress_dir(self): + def _timestamp_progress_dir(self) -> None: """Timestamp the checkpoint.""" # It is possible save checkpoints faster than 1 per second resulting in # collisions in the naming convention. - for _ in six.moves.range(2): + for _ in range(2): timestamp = self._checkpoint_timestamp() final_dir = os.path.join(self.config.backup_dir, timestamp) try: - compat.os_rename(self.config.in_progress_dir, final_dir) + filesystem.replace(self.config.in_progress_dir, final_dir) return except OSError: - logger.warning("Extreme, unexpected race condition, retrying (%s)", timestamp) + logger.warning("Unexpected race condition, retrying (%s)", timestamp) # After 10 attempts... something is probably wrong here... logger.error( diff --git a/certbot/ssl-dhparams.pem b/certbot/src/certbot/ssl-dhparams.pem similarity index 100% rename from certbot/ssl-dhparams.pem rename to certbot/src/certbot/ssl-dhparams.pem diff --git a/certbot/src/certbot/tests/__init__.py b/certbot/src/certbot/tests/__init__.py new file mode 100644 index 00000000000..82290ca0b1b --- /dev/null +++ b/certbot/src/certbot/tests/__init__.py @@ -0,0 +1 @@ +"""Utilities for running Certbot tests""" diff --git a/certbot/src/certbot/tests/acme_util.py b/certbot/src/certbot/tests/acme_util.py new file mode 100644 index 00000000000..b27d9f8fe35 --- /dev/null +++ b/certbot/src/certbot/tests/acme_util.py @@ -0,0 +1,90 @@ +"""ACME utilities for testing.""" +import datetime +from typing import Any +from typing import Iterable + +import josepy as jose + +from acme import challenges +from acme import messages +from certbot._internal import auth_handler +from certbot.tests import util + +JWK = jose.JWK.load(util.load_vector('rsa512_key.pem')) +KEY = util.load_jose_rsa_private_key_pem('rsa512_key.pem') + +# Challenges +HTTP01 = challenges.HTTP01( + token=b"evaGxfADs6pSRb2LAv9IZf17Dt3juxGJ+PCt92wr+oA") +DNS01 = challenges.DNS01(token=b"17817c66b60ce2e4012dfad92657527a") +DNS01_2 = challenges.DNS01(token=b"cafecafecafecafecafecafe0feedbac") + +CHALLENGES = [HTTP01, DNS01] + + +def chall_to_challb(chall: challenges.Challenge, status: messages.Status) -> messages.ChallengeBody: + """Return ChallengeBody from Challenge.""" + kwargs = { + "chall": chall, + "uri": chall.typ + "_uri", + "status": status, + } + + if status == messages.STATUS_VALID: + kwargs.update({"validated": datetime.datetime.now()}) + + return messages.ChallengeBody(**kwargs) + + +# Pending ChallengeBody objects +HTTP01_P = chall_to_challb(HTTP01, messages.STATUS_PENDING) +DNS01_P = chall_to_challb(DNS01, messages.STATUS_PENDING) +DNS01_P_2 = chall_to_challb(DNS01_2, messages.STATUS_PENDING) + +CHALLENGES_P = [HTTP01_P, DNS01_P] + + +# AnnotatedChallenge objects +HTTP01_A = auth_handler.challb_to_achall(HTTP01_P, JWK, messages.Identifier( + typ=messages.IDENTIFIER_FQDN, value="example.com")) +DNS01_A = auth_handler.challb_to_achall(DNS01_P, JWK, messages.Identifier( + typ=messages.IDENTIFIER_FQDN, value="example.org")) +DNS01_A_2 = auth_handler.challb_to_achall(DNS01_P_2, JWK, messages.Identifier( + typ=messages.IDENTIFIER_FQDN, value="esimerkki.example.org")) + +ACHALLENGES = [HTTP01_A, DNS01_A] + + +def gen_authzr(authz_status: messages.Status, domain: str, challs: Iterable[challenges.Challenge], + statuses: Iterable[messages.Status]) -> messages.AuthorizationResource: + """Generate an authorization resource. + + :param authz_status: Status object + :type authz_status: :class:`acme.messages.Status` + :param list challs: Challenge objects + :param list statuses: status of each challenge object + + """ + challbs = tuple( + chall_to_challb(chall, status) + for chall, status in zip(challs, statuses) + ) + authz_kwargs: dict[str, Any] = { + "identifier": messages.Identifier( + typ=messages.IDENTIFIER_FQDN, value=domain), + "challenges": challbs, + } + if authz_status == messages.STATUS_VALID: + authz_kwargs.update({ + "status": authz_status, + "expires": datetime.datetime.now() + datetime.timedelta(days=31), + }) + else: + authz_kwargs.update({ + "status": authz_status, + }) + + return messages.AuthorizationResource( + uri="https://trusted.ca/new-authz-resource", + body=messages.Authorization(**authz_kwargs) + ) diff --git a/certbot/src/certbot/tests/testdata/README b/certbot/src/certbot/tests/testdata/README new file mode 100644 index 00000000000..acdb41e85e3 --- /dev/null +++ b/certbot/src/certbot/tests/testdata/README @@ -0,0 +1,38 @@ +The following command has been used to generate test keys: + + for x in 512 2048; do openssl genrsa -out rsa${k}_key.pem $k; done + +For the elliptic curve private keys, this command was used: + + for k in "prime256v1" "secp384r1" "secp521r1" do + openssl genpkey -algorithm ${k} -out ec_${k}_key.pem + done + +and for the CSR PEM (Certificate Signing Request): + + openssl req -new -out csr-Xsans_X.pem -key rsa512_key.pem [-config csr-Xsans_X.conf | -subj '/CN=example.com'] [-outform DER > csr_X.der] + +and for the certificate: + + openssl req -new -out cert_X.pem -key rsaX_key.pem -subj '/CN=example.com' -x509 [-outform DER > cert_X.der] + +`csr-mixed.pem` was generated with pyca/cryptography using the following snippet: + + from cryptography import x509 + from cryptography.hazmat.primitives import hashes, serialization + k = serialization.load_pem_private_key( + open("./acme/acme/_internal/tests/testdata/rsa2048_key.pem", "rb").read(), None + ) + csr = ( + x509.CertificateSigningRequestBuilder().add_extension( + x509.SubjectAlternativeName([x509.DNSName('a.example.com'), x509.IPAddress(ipaddress.ipaddr('192.0.2.111'))]), + critical=False + ).subject_name( + x509.Name([]) + ).sign( + k, hashes.SHA256() + ) + ) + open("./acme/acme/_internal/tests/testdata/csr-mixed.pem", "wb").write( + csr.public_bytes(serialization.Encoding.PEM) + ) diff --git a/certbot/tests/testdata/cert-5sans_512.pem b/certbot/src/certbot/tests/testdata/cert-5sans_512.pem similarity index 100% rename from certbot/tests/testdata/cert-5sans_512.pem rename to certbot/src/certbot/tests/testdata/cert-5sans_512.pem diff --git a/certbot/tests/testdata/cert-nosans_nistp256.pem b/certbot/src/certbot/tests/testdata/cert-nosans_nistp256.pem similarity index 100% rename from certbot/tests/testdata/cert-nosans_nistp256.pem rename to certbot/src/certbot/tests/testdata/cert-nosans_nistp256.pem diff --git a/certbot/tests/testdata/cert-san_512.pem b/certbot/src/certbot/tests/testdata/cert-san_512.pem similarity index 100% rename from certbot/tests/testdata/cert-san_512.pem rename to certbot/src/certbot/tests/testdata/cert-san_512.pem diff --git a/certbot/tests/testdata/cert_2048.pem b/certbot/src/certbot/tests/testdata/cert_2048.pem similarity index 100% rename from certbot/tests/testdata/cert_2048.pem rename to certbot/src/certbot/tests/testdata/cert_2048.pem diff --git a/certbot/tests/testdata/cert_512.pem b/certbot/src/certbot/tests/testdata/cert_512.pem similarity index 100% rename from certbot/tests/testdata/cert_512.pem rename to certbot/src/certbot/tests/testdata/cert_512.pem diff --git a/certbot/tests/testdata/cert_512_bad.pem b/certbot/src/certbot/tests/testdata/cert_512_bad.pem similarity index 100% rename from certbot/tests/testdata/cert_512_bad.pem rename to certbot/src/certbot/tests/testdata/cert_512_bad.pem diff --git a/certbot/tests/testdata/cert_fullchain_2048.pem b/certbot/src/certbot/tests/testdata/cert_fullchain_2048.pem similarity index 100% rename from certbot/tests/testdata/cert_fullchain_2048.pem rename to certbot/src/certbot/tests/testdata/cert_fullchain_2048.pem diff --git a/certbot/src/certbot/tests/testdata/cert_intermediate_1.pem b/certbot/src/certbot/tests/testdata/cert_intermediate_1.pem new file mode 100644 index 00000000000..7c8f8cbe107 --- /dev/null +++ b/certbot/src/certbot/tests/testdata/cert_intermediate_1.pem @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDUDCCAjigAwIBAgIIYbnTKswm95swDQYJKoZIhvcNAQELBQAwIDEeMBwGA1UE +AxMVUGViYmxlIFJvb3QgQ0EgNzcwNjgzMCAXDTIwMDYxOTIxMDY1NVoYDzIwNTAw +NjE5MjAwNjU1WjAoMSYwJAYDVQQDEx1QZWJibGUgSW50ZXJtZWRpYXRlIENBIDI1 +OWJjMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALFeijP7HyxcUMrk +CuKgEqyaRmgjGj3JvPnm7RPJl0j6rgWe767eweP2dMJ8LnYHWZVDwmkysq1rb/kS +HuWoqUW1gpqwo4+ARMAb0fhx79Ze7eNp5M44PRN9SenK3TZQYrNFzC5t0NzNQaq8 +ksv0R4kxi2VqfPuV6GCEYHI2t5z7249U+4PHEPSLEjq0TvFYp8aLVJ2cRLsspdXd +ANQwbKCWz00j03CcK79LXR6ncNw1WqEoYxjzmagiAKBf9kJVawxeJF91A+kPH052 +hzGxhmMOU/VeWcl+zGeRR2cybyMF1qZ/tDX0n+J4q7FrcOjzJeZH8uxwOZ4N7Xn2 +lqavdTkCAwEAAaOBgzCBgDAOBgNVHQ8BAf8EBAMCAoQwHQYDVR0lBBYwFAYIKwYB +BQUHAwEGCCsGAQUFBwMCMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFMCE8MSR +KqrGnv0Vd4iv8FCVtVmhMB8GA1UdIwQYMBaAFBg/Nkzipd7/vMs/f9VV11uPo2bS +MA0GCSqGSIb3DQEBCwUAA4IBAQBISrMR9Fchj2u1FhxAr7eQsdM1Lus3B/eEJmcR +KDO/tNzns5zsrcJL42wzDVQA61X+aVzZBSfb1oMbwHCpWBvj88avL/mJ1OgrC+VZ +v7IDdKOwvXmFf0VVE5LLCPY+gfR65LVRkb0W9ZrZ9Oj0ke9A9ryPSNTYBvxE+Qar +V5Jqe0YdG7IQBd4xFFnpWZyEPnQBW2Sl5oudt6LgJaQosMJdRLp4lL805G/lKPEt +CKjWoaP6J4V1Ty+zwuWVdIpSUuIoWkZ7BeIIqVkTg7ieNj4eN2zddDxbTIquFD2K +7rZ1691krTWTe8bkLx6wGaAfvLOk97Ywmdsot3nTVRbaVTXq +-----END CERTIFICATE----- diff --git a/certbot/src/certbot/tests/testdata/cert_intermediate_2.pem b/certbot/src/certbot/tests/testdata/cert_intermediate_2.pem new file mode 100644 index 00000000000..ec94c566ceb --- /dev/null +++ b/certbot/src/certbot/tests/testdata/cert_intermediate_2.pem @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDUDCCAjigAwIBAgIILrJTDiWZDFkwDQYJKoZIhvcNAQELBQAwIDEeMBwGA1UE +AxMVUGViYmxlIFJvb3QgQ0EgMGNjNmYwMCAXDTIwMDYxOTIxMDY1NFoYDzIwNTAw +NjE5MjAwNjU0WjAoMSYwJAYDVQQDEx1QZWJibGUgSW50ZXJtZWRpYXRlIENBIDI1 +OWJjMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALFeijP7HyxcUMrk +CuKgEqyaRmgjGj3JvPnm7RPJl0j6rgWe767eweP2dMJ8LnYHWZVDwmkysq1rb/kS +HuWoqUW1gpqwo4+ARMAb0fhx79Ze7eNp5M44PRN9SenK3TZQYrNFzC5t0NzNQaq8 +ksv0R4kxi2VqfPuV6GCEYHI2t5z7249U+4PHEPSLEjq0TvFYp8aLVJ2cRLsspdXd +ANQwbKCWz00j03CcK79LXR6ncNw1WqEoYxjzmagiAKBf9kJVawxeJF91A+kPH052 +hzGxhmMOU/VeWcl+zGeRR2cybyMF1qZ/tDX0n+J4q7FrcOjzJeZH8uxwOZ4N7Xn2 +lqavdTkCAwEAAaOBgzCBgDAOBgNVHQ8BAf8EBAMCAoQwHQYDVR0lBBYwFAYIKwYB +BQUHAwEGCCsGAQUFBwMCMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFMCE8MSR +KqrGnv0Vd4iv8FCVtVmhMB8GA1UdIwQYMBaAFE4E9PDqHbaTzFi8XEeTc/s/xEal +MA0GCSqGSIb3DQEBCwUAA4IBAQCIEnLk9ZgIzwjew3ktKmZ/lDKl00EcIJ/or/GE +IM3exvEzUYJotoCKdw/6d1mjfsJ1AkJUrY5WASEhXgQ9wP5/z08xwmCSVowSnHhg +iKeQf6iNIqpxg7LGqOeSc32QXzeBMTsvQcqupZ7J6ptomiPopeAfYIMQWni9Ym/I +P1ZjydR2petIFioPuFtByu1UhvIcF4LCJ7UWwhVNcrivf5VGaRUVac4IxGZ4Urw+ +W1xgT8pt5Rk1yCoqXsenllXRxdakbDDG9F0ZBWQNmvArYZJt1mdq1dBHT/JO/wKI +aGF5PXa6k3YwhyvK4zTDcwPKmOBikL2FgzlohLYfETpTznW0 +-----END CERTIFICATE----- diff --git a/certbot/src/certbot/tests/testdata/cert_leaf.pem b/certbot/src/certbot/tests/testdata/cert_leaf.pem new file mode 100644 index 00000000000..1ef6163ef8b --- /dev/null +++ b/certbot/src/certbot/tests/testdata/cert_leaf.pem @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDWTCCAkGgAwIBAgIIYWk/LgSO+zowDQYJKoZIhvcNAQELBQAwKDEmMCQGA1UE +AxMdUGViYmxlIEludGVybWVkaWF0ZSBDQSAyNTliYzEwHhcNMjAwNjIxMDEyMTM3 +WhcNMjUwNjIxMDEyMTM3WjAWMRQwEgYDVQQDEwtleGFtcGxlLmNvbTCCASIwDQYJ +KoZIhvcNAQEBBQADggEPADCCAQoCggEBAOABmyVITyczj6/R/1EWH6FGEfzJgOFi +zUC1HweYouEnC3Pu4SV/qFyKC8FsVQAY5FYWV4rrexnMgAeCL7XBTFyvIYQ7FoCH +5SeyUr3T1j/BAaFZtdb419d+BXLBOYigIu36pyRrH/3z0oxvv0brG7zXty/AOHTK +/k5aE7e3iuUSPJqNoGIQi4NwtNw5Fwcpe4lOHRxNNp9kfjxKYzLgj9xaT6cw6r2T +9QJ7wnU9C0/cs9h98xz+IhApyoVWlF4SE87dpG99NQ8M92od5xv1jSIF34AMQzww +fAW3FIKr4onaMP6XHtiXnJFn4pSw+BryxPqsKz864Ritvokz/nJoylUCAwEAAaOB +mDCBlTAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUF +BwMCMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFKG2yAaJtpPVStXakLLD5Jc7fcaN +MB8GA1UdIwQYMBaAFMCE8MSRKqrGnv0Vd4iv8FCVtVmhMBYGA1UdEQQPMA2CC2V4 +YW1wbGUuY29tMA0GCSqGSIb3DQEBCwUAA4IBAQBde/j8lgRqzksphFVwfx310TaD +4foS+15cq0OnTM/iVt1JtjyyGaRamgCcFXkm31IP8c9vnaReYGY/QawshK1NSLii +W6mN/RqguQNoovp0xw3SkDb+lyDo0VuIyMrbwlbhP4P5uue+Shb1ZTy7QmfUeYuK +OTjFI3j0oBiSKins3ryEFHIMvuXcaQ4F2uw6UOHBgwv5XbVV5B9l62jGbZnxrpc7 +SB9mzU8I5CJoK1uetwuADwlhDX8ITQ778unR7yIZOojyIMKWd9z9KtKDVSPK9I+d +u0oYF4xG97nTks6Cqt2OUJizSY3ojgwK4U8w1795FCVUlq5Z58gpWf1BhA8a +-----END CERTIFICATE----- diff --git a/certbot/tests/testdata/cli.ini b/certbot/src/certbot/tests/testdata/cli.ini similarity index 100% rename from certbot/tests/testdata/cli.ini rename to certbot/src/certbot/tests/testdata/cli.ini diff --git a/certbot/tests/testdata/csr-6sans_512.conf b/certbot/src/certbot/tests/testdata/csr-6sans_512.conf similarity index 100% rename from certbot/tests/testdata/csr-6sans_512.conf rename to certbot/src/certbot/tests/testdata/csr-6sans_512.conf diff --git a/certbot/tests/testdata/csr-6sans_512.pem b/certbot/src/certbot/tests/testdata/csr-6sans_512.pem similarity index 100% rename from certbot/tests/testdata/csr-6sans_512.pem rename to certbot/src/certbot/tests/testdata/csr-6sans_512.pem diff --git a/certbot/tests/testdata/csr-nonames_512.pem b/certbot/src/certbot/tests/testdata/csr-nonames_512.pem similarity index 100% rename from certbot/tests/testdata/csr-nonames_512.pem rename to certbot/src/certbot/tests/testdata/csr-nonames_512.pem diff --git a/certbot/tests/testdata/csr-nosans_512.conf b/certbot/src/certbot/tests/testdata/csr-nosans_512.conf similarity index 100% rename from certbot/tests/testdata/csr-nosans_512.conf rename to certbot/src/certbot/tests/testdata/csr-nosans_512.conf diff --git a/certbot/tests/testdata/csr-nosans_512.pem b/certbot/src/certbot/tests/testdata/csr-nosans_512.pem similarity index 100% rename from certbot/tests/testdata/csr-nosans_512.pem rename to certbot/src/certbot/tests/testdata/csr-nosans_512.pem diff --git a/certbot/tests/testdata/csr-nosans_nistp256.pem b/certbot/src/certbot/tests/testdata/csr-nosans_nistp256.pem similarity index 100% rename from certbot/tests/testdata/csr-nosans_nistp256.pem rename to certbot/src/certbot/tests/testdata/csr-nosans_nistp256.pem diff --git a/certbot/tests/testdata/csr-san_512.pem b/certbot/src/certbot/tests/testdata/csr-san_512.pem similarity index 100% rename from certbot/tests/testdata/csr-san_512.pem rename to certbot/src/certbot/tests/testdata/csr-san_512.pem diff --git a/certbot/tests/testdata/csr_512.der b/certbot/src/certbot/tests/testdata/csr_512.der similarity index 100% rename from certbot/tests/testdata/csr_512.der rename to certbot/src/certbot/tests/testdata/csr_512.der diff --git a/certbot/tests/testdata/csr_512.pem b/certbot/src/certbot/tests/testdata/csr_512.pem similarity index 100% rename from certbot/tests/testdata/csr_512.pem rename to certbot/src/certbot/tests/testdata/csr_512.pem diff --git a/certbot/src/certbot/tests/testdata/ec_prime256v1_key.pem b/certbot/src/certbot/tests/testdata/ec_prime256v1_key.pem new file mode 100644 index 00000000000..77552a6569d --- /dev/null +++ b/certbot/src/certbot/tests/testdata/ec_prime256v1_key.pem @@ -0,0 +1,8 @@ +-----BEGIN EC PARAMETERS----- +BggqhkjOPQMBBw== +-----END EC PARAMETERS----- +-----BEGIN EC PRIVATE KEY----- +MHcCAQEEIDqQPQl69kuh+DrecC8SFPt21f0F/HHDP3T4/Lf0zIVFoAoGCCqGSM49 +AwEHoUQDQgAEHou50Ee9u+8Vial6VbUHExlzsiCHtORlW0X0pKo5RspIKB0QyKwo +dUXvBbv95I9yCO5+MlGkKjwLHtIEze0Hww== +-----END EC PRIVATE KEY----- diff --git a/certbot/src/certbot/tests/testdata/ec_secp384r1_key.pem b/certbot/src/certbot/tests/testdata/ec_secp384r1_key.pem new file mode 100644 index 00000000000..19a846a46fb --- /dev/null +++ b/certbot/src/certbot/tests/testdata/ec_secp384r1_key.pem @@ -0,0 +1,9 @@ +-----BEGIN EC PARAMETERS----- +BgUrgQQAIg== +-----END EC PARAMETERS----- +-----BEGIN EC PRIVATE KEY----- +MIGkAgEBBDAgvZGw5C7Mp26N0cXA+vIg5K/J5MJw+MVGYfGF4ZutuCLeYMrWT68R +A0h6hJvDtMSgBwYFK4EEACKhZANiAAR1uQYZeU5Kml5o53Q8/PCdwUbqdgCSkV0C +J5a6bhDRMp20fdp2T/mbkdxuVEl81lqfKPZhsd4CZsLaVIU3RUoGgIT1R3QKawpJ +SuXq37yWFX2hqlgt+lsBufZ8RD5QnZc= +-----END EC PRIVATE KEY----- diff --git a/certbot/src/certbot/tests/testdata/ec_secp521r1_key.pem b/certbot/src/certbot/tests/testdata/ec_secp521r1_key.pem new file mode 100644 index 00000000000..78bb9a0ea30 --- /dev/null +++ b/certbot/src/certbot/tests/testdata/ec_secp521r1_key.pem @@ -0,0 +1,10 @@ +-----BEGIN EC PARAMETERS----- +BgUrgQQAIw== +-----END EC PARAMETERS----- +-----BEGIN EC PRIVATE KEY----- +MIHcAgEBBEIACWWVKm1qAIejZ6qmqk9D69wQW5FAe3Er0IxWAMkonTEhu8EH5Q2i +2vT2bESm730zhGTe2Pn11b85H6UI9hxhCHygBwYFK4EEACOhgYkDgYYABAEQi1WF +m3suHjPyWACyOJYGUn1Kx6rfBo0PjC7X2TU9jr8umLkIpaaF5UsBuMBmdz1IHL0U +k0gQtoOQ0Qu8N74GuAGzGR0S3RYIv6gfYVz3dS1K4n4b307Lx62bnvtlNxcIvt3w +hmS5OdvQ1Kdxh6oqbSVhhbQmJcgab78Txx3R2QeCxw== +-----END EC PRIVATE KEY----- diff --git a/certbot/tests/testdata/nistp256_key.pem b/certbot/src/certbot/tests/testdata/nistp256_key.pem similarity index 100% rename from certbot/tests/testdata/nistp256_key.pem rename to certbot/src/certbot/tests/testdata/nistp256_key.pem diff --git a/certbot/src/certbot/tests/testdata/ocsp_certificate.pem b/certbot/src/certbot/tests/testdata/ocsp_certificate.pem new file mode 100644 index 00000000000..471844859c6 --- /dev/null +++ b/certbot/src/certbot/tests/testdata/ocsp_certificate.pem @@ -0,0 +1,37 @@ +-----BEGIN CERTIFICATE----- +MIIGYDCCBEigAwIBAgIKcjrC4hZcebbtODANBgkqhkiG9w0BAQsFADBRMQswCQYD +VQQGEwJOTzEdMBsGA1UECgwUQnV5cGFzcyBBUy05ODMxNjMzMjcxIzAhBgNVBAMM +GkJ1eXBhc3MgQ2xhc3MgMiBUZXN0NCBDQSA1MB4XDTE5MDUxMjE1NTgyMVoXDTE5 +MTEwODIyNTkwMFowADCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK9P +b+YhJPypm4ui+AZUHPrJ6IsB9R/6Wvgec2G/GuW/UNQFktIhU10HOHAbiJeYLqNZ +1Cia8JD6NXXGbprOjIbZWvjulYTaLSlClcK0H7HZrcgrK60OeIGEtur27ga68RML +hs1FG7TNyWVysifOtwW9Oo1mZQQtxViiE2Yb+Q4QqIxitnbrnFmKrVJSUHVXi8/I +BK1yLrJiRBZMIw0wvAWcWEG2Gpp9PAbemlb11Zx8sm/RSGh7u60rmETbB2Pu941s +XJCSQRtq5yKdtjIJTIgbe12SPkknqTqa3aUh7hgho0IymlDSeeocL60SUiUAsPEr +QRWleodOR1ChXz5mFokCAwEAAaOCAokwggKFMAkGA1UdEwQCMAAwHwYDVR0jBBgw +FoAUd9nQBpFm2N0ZJo1JrNowL2p7YrEwHQYDVR0OBBYEFExS23I6sLCeO6KIxzoc +tr9s+HmiMA4GA1UdDwEB/wQEAwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYB +BQUHAwIwIAYDVR0gBBkwFzALBglghEIBGgEAAgcwCAYGZ4EMAQIBMEIGA1UdHwQ7 +MDkwN6A1oDOGMWh0dHA6Ly9jcmwudGVzdDQuYnV5cGFzcy5uby9jcmwvQlBDbGFz +czJUNENBNS5jcmwwIQYDVR0RAQH/BBcwFYITYnV5cGFzcy5wYWNhbGlzLm5ldDB4 +BggrBgEFBQcBAQRsMGowKQYIKwYBBQUHMAGGHWh0dHA6Ly9vY3NwLnRlc3Q0LmJ1 +eXBhc3MuY29tMD0GCCsGAQUFBzAChjFodHRwOi8vY3J0LnRlc3Q0LmJ1eXBhc3Mu +bm8vY3J0L0JQQ2xhc3MyVDRDQTUuY2VyMIIBBAYKKwYBBAHWeQIEAgSB9QSB8gDw +AHYAsMyD5aX5fWuvfAnMKEkEhyrH6IsTLGNQt8b9JuFsbHcAAAFqrMQ/cQAABAMA +RzBFAiEA1oWB4c6q7+tqGA4HhLNACOemr9c2aIUuWxeQE7/PlSYCIEolZ7pWVs1J +VyQW/AqeuXGB7qScwUgLh9C1uOJoeRe6AHYAsMyD5aX5fWuvfAnMKEkEhyrH6IsT +LGNQt8b9JuFsbHcAAAFqrMQ/cQAABAMARzBFAiAoLaNvIwMDifsDAXJBsAKHlYx7 +QPLXL8onYKm8f+Sf1wIhAMepo2GX84UR7WtooqzkBZLG+PaBy1zMuUAG6mwnroF9 +MA0GCSqGSIb3DQEBCwUAA4ICAQAPWLdjNS5lLL5SEtghYebtDmNj2968NYSDvb1L +1/uFwg3LCVRR1Xb3z1Hc/sc1W0IFXU0zOqEQiuP8jkVP7UqkaWuK5Eu0eP0zPI83 +WBZM0+eBwxwzIMK/Q7fYKTu1+vg/FlH0WhtV43DQSik66366zvPi2Tfag9IPvRei +DOjbSOBF0o4er2oCrtI0lK5YrHOdWtD7xwQIuA606P9ucuufMf+JcmduRJsVZ2Zu +3K32SMDdAnyjvQWZNbt1ex3G8vuFQEi690UBhPcha/SO8QvLS89wcaLJnyMIWdv7 +54cbw+fa1nLKM7qph6Mk1yb0qpomPqLmKw4T6WX36c0vDlFSpexJLGgWDFqLUxPN +qV7cJz4mi1qaYfdWXRrnyU4bl55pHTTgEzbohV7apsmytkCe1uFNrpcTh8jzAhGN +PQqarX9UoESR56B/ufbBGlBWi0pkV49BFks6Ue0GVKo7djoxuV6+SsmYSE+6MNPv +IUsm54TSnwxjA8WyG7pl14g1hkGFQ4NRYJMiVqK3DMABaPxVmT7NRxUQQiM0mmM7 +EKNzLBeWHJF5ecdDR1MiIF3ayn+RiZb0r8aSQBMLwN1YwUZw+hSYz1eCd7bHN1gC +1ksxP61f8LBz0SwDoyOTr8wY++wqF26KfoYuKQ3LjLeHvuUtL3EMnAhiyuej8ZOZ +22spng== +-----END CERTIFICATE----- diff --git a/certbot/src/certbot/tests/testdata/ocsp_issuer_certificate.pem b/certbot/src/certbot/tests/testdata/ocsp_issuer_certificate.pem new file mode 100644 index 00000000000..4f894ae4b24 --- /dev/null +++ b/certbot/src/certbot/tests/testdata/ocsp_issuer_certificate.pem @@ -0,0 +1,38 @@ +22spng== +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIGMzCCBBugAwIBAgIJMvsa+ZFQCj8nMA0GCSqGSIb3DQEBCwUAMFQxCzAJBgNV +BAYTAk5PMR0wGwYDVQQKDBRCdXlwYXNzIEFTLTk4MzE2MzMyNzEmMCQGA1UEAwwd +QnV5cGFzcyBDbGFzcyAyIFRlc3Q0IFJvb3QgQ0EwHhcNMTcwMjEzMTY1MjQ2WhcN +MjcwMjEzMTY1MjQ2WjBRMQswCQYDVQQGEwJOTzEdMBsGA1UECgwUQnV5cGFzcyBB +Uy05ODMxNjMzMjcxIzAhBgNVBAMMGkJ1eXBhc3MgQ2xhc3MgMiBUZXN0NCBDQSA1 +MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAi/vpgO2sbUQZsoxWd6us +QvT/59kvw5ehoJABBXFs1J1AV1/K2hjhDXit/sNGKjzDvkfE9PJqXMnhKpPFkUzC +z/NmDK++d6aRflnDvJrxlPVpp0QGbe3qOErByFjWiHoobuVItlpRO/BaBdlgGvmQ +LeZFBXs/ZrLNFUKBcE+DZIyJH7vy2EB5dNNVn2mx0n+371InpKsYUaHNlxPpp+uj +TOL+e4OjWTBwDaI7rVzpavozb8SPzFxjpxLLVH/j+8VPwoe3lmxr8ATyI178iRdA +uxYfaKURSfu7PWjnDNTnq26E3pwW3E5zUbsADgUMh/PzoJAcszL1eHGUQaAGBP85 +PlLmHr+nsPMHXOUyl7Ts6KGkZlvjnVshKwUxYAqjAC7/BY0iI0xc406NK9heeVDk +NiFA8/To6mQ09vO/TBxQtkfNk2yuxiixa101peSg4/+E4VhwYv6MJxS/oVqBd2d3 +wemYW/JUVeJg9wXGq1e/c09/UjGwUGwU9s5LNFEgj4v1tcvWnONzWNXkyMrs5g4e +U8L/DQ3XgNrcA9zrfFq0cQhSJonj/VI/jbBYyB2yEuQAIjAN6eDIOoLmHGIIvZtE +0LL5jaZC3W518jB1OF7QSvaFtaFl0VqDy6LMXL50elMVC+hr9KpDnN0t8gaSiPyZ +wEC9SMdQ7SLVOUK1Xdh3dh0CAwEAAaOCAQkwggEFMA8GA1UdEwEB/wQFMAMBAf8w +HwYDVR0jBBgwFoAU0aT+MaGsc75ZynH0up0oH+tVHh4wHQYDVR0OBBYEFHfZ0AaR +ZtjdGSaNSazaMC9qe2KxMA4GA1UdDwEB/wQEAwIBBjAgBgNVHSAEGTAXMAsGCWCE +QgEaAQACBzAIBgZngQwBAgEwRQYDVR0fBD4wPDA6oDigNoY0aHR0cDovL2NybC50 +ZXN0NC5idXlwYXNzLm5vL2NybC9CUENsYXNzMlQ0Um9vdENBLmNybDA5BggrBgEF +BQcBAQQtMCswKQYIKwYBBQUHMAGGHWh0dHA6Ly9vY3NwLnRlc3Q0LmJ1eXBhc3Mu +Y29tMA0GCSqGSIb3DQEBCwUAA4ICAQBOgxedV31NCpZQRc8yFxoqQNgBnY1UeH/h +/s/9fGQzyGnTWZldEi5MGJKF6ulcYnklitlg/jic9au3xSoqP/i2smUHByX2wMrC +mDpLCwio2x2p/0Wscj5asqzJE2cCWqob2iHxo36nsr3Jdd2GIlzhZ0wm8rMZxsQG +FgbgHYIer79S+PIdHoZuUnCJhsJ+1PRUmm2t7vcmZpu8l4CeL0XJX98l2L8kbBds +MGo1EazGAEirZnSfQKCARhUcEdavsKl067+irsGGcK4+L78Vl9S1/QPfKG30L5fv +nM1X1qAdhsbjwVdrhLkjpzabT0icsW6W17HLh8UBYdA7k4GclA6h+mNrXAt7JAeZ +PzMFq0I7vVJNEdolZHTVCqT0sdJiTj+phS1ztK86Wb1R/5d5B1VSb789zSdJfrwV +ppXgPtZq5x3GQi6ooteWyuWj3cBcNu9TU1D8u1F0XI5gw4Y0VpxlDxysUgFQJlo4 +VYmMpgr442o/35UgwzkIC7x/6dkvMZvM4jYB5JZJXjynR35XawXB/hzybermJ8BB +DsY0MCOwxhpsTbyEC4wfxZ08B4JtORkToOt4OWuejovsr68Ht6ytOPj7dquoPPNM +9eGNSp94nEIiZ2n75ZMg0gIQArXU9OCV6B2TXxB7w2YB0y0teDgVhoM3IY/ltqJ/ +PJrUUjM8OQ== +-----END CERTIFICATE----- diff --git a/certbot/src/certbot/tests/testdata/ocsp_responder_certificate.pem b/certbot/src/certbot/tests/testdata/ocsp_responder_certificate.pem new file mode 100644 index 00000000000..53bc4a92ae5 --- /dev/null +++ b/certbot/src/certbot/tests/testdata/ocsp_responder_certificate.pem @@ -0,0 +1,27 @@ +-----BEGIN CERTIFICATE----- +MIIEpjCCAo6gAwIBAgINARMIGYlEsD1LTt6D7zANBgkqhkiG9w0BAQsFADBRMQsw +CQYDVQQGEwJOTzEdMBsGA1UECgwUQnV5cGFzcyBBUy05ODMxNjMzMjcxIzAhBgNV +BAMMGkJ1eXBhc3MgQ2xhc3MgMiBUZXN0NCBDQSA1MB4XDTE5MDQwNTEwMDAwMFoX +DTE5MDcwNDEwMDAwMFowSTELMAkGA1UEBhMCTk8xHTAbBgNVBAoMFEJ1eXBhc3Mg +QVMtOTgzMTYzMzI3MRswGQYDVQQDDBJCdXlwYXNzIFRlc3Q0IE9DU1AwggEiMA0G +CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDKGF+kYNd1fbhYT7Vf9xouZlx+4w45 +Y5EowPoaSKFo4uUDDxkj4PwmMiH4w9Q2bGrCbZRrDrvlNVY/kwzLu4CIk6Ip0dgm +VZGNFB3Xo9nai7rI5pn/YVvVnDIQXh1LRbekzLVyHvhRgMpRb19xN/iYsxaOJDph +8eAgbTKf6eitvfbvn/zXHj4KGKycuULI4+mwlfV3uioT4ulbT7PTVJetgi/XXFDO +xMjbqx6I1ZMmzKJ6LNaFlfx6GdZsaLRDCidHzGp8Fm4ZdV+UPvMZcVDQO6rvQ3wU +iGyCqgfE5e0aFvfeLoBPBtaoT0Ht1CvGdTfVet6PXrF6gh40fdEH5Ob5AgMBAAGj +gYQwgYEwCQYDVR0TBAIwADAfBgNVHSMEGDAWgBR32dAGkWbY3RkmjUms2jAvanti +sTAdBgNVHQ4EFgQU3VlR+sSIVpmXklieP7IlpVUcXIowDgYDVR0PAQH/BAQDAgeA +MBMGA1UdJQQMMAoGCCsGAQUFBwMJMA8GCSsGAQUFBzABBQQCBQAwDQYJKoZIhvcN +AQELBQADggIBAFBRLVsBadNFAoFi0HOrfxYsiqggZGJLlgxGyi/0NBIgduG4kcpM +THvplwBwMQEqyp5511pSEbLPAFj8EqC5c46hXZXmT49xlfRvr2Bo+qtTPV9szuWr +8muEIejwRrkATpqWPZWR2zVTXfB90mU2oGuRvxUVmnW4v+FrCChJo7+9yTocZJKx +p4vxYfPMeggomdGAAUz94+0ppSjOLDzs3MA8uOcR0zJ2Y7UHb7PBf/HiM3GO2uKB +sRgdDaGIf/PNpav0xJ/abGNNNwvXzHiMgqqImsuv/JoncPQWbClNurhXpdN7xt9C +HcLX2AdggabcogjWm4guBFuFTsL1i0l8Bsu/6iPJ7ddCeANfYzf7h6AcQq12uFl3 +070F29DtPh8D3FPWgRZZsxoANFjXErxfj4a4+DR+jhhkb9YM/wI0vCOM7W6PKxVn +ZK5kHGOQTcQMj7RCX52gEf27M33zC7HVam+kKhGvwq7D9Bs5hZclzcbjpR4eIxT7 +tzuiy5VpPh1DRLPrphPUB4xsA1dy6zbkg8OqddG6NxD++ja/iZyzSB3SeWyO02qA +QoK2FzDasxpZ9rT3ioAcms3wVNe4lcd4OP8gHZONuat/gvxk6OZvAld6cnIrQZYB +Tbu89ZWvhsyI3p4YC/15pUvA95j9Y0te+G+CF22Eoyb+rtz6mMletnUB +-----END CERTIFICATE----- diff --git a/certbot/tests/testdata/os-release b/certbot/src/certbot/tests/testdata/os-release similarity index 100% rename from certbot/tests/testdata/os-release rename to certbot/src/certbot/tests/testdata/os-release diff --git a/certbot/tests/testdata/rsa2048_key.pem b/certbot/src/certbot/tests/testdata/rsa2048_key.pem similarity index 100% rename from certbot/tests/testdata/rsa2048_key.pem rename to certbot/src/certbot/tests/testdata/rsa2048_key.pem diff --git a/certbot/tests/testdata/rsa512_key.pem b/certbot/src/certbot/tests/testdata/rsa512_key.pem similarity index 100% rename from certbot/tests/testdata/rsa512_key.pem rename to certbot/src/certbot/tests/testdata/rsa512_key.pem diff --git a/certbot/src/certbot/tests/testdata/sample-archive-ec/cert1.pem b/certbot/src/certbot/tests/testdata/sample-archive-ec/cert1.pem new file mode 100644 index 00000000000..b07af2bf75c --- /dev/null +++ b/certbot/src/certbot/tests/testdata/sample-archive-ec/cert1.pem @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC2zCCAcOgAwIBAgIIBvrEnbPRYu8wDQYJKoZIhvcNAQELBQAwKDEmMCQGA1UE +AxMdUGViYmxlIEludGVybWVkaWF0ZSBDQSAxMjZjNGIwHhcNMjAxMDEyMjEwNzQw +WhcNMjUxMDEyMjEwNzQwWjAjMSEwHwYDVQQDExhjLmVuY3J5cHRpb24tZXhhbXBs +ZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARjMhuW0ENPPC33PjB5XsYU +CRw640kPQENIDatcTJaENZIZdqKd6rI6jc+lpbmXot7Zi52clJlSJS+V6oDAt2Lh +o4HYMIHVMA4GA1UdDwEB/wQEAwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYB +BQUHAwIwDAYDVR0TAQH/BAIwADAdBgNVHQ4EFgQUj7Kd3ENqxlPf8B2bIGhsjydX +mPswHwYDVR0jBBgwFoAUEiGxlkRsi+VvcogH5dVD3h1laAcwMQYIKwYBBQUHAQEE +JTAjMCEGCCsGAQUFBzABhhVodHRwOi8vMTI3LjAuMC4xOjQwMDIwIwYDVR0RBBww +GoIYYy5lbmNyeXB0aW9uLWV4YW1wbGUuY29tMA0GCSqGSIb3DQEBCwUAA4IBAQCl +k0JXsa8y7fg41WWMDhw60bPW77O0FtOmTcnhdI5daYNemQVk+Q5EMaBLQ/oGjgXd +9QXFzXH1PL904YEnSLt+iTpXn++7rQSNzQsdYqw0neWk4f5pEBiN+WORpb6mwobV +ifMtBOkNEHvrJ2Pkci9U1lLwtKD/DSew6QtJU5DSkmH1XdGuMJiubygEIvELtvgq +cP9S368ZvPmPGmKaJQXBiuaR8MTjY/Bkr79aXQMjKbf+mpn7h0POCcePk1DY/rm6 +Da+X16lf0hHyQhSUa7Vgyim6rK1/hlw+Z00i+sQCKD9Ih7kXuuGqfSDC33cfO8Tj +o/MXO8lcxkrem5zU5QWP +-----END CERTIFICATE----- diff --git a/certbot/src/certbot/tests/testdata/sample-archive-ec/chain1.pem b/certbot/src/certbot/tests/testdata/sample-archive-ec/chain1.pem new file mode 100644 index 00000000000..64a805c0fd1 --- /dev/null +++ b/certbot/src/certbot/tests/testdata/sample-archive-ec/chain1.pem @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDUDCCAjigAwIBAgIIbi787yVrcMAwDQYJKoZIhvcNAQELBQAwIDEeMBwGA1UE +AxMVUGViYmxlIFJvb3QgQ0EgMGM1MjI1MCAXDTIwMTAxMjIwMjI0NloYDzIwNTAx +MDEyMjEyMjQ2WjAoMSYwJAYDVQQDEx1QZWJibGUgSW50ZXJtZWRpYXRlIENBIDEy +NmM0YjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALGeVk1BMJraeqRq +mJ2+hgso8VOAv2s2CVxUJjIVcn7f2adE8NyTsSQ1brlsnKCUYUw7yLTQH0izLQRB +qKVIDFkUqo5/FuTJ2QlfA2EwBL8J7s/7L7vj3L0DiVpwgxPSyFEwdl/Y5y7ofsX5 +CIhCFcaMAmTIuKLiSfCJjGwkbEMuolm+lO8Mikxxc/JtDVUC479ugU7PU9O09bMH +nm+sD6Bgd+KMoPkCCCoeShJS9X3Ziq9HGc7Z6nhM/zirFARt2XkonEdAZ8br01zY +MRiY9txhlWQ7mUkOtzOSoEuYJNoUbvMUf0+tNzto26WRyF7dJmh7lTBsYrvAwUTx +PzNyst0CAwEAAaOBgzCBgDAOBgNVHQ8BAf8EBAMCAoQwHQYDVR0lBBYwFAYIKwYB +BQUHAwEGCCsGAQUFBwMCMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFBIhsZZE +bIvlb3KIB+XVQ94dZWgHMB8GA1UdIwQYMBaAFOaKTaXg37vKgRt7d79YOjAoAtJT +MA0GCSqGSIb3DQEBCwUAA4IBAQAU2mZii7PH2pkw2lNM0QqPbcW/UYyvFoUeM8Aq +uCtsI2s+oxCJTqzfLsA0N8NY4nHLQ5wAlNJfJekngni8hbmJTKU4JFTMe7kLQO8P +fJbk0pTzhhHVQw7CVwB6Pwq3u2m/JV+d6xDIDc+AVkuEl19ZJU0rTWyooClfFLZV +EdZmEiUtA3PGlxoYwYhoGHYlhFxsoFONhCsBEdN7k7FKtFGVxN7oc5SKmKp0YZTW +fcrEtrdNThATO4ymhCC2zh33NI/MT1O74fpaAc2k6LcTl57MKiLfTYX4LTL6v9JG +9tlNqjFVRRmzEbtXTPcCb+w9g1VqoOGok7mGXYLTYtShCuvE +-----END CERTIFICATE----- diff --git a/certbot/src/certbot/tests/testdata/sample-archive-ec/fullchain1.pem b/certbot/src/certbot/tests/testdata/sample-archive-ec/fullchain1.pem new file mode 100644 index 00000000000..1ba80ba4e9e --- /dev/null +++ b/certbot/src/certbot/tests/testdata/sample-archive-ec/fullchain1.pem @@ -0,0 +1,38 @@ +-----BEGIN CERTIFICATE----- +MIIC2zCCAcOgAwIBAgIILlmGtZhUFEwwDQYJKoZIhvcNAQELBQAwKDEmMCQGA1UE +AxMdUGViYmxlIEludGVybWVkaWF0ZSBDQSAxMjZjNGIwHhcNMjAxMDEyMjA1MDM0 +WhcNMjUxMDEyMjA1MDM0WjAjMSEwHwYDVQQDExhjLmVuY3J5cHRpb24tZXhhbXBs +ZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAARHEzR8JPWrEmpmgM+F2bk5 +9mT0u6CjzmJG0QpbaqprLiG5NGpW84VQ5TFCrmC4KxYfigCfMhfHRNfFYvNUK3V/ +o4HYMIHVMA4GA1UdDwEB/wQEAwIFoDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYB +BQUHAwIwDAYDVR0TAQH/BAIwADAdBgNVHQ4EFgQU1CsVL+bPnzaxxQ5jUENmQJIO +lKwwHwYDVR0jBBgwFoAUEiGxlkRsi+VvcogH5dVD3h1laAcwMQYIKwYBBQUHAQEE +JTAjMCEGCCsGAQUFBzABhhVodHRwOi8vMTI3LjAuMC4xOjQwMDIwIwYDVR0RBBww +GoIYYy5lbmNyeXB0aW9uLWV4YW1wbGUuY29tMA0GCSqGSIb3DQEBCwUAA4IBAQBn +2D8loC7pfk28JYpFLr5lmFKJWWmtLGlpsWDj61fVjtTfGKLziJz+MM6il4Y3hIz5 +58qiFK0ue0M63dIBJ33N+XxSEXon4Q0gy/zRWfH9jtPJ3FwfjkU/RT9PAUClYi0G +ptNWnTmgQkNzousbcAtRNXuuShH3856vhUnwkX+xM+cbIDi1JVmFjcGrEEQJ0rUF +mv2ZTyfbWbUs3v4rReETi2NVzr1Ql6J+ByNcMvHODzFy3t0L6yelAw2ca1I+c9HU ++Z0tnp/ykR7eXNuVLivok8UBf5OC413lh8ZO5g+Bgzh/LdtkUuavg1MYtEX0H6mX +9U7y3nVI8WEbPGf+HDeu +-----END CERTIFICATE----- +-----BEGIN CERTIFICATE----- +MIIDUDCCAjigAwIBAgIIbi787yVrcMAwDQYJKoZIhvcNAQELBQAwIDEeMBwGA1UE +AxMVUGViYmxlIFJvb3QgQ0EgMGM1MjI1MCAXDTIwMTAxMjIwMjI0NloYDzIwNTAx +MDEyMjEyMjQ2WjAoMSYwJAYDVQQDEx1QZWJibGUgSW50ZXJtZWRpYXRlIENBIDEy +NmM0YjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALGeVk1BMJraeqRq +mJ2+hgso8VOAv2s2CVxUJjIVcn7f2adE8NyTsSQ1brlsnKCUYUw7yLTQH0izLQRB +qKVIDFkUqo5/FuTJ2QlfA2EwBL8J7s/7L7vj3L0DiVpwgxPSyFEwdl/Y5y7ofsX5 +CIhCFcaMAmTIuKLiSfCJjGwkbEMuolm+lO8Mikxxc/JtDVUC479ugU7PU9O09bMH +nm+sD6Bgd+KMoPkCCCoeShJS9X3Ziq9HGc7Z6nhM/zirFARt2XkonEdAZ8br01zY +MRiY9txhlWQ7mUkOtzOSoEuYJNoUbvMUf0+tNzto26WRyF7dJmh7lTBsYrvAwUTx +PzNyst0CAwEAAaOBgzCBgDAOBgNVHQ8BAf8EBAMCAoQwHQYDVR0lBBYwFAYIKwYB +BQUHAwEGCCsGAQUFBwMCMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYEFBIhsZZE +bIvlb3KIB+XVQ94dZWgHMB8GA1UdIwQYMBaAFOaKTaXg37vKgRt7d79YOjAoAtJT +MA0GCSqGSIb3DQEBCwUAA4IBAQAU2mZii7PH2pkw2lNM0QqPbcW/UYyvFoUeM8Aq +uCtsI2s+oxCJTqzfLsA0N8NY4nHLQ5wAlNJfJekngni8hbmJTKU4JFTMe7kLQO8P +fJbk0pTzhhHVQw7CVwB6Pwq3u2m/JV+d6xDIDc+AVkuEl19ZJU0rTWyooClfFLZV +EdZmEiUtA3PGlxoYwYhoGHYlhFxsoFONhCsBEdN7k7FKtFGVxN7oc5SKmKp0YZTW +fcrEtrdNThATO4ymhCC2zh33NI/MT1O74fpaAc2k6LcTl57MKiLfTYX4LTL6v9JG +9tlNqjFVRRmzEbtXTPcCb+w9g1VqoOGok7mGXYLTYtShCuvE +-----END CERTIFICATE----- diff --git a/certbot/src/certbot/tests/testdata/sample-archive-ec/privkey1.pem b/certbot/src/certbot/tests/testdata/sample-archive-ec/privkey1.pem new file mode 100644 index 00000000000..6843b83d6e4 --- /dev/null +++ b/certbot/src/certbot/tests/testdata/sample-archive-ec/privkey1.pem @@ -0,0 +1,5 @@ +-----BEGIN PRIVATE KEY----- +MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgNgefv2dad4U1VYEi +0WkdHuqywi5QXAe30OwNTTGjhbihRANCAARHEzR8JPWrEmpmgM+F2bk59mT0u6Cj +zmJG0QpbaqprLiG5NGpW84VQ5TFCrmC4KxYfigCfMhfHRNfFYvNUK3V/ +-----END PRIVATE KEY----- diff --git a/certbot/tests/testdata/sample-archive/cert1.pem b/certbot/src/certbot/tests/testdata/sample-archive/cert1.pem similarity index 100% rename from certbot/tests/testdata/sample-archive/cert1.pem rename to certbot/src/certbot/tests/testdata/sample-archive/cert1.pem diff --git a/certbot/tests/testdata/sample-archive/chain1.pem b/certbot/src/certbot/tests/testdata/sample-archive/chain1.pem similarity index 100% rename from certbot/tests/testdata/sample-archive/chain1.pem rename to certbot/src/certbot/tests/testdata/sample-archive/chain1.pem diff --git a/certbot/tests/testdata/sample-archive/fullchain1.pem b/certbot/src/certbot/tests/testdata/sample-archive/fullchain1.pem similarity index 100% rename from certbot/tests/testdata/sample-archive/fullchain1.pem rename to certbot/src/certbot/tests/testdata/sample-archive/fullchain1.pem diff --git a/certbot/tests/testdata/sample-archive/privkey1.pem b/certbot/src/certbot/tests/testdata/sample-archive/privkey1.pem similarity index 100% rename from certbot/tests/testdata/sample-archive/privkey1.pem rename to certbot/src/certbot/tests/testdata/sample-archive/privkey1.pem diff --git a/certbot/tests/testdata/sample-renewal-ancient.conf b/certbot/src/certbot/tests/testdata/sample-renewal-ancient.conf similarity index 96% rename from certbot/tests/testdata/sample-renewal-ancient.conf rename to certbot/src/certbot/tests/testdata/sample-renewal-ancient.conf index 333bcaa18b4..9586d549209 100644 --- a/certbot/tests/testdata/sample-renewal-ancient.conf +++ b/certbot/src/certbot/tests/testdata/sample-renewal-ancient.conf @@ -62,14 +62,12 @@ break_my_certs = False standalone = True manual = False server = https://acme-staging.api.letsencrypt.org/directory -standalone_supported_challenges = "tls-sni-01,http-01" webroot = True os_packages_only = False apache_init_script = None user_agent = None apache_le_vhost_ext = -le-ssl.conf debug = False -tls_sni_01_port = 443 logs_dir = /var/log/letsencrypt apache_vhost_root = /etc/apache2/sites-available configurator = None diff --git a/certbot/src/certbot/tests/testdata/sample-renewal-deprecated-option.conf b/certbot/src/certbot/tests/testdata/sample-renewal-deprecated-option.conf new file mode 100644 index 00000000000..2b777d3de7b --- /dev/null +++ b/certbot/src/certbot/tests/testdata/sample-renewal-deprecated-option.conf @@ -0,0 +1,14 @@ +# renew_before_expiry = 30 days +version = 1.11.0 +archive_dir = MAGICDIR/live/sample-renewal-deprecated-option +cert = MAGICDIR/live/sample-renewal-deprecated-option/cert.pem +privkey = MAGICDIR/live/sample-renewal-deprecated-option/privkey.pem +chain = MAGICDIR/live/sample-renewal-deprecated-option/chain.pem +fullchain = MAGICDIR/live/sample-renewal-deprecated-option/fullchain.pem + +# Options used in the renewal process +[renewalparams] +account = ffffffffffffffffffffffffffffffff +authenticator = nginx +installer = nginx +manual_public_ip_logging_ok = None diff --git a/certbot/src/certbot/tests/testdata/sample-renewal-ec.conf b/certbot/src/certbot/tests/testdata/sample-renewal-ec.conf new file mode 100644 index 00000000000..d8a4b32bff0 --- /dev/null +++ b/certbot/src/certbot/tests/testdata/sample-renewal-ec.conf @@ -0,0 +1,79 @@ +# add some stuff here +# assets/integration_tests + +cert = MAGICDIR/live/sample-renewal-ec/cert.pem +privkey = MAGICDIR/live/sample-renewal-ec/privkey.pem +chain = MAGICDIR/live/sample-renewal-ec/chain.pem +fullchain = MAGICDIR/live/sample-renewal-ec/fullchain.pem +renew_before_expiry = 4 years + +# Options and defaults used in the renewal process +[renewalparams] +no_self_upgrade = False +apache_enmod = a2enmod +no_verify_ssl = False +ifaces = None +apache_dismod = a2dismod +register_unsafely_without_email = False +apache_handle_modules = True +uir = None +installer = None +nginx_ctl = nginx +config_dir = MAGICDIR +text_mode = False +func = +staging = True +prepare = False +work_dir = /var/lib/letsencrypt +tos = False +init = False +http01_port = 80 +duplicate = False +noninteractive_mode = True +key_path = None +nginx = False +nginx_server_root = /etc/nginx +fullchain_path = /home/ubuntu/letsencrypt/chain.pem +email = None +csr = None +agree_dev_preview = None +redirect = None +verb = certonly +verbose_count = -3 +config_file = None +renew_by_default = False +hsts = False +apache_handle_sites = True +authenticator = standalone +domains = isnot.org, +key_type = ecdsa +elliptic_curve = secp256r1 +apache_challenge_location = /etc/apache2 +checkpoints = 1 +manual_test_mode = False +apache = False +cert_path = /home/ubuntu/letsencrypt/cert.pem +webroot_path = None +reinstall = False +expand = False +strict_permissions = False +apache_server_root = /etc/apache2 +account = None +dry_run = False +manual_public_ip_logging_ok = False +chain_path = /home/ubuntu/letsencrypt/chain.pem +break_my_certs = False +standalone = True +manual = False +server = https://acme-staging-v02.api.letsencrypt.org/directory +webroot = False +os_packages_only = False +apache_init_script = None +user_agent = None +apache_le_vhost_ext = -le-ssl.conf +debug = False +logs_dir = /var/log/letsencrypt +apache_vhost_root = /etc/apache2/sites-available +configurator = None +must_staple = True +[[webroot_map]] diff --git a/certbot/tests/testdata/sample-renewal.conf b/certbot/src/certbot/tests/testdata/sample-renewal.conf similarity index 96% rename from certbot/tests/testdata/sample-renewal.conf rename to certbot/src/certbot/tests/testdata/sample-renewal.conf index 04f9ae8ca20..0c56781b3dd 100644 --- a/certbot/tests/testdata/sample-renewal.conf +++ b/certbot/src/certbot/tests/testdata/sample-renewal.conf @@ -44,6 +44,7 @@ apache_handle_sites = True authenticator = standalone domains = isnot.org, rsa_key_size = 2048 +elliptic_curve = secp256r1 apache_challenge_location = /etc/apache2 checkpoints = 1 manual_test_mode = False @@ -62,14 +63,12 @@ break_my_certs = False standalone = True manual = False server = https://acme-staging-v02.api.letsencrypt.org/directory -standalone_supported_challenges = "tls-sni-01,http-01" webroot = False os_packages_only = False apache_init_script = None user_agent = None apache_le_vhost_ext = -le-ssl.conf debug = False -tls_sni_01_port = 443 logs_dir = /var/log/letsencrypt apache_vhost_root = /etc/apache2/sites-available configurator = None diff --git a/certbot/tests/testdata/webrootconftest.ini b/certbot/src/certbot/tests/testdata/webrootconftest.ini similarity index 100% rename from certbot/tests/testdata/webrootconftest.ini rename to certbot/src/certbot/tests/testdata/webrootconftest.ini diff --git a/certbot/src/certbot/tests/util.py b/certbot/src/certbot/tests/util.py new file mode 100644 index 00000000000..7a086f6c932 --- /dev/null +++ b/certbot/src/certbot/tests/util.py @@ -0,0 +1,459 @@ +"""Test utilities.""" +import atexit +from contextlib import ExitStack +import copy +from importlib import reload as reload_module +import importlib.resources +import io +import logging +import multiprocessing +from multiprocessing import synchronize +import shutil +import sys +import tempfile +from typing import Any +from typing import Callable +from typing import cast +from typing import IO +from typing import Iterable +from typing import Optional +from typing import Union +import unittest +from unittest import mock + +from cryptography import x509 +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey +import josepy as jose + +from certbot import configuration +from certbot import util +from certbot._internal import constants +from certbot._internal import lock +from certbot._internal import storage +from certbot._internal.display import obj as display_obj +from certbot.compat import filesystem +from certbot.compat import os +from certbot.display import util as display_util +from certbot.plugins import common + + +class DummyInstaller(common.Installer): + """Dummy installer plugin for test purpose.""" + def get_all_names(self) -> Iterable[str]: + return [] + + def deploy_cert(self, domain: str, cert_path: str, key_path: str, chain_path: str, + fullchain_path: str) -> None: + pass + + def enhance(self, domain: str, enhancement: str, + options: Optional[Union[list[str], str]] = None) -> None: + pass + + def supported_enhancements(self) -> list[str]: + return [] + + def save(self, title: Optional[str] = None, temporary: bool = False) -> None: + pass + + def config_test(self) -> None: + pass + + def restart(self) -> None: + pass + + @classmethod + def add_parser_arguments(cls, add: Callable[..., None]) -> None: + pass + + def prepare(self) -> None: + pass + + def more_info(self) -> str: + return "" + + +def vector_path(*names: str) -> str: + """Path to a test vector.""" + _file_manager = ExitStack() + atexit.register(_file_manager.close) + vector_ref = importlib.resources.files(__package__).joinpath('testdata', *names) + path = _file_manager.enter_context(importlib.resources.as_file(vector_ref)) + return str(path) + + +def load_vector(*names: str) -> bytes: + """Load contents of a test vector.""" + vector_ref = importlib.resources.files(__package__).joinpath('testdata', *names) + data = vector_ref.read_bytes() + # Try at most to convert CRLF to LF when data is text + try: + return data.decode().replace('\r\n', '\n').encode() + except ValueError: + # Failed to process the file with standard encoding. + # Most likely not a text file, return its bytes untouched. + return data + + +def _guess_loader(filename: str, loader_pem: Callable, loader_der: Callable) -> Callable: + _, ext = os.path.splitext(filename) + if ext.lower() == '.pem': + return loader_pem + elif ext.lower() == '.der': + return loader_der + raise ValueError("Loader could not be recognized based on extension") # pragma: no cover + + +def load_cert(*names: str) -> x509.Certificate: + """Load certificate.""" + loader = _guess_loader( + names[-1], x509.load_pem_x509_certificate, x509.load_der_x509_certificate + ) + return loader(load_vector(*names)) + + +def load_jose_rsa_private_key_pem(*names: str) -> jose.ComparableRSAKey: + """Load RSA private key wrapped in jose.ComparableRSAKey""" + return jose.ComparableRSAKey(load_rsa_private_key_pem(*names)) + + +def load_rsa_private_key_pem(*names: str) -> RSAPrivateKey: + """Load RSA private key.""" + _, ext = os.path.splitext(names[-1]) + assert ext.lower() == '.pem' + key = serialization.load_pem_private_key( + load_vector(*names), password=None, backend=default_backend()) + assert isinstance(key, RSAPrivateKey) + return key + + +def make_lineage(config_dir: str, testfile: str, ec: bool = True) -> str: + """Creates a lineage defined by testfile. + + This creates the archive, live, and renewal directories if + necessary and creates a simple lineage. + + :param str config_dir: path to the configuration directory + :param str testfile: configuration file to base the lineage on + :param bool ec: True if we generate the lineage with an ECDSA key + + :returns: path to the renewal conf file for the created lineage + :rtype: str + + """ + lineage_name = testfile[:-len('.conf')] + + conf_dir = os.path.join( + config_dir, constants.RENEWAL_CONFIGS_DIR) + archive_dir = os.path.join( + config_dir, constants.ARCHIVE_DIR, lineage_name) + live_dir = os.path.join( + config_dir, constants.LIVE_DIR, lineage_name) + + for directory in (archive_dir, conf_dir, live_dir,): + if not os.path.exists(directory): + filesystem.makedirs(directory) + + sample_archive = vector_path('sample-archive{}'.format('-ec' if ec else '')) + for kind in os.listdir(sample_archive): + shutil.copyfile(os.path.join(sample_archive, kind), + os.path.join(archive_dir, kind)) + + for kind in storage.ALL_FOUR: + os.symlink(os.path.join(archive_dir, '{0}1.pem'.format(kind)), + os.path.join(live_dir, '{0}.pem'.format(kind))) + + conf_path = os.path.join(config_dir, conf_dir, testfile) + with open(vector_path(testfile)) as src: + with open(conf_path, 'w') as dst: + dst.writelines( + line.replace('MAGICDIR', config_dir) for line in src) + + return conf_path + + +def patch_display_util() -> mock.MagicMock: + """Patch certbot.display.util to use a special mock display utility. + + The mock display utility works like a regular mock object, except it also + also asserts that methods are called with valid arguments. + + The mock created by this patch mocks out Certbot internals. That is, the + mock object will be called by the certbot.display.util functions and the + mock returned by that call will be used as the display utility. This was + done to simplify the transition from zope.component and mocking + certbot.display.util functions directly in test code should be preferred + over using this function in the future. + + See https://github.com/certbot/certbot/issues/8948 + + :returns: patch on the function used internally by certbot.display.util to + get a display utility instance + :rtype: mock.MagicMock + + """ + return cast(mock.MagicMock, mock.patch('certbot._internal.display.obj.get_display', + new_callable=_create_display_util_mock)) + + +def patch_display_util_with_stdout( + stdout: Optional[IO] = None) -> mock.MagicMock: + """Patch certbot.display.util to use a special mock display utility. + + The mock display utility works like a regular mock object, except it also + asserts that methods are called with valid arguments. + + The mock created by this patch mocks out Certbot internals. That is, the + mock object will be called by the certbot.display.util functions and the + mock returned by that call will be used as the display utility. This was + done to simplify the transition from zope.component and mocking + certbot.display.util functions directly in test code should be preferred + over using this function in the future. + + See https://github.com/certbot/certbot/issues/8948 + + The `message` argument passed to the display utility methods is passed to + stdout's write method. + + :param object stdout: object to write standard output to; it is + expected to have a `write` method + :returns: patch on the function used internally by certbot.display.util to + get a display utility instance + :rtype: mock.MagicMock + + """ + stdout = stdout if stdout else io.StringIO() + + return cast(mock.MagicMock, mock.patch('certbot._internal.display.obj.get_display', + new=_create_display_util_mock_with_stdout(stdout))) + + +class FreezableMock: + """Mock object with the ability to freeze attributes. + + This class works like a regular mock.MagicMock object, except + attributes and behavior set before the object is frozen cannot + be changed during tests. + + If a func argument is provided to the constructor, this function + is called first when an instance of FreezableMock is called, + followed by the usual behavior defined by MagicMock. The return + value of func is ignored. + + """ + def __init__(self, frozen: bool = False, func: Optional[Callable[..., Any]] = None, + return_value: Any = mock.sentinel.DEFAULT) -> None: + self._frozen_set = set() if frozen else {'freeze', } + self._func = func + self._mock = mock.MagicMock() + if return_value != mock.sentinel.DEFAULT: + self.return_value = return_value + self._frozen = frozen + + def freeze(self) -> None: + """Freeze object preventing further changes.""" + self._frozen = True + + def __call__(self, *args: Any, **kwargs: Any) -> mock.MagicMock: + if self._func is not None: + self._func(*args, **kwargs) + return self._mock(*args, **kwargs) + + def __getattribute__(self, name: str) -> Any: + if name == '_frozen': + try: + return object.__getattribute__(self, name) + except AttributeError: + return False + elif name in ('return_value', 'side_effect',): + return getattr(object.__getattribute__(self, '_mock'), name) + elif name == '_frozen_set' or name in self._frozen_set: + return object.__getattribute__(self, name) + else: + return getattr(object.__getattribute__(self, '_mock'), name) + + def __setattr__(self, name: str, value: Any) -> None: + """ Before it is frozen, attributes are set on the FreezableMock + instance and added to the _frozen_set. Attributes in the _frozen_set + cannot be changed after the FreezableMock is frozen. In this case, + they are set on the underlying _mock. + + In cases of return_value and side_effect, these attributes are always + passed through to the instance's _mock and added to the _frozen_set + before the object is frozen. + + """ + if self._frozen: + if name in self._frozen_set: + raise AttributeError('Cannot change frozen attribute ' + name) + return setattr(self._mock, name, value) + + if name != '_frozen_set': + self._frozen_set.add(name) + + if name in ('return_value', 'side_effect'): + return setattr(self._mock, name, value) + + return object.__setattr__(self, name, value) + + +def _create_display_util_mock() -> FreezableMock: + display = FreezableMock() + # Use pylint code for disable to keep on single line under line length limit + method_list = [func for func in dir(display_obj.FileDisplay) + if callable(getattr(display_obj.FileDisplay, func)) + and not func.startswith("__")] + for method in method_list: + if method != 'notification': + frozen_mock = FreezableMock(frozen=True, func=_assert_valid_call) + setattr(display, method, frozen_mock) + display.freeze() + return FreezableMock(frozen=True, return_value=display) + + +def _create_display_util_mock_with_stdout(stdout: IO) -> FreezableMock: + def _write_msg(message: str, *unused_args: Any, **unused_kwargs: Any) -> None: + """Write to message to stdout. + """ + if message: + stdout.write(message) + + def mock_method(*args: Any, **kwargs: Any) -> None: + """ + Mock function for display utility methods. + """ + _assert_valid_call(args, kwargs) + _write_msg(*args, **kwargs) + + display = FreezableMock() + # Use pylint code for disable to keep on single line under line length limit + method_list = [func for func in dir(display_obj.FileDisplay) + if callable(getattr(display_obj.FileDisplay, func)) + and not func.startswith("__")] + for method in method_list: + if method == 'notification': + frozen_mock = FreezableMock(frozen=True, + func=_write_msg) + else: + frozen_mock = FreezableMock(frozen=True, + func=mock_method) + setattr(display, method, frozen_mock) + display.freeze() + return FreezableMock(frozen=True, return_value=display) + + +def _assert_valid_call(*args: Any, **kwargs: Any) -> None: + assert_args = [args[0] if args else kwargs['message']] + + assert_kwargs = { + 'default': kwargs.get('default', None), + 'cli_flag': kwargs.get('cli_flag', None), + 'force_interactive': kwargs.get('force_interactive', False), + } + + display_util.assert_valid_call(*assert_args, **assert_kwargs) + + +class TempDirTestCase(unittest.TestCase): + """Base test class which sets up and tears down a temporary directory""" + + def setUp(self) -> None: + """Execute before test""" + self.tempdir = tempfile.mkdtemp() + + def tearDown(self) -> None: + """Execute after test""" + # Cleanup opened resources after a test. This is usually done through atexit handlers in + # Certbot, but during tests, atexit will not run registered functions before tearDown is + # called and instead will run them right before the entire test process exits. + # It is a problem on Windows, that does not accept to clean resources before closing them. + logging.shutdown() + # Remove logging handlers that have been closed so they won't be + # accidentally used in future tests. + logging.getLogger().handlers = [] + util._release_locks() # pylint: disable=protected-access + + shutil.rmtree(self.tempdir) + + +class ConfigTestCase(TempDirTestCase): + """Test class which sets up a NamespaceConfig object.""" + def setUp(self) -> None: + super().setUp() + self.config = configuration.NamespaceConfig( + # We make a copy here so any mutable values from CLI_DEFAULTS do not get modified. + mock.MagicMock(**copy.deepcopy(constants.CLI_DEFAULTS)), + ) + self.config.set_argument_sources({}) + self.config.namespace.verb = "certonly" + self.config.namespace.config_dir = os.path.join(self.tempdir, 'config') + self.config.namespace.work_dir = os.path.join(self.tempdir, 'work') + self.config.namespace.logs_dir = os.path.join(self.tempdir, 'logs') + self.config.namespace.cert_path = constants.CLI_DEFAULTS['auth_cert_path'] + self.config.namespace.fullchain_path = constants.CLI_DEFAULTS['auth_chain_path'] + self.config.namespace.chain_path = constants.CLI_DEFAULTS['auth_chain_path'] + self.config.namespace.server = "https://example.com" + + +def _handle_lock(event_in: synchronize.Event, event_out: synchronize.Event, path: str) -> None: + """ + Acquire a file lock on given path, then wait to release it. This worker is coordinated + using events to signal when the lock should be acquired and released. + :param multiprocessing.Event event_in: event object to signal when to release the lock + :param multiprocessing.Event event_out: event object to signal when the lock is acquired + :param path: the path to lock + """ + if os.path.isdir(path): + my_lock = lock.lock_dir(path) + else: + my_lock = lock.LockFile(path) + try: + event_out.set() + assert event_in.wait(timeout=20), 'Timeout while waiting to release the lock.' + finally: + my_lock.release() + + +def lock_and_call(callback: Callable[[], Any], path_to_lock: str) -> None: + """ + Grab a lock on path_to_lock from a foreign process then execute the callback. + :param callable callback: object to call after acquiring the lock + :param str path_to_lock: path to file or directory to lock + """ + # Reload certbot.util module to reset internal _LOCKS dictionary. + reload_module(util) + + emit_event = multiprocessing.Event() + receive_event = multiprocessing.Event() + process = multiprocessing.Process(target=_handle_lock, + args=(emit_event, receive_event, path_to_lock)) + process.start() + + # Wait confirmation that lock is acquired + assert receive_event.wait(timeout=20), 'Timeout while waiting to acquire the lock.' + # Execute the callback + callback() + # Trigger unlock from foreign process + emit_event.set() + + # Wait for process termination + process.join(timeout=10) + assert process.exitcode == 0 + + +def skip_on_windows(reason: str) -> Callable[[Callable[..., Any]], Callable[..., Any]]: + """Decorator to skip permanently a test on Windows. A reason is required.""" + def wrapper(function: Callable[..., Any]) -> Callable[..., Any]: + """Wrapped version""" + return unittest.skipIf(sys.platform == 'win32', reason)(function) + return wrapper + + +def temp_join(path: str) -> str: + """ + Return the given path joined to the tempdir path for the current platform + Eg.: 'cert' => /tmp/cert (Linux) or 'C:\\Users\\currentuser\\AppData\\Temp\\cert' (Windows) + """ + return os.path.join(tempfile.gettempdir(), path) diff --git a/certbot/src/certbot/util.py b/certbot/src/certbot/util.py new file mode 100644 index 00000000000..801c1964765 --- /dev/null +++ b/certbot/src/certbot/util.py @@ -0,0 +1,738 @@ +"""Utilities for all Certbot.""" +import argparse +import atexit +import errno +import itertools +import logging +import platform +import re +import socket +import subprocess +import sys +from typing import Any +from typing import Callable +from typing import IO +from typing import NamedTuple +from typing import Optional +from typing import Union + +import configargparse + +from certbot import errors +from certbot._internal import constants +from certbot._internal import lock +from certbot.compat import filesystem +from certbot.compat import os + +_USE_DISTRO = sys.platform.startswith('linux') +if _USE_DISTRO: + import distro + +logger = logging.getLogger(__name__) + + +class Key(NamedTuple): + """Container for an optional file path and contents for a PEM-formated private key.""" + file: Optional[str] + pem: bytes + + +class CSR(NamedTuple): + """Container for an optional file path and contents for a PEM or DER-formatted CSR.""" + file: Optional[str] + data: bytes + # Note: form is the type of data, "pem" or "der" + form: str + + +class LooseVersion: + """A version with loose rules, i.e. any given string is a valid version number. + + but regular comparison is not supported. Instead, the `try_risky_comparison` method is + provided, which may return an error if two LooseVersions are 'incomparible'. + For example when integer and string version components are present in the same position. + + Differences with old distutils.version.LooseVersion: + (https://github.com/python/cpython/blob/v3.10.0/Lib/distutils/version.py#L269) + Most version comparisons should give the same result. However, if a version has multiple + trailing zeroes, not all of them are used in the comparison. This ensure that, for example, + "2.0" and "2.0.0" are equal. + """ + + def __init__(self, version_string: str) -> None: + """Parses a version string into its components. + + :param str version_string: version string + """ + components: list[Union[int, str]] + components = [x for x in _VERSION_COMPONENT_RE.split(version_string) + if x and x != '.'] + for i, obj in enumerate(components): + try: + components[i] = int(obj) + except ValueError: + pass + + self.version_components = components + + def try_risky_comparison(self, other: 'LooseVersion') -> int: + """Compares the LooseVersion to another value. + + If the other value is another LooseVersion, the version components are compared. Otherwise, + an exception is raised. + + Comparison is performed element-wise. If the version components being compared are of + different types, the two versions are considered incompatible. Otherwise, if either of the + components is not equal to the other, less or greater is returned based on the comparison's + result. In case the two versions are of different lengths, some elements in the longer + version have not yet been compared. If these are all equal to zero, the two versions are + equal. Otherwise, the longer version is greater. + + If the two versions are incompatible, an exception is raised. Otherwise, the returned + integer indicates the result of the comparison. If self == other, 0 is returned. + If self > other, 1 is returned. If self < other -1 is returned. + + Examples: + Equality: + - LooseVersion('1.0').try_risky_comparison(LooseVersion('1.0')) -> 0 + - LooseVersion('2.0.0a').try_risky_comparison(LooseVersion('2.0.0a')) -> 0 + Inequality: + - LooseVersion('2.0.0').try_risky_comparison(LooseVersion('1.0')) -> 1 + - LooseVersion('1.0.1').try_risky_comparison(LooseVersion('2.0a')) -> -1 + Incomparability: + - LooseVersion('1a').try_risky_comparison(LooseVersion('1.0')) -> ValueError + """ + try: + for self_vc, other_vc in itertools.zip_longest(self.version_components, + other.version_components, + fillvalue=0): + # ensure mypy ignores types here and catch any TypeErrors + if self_vc < other_vc: # type: ignore + return -1 + elif self_vc > other_vc: # type: ignore + return 1 + return 0 + except TypeError: + raise ValueError("Cannot meaningfully compare LooseVersion {} with LooseVersion {} " + "due to comparison of version components with different types." + .format(self.version_components, other.version_components)) + + +# ANSI SGR escape codes +# Formats text as bold or with increased intensity +ANSI_SGR_BOLD = '\033[1m' +# Colors text red +ANSI_SGR_RED = "\033[31m" +# Resets output format +ANSI_SGR_RESET = "\033[0m" + + +PERM_ERR_FMT = os.linesep.join(( + "The following error was encountered:", "{0}", + "Either run as root, or set --config-dir, " + "--work-dir, and --logs-dir to writeable paths.")) + + +# Stores importing process ID to be used by atexit_register() +_INITIAL_PID = os.getpid() +# Maps paths to locked directories to their lock object. All locks in +# the dict are attempted to be cleaned up at program exit. If the +# program exits before the lock is cleaned up, it is automatically +# released, but the file isn't deleted. +_LOCKS: dict[str, lock.LockFile] = {} +_VERSION_COMPONENT_RE = re.compile(r'(\d+ | [a-z]+ | \.)', re.VERBOSE) + +def env_no_snap_for_external_calls() -> dict[str, str]: + """ + When Certbot is run inside a Snap, certain environment variables + are modified. But Certbot sometimes calls out to external programs, + since it uses classic confinement. When we do that, we must modify + the env to remove our modifications so it will use the system's + libraries, since they may be incompatible with the versions of + libraries included in the Snap. For example, apachectl, Nginx, and + anything run from inside a hook should call this function and pass + the results into the ``env`` argument of ``subprocess.Popen``. + + :returns: A modified copy of os.environ ready to pass to Popen + :rtype: dict + + """ + env = os.environ.copy() + # Avoid accidentally modifying env + if 'SNAP' not in env or 'CERTBOT_SNAPPED' not in env: + return env + + # These environment variables being set when running external programs can cause issues if these + # programs also use OpenSSL. See https://github.com/certbot/certbot/issues/10190. + env.pop('OPENSSL_FORCE_FIPS_MODE', None) + env.pop('OPENSSL_MODULES', None) + + for path_name in ('PATH', 'LD_LIBRARY_PATH'): + if path_name in env: + env[path_name] = ':'.join(x for x in env[path_name].split(':') if env['SNAP'] not in x) + return env + + +def run_script(params: list[str], log: Callable[[str], None]=logger.error) -> tuple[str, str]: + """Run the script with the given params. + + :param list params: List of parameters to pass to subprocess.run + :param callable log: Logger method to use for errors + + """ + try: + proc = subprocess.run(params, + check=False, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + env=env_no_snap_for_external_calls()) + + except (OSError, ValueError): + msg = "Unable to run the command: %s" % " ".join(params) + log(msg) + raise errors.SubprocessError(msg) + + if proc.returncode != 0: + msg = "Error while running %s.\n%s\n%s" % ( + " ".join(params), proc.stdout, proc.stderr) + # Enter recovery routine... + log(msg) + raise errors.SubprocessError(msg) + + return proc.stdout, proc.stderr + + +def exe_exists(exe: str) -> bool: + """Determine whether path/name refers to an executable. + + :param str exe: Executable path or name + + :returns: If exe is a valid executable + :rtype: bool + + """ + path, _ = os.path.split(exe) + if path: + return filesystem.is_executable(exe) + for path in os.environ["PATH"].split(os.pathsep): + if filesystem.is_executable(os.path.join(path, exe)): + return True + + return False + + +def lock_dir_until_exit(dir_path: str) -> None: + """Lock the directory at dir_path until program exit. + + :param str dir_path: path to directory + + :raises errors.LockError: if the lock is held by another process + + """ + if not _LOCKS: # this is the first lock to be released at exit + atexit_register(_release_locks) + + if dir_path not in _LOCKS: + _LOCKS[dir_path] = lock.lock_dir(dir_path) + + +def _release_locks() -> None: + for dir_lock in _LOCKS.values(): + try: + dir_lock.release() + except: # pylint: disable=bare-except + msg = 'Exception occurred releasing lock: {0!r}'.format(dir_lock) + logger.debug(msg, exc_info=True) + _LOCKS.clear() + + +def set_up_core_dir(directory: str, mode: int, strict: bool) -> None: + """Ensure directory exists with proper permissions and is locked. + + :param str directory: Path to a directory. + :param int mode: Directory mode. + :param bool strict: require directory to be owned by current user + + :raises .errors.LockError: if the directory cannot be locked + :raises .errors.Error: if the directory cannot be made or verified + + """ + try: + make_or_verify_dir(directory, mode, strict) + lock_dir_until_exit(directory) + except OSError as error: + logger.debug("Exception was:", exc_info=True) + raise errors.Error(PERM_ERR_FMT.format(error)) + + +def make_or_verify_dir(directory: str, mode: int = 0o755, strict: bool = False) -> None: + """Make sure directory exists with proper permissions. + + :param str directory: Path to a directory. + :param int mode: Directory mode. + :param bool strict: require directory to be owned by current user + + :raises .errors.Error: if a directory already exists, + but has wrong permissions or owner + + :raises OSError: if invalid or inaccessible file names and + paths, or other arguments that have the correct type, + but are not accepted by the operating system. + + """ + try: + filesystem.makedirs(directory, mode) + except OSError as exception: + if exception.errno == errno.EEXIST: + if strict and not filesystem.check_permissions(directory, mode): + raise errors.Error( + "%s exists, but it should be owned by current user with" + " permissions %s" % (directory, oct(mode))) + else: + raise + + +def safe_open(path: str, mode: str = "w", chmod: Optional[int] = None) -> IO: + """Safely open a file. + + :param str path: Path to a file. + :param str mode: Same os `mode` for `open`. + :param int chmod: Same as `mode` for `filesystem.open`, uses Python defaults + if ``None``. + + """ + open_args: Union[tuple[()], tuple[int]] = () + if chmod is not None: + open_args = (chmod,) + fdopen_args: Union[tuple[()], tuple[int]] = () + fd = filesystem.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, *open_args) + return os.fdopen(fd, mode, *fdopen_args) + + +def _unique_file(path: str, filename_pat: Callable[[int], str], count: int, + chmod: int, mode: str) -> tuple[IO, str]: + while True: + current_path = os.path.join(path, filename_pat(count)) + try: + return safe_open(current_path, chmod=chmod, mode=mode), os.path.abspath(current_path) + except OSError as err: + # "File exists," is okay, try a different name. + if err.errno != errno.EEXIST: + raise + count += 1 + + +def unique_file(path: str, chmod: int = 0o777, mode: str = "w") -> tuple[IO, str]: + """Safely finds a unique file. + + :param str path: path/filename.ext + :param int chmod: File mode + :param str mode: Open mode + + :returns: tuple of file object and file name + + """ + path, tail = os.path.split(path) + return _unique_file( + path, filename_pat=(lambda count: "%04d_%s" % (count, tail)), + count=0, chmod=chmod, mode=mode) + + +def unique_lineage_name(path: str, filename: str, chmod: int = 0o644, + mode: str = "w") -> tuple[IO, str]: + """Safely finds a unique file using lineage convention. + + :param str path: directory path + :param str filename: proposed filename + :param int chmod: file mode + :param str mode: open mode + + :returns: tuple of file object and file name (which may be modified + from the requested one by appending digits to ensure uniqueness) + + :raises OSError: if writing files fails for an unanticipated reason, + such as a full disk or a lack of permission to write to + specified location. + + """ + preferred_path = os.path.join(path, "%s.conf" % (filename)) + try: + return safe_open(preferred_path, chmod=chmod), preferred_path + except OSError as err: + if err.errno != errno.EEXIST: + raise + return _unique_file( + path, filename_pat=(lambda count: "%s-%04d.conf" % (filename, count)), + count=1, chmod=chmod, mode=mode) + + +def safely_remove(path: str) -> None: + """Remove a file that may not exist.""" + try: + os.remove(path) + except OSError as err: + if err.errno != errno.ENOENT: + raise + + +def get_filtered_names(all_names: set[str]) -> set[str]: + """Removes names that aren't considered valid by Let's Encrypt. + + :param set all_names: all names found in the configuration + + :returns: all found names that are considered valid by LE + :rtype: set + + """ + filtered_names = set() + for name in all_names: + try: + filtered_names.add(enforce_le_validity(name)) + except errors.ConfigurationError: + logger.debug('Not suggesting name "%s"', name, exc_info=True) + return filtered_names + +def get_os_info() -> tuple[str, str]: + """ + Get OS name and version + + :returns: (os_name, os_version) + :rtype: `tuple` of `str` + """ + + return get_python_os_info(pretty=False) + +def get_os_info_ua() -> str: + """ + Get OS name and version string for User Agent + + :returns: os_ua + :rtype: `str` + + """ + # distro.name returns an empty string if one cannot be determined. see + # https://github.com/python-distro/distro/blob/3bd19e61fcb7f8d2bf3d45d9e40d69c92e05d241/src/distro/distro.py#L883 + os_info = "" + if _USE_DISTRO: + os_info = distro.name(pretty=True) + + if not _USE_DISTRO or not os_info: + return " ".join(get_python_os_info(pretty=True)) + return os_info + +def get_systemd_os_like() -> list[str]: + """ + Get a list of strings that indicate the distribution likeness to + other distributions. + + :returns: List of distribution acronyms + :rtype: `list` of `str` + """ + + if _USE_DISTRO: + return distro.like().split(" ") + return [] + +def get_var_from_file(varname: str, filepath: str = "/etc/os-release") -> str: + """ + Get single value from a file formatted like systemd /etc/os-release + + :param str varname: Name of variable to fetch + :param str filepath: File path of os-release file + :returns: requested value + :rtype: `str` + """ + + var_string = varname+"=" + if not os.path.isfile(filepath): + return "" + with open(filepath, 'r') as fh: + contents = fh.readlines() + + for line in contents: + if line.strip().startswith(var_string): + # Return the value of var, normalized + return _normalize_string(line.strip()[len(var_string):]) + return "" + +def _normalize_string(orig: str) -> str: + """ + Helper function for get_var_from_file() to remove quotes + and whitespaces + """ + return orig.replace('"', '').replace("'", "").strip() + +def get_python_os_info(pretty: bool = False) -> tuple[str, str]: + """ + Get Operating System type/distribution and major version + using python platform module + + :param bool pretty: If the returned OS name should be in longer (pretty) form + + :returns: (os_name, os_version) + :rtype: `tuple` of `str` + """ + info = platform.system_alias( + platform.system(), + platform.release(), + platform.version() + ) + os_type, os_ver, _ = info + os_type = os_type.lower() + if os_type.startswith('linux') and _USE_DISTRO: + distro_name, distro_version = distro.name() if pretty else distro.id(), distro.version() + # On arch, these values are reportedly empty strings so handle it + # defensively + # so handle it defensively + if distro_name: + os_type = distro_name + if distro_version: + os_ver = distro_version + elif os_type.startswith('darwin'): + try: + proc = subprocess.run( + ["/usr/bin/sw_vers", "-productVersion"], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + check=False, universal_newlines=True, + env=env_no_snap_for_external_calls(), + ) + except OSError: + proc = subprocess.run( + ["sw_vers", "-productVersion"], + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + check=False, universal_newlines=True, + env=env_no_snap_for_external_calls(), + ) + os_ver = proc.stdout.rstrip('\n') + elif os_type.startswith('freebsd'): + # eg "9.3-RC3-p1" + os_ver = os_ver.partition("-")[0] + os_ver = os_ver.partition(".")[0] + elif platform.win32_ver()[1]: + os_ver = platform.win32_ver()[1] + else: + # Cases known to fall here: Cygwin python + os_ver = '' + return os_type, os_ver + +# Just make sure we don't get pwned... Make sure that it also doesn't +# start with a period or have two consecutive periods <- this needs to +# be done in addition to the regex +EMAIL_REGEX = re.compile("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+$") + + +def safe_email(email: str) -> bool: + """Scrub email address before using it.""" + if EMAIL_REGEX.match(email) is not None: + return not email.startswith(".") and ".." not in email + logger.error("Invalid email address: %s.", email) + return False + + +class DeprecatedArgumentAction(argparse.Action): + """Action to log a warning when an argument is used.""" + def __call__(self, unused1: Any, unused2: Any, unused3: Any, + option_string: Optional[str] = None) -> None: + logger.warning("Use of %s is deprecated.", option_string) + + +def add_deprecated_argument(add_argument: Callable[..., None], argument_name: str, + nargs: Union[str, int]) -> None: + """Adds a deprecated argument with the name argument_name. + + Deprecated arguments are not shown in the help. If they are used on + the command line, a warning is shown stating that the argument is + deprecated and no other action is taken. + + :param callable add_argument: Function that adds arguments to an + argument parser/group. + :param str argument_name: Name of deprecated argument. + :param nargs: Value for nargs when adding the argument to argparse. + + """ + if DeprecatedArgumentAction not in configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE: + # In version 0.12.0 ACTION_TYPES_THAT_DONT_NEED_A_VALUE was + # changed from a set to a tuple. + if isinstance(configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE, set): + configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE.add( + DeprecatedArgumentAction) + else: + configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE += ( + DeprecatedArgumentAction,) + add_argument(argument_name, action=DeprecatedArgumentAction, + help=argparse.SUPPRESS, nargs=nargs) + + +def enforce_le_validity(domain: str) -> str: + """Checks that Let's Encrypt will consider domain to be valid. + + :param str domain: FQDN to check + :type domain: `str` + :returns: The domain cast to `str`, with ASCII-only contents + :rtype: str + :raises ConfigurationError: for invalid domains and cases where Let's + Encrypt currently will not issue certificates + + """ + + domain = enforce_domain_sanity(domain) + if not re.match("^[A-Za-z0-9.-]*$", domain): + raise errors.ConfigurationError( + "{0} contains an invalid character. " + "Valid characters are A-Z, a-z, 0-9, ., and -.".format(domain)) + + labels = domain.split(".") + if len(labels) < 2: + raise errors.ConfigurationError( + "{0} needs at least two labels".format(domain)) + for label in labels: + if label.startswith("-"): + raise errors.ConfigurationError( + 'label "{0}" in domain "{1}" cannot start with "-"'.format( + label, domain)) + if label.endswith("-"): + raise errors.ConfigurationError( + 'label "{0}" in domain "{1}" cannot end with "-"'.format( + label, domain)) + return domain + + +def enforce_domain_sanity(domain: Union[str, bytes]) -> str: + """Method which validates domain value and errors out if + the requirements are not met. + + :param domain: Domain to check + :type domain: `str` or `bytes` + :raises ConfigurationError: for invalid domains and cases where Let's + Encrypt currently will not issue certificates + + :returns: The domain cast to `str`, with ASCII-only contents + :rtype: str + """ + # Unicode + try: + if isinstance(domain, bytes): + domain = domain.decode('utf-8') + domain.encode('ascii') + except UnicodeError: + raise errors.ConfigurationError("Non-ASCII domain names not supported. " + "To issue for an Internationalized Domain Name, use Punycode.") + + domain = domain.lower() + + # Remove trailing dot + domain = domain[:-1] if domain.endswith('.') else domain + + # Separately check for odd "domains" like "http://example.com" to fail + # fast and provide a clear error message + for scheme in ["http", "https"]: # Other schemes seem unlikely + if domain.startswith("{0}://".format(scheme)): + raise errors.ConfigurationError( + "Requested name {0} appears to be a URL, not a FQDN. " + "Try again without the leading \"{1}://\".".format( + domain, scheme + ) + ) + + if is_ipaddress(domain): + raise errors.ConfigurationError( + "Requested name {0} is an IP address. The Let's Encrypt " + "certificate authority will not issue certificates for a " + "bare IP address.".format(domain)) + + # FQDN checks according to RFC 2181: domain name should be less than 255 + # octets (inclusive). And each label is 1 - 63 octets (inclusive). + # https://tools.ietf.org/html/rfc2181#section-11 + msg = "Requested domain {0} is not a FQDN because".format(domain) + if len(domain) > 255: + raise errors.ConfigurationError("{0} it is too long.".format(msg)) + labels = domain.split('.') + for l in labels: + if not l: + raise errors.ConfigurationError("{0} it contains an empty label.".format(msg)) + if len(l) > 63: + raise errors.ConfigurationError("{0} label {1} is too long.".format(msg, l)) + + return domain + + +def is_ipaddress(address: str) -> bool: + """Is given address string form of IP(v4 or v6) address? + + :param address: address to check + :type address: `str` + + :returns: True if address is valid IP address, otherwise return False. + :rtype: bool + + """ + try: + socket.inet_pton(socket.AF_INET, address) + # If this line runs it was ip address (ipv4) + return True + except OSError: + # It wasn't an IPv4 address, so try ipv6 + try: + socket.inet_pton(socket.AF_INET6, address) + return True + except OSError: + return False + + +def is_wildcard_domain(domain: Union[str, bytes]) -> bool: + """"Is domain a wildcard domain? + + :param domain: domain to check + :type domain: `bytes` or `str` + + :returns: True if domain is a wildcard, otherwise, False + :rtype: bool + + """ + if isinstance(domain, str): + return domain.startswith("*.") + return domain.startswith(b"*.") + + +def is_staging(srv: str) -> bool: + """ + Determine whether a given ACME server is a known test / staging server. + + :param str srv: the URI for the ACME server + :returns: True iff srv is a known test / staging server + :rtype bool: + """ + return srv == constants.STAGING_URI or "staging" in srv + + +def atexit_register(func: Callable, *args: Any, **kwargs: Any) -> None: + """Sets func to be called before the program exits. + + Special care is taken to ensure func is only called when the process + that first imports this module exits rather than any child processes. + + :param function func: function to be called in case of an error + + """ + atexit.register(_atexit_call, func, *args, **kwargs) + + +def parse_loose_version(version_string: str) -> list[Union[int, str]]: + """Parses a version string into its components. + This code and the returned tuple is based on the now deprecated + distutils.version.LooseVersion class from the Python standard library. + Two LooseVersion classes and two lists as returned by this function should + compare in the same way. See + https://github.com/python/cpython/blob/v3.10.0/Lib/distutils/version.py#L205-L347. + :param str version_string: version string + :returns: list of parsed version string components + :rtype: list + """ + loose_version = LooseVersion(version_string) + return loose_version.version_components + + +def _atexit_call(func: Callable, *args: Any, **kwargs: Any) -> None: + if _INITIAL_PID == os.getpid(): + func(*args, **kwargs) diff --git a/certbot/storage.py b/certbot/storage.py deleted file mode 100644 index 7472df975bf..00000000000 --- a/certbot/storage.py +++ /dev/null @@ -1,1171 +0,0 @@ -"""Renewable certificates storage.""" -import datetime -import glob -import logging -import os -import re -import stat - -import configobj -import parsedatetime -import pytz -import shutil -import six - -import certbot -from certbot import cli -from certbot import compat -from certbot import constants -from certbot import crypto_util -from certbot import errors -from certbot import error_handler -from certbot import util - -from certbot.plugins import common as plugins_common -from certbot.plugins import disco as plugins_disco - -logger = logging.getLogger(__name__) - -ALL_FOUR = ("cert", "privkey", "chain", "fullchain") -README = "README" -CURRENT_VERSION = util.get_strict_version(certbot.__version__) -BASE_PRIVKEY_MODE = 0o600 - - -def renewal_conf_files(config): - """Build a list of all renewal configuration files. - - :param certbot.interfaces.IConfig config: Configuration object - - :returns: list of renewal configuration files - :rtype: `list` of `str` - - """ - result = glob.glob(os.path.join(config.renewal_configs_dir, "*.conf")) - result.sort() - return result - -def renewal_file_for_certname(config, certname): - """Return /path/to/certname.conf in the renewal conf directory""" - path = os.path.join(config.renewal_configs_dir, "{0}.conf".format(certname)) - if not os.path.exists(path): - raise errors.CertStorageError("No certificate found with name {0} (expected " - "{1}).".format(certname, path)) - return path - - -def cert_path_for_cert_name(config, cert_name): - """ If `--cert-name` was specified, but you need a value for `--cert-path`. - - :param `configuration.NamespaceConfig` config: parsed command line arguments - :param str cert_name: cert name. - - """ - cert_name_implied_conf = renewal_file_for_certname(config, cert_name) - fullchain_path = configobj.ConfigObj(cert_name_implied_conf)["fullchain"] - with open(fullchain_path) as f: - cert_path = (fullchain_path, f.read()) - return cert_path - - -def config_with_defaults(config=None): - """Merge supplied config, if provided, on top of builtin defaults.""" - defaults_copy = configobj.ConfigObj(constants.RENEWER_DEFAULTS) - defaults_copy.merge(config if config is not None else configobj.ConfigObj()) - return defaults_copy - - -def add_time_interval(base_time, interval, textparser=parsedatetime.Calendar()): - """Parse the time specified time interval, and add it to the base_time - - The interval can be in the English-language format understood by - parsedatetime, e.g., '10 days', '3 weeks', '6 months', '9 hours', or - a sequence of such intervals like '6 months 1 week' or '3 days 12 - hours'. If an integer is found with no associated unit, it is - interpreted by default as a number of days. - - :param datetime.datetime base_time: The time to be added with the interval. - :param str interval: The time interval to parse. - - :returns: The base_time plus the interpretation of the time interval. - :rtype: :class:`datetime.datetime`""" - - if interval.strip().isdigit(): - interval += " days" - - # try to use the same timezone, but fallback to UTC - tzinfo = base_time.tzinfo or pytz.UTC - - return textparser.parseDT(interval, base_time, tzinfo=tzinfo)[0] - - -def write_renewal_config(o_filename, n_filename, archive_dir, target, relevant_data): - """Writes a renewal config file with the specified name and values. - - :param str o_filename: Absolute path to the previous version of config file - :param str n_filename: Absolute path to the new destination of config file - :param str archive_dir: Absolute path to the archive directory - :param dict target: Maps ALL_FOUR to their symlink paths - :param dict relevant_data: Renewal configuration options to save - - :returns: Configuration object for the new config file - :rtype: configobj.ConfigObj - - """ - config = configobj.ConfigObj(o_filename) - config["version"] = certbot.__version__ - config["archive_dir"] = archive_dir - for kind in ALL_FOUR: - config[kind] = target[kind] - - if "renewalparams" not in config: - config["renewalparams"] = {} - config.comments["renewalparams"] = ["", - "Options used in " - "the renewal process"] - - config["renewalparams"].update(relevant_data) - - for k in config["renewalparams"].keys(): - if k not in relevant_data: - del config["renewalparams"][k] - - if "renew_before_expiry" not in config: - default_interval = constants.RENEWER_DEFAULTS["renew_before_expiry"] - config.initial_comment = ["renew_before_expiry = " + default_interval] - - # TODO: add human-readable comments explaining other available - # parameters - logger.debug("Writing new config %s.", n_filename) - - # Ensure that the file exists - open(n_filename, 'a').close() - - # Copy permissions from the old version of the file, if it exists. - if os.path.exists(o_filename): - current_permissions = stat.S_IMODE(os.lstat(o_filename).st_mode) - os.chmod(n_filename, current_permissions) - - with open(n_filename, "wb") as f: - config.write(outfile=f) - return config - - -def rename_renewal_config(prev_name, new_name, cli_config): - """Renames cli_config.certname's config to cli_config.new_certname. - - :param .NamespaceConfig cli_config: parsed command line - arguments - """ - prev_filename = renewal_filename_for_lineagename(cli_config, prev_name) - new_filename = renewal_filename_for_lineagename(cli_config, new_name) - if os.path.exists(new_filename): - raise errors.ConfigurationError("The new certificate name " - "is already in use.") - try: - os.rename(prev_filename, new_filename) - except OSError: - raise errors.ConfigurationError("Please specify a valid filename " - "for the new certificate name.") - - -def update_configuration(lineagename, archive_dir, target, cli_config): - """Modifies lineagename's config to contain the specified values. - - :param str lineagename: Name of the lineage being modified - :param str archive_dir: Absolute path to the archive directory - :param dict target: Maps ALL_FOUR to their symlink paths - :param .NamespaceConfig cli_config: parsed command line - arguments - - :returns: Configuration object for the updated config file - :rtype: configobj.ConfigObj - - """ - config_filename = renewal_filename_for_lineagename(cli_config, lineagename) - temp_filename = config_filename + ".new" - - # If an existing tempfile exists, delete it - if os.path.exists(temp_filename): - os.unlink(temp_filename) - - # Save only the config items that are relevant to renewal - values = relevant_values(vars(cli_config.namespace)) - write_renewal_config(config_filename, temp_filename, archive_dir, target, values) - compat.os_rename(temp_filename, config_filename) - - return configobj.ConfigObj(config_filename) - - -def get_link_target(link): - """Get an absolute path to the target of link. - - :param str link: Path to a symbolic link - - :returns: Absolute path to the target of link - :rtype: str - - :raises .CertStorageError: If link does not exists. - - """ - try: - target = os.readlink(link) - except OSError: - raise errors.CertStorageError( - "Expected {0} to be a symlink".format(link)) - - if not os.path.isabs(target): - target = os.path.join(os.path.dirname(link), target) - return os.path.abspath(target) - -def _write_live_readme_to(readme_path, is_base_dir=False): - prefix = "" - if is_base_dir: - prefix = "[cert name]/" - with open(readme_path, "w") as f: - logger.debug("Writing README to %s.", readme_path) - f.write("This directory contains your keys and certificates.\n\n" - "`{prefix}privkey.pem` : the private key for your certificate.\n" - "`{prefix}fullchain.pem`: the certificate file used in most server software.\n" - "`{prefix}chain.pem` : used for OCSP stapling in Nginx >=1.3.7.\n" - "`{prefix}cert.pem` : will break many server configurations, and " - "should not be used\n" - " without reading further documentation (see link below).\n\n" - "WARNING: DO NOT MOVE OR RENAME THESE FILES!\n" - " Certbot expects these files to remain in this location in order\n" - " to function properly!\n\n" - "We recommend not moving these files. For more information, see the Certbot\n" - "User Guide at https://certbot.eff.org/docs/using.html#where-are-my-" - "certificates.\n".format(prefix=prefix)) - - -def _relevant(option): - """ - Is this option one that could be restored for future renewal purposes? - :param str option: the name of the option - - :rtype: bool - """ - from certbot import renewal - plugins = plugins_disco.PluginsRegistry.find_all() - namespaces = [plugins_common.dest_namespace(plugin) for plugin in plugins] - - return (option in renewal.CONFIG_ITEMS or - any(option.startswith(namespace) for namespace in namespaces)) - - -def relevant_values(all_values): - """Return a new dict containing only items relevant for renewal. - - :param dict all_values: The original values. - - :returns: A new dictionary containing items that can be used in renewal. - :rtype dict: - - """ - rv = dict( - (option, value) - for option, value in six.iteritems(all_values) - if _relevant(option) and cli.option_was_set(option, value)) - # We always save the server value to help with forward compatibility - # and behavioral consistency when versions of Certbot with different - # server defaults are used. - rv["server"] = all_values["server"] - return rv - -def lineagename_for_filename(config_filename): - """Returns the lineagename for a configuration filename. - """ - if not config_filename.endswith(".conf"): - raise errors.CertStorageError( - "renewal config file name must end in .conf") - return os.path.basename(config_filename[:-len(".conf")]) - -def renewal_filename_for_lineagename(config, lineagename): - """Returns the lineagename for a configuration filename. - """ - return os.path.join(config.renewal_configs_dir, lineagename) + ".conf" - -def _relpath_from_file(archive_dir, from_file): - """Path to a directory from a file""" - return os.path.relpath(archive_dir, os.path.dirname(from_file)) - -def full_archive_path(config_obj, cli_config, lineagename): - """Returns the full archive path for a lineagename - - Uses cli_config to determine archive path if not available from config_obj. - - :param configobj.ConfigObj config_obj: Renewal conf file contents (can be None) - :param configuration.NamespaceConfig cli_config: Main config file - :param str lineagename: Certificate name - """ - if config_obj and "archive_dir" in config_obj: - return config_obj["archive_dir"] - else: - return os.path.join(cli_config.default_archive_dir, lineagename) - -def _full_live_path(cli_config, lineagename): - """Returns the full default live path for a lineagename""" - return os.path.join(cli_config.live_dir, lineagename) - -def delete_files(config, certname): - """Delete all files related to the certificate. - - If some files are not found, ignore them and continue. - """ - renewal_filename = renewal_file_for_certname(config, certname) - # file exists - full_default_archive_dir = full_archive_path(None, config, certname) - full_default_live_dir = _full_live_path(config, certname) - try: - renewal_config = configobj.ConfigObj(renewal_filename) - except configobj.ConfigObjError: - # config is corrupted - logger.warning("Could not parse %s. You may wish to manually " - "delete the contents of %s and %s.", renewal_filename, - full_default_live_dir, full_default_archive_dir) - raise errors.CertStorageError( - "error parsing {0}".format(renewal_filename)) - finally: - # we couldn't read it, but let's at least delete it - # if this was going to fail, it already would have. - os.remove(renewal_filename) - logger.debug("Removed %s", renewal_filename) - - # cert files and (hopefully) live directory - # it's not guaranteed that the files are in our default storage - # structure. so, first delete the cert files. - directory_names = set() - for kind in ALL_FOUR: - link = renewal_config.get(kind) - try: - os.remove(link) - logger.debug("Removed %s", link) - except OSError: - logger.debug("Unable to delete %s", link) - directory = os.path.dirname(link) - directory_names.add(directory) - - # if all four were in the same directory, and the only thing left - # is the README file (or nothing), delete that directory. - # this will be wrong in very few but some cases. - if len(directory_names) == 1: - # delete the README file - directory = directory_names.pop() - readme_path = os.path.join(directory, README) - try: - os.remove(readme_path) - logger.debug("Removed %s", readme_path) - except OSError: - logger.debug("Unable to delete %s", readme_path) - # if it's now empty, delete the directory - try: - os.rmdir(directory) # only removes empty directories - logger.debug("Removed %s", directory) - except OSError: - logger.debug("Unable to remove %s; may not be empty.", directory) - - # archive directory - try: - archive_path = full_archive_path(renewal_config, config, certname) - shutil.rmtree(archive_path) - logger.debug("Removed %s", archive_path) - except OSError: - logger.debug("Unable to remove %s", archive_path) - - -class RenewableCert(object): - # pylint: disable=too-many-instance-attributes,too-many-public-methods - """Renewable certificate. - - Represents a lineage of certificates that is under the management of - Certbot, indicated by the existence of an associated renewal - configuration file. - - Note that the notion of "current version" for a lineage is - maintained on disk in the structure of symbolic links, and is not - explicitly stored in any instance variable in this object. The - RenewableCert object is able to determine information about the - current (or other) version by accessing data on disk, but does not - inherently know any of this information except by examining the - symbolic links as needed. The instance variables mentioned below - point to symlinks that reflect the notion of "current version" of - each managed object, and it is these paths that should be used when - configuring servers to use the certificate managed in a lineage. - These paths are normally within the "live" directory, and their - symlink targets -- the actual cert files -- are normally found - within the "archive" directory. - - :ivar str cert: The path to the symlink representing the current - version of the certificate managed by this lineage. - :ivar str privkey: The path to the symlink representing the current - version of the private key managed by this lineage. - :ivar str chain: The path to the symlink representing the current version - of the chain managed by this lineage. - :ivar str fullchain: The path to the symlink representing the - current version of the fullchain (combined chain and cert) - managed by this lineage. - :ivar configobj.ConfigObj configuration: The renewal configuration - options associated with this lineage, obtained from parsing the - renewal configuration file and/or systemwide defaults. - - """ - def __init__(self, config_filename, cli_config, update_symlinks=False): - """Instantiate a RenewableCert object from an existing lineage. - - :param str config_filename: the path to the renewal config file - that defines this lineage. - :param .NamespaceConfig: parsed command line arguments - - :raises .CertStorageError: if the configuration file's name didn't end - in ".conf", or the file is missing or broken. - - """ - self.cli_config = cli_config - self.lineagename = lineagename_for_filename(config_filename) - - # self.configuration should be used to read parameters that - # may have been chosen based on default values from the - # systemwide renewal configuration; self.configfile should be - # used to make and save changes. - try: - self.configfile = configobj.ConfigObj(config_filename) - except configobj.ConfigObjError: - raise errors.CertStorageError( - "error parsing {0}".format(config_filename)) - # TODO: Do we actually use anything from defaults and do we want to - # read further defaults from the systemwide renewal configuration - # file at this stage? - self.configuration = config_with_defaults(self.configfile) - - if not all(x in self.configuration for x in ALL_FOUR): - raise errors.CertStorageError( - "renewal config file {0} is missing a required " - "file reference".format(self.configfile)) - - conf_version = self.configuration.get("version") - if (conf_version is not None and - util.get_strict_version(conf_version) > CURRENT_VERSION): - logger.info( - "Attempting to parse the version %s renewal configuration " - "file found at %s with version %s of Certbot. This might not " - "work.", conf_version, config_filename, certbot.__version__) - - self.cert = self.configuration["cert"] - self.privkey = self.configuration["privkey"] - self.chain = self.configuration["chain"] - self.fullchain = self.configuration["fullchain"] - self.live_dir = os.path.dirname(self.cert) - - self._fix_symlinks() - if update_symlinks: - self._update_symlinks() - self._check_symlinks() - - @property - def key_path(self): - """Duck type for self.privkey""" - return self.privkey - - @property - def cert_path(self): - """Duck type for self.cert""" - return self.cert - - @property - def chain_path(self): - """Duck type for self.chain""" - return self.chain - - @property - def fullchain_path(self): - """Duck type for self.fullchain""" - return self.fullchain - - @property - def target_expiry(self): - """The current target certificate's expiration datetime - - :returns: Expiration datetime of the current target certificate - :rtype: :class:`datetime.datetime` - """ - return crypto_util.notAfter(self.current_target("cert")) - - @property - def archive_dir(self): - """Returns the default or specified archive directory""" - return full_archive_path(self.configuration, - self.cli_config, self.lineagename) - - def relative_archive_dir(self, from_file): - """Returns the default or specified archive directory as a relative path - - Used for creating symbolic links. - """ - return _relpath_from_file(self.archive_dir, from_file) - - @property - def is_test_cert(self): - """Returns true if this is a test cert from a staging server.""" - server = self.configuration["renewalparams"].get("server", None) - if server: - return util.is_staging(server) - else: - return False - - def _check_symlinks(self): - """Raises an exception if a symlink doesn't exist""" - for kind in ALL_FOUR: - link = getattr(self, kind) - if not os.path.islink(link): - raise errors.CertStorageError( - "expected {0} to be a symlink".format(link)) - target = get_link_target(link) - if not os.path.exists(target): - raise errors.CertStorageError("target {0} of symlink {1} does " - "not exist".format(target, link)) - - def _update_symlinks(self): - """Updates symlinks to use archive_dir""" - for kind in ALL_FOUR: - link = getattr(self, kind) - previous_link = get_link_target(link) - new_link = os.path.join(self.relative_archive_dir(link), - os.path.basename(previous_link)) - - os.unlink(link) - os.symlink(new_link, link) - - def _consistent(self): - """Are the files associated with this lineage self-consistent? - - :returns: Whether the files stored in connection with this - lineage appear to be correct and consistent with one - another. - :rtype: bool - - """ - # Each element must be referenced with an absolute path - for x in (self.cert, self.privkey, self.chain, self.fullchain): - if not os.path.isabs(x): - logger.debug("Element %s is not referenced with an " - "absolute path.", x) - return False - - # Each element must exist and be a symbolic link - for x in (self.cert, self.privkey, self.chain, self.fullchain): - if not os.path.islink(x): - logger.debug("Element %s is not a symbolic link.", x) - return False - for kind in ALL_FOUR: - link = getattr(self, kind) - target = get_link_target(link) - - # Each element's link must point within the cert lineage's - # directory within the official archive directory - if not os.path.samefile(os.path.dirname(target), self.archive_dir): - logger.debug("Element's link does not point within the " - "cert lineage's directory within the " - "official archive directory. Link: %s, " - "target directory: %s, " - "archive directory: %s. If you've specified " - "the archive directory in the renewal configuration " - "file, you may need to update links by running " - "certbot update_symlinks.", - link, os.path.dirname(target), self.archive_dir) - return False - - # The link must point to a file that exists - if not os.path.exists(target): - logger.debug("Link %s points to file %s that does not exist.", - link, target) - return False - - # The link must point to a file that follows the archive - # naming convention - pattern = re.compile(r"^{0}([0-9]+)\.pem$".format(kind)) - if not pattern.match(os.path.basename(target)): - logger.debug("%s does not follow the archive naming " - "convention.", target) - return False - - # It is NOT required that the link's target be a regular - # file (it may itself be a symlink). But we should probably - # do a recursive check that ultimately the target does - # exist? - # XXX: Additional possible consistency checks (e.g. - # cryptographic validation of the chain being a chain, - # the chain matching the cert, and the cert matching - # the subject key) - # XXX: All four of the targets are in the same directory - # (This check is redundant with the check that they - # are all in the desired directory!) - # len(set(os.path.basename(self.current_target(x) - # for x in ALL_FOUR))) == 1 - return True - - def _fix(self): - """Attempt to fix defects or inconsistencies in this lineage. - - .. todo:: Currently unimplemented. - - """ - # TODO: Figure out what kinds of fixes are possible. For - # example, checking if there is a valid version that - # we can update the symlinks to. (Maybe involve - # parsing keys and certs to see if they exist and - # if a key corresponds to the subject key of a cert?) - - # TODO: In general, the symlink-reading functions below are not - # cautious enough about the possibility that links or their - # targets may not exist. (This shouldn't happen, but might - # happen as a result of random tampering by a sysadmin, or - # filesystem errors, or crashes.) - - def _previous_symlinks(self): - """Returns the kind and path of all symlinks used in recovery. - - :returns: list of (kind, symlink) tuples - :rtype: list - - """ - previous_symlinks = [] - for kind in ALL_FOUR: - link_dir = os.path.dirname(getattr(self, kind)) - link_base = "previous_{0}.pem".format(kind) - previous_symlinks.append((kind, os.path.join(link_dir, link_base))) - - return previous_symlinks - - def _fix_symlinks(self): - """Fixes symlinks in the event of an incomplete version update. - - If there is no problem with the current symlinks, this function - has no effect. - - """ - previous_symlinks = self._previous_symlinks() - if all(os.path.exists(link[1]) for link in previous_symlinks): - for kind, previous_link in previous_symlinks: - current_link = getattr(self, kind) - if os.path.lexists(current_link): - os.unlink(current_link) - os.symlink(os.readlink(previous_link), current_link) - - for _, link in previous_symlinks: - if os.path.exists(link): - os.unlink(link) - - def current_target(self, kind): - """Returns full path to which the specified item currently points. - - :param str kind: the lineage member item ("cert", "privkey", - "chain", or "fullchain") - - :returns: The path to the current version of the specified - member. - :rtype: str or None - - """ - if kind not in ALL_FOUR: - raise errors.CertStorageError("unknown kind of item") - link = getattr(self, kind) - if not os.path.exists(link): - logger.debug("Expected symlink %s for %s does not exist.", - link, kind) - return None - return get_link_target(link) - - def current_version(self, kind): - """Returns numerical version of the specified item. - - For example, if kind is "chain" and the current chain link - points to a file named "chain7.pem", returns the integer 7. - - :param str kind: the lineage member item ("cert", "privkey", - "chain", or "fullchain") - - :returns: the current version of the specified member. - :rtype: int - - """ - if kind not in ALL_FOUR: - raise errors.CertStorageError("unknown kind of item") - pattern = re.compile(r"^{0}([0-9]+)\.pem$".format(kind)) - target = self.current_target(kind) - if target is None or not os.path.exists(target): - logger.debug("Current-version target for %s " - "does not exist at %s.", kind, target) - target = "" - matches = pattern.match(os.path.basename(target)) - if matches: - return int(matches.groups()[0]) - else: - logger.debug("No matches for target %s.", kind) - return None - - def version(self, kind, version): - """The filename that corresponds to the specified version and kind. - - .. warning:: The specified version may not exist in this - lineage. There is no guarantee that the file path returned - by this method actually exists. - - :param str kind: the lineage member item ("cert", "privkey", - "chain", or "fullchain") - :param int version: the desired version - - :returns: The path to the specified version of the specified member. - :rtype: str - - """ - if kind not in ALL_FOUR: - raise errors.CertStorageError("unknown kind of item") - where = os.path.dirname(self.current_target(kind)) - return os.path.join(where, "{0}{1}.pem".format(kind, version)) - - def available_versions(self, kind): - """Which alternative versions of the specified kind of item exist? - - The archive directory where the current version is stored is - consulted to obtain the list of alternatives. - - :param str kind: the lineage member item ( - ``cert``, ``privkey``, ``chain``, or ``fullchain``) - - :returns: all of the version numbers that currently exist - :rtype: `list` of `int` - - """ - if kind not in ALL_FOUR: - raise errors.CertStorageError("unknown kind of item") - where = os.path.dirname(self.current_target(kind)) - files = os.listdir(where) - pattern = re.compile(r"^{0}([0-9]+)\.pem$".format(kind)) - matches = [pattern.match(f) for f in files] - return sorted([int(m.groups()[0]) for m in matches if m]) - - def newest_available_version(self, kind): - """Newest available version of the specified kind of item? - - :param str kind: the lineage member item (``cert``, - ``privkey``, ``chain``, or ``fullchain``) - - :returns: the newest available version of this member - :rtype: int - - """ - return max(self.available_versions(kind)) - - def latest_common_version(self): - """Newest version for which all items are available? - - :returns: the newest available version for which all members - (``cert, ``privkey``, ``chain``, and ``fullchain``) exist - :rtype: int - - """ - # TODO: this can raise CertStorageError if there is no version overlap - # (it should probably return None instead) - # TODO: this can raise a spurious AttributeError if the current - # link for any kind is missing (it should probably return None) - versions = [self.available_versions(x) for x in ALL_FOUR] - return max(n for n in versions[0] if all(n in v for v in versions[1:])) - - def next_free_version(self): - """Smallest version newer than all full or partial versions? - - :returns: the smallest version number that is larger than any - version of any item currently stored in this lineage - :rtype: int - - """ - # TODO: consider locking/mutual exclusion between updating processes - # This isn't self.latest_common_version() + 1 because we don't want - # collide with a version that might exist for one file type but not - # for the others. - return max(self.newest_available_version(x) for x in ALL_FOUR) + 1 - - def ensure_deployed(self): - """Make sure we've deployed the latest version. - - :returns: False if a change was needed, True otherwise - :rtype: bool - - May need to recover from rare interrupted / crashed states.""" - - if self.has_pending_deployment(): - logger.warning("Found a new cert /archive/ that was not linked to in /live/; " - "fixing...") - self.update_all_links_to(self.latest_common_version()) - return False - return True - - - def has_pending_deployment(self): - """Is there a later version of all of the managed items? - - :returns: ``True`` if there is a complete version of this - lineage with a larger version number than the current - version, and ``False`` otherwise - :rtype: bool - - """ - # TODO: consider whether to assume consistency or treat - # inconsistent/consistent versions differently - smallest_current = min(self.current_version(x) for x in ALL_FOUR) - return smallest_current < self.latest_common_version() - - def _update_link_to(self, kind, version): - """Make the specified item point at the specified version. - - (Note that this method doesn't verify that the specified version - exists.) - - :param str kind: the lineage member item ("cert", "privkey", - "chain", or "fullchain") - :param int version: the desired version - - """ - if kind not in ALL_FOUR: - raise errors.CertStorageError("unknown kind of item") - link = getattr(self, kind) - filename = "{0}{1}.pem".format(kind, version) - # Relative rather than absolute target directory - target_directory = os.path.dirname(os.readlink(link)) - # TODO: it could be safer to make the link first under a temporary - # filename, then unlink the old link, then rename the new link - # to the old link; this ensures that this process is able to - # create symlinks. - # TODO: we might also want to check consistency of related links - # for the other corresponding items - os.unlink(link) - os.symlink(os.path.join(target_directory, filename), link) - - def update_all_links_to(self, version): - """Change all member objects to point to the specified version. - - :param int version: the desired version - - """ - with error_handler.ErrorHandler(self._fix_symlinks): - previous_links = self._previous_symlinks() - for kind, link in previous_links: - os.symlink(self.current_target(kind), link) - - for kind in ALL_FOUR: - self._update_link_to(kind, version) - - for _, link in previous_links: - os.unlink(link) - - def names(self, version=None): - """What are the subject names of this certificate? - - (If no version is specified, use the current version.) - - :param int version: the desired version number - :returns: the subject names - :rtype: `list` of `str` - :raises .CertStorageError: if could not find cert file. - - """ - if version is None: - target = self.current_target("cert") - else: - target = self.version("cert", version) - if target is None: - raise errors.CertStorageError("could not find cert file") - with open(target) as f: - return crypto_util.get_names_from_cert(f.read()) - - def autodeployment_is_enabled(self): - """Is automatic deployment enabled for this cert? - - If autodeploy is not specified, defaults to True. - - :returns: True if automatic deployment is enabled - :rtype: bool - - """ - return ("autodeploy" not in self.configuration or - self.configuration.as_bool("autodeploy")) - - def should_autodeploy(self, interactive=False): - """Should this lineage now automatically deploy a newer version? - - This is a policy question and does not only depend on whether - there is a newer version of the cert. (This considers whether - autodeployment is enabled, whether a relevant newer version - exists, and whether the time interval for autodeployment has - been reached.) - - :param bool interactive: set to True to examine the question - regardless of whether the renewal configuration allows - automated deployment (for interactive use). Default False. - - :returns: whether the lineage now ought to autodeploy an - existing newer cert version - :rtype: bool - - """ - if interactive or self.autodeployment_is_enabled(): - if self.has_pending_deployment(): - interval = self.configuration.get("deploy_before_expiry", - "5 days") - now = pytz.UTC.fromutc(datetime.datetime.utcnow()) - if self.target_expiry < add_time_interval(now, interval): - return True - return False - - def ocsp_revoked(self, version=None): - # pylint: disable=no-self-use,unused-argument - """Is the specified cert version revoked according to OCSP? - - Also returns True if the cert version is declared as intended - to be revoked according to Let's Encrypt OCSP extensions. - (If no version is specified, uses the current version.) - - This method is not yet implemented and currently always returns - False. - - :param int version: the desired version number - - :returns: whether the certificate is or will be revoked - :rtype: bool - - """ - # XXX: This query and its associated network service aren't - # implemented yet, so we currently return False (indicating that the - # certificate is not revoked). - return False - - def autorenewal_is_enabled(self): - """Is automatic renewal enabled for this cert? - - If autorenew is not specified, defaults to True. - - :returns: True if automatic renewal is enabled - :rtype: bool - - """ - return ("autorenew" not in self.configuration["renewalparams"] or - self.configuration["renewalparams"].as_bool("autorenew")) - - def should_autorenew(self): - """Should we now try to autorenew the most recent cert version? - - This is a policy question and does not only depend on whether - the cert is expired. (This considers whether autorenewal is - enabled, whether the cert is revoked, and whether the time - interval for autorenewal has been reached.) - - Note that this examines the numerically most recent cert version, - not the currently deployed version. - - :returns: whether an attempt should now be made to autorenew the - most current cert version in this lineage - :rtype: bool - - """ - if self.autorenewal_is_enabled(): - # Consider whether to attempt to autorenew this cert now - - # Renewals on the basis of revocation - if self.ocsp_revoked(self.latest_common_version()): - logger.debug("Should renew, certificate is revoked.") - return True - - # Renews some period before expiry time - default_interval = constants.RENEWER_DEFAULTS["renew_before_expiry"] - interval = self.configuration.get("renew_before_expiry", default_interval) - expiry = crypto_util.notAfter(self.version( - "cert", self.latest_common_version())) - now = pytz.UTC.fromutc(datetime.datetime.utcnow()) - if expiry < add_time_interval(now, interval): - logger.debug("Should renew, less than %s before certificate " - "expiry %s.", interval, - expiry.strftime("%Y-%m-%d %H:%M:%S %Z")) - return True - return False - - @classmethod - def new_lineage(cls, lineagename, cert, privkey, chain, cli_config): - # pylint: disable=too-many-locals - """Create a new certificate lineage. - - Attempts to create a certificate lineage -- enrolled for - potential future renewal -- with the (suggested) lineage name - lineagename, and the associated cert, privkey, and chain (the - associated fullchain will be created automatically). Optional - configurator and renewalparams record the configuration that was - originally used to obtain this cert, so that it can be reused - later during automated renewal. - - Returns a new RenewableCert object referring to the created - lineage. (The actual lineage name, as well as all the relevant - file paths, will be available within this object.) - - :param str lineagename: the suggested name for this lineage - (normally the current cert's first subject DNS name) - :param str cert: the initial certificate version in PEM format - :param str privkey: the private key in PEM format - :param str chain: the certificate chain in PEM format - :param .NamespaceConfig cli_config: parsed command line - arguments - - :returns: the newly-created RenewalCert object - :rtype: :class:`storage.renewableCert` - - """ - - # Examine the configuration and find the new lineage's name - for i in (cli_config.renewal_configs_dir, cli_config.default_archive_dir, - cli_config.live_dir): - if not os.path.exists(i): - os.makedirs(i, 0o700) - logger.debug("Creating directory %s.", i) - config_file, config_filename = util.unique_lineage_name( - cli_config.renewal_configs_dir, lineagename) - base_readme_path = os.path.join(cli_config.live_dir, README) - if not os.path.exists(base_readme_path): - _write_live_readme_to(base_readme_path, is_base_dir=True) - - # Determine where on disk everything will go - # lineagename will now potentially be modified based on which - # renewal configuration file could actually be created - lineagename = lineagename_for_filename(config_filename) - archive = full_archive_path(None, cli_config, lineagename) - live_dir = _full_live_path(cli_config, lineagename) - if os.path.exists(archive): - config_file.close() - raise errors.CertStorageError( - "archive directory exists for " + lineagename) - if os.path.exists(live_dir): - config_file.close() - raise errors.CertStorageError( - "live directory exists for " + lineagename) - os.mkdir(archive) - os.mkdir(live_dir) - logger.debug("Archive directory %s and live " - "directory %s created.", archive, live_dir) - - # Put the data into the appropriate files on disk - target = dict([(kind, os.path.join(live_dir, kind + ".pem")) - for kind in ALL_FOUR]) - archive_target = dict([(kind, os.path.join(archive, kind + "1.pem")) - for kind in ALL_FOUR]) - for kind in ALL_FOUR: - os.symlink(_relpath_from_file(archive_target[kind], target[kind]), target[kind]) - with open(target["cert"], "wb") as f: - logger.debug("Writing certificate to %s.", target["cert"]) - f.write(cert) - with util.safe_open(archive_target["privkey"], "wb", chmod=BASE_PRIVKEY_MODE) as f: - logger.debug("Writing private key to %s.", target["privkey"]) - f.write(privkey) - # XXX: Let's make sure to get the file permissions right here - with open(target["chain"], "wb") as f: - logger.debug("Writing chain to %s.", target["chain"]) - f.write(chain) - with open(target["fullchain"], "wb") as f: - # assumes that OpenSSL.crypto.dump_certificate includes - # ending newline character - logger.debug("Writing full chain to %s.", target["fullchain"]) - f.write(cert + chain) - - # Write a README file to the live directory - readme_path = os.path.join(live_dir, README) - _write_live_readme_to(readme_path) - - # Document what we've done in a new renewal config file - config_file.close() - - # Save only the config items that are relevant to renewal - values = relevant_values(vars(cli_config.namespace)) - - new_config = write_renewal_config(config_filename, config_filename, archive, - target, values) - return cls(new_config.filename, cli_config) - - def save_successor(self, prior_version, new_cert, - new_privkey, new_chain, cli_config): - """Save new cert and chain as a successor of a prior version. - - Returns the new version number that was created. - - .. note:: this function does NOT update links to deploy this - version - - :param int prior_version: the old version to which this version - is regarded as a successor (used to choose a privkey, if the - key has not changed, but otherwise this information is not - permanently recorded anywhere) - :param bytes new_cert: the new certificate, in PEM format - :param bytes new_privkey: the new private key, in PEM format, - or ``None``, if the private key has not changed - :param bytes new_chain: the new chain, in PEM format - :param .NamespaceConfig cli_config: parsed command line - arguments - - :returns: the new version number that was created - :rtype: int - - """ - # XXX: assumes official archive location rather than examining links - # XXX: consider using os.open for availability of os.O_EXCL - # XXX: ensure file permissions are correct; also create directories - # if needed (ensuring their permissions are correct) - # Figure out what the new version is and hence where to save things - - self.cli_config = cli_config - target_version = self.next_free_version() - target = dict( - [(kind, - os.path.join(self.archive_dir, "{0}{1}.pem".format(kind, target_version))) - for kind in ALL_FOUR]) - - old_privkey = os.path.join( - self.archive_dir, "privkey{0}.pem".format(prior_version)) - - # Distinguish the cases where the privkey has changed and where it - # has not changed (in the latter case, making an appropriate symlink - # to an earlier privkey version) - if new_privkey is None: - # The behavior below keeps the prior key by creating a new - # symlink to the old key or the target of the old key symlink. - if os.path.islink(old_privkey): - old_privkey = os.readlink(old_privkey) - else: - old_privkey = "privkey{0}.pem".format(prior_version) - logger.debug("Writing symlink to old private key, %s.", old_privkey) - os.symlink(old_privkey, target["privkey"]) - else: - with util.safe_open(target["privkey"], "wb", chmod=BASE_PRIVKEY_MODE) as f: - logger.debug("Writing new private key to %s.", target["privkey"]) - f.write(new_privkey) - # Preserve gid and (mode & 074) from previous privkey in this lineage. - old_mode = stat.S_IMODE(os.stat(old_privkey).st_mode) & \ - (stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP | \ - stat.S_IROTH) - mode = BASE_PRIVKEY_MODE | old_mode - os.chown(target["privkey"], -1, os.stat(old_privkey).st_gid) - os.chmod(target["privkey"], mode) - - # Save everything else - with open(target["cert"], "wb") as f: - logger.debug("Writing certificate to %s.", target["cert"]) - f.write(new_cert) - with open(target["chain"], "wb") as f: - logger.debug("Writing chain to %s.", target["chain"]) - f.write(new_chain) - with open(target["fullchain"], "wb") as f: - logger.debug("Writing full chain to %s.", target["fullchain"]) - f.write(new_cert + new_chain) - - symlinks = dict((kind, self.configuration[kind]) for kind in ALL_FOUR) - # Update renewal config file - self.configfile = update_configuration( - self.lineagename, self.archive_dir, symlinks, cli_config) - self.configuration = config_with_defaults(self.configfile) - - return target_version diff --git a/certbot/tests/__init__.py b/certbot/tests/__init__.py deleted file mode 100644 index 2f4d6e07ce2..00000000000 --- a/certbot/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Certbot Tests""" diff --git a/certbot/tests/account_test.py b/certbot/tests/account_test.py deleted file mode 100644 index 278b0c54576..00000000000 --- a/certbot/tests/account_test.py +++ /dev/null @@ -1,342 +0,0 @@ -"""Tests for certbot.account.""" -import datetime -import json -import os -import shutil -import stat -import unittest - -import josepy as jose -import mock -import pytz - -from acme import messages - -from certbot import errors - -import certbot.tests.util as test_util - - -KEY = jose.JWKRSA.load(test_util.load_vector("rsa512_key.pem")) - - -class AccountTest(unittest.TestCase): - """Tests for certbot.account.Account.""" - - def setUp(self): - from certbot.account import Account - self.regr = mock.MagicMock() - self.meta = Account.Meta( - creation_host="test.certbot.org", - creation_dt=datetime.datetime( - 2015, 7, 4, 14, 4, 10, tzinfo=pytz.UTC)) - self.acc = Account(self.regr, KEY, self.meta) - self.regr.__repr__ = mock.MagicMock(return_value="i_am_a_regr") - - with mock.patch("certbot.account.socket") as mock_socket: - mock_socket.getfqdn.return_value = "test.certbot.org" - with mock.patch("certbot.account.datetime") as mock_dt: - mock_dt.datetime.now.return_value = self.meta.creation_dt - self.acc_no_meta = Account(self.regr, KEY) - - def test_init(self): - self.assertEqual(self.regr, self.acc.regr) - self.assertEqual(KEY, self.acc.key) - self.assertEqual(self.meta, self.acc_no_meta.meta) - - def test_id(self): - self.assertEqual( - self.acc.id, "7adac10320f585ddf118429c0c4af2cd") - - def test_slug(self): - self.assertEqual( - self.acc.slug, "test.certbot.org@2015-07-04T14:04:10Z (7ada)") - - def test_repr(self): - self.assertTrue(repr(self.acc).startswith( - " 0) - - def test_view_config_changes_bad_backups_dir(self): - # There shouldn't be any "in progress directories when this is called - # It must just be clean checkpoints - os.makedirs(os.path.join(self.config.backup_dir, "in_progress")) - - self.assertRaises( - errors.ReverterError, self.reverter.view_config_changes) - - def test_view_config_changes_for_logging(self): - self._setup_three_checkpoints() - - config_changes = self.reverter.view_config_changes(for_logging=True) - - self.assertTrue("First Checkpoint" in config_changes) - self.assertTrue("Second Checkpoint" in config_changes) - self.assertTrue("Third Checkpoint" in config_changes) - - def _setup_three_checkpoints(self): - """Generate some finalized checkpoints.""" - # Checkpoint1 - config1 - self.reverter.add_to_checkpoint(self.sets[0], "first save") - self.reverter.finalize_checkpoint("First Checkpoint") - - update_file(self.config1, "update config1") - - # Checkpoint2 - new file config3, update config2 - config3 = os.path.join(self.dir1, "config3.txt") - self.reverter.register_file_creation(False, config3) - update_file(config3, "directive-config3") - self.reverter.add_to_checkpoint(self.sets[1], "second save") - self.reverter.finalize_checkpoint("Second Checkpoint") - - update_file(self.config2, "update config2") - update_file(config3, "update config3") - - # Checkpoint3 - update config1, config2 - self.reverter.add_to_checkpoint(self.sets[2], "third save") - self.reverter.finalize_checkpoint("Third Checkpoint - Save both") - - update_file(self.config1, "Final form config1") - update_file(self.config2, "Final form config2") - update_file(config3, "Final form config3") - - return config3 - - -def setup_test_files(): - """Setup sample configuration files.""" - dir1 = tempfile.mkdtemp("dir1") - dir2 = tempfile.mkdtemp("dir2") - config1 = os.path.join(dir1, "config.txt") - config2 = os.path.join(dir2, "config.txt") - with open(config1, "w") as file_fd: - file_fd.write("directive-dir1") - with open(config2, "w") as file_fd: - file_fd.write("directive-dir2") - - sets = [set([config1]), - set([config2]), - set([config1, config2])] - - return config1, config2, dir1, dir2, sets - - -def get_save_notes(dire): - """Read save notes""" - return read_in(os.path.join(dire, "CHANGES_SINCE")) - - -def get_filepaths(dire): - """Get Filepaths""" - return read_in(os.path.join(dire, "FILEPATHS")) - - -def get_new_files(dire): - """Get new files.""" - return read_in(os.path.join(dire, "NEW_FILES")).splitlines() - - -def get_undo_commands(dire): - """Get new files.""" - with open(os.path.join(dire, "COMMANDS")) as csvfile: - return list(csv.reader(csvfile)) - - -def read_in(path): - """Read in a file, return the str""" - with open(path, "r") as file_fd: - return file_fd.read() - - -def update_file(filename, string): - """Update a file with a new value.""" - with open(filename, "w") as file_fd: - file_fd.write(string) - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/tests/storage_test.py b/certbot/tests/storage_test.py deleted file mode 100644 index d75f4f59593..00000000000 --- a/certbot/tests/storage_test.py +++ /dev/null @@ -1,980 +0,0 @@ -"""Tests for certbot.storage.""" -# pylint disable=protected-access -import datetime -import os -import shutil -import stat -import unittest - -import configobj -import mock -import pytz -import six - -import certbot -from certbot import cli -from certbot import compat -from certbot import errors -from certbot.storage import ALL_FOUR - -import certbot.tests.util as test_util - - -CERT = test_util.load_cert('cert_512.pem') - - - -def unlink_all(rc_object): - """Unlink all four items associated with this RenewableCert.""" - for kind in ALL_FOUR: - os.unlink(getattr(rc_object, kind)) - - -def fill_with_sample_data(rc_object): - """Put dummy data into all four files of this RenewableCert.""" - for kind in ALL_FOUR: - with open(getattr(rc_object, kind), "w") as f: - f.write(kind) - - -class BaseRenewableCertTest(test_util.ConfigTestCase): - """Base class for setting up Renewable Cert tests. - - .. note:: It may be required to write out self.config for - your test. Check :class:`.cli_test.DuplicateCertTest` for an example. - - """ - - def setUp(self): - from certbot import storage - - super(BaseRenewableCertTest, self).setUp() - - # TODO: maybe provide NamespaceConfig.make_dirs? - # TODO: main() should create those dirs, c.f. #902 - os.makedirs(os.path.join(self.config.config_dir, "live", "example.org")) - archive_path = os.path.join(self.config.config_dir, "archive", "example.org") - os.makedirs(archive_path) - os.makedirs(os.path.join(self.config.config_dir, "renewal")) - - config_file = configobj.ConfigObj() - for kind in ALL_FOUR: - kind_path = os.path.join(self.config.config_dir, "live", "example.org", - kind + ".pem") - config_file[kind] = kind_path - with open(os.path.join(self.config.config_dir, "live", "example.org", - "README"), 'a'): - pass - config_file["archive"] = archive_path - config_file.filename = os.path.join(self.config.config_dir, "renewal", - "example.org.conf") - config_file.write() - self.config_file = config_file - - # We also create a file that isn't a renewal config in the same - # location to test that logic that reads in all-and-only renewal - # configs will ignore it and NOT attempt to parse it. - with open(os.path.join(self.config.config_dir, "renewal", "IGNORE.THIS"), "w") as junk: - junk.write("This file should be ignored!") - - self.defaults = configobj.ConfigObj() - - with mock.patch("certbot.storage.RenewableCert._check_symlinks") as check: - check.return_value = True - self.test_rc = storage.RenewableCert(config_file.filename, self.config) - - def _write_out_kind(self, kind, ver, value=None): - link = getattr(self.test_rc, kind) - if os.path.lexists(link): - os.unlink(link) - os.symlink(os.path.join(os.path.pardir, os.path.pardir, "archive", - "example.org", "{0}{1}.pem".format(kind, ver)), - link) - with open(link, "wb") as f: - f.write(kind.encode('ascii') if value is None else value) - if kind == "privkey": - os.chmod(link, 0o600) - - def _write_out_ex_kinds(self): - for kind in ALL_FOUR: - self._write_out_kind(kind, 12) - self._write_out_kind(kind, 11) - - -class RenewableCertTests(BaseRenewableCertTest): - # pylint: disable=too-many-public-methods - """Tests for certbot.storage.""" - - def test_initialization(self): - self.assertEqual(self.test_rc.lineagename, "example.org") - for kind in ALL_FOUR: - self.assertEqual( - getattr(self.test_rc, kind), os.path.join( - self.config.config_dir, "live", "example.org", kind + ".pem")) - - def test_renewal_bad_config(self): - """Test that the RenewableCert constructor will complain if - the renewal configuration file doesn't end in ".conf" - - """ - from certbot import storage - broken = os.path.join(self.config.config_dir, "broken.conf") - with open(broken, "w") as f: - f.write("[No closing bracket for you!") - self.assertRaises(errors.CertStorageError, storage.RenewableCert, - broken, self.config) - os.unlink(broken) - self.assertRaises(errors.CertStorageError, storage.RenewableCert, - "fun", self.config) - - def test_renewal_incomplete_config(self): - """Test that the RenewableCert constructor will complain if - the renewal configuration file is missing a required file element.""" - from certbot import storage - config = configobj.ConfigObj() - config["cert"] = "imaginary_cert.pem" - # Here the required privkey is missing. - config["chain"] = "imaginary_chain.pem" - config["fullchain"] = "imaginary_fullchain.pem" - config.filename = os.path.join(self.config.config_dir, "imaginary_config.conf") - config.write() - self.assertRaises(errors.CertStorageError, storage.RenewableCert, - config.filename, self.config) - - def test_no_renewal_version(self): - from certbot import storage - - self._write_out_ex_kinds() - self.assertTrue("version" not in self.config_file) - - with mock.patch("certbot.storage.logger") as mock_logger: - storage.RenewableCert(self.config_file.filename, self.config) - self.assertFalse(mock_logger.warning.called) - - def test_renewal_newer_version(self): - from certbot import storage - - self._write_out_ex_kinds() - self.config_file["version"] = "99.99.99" - self.config_file.write() - - with mock.patch("certbot.storage.logger") as mock_logger: - storage.RenewableCert(self.config_file.filename, self.config) - self.assertTrue(mock_logger.info.called) - self.assertTrue("version" in mock_logger.info.call_args[0][0]) - - def test_consistent(self): - # pylint: disable=too-many-statements,protected-access - oldcert = self.test_rc.cert - self.test_rc.cert = "relative/path" - # Absolute path for item requirement - self.assertFalse(self.test_rc._consistent()) - self.test_rc.cert = oldcert - # Items must exist requirement - self.assertFalse(self.test_rc._consistent()) - # Items must be symlinks requirements - fill_with_sample_data(self.test_rc) - self.assertFalse(self.test_rc._consistent()) - unlink_all(self.test_rc) - # Items must point to desired place if they are relative - for kind in ALL_FOUR: - os.symlink(os.path.join("..", kind + "17.pem"), - getattr(self.test_rc, kind)) - self.assertFalse(self.test_rc._consistent()) - unlink_all(self.test_rc) - # Items must point to desired place if they are absolute - for kind in ALL_FOUR: - os.symlink(os.path.join(self.config.config_dir, kind + "17.pem"), - getattr(self.test_rc, kind)) - self.assertFalse(self.test_rc._consistent()) - unlink_all(self.test_rc) - # Items must point to things that exist - for kind in ALL_FOUR: - os.symlink(os.path.join("..", "..", "archive", "example.org", - kind + "17.pem"), - getattr(self.test_rc, kind)) - self.assertFalse(self.test_rc._consistent()) - # This version should work - fill_with_sample_data(self.test_rc) - self.assertTrue(self.test_rc._consistent()) - # Items must point to things that follow the naming convention - os.unlink(self.test_rc.fullchain) - os.symlink(os.path.join("..", "..", "archive", "example.org", - "fullchain_17.pem"), self.test_rc.fullchain) - with open(self.test_rc.fullchain, "w") as f: - f.write("wrongly-named fullchain") - self.assertFalse(self.test_rc._consistent()) - - def test_current_target(self): - # Relative path logic - self._write_out_kind("cert", 17) - self.assertTrue(os.path.samefile(self.test_rc.current_target("cert"), - os.path.join(self.config.config_dir, "archive", - "example.org", - "cert17.pem"))) - # Absolute path logic - os.unlink(self.test_rc.cert) - os.symlink(os.path.join(self.config.config_dir, "archive", "example.org", - "cert17.pem"), self.test_rc.cert) - with open(self.test_rc.cert, "w") as f: - f.write("cert") - self.assertTrue(os.path.samefile(self.test_rc.current_target("cert"), - os.path.join(self.config.config_dir, "archive", - "example.org", - "cert17.pem"))) - - def test_current_version(self): - for ver in (1, 5, 10, 20): - self._write_out_kind("cert", ver) - os.unlink(self.test_rc.cert) - os.symlink(os.path.join("..", "..", "archive", "example.org", - "cert10.pem"), self.test_rc.cert) - self.assertEqual(self.test_rc.current_version("cert"), 10) - - def test_no_current_version(self): - self.assertEqual(self.test_rc.current_version("cert"), None) - - def test_latest_and_next_versions(self): - for ver in six.moves.range(1, 6): - for kind in ALL_FOUR: - self._write_out_kind(kind, ver) - self.assertEqual(self.test_rc.latest_common_version(), 5) - self.assertEqual(self.test_rc.next_free_version(), 6) - # Having one kind of file of a later version doesn't change the - # result - self._write_out_kind("privkey", 7) - self.assertEqual(self.test_rc.latest_common_version(), 5) - # ... although it does change the next free version - self.assertEqual(self.test_rc.next_free_version(), 8) - # Nor does having three out of four change the result - self._write_out_kind("cert", 7) - self._write_out_kind("fullchain", 7) - self.assertEqual(self.test_rc.latest_common_version(), 5) - # If we have everything from a much later version, it does change - # the result - for kind in ALL_FOUR: - self._write_out_kind(kind, 17) - self.assertEqual(self.test_rc.latest_common_version(), 17) - self.assertEqual(self.test_rc.next_free_version(), 18) - - @mock.patch("certbot.storage.logger") - def test_ensure_deployed(self, mock_logger): - mock_update = self.test_rc.update_all_links_to = mock.Mock() - mock_has_pending = self.test_rc.has_pending_deployment = mock.Mock() - self.test_rc.latest_common_version = mock.Mock() - - mock_has_pending.return_value = False - self.assertEqual(self.test_rc.ensure_deployed(), True) - self.assertEqual(mock_update.call_count, 0) - self.assertEqual(mock_logger.warning.call_count, 0) - - mock_has_pending.return_value = True - self.assertEqual(self.test_rc.ensure_deployed(), False) - self.assertEqual(mock_update.call_count, 1) - self.assertEqual(mock_logger.warning.call_count, 1) - - - def test_update_link_to(self): - for ver in six.moves.range(1, 6): - for kind in ALL_FOUR: - self._write_out_kind(kind, ver) - self.assertEqual(ver, self.test_rc.current_version(kind)) - # pylint: disable=protected-access - self.test_rc._update_link_to("cert", 3) - self.test_rc._update_link_to("privkey", 2) - self.assertEqual(3, self.test_rc.current_version("cert")) - self.assertEqual(2, self.test_rc.current_version("privkey")) - self.assertEqual(5, self.test_rc.current_version("chain")) - self.assertEqual(5, self.test_rc.current_version("fullchain")) - # Currently we are allowed to update to a version that doesn't exist - self.test_rc._update_link_to("chain", 3000) - # However, current_version doesn't allow querying the resulting - # version (because it's a broken link). - self.assertEqual(os.path.basename(os.readlink(self.test_rc.chain)), - "chain3000.pem") - - def test_version(self): - self._write_out_kind("cert", 12) - # TODO: We should probably test that the directory is still the - # same, but it's tricky because we can get an absolute - # path out when we put a relative path in. - self.assertEqual("cert8.pem", - os.path.basename(self.test_rc.version("cert", 8))) - - def test_update_all_links_to_success(self): - for ver in six.moves.range(1, 6): - for kind in ALL_FOUR: - self._write_out_kind(kind, ver) - self.assertEqual(ver, self.test_rc.current_version(kind)) - self.assertEqual(self.test_rc.latest_common_version(), 5) - for ver in six.moves.range(1, 6): - self.test_rc.update_all_links_to(ver) - for kind in ALL_FOUR: - self.assertEqual(ver, self.test_rc.current_version(kind)) - self.assertEqual(self.test_rc.latest_common_version(), 5) - - def test_update_all_links_to_partial_failure(self): - def unlink_or_raise(path, real_unlink=os.unlink): - # pylint: disable=missing-docstring - basename = os.path.basename(path) - if "fullchain" in basename and basename.startswith("prev"): - raise ValueError - else: - real_unlink(path) - - self._write_out_ex_kinds() - with mock.patch("certbot.storage.os.unlink") as mock_unlink: - mock_unlink.side_effect = unlink_or_raise - self.assertRaises(ValueError, self.test_rc.update_all_links_to, 12) - - for kind in ALL_FOUR: - self.assertEqual(self.test_rc.current_version(kind), 12) - - def test_update_all_links_to_full_failure(self): - def unlink_or_raise(path, real_unlink=os.unlink): - # pylint: disable=missing-docstring - if "fullchain" in os.path.basename(path): - raise ValueError - else: - real_unlink(path) - - self._write_out_ex_kinds() - with mock.patch("certbot.storage.os.unlink") as mock_unlink: - mock_unlink.side_effect = unlink_or_raise - self.assertRaises(ValueError, self.test_rc.update_all_links_to, 12) - - for kind in ALL_FOUR: - self.assertEqual(self.test_rc.current_version(kind), 11) - - def test_has_pending_deployment(self): - for ver in six.moves.range(1, 6): - for kind in ALL_FOUR: - self._write_out_kind(kind, ver) - self.assertEqual(ver, self.test_rc.current_version(kind)) - for ver in six.moves.range(1, 6): - self.test_rc.update_all_links_to(ver) - for kind in ALL_FOUR: - self.assertEqual(ver, self.test_rc.current_version(kind)) - if ver < 5: - self.assertTrue(self.test_rc.has_pending_deployment()) - else: - self.assertFalse(self.test_rc.has_pending_deployment()) - - def test_names(self): - # Trying the current version - self._write_out_kind("cert", 12, test_util.load_vector("cert-san_512.pem")) - - self.assertEqual(self.test_rc.names(), - ["example.com", "www.example.com"]) - - # Trying a non-current version - self._write_out_kind("cert", 15, test_util.load_vector("cert_512.pem")) - - self.assertEqual(self.test_rc.names(12), - ["example.com", "www.example.com"]) - - # Testing common name is listed first - self._write_out_kind( - "cert", 12, test_util.load_vector("cert-5sans_512.pem")) - - self.assertEqual( - self.test_rc.names(12), - ["example.com"] + ["{0}.example.com".format(c) for c in "abcd"]) - - # Trying missing cert - os.unlink(self.test_rc.cert) - self.assertRaises(errors.CertStorageError, self.test_rc.names) - - @mock.patch("certbot.storage.cli") - @mock.patch("certbot.storage.datetime") - def test_time_interval_judgments(self, mock_datetime, mock_cli): - """Test should_autodeploy() and should_autorenew() on the basis - of expiry time windows.""" - test_cert = test_util.load_vector("cert_512.pem") - - self._write_out_ex_kinds() - - self.test_rc.update_all_links_to(12) - with open(self.test_rc.cert, "wb") as f: - f.write(test_cert) - self.test_rc.update_all_links_to(11) - with open(self.test_rc.cert, "wb") as f: - f.write(test_cert) - - mock_datetime.timedelta = datetime.timedelta - mock_cli.set_by_cli.return_value = False - self.test_rc.configuration["renewalparams"] = {} - - for (current_time, interval, result) in [ - # 2014-12-13 12:00:00+00:00 (about 5 days prior to expiry) - # Times that should result in autorenewal/autodeployment - (1418472000, "2 months", True), (1418472000, "1 week", True), - # Times that should not - (1418472000, "4 days", False), (1418472000, "2 days", False), - # 2009-05-01 12:00:00+00:00 (about 5 years prior to expiry) - # Times that should result in autorenewal/autodeployment - (1241179200, "7 years", True), - (1241179200, "11 years 2 months", True), - # Times that should not - (1241179200, "8 hours", False), (1241179200, "2 days", False), - (1241179200, "40 days", False), (1241179200, "9 months", False), - # 2015-01-01 (after expiry has already happened, so all - # intervals should cause autorenewal/autodeployment) - (1420070400, "0 seconds", True), - (1420070400, "10 seconds", True), - (1420070400, "10 minutes", True), - (1420070400, "10 weeks", True), (1420070400, "10 months", True), - (1420070400, "10 years", True), (1420070400, "99 months", True), - ]: - sometime = datetime.datetime.utcfromtimestamp(current_time) - mock_datetime.datetime.utcnow.return_value = sometime - self.test_rc.configuration["deploy_before_expiry"] = interval - self.test_rc.configuration["renew_before_expiry"] = interval - self.assertEqual(self.test_rc.should_autodeploy(), result) - self.assertEqual(self.test_rc.should_autorenew(), result) - - def test_autodeployment_is_enabled(self): - self.assertTrue(self.test_rc.autodeployment_is_enabled()) - self.test_rc.configuration["autodeploy"] = "1" - self.assertTrue(self.test_rc.autodeployment_is_enabled()) - - self.test_rc.configuration["autodeploy"] = "0" - self.assertFalse(self.test_rc.autodeployment_is_enabled()) - - def test_should_autodeploy(self): - """Test should_autodeploy() on the basis of reasons other than - expiry time window.""" - # pylint: disable=too-many-statements - # Autodeployment turned off - self.test_rc.configuration["autodeploy"] = "0" - self.assertFalse(self.test_rc.should_autodeploy()) - self.test_rc.configuration["autodeploy"] = "1" - # No pending deployment - for ver in six.moves.range(1, 6): - for kind in ALL_FOUR: - self._write_out_kind(kind, ver) - self.assertFalse(self.test_rc.should_autodeploy()) - - def test_autorenewal_is_enabled(self): - self.test_rc.configuration["renewalparams"] = {} - self.assertTrue(self.test_rc.autorenewal_is_enabled()) - self.test_rc.configuration["renewalparams"]["autorenew"] = "True" - self.assertTrue(self.test_rc.autorenewal_is_enabled()) - - self.test_rc.configuration["renewalparams"]["autorenew"] = "False" - self.assertFalse(self.test_rc.autorenewal_is_enabled()) - - @mock.patch("certbot.storage.cli") - @mock.patch("certbot.storage.RenewableCert.ocsp_revoked") - def test_should_autorenew(self, mock_ocsp, mock_cli): - """Test should_autorenew on the basis of reasons other than - expiry time window.""" - # pylint: disable=too-many-statements - mock_cli.set_by_cli.return_value = False - # Autorenewal turned off - self.test_rc.configuration["renewalparams"] = {"autorenew": "False"} - self.assertFalse(self.test_rc.should_autorenew()) - self.test_rc.configuration["renewalparams"]["autorenew"] = "True" - for kind in ALL_FOUR: - self._write_out_kind(kind, 12) - # Mandatory renewal on the basis of OCSP revocation - mock_ocsp.return_value = True - self.assertTrue(self.test_rc.should_autorenew()) - mock_ocsp.return_value = False - - @test_util.broken_on_windows - @mock.patch("certbot.storage.relevant_values") - def test_save_successor(self, mock_rv): - # Mock relevant_values() to claim that all values are relevant here - # (to avoid instantiating parser) - mock_rv.side_effect = lambda x: x - - for ver in six.moves.range(1, 6): - for kind in ALL_FOUR: - self._write_out_kind(kind, ver) - self.test_rc.update_all_links_to(3) - self.assertEqual( - 6, self.test_rc.save_successor(3, b'new cert', None, - b'new chain', self.config)) - with open(self.test_rc.version("cert", 6)) as f: - self.assertEqual(f.read(), "new cert") - with open(self.test_rc.version("chain", 6)) as f: - self.assertEqual(f.read(), "new chain") - with open(self.test_rc.version("fullchain", 6)) as f: - self.assertEqual(f.read(), "new cert" + "new chain") - # version 6 of the key should be a link back to version 3 - self.assertFalse(os.path.islink(self.test_rc.version("privkey", 3))) - self.assertTrue(os.path.islink(self.test_rc.version("privkey", 6))) - # Let's try two more updates - self.assertEqual( - 7, self.test_rc.save_successor(6, b'again', None, - b'newer chain', self.config)) - self.assertEqual( - 8, self.test_rc.save_successor(7, b'hello', None, - b'other chain', self.config)) - # All of the subsequent versions should link directly to the original - # privkey. - for i in (6, 7, 8): - self.assertTrue(os.path.islink(self.test_rc.version("privkey", i))) - self.assertEqual("privkey3.pem", os.path.basename(os.readlink( - self.test_rc.version("privkey", i)))) - - for kind in ALL_FOUR: - self.assertEqual(self.test_rc.available_versions(kind), list(six.moves.range(1, 9))) - self.assertEqual(self.test_rc.current_version(kind), 3) - # Test updating from latest version rather than old version - self.test_rc.update_all_links_to(8) - self.assertEqual( - 9, self.test_rc.save_successor(8, b'last', None, - b'attempt', self.config)) - for kind in ALL_FOUR: - self.assertEqual(self.test_rc.available_versions(kind), - list(six.moves.range(1, 10))) - self.assertEqual(self.test_rc.current_version(kind), 8) - with open(self.test_rc.version("fullchain", 9)) as f: - self.assertEqual(f.read(), "last" + "attempt") - temp_config_file = os.path.join(self.config.renewal_configs_dir, - self.test_rc.lineagename) + ".conf.new" - with open(temp_config_file, "w") as f: - f.write("We previously crashed while writing me :(") - # Test updating when providing a new privkey. The key should - # be saved in a new file rather than creating a new symlink. - self.assertEqual( - 10, self.test_rc.save_successor(9, b'with', b'a', - b'key', self.config)) - self.assertTrue(os.path.exists(self.test_rc.version("privkey", 10))) - self.assertFalse(os.path.islink(self.test_rc.version("privkey", 10))) - self.assertFalse(os.path.exists(temp_config_file)) - - @test_util.broken_on_windows - @mock.patch("certbot.storage.relevant_values") - def test_save_successor_maintains_group_mode(self, mock_rv): - # Mock relevant_values() to claim that all values are relevant here - # (to avoid instantiating parser) - mock_rv.side_effect = lambda x: x - for kind in ALL_FOUR: - self._write_out_kind(kind, 1) - self.test_rc.update_all_links_to(1) - self.assertTrue(compat.compare_file_modes( - os.stat(self.test_rc.version("privkey", 1)).st_mode, 0o600)) - os.chmod(self.test_rc.version("privkey", 1), 0o444) - # If no new key, permissions should be the same (we didn't write any keys) - self.test_rc.save_successor(1, b"newcert", None, b"new chain", self.config) - self.assertTrue(compat.compare_file_modes( - os.stat(self.test_rc.version("privkey", 2)).st_mode, 0o444)) - # If new key, permissions should be kept as 644 - self.test_rc.save_successor(2, b"newcert", b"new_privkey", b"new chain", self.config) - self.assertTrue(compat.compare_file_modes( - os.stat(self.test_rc.version("privkey", 3)).st_mode, 0o644)) - # If permissions reverted, next renewal will also revert permissions of new key - os.chmod(self.test_rc.version("privkey", 3), 0o400) - self.test_rc.save_successor(3, b"newcert", b"new_privkey", b"new chain", self.config) - self.assertTrue(compat.compare_file_modes( - os.stat(self.test_rc.version("privkey", 4)).st_mode, 0o600)) - - @test_util.broken_on_windows - @mock.patch("certbot.storage.relevant_values") - @mock.patch("certbot.storage.os.chown") - def test_save_successor_maintains_gid(self, mock_chown, mock_rv): - # Mock relevant_values() to claim that all values are relevant here - # (to avoid instantiating parser) - mock_rv.side_effect = lambda x: x - for kind in ALL_FOUR: - self._write_out_kind(kind, 1) - self.test_rc.update_all_links_to(1) - self.test_rc.save_successor(1, b"newcert", None, b"new chain", self.config) - self.assertFalse(mock_chown.called) - self.test_rc.save_successor(2, b"newcert", b"new_privkey", b"new chain", self.config) - self.assertTrue(mock_chown.called) - - def _test_relevant_values_common(self, values): - defaults = dict((option, cli.flag_default(option)) - for option in ("authenticator", "installer", - "rsa_key_size", "server",)) - mock_parser = mock.Mock(args=[], verb="plugins", - defaults=defaults) - - # make a copy to ensure values isn't modified - values = values.copy() - values.setdefault("server", defaults["server"]) - expected_server = values["server"] - - from certbot.storage import relevant_values - with mock.patch("certbot.cli.helpful_parser", mock_parser): - rv = relevant_values(values) - self.assertIn("server", rv) - self.assertEqual(rv.pop("server"), expected_server) - return rv - - def test_relevant_values(self): - """Test that relevant_values() can reject an irrelevant value.""" - self.assertEqual( - self._test_relevant_values_common({"hello": "there"}), {}) - - def test_relevant_values_default(self): - """Test that relevant_values() can reject a default value.""" - option = "rsa_key_size" - values = {option: cli.flag_default(option)} - self.assertEqual(self._test_relevant_values_common(values), {}) - - def test_relevant_values_nondefault(self): - """Test that relevant_values() can retain a non-default value.""" - values = {"rsa_key_size": 12} - self.assertEqual( - self._test_relevant_values_common(values), values) - - def test_relevant_values_bool(self): - values = {"allow_subset_of_names": True} - self.assertEqual( - self._test_relevant_values_common(values), values) - - def test_relevant_values_str(self): - values = {"authenticator": "apache"} - self.assertEqual( - self._test_relevant_values_common(values), values) - - def test_relevant_values_plugins_none(self): - self.assertEqual( - self._test_relevant_values_common( - {"authenticator": None, "installer": None}), {}) - - @mock.patch("certbot.cli.set_by_cli") - @mock.patch("certbot.plugins.disco.PluginsRegistry.find_all") - def test_relevant_values_namespace(self, mock_find_all, mock_set_by_cli): - mock_set_by_cli.return_value = True - mock_find_all.return_value = ["certbot-foo:bar"] - values = {"certbot_foo:bar_baz": 42} - self.assertEqual( - self._test_relevant_values_common(values), values) - - def test_relevant_values_server(self): - self.assertEqual( - # _test_relevant_values_common handles testing the server - # value and removes it - self._test_relevant_values_common({"server": "example.org"}), {}) - - @mock.patch("certbot.storage.relevant_values") - def test_new_lineage(self, mock_rv): - """Test for new_lineage() class method.""" - # Mock relevant_values to say everything is relevant here (so we - # don't have to mock the parser to help it decide!) - mock_rv.side_effect = lambda x: x - - from certbot import storage - result = storage.RenewableCert.new_lineage( - "the-lineage.com", b"cert", b"privkey", b"chain", self.config) - # This consistency check tests most relevant properties about the - # newly created cert lineage. - # pylint: disable=protected-access - self.assertTrue(result._consistent()) - self.assertTrue(os.path.exists(os.path.join( - self.config.renewal_configs_dir, "the-lineage.com.conf"))) - self.assertTrue(os.path.exists(os.path.join( - self.config.live_dir, "README"))) - self.assertTrue(os.path.exists(os.path.join( - self.config.live_dir, "the-lineage.com", "README"))) - self.assertTrue(compat.compare_file_modes(os.stat(result.key_path).st_mode, 0o600)) - with open(result.fullchain, "rb") as f: - self.assertEqual(f.read(), b"cert" + b"chain") - # Let's do it again and make sure it makes a different lineage - result = storage.RenewableCert.new_lineage( - "the-lineage.com", b"cert2", b"privkey2", b"chain2", self.config) - self.assertTrue(os.path.exists(os.path.join( - self.config.renewal_configs_dir, "the-lineage.com-0001.conf"))) - self.assertTrue(os.path.exists(os.path.join( - self.config.live_dir, "the-lineage.com-0001", "README"))) - # Now trigger the detection of already existing files - os.mkdir(os.path.join( - self.config.live_dir, "the-lineage.com-0002")) - self.assertRaises(errors.CertStorageError, - storage.RenewableCert.new_lineage, "the-lineage.com", - b"cert3", b"privkey3", b"chain3", self.config) - os.mkdir(os.path.join(self.config.default_archive_dir, "other-example.com")) - self.assertRaises(errors.CertStorageError, - storage.RenewableCert.new_lineage, - "other-example.com", b"cert4", - b"privkey4", b"chain4", self.config) - # Make sure it can accept renewal parameters - result = storage.RenewableCert.new_lineage( - "the-lineage.com", b"cert2", b"privkey2", b"chain2", self.config) - # TODO: Conceivably we could test that the renewal parameters actually - # got saved - - @mock.patch("certbot.storage.relevant_values") - def test_new_lineage_nonexistent_dirs(self, mock_rv): - """Test that directories can be created if they don't exist.""" - # Mock relevant_values to say everything is relevant here (so we - # don't have to mock the parser to help it decide!) - mock_rv.side_effect = lambda x: x - - from certbot import storage - shutil.rmtree(self.config.renewal_configs_dir) - shutil.rmtree(self.config.default_archive_dir) - shutil.rmtree(self.config.live_dir) - - storage.RenewableCert.new_lineage( - "the-lineage.com", b"cert2", b"privkey2", b"chain2", self.config) - self.assertTrue(os.path.exists( - os.path.join( - self.config.renewal_configs_dir, "the-lineage.com.conf"))) - self.assertTrue(os.path.exists(os.path.join( - self.config.live_dir, "the-lineage.com", "privkey.pem"))) - self.assertTrue(os.path.exists(os.path.join( - self.config.default_archive_dir, "the-lineage.com", "privkey1.pem"))) - - @mock.patch("certbot.storage.util.unique_lineage_name") - def test_invalid_config_filename(self, mock_uln): - from certbot import storage - mock_uln.return_value = "this_does_not_end_with_dot_conf", "yikes" - self.assertRaises(errors.CertStorageError, - storage.RenewableCert.new_lineage, "example.com", - "cert", "privkey", "chain", self.config) - - def test_bad_kind(self): - self.assertRaises( - errors.CertStorageError, self.test_rc.current_target, "elephant") - self.assertRaises( - errors.CertStorageError, self.test_rc.current_version, "elephant") - self.assertRaises( - errors.CertStorageError, self.test_rc.version, "elephant", 17) - self.assertRaises( - errors.CertStorageError, - self.test_rc.available_versions, "elephant") - self.assertRaises( - errors.CertStorageError, - self.test_rc.newest_available_version, "elephant") - # pylint: disable=protected-access - self.assertRaises( - errors.CertStorageError, - self.test_rc._update_link_to, "elephant", 17) - - def test_ocsp_revoked(self): - # XXX: This is currently hardcoded to False due to a lack of an - # OCSP server to test against. - self.assertFalse(self.test_rc.ocsp_revoked()) - - def test_add_time_interval(self): - from certbot import storage - - # this month has 30 days, and the next year is a leap year - time_1 = pytz.UTC.fromutc(datetime.datetime(2003, 11, 20, 11, 59, 21)) - - # this month has 31 days, and the next year is not a leap year - time_2 = pytz.UTC.fromutc(datetime.datetime(2012, 10, 18, 21, 31, 16)) - - # in different time zone (GMT+8) - time_3 = pytz.timezone('Asia/Shanghai').fromutc( - datetime.datetime(2015, 10, 26, 22, 25, 41)) - - intended = { - (time_1, ""): time_1, - (time_2, ""): time_2, - (time_3, ""): time_3, - (time_1, "17 days"): time_1 + datetime.timedelta(17), - (time_2, "17 days"): time_2 + datetime.timedelta(17), - (time_1, "30"): time_1 + datetime.timedelta(30), - (time_2, "30"): time_2 + datetime.timedelta(30), - (time_1, "7 weeks"): time_1 + datetime.timedelta(49), - (time_2, "7 weeks"): time_2 + datetime.timedelta(49), - # 1 month is always 30 days, no matter which month it is - (time_1, "1 month"): time_1 + datetime.timedelta(30), - (time_2, "1 month"): time_2 + datetime.timedelta(31), - # 1 year could be 365 or 366 days, depends on the year - (time_1, "1 year"): time_1 + datetime.timedelta(366), - (time_2, "1 year"): time_2 + datetime.timedelta(365), - (time_1, "1 year 1 day"): time_1 + datetime.timedelta(367), - (time_2, "1 year 1 day"): time_2 + datetime.timedelta(366), - (time_1, "1 year-1 day"): time_1 + datetime.timedelta(365), - (time_2, "1 year-1 day"): time_2 + datetime.timedelta(364), - (time_1, "4 years"): time_1 + datetime.timedelta(1461), - (time_2, "4 years"): time_2 + datetime.timedelta(1461), - } - - for parameters, excepted in intended.items(): - base_time, interval = parameters - self.assertEqual(storage.add_time_interval(base_time, interval), - excepted) - - def test_is_test_cert(self): - self.test_rc.configuration["renewalparams"] = {} - rp = self.test_rc.configuration["renewalparams"] - self.assertEqual(self.test_rc.is_test_cert, False) - rp["server"] = "https://acme-staging-v02.api.letsencrypt.org/directory" - self.assertEqual(self.test_rc.is_test_cert, True) - rp["server"] = "https://staging.someotherca.com/directory" - self.assertEqual(self.test_rc.is_test_cert, True) - rp["server"] = "https://acme-v01.api.letsencrypt.org/directory" - self.assertEqual(self.test_rc.is_test_cert, False) - rp["server"] = "https://acme-v02.api.letsencrypt.org/directory" - self.assertEqual(self.test_rc.is_test_cert, False) - - def test_missing_cert(self): - from certbot import storage - self.assertRaises(errors.CertStorageError, - storage.RenewableCert, - self.config_file.filename, self.config) - os.symlink("missing", self.config_file[ALL_FOUR[0]]) - self.assertRaises(errors.CertStorageError, - storage.RenewableCert, - self.config_file.filename, self.config) - - def test_write_renewal_config(self): - # Mostly tested by the process of creating and updating lineages, - # but we can test that this successfully creates files, removes - # unneeded items, and preserves comments. - temp = os.path.join(self.config.config_dir, "sample-file") - temp2 = os.path.join(self.config.config_dir, "sample-file.new") - with open(temp, "w") as f: - f.write("[renewalparams]\nuseful = value # A useful value\n" - "useless = value # Not needed\n") - os.chmod(temp, 0o640) - target = {} - for x in ALL_FOUR: - target[x] = "somewhere" - archive_dir = "the_archive" - relevant_data = {"useful": "new_value"} - - from certbot import storage - storage.write_renewal_config(temp, temp2, archive_dir, target, relevant_data) - - with open(temp2, "r") as f: - content = f.read() - # useful value was updated - self.assertTrue("useful = new_value" in content) - # associated comment was preserved - self.assertTrue("A useful value" in content) - # useless value was deleted - self.assertTrue("useless" not in content) - # check version was stored - self.assertTrue("version = {0}".format(certbot.__version__) in content) - # ensure permissions are copied - self.assertEqual(stat.S_IMODE(os.lstat(temp).st_mode), - stat.S_IMODE(os.lstat(temp2).st_mode)) - - def test_update_symlinks(self): - from certbot import storage - archive_dir_path = os.path.join(self.config.config_dir, "archive", "example.org") - for kind in ALL_FOUR: - live_path = self.config_file[kind] - basename = kind + "1.pem" - archive_path = os.path.join(archive_dir_path, basename) - open(archive_path, 'a').close() - os.symlink(os.path.join(self.config.config_dir, basename), live_path) - self.assertRaises(errors.CertStorageError, - storage.RenewableCert, self.config_file.filename, - self.config) - storage.RenewableCert(self.config_file.filename, self.config, - update_symlinks=True) - -class DeleteFilesTest(BaseRenewableCertTest): - """Tests for certbot.storage.delete_files""" - def setUp(self): - super(DeleteFilesTest, self).setUp() - - for kind in ALL_FOUR: - kind_path = os.path.join(self.config.config_dir, "live", "example.org", - kind + ".pem") - with open(kind_path, 'a'): - pass - self.config_file.write() - self.assertTrue(os.path.exists(os.path.join( - self.config.renewal_configs_dir, "example.org.conf"))) - self.assertTrue(os.path.exists(os.path.join( - self.config.live_dir, "example.org"))) - self.assertTrue(os.path.exists(os.path.join( - self.config.config_dir, "archive", "example.org"))) - - def _call(self): - from certbot import storage - with mock.patch("certbot.storage.logger"): - storage.delete_files(self.config, "example.org") - - def test_delete_all_files(self): - self._call() - - self.assertFalse(os.path.exists(os.path.join( - self.config.renewal_configs_dir, "example.org.conf"))) - self.assertFalse(os.path.exists(os.path.join( - self.config.live_dir, "example.org"))) - self.assertFalse(os.path.exists(os.path.join( - self.config.config_dir, "archive", "example.org"))) - - def test_bad_renewal_config(self): - with open(self.config_file.filename, 'a') as config_file: - config_file.write("asdfasfasdfasdf") - - self.assertRaises(errors.CertStorageError, self._call) - self.assertTrue(os.path.exists(os.path.join( - self.config.live_dir, "example.org"))) - self.assertFalse(os.path.exists(os.path.join( - self.config.renewal_configs_dir, "example.org.conf"))) - - def test_no_renewal_config(self): - os.remove(self.config_file.filename) - self.assertRaises(errors.CertStorageError, self._call) - self.assertTrue(os.path.exists(os.path.join( - self.config.live_dir, "example.org"))) - self.assertFalse(os.path.exists(self.config_file.filename)) - - def test_no_cert_file(self): - os.remove(os.path.join( - self.config.live_dir, "example.org", "cert.pem")) - self._call() - self.assertFalse(os.path.exists(self.config_file.filename)) - self.assertFalse(os.path.exists(os.path.join( - self.config.live_dir, "example.org"))) - self.assertFalse(os.path.exists(os.path.join( - self.config.config_dir, "archive", "example.org"))) - - def test_no_readme_file(self): - os.remove(os.path.join( - self.config.live_dir, "example.org", "README")) - self._call() - self.assertFalse(os.path.exists(self.config_file.filename)) - self.assertFalse(os.path.exists(os.path.join( - self.config.live_dir, "example.org"))) - self.assertFalse(os.path.exists(os.path.join( - self.config.config_dir, "archive", "example.org"))) - - def test_livedir_not_empty(self): - with open(os.path.join( - self.config.live_dir, "example.org", "other_file"), 'a'): - pass - self._call() - self.assertFalse(os.path.exists(self.config_file.filename)) - self.assertTrue(os.path.exists(os.path.join( - self.config.live_dir, "example.org"))) - self.assertFalse(os.path.exists(os.path.join( - self.config.config_dir, "archive", "example.org"))) - - def test_no_archive(self): - archive_dir = os.path.join(self.config.config_dir, "archive", "example.org") - os.rmdir(archive_dir) - self._call() - self.assertFalse(os.path.exists(self.config_file.filename)) - self.assertFalse(os.path.exists(os.path.join( - self.config.live_dir, "example.org"))) - self.assertFalse(os.path.exists(archive_dir)) - -class CertPathForCertNameTest(BaseRenewableCertTest): - """Test for certbot.storage.cert_path_for_cert_name""" - def setUp(self): - super(CertPathForCertNameTest, self).setUp() - self.config_file.write() - self._write_out_ex_kinds() - self.fullchain = os.path.join(self.config.config_dir, 'live', 'example.org', - 'fullchain.pem') - self.config.cert_path = (self.fullchain, '') - - def _call(self, cli_config, certname): - from certbot.storage import cert_path_for_cert_name - return cert_path_for_cert_name(cli_config, certname) - - def test_simple_cert_name(self): - self.assertEqual(self._call(self.config, 'example.org'), (self.fullchain, 'fullchain')) - - def test_no_such_cert_name(self): - self.assertRaises(errors.CertStorageError, self._call, self.config, 'fake-example.org') - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/tests/testdata/README b/certbot/tests/testdata/README deleted file mode 100644 index 8672159160a..00000000000 --- a/certbot/tests/testdata/README +++ /dev/null @@ -1,11 +0,0 @@ -The following command has been used to generate test keys: - - for x in 256 512 2048; do openssl genrsa -out rsa${k}_key.pem $k; done - -and for the CSR PEM (Certificate Signing Request): - - openssl req -new -out csr-Xsans_X.pem -key rsa512_key.pem [-config csr-Xsans_X.conf | -subj '/CN=example.com'] [-outform DER > csr_X.der] - -and for the certificate: - - openssl req -new -out cert_X.pem -key rsaX_key.pem -subj '/CN=example.com' -x509 [-outform DER > cert_X.der] \ No newline at end of file diff --git a/certbot/tests/testdata/rsa256_key.pem b/certbot/tests/testdata/rsa256_key.pem deleted file mode 100644 index 659274d1d9f..00000000000 --- a/certbot/tests/testdata/rsa256_key.pem +++ /dev/null @@ -1,6 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIGrAgEAAiEAm2Fylv+Uz7trgTW8EBHP3FQSMeZs2GNQ6VRo1sIVJEkCAwEAAQIh -AJT0BA/xD01dFCAXzSNyj9nfSZa3NpqzJZZn/eOm7vghAhEAzUVNZn4lLLBD1R6N -E8TKNQIRAMHHyn3O5JeY36lwKwkUlEUCEAliRauN0L0+QZuYjfJ9aJECEGx4dru3 -rTPCyighdqWNlHUCEQCiLjlwSRtWgmMBudCkVjzt ------END RSA PRIVATE KEY----- diff --git a/certbot/tests/util.py b/certbot/tests/util.py deleted file mode 100644 index 8c5db2c2f64..00000000000 --- a/certbot/tests/util.py +++ /dev/null @@ -1,423 +0,0 @@ -"""Test utilities. - -.. warning:: This module is not part of the public API. - -""" -import multiprocessing -import os -import pkg_resources -import shutil -import tempfile -import unittest -import sys -import warnings - -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives import serialization -import mock -import OpenSSL -import josepy as jose -import six -from six.moves import reload_module # pylint: disable=import-error - -from certbot import constants -from certbot import interfaces -from certbot import storage -from certbot import util -from certbot import configuration - -from certbot.display import util as display_util - - -def vector_path(*names): - """Path to a test vector.""" - return pkg_resources.resource_filename( - __name__, os.path.join('testdata', *names)) - - -def load_vector(*names): - """Load contents of a test vector.""" - # luckily, resource_string opens file in binary mode - data = pkg_resources.resource_string( - __name__, os.path.join('testdata', *names)) - # Try at most to convert CRLF to LF when data is text - try: - return data.decode().replace('\r\n', '\n').encode() - except ValueError: - # Failed to process the file with standard encoding. - # Most likely not a text file, return its bytes untouched. - return data - - -def _guess_loader(filename, loader_pem, loader_der): - _, ext = os.path.splitext(filename) - if ext.lower() == '.pem': - return loader_pem - elif ext.lower() == '.der': - return loader_der - else: # pragma: no cover - raise ValueError("Loader could not be recognized based on extension") - - -def load_cert(*names): - """Load certificate.""" - loader = _guess_loader( - names[-1], OpenSSL.crypto.FILETYPE_PEM, OpenSSL.crypto.FILETYPE_ASN1) - return OpenSSL.crypto.load_certificate(loader, load_vector(*names)) - - -def load_csr(*names): - """Load certificate request.""" - loader = _guess_loader( - names[-1], OpenSSL.crypto.FILETYPE_PEM, OpenSSL.crypto.FILETYPE_ASN1) - return OpenSSL.crypto.load_certificate_request(loader, load_vector(*names)) - - -def load_comparable_csr(*names): - """Load ComparableX509 certificate request.""" - return jose.ComparableX509(load_csr(*names)) - - -def load_rsa_private_key(*names): - """Load RSA private key.""" - loader = _guess_loader(names[-1], serialization.load_pem_private_key, - serialization.load_der_private_key) - return jose.ComparableRSAKey(loader( - load_vector(*names), password=None, backend=default_backend())) - - -def load_pyopenssl_private_key(*names): - """Load pyOpenSSL private key.""" - loader = _guess_loader( - names[-1], OpenSSL.crypto.FILETYPE_PEM, OpenSSL.crypto.FILETYPE_ASN1) - return OpenSSL.crypto.load_privatekey(loader, load_vector(*names)) - - -def skip_unless(condition, reason): # pragma: no cover - """Skip tests unless a condition holds. - - This implements the basic functionality of unittest.skipUnless - which is only available on Python 2.7+. - - :param bool condition: If ``False``, the test will be skipped - :param str reason: the reason for skipping the test - - :rtype: callable - :returns: decorator that hides tests unless condition is ``True`` - - """ - if hasattr(unittest, "skipUnless"): - return unittest.skipUnless(condition, reason) - elif condition: - return lambda cls: cls - else: - return lambda cls: None - - -def make_lineage(config_dir, testfile): - """Creates a lineage defined by testfile. - - This creates the archive, live, and renewal directories if - necessary and creates a simple lineage. - - :param str config_dir: path to the configuration directory - :param str testfile: configuration file to base the lineage on - - :returns: path to the renewal conf file for the created lineage - :rtype: str - - """ - lineage_name = testfile[:-len('.conf')] - - conf_dir = os.path.join( - config_dir, constants.RENEWAL_CONFIGS_DIR) - archive_dir = os.path.join( - config_dir, constants.ARCHIVE_DIR, lineage_name) - live_dir = os.path.join( - config_dir, constants.LIVE_DIR, lineage_name) - - for directory in (archive_dir, conf_dir, live_dir,): - if not os.path.exists(directory): - os.makedirs(directory) - - sample_archive = vector_path('sample-archive') - for kind in os.listdir(sample_archive): - shutil.copyfile(os.path.join(sample_archive, kind), - os.path.join(archive_dir, kind)) - - for kind in storage.ALL_FOUR: - os.symlink(os.path.join(archive_dir, '{0}1.pem'.format(kind)), - os.path.join(live_dir, '{0}.pem'.format(kind))) - - conf_path = os.path.join(config_dir, conf_dir, testfile) - with open(vector_path(testfile)) as src: - with open(conf_path, 'w') as dst: - dst.writelines( - line.replace('MAGICDIR', config_dir) for line in src) - - return conf_path - - -def patch_get_utility(target='zope.component.getUtility'): - """Patch zope.component.getUtility to use a special mock IDisplay. - - The mock IDisplay works like a regular mock object, except it also - also asserts that methods are called with valid arguments. - - :param str target: path to patch - - :returns: mock zope.component.getUtility - :rtype: mock.MagicMock - - """ - return mock.patch(target, new_callable=_create_get_utility_mock) - - -def patch_get_utility_with_stdout(target='zope.component.getUtility', - stdout=None): - """Patch zope.component.getUtility to use a special mock IDisplay. - - The mock IDisplay works like a regular mock object, except it also - also asserts that methods are called with valid arguments. - - The `message` argument passed to the IDisplay methods is passed to - stdout's write method. - - :param str target: path to patch - :param object stdout: object to write standard output to; it is - expected to have a `write` method - - :returns: mock zope.component.getUtility - :rtype: mock.MagicMock - - """ - stdout = stdout if stdout else six.StringIO() - - freezable_mock = _create_get_utility_mock_with_stdout(stdout) - return mock.patch(target, new=freezable_mock) - - -class FreezableMock(object): - """Mock object with the ability to freeze attributes. - - This class works like a regular mock.MagicMock object, except - attributes and behavior set before the object is frozen cannot - be changed during tests. - - If a func argument is provided to the constructor, this function - is called first when an instance of FreezableMock is called, - followed by the usual behavior defined by MagicMock. The return - value of func is ignored. - - """ - def __init__(self, frozen=False, func=None, return_value=mock.sentinel.DEFAULT): - self._frozen_set = set() if frozen else set(('freeze',)) - self._func = func - self._mock = mock.MagicMock() - if return_value != mock.sentinel.DEFAULT: - self.return_value = return_value - self._frozen = frozen - - def freeze(self): - """Freeze object preventing further changes.""" - self._frozen = True - - def __call__(self, *args, **kwargs): - if self._func is not None: - self._func(*args, **kwargs) - return self._mock(*args, **kwargs) - - def __getattribute__(self, name): - if name == '_frozen': - try: - return object.__getattribute__(self, name) - except AttributeError: - return False - elif name in ('return_value', 'side_effect',): - return getattr(object.__getattribute__(self, '_mock'), name) - elif name == '_frozen_set' or name in self._frozen_set: - return object.__getattribute__(self, name) - else: - return getattr(object.__getattribute__(self, '_mock'), name) - - def __setattr__(self, name, value): - """ Before it is frozen, attributes are set on the FreezableMock - instance and added to the _frozen_set. Attributes in the _frozen_set - cannot be changed after the FreezableMock is frozen. In this case, - they are set on the underlying _mock. - - In cases of return_value and side_effect, these attributes are always - passed through to the instance's _mock and added to the _frozen_set - before the object is frozen. - - """ - if self._frozen: - if name in self._frozen_set: - raise AttributeError('Cannot change frozen attribute ' + name) - else: - return setattr(self._mock, name, value) - - if name != '_frozen_set': - self._frozen_set.add(name) - - if name in ('return_value', 'side_effect'): - return setattr(self._mock, name, value) - - else: - return object.__setattr__(self, name, value) - - -def _create_get_utility_mock(): - display = FreezableMock() - for name in interfaces.IDisplay.names(): # pylint: disable=no-member - if name != 'notification': - frozen_mock = FreezableMock(frozen=True, func=_assert_valid_call) - setattr(display, name, frozen_mock) - display.freeze() - return FreezableMock(frozen=True, return_value=display) - - -def _create_get_utility_mock_with_stdout(stdout): - def _write_msg(message, *unused_args, **unused_kwargs): - """Write to message to stdout. - """ - if message: - stdout.write(message) - - def mock_method(*args, **kwargs): - """ - Mock function for IDisplay methods. - """ - _assert_valid_call(args, kwargs) - _write_msg(*args, **kwargs) - - - display = FreezableMock() - for name in interfaces.IDisplay.names(): # pylint: disable=no-member - if name == 'notification': - frozen_mock = FreezableMock(frozen=True, - func=_write_msg) - setattr(display, name, frozen_mock) - else: - frozen_mock = FreezableMock(frozen=True, - func=mock_method) - setattr(display, name, frozen_mock) - display.freeze() - - return FreezableMock(frozen=True, return_value=display) - - -def _assert_valid_call(*args, **kwargs): - assert_args = [args[0] if args else kwargs['message']] - - assert_kwargs = {} - assert_kwargs['default'] = kwargs.get('default', None) - assert_kwargs['cli_flag'] = kwargs.get('cli_flag', None) - assert_kwargs['force_interactive'] = kwargs.get('force_interactive', False) - - # pylint: disable=star-args - display_util.assert_valid_call(*assert_args, **assert_kwargs) - - -class TempDirTestCase(unittest.TestCase): - """Base test class which sets up and tears down a temporary directory""" - - def setUp(self): - """Execute before test""" - self.tempdir = tempfile.mkdtemp() - - def tearDown(self): - """Execute after test""" - # On Windows we have various files which are not correctly closed at the time of tearDown. - # For know, we log them until a proper file close handling is written. - # Useful for development only, so no warning when we are on a CI process. - def onerror_handler(_, path, excinfo): - """On error handler""" - if not os.environ.get('APPVEYOR'): # pragma: no cover - message = ('Following error occurred when deleting the tempdir {0}' - ' for path {1} during tearDown process: {2}' - .format(self.tempdir, path, str(excinfo))) - warnings.warn(message) - shutil.rmtree(self.tempdir, onerror=onerror_handler) - -class ConfigTestCase(TempDirTestCase): - """Test class which sets up a NamespaceConfig object. - - """ - def setUp(self): - super(ConfigTestCase, self).setUp() - self.config = configuration.NamespaceConfig( - mock.MagicMock(**constants.CLI_DEFAULTS) - ) - self.config.verb = "certonly" - self.config.config_dir = os.path.join(self.tempdir, 'config') - self.config.work_dir = os.path.join(self.tempdir, 'work') - self.config.logs_dir = os.path.join(self.tempdir, 'logs') - self.config.cert_path = constants.CLI_DEFAULTS['auth_cert_path'] - self.config.fullchain_path = constants.CLI_DEFAULTS['auth_chain_path'] - self.config.chain_path = constants.CLI_DEFAULTS['auth_chain_path'] - self.config.server = "https://example.com" - -def lock_and_call(func, lock_path): - """Grab a lock for lock_path and call func. - - :param callable func: object to call after acquiring the lock - :param str lock_path: path to file or directory to lock - - """ - # Reload module to reset internal _LOCKS dictionary - reload_module(util) - - # start child and wait for it to grab the lock - cv = multiprocessing.Condition() - cv.acquire() - child_args = (cv, lock_path,) - child = multiprocessing.Process(target=hold_lock, args=child_args) - child.start() - cv.wait() - - # call func and terminate the child - func() - cv.notify() - cv.release() - child.join() - assert child.exitcode == 0 - -def hold_lock(cv, lock_path): # pragma: no cover - """Acquire a file lock at lock_path and wait to release it. - - :param multiprocessing.Condition cv: condition for synchronization - :param str lock_path: path to the file lock - - """ - from certbot import lock - if os.path.isdir(lock_path): - my_lock = lock.lock_dir(lock_path) - else: - my_lock = lock.LockFile(lock_path) - cv.acquire() - cv.notify() - cv.wait() - my_lock.release() - -def skip_on_windows(reason): - """Decorator to skip permanently a test on Windows. A reason is required.""" - def wrapper(function): - """Wrapped version""" - return unittest.skipIf(sys.platform == 'win32', reason)(function) - return wrapper - -def broken_on_windows(function): - """Decorator to skip temporarily a broken test on Windows.""" - reason = 'Test is broken and ignored on windows but should be fixed.' - return unittest.skipIf( - sys.platform == 'win32' - and os.environ.get('SKIP_BROKEN_TESTS_ON_WINDOWS', 'true') == 'true', - reason)(function) - -def temp_join(path): - """ - Return the given path joined to the tempdir path for the current platform - Eg.: 'cert' => /tmp/cert (Linux) or 'C:\\Users\\currentuser\\AppData\\Temp\\cert' (Windows) - """ - return os.path.join(tempfile.gettempdir(), path) diff --git a/certbot/tests/util_test.py b/certbot/tests/util_test.py deleted file mode 100644 index 6685b88c679..00000000000 --- a/certbot/tests/util_test.py +++ /dev/null @@ -1,629 +0,0 @@ -"""Tests for certbot.util.""" -import argparse -import errno -import os -import shutil -import unittest - -import mock -import six -from six.moves import reload_module # pylint: disable=import-error - -from certbot import compat -from certbot import errors -import certbot.tests.util as test_util - - -class RunScriptTest(unittest.TestCase): - """Tests for certbot.util.run_script.""" - @classmethod - def _call(cls, params): - from certbot.util import run_script - return run_script(params) - - @mock.patch("certbot.util.subprocess.Popen") - def test_default(self, mock_popen): - """These will be changed soon enough with reload.""" - mock_popen().returncode = 0 - mock_popen().communicate.return_value = ("stdout", "stderr") - - out, err = self._call(["test"]) - self.assertEqual(out, "stdout") - self.assertEqual(err, "stderr") - - @mock.patch("certbot.util.subprocess.Popen") - def test_bad_process(self, mock_popen): - mock_popen.side_effect = OSError - - self.assertRaises(errors.SubprocessError, self._call, ["test"]) - - @mock.patch("certbot.util.subprocess.Popen") - def test_failure(self, mock_popen): - mock_popen().communicate.return_value = ("", "") - mock_popen().returncode = 1 - - self.assertRaises(errors.SubprocessError, self._call, ["test"]) - - -class ExeExistsTest(unittest.TestCase): - """Tests for certbot.util.exe_exists.""" - - @classmethod - def _call(cls, exe): - from certbot.util import exe_exists - return exe_exists(exe) - - @mock.patch("certbot.util.os.path.isfile") - @mock.patch("certbot.util.os.access") - def test_full_path(self, mock_access, mock_isfile): - mock_access.return_value = True - mock_isfile.return_value = True - self.assertTrue(self._call("/path/to/exe")) - - @mock.patch("certbot.util.os.path.isfile") - @mock.patch("certbot.util.os.access") - def test_on_path(self, mock_access, mock_isfile): - mock_access.return_value = True - mock_isfile.return_value = True - self.assertTrue(self._call("exe")) - - @mock.patch("certbot.util.os.path.isfile") - @mock.patch("certbot.util.os.access") - def test_not_found(self, mock_access, mock_isfile): - mock_access.return_value = False - mock_isfile.return_value = True - self.assertFalse(self._call("exe")) - - -class LockDirUntilExit(test_util.TempDirTestCase): - """Tests for certbot.util.lock_dir_until_exit.""" - @classmethod - def _call(cls, *args, **kwargs): - from certbot.util import lock_dir_until_exit - return lock_dir_until_exit(*args, **kwargs) - - def setUp(self): - super(LockDirUntilExit, self).setUp() - # reset global state from other tests - import certbot.util - reload_module(certbot.util) - - @test_util.broken_on_windows - @mock.patch('certbot.util.logger') - @mock.patch('certbot.util.atexit_register') - def test_it(self, mock_register, mock_logger): - subdir = os.path.join(self.tempdir, 'subdir') - os.mkdir(subdir) - self._call(self.tempdir) - self._call(subdir) - self._call(subdir) - - self.assertEqual(mock_register.call_count, 1) - registered_func = mock_register.call_args[0][0] - shutil.rmtree(subdir) - registered_func() # exception not raised - # logger.debug is only called once because the second call - # to lock subdir was ignored because it was already locked - self.assertEqual(mock_logger.debug.call_count, 1) - - -class SetUpCoreDirTest(test_util.TempDirTestCase): - """Tests for certbot.util.make_or_verify_core_dir.""" - - def _call(self, *args, **kwargs): - from certbot.util import set_up_core_dir - return set_up_core_dir(*args, **kwargs) - - @mock.patch('certbot.util.lock_dir_until_exit') - def test_success(self, mock_lock): - new_dir = os.path.join(self.tempdir, 'new') - self._call(new_dir, 0o700, compat.os_geteuid(), False) - self.assertTrue(os.path.exists(new_dir)) - self.assertEqual(mock_lock.call_count, 1) - - @mock.patch('certbot.util.make_or_verify_dir') - def test_failure(self, mock_make_or_verify): - mock_make_or_verify.side_effect = OSError - self.assertRaises(errors.Error, self._call, - self.tempdir, 0o700, compat.os_geteuid(), False) - - -class MakeOrVerifyDirTest(test_util.TempDirTestCase): - """Tests for certbot.util.make_or_verify_dir. - - Note that it is not possible to test for a wrong directory owner, - as this testing script would have to be run as root. - - """ - - def setUp(self): - super(MakeOrVerifyDirTest, self).setUp() - - self.path = os.path.join(self.tempdir, "foo") - os.mkdir(self.path, 0o600) - - self.uid = compat.os_geteuid() - - def _call(self, directory, mode): - from certbot.util import make_or_verify_dir - return make_or_verify_dir(directory, mode, self.uid, strict=True) - - def test_creates_dir_when_missing(self): - path = os.path.join(self.tempdir, "bar") - self._call(path, 0o650) - self.assertTrue(os.path.isdir(path)) - self.assertTrue(compat.compare_file_modes(os.stat(path).st_mode, 0o650)) - - def test_existing_correct_mode_does_not_fail(self): - self._call(self.path, 0o600) - self.assertTrue(compat.compare_file_modes(os.stat(self.path).st_mode, 0o600)) - - @test_util.skip_on_windows('Umask modes are mostly ignored on Windows.') - def test_existing_wrong_mode_fails(self): - self.assertRaises(errors.Error, self._call, self.path, 0o400) - - def test_reraises_os_error(self): - with mock.patch.object(os, "makedirs") as makedirs: - makedirs.side_effect = OSError() - self.assertRaises(OSError, self._call, "bar", 12312312) - - -class CheckPermissionsTest(test_util.TempDirTestCase): - """Tests for certbot.util.check_permissions. - - Note that it is not possible to test for a wrong file owner, - as this testing script would have to be run as root. - - """ - - def setUp(self): - super(CheckPermissionsTest, self).setUp() - - self.uid = compat.os_geteuid() - - def _call(self, mode): - from certbot.util import check_permissions - return check_permissions(self.tempdir, mode, self.uid) - - def test_ok_mode(self): - os.chmod(self.tempdir, 0o600) - self.assertTrue(self._call(0o600)) - - def test_wrong_mode(self): - os.chmod(self.tempdir, 0o400) - self.assertFalse(self._call(0o600)) - - -class UniqueFileTest(test_util.TempDirTestCase): - """Tests for certbot.util.unique_file.""" - - def setUp(self): - super(UniqueFileTest, self).setUp() - - self.default_name = os.path.join(self.tempdir, "foo.txt") - - def _call(self, mode=0o600): - from certbot.util import unique_file - return unique_file(self.default_name, mode) - - def test_returns_fd_for_writing(self): - fd, name = self._call() - fd.write("bar") - fd.close() - with open(name) as f: - self.assertEqual(f.read(), "bar") - - def test_right_mode(self): - fd1, name1 = self._call(0o700) - fd2, name2 = self._call(0o600) - self.assertTrue(compat.compare_file_modes(0o700, os.stat(name1).st_mode)) - self.assertTrue(compat.compare_file_modes(0o600, os.stat(name2).st_mode)) - fd1.close() - fd2.close() - - def test_default_exists(self): - fd1, name1 = self._call() # create 0000_foo.txt - fd2, name2 = self._call() - fd3, name3 = self._call() - - self.assertNotEqual(name1, name2) - self.assertNotEqual(name1, name3) - self.assertNotEqual(name2, name3) - - self.assertEqual(os.path.dirname(name1), self.tempdir) - self.assertEqual(os.path.dirname(name2), self.tempdir) - self.assertEqual(os.path.dirname(name3), self.tempdir) - - basename1 = os.path.basename(name2) - self.assertTrue(basename1.endswith("foo.txt")) - basename2 = os.path.basename(name2) - self.assertTrue(basename2.endswith("foo.txt")) - basename3 = os.path.basename(name3) - self.assertTrue(basename3.endswith("foo.txt")) - - fd1.close() - fd2.close() - fd3.close() - - -try: - file_type = file -except NameError: - import io - file_type = io.TextIOWrapper # type: ignore - - -class UniqueLineageNameTest(test_util.TempDirTestCase): - """Tests for certbot.util.unique_lineage_name.""" - - def _call(self, filename, mode=0o777): - from certbot.util import unique_lineage_name - return unique_lineage_name(self.tempdir, filename, mode) - - def test_basic(self): - f, path = self._call("wow") - self.assertTrue(isinstance(f, file_type)) - self.assertEqual(os.path.join(self.tempdir, "wow.conf"), path) - f.close() - - def test_multiple(self): - items = [] - for _ in six.moves.range(10): - items.append(self._call("wow")) - f, name = items[-1] - self.assertTrue(isinstance(f, file_type)) - self.assertTrue(isinstance(name, six.string_types)) - self.assertTrue("wow-0009.conf" in name) - for f, _ in items: - f.close() - - @mock.patch("certbot.util.os.fdopen") - def test_failure(self, mock_fdopen): - err = OSError("whoops") - err.errno = errno.EIO - mock_fdopen.side_effect = err - self.assertRaises(OSError, self._call, "wow") - - @mock.patch("certbot.util.os.fdopen") - def test_subsequent_failure(self, mock_fdopen): - self._call("wow") - err = OSError("whoops") - err.errno = errno.EIO - mock_fdopen.side_effect = err - self.assertRaises(OSError, self._call, "wow") - - -class SafelyRemoveTest(test_util.TempDirTestCase): - """Tests for certbot.util.safely_remove.""" - - def setUp(self): - super(SafelyRemoveTest, self).setUp() - - self.path = os.path.join(self.tempdir, "foo") - - def _call(self): - from certbot.util import safely_remove - return safely_remove(self.path) - - def test_exists(self): - with open(self.path, "w"): - pass # just create the file - self._call() - self.assertFalse(os.path.exists(self.path)) - - def test_missing(self): - self._call() - # no error, yay! - self.assertFalse(os.path.exists(self.path)) - - @mock.patch("certbot.util.os.remove") - def test_other_error_passthrough(self, mock_remove): - mock_remove.side_effect = OSError - self.assertRaises(OSError, self._call) - - -class SafeEmailTest(unittest.TestCase): - """Test safe_email.""" - @classmethod - def _call(cls, addr): - from certbot.util import safe_email - return safe_email(addr) - - def test_valid_emails(self): - addrs = [ - "certbot@certbot.org", - "tbd.ade@gmail.com", - "abc_def.jdk@hotmail.museum", - ] - for addr in addrs: - self.assertTrue(self._call(addr), "%s failed." % addr) - - def test_invalid_emails(self): - addrs = [ - "certbot@certbot..org", - ".tbd.ade@gmail.com", - "~/abc_def.jdk@hotmail.museum", - ] - for addr in addrs: - self.assertFalse(self._call(addr), "%s failed." % addr) - - -class AddDeprecatedArgumentTest(unittest.TestCase): - """Test add_deprecated_argument.""" - def setUp(self): - self.parser = argparse.ArgumentParser() - - def _call(self, argument_name, nargs): - from certbot.util import add_deprecated_argument - - add_deprecated_argument(self.parser.add_argument, argument_name, nargs) - - def test_warning_no_arg(self): - self._call("--old-option", 0) - stderr = self._get_argparse_warnings(["--old-option"]) - self.assertTrue("--old-option is deprecated" in stderr) - - def test_warning_with_arg(self): - self._call("--old-option", 1) - stderr = self._get_argparse_warnings(["--old-option", "42"]) - self.assertTrue("--old-option is deprecated" in stderr) - - def _get_argparse_warnings(self, args): - stderr = six.StringIO() - with mock.patch("certbot.util.sys.stderr", new=stderr): - self.parser.parse_args(args) - return stderr.getvalue() - - def test_help(self): - self._call("--old-option", 2) - stdout = six.StringIO() - with mock.patch("certbot.util.sys.stdout", new=stdout): - try: - self.parser.parse_args(["-h"]) - except SystemExit: - pass - self.assertTrue("--old-option" not in stdout.getvalue()) - - def test_set_constant(self): - """Test when ACTION_TYPES_THAT_DONT_NEED_A_VALUE is a set. - - This variable is a set in configargparse versions < 0.12.0. - - """ - self._test_constant_common(set) - - def test_tuple_constant(self): - """Test when ACTION_TYPES_THAT_DONT_NEED_A_VALUE is a tuple. - - This variable is a tuple in configargparse versions >= 0.12.0. - - """ - self._test_constant_common(tuple) - - def _test_constant_common(self, typ): - with mock.patch("certbot.util.configargparse") as mock_configargparse: - mock_configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE = typ() - self._call("--old-option", 1) - self._call("--old-option2", 2) - self.assertEqual( - len(mock_configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE), 1) - - -class EnforceLeValidity(unittest.TestCase): - """Test enforce_le_validity.""" - def _call(self, domain): - from certbot.util import enforce_le_validity - return enforce_le_validity(domain) - - def test_sanity(self): - self.assertRaises(errors.ConfigurationError, self._call, u"..") - - def test_invalid_chars(self): - self.assertRaises( - errors.ConfigurationError, self._call, u"hello_world.example.com") - - def test_leading_hyphen(self): - self.assertRaises( - errors.ConfigurationError, self._call, u"-a.example.com") - - def test_trailing_hyphen(self): - self.assertRaises( - errors.ConfigurationError, self._call, u"a-.example.com") - - def test_one_label(self): - self.assertRaises(errors.ConfigurationError, self._call, u"com") - - def test_valid_domain(self): - self.assertEqual(self._call(u"example.com"), u"example.com") - - def test_input_with_scheme(self): - self.assertRaises(errors.ConfigurationError, self._call, u"http://example.com") - self.assertRaises(errors.ConfigurationError, self._call, u"https://example.com") - - def test_valid_input_with_scheme_name(self): - self.assertEqual(self._call(u"http.example.com"), u"http.example.com") - - -class EnforceDomainSanityTest(unittest.TestCase): - """Test enforce_domain_sanity.""" - - def _call(self, domain): - from certbot.util import enforce_domain_sanity - return enforce_domain_sanity(domain) - - def test_nonascii_str(self): - self.assertRaises(errors.ConfigurationError, self._call, - u"eichh\u00f6rnchen.example.com".encode("utf-8")) - - def test_nonascii_unicode(self): - self.assertRaises(errors.ConfigurationError, self._call, - u"eichh\u00f6rnchen.example.com") - - def test_too_long(self): - long_domain = u"a"*256 - self.assertRaises(errors.ConfigurationError, self._call, - long_domain) - - def test_not_too_long(self): - not_too_long_domain = u"{0}.{1}.{2}.{3}".format("a"*63, "b"*63, "c"*63, "d"*63) - self._call(not_too_long_domain) - - def test_empty_label(self): - empty_label_domain = u"fizz..example.com" - self.assertRaises(errors.ConfigurationError, self._call, - empty_label_domain) - - def test_empty_trailing_label(self): - empty_trailing_label_domain = u"example.com.." - self.assertRaises(errors.ConfigurationError, self._call, - empty_trailing_label_domain) - - def test_long_label_1(self): - long_label_domain = u"a"*64 - self.assertRaises(errors.ConfigurationError, self._call, - long_label_domain) - - def test_long_label_2(self): - long_label_domain = u"{0}.{1}.com".format(u"a"*64, u"b"*63) - self.assertRaises(errors.ConfigurationError, self._call, - long_label_domain) - - def test_not_long_label(self): - not_too_long_label_domain = u"{0}.{1}.com".format(u"a"*63, u"b"*63) - self._call(not_too_long_label_domain) - - def test_empty_domain(self): - empty_domain = u"" - self.assertRaises(errors.ConfigurationError, self._call, - empty_domain) - - def test_punycode_ok(self): - # Punycode is now legal, so no longer an error; instead check - # that it's _not_ an error (at the initial sanity check stage) - self._call('this.is.xn--ls8h.tld') - - -class IsWildcardDomainTest(unittest.TestCase): - """Tests for is_wildcard_domain.""" - - def setUp(self): - self.wildcard = u"*.example.org" - self.no_wildcard = u"example.org" - - def _call(self, domain): - from certbot.util import is_wildcard_domain - return is_wildcard_domain(domain) - - def test_no_wildcard(self): - self.assertFalse(self._call(self.no_wildcard)) - self.assertFalse(self._call(self.no_wildcard.encode())) - - def test_wildcard(self): - self.assertTrue(self._call(self.wildcard)) - self.assertTrue(self._call(self.wildcard.encode())) - - -class OsInfoTest(unittest.TestCase): - """Test OS / distribution detection""" - - def test_systemd_os_release(self): - from certbot.util import (get_os_info, get_systemd_os_info, - get_os_info_ua) - - with mock.patch('os.path.isfile', return_value=True): - self.assertEqual(get_os_info( - test_util.vector_path("os-release"))[0], 'systemdos') - self.assertEqual(get_os_info( - test_util.vector_path("os-release"))[1], '42') - self.assertEqual(get_systemd_os_info(os.devnull), ("", "")) - self.assertEqual(get_os_info_ua( - test_util.vector_path("os-release")), "SystemdOS") - with mock.patch('os.path.isfile', return_value=False): - self.assertEqual(get_systemd_os_info(), ("", "")) - - def test_systemd_os_release_like(self): - from certbot.util import get_systemd_os_like - - with mock.patch('os.path.isfile', return_value=True): - id_likes = get_systemd_os_like(test_util.vector_path( - "os-release")) - self.assertEqual(len(id_likes), 3) - self.assertTrue("debian" in id_likes) - - @mock.patch("certbot.util.subprocess.Popen") - def test_non_systemd_os_info(self, popen_mock): - from certbot.util import (get_os_info, get_python_os_info, - get_os_info_ua) - with mock.patch('os.path.isfile', return_value=False): - with mock.patch('platform.system_alias', - return_value=('NonSystemD', '42', '42')): - self.assertEqual(get_os_info()[0], 'nonsystemd') - self.assertEqual(get_os_info_ua(), - " ".join(get_python_os_info())) - - with mock.patch('platform.system_alias', - return_value=('darwin', '', '')): - comm_mock = mock.Mock() - comm_attrs = {'communicate.return_value': - ('42.42.42', 'error')} - comm_mock.configure_mock(**comm_attrs) # pylint: disable=star-args - popen_mock.return_value = comm_mock - self.assertEqual(get_os_info()[0], 'darwin') - self.assertEqual(get_os_info()[1], '42.42.42') - - with mock.patch('platform.system_alias', - return_value=('linux', '', '')): - with mock.patch('platform.linux_distribution', - return_value=('', '', '')): - self.assertEqual(get_python_os_info(), ("linux", "")) - - with mock.patch('platform.linux_distribution', - return_value=('testdist', '42', '')): - self.assertEqual(get_python_os_info(), ("testdist", "42")) - - with mock.patch('platform.system_alias', - return_value=('freebsd', '9.3-RC3-p1', '')): - self.assertEqual(get_python_os_info(), ("freebsd", "9")) - - with mock.patch('platform.system_alias', - return_value=('windows', '', '')): - with mock.patch('platform.win32_ver', - return_value=('4242', '95', '2', '')): - self.assertEqual(get_python_os_info(), - ("windows", "95")) - - -class AtexitRegisterTest(unittest.TestCase): - """Tests for certbot.util.atexit_register.""" - def setUp(self): - self.func = mock.MagicMock() - self.args = ('hi',) - self.kwargs = {'answer': 42} - - @classmethod - def _call(cls, *args, **kwargs): - from certbot.util import atexit_register - return atexit_register(*args, **kwargs) - - def test_called(self): - self._test_common(os.getpid()) - self.func.assert_called_with(*self.args, **self.kwargs) - - def test_not_called(self): - self._test_common(initial_pid=-1) - self.assertFalse(self.func.called) - - def _test_common(self, initial_pid): - with mock.patch('certbot.util._INITIAL_PID', initial_pid): - with mock.patch('certbot.util.atexit') as mock_atexit: - self._call(self.func, *self.args, **self.kwargs) - - # _INITAL_PID must be mocked when calling atexit_func - self.assertTrue(mock_atexit.register.called) - args, kwargs = mock_atexit.register.call_args - atexit_func = args[0] - atexit_func(*args[1:], **kwargs) # pylint: disable=star-args - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/certbot/updater.py b/certbot/updater.py deleted file mode 100644 index 58df6fcb4b4..00000000000 --- a/certbot/updater.py +++ /dev/null @@ -1,122 +0,0 @@ -"""Updaters run at renewal""" -import logging - -from certbot import errors -from certbot import interfaces - -from certbot.plugins import selection as plug_sel -import certbot.plugins.enhancements as enhancements - -logger = logging.getLogger(__name__) - -def run_generic_updaters(config, lineage, plugins): - """Run updaters that the plugin supports - - :param config: Configuration object - :type config: interfaces.IConfig - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :param plugins: List of plugins - :type plugins: `list` of `str` - - :returns: `None` - :rtype: None - """ - if config.dry_run: - logger.debug("Skipping updaters in dry-run mode.") - return - try: - installer = plug_sel.get_unprepared_installer(config, plugins) - except errors.Error as e: - logger.warning("Could not choose appropriate plugin for updaters: %s", e) - return - if installer: - _run_updaters(lineage, installer, config) - _run_enhancement_updaters(lineage, installer, config) - -def run_renewal_deployer(config, lineage, installer): - """Helper function to run deployer interface method if supported by the used - installer plugin. - - :param config: Configuration object - :type config: interfaces.IConfig - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :param installer: Installer object - :type installer: interfaces.IInstaller - - :returns: `None` - :rtype: None - """ - if config.dry_run: - logger.debug("Skipping renewal deployer in dry-run mode.") - return - - if not config.disable_renew_updates and isinstance(installer, - interfaces.RenewDeployer): - installer.renew_deploy(lineage) - _run_enhancement_deployers(lineage, installer, config) - -def _run_updaters(lineage, installer, config): - """Helper function to run the updater interface methods if supported by the - used installer plugin. - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :param installer: Installer object - :type installer: interfaces.IInstaller - - :returns: `None` - :rtype: None - """ - if not config.disable_renew_updates: - if isinstance(installer, interfaces.GenericUpdater): - installer.generic_updates(lineage) - -def _run_enhancement_updaters(lineage, installer, config): - """Iterates through known enhancement interfaces. If the installer implements - an enhancement interface and the enhance interface has an updater method, the - updater method gets run. - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :param installer: Installer object - :type installer: interfaces.IInstaller - - :param config: Configuration object - :type config: interfaces.IConfig - """ - - if config.disable_renew_updates: - return - for enh in enhancements._INDEX: # pylint: disable=protected-access - if isinstance(installer, enh["class"]) and enh["updater_function"]: - getattr(installer, enh["updater_function"])(lineage) - - -def _run_enhancement_deployers(lineage, installer, config): - """Iterates through known enhancement interfaces. If the installer implements - an enhancement interface and the enhance interface has an deployer method, the - deployer method gets run. - - :param lineage: Certificate lineage object - :type lineage: storage.RenewableCert - - :param installer: Installer object - :type installer: interfaces.IInstaller - - :param config: Configuration object - :type config: interfaces.IConfig - """ - - if config.disable_renew_updates: - return - for enh in enhancements._INDEX: # pylint: disable=protected-access - if isinstance(installer, enh["class"]) and enh["deployer_function"]: - getattr(installer, enh["deployer_function"])(lineage) diff --git a/certbot/util.py b/certbot/util.py deleted file mode 100644 index d7c5424658d..00000000000 --- a/certbot/util.py +++ /dev/null @@ -1,665 +0,0 @@ -"""Utilities for all Certbot.""" -import argparse -import atexit -import collections -# distutils.version under virtualenv confuses pylint -# For more info, see: https://github.com/PyCQA/pylint/issues/73 -import distutils.version # pylint: disable=import-error,no-name-in-module -import errno -import logging -import os -import platform -import re -import six -import socket -import subprocess -import sys - -from collections import OrderedDict - -import configargparse - -from acme.magic_typing import Tuple, Union # pylint: disable=unused-import, no-name-in-module -from certbot import compat -from certbot import constants -from certbot import errors -from certbot import lock - - -logger = logging.getLogger(__name__) - - -Key = collections.namedtuple("Key", "file pem") -# Note: form is the type of data, "pem" or "der" -CSR = collections.namedtuple("CSR", "file data form") - - -# ANSI SGR escape codes -# Formats text as bold or with increased intensity -ANSI_SGR_BOLD = '\033[1m' -# Colors text red -ANSI_SGR_RED = "\033[31m" -# Resets output format -ANSI_SGR_RESET = "\033[0m" - - -PERM_ERR_FMT = os.linesep.join(( - "The following error was encountered:", "{0}", - "Either run as root, or set --config-dir, " - "--work-dir, and --logs-dir to writeable paths.")) - - -# Stores importing process ID to be used by atexit_register() -_INITIAL_PID = os.getpid() -# Maps paths to locked directories to their lock object. All locks in -# the dict are attempted to be cleaned up at program exit. If the -# program exits before the lock is cleaned up, it is automatically -# released, but the file isn't deleted. -_LOCKS = OrderedDict() # type: OrderedDict[str, lock.LockFile] - - -def run_script(params, log=logger.error): - """Run the script with the given params. - - :param list params: List of parameters to pass to Popen - :param logging.Logger log: Logger to use for errors - - """ - try: - proc = subprocess.Popen(params, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True) - - except (OSError, ValueError): - msg = "Unable to run the command: %s" % " ".join(params) - log(msg) - raise errors.SubprocessError(msg) - - stdout, stderr = proc.communicate() - - if proc.returncode != 0: - msg = "Error while running %s.\n%s\n%s" % ( - " ".join(params), stdout, stderr) - # Enter recovery routine... - log(msg) - raise errors.SubprocessError(msg) - - return stdout, stderr - - -def is_exe(path): - """Is path an executable file? - - :param str path: path to test - - :returns: True iff path is an executable file - :rtype: bool - - """ - return os.path.isfile(path) and os.access(path, os.X_OK) - - -def exe_exists(exe): - """Determine whether path/name refers to an executable. - - :param str exe: Executable path or name - - :returns: If exe is a valid executable - :rtype: bool - - """ - path, _ = os.path.split(exe) - if path: - return is_exe(exe) - else: - for path in os.environ["PATH"].split(os.pathsep): - if is_exe(os.path.join(path, exe)): - return True - - return False - - -def lock_dir_until_exit(dir_path): - """Lock the directory at dir_path until program exit. - - :param str dir_path: path to directory - - :raises errors.LockError: if the lock is held by another process - - """ - if not _LOCKS: # this is the first lock to be released at exit - atexit_register(_release_locks) - - if dir_path not in _LOCKS: - _LOCKS[dir_path] = lock.lock_dir(dir_path) - - -def _release_locks(): - for dir_lock in six.itervalues(_LOCKS): - try: - dir_lock.release() - except: # pylint: disable=bare-except - msg = 'Exception occurred releasing lock: {0!r}'.format(dir_lock) - logger.debug(msg, exc_info=True) - - -def set_up_core_dir(directory, mode, uid, strict): - """Ensure directory exists with proper permissions and is locked. - - :param str directory: Path to a directory. - :param int mode: Directory mode. - :param int uid: Directory owner. - :param bool strict: require directory to be owned by current user - - :raises .errors.LockError: if the directory cannot be locked - :raises .errors.Error: if the directory cannot be made or verified - - """ - try: - make_or_verify_dir(directory, mode, uid, strict) - lock_dir_until_exit(directory) - except OSError as error: - logger.debug("Exception was:", exc_info=True) - raise errors.Error(PERM_ERR_FMT.format(error)) - - -def make_or_verify_dir(directory, mode=0o755, uid=0, strict=False): - """Make sure directory exists with proper permissions. - - :param str directory: Path to a directory. - :param int mode: Directory mode. - :param int uid: Directory owner. - :param bool strict: require directory to be owned by current user - - :raises .errors.Error: if a directory already exists, - but has wrong permissions or owner - - :raises OSError: if invalid or inaccessible file names and - paths, or other arguments that have the correct type, - but are not accepted by the operating system. - - """ - try: - os.makedirs(directory, mode) - except OSError as exception: - if exception.errno == errno.EEXIST: - if strict and not check_permissions(directory, mode, uid): - raise errors.Error( - "%s exists, but it should be owned by user %d with" - "permissions %s" % (directory, uid, oct(mode))) - else: - raise - - -def check_permissions(filepath, mode, uid=0): - """Check file or directory permissions. - - :param str filepath: Path to the tested file (or directory). - :param int mode: Expected file mode. - :param int uid: Expected file owner. - - :returns: True if `mode` and `uid` match, False otherwise. - :rtype: bool - - """ - file_stat = os.stat(filepath) - return compat.compare_file_modes(file_stat.st_mode, mode) and file_stat.st_uid == uid - - -def safe_open(path, mode="w", chmod=None, buffering=None): - """Safely open a file. - - :param str path: Path to a file. - :param str mode: Same os `mode` for `open`. - :param int chmod: Same as `mode` for `os.open`, uses Python defaults - if ``None``. - :param int buffering: Same as `bufsize` for `os.fdopen`, uses Python - defaults if ``None``. - - """ - # pylint: disable=star-args - open_args = () # type: Union[Tuple[()], Tuple[int]] - if chmod is not None: - open_args = (chmod,) - fdopen_args = () # type: Union[Tuple[()], Tuple[int]] - if buffering is not None: - fdopen_args = (buffering,) - return os.fdopen( - os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, *open_args), - mode, *fdopen_args) - - -def _unique_file(path, filename_pat, count, chmod, mode): - while True: - current_path = os.path.join(path, filename_pat(count)) - try: - return safe_open(current_path, chmod=chmod, mode=mode),\ - os.path.abspath(current_path) - except OSError as err: - # "File exists," is okay, try a different name. - if err.errno != errno.EEXIST: - raise - count += 1 - - -def unique_file(path, chmod=0o777, mode="w"): - """Safely finds a unique file. - - :param str path: path/filename.ext - :param int chmod: File mode - :param str mode: Open mode - - :returns: tuple of file object and file name - - """ - path, tail = os.path.split(path) - return _unique_file( - path, filename_pat=(lambda count: "%04d_%s" % (count, tail)), - count=0, chmod=chmod, mode=mode) - - -def unique_lineage_name(path, filename, chmod=0o644, mode="w"): - """Safely finds a unique file using lineage convention. - - :param str path: directory path - :param str filename: proposed filename - :param int chmod: file mode - :param str mode: open mode - - :returns: tuple of file object and file name (which may be modified - from the requested one by appending digits to ensure uniqueness) - - :raises OSError: if writing files fails for an unanticipated reason, - such as a full disk or a lack of permission to write to - specified location. - - """ - preferred_path = os.path.join(path, "%s.conf" % (filename)) - try: - return safe_open(preferred_path, chmod=chmod), preferred_path - except OSError as err: - if err.errno != errno.EEXIST: - raise - return _unique_file( - path, filename_pat=(lambda count: "%s-%04d.conf" % (filename, count)), - count=1, chmod=chmod, mode=mode) - - -def safely_remove(path): - """Remove a file that may not exist.""" - try: - os.remove(path) - except OSError as err: - if err.errno != errno.ENOENT: - raise - - -def get_filtered_names(all_names): - """Removes names that aren't considered valid by Let's Encrypt. - - :param set all_names: all names found in the configuration - - :returns: all found names that are considered valid by LE - :rtype: set - - """ - filtered_names = set() - for name in all_names: - try: - filtered_names.add(enforce_le_validity(name)) - except errors.ConfigurationError: - logger.debug('Not suggesting name "%s"', name, exc_info=True) - return filtered_names - - -def get_os_info(filepath="/etc/os-release"): - """ - Get OS name and version - - :param str filepath: File path of os-release file - :returns: (os_name, os_version) - :rtype: `tuple` of `str` - """ - - if os.path.isfile(filepath): - # Systemd os-release parsing might be viable - os_name, os_version = get_systemd_os_info(filepath=filepath) - if os_name: - return (os_name, os_version) - - # Fallback to platform module - return get_python_os_info() - - -def get_os_info_ua(filepath="/etc/os-release"): - """ - Get OS name and version string for User Agent - - :param str filepath: File path of os-release file - :returns: os_ua - :rtype: `str` - """ - - if os.path.isfile(filepath): - os_ua = get_var_from_file("PRETTY_NAME", filepath=filepath) - if not os_ua: - os_ua = get_var_from_file("NAME", filepath=filepath) - if os_ua: - return os_ua - - # Fallback - return " ".join(get_python_os_info()) - - -def get_systemd_os_info(filepath="/etc/os-release"): - """ - Parse systemd /etc/os-release for distribution information - - :param str filepath: File path of os-release file - :returns: (os_name, os_version) - :rtype: `tuple` of `str` - """ - - os_name = get_var_from_file("ID", filepath=filepath) - os_version = get_var_from_file("VERSION_ID", filepath=filepath) - - return (os_name, os_version) - - -def get_systemd_os_like(filepath="/etc/os-release"): - """ - Get a list of strings that indicate the distribution likeness to - other distributions. - - :param str filepath: File path of os-release file - :returns: List of distribution acronyms - :rtype: `list` of `str` - """ - - return get_var_from_file("ID_LIKE", filepath).split(" ") - - -def get_var_from_file(varname, filepath="/etc/os-release"): - """ - Get single value from systemd /etc/os-release - - :param str varname: Name of variable to fetch - :param str filepath: File path of os-release file - :returns: requested value - :rtype: `str` - """ - - var_string = varname+"=" - if not os.path.isfile(filepath): - return "" - with open(filepath, 'r') as fh: - contents = fh.readlines() - - for line in contents: - if line.strip().startswith(var_string): - # Return the value of var, normalized - return _normalize_string(line.strip()[len(var_string):]) - return "" - - -def _normalize_string(orig): - """ - Helper function for get_var_from_file() to remove quotes - and whitespaces - """ - return orig.replace('"', '').replace("'", "").strip() - - -def get_python_os_info(): - """ - Get Operating System type/distribution and major version - using python platform module - - :returns: (os_name, os_version) - :rtype: `tuple` of `str` - """ - info = platform.system_alias( - platform.system(), - platform.release(), - platform.version() - ) - os_type, os_ver, _ = info - os_type = os_type.lower() - if os_type.startswith('linux'): - info = platform.linux_distribution() - # On arch, platform.linux_distribution() is reportedly ('','',''), - # so handle it defensively - if info[0]: - os_type = info[0] - if info[1]: - os_ver = info[1] - elif os_type.startswith('darwin'): - try: - proc = subprocess.Popen( - ["/usr/bin/sw_vers", "-productVersion"], - stdout=subprocess.PIPE, - universal_newlines=True, - ) - except OSError: - proc = subprocess.Popen( - ["sw_vers", "-productVersion"], - stdout=subprocess.PIPE, - universal_newlines=True, - ) - os_ver = proc.communicate()[0].rstrip('\n') - elif os_type.startswith('freebsd'): - # eg "9.3-RC3-p1" - os_ver = os_ver.partition("-")[0] - os_ver = os_ver.partition(".")[0] - elif platform.win32_ver()[1]: - os_ver = platform.win32_ver()[1] - else: - # Cases known to fall here: Cygwin python - os_ver = '' - return os_type, os_ver - -# Just make sure we don't get pwned... Make sure that it also doesn't -# start with a period or have two consecutive periods <- this needs to -# be done in addition to the regex -EMAIL_REGEX = re.compile("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+$") - - -def safe_email(email): - """Scrub email address before using it.""" - if EMAIL_REGEX.match(email) is not None: - return not email.startswith(".") and ".." not in email - else: - logger.warning("Invalid email address: %s.", email) - return False - - -class _ShowWarning(argparse.Action): - """Action to log a warning when an argument is used.""" - def __call__(self, unused1, unused2, unused3, option_string=None): - sys.stderr.write( - "Use of {0} is deprecated.\n".format(option_string)) - - -def add_deprecated_argument(add_argument, argument_name, nargs): - """Adds a deprecated argument with the name argument_name. - - Deprecated arguments are not shown in the help. If they are used on - the command line, a warning is shown stating that the argument is - deprecated and no other action is taken. - - :param callable add_argument: Function that adds arguments to an - argument parser/group. - :param str argument_name: Name of deprecated argument. - :param nargs: Value for nargs when adding the argument to argparse. - - """ - if _ShowWarning not in configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE: - # In version 0.12.0 ACTION_TYPES_THAT_DONT_NEED_A_VALUE was - # changed from a set to a tuple. - if isinstance(configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE, set): - # pylint: disable=no-member - configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE.add( - _ShowWarning) - else: - configargparse.ACTION_TYPES_THAT_DONT_NEED_A_VALUE += ( - _ShowWarning,) - add_argument(argument_name, action=_ShowWarning, - help=argparse.SUPPRESS, nargs=nargs) - - -def enforce_le_validity(domain): - """Checks that Let's Encrypt will consider domain to be valid. - - :param str domain: FQDN to check - :type domain: `str` or `unicode` - :returns: The domain cast to `str`, with ASCII-only contents - :rtype: str - :raises ConfigurationError: for invalid domains and cases where Let's - Encrypt currently will not issue certificates - - """ - domain = enforce_domain_sanity(domain) - if not re.match("^[A-Za-z0-9.-]*$", domain): - raise errors.ConfigurationError( - "{0} contains an invalid character. " - "Valid characters are A-Z, a-z, 0-9, ., and -.".format(domain)) - - labels = domain.split(".") - if len(labels) < 2: - raise errors.ConfigurationError( - "{0} needs at least two labels".format(domain)) - for label in labels: - if label.startswith("-"): - raise errors.ConfigurationError( - 'label "{0}" in domain "{1}" cannot start with "-"'.format( - label, domain)) - if label.endswith("-"): - raise errors.ConfigurationError( - 'label "{0}" in domain "{1}" cannot end with "-"'.format( - label, domain)) - return domain - -def enforce_domain_sanity(domain): - """Method which validates domain value and errors out if - the requirements are not met. - - :param domain: Domain to check - :type domain: `str` or `unicode` - :raises ConfigurationError: for invalid domains and cases where Let's - Encrypt currently will not issue certificates - - :returns: The domain cast to `str`, with ASCII-only contents - :rtype: str - """ - # Unicode - try: - if isinstance(domain, six.binary_type): - domain = domain.decode('utf-8') - domain.encode('ascii') - except UnicodeError: - raise errors.ConfigurationError("Non-ASCII domain names not supported. " - "To issue for an Internationalized Domain Name, use Punycode.") - - domain = domain.lower() - - # Remove trailing dot - domain = domain[:-1] if domain.endswith(u'.') else domain - - # Separately check for odd "domains" like "http://example.com" to fail - # fast and provide a clear error message - for scheme in ["http", "https"]: # Other schemes seem unlikely - if domain.startswith("{0}://".format(scheme)): - raise errors.ConfigurationError( - "Requested name {0} appears to be a URL, not a FQDN. " - "Try again without the leading \"{1}://\".".format( - domain, scheme - ) - ) - - # Explain separately that IP addresses aren't allowed (apart from not - # being FQDNs) because hope springs eternal concerning this point - try: - socket.inet_aton(domain) - raise errors.ConfigurationError( - "Requested name {0} is an IP address. The Let's Encrypt " - "certificate authority will not issue certificates for a " - "bare IP address.".format(domain)) - except socket.error: - # It wasn't an IP address, so that's good - pass - - # FQDN checks according to RFC 2181: domain name should be less than 255 - # octets (inclusive). And each label is 1 - 63 octets (inclusive). - # https://tools.ietf.org/html/rfc2181#section-11 - msg = "Requested domain {0} is not a FQDN because".format(domain) - if len(domain) > 255: - raise errors.ConfigurationError("{0} it is too long.".format(msg)) - labels = domain.split('.') - for l in labels: - if not l: - raise errors.ConfigurationError("{0} it contains an empty label.".format(msg)) - elif len(l) > 63: - raise errors.ConfigurationError("{0} label {1} is too long.".format(msg, l)) - - return domain - - -def is_wildcard_domain(domain): - """"Is domain a wildcard domain? - - :param domain: domain to check - :type domain: `bytes` or `str` or `unicode` - - :returns: True if domain is a wildcard, otherwise, False - :rtype: bool - - """ - if isinstance(domain, six.text_type): - wildcard_marker = u"*." - else: - wildcard_marker = b"*." - - return domain.startswith(wildcard_marker) - - -def get_strict_version(normalized): - """Converts a normalized version to a strict version. - - :param str normalized: normalized version string - - :returns: An equivalent strict version - :rtype: distutils.version.StrictVersion - - """ - # strict version ending with "a" and a number designates a pre-release - # pylint: disable=no-member - return distutils.version.StrictVersion(normalized.replace(".dev", "a")) - - -def is_staging(srv): - """ - Determine whether a given ACME server is a known test / staging server. - - :param str srv: the URI for the ACME server - :returns: True iff srv is a known test / staging server - :rtype bool: - """ - return srv == constants.STAGING_URI or "staging" in srv - - -def atexit_register(func, *args, **kwargs): - """Sets func to be called before the program exits. - - Special care is taken to ensure func is only called when the process - that first imports this module exits rather than any child processes. - - :param function func: function to be called in case of an error - - """ - atexit.register(_atexit_call, func, *args, **kwargs) - - -def _atexit_call(func, *args, **kwargs): - if _INITIAL_PID == os.getpid(): - func(*args, **kwargs) diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 75a5b9aab0d..00000000000 --- a/docker-compose.yml +++ /dev/null @@ -1,19 +0,0 @@ -version: '2' -services: - production: - build: . - ports: - - "443:443" - -# For development, mount git root to /opt/certbot/src in order to -# make the dev workflow more vagrant-like. - development: - build: - context: . - dockerfile: Dockerfile-dev - ports: - - "80:80" - - "443:443" - volumes: - - .:/opt/certbot/src - - /opt/certbot/venv diff --git a/docs/.gitignore b/docs/.gitignore deleted file mode 100644 index ba65b13af5e..00000000000 --- a/docs/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/_build/ diff --git a/docs/Makefile b/docs/Makefile deleted file mode 100644 index 415eebca195..00000000000 --- a/docs/Makefile +++ /dev/null @@ -1,183 +0,0 @@ -# Makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -PAPER = -BUILDDIR = _build - -# User-friendly check for sphinx-build -ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) -$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) -endif - -# Internal variables. -PAPEROPT_a4 = -D latex_paper_size=a4 -PAPEROPT_letter = -D latex_paper_size=letter -ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . -# the i18n builder cannot share the environment and doctrees with the others -I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . - -.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext - -help: - @echo "Please use \`make ' where is one of" - @echo " html to make standalone HTML files" - @echo " dirhtml to make HTML files named index.html in directories" - @echo " singlehtml to make a single large HTML file" - @echo " pickle to make pickle files" - @echo " json to make JSON files" - @echo " htmlhelp to make HTML files and a HTML help project" - @echo " qthelp to make HTML files and a qthelp project" - @echo " devhelp to make HTML files and a Devhelp project" - @echo " epub to make an epub" - @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" - @echo " latexpdf to make LaTeX files and run them through pdflatex" - @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" - @echo " text to make text files" - @echo " man to make manual pages" - @echo " texinfo to make Texinfo files" - @echo " info to make Texinfo files and run them through makeinfo" - @echo " gettext to make PO message catalogs" - @echo " changes to make an overview of all changed/added/deprecated items" - @echo " xml to make Docutils-native XML files" - @echo " pseudoxml to make pseudoxml-XML files for display purposes" - @echo " linkcheck to check all external links for integrity" - @echo " doctest to run all doctests embedded in the documentation (if enabled)" - @echo " coverage to run coverage check of the documentation (if enabled)" - -clean: - rm -rf $(BUILDDIR)/* - -html: - $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." - -dirhtml: - $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." - -singlehtml: - $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml - @echo - @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." - -pickle: - $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle - @echo - @echo "Build finished; now you can process the pickle files." - -json: - $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json - @echo - @echo "Build finished; now you can process the JSON files." - -htmlhelp: - $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp - @echo - @echo "Build finished; now you can run HTML Help Workshop with the" \ - ".hhp project file in $(BUILDDIR)/htmlhelp." - -qthelp: - $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp - @echo - @echo "Build finished; now you can run "qcollectiongenerator" with the" \ - ".qhcp project file in $(BUILDDIR)/qthelp, like this:" - @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/LetsEncrypt.qhcp" - @echo "To view the help file:" - @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/LetsEncrypt.qhc" - -devhelp: - $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp - @echo - @echo "Build finished." - @echo "To view the help file:" - @echo "# mkdir -p $$HOME/.local/share/devhelp/LetsEncrypt" - @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/LetsEncrypt" - @echo "# devhelp" - -epub: - $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub - @echo - @echo "Build finished. The epub file is in $(BUILDDIR)/epub." - -latex: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo - @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." - @echo "Run \`make' in that directory to run these through (pdf)latex" \ - "(use \`make latexpdf' here to do that automatically)." - -latexpdf: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through pdflatex..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -latexpdfja: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through platex and dvipdfmx..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -text: - $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text - @echo - @echo "Build finished. The text files are in $(BUILDDIR)/text." - -man: - $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man - @echo - @echo "Build finished. The manual pages are in $(BUILDDIR)/man." - -texinfo: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo - @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." - @echo "Run \`make' in that directory to run these through makeinfo" \ - "(use \`make info' here to do that automatically)." - -info: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo "Running Texinfo files through makeinfo..." - make -C $(BUILDDIR)/texinfo info - @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." - -gettext: - $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale - @echo - @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." - -changes: - $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes - @echo - @echo "The overview file is in $(BUILDDIR)/changes." - -linkcheck: - $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck - @echo - @echo "Link check complete; look for any errors in the above output " \ - "or in $(BUILDDIR)/linkcheck/output.txt." - -doctest: - $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest - @echo "Testing of doctests in the sources finished, look at the " \ - "results in $(BUILDDIR)/doctest/output.txt." - -coverage: - $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage - @echo "Testing of coverage in the sources finished, look at the " \ - "results in $(BUILDDIR)/coverage/python.txt." - -xml: - $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml - @echo - @echo "Build finished. The XML files are in $(BUILDDIR)/xml." - -pseudoxml: - $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml - @echo - @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/docs/api.rst b/docs/api.rst deleted file mode 100644 index 8668ec5d8ad..00000000000 --- a/docs/api.rst +++ /dev/null @@ -1,8 +0,0 @@ -================= -API Documentation -================= - -.. toctree:: - :glob: - - api/** diff --git a/docs/api/account.rst b/docs/api/account.rst deleted file mode 100644 index fd90230ea51..00000000000 --- a/docs/api/account.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.account` --------------------------- - -.. automodule:: certbot.account - :members: diff --git a/docs/api/achallenges.rst b/docs/api/achallenges.rst deleted file mode 100644 index 90dda3f060e..00000000000 --- a/docs/api/achallenges.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.achallenges` ------------------------------- - -.. automodule:: certbot.achallenges - :members: diff --git a/docs/api/auth_handler.rst b/docs/api/auth_handler.rst deleted file mode 100644 index 8819bb1bdd5..00000000000 --- a/docs/api/auth_handler.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.auth_handler` -------------------------------- - -.. automodule:: certbot.auth_handler - :members: diff --git a/docs/api/cert_manager.rst b/docs/api/cert_manager.rst deleted file mode 100644 index c8c86f8b999..00000000000 --- a/docs/api/cert_manager.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.cert_manager` -------------------------------- - -.. automodule:: certbot.cert_manager - :members: diff --git a/docs/api/cli.rst b/docs/api/cli.rst deleted file mode 100644 index 91be8675817..00000000000 --- a/docs/api/cli.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.cli` ----------------------- - -.. automodule:: certbot.cli - :members: diff --git a/docs/api/client.rst b/docs/api/client.rst deleted file mode 100644 index 00a443cd9af..00000000000 --- a/docs/api/client.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.client` -------------------------- - -.. automodule:: certbot.client - :members: diff --git a/docs/api/configuration.rst b/docs/api/configuration.rst deleted file mode 100644 index 4e99c73d2c8..00000000000 --- a/docs/api/configuration.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.configuration` --------------------------------- - -.. automodule:: certbot.configuration - :members: diff --git a/docs/api/constants.rst b/docs/api/constants.rst deleted file mode 100644 index 99ecc240ac3..00000000000 --- a/docs/api/constants.rst +++ /dev/null @@ -1,9 +0,0 @@ -:mod:`certbot.constants` ------------------------------------ - -.. automodule:: certbot.constants - :members: - :exclude-members: SSL_DHPARAMS_SRC - -.. autodata:: SSL_DHPARAMS_SRC - :annotation: = '/path/to/certbot/ssl-dhparams.pem' diff --git a/docs/api/crypto_util.rst b/docs/api/crypto_util.rst deleted file mode 100644 index 2f473944c84..00000000000 --- a/docs/api/crypto_util.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.crypto_util` ------------------------------- - -.. automodule:: certbot.crypto_util - :members: diff --git a/docs/api/display.rst b/docs/api/display.rst deleted file mode 100644 index 1a18e653449..00000000000 --- a/docs/api/display.rst +++ /dev/null @@ -1,23 +0,0 @@ -:mod:`certbot.display` --------------------------- - -.. automodule:: certbot.display - :members: - -:mod:`certbot.display.util` -=============================== - -.. automodule:: certbot.display.util - :members: - -:mod:`certbot.display.ops` -============================== - -.. automodule:: certbot.display.ops - :members: - -:mod:`certbot.display.enhancements` -======================================= - -.. automodule:: certbot.display.enhancements - :members: diff --git a/docs/api/eff.rst b/docs/api/eff.rst deleted file mode 100644 index 2924b256deb..00000000000 --- a/docs/api/eff.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.eff` ----------------------- - -.. automodule:: certbot.eff - :members: diff --git a/docs/api/error_handler.rst b/docs/api/error_handler.rst deleted file mode 100644 index f1306177d77..00000000000 --- a/docs/api/error_handler.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.error_handler` --------------------------------- - -.. automodule:: certbot.error_handler - :members: diff --git a/docs/api/errors.rst b/docs/api/errors.rst deleted file mode 100644 index a9324765b85..00000000000 --- a/docs/api/errors.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.errors` -------------------------- - -.. automodule:: certbot.errors - :members: diff --git a/docs/api/hooks.rst b/docs/api/hooks.rst deleted file mode 100644 index 140fbf2842c..00000000000 --- a/docs/api/hooks.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.hooks` ------------------------- - -.. automodule:: certbot.hooks - :members: diff --git a/docs/api/index.rst b/docs/api/index.rst deleted file mode 100644 index be94214c9b4..00000000000 --- a/docs/api/index.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot` ------------------- - -.. automodule:: certbot - :members: diff --git a/docs/api/interfaces.rst b/docs/api/interfaces.rst deleted file mode 100644 index 2988b3b872a..00000000000 --- a/docs/api/interfaces.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.interfaces` ------------------------------ - -.. automodule:: certbot.interfaces - :members: diff --git a/docs/api/lock.rst b/docs/api/lock.rst deleted file mode 100644 index 6dcbf95896b..00000000000 --- a/docs/api/lock.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.lock` ------------------------ - -.. automodule:: certbot.lock - :members: diff --git a/docs/api/log.rst b/docs/api/log.rst deleted file mode 100644 index 41311de90e0..00000000000 --- a/docs/api/log.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.log` ----------------------- - -.. automodule:: certbot.log - :members: diff --git a/docs/api/main.rst b/docs/api/main.rst deleted file mode 100644 index a555bab01fd..00000000000 --- a/docs/api/main.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.main` ------------------------ - -.. automodule:: certbot.main - :members: diff --git a/docs/api/notify.rst b/docs/api/notify.rst deleted file mode 100644 index fa042b2d2ef..00000000000 --- a/docs/api/notify.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.notify` -------------------------- - -.. automodule:: certbot.notify - :members: diff --git a/docs/api/ocsp.rst b/docs/api/ocsp.rst deleted file mode 100644 index 7044f405242..00000000000 --- a/docs/api/ocsp.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.ocsp` ------------------------ - -.. automodule:: certbot.ocsp - :members: diff --git a/docs/api/plugins/common.rst b/docs/api/plugins/common.rst deleted file mode 100644 index 7cfaf8d70f5..00000000000 --- a/docs/api/plugins/common.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.plugins.common` ---------------------------------- - -.. automodule:: certbot.plugins.common - :members: diff --git a/docs/api/plugins/disco.rst b/docs/api/plugins/disco.rst deleted file mode 100644 index 1a27f0f69c2..00000000000 --- a/docs/api/plugins/disco.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.plugins.disco` --------------------------------- - -.. automodule:: certbot.plugins.disco - :members: diff --git a/docs/api/plugins/dns_common.rst b/docs/api/plugins/dns_common.rst deleted file mode 100644 index ee3945e74a1..00000000000 --- a/docs/api/plugins/dns_common.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.plugins.dns_common` ---------------------------------- - -.. automodule:: certbot.plugins.dns_common - :members: diff --git a/docs/api/plugins/dns_common_lexicon.rst b/docs/api/plugins/dns_common_lexicon.rst deleted file mode 100644 index a4816682803..00000000000 --- a/docs/api/plugins/dns_common_lexicon.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.plugins.dns_common_lexicon` ------------------------------------------ - -.. automodule:: certbot.plugins.dns_common_lexicon - :members: diff --git a/docs/api/plugins/manual.rst b/docs/api/plugins/manual.rst deleted file mode 100644 index eea4434990f..00000000000 --- a/docs/api/plugins/manual.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.plugins.manual` ---------------------------------- - -.. automodule:: certbot.plugins.manual - :members: diff --git a/docs/api/plugins/selection.rst b/docs/api/plugins/selection.rst deleted file mode 100644 index 6211bf9c003..00000000000 --- a/docs/api/plugins/selection.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.plugins.selection` ------------------------------------- - -.. automodule:: certbot.plugins.selection - :members: diff --git a/docs/api/plugins/standalone.rst b/docs/api/plugins/standalone.rst deleted file mode 100644 index 60aa48b4f1d..00000000000 --- a/docs/api/plugins/standalone.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.plugins.standalone` -------------------------------------- - -.. automodule:: certbot.plugins.standalone - :members: diff --git a/docs/api/plugins/util.rst b/docs/api/plugins/util.rst deleted file mode 100644 index 30ab3d49f4d..00000000000 --- a/docs/api/plugins/util.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.plugins.util` -------------------------------- - -.. automodule:: certbot.plugins.util - :members: diff --git a/docs/api/plugins/webroot.rst b/docs/api/plugins/webroot.rst deleted file mode 100644 index e1f4523f7f7..00000000000 --- a/docs/api/plugins/webroot.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.plugins.webroot` ----------------------------------- - -.. automodule:: certbot.plugins.webroot - :members: diff --git a/docs/api/renewal.rst b/docs/api/renewal.rst deleted file mode 100644 index 58557351f76..00000000000 --- a/docs/api/renewal.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.renewal` --------------------------- - -.. automodule:: certbot.renewal - :members: diff --git a/docs/api/reporter.rst b/docs/api/reporter.rst deleted file mode 100644 index ad71dbb698c..00000000000 --- a/docs/api/reporter.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.reporter` ---------------------------- - -.. automodule:: certbot.reporter - :members: diff --git a/docs/api/reverter.rst b/docs/api/reverter.rst deleted file mode 100644 index 3e0ac750b8b..00000000000 --- a/docs/api/reverter.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.reverter` ---------------------------- - -.. automodule:: certbot.reverter - :members: diff --git a/docs/api/storage.rst b/docs/api/storage.rst deleted file mode 100644 index 34e3a45c0c4..00000000000 --- a/docs/api/storage.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.storage` --------------------------- - -.. automodule:: certbot.storage - :members: diff --git a/docs/api/util.rst b/docs/api/util.rst deleted file mode 100644 index 7d0e33501f1..00000000000 --- a/docs/api/util.rst +++ /dev/null @@ -1,5 +0,0 @@ -:mod:`certbot.util` --------------------------- - -.. automodule:: certbot.util - :members: diff --git a/docs/cli-help.txt b/docs/cli-help.txt deleted file mode 100644 index bad6275e790..00000000000 --- a/docs/cli-help.txt +++ /dev/null @@ -1,726 +0,0 @@ -usage: - certbot [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ... - -Certbot can obtain and install HTTPS/TLS/SSL certificates. By default, -it will attempt to use a webserver both for obtaining and installing the -certificate. The most common SUBCOMMANDS and flags are: - -obtain, install, and renew certificates: - (default) run Obtain & install a certificate in your current webserver - certonly Obtain or renew a certificate, but do not install it - renew Renew all previously obtained certificates that are near expiry - enhance Add security enhancements to your existing configuration - -d DOMAINS Comma-separated list of domains to obtain a certificate for - - --apache Use the Apache plugin for authentication & installation - --standalone Run a standalone webserver for authentication - --nginx Use the Nginx plugin for authentication & installation - --webroot Place files in a server's webroot folder for authentication - --manual Obtain certificates interactively, or using shell script hooks - - -n Run non-interactively - --test-cert Obtain a test certificate from a staging server - --dry-run Test "renew" or "certonly" without saving any certificates to disk - -manage certificates: - certificates Display information about certificates you have from Certbot - revoke Revoke a certificate (supply --cert-path or --cert-name) - delete Delete a certificate - -manage your account with Let's Encrypt: - register Create a Let's Encrypt ACME account - update_account Update a Let's Encrypt ACME account - --agree-tos Agree to the ACME server's Subscriber Agreement - -m EMAIL Email address for important account notifications - -optional arguments: - -h, --help show this help message and exit - -c CONFIG_FILE, --config CONFIG_FILE - path to config file (default: /etc/letsencrypt/cli.ini - and ~/.config/letsencrypt/cli.ini) - -v, --verbose This flag can be used multiple times to incrementally - increase the verbosity of output, e.g. -vvv. (default: - -2) - --max-log-backups MAX_LOG_BACKUPS - Specifies the maximum number of backup logs that - should be kept by Certbot's built in log rotation. - Setting this flag to 0 disables log rotation entirely, - causing Certbot to always append to the same log file. - (default: 1000) - -n, --non-interactive, --noninteractive - Run without ever asking for user input. This may - require additional command line flags; the client will - try to explain which ones are required if it finds one - missing (default: False) - --force-interactive Force Certbot to be interactive even if it detects - it's not being run in a terminal. This flag cannot be - used with the renew subcommand. (default: False) - -d DOMAIN, --domains DOMAIN, --domain DOMAIN - Domain names to apply. For multiple domains you can - use multiple -d flags or enter a comma separated list - of domains as a parameter. The first domain provided - will be the subject CN of the certificate, and all - domains will be Subject Alternative Names on the - certificate. The first domain will also be used in - some software user interfaces and as the file paths - for the certificate and related material unless - otherwise specified or you already have a certificate - with the same name. In the case of a name collision it - will append a number like 0001 to the file path name. - (default: Ask) - --eab-kid EAB_KID Key Identifier for External Account Binding (default: - None) - --eab-hmac-key EAB_HMAC_KEY - HMAC key for External Account Binding (default: None) - --cert-name CERTNAME Certificate name to apply. This name is used by - Certbot for housekeeping and in file paths; it doesn't - affect the content of the certificate itself. To see - certificate names, run 'certbot certificates'. When - creating a new certificate, specifies the new - certificate's name. (default: the first provided - domain or the name of an existing certificate on your - system for the same domains) - --dry-run Perform a test run of the client, obtaining test - (invalid) certificates but not saving them to disk. - This can currently only be used with the 'certonly' - and 'renew' subcommands. Note: Although --dry-run - tries to avoid making any persistent changes on a - system, it is not completely side-effect free: if used - with webserver authenticator plugins like apache and - nginx, it makes and then reverts temporary config - changes in order to obtain test certificates, and - reloads webservers to deploy and then roll back those - changes. It also calls --pre-hook and --post-hook - commands if they are defined because they may be - necessary to accurately simulate renewal. --deploy- - hook commands are not called. (default: False) - --debug-challenges After setting up challenges, wait for user input - before submitting to CA (default: False) - --preferred-challenges PREF_CHALLS - A sorted, comma delimited list of the preferred - challenge to use during authorization with the most - preferred challenge listed first (Eg, "dns" or "tls- - sni-01,http,dns"). Not all plugins support all - challenges. See - https://certbot.eff.org/docs/using.html#plugins for - details. ACME Challenges are versioned, but if you - pick "http" rather than "http-01", Certbot will select - the latest version automatically. (default: []) - --user-agent USER_AGENT - Set a custom user agent string for the client. User - agent strings allow the CA to collect high level - statistics about success rates by OS, plugin and use - case, and to know when to deprecate support for past - Python versions and flags. If you wish to hide this - information from the Let's Encrypt server, set this to - "". (default: CertbotACMEClient/0.30.0 - (certbot(-auto); OS_NAME OS_VERSION) Authenticator/XXX - Installer/YYY (SUBCOMMAND; flags: FLAGS) - Py/major.minor.patchlevel). The flags encoded in the - user agent are: --duplicate, --force-renew, --allow- - subset-of-names, -n, and whether any hooks are set. - --user-agent-comment USER_AGENT_COMMENT - Add a comment to the default user agent string. May be - used when repackaging Certbot or calling it from - another tool to allow additional statistical data to - be collected. Ignored if --user-agent is set. - (Example: Foo-Wrapper/1.0) (default: None) - -automation: - Flags for automating execution & other tweaks - - --keep-until-expiring, --keep, --reinstall - If the requested certificate matches an existing - certificate, always keep the existing one until it is - due for renewal (for the 'run' subcommand this means - reinstall the existing certificate). (default: Ask) - --expand If an existing certificate is a strict subset of the - requested names, always expand and replace it with the - additional names. (default: Ask) - --version show program's version number and exit - --force-renewal, --renew-by-default - If a certificate already exists for the requested - domains, renew it now, regardless of whether it is - near expiry. (Often --keep-until-expiring is more - appropriate). Also implies --expand. (default: False) - --renew-with-new-domains - If a certificate already exists for the requested - certificate name but does not match the requested - domains, renew it now, regardless of whether it is - near expiry. (default: False) - --reuse-key When renewing, use the same private key as the - existing certificate. (default: False) - --allow-subset-of-names - When performing domain validation, do not consider it - a failure if authorizations can not be obtained for a - strict subset of the requested domains. This may be - useful for allowing renewals for multiple domains to - succeed even if some domains no longer point at this - system. This option cannot be used with --csr. - (default: False) - --agree-tos Agree to the ACME Subscriber Agreement (default: Ask) - --duplicate Allow making a certificate lineage that duplicates an - existing one (both can be renewed in parallel) - (default: False) - --os-packages-only (certbot-auto only) install OS package dependencies - and then stop (default: False) - --no-self-upgrade (certbot-auto only) prevent the certbot-auto script - from upgrading itself to newer released versions - (default: Upgrade automatically) - --no-bootstrap (certbot-auto only) prevent the certbot-auto script - from installing OS-level dependencies (default: Prompt - to install OS-wide dependencies, but exit if the user - says 'No') - -q, --quiet Silence all output except errors. Useful for - automation via cron. Implies --non-interactive. - (default: False) - -security: - Security parameters & server settings - - --rsa-key-size N Size of the RSA key. (default: 2048) - --must-staple Adds the OCSP Must Staple extension to the - certificate. Autoconfigures OCSP Stapling for - supported setups (Apache version >= 2.3.3 ). (default: - False) - --redirect Automatically redirect all HTTP traffic to HTTPS for - the newly authenticated vhost. (default: Ask) - --no-redirect Do not automatically redirect all HTTP traffic to - HTTPS for the newly authenticated vhost. (default: - Ask) - --hsts Add the Strict-Transport-Security header to every HTTP - response. Forcing browser to always use SSL for the - domain. Defends against SSL Stripping. (default: None) - --uir Add the "Content-Security-Policy: upgrade-insecure- - requests" header to every HTTP response. Forcing the - browser to use https:// for every http:// resource. - (default: None) - --staple-ocsp Enables OCSP Stapling. A valid OCSP response is - stapled to the certificate that the server offers - during TLS. (default: None) - --strict-permissions Require that all configuration files are owned by the - current user; only needed if your config is somewhere - unsafe like /tmp/ (default: False) - --auto-hsts Gradually increasing max-age value for HTTP Strict - Transport Security security header (default: False) - -testing: - The following flags are meant for testing and integration purposes only. - - --test-cert, --staging - Use the staging server to obtain or revoke test - (invalid) certificates; equivalent to --server https - ://acme-staging-v02.api.letsencrypt.org/directory - (default: False) - --debug Show tracebacks in case of errors, and allow certbot- - auto execution on experimental platforms (default: - False) - --no-verify-ssl Disable verification of the ACME server's certificate. - (default: False) - --tls-sni-01-port TLS_SNI_01_PORT - Port used during tls-sni-01 challenge. This only - affects the port Certbot listens on. A conforming ACME - server will still attempt to connect on port 443. - (default: 443) - --tls-sni-01-address TLS_SNI_01_ADDRESS - The address the server listens to during tls-sni-01 - challenge. (default: ) - --http-01-port HTTP01_PORT - Port used in the http-01 challenge. This only affects - the port Certbot listens on. A conforming ACME server - will still attempt to connect on port 80. (default: - 80) - --http-01-address HTTP01_ADDRESS - The address the server listens to during http-01 - challenge. (default: ) - --break-my-certs Be willing to replace or renew valid certificates with - invalid (testing/staging) certificates (default: - False) - -paths: - Flags for changing execution paths & servers - - --cert-path CERT_PATH - Path to where certificate is saved (with auth --csr), - installed from, or revoked. (default: None) - --key-path KEY_PATH Path to private key for certificate installation or - revocation (if account key is missing) (default: None) - --fullchain-path FULLCHAIN_PATH - Accompanying path to a full certificate chain - (certificate plus chain). (default: None) - --chain-path CHAIN_PATH - Accompanying path to a certificate chain. (default: - None) - --config-dir CONFIG_DIR - Configuration directory. (default: /etc/letsencrypt) - --work-dir WORK_DIR Working directory. (default: /var/lib/letsencrypt) - --logs-dir LOGS_DIR Logs directory. (default: /var/log/letsencrypt) - --server SERVER ACME Directory Resource URI. (default: - https://acme-v02.api.letsencrypt.org/directory) - -manage: - Various subcommands and flags are available for managing your - certificates: - - certificates List certificates managed by Certbot - delete Clean up all files related to a certificate - renew Renew all certificates (or one specified with --cert- - name) - revoke Revoke a certificate specified with --cert-path or - --cert-name - update_symlinks Recreate symlinks in your /etc/letsencrypt/live/ - directory - -run: - Options for obtaining & installing certificates - -certonly: - Options for modifying how a certificate is obtained - - --csr CSR Path to a Certificate Signing Request (CSR) in DER or - PEM format. Currently --csr only works with the - 'certonly' subcommand. (default: None) - -renew: - The 'renew' subcommand will attempt to renew all certificates (or more - precisely, certificate lineages) you have previously obtained if they are - close to expiry, and print a summary of the results. By default, 'renew' - will reuse the options used to create obtain or most recently successfully - renew each certificate lineage. You can try it with `--dry-run` first. For - more fine-grained control, you can renew individual lineages with the - `certonly` subcommand. Hooks are available to run commands before and - after renewal; see https://certbot.eff.org/docs/using.html#renewal for - more information on these. - - --pre-hook PRE_HOOK Command to be run in a shell before obtaining any - certificates. Intended primarily for renewal, where it - can be used to temporarily shut down a webserver that - might conflict with the standalone plugin. This will - only be called if a certificate is actually to be - obtained/renewed. When renewing several certificates - that have identical pre-hooks, only the first will be - executed. (default: None) - --post-hook POST_HOOK - Command to be run in a shell after attempting to - obtain/renew certificates. Can be used to deploy - renewed certificates, or to restart any servers that - were stopped by --pre-hook. This is only run if an - attempt was made to obtain/renew a certificate. If - multiple renewed certificates have identical post- - hooks, only one will be run. (default: None) - --deploy-hook DEPLOY_HOOK - Command to be run in a shell once for each - successfully issued certificate. For this command, the - shell variable $RENEWED_LINEAGE will point to the - config live subdirectory (for example, - "/etc/letsencrypt/live/example.com") containing the - new certificates and keys; the shell variable - $RENEWED_DOMAINS will contain a space-delimited list - of renewed certificate domains (for example, - "example.com www.example.com" (default: None) - --disable-hook-validation - Ordinarily the commands specified for --pre-hook - /--post-hook/--deploy-hook will be checked for - validity, to see if the programs being run are in the - $PATH, so that mistakes can be caught early, even when - the hooks aren't being run just yet. The validation is - rather simplistic and fails if you use more advanced - shell constructs, so you can use this switch to - disable it. (default: False) - --no-directory-hooks Disable running executables found in Certbot's hook - directories during renewal. (default: False) - --disable-renew-updates - Disable automatic updates to your server configuration - that would otherwise be done by the selected installer - plugin, and triggered when the user executes "certbot - renew", regardless of if the certificate is renewed. - This setting does not apply to important TLS - configuration updates. (default: False) - --no-autorenew Disable auto renewal of certificates. (default: True) - -certificates: - List certificates managed by Certbot - -delete: - Options for deleting a certificate - -revoke: - Options for revocation of certificates - - --reason {unspecified,keycompromise,affiliationchanged,superseded,cessationofoperation} - Specify reason for revoking certificate. (default: - unspecified) - --delete-after-revoke - Delete certificates after revoking them. (default: - None) - --no-delete-after-revoke - Do not delete certificates after revoking them. This - option should be used with caution because the 'renew' - subcommand will attempt to renew undeleted revoked - certificates. (default: None) - -register: - Options for account registration - - --register-unsafely-without-email - Specifying this flag enables registering an account - with no email address. This is strongly discouraged, - because in the event of key loss or account compromise - you will irrevocably lose access to your account. You - will also be unable to receive notice about impending - expiration or revocation of your certificates. Updates - to the Subscriber Agreement will still affect you, and - will be effective 14 days after posting an update to - the web site. (default: False) - -m EMAIL, --email EMAIL - Email used for registration and recovery contact. Use - comma to register multiple emails, ex: - u1@example.com,u2@example.com. (default: Ask). - --eff-email Share your e-mail address with EFF (default: None) - --no-eff-email Don't share your e-mail address with EFF (default: - None) - -update_account: - Options for account modification - -unregister: - Options for account deactivation. - - --account ACCOUNT_ID Account ID to use (default: None) - -install: - Options for modifying how a certificate is deployed - -config_changes: - Options for controlling which changes are displayed - - --num NUM How many past revisions you want to be displayed - (default: None) - -rollback: - Options for rolling back server configuration changes - - --checkpoints N Revert configuration N number of checkpoints. - (default: 1) - -plugins: - Options for for the "plugins" subcommand - - --init Initialize plugins. (default: False) - --prepare Initialize and prepare plugins. (default: False) - --authenticators Limit to authenticator plugins only. (default: None) - --installers Limit to installer plugins only. (default: None) - -update_symlinks: - Recreates certificate and key symlinks in /etc/letsencrypt/live, if you - changed them by hand or edited a renewal configuration file - -enhance: - Helps to harden the TLS configuration by adding security enhancements to - already existing configuration. - -plugins: - Plugin Selection: Certbot client supports an extensible plugins - architecture. See 'certbot plugins' for a list of all installed plugins - and their names. You can force a particular plugin by setting options - provided below. Running --help will list flags specific to - that plugin. - - --configurator CONFIGURATOR - Name of the plugin that is both an authenticator and - an installer. Should not be used together with - --authenticator or --installer. (default: Ask) - -a AUTHENTICATOR, --authenticator AUTHENTICATOR - Authenticator plugin name. (default: None) - -i INSTALLER, --installer INSTALLER - Installer plugin name (also used to find domains). - (default: None) - --apache Obtain and install certificates using Apache (default: - False) - --nginx Obtain and install certificates using Nginx (default: - False) - --standalone Obtain certificates using a "standalone" webserver. - (default: False) - --manual Provide laborious manual instructions for obtaining a - certificate (default: False) - --webroot Obtain certificates by placing files in a webroot - directory. (default: False) - --dns-cloudflare Obtain certificates using a DNS TXT record (if you are - using Cloudflare for DNS). (default: False) - --dns-cloudxns Obtain certificates using a DNS TXT record (if you are - using CloudXNS for DNS). (default: False) - --dns-digitalocean Obtain certificates using a DNS TXT record (if you are - using DigitalOcean for DNS). (default: False) - --dns-dnsimple Obtain certificates using a DNS TXT record (if you are - using DNSimple for DNS). (default: False) - --dns-dnsmadeeasy Obtain certificates using a DNS TXT record (if you - areusing DNS Made Easy for DNS). (default: False) - --dns-gehirn Obtain certificates using a DNS TXT record (if you are - using Gehirn Infrastracture Service for DNS). - (default: False) - --dns-google Obtain certificates using a DNS TXT record (if you are - using Google Cloud DNS). (default: False) - --dns-linode Obtain certificates using a DNS TXT record (if you are - using Linode for DNS). (default: False) - --dns-luadns Obtain certificates using a DNS TXT record (if you are - using LuaDNS for DNS). (default: False) - --dns-nsone Obtain certificates using a DNS TXT record (if you are - using NS1 for DNS). (default: False) - --dns-ovh Obtain certificates using a DNS TXT record (if you are - using OVH for DNS). (default: False) - --dns-rfc2136 Obtain certificates using a DNS TXT record (if you are - using BIND for DNS). (default: False) - --dns-route53 Obtain certificates using a DNS TXT record (if you are - using Route53 for DNS). (default: False) - --dns-sakuracloud Obtain certificates using a DNS TXT record (if you are - using Sakura Cloud for DNS). (default: False) - -apache: - Apache Web Server plugin - - --apache-enmod APACHE_ENMOD - Path to the Apache 'a2enmod' binary (default: a2enmod) - --apache-dismod APACHE_DISMOD - Path to the Apache 'a2dismod' binary (default: - a2dismod) - --apache-le-vhost-ext APACHE_LE_VHOST_EXT - SSL vhost configuration extension (default: -le- - ssl.conf) - --apache-server-root APACHE_SERVER_ROOT - Apache server root directory (default: /etc/apache2) - --apache-vhost-root APACHE_VHOST_ROOT - Apache server VirtualHost configuration root (default: - None) - --apache-logs-root APACHE_LOGS_ROOT - Apache server logs directory (default: - /var/log/apache2) - --apache-challenge-location APACHE_CHALLENGE_LOCATION - Directory path for challenge configuration (default: - /etc/apache2) - --apache-handle-modules APACHE_HANDLE_MODULES - Let installer handle enabling required modules for you - (Only Ubuntu/Debian currently) (default: True) - --apache-handle-sites APACHE_HANDLE_SITES - Let installer handle enabling sites for you (Only - Ubuntu/Debian currently) (default: True) - --apache-ctl APACHE_CTL - Full path to Apache control script (default: - apache2ctl) - -dns-cloudflare: - Obtain certificates using a DNS TXT record (if you are using Cloudflare - for DNS). - - --dns-cloudflare-propagation-seconds DNS_CLOUDFLARE_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 10) - --dns-cloudflare-credentials DNS_CLOUDFLARE_CREDENTIALS - Cloudflare credentials INI file. (default: None) - -dns-cloudxns: - Obtain certificates using a DNS TXT record (if you are using CloudXNS for - DNS). - - --dns-cloudxns-propagation-seconds DNS_CLOUDXNS_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 30) - --dns-cloudxns-credentials DNS_CLOUDXNS_CREDENTIALS - CloudXNS credentials INI file. (default: None) - -dns-digitalocean: - Obtain certs using a DNS TXT record (if you are using DigitalOcean for - DNS). - - --dns-digitalocean-propagation-seconds DNS_DIGITALOCEAN_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 10) - --dns-digitalocean-credentials DNS_DIGITALOCEAN_CREDENTIALS - DigitalOcean credentials INI file. (default: None) - -dns-dnsimple: - Obtain certificates using a DNS TXT record (if you are using DNSimple for - DNS). - - --dns-dnsimple-propagation-seconds DNS_DNSIMPLE_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 30) - --dns-dnsimple-credentials DNS_DNSIMPLE_CREDENTIALS - DNSimple credentials INI file. (default: None) - -dns-dnsmadeeasy: - Obtain certificates using a DNS TXT record (if you are using DNS Made Easy - for DNS). - - --dns-dnsmadeeasy-propagation-seconds DNS_DNSMADEEASY_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 60) - --dns-dnsmadeeasy-credentials DNS_DNSMADEEASY_CREDENTIALS - DNS Made Easy credentials INI file. (default: None) - -dns-gehirn: - Obtain certificates using a DNS TXT record (if you are using Gehirn - Infrastracture Service for DNS). - - --dns-gehirn-propagation-seconds DNS_GEHIRN_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 30) - --dns-gehirn-credentials DNS_GEHIRN_CREDENTIALS - Gehirn Infrastracture Service credentials file. - (default: None) - -dns-google: - Obtain certificates using a DNS TXT record (if you are using Google Cloud - DNS for DNS). - - --dns-google-propagation-seconds DNS_GOOGLE_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 60) - --dns-google-credentials DNS_GOOGLE_CREDENTIALS - Path to Google Cloud DNS service account JSON file. - (See https://developers.google.com/identity/protocols/ - OAuth2ServiceAccount#creatinganaccount forinformation - about creating a service account and - https://cloud.google.com/dns/access- - control#permissions_and_roles for information about - therequired permissions.) (default: None) - -dns-linode: - Obtain certs using a DNS TXT record (if you are using Linode for DNS). - - --dns-linode-propagation-seconds DNS_LINODE_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 1200) - --dns-linode-credentials DNS_LINODE_CREDENTIALS - Linode credentials INI file. (default: None) - -dns-luadns: - Obtain certificates using a DNS TXT record (if you are using LuaDNS for - DNS). - - --dns-luadns-propagation-seconds DNS_LUADNS_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 30) - --dns-luadns-credentials DNS_LUADNS_CREDENTIALS - LuaDNS credentials INI file. (default: None) - -dns-nsone: - Obtain certificates using a DNS TXT record (if you are using NS1 for DNS). - - --dns-nsone-propagation-seconds DNS_NSONE_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 30) - --dns-nsone-credentials DNS_NSONE_CREDENTIALS - NS1 credentials file. (default: None) - -dns-ovh: - Obtain certificates using a DNS TXT record (if you are using OVH for DNS). - - --dns-ovh-propagation-seconds DNS_OVH_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 30) - --dns-ovh-credentials DNS_OVH_CREDENTIALS - OVH credentials INI file. (default: None) - -dns-rfc2136: - Obtain certificates using a DNS TXT record (if you are using BIND for - DNS). - - --dns-rfc2136-propagation-seconds DNS_RFC2136_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 60) - --dns-rfc2136-credentials DNS_RFC2136_CREDENTIALS - RFC 2136 credentials INI file. (default: None) - -dns-route53: - Obtain certificates using a DNS TXT record (if you are using AWS Route53 - for DNS). - - --dns-route53-propagation-seconds DNS_ROUTE53_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 10) - -dns-sakuracloud: - Obtain certificates using a DNS TXT record (if you are using Sakura Cloud - for DNS). - - --dns-sakuracloud-propagation-seconds DNS_SAKURACLOUD_PROPAGATION_SECONDS - The number of seconds to wait for DNS to propagate - before asking the ACME server to verify the DNS - record. (default: 90) - --dns-sakuracloud-credentials DNS_SAKURACLOUD_CREDENTIALS - Sakura Cloud credentials file. (default: None) - -manual: - Authenticate through manual configuration or custom shell scripts. When - using shell scripts, an authenticator script must be provided. The - environment variables available to this script depend on the type of - challenge. $CERTBOT_DOMAIN will always contain the domain being - authenticated. For HTTP-01 and DNS-01, $CERTBOT_VALIDATION is the - validation string, and $CERTBOT_TOKEN is the filename of the resource - requested when performing an HTTP-01 challenge. When performing a TLS- - SNI-01 challenge, $CERTBOT_SNI_DOMAIN will contain the SNI name for which - the ACME server expects to be presented with the self-signed certificate - located at $CERTBOT_CERT_PATH. The secret key needed to complete the TLS - handshake is located at $CERTBOT_KEY_PATH. An additional cleanup script - can also be provided and can use the additional variable - $CERTBOT_AUTH_OUTPUT which contains the stdout output from the auth - script. - - --manual-auth-hook MANUAL_AUTH_HOOK - Path or command to execute for the authentication - script (default: None) - --manual-cleanup-hook MANUAL_CLEANUP_HOOK - Path or command to execute for the cleanup script - (default: None) - --manual-public-ip-logging-ok - Automatically allows public IP logging (default: Ask) - -nginx: - Nginx Web Server plugin - - --nginx-server-root NGINX_SERVER_ROOT - Nginx server root directory. (default: /etc/nginx or - /usr/local/etc/nginx) - --nginx-ctl NGINX_CTL - Path to the 'nginx' binary, used for 'configtest' and - retrieving nginx version number. (default: nginx) - -null: - Null Installer - -standalone: - Spin up a temporary webserver - -webroot: - Place files in webroot directory - - --webroot-path WEBROOT_PATH, -w WEBROOT_PATH - public_html / webroot path. This can be specified - multiple times to handle different domains; each - domain will have the webroot path that preceded it. - For instance: `-w /var/www/example -d example.com -d - www.example.com -w /var/www/thing -d thing.net -d - m.thing.net` (default: Ask) - --webroot-map WEBROOT_MAP - JSON dictionary mapping domains to webroot paths; this - implies -d for each entry. You may need to escape this - from your shell. E.g.: --webroot-map - '{"eg1.is,m.eg1.is":"/www/eg1/", "eg2.is":"/www/eg2"}' - This option is merged with, but takes precedence over, - -w / -d entries. At present, if you put webroot-map in - a config file, it needs to be on a single line, like: - webroot-map = {"example.com":"/var/www"}. (default: - {}) diff --git a/docs/conf.py b/docs/conf.py deleted file mode 100644 index c72d1c1cff1..00000000000 --- a/docs/conf.py +++ /dev/null @@ -1,323 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Certbot documentation build configuration file, created by -# sphinx-quickstart on Sun Nov 23 20:35:21 2014. -# -# This file is execfile()d with the current directory set to its -# containing dir. -# -# Note that not all possible configuration values are present in this -# autogenerated file. -# -# All configuration values have a default; values that are commented out -# serve to show the default. - -import codecs -import os -import re -import sys - -import sphinx - - -here = os.path.abspath(os.path.dirname(__file__)) - -# read version number (and other metadata) from package init -init_fn = os.path.join(here, '..', 'certbot', '__init__.py') -with codecs.open(init_fn, encoding='utf8') as fd: - meta = dict(re.findall(r"""__([a-z]+)__ = '([^']+)""", fd.read())) - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath(os.path.join(here, '..'))) - -# -- General configuration ------------------------------------------------ - -# If your documentation needs a minimal Sphinx version, state it here. -needs_sphinx = '1.2' - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.viewcode', - 'repoze.sphinx.autointerface', -] - -if sphinx.version_info >= (1, 6): - extensions.append('sphinx.ext.imgconverter') - -autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# The suffix of source filenames. -source_suffix = '.rst' - -# The encoding of source files. -#source_encoding = 'utf-8-sig' - -# The master toctree document. -master_doc = 'index' - -# General information about the project. -project = u'Certbot' -# this is now overridden by the footer.html template -#copyright = u'2014-2018 - The Certbot software and documentation are licensed under the Apache 2.0 license as described at https://eff.org/cb-license.' - -# The version info for the project you're documenting, acts as replacement for -# |version| and |release|, also used in various other places throughout the -# built documents. -# -# The short X.Y version. -version = '.'.join(meta['version'].split('.')[:2]) -# The full version, including alpha/beta/rc tags. -release = meta['version'] - -# The language for content autogenerated by Sphinx. Refer to documentation -# for a list of supported languages. -# -# This is also used if you do content translation via gettext catalogs. -# Usually you set "language" from the command line for these cases. -language = None - -# There are two options for replacing |today|: either, you set today to some -# non-false value, then it is used: -#today = '' -# Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -exclude_patterns = ['_build'] - -# The reST default role (used for this markup: `text`) to use for all -# documents. -default_role = 'py:obj' - -# If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True - -# If true, the current module name will be prepended to all description -# unit titles (such as .. function::). -#add_module_names = True - -# If true, sectionauthor and moduleauthor directives will be shown in the -# output. They are ignored by default. -#show_authors = False - -# The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' - -# A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] - -# If true, keep warnings as "system message" paragraphs in the built documents. -#keep_warnings = False - -# If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True - -suppress_warnings = ['image.nonlocal_uri'] - -# -- Options for HTML output ---------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. - -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it - -# Theme options are theme-specific and customize the look and feel of a theme -# further. For a list of options available for each theme, see the -# documentation. -#html_theme_options = {} - -# Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] - -# The name for this set of Sphinx documents. If None, it defaults to -# " v documentation". -#html_title = None - -# A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None - -# The name of an image file (relative to this directory) to place at the top -# of the sidebar. -#html_logo = None - -# The name of an image file (within the static path) to use as favicon of the -# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 -# pixels large. -#html_favicon = None - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] - -# Add any extra paths that contain custom files (such as robots.txt or -# .htaccess) here, relative to this directory. These files are copied -# directly to the root of the documentation. -#html_extra_path = [] - -# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, -# using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' - -# If true, SmartyPants will be used to convert quotes and dashes to -# typographically correct entities. -#html_use_smartypants = True - -# Custom sidebar templates, maps document names to template names. -#html_sidebars = {} - -# Additional templates that should be rendered to pages, maps page names to -# template names. -#html_additional_pages = {} - -# If false, no module index is generated. -#html_domain_indices = True - -# If false, no index is generated. -#html_use_index = True - -# If true, the index is split into individual pages for each letter. -#html_split_index = False - -# If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True - -# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True - -# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True - -# If true, an OpenSearch description file will be output, and all pages will -# contain a tag referring to it. The value of this option must be the -# base URL from which the finished HTML is served. -#html_use_opensearch = '' - -# This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None - -# Language to be used for generating the HTML full-text search index. -# Sphinx supports the following languages: -# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' -# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' -#html_search_language = 'en' - -# A dictionary with options for the search language support, empty by default. -# Now only 'ja' uses this config value -#html_search_options = {'type': 'default'} - -# The name of a javascript file (relative to the configuration directory) that -# implements a search results scorer. If empty, the default will be used. -#html_search_scorer = 'scorer.js' - -# Output file base name for HTML help builder. -htmlhelp_basename = 'Certbotdoc' - -# -- Options for LaTeX output --------------------------------------------- - -latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - #'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - #'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - #'preamble': '', - - # Latex figure (float) alignment - #'figure_align': 'htbp', -} - -# Grouping the document tree into LaTeX files. List of tuples -# (source start file, target name, title, -# author, documentclass [howto, manual, or own class]). -latex_documents = [ - ('index', 'Certbot.tex', u'Certbot Documentation', - u'Certbot Project', 'manual'), -] - -# The name of an image file (relative to this directory) to place at the top of -# the title page. -#latex_logo = None - -# For "manual" documents, if this is true, then toplevel headings are parts, -# not chapters. -#latex_use_parts = False - -# If true, show page references after internal links. -#latex_show_pagerefs = False - -# If true, show URL addresses after external links. -#latex_show_urls = False - -# Documents to append as an appendix to all manuals. -#latex_appendices = [] - -# If false, no module index is generated. -#latex_domain_indices = True - - -# -- Options for manual page output --------------------------------------- - -# One entry per manual page. List of tuples -# (source start file, name, description, authors, manual section). -man_pages = [ - ('index', 'certbot', u'Certbot Documentation', - [project], 7), - ('man/certbot', 'certbot', u'certbot script documentation', - [project], 1), -] - -# If true, show URL addresses after external links. -#man_show_urls = False - - -# -- Options for Texinfo output ------------------------------------------- - -# Grouping the document tree into Texinfo files. List of tuples -# (source start file, target name, title, author, -# dir menu entry, description, category) -texinfo_documents = [ - ('index', 'Certbot', u'Certbot Documentation', - u'Certbot Project', 'Certbot', 'One line description of project.', - 'Miscellaneous'), -] - -# Documents to append as an appendix to all manuals. -#texinfo_appendices = [] - -# If false, no module index is generated. -#texinfo_domain_indices = True - -# How to display URL addresses: 'footnote', 'no', or 'inline'. -#texinfo_show_urls = 'footnote' - -# If true, do not generate a @detailmenu in the "Top" node's menu. -#texinfo_no_detailmenu = False - - -intersphinx_mapping = { - 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), -} diff --git a/docs/contributing.rst b/docs/contributing.rst deleted file mode 100644 index 264db630fc8..00000000000 --- a/docs/contributing.rst +++ /dev/null @@ -1,494 +0,0 @@ -=============== -Developer Guide -=============== - -.. contents:: Table of Contents - :local: - - -.. _getting_started: - -Getting Started -=============== - -Certbot has the same :ref:`system requirements ` when set -up for development. While the section below will help you install Certbot and -its dependencies, Certbot needs to be run on a UNIX-like OS so if you're using -Windows, you'll need to set up a (virtual) machine running an OS such as Linux -and continue with these instructions on that UNIX-like OS. - -Running a local copy of the client ----------------------------------- - -Running the client in developer mode from your local tree is a little different -than running Certbot as a user. To get set up, clone our git repository by -running: - -.. code-block:: shell - - git clone https://github.com/certbot/certbot - -If you're on macOS, we recommend you skip the rest of this section and instead -run Certbot in Docker. You can find instructions for how to do this :ref:`here -`. If you're running on Linux, you can run the following commands to -install dependencies and set up a virtual environment where you can run -Certbot. - -.. code-block:: shell - - cd certbot - ./certbot-auto --debug --os-packages-only - python tools/venv.py - -If you have Python3 available and want to use it, run the ``venv3.py`` script. - -.. code-block:: shell - - python tools/venv3.py - -.. note:: You may need to repeat this when - Certbot's dependencies change or when a new plugin is introduced. - -You can now run the copy of Certbot from git either by executing -``venv/bin/certbot``, or by activating the virtual environment. You can do the -latter by running: - -.. code-block:: shell - - source venv/bin/activate - # or - source venv3/bin/activate - -After running this command, ``certbot`` and development tools like ``ipdb``, -``ipython``, ``pytest``, and ``tox`` are available in the shell where you ran -the command. These tools are installed in the virtual environment and are kept -separate from your global Python installation. This works by setting -environment variables so the right executables are found and Python can pull in -the versions of various packages needed by Certbot. More information can be -found in the `virtualenv docs`_. - -.. _`virtualenv docs`: https://virtualenv.pypa.io - -Find issues to work on ----------------------- - -You can find the open issues in the `github issue tracker`_. Comparatively -easy ones are marked `good first issue`_. If you're starting work on -something, post a comment to let others know and seek feedback on your plan -where appropriate. - -Once you've got a working branch, you can open a pull request. All changes in -your pull request must have thorough unit test coverage, pass our -tests, and be compliant with the :ref:`coding style `. - -.. _github issue tracker: https://github.com/certbot/certbot/issues -.. _good first issue: https://github.com/certbot/certbot/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22 - -.. _testing: - -Testing -------- - -When you are working in a file ``foo.py``, there should also be a file ``foo_test.py`` -either in the same directory as ``foo.py`` or in the ``tests`` subdirectory -(if there isn't, make one). While you are working on your code and tests, run -``python foo_test.py`` to run the relevant tests. - -For debugging, we recommend putting -``import ipdb; ipdb.set_trace()`` statements inside the source code. - -Once you are done with your code changes, and the tests in ``foo_test.py`` pass, -run all of the unittests for Certbot with ``tox -e py27`` (this uses Python -2.7). - -Once all the unittests pass, check for sufficient test coverage using -``tox -e cover``, and then check for code style with ``tox -e lint`` (all files) -or ``pylint --rcfile=.pylintrc path/to/file.py`` (single file at a time). - -Once all of the above is successful, you may run the full test suite using -``tox --skip-missing-interpreters``. We recommend running the commands above -first, because running all tests like this is very slow, and the large amount -of output can make it hard to find specific failures when they happen. - -.. warning:: The full test suite may attempt to modify your system's Apache - config if your user has sudo permissions, so it should not be run on a - production Apache server. - -.. _integration: - -Integration testing with the Boulder CA -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Generally it is sufficient to open a pull request and let Github and Travis run -integration tests for you, however, if you want to run them locally you need -Docker and docker-compose installed and working. Fetch and start Boulder, Let's -Encrypt's ACME CA software, by using: - -.. code-block:: shell - - ./tests/boulder-fetch.sh - -If you have problems with Docker, you may want to try `removing all containers and -volumes`_ and making sure you have at least 1GB of memory. - -Set up a certbot_test alias that enables easily running against the local -Boulder: - -.. code-block:: shell - - export SERVER=http://localhost:4000/directory - source tests/integration/_common.sh - -Run the integration tests using: - -.. code-block:: shell - - ./tests/boulder-integration.sh - -.. _removing all containers and volumes: https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes - -Code components and layout -========================== - -acme - contains all protocol specific code -certbot - main client code -certbot-apache and certbot-nginx - client code to configure specific web servers -certbot.egg-info - configuration for packaging Certbot - - -Plugin-architecture -------------------- - -Certbot has a plugin architecture to facilitate support for -different webservers, other TLS servers, and operating systems. -The interfaces available for plugins to implement are defined in -`interfaces.py`_ and `plugins/common.py`_. - -The main two plugin interfaces are `~certbot.interfaces.IAuthenticator`, which -implements various ways of proving domain control to a certificate authority, -and `~certbot.interfaces.IInstaller`, which configures a server to use a -certificate once it is issued. Some plugins, like the built-in Apache and Nginx -plugins, implement both interfaces and perform both tasks. Others, like the -built-in Standalone authenticator, implement just one interface. - -There are also `~certbot.interfaces.IDisplay` plugins, -which can change how prompts are displayed to a user. - -.. _interfaces.py: https://github.com/certbot/certbot/blob/master/certbot/interfaces.py -.. _plugins/common.py: https://github.com/certbot/certbot/blob/master/certbot/plugins/common.py#L34 - - -Authenticators --------------- - -Authenticators are plugins that prove control of a domain name by solving a -challenge provided by the ACME server. ACME currently defines several types of -challenges: HTTP, TLS-SNI (deprecated), TLS-ALPR, and DNS, represented by classes in `acme.challenges`. -An authenticator plugin should implement support for at least one challenge type. - -An Authenticator indicates which challenges it supports by implementing -`get_chall_pref(domain)` to return a sorted list of challenge types in -preference order. - -An Authenticator must also implement `perform(achalls)`, which "performs" a list -of challenges by, for instance, provisioning a file on an HTTP server, or -setting a TXT record in DNS. Once all challenges have succeeded or failed, -Certbot will call the plugin's `cleanup(achalls)` method to remove any files or -DNS records that were needed only during authentication. - -Installer ---------- - -Installers plugins exist to actually setup the certificate in a server, -possibly tweak the security configuration to make it more correct and secure -(Fix some mixed content problems, turn on HSTS, redirect to HTTPS, etc). -Installer plugins tell the main client about their abilities to do the latter -via the :meth:`~.IInstaller.supported_enhancements` call. We currently -have two Installers in the tree, the `~.ApacheConfigurator`. and the -`~.NginxConfigurator`. External projects have made some progress toward -support for IIS, Icecast and Plesk. - -Installers and Authenticators will oftentimes be the same class/object -(because for instance both tasks can be performed by a webserver like nginx) -though this is not always the case (the standalone plugin is an authenticator -that listens on port 80, but it cannot install certs; a postfix plugin would -be an installer but not an authenticator). - -Installers and Authenticators are kept separate because -it should be possible to use the `~.StandaloneAuthenticator` (it sets -up its own Python server to perform challenges) with a program that -cannot solve challenges itself (Such as MTA installers). - - -Installer Development ---------------------- - -There are a few existing classes that may be beneficial while -developing a new `~certbot.interfaces.IInstaller`. -Installers aimed to reconfigure UNIX servers may use Augeas for -configuration parsing and can inherit from `~.AugeasConfigurator` class -to handle much of the interface. Installers that are unable to use -Augeas may still find the `~.Reverter` class helpful in handling -configuration checkpoints and rollback. - - -.. _dev-plugin: - -Writing your own plugin -~~~~~~~~~~~~~~~~~~~~~~~ - -Certbot client supports dynamic discovery of plugins through the -`setuptools entry points`_ using the `certbot.plugins` group. This -way you can, for example, create a custom implementation of -`~certbot.interfaces.IAuthenticator` or the -`~certbot.interfaces.IInstaller` without having to merge it -with the core upstream source code. An example is provided in -``examples/plugins/`` directory. - -While developing, you can install your plugin into a Certbot development -virtualenv like this: - -.. code-block:: shell - - . venv/bin/activate - . tests/integration/_common.sh - pip install -e examples/plugins/ - certbot_test plugins - -Your plugin should show up in the output of the last command. If not, -it was not installed properly. - -Once you've finished your plugin and published it, you can have your -users install it system-wide with `pip install`. Note that this will -only work for users who have Certbot installed from OS packages or via -pip. Users who run `certbot-auto` are currently unable to use third-party -plugins. It's technically possible to install third-party plugins into -the virtualenv used by `certbot-auto`, but they will be wiped away when -`certbot-auto` upgrades. - -.. warning:: Please be aware though that as this client is still in a - developer-preview stage, the API may undergo a few changes. If you - believe the plugin will be beneficial to the community, please - consider submitting a pull request to the repo and we will update - it with any necessary API changes. - -.. _`setuptools entry points`: - http://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points - -.. _coding-style: - -Coding style -============ - -Please: - -1. **Be consistent with the rest of the code**. - -2. Read `PEP 8 - Style Guide for Python Code`_. - -3. Follow the `Google Python Style Guide`_, with the exception that we - use `Sphinx-style`_ documentation:: - - def foo(arg): - """Short description. - - :param int arg: Some number. - - :returns: Argument - :rtype: int - - """ - return arg - -4. Remember to use ``pylint``. - -.. _Google Python Style Guide: - https://google.github.io/styleguide/pyguide.html -.. _Sphinx-style: http://sphinx-doc.org/ -.. _PEP 8 - Style Guide for Python Code: - https://www.python.org/dev/peps/pep-0008 - -Mypy type annotations -===================== - -Certbot uses the `mypy`_ static type checker. Python 3 natively supports official type annotations, -which can then be tested for consistency using mypy. Python 2 doesn’t, but type annotations can -be `added in comments`_. Mypy does some type checks even without type annotations; we can find -bugs in Certbot even without a fully annotated codebase. - -Certbot supports both Python 2 and 3, so we’re using Python 2-style annotations. - -Zulip wrote a `great guide`_ to using mypy. It’s useful, but you don’t have to read the whole thing -to start contributing to Certbot. - -To run mypy on Certbot, use ``tox -e mypy`` on a machine that has Python 3 installed. - -Note that instead of just importing ``typing``, due to packaging issues, in Certbot we import from -``acme.magic_typing`` and have to add some comments for pylint like this: - -.. code-block:: python - - from acme.magic_typing import Dict # pylint: disable=unused-import, no-name-in-module - -Also note that OpenSSL, which we rely on, has type definitions for crypto but not SSL. We use both. -Those imports should look like this: - -.. code-block:: python - - from OpenSSL import crypto - from OpenSSL import SSL # type: ignore # https://github.com/python/typeshed/issues/2052 - -.. _mypy: https://mypy.readthedocs.io -.. _added in comments: https://mypy.readthedocs.io/en/latest/cheat_sheet.html -.. _great guide: https://blog.zulip.org/2016/10/13/static-types-in-python-oh-mypy/ - -Submitting a pull request -========================= - -Steps: - -1. Write your code! -2. Make sure your environment is set up properly and that you're in your - virtualenv. You can do this by running ``pip tools/venv.py``. - (this is a **very important** step) -3. Run ``tox -e lint`` to check for pylint errors. Fix any errors. -4. Run ``tox --skip-missing-interpreters`` to run the entire test suite - including coverage. The ``--skip-missing-interpreters`` argument ignores - missing versions of Python needed for running the tests. Fix any errors. -5. Submit the PR. Once your PR is open, please do not force push to the branch - containing your pull request to squash or amend commits. We use `squash - merges `_ on PRs and - rewriting commits makes changes harder to track between reviews. -6. Did your tests pass on Travis? If they didn't, fix any errors. - -Asking for help -=============== - -If you have any questions while working on a Certbot issue, don't hesitate to -ask for help! You can do this in the #letsencrypt-dev IRC channel on Freenode. -If you don't already have an IRC client set up, we recommend you join using -`Riot `_. - -Updating certbot-auto and letsencrypt-auto -========================================== - -Updating the scripts --------------------- -Developers should *not* modify the ``certbot-auto`` and ``letsencrypt-auto`` files -in the root directory of the repository. Rather, modify the -``letsencrypt-auto.template`` and associated platform-specific shell scripts in -the ``letsencrypt-auto-source`` and -``letsencrypt-auto-source/pieces/bootstrappers`` directory, respectively. - -Building letsencrypt-auto-source/letsencrypt-auto -------------------------------------------------- -Once changes to any of the aforementioned files have been made, the -``letsencrypt-auto-source/letsencrypt-auto`` script should be updated. In lieu of -manually updating this script, run the build script, which lives at -``letsencrypt-auto-source/build.py``: - -.. code-block:: shell - - python letsencrypt-auto-source/build.py - -Running ``build.py`` will update the ``letsencrypt-auto-source/letsencrypt-auto`` -script. Note that the ``certbot-auto`` and ``letsencrypt-auto`` scripts in the root -directory of the repository will remain **unchanged** after this script is run. -Your changes will be propagated to these files during the next release of -Certbot. - -Opening a PR ------------- -When opening a PR, ensure that the following files are committed: - -1. ``letsencrypt-auto-source/letsencrypt-auto.template`` and - ``letsencrypt-auto-source/pieces/bootstrappers/*`` -2. ``letsencrypt-auto-source/letsencrypt-auto`` (generated by ``build.py``) - -It might also be a good idea to double check that **no** changes were -inadvertently made to the ``certbot-auto`` or ``letsencrypt-auto`` scripts in the -root of the repository. These scripts will be updated by the core developers -during the next release. - - -Updating the documentation -========================== - -In order to generate the Sphinx documentation, run the following -commands: - -.. code-block:: shell - - make -C docs clean html man - -This should generate documentation in the ``docs/_build/html`` -directory. - -.. note:: If you skipped the "Getting Started" instructions above, - run ``pip install -e ".[docs]"`` to install Certbot's docs extras modules. - - -.. _docker-dev: - -Running the client with Docker -============================== - -You can use Docker Compose to quickly set up an environment for running and -testing Certbot. To install Docker Compose, follow the instructions at -https://docs.docker.com/compose/install/. - -.. note:: Linux users can simply run ``pip install docker-compose`` to get - Docker Compose after installing Docker Engine and activating your shell as - described in the :ref:`Getting Started ` section. - -Now you can develop on your host machine, but run Certbot and test your changes -in Docker. When using ``docker-compose`` make sure you are inside your clone of -the Certbot repository. As an example, you can run the following command to -check for linting errors:: - - docker-compose run --rm --service-ports development bash -c 'tox -e lint' - -You can also leave a terminal open running a shell in the Docker container and -modify Certbot code in another window. The Certbot repo on your host machine is -mounted inside of the container so any changes you make immediately take -effect. To do this, run:: - - docker-compose run --rm --service-ports development bash - -Now running the check for linting errors described above is as easy as:: - - tox -e lint - -.. _prerequisites: - -Notes on OS dependencies -======================== - -OS-level dependencies can be installed like so: - -.. code-block:: shell - - ./certbot-auto --debug --os-packages-only - -In general... - -* ``sudo`` is required as a suggested way of running privileged process -* `Python`_ 2.7 or 3.4+ is required -* `Augeas`_ is required for the Python bindings -* ``virtualenv`` is used for managing other Python library dependencies - -.. _Python: https://wiki.python.org/moin/BeginnersGuide/Download -.. _Augeas: http://augeas.net/ -.. _Virtualenv: https://virtualenv.pypa.io - - -FreeBSD -------- - -FreeBSD by default uses ``tcsh``. In order to activate virtualenv (see -above), you will need a compatible shell, e.g. ``pkg install bash && -bash``. diff --git a/docs/index.rst b/docs/index.rst deleted file mode 100644 index 17cde1adfb8..00000000000 --- a/docs/index.rst +++ /dev/null @@ -1,26 +0,0 @@ -Welcome to the Certbot documentation! -================================================== - -.. toctree:: - :maxdepth: 2 - - intro - what - install - using - contributing - packaging - resources - -.. toctree:: - :maxdepth: 1 - - api - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` diff --git a/docs/install.rst b/docs/install.rst deleted file mode 100644 index 35b2624825f..00000000000 --- a/docs/install.rst +++ /dev/null @@ -1,291 +0,0 @@ -===================== -Get Certbot -===================== - -.. contents:: Table of Contents - :local: - - -About Certbot -============= - -*Certbot is meant to be run directly on a web server*, normally by a system administrator. In most cases, running Certbot on your personal computer is not a useful option. The instructions below relate to installing and running Certbot on a server. - -Certbot is packaged for many common operating systems and web servers. Check whether -``certbot`` (or ``letsencrypt``) is packaged for your web server's OS by visiting -certbot.eff.org_, where you will also find the correct installation instructions for -your system. - -.. Note:: Unless you have very specific requirements, we kindly suggest that you use the Certbot packages provided by your package manager (see certbot.eff.org_). If such packages are not available, we recommend using ``certbot-auto``, which automates the process of installing Certbot on your system. - -.. _certbot.eff.org: https://certbot.eff.org - - -.. _system_requirements: - -System Requirements -=================== - -Certbot currently requires Python 2.7 or 3.4+ running on a UNIX-like operating -system. By default, it requires root access in order to write to -``/etc/letsencrypt``, ``/var/log/letsencrypt``, ``/var/lib/letsencrypt``; to -bind to port 80 (if you use the ``standalone`` plugin) and to read and -modify webserver configurations (if you use the ``apache`` or ``nginx`` -plugins). If none of these apply to you, it is theoretically possible to run -without root privileges, but for most users who want to avoid running an ACME -client as root, either `letsencrypt-nosudo -`_ or `simp_le -`_ are more appropriate choices. - -The Apache plugin currently requires an OS with augeas version 1.0; currently `it -supports -`_ -modern OSes based on Debian, Fedora, SUSE, Gentoo and Darwin. - - -Additional integrity verification of certbot-auto script can be done by verifying its digital signature. -This requires a local installation of gpg2, which comes packaged in many Linux distributions under name gnupg or gnupg2. - - -Installing with ``certbot-auto`` requires 512MB of RAM in order to build some -of the dependencies. Installing from pre-built OS packages avoids this -requirement. You can also temporarily set a swap file. See "Problems with -Python virtual environment" below for details. - - -Alternate installation methods -================================ - -If you are offline or your operating system doesn't provide a package, you can use -an alternate method for installing ``certbot``. - -.. _certbot-auto: - -Certbot-Auto ------------- - -The ``certbot-auto`` wrapper script installs Certbot, obtaining some dependencies -from your web server OS and putting others in a python virtual environment. You can -download and run it as follows:: - - user@webserver:~$ wget https://dl.eff.org/certbot-auto - user@webserver:~$ chmod a+x ./certbot-auto - user@webserver:~$ ./certbot-auto --help - -To check the integrity of the ``certbot-auto`` script, -you can use these steps:: - - - user@webserver:~$ wget -N https://dl.eff.org/certbot-auto.asc - user@webserver:~$ gpg2 --keyserver pool.sks-keyservers.net --recv-key A2CFB51FA275A7286234E7B24D17C995CD9775F2 - user@webserver:~$ gpg2 --trusted-key 4D17C995CD9775F2 --verify certbot-auto.asc certbot-auto - - - -The output of the last command should look something like:: - - - gpg: Signature made Wed 02 May 2018 05:29:12 AM IST - gpg: using RSA key A2CFB51FA275A7286234E7B24D17C995CD9775F2 - gpg: key 4D17C995CD9775F2 marked as ultimately trusted - gpg: checking the trustdb - gpg: marginals needed: 3 completes needed: 1 trust model: pgp - gpg: depth: 0 valid: 2 signed: 2 trust: 0-, 0q, 0n, 0m, 0f, 2u - gpg: depth: 1 valid: 2 signed: 0 trust: 2-, 0q, 0n, 0m, 0f, 0u - gpg: next trustdb check due at 2027-11-22 - gpg: Good signature from "Let's Encrypt Client Team " [ultimate] - - - -The ``certbot-auto`` command updates to the latest client release automatically. -Since ``certbot-auto`` is a wrapper to ``certbot``, it accepts exactly -the same command line flags and arguments. For more information, see -`Certbot command-line options `_. - -For full command line help, you can type:: - - ./certbot-auto --help all - -Problems with Python virtual environment ----------------------------------------- - -On a low memory system such as VPS with less than 512MB of RAM, the required dependencies of Certbot will fail to build. -This can be identified if the pip outputs contains something like ``internal compiler error: Killed (program cc1)``. -You can workaround this restriction by creating a temporary swapfile:: - - user@webserver:~$ sudo fallocate -l 1G /tmp/swapfile - user@webserver:~$ sudo chmod 600 /tmp/swapfile - user@webserver:~$ sudo mkswap /tmp/swapfile - user@webserver:~$ sudo swapon /tmp/swapfile - -Disable and remove the swapfile once the virtual environment is constructed:: - - user@webserver:~$ sudo swapoff /tmp/swapfile - user@webserver:~$ sudo rm /tmp/swapfile - -.. _docker-user: - -Running with Docker -------------------- - -Docker_ is an amazingly simple and quick way to obtain a -certificate. However, this mode of operation is unable to install -certificates or configure your webserver, because our installer -plugins cannot reach your webserver from inside the Docker container. - -Most users should use the operating system packages (see instructions at -certbot.eff.org_) or, as a fallback, ``certbot-auto``. You should only -use Docker if you are sure you know what you are doing and have a -good reason to do so. - -You should definitely read the :ref:`where-certs` section, in order to -know how to manage the certs -manually. `Our ciphersuites page `__ -provides some information about recommended ciphersuites. If none of -these make much sense to you, you should definitely use the -certbot-auto_ method, which enables you to use installer plugins -that cover both of those hard topics. - -If you're still not convinced and have decided to use this method, from -the server that the domain you're requesting a certficate for resolves -to, `install Docker`_, then issue a command like the one found below. If -you are using Certbot with the :ref:`Standalone` plugin, you will need -to make the port it uses accessible from outside of the container by -including something like ``-p 80:80`` or ``-p 443:443`` on the command -line before ``certbot/certbot``. - -.. code-block:: shell - - sudo docker run -it --rm --name certbot \ - -v "/etc/letsencrypt:/etc/letsencrypt" \ - -v "/var/lib/letsencrypt:/var/lib/letsencrypt" \ - certbot/certbot certonly - -Running Certbot with the ``certonly`` command will obtain a certificate and place it in the directory -``/etc/letsencrypt/live`` on your system. Because Certonly cannot install the certificate from -within Docker, you must install the certificate manually according to the procedure -recommended by the provider of your webserver. - -There are also Docker images for each of Certbot's DNS plugins available -at https://hub.docker.com/u/certbot which automate doing domain -validation over DNS for popular providers. To use one, just replace -``certbot/certbot`` in the command above with the name of the image you -want to use. For example, to use Certbot's plugin for Amazon Route 53, -you'd use ``certbot/dns-route53``. You may also need to add flags to -Certbot and/or mount additional directories to provide access to your -DNS API credentials as specified in the :ref:`DNS plugin documentation -`. If you would like to obtain a wildcard certificate from -Let's Encrypt's ACMEv2 server, you'll need to include ``--server -https://acme-v02.api.letsencrypt.org/directory`` on the command line as -well. - -For more information about the layout -of the ``/etc/letsencrypt`` directory, see :ref:`where-certs`. - -.. _Docker: https://docker.com -.. _`install Docker`: https://docs.docker.com/engine/installation/ - -Operating System Packages -------------------------- - -**Arch Linux** - -.. code-block:: shell - - sudo pacman -S certbot - -**Debian** - -If you run Debian Stretch or Debian Sid, you can install certbot packages. - -.. code-block:: shell - - sudo apt-get update - sudo apt-get install certbot python-certbot-apache - -If you don't want to use the Apache plugin, you can omit the -``python-certbot-apache`` package. Or you can install ``python-certbot-nginx`` instead. - -Packages exist for Debian Jessie via backports. First you'll have to follow the -instructions at http://backports.debian.org/Instructions/ to enable the Jessie backports -repo, if you have not already done so. Then run: - -.. code-block:: shell - - sudo apt-get install certbot python-certbot-apache -t jessie-backports - -**Fedora** - -.. code-block:: shell - - sudo dnf install certbot python2-certbot-apache - -**FreeBSD** - - * Port: ``cd /usr/ports/security/py-certbot && make install clean`` - * Package: ``pkg install py27-certbot`` - -**Gentoo** - -The official Certbot client is available in Gentoo Portage. If you -want to use the Apache plugin, it has to be installed separately: - -.. code-block:: shell - - emerge -av app-crypt/certbot - emerge -av app-crypt/certbot-apache - -When using the Apache plugin, you will run into a "cannot find an -SSLCertificateFile directive" or "cannot find an SSLCertificateKeyFile -directive for certificate" error if you're sporting the default Gentoo -``httpd.conf``. You can fix this by commenting out two lines in -``/etc/apache2/httpd.conf`` as follows: - -Change - -.. code-block:: shell - - - LoadModule ssl_module modules/mod_ssl.so - - -to - -.. code-block:: shell - - # - LoadModule ssl_module modules/mod_ssl.so - # - -For the time being, this is the only way for the Apache plugin to recognise -the appropriate directives when installing the certificate. -Note: this change is not required for the other plugins. - -**NetBSD** - - * Build from source: ``cd /usr/pkgsrc/security/py-certbot && make install clean`` - * Install pre-compiled package: ``pkg_add py27-certbot`` - -**OpenBSD** - - * Port: ``cd /usr/ports/security/letsencrypt/client && make install clean`` - * Package: ``pkg_add letsencrypt`` - -**Other Operating Systems** - -OS packaging is an ongoing effort. If you'd like to package -Certbot for your distribution of choice please have a -look at the :doc:`packaging`. - -Installing from source ----------------------- - -Installation from source is only supported for developers and the -whole process is described in the :doc:`contributing`. - -.. warning:: Please do **not** use ``python setup.py install``, ``python pip - install .``, or ``easy_install .``. Please do **not** attempt the - installation commands as superuser/root and/or without virtual environment, - e.g. ``sudo python setup.py install``, ``sudo pip install``, ``sudo - ./venv/bin/...``. These modes of operation might corrupt your operating - system and are **not supported** by the Certbot team! diff --git a/docs/make.bat b/docs/make.bat deleted file mode 100644 index 198e864c3e1..00000000000 --- a/docs/make.bat +++ /dev/null @@ -1,263 +0,0 @@ -@ECHO OFF - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set BUILDDIR=_build -set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . -set I18NSPHINXOPTS=%SPHINXOPTS% . -if NOT "%PAPER%" == "" ( - set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% - set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% -) - -if "%1" == "" goto help - -if "%1" == "help" ( - :help - echo.Please use `make ^` where ^ is one of - echo. html to make standalone HTML files - echo. dirhtml to make HTML files named index.html in directories - echo. singlehtml to make a single large HTML file - echo. pickle to make pickle files - echo. json to make JSON files - echo. htmlhelp to make HTML files and a HTML help project - echo. qthelp to make HTML files and a qthelp project - echo. devhelp to make HTML files and a Devhelp project - echo. epub to make an epub - echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter - echo. text to make text files - echo. man to make manual pages - echo. texinfo to make Texinfo files - echo. gettext to make PO message catalogs - echo. changes to make an overview over all changed/added/deprecated items - echo. xml to make Docutils-native XML files - echo. pseudoxml to make pseudoxml-XML files for display purposes - echo. linkcheck to check all external links for integrity - echo. doctest to run all doctests embedded in the documentation if enabled - echo. coverage to run coverage check of the documentation if enabled - goto end -) - -if "%1" == "clean" ( - for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i - del /q /s %BUILDDIR%\* - goto end -) - - -REM Check if sphinx-build is available and fallback to Python version if any -%SPHINXBUILD% 2> nul -if errorlevel 9009 goto sphinx_python -goto sphinx_ok - -:sphinx_python - -set SPHINXBUILD=python -m sphinx.__init__ -%SPHINXBUILD% 2> nul -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ - exit /b 1 -) - -:sphinx_ok - - -if "%1" == "html" ( - %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/html. - goto end -) - -if "%1" == "dirhtml" ( - %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. - goto end -) - -if "%1" == "singlehtml" ( - %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. - goto end -) - -if "%1" == "pickle" ( - %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the pickle files. - goto end -) - -if "%1" == "json" ( - %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the JSON files. - goto end -) - -if "%1" == "htmlhelp" ( - %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run HTML Help Workshop with the ^ -.hhp project file in %BUILDDIR%/htmlhelp. - goto end -) - -if "%1" == "qthelp" ( - %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run "qcollectiongenerator" with the ^ -.qhcp project file in %BUILDDIR%/qthelp, like this: - echo.^> qcollectiongenerator %BUILDDIR%\qthelp\LetsEncrypt.qhcp - echo.To view the help file: - echo.^> assistant -collectionFile %BUILDDIR%\qthelp\LetsEncrypt.ghc - goto end -) - -if "%1" == "devhelp" ( - %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. - goto end -) - -if "%1" == "epub" ( - %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The epub file is in %BUILDDIR%/epub. - goto end -) - -if "%1" == "latex" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdf" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdfja" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf-ja - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "text" ( - %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The text files are in %BUILDDIR%/text. - goto end -) - -if "%1" == "man" ( - %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The manual pages are in %BUILDDIR%/man. - goto end -) - -if "%1" == "texinfo" ( - %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. - goto end -) - -if "%1" == "gettext" ( - %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The message catalogs are in %BUILDDIR%/locale. - goto end -) - -if "%1" == "changes" ( - %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes - if errorlevel 1 exit /b 1 - echo. - echo.The overview file is in %BUILDDIR%/changes. - goto end -) - -if "%1" == "linkcheck" ( - %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck - if errorlevel 1 exit /b 1 - echo. - echo.Link check complete; look for any errors in the above output ^ -or in %BUILDDIR%/linkcheck/output.txt. - goto end -) - -if "%1" == "doctest" ( - %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest - if errorlevel 1 exit /b 1 - echo. - echo.Testing of doctests in the sources finished, look at the ^ -results in %BUILDDIR%/doctest/output.txt. - goto end -) - -if "%1" == "coverage" ( - %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage - if errorlevel 1 exit /b 1 - echo. - echo.Testing of coverage in the sources finished, look at the ^ -results in %BUILDDIR%/coverage/python.txt. - goto end -) - -if "%1" == "xml" ( - %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The XML files are in %BUILDDIR%/xml. - goto end -) - -if "%1" == "pseudoxml" ( - %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. - goto end -) - -:end diff --git a/docs/man/certbot.rst b/docs/man/certbot.rst deleted file mode 100644 index d03f3eed4a9..00000000000 --- a/docs/man/certbot.rst +++ /dev/null @@ -1 +0,0 @@ -.. literalinclude:: ../cli-help.txt diff --git a/docs/packaging.rst b/docs/packaging.rst deleted file mode 100644 index c13a14af359..00000000000 --- a/docs/packaging.rst +++ /dev/null @@ -1,126 +0,0 @@ -=============== -Packaging Guide -=============== - -Releases -======== - -We release packages and upload them to PyPI (wheels and source tarballs). - -- https://pypi.python.org/pypi/acme -- https://pypi.python.org/pypi/certbot -- https://pypi.python.org/pypi/certbot-apache -- https://pypi.python.org/pypi/certbot-nginx -- https://pypi.python.org/pypi/certbot-dns-cloudflare -- https://pypi.python.org/pypi/certbot-dns-cloudxns -- https://pypi.python.org/pypi/certbot-dns-digitalocean -- https://pypi.python.org/pypi/certbot-dns-dnsimple -- https://pypi.python.org/pypi/certbot-dns-dnsmadeeasy -- https://pypi.python.org/pypi/certbot-dns-google -- https://pypi.python.org/pypi/certbot-dns-linode -- https://pypi.python.org/pypi/certbot-dns-luadns -- https://pypi.python.org/pypi/certbot-dns-nsone -- https://pypi.python.org/pypi/certbot-dns-ovh -- https://pypi.python.org/pypi/certbot-dns-rfc2136 -- https://pypi.python.org/pypi/certbot-dns-route53 - -The following scripts are used in the process: - -- https://github.com/letsencrypt/letsencrypt/blob/master/tools/release.sh - -We use git tags to identify releases, using `Semantic Versioning`_. For -example: `v0.11.1`. - -.. _`Semantic Versioning`: http://semver.org/ - -Notes for package maintainers -============================= - -0. Please use our tagged releases, not ``master``! - -1. Do not package ``certbot-compatibility-test`` or ``letshelp-certbot`` - it's only used internally. - -2. If you'd like to include automated renewal in your package ``certbot renew -q`` should be added to crontab or systemd timer. Additionally you should include a random per-machine time offset to avoid having a large number of your clients hit Let's Encrypt's servers simultaneously. - -3. ``jws`` is an internal script for ``acme`` module and it doesn't have to be packaged - it's mostly for debugging: you can use it as ``echo foo | jws sign | jws verify``. - -4. Do get in touch with us. We are happy to make any changes that will make packaging easier. If you need to apply some patches don't do it downstream - make a PR here. - -Already ongoing efforts -======================= - - -Arch ----- - -From our official releases: - -- https://www.archlinux.org/packages/community/any/python-acme -- https://www.archlinux.org/packages/community/any/certbot -- https://www.archlinux.org/packages/community/any/certbot-apache -- https://www.archlinux.org/packages/community/any/certbot-nginx -- https://www.archlinux.org/packages/community/any/certbot-dns-cloudflare -- https://www.archlinux.org/packages/community/any/certbot-dns-cloudxns -- https://www.archlinux.org/packages/community/any/certbot-dns-digitalocean -- https://www.archlinux.org/packages/community/any/certbot-dns-dnsimple -- https://www.archlinux.org/packages/community/any/certbot-dns-dnsmadeeasy -- https://www.archlinux.org/packages/community/any/certbot-dns-google -- https://www.archlinux.org/packages/community/any/certbot-dns-luadns -- https://www.archlinux.org/packages/community/any/certbot-dns-nsone -- https://www.archlinux.org/packages/community/any/certbot-dns-rfc2136 -- https://www.archlinux.org/packages/community/any/certbot-dns-route53 - -From ``master``: https://aur.archlinux.org/packages/certbot-git - -Debian (and its derivatives, including Ubuntu) ----------------------------------------------- - -- https://packages.debian.org/sid/certbot -- https://packages.debian.org/sid/python-certbot -- https://packages.debian.org/sid/python-certbot-apache - -Fedora ------- - -In Fedora 23+. - -- https://apps.fedoraproject.org/packages/python-acme -- https://apps.fedoraproject.org/packages/certbot -- https://apps.fedoraproject.org/packages/python-certbot-apache -- https://apps.fedoraproject.org/packages/python-certbot-dns-cloudflare -- https://apps.fedoraproject.org/packages/python-certbot-dns-cloudxns -- https://apps.fedoraproject.org/packages/python-certbot-dns-digitalocean -- https://apps.fedoraproject.org/packages/python-certbot-dns-dnsimple -- https://apps.fedoraproject.org/packages/python-certbot-dns-dnsmadeeasy -- https://apps.fedoraproject.org/packages/python-certbot-dns-google -- https://apps.fedoraproject.org/packages/python-certbot-dns-luadns -- https://apps.fedoraproject.org/packages/python-certbot-dns-nsone -- https://apps.fedoraproject.org/packages/python-certbot-dns-rfc2136 -- https://apps.fedoraproject.org/packages/python-certbot-dns-route53 -- https://apps.fedoraproject.org/packages/python-certbot-nginx - -FreeBSD -------- - -- https://www.freshports.org/security/py-acme/ -- https://www.freshports.org/security/py-certbot/ - -Gentoo ------- - -Currently, all ``certbot`` related packages are in the testing branch: - -- https://packages.gentoo.org/packages/app-crypt/certbot -- https://packages.gentoo.org/packages/app-crypt/certbot-apache -- https://packages.gentoo.org/packages/app-crypt/certbot-nginx -- https://packages.gentoo.org/packages/app-crypt/acme - -GNU Guix --------- - -- https://www.gnu.org/software/guix/package-list.html#certbot - -OpenBSD -------- - -- http://cvsweb.openbsd.org/cgi-bin/cvsweb/ports/security/letsencrypt/client/ diff --git a/docs/using.rst b/docs/using.rst deleted file mode 100644 index d77badd6555..00000000000 --- a/docs/using.rst +++ /dev/null @@ -1,994 +0,0 @@ -========== -User Guide -========== - -.. contents:: Table of Contents - :local: - -Certbot Commands -================ - -Certbot uses a number of different commands (also referred -to as "subcommands") to request specific actions such as -obtaining, renewing, or revoking certificates. The most important -and commonly-used commands will be discussed throughout this -document; an exhaustive list also appears near the end of the document. - -The ``certbot`` script on your web server might be named ``letsencrypt`` if your system uses an older package, or ``certbot-auto`` if you used an alternate installation method. Throughout the docs, whenever you see ``certbot``, swap in the correct name as needed. - -.. _plugins: - -Getting certificates (and choosing plugins) -=========================================== - -The Certbot client supports two types of plugins for -obtaining and installing certificates: authenticators and installers. - -Authenticators are plugins used with the ``certonly`` command to obtain a certificate. -The authenticator validates that you -control the domain(s) you are requesting a certificate for, obtains a certificate for the specified -domain(s), and places the certificate in the ``/etc/letsencrypt`` directory on your -machine. The authenticator does not install the certificate (it does not edit any of your server's configuration files to serve the -obtained certificate). If you specify multiple domains to authenticate, they will -all be listed in a single certificate. To obtain multiple separate certificates -you will need to run Certbot multiple times. - -Installers are Plugins used with the ``install`` command to install a certificate. -These plugins can modify your webserver's configuration to -serve your website over HTTPS using certificates obtained by certbot. - -Plugins that do both can be used with the ``certbot run`` command, which is the default -when no command is specified. The ``run`` subcommand can also be used to specify -a combination_ of distinct authenticator and installer plugins. - -=========== ==== ==== =============================================================== ============================= -Plugin Auth Inst Notes Challenge types (and port) -=========== ==== ==== =============================================================== ============================= -apache_ Y Y | Automates obtaining and installing a certificate with Apache. http-01_ (80) -nginx_ Y Y | Automates obtaining and installing a certificate with Nginx. http-01_ (80) -webroot_ Y N | Obtains a certificate by writing to the webroot directory of http-01_ (80) - | an already running webserver. -standalone_ Y N | Uses a "standalone" webserver to obtain a certificate. http-01_ (80) - | Requires port 80 to be available. This is useful on - | systems with no webserver, or when direct integration with - | the local webserver is not supported or not desired. -|dns_plugs| Y N | This category of plugins automates obtaining a certificate by dns-01_ (53) - | modifying DNS records to prove you have control over a - | domain. Doing domain validation in this way is - | the only way to obtain wildcard certificates from Let's - | Encrypt. -manual_ Y N | Helps you obtain a certificate by giving you instructions to http-01_ (80) or - | perform domain validation yourself. Additionally allows you dns-01_ (53) - | to specify scripts to automate the validation task in a - | customized way. -=========== ==== ==== =============================================================== ============================= - -.. |dns_plugs| replace:: :ref:`DNS plugins ` - -Under the hood, plugins use one of several ACME protocol challenges_ to -prove you control a domain. The options are http-01_ (which uses port 80) -and dns-01_ (requiring configuration of a DNS server on -port 53, though that's often not the same machine as your webserver). A few -plugins support more than one challenge type, in which case you can choose one -with ``--preferred-challenges``. - -There are also many third-party-plugins_ available. Below we describe in more detail -the circumstances in which each plugin can be used, and how to use it. - -.. _challenges: https://tools.ietf.org/html/draft-ietf-acme-acme-03#section-7 -.. _http-01: https://tools.ietf.org/html/draft-ietf-acme-acme-03#section-7.2 -.. _dns-01: https://tools.ietf.org/html/draft-ietf-acme-acme-03#section-7.4 - -Apache ------- - -The Apache plugin currently `supports -`_ -modern OSes based on Debian, Fedora, SUSE, Gentoo and Darwin. -This automates both obtaining *and* installing certificates on an Apache -webserver. To specify this plugin on the command line, simply include -``--apache``. - -Webroot -------- - -If you're running a local webserver for which you have the ability -to modify the content being served, and you'd prefer not to stop the -webserver during the certificate issuance process, you can use the webroot -plugin to obtain a certificate by including ``certonly`` and ``--webroot`` on -the command line. In addition, you'll need to specify ``--webroot-path`` -or ``-w`` with the top-level directory ("web root") containing the files -served by your webserver. For example, ``--webroot-path /var/www/html`` -or ``--webroot-path /usr/share/nginx/html`` are two common webroot paths. - -If you're getting a certificate for many domains at once, the plugin -needs to know where each domain's files are served from, which could -potentially be a separate directory for each domain. When requesting a -certificate for multiple domains, each domain will use the most recently -specified ``--webroot-path``. So, for instance, - -:: - - certbot certonly --webroot -w /var/www/example -d www.example.com -d example.com -w /var/www/other -d other.example.net -d another.other.example.net - -would obtain a single certificate for all of those names, using the -``/var/www/example`` webroot directory for the first two, and -``/var/www/other`` for the second two. - -The webroot plugin works by creating a temporary file for each of your requested -domains in ``${webroot-path}/.well-known/acme-challenge``. Then the Let's Encrypt -validation server makes HTTP requests to validate that the DNS for each -requested domain resolves to the server running certbot. An example request -made to your web server would look like: - -:: - - 66.133.109.36 - - [05/Jan/2016:20:11:24 -0500] "GET /.well-known/acme-challenge/HGr8U1IeTW4kY_Z6UIyaakzOkyQgPr_7ArlLgtZE8SX HTTP/1.1" 200 87 "-" "Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)" - -Note that to use the webroot plugin, your server must be configured to serve -files from hidden directories. If ``/.well-known`` is treated specially by -your webserver configuration, you might need to modify the configuration -to ensure that files inside ``/.well-known/acme-challenge`` are served by -the webserver. - -Nginx ------ - -The Nginx plugin should work for most configurations. We recommend backing up -Nginx configurations before using it (though you can also revert changes to -configurations with ``certbot --nginx rollback``). You can use it by providing -the ``--nginx`` flag on the commandline. - -:: - - certbot --nginx - -.. _standalone: - -Standalone ----------- - -Use standalone mode to obtain a certificate if you don't want to use (or don't currently have) -existing server software. The standalone plugin does not rely on any other server -software running on the machine where you obtain the certificate. - -To obtain a certificate using a "standalone" webserver, you can use the -standalone plugin by including ``certonly`` and ``--standalone`` -on the command line. This plugin needs to bind to port 80 in -order to perform domain validation, so you may need to stop your -existing webserver. - -It must still be possible for your machine to accept inbound connections from -the Internet on the specified port using each requested domain name. - -By default, Certbot first attempts to bind to the port for all interfaces using -IPv6 and then bind to that port using IPv4; Certbot continues so long as at -least one bind succeeds. On most Linux systems, IPv4 traffic will be routed to -the bound IPv6 port and the failure during the second bind is expected. - -Use ``---address`` to explicitly tell Certbot which interface -(and protocol) to bind. - -.. note:: The ``--standalone-supported-challenges`` option has been - deprecated since ``certbot`` version 0.9.0. - -.. _dns_plugins: - -DNS Plugins ------------ - -If you'd like to obtain a wildcard certificate from Let's Encrypt or run -``certbot`` on a machine other than your target webserver, you can use one of -Certbot's DNS plugins. - -These plugins are not included in a default Certbot installation and must be -installed separately. While the DNS plugins cannot currently be used with -``certbot-auto``, they are available in many OS package managers and as Docker -images. Visit https://certbot.eff.org to learn the best way to use the DNS -plugins on your system. - -Once installed, you can find documentation on how to use each plugin at: - -* `certbot-dns-cloudflare `_ -* `certbot-dns-cloudxns `_ -* `certbot-dns-digitalocean `_ -* `certbot-dns-dnsimple `_ -* `certbot-dns-dnsmadeeasy `_ -* `certbot-dns-google `_ -* `certbot-dns-linode `_ -* `certbot-dns-luadns `_ -* `certbot-dns-nsone `_ -* `certbot-dns-ovh `_ -* `certbot-dns-rfc2136 `_ -* `certbot-dns-route53 `_ - -Manual ------- - -If you'd like to obtain a certificate running ``certbot`` on a machine -other than your target webserver or perform the steps for domain -validation yourself, you can use the manual plugin. While hidden from -the UI, you can use the plugin to obtain a certificate by specifying -``certonly`` and ``--manual`` on the command line. This requires you -to copy and paste commands into another terminal session, which may -be on a different computer. - -The manual plugin can use either the ``http`` or the ``dns`` challenge. You can use the ``--preferred-challenges`` option -to choose the challenge of your preference. - -The ``http`` challenge will ask you to place a file with a specific name and -specific content in the ``/.well-known/acme-challenge/`` directory directly -in the top-level directory (“web rootâ€) containing the files served by your -webserver. In essence it's the same as the webroot_ plugin, but not automated. - -When using the ``dns`` challenge, ``certbot`` will ask you to place a TXT DNS -record with specific contents under the domain name consisting of the hostname -for which you want a certificate issued, prepended by ``_acme-challenge``. - -For example, for the domain ``example.com``, a zone file entry would look like: - -:: - - _acme-challenge.example.com. 300 IN TXT "gfj9Xq...Rg85nM" - - -Additionally you can specify scripts to prepare for validation and -perform the authentication procedure and/or clean up after it by using -the ``--manual-auth-hook`` and ``--manual-cleanup-hook`` flags. This is -described in more depth in the hooks_ section. - -.. _combination: - -Combining plugins ------------------ - -Sometimes you may want to specify a combination of distinct authenticator and -installer plugins. To do so, specify the authenticator plugin with -``--authenticator`` or ``-a`` and the installer plugin with ``--installer`` or -``-i``. - -For instance, you could create a certificate using the webroot_ plugin -for authentication and the apache_ plugin for installation. - -:: - - certbot run -a webroot -i apache -w /var/www/html -d example.com - -Or you could create a certificate using the manual_ plugin for authentication -and the nginx_ plugin for installation. (Note that this certificate cannot -be renewed automatically.) - -:: - certbot run -a manual -i nginx -d example.com - -.. _third-party-plugins: - -Third-party plugins -------------------- - -There are also a number of third-party plugins for the client, provided by -other developers. Many are beta/experimental, but some are already in -widespread use: - -=========== ==== ==== =============================================================== -Plugin Auth Inst Notes -=========== ==== ==== =============================================================== -plesk_ Y Y Integration with the Plesk web hosting tool -haproxy_ Y Y Integration with the HAProxy load balancer -s3front_ Y Y Integration with Amazon CloudFront distribution of S3 buckets -gandi_ Y Y Integration with Gandi's hosting products and API -varnish_ Y N Obtain certificates via a Varnish server -external_ Y N A plugin for convenient scripting (See also ticket 2782_) -icecast_ N Y Deploy certificates to Icecast 2 streaming media servers -pritunl_ N Y Install certificates in pritunl distributed OpenVPN servers -proxmox_ N Y Install certificates in Proxmox Virtualization servers -postfix_ N Y STARTTLS Everywhere is becoming a Certbot Postfix/Exim plugin -heroku_ Y Y Integration with Heroku SSL -=========== ==== ==== =============================================================== - -.. _plesk: https://github.com/plesk/letsencrypt-plesk -.. _haproxy: https://github.com/greenhost/certbot-haproxy -.. _s3front: https://github.com/dlapiduz/letsencrypt-s3front -.. _gandi: https://github.com/Gandi/letsencrypt-gandi -.. _icecast: https://github.com/e00E/lets-encrypt-icecast -.. _varnish: http://git.sesse.net/?p=letsencrypt-varnish-plugin -.. _2782: https://github.com/certbot/certbot/issues/2782 -.. _pritunl: https://github.com/kharkevich/letsencrypt-pritunl -.. _proxmox: https://github.com/kharkevich/letsencrypt-proxmox -.. _external: https://github.com/marcan/letsencrypt-external -.. _postfix: https://github.com/EFForg/starttls-everywhere -.. _heroku: https://github.com/gboudreau/certbot-heroku - -If you're interested, you can also :ref:`write your own plugin `. - -.. _managing-certs: - -Managing certificates -===================== - -To view a list of the certificates Certbot knows about, run -the ``certificates`` subcommand: - -``certbot certificates`` - -This returns information in the following format:: - - Found the following certs: - Certificate Name: example.com - Domains: example.com, www.example.com - Expiry Date: 2017-02-19 19:53:00+00:00 (VALID: 30 days) - Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem - Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem - -``Certificate Name`` shows the name of the certificate. Pass this name -using the ``--cert-name`` flag to specify a particular certificate for the ``run``, -``certonly``, ``certificates``, ``renew``, and ``delete`` commands. Example:: - - certbot certonly --cert-name example.com - -.. _updating_certs: - -Re-creating and Updating Existing Certificates ----------------------------------------------- - -You can use ``certonly`` or ``run`` subcommands to request -the creation of a single new certificate even if you already have an -existing certificate with some of the same domain names. - -If a certificate is requested with ``run`` or ``certonly`` specifying a -certificate name that already exists, Certbot updates -the existing certificate. Otherwise a new certificate -is created and assigned the specified name. - -The ``--force-renewal``, ``--duplicate``, and ``--expand`` options -control Certbot's behavior when re-creating -a certificate with the same name as an existing certificate. -If you don't specify a requested behavior, Certbot may ask you what you intended. - - -``--force-renewal`` tells Certbot to request a new certificate -with the same domains as an existing certificate. Each domain -must be explicitly specified via ``-d``. If successful, this certificate -is saved alongside the earlier one and symbolic links (the "``live``" -reference) will be updated to point to the new certificate. This is a -valid method of renewing a specific individual -certificate. - -``--duplicate`` tells Certbot to create a separate, unrelated certificate -with the same domains as an existing certificate. This certificate is -saved completely separately from the prior one. Most users will not -need to issue this command in normal circumstances. - -``--expand`` tells Certbot to update an existing certificate with a new -certificate that contains all of the old domains and one or more additional -new domains. With the ``--expand`` option, use the ``-d`` option to specify -all existing domains and one or more new domains. - -Example: - -.. code-block:: none - - certbot --expand -d existing.com,example.com,newdomain.com - -If you prefer, you can specify the domains individually like this: - -.. code-block:: none - - certbot --expand -d existing.com -d example.com -d newdomain.com - -Consider using ``--cert-name`` instead of ``--expand``, as it gives more control -over which certificate is modified and it lets you remove domains as well as adding them. - - -``--allow-subset-of-names`` tells Certbot to continue with certificate generation if -only some of the specified domain authorizations can be obtained. This may -be useful if some domains specified in a certificate no longer point at this -system. - -Whenever you obtain a new certificate in any of these ways, the new -certificate exists alongside any previously obtained certificates, whether -or not the previous certificates have expired. The generation of a new -certificate counts against several rate limits that are intended to prevent -abuse of the ACME protocol, as described -`here `__. - -.. _changing: - -Changing a Certificate's Domains -================================ - -The ``--cert-name`` flag can also be used to modify the domains a certificate contains, -by specifying new domains using the ``-d`` or ``--domains`` flag. If certificate ``example.com`` -previously contained ``example.com`` and ``www.example.com``, it can be modified to only -contain ``example.com`` by specifying only ``example.com`` with the ``-d`` or ``--domains`` flag. Example:: - - certbot certonly --cert-name example.com -d example.com - -The same format can be used to expand the set of domains a certificate contains, or to -replace that set entirely:: - - certbot certonly --cert-name example.com -d example.org,www.example.org - - -Revoking certificates ---------------------- - -If your account key has been compromised or you otherwise need to revoke a certificate, -use the ``revoke`` command to do so. Note that the ``revoke`` command takes the certificate path -(ending in ``cert.pem``), not a certificate name or domain. Example:: - - certbot revoke --cert-path /etc/letsencrypt/live/CERTNAME/cert.pem - -You can also specify the reason for revoking your certificate by using the ``reason`` flag. -Reasons include ``unspecified`` which is the default, as well as ``keycompromise``, -``affiliationchanged``, ``superseded``, and ``cessationofoperation``:: - - certbot revoke --cert-path /etc/letsencrypt/live/CERTNAME/cert.pem --reason keycompromise - -Additionally, if a certificate -is a test certificate obtained via the ``--staging`` or ``--test-cert`` flag, that flag must be passed to the -``revoke`` subcommand. -Once a certificate is revoked (or for other certificate management tasks), all of a certificate's -relevant files can be removed from the system with the ``delete`` subcommand:: - - certbot delete --cert-name example.com - -.. note:: If you don't use ``delete`` to remove the certificate completely, it will be renewed automatically at the next renewal event. - -.. note:: Revoking a certificate will have no effect on the rate limit imposed by the Let's Encrypt server. - -.. _renewal: - -Renewing certificates ---------------------- - -.. note:: Let's Encrypt CA issues short-lived certificates (90 - days). Make sure you renew the certificates at least once in 3 - months. - -.. seealso:: Many of the certbot clients obtained through a - distribution come with automatic renewal out of the box, - such as Debian and Ubuntu versions installed through `apt`, - CentOS/RHEL 7 through EPEL, etc. See `Automated Renewals`_ - for more details. - -As of version 0.10.0, Certbot supports a ``renew`` action to check -all installed certificates for impending expiry and attempt to renew -them. The simplest form is simply - -``certbot renew`` - -This command attempts to renew any previously-obtained certificates that -expire in less than 30 days. The same plugin and options that were used -at the time the certificate was originally issued will be used for the -renewal attempt, unless you specify other plugins or options. Unlike ``certonly``, ``renew`` acts on -multiple certificates and always takes into account whether each one is near -expiry. Because of this, ``renew`` is suitable (and designed) for automated use, -to allow your system to automatically renew each certificate when appropriate. -Since ``renew`` only renews certificates that are near expiry it can be -run as frequently as you want - since it will usually take no action. - -The ``renew`` command includes hooks for running commands or scripts before or after a certificate is -renewed. For example, if you have a single certificate obtained using -the standalone_ plugin, you might need to stop the webserver -before renewing so standalone can bind to the necessary ports, and -then restart it after the plugin is finished. Example:: - - certbot renew --pre-hook "service nginx stop" --post-hook "service nginx start" - -If a hook exits with a non-zero exit code, the error will be printed -to ``stderr`` but renewal will be attempted anyway. A failing hook -doesn't directly cause Certbot to exit with a non-zero exit code, but -since Certbot exits with a non-zero exit code when renewals fail, a -failed hook causing renewal failures will indirectly result in a -non-zero exit code. Hooks will only be run if a certificate is due for -renewal, so you can run the above command frequently without -unnecessarily stopping your webserver. - -``--pre-hook`` and ``--post-hook`` hooks run before and after every renewal -attempt. If you want your hook to run only after a successful renewal, use -``--deploy-hook`` in a command like this. - -``certbot renew --deploy-hook /path/to/deploy-hook-script`` - -For example, if you have a daemon that does not read its certificates as the -root user, a deploy hook like this can copy them to the correct location and -apply appropriate file permissions. - -/path/to/deploy-hook-script - -.. code-block:: none - - #!/bin/sh - - set -e - - for domain in $RENEWED_DOMAINS; do - case $domain in - example.com) - daemon_cert_root=/etc/some-daemon/certs - - # Make sure the certificate and private key files are - # never world readable, even just for an instant while - # we're copying them into daemon_cert_root. - umask 077 - - cp "$RENEWED_LINEAGE/fullchain.pem" "$daemon_cert_root/$domain.cert" - cp "$RENEWED_LINEAGE/privkey.pem" "$daemon_cert_root/$domain.key" - - # Apply the proper file ownership and permissions for - # the daemon to read its certificate and key. - chown some-daemon "$daemon_cert_root/$domain.cert" \ - "$daemon_cert_root/$domain.key" - chmod 400 "$daemon_cert_root/$domain.cert" \ - "$daemon_cert_root/$domain.key" - - service some-daemon restart >/dev/null - ;; - esac - done - -You can also specify hooks by placing files in subdirectories of Certbot's -configuration directory. Assuming your configuration directory is -``/etc/letsencrypt``, any executable files found in -``/etc/letsencrypt/renewal-hooks/pre``, -``/etc/letsencrypt/renewal-hooks/deploy``, and -``/etc/letsencrypt/renewal-hooks/post`` will be run as pre, deploy, and post -hooks respectively when any certificate is renewed with the ``renew`` -subcommand. These hooks are run in alphabetical order and are not run for other -subcommands. (The order the hooks are run is determined by the byte value of -the characters in their filenames and is not dependent on your locale.) - -Hooks specified in the command line, :ref:`configuration file -`, or :ref:`renewal configuration files ` are -run as usual after running all hooks in these directories. One minor exception -to this is if a hook specified elsewhere is simply the path to an executable -file in the hook directory of the same type (e.g. your pre-hook is the path to -an executable in ``/etc/letsencrypt/renewal-hooks/pre``), the file is not run a -second time. You can stop Certbot from automatically running executables found -in these directories by including ``--no-directory-hooks`` on the command line. - -More information about hooks can be found by running -``certbot --help renew``. - -If you're sure that this command executes successfully without human -intervention, you can add the command to ``crontab`` (since certificates -are only renewed when they're determined to be near expiry, the command -can run on a regular basis, like every week or every day). In that case, -you are likely to want to use the ``-q`` or ``--quiet`` quiet flag to -silence all output except errors. - -If you are manually renewing all of your certificates, the -``--force-renewal`` flag may be helpful; it causes the expiration time of -the certificate(s) to be ignored when considering renewal, and attempts to -renew each and every installed certificate regardless of its age. (This -form is not appropriate to run daily because each certificate will be -renewed every day, which will quickly run into the certificate authority -rate limit.) - -Note that options provided to ``certbot renew`` will apply to -*every* certificate for which renewal is attempted; for example, -``certbot renew --rsa-key-size 4096`` would try to replace every -near-expiry certificate with an equivalent certificate using a 4096-bit -RSA public key. If a certificate is successfully renewed using -specified options, those options will be saved and used for future -renewals of that certificate. - -An alternative form that provides for more fine-grained control over the -renewal process (while renewing specified certificates one at a time), -is ``certbot certonly`` with the complete set of subject domains of -a specific certificate specified via `-d` flags. You may also want to -include the ``-n`` or ``--noninteractive`` flag to prevent blocking on -user input (which is useful when running the command from cron). - -``certbot certonly -n -d example.com -d www.example.com`` - -All of the domains covered by the certificate must be specified in -this case in order to renew and replace the old certificate rather -than obtaining a new one; don't forget any `www.` domains! Specifying -a subset of the domains creates a new, separate certificate containing -only those domains, rather than replacing the original certificate. -When run with a set of domains corresponding to an existing certificate, -the ``certonly`` command attempts to renew that specific certificate. - -Please note that the CA will send notification emails to the address -you provide if you do not renew certificates that are about to expire. - -Certbot is working hard to improve the renewal process, and we -apologize for any inconvenience you encounter in integrating these -commands into your individual environment. - -.. note:: ``certbot renew`` exit status will only be 1 if a renewal attempt failed. - This means ``certbot renew`` exit status will be 0 if no certificate needs to be updated. - If you write a custom script and expect to run a command only after a certificate was actually renewed - you will need to use the ``--deploy-hook`` since the exit status will be 0 both on successful renewal - and when renewal is not necessary. - -.. _renewal-config-file: - - -Modifying the Renewal Configuration File ----------------------------------------- - -When a certificate is issued, by default Certbot creates a renewal configuration file that -tracks the options that were selected when Certbot was run. This allows Certbot -to use those same options again when it comes time for renewal. These renewal -configuration files are located at ``/etc/letsencrypt/renewal/CERTNAME``. - -For advanced certificate management tasks, it is possible to manually modify the certificate's -renewal configuration file, but this is discouraged since it can easily break Certbot's -ability to renew your certificates. If you choose to modify the renewal configuration file -we advise you to test its validity with the ``certbot renew --dry-run`` command. - -.. warning:: Modifying any files in ``/etc/letsencrypt`` can damage them so Certbot can no longer properly manage its certificates, and we do not recommend doing so. - -For most tasks, it is safest to limit yourself to pointing symlinks at the files there, or using -``--deploy-hook`` to copy / make new files based upon those files, if your operational situation requires it -(for instance, combining certificates and keys in different way, or having copies of things with different -specific permissions that are demanded by other programs). - -If the contents of ``/etc/letsencrypt/archive/CERTNAME`` are moved to a new folder, first specify -the new folder's name in the renewal configuration file, then run ``certbot update_symlinks`` to -point the symlinks in ``/etc/letsencrypt/live/CERTNAME`` to the new folder. - -If you would like the live certificate files whose symlink location Certbot updates on each run to -reside in a different location, first move them to that location, then specify the full path of -each of the four files in the renewal configuration file. Since the symlinks are relative links, -you must follow this with an invocation of ``certbot update_symlinks``. - -For example, say that a certificate's renewal configuration file previously contained the following -directives:: - - archive_dir = /etc/letsencrypt/archive/example.com - cert = /etc/letsencrypt/live/example.com/cert.pem - privkey = /etc/letsencrypt/live/example.com/privkey.pem - chain = /etc/letsencrypt/live/example.com/chain.pem - fullchain = /etc/letsencrypt/live/example.com/fullchain.pem - -The following commands could be used to specify where these files are located:: - - mv /etc/letsencrypt/archive/example.com /home/user/me/certbot/example_archive - sed -i 's,/etc/letsencrypt/archive/example.com,/home/user/me/certbot/example_archive,' /etc/letsencrypt/renewal/example.com.conf - mv /etc/letsencrypt/live/example.com/*.pem /home/user/me/certbot/ - sed -i 's,/etc/letsencrypt/live/example.com,/home/user/me/certbot,g' /etc/letsencrypt/renewal/example.com.conf - certbot update_symlinks - -Automated Renewals ------------------- - -Many Linux distributions provide automated renewal when you use the -packages installed through their system package manager. The -following table is an *incomplete* list of distributions which do so, -as well as their methods for doing so. - -If you are not sure whether or not your system has this already -automated, refer to your distribution's documentation, or check your -system's crontab (typically in `/etc/crontab/` and `/etc/cron.*/*` and -systemd timers (`systemctl list-timers`). - -.. csv-table:: Distributions with Automated Renewal - :header: "Distribution Name", "Distribution Version", "Automation Method" - - "CentOS", "EPEL 7", "systemd" - "Debian", "jessie", "cron, systemd" - "Debian", "stretch", "cron, systemd" - "Debian", "testing/sid", "cron, systemd" - "Fedora", "26", "systemd" - "Fedora", "27", "systemd" - "RHEL", "EPEL 7", "systemd" - "Ubuntu", "17.10", "cron, systemd" - "Ubuntu", "certbot PPA", "cron, systemd" - -.. _where-certs: - -Where are my certificates? -========================== - -All generated keys and issued certificates can be found in -``/etc/letsencrypt/live/$domain``. In the case of creating a SAN certificate -with multiple alternative names, ``$domain`` is the first domain passed in -via -d parameter. Rather than copying, please point -your (web) server configuration directly to those files (or create -symlinks). During the renewal_, ``/etc/letsencrypt/live`` is updated -with the latest necessary files. - -.. note:: ``/etc/letsencrypt/archive`` and ``/etc/letsencrypt/keys`` - contain all previous keys and certificates, while - ``/etc/letsencrypt/live`` symlinks to the latest versions. - -The following files are available: - -``privkey.pem`` - Private key for the certificate. - - .. warning:: This **must be kept secret at all times**! Never share - it with anyone, including Certbot developers. You cannot - put it into a safe, however - your server still needs to access - this file in order for SSL/TLS to work. - - .. note:: As of Certbot version 0.29.0, private keys for new certificate - default to ``0600``. Any changes to the group mode or group owner (gid) - of this file will be preserved on renewals. - - This is what Apache needs for `SSLCertificateKeyFile - `_, - and Nginx for `ssl_certificate_key - `_. - -``fullchain.pem`` - All certificates, **including** server certificate (aka leaf certificate or - end-entity certificate). The server certificate is the first one in this file, - followed by any intermediates. - - This is what Apache >= 2.4.8 needs for `SSLCertificateFile - `_, - and what Nginx needs for `ssl_certificate - `_. - -``cert.pem`` and ``chain.pem`` (less common) - ``cert.pem`` contains the server certificate by itself, and - ``chain.pem`` contains the additional intermediate certificate or - certificates that web browsers will need in order to validate the - server certificate. If you provide one of these files to your web - server, you **must** provide both of them, or some browsers will show - "This Connection is Untrusted" errors for your site, `some of the time - `_. - - Apache < 2.4.8 needs these for `SSLCertificateFile - `_. - and `SSLCertificateChainFile - `_, - respectively. - - If you're using OCSP stapling with Nginx >= 1.3.7, ``chain.pem`` should be - provided as the `ssl_trusted_certificate - `_ - to validate OCSP responses. - -.. note:: All files are PEM-encoded. - If you need other format, such as DER or PFX, then you - could convert using ``openssl``. You can automate that with - ``--deploy-hook`` if you're using automatic renewal_. - -.. _hooks: - -Pre and Post Validation Hooks -============================= - -Certbot allows for the specification of pre and post validation hooks when run -in manual mode. The flags to specify these scripts are ``--manual-auth-hook`` -and ``--manual-cleanup-hook`` respectively and can be used as follows: - -:: - - certbot certonly --manual --manual-auth-hook /path/to/http/authenticator.sh --manual-cleanup-hook /path/to/http/cleanup.sh -d secure.example.com - -This will run the ``authenticator.sh`` script, attempt the validation, and then run -the ``cleanup.sh`` script. Additionally certbot will pass relevant environment -variables to these scripts: - -- ``CERTBOT_DOMAIN``: The domain being authenticated -- ``CERTBOT_VALIDATION``: The validation string (HTTP-01 and DNS-01 only) -- ``CERTBOT_TOKEN``: Resource name part of the HTTP-01 challenge (HTTP-01 only) - -Additionally for cleanup: - -- ``CERTBOT_AUTH_OUTPUT``: Whatever the auth script wrote to stdout - -Example usage for HTTP-01: - -:: - - certbot certonly --manual --preferred-challenges=http --manual-auth-hook /path/to/http/authenticator.sh --manual-cleanup-hook /path/to/http/cleanup.sh -d secure.example.com - -/path/to/http/authenticator.sh - -.. code-block:: none - - #!/bin/bash - echo $CERTBOT_VALIDATION > /var/www/htdocs/.well-known/acme-challenge/$CERTBOT_TOKEN - -/path/to/http/cleanup.sh - -.. code-block:: none - - #!/bin/bash - rm -f /var/www/htdocs/.well-known/acme-challenge/$CERTBOT_TOKEN - -Example usage for DNS-01 (Cloudflare API v4) (for example purposes only, do not use as-is) - -:: - - certbot certonly --manual --preferred-challenges=dns --manual-auth-hook /path/to/dns/authenticator.sh --manual-cleanup-hook /path/to/dns/cleanup.sh -d secure.example.com - -/path/to/dns/authenticator.sh - -.. code-block:: none - - #!/bin/bash - - # Get your API key from https://www.cloudflare.com/a/account/my-account - API_KEY="your-api-key" - EMAIL="your.email@example.com" - - # Strip only the top domain to get the zone id - DOMAIN=$(expr match "$CERTBOT_DOMAIN" '.*\.\(.*\..*\)') - - # Get the Cloudflare zone id - ZONE_EXTRA_PARAMS="status=active&page=1&per_page=20&order=status&direction=desc&match=all" - ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN&$ZONE_EXTRA_PARAMS" \ - -H "X-Auth-Email: $EMAIL" \ - -H "X-Auth-Key: $API_KEY" \ - -H "Content-Type: application/json" | python -c "import sys,json;print(json.load(sys.stdin)['result'][0]['id'])") - - # Create TXT record - CREATE_DOMAIN="_acme-challenge.$CERTBOT_DOMAIN" - RECORD_ID=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \ - -H "X-Auth-Email: $EMAIL" \ - -H "X-Auth-Key: $API_KEY" \ - -H "Content-Type: application/json" \ - --data '{"type":"TXT","name":"'"$CREATE_DOMAIN"'","content":"'"$CERTBOT_VALIDATION"'","ttl":120}' \ - | python -c "import sys,json;print(json.load(sys.stdin)['result']['id'])") - # Save info for cleanup - if [ ! -d /tmp/CERTBOT_$CERTBOT_DOMAIN ];then - mkdir -m 0700 /tmp/CERTBOT_$CERTBOT_DOMAIN - fi - echo $ZONE_ID > /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID - echo $RECORD_ID > /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID - - # Sleep to make sure the change has time to propagate over to DNS - sleep 25 - -/path/to/dns/cleanup.sh - -.. code-block:: none - - #!/bin/bash - - # Get your API key from https://www.cloudflare.com/a/account/my-account - API_KEY="your-api-key" - EMAIL="your.email@example.com" - - if [ -f /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID ]; then - ZONE_ID=$(cat /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID) - rm -f /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID - fi - - if [ -f /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID ]; then - RECORD_ID=$(cat /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID) - rm -f /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID - fi - - # Remove the challenge TXT record from the zone - if [ -n "${ZONE_ID}" ]; then - if [ -n "${RECORD_ID}" ]; then - curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \ - -H "X-Auth-Email: $EMAIL" \ - -H "X-Auth-Key: $API_KEY" \ - -H "Content-Type: application/json" - fi - fi - -.. _lock-files: - -Changing the ACME Server -======================== - -By default, Certbot uses Let's Encrypt's initial production server at -https://acme-v01.api.letsencrypt.org/. You can tell Certbot to use a -different CA by providing ``--server`` on the command line or in a -:ref:`configuration file ` with the URL of the server's -ACME directory. For example, if you would like to use Let's Encrypt's -new ACMEv2 server, you would add ``--server -https://acme-v02.api.letsencrypt.org/directory`` to the command line. -Certbot will automatically select which version of the ACME protocol to -use based on the contents served at the provided URL. - -If you use ``--server`` to specify an ACME CA that implements a newer -version of the spec, you may be able to obtain a certificate for a -wildcard domain. Some CAs (such as Let's Encrypt) require that domain -validation for wildcard domains must be done through modifications to -DNS records which means that the dns-01_ challenge type must be used. To -see a list of Certbot plugins that support this challenge type and how -to use them, see plugins_. - -Lock Files -========== - -When processing a validation Certbot writes a number of lock files on your system -to prevent multiple instances from overwriting each other's changes. This means -that by default two instances of Certbot will not be able to run in parallel. - -Since the directories used by Certbot are configurable, Certbot -will write a lock file for all of the directories it uses. This include Certbot's -``--work-dir``, ``--logs-dir``, and ``--config-dir``. By default these are -``/var/lib/letsencrypt``, ``/var/logs/letsencrypt``, and ``/etc/letsencrypt`` -respectively. Additionally if you are using Certbot with Apache or nginx it will -lock the configuration folder for that program, which are typically also in the -``/etc`` directory. - -Note that these lock files will only prevent other instances of Certbot from -using those directories, not other processes. If you'd like to run multiple -instances of Certbot simultaneously you should specify different directories -as the ``--work-dir``, ``--logs-dir``, and ``--config-dir`` for each instance -of Certbot that you would like to run. - -.. _config-file: - -Configuration file -================== - -Certbot accepts a global configuration file that applies its options to all invocations -of Certbot. Certificate specific configuration choices should be set in the ``.conf`` -files that can be found in ``/etc/letsencrypt/renewal``. - -By default no cli.ini file is created, after creating one -it is possible to specify the location of this configuration file with -``certbot-auto --config cli.ini`` (or shorter ``-c cli.ini``). An -example configuration file is shown below: - -.. include:: ../examples/cli.ini - :code: ini - -By default, the following locations are searched: - -- ``/etc/letsencrypt/cli.ini`` -- ``$XDG_CONFIG_HOME/letsencrypt/cli.ini`` (or - ``~/.config/letsencrypt/cli.ini`` if ``$XDG_CONFIG_HOME`` is not - set). - -Since this configuration file applies to all invocations of certbot it is incorrect -to list domains in it. Listing domains in cli.ini may prevent renewal from working. -Additionally due to how arguments in cli.ini are parsed, options which wish to -not be set should not be listed. Options set to false will instead be read -as being set to true by older versions of Certbot, since they have been listed -in the config file. - -.. keep it up to date with constants.py - -.. _log-rotation: - -Log Rotation -============ - -By default certbot stores status logs in ``/var/log/letsencrypt``. By default -certbot will begin rotating logs once there are 1000 logs in the log directory. -Meaning that once 1000 files are in ``/var/log/letsencrypt`` Certbot will delete -the oldest one to make room for new logs. The number of subsequent logs can be -changed by passing the desired number to the command line flag -``--max-log-backups``. - -.. note:: Some distributions, including Debian and Ubuntu, disable - certbot's internal log rotation in favor of a more traditional - logrotate script. If you are using a distribution's packages and - want to alter the log rotation, check `/etc/logrotate.d/` for a - certbot rotation script. - -.. _command-line: - -Certbot command-line options -============================ - -Certbot supports a lot of command line options. Here's the full list, from -``certbot --help all``: - -.. literalinclude:: cli-help.txt - -Getting help -============ - -If you're having problems, we recommend posting on the Let's Encrypt -`Community Forum `_. - -If you find a bug in the software, please do report it in our `issue -tracker `_. Remember to -give us as much information as possible: - -- copy and paste exact command line used and the output (though mind - that the latter might include some personally identifiable - information, including your email and domains) -- copy and paste logs from ``/var/log/letsencrypt`` (though mind they - also might contain personally identifiable information) -- copy and paste ``certbot --version`` output -- your operating system, including specific version -- specify which installation method you've chosen diff --git a/examples/cli.ini b/examples/cli.ini deleted file mode 100644 index dbaa9c59957..00000000000 --- a/examples/cli.ini +++ /dev/null @@ -1,23 +0,0 @@ -# This is an example of the kind of things you can do in a configuration file. -# All flags used by the client can be configured here. Run Certbot with -# "--help" to learn more about the available options. -# -# Note that these options apply automatically to all use of Certbot for -# obtaining or renewing certificates, so options specific to a single -# certificate on a system with several certificates should not be placed -# here. - -# Use a 4096 bit RSA key instead of 2048 -rsa-key-size = 4096 - -# Uncomment and update to register with the specified e-mail address -# email = foo@example.com - -# Uncomment to use the standalone authenticator on port 443 -# authenticator = standalone -# standalone-supported-challenges = tls-sni-01 - -# Uncomment to use the webroot authenticator. Replace webroot-path with the -# path to the public_html / webroot folder being served by your web server. -# authenticator = webroot -# webroot-path = /usr/share/nginx/html diff --git a/examples/plugins/certbot_example_plugins.py b/examples/plugins/certbot_example_plugins.py deleted file mode 100644 index 9dec2e108a7..00000000000 --- a/examples/plugins/certbot_example_plugins.py +++ /dev/null @@ -1,31 +0,0 @@ -"""Example Certbot plugins. - -For full examples, see `certbot.plugins`. - -""" -import zope.interface - -from certbot import interfaces -from certbot.plugins import common - - -@zope.interface.implementer(interfaces.IAuthenticator) -@zope.interface.provider(interfaces.IPluginFactory) -class Authenticator(common.Plugin): - """Example Authenticator.""" - - description = "Example Authenticator plugin" - - # Implement all methods from IAuthenticator, remembering to add - # "self" as first argument, e.g. def prepare(self)... - - -@zope.interface.implementer(interfaces.IInstaller) -@zope.interface.provider(interfaces.IPluginFactory) -class Installer(common.Plugin): - """Example Installer.""" - - description = "Example Installer plugin" - - # Implement all methods from IInstaller, remembering to add - # "self" as first argument, e.g. def get_all_names(self)... diff --git a/examples/plugins/setup.py b/examples/plugins/setup.py deleted file mode 100644 index 4538e83b88e..00000000000 --- a/examples/plugins/setup.py +++ /dev/null @@ -1,17 +0,0 @@ -from setuptools import setup - - -setup( - name='certbot-example-plugins', - package='certbot_example_plugins.py', - install_requires=[ - 'certbot', - 'zope.interface', - ], - entry_points={ - 'certbot.plugins': [ - 'example_authenticator = certbot_example_plugins:Authenticator', - 'example_installer = certbot_example_plugins:Installer', - ], - }, -) diff --git a/letsencrypt-auto b/letsencrypt-auto deleted file mode 100755 index be2c3679b70..00000000000 --- a/letsencrypt-auto +++ /dev/null @@ -1,1702 +0,0 @@ -#!/bin/sh -# -# Download and run the latest release version of the Certbot client. -# -# NOTE: THIS SCRIPT IS AUTO-GENERATED AND SELF-UPDATING -# -# IF YOU WANT TO EDIT IT LOCALLY, *ALWAYS* RUN YOUR COPY WITH THE -# "--no-self-upgrade" FLAG -# -# IF YOU WANT TO SEND PULL REQUESTS, THE REAL SOURCE FOR THIS FILE IS -# letsencrypt-auto-source/letsencrypt-auto.template AND -# letsencrypt-auto-source/pieces/bootstrappers/* - -set -e # Work even if somebody does "sh thisscript.sh". - -# Note: you can set XDG_DATA_HOME or VENV_PATH before running this script, -# if you want to change where the virtual environment will be installed - -# HOME might not be defined when being run through something like systemd -if [ -z "$HOME" ]; then - HOME=~root -fi -if [ -z "$XDG_DATA_HOME" ]; then - XDG_DATA_HOME=~/.local/share -fi -if [ -z "$VENV_PATH" ]; then - # We export these values so they are preserved properly if this script is - # rerun with sudo/su where $HOME/$XDG_DATA_HOME may have a different value. - export OLD_VENV_PATH="$XDG_DATA_HOME/letsencrypt" - export VENV_PATH="/opt/eff.org/certbot/venv" -fi -VENV_BIN="$VENV_PATH/bin" -BOOTSTRAP_VERSION_PATH="$VENV_PATH/certbot-auto-bootstrap-version.txt" -LE_AUTO_VERSION="0.30.0" -BASENAME=$(basename $0) -USAGE="Usage: $BASENAME [OPTIONS] -A self-updating wrapper script for the Certbot ACME client. When run, updates -to both this script and certbot will be downloaded and installed. After -ensuring you have the latest versions installed, certbot will be invoked with -all arguments you have provided. - -Help for certbot itself cannot be provided until it is installed. - - --debug attempt experimental installation - -h, --help print this help - -n, --non-interactive, --noninteractive run without asking for user input - --no-bootstrap do not install OS dependencies - --no-self-upgrade do not download updates - --os-packages-only install OS dependencies and exit - --install-only install certbot, upgrade if needed, and exit - -v, --verbose provide more output - -q, --quiet provide only update/error output; - implies --non-interactive - -All arguments are accepted and forwarded to the Certbot client when run." -export CERTBOT_AUTO="$0" - -for arg in "$@" ; do - case "$arg" in - --debug) - DEBUG=1;; - --os-packages-only) - OS_PACKAGES_ONLY=1;; - --install-only) - INSTALL_ONLY=1;; - --no-self-upgrade) - # Do not upgrade this script (also prevents client upgrades, because each - # copy of the script pins a hash of the python client) - NO_SELF_UPGRADE=1;; - --no-bootstrap) - NO_BOOTSTRAP=1;; - --help) - HELP=1;; - --noninteractive|--non-interactive) - NONINTERACTIVE=1;; - --quiet) - QUIET=1;; - renew) - ASSUME_YES=1;; - --verbose) - VERBOSE=1;; - -[!-]*) - OPTIND=1 - while getopts ":hnvq" short_arg $arg; do - case "$short_arg" in - h) - HELP=1;; - n) - NONINTERACTIVE=1;; - q) - QUIET=1;; - v) - VERBOSE=1;; - esac - done;; - esac -done - -if [ $BASENAME = "letsencrypt-auto" ]; then - # letsencrypt-auto does not respect --help or --yes for backwards compatibility - NONINTERACTIVE=1 - HELP=0 -fi - -# Set ASSUME_YES to 1 if QUIET or NONINTERACTIVE -if [ "$QUIET" = 1 -o "$NONINTERACTIVE" = 1 ]; then - ASSUME_YES=1 -fi - -say() { - if [ "$QUIET" != 1 ]; then - echo "$@" - fi -} - -error() { - echo "$@" -} - -# Support for busybox and others where there is no "command", -# but "which" instead -if command -v command > /dev/null 2>&1 ; then - export EXISTS="command -v" -elif which which > /dev/null 2>&1 ; then - export EXISTS="which" -else - error "Cannot find command nor which... please install one!" - exit 1 -fi - -# Certbot itself needs root access for almost all modes of operation. -# certbot-auto needs root access to bootstrap OS dependencies and install -# Certbot at a protected path so it can be safely run as root. To accomplish -# this, this script will attempt to run itself as root if it doesn't have the -# necessary privileges by using `sudo` or falling back to `su` if it is not -# available. The mechanism used to obtain root access can be set explicitly by -# setting the environment variable LE_AUTO_SUDO to 'sudo', 'su', 'su_sudo', -# 'SuSudo', or '' as used below. - -# Because the parameters in `su -c` has to be a string, -# we need to properly escape it. -SuSudo() { - args="" - # This `while` loop iterates over all parameters given to this function. - # For each parameter, all `'` will be replace by `'"'"'`, and the escaped string - # will be wrapped in a pair of `'`, then appended to `$args` string - # For example, `echo "It's only 1\$\!"` will be escaped to: - # 'echo' 'It'"'"'s only 1$!' - # │ │└┼┘│ - # │ │ │ └── `'s only 1$!'` the literal string - # │ │ └── `\"'\"` is a single quote (as a string) - # │ └── `'It'`, to be concatenated with the strings following it - # └── `echo` wrapped in a pair of `'`, it's totally fine for the shell command itself - while [ $# -ne 0 ]; do - args="$args'$(printf "%s" "$1" | sed -e "s/'/'\"'\"'/g")' " - shift - done - su root -c "$args" -} - -# Sets the environment variable SUDO to be the name of the program or function -# to call to get root access. If this script already has root privleges, SUDO -# is set to an empty string. The value in SUDO should be run with the command -# to called with root privileges as arguments. -SetRootAuthMechanism() { - SUDO="" - if [ -n "${LE_AUTO_SUDO+x}" ]; then - case "$LE_AUTO_SUDO" in - SuSudo|su_sudo|su) - SUDO=SuSudo - ;; - sudo) - SUDO="sudo -E" - ;; - '') ;; # Nothing to do for plain root method. - *) - error "Error: unknown root authorization mechanism '$LE_AUTO_SUDO'." - exit 1 - esac - say "Using preset root authorization mechanism '$LE_AUTO_SUDO'." - else - if test "`id -u`" -ne "0" ; then - if $EXISTS sudo 1>/dev/null 2>&1; then - SUDO="sudo -E" - else - say \"sudo\" is not available, will use \"su\" for installation steps... - SUDO=SuSudo - fi - fi - fi -} - -if [ "$1" = "--cb-auto-has-root" ]; then - shift 1 -else - SetRootAuthMechanism - if [ -n "$SUDO" ]; then - say "Requesting to rerun $0 with root privileges..." - $SUDO "$0" --cb-auto-has-root "$@" - exit 0 - fi -fi - -# Runs this script again with the given arguments. --cb-auto-has-root is added -# to the command line arguments to ensure we don't try to acquire root a -# second time. After the script is rerun, we exit the current script. -RerunWithArgs() { - "$0" --cb-auto-has-root "$@" - exit 0 -} - -BootstrapMessage() { - # Arguments: Platform name - say "Bootstrapping dependencies for $1... (you can skip this with --no-bootstrap)" -} - -ExperimentalBootstrap() { - # Arguments: Platform name, bootstrap function name - if [ "$DEBUG" = 1 ]; then - if [ "$2" != "" ]; then - BootstrapMessage $1 - $2 - fi - else - error "FATAL: $1 support is very experimental at present..." - error "if you would like to work on improving it, please ensure you have backups" - error "and then run this script again with the --debug flag!" - error "Alternatively, you can install OS dependencies yourself and run this script" - error "again with --no-bootstrap." - exit 1 - fi -} - -DeprecationBootstrap() { - # Arguments: Platform name, bootstrap function name - if [ "$DEBUG" = 1 ]; then - if [ "$2" != "" ]; then - BootstrapMessage $1 - $2 - fi - else - error "WARNING: certbot-auto support for this $1 is DEPRECATED!" - error "Please visit certbot.eff.org to learn how to download a version of" - error "Certbot that is packaged for your system. While an existing version" - error "of certbot-auto may work currently, we have stopped supporting updating" - error "system packages for your system. Please switch to a packaged version" - error "as soon as possible." - exit 1 - fi -} - -MIN_PYTHON_VERSION="2.7" -MIN_PYVER=$(echo "$MIN_PYTHON_VERSION" | sed 's/\.//') -# Sets LE_PYTHON to Python version string and PYVER to the first two -# digits of the python version -DeterminePythonVersion() { - # Arguments: "NOCRASH" if we shouldn't crash if we don't find a good python - # - # If no Python is found, PYVER is set to 0. - if [ "$USE_PYTHON_3" = 1 ]; then - for LE_PYTHON in "$LE_PYTHON" python3; do - # Break (while keeping the LE_PYTHON value) if found. - $EXISTS "$LE_PYTHON" > /dev/null && break - done - else - for LE_PYTHON in "$LE_PYTHON" python2.7 python27 python2 python; do - # Break (while keeping the LE_PYTHON value) if found. - $EXISTS "$LE_PYTHON" > /dev/null && break - done - fi - if [ "$?" != "0" ]; then - if [ "$1" != "NOCRASH" ]; then - error "Cannot find any Pythons; please install one!" - exit 1 - else - PYVER=0 - return 0 - fi - fi - - PYVER=`"$LE_PYTHON" -V 2>&1 | cut -d" " -f 2 | cut -d. -f1,2 | sed 's/\.//'` - if [ "$PYVER" -lt "$MIN_PYVER" ]; then - if [ "$1" != "NOCRASH" ]; then - error "You have an ancient version of Python entombed in your operating system..." - error "This isn't going to work; you'll need at least version $MIN_PYTHON_VERSION." - exit 1 - fi - fi -} - -# If new packages are installed by BootstrapDebCommon below, this version -# number must be increased. -BOOTSTRAP_DEB_COMMON_VERSION=1 - -BootstrapDebCommon() { - # Current version tested with: - # - # - Ubuntu - # - 14.04 (x64) - # - 15.04 (x64) - # - Debian - # - 7.9 "wheezy" (x64) - # - sid (2015-10-21) (x64) - - # Past versions tested with: - # - # - Debian 8.0 "jessie" (x64) - # - Raspbian 7.8 (armhf) - - # Believed not to work: - # - # - Debian 6.0.10 "squeeze" (x64) - - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='-qq' - fi - - apt-get $QUIET_FLAG update || error apt-get update hit problems but continuing anyway... - - # virtualenv binary can be found in different packages depending on - # distro version (#346) - - virtualenv= - # virtual env is known to apt and is installable - if apt-cache show virtualenv > /dev/null 2>&1 ; then - if ! LC_ALL=C apt-cache --quiet=0 show virtualenv 2>&1 | grep -q 'No packages found'; then - virtualenv="virtualenv" - fi - fi - - if apt-cache show python-virtualenv > /dev/null 2>&1; then - virtualenv="$virtualenv python-virtualenv" - fi - - augeas_pkg="libaugeas0 augeas-lenses" - AUGVERSION=`LC_ALL=C apt-cache show --no-all-versions libaugeas0 | grep ^Version: | cut -d" " -f2` - - if [ "$ASSUME_YES" = 1 ]; then - YES_FLAG="-y" - fi - - AddBackportRepo() { - # ARGS: - BACKPORT_NAME="$1" - BACKPORT_SOURCELINE="$2" - say "To use the Apache Certbot plugin, augeas needs to be installed from $BACKPORT_NAME." - if ! grep -v -e ' *#' /etc/apt/sources.list | grep -q "$BACKPORT_NAME" ; then - # This can theoretically error if sources.list.d is empty, but in that case we don't care. - if ! grep -v -e ' *#' /etc/apt/sources.list.d/* 2>/dev/null | grep -q "$BACKPORT_NAME"; then - if [ "$ASSUME_YES" = 1 ]; then - /bin/echo -n "Installing augeas from $BACKPORT_NAME in 3 seconds..." - sleep 1s - /bin/echo -ne "\e[0K\rInstalling augeas from $BACKPORT_NAME in 2 seconds..." - sleep 1s - /bin/echo -e "\e[0K\rInstalling augeas from $BACKPORT_NAME in 1 second ..." - sleep 1s - add_backports=1 - else - read -p "Would you like to enable the $BACKPORT_NAME repository [Y/n]? " response - case $response in - [yY][eE][sS]|[yY]|"") - add_backports=1;; - *) - add_backports=0;; - esac - fi - if [ "$add_backports" = 1 ]; then - sh -c "echo $BACKPORT_SOURCELINE >> /etc/apt/sources.list.d/$BACKPORT_NAME.list" - apt-get $QUIET_FLAG update - fi - fi - fi - if [ "$add_backports" != 0 ]; then - apt-get install $QUIET_FLAG $YES_FLAG --no-install-recommends -t "$BACKPORT_NAME" $augeas_pkg - augeas_pkg= - fi - } - - - if dpkg --compare-versions 1.0 gt "$AUGVERSION" ; then - if lsb_release -a | grep -q wheezy ; then - AddBackportRepo wheezy-backports "deb http://http.debian.net/debian wheezy-backports main" - elif lsb_release -a | grep -q precise ; then - # XXX add ARM case - AddBackportRepo precise-backports "deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse" - else - echo "No libaugeas0 version is available that's new enough to run the" - echo "Certbot apache plugin..." - fi - # XXX add a case for ubuntu PPAs - fi - - apt-get install $QUIET_FLAG $YES_FLAG --no-install-recommends \ - python \ - python-dev \ - $virtualenv \ - gcc \ - $augeas_pkg \ - libssl-dev \ - openssl \ - libffi-dev \ - ca-certificates \ - - - if ! $EXISTS virtualenv > /dev/null ; then - error Failed to install a working \"virtualenv\" command, exiting - exit 1 - fi -} - -# If new packages are installed by BootstrapRpmCommonBase below, version -# numbers in rpm_common.sh and rpm_python3.sh must be increased. - -# Sets TOOL to the name of the package manager -# Sets appropriate values for YES_FLAG and QUIET_FLAG based on $ASSUME_YES and $QUIET_FLAG. -# Enables EPEL if applicable and possible. -InitializeRPMCommonBase() { - if type dnf 2>/dev/null - then - TOOL=dnf - elif type yum 2>/dev/null - then - TOOL=yum - - else - error "Neither yum nor dnf found. Aborting bootstrap!" - exit 1 - fi - - if [ "$ASSUME_YES" = 1 ]; then - YES_FLAG="-y" - fi - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='--quiet' - fi - - if ! $TOOL list *virtualenv >/dev/null 2>&1; then - echo "To use Certbot, packages from the EPEL repository need to be installed." - if ! $TOOL list epel-release >/dev/null 2>&1; then - error "Enable the EPEL repository and try running Certbot again." - exit 1 - fi - if [ "$ASSUME_YES" = 1 ]; then - /bin/echo -n "Enabling the EPEL repository in 3 seconds..." - sleep 1s - /bin/echo -ne "\e[0K\rEnabling the EPEL repository in 2 seconds..." - sleep 1s - /bin/echo -e "\e[0K\rEnabling the EPEL repository in 1 second..." - sleep 1s - fi - if ! $TOOL install $YES_FLAG $QUIET_FLAG epel-release; then - error "Could not enable EPEL. Aborting bootstrap!" - exit 1 - fi - fi -} - -BootstrapRpmCommonBase() { - # Arguments: whitespace-delimited python packages to install - - InitializeRPMCommonBase # This call is superfluous in practice - - pkgs=" - gcc - augeas-libs - openssl - openssl-devel - libffi-devel - redhat-rpm-config - ca-certificates - " - - # Add the python packages - pkgs="$pkgs - $1 - " - - if $TOOL list installed "httpd" >/dev/null 2>&1; then - pkgs="$pkgs - mod_ssl - " - fi - - if ! $TOOL install $YES_FLAG $QUIET_FLAG $pkgs; then - error "Could not install OS dependencies. Aborting bootstrap!" - exit 1 - fi -} - -# If new packages are installed by BootstrapRpmCommon below, this version -# number must be increased. -BOOTSTRAP_RPM_COMMON_VERSION=1 - -BootstrapRpmCommon() { - # Tested with: - # - Fedora 20, 21, 22, 23 (x64) - # - Centos 7 (x64: on DigitalOcean droplet) - # - CentOS 7 Minimal install in a Hyper-V VM - # - CentOS 6 - - InitializeRPMCommonBase - - # Most RPM distros use the "python" or "python-" naming convention. Let's try that first. - if $TOOL list python >/dev/null 2>&1; then - python_pkgs="$python - python-devel - python-virtualenv - python-tools - python-pip - " - # Fedora 26 starts to use the prefix python2 for python2 based packages. - # this elseif is theoretically for any Fedora over version 26: - elif $TOOL list python2 >/dev/null 2>&1; then - python_pkgs="$python2 - python2-libs - python2-setuptools - python2-devel - python2-virtualenv - python2-tools - python2-pip - " - # Some distros and older versions of current distros use a "python27" - # instead of the "python" or "python-" naming convention. - else - python_pkgs="$python27 - python27-devel - python27-virtualenv - python27-tools - python27-pip - " - fi - - BootstrapRpmCommonBase "$python_pkgs" -} - -# If new packages are installed by BootstrapRpmPython3 below, this version -# number must be increased. -BOOTSTRAP_RPM_PYTHON3_VERSION=1 - -BootstrapRpmPython3() { - # Tested with: - # - CentOS 6 - - InitializeRPMCommonBase - - # EPEL uses python34 - if $TOOL list python34 >/dev/null 2>&1; then - python_pkgs="python34 - python34-devel - python34-tools - " - else - error "No supported Python package available to install. Aborting bootstrap!" - exit 1 - fi - - BootstrapRpmCommonBase "$python_pkgs" -} - -# If new packages are installed by BootstrapSuseCommon below, this version -# number must be increased. -BOOTSTRAP_SUSE_COMMON_VERSION=1 - -BootstrapSuseCommon() { - # SLE12 don't have python-virtualenv - - if [ "$ASSUME_YES" = 1 ]; then - zypper_flags="-nq" - install_flags="-l" - fi - - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='-qq' - fi - - zypper $QUIET_FLAG $zypper_flags in $install_flags \ - python \ - python-devel \ - python-virtualenv \ - gcc \ - augeas-lenses \ - libopenssl-devel \ - libffi-devel \ - ca-certificates -} - -# If new packages are installed by BootstrapArchCommon below, this version -# number must be increased. -BOOTSTRAP_ARCH_COMMON_VERSION=1 - -BootstrapArchCommon() { - # Tested with: - # - ArchLinux (x86_64) - # - # "python-virtualenv" is Python3, but "python2-virtualenv" provides - # only "virtualenv2" binary, not "virtualenv". - - deps=" - python2 - python-virtualenv - gcc - augeas - openssl - libffi - ca-certificates - pkg-config - " - - # pacman -T exits with 127 if there are missing dependencies - missing=$(pacman -T $deps) || true - - if [ "$ASSUME_YES" = 1 ]; then - noconfirm="--noconfirm" - fi - - if [ "$missing" ]; then - if [ "$QUIET" = 1 ]; then - pacman -S --needed $missing $noconfirm > /dev/null - else - pacman -S --needed $missing $noconfirm - fi - fi -} - -# If new packages are installed by BootstrapGentooCommon below, this version -# number must be increased. -BOOTSTRAP_GENTOO_COMMON_VERSION=1 - -BootstrapGentooCommon() { - PACKAGES=" - dev-lang/python:2.7 - dev-python/virtualenv - app-admin/augeas - dev-libs/openssl - dev-libs/libffi - app-misc/ca-certificates - virtual/pkgconfig" - - ASK_OPTION="--ask" - if [ "$ASSUME_YES" = 1 ]; then - ASK_OPTION="" - fi - - case "$PACKAGE_MANAGER" in - (paludis) - cave resolve --preserve-world --keep-targets if-possible $PACKAGES -x - ;; - (pkgcore) - pmerge --noreplace --oneshot $ASK_OPTION $PACKAGES - ;; - (portage|*) - emerge --noreplace --oneshot $ASK_OPTION $PACKAGES - ;; - esac -} - -# If new packages are installed by BootstrapFreeBsd below, this version number -# must be increased. -BOOTSTRAP_FREEBSD_VERSION=1 - -BootstrapFreeBsd() { - if [ "$QUIET" = 1 ]; then - QUIET_FLAG="--quiet" - fi - - pkg install -Ay $QUIET_FLAG \ - python \ - py27-virtualenv \ - augeas \ - libffi -} - -# If new packages are installed by BootstrapMac below, this version number must -# be increased. -BOOTSTRAP_MAC_VERSION=1 - -BootstrapMac() { - if hash brew 2>/dev/null; then - say "Using Homebrew to install dependencies..." - pkgman=brew - pkgcmd="brew install" - elif hash port 2>/dev/null; then - say "Using MacPorts to install dependencies..." - pkgman=port - pkgcmd="port install" - else - say "No Homebrew/MacPorts; installing Homebrew..." - ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - pkgman=brew - pkgcmd="brew install" - fi - - $pkgcmd augeas - if [ "$(which python)" = "/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python" \ - -o "$(which python)" = "/usr/bin/python" ]; then - # We want to avoid using the system Python because it requires root to use pip. - # python.org, MacPorts or HomeBrew Python installations should all be OK. - say "Installing python..." - $pkgcmd python - fi - - # Workaround for _dlopen not finding augeas on macOS - if [ "$pkgman" = "port" ] && ! [ -e "/usr/local/lib/libaugeas.dylib" ] && [ -e "/opt/local/lib/libaugeas.dylib" ]; then - say "Applying augeas workaround" - mkdir -p /usr/local/lib/ - ln -s /opt/local/lib/libaugeas.dylib /usr/local/lib/ - fi - - if ! hash pip 2>/dev/null; then - say "pip not installed" - say "Installing pip..." - curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | python - fi - - if ! hash virtualenv 2>/dev/null; then - say "virtualenv not installed." - say "Installing with pip..." - pip install virtualenv - fi -} - -# If new packages are installed by BootstrapSmartOS below, this version number -# must be increased. -BOOTSTRAP_SMARTOS_VERSION=1 - -BootstrapSmartOS() { - pkgin update - pkgin -y install 'gcc49' 'py27-augeas' 'py27-virtualenv' -} - -# If new packages are installed by BootstrapMageiaCommon below, this version -# number must be increased. -BOOTSTRAP_MAGEIA_COMMON_VERSION=1 - -BootstrapMageiaCommon() { - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='--quiet' - fi - - if ! urpmi --force $QUIET_FLAG \ - python \ - libpython-devel \ - python-virtualenv - then - error "Could not install Python dependencies. Aborting bootstrap!" - exit 1 - fi - - if ! urpmi --force $QUIET_FLAG \ - git \ - gcc \ - python-augeas \ - libopenssl-devel \ - libffi-devel \ - rootcerts - then - error "Could not install additional dependencies. Aborting bootstrap!" - exit 1 - fi -} - - -# Set Bootstrap to the function that installs OS dependencies on this system -# and BOOTSTRAP_VERSION to the unique identifier for the current version of -# that function. If Bootstrap is set to a function that doesn't install any -# packages BOOTSTRAP_VERSION is not set. -if [ -f /etc/debian_version ]; then - Bootstrap() { - BootstrapMessage "Debian-based OSes" - BootstrapDebCommon - } - BOOTSTRAP_VERSION="BootstrapDebCommon $BOOTSTRAP_DEB_COMMON_VERSION" -elif [ -f /etc/mageia-release ]; then - # Mageia has both /etc/mageia-release and /etc/redhat-release - Bootstrap() { - ExperimentalBootstrap "Mageia" BootstrapMageiaCommon - } - BOOTSTRAP_VERSION="BootstrapMageiaCommon $BOOTSTRAP_MAGEIA_COMMON_VERSION" -elif [ -f /etc/redhat-release ]; then - # Run DeterminePythonVersion to decide on the basis of available Python versions - # whether to use 2.x or 3.x on RedHat-like systems. - # Then, revert LE_PYTHON to its previous state. - prev_le_python="$LE_PYTHON" - unset LE_PYTHON - DeterminePythonVersion "NOCRASH" - if [ "$PYVER" -eq 26 ]; then - Bootstrap() { - BootstrapMessage "RedHat-based OSes that will use Python3" - BootstrapRpmPython3 - } - USE_PYTHON_3=1 - BOOTSTRAP_VERSION="BootstrapRpmPython3 $BOOTSTRAP_RPM_PYTHON3_VERSION" - else - Bootstrap() { - BootstrapMessage "RedHat-based OSes" - BootstrapRpmCommon - } - BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION" - fi - LE_PYTHON="$prev_le_python" -elif [ -f /etc/os-release ] && `grep -q openSUSE /etc/os-release` ; then - Bootstrap() { - BootstrapMessage "openSUSE-based OSes" - BootstrapSuseCommon - } - BOOTSTRAP_VERSION="BootstrapSuseCommon $BOOTSTRAP_SUSE_COMMON_VERSION" -elif [ -f /etc/arch-release ]; then - Bootstrap() { - if [ "$DEBUG" = 1 ]; then - BootstrapMessage "Archlinux" - BootstrapArchCommon - else - error "Please use pacman to install letsencrypt packages:" - error "# pacman -S certbot certbot-apache" - error - error "If you would like to use the virtualenv way, please run the script again with the" - error "--debug flag." - exit 1 - fi - } - BOOTSTRAP_VERSION="BootstrapArchCommon $BOOTSTRAP_ARCH_COMMON_VERSION" -elif [ -f /etc/manjaro-release ]; then - Bootstrap() { - ExperimentalBootstrap "Manjaro Linux" BootstrapArchCommon - } - BOOTSTRAP_VERSION="BootstrapArchCommon $BOOTSTRAP_ARCH_COMMON_VERSION" -elif [ -f /etc/gentoo-release ]; then - Bootstrap() { - DeprecationBootstrap "Gentoo" BootstrapGentooCommon - } - BOOTSTRAP_VERSION="BootstrapGentooCommon $BOOTSTRAP_GENTOO_COMMON_VERSION" -elif uname | grep -iq FreeBSD ; then - Bootstrap() { - DeprecationBootstrap "FreeBSD" BootstrapFreeBsd - } - BOOTSTRAP_VERSION="BootstrapFreeBsd $BOOTSTRAP_FREEBSD_VERSION" -elif uname | grep -iq Darwin ; then - Bootstrap() { - DeprecationBootstrap "macOS" BootstrapMac - } - BOOTSTRAP_VERSION="BootstrapMac $BOOTSTRAP_MAC_VERSION" -elif [ -f /etc/issue ] && grep -iq "Amazon Linux" /etc/issue ; then - Bootstrap() { - ExperimentalBootstrap "Amazon Linux" BootstrapRpmCommon - } - BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION" -elif [ -f /etc/product ] && grep -q "Joyent Instance" /etc/product ; then - Bootstrap() { - ExperimentalBootstrap "Joyent SmartOS Zone" BootstrapSmartOS - } - BOOTSTRAP_VERSION="BootstrapSmartOS $BOOTSTRAP_SMARTOS_VERSION" -else - Bootstrap() { - error "Sorry, I don't know how to bootstrap Certbot on your operating system!" - error - error "You will need to install OS dependencies, configure virtualenv, and run pip install manually." - error "Please see https://letsencrypt.readthedocs.org/en/latest/contributing.html#prerequisites" - error "for more info." - exit 1 - } -fi - -# We handle this case after determining the normal bootstrap version to allow -# variables like USE_PYTHON_3 to be properly set. As described above, if the -# Bootstrap function doesn't install any packages, BOOTSTRAP_VERSION should not -# be set so we unset it here. -if [ "$NO_BOOTSTRAP" = 1 ]; then - Bootstrap() { - : - } - unset BOOTSTRAP_VERSION -fi - -# Sets PREV_BOOTSTRAP_VERSION to the identifier for the bootstrap script used -# to install OS dependencies on this system. PREV_BOOTSTRAP_VERSION isn't set -# if it is unknown how OS dependencies were installed on this system. -SetPrevBootstrapVersion() { - if [ -f $BOOTSTRAP_VERSION_PATH ]; then - PREV_BOOTSTRAP_VERSION=$(cat "$BOOTSTRAP_VERSION_PATH") - # The list below only contains bootstrap version strings that existed before - # we started writing them to disk. - # - # DO NOT MODIFY THIS LIST UNLESS YOU KNOW WHAT YOU'RE DOING! - elif grep -Fqx "$BOOTSTRAP_VERSION" << "UNLIKELY_EOF" -BootstrapDebCommon 1 -BootstrapMageiaCommon 1 -BootstrapRpmCommon 1 -BootstrapSuseCommon 1 -BootstrapArchCommon 1 -BootstrapGentooCommon 1 -BootstrapFreeBsd 1 -BootstrapMac 1 -BootstrapSmartOS 1 -UNLIKELY_EOF - then - # If there's no bootstrap version saved to disk, but the currently selected - # bootstrap script is from before we started saving the version number, - # return the currently selected version to prevent us from rebootstrapping - # unnecessarily. - PREV_BOOTSTRAP_VERSION="$BOOTSTRAP_VERSION" - fi -} - -TempDir() { - mktemp -d 2>/dev/null || mktemp -d -t 'le' # Linux || macOS -} - -# Returns 0 if a letsencrypt installation exists at $OLD_VENV_PATH, otherwise, -# returns a non-zero number. -OldVenvExists() { - [ -n "$OLD_VENV_PATH" -a -f "$OLD_VENV_PATH/bin/letsencrypt" ] -} - -# Given python path, version 1 and version 2, check if version 1 is outdated compared to version 2. -# An unofficial version provided as version 1 (eg. 0.28.0.dev0) will be treated -# specifically by printing "UNOFFICIAL". Otherwise, print "OUTDATED" if version 1 -# is outdated, and "UP_TO_DATE" if not. -# This function relies only on installed python environment (2.x or 3.x) by certbot-auto. -CompareVersions() { - "$1" - "$2" "$3" << "UNLIKELY_EOF" -import sys -from distutils.version import StrictVersion - -try: - current = StrictVersion(sys.argv[1]) -except ValueError: - sys.stdout.write('UNOFFICIAL') - sys.exit() - -try: - remote = StrictVersion(sys.argv[2]) -except ValueError: - sys.stdout.write('UP_TO_DATE') - sys.exit() - -if current < remote: - sys.stdout.write('OUTDATED') -else: - sys.stdout.write('UP_TO_DATE') -UNLIKELY_EOF -} - -if [ "$1" = "--le-auto-phase2" ]; then - # Phase 2: Create venv, install LE, and run. - - shift 1 # the --le-auto-phase2 arg - SetPrevBootstrapVersion - - if [ -z "$PHASE_1_VERSION" -a "$USE_PYTHON_3" = 1 ]; then - unset LE_PYTHON - fi - - INSTALLED_VERSION="none" - if [ -d "$VENV_PATH" ] || OldVenvExists; then - # If the selected Bootstrap function isn't a noop and it differs from the - # previously used version - if [ -n "$BOOTSTRAP_VERSION" -a "$BOOTSTRAP_VERSION" != "$PREV_BOOTSTRAP_VERSION" ]; then - # if non-interactive mode or stdin and stdout are connected to a terminal - if [ \( "$NONINTERACTIVE" = 1 \) -o \( \( -t 0 \) -a \( -t 1 \) \) ]; then - if [ -d "$VENV_PATH" ]; then - rm -rf "$VENV_PATH" - fi - # In the case the old venv was just a symlink to the new one, - # OldVenvExists is now false because we deleted the venv at VENV_PATH. - if OldVenvExists; then - rm -rf "$OLD_VENV_PATH" - ln -s "$VENV_PATH" "$OLD_VENV_PATH" - fi - RerunWithArgs "$@" - else - error "Skipping upgrade because new OS dependencies may need to be installed." - error - error "To upgrade to a newer version, please run this script again manually so you can" - error "approve changes or with --non-interactive on the command line to automatically" - error "install any required packages." - # Set INSTALLED_VERSION to be the same so we don't update the venv - INSTALLED_VERSION="$LE_AUTO_VERSION" - # Continue to use OLD_VENV_PATH if the new venv doesn't exist - if [ ! -d "$VENV_PATH" ]; then - VENV_BIN="$OLD_VENV_PATH/bin" - fi - fi - elif [ -f "$VENV_BIN/letsencrypt" ]; then - # --version output ran through grep due to python-cryptography DeprecationWarnings - # grep for both certbot and letsencrypt until certbot and shim packages have been released - INSTALLED_VERSION=$("$VENV_BIN/letsencrypt" --version 2>&1 | grep "^certbot\|^letsencrypt" | cut -d " " -f 2) - if [ -z "$INSTALLED_VERSION" ]; then - error "Error: couldn't get currently installed version for $VENV_BIN/letsencrypt: " 1>&2 - "$VENV_BIN/letsencrypt" --version - exit 1 - fi - fi - fi - - if [ "$LE_AUTO_VERSION" != "$INSTALLED_VERSION" ]; then - say "Creating virtual environment..." - DeterminePythonVersion - rm -rf "$VENV_PATH" - if [ "$PYVER" -le 27 ]; then - if [ "$VERBOSE" = 1 ]; then - virtualenv --no-site-packages --python "$LE_PYTHON" "$VENV_PATH" - else - virtualenv --no-site-packages --python "$LE_PYTHON" "$VENV_PATH" > /dev/null - fi - else - if [ "$VERBOSE" = 1 ]; then - "$LE_PYTHON" -m venv "$VENV_PATH" - else - "$LE_PYTHON" -m venv "$VENV_PATH" > /dev/null - fi - fi - - if [ -n "$BOOTSTRAP_VERSION" ]; then - echo "$BOOTSTRAP_VERSION" > "$BOOTSTRAP_VERSION_PATH" - elif [ -n "$PREV_BOOTSTRAP_VERSION" ]; then - echo "$PREV_BOOTSTRAP_VERSION" > "$BOOTSTRAP_VERSION_PATH" - fi - - say "Installing Python packages..." - TEMP_DIR=$(TempDir) - trap 'rm -rf "$TEMP_DIR"' EXIT - # There is no $ interpolation due to quotes on starting heredoc delimiter. - # ------------------------------------------------------------------------- - cat << "UNLIKELY_EOF" > "$TEMP_DIR/letsencrypt-auto-requirements.txt" -# This is the flattened list of packages certbot-auto installs. To generate -# this, do -# `pip install --no-cache-dir -e acme -e . -e certbot-apache -e certbot-nginx`, -# and then use `hashin` or a more secure method to gather the hashes. - -# Hashin example: -# pip install hashin -# hashin -r dependency-requirements.txt cryptography==1.5.2 -# sets the new certbot-auto pinned version of cryptography to 1.5.2 - -argparse==1.4.0 \ - --hash=sha256:c31647edb69fd3d465a847ea3157d37bed1f95f19760b11a47aa91c04b666314 \ - --hash=sha256:62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4 - -# This comes before cffi because cffi will otherwise install an unchecked -# version via setup_requires. -pycparser==2.14 \ - --hash=sha256:7959b4a74abdc27b312fed1c21e6caf9309ce0b29ea86b591fd2e99ecdf27f73 \ - --no-binary pycparser - -asn1crypto==0.22.0 \ - --hash=sha256:d232509fefcfcdb9a331f37e9c9dc20441019ad927c7d2176cf18ed5da0ba097 \ - --hash=sha256:cbbadd640d3165ab24b06ef25d1dca09a3441611ac15f6a6b452474fdf0aed1a -cffi==1.11.5 \ - --hash=sha256:1b0493c091a1898f1136e3f4f991a784437fac3673780ff9de3bcf46c80b6b50 \ - --hash=sha256:87f37fe5130574ff76c17cab61e7d2538a16f843bb7bca8ebbc4b12de3078596 \ - --hash=sha256:1553d1e99f035ace1c0544050622b7bc963374a00c467edafac50ad7bd276aef \ - --hash=sha256:151b7eefd035c56b2b2e1eb9963c90c6302dc15fbd8c1c0a83a163ff2c7d7743 \ - --hash=sha256:edabd457cd23a02965166026fd9bfd196f4324fe6032e866d0f3bd0301cd486f \ - --hash=sha256:ba5e697569f84b13640c9e193170e89c13c6244c24400fc57e88724ef610cd31 \ - --hash=sha256:79f9b6f7c46ae1f8ded75f68cf8ad50e5729ed4d590c74840471fc2823457d04 \ - --hash=sha256:b0f7d4a3df8f06cf49f9f121bead236e328074de6449866515cea4907bbc63d6 \ - --hash=sha256:4c91af6e967c2015729d3e69c2e51d92f9898c330d6a851bf8f121236f3defd3 \ - --hash=sha256:7a33145e04d44ce95bcd71e522b478d282ad0eafaf34fe1ec5bbd73e662f22b6 \ - --hash=sha256:95d5251e4b5ca00061f9d9f3d6fe537247e145a8524ae9fd30a2f8fbce993b5b \ - --hash=sha256:b75110fb114fa366b29a027d0c9be3709579602ae111ff61674d28c93606acca \ - --hash=sha256:ae5e35a2c189d397b91034642cb0eab0e346f776ec2eb44a49a459e6615d6e2e \ - --hash=sha256:fdf1c1dc5bafc32bc5d08b054f94d659422b05aba244d6be4ddc1c72d9aa70fb \ - --hash=sha256:9d1d3e63a4afdc29bd76ce6aa9d58c771cd1599fbba8cf5057e7860b203710dd \ - --hash=sha256:be2a9b390f77fd7676d80bc3cdc4f8edb940d8c198ed2d8c0be1319018c778e1 \ - --hash=sha256:ed01918d545a38998bfa5902c7c00e0fee90e957ce036a4000a88e3fe2264917 \ - --hash=sha256:857959354ae3a6fa3da6651b966d13b0a8bed6bbc87a0de7b38a549db1d2a359 \ - --hash=sha256:2ba8a45822b7aee805ab49abfe7eec16b90587f7f26df20c71dd89e45a97076f \ - --hash=sha256:a36c5c154f9d42ec176e6e620cb0dd275744aa1d804786a71ac37dc3661a5e95 \ - --hash=sha256:e55e22ac0a30023426564b1059b035973ec82186ddddbac867078435801c7801 \ - --hash=sha256:3eb6434197633b7748cea30bf0ba9f66727cdce45117a712b29a443943733257 \ - --hash=sha256:ecbb7b01409e9b782df5ded849c178a0aa7c906cf8c5a67368047daab282b184 \ - --hash=sha256:770f3782b31f50b68627e22f91cb182c48c47c02eb405fd689472aa7b7aa16dc \ - --hash=sha256:d5d8555d9bfc3f02385c1c37e9f998e2011f0db4f90e250e5bc0c0a85a813085 \ - --hash=sha256:3c85641778460581c42924384f5e68076d724ceac0f267d66c757f7535069c93 \ - --hash=sha256:ca1bd81f40adc59011f58159e4aa6445fc585a32bb8ac9badf7a2c1aa23822f2 \ - --hash=sha256:3bb6bd7266598f318063e584378b8e27c67de998a43362e8fce664c54ee52d30 \ - --hash=sha256:a6a5cb8809091ec9ac03edde9304b3ad82ad4466333432b16d78ef40e0cce0d5 \ - --hash=sha256:57b2533356cb2d8fac1555815929f7f5f14d68ac77b085d2326b571310f34f6e \ - --hash=sha256:495c5c2d43bf6cebe0178eb3e88f9c4aa48d8934aa6e3cddb865c058da76756b \ - --hash=sha256:e90f17980e6ab0f3c2f3730e56d1fe9bcba1891eeea58966e89d352492cc74f4 -ConfigArgParse==0.12.0 \ - --hash=sha256:28cd7d67669651f2a4518367838c49539457504584a139709b2b8f6c208ef339 \ - --no-binary ConfigArgParse -configobj==5.0.6 \ - --hash=sha256:a2f5650770e1c87fb335af19a9b7eb73fc05ccf22144eb68db7d00cd2bcb0902 \ - --no-binary configobj -cryptography==2.2.2 \ - --hash=sha256:3f3b65d5a16e6b52fba63dc860b62ca9832f51f1a2ae5083c78b6840275f12dd \ - --hash=sha256:5251e7de0de66810833606439ca65c9b9e45da62196b0c88bfadf27740aac09f \ - --hash=sha256:551a3abfe0c8c6833df4192a63371aa2ff43afd8f570ed345d31f251d78e7e04 \ - --hash=sha256:5cb990056b7cadcca26813311187ad751ea644712022a3976443691168781b6f \ - --hash=sha256:60bda7f12ecb828358be53095fc9c6edda7de8f1ef571f96c00b2363643fa3cd \ - --hash=sha256:64b5c67acc9a7c83fbb4b69166f3105a0ab722d27934fac2cb26456718eec2ba \ - --hash=sha256:6fef51ec447fe9f8351894024e94736862900d3a9aa2961528e602eb65c92bdb \ - --hash=sha256:77d0ad229d47a6e0272d00f6bf8ac06ce14715a9fd02c9a97f5a2869aab3ccb2 \ - --hash=sha256:808fe471b1a6b777f026f7dc7bd9a4959da4bfab64972f2bbe91e22527c1c037 \ - --hash=sha256:9b62fb4d18529c84b961efd9187fecbb48e89aa1a0f9f4161c61b7fc42a101bd \ - --hash=sha256:9e5bed45ec6b4f828866ac6a6bedf08388ffcfa68abe9e94b34bb40977aba531 \ - --hash=sha256:9fc295bf69130a342e7a19a39d7bbeb15c0bcaabc7382ec33ef3b2b7d18d2f63 \ - --hash=sha256:abd070b5849ed64e6d349199bef955ee0ad99aefbad792f0c587f8effa681a5e \ - --hash=sha256:ba6a774749b6e510cffc2fb98535f717e0e5fd91c7c99a61d223293df79ab351 \ - --hash=sha256:c332118647f084c983c6a3e1dba0f3bcb051f69d12baccac68db8d62d177eb8a \ - --hash=sha256:d6f46e862ee36df81e6342c2177ba84e70f722d9dc9c6c394f9f1f434c4a5563 \ - --hash=sha256:db6013746f73bf8edd9c3d1d3f94db635b9422f503db3fc5ef105233d4c011ab \ - --hash=sha256:f57008eaff597c69cf692c3518f6d4800f0309253bb138b526a37fe9ef0c7471 \ - --hash=sha256:f6c821ac253c19f2ad4c8691633ae1d1a17f120d5b01ea1d256d7b602bc59887 -enum34==1.1.2 ; python_version < '3.4' \ - --hash=sha256:2475d7fcddf5951e92ff546972758802de5260bf409319a9f1934e6bbc8b1dc7 \ - --hash=sha256:35907defb0f992b75ab7788f65fedc1cf20ffa22688e0e6f6f12afc06b3ea501 -funcsigs==1.0.2 \ - --hash=sha256:330cc27ccbf7f1e992e69fef78261dc7c6569012cf397db8d3de0234e6c937ca \ - --hash=sha256:a7bb0f2cf3a3fd1ab2732cb49eba4252c2af4240442415b4abce3b87022a8f50 -idna==2.5 \ - --hash=sha256:cc19709fd6d0cbfed39ea875d29ba6d4e22c0cebc510a76d6302a28385e8bb70 \ - --hash=sha256:3cb5ce08046c4e3a560fc02f138d0ac63e00f8ce5901a56b32ec8b7994082aab -ipaddress==1.0.16 \ - --hash=sha256:935712800ce4760701d89ad677666cd52691fd2f6f0b340c8b4239a3c17988a5 \ - --hash=sha256:5a3182b322a706525c46282ca6f064d27a02cffbd449f9f47416f1dc96aa71b0 -josepy==1.1.0 \ - --hash=sha256:1309a25aac3caeff5239729c58ff9b583f7d022ffdb1553406ddfc8e5b52b76e \ - --hash=sha256:fb5c62c77d26e04df29cb5ecd01b9ce69b6fcc9e521eb1ca193b7faa2afa7086 -linecache2==1.0.0 \ - --hash=sha256:e78be9c0a0dfcbac712fe04fbf92b96cddae80b1b842f24248214c8496f006ef \ - --hash=sha256:4b26ff4e7110db76eeb6f5a7b64a82623839d595c2038eeda662f2a2db78e97c -# Using an older version of mock here prevents regressions of #5276. -mock==1.3.0 \ - --hash=sha256:3f573a18be94de886d1191f27c168427ef693e8dcfcecf95b170577b2eb69cbb \ - --hash=sha256:1e247dbecc6ce057299eb7ee019ad68314bb93152e81d9a6110d35f4d5eca0f6 -ordereddict==1.1 \ - --hash=sha256:1c35b4ac206cef2d24816c89f89cf289dd3d38cf7c449bb3fab7bf6d43f01b1f \ - --no-binary ordereddict -packaging==16.8 \ - --hash=sha256:99276dc6e3a7851f32027a68f1095cd3f77c148091b092ea867a351811cfe388 \ - --hash=sha256:5d50835fdf0a7edf0b55e311b7c887786504efea1177abd7e69329a8e5ea619e -parsedatetime==2.1 \ - --hash=sha256:ce9d422165cf6e963905cd5f74f274ebf7cc98c941916169178ef93f0e557838 \ - --hash=sha256:17c578775520c99131634e09cfca5a05ea9e1bd2a05cd06967ebece10df7af2d -pbr==1.8.1 \ - --hash=sha256:46c8db75ae75a056bd1cc07fa21734fe2e603d11a07833ecc1eeb74c35c72e0c \ - --hash=sha256:e2127626a91e6c885db89668976db31020f0af2da728924b56480fc7ccf09649 -pyOpenSSL==16.2.0 \ - --hash=sha256:26ca380ddf272f7556e48064bbcd5bd71f83dfc144f3583501c7ddbd9434ee17 \ - --hash=sha256:7779a3bbb74e79db234af6a08775568c6769b5821faecf6e2f4143edb227516e -pyparsing==2.1.8 \ - --hash=sha256:2f0f5ceb14eccd5aef809d6382e87df22ca1da583c79f6db01675ce7d7f49c18 \ - --hash=sha256:03a4869b9f3493807ee1f1cb405e6d576a1a2ca4d81a982677c0c1ad6177c56b \ - --hash=sha256:ab09aee814c0241ff0c503cff30018219fe1fc14501d89f406f4664a0ec9fbcd \ - --hash=sha256:6e9a7f052f8e26bcf749e4033e3115b6dc7e3c85aafcb794b9a88c9d9ef13c97 \ - --hash=sha256:9f463a6bcc4eeb6c08f1ed84439b17818e2085937c0dee0d7674ac127c67c12b \ - --hash=sha256:3626b4d81cfb300dad57f52f2f791caaf7b06c09b368c0aa7b868e53a5775424 \ - --hash=sha256:367b90cc877b46af56d4580cd0ae278062903f02b8204ab631f5a2c0f50adfd0 \ - --hash=sha256:9f1ea360086cd68681e7f4ca8f1f38df47bf81942a0d76a9673c2d23eff35b13 -pyRFC3339==1.0 \ - --hash=sha256:eea31835c56e2096af4363a5745a784878a61d043e247d3a6d6a0a32a9741f56 \ - --hash=sha256:8dfbc6c458b8daba1c0f3620a8c78008b323a268b27b7359e92a4ae41325f535 -python-augeas==0.5.0 \ - --hash=sha256:67d59d66cdba8d624e0389b87b2a83a176f21f16a87553b50f5703b23f29bac2 \ - --no-binary python-augeas -pytz==2015.7 \ - --hash=sha256:3abe6a6d3fc2fbbe4c60144211f45da2edbe3182a6f6511af6bbba0598b1f992 \ - --hash=sha256:939ef9c1e1224d980405689a97ffcf7828c56d1517b31d73464356c1f2b7769e \ - --hash=sha256:ead4aefa7007249e05e51b01095719d5a8dd95760089f5730aac5698b1932918 \ - --hash=sha256:3cca0df08bd0ed98432390494ce3ded003f5e661aa460be7a734bffe35983605 \ - --hash=sha256:3ede470d3d17ba3c07638dfa0d10452bc1b6e5ad326127a65ba77e6aaeb11bec \ - --hash=sha256:68c47964f7186eec306b13629627722b9079cd4447ed9e5ecaecd4eac84ca734 \ - --hash=sha256:dd5d3991950aae40a6c81de1578942e73d629808cefc51d12cd157980e6cfc18 \ - --hash=sha256:a77c52062c07eb7c7b30545dbc73e32995b7e117eea750317b5cb5c7a4618f14 \ - --hash=sha256:81af9aec4bc960a9a0127c488f18772dae4634689233f06f65443e7b11ebeb51 \ - --hash=sha256:e079b1dadc5c06246cc1bb6fe1b23a50b1d1173f2edd5104efd40bb73a28f406 \ - --hash=sha256:fbd26746772c24cb93c8b97cbdad5cb9e46c86bbdb1b9d8a743ee00e2fb1fc5d \ - --hash=sha256:99266ef30a37e43932deec2b7ca73e83c8dbc3b9ff703ec73eca6b1dae6befea \ - --hash=sha256:8b6ce1c993909783bc96e0b4f34ea223bff7a4df2c90bdb9c4e0f1ac928689e3 -requests==2.20.0 \ - --hash=sha256:99dcfdaaeb17caf6e526f32b6a7b780461512ab3f1d992187801694cba42770c \ - --hash=sha256:a84b8c9ab6239b578f22d1c21d51b696dcfe004032bb80ea832398d6909d7279 -six==1.10.0 \ - --hash=sha256:0ff78c403d9bccf5a425a6d31a12aa6b47f1c21ca4dc2573a7e2f32a97335eb1 \ - --hash=sha256:105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a -traceback2==1.4.0 \ - --hash=sha256:8253cebec4b19094d67cc5ed5af99bf1dba1285292226e98a31929f87a5d6b23 \ - --hash=sha256:05acc67a09980c2ecfedd3423f7ae0104839eccb55fc645773e1caa0951c3030 -unittest2==1.1.0 \ - --hash=sha256:13f77d0875db6d9b435e1d4f41e74ad4cc2eb6e1d5c824996092b3430f088bb8 \ - --hash=sha256:22882a0e418c284e1f718a822b3b022944d53d2d908e1690b319a9d3eb2c0579 -zope.component==4.2.2 \ - --hash=sha256:282c112b55dd8e3c869a3571f86767c150ab1284a9ace2bdec226c592acaf81a \ - --no-binary zope.component -zope.event==4.1.0 \ - --hash=sha256:dc7a59a2fd91730d3793131a5d261b29e93ec4e2a97f1bc487ce8defee2fe786 \ - --no-binary zope.event -zope.interface==4.1.3 \ - --hash=sha256:f07b631f7a601cd8cbd3332d54f43142c7088a83299f859356f08d1d4d4259b3 \ - --hash=sha256:de5cca083b9439d8002fb76bbe6b4998c5a5a721fab25b84298967f002df4c94 \ - --hash=sha256:6788416f7ea7f5b8a97be94825377aa25e8bdc73463e07baaf9858b29e737077 \ - --hash=sha256:6f3230f7254518201e5a3708cbb2de98c848304f06e3ded8bfb39e5825cba2e1 \ - --hash=sha256:5fa575a5240f04200c3088427d0d4b7b737f6e9018818a51d8d0f927a6a2517a \ - --hash=sha256:522194ad6a545735edd75c8a83f48d65d1af064e432a7d320d64f56bafc12e99 \ - --hash=sha256:e8c7b2d40943f71c99148c97f66caa7f5134147f57423f8db5b4825099ce9a09 \ - --hash=sha256:279024f0208601c3caa907c53876e37ad88625f7eaf1cb3842dbe360b2287017 \ - --hash=sha256:2e221a9eec7ccc58889a278ea13dcfed5ef939d80b07819a9a8b3cb1c681484f \ - --hash=sha256:69118965410ec86d44dc6b9017ee3ddbd582e0c0abeef62b3a19dbf6c8ad132b \ - --hash=sha256:d04df8686ec864d0cade8cf199f7f83aecd416109a20834d568f8310ded12dea \ - --hash=sha256:e75a947e15ee97e7e71e02ea302feb2fc62d3a2bb4668bf9dfbed43a506ac7e7 \ - --hash=sha256:4e45d22fb883222a5ab9f282a116fec5ee2e8d1a568ccff6a2d75bbd0eb6bcfc \ - --hash=sha256:bce9339bb3c7a55e0803b63d21c5839e8e479bc85c4adf42ae415b72f94facb2 \ - --hash=sha256:928138365245a0e8869a5999fbcc2a45475a0a6ed52a494d60dbdc540335fedd \ - --hash=sha256:0d841ba1bb840eea0e6489dc5ecafa6125554971f53b5acb87764441e61bceba \ - --hash=sha256:b09c8c1d47b3531c400e0195697f1414a63221de6ef478598a4f1460f7d9a392 -requests-toolbelt==0.8.0 \ - --hash=sha256:42c9c170abc2cacb78b8ab23ac957945c7716249206f90874651971a4acff237 \ - --hash=sha256:f6a531936c6fa4c6cfce1b9c10d5c4f498d16528d2a54a22ca00011205a187b5 -chardet==3.0.2 \ - --hash=sha256:4f7832e7c583348a9eddd927ee8514b3bf717c061f57b21dbe7697211454d9bb \ - --hash=sha256:6ebf56457934fdce01fb5ada5582762a84eed94cad43ed877964aebbdd8174c0 -urllib3==1.24.1 \ - --hash=sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39 \ - --hash=sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22 -certifi==2017.4.17 \ - --hash=sha256:f4318671072f030a33c7ca6acaef720ddd50ff124d1388e50c1bda4cbd6d7010 \ - --hash=sha256:f7527ebf7461582ce95f7a9e03dd141ce810d40590834f4ec20cddd54234c10a - -# Contains the requirements for the letsencrypt package. -# -# Since the letsencrypt package depends on certbot and using pip with hashes -# requires that all installed packages have hashes listed, this allows -# dependency-requirements.txt to be used without requiring a hash for a -# (potentially unreleased) Certbot package. - -letsencrypt==0.7.0 \ - --hash=sha256:105a5fb107e45bcd0722eb89696986dcf5f08a86a321d6aef25a0c7c63375ade \ - --hash=sha256:c36e532c486a7e92155ee09da54b436a3c420813ec1c590b98f635d924720de9 - -certbot==0.30.0 \ - --hash=sha256:b3468e128e74d2295598f6d3fbf9d0edfb67fe5abaca3b985a9e858395bd027f \ - --hash=sha256:d631fe6c75700ce9b2fdae194ff8b53c7518545d87dd451a1704f7572dcd49e8 -acme==0.30.0 \ - --hash=sha256:eed9389f802ebf4988c9e43c28ad3d5c2734237371d78e97450a1d61189a15aa \ - --hash=sha256:984b6d00bec73dcfa616636a760e80ca14bd246fb908710a656547f542f09445 -certbot-apache==0.30.0 \ - --hash=sha256:d38c70fc6930db298ea992a3145362eebdce460d3d2651f86a8f2f43d838c6d0 \ - --hash=sha256:1d4bc207d53a3e5d37e5d9ebd05f26089aa21d1fbf384113ed9d1829b4d1e9bf -certbot-nginx==0.30.0 \ - --hash=sha256:6163c7d0080f59b4ebe510afcc6af2d2eebf15469275c3835866690db4d465d6 \ - --hash=sha256:e39a3f3d77cd4c653949cf066fb2211039fd2032665697c27b6e8501c7c2dd92 - -UNLIKELY_EOF - # ------------------------------------------------------------------------- - cat << "UNLIKELY_EOF" > "$TEMP_DIR/pipstrap.py" -#!/usr/bin/env python -"""A small script that can act as a trust root for installing pip >=8 -Embed this in your project, and your VCS checkout is all you have to trust. In -a post-peep era, this lets you claw your way to a hash-checking version of pip, -with which you can install the rest of your dependencies safely. All it assumes -is Python 2.6 or better and *some* version of pip already installed. If -anything goes wrong, it will exit with a non-zero status code. -""" -# This is here so embedded copies are MIT-compliant: -# Copyright (c) 2016 Erik Rose -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -from __future__ import print_function -from distutils.version import StrictVersion -from hashlib import sha256 -from os import environ -from os.path import join -from pipes import quote -from shutil import rmtree -try: - from subprocess import check_output -except ImportError: - from subprocess import CalledProcessError, PIPE, Popen - - def check_output(*popenargs, **kwargs): - if 'stdout' in kwargs: - raise ValueError('stdout argument not allowed, it will be ' - 'overridden.') - process = Popen(stdout=PIPE, *popenargs, **kwargs) - output, unused_err = process.communicate() - retcode = process.poll() - if retcode: - cmd = kwargs.get("args") - if cmd is None: - cmd = popenargs[0] - raise CalledProcessError(retcode, cmd) - return output -from sys import exit, version_info -from tempfile import mkdtemp -try: - from urllib2 import build_opener, HTTPHandler, HTTPSHandler -except ImportError: - from urllib.request import build_opener, HTTPHandler, HTTPSHandler -try: - from urlparse import urlparse -except ImportError: - from urllib.parse import urlparse # 3.4 - - -__version__ = 1, 5, 1 -PIP_VERSION = '9.0.1' -DEFAULT_INDEX_BASE = 'https://pypi.python.org' - - -# wheel has a conditional dependency on argparse: -maybe_argparse = ( - [('18/dd/e617cfc3f6210ae183374cd9f6a26b20514bbb5a792af97949c5aacddf0f/' - 'argparse-1.4.0.tar.gz', - '62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4')] - if version_info < (2, 7, 0) else []) - - -PACKAGES = maybe_argparse + [ - # Pip has no dependencies, as it vendors everything: - ('11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/' - 'pip-{0}.tar.gz'.format(PIP_VERSION), - '09f243e1a7b461f654c26a725fa373211bb7ff17a9300058b205c61658ca940d'), - # This version of setuptools has only optional dependencies: - ('59/88/2f3990916931a5de6fa9706d6d75eb32ee8b78627bb2abaab7ed9e6d0622/' - 'setuptools-29.0.1.tar.gz', - 'b539118819a4857378398891fa5366e090690e46b3e41421a1e07d6e9fd8feb0'), - ('c9/1d/bd19e691fd4cfe908c76c429fe6e4436c9e83583c4414b54f6c85471954a/' - 'wheel-0.29.0.tar.gz', - '1ebb8ad7e26b448e9caa4773d2357849bf80ff9e313964bcaf79cbf0201a1648') -] - - -class HashError(Exception): - def __str__(self): - url, path, actual, expected = self.args - return ('{url} did not match the expected hash {expected}. Instead, ' - 'it was {actual}. The file (left at {path}) may have been ' - 'tampered with.'.format(**locals())) - - -def hashed_download(url, temp, digest): - """Download ``url`` to ``temp``, make sure it has the SHA-256 ``digest``, - and return its path.""" - # Based on pip 1.4.1's URLOpener but with cert verification removed. Python - # >=2.7.9 verifies HTTPS certs itself, and, in any case, the cert - # authenticity has only privacy (not arbitrary code execution) - # implications, since we're checking hashes. - def opener(using_https=True): - opener = build_opener(HTTPSHandler()) - if using_https: - # Strip out HTTPHandler to prevent MITM spoof: - for handler in opener.handlers: - if isinstance(handler, HTTPHandler): - opener.handlers.remove(handler) - return opener - - def read_chunks(response, chunk_size): - while True: - chunk = response.read(chunk_size) - if not chunk: - break - yield chunk - - parsed_url = urlparse(url) - response = opener(using_https=parsed_url.scheme == 'https').open(url) - path = join(temp, parsed_url.path.split('/')[-1]) - actual_hash = sha256() - with open(path, 'wb') as file: - for chunk in read_chunks(response, 4096): - file.write(chunk) - actual_hash.update(chunk) - - actual_digest = actual_hash.hexdigest() - if actual_digest != digest: - raise HashError(url, path, actual_digest, digest) - return path - - -def get_index_base(): - """Return the URL to the dir containing the "packages" folder. - Try to wring something out of PIP_INDEX_URL, if set. Hack "/simple" off the - end if it's there; that is likely to give us the right dir. - """ - env_var = environ.get('PIP_INDEX_URL', '').rstrip('/') - if env_var: - SIMPLE = '/simple' - if env_var.endswith(SIMPLE): - return env_var[:-len(SIMPLE)] - else: - return env_var - else: - return DEFAULT_INDEX_BASE - - -def main(): - pip_version = StrictVersion(check_output(['pip', '--version']) - .decode('utf-8').split()[1]) - min_pip_version = StrictVersion(PIP_VERSION) - if pip_version >= min_pip_version: - return 0 - has_pip_cache = pip_version >= StrictVersion('6.0') - index_base = get_index_base() - temp = mkdtemp(prefix='pipstrap-') - try: - downloads = [hashed_download(index_base + '/packages/' + path, - temp, - digest) - for path, digest in PACKAGES] - check_output('pip install --no-index --no-deps -U ' + - # Disable cache since we're not using it and it otherwise - # sometimes throws permission warnings: - ('--no-cache-dir ' if has_pip_cache else '') + - ' '.join(quote(d) for d in downloads), - shell=True) - except HashError as exc: - print(exc) - except Exception: - rmtree(temp) - raise - else: - rmtree(temp) - return 0 - return 1 - - -if __name__ == '__main__': - exit(main()) - -UNLIKELY_EOF - # ------------------------------------------------------------------------- - # Set PATH so pipstrap upgrades the right (v)env: - PATH="$VENV_BIN:$PATH" "$VENV_BIN/python" "$TEMP_DIR/pipstrap.py" - set +e - if [ "$VERBOSE" = 1 ]; then - "$VENV_BIN/pip" install --disable-pip-version-check --no-cache-dir --require-hashes -r "$TEMP_DIR/letsencrypt-auto-requirements.txt" - else - PIP_OUT=`"$VENV_BIN/pip" install --disable-pip-version-check --no-cache-dir --require-hashes -r "$TEMP_DIR/letsencrypt-auto-requirements.txt" 2>&1` - fi - PIP_STATUS=$? - set -e - if [ "$PIP_STATUS" != 0 ]; then - # Report error. (Otherwise, be quiet.) - error "Had a problem while installing Python packages." - if [ "$VERBOSE" != 1 ]; then - error - error "pip prints the following errors: " - error "=====================================================" - error "$PIP_OUT" - error "=====================================================" - error - error "Certbot has problem setting up the virtual environment." - - if `echo $PIP_OUT | grep -q Killed` || `echo $PIP_OUT | grep -q "allocate memory"` ; then - error - error "Based on your pip output, the problem can likely be fixed by " - error "increasing the available memory." - else - error - error "We were not be able to guess the right solution from your pip " - error "output." - fi - - error - error "Consult https://certbot.eff.org/docs/install.html#problems-with-python-virtual-environment" - error "for possible solutions." - error "You may also find some support resources at https://certbot.eff.org/support/ ." - fi - rm -rf "$VENV_PATH" - exit 1 - fi - - if [ -d "$OLD_VENV_PATH" -a ! -L "$OLD_VENV_PATH" ]; then - rm -rf "$OLD_VENV_PATH" - ln -s "$VENV_PATH" "$OLD_VENV_PATH" - fi - - say "Installation succeeded." - fi - - if [ "$INSTALL_ONLY" = 1 ]; then - say "Certbot is installed." - exit 0 - fi - - "$VENV_BIN/letsencrypt" "$@" - -else - # Phase 1: Upgrade certbot-auto if necessary, then self-invoke. - # - # Each phase checks the version of only the thing it is responsible for - # upgrading. Phase 1 checks the version of the latest release of - # certbot-auto (which is always the same as that of the certbot - # package). Phase 2 checks the version of the locally installed certbot. - export PHASE_1_VERSION="$LE_AUTO_VERSION" - - if [ ! -f "$VENV_BIN/letsencrypt" ]; then - if ! OldVenvExists; then - if [ "$HELP" = 1 ]; then - echo "$USAGE" - exit 0 - fi - # If it looks like we've never bootstrapped before, bootstrap: - Bootstrap - fi - fi - if [ "$OS_PACKAGES_ONLY" = 1 ]; then - say "OS packages installed." - exit 0 - fi - - if [ "$NO_SELF_UPGRADE" != 1 ]; then - TEMP_DIR=$(TempDir) - trap 'rm -rf "$TEMP_DIR"' EXIT - # --------------------------------------------------------------------------- - cat << "UNLIKELY_EOF" > "$TEMP_DIR/fetch.py" -"""Do downloading and JSON parsing without additional dependencies. :: - - # Print latest released version of LE to stdout: - python fetch.py --latest-version - - # Download letsencrypt-auto script from git tag v1.2.3 into the folder I'm - # in, and make sure its signature verifies: - python fetch.py --le-auto-script v1.2.3 - -On failure, return non-zero. - -""" - -from __future__ import print_function, unicode_literals - -from distutils.version import LooseVersion -from json import loads -from os import devnull, environ -from os.path import dirname, join -import re -import ssl -from subprocess import check_call, CalledProcessError -from sys import argv, exit -try: - from urllib2 import build_opener, HTTPHandler, HTTPSHandler - from urllib2 import HTTPError, URLError -except ImportError: - from urllib.request import build_opener, HTTPHandler, HTTPSHandler - from urllib.error import HTTPError, URLError - -PUBLIC_KEY = environ.get('LE_AUTO_PUBLIC_KEY', """-----BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6MR8W/galdxnpGqBsYbq -OzQb2eyW15YFjDDEMI0ZOzt8f504obNs920lDnpPD2/KqgsfjOgw2K7xWDJIj/18 -xUvWPk3LDkrnokNiRkA3KOx3W6fHycKL+zID7zy+xZYBuh2fLyQtWV1VGQ45iNRp -9+Zo7rH86cdfgkdnWTlNSHyTLW9NbXvyv/E12bppPcEvgCTAQXgnDVJ0/sqmeiij -n9tTFh03aM+R2V/21h8aTraAS24qiPCz6gkmYGC8yr6mglcnNoYbsLNYZ69zF1XH -cXPduCPdPdfLlzVlKK1/U7hkA28eG3BIAMh6uJYBRJTpiGgaGdPd7YekUB8S6cy+ -CQIDAQAB ------END PUBLIC KEY----- -""") - -class ExpectedError(Exception): - """A novice-readable exception that also carries the original exception for - debugging""" - - -class HttpsGetter(object): - def __init__(self): - """Build an HTTPS opener.""" - # Based on pip 1.4.1's URLOpener - # This verifies certs on only Python >=2.7.9, and when NO_CERT_VERIFY isn't set. - if environ.get('NO_CERT_VERIFY') == '1' and hasattr(ssl, 'SSLContext'): - self._opener = build_opener(HTTPSHandler(context=cert_none_context())) - else: - self._opener = build_opener(HTTPSHandler()) - # Strip out HTTPHandler to prevent MITM spoof: - for handler in self._opener.handlers: - if isinstance(handler, HTTPHandler): - self._opener.handlers.remove(handler) - - def get(self, url): - """Return the document contents pointed to by an HTTPS URL. - - If something goes wrong (404, timeout, etc.), raise ExpectedError. - - """ - try: - # socket module docs say default timeout is None: that is, no - # timeout - return self._opener.open(url, timeout=30).read() - except (HTTPError, IOError) as exc: - raise ExpectedError("Couldn't download %s." % url, exc) - - -def write(contents, dir, filename): - """Write something to a file in a certain directory.""" - with open(join(dir, filename), 'wb') as file: - file.write(contents) - - -def latest_stable_version(get): - """Return the latest stable release of letsencrypt.""" - metadata = loads(get( - environ.get('LE_AUTO_JSON_URL', - 'https://pypi.python.org/pypi/certbot/json')).decode('UTF-8')) - # metadata['info']['version'] actually returns the latest of any kind of - # release release, contrary to https://wiki.python.org/moin/PyPIJSON. - # The regex is a sufficient regex for picking out prereleases for most - # packages, LE included. - return str(max(LooseVersion(r) for r - in metadata['releases'].keys() - if re.match('^[0-9.]+$', r))) - - -def verified_new_le_auto(get, tag, temp_dir): - """Return the path to a verified, up-to-date letsencrypt-auto script. - - If the download's signature does not verify or something else goes wrong - with the verification process, raise ExpectedError. - - """ - le_auto_dir = environ.get( - 'LE_AUTO_DIR_TEMPLATE', - 'https://raw.githubusercontent.com/certbot/certbot/%s/' - 'letsencrypt-auto-source/') % tag - write(get(le_auto_dir + 'letsencrypt-auto'), temp_dir, 'letsencrypt-auto') - write(get(le_auto_dir + 'letsencrypt-auto.sig'), temp_dir, 'letsencrypt-auto.sig') - write(PUBLIC_KEY.encode('UTF-8'), temp_dir, 'public_key.pem') - try: - with open(devnull, 'w') as dev_null: - check_call(['openssl', 'dgst', '-sha256', '-verify', - join(temp_dir, 'public_key.pem'), - '-signature', - join(temp_dir, 'letsencrypt-auto.sig'), - join(temp_dir, 'letsencrypt-auto')], - stdout=dev_null, - stderr=dev_null) - except CalledProcessError as exc: - raise ExpectedError("Couldn't verify signature of downloaded " - "certbot-auto.", exc) - - -def cert_none_context(): - """Create a SSLContext object to not check hostname.""" - # PROTOCOL_TLS isn't available before 2.7.13 but this code is for 2.7.9+, so use this. - context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) - context.verify_mode = ssl.CERT_NONE - return context - - -def main(): - get = HttpsGetter().get - flag = argv[1] - try: - if flag == '--latest-version': - print(latest_stable_version(get)) - elif flag == '--le-auto-script': - tag = argv[2] - verified_new_le_auto(get, tag, dirname(argv[0])) - except ExpectedError as exc: - print(exc.args[0], exc.args[1]) - return 1 - else: - return 0 - - -if __name__ == '__main__': - exit(main()) - -UNLIKELY_EOF - # --------------------------------------------------------------------------- - DeterminePythonVersion "NOCRASH" - if [ "$PYVER" -lt "$MIN_PYVER" ]; then - error "WARNING: couldn't find Python $MIN_PYTHON_VERSION+ to check for updates." - elif ! REMOTE_VERSION=`"$LE_PYTHON" "$TEMP_DIR/fetch.py" --latest-version` ; then - error "WARNING: unable to check for updates." - fi - - LE_VERSION_STATE=`CompareVersions "$LE_PYTHON" "$LE_AUTO_VERSION" "$REMOTE_VERSION"` - if [ "$LE_VERSION_STATE" = "UNOFFICIAL" ]; then - say "Unofficial certbot-auto version detected, self-upgrade is disabled: $LE_AUTO_VERSION" - elif [ "$LE_VERSION_STATE" = "OUTDATED" ]; then - say "Upgrading certbot-auto $LE_AUTO_VERSION to $REMOTE_VERSION..." - - # Now we drop into Python so we don't have to install even more - # dependencies (curl, etc.), for better flow control, and for the option of - # future Windows compatibility. - "$LE_PYTHON" "$TEMP_DIR/fetch.py" --le-auto-script "v$REMOTE_VERSION" - - # Install new copy of certbot-auto. - # TODO: Deal with quotes in pathnames. - say "Replacing certbot-auto..." - # Clone permissions with cp. chmod and chown don't have a --reference - # option on macOS or BSD, and stat -c on Linux is stat -f on macOS and BSD: - cp -p "$0" "$TEMP_DIR/letsencrypt-auto.permission-clone" - cp "$TEMP_DIR/letsencrypt-auto" "$TEMP_DIR/letsencrypt-auto.permission-clone" - # Using mv rather than cp leaves the old file descriptor pointing to the - # original copy so the shell can continue to read it unmolested. mv across - # filesystems is non-atomic, doing `rm dest, cp src dest, rm src`, but the - # cp is unlikely to fail if the rm doesn't. - mv -f "$TEMP_DIR/letsencrypt-auto.permission-clone" "$0" - fi # A newer version is available. - fi # Self-upgrading is allowed. - - RerunWithArgs --le-auto-phase2 "$@" -fi diff --git a/letsencrypt-auto-source/Dockerfile.centos6 b/letsencrypt-auto-source/Dockerfile.centos6 deleted file mode 100644 index 92fec168b47..00000000000 --- a/letsencrypt-auto-source/Dockerfile.centos6 +++ /dev/null @@ -1,37 +0,0 @@ -# For running tests, build a docker image with a passwordless sudo and a trust -# store we can manipulate. - -FROM centos:6 - -RUN yum install -y epel-release - -# Install pip and sudo: -RUN yum install -y python-pip sudo -# Use pipstrap to update to a stable and tested version of pip -COPY ./pieces/pipstrap.py /opt -RUN /opt/pipstrap.py -# Pin pytest version for increased stability -RUN pip install pytest==3.2.5 six==1.10.0 - -# Add an unprivileged user: -RUN useradd --create-home --home-dir /home/lea --shell /bin/bash --groups wheel --uid 1000 lea - -# Let that user sudo: -RUN sed -i.bkp -e \ - 's/# %wheel\(NOPASSWD: ALL\)\?/%wheel/g' \ - /etc/sudoers - -RUN mkdir -p /home/lea/certbot - -# Install fake testing CA: -COPY ./tests/certs/ca/my-root-ca.crt.pem /usr/local/share/ca-certificates/ -RUN update-ca-trust - -# Copy code: -COPY . /home/lea/certbot/letsencrypt-auto-source - -USER lea -WORKDIR /home/lea - -RUN sudo chmod +x certbot/letsencrypt-auto-source/tests/centos6_tests.sh -CMD sudo certbot/letsencrypt-auto-source/tests/centos6_tests.sh diff --git a/letsencrypt-auto-source/Dockerfile.precise b/letsencrypt-auto-source/Dockerfile.precise deleted file mode 100644 index 39a167c14c6..00000000000 --- a/letsencrypt-auto-source/Dockerfile.precise +++ /dev/null @@ -1,36 +0,0 @@ -# For running tests, build a docker image with a passwordless sudo and a trust -# store we can manipulate. - -FROM ubuntu:precise - -# Add an unprivileged user: -RUN useradd --create-home --home-dir /home/lea --shell /bin/bash --groups sudo --uid 1000 lea - -# Install pip, sudo, and openssl: -RUN apt-get update && \ - apt-get -q -y install python-pip sudo openssl && \ - apt-get clean - -# Use pipstrap to update to a stable and tested version of pip -COPY ./pieces/pipstrap.py /opt -RUN /opt/pipstrap.py -# Pin pytest version for increased stability -RUN pip install pytest==3.2.5 six==1.10.0 - -# Let that user sudo: -RUN sed -i.bkp -e \ - 's/%sudo\s\+ALL=(ALL\(:ALL\)\?)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' \ - /etc/sudoers - -RUN mkdir -p /home/lea/certbot - -# Install fake testing CA: -COPY ./tests/certs/ca/my-root-ca.crt.pem /usr/local/share/ca-certificates/ - -# Copy code: -COPY . /home/lea/certbot/letsencrypt-auto-source - -USER lea -WORKDIR /home/lea - -CMD ["pytest", "-v", "-s", "certbot/letsencrypt-auto-source/tests"] diff --git a/letsencrypt-auto-source/Dockerfile.trusty b/letsencrypt-auto-source/Dockerfile.trusty deleted file mode 100644 index 3de88f9afd6..00000000000 --- a/letsencrypt-auto-source/Dockerfile.trusty +++ /dev/null @@ -1,36 +0,0 @@ -# For running tests, build a docker image with a passwordless sudo and a trust -# store we can manipulate. - -FROM ubuntu:trusty - -# Add an unprivileged user: -RUN useradd --create-home --home-dir /home/lea --shell /bin/bash --groups sudo --uid 1000 lea - -# Let that user sudo: -RUN sed -i.bkp -e \ - 's/%sudo\s\+ALL=(ALL\(:ALL\)\?)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' \ - /etc/sudoers - -# Install pip: -RUN apt-get update && \ - apt-get -q -y install python-pip && \ - apt-get clean -# Use pipstrap to update to a stable and tested version of pip -COPY ./pieces/pipstrap.py /opt -RUN /opt/pipstrap.py -# Pin pytest version for increased stability -RUN pip install pytest==3.2.5 six==1.10.0 - -RUN mkdir -p /home/lea/certbot - -# Install fake testing CA: -COPY ./tests/certs/ca/my-root-ca.crt.pem /usr/local/share/ca-certificates/ -RUN update-ca-certificates - -# Copy code: -COPY . /home/lea/certbot/letsencrypt-auto-source - -USER lea -WORKDIR /home/lea - -CMD ["pytest", "-v", "-s", "certbot/letsencrypt-auto-source/tests"] diff --git a/letsencrypt-auto-source/Dockerfile.wheezy b/letsencrypt-auto-source/Dockerfile.wheezy deleted file mode 100644 index f4f3fea15ed..00000000000 --- a/letsencrypt-auto-source/Dockerfile.wheezy +++ /dev/null @@ -1,35 +0,0 @@ -# For running tests, build a docker image with a passwordless sudo and a trust -# store we can manipulate. - -FROM debian:wheezy - -# Add an unprivileged user: -RUN useradd --create-home --home-dir /home/lea --shell /bin/bash --groups sudo --uid 1000 lea - -# Install pip, sudo, and openssl: -RUN apt-get update && \ - apt-get -q -y install python-pip sudo openssl && \ - apt-get clean -# Use pipstrap to update to a stable and tested version of pip -COPY ./pieces/pipstrap.py /opt -RUN /opt/pipstrap.py -# Pin pytest version for increased stability -RUN pip install pytest==3.2.5 six==1.10.0 - -# Let that user sudo: -RUN sed -i.bkp -e \ - 's/%sudo\s\+ALL=(ALL\(:ALL\)\?)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' \ - /etc/sudoers - -RUN mkdir -p /home/lea/certbot - -# Install fake testing CA: -COPY ./tests/certs/ca/my-root-ca.crt.pem /usr/local/share/ca-certificates/ - -# Copy code: -COPY . /home/lea/certbot/letsencrypt-auto-source - -USER lea -WORKDIR /home/lea - -CMD ["pytest", "-v", "-s", "certbot/letsencrypt-auto-source/tests"] diff --git a/letsencrypt-auto-source/README.md b/letsencrypt-auto-source/README.md new file mode 100644 index 00000000000..f37f3dfbd44 --- /dev/null +++ b/letsencrypt-auto-source/README.md @@ -0,0 +1,12 @@ +# Legacy letsencrypt-auto files + +`certbot-auto` and `letsencrypt-auto` were two names for the same self-updating +shell script that wrapped Certbot. Old versions of the script continue to rely +on pulling `letsencrypt-auto` and `letsencrypt-auto.sig` from this directory hosted on Github to download and +verify updates. We're keeping these files and the tests for them around to +prevent these old scripts from breaking. + +If we need or want to remove these files and tests in the future, we can, but +before we do, we should write a Let's Encrypt forum post describing the error +message users will see and how they can work around the problem. See +https://github.com/certbot/certbot/issues/8812 for more info. diff --git a/letsencrypt-auto-source/build.py b/letsencrypt-auto-source/build.py deleted file mode 100755 index a1e40fe446d..00000000000 --- a/letsencrypt-auto-source/build.py +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env python -"""Stitch together the letsencrypt-auto script. - -Implement a simple templating language in which {{ some/file }} turns into the -contents of the file at ./pieces/some/file except for certain tokens which have -other, special definitions. - -""" -from os.path import abspath, dirname, join -import re - -from version import certbot_version, file_contents - - -DIR = dirname(abspath(__file__)) - - -def build(version=None, requirements=None): - """Return the built contents of the letsencrypt-auto script. - - :arg version: The version to attach to the script. Default: the version of - the certbot package - :arg requirements: The contents of the requirements file to embed. Default: - contents of dependency-requirements.txt, letsencrypt-requirements.txt, - and certbot-requirements.txt - - """ - special_replacements = { - 'LE_AUTO_VERSION': version or certbot_version(DIR) - } - if requirements: - special_replacements['dependency-requirements.txt'] = '' - special_replacements['letsencrypt-requirements.txt'] = '' - special_replacements['certbot-requirements.txt'] = requirements - - def replacer(match): - token = match.group(1) - if token in special_replacements: - return special_replacements[token] - else: - return file_contents(join(DIR, 'pieces', token)) - - return re.sub(r'{{\s*([A-Za-z0-9_./-]+)\s*}}', - replacer, - file_contents(join(DIR, 'letsencrypt-auto.template'))) - - -def main(): - with open(join(DIR, 'letsencrypt-auto'), 'w') as out: - out.write(build()) - - -if __name__ == '__main__': - main() diff --git a/letsencrypt-auto-source/certbot-auto.asc b/letsencrypt-auto-source/certbot-auto.asc deleted file mode 100644 index 4e2a700e511..00000000000 --- a/letsencrypt-auto-source/certbot-auto.asc +++ /dev/null @@ -1,11 +0,0 @@ ------BEGIN PGP SIGNATURE----- - -iQEzBAABCAAdFiEEos+1H6J1pyhiNOeyTRfJlc2XdfIFAlwtH9cACgkQTRfJlc2X -dfIqUwf/RXLZAeFF/59PjTAzcV+eEISlvEmFcV0zL3vv23PsY3S5Iuuwcd6rTm5M -UWNtmUTmFVo0xmxAj6Eqfpnt0P+JPpPcnbLNIGKFekBWIshgH84RRFWPJjNh/hu1 -pyzkkcWaOB86egdVfjvuRJ0j7AGd0ih6ur2rlgfHVjTYR+0EdWszFDEFBlq8cpct -9d1gCgH7VWKSIQMhzGLMsmdMxNoDl4hiqVPU0FP5/mn2xGF7FgeKNW3+NiTouKuB -mZOeEl3f3uOze/suHPyfOu+49jk+TWWE05Xfqfowjf486nKPg6/uSA2izW/MwIKN -HuIuY3bBf+lx5yUVIraoZhH2MxODDQ== -=BZqz ------END PGP SIGNATURE----- diff --git a/letsencrypt-auto-source/letsencrypt-auto b/letsencrypt-auto-source/letsencrypt-auto index c08a081605d..c37c45596ef 100755 --- a/letsencrypt-auto-source/letsencrypt-auto +++ b/letsencrypt-auto-source/letsencrypt-auto @@ -31,7 +31,7 @@ if [ -z "$VENV_PATH" ]; then fi VENV_BIN="$VENV_PATH/bin" BOOTSTRAP_VERSION_PATH="$VENV_PATH/certbot-auto-bootstrap-version.txt" -LE_AUTO_VERSION="0.31.0.dev0" +LE_AUTO_VERSION="1.14.0" BASENAME=$(basename $0) USAGE="Usage: $BASENAME [OPTIONS] A self-updating wrapper script for the Certbot ACME client. When run, updates @@ -45,6 +45,7 @@ Help for certbot itself cannot be provided until it is installed. -h, --help print this help -n, --non-interactive, --noninteractive run without asking for user input --no-bootstrap do not install OS dependencies + --no-permissions-check do not warn about file system permissions --no-self-upgrade do not download updates --os-packages-only install OS dependencies and exit --install-only install certbot, upgrade if needed, and exit @@ -67,6 +68,8 @@ for arg in "$@" ; do # Do not upgrade this script (also prevents client upgrades, because each # copy of the script pins a hash of the python client) NO_SELF_UPGRADE=1;; + --no-permissions-check) + NO_PERMISSIONS_CHECK=1;; --no-bootstrap) NO_BOOTSTRAP=1;; --help) @@ -172,7 +175,11 @@ SetRootAuthMechanism() { sudo) SUDO="sudo -E" ;; - '') ;; # Nothing to do for plain root method. + '') + # If we're not running with root, don't check that this script can only + # be modified by system users and groups. + NO_PERMISSIONS_CHECK=1 + ;; *) error "Error: unknown root authorization mechanism '$LE_AUTO_SUDO'." exit 1 @@ -249,20 +256,28 @@ DeprecationBootstrap() { fi } -MIN_PYTHON_VERSION="2.7" -MIN_PYVER=$(echo "$MIN_PYTHON_VERSION" | sed 's/\.//') +MIN_PYTHON_2_VERSION="2.7" +MIN_PYVER2=$(echo "$MIN_PYTHON_2_VERSION" | sed 's/\.//') +MIN_PYTHON_3_VERSION="3.6" +MIN_PYVER3=$(echo "$MIN_PYTHON_3_VERSION" | sed 's/\.//') # Sets LE_PYTHON to Python version string and PYVER to the first two -# digits of the python version +# digits of the python version. +# MIN_PYVER and MIN_PYTHON_VERSION are also set by this function, and their +# values depend on if we try to use Python 3 or Python 2. DeterminePythonVersion() { # Arguments: "NOCRASH" if we shouldn't crash if we don't find a good python # # If no Python is found, PYVER is set to 0. if [ "$USE_PYTHON_3" = 1 ]; then + MIN_PYVER=$MIN_PYVER3 + MIN_PYTHON_VERSION=$MIN_PYTHON_3_VERSION for LE_PYTHON in "$LE_PYTHON" python3; do # Break (while keeping the LE_PYTHON value) if found. $EXISTS "$LE_PYTHON" > /dev/null && break done else + MIN_PYVER=$MIN_PYVER2 + MIN_PYTHON_VERSION=$MIN_PYTHON_2_VERSION for LE_PYTHON in "$LE_PYTHON" python2.7 python27 python2 python; do # Break (while keeping the LE_PYTHON value) if found. $EXISTS "$LE_PYTHON" > /dev/null && break @@ -278,7 +293,7 @@ DeterminePythonVersion() { fi fi - PYVER=`"$LE_PYTHON" -V 2>&1 | cut -d" " -f 2 | cut -d. -f1,2 | sed 's/\.//'` + PYVER=$("$LE_PYTHON" -V 2>&1 | cut -d" " -f 2 | cut -d. -f1,2 | sed 's/\.//') if [ "$PYVER" -lt "$MIN_PYVER" ]; then if [ "$1" != "NOCRASH" ]; then error "You have an ancient version of Python entombed in your operating system..." @@ -333,63 +348,11 @@ BootstrapDebCommon() { fi augeas_pkg="libaugeas0 augeas-lenses" - AUGVERSION=`LC_ALL=C apt-cache show --no-all-versions libaugeas0 | grep ^Version: | cut -d" " -f2` if [ "$ASSUME_YES" = 1 ]; then YES_FLAG="-y" fi - AddBackportRepo() { - # ARGS: - BACKPORT_NAME="$1" - BACKPORT_SOURCELINE="$2" - say "To use the Apache Certbot plugin, augeas needs to be installed from $BACKPORT_NAME." - if ! grep -v -e ' *#' /etc/apt/sources.list | grep -q "$BACKPORT_NAME" ; then - # This can theoretically error if sources.list.d is empty, but in that case we don't care. - if ! grep -v -e ' *#' /etc/apt/sources.list.d/* 2>/dev/null | grep -q "$BACKPORT_NAME"; then - if [ "$ASSUME_YES" = 1 ]; then - /bin/echo -n "Installing augeas from $BACKPORT_NAME in 3 seconds..." - sleep 1s - /bin/echo -ne "\e[0K\rInstalling augeas from $BACKPORT_NAME in 2 seconds..." - sleep 1s - /bin/echo -e "\e[0K\rInstalling augeas from $BACKPORT_NAME in 1 second ..." - sleep 1s - add_backports=1 - else - read -p "Would you like to enable the $BACKPORT_NAME repository [Y/n]? " response - case $response in - [yY][eE][sS]|[yY]|"") - add_backports=1;; - *) - add_backports=0;; - esac - fi - if [ "$add_backports" = 1 ]; then - sh -c "echo $BACKPORT_SOURCELINE >> /etc/apt/sources.list.d/$BACKPORT_NAME.list" - apt-get $QUIET_FLAG update - fi - fi - fi - if [ "$add_backports" != 0 ]; then - apt-get install $QUIET_FLAG $YES_FLAG --no-install-recommends -t "$BACKPORT_NAME" $augeas_pkg - augeas_pkg= - fi - } - - - if dpkg --compare-versions 1.0 gt "$AUGVERSION" ; then - if lsb_release -a | grep -q wheezy ; then - AddBackportRepo wheezy-backports "deb http://http.debian.net/debian wheezy-backports main" - elif lsb_release -a | grep -q precise ; then - # XXX add ARM case - AddBackportRepo precise-backports "deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse" - else - echo "No libaugeas0 version is available that's new enough to run the" - echo "Certbot apache plugin..." - fi - # XXX add a case for ubuntu PPAs - fi - apt-get install $QUIET_FLAG $YES_FLAG --no-install-recommends \ python \ python-dev \ @@ -413,7 +376,9 @@ BootstrapDebCommon() { # Sets TOOL to the name of the package manager # Sets appropriate values for YES_FLAG and QUIET_FLAG based on $ASSUME_YES and $QUIET_FLAG. -# Enables EPEL if applicable and possible. +# Note: this function is called both while selecting the bootstrap scripts and +# during the actual bootstrap. Some things like prompting to user can be done in the latter +# case, but not in the former one. InitializeRPMCommonBase() { if type dnf 2>/dev/null then @@ -433,26 +398,6 @@ InitializeRPMCommonBase() { if [ "$QUIET" = 1 ]; then QUIET_FLAG='--quiet' fi - - if ! $TOOL list *virtualenv >/dev/null 2>&1; then - echo "To use Certbot, packages from the EPEL repository need to be installed." - if ! $TOOL list epel-release >/dev/null 2>&1; then - error "Enable the EPEL repository and try running Certbot again." - exit 1 - fi - if [ "$ASSUME_YES" = 1 ]; then - /bin/echo -n "Enabling the EPEL repository in 3 seconds..." - sleep 1s - /bin/echo -ne "\e[0K\rEnabling the EPEL repository in 2 seconds..." - sleep 1s - /bin/echo -e "\e[0K\rEnabling the EPEL repository in 1 second..." - sleep 1s - fi - if ! $TOOL install $YES_FLAG $QUIET_FLAG epel-release; then - error "Could not enable EPEL. Aborting bootstrap!" - exit 1 - fi - fi } BootstrapRpmCommonBase() { @@ -533,21 +478,100 @@ BootstrapRpmCommon() { BootstrapRpmCommonBase "$python_pkgs" } +# If new packages are installed by BootstrapRpmPython3 below, this version +# number must be increased. +BOOTSTRAP_RPM_PYTHON3_LEGACY_VERSION=1 + +# Checks if rh-python36 can be installed. +Python36SclIsAvailable() { + InitializeRPMCommonBase >/dev/null 2>&1; + + if "${TOOL}" list rh-python36 >/dev/null 2>&1; then + return 0 + fi + if "${TOOL}" list centos-release-scl >/dev/null 2>&1; then + return 0 + fi + return 1 +} + +# Try to enable rh-python36 from SCL if it is necessary and possible. +EnablePython36SCL() { + if "$EXISTS" python3.6 > /dev/null 2> /dev/null; then + return 0 + fi + if [ ! -f /opt/rh/rh-python36/enable ]; then + return 0 + fi + set +e + if ! . /opt/rh/rh-python36/enable; then + error 'Unable to enable rh-python36!' + exit 1 + fi + set -e +} + +# This bootstrap concerns old RedHat-based distributions that do not ship by default +# with Python 2.7, but only Python 2.6. We bootstrap them by enabling SCL and installing +# Python 3.6. Some of these distributions are: CentOS/RHEL/OL/SL 6. +BootstrapRpmPython3Legacy() { + # Tested with: + # - CentOS 6 + + InitializeRPMCommonBase + + if ! "${TOOL}" list rh-python36 >/dev/null 2>&1; then + echo "To use Certbot on this operating system, packages from the SCL repository need to be installed." + if ! "${TOOL}" list centos-release-scl >/dev/null 2>&1; then + error "Enable the SCL repository and try running Certbot again." + exit 1 + fi + if [ "${ASSUME_YES}" = 1 ]; then + /bin/echo -n "Enabling the SCL repository in 3 seconds... (Press Ctrl-C to cancel)" + sleep 1s + /bin/echo -ne "\e[0K\rEnabling the SCL repository in 2 seconds... (Press Ctrl-C to cancel)" + sleep 1s + /bin/echo -e "\e[0K\rEnabling the SCL repository in 1 second... (Press Ctrl-C to cancel)" + sleep 1s + fi + if ! "${TOOL}" install "${YES_FLAG}" "${QUIET_FLAG}" centos-release-scl; then + error "Could not enable SCL. Aborting bootstrap!" + exit 1 + fi + fi + + # CentOS 6 must use rh-python36 from SCL + if "${TOOL}" list rh-python36 >/dev/null 2>&1; then + python_pkgs="rh-python36-python + rh-python36-python-virtualenv + rh-python36-python-devel + " + else + error "No supported Python package available to install. Aborting bootstrap!" + exit 1 + fi + + BootstrapRpmCommonBase "${python_pkgs}" + + # Enable SCL rh-python36 after bootstrapping. + EnablePython36SCL +} + # If new packages are installed by BootstrapRpmPython3 below, this version # number must be increased. BOOTSTRAP_RPM_PYTHON3_VERSION=1 BootstrapRpmPython3() { # Tested with: - # - CentOS 6 + # - Fedora 29 InitializeRPMCommonBase - # EPEL uses python34 - if $TOOL list python34 >/dev/null 2>&1; then - python_pkgs="python34 - python34-devel - python34-tools + # Fedora 29 must use python3-virtualenv + if $TOOL list python3-virtualenv >/dev/null 2>&1; then + python_pkgs="python3 + python3-virtualenv + python3-devel " else error "No supported Python package available to install. Aborting bootstrap!" @@ -573,10 +597,20 @@ BootstrapSuseCommon() { QUIET_FLAG='-qq' fi + if zypper search -x python-virtualenv >/dev/null 2>&1; then + OPENSUSE_VIRTUALENV_PACKAGES="python-virtualenv" + else + # Since Leap 15.0 (and associated Tumbleweed version), python-virtualenv + # is a source package, and python2-virtualenv must be used instead. + # Also currently python2-setuptools is not a dependency of python2-virtualenv, + # while it should be. Installing it explicitly until upstream fix. + OPENSUSE_VIRTUALENV_PACKAGES="python2-virtualenv python2-setuptools" + fi + zypper $QUIET_FLAG $zypper_flags in $install_flags \ python \ python-devel \ - python-virtualenv \ + $OPENSUSE_VIRTUALENV_PACKAGES \ gcc \ augeas-lenses \ libopenssl-devel \ @@ -765,99 +799,98 @@ BootstrapMageiaCommon() { # that function. If Bootstrap is set to a function that doesn't install any # packages BOOTSTRAP_VERSION is not set. if [ -f /etc/debian_version ]; then - Bootstrap() { - BootstrapMessage "Debian-based OSes" - BootstrapDebCommon - } - BOOTSTRAP_VERSION="BootstrapDebCommon $BOOTSTRAP_DEB_COMMON_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 elif [ -f /etc/mageia-release ]; then # Mageia has both /etc/mageia-release and /etc/redhat-release - Bootstrap() { - ExperimentalBootstrap "Mageia" BootstrapMageiaCommon - } - BOOTSTRAP_VERSION="BootstrapMageiaCommon $BOOTSTRAP_MAGEIA_COMMON_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 elif [ -f /etc/redhat-release ]; then + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 # Run DeterminePythonVersion to decide on the basis of available Python versions # whether to use 2.x or 3.x on RedHat-like systems. # Then, revert LE_PYTHON to its previous state. prev_le_python="$LE_PYTHON" unset LE_PYTHON DeterminePythonVersion "NOCRASH" + + RPM_DIST_NAME=`(. /etc/os-release 2> /dev/null && echo $ID) || echo "unknown"` + + if [ "$PYVER" -eq 26 -a $(uname -m) != 'x86_64' ]; then + # 32 bits CentOS 6 and affiliates are not supported anymore by certbot-auto. + DEPRECATED_OS=1 + fi + + # Set RPM_DIST_VERSION to VERSION_ID from /etc/os-release after splitting on + # '.' characters (e.g. "8.0" becomes "8"). If the command exits with an + # error, RPM_DIST_VERSION is set to "unknown". + RPM_DIST_VERSION=$( (. /etc/os-release 2> /dev/null && echo "$VERSION_ID") | cut -d '.' -f1 || echo "unknown") + + # If RPM_DIST_VERSION is an empty string or it contains any nonnumeric + # characters, the value is unexpected so we set RPM_DIST_VERSION to 0. + if [ -z "$RPM_DIST_VERSION" ] || [ -n "$(echo "$RPM_DIST_VERSION" | tr -d '[0-9]')" ]; then + RPM_DIST_VERSION=0 + fi + + # Handle legacy RPM distributions if [ "$PYVER" -eq 26 ]; then - Bootstrap() { - BootstrapMessage "RedHat-based OSes that will use Python3" - BootstrapRpmPython3 - } + # Check if an automated bootstrap can be achieved on this system. + if ! Python36SclIsAvailable; then + INTERACTIVE_BOOTSTRAP=1 + fi + USE_PYTHON_3=1 - BOOTSTRAP_VERSION="BootstrapRpmPython3 $BOOTSTRAP_RPM_PYTHON3_VERSION" + + # Try now to enable SCL rh-python36 for systems already bootstrapped + # NB: EnablePython36SCL has been defined along with BootstrapRpmPython3Legacy in certbot-auto + EnablePython36SCL else - Bootstrap() { - BootstrapMessage "RedHat-based OSes" - BootstrapRpmCommon - } - BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION" + # Starting to Fedora 29, python2 is on a deprecation path. Let's move to python3 then. + # RHEL 8 also uses python3 by default. + if [ "$RPM_DIST_NAME" = "fedora" -a "$RPM_DIST_VERSION" -ge 29 ]; then + RPM_USE_PYTHON_3=1 + elif [ "$RPM_DIST_NAME" = "rhel" -a "$RPM_DIST_VERSION" -ge 8 ]; then + RPM_USE_PYTHON_3=1 + elif [ "$RPM_DIST_NAME" = "centos" -a "$RPM_DIST_VERSION" -ge 8 ]; then + RPM_USE_PYTHON_3=1 + else + RPM_USE_PYTHON_3=0 + fi + + if [ "$RPM_USE_PYTHON_3" = 1 ]; then + USE_PYTHON_3=1 + fi fi + LE_PYTHON="$prev_le_python" elif [ -f /etc/os-release ] && `grep -q openSUSE /etc/os-release` ; then - Bootstrap() { - BootstrapMessage "openSUSE-based OSes" - BootstrapSuseCommon - } - BOOTSTRAP_VERSION="BootstrapSuseCommon $BOOTSTRAP_SUSE_COMMON_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 elif [ -f /etc/arch-release ]; then - Bootstrap() { - if [ "$DEBUG" = 1 ]; then - BootstrapMessage "Archlinux" - BootstrapArchCommon - else - error "Please use pacman to install letsencrypt packages:" - error "# pacman -S certbot certbot-apache" - error - error "If you would like to use the virtualenv way, please run the script again with the" - error "--debug flag." - exit 1 - fi - } - BOOTSTRAP_VERSION="BootstrapArchCommon $BOOTSTRAP_ARCH_COMMON_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 elif [ -f /etc/manjaro-release ]; then - Bootstrap() { - ExperimentalBootstrap "Manjaro Linux" BootstrapArchCommon - } - BOOTSTRAP_VERSION="BootstrapArchCommon $BOOTSTRAP_ARCH_COMMON_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 elif [ -f /etc/gentoo-release ]; then - Bootstrap() { - DeprecationBootstrap "Gentoo" BootstrapGentooCommon - } - BOOTSTRAP_VERSION="BootstrapGentooCommon $BOOTSTRAP_GENTOO_COMMON_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 elif uname | grep -iq FreeBSD ; then - Bootstrap() { - DeprecationBootstrap "FreeBSD" BootstrapFreeBsd - } - BOOTSTRAP_VERSION="BootstrapFreeBsd $BOOTSTRAP_FREEBSD_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 elif uname | grep -iq Darwin ; then - Bootstrap() { - DeprecationBootstrap "macOS" BootstrapMac - } - BOOTSTRAP_VERSION="BootstrapMac $BOOTSTRAP_MAC_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 elif [ -f /etc/issue ] && grep -iq "Amazon Linux" /etc/issue ; then - Bootstrap() { - ExperimentalBootstrap "Amazon Linux" BootstrapRpmCommon - } - BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 elif [ -f /etc/product ] && grep -q "Joyent Instance" /etc/product ; then - Bootstrap() { - ExperimentalBootstrap "Joyent SmartOS Zone" BootstrapSmartOS - } - BOOTSTRAP_VERSION="BootstrapSmartOS $BOOTSTRAP_SMARTOS_VERSION" + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 else - Bootstrap() { - error "Sorry, I don't know how to bootstrap Certbot on your operating system!" - error - error "You will need to install OS dependencies, configure virtualenv, and run pip install manually." - error "Please see https://letsencrypt.readthedocs.org/en/latest/contributing.html#prerequisites" - error "for more info." - exit 1 - } + DEPRECATED_OS=1 + NO_SELF_UPGRADE=1 fi # We handle this case after determining the normal bootstrap version to allow @@ -871,6 +904,13 @@ if [ "$NO_BOOTSTRAP" = 1 ]; then unset BOOTSTRAP_VERSION fi +if [ "$DEPRECATED_OS" = 1 ]; then + Bootstrap() { + error "Skipping bootstrap because certbot-auto is deprecated on this system." + } + unset BOOTSTRAP_VERSION +fi + # Sets PREV_BOOTSTRAP_VERSION to the identifier for the bootstrap script used # to install OS dependencies on this system. PREV_BOOTSTRAP_VERSION isn't set # if it is unknown how OS dependencies were installed on this system. @@ -940,10 +980,158 @@ else: UNLIKELY_EOF } +# Create a new virtual environment for Certbot. It will overwrite any existing one. +# Parameters: LE_PYTHON, VENV_PATH, PYVER, VERBOSE +CreateVenv() { + "$1" - "$2" "$3" "$4" << "UNLIKELY_EOF" +#!/usr/bin/env python +import os +import shutil +import subprocess +import sys + + +def create_venv(venv_path, pyver, verbose): + if os.path.exists(venv_path): + shutil.rmtree(venv_path) + + stdout = sys.stdout if verbose == '1' else open(os.devnull, 'w') + + if int(pyver) <= 27: + # Use virtualenv binary + environ = os.environ.copy() + environ['VIRTUALENV_NO_DOWNLOAD'] = '1' + command = ['virtualenv', '--no-site-packages', '--python', sys.executable, venv_path] + subprocess.check_call(command, stdout=stdout, env=environ) + else: + # Use embedded venv module in Python 3 + command = [sys.executable, '-m', 'venv', venv_path] + subprocess.check_call(command, stdout=stdout) + + +if __name__ == '__main__': + create_venv(*sys.argv[1:]) + +UNLIKELY_EOF +} + +# Check that the given PATH_TO_CHECK has secured permissions. +# Parameters: LE_PYTHON, PATH_TO_CHECK +CheckPathPermissions() { + "$1" - "$2" << "UNLIKELY_EOF" +"""Verifies certbot-auto cannot be modified by unprivileged users. + +This script takes the path to certbot-auto as its only command line +argument. It then checks that the file can only be modified by uid/gid +< 1000 and if other users can modify the file, it prints a warning with +a suggestion on how to solve the problem. + +Permissions on symlinks in the absolute path of certbot-auto are ignored +and only the canonical path to certbot-auto is checked. There could be +permissions problems due to the symlinks that are unreported by this +script, however, issues like this were not caused by our documentation +and are ignored for the sake of simplicity. + +All warnings are printed to stdout rather than stderr so all stderr +output from this script can be suppressed to avoid printing messages if +this script fails for some reason. + +""" +from __future__ import print_function + +import os +import stat +import sys + + +FORUM_POST_URL = 'https://community.letsencrypt.org/t/certbot-auto-deployment-best-practices/91979/' + + +def has_safe_permissions(path): + """Returns True if the given path has secure permissions. + + The permissions are considered safe if the file is only writable by + uid/gid < 1000. + + The reason we allow more IDs than 0 is because on some systems such + as Debian, system users/groups other than uid/gid 0 are used for the + path we recommend in our instructions which is /usr/local/bin. 1000 + was chosen because on Debian 0-999 is reserved for system IDs[1] and + on RHEL either 0-499 or 0-999 is reserved depending on the + version[2][3]. Due to these differences across different OSes, this + detection isn't perfect so we only determine permissions are + insecure when we can be reasonably confident there is a problem + regardless of the underlying OS. + + [1] https://www.debian.org/doc/debian-policy/ch-opersys.html#uid-and-gid-classes + [2] https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/deployment_guide/ch-managing_users_and_groups + [3] https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/ch-managing_users_and_groups + + :param str path: filesystem path to check + :returns: True if the path has secure permissions, otherwise, False + :rtype: bool + + """ + # os.stat follows symlinks before obtaining information about a file. + stat_result = os.stat(path) + if stat_result.st_mode & stat.S_IWOTH: + return False + if stat_result.st_mode & stat.S_IWGRP and stat_result.st_gid >= 1000: + return False + if stat_result.st_mode & stat.S_IWUSR and stat_result.st_uid >= 1000: + return False + return True + + +def main(certbot_auto_path): + current_path = os.path.realpath(certbot_auto_path) + last_path = None + permissions_ok = True + # This loop makes use of the fact that os.path.dirname('/') == '/'. + while current_path != last_path and permissions_ok: + permissions_ok = has_safe_permissions(current_path) + last_path = current_path + current_path = os.path.dirname(current_path) + + if not permissions_ok: + print('{0} has insecure permissions!'.format(certbot_auto_path)) + print('To learn how to fix them, visit {0}'.format(FORUM_POST_URL)) + + +if __name__ == '__main__': + main(sys.argv[1]) + +UNLIKELY_EOF +} + if [ "$1" = "--le-auto-phase2" ]; then # Phase 2: Create venv, install LE, and run. shift 1 # the --le-auto-phase2 arg + + if [ "$DEPRECATED_OS" = 1 ]; then + # Phase 2 damage control mode for deprecated OSes. + # In this situation, we bypass any bootstrap or certbot venv setup. + error "Your system is not supported by certbot-auto anymore." + + if [ ! -d "$VENV_PATH" ] && OldVenvExists; then + VENV_BIN="$OLD_VENV_PATH/bin" + fi + + if [ -f "$VENV_BIN/letsencrypt" -a "$INSTALL_ONLY" != 1 ]; then + error "certbot-auto and its Certbot installation will no longer receive updates." + error "You will not receive any bug fixes including those fixing server compatibility" + error "or security problems." + error "Please visit https://certbot.eff.org/ to check for other alternatives." + "$VENV_BIN/letsencrypt" "$@" + exit 0 + else + error "Certbot cannot be installed." + error "Please visit https://certbot.eff.org/ to check for other alternatives." + exit 1 + fi + fi + SetPrevBootstrapVersion if [ -z "$PHASE_1_VERSION" -a "$USE_PYTHON_3" = 1 ]; then @@ -955,8 +1143,15 @@ if [ "$1" = "--le-auto-phase2" ]; then # If the selected Bootstrap function isn't a noop and it differs from the # previously used version if [ -n "$BOOTSTRAP_VERSION" -a "$BOOTSTRAP_VERSION" != "$PREV_BOOTSTRAP_VERSION" ]; then - # if non-interactive mode or stdin and stdout are connected to a terminal - if [ \( "$NONINTERACTIVE" = 1 \) -o \( \( -t 0 \) -a \( -t 1 \) \) ]; then + # Check if we can rebootstrap without manual user intervention: this requires that + # certbot-auto is in non-interactive mode AND selected bootstrap does not claim to + # require a manual user intervention. + if [ "$NONINTERACTIVE" = 1 -a "$INTERACTIVE_BOOTSTRAP" != 1 ]; then + CAN_REBOOTSTRAP=1 + fi + # Check if rebootstrap can be done non-interactively and current shell is non-interactive + # (true if stdin and stdout are not attached to a terminal). + if [ \( "$CAN_REBOOTSTRAP" = 1 \) -o \( \( -t 0 \) -a \( -t 1 \) \) ]; then if [ -d "$VENV_PATH" ]; then rm -rf "$VENV_PATH" fi @@ -967,12 +1162,21 @@ if [ "$1" = "--le-auto-phase2" ]; then ln -s "$VENV_PATH" "$OLD_VENV_PATH" fi RerunWithArgs "$@" + # Otherwise bootstrap needs to be done manually by the user. else - error "Skipping upgrade because new OS dependencies may need to be installed." - error - error "To upgrade to a newer version, please run this script again manually so you can" - error "approve changes or with --non-interactive on the command line to automatically" - error "install any required packages." + # If it is because bootstrapping is interactive, --non-interactive will be of no use. + if [ "$INTERACTIVE_BOOTSTRAP" = 1 ]; then + error "Skipping upgrade because new OS dependencies may need to be installed." + error "This requires manual user intervention: please run this script again manually." + # If this is because of the environment (eg. non interactive shell without + # --non-interactive flag set), help the user in that direction. + else + error "Skipping upgrade because new OS dependencies may need to be installed." + error + error "To upgrade to a newer version, please run this script again manually so you can" + error "approve changes or with --non-interactive on the command line to automatically" + error "install any required packages." + fi # Set INSTALLED_VERSION to be the same so we don't update the venv INSTALLED_VERSION="$LE_AUTO_VERSION" # Continue to use OLD_VENV_PATH if the new venv doesn't exist @@ -995,20 +1199,7 @@ if [ "$1" = "--le-auto-phase2" ]; then if [ "$LE_AUTO_VERSION" != "$INSTALLED_VERSION" ]; then say "Creating virtual environment..." DeterminePythonVersion - rm -rf "$VENV_PATH" - if [ "$PYVER" -le 27 ]; then - if [ "$VERBOSE" = 1 ]; then - virtualenv --no-site-packages --python "$LE_PYTHON" "$VENV_PATH" - else - virtualenv --no-site-packages --python "$LE_PYTHON" "$VENV_PATH" > /dev/null - fi - else - if [ "$VERBOSE" = 1 ]; then - "$LE_PYTHON" -m venv "$VENV_PATH" - else - "$LE_PYTHON" -m venv "$VENV_PATH" > /dev/null - fi - fi + CreateVenv "$LE_PYTHON" "$VENV_PATH" "$PYVER" "$VERBOSE" if [ -n "$BOOTSTRAP_VERSION" ]; then echo "$BOOTSTRAP_VERSION" > "$BOOTSTRAP_VERSION_PATH" @@ -1022,202 +1213,270 @@ if [ "$1" = "--le-auto-phase2" ]; then # There is no $ interpolation due to quotes on starting heredoc delimiter. # ------------------------------------------------------------------------- cat << "UNLIKELY_EOF" > "$TEMP_DIR/letsencrypt-auto-requirements.txt" -# This is the flattened list of packages certbot-auto installs. To generate -# this, do -# `pip install --no-cache-dir -e acme -e . -e certbot-apache -e certbot-nginx`, -# and then use `hashin` or a more secure method to gather the hashes. - -# Hashin example: +# This is the flattened list of packages certbot-auto installs. +# To generate this, do (with docker and package hashin installed): +# ``` +# letsencrypt-auto-source/rebuild_dependencies.py \ +# letsencrypt-auto-source/pieces/dependency-requirements.txt +# ``` +# If you want to update a single dependency, run commands similar to these: +# ``` # pip install hashin # hashin -r dependency-requirements.txt cryptography==1.5.2 -# sets the new certbot-auto pinned version of cryptography to 1.5.2 - -argparse==1.4.0 \ - --hash=sha256:c31647edb69fd3d465a847ea3157d37bed1f95f19760b11a47aa91c04b666314 \ - --hash=sha256:62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4 - -# This comes before cffi because cffi will otherwise install an unchecked -# version via setup_requires. -pycparser==2.14 \ - --hash=sha256:7959b4a74abdc27b312fed1c21e6caf9309ce0b29ea86b591fd2e99ecdf27f73 \ - --no-binary pycparser - -asn1crypto==0.22.0 \ - --hash=sha256:d232509fefcfcdb9a331f37e9c9dc20441019ad927c7d2176cf18ed5da0ba097 \ - --hash=sha256:cbbadd640d3165ab24b06ef25d1dca09a3441611ac15f6a6b452474fdf0aed1a -cffi==1.11.5 \ - --hash=sha256:1b0493c091a1898f1136e3f4f991a784437fac3673780ff9de3bcf46c80b6b50 \ - --hash=sha256:87f37fe5130574ff76c17cab61e7d2538a16f843bb7bca8ebbc4b12de3078596 \ - --hash=sha256:1553d1e99f035ace1c0544050622b7bc963374a00c467edafac50ad7bd276aef \ - --hash=sha256:151b7eefd035c56b2b2e1eb9963c90c6302dc15fbd8c1c0a83a163ff2c7d7743 \ - --hash=sha256:edabd457cd23a02965166026fd9bfd196f4324fe6032e866d0f3bd0301cd486f \ - --hash=sha256:ba5e697569f84b13640c9e193170e89c13c6244c24400fc57e88724ef610cd31 \ - --hash=sha256:79f9b6f7c46ae1f8ded75f68cf8ad50e5729ed4d590c74840471fc2823457d04 \ - --hash=sha256:b0f7d4a3df8f06cf49f9f121bead236e328074de6449866515cea4907bbc63d6 \ - --hash=sha256:4c91af6e967c2015729d3e69c2e51d92f9898c330d6a851bf8f121236f3defd3 \ - --hash=sha256:7a33145e04d44ce95bcd71e522b478d282ad0eafaf34fe1ec5bbd73e662f22b6 \ - --hash=sha256:95d5251e4b5ca00061f9d9f3d6fe537247e145a8524ae9fd30a2f8fbce993b5b \ - --hash=sha256:b75110fb114fa366b29a027d0c9be3709579602ae111ff61674d28c93606acca \ - --hash=sha256:ae5e35a2c189d397b91034642cb0eab0e346f776ec2eb44a49a459e6615d6e2e \ - --hash=sha256:fdf1c1dc5bafc32bc5d08b054f94d659422b05aba244d6be4ddc1c72d9aa70fb \ - --hash=sha256:9d1d3e63a4afdc29bd76ce6aa9d58c771cd1599fbba8cf5057e7860b203710dd \ - --hash=sha256:be2a9b390f77fd7676d80bc3cdc4f8edb940d8c198ed2d8c0be1319018c778e1 \ - --hash=sha256:ed01918d545a38998bfa5902c7c00e0fee90e957ce036a4000a88e3fe2264917 \ - --hash=sha256:857959354ae3a6fa3da6651b966d13b0a8bed6bbc87a0de7b38a549db1d2a359 \ - --hash=sha256:2ba8a45822b7aee805ab49abfe7eec16b90587f7f26df20c71dd89e45a97076f \ - --hash=sha256:a36c5c154f9d42ec176e6e620cb0dd275744aa1d804786a71ac37dc3661a5e95 \ - --hash=sha256:e55e22ac0a30023426564b1059b035973ec82186ddddbac867078435801c7801 \ - --hash=sha256:3eb6434197633b7748cea30bf0ba9f66727cdce45117a712b29a443943733257 \ - --hash=sha256:ecbb7b01409e9b782df5ded849c178a0aa7c906cf8c5a67368047daab282b184 \ - --hash=sha256:770f3782b31f50b68627e22f91cb182c48c47c02eb405fd689472aa7b7aa16dc \ - --hash=sha256:d5d8555d9bfc3f02385c1c37e9f998e2011f0db4f90e250e5bc0c0a85a813085 \ - --hash=sha256:3c85641778460581c42924384f5e68076d724ceac0f267d66c757f7535069c93 \ - --hash=sha256:ca1bd81f40adc59011f58159e4aa6445fc585a32bb8ac9badf7a2c1aa23822f2 \ - --hash=sha256:3bb6bd7266598f318063e584378b8e27c67de998a43362e8fce664c54ee52d30 \ - --hash=sha256:a6a5cb8809091ec9ac03edde9304b3ad82ad4466333432b16d78ef40e0cce0d5 \ - --hash=sha256:57b2533356cb2d8fac1555815929f7f5f14d68ac77b085d2326b571310f34f6e \ - --hash=sha256:495c5c2d43bf6cebe0178eb3e88f9c4aa48d8934aa6e3cddb865c058da76756b \ - --hash=sha256:e90f17980e6ab0f3c2f3730e56d1fe9bcba1891eeea58966e89d352492cc74f4 -ConfigArgParse==0.12.0 \ - --hash=sha256:28cd7d67669651f2a4518367838c49539457504584a139709b2b8f6c208ef339 \ - --no-binary ConfigArgParse +# ``` +ConfigArgParse==1.2.3 \ + --hash=sha256:edd17be986d5c1ba2e307150b8e5f5107aba125f3574dddd02c85d5cdcfd37dc +certifi==2020.4.5.1 \ + --hash=sha256:1d987a998c75633c40847cc966fcf5904906c920a7f17ef374f5aa4282abd304 \ + --hash=sha256:51fcb31174be6e6664c5f69e3e1691a2d72a1a12e90f872cbdb1567eb47b6519 +cffi==1.14.0 \ + --hash=sha256:001bf3242a1bb04d985d63e138230802c6c8d4db3668fb545fb5005ddf5bb5ff \ + --hash=sha256:00789914be39dffba161cfc5be31b55775de5ba2235fe49aa28c148236c4e06b \ + --hash=sha256:028a579fc9aed3af38f4892bdcc7390508adabc30c6af4a6e4f611b0c680e6ac \ + --hash=sha256:14491a910663bf9f13ddf2bc8f60562d6bc5315c1f09c704937ef17293fb85b0 \ + --hash=sha256:1cae98a7054b5c9391eb3249b86e0e99ab1e02bb0cc0575da191aedadbdf4384 \ + --hash=sha256:2089ed025da3919d2e75a4d963d008330c96751127dd6f73c8dc0c65041b4c26 \ + --hash=sha256:2d384f4a127a15ba701207f7639d94106693b6cd64173d6c8988e2c25f3ac2b6 \ + --hash=sha256:337d448e5a725bba2d8293c48d9353fc68d0e9e4088d62a9571def317797522b \ + --hash=sha256:399aed636c7d3749bbed55bc907c3288cb43c65c4389964ad5ff849b6370603e \ + --hash=sha256:3b911c2dbd4f423b4c4fcca138cadde747abdb20d196c4a48708b8a2d32b16dd \ + --hash=sha256:3d311bcc4a41408cf5854f06ef2c5cab88f9fded37a3b95936c9879c1640d4c2 \ + --hash=sha256:62ae9af2d069ea2698bf536dcfe1e4eed9090211dbaafeeedf5cb6c41b352f66 \ + --hash=sha256:66e41db66b47d0d8672d8ed2708ba91b2f2524ece3dee48b5dfb36be8c2f21dc \ + --hash=sha256:675686925a9fb403edba0114db74e741d8181683dcf216be697d208857e04ca8 \ + --hash=sha256:7e63cbcf2429a8dbfe48dcc2322d5f2220b77b2e17b7ba023d6166d84655da55 \ + --hash=sha256:8a6c688fefb4e1cd56feb6c511984a6c4f7ec7d2a1ff31a10254f3c817054ae4 \ + --hash=sha256:8c0ffc886aea5df6a1762d0019e9cb05f825d0eec1f520c51be9d198701daee5 \ + --hash=sha256:95cd16d3dee553f882540c1ffe331d085c9e629499ceadfbda4d4fde635f4b7d \ + --hash=sha256:99f748a7e71ff382613b4e1acc0ac83bf7ad167fb3802e35e90d9763daba4d78 \ + --hash=sha256:b8c78301cefcf5fd914aad35d3c04c2b21ce8629b5e4f4e45ae6812e461910fa \ + --hash=sha256:c420917b188a5582a56d8b93bdd8e0f6eca08c84ff623a4c16e809152cd35793 \ + --hash=sha256:c43866529f2f06fe0edc6246eb4faa34f03fe88b64a0a9a942561c8e22f4b71f \ + --hash=sha256:cab50b8c2250b46fe738c77dbd25ce017d5e6fb35d3407606e7a4180656a5a6a \ + --hash=sha256:cef128cb4d5e0b3493f058f10ce32365972c554572ff821e175dbc6f8ff6924f \ + --hash=sha256:cf16e3cf6c0a5fdd9bc10c21687e19d29ad1fe863372b5543deaec1039581a30 \ + --hash=sha256:e56c744aa6ff427a607763346e4170629caf7e48ead6921745986db3692f987f \ + --hash=sha256:e577934fc5f8779c554639376beeaa5657d54349096ef24abe8c74c5d9c117c3 \ + --hash=sha256:f2b0fa0c01d8a0c7483afd9f31d7ecf2d71760ca24499c8697aeb5ca37dc090c +chardet==3.0.4 \ + --hash=sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae \ + --hash=sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691 configobj==5.0.6 \ - --hash=sha256:a2f5650770e1c87fb335af19a9b7eb73fc05ccf22144eb68db7d00cd2bcb0902 \ - --no-binary configobj -cryptography==2.2.2 \ - --hash=sha256:3f3b65d5a16e6b52fba63dc860b62ca9832f51f1a2ae5083c78b6840275f12dd \ - --hash=sha256:5251e7de0de66810833606439ca65c9b9e45da62196b0c88bfadf27740aac09f \ - --hash=sha256:551a3abfe0c8c6833df4192a63371aa2ff43afd8f570ed345d31f251d78e7e04 \ - --hash=sha256:5cb990056b7cadcca26813311187ad751ea644712022a3976443691168781b6f \ - --hash=sha256:60bda7f12ecb828358be53095fc9c6edda7de8f1ef571f96c00b2363643fa3cd \ - --hash=sha256:64b5c67acc9a7c83fbb4b69166f3105a0ab722d27934fac2cb26456718eec2ba \ - --hash=sha256:6fef51ec447fe9f8351894024e94736862900d3a9aa2961528e602eb65c92bdb \ - --hash=sha256:77d0ad229d47a6e0272d00f6bf8ac06ce14715a9fd02c9a97f5a2869aab3ccb2 \ - --hash=sha256:808fe471b1a6b777f026f7dc7bd9a4959da4bfab64972f2bbe91e22527c1c037 \ - --hash=sha256:9b62fb4d18529c84b961efd9187fecbb48e89aa1a0f9f4161c61b7fc42a101bd \ - --hash=sha256:9e5bed45ec6b4f828866ac6a6bedf08388ffcfa68abe9e94b34bb40977aba531 \ - --hash=sha256:9fc295bf69130a342e7a19a39d7bbeb15c0bcaabc7382ec33ef3b2b7d18d2f63 \ - --hash=sha256:abd070b5849ed64e6d349199bef955ee0ad99aefbad792f0c587f8effa681a5e \ - --hash=sha256:ba6a774749b6e510cffc2fb98535f717e0e5fd91c7c99a61d223293df79ab351 \ - --hash=sha256:c332118647f084c983c6a3e1dba0f3bcb051f69d12baccac68db8d62d177eb8a \ - --hash=sha256:d6f46e862ee36df81e6342c2177ba84e70f722d9dc9c6c394f9f1f434c4a5563 \ - --hash=sha256:db6013746f73bf8edd9c3d1d3f94db635b9422f503db3fc5ef105233d4c011ab \ - --hash=sha256:f57008eaff597c69cf692c3518f6d4800f0309253bb138b526a37fe9ef0c7471 \ - --hash=sha256:f6c821ac253c19f2ad4c8691633ae1d1a17f120d5b01ea1d256d7b602bc59887 -enum34==1.1.2 ; python_version < '3.4' \ - --hash=sha256:2475d7fcddf5951e92ff546972758802de5260bf409319a9f1934e6bbc8b1dc7 \ - --hash=sha256:35907defb0f992b75ab7788f65fedc1cf20ffa22688e0e6f6f12afc06b3ea501 + --hash=sha256:a2f5650770e1c87fb335af19a9b7eb73fc05ccf22144eb68db7d00cd2bcb0902 +cryptography==2.8 \ + --hash=sha256:02079a6addc7b5140ba0825f542c0869ff4df9a69c360e339ecead5baefa843c \ + --hash=sha256:1df22371fbf2004c6f64e927668734070a8953362cd8370ddd336774d6743595 \ + --hash=sha256:369d2346db5934345787451504853ad9d342d7f721ae82d098083e1f49a582ad \ + --hash=sha256:3cda1f0ed8747339bbdf71b9f38ca74c7b592f24f65cdb3ab3765e4b02871651 \ + --hash=sha256:44ff04138935882fef7c686878e1c8fd80a723161ad6a98da31e14b7553170c2 \ + --hash=sha256:4b1030728872c59687badcca1e225a9103440e467c17d6d1730ab3d2d64bfeff \ + --hash=sha256:58363dbd966afb4f89b3b11dfb8ff200058fbc3b947507675c19ceb46104b48d \ + --hash=sha256:6ec280fb24d27e3d97aa731e16207d58bd8ae94ef6eab97249a2afe4ba643d42 \ + --hash=sha256:7270a6c29199adc1297776937a05b59720e8a782531f1f122f2eb8467f9aab4d \ + --hash=sha256:73fd30c57fa2d0a1d7a49c561c40c2f79c7d6c374cc7750e9ac7c99176f6428e \ + --hash=sha256:7f09806ed4fbea8f51585231ba742b58cbcfbfe823ea197d8c89a5e433c7e912 \ + --hash=sha256:90df0cc93e1f8d2fba8365fb59a858f51a11a394d64dbf3ef844f783844cc793 \ + --hash=sha256:971221ed40f058f5662a604bd1ae6e4521d84e6cad0b7b170564cc34169c8f13 \ + --hash=sha256:a518c153a2b5ed6b8cc03f7ae79d5ffad7315ad4569b2d5333a13c38d64bd8d7 \ + --hash=sha256:b0de590a8b0979649ebeef8bb9f54394d3a41f66c5584fff4220901739b6b2f0 \ + --hash=sha256:b43f53f29816ba1db8525f006fa6f49292e9b029554b3eb56a189a70f2a40879 \ + --hash=sha256:d31402aad60ed889c7e57934a03477b572a03af7794fa8fb1780f21ea8f6551f \ + --hash=sha256:de96157ec73458a7f14e3d26f17f8128c959084931e8997b9e655a39c8fde9f9 \ + --hash=sha256:df6b4dca2e11865e6cfbfb708e800efb18370f5a46fd601d3755bc7f85b3a8a2 \ + --hash=sha256:ecadccc7ba52193963c0475ac9f6fa28ac01e01349a2ca48509667ef41ffd2cf \ + --hash=sha256:fb81c17e0ebe3358486cd8cc3ad78adbae58af12fc2bf2bc0bb84e8090fa5ce8 +distro==1.5.0 \ + --hash=sha256:0e58756ae38fbd8fc3020d54badb8eae17c5b9dcbed388b17bb55b8a5928df92 \ + --hash=sha256:df74eed763e18d10d0da624258524ae80486432cd17392d9c3d96f5e83cd2799 +enum34==1.1.10; python_version < '3.4' \ + --hash=sha256:a98a201d6de3f2ab3db284e70a33b0f896fbf35f8086594e8c9e74b909058d53 \ + --hash=sha256:c3858660960c984d6ab0ebad691265180da2b43f07e061c0f8dca9ef3cffd328 \ + --hash=sha256:cce6a7477ed816bd2542d03d53db9f0db935dd013b70f336a95c73979289f248 funcsigs==1.0.2 \ --hash=sha256:330cc27ccbf7f1e992e69fef78261dc7c6569012cf397db8d3de0234e6c937ca \ --hash=sha256:a7bb0f2cf3a3fd1ab2732cb49eba4252c2af4240442415b4abce3b87022a8f50 -idna==2.5 \ - --hash=sha256:cc19709fd6d0cbfed39ea875d29ba6d4e22c0cebc510a76d6302a28385e8bb70 \ - --hash=sha256:3cb5ce08046c4e3a560fc02f138d0ac63e00f8ce5901a56b32ec8b7994082aab -ipaddress==1.0.16 \ - --hash=sha256:935712800ce4760701d89ad677666cd52691fd2f6f0b340c8b4239a3c17988a5 \ - --hash=sha256:5a3182b322a706525c46282ca6f064d27a02cffbd449f9f47416f1dc96aa71b0 -josepy==1.1.0 \ - --hash=sha256:1309a25aac3caeff5239729c58ff9b583f7d022ffdb1553406ddfc8e5b52b76e \ - --hash=sha256:fb5c62c77d26e04df29cb5ecd01b9ce69b6fcc9e521eb1ca193b7faa2afa7086 -linecache2==1.0.0 \ - --hash=sha256:e78be9c0a0dfcbac712fe04fbf92b96cddae80b1b842f24248214c8496f006ef \ - --hash=sha256:4b26ff4e7110db76eeb6f5a7b64a82623839d595c2038eeda662f2a2db78e97c -# Using an older version of mock here prevents regressions of #5276. +idna==2.9 \ + --hash=sha256:7588d1c14ae4c77d74036e8c22ff447b26d0fde8f007354fd48a7814db15b7cb \ + --hash=sha256:a068a21ceac8a4d63dbfd964670474107f541babbd2250d61922f029858365fa +ipaddress==1.0.23 \ + --hash=sha256:6e0f4a39e66cb5bb9a137b00276a2eff74f93b71dcbdad6f10ff7df9d3557fcc \ + --hash=sha256:b7f8e0369580bb4a24d5ba1d7cc29660a4a6987763faf1d8a8046830e020e7e2 +josepy==1.3.0 \ + --hash=sha256:c341ffa403399b18e9eae9012f804843045764d1390f9cb4648980a7569b1619 \ + --hash=sha256:e54882c64be12a2a76533f73d33cba9e331950fda9e2731e843490b774e7a01c mock==1.3.0 \ - --hash=sha256:3f573a18be94de886d1191f27c168427ef693e8dcfcecf95b170577b2eb69cbb \ - --hash=sha256:1e247dbecc6ce057299eb7ee019ad68314bb93152e81d9a6110d35f4d5eca0f6 -ordereddict==1.1 \ - --hash=sha256:1c35b4ac206cef2d24816c89f89cf289dd3d38cf7c449bb3fab7bf6d43f01b1f \ - --no-binary ordereddict -packaging==16.8 \ - --hash=sha256:99276dc6e3a7851f32027a68f1095cd3f77c148091b092ea867a351811cfe388 \ - --hash=sha256:5d50835fdf0a7edf0b55e311b7c887786504efea1177abd7e69329a8e5ea619e -parsedatetime==2.1 \ - --hash=sha256:ce9d422165cf6e963905cd5f74f274ebf7cc98c941916169178ef93f0e557838 \ - --hash=sha256:17c578775520c99131634e09cfca5a05ea9e1bd2a05cd06967ebece10df7af2d -pbr==1.8.1 \ - --hash=sha256:46c8db75ae75a056bd1cc07fa21734fe2e603d11a07833ecc1eeb74c35c72e0c \ - --hash=sha256:e2127626a91e6c885db89668976db31020f0af2da728924b56480fc7ccf09649 -pyOpenSSL==16.2.0 \ - --hash=sha256:26ca380ddf272f7556e48064bbcd5bd71f83dfc144f3583501c7ddbd9434ee17 \ - --hash=sha256:7779a3bbb74e79db234af6a08775568c6769b5821faecf6e2f4143edb227516e -pyparsing==2.1.8 \ - --hash=sha256:2f0f5ceb14eccd5aef809d6382e87df22ca1da583c79f6db01675ce7d7f49c18 \ - --hash=sha256:03a4869b9f3493807ee1f1cb405e6d576a1a2ca4d81a982677c0c1ad6177c56b \ - --hash=sha256:ab09aee814c0241ff0c503cff30018219fe1fc14501d89f406f4664a0ec9fbcd \ - --hash=sha256:6e9a7f052f8e26bcf749e4033e3115b6dc7e3c85aafcb794b9a88c9d9ef13c97 \ - --hash=sha256:9f463a6bcc4eeb6c08f1ed84439b17818e2085937c0dee0d7674ac127c67c12b \ - --hash=sha256:3626b4d81cfb300dad57f52f2f791caaf7b06c09b368c0aa7b868e53a5775424 \ - --hash=sha256:367b90cc877b46af56d4580cd0ae278062903f02b8204ab631f5a2c0f50adfd0 \ - --hash=sha256:9f1ea360086cd68681e7f4ca8f1f38df47bf81942a0d76a9673c2d23eff35b13 -pyRFC3339==1.0 \ - --hash=sha256:eea31835c56e2096af4363a5745a784878a61d043e247d3a6d6a0a32a9741f56 \ - --hash=sha256:8dfbc6c458b8daba1c0f3620a8c78008b323a268b27b7359e92a4ae41325f535 + --hash=sha256:1e247dbecc6ce057299eb7ee019ad68314bb93152e81d9a6110d35f4d5eca0f6 \ + --hash=sha256:3f573a18be94de886d1191f27c168427ef693e8dcfcecf95b170577b2eb69cbb +parsedatetime==2.5 \ + --hash=sha256:3b835fc54e472c17ef447be37458b400e3fefdf14bb1ffdedb5d2c853acf4ba1 \ + --hash=sha256:d2e9ddb1e463de871d32088a3f3cea3dc8282b1b2800e081bd0ef86900451667 +pbr==5.4.5 \ + --hash=sha256:07f558fece33b05caf857474a366dfcc00562bca13dd8b47b2b3e22d9f9bf55c \ + --hash=sha256:579170e23f8e0c2f24b0de612f71f648eccb79fb1322c814ae6b3c07b5ba23e8 +pyOpenSSL==19.1.0 \ + --hash=sha256:621880965a720b8ece2f1b2f54ea2071966ab00e2970ad2ce11d596102063504 \ + --hash=sha256:9a24494b2602aaf402be5c9e30a0b82d4a5c67528fe8fb475e3f3bc00dd69507 +pyRFC3339==1.1 \ + --hash=sha256:67196cb83b470709c580bb4738b83165e67c6cc60e1f2e4f286cfcb402a926f4 \ + --hash=sha256:81b8cbe1519cdb79bed04910dd6fa4e181faf8c88dff1e1b987b5f7ab23a5b1a +pycparser==2.20 \ + --hash=sha256:2d475327684562c3a96cc71adf7dc8c4f0565175cf86b6d7a404ff4c771f15f0 \ + --hash=sha256:7582ad22678f0fcd81102833f60ef8d0e57288b6b5fb00323d101be910e35705 +pyparsing==2.4.7 \ + --hash=sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1 \ + --hash=sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b python-augeas==0.5.0 \ - --hash=sha256:67d59d66cdba8d624e0389b87b2a83a176f21f16a87553b50f5703b23f29bac2 \ - --no-binary python-augeas -pytz==2015.7 \ - --hash=sha256:3abe6a6d3fc2fbbe4c60144211f45da2edbe3182a6f6511af6bbba0598b1f992 \ - --hash=sha256:939ef9c1e1224d980405689a97ffcf7828c56d1517b31d73464356c1f2b7769e \ - --hash=sha256:ead4aefa7007249e05e51b01095719d5a8dd95760089f5730aac5698b1932918 \ - --hash=sha256:3cca0df08bd0ed98432390494ce3ded003f5e661aa460be7a734bffe35983605 \ - --hash=sha256:3ede470d3d17ba3c07638dfa0d10452bc1b6e5ad326127a65ba77e6aaeb11bec \ - --hash=sha256:68c47964f7186eec306b13629627722b9079cd4447ed9e5ecaecd4eac84ca734 \ - --hash=sha256:dd5d3991950aae40a6c81de1578942e73d629808cefc51d12cd157980e6cfc18 \ - --hash=sha256:a77c52062c07eb7c7b30545dbc73e32995b7e117eea750317b5cb5c7a4618f14 \ - --hash=sha256:81af9aec4bc960a9a0127c488f18772dae4634689233f06f65443e7b11ebeb51 \ - --hash=sha256:e079b1dadc5c06246cc1bb6fe1b23a50b1d1173f2edd5104efd40bb73a28f406 \ - --hash=sha256:fbd26746772c24cb93c8b97cbdad5cb9e46c86bbdb1b9d8a743ee00e2fb1fc5d \ - --hash=sha256:99266ef30a37e43932deec2b7ca73e83c8dbc3b9ff703ec73eca6b1dae6befea \ - --hash=sha256:8b6ce1c993909783bc96e0b4f34ea223bff7a4df2c90bdb9c4e0f1ac928689e3 -requests==2.20.0 \ - --hash=sha256:99dcfdaaeb17caf6e526f32b6a7b780461512ab3f1d992187801694cba42770c \ - --hash=sha256:a84b8c9ab6239b578f22d1c21d51b696dcfe004032bb80ea832398d6909d7279 -six==1.10.0 \ - --hash=sha256:0ff78c403d9bccf5a425a6d31a12aa6b47f1c21ca4dc2573a7e2f32a97335eb1 \ - --hash=sha256:105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a -traceback2==1.4.0 \ - --hash=sha256:8253cebec4b19094d67cc5ed5af99bf1dba1285292226e98a31929f87a5d6b23 \ - --hash=sha256:05acc67a09980c2ecfedd3423f7ae0104839eccb55fc645773e1caa0951c3030 -unittest2==1.1.0 \ - --hash=sha256:13f77d0875db6d9b435e1d4f41e74ad4cc2eb6e1d5c824996092b3430f088bb8 \ - --hash=sha256:22882a0e418c284e1f718a822b3b022944d53d2d908e1690b319a9d3eb2c0579 -zope.component==4.2.2 \ - --hash=sha256:282c112b55dd8e3c869a3571f86767c150ab1284a9ace2bdec226c592acaf81a \ - --no-binary zope.component -zope.event==4.1.0 \ - --hash=sha256:dc7a59a2fd91730d3793131a5d261b29e93ec4e2a97f1bc487ce8defee2fe786 \ - --no-binary zope.event -zope.interface==4.1.3 \ - --hash=sha256:f07b631f7a601cd8cbd3332d54f43142c7088a83299f859356f08d1d4d4259b3 \ - --hash=sha256:de5cca083b9439d8002fb76bbe6b4998c5a5a721fab25b84298967f002df4c94 \ - --hash=sha256:6788416f7ea7f5b8a97be94825377aa25e8bdc73463e07baaf9858b29e737077 \ - --hash=sha256:6f3230f7254518201e5a3708cbb2de98c848304f06e3ded8bfb39e5825cba2e1 \ - --hash=sha256:5fa575a5240f04200c3088427d0d4b7b737f6e9018818a51d8d0f927a6a2517a \ - --hash=sha256:522194ad6a545735edd75c8a83f48d65d1af064e432a7d320d64f56bafc12e99 \ - --hash=sha256:e8c7b2d40943f71c99148c97f66caa7f5134147f57423f8db5b4825099ce9a09 \ - --hash=sha256:279024f0208601c3caa907c53876e37ad88625f7eaf1cb3842dbe360b2287017 \ - --hash=sha256:2e221a9eec7ccc58889a278ea13dcfed5ef939d80b07819a9a8b3cb1c681484f \ - --hash=sha256:69118965410ec86d44dc6b9017ee3ddbd582e0c0abeef62b3a19dbf6c8ad132b \ - --hash=sha256:d04df8686ec864d0cade8cf199f7f83aecd416109a20834d568f8310ded12dea \ - --hash=sha256:e75a947e15ee97e7e71e02ea302feb2fc62d3a2bb4668bf9dfbed43a506ac7e7 \ - --hash=sha256:4e45d22fb883222a5ab9f282a116fec5ee2e8d1a568ccff6a2d75bbd0eb6bcfc \ - --hash=sha256:bce9339bb3c7a55e0803b63d21c5839e8e479bc85c4adf42ae415b72f94facb2 \ - --hash=sha256:928138365245a0e8869a5999fbcc2a45475a0a6ed52a494d60dbdc540335fedd \ - --hash=sha256:0d841ba1bb840eea0e6489dc5ecafa6125554971f53b5acb87764441e61bceba \ - --hash=sha256:b09c8c1d47b3531c400e0195697f1414a63221de6ef478598a4f1460f7d9a392 -requests-toolbelt==0.8.0 \ - --hash=sha256:42c9c170abc2cacb78b8ab23ac957945c7716249206f90874651971a4acff237 \ - --hash=sha256:f6a531936c6fa4c6cfce1b9c10d5c4f498d16528d2a54a22ca00011205a187b5 -chardet==3.0.2 \ - --hash=sha256:4f7832e7c583348a9eddd927ee8514b3bf717c061f57b21dbe7697211454d9bb \ - --hash=sha256:6ebf56457934fdce01fb5ada5582762a84eed94cad43ed877964aebbdd8174c0 -urllib3==1.24.1 \ - --hash=sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39 \ - --hash=sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22 -certifi==2017.4.17 \ - --hash=sha256:f4318671072f030a33c7ca6acaef720ddd50ff124d1388e50c1bda4cbd6d7010 \ - --hash=sha256:f7527ebf7461582ce95f7a9e03dd141ce810d40590834f4ec20cddd54234c10a + --hash=sha256:67d59d66cdba8d624e0389b87b2a83a176f21f16a87553b50f5703b23f29bac2 +pytz==2020.1 \ + --hash=sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed \ + --hash=sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048 +requests==2.23.0 \ + --hash=sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee \ + --hash=sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6 +requests-toolbelt==0.9.1 \ + --hash=sha256:380606e1d10dc85c3bd47bf5a6095f815ec007be7a8b69c878507068df059e6f \ + --hash=sha256:968089d4584ad4ad7c171454f0a5c6dac23971e9472521ea3b6d49d610aa6fc0 +six==1.15.0 \ + --hash=sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259 \ + --hash=sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced +urllib3==1.25.9 \ + --hash=sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527 \ + --hash=sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115 +zope.component==4.6.1 \ + --hash=sha256:bfbe55d4a93e70a78b10edc3aad4de31bb8860919b7cbd8d66f717f7d7b279ac \ + --hash=sha256:d9c7c27673d787faff8a83797ce34d6ebcae26a370e25bddb465ac2182766aca +zope.deferredimport==4.3.1 \ + --hash=sha256:57b2345e7b5eef47efcd4f634ff16c93e4265de3dcf325afc7315ade48d909e1 \ + --hash=sha256:9a0c211df44aa95f1c4e6d2626f90b400f56989180d3ef96032d708da3d23e0a +zope.deprecation==4.4.0 \ + --hash=sha256:0d453338f04bacf91bbfba545d8bcdf529aa829e67b705eac8c1a7fdce66e2df \ + --hash=sha256:f1480b74995958b24ce37b0ef04d3663d2683e5d6debc96726eff18acf4ea113 +zope.event==4.4 \ + --hash=sha256:69c27debad9bdacd9ce9b735dad382142281ac770c4a432b533d6d65c4614bcf \ + --hash=sha256:d8e97d165fd5a0997b45f5303ae11ea3338becfe68c401dd88ffd2113fe5cae7 +zope.hookable==5.0.1 \ + --hash=sha256:0194b9b9e7f614abba60c90b231908861036578297515d3d6508eb10190f266d \ + --hash=sha256:0c2977473918bdefc6fa8dfb311f154e7f13c6133957fe649704deca79b92093 \ + --hash=sha256:17b8bdb3b77e03a152ca0d5ca185a7ae0156f5e5a2dbddf538676633a1f7380f \ + --hash=sha256:29d07681a78042cdd15b268ae9decffed9ace68a53eebeb61d65ae931d158841 \ + --hash=sha256:36fb1b35d1150267cb0543a1ddd950c0bc2c75ed0e6e92e3aaa6ac2e29416cb7 \ + --hash=sha256:3aed60c2bb5e812bbf9295c70f25b17ac37c233f30447a96c67913ba5073642f \ + --hash=sha256:3cac1565cc768911e72ca9ec4ddf5c5109e1fef0104f19f06649cf1874943b60 \ + --hash=sha256:3d4bc0cc4a37c3cd3081063142eeb2125511db3c13f6dc932d899c512690378e \ + --hash=sha256:3f73096f27b8c28be53ffb6604f7b570fbbb82f273c6febe5f58119009b59898 \ + --hash=sha256:522d1153d93f2d48aa0bd9fb778d8d4500be2e4dcf86c3150768f0e3adbbc4ef \ + --hash=sha256:523d2928fb7377bbdbc9af9c0b14ad73e6eaf226349f105733bdae27efd15b5a \ + --hash=sha256:5848309d4fc5c02150a45e8f8d2227e5bfda386a508bbd3160fed7c633c5a2fa \ + --hash=sha256:6781f86e6d54a110980a76e761eb54590630fd2af2a17d7edf02a079d2646c1d \ + --hash=sha256:6fd27921ebf3aaa945fa25d790f1f2046204f24dba4946f82f5f0a442577c3e9 \ + --hash=sha256:70d581862863f6bf9e175e85c9d70c2d7155f53fb04dcdb2f73cf288ca559a53 \ + --hash=sha256:81867c23b0dc66c8366f351d00923f2bc5902820a24c2534dfd7bf01a5879963 \ + --hash=sha256:81db29edadcbb740cd2716c95a297893a546ed89db1bfe9110168732d7f0afdd \ + --hash=sha256:86bd12624068cea60860a0759af5e2c3adc89c12aef6f71cf12f577e28deefe3 \ + --hash=sha256:9c184d8f9f7a76e1ced99855ccf390ffdd0ec3765e5cbf7b9cada600accc0a1e \ + --hash=sha256:acc789e8c29c13555e43fe4bf9fcd15a65512c9645e97bbaa5602e3201252b02 \ + --hash=sha256:afaa740206b7660d4cc3b8f120426c85761f51379af7a5b05451f624ad12b0af \ + --hash=sha256:b5f5fa323f878bb16eae68ea1ba7f6c0419d4695d0248bed4b18f51d7ce5ab85 \ + --hash=sha256:bd89e0e2c67bf4ac3aca2a19702b1a37269fb1923827f68324ac2e7afd6e3406 \ + --hash=sha256:c212de743283ec0735db24ec6ad913758df3af1b7217550ff270038062afd6ae \ + --hash=sha256:ca553f524293a0bdea05e7f44c3e685e4b7b022cb37d87bc4a3efa0f86587a8d \ + --hash=sha256:cab67065a3db92f636128d3157cc5424a145f82d96fb47159c539132833a6d36 \ + --hash=sha256:d3b3b3eedfdbf6b02898216e85aa6baf50207f4378a2a6803d6d47650cd37031 \ + --hash=sha256:d9f4a5a72f40256b686d31c5c0b1fde503172307beb12c1568296e76118e402c \ + --hash=sha256:df5067d87aaa111ed5d050e1ee853ba284969497f91806efd42425f5348f1c06 \ + --hash=sha256:e2587644812c6138f05b8a41594a8337c6790e3baf9a01915e52438c13fc6bef \ + --hash=sha256:e27fd877662db94f897f3fd532ef211ca4901eb1a70ba456f15c0866a985464a \ + --hash=sha256:e427ebbdd223c72e06ba94c004bb04e996c84dec8a0fa84e837556ae145c439e \ + --hash=sha256:e583ad4309c203ef75a09d43434cf9c2b4fa247997ecb0dcad769982c39411c7 \ + --hash=sha256:e760b2bc8ece9200804f0c2b64d10147ecaf18455a2a90827fbec4c9d84f3ad5 \ + --hash=sha256:ea9a9cc8bcc70e18023f30fa2f53d11ae069572a162791224e60cd65df55fb69 \ + --hash=sha256:ecb3f17dce4803c1099bd21742cd126b59817a4e76a6544d31d2cca6e30dbffd \ + --hash=sha256:ed794e3b3de42486d30444fb60b5561e724ee8a2d1b17b0c2e0f81e3ddaf7a87 \ + --hash=sha256:ee885d347279e38226d0a437b6a932f207f691c502ee565aba27a7022f1285df \ + --hash=sha256:fd5e7bc5f24f7e3d490698f7b854659a9851da2187414617cd5ed360af7efd63 \ + --hash=sha256:fe45f6870f7588ac7b2763ff1ce98cce59369717afe70cc353ec5218bc854bcc +zope.interface==5.1.0 \ + --hash=sha256:0103cba5ed09f27d2e3de7e48bb320338592e2fabc5ce1432cf33808eb2dfd8b \ + --hash=sha256:14415d6979356629f1c386c8c4249b4d0082f2ea7f75871ebad2e29584bd16c5 \ + --hash=sha256:1ae4693ccee94c6e0c88a4568fb3b34af8871c60f5ba30cf9f94977ed0e53ddd \ + --hash=sha256:1b87ed2dc05cb835138f6a6e3595593fea3564d712cb2eb2de963a41fd35758c \ + --hash=sha256:269b27f60bcf45438e8683269f8ecd1235fa13e5411de93dae3b9ee4fe7f7bc7 \ + --hash=sha256:27d287e61639d692563d9dab76bafe071fbeb26818dd6a32a0022f3f7ca884b5 \ + --hash=sha256:39106649c3082972106f930766ae23d1464a73b7d30b3698c986f74bf1256a34 \ + --hash=sha256:40e4c42bd27ed3c11b2c983fecfb03356fae1209de10686d03c02c8696a1d90e \ + --hash=sha256:461d4339b3b8f3335d7e2c90ce335eb275488c587b61aca4b305196dde2ff086 \ + --hash=sha256:4f98f70328bc788c86a6a1a8a14b0ea979f81ae6015dd6c72978f1feff70ecda \ + --hash=sha256:558a20a0845d1a5dc6ff87cd0f63d7dac982d7c3be05d2ffb6322a87c17fa286 \ + --hash=sha256:562dccd37acec149458c1791da459f130c6cf8902c94c93b8d47c6337b9fb826 \ + --hash=sha256:5e86c66a6dea8ab6152e83b0facc856dc4d435fe0f872f01d66ce0a2131b7f1d \ + --hash=sha256:60a207efcd8c11d6bbeb7862e33418fba4e4ad79846d88d160d7231fcb42a5ee \ + --hash=sha256:645a7092b77fdbc3f68d3cc98f9d3e71510e419f54019d6e282328c0dd140dcd \ + --hash=sha256:6874367586c020705a44eecdad5d6b587c64b892e34305bb6ed87c9bbe22a5e9 \ + --hash=sha256:74bf0a4f9091131de09286f9a605db449840e313753949fe07c8d0fe7659ad1e \ + --hash=sha256:7b726194f938791a6691c7592c8b9e805fc6d1b9632a833b9c0640828cd49cbc \ + --hash=sha256:8149ded7f90154fdc1a40e0c8975df58041a6f693b8f7edcd9348484e9dc17fe \ + --hash=sha256:8cccf7057c7d19064a9e27660f5aec4e5c4001ffcf653a47531bde19b5aa2a8a \ + --hash=sha256:911714b08b63d155f9c948da2b5534b223a1a4fc50bb67139ab68b277c938578 \ + --hash=sha256:a5f8f85986197d1dd6444763c4a15c991bfed86d835a1f6f7d476f7198d5f56a \ + --hash=sha256:a744132d0abaa854d1aad50ba9bc64e79c6f835b3e92521db4235a1991176813 \ + --hash=sha256:af2c14efc0bb0e91af63d00080ccc067866fb8cbbaca2b0438ab4105f5e0f08d \ + --hash=sha256:b054eb0a8aa712c8e9030065a59b5e6a5cf0746ecdb5f087cca5ec7685690c19 \ + --hash=sha256:b0becb75418f8a130e9d465e718316cd17c7a8acce6fe8fe07adc72762bee425 \ + --hash=sha256:b1d2ed1cbda2ae107283befd9284e650d840f8f7568cb9060b5466d25dc48975 \ + --hash=sha256:ba4261c8ad00b49d48bbb3b5af388bb7576edfc0ca50a49c11dcb77caa1d897e \ + --hash=sha256:d1fe9d7d09bb07228650903d6a9dc48ea649e3b8c69b1d263419cc722b3938e8 \ + --hash=sha256:d7804f6a71fc2dda888ef2de266727ec2f3915373d5a785ed4ddc603bbc91e08 \ + --hash=sha256:da2844fba024dd58eaa712561da47dcd1e7ad544a257482392472eae1c86d5e5 \ + --hash=sha256:dcefc97d1daf8d55199420e9162ab584ed0893a109f45e438b9794ced44c9fd0 \ + --hash=sha256:dd98c436a1fc56f48c70882cc243df89ad036210d871c7427dc164b31500dc11 \ + --hash=sha256:e74671e43ed4569fbd7989e5eecc7d06dc134b571872ab1d5a88f4a123814e9f \ + --hash=sha256:eb9b92f456ff3ec746cd4935b73c1117538d6124b8617bc0fe6fda0b3816e345 \ + --hash=sha256:ebb4e637a1fb861c34e48a00d03cffa9234f42bef923aec44e5625ffb9a8e8f9 \ + --hash=sha256:ef739fe89e7f43fb6494a43b1878a36273e5924869ba1d866f752c5812ae8d58 \ + --hash=sha256:f40db0e02a8157d2b90857c24d89b6310f9b6c3642369852cdc3b5ac49b92afc \ + --hash=sha256:f68bf937f113b88c866d090fea0bc52a098695173fc613b055a17ff0cf9683b6 \ + --hash=sha256:fb55c182a3f7b84c1a2d6de5fa7b1a05d4660d866b91dbf8d74549c57a1499e8 +zope.proxy==4.3.5 \ + --hash=sha256:00573dfa755d0703ab84bb23cb6ecf97bb683c34b340d4df76651f97b0bab068 \ + --hash=sha256:092049280f2848d2ba1b57b71fe04881762a220a97b65288bcb0968bb199ec30 \ + --hash=sha256:0cbd27b4d3718b5ec74fc65ffa53c78d34c65c6fd9411b8352d2a4f855220cf1 \ + --hash=sha256:17fc7e16d0c81f833a138818a30f366696653d521febc8e892858041c4d88785 \ + --hash=sha256:19577dfeb70e8a67249ba92c8ad20589a1a2d86a8d693647fa8385408a4c17b0 \ + --hash=sha256:207aa914576b1181597a1516e1b90599dc690c095343ae281b0772e44945e6a4 \ + --hash=sha256:219a7db5ed53e523eb4a4769f13105118b6d5b04ed169a283c9775af221e231f \ + --hash=sha256:2b50ea79849e46b5f4f2b0247a3687505d32d161eeb16a75f6f7e6cd81936e43 \ + --hash=sha256:5903d38362b6c716e66bbe470f190579c530a5baf03dbc8500e5c2357aa569a5 \ + --hash=sha256:5c24903675e271bd688c6e9e7df5775ac6b168feb87dbe0e4bcc90805f21b28f \ + --hash=sha256:5ef6bc5ed98139e084f4e91100f2b098a0cd3493d4e76f9d6b3f7b95d7ad0f06 \ + --hash=sha256:61b55ae3c23a126a788b33ffb18f37d6668e79a05e756588d9e4d4be7246ab1c \ + --hash=sha256:63ddb992931a5e616c87d3d89f5a58db086e617548005c7f9059fac68c03a5cc \ + --hash=sha256:6943da9c09870490dcfd50c4909c0cc19f434fa6948f61282dc9cb07bcf08160 \ + --hash=sha256:6ad40f85c1207803d581d5d75e9ea25327cd524925699a83dfc03bf8e4ba72b7 \ + --hash=sha256:6b44433a79bdd7af0e3337bd7bbcf53dd1f9b0fa66bf21bcb756060ce32a96c1 \ + --hash=sha256:6bbaa245015d933a4172395baad7874373f162955d73612f0b66b6c2c33b6366 \ + --hash=sha256:7007227f4ea85b40a2f5e5a244479f6a6dfcf906db9b55e812a814a8f0e2c28d \ + --hash=sha256:74884a0aec1f1609190ec8b34b5d58fb3b5353cf22b96161e13e0e835f13518f \ + --hash=sha256:7d25fe5571ddb16369054f54cdd883f23de9941476d97f2b92eb6d7d83afe22d \ + --hash=sha256:7e162bdc5e3baad26b2262240be7d2bab36991d85a6a556e48b9dfb402370261 \ + --hash=sha256:814d62678dc3a30f4aa081982d830b7c342cf230ffc9d030b020cb154eeebf9e \ + --hash=sha256:8878a34c5313ee52e20aa50b03138af8d472bae465710fb954d133a9bfd3c38d \ + --hash=sha256:a66a0d94e5b081d5d695e66d6667e91e74d79e273eee95c1747717ba9cb70792 \ + --hash=sha256:a69f5cbf4addcfdf03dda564a671040127a6b7c34cf9fe4973582e68441b63fa \ + --hash=sha256:b00f9f0c334d07709d3f73a7cb8ae63c6ca1a90c790a63b5e7effa666ef96021 \ + --hash=sha256:b6ed71e4a7b4690447b626f499d978aa13197a0e592950e5d7020308f6054698 \ + --hash=sha256:bdf5041e5851526e885af579d2f455348dba68d74f14a32781933569a327fddf \ + --hash=sha256:be034360dd34e62608419f86e799c97d389c10a0e677a25f236a971b2f40dac9 \ + --hash=sha256:cc8f590a5eed30b314ae6b0232d925519ade433f663de79cc3783e4b10d662ba \ + --hash=sha256:cd7a318a15fe6cc4584bf3c4426f092ed08c0fd012cf2a9173114234fe193e11 \ + --hash=sha256:cf19b5f63a59c20306e034e691402b02055c8f4e38bf6792c23cad489162a642 \ + --hash=sha256:cfc781ce442ec407c841e9aa51d0e1024f72b6ec34caa8fdb6ef9576d549acf2 \ + --hash=sha256:dea9f6f8633571e18bc20cad83603072e697103a567f4b0738d52dd0211b4527 \ + --hash=sha256:e4a86a1d5eb2cce83c5972b3930c7c1eac81ab3508464345e2b8e54f119d5505 \ + --hash=sha256:e7106374d4a74ed9ff00c46cc00f0a9f06a0775f8868e423f85d4464d2333679 \ + --hash=sha256:e98a8a585b5668aa9e34d10f7785abf9545fe72663b4bfc16c99a115185ae6a5 \ + --hash=sha256:f64840e68483316eb58d82c376ad3585ca995e69e33b230436de0cdddf7363f9 \ + --hash=sha256:f8f4b0a9e6683e43889852130595c8854d8ae237f2324a053cdd884de936aa9b \ + --hash=sha256:fc45a53219ed30a7f670a6d8c98527af0020e6fd4ee4c0a8fb59f147f06d816c # Contains the requirements for the letsencrypt package. # @@ -1230,18 +1489,18 @@ letsencrypt==0.7.0 \ --hash=sha256:105a5fb107e45bcd0722eb89696986dcf5f08a86a321d6aef25a0c7c63375ade \ --hash=sha256:c36e532c486a7e92155ee09da54b436a3c420813ec1c590b98f635d924720de9 -certbot==0.30.0 \ - --hash=sha256:b3468e128e74d2295598f6d3fbf9d0edfb67fe5abaca3b985a9e858395bd027f \ - --hash=sha256:d631fe6c75700ce9b2fdae194ff8b53c7518545d87dd451a1704f7572dcd49e8 -acme==0.30.0 \ - --hash=sha256:eed9389f802ebf4988c9e43c28ad3d5c2734237371d78e97450a1d61189a15aa \ - --hash=sha256:984b6d00bec73dcfa616636a760e80ca14bd246fb908710a656547f542f09445 -certbot-apache==0.30.0 \ - --hash=sha256:d38c70fc6930db298ea992a3145362eebdce460d3d2651f86a8f2f43d838c6d0 \ - --hash=sha256:1d4bc207d53a3e5d37e5d9ebd05f26089aa21d1fbf384113ed9d1829b4d1e9bf -certbot-nginx==0.30.0 \ - --hash=sha256:6163c7d0080f59b4ebe510afcc6af2d2eebf15469275c3835866690db4d465d6 \ - --hash=sha256:e39a3f3d77cd4c653949cf066fb2211039fd2032665697c27b6e8501c7c2dd92 +certbot==1.14.0 \ + --hash=sha256:67b4d26ceaea6c7f8325d0d45169e7a165a2cabc7122c84bc971ba068ca19cca \ + --hash=sha256:959ea90c6bb8dca38eab9772722cb940972ef6afcd5f15deef08b3c3636841eb +acme==1.14.0 \ + --hash=sha256:4f48c41261202f1a389ec2986b2580b58f53e0d5a1ae2463b34318d78b87fc66 \ + --hash=sha256:61daccfb0343628cbbca551a7fc4c82482113952c21db3fe0c585b7c98fa1c35 +certbot-apache==1.14.0 \ + --hash=sha256:b757038db23db707c44630fecb46e99172bd791f0db5a8e623c0842613c4d3d9 \ + --hash=sha256:887fe4a21af2de1e5c2c9428bacba6eb7c1219257bc70f1a1d8447c8a321adb0 +certbot-nginx==1.14.0 \ + --hash=sha256:8916a815437988d6c192df9f035bb7a176eab20eee0956677b335d0698d243fb \ + --hash=sha256:cc2a8a0de56d9bb6b2efbda6c80c647dad8db2bb90675cac03ade94bd5fc8597 UNLIKELY_EOF # ------------------------------------------------------------------------- @@ -1271,7 +1530,6 @@ from distutils.version import StrictVersion from hashlib import sha256 from os import environ from os.path import join -from pipes import quote from shutil import rmtree try: from subprocess import check_output @@ -1291,7 +1549,7 @@ except ImportError: cmd = popenargs[0] raise CalledProcessError(retcode, cmd) return output -from sys import exit, version_info +import sys from tempfile import mkdtemp try: from urllib2 import build_opener, HTTPHandler, HTTPSHandler @@ -1313,18 +1571,23 @@ maybe_argparse = ( [('18/dd/e617cfc3f6210ae183374cd9f6a26b20514bbb5a792af97949c5aacddf0f/' 'argparse-1.4.0.tar.gz', '62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4')] - if version_info < (2, 7, 0) else []) + if sys.version_info < (2, 7, 0) else []) +# Be careful when updating the pinned versions here, in particular for pip. +# Indeed starting from 10.0, pip will build dependencies in isolation if the +# related projects are compliant with PEP 517. This is not something we want +# as of now, so the isolation build will need to be disabled wherever +# pipstrap is used (see https://github.com/certbot/certbot/issues/8256). PACKAGES = maybe_argparse + [ # Pip has no dependencies, as it vendors everything: ('11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/' 'pip-{0}.tar.gz'.format(PIP_VERSION), '09f243e1a7b461f654c26a725fa373211bb7ff17a9300058b205c61658ca940d'), # This version of setuptools has only optional dependencies: - ('59/88/2f3990916931a5de6fa9706d6d75eb32ee8b78627bb2abaab7ed9e6d0622/' - 'setuptools-29.0.1.tar.gz', - 'b539118819a4857378398891fa5366e090690e46b3e41421a1e07d6e9fd8feb0'), + ('37/1b/b25507861991beeade31473868463dad0e58b1978c209de27384ae541b0b/' + 'setuptools-40.6.3.zip', + '3b474dad69c49f0d2d86696b68105f3a6f195f7ab655af12ef9a9c326d2b08f8'), ('c9/1d/bd19e691fd4cfe908c76c429fe6e4436c9e83583c4414b54f6c85471954a/' 'wheel-0.29.0.tar.gz', '1ebb8ad7e26b448e9caa4773d2357849bf80ff9e313964bcaf79cbf0201a1648') @@ -1394,11 +1657,9 @@ def get_index_base(): def main(): - pip_version = StrictVersion(check_output(['pip', '--version']) + python = sys.executable or 'python' + pip_version = StrictVersion(check_output([python, '-m', 'pip', '--version']) .decode('utf-8').split()[1]) - min_pip_version = StrictVersion(PIP_VERSION) - if pip_version >= min_pip_version: - return 0 has_pip_cache = pip_version >= StrictVersion('6.0') index_base = get_index_base() temp = mkdtemp(prefix='pipstrap-') @@ -1407,12 +1668,12 @@ def main(): temp, digest) for path, digest in PACKAGES] - check_output('pip install --no-index --no-deps -U ' + - # Disable cache since we're not using it and it otherwise - # sometimes throws permission warnings: - ('--no-cache-dir ' if has_pip_cache else '') + - ' '.join(quote(d) for d in downloads), - shell=True) + # Calling pip as a module is the preferred way to avoid problems about pip self-upgrade. + command = [python, '-m', 'pip', 'install', '--no-index', '--no-deps', '-U'] + # Disable cache since it is not used and it otherwise sometimes throws permission warnings: + command.extend(['--no-cache-dir'] if has_pip_cache else []) + command.extend(downloads) + check_output(command) except HashError as exc: print(exc) except Exception: @@ -1425,7 +1686,7 @@ def main(): if __name__ == '__main__': - exit(main()) + sys.exit(main()) UNLIKELY_EOF # ------------------------------------------------------------------------- @@ -1478,6 +1739,9 @@ UNLIKELY_EOF say "Installation succeeded." fi + # If you're modifying any of the code after this point in this current `if` block, you + # may need to update the "$DEPRECATED_OS" = 1 case at the beginning of phase 2 as well. + if [ "$INSTALL_ONLY" = 1 ]; then say "Certbot is installed." exit 0 @@ -1509,6 +1773,24 @@ else exit 0 fi + DeterminePythonVersion "NOCRASH" + # Don't warn about file permissions if the user disabled the check or we + # can't find an up-to-date Python. + if [ "$PYVER" -ge "$MIN_PYVER" -a "$NO_PERMISSIONS_CHECK" != 1 ]; then + # If the script fails for some reason, don't break certbot-auto. + set +e + # Suppress unexpected error output. + CHECK_PERM_OUT=$(CheckPathPermissions "$LE_PYTHON" "$0" 2>/dev/null) + CHECK_PERM_STATUS="$?" + set -e + # Only print output if the script ran successfully and it actually produced + # output. The latter check resolves + # https://github.com/certbot/certbot/issues/7012. + if [ "$CHECK_PERM_STATUS" = 0 -a -n "$CHECK_PERM_OUT" ]; then + error "$CHECK_PERM_OUT" + fi + fi + if [ "$NO_SELF_UPGRADE" != 1 ]; then TEMP_DIR=$(TempDir) trap 'rm -rf "$TEMP_DIR"' EXIT @@ -1665,37 +1947,41 @@ if __name__ == '__main__': UNLIKELY_EOF # --------------------------------------------------------------------------- - DeterminePythonVersion "NOCRASH" if [ "$PYVER" -lt "$MIN_PYVER" ]; then error "WARNING: couldn't find Python $MIN_PYTHON_VERSION+ to check for updates." elif ! REMOTE_VERSION=`"$LE_PYTHON" "$TEMP_DIR/fetch.py" --latest-version` ; then error "WARNING: unable to check for updates." fi - LE_VERSION_STATE=`CompareVersions "$LE_PYTHON" "$LE_AUTO_VERSION" "$REMOTE_VERSION"` - if [ "$LE_VERSION_STATE" = "UNOFFICIAL" ]; then - say "Unofficial certbot-auto version detected, self-upgrade is disabled: $LE_AUTO_VERSION" - elif [ "$LE_VERSION_STATE" = "OUTDATED" ]; then - say "Upgrading certbot-auto $LE_AUTO_VERSION to $REMOTE_VERSION..." - - # Now we drop into Python so we don't have to install even more - # dependencies (curl, etc.), for better flow control, and for the option of - # future Windows compatibility. - "$LE_PYTHON" "$TEMP_DIR/fetch.py" --le-auto-script "v$REMOTE_VERSION" - - # Install new copy of certbot-auto. - # TODO: Deal with quotes in pathnames. - say "Replacing certbot-auto..." - # Clone permissions with cp. chmod and chown don't have a --reference - # option on macOS or BSD, and stat -c on Linux is stat -f on macOS and BSD: - cp -p "$0" "$TEMP_DIR/letsencrypt-auto.permission-clone" - cp "$TEMP_DIR/letsencrypt-auto" "$TEMP_DIR/letsencrypt-auto.permission-clone" - # Using mv rather than cp leaves the old file descriptor pointing to the - # original copy so the shell can continue to read it unmolested. mv across - # filesystems is non-atomic, doing `rm dest, cp src dest, rm src`, but the - # cp is unlikely to fail if the rm doesn't. - mv -f "$TEMP_DIR/letsencrypt-auto.permission-clone" "$0" - fi # A newer version is available. + # If for any reason REMOTE_VERSION is not set, let's assume certbot-auto is up-to-date, + # and do not go into the self-upgrading process. + if [ -n "$REMOTE_VERSION" ]; then + LE_VERSION_STATE=`CompareVersions "$LE_PYTHON" "$LE_AUTO_VERSION" "$REMOTE_VERSION"` + + if [ "$LE_VERSION_STATE" = "UNOFFICIAL" ]; then + say "Unofficial certbot-auto version detected, self-upgrade is disabled: $LE_AUTO_VERSION" + elif [ "$LE_VERSION_STATE" = "OUTDATED" ]; then + say "Upgrading certbot-auto $LE_AUTO_VERSION to $REMOTE_VERSION..." + + # Now we drop into Python so we don't have to install even more + # dependencies (curl, etc.), for better flow control, and for the option of + # future Windows compatibility. + "$LE_PYTHON" "$TEMP_DIR/fetch.py" --le-auto-script "v$REMOTE_VERSION" + + # Install new copy of certbot-auto. + # TODO: Deal with quotes in pathnames. + say "Replacing certbot-auto..." + # Clone permissions with cp. chmod and chown don't have a --reference + # option on macOS or BSD, and stat -c on Linux is stat -f on macOS and BSD: + cp -p "$0" "$TEMP_DIR/letsencrypt-auto.permission-clone" + cp "$TEMP_DIR/letsencrypt-auto" "$TEMP_DIR/letsencrypt-auto.permission-clone" + # Using mv rather than cp leaves the old file descriptor pointing to the + # original copy so the shell can continue to read it unmolested. mv across + # filesystems is non-atomic, doing `rm dest, cp src dest, rm src`, but the + # cp is unlikely to fail if the rm doesn't. + mv -f "$TEMP_DIR/letsencrypt-auto.permission-clone" "$0" + fi # A newer version is available. + fi fi # Self-upgrading is allowed. RerunWithArgs --le-auto-phase2 "$@" diff --git a/letsencrypt-auto-source/letsencrypt-auto.sig b/letsencrypt-auto-source/letsencrypt-auto.sig index e4902c0ee72..b297bdfb351 100644 Binary files a/letsencrypt-auto-source/letsencrypt-auto.sig and b/letsencrypt-auto-source/letsencrypt-auto.sig differ diff --git a/letsencrypt-auto-source/letsencrypt-auto.template b/letsencrypt-auto-source/letsencrypt-auto.template deleted file mode 100755 index f431e32e49f..00000000000 --- a/letsencrypt-auto-source/letsencrypt-auto.template +++ /dev/null @@ -1,696 +0,0 @@ -#!/bin/sh -# -# Download and run the latest release version of the Certbot client. -# -# NOTE: THIS SCRIPT IS AUTO-GENERATED AND SELF-UPDATING -# -# IF YOU WANT TO EDIT IT LOCALLY, *ALWAYS* RUN YOUR COPY WITH THE -# "--no-self-upgrade" FLAG -# -# IF YOU WANT TO SEND PULL REQUESTS, THE REAL SOURCE FOR THIS FILE IS -# letsencrypt-auto-source/letsencrypt-auto.template AND -# letsencrypt-auto-source/pieces/bootstrappers/* - -set -e # Work even if somebody does "sh thisscript.sh". - -# Note: you can set XDG_DATA_HOME or VENV_PATH before running this script, -# if you want to change where the virtual environment will be installed - -# HOME might not be defined when being run through something like systemd -if [ -z "$HOME" ]; then - HOME=~root -fi -if [ -z "$XDG_DATA_HOME" ]; then - XDG_DATA_HOME=~/.local/share -fi -if [ -z "$VENV_PATH" ]; then - # We export these values so they are preserved properly if this script is - # rerun with sudo/su where $HOME/$XDG_DATA_HOME may have a different value. - export OLD_VENV_PATH="$XDG_DATA_HOME/letsencrypt" - export VENV_PATH="/opt/eff.org/certbot/venv" -fi -VENV_BIN="$VENV_PATH/bin" -BOOTSTRAP_VERSION_PATH="$VENV_PATH/certbot-auto-bootstrap-version.txt" -LE_AUTO_VERSION="{{ LE_AUTO_VERSION }}" -BASENAME=$(basename $0) -USAGE="Usage: $BASENAME [OPTIONS] -A self-updating wrapper script for the Certbot ACME client. When run, updates -to both this script and certbot will be downloaded and installed. After -ensuring you have the latest versions installed, certbot will be invoked with -all arguments you have provided. - -Help for certbot itself cannot be provided until it is installed. - - --debug attempt experimental installation - -h, --help print this help - -n, --non-interactive, --noninteractive run without asking for user input - --no-bootstrap do not install OS dependencies - --no-self-upgrade do not download updates - --os-packages-only install OS dependencies and exit - --install-only install certbot, upgrade if needed, and exit - -v, --verbose provide more output - -q, --quiet provide only update/error output; - implies --non-interactive - -All arguments are accepted and forwarded to the Certbot client when run." -export CERTBOT_AUTO="$0" - -for arg in "$@" ; do - case "$arg" in - --debug) - DEBUG=1;; - --os-packages-only) - OS_PACKAGES_ONLY=1;; - --install-only) - INSTALL_ONLY=1;; - --no-self-upgrade) - # Do not upgrade this script (also prevents client upgrades, because each - # copy of the script pins a hash of the python client) - NO_SELF_UPGRADE=1;; - --no-bootstrap) - NO_BOOTSTRAP=1;; - --help) - HELP=1;; - --noninteractive|--non-interactive) - NONINTERACTIVE=1;; - --quiet) - QUIET=1;; - renew) - ASSUME_YES=1;; - --verbose) - VERBOSE=1;; - -[!-]*) - OPTIND=1 - while getopts ":hnvq" short_arg $arg; do - case "$short_arg" in - h) - HELP=1;; - n) - NONINTERACTIVE=1;; - q) - QUIET=1;; - v) - VERBOSE=1;; - esac - done;; - esac -done - -if [ $BASENAME = "letsencrypt-auto" ]; then - # letsencrypt-auto does not respect --help or --yes for backwards compatibility - NONINTERACTIVE=1 - HELP=0 -fi - -# Set ASSUME_YES to 1 if QUIET or NONINTERACTIVE -if [ "$QUIET" = 1 -o "$NONINTERACTIVE" = 1 ]; then - ASSUME_YES=1 -fi - -say() { - if [ "$QUIET" != 1 ]; then - echo "$@" - fi -} - -error() { - echo "$@" -} - -# Support for busybox and others where there is no "command", -# but "which" instead -if command -v command > /dev/null 2>&1 ; then - export EXISTS="command -v" -elif which which > /dev/null 2>&1 ; then - export EXISTS="which" -else - error "Cannot find command nor which... please install one!" - exit 1 -fi - -# Certbot itself needs root access for almost all modes of operation. -# certbot-auto needs root access to bootstrap OS dependencies and install -# Certbot at a protected path so it can be safely run as root. To accomplish -# this, this script will attempt to run itself as root if it doesn't have the -# necessary privileges by using `sudo` or falling back to `su` if it is not -# available. The mechanism used to obtain root access can be set explicitly by -# setting the environment variable LE_AUTO_SUDO to 'sudo', 'su', 'su_sudo', -# 'SuSudo', or '' as used below. - -# Because the parameters in `su -c` has to be a string, -# we need to properly escape it. -SuSudo() { - args="" - # This `while` loop iterates over all parameters given to this function. - # For each parameter, all `'` will be replace by `'"'"'`, and the escaped string - # will be wrapped in a pair of `'`, then appended to `$args` string - # For example, `echo "It's only 1\$\!"` will be escaped to: - # 'echo' 'It'"'"'s only 1$!' - # │ │└┼┘│ - # │ │ │ └── `'s only 1$!'` the literal string - # │ │ └── `\"'\"` is a single quote (as a string) - # │ └── `'It'`, to be concatenated with the strings following it - # └── `echo` wrapped in a pair of `'`, it's totally fine for the shell command itself - while [ $# -ne 0 ]; do - args="$args'$(printf "%s" "$1" | sed -e "s/'/'\"'\"'/g")' " - shift - done - su root -c "$args" -} - -# Sets the environment variable SUDO to be the name of the program or function -# to call to get root access. If this script already has root privleges, SUDO -# is set to an empty string. The value in SUDO should be run with the command -# to called with root privileges as arguments. -SetRootAuthMechanism() { - SUDO="" - if [ -n "${LE_AUTO_SUDO+x}" ]; then - case "$LE_AUTO_SUDO" in - SuSudo|su_sudo|su) - SUDO=SuSudo - ;; - sudo) - SUDO="sudo -E" - ;; - '') ;; # Nothing to do for plain root method. - *) - error "Error: unknown root authorization mechanism '$LE_AUTO_SUDO'." - exit 1 - esac - say "Using preset root authorization mechanism '$LE_AUTO_SUDO'." - else - if test "`id -u`" -ne "0" ; then - if $EXISTS sudo 1>/dev/null 2>&1; then - SUDO="sudo -E" - else - say \"sudo\" is not available, will use \"su\" for installation steps... - SUDO=SuSudo - fi - fi - fi -} - -if [ "$1" = "--cb-auto-has-root" ]; then - shift 1 -else - SetRootAuthMechanism - if [ -n "$SUDO" ]; then - say "Requesting to rerun $0 with root privileges..." - $SUDO "$0" --cb-auto-has-root "$@" - exit 0 - fi -fi - -# Runs this script again with the given arguments. --cb-auto-has-root is added -# to the command line arguments to ensure we don't try to acquire root a -# second time. After the script is rerun, we exit the current script. -RerunWithArgs() { - "$0" --cb-auto-has-root "$@" - exit 0 -} - -BootstrapMessage() { - # Arguments: Platform name - say "Bootstrapping dependencies for $1... (you can skip this with --no-bootstrap)" -} - -ExperimentalBootstrap() { - # Arguments: Platform name, bootstrap function name - if [ "$DEBUG" = 1 ]; then - if [ "$2" != "" ]; then - BootstrapMessage $1 - $2 - fi - else - error "FATAL: $1 support is very experimental at present..." - error "if you would like to work on improving it, please ensure you have backups" - error "and then run this script again with the --debug flag!" - error "Alternatively, you can install OS dependencies yourself and run this script" - error "again with --no-bootstrap." - exit 1 - fi -} - -DeprecationBootstrap() { - # Arguments: Platform name, bootstrap function name - if [ "$DEBUG" = 1 ]; then - if [ "$2" != "" ]; then - BootstrapMessage $1 - $2 - fi - else - error "WARNING: certbot-auto support for this $1 is DEPRECATED!" - error "Please visit certbot.eff.org to learn how to download a version of" - error "Certbot that is packaged for your system. While an existing version" - error "of certbot-auto may work currently, we have stopped supporting updating" - error "system packages for your system. Please switch to a packaged version" - error "as soon as possible." - exit 1 - fi -} - -MIN_PYTHON_VERSION="2.7" -MIN_PYVER=$(echo "$MIN_PYTHON_VERSION" | sed 's/\.//') -# Sets LE_PYTHON to Python version string and PYVER to the first two -# digits of the python version -DeterminePythonVersion() { - # Arguments: "NOCRASH" if we shouldn't crash if we don't find a good python - # - # If no Python is found, PYVER is set to 0. - if [ "$USE_PYTHON_3" = 1 ]; then - for LE_PYTHON in "$LE_PYTHON" python3; do - # Break (while keeping the LE_PYTHON value) if found. - $EXISTS "$LE_PYTHON" > /dev/null && break - done - else - for LE_PYTHON in "$LE_PYTHON" python2.7 python27 python2 python; do - # Break (while keeping the LE_PYTHON value) if found. - $EXISTS "$LE_PYTHON" > /dev/null && break - done - fi - if [ "$?" != "0" ]; then - if [ "$1" != "NOCRASH" ]; then - error "Cannot find any Pythons; please install one!" - exit 1 - else - PYVER=0 - return 0 - fi - fi - - PYVER=`"$LE_PYTHON" -V 2>&1 | cut -d" " -f 2 | cut -d. -f1,2 | sed 's/\.//'` - if [ "$PYVER" -lt "$MIN_PYVER" ]; then - if [ "$1" != "NOCRASH" ]; then - error "You have an ancient version of Python entombed in your operating system..." - error "This isn't going to work; you'll need at least version $MIN_PYTHON_VERSION." - exit 1 - fi - fi -} - -{{ bootstrappers/deb_common.sh }} -{{ bootstrappers/rpm_common_base.sh }} -{{ bootstrappers/rpm_common.sh }} -{{ bootstrappers/rpm_python3.sh }} -{{ bootstrappers/suse_common.sh }} -{{ bootstrappers/arch_common.sh }} -{{ bootstrappers/gentoo_common.sh }} -{{ bootstrappers/free_bsd.sh }} -{{ bootstrappers/mac.sh }} -{{ bootstrappers/smartos.sh }} -{{ bootstrappers/mageia_common.sh }} - -# Set Bootstrap to the function that installs OS dependencies on this system -# and BOOTSTRAP_VERSION to the unique identifier for the current version of -# that function. If Bootstrap is set to a function that doesn't install any -# packages BOOTSTRAP_VERSION is not set. -if [ -f /etc/debian_version ]; then - Bootstrap() { - BootstrapMessage "Debian-based OSes" - BootstrapDebCommon - } - BOOTSTRAP_VERSION="BootstrapDebCommon $BOOTSTRAP_DEB_COMMON_VERSION" -elif [ -f /etc/mageia-release ]; then - # Mageia has both /etc/mageia-release and /etc/redhat-release - Bootstrap() { - ExperimentalBootstrap "Mageia" BootstrapMageiaCommon - } - BOOTSTRAP_VERSION="BootstrapMageiaCommon $BOOTSTRAP_MAGEIA_COMMON_VERSION" -elif [ -f /etc/redhat-release ]; then - # Run DeterminePythonVersion to decide on the basis of available Python versions - # whether to use 2.x or 3.x on RedHat-like systems. - # Then, revert LE_PYTHON to its previous state. - prev_le_python="$LE_PYTHON" - unset LE_PYTHON - DeterminePythonVersion "NOCRASH" - if [ "$PYVER" -eq 26 ]; then - Bootstrap() { - BootstrapMessage "RedHat-based OSes that will use Python3" - BootstrapRpmPython3 - } - USE_PYTHON_3=1 - BOOTSTRAP_VERSION="BootstrapRpmPython3 $BOOTSTRAP_RPM_PYTHON3_VERSION" - else - Bootstrap() { - BootstrapMessage "RedHat-based OSes" - BootstrapRpmCommon - } - BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION" - fi - LE_PYTHON="$prev_le_python" -elif [ -f /etc/os-release ] && `grep -q openSUSE /etc/os-release` ; then - Bootstrap() { - BootstrapMessage "openSUSE-based OSes" - BootstrapSuseCommon - } - BOOTSTRAP_VERSION="BootstrapSuseCommon $BOOTSTRAP_SUSE_COMMON_VERSION" -elif [ -f /etc/arch-release ]; then - Bootstrap() { - if [ "$DEBUG" = 1 ]; then - BootstrapMessage "Archlinux" - BootstrapArchCommon - else - error "Please use pacman to install letsencrypt packages:" - error "# pacman -S certbot certbot-apache" - error - error "If you would like to use the virtualenv way, please run the script again with the" - error "--debug flag." - exit 1 - fi - } - BOOTSTRAP_VERSION="BootstrapArchCommon $BOOTSTRAP_ARCH_COMMON_VERSION" -elif [ -f /etc/manjaro-release ]; then - Bootstrap() { - ExperimentalBootstrap "Manjaro Linux" BootstrapArchCommon - } - BOOTSTRAP_VERSION="BootstrapArchCommon $BOOTSTRAP_ARCH_COMMON_VERSION" -elif [ -f /etc/gentoo-release ]; then - Bootstrap() { - DeprecationBootstrap "Gentoo" BootstrapGentooCommon - } - BOOTSTRAP_VERSION="BootstrapGentooCommon $BOOTSTRAP_GENTOO_COMMON_VERSION" -elif uname | grep -iq FreeBSD ; then - Bootstrap() { - DeprecationBootstrap "FreeBSD" BootstrapFreeBsd - } - BOOTSTRAP_VERSION="BootstrapFreeBsd $BOOTSTRAP_FREEBSD_VERSION" -elif uname | grep -iq Darwin ; then - Bootstrap() { - DeprecationBootstrap "macOS" BootstrapMac - } - BOOTSTRAP_VERSION="BootstrapMac $BOOTSTRAP_MAC_VERSION" -elif [ -f /etc/issue ] && grep -iq "Amazon Linux" /etc/issue ; then - Bootstrap() { - ExperimentalBootstrap "Amazon Linux" BootstrapRpmCommon - } - BOOTSTRAP_VERSION="BootstrapRpmCommon $BOOTSTRAP_RPM_COMMON_VERSION" -elif [ -f /etc/product ] && grep -q "Joyent Instance" /etc/product ; then - Bootstrap() { - ExperimentalBootstrap "Joyent SmartOS Zone" BootstrapSmartOS - } - BOOTSTRAP_VERSION="BootstrapSmartOS $BOOTSTRAP_SMARTOS_VERSION" -else - Bootstrap() { - error "Sorry, I don't know how to bootstrap Certbot on your operating system!" - error - error "You will need to install OS dependencies, configure virtualenv, and run pip install manually." - error "Please see https://letsencrypt.readthedocs.org/en/latest/contributing.html#prerequisites" - error "for more info." - exit 1 - } -fi - -# We handle this case after determining the normal bootstrap version to allow -# variables like USE_PYTHON_3 to be properly set. As described above, if the -# Bootstrap function doesn't install any packages, BOOTSTRAP_VERSION should not -# be set so we unset it here. -if [ "$NO_BOOTSTRAP" = 1 ]; then - Bootstrap() { - : - } - unset BOOTSTRAP_VERSION -fi - -# Sets PREV_BOOTSTRAP_VERSION to the identifier for the bootstrap script used -# to install OS dependencies on this system. PREV_BOOTSTRAP_VERSION isn't set -# if it is unknown how OS dependencies were installed on this system. -SetPrevBootstrapVersion() { - if [ -f $BOOTSTRAP_VERSION_PATH ]; then - PREV_BOOTSTRAP_VERSION=$(cat "$BOOTSTRAP_VERSION_PATH") - # The list below only contains bootstrap version strings that existed before - # we started writing them to disk. - # - # DO NOT MODIFY THIS LIST UNLESS YOU KNOW WHAT YOU'RE DOING! - elif grep -Fqx "$BOOTSTRAP_VERSION" << "UNLIKELY_EOF" -BootstrapDebCommon 1 -BootstrapMageiaCommon 1 -BootstrapRpmCommon 1 -BootstrapSuseCommon 1 -BootstrapArchCommon 1 -BootstrapGentooCommon 1 -BootstrapFreeBsd 1 -BootstrapMac 1 -BootstrapSmartOS 1 -UNLIKELY_EOF - then - # If there's no bootstrap version saved to disk, but the currently selected - # bootstrap script is from before we started saving the version number, - # return the currently selected version to prevent us from rebootstrapping - # unnecessarily. - PREV_BOOTSTRAP_VERSION="$BOOTSTRAP_VERSION" - fi -} - -TempDir() { - mktemp -d 2>/dev/null || mktemp -d -t 'le' # Linux || macOS -} - -# Returns 0 if a letsencrypt installation exists at $OLD_VENV_PATH, otherwise, -# returns a non-zero number. -OldVenvExists() { - [ -n "$OLD_VENV_PATH" -a -f "$OLD_VENV_PATH/bin/letsencrypt" ] -} - -# Given python path, version 1 and version 2, check if version 1 is outdated compared to version 2. -# An unofficial version provided as version 1 (eg. 0.28.0.dev0) will be treated -# specifically by printing "UNOFFICIAL". Otherwise, print "OUTDATED" if version 1 -# is outdated, and "UP_TO_DATE" if not. -# This function relies only on installed python environment (2.x or 3.x) by certbot-auto. -CompareVersions() { - "$1" - "$2" "$3" << "UNLIKELY_EOF" -import sys -from distutils.version import StrictVersion - -try: - current = StrictVersion(sys.argv[1]) -except ValueError: - sys.stdout.write('UNOFFICIAL') - sys.exit() - -try: - remote = StrictVersion(sys.argv[2]) -except ValueError: - sys.stdout.write('UP_TO_DATE') - sys.exit() - -if current < remote: - sys.stdout.write('OUTDATED') -else: - sys.stdout.write('UP_TO_DATE') -UNLIKELY_EOF -} - -if [ "$1" = "--le-auto-phase2" ]; then - # Phase 2: Create venv, install LE, and run. - - shift 1 # the --le-auto-phase2 arg - SetPrevBootstrapVersion - - if [ -z "$PHASE_1_VERSION" -a "$USE_PYTHON_3" = 1 ]; then - unset LE_PYTHON - fi - - INSTALLED_VERSION="none" - if [ -d "$VENV_PATH" ] || OldVenvExists; then - # If the selected Bootstrap function isn't a noop and it differs from the - # previously used version - if [ -n "$BOOTSTRAP_VERSION" -a "$BOOTSTRAP_VERSION" != "$PREV_BOOTSTRAP_VERSION" ]; then - # if non-interactive mode or stdin and stdout are connected to a terminal - if [ \( "$NONINTERACTIVE" = 1 \) -o \( \( -t 0 \) -a \( -t 1 \) \) ]; then - if [ -d "$VENV_PATH" ]; then - rm -rf "$VENV_PATH" - fi - # In the case the old venv was just a symlink to the new one, - # OldVenvExists is now false because we deleted the venv at VENV_PATH. - if OldVenvExists; then - rm -rf "$OLD_VENV_PATH" - ln -s "$VENV_PATH" "$OLD_VENV_PATH" - fi - RerunWithArgs "$@" - else - error "Skipping upgrade because new OS dependencies may need to be installed." - error - error "To upgrade to a newer version, please run this script again manually so you can" - error "approve changes or with --non-interactive on the command line to automatically" - error "install any required packages." - # Set INSTALLED_VERSION to be the same so we don't update the venv - INSTALLED_VERSION="$LE_AUTO_VERSION" - # Continue to use OLD_VENV_PATH if the new venv doesn't exist - if [ ! -d "$VENV_PATH" ]; then - VENV_BIN="$OLD_VENV_PATH/bin" - fi - fi - elif [ -f "$VENV_BIN/letsencrypt" ]; then - # --version output ran through grep due to python-cryptography DeprecationWarnings - # grep for both certbot and letsencrypt until certbot and shim packages have been released - INSTALLED_VERSION=$("$VENV_BIN/letsencrypt" --version 2>&1 | grep "^certbot\|^letsencrypt" | cut -d " " -f 2) - if [ -z "$INSTALLED_VERSION" ]; then - error "Error: couldn't get currently installed version for $VENV_BIN/letsencrypt: " 1>&2 - "$VENV_BIN/letsencrypt" --version - exit 1 - fi - fi - fi - - if [ "$LE_AUTO_VERSION" != "$INSTALLED_VERSION" ]; then - say "Creating virtual environment..." - DeterminePythonVersion - rm -rf "$VENV_PATH" - if [ "$PYVER" -le 27 ]; then - if [ "$VERBOSE" = 1 ]; then - virtualenv --no-site-packages --python "$LE_PYTHON" "$VENV_PATH" - else - virtualenv --no-site-packages --python "$LE_PYTHON" "$VENV_PATH" > /dev/null - fi - else - if [ "$VERBOSE" = 1 ]; then - "$LE_PYTHON" -m venv "$VENV_PATH" - else - "$LE_PYTHON" -m venv "$VENV_PATH" > /dev/null - fi - fi - - if [ -n "$BOOTSTRAP_VERSION" ]; then - echo "$BOOTSTRAP_VERSION" > "$BOOTSTRAP_VERSION_PATH" - elif [ -n "$PREV_BOOTSTRAP_VERSION" ]; then - echo "$PREV_BOOTSTRAP_VERSION" > "$BOOTSTRAP_VERSION_PATH" - fi - - say "Installing Python packages..." - TEMP_DIR=$(TempDir) - trap 'rm -rf "$TEMP_DIR"' EXIT - # There is no $ interpolation due to quotes on starting heredoc delimiter. - # ------------------------------------------------------------------------- - cat << "UNLIKELY_EOF" > "$TEMP_DIR/letsencrypt-auto-requirements.txt" -{{ dependency-requirements.txt }} -{{ letsencrypt-requirements.txt }} -{{ certbot-requirements.txt }} -UNLIKELY_EOF - # ------------------------------------------------------------------------- - cat << "UNLIKELY_EOF" > "$TEMP_DIR/pipstrap.py" -{{ pipstrap.py }} -UNLIKELY_EOF - # ------------------------------------------------------------------------- - # Set PATH so pipstrap upgrades the right (v)env: - PATH="$VENV_BIN:$PATH" "$VENV_BIN/python" "$TEMP_DIR/pipstrap.py" - set +e - if [ "$VERBOSE" = 1 ]; then - "$VENV_BIN/pip" install --disable-pip-version-check --no-cache-dir --require-hashes -r "$TEMP_DIR/letsencrypt-auto-requirements.txt" - else - PIP_OUT=`"$VENV_BIN/pip" install --disable-pip-version-check --no-cache-dir --require-hashes -r "$TEMP_DIR/letsencrypt-auto-requirements.txt" 2>&1` - fi - PIP_STATUS=$? - set -e - if [ "$PIP_STATUS" != 0 ]; then - # Report error. (Otherwise, be quiet.) - error "Had a problem while installing Python packages." - if [ "$VERBOSE" != 1 ]; then - error - error "pip prints the following errors: " - error "=====================================================" - error "$PIP_OUT" - error "=====================================================" - error - error "Certbot has problem setting up the virtual environment." - - if `echo $PIP_OUT | grep -q Killed` || `echo $PIP_OUT | grep -q "allocate memory"` ; then - error - error "Based on your pip output, the problem can likely be fixed by " - error "increasing the available memory." - else - error - error "We were not be able to guess the right solution from your pip " - error "output." - fi - - error - error "Consult https://certbot.eff.org/docs/install.html#problems-with-python-virtual-environment" - error "for possible solutions." - error "You may also find some support resources at https://certbot.eff.org/support/ ." - fi - rm -rf "$VENV_PATH" - exit 1 - fi - - if [ -d "$OLD_VENV_PATH" -a ! -L "$OLD_VENV_PATH" ]; then - rm -rf "$OLD_VENV_PATH" - ln -s "$VENV_PATH" "$OLD_VENV_PATH" - fi - - say "Installation succeeded." - fi - - if [ "$INSTALL_ONLY" = 1 ]; then - say "Certbot is installed." - exit 0 - fi - - "$VENV_BIN/letsencrypt" "$@" - -else - # Phase 1: Upgrade certbot-auto if necessary, then self-invoke. - # - # Each phase checks the version of only the thing it is responsible for - # upgrading. Phase 1 checks the version of the latest release of - # certbot-auto (which is always the same as that of the certbot - # package). Phase 2 checks the version of the locally installed certbot. - export PHASE_1_VERSION="$LE_AUTO_VERSION" - - if [ ! -f "$VENV_BIN/letsencrypt" ]; then - if ! OldVenvExists; then - if [ "$HELP" = 1 ]; then - echo "$USAGE" - exit 0 - fi - # If it looks like we've never bootstrapped before, bootstrap: - Bootstrap - fi - fi - if [ "$OS_PACKAGES_ONLY" = 1 ]; then - say "OS packages installed." - exit 0 - fi - - if [ "$NO_SELF_UPGRADE" != 1 ]; then - TEMP_DIR=$(TempDir) - trap 'rm -rf "$TEMP_DIR"' EXIT - # --------------------------------------------------------------------------- - cat << "UNLIKELY_EOF" > "$TEMP_DIR/fetch.py" -{{ fetch.py }} -UNLIKELY_EOF - # --------------------------------------------------------------------------- - DeterminePythonVersion "NOCRASH" - if [ "$PYVER" -lt "$MIN_PYVER" ]; then - error "WARNING: couldn't find Python $MIN_PYTHON_VERSION+ to check for updates." - elif ! REMOTE_VERSION=`"$LE_PYTHON" "$TEMP_DIR/fetch.py" --latest-version` ; then - error "WARNING: unable to check for updates." - fi - - LE_VERSION_STATE=`CompareVersions "$LE_PYTHON" "$LE_AUTO_VERSION" "$REMOTE_VERSION"` - if [ "$LE_VERSION_STATE" = "UNOFFICIAL" ]; then - say "Unofficial certbot-auto version detected, self-upgrade is disabled: $LE_AUTO_VERSION" - elif [ "$LE_VERSION_STATE" = "OUTDATED" ]; then - say "Upgrading certbot-auto $LE_AUTO_VERSION to $REMOTE_VERSION..." - - # Now we drop into Python so we don't have to install even more - # dependencies (curl, etc.), for better flow control, and for the option of - # future Windows compatibility. - "$LE_PYTHON" "$TEMP_DIR/fetch.py" --le-auto-script "v$REMOTE_VERSION" - - # Install new copy of certbot-auto. - # TODO: Deal with quotes in pathnames. - say "Replacing certbot-auto..." - # Clone permissions with cp. chmod and chown don't have a --reference - # option on macOS or BSD, and stat -c on Linux is stat -f on macOS and BSD: - cp -p "$0" "$TEMP_DIR/letsencrypt-auto.permission-clone" - cp "$TEMP_DIR/letsencrypt-auto" "$TEMP_DIR/letsencrypt-auto.permission-clone" - # Using mv rather than cp leaves the old file descriptor pointing to the - # original copy so the shell can continue to read it unmolested. mv across - # filesystems is non-atomic, doing `rm dest, cp src dest, rm src`, but the - # cp is unlikely to fail if the rm doesn't. - mv -f "$TEMP_DIR/letsencrypt-auto.permission-clone" "$0" - fi # A newer version is available. - fi # Self-upgrading is allowed. - - RerunWithArgs --le-auto-phase2 "$@" -fi diff --git a/letsencrypt-auto-source/pieces/bootstrappers/arch_common.sh b/letsencrypt-auto-source/pieces/bootstrappers/arch_common.sh deleted file mode 100755 index 3be78d3f852..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/arch_common.sh +++ /dev/null @@ -1,37 +0,0 @@ -# If new packages are installed by BootstrapArchCommon below, this version -# number must be increased. -BOOTSTRAP_ARCH_COMMON_VERSION=1 - -BootstrapArchCommon() { - # Tested with: - # - ArchLinux (x86_64) - # - # "python-virtualenv" is Python3, but "python2-virtualenv" provides - # only "virtualenv2" binary, not "virtualenv". - - deps=" - python2 - python-virtualenv - gcc - augeas - openssl - libffi - ca-certificates - pkg-config - " - - # pacman -T exits with 127 if there are missing dependencies - missing=$(pacman -T $deps) || true - - if [ "$ASSUME_YES" = 1 ]; then - noconfirm="--noconfirm" - fi - - if [ "$missing" ]; then - if [ "$QUIET" = 1 ]; then - pacman -S --needed $missing $noconfirm > /dev/null - else - pacman -S --needed $missing $noconfirm - fi - fi -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/deb_common.sh b/letsencrypt-auto-source/pieces/bootstrappers/deb_common.sh deleted file mode 100644 index eb22225e417..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/deb_common.sh +++ /dev/null @@ -1,119 +0,0 @@ -# If new packages are installed by BootstrapDebCommon below, this version -# number must be increased. -BOOTSTRAP_DEB_COMMON_VERSION=1 - -BootstrapDebCommon() { - # Current version tested with: - # - # - Ubuntu - # - 14.04 (x64) - # - 15.04 (x64) - # - Debian - # - 7.9 "wheezy" (x64) - # - sid (2015-10-21) (x64) - - # Past versions tested with: - # - # - Debian 8.0 "jessie" (x64) - # - Raspbian 7.8 (armhf) - - # Believed not to work: - # - # - Debian 6.0.10 "squeeze" (x64) - - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='-qq' - fi - - apt-get $QUIET_FLAG update || error apt-get update hit problems but continuing anyway... - - # virtualenv binary can be found in different packages depending on - # distro version (#346) - - virtualenv= - # virtual env is known to apt and is installable - if apt-cache show virtualenv > /dev/null 2>&1 ; then - if ! LC_ALL=C apt-cache --quiet=0 show virtualenv 2>&1 | grep -q 'No packages found'; then - virtualenv="virtualenv" - fi - fi - - if apt-cache show python-virtualenv > /dev/null 2>&1; then - virtualenv="$virtualenv python-virtualenv" - fi - - augeas_pkg="libaugeas0 augeas-lenses" - AUGVERSION=`LC_ALL=C apt-cache show --no-all-versions libaugeas0 | grep ^Version: | cut -d" " -f2` - - if [ "$ASSUME_YES" = 1 ]; then - YES_FLAG="-y" - fi - - AddBackportRepo() { - # ARGS: - BACKPORT_NAME="$1" - BACKPORT_SOURCELINE="$2" - say "To use the Apache Certbot plugin, augeas needs to be installed from $BACKPORT_NAME." - if ! grep -v -e ' *#' /etc/apt/sources.list | grep -q "$BACKPORT_NAME" ; then - # This can theoretically error if sources.list.d is empty, but in that case we don't care. - if ! grep -v -e ' *#' /etc/apt/sources.list.d/* 2>/dev/null | grep -q "$BACKPORT_NAME"; then - if [ "$ASSUME_YES" = 1 ]; then - /bin/echo -n "Installing augeas from $BACKPORT_NAME in 3 seconds..." - sleep 1s - /bin/echo -ne "\e[0K\rInstalling augeas from $BACKPORT_NAME in 2 seconds..." - sleep 1s - /bin/echo -e "\e[0K\rInstalling augeas from $BACKPORT_NAME in 1 second ..." - sleep 1s - add_backports=1 - else - read -p "Would you like to enable the $BACKPORT_NAME repository [Y/n]? " response - case $response in - [yY][eE][sS]|[yY]|"") - add_backports=1;; - *) - add_backports=0;; - esac - fi - if [ "$add_backports" = 1 ]; then - sh -c "echo $BACKPORT_SOURCELINE >> /etc/apt/sources.list.d/$BACKPORT_NAME.list" - apt-get $QUIET_FLAG update - fi - fi - fi - if [ "$add_backports" != 0 ]; then - apt-get install $QUIET_FLAG $YES_FLAG --no-install-recommends -t "$BACKPORT_NAME" $augeas_pkg - augeas_pkg= - fi - } - - - if dpkg --compare-versions 1.0 gt "$AUGVERSION" ; then - if lsb_release -a | grep -q wheezy ; then - AddBackportRepo wheezy-backports "deb http://http.debian.net/debian wheezy-backports main" - elif lsb_release -a | grep -q precise ; then - # XXX add ARM case - AddBackportRepo precise-backports "deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse" - else - echo "No libaugeas0 version is available that's new enough to run the" - echo "Certbot apache plugin..." - fi - # XXX add a case for ubuntu PPAs - fi - - apt-get install $QUIET_FLAG $YES_FLAG --no-install-recommends \ - python \ - python-dev \ - $virtualenv \ - gcc \ - $augeas_pkg \ - libssl-dev \ - openssl \ - libffi-dev \ - ca-certificates \ - - - if ! $EXISTS virtualenv > /dev/null ; then - error Failed to install a working \"virtualenv\" command, exiting - exit 1 - fi -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/free_bsd.sh b/letsencrypt-auto-source/pieces/bootstrappers/free_bsd.sh deleted file mode 100755 index a67c856192f..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/free_bsd.sh +++ /dev/null @@ -1,15 +0,0 @@ -# If new packages are installed by BootstrapFreeBsd below, this version number -# must be increased. -BOOTSTRAP_FREEBSD_VERSION=1 - -BootstrapFreeBsd() { - if [ "$QUIET" = 1 ]; then - QUIET_FLAG="--quiet" - fi - - pkg install -Ay $QUIET_FLAG \ - python \ - py27-virtualenv \ - augeas \ - libffi -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/gentoo_common.sh b/letsencrypt-auto-source/pieces/bootstrappers/gentoo_common.sh deleted file mode 100755 index e2d24b5fb7b..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/gentoo_common.sh +++ /dev/null @@ -1,31 +0,0 @@ -# If new packages are installed by BootstrapGentooCommon below, this version -# number must be increased. -BOOTSTRAP_GENTOO_COMMON_VERSION=1 - -BootstrapGentooCommon() { - PACKAGES=" - dev-lang/python:2.7 - dev-python/virtualenv - app-admin/augeas - dev-libs/openssl - dev-libs/libffi - app-misc/ca-certificates - virtual/pkgconfig" - - ASK_OPTION="--ask" - if [ "$ASSUME_YES" = 1 ]; then - ASK_OPTION="" - fi - - case "$PACKAGE_MANAGER" in - (paludis) - cave resolve --preserve-world --keep-targets if-possible $PACKAGES -x - ;; - (pkgcore) - pmerge --noreplace --oneshot $ASK_OPTION $PACKAGES - ;; - (portage|*) - emerge --noreplace --oneshot $ASK_OPTION $PACKAGES - ;; - esac -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/mac.sh b/letsencrypt-auto-source/pieces/bootstrappers/mac.sh deleted file mode 100755 index 9e26d33894e..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/mac.sh +++ /dev/null @@ -1,48 +0,0 @@ -# If new packages are installed by BootstrapMac below, this version number must -# be increased. -BOOTSTRAP_MAC_VERSION=1 - -BootstrapMac() { - if hash brew 2>/dev/null; then - say "Using Homebrew to install dependencies..." - pkgman=brew - pkgcmd="brew install" - elif hash port 2>/dev/null; then - say "Using MacPorts to install dependencies..." - pkgman=port - pkgcmd="port install" - else - say "No Homebrew/MacPorts; installing Homebrew..." - ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" - pkgman=brew - pkgcmd="brew install" - fi - - $pkgcmd augeas - if [ "$(which python)" = "/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python" \ - -o "$(which python)" = "/usr/bin/python" ]; then - # We want to avoid using the system Python because it requires root to use pip. - # python.org, MacPorts or HomeBrew Python installations should all be OK. - say "Installing python..." - $pkgcmd python - fi - - # Workaround for _dlopen not finding augeas on macOS - if [ "$pkgman" = "port" ] && ! [ -e "/usr/local/lib/libaugeas.dylib" ] && [ -e "/opt/local/lib/libaugeas.dylib" ]; then - say "Applying augeas workaround" - mkdir -p /usr/local/lib/ - ln -s /opt/local/lib/libaugeas.dylib /usr/local/lib/ - fi - - if ! hash pip 2>/dev/null; then - say "pip not installed" - say "Installing pip..." - curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | python - fi - - if ! hash virtualenv 2>/dev/null; then - say "virtualenv not installed." - say "Installing with pip..." - pip install virtualenv - fi -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/mageia_common.sh b/letsencrypt-auto-source/pieces/bootstrappers/mageia_common.sh deleted file mode 100644 index dfa5b47f387..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/mageia_common.sh +++ /dev/null @@ -1,30 +0,0 @@ -# If new packages are installed by BootstrapMageiaCommon below, this version -# number must be increased. -BOOTSTRAP_MAGEIA_COMMON_VERSION=1 - -BootstrapMageiaCommon() { - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='--quiet' - fi - - if ! urpmi --force $QUIET_FLAG \ - python \ - libpython-devel \ - python-virtualenv - then - error "Could not install Python dependencies. Aborting bootstrap!" - exit 1 - fi - - if ! urpmi --force $QUIET_FLAG \ - git \ - gcc \ - python-augeas \ - libopenssl-devel \ - libffi-devel \ - rootcerts - then - error "Could not install additional dependencies. Aborting bootstrap!" - exit 1 - fi -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/rpm_common.sh b/letsencrypt-auto-source/pieces/bootstrappers/rpm_common.sh deleted file mode 100755 index 80d55a3931e..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/rpm_common.sh +++ /dev/null @@ -1,45 +0,0 @@ -# If new packages are installed by BootstrapRpmCommon below, this version -# number must be increased. -BOOTSTRAP_RPM_COMMON_VERSION=1 - -BootstrapRpmCommon() { - # Tested with: - # - Fedora 20, 21, 22, 23 (x64) - # - Centos 7 (x64: on DigitalOcean droplet) - # - CentOS 7 Minimal install in a Hyper-V VM - # - CentOS 6 - - InitializeRPMCommonBase - - # Most RPM distros use the "python" or "python-" naming convention. Let's try that first. - if $TOOL list python >/dev/null 2>&1; then - python_pkgs="$python - python-devel - python-virtualenv - python-tools - python-pip - " - # Fedora 26 starts to use the prefix python2 for python2 based packages. - # this elseif is theoretically for any Fedora over version 26: - elif $TOOL list python2 >/dev/null 2>&1; then - python_pkgs="$python2 - python2-libs - python2-setuptools - python2-devel - python2-virtualenv - python2-tools - python2-pip - " - # Some distros and older versions of current distros use a "python27" - # instead of the "python" or "python-" naming convention. - else - python_pkgs="$python27 - python27-devel - python27-virtualenv - python27-tools - python27-pip - " - fi - - BootstrapRpmCommonBase "$python_pkgs" -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/rpm_common_base.sh b/letsencrypt-auto-source/pieces/bootstrappers/rpm_common_base.sh deleted file mode 100644 index 326ad8b3f4c..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/rpm_common_base.sh +++ /dev/null @@ -1,78 +0,0 @@ -# If new packages are installed by BootstrapRpmCommonBase below, version -# numbers in rpm_common.sh and rpm_python3.sh must be increased. - -# Sets TOOL to the name of the package manager -# Sets appropriate values for YES_FLAG and QUIET_FLAG based on $ASSUME_YES and $QUIET_FLAG. -# Enables EPEL if applicable and possible. -InitializeRPMCommonBase() { - if type dnf 2>/dev/null - then - TOOL=dnf - elif type yum 2>/dev/null - then - TOOL=yum - - else - error "Neither yum nor dnf found. Aborting bootstrap!" - exit 1 - fi - - if [ "$ASSUME_YES" = 1 ]; then - YES_FLAG="-y" - fi - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='--quiet' - fi - - if ! $TOOL list *virtualenv >/dev/null 2>&1; then - echo "To use Certbot, packages from the EPEL repository need to be installed." - if ! $TOOL list epel-release >/dev/null 2>&1; then - error "Enable the EPEL repository and try running Certbot again." - exit 1 - fi - if [ "$ASSUME_YES" = 1 ]; then - /bin/echo -n "Enabling the EPEL repository in 3 seconds..." - sleep 1s - /bin/echo -ne "\e[0K\rEnabling the EPEL repository in 2 seconds..." - sleep 1s - /bin/echo -e "\e[0K\rEnabling the EPEL repository in 1 second..." - sleep 1s - fi - if ! $TOOL install $YES_FLAG $QUIET_FLAG epel-release; then - error "Could not enable EPEL. Aborting bootstrap!" - exit 1 - fi - fi -} - -BootstrapRpmCommonBase() { - # Arguments: whitespace-delimited python packages to install - - InitializeRPMCommonBase # This call is superfluous in practice - - pkgs=" - gcc - augeas-libs - openssl - openssl-devel - libffi-devel - redhat-rpm-config - ca-certificates - " - - # Add the python packages - pkgs="$pkgs - $1 - " - - if $TOOL list installed "httpd" >/dev/null 2>&1; then - pkgs="$pkgs - mod_ssl - " - fi - - if ! $TOOL install $YES_FLAG $QUIET_FLAG $pkgs; then - error "Could not install OS dependencies. Aborting bootstrap!" - exit 1 - fi -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/rpm_python3.sh b/letsencrypt-auto-source/pieces/bootstrappers/rpm_python3.sh deleted file mode 100644 index b011a7235b2..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/rpm_python3.sh +++ /dev/null @@ -1,23 +0,0 @@ -# If new packages are installed by BootstrapRpmPython3 below, this version -# number must be increased. -BOOTSTRAP_RPM_PYTHON3_VERSION=1 - -BootstrapRpmPython3() { - # Tested with: - # - CentOS 6 - - InitializeRPMCommonBase - - # EPEL uses python34 - if $TOOL list python34 >/dev/null 2>&1; then - python_pkgs="python34 - python34-devel - python34-tools - " - else - error "No supported Python package available to install. Aborting bootstrap!" - exit 1 - fi - - BootstrapRpmCommonBase "$python_pkgs" -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/smartos.sh b/letsencrypt-auto-source/pieces/bootstrappers/smartos.sh deleted file mode 100644 index ac7c0ed4ae9..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/smartos.sh +++ /dev/null @@ -1,8 +0,0 @@ -# If new packages are installed by BootstrapSmartOS below, this version number -# must be increased. -BOOTSTRAP_SMARTOS_VERSION=1 - -BootstrapSmartOS() { - pkgin update - pkgin -y install 'gcc49' 'py27-augeas' 'py27-virtualenv' -} diff --git a/letsencrypt-auto-source/pieces/bootstrappers/suse_common.sh b/letsencrypt-auto-source/pieces/bootstrappers/suse_common.sh deleted file mode 100755 index c531cbe997b..00000000000 --- a/letsencrypt-auto-source/pieces/bootstrappers/suse_common.sh +++ /dev/null @@ -1,26 +0,0 @@ -# If new packages are installed by BootstrapSuseCommon below, this version -# number must be increased. -BOOTSTRAP_SUSE_COMMON_VERSION=1 - -BootstrapSuseCommon() { - # SLE12 don't have python-virtualenv - - if [ "$ASSUME_YES" = 1 ]; then - zypper_flags="-nq" - install_flags="-l" - fi - - if [ "$QUIET" = 1 ]; then - QUIET_FLAG='-qq' - fi - - zypper $QUIET_FLAG $zypper_flags in $install_flags \ - python \ - python-devel \ - python-virtualenv \ - gcc \ - augeas-lenses \ - libopenssl-devel \ - libffi-devel \ - ca-certificates -} diff --git a/letsencrypt-auto-source/pieces/certbot-requirements.txt b/letsencrypt-auto-source/pieces/certbot-requirements.txt deleted file mode 100644 index 08d8d553f56..00000000000 --- a/letsencrypt-auto-source/pieces/certbot-requirements.txt +++ /dev/null @@ -1,12 +0,0 @@ -certbot==0.30.0 \ - --hash=sha256:b3468e128e74d2295598f6d3fbf9d0edfb67fe5abaca3b985a9e858395bd027f \ - --hash=sha256:d631fe6c75700ce9b2fdae194ff8b53c7518545d87dd451a1704f7572dcd49e8 -acme==0.30.0 \ - --hash=sha256:eed9389f802ebf4988c9e43c28ad3d5c2734237371d78e97450a1d61189a15aa \ - --hash=sha256:984b6d00bec73dcfa616636a760e80ca14bd246fb908710a656547f542f09445 -certbot-apache==0.30.0 \ - --hash=sha256:d38c70fc6930db298ea992a3145362eebdce460d3d2651f86a8f2f43d838c6d0 \ - --hash=sha256:1d4bc207d53a3e5d37e5d9ebd05f26089aa21d1fbf384113ed9d1829b4d1e9bf -certbot-nginx==0.30.0 \ - --hash=sha256:6163c7d0080f59b4ebe510afcc6af2d2eebf15469275c3835866690db4d465d6 \ - --hash=sha256:e39a3f3d77cd4c653949cf066fb2211039fd2032665697c27b6e8501c7c2dd92 diff --git a/letsencrypt-auto-source/pieces/dependency-requirements.txt b/letsencrypt-auto-source/pieces/dependency-requirements.txt deleted file mode 100644 index eb297bc6e0e..00000000000 --- a/letsencrypt-auto-source/pieces/dependency-requirements.txt +++ /dev/null @@ -1,196 +0,0 @@ -# This is the flattened list of packages certbot-auto installs. To generate -# this, do -# `pip install --no-cache-dir -e acme -e . -e certbot-apache -e certbot-nginx`, -# and then use `hashin` or a more secure method to gather the hashes. - -# Hashin example: -# pip install hashin -# hashin -r dependency-requirements.txt cryptography==1.5.2 -# sets the new certbot-auto pinned version of cryptography to 1.5.2 - -argparse==1.4.0 \ - --hash=sha256:c31647edb69fd3d465a847ea3157d37bed1f95f19760b11a47aa91c04b666314 \ - --hash=sha256:62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4 - -# This comes before cffi because cffi will otherwise install an unchecked -# version via setup_requires. -pycparser==2.14 \ - --hash=sha256:7959b4a74abdc27b312fed1c21e6caf9309ce0b29ea86b591fd2e99ecdf27f73 \ - --no-binary pycparser - -asn1crypto==0.22.0 \ - --hash=sha256:d232509fefcfcdb9a331f37e9c9dc20441019ad927c7d2176cf18ed5da0ba097 \ - --hash=sha256:cbbadd640d3165ab24b06ef25d1dca09a3441611ac15f6a6b452474fdf0aed1a -cffi==1.11.5 \ - --hash=sha256:1b0493c091a1898f1136e3f4f991a784437fac3673780ff9de3bcf46c80b6b50 \ - --hash=sha256:87f37fe5130574ff76c17cab61e7d2538a16f843bb7bca8ebbc4b12de3078596 \ - --hash=sha256:1553d1e99f035ace1c0544050622b7bc963374a00c467edafac50ad7bd276aef \ - --hash=sha256:151b7eefd035c56b2b2e1eb9963c90c6302dc15fbd8c1c0a83a163ff2c7d7743 \ - --hash=sha256:edabd457cd23a02965166026fd9bfd196f4324fe6032e866d0f3bd0301cd486f \ - --hash=sha256:ba5e697569f84b13640c9e193170e89c13c6244c24400fc57e88724ef610cd31 \ - --hash=sha256:79f9b6f7c46ae1f8ded75f68cf8ad50e5729ed4d590c74840471fc2823457d04 \ - --hash=sha256:b0f7d4a3df8f06cf49f9f121bead236e328074de6449866515cea4907bbc63d6 \ - --hash=sha256:4c91af6e967c2015729d3e69c2e51d92f9898c330d6a851bf8f121236f3defd3 \ - --hash=sha256:7a33145e04d44ce95bcd71e522b478d282ad0eafaf34fe1ec5bbd73e662f22b6 \ - --hash=sha256:95d5251e4b5ca00061f9d9f3d6fe537247e145a8524ae9fd30a2f8fbce993b5b \ - --hash=sha256:b75110fb114fa366b29a027d0c9be3709579602ae111ff61674d28c93606acca \ - --hash=sha256:ae5e35a2c189d397b91034642cb0eab0e346f776ec2eb44a49a459e6615d6e2e \ - --hash=sha256:fdf1c1dc5bafc32bc5d08b054f94d659422b05aba244d6be4ddc1c72d9aa70fb \ - --hash=sha256:9d1d3e63a4afdc29bd76ce6aa9d58c771cd1599fbba8cf5057e7860b203710dd \ - --hash=sha256:be2a9b390f77fd7676d80bc3cdc4f8edb940d8c198ed2d8c0be1319018c778e1 \ - --hash=sha256:ed01918d545a38998bfa5902c7c00e0fee90e957ce036a4000a88e3fe2264917 \ - --hash=sha256:857959354ae3a6fa3da6651b966d13b0a8bed6bbc87a0de7b38a549db1d2a359 \ - --hash=sha256:2ba8a45822b7aee805ab49abfe7eec16b90587f7f26df20c71dd89e45a97076f \ - --hash=sha256:a36c5c154f9d42ec176e6e620cb0dd275744aa1d804786a71ac37dc3661a5e95 \ - --hash=sha256:e55e22ac0a30023426564b1059b035973ec82186ddddbac867078435801c7801 \ - --hash=sha256:3eb6434197633b7748cea30bf0ba9f66727cdce45117a712b29a443943733257 \ - --hash=sha256:ecbb7b01409e9b782df5ded849c178a0aa7c906cf8c5a67368047daab282b184 \ - --hash=sha256:770f3782b31f50b68627e22f91cb182c48c47c02eb405fd689472aa7b7aa16dc \ - --hash=sha256:d5d8555d9bfc3f02385c1c37e9f998e2011f0db4f90e250e5bc0c0a85a813085 \ - --hash=sha256:3c85641778460581c42924384f5e68076d724ceac0f267d66c757f7535069c93 \ - --hash=sha256:ca1bd81f40adc59011f58159e4aa6445fc585a32bb8ac9badf7a2c1aa23822f2 \ - --hash=sha256:3bb6bd7266598f318063e584378b8e27c67de998a43362e8fce664c54ee52d30 \ - --hash=sha256:a6a5cb8809091ec9ac03edde9304b3ad82ad4466333432b16d78ef40e0cce0d5 \ - --hash=sha256:57b2533356cb2d8fac1555815929f7f5f14d68ac77b085d2326b571310f34f6e \ - --hash=sha256:495c5c2d43bf6cebe0178eb3e88f9c4aa48d8934aa6e3cddb865c058da76756b \ - --hash=sha256:e90f17980e6ab0f3c2f3730e56d1fe9bcba1891eeea58966e89d352492cc74f4 -ConfigArgParse==0.12.0 \ - --hash=sha256:28cd7d67669651f2a4518367838c49539457504584a139709b2b8f6c208ef339 \ - --no-binary ConfigArgParse -configobj==5.0.6 \ - --hash=sha256:a2f5650770e1c87fb335af19a9b7eb73fc05ccf22144eb68db7d00cd2bcb0902 \ - --no-binary configobj -cryptography==2.2.2 \ - --hash=sha256:3f3b65d5a16e6b52fba63dc860b62ca9832f51f1a2ae5083c78b6840275f12dd \ - --hash=sha256:5251e7de0de66810833606439ca65c9b9e45da62196b0c88bfadf27740aac09f \ - --hash=sha256:551a3abfe0c8c6833df4192a63371aa2ff43afd8f570ed345d31f251d78e7e04 \ - --hash=sha256:5cb990056b7cadcca26813311187ad751ea644712022a3976443691168781b6f \ - --hash=sha256:60bda7f12ecb828358be53095fc9c6edda7de8f1ef571f96c00b2363643fa3cd \ - --hash=sha256:64b5c67acc9a7c83fbb4b69166f3105a0ab722d27934fac2cb26456718eec2ba \ - --hash=sha256:6fef51ec447fe9f8351894024e94736862900d3a9aa2961528e602eb65c92bdb \ - --hash=sha256:77d0ad229d47a6e0272d00f6bf8ac06ce14715a9fd02c9a97f5a2869aab3ccb2 \ - --hash=sha256:808fe471b1a6b777f026f7dc7bd9a4959da4bfab64972f2bbe91e22527c1c037 \ - --hash=sha256:9b62fb4d18529c84b961efd9187fecbb48e89aa1a0f9f4161c61b7fc42a101bd \ - --hash=sha256:9e5bed45ec6b4f828866ac6a6bedf08388ffcfa68abe9e94b34bb40977aba531 \ - --hash=sha256:9fc295bf69130a342e7a19a39d7bbeb15c0bcaabc7382ec33ef3b2b7d18d2f63 \ - --hash=sha256:abd070b5849ed64e6d349199bef955ee0ad99aefbad792f0c587f8effa681a5e \ - --hash=sha256:ba6a774749b6e510cffc2fb98535f717e0e5fd91c7c99a61d223293df79ab351 \ - --hash=sha256:c332118647f084c983c6a3e1dba0f3bcb051f69d12baccac68db8d62d177eb8a \ - --hash=sha256:d6f46e862ee36df81e6342c2177ba84e70f722d9dc9c6c394f9f1f434c4a5563 \ - --hash=sha256:db6013746f73bf8edd9c3d1d3f94db635b9422f503db3fc5ef105233d4c011ab \ - --hash=sha256:f57008eaff597c69cf692c3518f6d4800f0309253bb138b526a37fe9ef0c7471 \ - --hash=sha256:f6c821ac253c19f2ad4c8691633ae1d1a17f120d5b01ea1d256d7b602bc59887 -enum34==1.1.2 ; python_version < '3.4' \ - --hash=sha256:2475d7fcddf5951e92ff546972758802de5260bf409319a9f1934e6bbc8b1dc7 \ - --hash=sha256:35907defb0f992b75ab7788f65fedc1cf20ffa22688e0e6f6f12afc06b3ea501 -funcsigs==1.0.2 \ - --hash=sha256:330cc27ccbf7f1e992e69fef78261dc7c6569012cf397db8d3de0234e6c937ca \ - --hash=sha256:a7bb0f2cf3a3fd1ab2732cb49eba4252c2af4240442415b4abce3b87022a8f50 -idna==2.5 \ - --hash=sha256:cc19709fd6d0cbfed39ea875d29ba6d4e22c0cebc510a76d6302a28385e8bb70 \ - --hash=sha256:3cb5ce08046c4e3a560fc02f138d0ac63e00f8ce5901a56b32ec8b7994082aab -ipaddress==1.0.16 \ - --hash=sha256:935712800ce4760701d89ad677666cd52691fd2f6f0b340c8b4239a3c17988a5 \ - --hash=sha256:5a3182b322a706525c46282ca6f064d27a02cffbd449f9f47416f1dc96aa71b0 -josepy==1.1.0 \ - --hash=sha256:1309a25aac3caeff5239729c58ff9b583f7d022ffdb1553406ddfc8e5b52b76e \ - --hash=sha256:fb5c62c77d26e04df29cb5ecd01b9ce69b6fcc9e521eb1ca193b7faa2afa7086 -linecache2==1.0.0 \ - --hash=sha256:e78be9c0a0dfcbac712fe04fbf92b96cddae80b1b842f24248214c8496f006ef \ - --hash=sha256:4b26ff4e7110db76eeb6f5a7b64a82623839d595c2038eeda662f2a2db78e97c -# Using an older version of mock here prevents regressions of #5276. -mock==1.3.0 \ - --hash=sha256:3f573a18be94de886d1191f27c168427ef693e8dcfcecf95b170577b2eb69cbb \ - --hash=sha256:1e247dbecc6ce057299eb7ee019ad68314bb93152e81d9a6110d35f4d5eca0f6 -ordereddict==1.1 \ - --hash=sha256:1c35b4ac206cef2d24816c89f89cf289dd3d38cf7c449bb3fab7bf6d43f01b1f \ - --no-binary ordereddict -packaging==16.8 \ - --hash=sha256:99276dc6e3a7851f32027a68f1095cd3f77c148091b092ea867a351811cfe388 \ - --hash=sha256:5d50835fdf0a7edf0b55e311b7c887786504efea1177abd7e69329a8e5ea619e -parsedatetime==2.1 \ - --hash=sha256:ce9d422165cf6e963905cd5f74f274ebf7cc98c941916169178ef93f0e557838 \ - --hash=sha256:17c578775520c99131634e09cfca5a05ea9e1bd2a05cd06967ebece10df7af2d -pbr==1.8.1 \ - --hash=sha256:46c8db75ae75a056bd1cc07fa21734fe2e603d11a07833ecc1eeb74c35c72e0c \ - --hash=sha256:e2127626a91e6c885db89668976db31020f0af2da728924b56480fc7ccf09649 -pyOpenSSL==16.2.0 \ - --hash=sha256:26ca380ddf272f7556e48064bbcd5bd71f83dfc144f3583501c7ddbd9434ee17 \ - --hash=sha256:7779a3bbb74e79db234af6a08775568c6769b5821faecf6e2f4143edb227516e -pyparsing==2.1.8 \ - --hash=sha256:2f0f5ceb14eccd5aef809d6382e87df22ca1da583c79f6db01675ce7d7f49c18 \ - --hash=sha256:03a4869b9f3493807ee1f1cb405e6d576a1a2ca4d81a982677c0c1ad6177c56b \ - --hash=sha256:ab09aee814c0241ff0c503cff30018219fe1fc14501d89f406f4664a0ec9fbcd \ - --hash=sha256:6e9a7f052f8e26bcf749e4033e3115b6dc7e3c85aafcb794b9a88c9d9ef13c97 \ - --hash=sha256:9f463a6bcc4eeb6c08f1ed84439b17818e2085937c0dee0d7674ac127c67c12b \ - --hash=sha256:3626b4d81cfb300dad57f52f2f791caaf7b06c09b368c0aa7b868e53a5775424 \ - --hash=sha256:367b90cc877b46af56d4580cd0ae278062903f02b8204ab631f5a2c0f50adfd0 \ - --hash=sha256:9f1ea360086cd68681e7f4ca8f1f38df47bf81942a0d76a9673c2d23eff35b13 -pyRFC3339==1.0 \ - --hash=sha256:eea31835c56e2096af4363a5745a784878a61d043e247d3a6d6a0a32a9741f56 \ - --hash=sha256:8dfbc6c458b8daba1c0f3620a8c78008b323a268b27b7359e92a4ae41325f535 -python-augeas==0.5.0 \ - --hash=sha256:67d59d66cdba8d624e0389b87b2a83a176f21f16a87553b50f5703b23f29bac2 \ - --no-binary python-augeas -pytz==2015.7 \ - --hash=sha256:3abe6a6d3fc2fbbe4c60144211f45da2edbe3182a6f6511af6bbba0598b1f992 \ - --hash=sha256:939ef9c1e1224d980405689a97ffcf7828c56d1517b31d73464356c1f2b7769e \ - --hash=sha256:ead4aefa7007249e05e51b01095719d5a8dd95760089f5730aac5698b1932918 \ - --hash=sha256:3cca0df08bd0ed98432390494ce3ded003f5e661aa460be7a734bffe35983605 \ - --hash=sha256:3ede470d3d17ba3c07638dfa0d10452bc1b6e5ad326127a65ba77e6aaeb11bec \ - --hash=sha256:68c47964f7186eec306b13629627722b9079cd4447ed9e5ecaecd4eac84ca734 \ - --hash=sha256:dd5d3991950aae40a6c81de1578942e73d629808cefc51d12cd157980e6cfc18 \ - --hash=sha256:a77c52062c07eb7c7b30545dbc73e32995b7e117eea750317b5cb5c7a4618f14 \ - --hash=sha256:81af9aec4bc960a9a0127c488f18772dae4634689233f06f65443e7b11ebeb51 \ - --hash=sha256:e079b1dadc5c06246cc1bb6fe1b23a50b1d1173f2edd5104efd40bb73a28f406 \ - --hash=sha256:fbd26746772c24cb93c8b97cbdad5cb9e46c86bbdb1b9d8a743ee00e2fb1fc5d \ - --hash=sha256:99266ef30a37e43932deec2b7ca73e83c8dbc3b9ff703ec73eca6b1dae6befea \ - --hash=sha256:8b6ce1c993909783bc96e0b4f34ea223bff7a4df2c90bdb9c4e0f1ac928689e3 -requests==2.20.0 \ - --hash=sha256:99dcfdaaeb17caf6e526f32b6a7b780461512ab3f1d992187801694cba42770c \ - --hash=sha256:a84b8c9ab6239b578f22d1c21d51b696dcfe004032bb80ea832398d6909d7279 -six==1.10.0 \ - --hash=sha256:0ff78c403d9bccf5a425a6d31a12aa6b47f1c21ca4dc2573a7e2f32a97335eb1 \ - --hash=sha256:105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a -traceback2==1.4.0 \ - --hash=sha256:8253cebec4b19094d67cc5ed5af99bf1dba1285292226e98a31929f87a5d6b23 \ - --hash=sha256:05acc67a09980c2ecfedd3423f7ae0104839eccb55fc645773e1caa0951c3030 -unittest2==1.1.0 \ - --hash=sha256:13f77d0875db6d9b435e1d4f41e74ad4cc2eb6e1d5c824996092b3430f088bb8 \ - --hash=sha256:22882a0e418c284e1f718a822b3b022944d53d2d908e1690b319a9d3eb2c0579 -zope.component==4.2.2 \ - --hash=sha256:282c112b55dd8e3c869a3571f86767c150ab1284a9ace2bdec226c592acaf81a \ - --no-binary zope.component -zope.event==4.1.0 \ - --hash=sha256:dc7a59a2fd91730d3793131a5d261b29e93ec4e2a97f1bc487ce8defee2fe786 \ - --no-binary zope.event -zope.interface==4.1.3 \ - --hash=sha256:f07b631f7a601cd8cbd3332d54f43142c7088a83299f859356f08d1d4d4259b3 \ - --hash=sha256:de5cca083b9439d8002fb76bbe6b4998c5a5a721fab25b84298967f002df4c94 \ - --hash=sha256:6788416f7ea7f5b8a97be94825377aa25e8bdc73463e07baaf9858b29e737077 \ - --hash=sha256:6f3230f7254518201e5a3708cbb2de98c848304f06e3ded8bfb39e5825cba2e1 \ - --hash=sha256:5fa575a5240f04200c3088427d0d4b7b737f6e9018818a51d8d0f927a6a2517a \ - --hash=sha256:522194ad6a545735edd75c8a83f48d65d1af064e432a7d320d64f56bafc12e99 \ - --hash=sha256:e8c7b2d40943f71c99148c97f66caa7f5134147f57423f8db5b4825099ce9a09 \ - --hash=sha256:279024f0208601c3caa907c53876e37ad88625f7eaf1cb3842dbe360b2287017 \ - --hash=sha256:2e221a9eec7ccc58889a278ea13dcfed5ef939d80b07819a9a8b3cb1c681484f \ - --hash=sha256:69118965410ec86d44dc6b9017ee3ddbd582e0c0abeef62b3a19dbf6c8ad132b \ - --hash=sha256:d04df8686ec864d0cade8cf199f7f83aecd416109a20834d568f8310ded12dea \ - --hash=sha256:e75a947e15ee97e7e71e02ea302feb2fc62d3a2bb4668bf9dfbed43a506ac7e7 \ - --hash=sha256:4e45d22fb883222a5ab9f282a116fec5ee2e8d1a568ccff6a2d75bbd0eb6bcfc \ - --hash=sha256:bce9339bb3c7a55e0803b63d21c5839e8e479bc85c4adf42ae415b72f94facb2 \ - --hash=sha256:928138365245a0e8869a5999fbcc2a45475a0a6ed52a494d60dbdc540335fedd \ - --hash=sha256:0d841ba1bb840eea0e6489dc5ecafa6125554971f53b5acb87764441e61bceba \ - --hash=sha256:b09c8c1d47b3531c400e0195697f1414a63221de6ef478598a4f1460f7d9a392 -requests-toolbelt==0.8.0 \ - --hash=sha256:42c9c170abc2cacb78b8ab23ac957945c7716249206f90874651971a4acff237 \ - --hash=sha256:f6a531936c6fa4c6cfce1b9c10d5c4f498d16528d2a54a22ca00011205a187b5 -chardet==3.0.2 \ - --hash=sha256:4f7832e7c583348a9eddd927ee8514b3bf717c061f57b21dbe7697211454d9bb \ - --hash=sha256:6ebf56457934fdce01fb5ada5582762a84eed94cad43ed877964aebbdd8174c0 -urllib3==1.24.1 \ - --hash=sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39 \ - --hash=sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22 -certifi==2017.4.17 \ - --hash=sha256:f4318671072f030a33c7ca6acaef720ddd50ff124d1388e50c1bda4cbd6d7010 \ - --hash=sha256:f7527ebf7461582ce95f7a9e03dd141ce810d40590834f4ec20cddd54234c10a diff --git a/letsencrypt-auto-source/pieces/fetch.py b/letsencrypt-auto-source/pieces/fetch.py deleted file mode 100644 index 1515fe3533b..00000000000 --- a/letsencrypt-auto-source/pieces/fetch.py +++ /dev/null @@ -1,148 +0,0 @@ -"""Do downloading and JSON parsing without additional dependencies. :: - - # Print latest released version of LE to stdout: - python fetch.py --latest-version - - # Download letsencrypt-auto script from git tag v1.2.3 into the folder I'm - # in, and make sure its signature verifies: - python fetch.py --le-auto-script v1.2.3 - -On failure, return non-zero. - -""" - -from __future__ import print_function, unicode_literals - -from distutils.version import LooseVersion -from json import loads -from os import devnull, environ -from os.path import dirname, join -import re -import ssl -from subprocess import check_call, CalledProcessError -from sys import argv, exit -try: - from urllib2 import build_opener, HTTPHandler, HTTPSHandler - from urllib2 import HTTPError, URLError -except ImportError: - from urllib.request import build_opener, HTTPHandler, HTTPSHandler - from urllib.error import HTTPError, URLError - -PUBLIC_KEY = environ.get('LE_AUTO_PUBLIC_KEY', """-----BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6MR8W/galdxnpGqBsYbq -OzQb2eyW15YFjDDEMI0ZOzt8f504obNs920lDnpPD2/KqgsfjOgw2K7xWDJIj/18 -xUvWPk3LDkrnokNiRkA3KOx3W6fHycKL+zID7zy+xZYBuh2fLyQtWV1VGQ45iNRp -9+Zo7rH86cdfgkdnWTlNSHyTLW9NbXvyv/E12bppPcEvgCTAQXgnDVJ0/sqmeiij -n9tTFh03aM+R2V/21h8aTraAS24qiPCz6gkmYGC8yr6mglcnNoYbsLNYZ69zF1XH -cXPduCPdPdfLlzVlKK1/U7hkA28eG3BIAMh6uJYBRJTpiGgaGdPd7YekUB8S6cy+ -CQIDAQAB ------END PUBLIC KEY----- -""") - -class ExpectedError(Exception): - """A novice-readable exception that also carries the original exception for - debugging""" - - -class HttpsGetter(object): - def __init__(self): - """Build an HTTPS opener.""" - # Based on pip 1.4.1's URLOpener - # This verifies certs on only Python >=2.7.9, and when NO_CERT_VERIFY isn't set. - if environ.get('NO_CERT_VERIFY') == '1' and hasattr(ssl, 'SSLContext'): - self._opener = build_opener(HTTPSHandler(context=cert_none_context())) - else: - self._opener = build_opener(HTTPSHandler()) - # Strip out HTTPHandler to prevent MITM spoof: - for handler in self._opener.handlers: - if isinstance(handler, HTTPHandler): - self._opener.handlers.remove(handler) - - def get(self, url): - """Return the document contents pointed to by an HTTPS URL. - - If something goes wrong (404, timeout, etc.), raise ExpectedError. - - """ - try: - # socket module docs say default timeout is None: that is, no - # timeout - return self._opener.open(url, timeout=30).read() - except (HTTPError, IOError) as exc: - raise ExpectedError("Couldn't download %s." % url, exc) - - -def write(contents, dir, filename): - """Write something to a file in a certain directory.""" - with open(join(dir, filename), 'wb') as file: - file.write(contents) - - -def latest_stable_version(get): - """Return the latest stable release of letsencrypt.""" - metadata = loads(get( - environ.get('LE_AUTO_JSON_URL', - 'https://pypi.python.org/pypi/certbot/json')).decode('UTF-8')) - # metadata['info']['version'] actually returns the latest of any kind of - # release release, contrary to https://wiki.python.org/moin/PyPIJSON. - # The regex is a sufficient regex for picking out prereleases for most - # packages, LE included. - return str(max(LooseVersion(r) for r - in metadata['releases'].keys() - if re.match('^[0-9.]+$', r))) - - -def verified_new_le_auto(get, tag, temp_dir): - """Return the path to a verified, up-to-date letsencrypt-auto script. - - If the download's signature does not verify or something else goes wrong - with the verification process, raise ExpectedError. - - """ - le_auto_dir = environ.get( - 'LE_AUTO_DIR_TEMPLATE', - 'https://raw.githubusercontent.com/certbot/certbot/%s/' - 'letsencrypt-auto-source/') % tag - write(get(le_auto_dir + 'letsencrypt-auto'), temp_dir, 'letsencrypt-auto') - write(get(le_auto_dir + 'letsencrypt-auto.sig'), temp_dir, 'letsencrypt-auto.sig') - write(PUBLIC_KEY.encode('UTF-8'), temp_dir, 'public_key.pem') - try: - with open(devnull, 'w') as dev_null: - check_call(['openssl', 'dgst', '-sha256', '-verify', - join(temp_dir, 'public_key.pem'), - '-signature', - join(temp_dir, 'letsencrypt-auto.sig'), - join(temp_dir, 'letsencrypt-auto')], - stdout=dev_null, - stderr=dev_null) - except CalledProcessError as exc: - raise ExpectedError("Couldn't verify signature of downloaded " - "certbot-auto.", exc) - - -def cert_none_context(): - """Create a SSLContext object to not check hostname.""" - # PROTOCOL_TLS isn't available before 2.7.13 but this code is for 2.7.9+, so use this. - context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) - context.verify_mode = ssl.CERT_NONE - return context - - -def main(): - get = HttpsGetter().get - flag = argv[1] - try: - if flag == '--latest-version': - print(latest_stable_version(get)) - elif flag == '--le-auto-script': - tag = argv[2] - verified_new_le_auto(get, tag, dirname(argv[0])) - except ExpectedError as exc: - print(exc.args[0], exc.args[1]) - return 1 - else: - return 0 - - -if __name__ == '__main__': - exit(main()) diff --git a/letsencrypt-auto-source/pieces/letsencrypt-requirements.txt b/letsencrypt-auto-source/pieces/letsencrypt-requirements.txt deleted file mode 100644 index 8e745c9cdb0..00000000000 --- a/letsencrypt-auto-source/pieces/letsencrypt-requirements.txt +++ /dev/null @@ -1,10 +0,0 @@ -# Contains the requirements for the letsencrypt package. -# -# Since the letsencrypt package depends on certbot and using pip with hashes -# requires that all installed packages have hashes listed, this allows -# dependency-requirements.txt to be used without requiring a hash for a -# (potentially unreleased) Certbot package. - -letsencrypt==0.7.0 \ - --hash=sha256:105a5fb107e45bcd0722eb89696986dcf5f08a86a321d6aef25a0c7c63375ade \ - --hash=sha256:c36e532c486a7e92155ee09da54b436a3c420813ec1c590b98f635d924720de9 diff --git a/letsencrypt-auto-source/pieces/pipstrap.py b/letsencrypt-auto-source/pieces/pipstrap.py deleted file mode 100755 index 727040c3cb6..00000000000 --- a/letsencrypt-auto-source/pieces/pipstrap.py +++ /dev/null @@ -1,180 +0,0 @@ -#!/usr/bin/env python -"""A small script that can act as a trust root for installing pip >=8 -Embed this in your project, and your VCS checkout is all you have to trust. In -a post-peep era, this lets you claw your way to a hash-checking version of pip, -with which you can install the rest of your dependencies safely. All it assumes -is Python 2.6 or better and *some* version of pip already installed. If -anything goes wrong, it will exit with a non-zero status code. -""" -# This is here so embedded copies are MIT-compliant: -# Copyright (c) 2016 Erik Rose -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to -# deal in the Software without restriction, including without limitation the -# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -# sell copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -from __future__ import print_function -from distutils.version import StrictVersion -from hashlib import sha256 -from os import environ -from os.path import join -from pipes import quote -from shutil import rmtree -try: - from subprocess import check_output -except ImportError: - from subprocess import CalledProcessError, PIPE, Popen - - def check_output(*popenargs, **kwargs): - if 'stdout' in kwargs: - raise ValueError('stdout argument not allowed, it will be ' - 'overridden.') - process = Popen(stdout=PIPE, *popenargs, **kwargs) - output, unused_err = process.communicate() - retcode = process.poll() - if retcode: - cmd = kwargs.get("args") - if cmd is None: - cmd = popenargs[0] - raise CalledProcessError(retcode, cmd) - return output -from sys import exit, version_info -from tempfile import mkdtemp -try: - from urllib2 import build_opener, HTTPHandler, HTTPSHandler -except ImportError: - from urllib.request import build_opener, HTTPHandler, HTTPSHandler -try: - from urlparse import urlparse -except ImportError: - from urllib.parse import urlparse # 3.4 - - -__version__ = 1, 5, 1 -PIP_VERSION = '9.0.1' -DEFAULT_INDEX_BASE = 'https://pypi.python.org' - - -# wheel has a conditional dependency on argparse: -maybe_argparse = ( - [('18/dd/e617cfc3f6210ae183374cd9f6a26b20514bbb5a792af97949c5aacddf0f/' - 'argparse-1.4.0.tar.gz', - '62b089a55be1d8949cd2bc7e0df0bddb9e028faefc8c32038cc84862aefdd6e4')] - if version_info < (2, 7, 0) else []) - - -PACKAGES = maybe_argparse + [ - # Pip has no dependencies, as it vendors everything: - ('11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/' - 'pip-{0}.tar.gz'.format(PIP_VERSION), - '09f243e1a7b461f654c26a725fa373211bb7ff17a9300058b205c61658ca940d'), - # This version of setuptools has only optional dependencies: - ('59/88/2f3990916931a5de6fa9706d6d75eb32ee8b78627bb2abaab7ed9e6d0622/' - 'setuptools-29.0.1.tar.gz', - 'b539118819a4857378398891fa5366e090690e46b3e41421a1e07d6e9fd8feb0'), - ('c9/1d/bd19e691fd4cfe908c76c429fe6e4436c9e83583c4414b54f6c85471954a/' - 'wheel-0.29.0.tar.gz', - '1ebb8ad7e26b448e9caa4773d2357849bf80ff9e313964bcaf79cbf0201a1648') -] - - -class HashError(Exception): - def __str__(self): - url, path, actual, expected = self.args - return ('{url} did not match the expected hash {expected}. Instead, ' - 'it was {actual}. The file (left at {path}) may have been ' - 'tampered with.'.format(**locals())) - - -def hashed_download(url, temp, digest): - """Download ``url`` to ``temp``, make sure it has the SHA-256 ``digest``, - and return its path.""" - # Based on pip 1.4.1's URLOpener but with cert verification removed. Python - # >=2.7.9 verifies HTTPS certs itself, and, in any case, the cert - # authenticity has only privacy (not arbitrary code execution) - # implications, since we're checking hashes. - def opener(using_https=True): - opener = build_opener(HTTPSHandler()) - if using_https: - # Strip out HTTPHandler to prevent MITM spoof: - for handler in opener.handlers: - if isinstance(handler, HTTPHandler): - opener.handlers.remove(handler) - return opener - - def read_chunks(response, chunk_size): - while True: - chunk = response.read(chunk_size) - if not chunk: - break - yield chunk - - parsed_url = urlparse(url) - response = opener(using_https=parsed_url.scheme == 'https').open(url) - path = join(temp, parsed_url.path.split('/')[-1]) - actual_hash = sha256() - with open(path, 'wb') as file: - for chunk in read_chunks(response, 4096): - file.write(chunk) - actual_hash.update(chunk) - - actual_digest = actual_hash.hexdigest() - if actual_digest != digest: - raise HashError(url, path, actual_digest, digest) - return path - - -def get_index_base(): - """Return the URL to the dir containing the "packages" folder. - Try to wring something out of PIP_INDEX_URL, if set. Hack "/simple" off the - end if it's there; that is likely to give us the right dir. - """ - env_var = environ.get('PIP_INDEX_URL', '').rstrip('/') - if env_var: - SIMPLE = '/simple' - if env_var.endswith(SIMPLE): - return env_var[:-len(SIMPLE)] - else: - return env_var - else: - return DEFAULT_INDEX_BASE - - -def main(): - pip_version = StrictVersion(check_output(['pip', '--version']) - .decode('utf-8').split()[1]) - min_pip_version = StrictVersion(PIP_VERSION) - if pip_version >= min_pip_version: - return 0 - has_pip_cache = pip_version >= StrictVersion('6.0') - index_base = get_index_base() - temp = mkdtemp(prefix='pipstrap-') - try: - downloads = [hashed_download(index_base + '/packages/' + path, - temp, - digest) - for path, digest in PACKAGES] - check_output('pip install --no-index --no-deps -U ' + - # Disable cache since we're not using it and it otherwise - # sometimes throws permission warnings: - ('--no-cache-dir ' if has_pip_cache else '') + - ' '.join(quote(d) for d in downloads), - shell=True) - except HashError as exc: - print(exc) - except Exception: - rmtree(temp) - raise - else: - rmtree(temp) - return 0 - return 1 - - -if __name__ == '__main__': - exit(main()) diff --git a/letsencrypt-auto-source/tests/__init__.py b/letsencrypt-auto-source/tests/__init__.py deleted file mode 100644 index 8a1613aa548..00000000000 --- a/letsencrypt-auto-source/tests/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -"""Tests for letsencrypt-auto - -Run these locally by saying... :: - - ./build.py && docker build -t lea . -f Dockerfile. && docker run --rm -t -i lea - -""" diff --git a/letsencrypt-auto-source/tests/auto_test.py b/letsencrypt-auto-source/tests/auto_test.py deleted file mode 100644 index 16c478f2068..00000000000 --- a/letsencrypt-auto-source/tests/auto_test.py +++ /dev/null @@ -1,397 +0,0 @@ -"""Tests for letsencrypt-auto""" - -from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler -from contextlib import contextmanager -from functools import partial -from json import dumps -from os import chmod, environ, makedirs -from os.path import abspath, dirname, exists, join -import re -from shutil import copy, rmtree -import socket -import ssl -from stat import S_IRUSR, S_IXUSR -from subprocess import CalledProcessError, Popen, PIPE -import sys -from tempfile import mkdtemp -from threading import Thread -from unittest import TestCase - -from pytest import mark -from six.moves import xrange # pylint: disable=redefined-builtin - - -@mark.skip -def tests_dir(): - """Return a path to the "tests" directory.""" - return dirname(abspath(__file__)) - - -def copy_stable(src, dst): - """ - Copy letsencrypt-auto, and replace its current version to its equivalent stable one. - This is needed to test correctly the self-upgrade functionality. - """ - copy(src, dst) - with open(dst, 'r') as file: - filedata = file.read() - filedata = re.sub(r'LE_AUTO_VERSION="(.*)\.dev0"', r'LE_AUTO_VERSION="\1"', filedata) - with open(dst, 'w') as file: - file.write(filedata) - - -sys.path.insert(0, dirname(tests_dir())) -from build import build as build_le_auto - - -BOOTSTRAP_FILENAME = 'certbot-auto-bootstrap-version.txt' -"""Name of the file where certbot-auto saves its bootstrap version.""" - - -class RequestHandler(BaseHTTPRequestHandler): - """An HTTPS request handler which is quiet and serves a specific folder.""" - - def __init__(self, resources, *args, **kwargs): - """ - :arg resources: A dict of resource paths pointing to content bytes - - """ - self.resources = resources - BaseHTTPRequestHandler.__init__(self, *args, **kwargs) - - def log_message(self, format, *args): - """Don't log each request to the terminal.""" - - def do_GET(self): - """Serve a GET request.""" - content = self.send_head() - if content is not None: - self.wfile.write(content) - - def send_head(self): - """Common code for GET and HEAD commands - - This sends the response code and MIME headers and returns either a - bytestring of content or, if none is found, None. - - """ - path = self.path[1:] # Strip leading slash. - content = self.resources.get(path) - if content is None: - self.send_error(404, 'Path "%s" not found in self.resources' % path) - else: - self.send_response(200) - self.send_header('Content-type', 'text/plain') - self.send_header('Content-Length', str(len(content))) - self.end_headers() - return content - - -def server_and_port(resources): - """Return an unstarted HTTPS server and the port it will use.""" - # Find a port, and bind to it. I can't get the OS to close the socket - # promptly after we shut down the server, so we typically need to try - # a couple ports after the first test case. Setting - # TCPServer.allow_reuse_address = True seems to have nothing to do - # with this behavior. - worked = False - for port in xrange(4443, 4543): - try: - server = HTTPServer(('localhost', port), - partial(RequestHandler, resources)) - except socket.error: - pass - else: - worked = True - server.socket = ssl.wrap_socket( - server.socket, - certfile=join(tests_dir(), 'certs', 'localhost', 'server.pem'), - server_side=True) - break - if not worked: - raise RuntimeError("Couldn't find an unused socket for the testing HTTPS server.") - return server, port - - -@contextmanager -def serving(resources): - """Spin up a local HTTPS server, and yield its base URL. - - Use a self-signed cert generated as outlined by - https://coolaj86.com/articles/create-your-own-certificate-authority-for- - testing/. - - """ - server, port = server_and_port(resources) - thread = Thread(target=server.serve_forever) - try: - thread.start() - yield 'https://localhost:{port}/'.format(port=port) - finally: - server.shutdown() - thread.join() - - -LE_AUTO_PATH = join(dirname(tests_dir()), 'letsencrypt-auto') - - -@contextmanager -def temp_paths(): - """Creates and deletes paths for letsencrypt-auto and its venv.""" - dir = mkdtemp(prefix='le-test-') - try: - yield join(dir, 'letsencrypt-auto'), join(dir, 'venv') - finally: - rmtree(dir, ignore_errors=True) - - -def out_and_err(command, input=None, shell=False, env=None): - """Run a shell command, and return stderr and stdout as string. - - If the command returns nonzero, raise CalledProcessError. - - :arg command: A list of commandline args - :arg input: Data to pipe to stdin. Omit for none. - - Remaining args have the same meaning as for Popen. - - """ - process = Popen(command, - stdout=PIPE, - stdin=PIPE, - stderr=PIPE, - shell=shell, - env=env) - out, err = process.communicate(input=input) - status = process.poll() # same as in check_output(), though wait() sounds better - if status: - error = CalledProcessError(status, command) - error.output = out - raise error - return out, err - - -def signed(content, private_key_name='signing.key'): - """Return the signed SHA-256 hash of ``content``, using the given key file.""" - command = ['openssl', 'dgst', '-sha256', '-sign', - join(tests_dir(), private_key_name)] - out, err = out_and_err(command, input=content) - return out - - -def install_le_auto(contents, install_path): - """Install some given source code as the letsencrypt-auto script at the - root level of a virtualenv. - - :arg contents: The contents of the built letsencrypt-auto script - :arg install_path: The path where to install the script - - """ - with open(install_path, 'w') as le_auto: - le_auto.write(contents) - chmod(install_path, S_IRUSR | S_IXUSR) - - -def run_le_auto(le_auto_path, venv_dir, base_url, **kwargs): - """Run the prebuilt version of letsencrypt-auto, returning stdout and - stderr strings. - - If the command returns other than 0, raise CalledProcessError. - - """ - env = environ.copy() - d = dict(VENV_PATH=venv_dir, - # URL to PyPI-style JSON that tell us the latest released version - # of LE: - LE_AUTO_JSON_URL=base_url + 'certbot/json', - # URL to dir containing letsencrypt-auto and letsencrypt-auto.sig: - LE_AUTO_DIR_TEMPLATE=base_url + '%s/', - # The public key corresponding to signing.key: - LE_AUTO_PUBLIC_KEY="""-----BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsMoSzLYQ7E1sdSOkwelg -tzKIh2qi3bpXuYtcfFC0XrvWig071NwIj+dZiT0OLZ2hPispEH0B7ISuuWg1ll7G -hFW0VdbxL6JdGzS2ShNWkX9hE9z+j8VqwDPOBn3ZHm03qwpYkBDwQib3KqOdYbTT -uUtJmmGcuk3a9Aq/sCT6DdfmTSdP5asdQYwIcaQreDrOosaS84DTWI3IU+UYJVgl -LsIVPBuy9IcgHidUQ96hJnoPsDCWsHwX62495QKEarauyKQrJzFes0EY95orDM47 -Z5o/NDiQB11m91yNB0MmPYY9QSbnOA9j7IaaC97AwRLuwXY+/R2ablTcxurWou68 -iQIDAQAB ------END PUBLIC KEY-----""", - NO_CERT_VERIFY='1', - **kwargs) - env.update(d) - return out_and_err( - le_auto_path + ' --version', - shell=True, - env=env) - - -def set_le_script_version(venv_dir, version): - """Tell the letsencrypt script to report a certain version. - - We actually replace the script with a dummy version that knows only how to - print its version. - - """ - letsencrypt_path = join(venv_dir, 'bin', 'letsencrypt') - with open(letsencrypt_path, 'w') as script: - script.write("#!/usr/bin/env python\n" - "from sys import stderr\n" - "stderr.write('letsencrypt %s\\n')" % version) - chmod(letsencrypt_path, S_IRUSR | S_IXUSR) - - -class AutoTests(TestCase): - """Test the major branch points of letsencrypt-auto: - - * An le-auto upgrade is needed. - * An le-auto upgrade is not needed. - * There was an out-of-date LE script installed. - * There was a current LE script installed. - * There was no LE script installed (less important). - * Pip hash-verification passes. - * Pip has a hash mismatch. - * The OpenSSL sig matches. - * The OpenSSL sig mismatches. - - For tests which get to the end, we run merely ``letsencrypt --version``. - The functioning of the rest of the certbot script is covered by other - test suites. - - """ - NEW_LE_AUTO = build_le_auto( - version='99.9.9', - requirements='letsencrypt==99.9.9 --hash=sha256:1cc14d61ab424cdee446f51e50f1123f8482ec740587fe78626c933bba2873a0') - NEW_LE_AUTO_SIG = signed(NEW_LE_AUTO) - - def test_successes(self): - """Exercise most branches of letsencrypt-auto. - - They just happen to be the branches in which everything goes well. - - I violate my usual rule of having small, decoupled tests, because... - - 1. We shouldn't need to run a Cartesian product of the branches: the - phases run in separate shell processes, containing state leakage - pretty effectively. The only shared state is FS state, and it's - limited to a temp dir, assuming (if we dare) all functions properly. - 2. One combination of branches happens to set us up nicely for testing - the next, saving code. - - """ - with temp_paths() as (le_auto_path, venv_dir): - # This serves a PyPI page with a higher version, a GitHub-alike - # with a corresponding le-auto script, and a matching signature. - resources = {'certbot/json': dumps({'releases': {'99.9.9': None}}), - 'v99.9.9/letsencrypt-auto': self.NEW_LE_AUTO, - 'v99.9.9/letsencrypt-auto.sig': self.NEW_LE_AUTO_SIG} - with serving(resources) as base_url: - run_letsencrypt_auto = partial( - run_le_auto, - le_auto_path, - venv_dir, - base_url, - PIP_FIND_LINKS=join(tests_dir(), - 'fake-letsencrypt', - 'dist')) - - # Test when a phase-1 upgrade is needed, there's no LE binary - # installed, and pip hashes verify: - install_le_auto(build_le_auto(version='50.0.0'), le_auto_path) - out, err = run_letsencrypt_auto() - self.assertTrue(re.match(r'letsencrypt \d+\.\d+\.\d+', - err.strip().splitlines()[-1])) - # Make a few assertions to test the validity of the next tests: - self.assertTrue('Upgrading certbot-auto ' in out) - self.assertTrue('Creating virtual environment...' in out) - - # Now we have le-auto 99.9.9 and LE 99.9.9 installed. This - # conveniently sets us up to test the next 2 cases. - - # Test when neither phase-1 upgrade nor phase-2 upgrade is - # needed (probably a common case): - out, err = run_letsencrypt_auto() - self.assertFalse('Upgrading certbot-auto ' in out) - self.assertFalse('Creating virtual environment...' in out) - - def test_phase2_upgrade(self): - """Test a phase-2 upgrade without a phase-1 upgrade.""" - resources = {'certbot/json': dumps({'releases': {'99.9.9': None}}), - 'v99.9.9/letsencrypt-auto': self.NEW_LE_AUTO, - 'v99.9.9/letsencrypt-auto.sig': self.NEW_LE_AUTO_SIG} - with serving(resources) as base_url: - pip_find_links=join(tests_dir(), 'fake-letsencrypt', 'dist') - with temp_paths() as (le_auto_path, venv_dir): - install_le_auto(self.NEW_LE_AUTO, le_auto_path) - - # Create venv saving the correct bootstrap script version - out, err = run_le_auto(le_auto_path, venv_dir, base_url, - PIP_FIND_LINKS=pip_find_links) - self.assertFalse('Upgrading certbot-auto ' in out) - self.assertTrue('Creating virtual environment...' in out) - with open(join(venv_dir, BOOTSTRAP_FILENAME)) as f: - bootstrap_version = f.read() - - # Create a new venv with an old letsencrypt version - with temp_paths() as (le_auto_path, venv_dir): - venv_bin = join(venv_dir, 'bin') - makedirs(venv_bin) - set_le_script_version(venv_dir, '0.0.1') - with open(join(venv_dir, BOOTSTRAP_FILENAME), 'w') as f: - f.write(bootstrap_version) - - install_le_auto(self.NEW_LE_AUTO, le_auto_path) - out, err = run_le_auto(le_auto_path, venv_dir, base_url, - PIP_FIND_LINKS=pip_find_links) - - self.assertFalse('Upgrading certbot-auto ' in out) - self.assertTrue('Creating virtual environment...' in out) - - def test_openssl_failure(self): - """Make sure we stop if the openssl signature check fails.""" - with temp_paths() as (le_auto_path, venv_dir): - # Serve an unrelated hash signed with the good key (easier than - # making a bad key, and a mismatch is a mismatch): - resources = {'': 'certbot/', - 'certbot/json': dumps({'releases': {'99.9.9': None}}), - 'v99.9.9/letsencrypt-auto': build_le_auto(version='99.9.9'), - 'v99.9.9/letsencrypt-auto.sig': signed('something else')} - with serving(resources) as base_url: - copy_stable(LE_AUTO_PATH, le_auto_path) - try: - out, err = run_le_auto(le_auto_path, venv_dir, base_url) - except CalledProcessError as exc: - self.assertEqual(exc.returncode, 1) - self.assertTrue("Couldn't verify signature of downloaded " - "certbot-auto." in exc.output) - else: - print(out) - self.fail('Signature check on certbot-auto erroneously passed.') - - def test_pip_failure(self): - """Make sure pip stops us if there is a hash mismatch.""" - with temp_paths() as (le_auto_path, venv_dir): - resources = {'': 'certbot/', - 'certbot/json': dumps({'releases': {'99.9.9': None}})} - with serving(resources) as base_url: - # Build a le-auto script embedding a bad requirements file: - install_le_auto( - build_le_auto( - version='99.9.9', - requirements='configobj==5.0.6 --hash=sha256:badbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadb'), - le_auto_path) - try: - out, err = run_le_auto(le_auto_path, venv_dir, base_url) - except CalledProcessError as exc: - self.assertEqual(exc.returncode, 1) - self.assertTrue("THESE PACKAGES DO NOT MATCH THE HASHES " - "FROM THE REQUIREMENTS FILE" in exc.output) - self.assertFalse( - exists(venv_dir), - msg="The virtualenv was left around, even though " - "installation didn't succeed. We shouldn't do " - "this, as it foils our detection of whether we " - "need to recreate the virtualenv, which hinges " - "on the presence of $VENV_BIN/letsencrypt.") - else: - self.fail("Pip didn't detect a bad hash and stop the " - "installation.") diff --git a/letsencrypt-auto-source/tests/centos6_tests.sh b/letsencrypt-auto-source/tests/centos6_tests.sh deleted file mode 100644 index 2c6dcf73410..00000000000 --- a/letsencrypt-auto-source/tests/centos6_tests.sh +++ /dev/null @@ -1,81 +0,0 @@ -#!/bin/bash -# Start by making sure your system is up-to-date: -yum update -y > /dev/null -yum install -y centos-release-scl > /dev/null -yum install -y python27 > /dev/null 2> /dev/null - -LE_AUTO="certbot/letsencrypt-auto-source/letsencrypt-auto" - -# we're going to modify env variables, so do this in a subshell -( -source /opt/rh/python27/enable - -# ensure python 3 isn't installed -python3 --version 2> /dev/null -RESULT=$? -if [ $RESULT -eq 0 ]; then - error "Python3 is already installed." - exit 1 -fi - -# ensure python2.7 is available -python2.7 --version 2> /dev/null -RESULT=$? -if [ $RESULT -ne 0 ]; then - error "Python3 is not available." - exit 1 -fi - -# bootstrap, but don't install python 3. -"$LE_AUTO" --no-self-upgrade -n > /dev/null 2> /dev/null - -# ensure python 3 isn't installed -python3 --version 2> /dev/null -RESULT=$? -if [ $RESULT -eq 0 ]; then - error "letsencrypt-auto installed Python3 even though Python2.7 is present." - exit 1 -fi - -echo "" -echo "PASSED: Did not upgrade to Python3 when Python2.7 is present." -) - -# ensure python2.7 isn't available -python2.7 --version 2> /dev/null -RESULT=$? -if [ $RESULT -eq 0 ]; then - error "Python2.7 is still available." - exit 1 -fi - -# Skip self upgrade due to Python 3 not being available. -if ! "$LE_AUTO" 2>&1 | grep -q "WARNING: couldn't find Python"; then - echo "Python upgrade failure warning not printed!" - exit 1 -fi - -# bootstrap, this time installing python3 -"$LE_AUTO" --no-self-upgrade -n > /dev/null 2> /dev/null - -# ensure python 3 is installed -python3 --version > /dev/null -RESULT=$? -if [ $RESULT -ne 0 ]; then - error "letsencrypt-auto failed to install Python3 when only Python2.6 is present." - exit 1 -fi - -echo "PASSED: Successfully upgraded to Python3 when only Python2.6 is present." -echo "" - -export VENV_PATH=$(mktemp -d) -"$LE_AUTO" -n --no-bootstrap --no-self-upgrade --version >/dev/null 2>&1 -if [ "$($VENV_PATH/bin/python -V 2>&1 | cut -d" " -f2 | cut -d. -f1)" != 3 ]; then - echo "Python 3 wasn't used with --no-bootstrap!" - exit 1 -fi -unset VENV_PATH - -# test using python3 -pytest -v -s certbot/letsencrypt-auto-source/tests diff --git a/letsencrypt-auto-source/tests/certs/ca/my-root-ca.crt.pem b/letsencrypt-auto-source/tests/certs/ca/my-root-ca.crt.pem deleted file mode 100644 index 4e4d29bd225..00000000000 --- a/letsencrypt-auto-source/tests/certs/ca/my-root-ca.crt.pem +++ /dev/null @@ -1,23 +0,0 @@ ------BEGIN CERTIFICATE----- -MIID5jCCAs6gAwIBAgIJAI1Qkfyw88REMA0GCSqGSIb3DQEBBQUAMFUxCzAJBgNV -BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMRswGQYDVQQKExJNeSBCb2d1cyBS -b290IENlcnQxFDASBgNVBAMTC2V4YW1wbGUuY29tMB4XDTE1MTIwNDIwNTIxNVoX -DTQwMTIwMzIwNTIxNVowVTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3Rh -dGUxGzAZBgNVBAoTEk15IEJvZ3VzIFJvb3QgQ2VydDEUMBIGA1UEAxMLZXhhbXBs -ZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDQVQpQ2EH4gTJB -NJP6+ocT3xJwT8mSXYUnvzjj6iv+JxZiXRGzAPziNzrrSRKY0yDHF+UiJwuOerLa -n8laZkLb1Ogqzs2u64rKeb0xWv90Qp+eXG0J/1xb4dw+GExqe5QFo1JUJzO/eK7m -1S04SeFkN1qV9mD5yJUy7DGiTUzDHgCxM2tXMLusXYqkxsQQ9+2EJ7BEOK4YJGEx -Sign5FuSxb64PiNow6OA97CaLl7tV4INP4w195ueDRIaS4poeOep4s8U7IAdMjIZ -EryJgKNCij50xK92vPBBJSj0NOitltBlwoEqkOZpQCOZamFd6nvt78LQ6W8Am+l6 -y6oCON5JAgMBAAGjgbgwgbUwHQYDVR0OBBYEFAlrdStDhaayLLj89Whe3Gc+HE8y -MIGFBgNVHSMEfjB8gBQJa3UrQ4Wmsiy4/PVoXtxnPhxPMqFZpFcwVTELMAkGA1UE -BhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxGzAZBgNVBAoTEk15IEJvZ3VzIFJv -b3QgQ2VydDEUMBIGA1UEAxMLZXhhbXBsZS5jb22CCQCNUJH8sPPERDAMBgNVHRME -BTADAQH/MA0GCSqGSIb3DQEBBQUAA4IBAQC7KAQfDTiNM3QO8Ic3x21CAPJUavkH -zshifN+Ei0+nmseHDTCTgsGfGDOToLUpUEZ4PuiHnz08UwRfd9wotc3SgY9ZaXMe -vRs8KUAF9EoyTvESzPyv2b6cS9NNMpj5y7KyXSyP17VoGbNavtiGQ4dwgEH6VgNl -0RtBvcSBv/tqxIIx1tWzL74tVEm0Kbd9BAZsYpQNKL8e6WXP35/j0PvCCvtofGrA -E8LTqMz4kCwnX+QaJIMJhBophRCsjXdAkvFbFxX0DGPztQtzIwBPcdMjsft7AFeE -0XchhDDXxw8YsbpvPfCvrD8XiiVuBycbnB1zt0LLVwB/QsCzUW9ImpLC ------END CERTIFICATE----- diff --git a/letsencrypt-auto-source/tests/certs/ca/my-root-ca.key.pem b/letsencrypt-auto-source/tests/certs/ca/my-root-ca.key.pem deleted file mode 100644 index 9caa7ddaad6..00000000000 --- a/letsencrypt-auto-source/tests/certs/ca/my-root-ca.key.pem +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEowIBAAKCAQEA0FUKUNhB+IEyQTST+vqHE98ScE/Jkl2FJ7844+or/icWYl0R -swD84jc660kSmNMgxxflIicLjnqy2p/JWmZC29ToKs7NruuKynm9MVr/dEKfnlxt -Cf9cW+HcPhhManuUBaNSVCczv3iu5tUtOEnhZDdalfZg+ciVMuwxok1Mwx4AsTNr -VzC7rF2KpMbEEPfthCewRDiuGCRhMUooJ+RbksW+uD4jaMOjgPewmi5e7VeCDT+M -Nfebng0SGkuKaHjnqeLPFOyAHTIyGRK8iYCjQoo+dMSvdrzwQSUo9DTorZbQZcKB -KpDmaUAjmWphXep77e/C0OlvAJvpesuqAjjeSQIDAQABAoIBAH+qbVzneV3wxjwh -HUHi/p3VyHXc3xh7iNq3mwRH/1eK2nPCttLsGwwBbnC64dOXJfH7maWZKcLRPAMv -gfOM0RHn4bJB8tdrbizv91lke0DihvBDkWpb+1wvB4lh2Io0Wpwt3ojFUTfXm87G -+iQRWjbQmQlm5zyKh6uiBDSCjDTQdb9omZEBMAwlGPTZwt8TRUEtWd8QgW8FCHoB -iLER2WBwXdvn3PBtocI3VE6IYDSeZ81Xv+d7925RtVintT8Suk4toYwX+jfSz+wZ -sgHd5V6PSv9a7GUlWoUihD99D9wqDZE8IvMDZ5ofSAUd1KfICDtmsEyugY7u2yYZ -tYt49AECgYEA73f7ITMHg8JsUipqb6eG10gCRtRhkqrrO1g/TNeTBh3CTrQGb56e -y6kmUivn5gK46t3T2N4Ht4IR8fpLcJcbPYPQNulSjmWm5y6WduafXW/VCW1NA9Lc -FyGPkMxFCIVJTLFxfLFepBVvtUzLLDKGGtQxru/GNbBzjdtmVfDPIoECgYEA3rbM -cTfvj+jWrV1YsRbphyjy+k3OJEIVx6KA4s5d7Tp12UfYQp/B3HPhXXm5wqeo1Nos -UAEWZIMi1VoE8iu6jjeJ6uERtbKKQVed25Us/ff0jUPbxlXgiBOtRcllq9d9Srjm -ybHUgfjLsZ2/xpIcOl+oI5pDM9JvD8Sq4ZCFR8kCgYBK/H0tFjeiML2OtS2DLShy -PWBJIbQ0I0Vp3eZkf5TQc30m/ASP61G6YItZa9pAElYpZbEy1cQA2MAZz9DTvt2O -07ndmA57/KTY+6OuM+Vvctd5DjrxmZPFwoKcSvrLAkHDvETXUQtbwkKquRNeEawg -tpWgPAELSufEYhGXk8KpAQKBgBDCqPgMQZcO6rj5QWdyVfi5+C8mE9Fet8ziSdjH -twHXWG8VnQzGgQxaHCewtW4Ut/vsv1D2A/1kcQalU6H18IArZdGrRm3qFcV9FoAj -5dLnChxncu6mH9Odx3htA52/BcrNx3B+VYPCeXHQcVI8RKuP71NelJgdygXhwwpe -mekhAoGBAOUovnqylciYa9HRqo+xZk59eyX+ehhnlV8SeJ2K0PwaQkzQ0KYtCmE7 -kdSdhcv8h/IQKGaFfc/LyFMM/a26PfAeY5bj41UjkT0K5hQrYuL/52xaT401YLcb -Xo+bZz9K0hrdP7TdZFuTY/WxojXgjsVAuAN1NwnJumqxhzPh+hfl ------END RSA PRIVATE KEY----- diff --git a/letsencrypt-auto-source/tests/certs/ca/my-root-ca.srl b/letsencrypt-auto-source/tests/certs/ca/my-root-ca.srl deleted file mode 100644 index ad6d262b459..00000000000 --- a/letsencrypt-auto-source/tests/certs/ca/my-root-ca.srl +++ /dev/null @@ -1 +0,0 @@ -D613482D0EF95DD0 diff --git a/letsencrypt-auto-source/tests/certs/localhost/cert.pem b/letsencrypt-auto-source/tests/certs/localhost/cert.pem deleted file mode 100644 index ac83535ced6..00000000000 --- a/letsencrypt-auto-source/tests/certs/localhost/cert.pem +++ /dev/null @@ -1,19 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIDKjCCAhICCQDWE0gtDvld0DANBgkqhkiG9w0BAQUFADBVMQswCQYDVQQGEwJB -VTETMBEGA1UECBMKU29tZS1TdGF0ZTEbMBkGA1UEChMSTXkgQm9ndXMgUm9vdCBD -ZXJ0MRQwEgYDVQQDEwtleGFtcGxlLmNvbTAeFw0xNTEyMDQyMDU0MzFaFw00MDEy -MDMyMDU0MzFaMFkxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEw -HwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMTCWxvY2Fs -aG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2WIIi86Mis4UQH -a5PrFbX2PBtQHbI3t3ekN1CewRsgQ/2X3lCeWhKmr3CJYXVgA7q/23PORQAiuV6y -DG2dQIrjeahWCXaCptTi49ljfVRTW2IxrHke/iA8TkDuZbWGzVLb8TB83ipBOD41 -SjuomoN4A/ktnIfbNqRqgjjHs2wwJHDfxPiCQlwyOayjHmdlh8cqfVE8rWEm5/3T -Iu0X1J53SammR1SbUmsLJNofxFYMK1ogHb0CaFEG9QuuUDPJl5K74Rr6InMQZKPn -ne4W3cGoALxPHAca7yicpSMSmdsmd6pqylc2Fdua7o/wf0SwShxS4A1DqA/HWLEM -V6MSEF8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAz5sMAFG6W/ZEULZITkBTCU6P -NttpGiKufnqyBW5HyNylaczfnHnClvQjr8f/84xvKVcfC3xP0lz+92aIQqo+5L/n -v7gLhBFR4Vr2XwMt2qz2FpkaxmVwnhVAHaaC05WIKQ6W2gDwWT0u1K8YdTh+7mvN -AT9FW4vDgtNZWq4W/PePh9QCiOOQhGOuBYj/7zqLtz4XPifhi66ILIRDHiu0kond -3YMFcECIAf4MPT9vT0iNcWX+c8CfAixPt8nMD6bzOo3oTcfuZh/2enfgLbMqOlOi -uk72FM5VVPXTWAckJvL/vVjqsvDuJQKqbr0oUc3bdWbS36xtWZUycp4IQLguAQ== ------END CERTIFICATE----- diff --git a/letsencrypt-auto-source/tests/certs/localhost/localhost.csr.pem b/letsencrypt-auto-source/tests/certs/localhost/localhost.csr.pem deleted file mode 100644 index 8a6189f8841..00000000000 --- a/letsencrypt-auto-source/tests/certs/localhost/localhost.csr.pem +++ /dev/null @@ -1,17 +0,0 @@ ------BEGIN CERTIFICATE REQUEST----- -MIICnjCCAYYCAQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUx -ITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDESMBAGA1UEAxMJbG9j -YWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArZYgiLzoyKzh -RAdrk+sVtfY8G1Adsje3d6Q3UJ7BGyBD/ZfeUJ5aEqavcIlhdWADur/bc85FACK5 -XrIMbZ1AiuN5qFYJdoKm1OLj2WN9VFNbYjGseR7+IDxOQO5ltYbNUtvxMHzeKkE4 -PjVKO6iag3gD+S2ch9s2pGqCOMezbDAkcN/E+IJCXDI5rKMeZ2WHxyp9UTytYSbn -/dMi7RfUnndJqaZHVJtSawsk2h/EVgwrWiAdvQJoUQb1C65QM8mXkrvhGvoicxBk -o+ed7hbdwagAvE8cBxrvKJylIxKZ2yZ3qmrKVzYV25ruj/B/RLBKHFLgDUOoD8dY -sQxXoxIQXwIDAQABoAAwDQYJKoZIhvcNAQEFBQADggEBAFbg3WrAokoPx7iAYG6z -PqeDd4/XanXjeL4Ryxv6LoGhu69mmBAd3N5ILPyQJjnkWpIjEmJDzEcPMzhQjRh5 -GlWTyvKWO4zClYU840KZk7crVkpzNZ+HP0YeM/Agz6sab00ffRcq5m1wEF9MCvDE -8FUXk1HBHRAb/6t9QV/7axsPOkGT8SjQ1v2SCaiB0HQL3sYChYLi5zu4dfmQNPGq -ar9Xm5a0YqOQIFfmy8RSwxk0Q/ipNFTGN1uvlIRkgbT9zPnodxjWZsSI9BF+q5Af -uiE/oAk7MxfJ0LyLfhOWB+T98bKIOVtFT3wMLS1IIgMogwqCEXFf30Q9p2iTEzqT -6UE= ------END CERTIFICATE REQUEST----- diff --git a/letsencrypt-auto-source/tests/certs/localhost/privkey.pem b/letsencrypt-auto-source/tests/certs/localhost/privkey.pem deleted file mode 100644 index 18feba40363..00000000000 --- a/letsencrypt-auto-source/tests/certs/localhost/privkey.pem +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEowIBAAKCAQEArZYgiLzoyKzhRAdrk+sVtfY8G1Adsje3d6Q3UJ7BGyBD/Zfe -UJ5aEqavcIlhdWADur/bc85FACK5XrIMbZ1AiuN5qFYJdoKm1OLj2WN9VFNbYjGs -eR7+IDxOQO5ltYbNUtvxMHzeKkE4PjVKO6iag3gD+S2ch9s2pGqCOMezbDAkcN/E -+IJCXDI5rKMeZ2WHxyp9UTytYSbn/dMi7RfUnndJqaZHVJtSawsk2h/EVgwrWiAd -vQJoUQb1C65QM8mXkrvhGvoicxBko+ed7hbdwagAvE8cBxrvKJylIxKZ2yZ3qmrK -VzYV25ruj/B/RLBKHFLgDUOoD8dYsQxXoxIQXwIDAQABAoIBAG8bVJ+xKt6nqVg9 -16HKKw9ZGIfy888K0qgFuFImCzwtntdGycmYUdb2Uf0aMgNK/ZgfDXxGXuwDTdtK -46GVsaY0i74vs8bjQZ2pzGVsxN+gqzFi0h6Es+w2LXBqJzfVnL6YgPykMB+jtzg6 -K9Wbyaq0uvZXN4XNzl/WvJtTV4i7Cff1MOd5EhKFdqxrZvB/SRBCr/SMMafRtB9P -EvMneNKzhmlrutHAxuyxEKZR32Kkx7ydAdTjGgn+rE+NL5BweXfeWhLU4Bv14bn9 -Mkneu3w5o1ryJfE2YnVajUP//jeopUT0nTQ3MpEusBQCLBlvFXjjM9uCaFX+5+MP -0H4xVcECgYEA1Q+wR3GHbk37vIGSlbENyUsri5WlMt8IVAHsDsTOpxAjYB0yyo+x -h9RS+RJZQECJlA6H72peUl3GM7RgdWIcKOT3nZ12XqYKG57rr/N5zlUuxbdS8KBk -JhyZeJdYjq/Jrno1ZP+OSmc7VvBLcM7irY7LHlvK0o8W1W0TNJ8jrZkCgYEA0JHX -lJd+fiezcUS7g4moHtzJp0JKquQiXLX+c2urmpyhb3ZrTuQ8OUjSy6DlwHlgDx8K -Hg2sdx/ZCuDaGjR4IY/Qs5RFt9WUqlK9gi9V3nYVrzBOQkdFOf/Ad3j4pQ8/aeCX -nP6snHXz1WqPpbCXG6l6GzFGbQU473GfuKsDuLcCgYAWQaNKc0OQdDj9whNL68ji -5CVSWXl+TOoTzHeaO1jS/s6TNbmei1AiPj3EovQL0DIO802j5tqfhAg2UntZB7yl -UPXE0zQQQwv/QqSgJrDsqt1N7g6N8FNF3+rwO+8WSKqqvT1ipYd5ojsCo+tdh18K -fkYdj70qLaRW+yPsdUtG0QKBgEYc8NqbvsML94+ZKmwCh4iwcf2PFGi0PjTqXTpR -tKNKCh7dMR+ZLAGZ0HrxgKqeYsNSjOUjdZmqFB1LDyaGAuhNXzwvGOy+mLZVEC3G -Wdhp28pDs9sl+EiSCBJhkTxzjr656F23YzFJmYlhxB5P6cw7wbeIbgNSIRylFqtO -mfarAoGBAICsAEWypOctxtmtOcjxgJ7jMbOA7rrsGlXpiy1/WlwIwRGF5LMvIIFX -qFAfiPcZn05ZgdAGzaFYowdjmQB10FW0jZbDf+nIHfOF5YmfmfWjsaweEGALJmqB -okGu/lGNGf3XoYzy0/hC3WAqk3znSZtQLUq8jEWF7dLNUizUeUow ------END RSA PRIVATE KEY----- diff --git a/letsencrypt-auto-source/tests/certs/localhost/server.pem b/letsencrypt-auto-source/tests/certs/localhost/server.pem deleted file mode 100644 index c5765dd890d..00000000000 --- a/letsencrypt-auto-source/tests/certs/localhost/server.pem +++ /dev/null @@ -1,46 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEowIBAAKCAQEArZYgiLzoyKzhRAdrk+sVtfY8G1Adsje3d6Q3UJ7BGyBD/Zfe -UJ5aEqavcIlhdWADur/bc85FACK5XrIMbZ1AiuN5qFYJdoKm1OLj2WN9VFNbYjGs -eR7+IDxOQO5ltYbNUtvxMHzeKkE4PjVKO6iag3gD+S2ch9s2pGqCOMezbDAkcN/E -+IJCXDI5rKMeZ2WHxyp9UTytYSbn/dMi7RfUnndJqaZHVJtSawsk2h/EVgwrWiAd -vQJoUQb1C65QM8mXkrvhGvoicxBko+ed7hbdwagAvE8cBxrvKJylIxKZ2yZ3qmrK -VzYV25ruj/B/RLBKHFLgDUOoD8dYsQxXoxIQXwIDAQABAoIBAG8bVJ+xKt6nqVg9 -16HKKw9ZGIfy888K0qgFuFImCzwtntdGycmYUdb2Uf0aMgNK/ZgfDXxGXuwDTdtK -46GVsaY0i74vs8bjQZ2pzGVsxN+gqzFi0h6Es+w2LXBqJzfVnL6YgPykMB+jtzg6 -K9Wbyaq0uvZXN4XNzl/WvJtTV4i7Cff1MOd5EhKFdqxrZvB/SRBCr/SMMafRtB9P -EvMneNKzhmlrutHAxuyxEKZR32Kkx7ydAdTjGgn+rE+NL5BweXfeWhLU4Bv14bn9 -Mkneu3w5o1ryJfE2YnVajUP//jeopUT0nTQ3MpEusBQCLBlvFXjjM9uCaFX+5+MP -0H4xVcECgYEA1Q+wR3GHbk37vIGSlbENyUsri5WlMt8IVAHsDsTOpxAjYB0yyo+x -h9RS+RJZQECJlA6H72peUl3GM7RgdWIcKOT3nZ12XqYKG57rr/N5zlUuxbdS8KBk -JhyZeJdYjq/Jrno1ZP+OSmc7VvBLcM7irY7LHlvK0o8W1W0TNJ8jrZkCgYEA0JHX -lJd+fiezcUS7g4moHtzJp0JKquQiXLX+c2urmpyhb3ZrTuQ8OUjSy6DlwHlgDx8K -Hg2sdx/ZCuDaGjR4IY/Qs5RFt9WUqlK9gi9V3nYVrzBOQkdFOf/Ad3j4pQ8/aeCX -nP6snHXz1WqPpbCXG6l6GzFGbQU473GfuKsDuLcCgYAWQaNKc0OQdDj9whNL68ji -5CVSWXl+TOoTzHeaO1jS/s6TNbmei1AiPj3EovQL0DIO802j5tqfhAg2UntZB7yl -UPXE0zQQQwv/QqSgJrDsqt1N7g6N8FNF3+rwO+8WSKqqvT1ipYd5ojsCo+tdh18K -fkYdj70qLaRW+yPsdUtG0QKBgEYc8NqbvsML94+ZKmwCh4iwcf2PFGi0PjTqXTpR -tKNKCh7dMR+ZLAGZ0HrxgKqeYsNSjOUjdZmqFB1LDyaGAuhNXzwvGOy+mLZVEC3G -Wdhp28pDs9sl+EiSCBJhkTxzjr656F23YzFJmYlhxB5P6cw7wbeIbgNSIRylFqtO -mfarAoGBAICsAEWypOctxtmtOcjxgJ7jMbOA7rrsGlXpiy1/WlwIwRGF5LMvIIFX -qFAfiPcZn05ZgdAGzaFYowdjmQB10FW0jZbDf+nIHfOF5YmfmfWjsaweEGALJmqB -okGu/lGNGf3XoYzy0/hC3WAqk3znSZtQLUq8jEWF7dLNUizUeUow ------END RSA PRIVATE KEY----- ------BEGIN CERTIFICATE----- -MIIDKjCCAhICCQDWE0gtDvld0DANBgkqhkiG9w0BAQUFADBVMQswCQYDVQQGEwJB -VTETMBEGA1UECBMKU29tZS1TdGF0ZTEbMBkGA1UEChMSTXkgQm9ndXMgUm9vdCBD -ZXJ0MRQwEgYDVQQDEwtleGFtcGxlLmNvbTAeFw0xNTEyMDQyMDU0MzFaFw00MDEy -MDMyMDU0MzFaMFkxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEw -HwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMTCWxvY2Fs -aG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2WIIi86Mis4UQH -a5PrFbX2PBtQHbI3t3ekN1CewRsgQ/2X3lCeWhKmr3CJYXVgA7q/23PORQAiuV6y -DG2dQIrjeahWCXaCptTi49ljfVRTW2IxrHke/iA8TkDuZbWGzVLb8TB83ipBOD41 -SjuomoN4A/ktnIfbNqRqgjjHs2wwJHDfxPiCQlwyOayjHmdlh8cqfVE8rWEm5/3T -Iu0X1J53SammR1SbUmsLJNofxFYMK1ogHb0CaFEG9QuuUDPJl5K74Rr6InMQZKPn -ne4W3cGoALxPHAca7yicpSMSmdsmd6pqylc2Fdua7o/wf0SwShxS4A1DqA/HWLEM -V6MSEF8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAz5sMAFG6W/ZEULZITkBTCU6P -NttpGiKufnqyBW5HyNylaczfnHnClvQjr8f/84xvKVcfC3xP0lz+92aIQqo+5L/n -v7gLhBFR4Vr2XwMt2qz2FpkaxmVwnhVAHaaC05WIKQ6W2gDwWT0u1K8YdTh+7mvN -AT9FW4vDgtNZWq4W/PePh9QCiOOQhGOuBYj/7zqLtz4XPifhi66ILIRDHiu0kond -3YMFcECIAf4MPT9vT0iNcWX+c8CfAixPt8nMD6bzOo3oTcfuZh/2enfgLbMqOlOi -uk72FM5VVPXTWAckJvL/vVjqsvDuJQKqbr0oUc3bdWbS36xtWZUycp4IQLguAQ== ------END CERTIFICATE----- diff --git a/letsencrypt-auto-source/tests/fake-letsencrypt/dist/letsencrypt-99.9.9.tar.gz b/letsencrypt-auto-source/tests/fake-letsencrypt/dist/letsencrypt-99.9.9.tar.gz deleted file mode 100644 index 5f9a48a34a2..00000000000 Binary files a/letsencrypt-auto-source/tests/fake-letsencrypt/dist/letsencrypt-99.9.9.tar.gz and /dev/null differ diff --git a/letsencrypt-auto-source/tests/fake-letsencrypt/letsencrypt.py b/letsencrypt-auto-source/tests/fake-letsencrypt/letsencrypt.py deleted file mode 100755 index 9d811fab527..00000000000 --- a/letsencrypt-auto-source/tests/fake-letsencrypt/letsencrypt.py +++ /dev/null @@ -1,8 +0,0 @@ -from sys import argv, stderr - - -def main(): - """Act like letsencrypt --version insofar as printing the version number to - stderr.""" - if '--version' in argv: - stderr.write('letsencrypt 99.9.9\n') diff --git a/letsencrypt-auto-source/tests/fake-letsencrypt/setup.py b/letsencrypt-auto-source/tests/fake-letsencrypt/setup.py deleted file mode 100644 index e5f7fde350e..00000000000 --- a/letsencrypt-auto-source/tests/fake-letsencrypt/setup.py +++ /dev/null @@ -1,12 +0,0 @@ -from setuptools import setup - - -setup( - name='letsencrypt', - version='99.9.9', - description='A mock version of letsencrypt that just prints its version', - py_modules=['letsencrypt'], - entry_points={ - 'console_scripts': ['letsencrypt = letsencrypt:main'] - } -) diff --git a/letsencrypt-auto-source/tests/signing.key b/letsencrypt-auto-source/tests/signing.key deleted file mode 100644 index b9964d00cbe..00000000000 --- a/letsencrypt-auto-source/tests/signing.key +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEpQIBAAKCAQEAsMoSzLYQ7E1sdSOkwelgtzKIh2qi3bpXuYtcfFC0XrvWig07 -1NwIj+dZiT0OLZ2hPispEH0B7ISuuWg1ll7GhFW0VdbxL6JdGzS2ShNWkX9hE9z+ -j8VqwDPOBn3ZHm03qwpYkBDwQib3KqOdYbTTuUtJmmGcuk3a9Aq/sCT6DdfmTSdP -5asdQYwIcaQreDrOosaS84DTWI3IU+UYJVglLsIVPBuy9IcgHidUQ96hJnoPsDCW -sHwX62495QKEarauyKQrJzFes0EY95orDM47Z5o/NDiQB11m91yNB0MmPYY9QSbn -OA9j7IaaC97AwRLuwXY+/R2ablTcxurWou68iQIDAQABAoIBAQCJE3W2Mqk2f+XL -geKa1BjAkzcXQJCduYGRhUQlw/HGzoBPtGki56Tf53MeHTAkIGfIq3CAr1zRhiNv -8SQzvrLQIx/buvhxhcQJdzqsfwgNcqXT3/OliF34P3LMx8GUfPy/6xq2Qdv4fvwA -nLJH8wyDTKP6RxtdvUY7GSZ+Ln2QQv/3Nco7tax4GHNGom8iSgeH/YKTDnvitdqh -a0fr930QzU39TfOftLmasdmKUOIg8G2wr4Sy6Kn060+OUoQr1fZF5mnLvvQeILCK -uav91JkIeMLggzk+t88IJUFWdOoxv5hWTnNzHyt+/GYfovyRz2fKQMwzdh1F8iM5 -+867rEb9AoGBANn1ncemJBedDshStdCBUH0+2ExPrawveaXOZKnx8/VGFXNi0hAf -KzkntMWd5g5kB077FtKO9CYTBvK4pZBWIFLcJEqAz88JeXME6dfUbRucDr72ko+l -rcLHXj7F0IDVzj/9CphMGAhC9J/4YW9SPcSbMw6dQ6xOk73f1Vowve0DAoGBAM+k -/F+hVqCS3f22Bg9KuDtx+zCydaZxC842DgIkV1SO2iFhNHjnpQ5EIR0WrSYeV2n+ -rD7kVs5OH1HvnGScHaQKtAVqZClSwF14jzE+Aj8XDwxiHLSOhJgKlzfVX7h1ymMh -7fsslDl6xNGQ+40gubhkCLT5qABFKy1mrZ8b+3yDAoGAGLGUI6d2FVrM7vM3+Bx+ -gwIYvWSVl5l1XcypaPupmRNMoNsEU6FEY2BVQcJm6yB4F4GpD0f0709ejSdQUq7/ -UIPydKJtaNZ49QgMelBt4B/pJ8eFyVKLAjNWQSRmQAJ5MJS5m5Gbc2wqjOk2GMen -idvPiAtXPHFWmb9/S42UJwMCgYEAjymAe2qgcGtyNNfIC8kHhqzKdEPGi/ALJKzu -MZnewEURrcv4QpfrnA9rCUQ2Mz7eJA1bsqz6EJmaTIK4wEFGynA6uDUnQ7pzOL7D -cz7+i4MZc/89LVvJnY5Hvk4WBfboiDq/etq8g3jatGaSmTYD9la6DhTHORB3eYD+ -meHQHYMCgYEA18y9hnx2k4vNeBei4YXF4pAvKdwKLQD+CcP9ljb3VT+kXktjRA1C -aWj3HhMwvcxtttfkQzEnwwGRAkTEtNewJ8KFxhmc9nYElZTNZ+SuHD5Dkv8xqoj8 -NvG8rU1eiEyPwE2wQxpM5JLqbo7IWtR0dmptjKoF1gRxn6Wh4TwEiHA= ------END RSA PRIVATE KEY----- diff --git a/letsencrypt-auto-source/version.py b/letsencrypt-auto-source/version.py deleted file mode 100755 index c49d96654c7..00000000000 --- a/letsencrypt-auto-source/version.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python -"""Get the current Certbot version number. - -Provides simple utilities for determining the Certbot version number and -building letsencrypt-auto. - -""" -from __future__ import print_function -from os.path import abspath, dirname, join -import re - - -def certbot_version(build_script_dir): - """Return the version number stamped in certbot/__init__.py.""" - return re.search('''^__version__ = ['"](.+)['"].*''', - file_contents(join(dirname(build_script_dir), - 'certbot', - '__init__.py')), - re.M).group(1) - - -def file_contents(path): - with open(path) as file: - return file.read() - - -if __name__ == '__main__': - print(certbot_version(dirname(abspath(__file__)))) diff --git a/letshelp-certbot/LICENSE.txt b/letshelp-certbot/LICENSE.txt deleted file mode 100644 index 981c46c9f93..00000000000 --- a/letshelp-certbot/LICENSE.txt +++ /dev/null @@ -1,190 +0,0 @@ - Copyright 2015 Electronic Frontier Foundation and others - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS diff --git a/letshelp-certbot/MANIFEST.in b/letshelp-certbot/MANIFEST.in deleted file mode 100644 index 623392f2807..00000000000 --- a/letshelp-certbot/MANIFEST.in +++ /dev/null @@ -1,4 +0,0 @@ -include LICENSE.txt -include README.rst -recursive-include docs * -recursive-include letshelp_certbot/testdata * diff --git a/letshelp-certbot/README.rst b/letshelp-certbot/README.rst deleted file mode 100644 index bbe2f257019..00000000000 --- a/letshelp-certbot/README.rst +++ /dev/null @@ -1 +0,0 @@ -Let's help Certbot client diff --git a/letshelp-certbot/docs/.gitignore b/letshelp-certbot/docs/.gitignore deleted file mode 100644 index ba65b13af5e..00000000000 --- a/letshelp-certbot/docs/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/_build/ diff --git a/letshelp-certbot/docs/Makefile b/letshelp-certbot/docs/Makefile deleted file mode 100644 index 4b392ab8dfb..00000000000 --- a/letshelp-certbot/docs/Makefile +++ /dev/null @@ -1,192 +0,0 @@ -# Makefile for Sphinx documentation -# - -# You can set these variables from the command line. -SPHINXOPTS = -SPHINXBUILD = sphinx-build -PAPER = -BUILDDIR = _build - -# User-friendly check for sphinx-build -ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) -$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) -endif - -# Internal variables. -PAPEROPT_a4 = -D latex_paper_size=a4 -PAPEROPT_letter = -D latex_paper_size=letter -ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . -# the i18n builder cannot share the environment and doctrees with the others -I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . - -.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext - -help: - @echo "Please use \`make ' where is one of" - @echo " html to make standalone HTML files" - @echo " dirhtml to make HTML files named index.html in directories" - @echo " singlehtml to make a single large HTML file" - @echo " pickle to make pickle files" - @echo " json to make JSON files" - @echo " htmlhelp to make HTML files and a HTML help project" - @echo " qthelp to make HTML files and a qthelp project" - @echo " applehelp to make an Apple Help Book" - @echo " devhelp to make HTML files and a Devhelp project" - @echo " epub to make an epub" - @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" - @echo " latexpdf to make LaTeX files and run them through pdflatex" - @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" - @echo " text to make text files" - @echo " man to make manual pages" - @echo " texinfo to make Texinfo files" - @echo " info to make Texinfo files and run them through makeinfo" - @echo " gettext to make PO message catalogs" - @echo " changes to make an overview of all changed/added/deprecated items" - @echo " xml to make Docutils-native XML files" - @echo " pseudoxml to make pseudoxml-XML files for display purposes" - @echo " linkcheck to check all external links for integrity" - @echo " doctest to run all doctests embedded in the documentation (if enabled)" - @echo " coverage to run coverage check of the documentation (if enabled)" - -clean: - rm -rf $(BUILDDIR)/* - -html: - $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." - -dirhtml: - $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml - @echo - @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." - -singlehtml: - $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml - @echo - @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." - -pickle: - $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle - @echo - @echo "Build finished; now you can process the pickle files." - -json: - $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json - @echo - @echo "Build finished; now you can process the JSON files." - -htmlhelp: - $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp - @echo - @echo "Build finished; now you can run HTML Help Workshop with the" \ - ".hhp project file in $(BUILDDIR)/htmlhelp." - -qthelp: - $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp - @echo - @echo "Build finished; now you can run "qcollectiongenerator" with the" \ - ".qhcp project file in $(BUILDDIR)/qthelp, like this:" - @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/letshelp-certbot.qhcp" - @echo "To view the help file:" - @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/letshelp-certbot.qhc" - -applehelp: - $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp - @echo - @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." - @echo "N.B. You won't be able to view it unless you put it in" \ - "~/Library/Documentation/Help or install it in your application" \ - "bundle." - -devhelp: - $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp - @echo - @echo "Build finished." - @echo "To view the help file:" - @echo "# mkdir -p $$HOME/.local/share/devhelp/letshelp-certbot" - @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/letshelp-certbot" - @echo "# devhelp" - -epub: - $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub - @echo - @echo "Build finished. The epub file is in $(BUILDDIR)/epub." - -latex: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo - @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." - @echo "Run \`make' in that directory to run these through (pdf)latex" \ - "(use \`make latexpdf' here to do that automatically)." - -latexpdf: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through pdflatex..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -latexpdfja: - $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex - @echo "Running LaTeX files through platex and dvipdfmx..." - $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja - @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." - -text: - $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text - @echo - @echo "Build finished. The text files are in $(BUILDDIR)/text." - -man: - $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man - @echo - @echo "Build finished. The manual pages are in $(BUILDDIR)/man." - -texinfo: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo - @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." - @echo "Run \`make' in that directory to run these through makeinfo" \ - "(use \`make info' here to do that automatically)." - -info: - $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo - @echo "Running Texinfo files through makeinfo..." - make -C $(BUILDDIR)/texinfo info - @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." - -gettext: - $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale - @echo - @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." - -changes: - $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes - @echo - @echo "The overview file is in $(BUILDDIR)/changes." - -linkcheck: - $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck - @echo - @echo "Link check complete; look for any errors in the above output " \ - "or in $(BUILDDIR)/linkcheck/output.txt." - -doctest: - $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest - @echo "Testing of doctests in the sources finished, look at the " \ - "results in $(BUILDDIR)/doctest/output.txt." - -coverage: - $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage - @echo "Testing of coverage in the sources finished, look at the " \ - "results in $(BUILDDIR)/coverage/python.txt." - -xml: - $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml - @echo - @echo "Build finished. The XML files are in $(BUILDDIR)/xml." - -pseudoxml: - $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml - @echo - @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/letshelp-certbot/docs/api.rst b/letshelp-certbot/docs/api.rst deleted file mode 100644 index 8668ec5d8ad..00000000000 --- a/letshelp-certbot/docs/api.rst +++ /dev/null @@ -1,8 +0,0 @@ -================= -API Documentation -================= - -.. toctree:: - :glob: - - api/** diff --git a/letshelp-certbot/docs/api/index.rst b/letshelp-certbot/docs/api/index.rst deleted file mode 100644 index 5ced5f501d1..00000000000 --- a/letshelp-certbot/docs/api/index.rst +++ /dev/null @@ -1,11 +0,0 @@ -:mod:`letshelp_certbot` ---------------------------- - -.. automodule:: letshelp_certbot - :members: - -:mod:`letshelp_certbot.apache` -================================== - -.. automodule:: letshelp_certbot.apache - :members: diff --git a/letshelp-certbot/docs/conf.py b/letshelp-certbot/docs/conf.py deleted file mode 100644 index 17d8b3ea9fe..00000000000 --- a/letshelp-certbot/docs/conf.py +++ /dev/null @@ -1,311 +0,0 @@ -# -*- coding: utf-8 -*- -# -# letshelp-certbot documentation build configuration file, created by -# sphinx-quickstart on Sun Oct 18 13:40:19 2015. -# -# This file is execfile()d with the current directory set to its -# containing dir. -# -# Note that not all possible configuration values are present in this -# autogenerated file. -# -# All configuration values have a default; values that are commented out -# serve to show the default. - -import sys -import os -import shlex - - -here = os.path.abspath(os.path.dirname(__file__)) - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath(os.path.join(here, '..'))) - -# -- General configuration ------------------------------------------------ - -# If your documentation needs a minimal Sphinx version, state it here. -needs_sphinx = '1.0' - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.viewcode', -] - -autodoc_member_order = 'bysource' -autodoc_default_flags = ['show-inheritance', 'private-members'] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# The suffix(es) of source filenames. -# You can specify multiple suffix as a list of string: -# source_suffix = ['.rst', '.md'] -source_suffix = '.rst' - -# The encoding of source files. -#source_encoding = 'utf-8-sig' - -# The master toctree document. -master_doc = 'index' - -# General information about the project. -project = u'letshelp-certbot' -copyright = u'2014-2015, Let\'s Encrypt Project' -author = u'Certbot Project' - -# The version info for the project you're documenting, acts as replacement for -# |version| and |release|, also used in various other places throughout the -# built documents. -# -# The short X.Y version. -version = '0' -# The full version, including alpha/beta/rc tags. -release = '0' - -# The language for content autogenerated by Sphinx. Refer to documentation -# for a list of supported languages. -# -# This is also used if you do content translation via gettext catalogs. -# Usually you set "language" from the command line for these cases. -language = 'en' - -# There are two options for replacing |today|: either, you set today to some -# non-false value, then it is used: -#today = '' -# Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -exclude_patterns = ['_build'] - -# The reST default role (used for this markup: `text`) to use for all -# documents. -default_role = 'py:obj' - -# If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True - -# If true, the current module name will be prepended to all description -# unit titles (such as .. function::). -#add_module_names = True - -# If true, sectionauthor and moduleauthor directives will be shown in the -# output. They are ignored by default. -#show_authors = False - -# The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' - -# A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] - -# If true, keep warnings as "system message" paragraphs in the built documents. -#keep_warnings = False - -# If true, `todo` and `todoList` produce output, else they produce nothing. -todo_include_todos = True - - -# -- Options for HTML output ---------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. - -# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs -# on_rtd is whether we are on readthedocs.org -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: # only import and set the theme if we're building docs locally - import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# otherwise, readthedocs.org uses their theme by default, so no need to specify it - -# Theme options are theme-specific and customize the look and feel of a theme -# further. For a list of options available for each theme, see the -# documentation. -#html_theme_options = {} - -# Add any paths that contain custom themes here, relative to this directory. -#html_theme_path = [] - -# The name for this set of Sphinx documents. If None, it defaults to -# " v documentation". -#html_title = None - -# A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None - -# The name of an image file (relative to this directory) to place at the top -# of the sidebar. -#html_logo = None - -# The name of an image file (within the static path) to use as favicon of the -# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 -# pixels large. -#html_favicon = None - -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] - -# Add any extra paths that contain custom files (such as robots.txt or -# .htaccess) here, relative to this directory. These files are copied -# directly to the root of the documentation. -#html_extra_path = [] - -# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, -# using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' - -# If true, SmartyPants will be used to convert quotes and dashes to -# typographically correct entities. -#html_use_smartypants = True - -# Custom sidebar templates, maps document names to template names. -#html_sidebars = {} - -# Additional templates that should be rendered to pages, maps page names to -# template names. -#html_additional_pages = {} - -# If false, no module index is generated. -#html_domain_indices = True - -# If false, no index is generated. -#html_use_index = True - -# If true, the index is split into individual pages for each letter. -#html_split_index = False - -# If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True - -# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True - -# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True - -# If true, an OpenSearch description file will be output, and all pages will -# contain a tag referring to it. The value of this option must be the -# base URL from which the finished HTML is served. -#html_use_opensearch = '' - -# This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None - -# Language to be used for generating the HTML full-text search index. -# Sphinx supports the following languages: -# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' -# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' -#html_search_language = 'en' - -# A dictionary with options for the search language support, empty by default. -# Now only 'ja' uses this config value -#html_search_options = {'type': 'default'} - -# The name of a javascript file (relative to the configuration directory) that -# implements a search results scorer. If empty, the default will be used. -#html_search_scorer = 'scorer.js' - -# Output file base name for HTML help builder. -htmlhelp_basename = 'letshelp-certbotdoc' - -# -- Options for LaTeX output --------------------------------------------- - -latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - #'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - #'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - #'preamble': '', - - # Latex figure (float) alignment - #'figure_align': 'htbp', -} - -# Grouping the document tree into LaTeX files. List of tuples -# (source start file, target name, title, -# author, documentclass [howto, manual, or own class]). -latex_documents = [ - (master_doc, 'letshelp-certbot.tex', u'letshelp-certbot Documentation', - u'Certbot Project', 'manual'), -] - -# The name of an image file (relative to this directory) to place at the top of -# the title page. -#latex_logo = None - -# For "manual" documents, if this is true, then toplevel headings are parts, -# not chapters. -#latex_use_parts = False - -# If true, show page references after internal links. -#latex_show_pagerefs = False - -# If true, show URL addresses after external links. -#latex_show_urls = False - -# Documents to append as an appendix to all manuals. -#latex_appendices = [] - -# If false, no module index is generated. -#latex_domain_indices = True - - -# -- Options for manual page output --------------------------------------- - -# One entry per manual page. List of tuples -# (source start file, name, description, authors, manual section). -man_pages = [ - (master_doc, 'letshelp-certbot', u'letshelp-certbot Documentation', - [author], 1) -] - -# If true, show URL addresses after external links. -#man_show_urls = False - - -# -- Options for Texinfo output ------------------------------------------- - -# Grouping the document tree into Texinfo files. List of tuples -# (source start file, target name, title, author, -# dir menu entry, description, category) -texinfo_documents = [ - (master_doc, 'letshelp-certbot', u'letshelp-certbot Documentation', - author, 'letshelp-certbot', 'One line description of project.', - 'Miscellaneous'), -] - -# Documents to append as an appendix to all manuals. -#texinfo_appendices = [] - -# If false, no module index is generated. -#texinfo_domain_indices = True - -# How to display URL addresses: 'footnote', 'no', or 'inline'. -#texinfo_show_urls = 'footnote' - -# If true, do not generate a @detailmenu in the "Top" node's menu. -#texinfo_no_detailmenu = False - - -intersphinx_mapping = { - 'python': ('https://docs.python.org/', None), - 'acme': ('https://acme-python.readthedocs.org/en/latest/', None), - 'certbot': ('https://certbot.eff.org/docs/', None), -} diff --git a/letshelp-certbot/docs/index.rst b/letshelp-certbot/docs/index.rst deleted file mode 100644 index 678d9be2e2c..00000000000 --- a/letshelp-certbot/docs/index.rst +++ /dev/null @@ -1,27 +0,0 @@ -.. letshelp-certbot documentation master file, created by - sphinx-quickstart on Sun Oct 18 13:40:19 2015. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -Welcome to letshelp-certbot's documentation! -================================================ - -Contents: - -.. toctree:: - :maxdepth: 2 - - -.. toctree:: - :maxdepth: 1 - - api - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` - diff --git a/letshelp-certbot/docs/make.bat b/letshelp-certbot/docs/make.bat deleted file mode 100644 index 0229b4f69a5..00000000000 --- a/letshelp-certbot/docs/make.bat +++ /dev/null @@ -1,263 +0,0 @@ -@ECHO OFF - -REM Command file for Sphinx documentation - -if "%SPHINXBUILD%" == "" ( - set SPHINXBUILD=sphinx-build -) -set BUILDDIR=_build -set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . -set I18NSPHINXOPTS=%SPHINXOPTS% . -if NOT "%PAPER%" == "" ( - set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% - set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% -) - -if "%1" == "" goto help - -if "%1" == "help" ( - :help - echo.Please use `make ^` where ^ is one of - echo. html to make standalone HTML files - echo. dirhtml to make HTML files named index.html in directories - echo. singlehtml to make a single large HTML file - echo. pickle to make pickle files - echo. json to make JSON files - echo. htmlhelp to make HTML files and a HTML help project - echo. qthelp to make HTML files and a qthelp project - echo. devhelp to make HTML files and a Devhelp project - echo. epub to make an epub - echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter - echo. text to make text files - echo. man to make manual pages - echo. texinfo to make Texinfo files - echo. gettext to make PO message catalogs - echo. changes to make an overview over all changed/added/deprecated items - echo. xml to make Docutils-native XML files - echo. pseudoxml to make pseudoxml-XML files for display purposes - echo. linkcheck to check all external links for integrity - echo. doctest to run all doctests embedded in the documentation if enabled - echo. coverage to run coverage check of the documentation if enabled - goto end -) - -if "%1" == "clean" ( - for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i - del /q /s %BUILDDIR%\* - goto end -) - - -REM Check if sphinx-build is available and fallback to Python version if any -%SPHINXBUILD% 2> nul -if errorlevel 9009 goto sphinx_python -goto sphinx_ok - -:sphinx_python - -set SPHINXBUILD=python -m sphinx.__init__ -%SPHINXBUILD% 2> nul -if errorlevel 9009 ( - echo. - echo.The 'sphinx-build' command was not found. Make sure you have Sphinx - echo.installed, then set the SPHINXBUILD environment variable to point - echo.to the full path of the 'sphinx-build' executable. Alternatively you - echo.may add the Sphinx directory to PATH. - echo. - echo.If you don't have Sphinx installed, grab it from - echo.http://sphinx-doc.org/ - exit /b 1 -) - -:sphinx_ok - - -if "%1" == "html" ( - %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/html. - goto end -) - -if "%1" == "dirhtml" ( - %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. - goto end -) - -if "%1" == "singlehtml" ( - %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. - goto end -) - -if "%1" == "pickle" ( - %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the pickle files. - goto end -) - -if "%1" == "json" ( - %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can process the JSON files. - goto end -) - -if "%1" == "htmlhelp" ( - %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run HTML Help Workshop with the ^ -.hhp project file in %BUILDDIR%/htmlhelp. - goto end -) - -if "%1" == "qthelp" ( - %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; now you can run "qcollectiongenerator" with the ^ -.qhcp project file in %BUILDDIR%/qthelp, like this: - echo.^> qcollectiongenerator %BUILDDIR%\qthelp\letshelp-certbot.qhcp - echo.To view the help file: - echo.^> assistant -collectionFile %BUILDDIR%\qthelp\letshelp-certbot.ghc - goto end -) - -if "%1" == "devhelp" ( - %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. - goto end -) - -if "%1" == "epub" ( - %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The epub file is in %BUILDDIR%/epub. - goto end -) - -if "%1" == "latex" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - if errorlevel 1 exit /b 1 - echo. - echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdf" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "latexpdfja" ( - %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex - cd %BUILDDIR%/latex - make all-pdf-ja - cd %~dp0 - echo. - echo.Build finished; the PDF files are in %BUILDDIR%/latex. - goto end -) - -if "%1" == "text" ( - %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The text files are in %BUILDDIR%/text. - goto end -) - -if "%1" == "man" ( - %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The manual pages are in %BUILDDIR%/man. - goto end -) - -if "%1" == "texinfo" ( - %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. - goto end -) - -if "%1" == "gettext" ( - %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The message catalogs are in %BUILDDIR%/locale. - goto end -) - -if "%1" == "changes" ( - %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes - if errorlevel 1 exit /b 1 - echo. - echo.The overview file is in %BUILDDIR%/changes. - goto end -) - -if "%1" == "linkcheck" ( - %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck - if errorlevel 1 exit /b 1 - echo. - echo.Link check complete; look for any errors in the above output ^ -or in %BUILDDIR%/linkcheck/output.txt. - goto end -) - -if "%1" == "doctest" ( - %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest - if errorlevel 1 exit /b 1 - echo. - echo.Testing of doctests in the sources finished, look at the ^ -results in %BUILDDIR%/doctest/output.txt. - goto end -) - -if "%1" == "coverage" ( - %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage - if errorlevel 1 exit /b 1 - echo. - echo.Testing of coverage in the sources finished, look at the ^ -results in %BUILDDIR%/coverage/python.txt. - goto end -) - -if "%1" == "xml" ( - %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The XML files are in %BUILDDIR%/xml. - goto end -) - -if "%1" == "pseudoxml" ( - %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml - if errorlevel 1 exit /b 1 - echo. - echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. - goto end -) - -:end diff --git a/letshelp-certbot/letshelp_certbot/__init__.py b/letshelp-certbot/letshelp_certbot/__init__.py deleted file mode 100644 index 6882a19d4ac..00000000000 --- a/letshelp-certbot/letshelp_certbot/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Tools for submitting server configurations""" diff --git a/letshelp-certbot/letshelp_certbot/apache.py b/letshelp-certbot/letshelp_certbot/apache.py deleted file mode 100755 index 50f3c5ef64b..00000000000 --- a/letshelp-certbot/letshelp_certbot/apache.py +++ /dev/null @@ -1,314 +0,0 @@ -#!/usr/bin/env python -"""Certbot Apache configuration submission script""" - -from __future__ import print_function - -import argparse -import atexit -import os -import re -import shutil -import subprocess -import sys -import tarfile -import tempfile -import textwrap - -import six - -from letshelp_certbot.magic_typing import List # pylint: disable=unused-import, no-name-in-module - -_DESCRIPTION = """ -Let's Help is a simple script you can run to help out the Certbot -project. Since Certbot will support automatically configuring HTTPS on -many servers, we want to test this functionality on as many configurations as -possible. This script will create a sanitized copy of your Apache -configuration, notifying you of the files that have been selected. If (and only -if) you approve this selection, these files will be sent to the Certbot -developers. - -""" - - -_NO_APACHECTL = """ -Unable to find `apachectl` which is required for this script to work. If it is -installed, please run this script again with the --apache-ctl command line -argument and the path to the binary. - -""" - - -# Keywords likely to be found in filenames of sensitive files -_SENSITIVE_FILENAME_REGEX = re.compile(r"^(?!.*proxy_fdpass).*pass.*$|private|" - r"secret|^(?!.*certbot).*cert.*$|crt|" - r"key|rsa|dsa|pw|\.pem|\.der|\.p12|" - r"\.pfx|\.p7b") - - -def make_and_verify_selection(server_root, temp_dir): - """Copies server_root to temp_dir and verifies selection with the user - - :param str server_root: Path to the Apache server root - :param str temp_dir: Path to the temporary directory to copy files to - - """ - copied_files, copied_dirs = copy_config(server_root, temp_dir) - - print(textwrap.fill("A secure copy of the files that have been selected " - "for submission has been created under {0}. All " - "comments have been removed and the files are only " - "accessible by the current user. A list of the files " - "that have been included is shown below. Please make " - "sure that this selection does not contain private " - "keys, passwords, or any other sensitive " - "information.".format(temp_dir))) - print("\nFiles:") - for copied_file in copied_files: - print(copied_file) - print("Directories (including all contained files):") - for copied_dir in copied_dirs: - print(copied_dir) - - sys.stdout.write("\nIs it safe to submit these files? ") - while True: - ans = six.moves.input("(Y)es/(N)o: ").lower() - if ans.startswith("y"): - return - elif ans.startswith("n"): - sys.exit("Your files were not submitted") - - -def copy_config(server_root, temp_dir): - """Safely copies server_root to temp_dir and returns copied files - - :param str server_root: Absolute path to the Apache server root - :param str temp_dir: Path to the temporary directory to copy files to - - :returns: List of copied files and a list of leaf directories where - all contained files were copied - :rtype: `tuple` of `list` of `str` - - """ - copied_files = [] # type: List[str] - copied_dirs = [] # type: List[str] - dir_len = len(os.path.dirname(server_root)) - - for config_path, config_dirs, config_files in os.walk(server_root): - temp_path = os.path.join(temp_dir, config_path[dir_len + 1:]) - os.mkdir(temp_path) - - copied_all = True - copied_files_in_current_dir = [] - for config_file in config_files: - config_file_path = os.path.join(config_path, config_file) - temp_file_path = os.path.join(temp_path, config_file) - if os.path.islink(config_file_path): - os.symlink(os.readlink(config_file_path), temp_file_path) - elif safe_config_file(config_file_path): - copy_file_without_comments(config_file_path, temp_file_path) - copied_files_in_current_dir.append(config_file_path) - else: - copied_all = False - - # If copied all files in leaf directory - if copied_all and not config_dirs: - copied_dirs.append(config_path) - else: - copied_files += copied_files_in_current_dir - - return copied_files, copied_dirs - - -def copy_file_without_comments(source, destination): - """Copies source to destination, removing comments - - :param str source: Path to the file to be copied - :param str destination: Path where source should be copied to - - """ - with open(source, "r") as infile: - with open(destination, "w") as outfile: - for line in infile: - if not (line.isspace() or line.lstrip().startswith("#")): - outfile.write(line) - - -def safe_config_file(config_file): - """Returns True if config_file can be safely copied - - :param str config_file: Path to an Apache configuration file - - :returns: True if config_file can be safely copied - :rtype: bool - - """ - config_file_lower = config_file.lower() - if _SENSITIVE_FILENAME_REGEX.search(config_file_lower): - return False - - proc = subprocess.Popen(["file", config_file], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True) - file_output, _ = proc.communicate() - - if "ASCII" in file_output: - possible_password_file = empty_or_all_comments = True - with open(config_file) as config_fd: - for line in config_fd: - if not (line.isspace() or line.lstrip().startswith("#")): - empty_or_all_comments = False - if line.startswith("-----BEGIN"): - return False - elif ":" not in line: - possible_password_file = False - # If file isn't empty or commented out and could be a password file, - # don't include it in selection. It is safe to include the file if - # it consists solely of comments because comments are removed before - # submission. - return empty_or_all_comments or not possible_password_file - - return False - - -def setup_tempdir(args): - """Creates a temporary directory and necessary files for config - - :param argparse.Namespace args: Parsed command line arguments - - :returns: Path to temporary directory - :rtype: str - - """ - tempdir = tempfile.mkdtemp() - - with open(os.path.join(tempdir, "config_file"), "w") as config_fd: - config_fd.write(args.config_file + "\n") - - proc = subprocess.Popen([args.apache_ctl, "-v"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True) - with open(os.path.join(tempdir, "version"), "w") as version_fd: - version_fd.write(proc.communicate()[0]) - - proc = subprocess.Popen([args.apache_ctl, "-d", args.server_root, "-f", - args.config_file, "-M"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True) - with open(os.path.join(tempdir, "modules"), "w") as modules_fd: - modules_fd.write(proc.communicate()[0]) - - proc = subprocess.Popen([args.apache_ctl, "-d", args.server_root, "-f", - args.config_file, "-t", "-D", "DUMP_VHOSTS"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True) - with open(os.path.join(tempdir, "vhosts"), "w") as vhosts_fd: - vhosts_fd.write(proc.communicate()[0]) - - return tempdir - - -def verify_config(args): - """Verifies server_root and config_file specify a valid config - - :param argparse.Namespace args: Parsed command line arguments - - """ - with open(os.devnull, "w") as devnull: - try: - subprocess.check_call([args.apache_ctl, "-d", args.server_root, - "-f", args.config_file, "-t"], - stdout=devnull, stderr=subprocess.STDOUT) - except OSError: - sys.exit(_NO_APACHECTL) - except subprocess.CalledProcessError: - sys.exit("Syntax check from apachectl failed") - - -def locate_config(apache_ctl): - """Uses the apachectl binary to find configuration files - - :param str apache_ctl: Path to `apachectl` binary - - - :returns: Path to Apache server root and main configuration file - :rtype: `tuple` of `str` - - """ - try: - proc = subprocess.Popen([apache_ctl, "-V"], - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True) - output, _ = proc.communicate() - except OSError: - sys.exit(_NO_APACHECTL) - - server_root = config_file = "" - for line in output.splitlines(): - # Relevant output lines are of the form: -D DIRECTIVE="VALUE" - if "HTTPD_ROOT" in line: - server_root = line[line.find('"') + 1:-1] - elif "SERVER_CONFIG_FILE" in line: - config_file = line[line.find('"') + 1:-1] - - if not (server_root and config_file): - sys.exit("Unable to locate Apache configuration. Please run this " - "script again and specify --server-root and --config-file") - - return server_root, config_file - - -def get_args(): - """Parses command line arguments - - :returns: Parsed command line options - :rtype: argparse.Namespace - - """ - parser = argparse.ArgumentParser(description=_DESCRIPTION) - parser.add_argument("-c", "--apache-ctl", default="apachectl", - help="path to the `apachectl` binary") - parser.add_argument("-d", "--server-root", - help=("location of the root directory of your Apache " - "configuration")) - parser.add_argument("-f", "--config-file", - help=("location of your main Apache configuration " - "file relative to the server root")) - args = parser.parse_args() - - # args.server_root XOR args.config_file - if bool(args.server_root) != bool(args.config_file): - sys.exit("If either --server-root and --config-file are specified, " - "they both must be included") - elif args.server_root and args.config_file: - args.server_root = os.path.abspath(args.server_root) - args.config_file = os.path.abspath(args.config_file) - - if args.config_file.startswith(args.server_root): - args.config_file = args.config_file[len(args.server_root) + 1:] - else: - sys.exit("This script expects the Apache configuration file to be " - "inside the server root") - - return args - - -def main(): - """Main script execution""" - args = get_args() - if args.server_root is None: - args.server_root, args.config_file = locate_config(args.apache_ctl) - - verify_config(args) - tempdir = setup_tempdir(args) - atexit.register(lambda: shutil.rmtree(tempdir)) - make_and_verify_selection(args.server_root, tempdir) - - tarpath = os.path.join(tempdir, "config.tar.gz") - with tarfile.open(tarpath, mode="w:gz") as tar: - tar.add(tempdir, arcname=".") - - # TODO: Submit tarpath - - -if __name__ == "__main__": - main() # pragma: no cover diff --git a/letshelp-certbot/letshelp_certbot/apache_test.py b/letshelp-certbot/letshelp_certbot/apache_test.py deleted file mode 100644 index a1115bc0695..00000000000 --- a/letshelp-certbot/letshelp_certbot/apache_test.py +++ /dev/null @@ -1,242 +0,0 @@ -"""Tests for letshelp.letshelp_certbot_apache.py""" -import argparse -import functools -import os -import pkg_resources -import subprocess -import tarfile -import tempfile -import unittest - -import mock -# six is used in mock.patch() -import six # pylint: disable=unused-import - -import letshelp_certbot.apache as letshelp_le_apache - - -_PARTIAL_CONF_PATH = os.path.join("mods-available", "ssl.load") -_PARTIAL_LINK_PATH = os.path.join("mods-enabled", "ssl.load") -_CONFIG_FILE = pkg_resources.resource_filename( - __name__, os.path.join("testdata", _PARTIAL_CONF_PATH)) -_PASSWD_FILE = pkg_resources.resource_filename( - __name__, os.path.join("testdata", "uncommonly_named_p4sswd")) -_KEY_FILE = pkg_resources.resource_filename( - __name__, os.path.join("testdata", "uncommonly_named_k3y")) -_SECRET_FILE = pkg_resources.resource_filename( - __name__, os.path.join("testdata", "super_secret_file.txt")) - - -_MODULE_NAME = "letshelp_certbot.apache" - - -_COMPILE_SETTINGS = """Server version: Apache/2.4.10 (Debian) -Server built: Mar 15 2015 09:51:43 -Server's Module Magic Number: 20120211:37 -Server loaded: APR 1.5.1, APR-UTIL 1.5.4 -Compiled using: APR 1.5.1, APR-UTIL 1.5.4 -Architecture: 64-bit -Server MPM: event - threaded: yes (fixed thread count) - forked: yes (variable process count) -Server compiled with.... - -D APR_HAS_SENDFILE - -D APR_HAS_MMAP - -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) - -D APR_USE_SYSVSEM_SERIALIZE - -D APR_USE_PTHREAD_SERIALIZE - -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT - -D APR_HAS_OTHER_CHILD - -D AP_HAVE_RELIABLE_PIPED_LOGS - -D DYNAMIC_MODULE_LIMIT=256 - -D HTTPD_ROOT="/etc/apache2" - -D SUEXEC_BIN="/usr/lib/apache2/suexec" - -D DEFAULT_PIDLOG="/var/run/apache2.pid" - -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" - -D DEFAULT_ERRORLOG="logs/error_log" - -D AP_TYPES_CONFIG_FILE="mime.types" - -D SERVER_CONFIG_FILE="apache2.conf" - -""" - - -class LetsHelpApacheTest(unittest.TestCase): - @mock.patch(_MODULE_NAME + ".copy_config") - def test_make_and_verify_selection(self, mock_copy_config): - mock_copy_config.return_value = (["apache2.conf"], ["apache2"]) - - with mock.patch("six.moves.input") as mock_input: - with mock.patch(_MODULE_NAME + ".sys.stdout"): - mock_input.side_effect = ["Yes", "No"] - letshelp_le_apache.make_and_verify_selection("root", "temp") - self.assertRaises( - SystemExit, letshelp_le_apache.make_and_verify_selection, - "server_root", "temp_dir") - - def test_copy_config(self): - tempdir = tempfile.mkdtemp() - server_root = pkg_resources.resource_filename(__name__, "testdata") - letshelp_le_apache.copy_config(server_root, tempdir) - - temp_testdata = os.path.join(tempdir, "testdata") - self.assertFalse(os.path.exists(os.path.join( - temp_testdata, os.path.basename(_PASSWD_FILE)))) - self.assertFalse(os.path.exists(os.path.join( - temp_testdata, os.path.basename(_KEY_FILE)))) - self.assertFalse(os.path.exists(os.path.join( - temp_testdata, os.path.basename(_SECRET_FILE)))) - self.assertTrue(os.path.exists(os.path.join( - temp_testdata, _PARTIAL_CONF_PATH))) - self.assertTrue(os.path.exists(os.path.join( - temp_testdata, _PARTIAL_LINK_PATH))) - - def test_copy_file_without_comments(self): - dest = tempfile.mkstemp()[1] - letshelp_le_apache.copy_file_without_comments(_PASSWD_FILE, dest) - - with open(_PASSWD_FILE) as original: - with open(dest) as copy: - for original_line, copied_line in zip(original, copy): - self.assertEqual(original_line, copied_line) - - @mock.patch(_MODULE_NAME + ".subprocess.Popen") - def test_safe_config_file(self, mock_popen): - mock_popen().communicate.return_value = ("PEM RSA private key", None) - self.assertFalse(letshelp_le_apache.safe_config_file("filename")) - - mock_popen().communicate.return_value = ("ASCII text", None) - self.assertFalse(letshelp_le_apache.safe_config_file(_PASSWD_FILE)) - self.assertFalse(letshelp_le_apache.safe_config_file(_KEY_FILE)) - self.assertFalse(letshelp_le_apache.safe_config_file(_SECRET_FILE)) - self.assertTrue(letshelp_le_apache.safe_config_file(_CONFIG_FILE)) - - @mock.patch(_MODULE_NAME + ".subprocess.Popen") - def test_tempdir(self, mock_popen): - mock_popen().communicate.side_effect = [ - ("version", None), ("modules", None), ("vhosts", None)] - args = _get_args() - - tempdir = letshelp_le_apache.setup_tempdir(args) - - with open(os.path.join(tempdir, "config_file")) as config_fd: - self.assertEqual(config_fd.read(), args.config_file + "\n") - - with open(os.path.join(tempdir, "version")) as version_fd: - self.assertEqual(version_fd.read(), "version") - - with open(os.path.join(tempdir, "modules")) as modules_fd: - self.assertEqual(modules_fd.read(), "modules") - - with open(os.path.join(tempdir, "vhosts")) as vhosts_fd: - self.assertEqual(vhosts_fd.read(), "vhosts") - - @mock.patch(_MODULE_NAME + ".subprocess.check_call") - def test_verify_config(self, mock_check_call): - args = _get_args() - mock_check_call.side_effect = [ - None, OSError, subprocess.CalledProcessError(1, "apachectl")] - - letshelp_le_apache.verify_config(args) - self.assertRaises(SystemExit, letshelp_le_apache.verify_config, args) - self.assertRaises(SystemExit, letshelp_le_apache.verify_config, args) - - @mock.patch(_MODULE_NAME + ".subprocess.Popen") - def test_locate_config(self, mock_popen): - mock_popen().communicate.side_effect = [ - OSError, ("bad_output", None), (_COMPILE_SETTINGS, None)] - - self.assertRaises( - SystemExit, letshelp_le_apache.locate_config, "ctl") - self.assertRaises( - SystemExit, letshelp_le_apache.locate_config, "ctl") - server_root, config_file = letshelp_le_apache.locate_config("ctl") - self.assertEqual(server_root, "/etc/apache2") - self.assertEqual(config_file, "apache2.conf") - - @mock.patch(_MODULE_NAME + ".argparse") - def test_get_args(self, mock_argparse): - argv = ["-d", "/etc/apache2"] - mock_argparse.ArgumentParser.return_value = _create_mock_parser(argv) - self.assertRaises(SystemExit, letshelp_le_apache.get_args) - - server_root = "/etc/apache2" - config_file = server_root + "/apache2.conf" - argv = ["-d", server_root, "-f", config_file] - mock_argparse.ArgumentParser.return_value = _create_mock_parser(argv) - args = letshelp_le_apache.get_args() - self.assertEqual(args.apache_ctl, "apachectl") - self.assertEqual(args.server_root, server_root) - self.assertEqual(args.config_file, os.path.basename(config_file)) - - server_root = "/etc/apache2" - config_file = "/etc/httpd/httpd.conf" - argv = ["-d", server_root, "-f", config_file] - mock_argparse.ArgumentParser.return_value = _create_mock_parser(argv) - self.assertRaises(SystemExit, letshelp_le_apache.get_args) - - def test_main_with_args(self): - with mock.patch(_MODULE_NAME + ".get_args"): - self._test_main_common() - - def test_main_without_args(self): - with mock.patch(_MODULE_NAME + ".get_args") as get_args: - args = _get_args() - server_root, config_file = args.server_root, args.config_file - args.server_root = args.config_file = None - get_args.return_value = args - with mock.patch(_MODULE_NAME + ".locate_config") as locate: - locate.return_value = (server_root, config_file) - self._test_main_common() - - def _test_main_common(self): - with mock.patch(_MODULE_NAME + ".verify_config"): - with mock.patch(_MODULE_NAME + ".setup_tempdir") as mock_setup: - tempdir_path = tempfile.mkdtemp() - mock_setup.return_value = tempdir_path - with mock.patch(_MODULE_NAME + ".make_and_verify_selection"): - testdir_basename = "test" - os.mkdir(os.path.join(tempdir_path, testdir_basename)) - - letshelp_le_apache.main() - - tar = tarfile.open(os.path.join( - tempdir_path, "config.tar.gz")) - - tempdir = tar.next() - if tempdir is None: - self.fail("Invalid tarball!") # pragma: no cover - else: - self.assertTrue(tempdir.isdir()) - self.assertEqual(tempdir.name, ".") - - testdir = tar.next() - if testdir is None: - self.fail("Invalid tarball!") # pragma: no cover - else: - self.assertTrue(testdir.isdir()) - self.assertEqual(os.path.basename(testdir.name), - testdir_basename) - - self.assertEqual(tar.next(), None) - - -def _create_mock_parser(argv): - parser = argparse.ArgumentParser() - mock_parser = mock.MagicMock() - mock_parser.add_argument = parser.add_argument - mock_parser.parse_args = functools.partial(parser.parse_args, argv) - - return mock_parser - - -def _get_args(): - args = argparse.Namespace() - args.apache_ctl = "apache_ctl" - args.config_file = "config_file" - args.server_root = "server_root" - - return args - - -if __name__ == "__main__": - unittest.main() # pragma: no cover diff --git a/letshelp-certbot/letshelp_certbot/magic_typing.py b/letshelp-certbot/letshelp_certbot/magic_typing.py deleted file mode 100644 index 471b8dfa95c..00000000000 --- a/letshelp-certbot/letshelp_certbot/magic_typing.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Shim class to not have to depend on typing module in prod.""" -import sys - -class TypingClass(object): - """Ignore import errors by getting anything""" - def __getattr__(self, name): - return None - -try: - # mypy doesn't respect modifying sys.modules - from typing import * # pylint: disable=wildcard-import, unused-wildcard-import - # pylint: disable=unused-import - from typing import Collection, IO # type: ignore - # pylint: enable=unused-import -except ImportError: - sys.modules[__name__] = TypingClass() diff --git a/letshelp-certbot/letshelp_certbot/magic_typing_test.py b/letshelp-certbot/letshelp_certbot/magic_typing_test.py deleted file mode 100644 index 200ca03b861..00000000000 --- a/letshelp-certbot/letshelp_certbot/magic_typing_test.py +++ /dev/null @@ -1,41 +0,0 @@ -"""Tests for letshelp_certbot.magic_typing.""" -import sys -import unittest - -import mock - - -class MagicTypingTest(unittest.TestCase): - """Tests for letshelp_certbot.magic_typing.""" - def test_import_success(self): - try: - import typing as temp_typing - except ImportError: # pragma: no cover - temp_typing = None # pragma: no cover - typing_class_mock = mock.MagicMock() - text_mock = mock.MagicMock() - typing_class_mock.Text = text_mock - sys.modules['typing'] = typing_class_mock - if 'letshelp_certbot.magic_typing' in sys.modules: - del sys.modules['letshelp_certbot.magic_typing'] # pragma: no cover - from letshelp_certbot.magic_typing import Text # pylint: disable=no-name-in-module - self.assertEqual(Text, text_mock) - del sys.modules['letshelp_certbot.magic_typing'] - sys.modules['typing'] = temp_typing - - def test_import_failure(self): - try: - import typing as temp_typing - except ImportError: # pragma: no cover - temp_typing = None # pragma: no cover - sys.modules['typing'] = None - if 'letshelp_certbot.magic_typing' in sys.modules: - del sys.modules['letshelp_certbot.magic_typing'] # pragma: no cover - from letshelp_certbot.magic_typing import Text # pylint: disable=no-name-in-module - self.assertTrue(Text is None) - del sys.modules['letshelp_certbot.magic_typing'] - sys.modules['typing'] = temp_typing - - -if __name__ == '__main__': - unittest.main() # pragma: no cover diff --git a/letshelp-certbot/letshelp_certbot/testdata/mods-available/ssl.load b/letshelp-certbot/letshelp_certbot/testdata/mods-available/ssl.load deleted file mode 100644 index 3d2336ae0f4..00000000000 --- a/letshelp-certbot/letshelp_certbot/testdata/mods-available/ssl.load +++ /dev/null @@ -1,2 +0,0 @@ -# Depends: setenvif mime socache_shmcb -LoadModule ssl_module /usr/lib/apache2/modules/mod_ssl.so diff --git a/letshelp-certbot/letshelp_certbot/testdata/mods-enabled/ssl.load b/letshelp-certbot/letshelp_certbot/testdata/mods-enabled/ssl.load deleted file mode 120000 index 9d7972384b4..00000000000 --- a/letshelp-certbot/letshelp_certbot/testdata/mods-enabled/ssl.load +++ /dev/null @@ -1 +0,0 @@ -../mods-available/ssl.load \ No newline at end of file diff --git a/letshelp-certbot/letshelp_certbot/testdata/super_secret_file.txt b/letshelp-certbot/letshelp_certbot/testdata/super_secret_file.txt deleted file mode 100644 index 9f592eb7d58..00000000000 --- a/letshelp-certbot/letshelp_certbot/testdata/super_secret_file.txt +++ /dev/null @@ -1 +0,0 @@ -hunter2 diff --git a/letshelp-certbot/letshelp_certbot/testdata/uncommonly_named_k3y b/letshelp-certbot/letshelp_certbot/testdata/uncommonly_named_k3y deleted file mode 100644 index 659274d1d9f..00000000000 --- a/letshelp-certbot/letshelp_certbot/testdata/uncommonly_named_k3y +++ /dev/null @@ -1,6 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIGrAgEAAiEAm2Fylv+Uz7trgTW8EBHP3FQSMeZs2GNQ6VRo1sIVJEkCAwEAAQIh -AJT0BA/xD01dFCAXzSNyj9nfSZa3NpqzJZZn/eOm7vghAhEAzUVNZn4lLLBD1R6N -E8TKNQIRAMHHyn3O5JeY36lwKwkUlEUCEAliRauN0L0+QZuYjfJ9aJECEGx4dru3 -rTPCyighdqWNlHUCEQCiLjlwSRtWgmMBudCkVjzt ------END RSA PRIVATE KEY----- diff --git a/letshelp-certbot/letshelp_certbot/testdata/uncommonly_named_p4sswd b/letshelp-certbot/letshelp_certbot/testdata/uncommonly_named_p4sswd deleted file mode 100644 index 3559c1d1fc5..00000000000 --- a/letshelp-certbot/letshelp_certbot/testdata/uncommonly_named_p4sswd +++ /dev/null @@ -1 +0,0 @@ -johntheripper:$apr1$fIGE9.JL$jTCwNWZy9Ak/yvOLuOyzQ1 diff --git a/letshelp-certbot/readthedocs.org.requirements.txt b/letshelp-certbot/readthedocs.org.requirements.txt deleted file mode 100644 index 7858b312f8a..00000000000 --- a/letshelp-certbot/readthedocs.org.requirements.txt +++ /dev/null @@ -1,10 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e letshelp-certbot[docs] diff --git a/letshelp-certbot/setup.cfg b/letshelp-certbot/setup.cfg deleted file mode 100644 index 2a9acf13daa..00000000000 --- a/letshelp-certbot/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[bdist_wheel] -universal = 1 diff --git a/letshelp-certbot/setup.py b/letshelp-certbot/setup.py deleted file mode 100644 index 3e9e3172551..00000000000 --- a/letshelp-certbot/setup.py +++ /dev/null @@ -1,59 +0,0 @@ -from setuptools import setup -from setuptools import find_packages - - -version = '0.7.0.dev0' - -install_requires = [ - 'mock', - 'setuptools', # pkg_resources -] - -docs_extras = [ - 'Sphinx>=1.0', # autodoc_member_order = 'bysource', autodoc_default_flags - 'sphinx_rtd_theme', -] - -setup( - name='letshelp-certbot', - version=version, - description="Let's help Certbot client", - url='https://github.com/letsencrypt/letsencrypt', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 3 - Alpha', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(), - include_package_data=True, - install_requires=install_requires, - extras_require={ - 'docs': docs_extras, - }, - entry_points={ - 'console_scripts': [ - 'letshelp-certbot-apache = letshelp_certbot.apache:main', - ], - }, - test_suite='letshelp_certbot', -) diff --git a/letstest/README.md b/letstest/README.md new file mode 100644 index 00000000000..40e4833a723 --- /dev/null +++ b/letstest/README.md @@ -0,0 +1,53 @@ +# letstest +Simple AWS testfarm scripts for certbot client testing + +- Launches EC2 instances with a given list of AMIs for different distros +- Copies certbot repo and puts it on the instances +- Runs certbot tests (bash scripts) on all of these +- Logs execution and success/fail for debugging + +## Notes + - Some AWS images, e.g. official CentOS and FreeBSD images + require acceptance of user terms on the AWS marketplace + website. This can't be automated. + - AWS EC2 has a default limit of 20 t2/t1 instances, if more + are needed, they need to be requested via online webform. + +## Installation and configuration + +This package is installed in the Certbot development environment that is +created by following the instructions at +https://certbot.eff.org/docs/contributing.html#running-a-local-copy-of-the-client. + +These tests use the AWS SDK for Python (boto3) to manipulate EC2 instances. +Before running the tests, you'll need to set up credentials by following the +instructions at +https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#configuration. +You will also want to create a `~/.aws/config` file setting the region for your +profile to `us-east-1`, following the instructions in the boto3 quickstart guide above. + +Lastly, you will want to create a file on your system containing a trusted SSH key +by following the instructions at +https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/create-key-pairs.html. + +## Usage +To run tests, activate the virtual environment you created above and from this directory run: +``` +>letstest targets/targets.yaml /path/to/your/key.pem scripts/ +``` + +A temporary directory whose name is output by the tests is also created with a log file from each instance of the test and a file named "results" containing the output above. +The tests take quite a while to run. + +## Scripts +Example scripts are in the 'scripts' directory, these are just bash scripts that have a few parameters passed +to them at runtime via environment variables. test_apache2.sh is a useful reference. + +test_apache2 runs the dev venv and does local tests. + +See: +- https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html +- https://docs.aws.amazon.com/cli/latest/userguide/cli-ec2-keypairs.html + +Main repos: +- https://github.com/certbot/certbot diff --git a/letstest/letstest/__init__.py b/letstest/letstest/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/letstest/letstest/multitester.py b/letstest/letstest/multitester.py new file mode 100644 index 00000000000..c5f2a6b5f79 --- /dev/null +++ b/letstest/letstest/multitester.py @@ -0,0 +1,520 @@ +""" +Certbot Integration Test Tool + +- Launches EC2 instances with a given list of AMIs for different distros +- Copies certbot repo and puts it on the instances +- Runs certbot tests (bash scripts) on all of these +- Logs execution and success/fail for debugging + +Notes: + - Some AWS images, e.g. official CentOS and FreeBSD images + require acceptance of user terms on the AWS marketplace + website. This can't be automated. + - AWS EC2 has a default limit of 20 t2/t1 instances, if more + are needed, they need to be requested via online webform. + +Usage: + - Requires AWS IAM secrets to be set up with aws cli + - Requires an AWS associated keyfile .pem + +>aws configure --profile HappyHacker +[interactive: enter secrets for IAM role] +>aws ec2 create-key-pair --profile HappyHacker --key-name MyKeyPair \ + --query 'KeyMaterial' --output text > MyKeyPair.pem +then: +>letstest targets/targets.yaml MyKeyPair.pem HappyHacker scripts/test_apache2.sh +see: + https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html + https://docs.aws.amazon.com/cli/latest/userguide/cli-ec2-keypairs.html +""" +import argparse +import multiprocessing as mp +from multiprocessing import Manager +import os +import socket +import sys +import tempfile +import time +import traceback + +import boto3 +from fabric import Config +from fabric import Connection +import yaml + +# Command line parser +#------------------------------------------------------------------------------- +parser = argparse.ArgumentParser(description='Builds EC2 cluster for testing.') +parser.add_argument('config_file', + help='yaml configuration file for AWS server cluster') +parser.add_argument('key_file', + help='key file (.pem) for AWS') +parser.add_argument('aws_profile', + help='profile for AWS (i.e. as in ~/.aws/certificates)') +parser.add_argument('test_script', + default='test_apache2.sh', + help='path of bash script in to deploy and run') +parser.add_argument('--repo', + default='https://github.com/certbot/certbot.git', + help='certbot git repo to use') +parser.add_argument('--branch', + default='~', + help='certbot git branch to trial') +parser.add_argument('--pull_request', + default='~', + help='certbot/certbot pull request to trial') +parser.add_argument('--merge_main', + action='store_true', + help="if set merges PR into main branch of certbot/certbot") +parser.add_argument('--saveinstances', + action='store_true', + help="don't kill EC2 instances after run, useful for debugging") +parser.add_argument('--alt_pip', + default='', + help="server from which to pull candidate release packages") +cl_args = parser.parse_args() + +# Credential Variables +#------------------------------------------------------------------------------- +# assumes naming: = .pem +KEYFILE = cl_args.key_file +KEYNAME = os.path.split(cl_args.key_file)[1].split('.pem')[0] +PROFILE = None if cl_args.aws_profile == 'SET_BY_ENV' else cl_args.aws_profile + +# Globals +#------------------------------------------------------------------------------- +SECURITY_GROUP_NAME = 'certbot-security-group' +SENTINEL = None #queue kill signal +SUBNET_NAME = 'certbot-subnet' + +class Status: + """Possible statuses of client tests.""" + PASS = 'pass' + FAIL = 'fail' + +# Boto3/AWS automation functions +#------------------------------------------------------------------------------- +def should_use_subnet(subnet): + """Should we use the given subnet for these tests? + + We should if it is the default subnet for the availability zone or the + subnet is named "certbot-subnet". + + """ + if not subnet.map_public_ip_on_launch: + return False + if subnet.default_for_az: + return True + for tag in subnet.tags: + if tag['Key'] == 'Name' and tag['Value'] == SUBNET_NAME: + return True + return False + +def make_security_group(vpc): + """Creates a security group in the given VPC.""" + # will fail if security group of GroupName already exists + # cannot have duplicate SGs of the same name + mysg = vpc.create_security_group(GroupName=SECURITY_GROUP_NAME, + Description='security group for automated testing') + mysg.authorize_ingress(IpProtocol="tcp", CidrIp="0.0.0.0/0", FromPort=22, ToPort=22) + # for mosh + mysg.authorize_ingress(IpProtocol="udp", CidrIp="0.0.0.0/0", FromPort=60000, ToPort=61000) + return mysg + +def make_instance(ec2_client, + instance_name, + ami_id, + keyname, + security_group_id, + subnet_id, + self_destruct, + machine_type='t2.micro'): + """Creates an instance using the given parameters. + + If self_destruct is True, the instance will be configured to shutdown after + 1 hour and to terminate itself on shutdown. + + """ + block_device_mappings = _get_block_device_mappings(ec2_client, ami_id) + tags = [{'Key': 'Name', 'Value': instance_name}] + tag_spec = [{'ResourceType': 'instance', 'Tags': tags}] + kwargs = { + 'BlockDeviceMappings': block_device_mappings, + 'ImageId': ami_id, + 'SecurityGroupIds': [security_group_id], + 'SubnetId': subnet_id, + 'KeyName': keyname, + 'MinCount': 1, + 'MaxCount': 1, + 'InstanceType': machine_type, + 'TagSpecifications': tag_spec + } + if self_destruct: + kwargs['InstanceInitiatedShutdownBehavior'] = 'terminate' + kwargs['UserData'] = '#!/bin/bash\nshutdown -P +60\n' + return ec2_client.create_instances(**kwargs)[0] + +def _get_block_device_mappings(ec2_client, ami_id): + """Returns the list of block device mappings to ensure cleanup. + + This list sets connected EBS volumes to be deleted when the EC2 + instance is terminated. + + """ + # Not all devices use EBS, but the default value for DeleteOnTermination + # when the device does use EBS is true. See: + # * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-blockdev-mapping.html + # * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-blockdev-template.html + return [{'DeviceName': mapping['DeviceName'], + 'Ebs': {'DeleteOnTermination': True}} + for mapping in ec2_client.Image(ami_id).block_device_mappings + if not mapping.get('Ebs', {}).get('DeleteOnTermination', True)] + + +# Helper Routines +#------------------------------------------------------------------------------- +def block_until_ssh_open(ipstring, wait_time=10, timeout=120): + "Blocks until server at ipstring has an open port 22" + reached = False + t_elapsed = 0 + while not reached and t_elapsed < timeout: + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((ipstring, 22)) + reached = True + except OSError: + time.sleep(wait_time) + t_elapsed += wait_time + sock.close() + +def block_until_instance_ready(booting_instance, extra_wait_time=20): + "Blocks booting_instance until AWS EC2 instance is ready to accept SSH connections" + booting_instance.wait_until_running() + # The instance needs to be reloaded to update its local attributes. See + # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Instance.reload. + booting_instance.reload() + # After waiting for the instance to be running and reloading the instance + # state, we should have an IP address. + assert booting_instance.public_ip_address is not None + block_until_ssh_open(booting_instance.public_ip_address) + time.sleep(extra_wait_time) + return booting_instance + + +# Fabric Routines +#------------------------------------------------------------------------------- +def local_git_clone(local_cxn, repo_url, log_dir): + """clones main of repo_url""" + local_cxn.local('cd %s && if [ -d letsencrypt ]; then rm -rf letsencrypt; fi' % log_dir) + local_cxn.local('cd %s && git clone %s letsencrypt'% (log_dir, repo_url)) + local_cxn.local('cd %s && tar czf le.tar.gz letsencrypt'% log_dir) + +def local_git_branch(local_cxn, repo_url, branch_name, log_dir): + """clones branch of repo_url""" + local_cxn.local('cd %s && if [ -d letsencrypt ]; then rm -rf letsencrypt; fi' % log_dir) + local_cxn.local('cd %s && git clone %s letsencrypt --branch %s --single-branch'% + (log_dir, repo_url, branch_name)) + local_cxn.local('cd %s && tar czf le.tar.gz letsencrypt' % log_dir) + +def local_git_PR(local_cxn, repo_url, PRnumstr, log_dir, merge_main=True): + """clones specified pull request from repo_url and optionally merges into main""" + local_cxn.local('cd %s && if [ -d letsencrypt ]; then rm -rf letsencrypt; fi' % log_dir) + local_cxn.local('cd %s && git clone %s letsencrypt' % (log_dir, repo_url)) + local_cxn.local('cd %s && cd letsencrypt && ' + 'git fetch origin pull/%s/head:lePRtest' % (log_dir, PRnumstr)) + local_cxn.local('cd %s && cd letsencrypt && git checkout lePRtest' % log_dir) + if merge_main: + local_cxn.local('cd %s && cd letsencrypt && git remote update origin' % log_dir) + local_cxn.local('cd %s && cd letsencrypt && ' + 'git merge origin/main -m "testmerge"' % log_dir) + local_cxn.local('cd %s && tar czf le.tar.gz letsencrypt' % log_dir) + +def local_repo_to_remote(cxn, log_dir): + """copies local tarball of repo to remote""" + filename = 'le.tar.gz' + local_path = os.path.join(log_dir, filename) + cxn.put(local=local_path, remote='') + cxn.run('tar xzf %s' % filename) + +def local_repo_clean(local_cxn, log_dir): + """delete tarball""" + filename = 'le.tar.gz' + local_path = os.path.join(log_dir, filename) + local_cxn.local('rm %s' % local_path) + +def deploy_script(cxn, scriptpath, *args): + """copies to remote and executes local script""" + cxn.put(local=scriptpath, remote='', preserve_mode=True) + scriptfile = os.path.split(scriptpath)[1] + args_str = ' '.join(args) + cxn.run('./'+scriptfile+' '+args_str) + +def install_and_launch_certbot(cxn, instance, target, log_dir): + local_repo_to_remote(cxn, log_dir) + # This needs to be like this, I promise. 1) The env argument to run doesn't work. + # See https://github.com/fabric/fabric/issues/1744. 2) prefix() sticks an && between + # the commands, so it needs to be exports rather than no &&s in between for the script subshell. + with cxn.prefix('export PUBLIC_IP=%s && export PRIVATE_IP=%s && ' + 'export PUBLIC_HOSTNAME=%s && export PIP_EXTRA_INDEX_URL=%s && ' + 'export OS_TYPE=%s' % + (instance.public_ip_address, + instance.private_ip_address, + instance.public_dns_name, + cl_args.alt_pip, + target['type'])): + deploy_script(cxn, cl_args.test_script) + +def grab_certbot_log(cxn): + "grabs letsencrypt.log via cat into logged stdout" + cxn.sudo('/bin/bash -l -i -c \'if [ -f "/var/log/letsencrypt/letsencrypt.log" ]; then ' + + 'cat "/var/log/letsencrypt/letsencrypt.log"; else echo "[novarlog]"; fi\'') + # fallback file if /var/log is unwriteable...? correct? + cxn.sudo('/bin/bash -l -i -c \'if [ -f ./certbot.log ]; then ' + + 'cat ./certbot.log; else echo "[nolocallog]"; fi\'') + + +def create_client_instance(ec2_client, target, security_group_id, subnet_id, self_destruct): + """Create a single client instance for running tests.""" + if 'machine_type' in target: + machine_type = target['machine_type'] + elif target['virt'] == 'hvm': + machine_type = 't2.medium' + else: + # 32 bit systems + machine_type = 'c1.medium' + name = 'le-%s'%target['name'] + try: + instance = make_instance(ec2_client, name, target['ami'], KEYNAME, + machine_type=machine_type, security_group_id=security_group_id, + subnet_id=subnet_id, self_destruct=self_destruct) + except Exception: + print(f'FAIL: Unable to create instance {name}') + raise + print(f'Created instance {name}') + return instance + + +def test_client_process(fab_config, inqueue, outqueue, log_dir): + cur_proc = mp.current_process() + for inreq in iter(inqueue.get, SENTINEL): + ii, instance_id, target = inreq + + # Each client process is given its own session due to the suggestion at + # https://boto3.amazonaws.com/v1/documentation/api/latest/guide/resources.html?highlight=multithreading#multithreading-multiprocessing. + aws_session = boto3.session.Session(profile_name=PROFILE) + ec2_client = aws_session.resource('ec2') + instance = ec2_client.Instance(id=instance_id) + + #save all stdout to log file + sys.stdout = open(log_dir+'/'+'%d_%s.log'%(ii,target['name']), 'w') + + print("[%s : client %d %s %s]" % (cur_proc.name, ii, target['ami'], target['name'])) + instance = block_until_instance_ready(instance) + print("server %s at %s"%(instance, instance.public_ip_address)) + host_string = "%s@%s"%(target['user'], instance.public_ip_address) + print(host_string) + + with Connection(host_string, config=fab_config) as cxn: + try: + install_and_launch_certbot(cxn, instance, target, log_dir) + outqueue.put((ii, target, Status.PASS)) + print("%s - %s SUCCESS"%(target['ami'], target['name'])) + except: + outqueue.put((ii, target, Status.FAIL)) + print("%s - %s FAIL"%(target['ami'], target['name'])) + traceback.print_exc(file=sys.stdout) + pass + + # append server certbot.log to each per-machine output log + print("\n\ncertbot.log\n" + "-"*80 + "\n") + try: + grab_certbot_log(cxn) + except: + print("log fail\n") + traceback.print_exc(file=sys.stdout) + pass + + +def cleanup(cl_args, instances, targetlist, log_dir): + print('Logs in ', log_dir) + # If lengths of instances and targetlist aren't equal, instances failed to + # start before running tests so leaving instances running for debugging + # isn't very useful. Let's cleanup after ourselves instead. + if len(instances) != len(targetlist) or not cl_args.saveinstances: + print('Terminating EC2 Instances') + for instance in instances: + instance.terminate() + else: + # print login information for the boxes for debugging + for ii, target in enumerate(targetlist): + print(target['name'], + target['ami'], + "%s@%s"%(target['user'], instances[ii].public_ip_address)) + + +def main(): + # Fabric library controlled through global env parameters + fab_config = Config(overrides={ + "connect_kwargs": { + "key_filename": [KEYFILE], # https://github.com/fabric/fabric/issues/2007 + }, + "run": { + "echo": True, + "pty": True, + }, + "timeouts": { + "connect": 10, + }, + }) + # no network connection, so don't worry about closing this one. + local_cxn = Connection('localhost', config=fab_config) + + # Set up local copy of git repo + #------------------------------------------------------------------------------- + log_dir = tempfile.mkdtemp() # points to logging / working directory + print("Local dir for test repo and logs: %s"%log_dir) + + try: + # figure out what git object to test and locally create it in log_dir + print("Making local git repo") + if cl_args.pull_request != '~': + print('Testing PR %s ' % cl_args.pull_request, + "MERGING into main" if cl_args.merge_main else "") + local_git_PR(local_cxn, cl_args.repo, cl_args.pull_request, log_dir, + cl_args.merge_main) + elif cl_args.branch != '~': + print('Testing branch %s of %s' % (cl_args.branch, cl_args.repo)) + local_git_branch(local_cxn, cl_args.repo, cl_args.branch, log_dir) + else: + print('Testing current branch of %s' % cl_args.repo, log_dir) + local_git_clone(local_cxn, cl_args.repo, log_dir) + except BaseException: + print("FAIL: trouble with git repo") + traceback.print_exc() + exit(1) + + + # Set up EC2 instances + #------------------------------------------------------------------------------- + configdata = yaml.safe_load(open(cl_args.config_file, 'r')) + targetlist = configdata['targets'] + print('Testing against these images: [%d total]'%len(targetlist)) + for target in targetlist: + print(target['ami'], target['name']) + + print("Connecting to EC2 using\n profile %s\n keyname %s\n keyfile %s"%(PROFILE, KEYNAME, KEYFILE)) + aws_session = boto3.session.Session(profile_name=PROFILE) + ec2_client = aws_session.resource('ec2') + + print("Determining Subnet") + for subnet in ec2_client.subnets.all(): + if should_use_subnet(subnet): + subnet_id = subnet.id + vpc_id = subnet.vpc.id + break + else: + print("No usable subnet exists!") + print("Please create a VPC with a subnet named {0}".format(SUBNET_NAME)) + print("that maps public IPv4 addresses to instances launched in the subnet.") + sys.exit(1) + + print("Making Security Group") + vpc = ec2_client.Vpc(vpc_id) + sg_exists = False + for sg in vpc.security_groups.all(): + if sg.group_name == SECURITY_GROUP_NAME: + security_group_id = sg.id + sg_exists = True + print(" %s already exists"%SECURITY_GROUP_NAME) + if not sg_exists: + security_group_id = make_security_group(vpc).id + time.sleep(30) + + instances = [] + try: + # If we want to preserve instances, do not have them self-destruct. + self_destruct = not cl_args.saveinstances + for target in targetlist: + instances.append( + create_client_instance(ec2_client, target, + security_group_id, subnet_id, + self_destruct) + ) + + # Install and launch client scripts in parallel + #------------------------------------------------------------------------------- + print("Uploading and running test script in parallel: %s"%cl_args.test_script) + print("Output routed to log files in %s"%log_dir) + # (Advice: always use Manager.Queue, never regular multiprocessing.Queue + # the latter has implementation flaws that deadlock it in some circumstances) + manager = Manager() + outqueue = manager.Queue() + inqueue = manager.Queue() + + # launch as many processes as clients to test + num_processes = len(targetlist) + jobs = [] #keep a reference to current procs + + + # initiate process execution + client_process_args=(fab_config, inqueue, outqueue, log_dir) + for i in range(num_processes): + p = mp.Process(target=test_client_process, args=client_process_args) + jobs.append(p) + p.daemon = True # kills subprocesses if parent is killed + p.start() + + # fill up work queue + for ii, target in enumerate(targetlist): + inqueue.put((ii, instances[ii].id, target)) + + # add SENTINELs to end client processes + for i in range(num_processes): + inqueue.put(SENTINEL) + print('Waiting on client processes', end='') + for p in jobs: + while p.is_alive(): + p.join(5 * 60) + # Regularly print output to keep Travis happy + print('.', end='') + sys.stdout.flush() + print() + # add SENTINEL to output queue + outqueue.put(SENTINEL) + + # clean up + local_repo_clean(local_cxn, log_dir) + + # print and save summary results + results_file = open(log_dir+'/results', 'w') + outputs = list(iter(outqueue.get, SENTINEL)) + outputs.sort(key=lambda x: x[0]) + failed = False + results_msg = "" + for outq in outputs: + ii, target, status = outq + if status == Status.FAIL: + failed = True + with open(log_dir+'/'+'%d_%s.log'%(ii,target['name']), 'r') as f: + print(target['name'] + " test failed. Test log:") + print(f.read()) + results_msg = results_msg + '%d %s %s\n'%(ii, target['name'], status) + results_file.write('%d %s %s\n'%(ii, target['name'], status)) + print(results_msg) + if len(outputs) != num_processes: + failed = True + failure_message = 'FAILURE: Some target machines failed to run and were not tested. ' +\ + 'Tests should be rerun.' + print(failure_message) + results_file.write(failure_message + '\n') + results_file.close() + + if failed: + sys.exit(1) + + finally: + cleanup(cl_args, instances, targetlist, log_dir) + + +if __name__ == '__main__': + main() diff --git a/letstest/pyproject.toml b/letstest/pyproject.toml new file mode 100644 index 00000000000..65fde0523b8 --- /dev/null +++ b/letstest/pyproject.toml @@ -0,0 +1,43 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[project] +name = "letstest" +dynamic = ["version"] +description = "Test Certbot on different AWS images" +readme = "README.md" +license = "Apache-2.0" +requires-python = ">=3.10" +authors = [ + { name = "Certbot Project" }, +] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.14", + "Topic :: Internet :: WWW/HTTP", + "Topic :: Security", +] +dependencies = [ + "boto3", + "botocore", + # The API from Fabric 2.0+ is used instead of the 1.0 API. + "fabric>=2", + "pyyaml", +] + +[project.scripts] +letstest = "letstest.multitester:main" + +[project.urls] +Homepage = "https://github.com/certbot/certbot" + +[tool.setuptools.packages.find] +include = ["letstest"] diff --git a/letstest/scripts/bootstrap_os_packages.sh b/letstest/scripts/bootstrap_os_packages.sh new file mode 100755 index 00000000000..199f3735fc4 --- /dev/null +++ b/letstest/scripts/bootstrap_os_packages.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# +# Install OS dependencies for test farm tests. +# +# This does not include the dependencies needed to build cryptography. See +# https://cryptography.io/en/latest/installation/#building-cryptography-on-linux + +set -ex # Work even if somebody does "sh thisscript.sh". + +error() { + echo "$@" +} + +if [ -f /etc/debian_version ]; then + sudo apt-get update || error apt-get update hit problems but continuing anyway... + + PYENV_DEPS="make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev \ + wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev \ + liblzma-dev git" + ALL_DEPS="libaugeas-dev $PYENV_DEPS" + sudo DEBIAN_FRONTEND=noninteractive apt-get install -y $ALL_DEPS +elif [ -f /etc/redhat-release ]; then + # the "codeready builder" repository must be enabled to install the + # augeas-devel package needed to compile newer versions of python-augeas + sudo yum config-manager --set-enabled crb + PYENV_DEPS="gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel \ + openssl-devel tk-devel libffi-devel xz-devel git" + ALL_DEPS="augeas-devel $PYENV_DEPS" + + if yum list installed "httpd" >/dev/null 2>&1; then + ALL_DEPS="mod_ssl $ALL_DEPS" + fi + + sudo yum install -y $ALL_DEPS +fi diff --git a/letstest/scripts/test_apache2.sh b/letstest/scripts/test_apache2.sh new file mode 100755 index 00000000000..915615e44b3 --- /dev/null +++ b/letstest/scripts/test_apache2.sh @@ -0,0 +1,103 @@ +#!/bin/bash -ex + +# $OS_TYPE $PUBLIC_IP $PRIVATE_IP $PUBLIC_HOSTNAME $BOULDER_URL +# are dynamically set at execution + +if [ "$OS_TYPE" = "ubuntu" ] +then + CONFFILE=/etc/apache2/sites-available/000-default.conf + sudo apt-get update + sudo DEBIAN_FRONTEND=noninteractive apt-get -y --no-upgrade install apache2 curl + # For apache 2.4, set up ServerName + sudo sed -i '/ServerName/ s/#ServerName/ServerName/' $CONFFILE + sudo sed -i '/ServerName/ s/www.example.com/'$PUBLIC_HOSTNAME'/' $CONFFILE +elif [ "$OS_TYPE" = "centos" ] +then + CONFFILE=/etc/httpd/conf/httpd.conf + sudo setenforce 0 || true #disable selinux + sudo yum -y install httpd mod_ssl + sudo service httpd start + sudo mkdir -p /var/www/$PUBLIC_HOSTNAME/public_html + sudo chmod -R oug+rwx /var/www + sudo chmod -R oug+rw /etc/httpd + sudo echo 'foobar' > /var/www/$PUBLIC_HOSTNAME/public_html/index.html + sudo mkdir /etc/httpd/sites-available #certbot requires this... + sudo mkdir /etc/httpd/sites-enabled #certbot requires this... + #sudo echo "IncludeOptional sites-enabled/*.conf" >> /etc/httpd/conf/httpd.conf + sudo echo """ + + ServerName $PUBLIC_HOSTNAME + DocumentRoot /var/www/$PUBLIC_HOSTNAME/public_html + ErrorLog /var/www/$PUBLIC_HOSTNAME/error.log + CustomLog /var/www/$PUBLIC_HOSTNAME/requests.log combined +""" >> /etc/httpd/conf.d/$PUBLIC_HOSTNAME.conf + #sudo cp /etc/httpd/sites-available/$PUBLIC_HOSTNAME.conf /etc/httpd/sites-enabled/ +fi + +# Run certbot-apache2. +cd letsencrypt + +echo "Bootstrapping dependencies..." +sudo letstest/scripts/bootstrap_os_packages.sh + +# Install pyenv +curl https://pyenv.run | bash +export PYENV_ROOT="$HOME/.pyenv" +export PATH="$PYENV_ROOT/bin:$PATH" +eval "$(pyenv init --path)" +eval "$(pyenv init -)" + +# Install and configure Python +pyenv install 3.10 +pyenv shell 3.10 + +tools/venv.py -e acme -e certbot -e certbot-apache -e certbot-ci tox +PEBBLE_LOGS="acme_server.log" +PEBBLE_URL="https://localhost:14000/dir" +# We configure Pebble to use port 80 for http-01 validation rather than an +# alternate port because: +# 1) It allows us to test with Apache configurations that are more realistic +# and closer to the default configuration on various OSes. +# 2) As of writing this, Certbot's Apache plugin requires there to be an +# existing virtual host for the port used for http-01 validation. +venv/bin/run_acme_server --http-01-port 80 > "${PEBBLE_LOGS}" 2>&1 & + +DumpPebbleLogsOnFailure() { + exit_status="$?" + if [ "$exit_status" != 0 ] && [ -f "${PEBBLE_LOGS}" ] ; then + echo "Pebble's logs were:" + cat "${PEBBLE_LOGS}" + fi + exit "$exit_status" +} +trap DumpPebbleLogsOnFailure EXIT + +for n in $(seq 1 150) ; do + if curl --insecure "${PEBBLE_URL}" 2>/dev/null; then + break + else + echo "waiting for pebble" + sleep 1 + fi +done +if ! curl --insecure "${PEBBLE_URL}" 2>/dev/null; then + echo "timed out waiting for pebble to start" + DumpPebbleLogs + exit 1 +fi + +sudo "venv/bin/certbot" -v --debug --text --agree-tos --no-verify-ssl \ + --renew-by-default --redirect --register-unsafely-without-email \ + --domain "${PUBLIC_HOSTNAME}" --server "${PEBBLE_URL}" + +if ! grep -q SSLSessionTickets /etc/letsencrypt/options-ssl-apache.conf; then + echo "modern TLS options were not used" + exit 1 +fi + +if [ "$OS_TYPE" = "ubuntu" ] ; then + export SERVER="${PEBBLE_URL}" + "venv/bin/tox" -e apacheconftest +else + echo Not running hackish apache tests on $OS_TYPE +fi diff --git a/letstest/scripts/version.py b/letstest/scripts/version.py new file mode 100755 index 00000000000..c5debd68939 --- /dev/null +++ b/letstest/scripts/version.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +"""Get the current Certbot version number. + +Provides a simple utility for determining the Certbot version number + +""" +from __future__ import print_function + +from os.path import abspath +from os.path import dirname +from os.path import join +import re + + +def certbot_version(letstest_scripts_dir): + """Return the version number stamped in certbot/__init__.py.""" + return re.search('''^__version__ = ['"](.+)['"].*''', + file_contents(join(dirname(dirname(letstest_scripts_dir)), + 'certbot', + 'src', + 'certbot', + '__init__.py')), + re.M).group(1) + + +def file_contents(path): + with open(path) as file: + return file.read() + + +if __name__ == '__main__': + print(certbot_version(dirname(abspath(__file__)))) diff --git a/letstest/setup.py b/letstest/setup.py new file mode 100644 index 00000000000..11fc858b9b0 --- /dev/null +++ b/letstest/setup.py @@ -0,0 +1,7 @@ +from setuptools import setup + +version = '5.8.0.dev0' + +setup( + version=version, +) diff --git a/letstest/targets/targets.yaml b/letstest/targets/targets.yaml new file mode 100644 index 00000000000..2853bf348ec --- /dev/null +++ b/letstest/targets/targets.yaml @@ -0,0 +1,35 @@ +# These images are located in us-east-1. +# +# All machines must currently use x86_64 since Pebble does not currently +# publish images for other architectures. + +targets: + #----------------------------------------------------------------------------- + # Ubuntu + # These AMI were found on https://cloud-images.ubuntu.com/locator/ec2/. + - ami: ami-045a47a3b15302634 + name: ubuntu24.04 + type: ubuntu + virt: hvm + user: ubuntu + - ami: ami-0fc5d935ebf8bc3bc + name: ubuntu22.04 + type: ubuntu + virt: hvm + user: ubuntu + #----------------------------------------------------------------------------- + # Debian + # These AMI were found on https://wiki.debian.org/Cloud/AmazonEC2Image. + - ami: ami-0f238cd7c96d866ad + name: debian12 + type: ubuntu + virt: hvm + user: admin + #----------------------------------------------------------------------------- + # CentOS + # These AMI were found on https://centos.org/download/aws-images/. + - ami: ami-013db6d0c7167e046 + name: centos9stream + type: centos + virt: hvm + user: ec2-user diff --git a/linter_plugin.py b/linter_plugin.py index 4938755cf46..0660d171787 100644 --- a/linter_plugin.py +++ b/linter_plugin.py @@ -1,29 +1,56 @@ -"""Certbot ACME PyLint plugin. - -http://docs.pylint.org/plugins.html - """ -from astroid import MANAGER -from astroid import nodes - - -def register(unused_linter): - """Register this module as PyLint plugin.""" - -def _transform(cls): - # fix the "no-member" error on instances of - # letsencrypt.acme.util.ImmutableMap subclasses (instance - # attributes are initialized dynamically based on __slots__) +Certbot PyLint plugin. - # TODO: this is too broad and applies to any tested class... - - if cls.slots() is not None: - for slot in cls.slots(): - cls.locals[slot.value] = [nodes.EmptyNode()] - - if cls.name == 'JSONObjectWithFields': - # _fields is magically introduced by JSONObjectWithFieldsMeta - cls.locals['_fields'] = [nodes.EmptyNode()] - - -MANAGER.register_transform(nodes.Class, _transform) +The built-in ImportChecker of Pylint does a similar job to ForbidStandardOsModule to detect +deprecated modules. You can check its behavior as a reference to what is coded here. +See https://github.com/PyCQA/pylint/blob/b20a2984c94e2946669d727dbda78735882bf50a/pylint/checkers/imports.py#L287 +See https://docs.pytest.org/en/latest/writing_plugins.html +""" +import os.path + +from pylint.checkers import BaseChecker + +# Modules whose file is matching one of these paths can import the os module. +ALLOWLIST_PATHS = [ + '/acme/src/acme/', + '/certbot-ci/', + '/certbot-compatibility-test/', +] + + +class ForbidStandardOsModule(BaseChecker): + """ + This checker ensures that standard os module (and submodules) is not imported by certbot + modules. Otherwise an 'os-module-forbidden' error will be registered for the faulty lines. + """ + + name = 'forbid-os-module' + msgs = { + 'E5001': ( + 'Forbidden use of os module, certbot.compat.os must be used instead', + 'os-module-forbidden', + 'Some methods from the standard os module cannot be used for security reasons on ' + 'Windows: the safe wrapper certbot.compat.os must be used instead in Certbot.' + ) + } + priority = -1 + + def visit_import(self, node): + os_used = any(name for name in node.names if name[0] == 'os' or name[0].startswith('os.')) + if os_used and not _check_disabled(node): + self.add_message('os-module-forbidden', node=node) + + def visit_importfrom(self, node): + if node.modname == 'os' or node.modname.startswith('os.') and not _check_disabled(node): + self.add_message('os-module-forbidden', node=node) + + +def register(linter): + """Pylint hook to auto-register this linter""" + linter.register_checker(ForbidStandardOsModule(linter)) + + +def _check_disabled(node): + module = node.root() + return any(path for path in ALLOWLIST_PATHS + if os.path.normpath(path) in os.path.normpath(module.file)) diff --git a/local-oldest-requirements.txt b/local-oldest-requirements.txt deleted file mode 100644 index d582d5c6521..00000000000 --- a/local-oldest-requirements.txt +++ /dev/null @@ -1 +0,0 @@ -acme[dev]==0.29.0 diff --git a/mypy.ini b/mypy.ini index 188ed031fc7..4314e4db8bd 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,10 +1,15 @@ [mypy] -check_untyped_defs = True ignore_missing_imports = True -python_version = 2.7 +warn_unused_ignores = True +show_error_codes = True +disallow_untyped_defs = True +strict_equality = True -[mypy-acme.magic_typing_test] -ignore_errors = True - -[mypy-letshelp_certbot.magic_typing_test] -ignore_errors = True +# Using stricter settings here is being tracked by +# https://github.com/certbot/certbot/issues/9647. +[mypy-*._internal.tests.*] +# By default, mypy prints notes without erroring about any type annotations it +# finds in untyped function bodies when check_untyped_defs is false. Disabling +# this "error" code removes this visual noise. +disable_error_code = annotation-unchecked +disallow_untyped_defs = False diff --git a/newsfragments/.gitignore b/newsfragments/.gitignore new file mode 100644 index 00000000000..f935021a8f8 --- /dev/null +++ b/newsfragments/.gitignore @@ -0,0 +1 @@ +!.gitignore diff --git a/newsfragments/10748.changed b/newsfragments/10748.changed new file mode 100644 index 00000000000..c285023d80a --- /dev/null +++ b/newsfragments/10748.changed @@ -0,0 +1 @@ +certbot and its acme library now require cryptography>=47.0.0. diff --git a/pull_request_template.md b/pull_request_template.md deleted file mode 100644 index 60fd6da7e33..00000000000 --- a/pull_request_template.md +++ /dev/null @@ -1,3 +0,0 @@ -Be sure to edit the `master` section of `CHANGELOG.md`. This includes a -description of the change and ensuring the modified package(s) are listed as -having been changed. diff --git a/pytest.ini b/pytest.ini index 9a5807f34c3..87008a4e361 100644 --- a/pytest.ini +++ b/pytest.ini @@ -2,11 +2,25 @@ # settings we want to also change there must be added to the release script # directly. [pytest] -addopts = --numprocesses auto --pyargs -# ResourceWarnings are ignored as errors, since they're raised at close -# decodestring: https://github.com/rthalley/dnspython/issues/338 -# ignore our own TLS-SNI-01 warning +# Warnings being triggered by our plugins using deprecated features in +# acme/certbot should be fixed by having our plugins no longer using the +# deprecated code rather than adding them to the list of ignored warnings here. +# Fixing things in this way prevents us from shipping packages raising our own +# deprecation warnings and gives time for plugins that don't use the deprecated +# API to propagate, especially for plugins packaged as an external snap, before +# we release breaking changes. +# +# 1) In v2.28.0, google-api-core added an annoying message that the current python's EOL +# is coming up. We deprecate python versions on schedule, so mostly this is just an +# annoyance for our own tests, and can probably be silenced forever. +# 2) cloudflare 4.x imports pydantic.v1 for compatibility, which emits a +# UserWarning on Python 3.14+. Cloudflare plan to add official 3.13/3.14 +# support in their upcoming v5 release; track progress (and remove this +# entry once released) via https://github.com/cloudflare/cloudflare-python/issues/2679 +# When this pytest.ini entry is removed, the warnings filter at the top of +# certbot-dns-cloudflare/src/certbot_dns_cloudflare/_internal/dns_cloudflare.py +# should be removed as well. filterwarnings = error - ignore:decodestring:DeprecationWarning - ignore:TLS-SNI-01:DeprecationWarning + ignore:You are using a Python version (.*) which Google will stop supporting:FutureWarning + ignore:Core Pydantic V1 functionality:UserWarning diff --git a/readthedocs.org.requirements.txt b/readthedocs.org.requirements.txt deleted file mode 100644 index 94a81e788a1..00000000000 --- a/readthedocs.org.requirements.txt +++ /dev/null @@ -1,11 +0,0 @@ -# readthedocs.org gives no way to change the install command to "pip -# install -e .[docs]" (that would in turn install documentation -# dependencies), but it allows to specify a requirements.txt file at -# https://readthedocs.org/dashboard/letsencrypt/advanced/ (c.f. #259) - -# Although ReadTheDocs certainly doesn't need to install the project -# in --editable mode (-e), just "pip install .[docs]" does not work as -# expected and "pip install -e .[docs]" must be used instead - --e acme --e .[docs] diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 00000000000..e44d919221c --- /dev/null +++ b/ruff.toml @@ -0,0 +1,14 @@ +line-length = 100 + +extend-exclude = ['tools', 'letstest'] + +[lint] +# Revert default rules to pre-16.0.0 version. Expanding the list to match the new defaults is +# tracked by https://github.com/certbot/certbot/issues/10749. +select = ["E4", "E7", "E9", "F"] +# Check for PEP 585 style annotations to prevent regressions in +# https://github.com/certbot/certbot/issues/10195 +extend-select = ["UP006"] +# Skip bare `except` rules (`E722`). +# Skip ambiguous variable name (`E741`). +ignore = ["E722", "E741",] diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index a21bab7937c..00000000000 --- a/setup.cfg +++ /dev/null @@ -1,5 +0,0 @@ -[bdist_wheel] -universal = 1 - -[easy_install] -zip_ok = false diff --git a/setup.py b/setup.py deleted file mode 100644 index 9e6af2d4fa7..00000000000 --- a/setup.py +++ /dev/null @@ -1,136 +0,0 @@ -import codecs -import os -import re - -from setuptools import find_packages, setup - -# Workaround for http://bugs.python.org/issue8876, see -# http://bugs.python.org/issue8876#msg208792 -# This can be removed when using Python 2.7.9 or later: -# https://hg.python.org/cpython/raw-file/v2.7.9/Misc/NEWS -if os.path.abspath(__file__).split(os.path.sep)[1] == 'vagrant': - del os.link - - -def read_file(filename, encoding='utf8'): - """Read unicode from given file.""" - with codecs.open(filename, encoding=encoding) as fd: - return fd.read() - - -here = os.path.abspath(os.path.dirname(__file__)) - -# read version number (and other metadata) from package init -init_fn = os.path.join(here, 'certbot', '__init__.py') -meta = dict(re.findall(r"""__([a-z]+)__ = '([^']+)""", read_file(init_fn))) - -readme = read_file(os.path.join(here, 'README.rst')) -version = meta['version'] - -# This package relies on PyOpenSSL, requests, and six, however, it isn't -# specified here to avoid masking the more specific request requirements in -# acme. See https://github.com/pypa/pip/issues/988 for more info. -install_requires = [ - 'acme>=0.29.0', - # We technically need ConfigArgParse 0.10.0 for Python 2.6 support, but - # saying so here causes a runtime error against our temporary fork of 0.9.3 - # in which we added 2.6 support (see #2243), so we relax the requirement. - 'ConfigArgParse>=0.9.3', - 'configobj', - 'cryptography>=1.2.3', # load_pem_x509_certificate - 'josepy', - 'mock', - 'parsedatetime>=1.3', # Calendar.parseDT - 'pyrfc3339', - 'pytz', - 'setuptools', - 'zope.component', - 'zope.interface', -] - -dev_extras = [ - # Pin astroid==1.3.5, pylint==1.4.2 as a workaround for #289 - 'astroid==1.3.5', - 'coverage', - 'ipdb', - 'pytest', - 'pytest-cov', - 'pytest-xdist', - 'pylint==1.4.2', # upstream #248 - 'tox', - 'twine', - 'wheel', -] - -dev3_extras = [ - 'mypy', - 'typing', # for python3.4 -] - -docs_extras = [ - # If you have Sphinx<1.5.1, you need docutils<0.13.1 - # https://github.com/sphinx-doc/sphinx/issues/3212 - 'repoze.sphinx.autointerface', - 'Sphinx>=1.2', # Annotation support - 'sphinx_rtd_theme', -] - -setup( - name='certbot', - version=version, - description="ACME client", - long_description=readme, - url='https://github.com/letsencrypt/letsencrypt', - author="Certbot Project", - author_email='client-dev@letsencrypt.org', - license='Apache License 2.0', - python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*', - classifiers=[ - 'Development Status :: 5 - Production/Stable', - 'Environment :: Console', - 'Environment :: Console :: Curses', - 'Intended Audience :: System Administrators', - 'License :: OSI Approved :: Apache Software License', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Topic :: Internet :: WWW/HTTP', - 'Topic :: Security', - 'Topic :: System :: Installation/Setup', - 'Topic :: System :: Networking', - 'Topic :: System :: Systems Administration', - 'Topic :: Utilities', - ], - - packages=find_packages(exclude=['docs', 'examples', 'tests', 'venv']), - include_package_data=True, - - install_requires=install_requires, - extras_require={ - 'dev': dev_extras, - 'dev3': dev3_extras, - 'docs': docs_extras, - }, - - # to test all packages run "python setup.py test -s - # {acme,certbot_apache,certbot_nginx}" - test_suite='certbot', - - entry_points={ - 'console_scripts': [ - 'certbot = certbot.main:main', - ], - 'certbot.plugins': [ - 'manual = certbot.plugins.manual:Authenticator', - 'null = certbot.plugins.null:Installer', - 'standalone = certbot.plugins.standalone:Authenticator', - 'webroot = certbot.plugins.webroot:Authenticator', - ], - }, -) diff --git a/snap/hooks/configure b/snap/hooks/configure new file mode 100644 index 00000000000..9545f8b8abd --- /dev/null +++ b/snap/hooks/configure @@ -0,0 +1,3 @@ +#!/bin/sh -e + +exit 0 diff --git a/snap/hooks/prepare-plug-plugin b/snap/hooks/prepare-plug-plugin new file mode 100644 index 00000000000..5765fdfb49a --- /dev/null +++ b/snap/hooks/prepare-plug-plugin @@ -0,0 +1,25 @@ +#!/bin/sh -e + +# Workaround for a very old snapctl binary on the host connecting to the wrong socket and crashing. +# Prefer an up-to-date snapctl from the core or snapd snaps, if they exist. We ask users to install +# the core snap in the Certbot installation instructions. +# See https://github.com/certbot/certbot/issues/8922, https://bugs.launchpad.net/snapd/+bug/1933392 +SNAPCTL_CORE="/snap/core/current/usr/bin/snapctl" +SNAPCTL_SNAPD="/snap/snapd/current/usr/bin/snapctl" +SNAPCTL="snapctl" +if [ -x $SNAPCTL_CORE ]; then + SNAPCTL=$SNAPCTL_CORE +elif [ -x $SNAPCTL_SNAPD ]; then + SNAPCTL=$SNAPCTL_SNAPD +fi + +if [ "$($SNAPCTL get trust-plugin-with-root)" = "ok" ]; then + # allow the connection, but reset config to allow for other slots to go through this auth flow + $SNAPCTL unset trust-plugin-with-root + exit 0 +else + echo "Only connect this interface if you trust the plugin author to have root on the system." + echo "Run \`snap set $SNAP_NAME trust-plugin-with-root=ok\` to acknowledge this and then run this command again to perform the connection." + echo "If that doesn't work, you may need to remove all certbot-dns-* plugins from the system, then try installing the certbot snap again." + exit 1 +fi diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml new file mode 100644 index 00000000000..54b299a6f8e --- /dev/null +++ b/snap/snapcraft.yaml @@ -0,0 +1,149 @@ +name: certbot +summary: Automatically configure HTTPS using Let's Encrypt +description: | + The objective of Certbot, Let's Encrypt, and the ACME (Automated + Certificate Management Environment) protocol is to make it possible + to set up an HTTPS server and have it automatically obtain a + browser-trusted certificate, without any human intervention. This is + accomplished by running a certificate management agent on the web + server. + + This agent is used to: + - Automatically prove to the Let's Encrypt CA that you control the website + - Obtain a browser-trusted certificate and set it up on your web server + - Keep track of when your certificate is going to expire, and renew it + - Help you revoke the certificate if that ever becomes necessary. +confinement: classic +base: core24 +grade: stable +adopt-info: certbot + +environment: + AUGEAS_LENS_LIB: "$SNAP/usr/share/augeas/lenses/dist" + CERTBOT_SNAPPED: "True" + # This is needed to help openssl find its legacy provider on architectures + # where we cannot use cryptography's pre-built wheels. See + # https://github.com/certbot/certbot/issues/10055. + OPENSSL_MODULES: "$SNAP/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/ossl-modules" + PATH: "$SNAP/bin:$SNAP/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" + # Disable FIPS mode detection. See + # https://git.launchpad.net/ubuntu/+source/openssl/tree/debian/patches/fips/crypto-Add-kernel-FIPS-mode-detection.patch?h=applied/ubuntu/noble + # for more on this flag, and https://github.com/certbot/certbot/issues/10044 + # for more on the issue. + # + # This is needed because the Python + OpenSSL bundled in core24 don't include + # an OpenSSL FIPS provider, which causes crashes on host systems with OpenSSL + # 1.1.1f (e.g. Ubuntu Pro 20.04). For some reason, core24's OpenSSL also looks + # in a non-standard location for the provider, which also causes crashes on + # systems with OpenSSL 3.x (e.g. RHEL 9). If you need FIPS functionality in + # certbot, install via pip. + OPENSSL_FORCE_FIPS_MODE: "0" + +apps: + certbot: + command: bin/python3 -s $SNAP/bin/certbot + renew: + command: bin/python3 -s $SNAP/bin/certbot -q renew + daemon: oneshot + # Run approximately twice a day with randomization + timer: 00:00~24:00/2 + + +parts: + certbot: + plugin: python + source: . + python-packages: + - git+https://github.com/certbot/python-augeas.git@certbot-patched + - ./acme + - ./certbot + - ./certbot-apache + - ./certbot-nginx + stage: + - -usr/lib/python3.12/sitecustomize.py # maybe unnecessary + # Old versions of this file used to unstage + # lib/python3.8/site-packages/augeas.py to avoid conflicts between + # python-augeas 0.5.0 which was pinned in snap-constraints.txt and + # our python-augeas fork which creates an auto-generated cffi file at + # the same path. Since we've combined things in one part and removed the + # python-augeas pinning, unstaging this file had a different, unintended + # effect so we now stage the file to keep the auto-generated cffi file. + stage-packages: + - libaugeas0 + - libpython3.12-dev + # This library included so openssl has a legacy provider available at + # runtime when we are unable to use cryptography's pre-built wheels. See + # https://github.com/certbot/certbot/issues/10055. + - libssl3t64 + # added to stage python: + - libpython3-stdlib + - libpython3.12-stdlib + - libpython3.12-minimal + - python3-pip + - python3-wheel + - python3-venv + - python3-minimal + - python3-pkg-resources + - python3.12-minimal + # To build cryptography and cffi if needed + build-packages: + - gcc + - git + - libaugeas-dev + - build-essential + - libssl-dev + - libffi-dev + - python3-dev + - cargo + - pkg-config + build-environment: + # We set this environment variable while building to try and increase the + # stability of fetching the rust crates needed to build the cryptography + # library. + - CARGO_NET_GIT_FETCH_WITH_CLI: "true" + - PARTS_PYTHON_VENV_ARGS: --upgrade + # Constraints are passed through the environment variable PIP_CONSTRAINTS instead of using the + # parts.[part_name].constraints option available in snapcraft.yaml when the Python plugin is + # used. This is done to let these constraints be applied not only on the certbot package + # build, but also on any isolated build that pip could trigger when building wheels for + # dependencies. See https://github.com/certbot/certbot/pull/8443 for more info. + - PIP_CONSTRAINT: $CRAFT_PART_SRC/snap-constraints.txt + # This is a workaround for + # https://github.com/pypa/setuptools/issues/5039 forcing pip to use + # modern build conventions even in the absence of pyproject.toml files. + - PIP_USE_PEP517: "true" + override-build: | + python3 -m venv "${CRAFT_PART_INSTALL}" + "${CRAFT_PART_INSTALL}/bin/python3" "${CRAFT_PART_SRC}/tools/pipstrap.py" + craftctl default + # Workaround for misconfigured pyvenv.cfg; see discussion on snapcraft forum for more info + # https://forum.snapcraft.io/t/upgrading-classic-snap-to-core24-using-snapcraft-8-3-causes-python-3-12-errors-at-runtime/ + sed -i "${CRAFT_PART_INSTALL}/pyvenv.cfg" \ + -e 's@^home = '"${CRAFT_PART_INSTALL}"'/usr/bin$@home = /snap/certbot/current/usr/bin@g' + override-pull: | + craftctl default + grep -v python-augeas "${CRAFT_PART_SRC}/tools/requirements.txt" >> "${CRAFT_PART_SRC}/snap-constraints.txt" + craftctl set version=$(grep -oP "__version__ = '\K.*(?=')" "${CRAFT_PART_SRC}/certbot/src/certbot/__init__.py") + build-attributes: + # https://snapcraft.io/docs/how-to-classic#fix-linter-warnings-by-patching-elf-binaries + - enable-patchelf + shared-metadata: + plugin: dump + source: . + override-pull: | + craftctl default + mkdir -p certbot-metadata + grep -oP "__version__ = '\K.*(?=')" $CRAFT_PART_SRC/certbot/src/certbot/__init__.py > certbot-metadata/certbot-version.txt + stage: [certbot-metadata/certbot-version.txt] + +plugs: + plugin: + interface: content + content: certbot-1 + target: $SNAP/certbot-plugin + +slots: + certbot-metadata: + interface: content + content: metadata-1 + read: [$SNAP/certbot-metadata] diff --git a/tests/boulder-fetch.sh b/tests/boulder-fetch.sh deleted file mode 100755 index f34deb74e12..00000000000 --- a/tests/boulder-fetch.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash -# Download and run Boulder instance for integration testing -set -xe - -# Clone Boulder into a GOPATH-style directory structure even if Go isn't -# installed, because Boulder's docker-compose.yml file wll look for it there. -export GOPATH=${GOPATH:-$HOME/gopath} -BOULDERPATH=${BOULDERPATH:-$GOPATH/src/github.com/letsencrypt/boulder} -if [ ! -d ${BOULDERPATH} ]; then - git clone --depth=1 https://github.com/letsencrypt/boulder ${BOULDERPATH} -fi - -cd ${BOULDERPATH} - -docker-compose up -d boulder - -set +x # reduce verbosity while waiting for boulder -for n in `seq 1 150` ; do - if curl http://localhost:4000/directory 2>/dev/null; then - break - else - sleep 1 - fi -done - -if ! curl http://localhost:4000/directory 2>/dev/null; then - echo "timed out waiting for boulder to start" - exit 1 -fi - -# Setup the DNS resolution used by boulder instance to docker host -curl -X POST -d '{"ip":"10.77.77.1"}' http://localhost:8055/set-default-ipv4 diff --git a/tests/boulder-integration.sh b/tests/boulder-integration.sh deleted file mode 100755 index 3e16fcbbc7b..00000000000 --- a/tests/boulder-integration.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -set -e - -if [ "$INTEGRATION_TEST" = "certbot" ]; then - tests/certbot-boulder-integration.sh -elif [ "$INTEGRATION_TEST" = "nginx" ]; then - certbot-nginx/tests/boulder-integration.sh -else - tests/certbot-boulder-integration.sh - # Most CI systems set this variable to true. - # If the tests are running as part of CI, Nginx should be available. - if ${CI:-false} || type nginx; then - certbot-nginx/tests/boulder-integration.sh - fi -fi diff --git a/tests/certbot-boulder-integration.sh b/tests/certbot-boulder-integration.sh deleted file mode 100755 index 630571148b2..00000000000 --- a/tests/certbot-boulder-integration.sh +++ /dev/null @@ -1,529 +0,0 @@ -#!/bin/bash -# Simple integration test. Make sure to activate virtualenv beforehand -# (source venv/bin/activate) and that you are running Boulder test -# instance (see ./boulder-fetch.sh). -# -# Environment variables: -# SERVER: Passed as "certbot --server" argument. -# -# Note: this script is called by Boulder integration test suite! - -set -eux - -# Check that python executable is available in the PATH. Fail immediatly if not. -command -v python > /dev/null || (echo "Error, python executable is not in the PATH" && exit 1) - -. ./tests/integration/_common.sh -export PATH="$PATH:/usr/sbin" # /usr/sbin/nginx - -cleanup_and_exit() { - EXIT_STATUS=$? - if SERVER_STILL_RUNNING=`ps -p $python_server_pid -o pid=` - then - echo Kill server subprocess, left running by abnormal exit - kill $SERVER_STILL_RUNNING - fi - if [ -f "$HOOK_DIRS_TEST" ]; then - rm -f "$HOOK_DIRS_TEST" - fi - exit $EXIT_STATUS -} - -trap cleanup_and_exit EXIT - -export HOOK_DIRS_TEST="$(mktemp)" -renewal_hooks_root="$config_dir/renewal-hooks" -renewal_hooks_dirs=$(echo "$renewal_hooks_root/"{pre,deploy,post}) -renewal_dir_pre_hook="$(echo $renewal_hooks_dirs | cut -f 1 -d " ")/hook.sh" -renewal_dir_deploy_hook="$(echo $renewal_hooks_dirs | cut -f 2 -d " ")/hook.sh" -renewal_dir_post_hook="$(echo $renewal_hooks_dirs | cut -f 3 -d " ")/hook.sh" - -# Creates hooks in Certbot's renewal hook directory that write to a file -CreateDirHooks() { - for hook_dir in $renewal_hooks_dirs; do - mkdir -p $hook_dir - hook_path="$hook_dir/hook.sh" - cat << EOF > "$hook_path" -#!/bin/bash -xe -if [ "\$0" = "$renewal_dir_deploy_hook" ]; then - if [ -z "\$RENEWED_DOMAINS" -o -z "\$RENEWED_LINEAGE" ]; then - echo "Environment variables not properly set!" >&2 - exit 1 - fi -fi -echo \$(basename \$(dirname "\$0")) >> "\$HOOK_DIRS_TEST" -EOF - chmod +x "$hook_path" - done -} - -# Asserts that the hooks created by CreateDirHooks have been run once and -# resets the file. -# -# Arguments: -# The number of times the deploy hook should have been run. (It should run -# once for each certificate that was issued in that run of Certbot.) -CheckDirHooks() { - expected="pre\n" - for ((i=0; i<$1; i++)); do - expected=$expected"deploy\n" - done - expected=$expected"post" - - if ! diff "$HOOK_DIRS_TEST" <(echo -e "$expected"); then - echo "Unexpected directory hook output!" >&2 - echo "Expected:" >&2 - echo -e "$expected" >&2 - echo "Got:" >&2 - cat "$HOOK_DIRS_TEST" >&2 - exit 1 - fi - - rm -f "$HOOK_DIRS_TEST" - export HOOK_DIRS_TEST="$(mktemp)" -} - -common_no_force_renew() { - certbot_test_no_force_renew \ - --authenticator standalone \ - --installer null \ - "$@" -} - -common() { - common_no_force_renew \ - --renew-by-default \ - "$@" -} - -export HOOK_TEST="/tmp/hook$$" -CheckHooks() { - if [ $(head -n1 "$HOOK_TEST") = "wtf.pre" ]; then - expected="wtf.pre\ndeploy\n" - if [ $(sed '3q;d' "$HOOK_TEST") = "deploy" ]; then - expected=$expected"deploy\nwtf2.pre\n" - else - expected=$expected"wtf2.pre\ndeploy\n" - fi - expected=$expected"deploy\ndeploy\nwtf.post\nwtf2.post" - else - expected="wtf2.pre\ndeploy\n" - if [ $(sed '3q;d' "$HOOK_TEST") = "deploy" ]; then - expected=$expected"deploy\nwtf.pre\n" - else - expected=$expected"wtf.pre\ndeploy\n" - fi - expected=$expected"deploy\ndeploy\nwtf2.post\nwtf.post" - fi - - if ! cmp --quiet <(echo -e "$expected") "$HOOK_TEST" ; then - echo Hooks did not run as expected\; got >&2 - cat "$HOOK_TEST" >&2 - echo -e "Expected\n$expected" >&2 - rm "$HOOK_TEST" - exit 1 - fi - rm "$HOOK_TEST" -} - -# Checks if deploy is in the hook output and deletes the file -DeployInHookOutput() { - CONTENTS=$(cat "$HOOK_TEST") - rm "$HOOK_TEST" - grep deploy <(echo "$CONTENTS") -} - -# Asserts that there is a saved renew_hook for a lineage. -# -# Arguments: -# Name of lineage to check -CheckSavedRenewHook() { - if ! grep renew_hook "$config_dir/renewal/$1.conf"; then - echo "Hook wasn't saved as renew_hook" >&2 - exit 1 - fi -} - -# Asserts the deploy hook was properly run and saved and deletes the hook file -# -# Arguments: -# Lineage name of the issued cert -CheckDeployHook() { - if ! DeployInHookOutput; then - echo "The deploy hook wasn't run" >&2 - exit 1 - fi - CheckSavedRenewHook $1 -} - -# Asserts the renew hook wasn't run but was saved and deletes the hook file -# -# Arguments: -# Lineage name of the issued cert -# Asserts the deploy hook wasn't run and deletes the hook file -CheckRenewHook() { - if DeployInHookOutput; then - echo "The renew hook was incorrectly run" >&2 - exit 1 - fi - CheckSavedRenewHook $1 -} - -# Return success only if input contains exactly $1 lines of text, of -# which $2 different values occur in the first field. -TotalAndDistinctLines() { - total=$1 - distinct=$2 - awk '{a[$1] = 1}; END {n = 0; for (i in a) { n++ }; exit(NR !='$total' || n !='$distinct')}' -} - -# Cleanup coverage data -coverage erase - -# test for regressions of #4719 -get_num_tmp_files() { - ls -1 /tmp | wc -l -} -num_tmp_files=$(get_num_tmp_files) -common --csr / > /dev/null && echo expected error && exit 1 || true -common --help > /dev/null -common --help all > /dev/null -common --version > /dev/null -if [ $(get_num_tmp_files) -ne $num_tmp_files ]; then - echo "New files or directories created in /tmp!" - exit 1 -fi -CreateDirHooks - -common register -for dir in $renewal_hooks_dirs; do - if [ ! -d "$dir" ]; then - echo "Hook directory not created by Certbot!" >&2 - exit 1 - fi -done - -common unregister - -common register --email ex1@domain.org,ex2@domain.org - -# TODO: When `certbot register --update-registration` is fully deprecated, delete the two following deprecated uses - -common register --update-registration --email ex1@domain.org - -common register --update-registration --email ex1@domain.org,ex2@domain.org - -common update_account --email example@domain.org - -common update_account --email ex1@domain.org,ex2@domain.org - -common plugins --init --prepare | grep webroot - -# We start a server listening on the port for the -# unrequested challenge to prevent regressions in #3601. -python ./tests/run_http_server.py $http_01_port & -python_server_pid=$! - -certname="le1.wtf" -common --domains le1.wtf --preferred-challenges tls-sni-01 auth \ - --cert-name $certname \ - --pre-hook 'echo wtf.pre >> "$HOOK_TEST"' \ - --post-hook 'echo wtf.post >> "$HOOK_TEST"'\ - --deploy-hook 'echo deploy >> "$HOOK_TEST"' -kill $python_server_pid -CheckDeployHook $certname - -python ./tests/run_http_server.py $tls_sni_01_port & -python_server_pid=$! -certname="le2.wtf" -common --domains le2.wtf --preferred-challenges http-01 run \ - --cert-name $certname \ - --pre-hook 'echo wtf.pre >> "$HOOK_TEST"' \ - --post-hook 'echo wtf.post >> "$HOOK_TEST"'\ - --deploy-hook 'echo deploy >> "$HOOK_TEST"' -kill $python_server_pid -CheckDeployHook $certname - -certname="le.wtf" -common certonly -a manual -d le.wtf --rsa-key-size 4096 --cert-name $certname \ - --manual-auth-hook ./tests/manual-http-auth.sh \ - --manual-cleanup-hook ./tests/manual-http-cleanup.sh \ - --pre-hook 'echo wtf2.pre >> "$HOOK_TEST"' \ - --post-hook 'echo wtf2.post >> "$HOOK_TEST"' \ - --renew-hook 'echo deploy >> "$HOOK_TEST"' -CheckRenewHook $certname - -certname="dns.le.wtf" -common -a manual -d dns.le.wtf --preferred-challenges dns,tls-sni run \ - --cert-name $certname \ - --manual-auth-hook ./tests/manual-dns-auth.sh \ - --manual-cleanup-hook ./tests/manual-dns-cleanup.sh \ - --pre-hook 'echo wtf2.pre >> "$HOOK_TEST"' \ - --post-hook 'echo wtf2.post >> "$HOOK_TEST"' \ - --renew-hook 'echo deploy >> "$HOOK_TEST"' -CheckRenewHook $certname - -common certonly --cert-name newname -d newname.le.wtf - -export CSR_PATH="${root}/csr.der" KEY_PATH="${root}/key.pem" \ - OPENSSL_CNF=examples/openssl.cnf -./examples/generate-csr.sh le3.wtf -common auth --csr "$CSR_PATH" \ - --cert-path "${root}/csr/cert.pem" \ - --chain-path "${root}/csr/chain.pem" -openssl x509 -in "${root}/csr/cert.pem" -text -openssl x509 -in "${root}/csr/chain.pem" -text - -common --domains le3.wtf install \ - --cert-path "${root}/csr/cert.pem" \ - --key-path "${root}/key.pem" - -CheckCertCount() { - CERTCOUNT=`ls "${root}/conf/archive/$1/cert"* | wc -l` - if [ "$CERTCOUNT" -ne "$2" ] ; then - echo Wrong cert count, not "$2" `ls "${root}/conf/archive/$1/"*` - exit 1 - fi -} - -CheckPermissions() { -# Args: -# Checks mode of two files match under - masked_mode() { echo $((0`stat -c %a $1` & 0$2)); } - if [ `masked_mode $1 $3` -ne `masked_mode $2 $3` ] ; then - echo "With $3 mask, expected mode `masked_mode $1 $3`, got `masked_mode $2 $3` on file $2" - exit 1 - fi -} - -CheckGID() { -# Args: -# Checks group owner of two files match - group_owner() { echo `stat -c %G $1`; } - if [ `group_owner $1` != `group_owner $2` ] ; then - echo "Expected group owner `group_owner $1`, got `group_owner $2` on file $2" - exit 1 - fi -} - -CheckOthersPermission() { -# Args: -# Tests file's other/world permission against expected mode - other_permission=$((0`stat -c %a $1` & 07)) - if [ $other_permission -ne $2 ] ; then - echo "Expected file $1 to have others mode $2, got $other_permission instead" - exit 1 - fi -} - -CheckCertCount "le.wtf" 1 - -# This won't renew (because it's not time yet) -common_no_force_renew renew -CheckCertCount "le.wtf" 1 -if [ -s "$HOOK_DIRS_TEST" ]; then - echo "Directory hooks were executed for non-renewal!" >&2; - exit 1 -fi - -rm -rf "$renewal_hooks_root" -# renew using HTTP manual auth hooks -common renew --cert-name le.wtf --authenticator manual -CheckCertCount "le.wtf" 2 - -CheckOthersPermission "${root}/conf/archive/le.wtf/privkey1.pem" 0 -CheckOthersPermission "${root}/conf/archive/le.wtf/privkey2.pem" 0 -CheckPermissions "${root}/conf/archive/le.wtf/privkey1.pem" "${root}/conf/archive/le.wtf/privkey2.pem" 074 -CheckGID "${root}/conf/archive/le.wtf/privkey1.pem" "${root}/conf/archive/le.wtf/privkey2.pem" -chmod 0444 "${root}/conf/archive/le.wtf/privkey2.pem" - -# test renewal with no executables in hook directories -for hook_dir in $renewal_hooks_dirs; do - touch "$hook_dir/file" - mkdir "$hook_dir/dir" -done -# renew using DNS manual auth hooks -common renew --cert-name dns.le.wtf --authenticator manual -CheckCertCount "dns.le.wtf" 2 - -# test with disabled directory hooks -rm -rf "$renewal_hooks_root" -CreateDirHooks -# This will renew because the expiry is less than 10 years from now -sed -i "4arenew_before_expiry = 4 years" "$root/conf/renewal/le.wtf.conf" -common_no_force_renew renew --rsa-key-size 2048 --no-directory-hooks -CheckCertCount "le.wtf" 3 -CheckGID "${root}/conf/archive/le.wtf/privkey2.pem" "${root}/conf/archive/le.wtf/privkey3.pem" -CheckPermissions "${root}/conf/archive/le.wtf/privkey2.pem" "${root}/conf/archive/le.wtf/privkey3.pem" 074 -CheckOthersPermission "${root}/conf/archive/le.wtf/privkey3.pem" 04 - -if [ -s "$HOOK_DIRS_TEST" ]; then - echo "Directory hooks were executed with --no-directory-hooks!" >&2 - exit 1 -fi - -# The 4096 bit setting should persist to the first renewal, but be overridden in the second - -size1=`wc -c ${root}/conf/archive/le.wtf/privkey1.pem | cut -d" " -f1` -size2=`wc -c ${root}/conf/archive/le.wtf/privkey2.pem | cut -d" " -f1` -size3=`wc -c ${root}/conf/archive/le.wtf/privkey3.pem | cut -d" " -f1` -# 4096 bit PEM keys are about ~3270 bytes, 2048 ones are about 1700 bytes -if [ "$size1" -lt 3000 ] || [ "$size2" -lt 3000 ] || [ "$size3" -gt 1800 ] ; then - echo key sizes violate assumptions: - ls -l "${root}/conf/archive/le.wtf/privkey"* - exit 1 -fi - -# --renew-by-default is used, so renewal should occur -[ -f "$HOOK_TEST" ] && rm -f "$HOOK_TEST" -common renew -CheckCertCount "le.wtf" 4 -CheckHooks -CheckDirHooks 5 - -# test with overlapping directory hooks on the command line -common renew --cert-name le2.wtf \ - --pre-hook "$renewal_dir_pre_hook" \ - --deploy-hook "$renewal_dir_deploy_hook" \ - --post-hook "$renewal_dir_post_hook" -CheckDirHooks 1 - -# test with overlapping directory hooks in the renewal conf files -common renew --cert-name le2.wtf -CheckDirHooks 1 - -# manual-dns-auth.sh will skip completing the challenge for domains that begin -# with fail. -common -a manual -d dns1.le.wtf,fail.dns1.le.wtf \ - --allow-subset-of-names \ - --preferred-challenges dns,tls-sni \ - --manual-auth-hook ./tests/manual-dns-auth.sh \ - --manual-cleanup-hook ./tests/manual-dns-cleanup.sh - -if common certificates | grep "fail\.dns1\.le\.wtf"; then - echo "certificate should not have been issued for domain!" >&2 - exit 1 -fi - -# reuse-key -common --domains reusekey.le.wtf --reuse-key -common renew --cert-name reusekey.le.wtf -CheckCertCount "reusekey.le.wtf" 2 -ls -l "${root}/conf/archive/reusekey.le.wtf/privkey"* -# The final awk command here exits successfully if its input consists of -# exactly two lines with identical first fields, and unsuccessfully otherwise. -sha256sum "${root}/conf/archive/reusekey.le.wtf/privkey"* | TotalAndDistinctLines 2 1 - -# don't reuse key (just by forcing reissuance without --reuse-key) -common --cert-name reusekey.le.wtf --domains reusekey.le.wtf --force-renewal -CheckCertCount "reusekey.le.wtf" 3 -ls -l "${root}/conf/archive/reusekey.le.wtf/privkey"* -# Exactly three lines, of which exactly two identical first fields. -sha256sum "${root}/conf/archive/reusekey.le.wtf/privkey"* | TotalAndDistinctLines 3 2 - -# Nonetheless, all three certificates are different even though two of them -# share the same subject key. -sha256sum "${root}/conf/archive/reusekey.le.wtf/cert"* | TotalAndDistinctLines 3 3 - -# ECDSA -openssl ecparam -genkey -name secp384r1 -out "${root}/privkey-p384.pem" -SAN="DNS:ecdsa.le.wtf" openssl req -new -sha256 \ - -config "${OPENSSL_CNF:-openssl.cnf}" \ - -key "${root}/privkey-p384.pem" \ - -subj "/" \ - -reqexts san \ - -outform der \ - -out "${root}/csr-p384.der" -common auth --csr "${root}/csr-p384.der" \ - --cert-path "${root}/csr/cert-p384.pem" \ - --chain-path "${root}/csr/chain-p384.pem" -openssl x509 -in "${root}/csr/cert-p384.pem" -text | grep 'ASN1 OID: secp384r1' - -# OCSP Must Staple -common auth --must-staple --domains "must-staple.le.wtf" -openssl x509 -in "${root}/conf/live/must-staple.le.wtf/cert.pem" -text | grep -E 'status_request|1\.3\.6\.1\.5\.5\.7\.1\.24' - -# revoke by account key -common revoke --cert-path "$root/conf/live/le.wtf/cert.pem" --delete-after-revoke -# revoke renewed -common revoke --cert-path "$root/conf/live/le1.wtf/cert.pem" --no-delete-after-revoke -if [ ! -d "$root/conf/live/le1.wtf" ]; then - echo "cert deleted when --no-delete-after-revoke was used!" - exit 1 -fi -common delete --cert-name le1.wtf -# revoke by cert key -common revoke --cert-path "$root/conf/live/le2.wtf/cert.pem" \ - --key-path "$root/conf/live/le2.wtf/privkey.pem" - -# Get new certs to test revoke with a reason, by account and by cert key -common --domains le1.wtf -common revoke --cert-path "$root/conf/live/le1.wtf/cert.pem" \ - --reason cessationOfOperation -common --domains le2.wtf -common revoke --cert-path "$root/conf/live/le2.wtf/cert.pem" \ - --key-path "$root/conf/live/le2.wtf/privkey.pem" \ - --reason keyCompromise - -common unregister - -out=$(common certificates) -subdomains="le dns.le newname.le must-staple.le" -for subdomain in $subdomains; do - domain="$subdomain.wtf" - if ! echo $out | grep "$domain"; then - echo "$domain not in certificates output!" - exit 1; - fi -done - -# Testing that revocation also deletes by default -subdomains="le1 le2" -for subdomain in $subdomains; do - domain="$subdomain.wtf" - if echo $out | grep "$domain"; then - echo "Revoked $domain in certificates output! Should not be!" - exit 1; - fi -done - -# Test that revocation raises correct error when both --cert-name and --cert-path specified -common --domains le1.wtf -out=$(common revoke --cert-path "$root/conf/live/le1.wtf/fullchain.pem" --cert-name "le1.wtf" 2>&1) || true -if ! echo $out | grep "Exactly one of --cert-path or --cert-name must be specified"; then - echo "Non-interactive revoking with both --cert-name and --cert-path " - echo "did not raise the correct error!" - exit 1 -fi - -# Test that revocation doesn't delete if multiple lineages share an archive dir -common --domains le1.wtf -common --domains le2.wtf -sed -i "s|^archive_dir = .*$|archive_dir = $root/conf/archive/le1.wtf|" "$root/conf/renewal/le2.wtf.conf" -#common update_symlinks # not needed, but a bit more context for what this test is about -out=$(common revoke --cert-path "$root/conf/live/le1.wtf/cert.pem") -if ! echo $out | grep "Not deleting revoked certs due to overlapping archive dirs"; then - echo "Deleted a cert that had an overlapping archive dir with another lineage!" - exit 1 -fi - -cert_name="must-staple.le.wtf" -common delete --cert-name $cert_name -archive="$root/conf/archive/$cert_name" -conf="$root/conf/renewal/$cert_name.conf" -live="$root/conf/live/$cert_name" -for path in $archive $conf $live; do - if [ -e $path ]; then - echo "Lineage not properly deleted!" - exit 1 - fi -done - -# Test ACMEv2-only features -if [ "${BOULDER_INTEGRATION:-v1}" = "v2" ]; then - common -a manual -d '*.le4.wtf,le4.wtf' --preferred-challenges dns \ - --manual-auth-hook ./tests/manual-dns-auth.sh \ - --manual-cleanup-hook ./tests/manual-dns-cleanup.sh -fi - -coverage report --fail-under 64 --include 'certbot/*' --show-missing diff --git a/tests/certbot-pebble-integration.sh b/tests/certbot-pebble-integration.sh deleted file mode 100755 index 8711f72c18c..00000000000 --- a/tests/certbot-pebble-integration.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -# Simple integration test. Make sure to activate virtualenv beforehand -# (source venv/bin/activate) and that you are running Pebble test -# instance (see ./pebble-fetch.sh). - -cleanup_and_exit() { - EXIT_STATUS=$? - unset SERVER - exit $EXIT_STATUS -} - -trap cleanup_and_exit EXIT - -export SERVER=https://localhost:14000/dir - -./tests/certbot-boulder-integration.sh diff --git a/tests/display.py b/tests/display.py deleted file mode 100644 index 1f548e33d6a..00000000000 --- a/tests/display.py +++ /dev/null @@ -1,22 +0,0 @@ -"""Manual test of display functions.""" -import sys - -from certbot.display import util -from certbot.tests.display import util_test - - -def test_visual(displayer, choices): - """Visually test all of the display functions.""" - displayer.notification("Random notification!") - displayer.menu("Question?", choices, - ok_label="O", cancel_label="Can", help_label="??") - displayer.menu("Question?", [choice[1] for choice in choices], - ok_label="O", cancel_label="Can", help_label="??") - displayer.input("Input Message") - displayer.yesno("YesNo Message", yes_label="Yessir", no_label="Nosir") - displayer.checklist("Checklist Message", [choice[0] for choice in choices]) - - -if __name__ == "__main__": - displayer = util.FileDisplay(sys.stdout, False) - test_visual(displayer, util_test.CHOICES) diff --git a/tests/integration/_common.sh b/tests/integration/_common.sh deleted file mode 100755 index 83aa91a9ede..00000000000 --- a/tests/integration/_common.sh +++ /dev/null @@ -1,74 +0,0 @@ -# The -t is required on macOS. It provides a template file path for -# the kernel to use. -root=${root:-$(mktemp -d -t leitXXXX)} -echo "Root integration tests directory: $root" -config_dir="$root/conf" -tls_sni_01_port=5001 -http_01_port=5002 -sources="acme/,$(ls -dm certbot*/ | tr -d ' \n')" -export root config_dir tls_sni_01_port http_01_port sources -certbot_path="$(command -v certbot)" -# Flags that are added here will be added to Certbot calls within -# certbot_test_no_force_renew. -other_flags="--config-dir $config_dir --work-dir $root/work" -other_flags="$other_flags --logs-dir $root/logs" - -certbot_test () { - certbot_test_no_force_renew \ - --renew-by-default \ - "$@" -} - -# Succeeds if Certbot version is at least the given version number and fails -# otherwise. This is useful for making sure Certbot has certain features -# available. The patch version is currently ignored. -# -# Arguments: -# First argument is the minimum major version -# Second argument is the minimum minor version -version_at_least () { - # Certbot major and minor version (e.g. 0.30) - major_minor=$("$certbot_path" --version 2>&1 | cut -d' ' -f2 | cut -d. -f1,2) - major=$(echo "$major_minor" | cut -d. -f1) - minor=$(echo "$major_minor" | cut -d. -f2) - # Test that either the major version is greater or major version is equal - # and minor version is greater than or equal to. - [ \( "$major" -gt "$1" \) -o \( "$major" -eq "$1" -a "$minor" -ge "$2" \) ] -} - -# Use local ACMEv2 endpoint if requested and SERVER isn't already set. -if [ "${BOULDER_INTEGRATION:-v1}" = "v2" -a -z "${SERVER:+x}" ]; then - SERVER="http://localhost:4001/directory" -fi - -# --no-random-sleep-on-renew was added in -# https://github.com/certbot/certbot/pull/6599 and first released in Certbot -# 0.30.0. -if version_at_least 0 30; then - other_flags="$other_flags --no-random-sleep-on-renew" -fi - -certbot_test_no_force_renew () { - omit_patterns="*/*.egg-info/*,*/dns_common*,*/setup.py,*/test_*,*/tests/*" - omit_patterns="$omit_patterns,*_test.py,*_test_*,certbot-apache/*" - omit_patterns="$omit_patterns,certbot-compatibility-test/*,certbot-dns*/" - omit_patterns="$omit_patterns,certbot-nginx/certbot_nginx/parser_obj.py" - coverage run \ - --append \ - --source $sources \ - --omit $omit_patterns \ - "$certbot_path" \ - --server "${SERVER:-http://localhost:4000/directory}" \ - --no-verify-ssl \ - --tls-sni-01-port $tls_sni_01_port \ - --http-01-port $http_01_port \ - --manual-public-ip-logging-ok \ - $other_flags \ - --non-interactive \ - --no-redirect \ - --agree-tos \ - --register-unsafely-without-email \ - --debug \ - -vv \ - "$@" -} diff --git a/tests/letstest/README.md b/tests/letstest/README.md deleted file mode 100644 index 0155065b054..00000000000 --- a/tests/letstest/README.md +++ /dev/null @@ -1,73 +0,0 @@ -# letstest -Simple AWS testfarm scripts for certbot client testing - -- Configures (canned) boulder server -- Launches EC2 instances with a given list of AMIs for different distros -- Copies certbot repo and puts it on the instances -- Runs certbot tests (bash scripts) on all of these -- Logs execution and success/fail for debugging - -## Notes - - Some AWS images, e.g. official CentOS and FreeBSD images - require acceptance of user terms on the AWS marketplace - website. This can't be automated. - - AWS EC2 has a default limit of 20 t2/t1 instances, if more - are needed, they need to be requested via online webform. - -## Usage - - To install the necessary dependencies on Ubuntu 16.04, run: -``` -sudo apt install awscli python-yaml python-boto3 fabric -``` - - - Requires AWS IAM secrets to be set up with aws cli - - Requires an AWS associated keyfile .pem - -``` ->aws configure --profile -[interactive: enter secrets for IAM role] ->aws ec2 create-key-pair --profile --key-name --query 'KeyMaterial' --output text > whatever/path/you/want.pem -``` -Note: whatever you pick for `` will be shown to other users with AWS access. - -When prompted for a default region name, enter: `us-east-1` - -then: -``` ->python multitester.py targets.yaml /path/to/your/key.pem scripts/ -``` - -You can only run up to two tests at once. The following error is often indicative of there being too many AWS instances running on our account: -``` -NameError: name 'instances' is not defined -``` - -If you see this, you can run the following command to shut down all running instances: -``` -aws ec2 terminate-instances --profile --instance-ids $(aws ec2 describe-instances --profile | grep | cut -f8) -``` - -It will take a minute for these instances to shut down and become available again. Running this will invalidate any in progress tests. - -A folder named `letest-` is also created with a log file from each instance of the test and a file named "results" containing the output above. -The tests take quite a while to run. - -Also, the way all of the tests work is to check if there is already a boulder server running and if not start one. The boulder server is left running between tests, -and there are known issues if two instances of boulder attempt to be started. After starting your first test, wait until you see "Found existing boulder server:" or if you see output -about creating a boulder server, wait a minute before starting the 2nd test. You only have to do this after starting your first session of tests or after running -the `aws ec2 terminate-instances` command above. - -## Scripts -Example scripts are in the 'scripts' directory, these are just bash scripts that have a few parameters passed -to them at runtime via environment variables. test_apache2.sh is a useful reference. - -Note that the
test_letsencrypt_auto_*
scripts pull code from PyPI using the letsencrypt-auto script, -__not__ the local python code. test_apache2 runs the dev venv and does local tests. - -See: -- https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html -- https://docs.aws.amazon.com/cli/latest/userguide/cli-ec2-keypairs.html - -Main repos: -- https://github.com/letsencrypt/boulder -- https://github.com/letsencrypt/letsencrypt diff --git a/tests/letstest/apache2_targets.yaml b/tests/letstest/apache2_targets.yaml deleted file mode 100644 index e707b863606..00000000000 --- a/tests/letstest/apache2_targets.yaml +++ /dev/null @@ -1,57 +0,0 @@ -targets: - #----------------------------------------------------------------------------- - # Apache 2.4 - - ami: ami-26d5af4c - name: ubuntu15.10 - type: ubuntu - virt: hvm - user: ubuntu - - ami: ami-d92e6bb3 - name: ubuntu15.04LTS - type: ubuntu - virt: hvm - user: ubuntu - - ami: ami-7b89cc11 - name: ubuntu14.04LTS - type: ubuntu - virt: hvm - user: ubuntu - - ami: ami-9295d0f8 - name: ubuntu14.04LTS_32bit - type: ubuntu - virt: pv - user: ubuntu - - ami: ami-116d857a - name: debian8.1 - type: debian - virt: hvm - user: admin - userdata: | - #cloud-init - runcmd: - - [ apt-get, install, -y, curl ] - #----------------------------------------------------------------------------- - # Apache 2.2 - # - ami: ami-0611546c - # name: ubuntu12.04LTS - # type: ubuntu - # virt: hvm - # user: ubuntu - # - ami: ami-e0efab88 - # name: debian7.8.aws.1 - # type: debian - # virt: hvm - # user: admin - # userdata: | - # #cloud-init - # runcmd: - # - [ apt-get, install, -y, curl ] - # - ami: ami-e6eeaa8e - # name: debian7.8.aws.1_32bit - # type: debian - # virt: pv - # user: admin - # userdata: | - # #cloud-init - # runcmd: - # - [ apt-get, install, -y, curl ] \ No newline at end of file diff --git a/tests/letstest/multitester.py b/tests/letstest/multitester.py deleted file mode 100644 index 320328d9e29..00000000000 --- a/tests/letstest/multitester.py +++ /dev/null @@ -1,570 +0,0 @@ -""" -Certbot Integration Test Tool - -- Configures (canned) boulder server -- Launches EC2 instances with a given list of AMIs for different distros -- Copies certbot repo and puts it on the instances -- Runs certbot tests (bash scripts) on all of these -- Logs execution and success/fail for debugging - -Notes: - - Some AWS images, e.g. official CentOS and FreeBSD images - require acceptance of user terms on the AWS marketplace - website. This can't be automated. - - AWS EC2 has a default limit of 20 t2/t1 instances, if more - are needed, they need to be requested via online webform. - -Usage: - - Requires AWS IAM secrets to be set up with aws cli - - Requires an AWS associated keyfile .pem - ->aws configure --profile HappyHacker -[interactive: enter secrets for IAM role] ->aws ec2 create-key-pair --profile HappyHacker --key-name MyKeyPair \ - --query 'KeyMaterial' --output text > MyKeyPair.pem -then: ->python multitester.py targets.yaml MyKeyPair.pem HappyHacker scripts/test_letsencrypt_auto_venv_only.sh -see: - https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html - https://docs.aws.amazon.com/cli/latest/userguide/cli-ec2-keypairs.html -""" - -from __future__ import print_function -from __future__ import with_statement - -import sys, os, time, argparse, socket -import multiprocessing as mp -from multiprocessing import Manager -import urllib2 -import yaml -import boto3 -from botocore.exceptions import ClientError -import fabric -from fabric.api import run, execute, local, env, sudo, cd, lcd -from fabric.operations import get, put -from fabric.context_managers import shell_env - -# Command line parser -#------------------------------------------------------------------------------- -parser = argparse.ArgumentParser(description='Builds EC2 cluster for testing.') -parser.add_argument('config_file', - help='yaml configuration file for AWS server cluster') -parser.add_argument('key_file', - help='key file (.pem) for AWS') -parser.add_argument('aws_profile', - help='profile for AWS (i.e. as in ~/.aws/certificates)') -parser.add_argument('test_script', - default='test_letsencrypt_auto_certonly_standalone.sh', - help='path of bash script in to deploy and run') -#parser.add_argument('--script_args', -# nargs='+', -# help='space-delimited list of arguments to pass to the bash test script', -# required=False) -parser.add_argument('--repo', - default='https://github.com/letsencrypt/letsencrypt.git', - help='certbot git repo to use') -parser.add_argument('--branch', - default='~', - help='certbot git branch to trial') -parser.add_argument('--pull_request', - default='~', - help='letsencrypt/letsencrypt pull request to trial') -parser.add_argument('--merge_master', - action='store_true', - help="if set merges PR into master branch of letsencrypt/letsencrypt") -parser.add_argument('--saveinstances', - action='store_true', - help="don't kill EC2 instances after run, useful for debugging") -parser.add_argument('--alt_pip', - default='', - help="server from which to pull candidate release packages") -parser.add_argument('--killboulder', - action='store_true', - help="do not leave a persistent boulder server running") -parser.add_argument('--boulderonly', - action='store_true', - help="only make a boulder server") -parser.add_argument('--fast', - action='store_true', - help="use larger instance types to run faster (saves about a minute, probably not worth it)") -cl_args = parser.parse_args() - -# Credential Variables -#------------------------------------------------------------------------------- -# assumes naming: = .pem -KEYFILE = cl_args.key_file -KEYNAME = os.path.split(cl_args.key_file)[1].split('.pem')[0] -PROFILE = cl_args.aws_profile - -# Globals -#------------------------------------------------------------------------------- -BOULDER_AMI = 'ami-5f490b35' # premade shared boulder AMI 14.04LTS us-east-1 -LOGDIR = "" #points to logging / working directory -# boto3/AWS api globals -AWS_SESSION = None -EC2 = None -SECURITY_GROUP_NAME = 'certbot-security-group' -SUBNET_NAME = 'certbot-subnet' - -# Boto3/AWS automation functions -#------------------------------------------------------------------------------- -def should_use_subnet(subnet): - """Should we use the given subnet for these tests? - - We should if it is the default subnet for the availability zone or the - subnet is named "certbot-subnet". - - """ - if not subnet.map_public_ip_on_launch: - return False - if subnet.default_for_az: - return True - for tag in subnet.tags: - if tag['Key'] == 'Name' and tag['Value'] == SUBNET_NAME: - return True - return False - -def make_security_group(vpc): - """Creates a security group in the given VPC.""" - # will fail if security group of GroupName already exists - # cannot have duplicate SGs of the same name - mysg = vpc.create_security_group(GroupName=SECURITY_GROUP_NAME, - Description='security group for automated testing') - mysg.authorize_ingress(IpProtocol="tcp", CidrIp="0.0.0.0/0", FromPort=22, ToPort=22) - mysg.authorize_ingress(IpProtocol="tcp", CidrIp="0.0.0.0/0", FromPort=80, ToPort=80) - mysg.authorize_ingress(IpProtocol="tcp", CidrIp="0.0.0.0/0", FromPort=443, ToPort=443) - # for boulder wfe (http) server - mysg.authorize_ingress(IpProtocol="tcp", CidrIp="0.0.0.0/0", FromPort=4000, ToPort=4000) - # for mosh - mysg.authorize_ingress(IpProtocol="udp", CidrIp="0.0.0.0/0", FromPort=60000, ToPort=61000) - return mysg - -def make_instance(instance_name, - ami_id, - keyname, - security_group_id, - subnet_id, - machine_type='t2.micro', - userdata=""): #userdata contains bash or cloud-init script - - new_instance = EC2.create_instances( - BlockDeviceMappings=_get_block_device_mappings(ami_id), - ImageId=ami_id, - SecurityGroupIds=[security_group_id], - SubnetId=subnet_id, - KeyName=keyname, - MinCount=1, - MaxCount=1, - UserData=userdata, - InstanceType=machine_type)[0] - - # brief pause to prevent rare error on EC2 delay, should block until ready instead - time.sleep(1.0) - - # give instance a name - try: - new_instance.create_tags(Tags=[{'Key': 'Name', 'Value': instance_name}]) - except ClientError as e: - if "InvalidInstanceID.NotFound" in str(e): - # This seems to be ephemeral... retry - time.sleep(1) - new_instance.create_tags(Tags=[{'Key': 'Name', 'Value': instance_name}]) - else: - raise - return new_instance - -def _get_block_device_mappings(ami_id): - """Returns the list of block device mappings to ensure cleanup. - - This list sets connected EBS volumes to be deleted when the EC2 - instance is terminated. - - """ - # Not all devices use EBS, but the default value for DeleteOnTermination - # when the device does use EBS is true. See: - # * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-blockdev-mapping.html - # * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-blockdev-template.html - return [{'DeviceName': mapping['DeviceName'], - 'Ebs': {'DeleteOnTermination': True}} - for mapping in EC2.Image(ami_id).block_device_mappings - if not mapping.get('Ebs', {}).get('DeleteOnTermination', True)] - - -# Helper Routines -#------------------------------------------------------------------------------- -def block_until_http_ready(urlstring, wait_time=10, timeout=240): - "Blocks until server at urlstring can respond to http requests" - server_ready = False - t_elapsed = 0 - while not server_ready and t_elapsed < timeout: - try: - sys.stdout.write('.') - sys.stdout.flush() - req = urllib2.Request(urlstring) - response = urllib2.urlopen(req) - #if response.code == 200: - server_ready = True - except urllib2.URLError: - pass - time.sleep(wait_time) - t_elapsed += wait_time - -def block_until_ssh_open(ipstring, wait_time=10, timeout=120): - "Blocks until server at ipstring has an open port 22" - reached = False - t_elapsed = 0 - while not reached and t_elapsed < timeout: - try: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((ipstring, 22)) - reached = True - except socket.error as err: - time.sleep(wait_time) - t_elapsed += wait_time - sock.close() - -def block_until_instance_ready(booting_instance, wait_time=5, extra_wait_time=20): - "Blocks booting_instance until AWS EC2 instance is ready to accept SSH connections" - # the reinstantiation from id is necessary to force boto3 - # to correctly update the 'state' variable during init - _id = booting_instance.id - _instance = EC2.Instance(id=_id) - _state = _instance.state['Name'] - _ip = _instance.public_ip_address - while _state != 'running' or _ip is None: - time.sleep(wait_time) - _instance = EC2.Instance(id=_id) - _state = _instance.state['Name'] - _ip = _instance.public_ip_address - block_until_ssh_open(_ip) - time.sleep(extra_wait_time) - return _instance - - -# Fabric Routines -#------------------------------------------------------------------------------- -def local_git_clone(repo_url): - "clones master of repo_url" - with lcd(LOGDIR): - local('if [ -d letsencrypt ]; then rm -rf letsencrypt; fi') - local('git clone %s letsencrypt'% repo_url) - local('tar czf le.tar.gz letsencrypt') - -def local_git_branch(repo_url, branch_name): - "clones branch of repo_url" - with lcd(LOGDIR): - local('if [ -d letsencrypt ]; then rm -rf letsencrypt; fi') - local('git clone %s letsencrypt --branch %s --single-branch'%(repo_url, branch_name)) - local('tar czf le.tar.gz letsencrypt') - -def local_git_PR(repo_url, PRnumstr, merge_master=True): - "clones specified pull request from repo_url and optionally merges into master" - with lcd(LOGDIR): - local('if [ -d letsencrypt ]; then rm -rf letsencrypt; fi') - local('git clone %s letsencrypt'% repo_url) - local('cd letsencrypt && git fetch origin pull/%s/head:lePRtest'%PRnumstr) - local('cd letsencrypt && git checkout lePRtest') - if merge_master: - local('cd letsencrypt && git remote update origin') - local('cd letsencrypt && git merge origin/master -m "testmerge"') - local('tar czf le.tar.gz letsencrypt') - -def local_repo_to_remote(): - "copies local tarball of repo to remote" - with lcd(LOGDIR): - put(local_path='le.tar.gz', remote_path='') - run('tar xzf le.tar.gz') - -def local_repo_clean(): - "delete tarball" - with lcd(LOGDIR): - local('rm le.tar.gz') - -def deploy_script(scriptpath, *args): - "copies to remote and executes local script" - #with lcd('scripts'): - put(local_path=scriptpath, remote_path='', mirror_local_mode=True) - scriptfile = os.path.split(scriptpath)[1] - args_str = ' '.join(args) - run('./'+scriptfile+' '+args_str) - -def run_boulder(): - with cd('$GOPATH/src/github.com/letsencrypt/boulder'): - run('go run cmd/rabbitmq-setup/main.go -server amqp://localhost') - run('nohup ./start.py >& /dev/null < /dev/null &') - -def config_and_launch_boulder(instance): - execute(deploy_script, 'scripts/boulder_config.sh') - execute(run_boulder) - -def install_and_launch_certbot(instance, boulder_url, target): - execute(local_repo_to_remote) - with shell_env(BOULDER_URL=boulder_url, - PUBLIC_IP=instance.public_ip_address, - PRIVATE_IP=instance.private_ip_address, - PUBLIC_HOSTNAME=instance.public_dns_name, - PIP_EXTRA_INDEX_URL=cl_args.alt_pip, - OS_TYPE=target['type']): - execute(deploy_script, cl_args.test_script) - -def grab_certbot_log(): - "grabs letsencrypt.log via cat into logged stdout" - sudo('if [ -f /var/log/letsencrypt/letsencrypt.log ]; then \ - cat /var/log/letsencrypt/letsencrypt.log; else echo "[novarlog]"; fi') - # fallback file if /var/log is unwriteable...? correct? - sudo('if [ -f ./certbot.log ]; then \ - cat ./certbot.log; else echo "[nolocallog]"; fi') - -def create_client_instances(targetlist, security_group_id, subnet_id): - "Create a fleet of client instances" - instances = [] - print("Creating instances: ", end="") - for target in targetlist: - if target['virt'] == 'hvm': - machine_type = 't2.medium' if cl_args.fast else 't2.micro' - else: - # 32 bit systems - machine_type = 'c1.medium' if cl_args.fast else 't1.micro' - if 'userdata' in target.keys(): - userdata = target['userdata'] - else: - userdata = '' - name = 'le-%s'%target['name'] - print(name, end=" ") - instances.append(make_instance(name, - target['ami'], - KEYNAME, - machine_type=machine_type, - security_group_id=security_group_id, - subnet_id=subnet_id, - userdata=userdata)) - print() - return instances - - -def test_client_process(inqueue, outqueue): - cur_proc = mp.current_process() - for inreq in iter(inqueue.get, SENTINEL): - ii, target = inreq - - #save all stdout to log file - sys.stdout = open(LOGDIR+'/'+'%d_%s.log'%(ii,target['name']), 'w') - - print("[%s : client %d %s %s]" % (cur_proc.name, ii, target['ami'], target['name'])) - instances[ii] = block_until_instance_ready(instances[ii]) - print("server %s at %s"%(instances[ii], instances[ii].public_ip_address)) - env.host_string = "%s@%s"%(target['user'], instances[ii].public_ip_address) - print(env.host_string) - - try: - install_and_launch_certbot(instances[ii], boulder_url, target) - outqueue.put((ii, target, 'pass')) - print("%s - %s SUCCESS"%(target['ami'], target['name'])) - except: - outqueue.put((ii, target, 'fail')) - print("%s - %s FAIL"%(target['ami'], target['name'])) - pass - - # append server certbot.log to each per-machine output log - print("\n\ncertbot.log\n" + "-"*80 + "\n") - try: - execute(grab_certbot_log) - except: - print("log fail\n") - pass - - -def cleanup(cl_args, instances, targetlist): - print('Logs in ', LOGDIR) - if not cl_args.saveinstances: - print('Terminating EC2 Instances') - if cl_args.killboulder: - boulder_server.terminate() - for instance in instances: - instance.terminate() - else: - # print login information for the boxes for debugging - for ii, target in enumerate(targetlist): - print(target['name'], - target['ami'], - "%s@%s"%(target['user'], instances[ii].public_ip_address)) - - - -#------------------------------------------------------------------------------- -# SCRIPT BEGINS -#------------------------------------------------------------------------------- - -# Fabric library controlled through global env parameters -env.key_filename = KEYFILE -env.shell = '/bin/bash -l -i -c' -env.connection_attempts = 5 -env.timeout = 10 -# replace default SystemExit thrown by fabric during trouble -class FabricException(Exception): - pass -env['abort_exception'] = FabricException - -# Set up local copy of git repo -#------------------------------------------------------------------------------- -LOGDIR = "letest-%d"%int(time.time()) -print("Making local dir for test repo and logs: %s"%LOGDIR) -local('mkdir %s'%LOGDIR) - -# figure out what git object to test and locally create it in LOGDIR -print("Making local git repo") -try: - if cl_args.pull_request != '~': - print('Testing PR %s '%cl_args.pull_request, - "MERGING into master" if cl_args.merge_master else "") - execute(local_git_PR, cl_args.repo, cl_args.pull_request, cl_args.merge_master) - elif cl_args.branch != '~': - print('Testing branch %s of %s'%(cl_args.branch, cl_args.repo)) - execute(local_git_branch, cl_args.repo, cl_args.branch) - else: - print('Testing master of %s'%cl_args.repo) - execute(local_git_clone, cl_args.repo) -except FabricException: - print("FAIL: trouble with git repo") - exit() - - -# Set up EC2 instances -#------------------------------------------------------------------------------- -configdata = yaml.load(open(cl_args.config_file, 'r')) -targetlist = configdata['targets'] -print('Testing against these images: [%d total]'%len(targetlist)) -for target in targetlist: - print(target['ami'], target['name']) - -print("Connecting to EC2 using\n profile %s\n keyname %s\n keyfile %s"%(PROFILE, KEYNAME, KEYFILE)) -AWS_SESSION = boto3.session.Session(profile_name=PROFILE) -EC2 = AWS_SESSION.resource('ec2') - -print("Determining Subnet") -for subnet in EC2.subnets.all(): - if should_use_subnet(subnet): - subnet_id = subnet.id - vpc_id = subnet.vpc.id - break -else: - print("No usable subnet exists!") - print("Please create a VPC with a subnet named {0}".format(SUBNET_NAME)) - print("that maps public IPv4 addresses to instances launched in the subnet.") - sys.exit(1) - -print("Making Security Group") -vpc = EC2.Vpc(vpc_id) -sg_exists = False -for sg in vpc.security_groups.all(): - if sg.group_name == SECURITY_GROUP_NAME: - security_group_id = sg.id - sg_exists = True - print(" %s already exists"%SECURITY_GROUP_NAME) -if not sg_exists: - security_group_id = make_security_group(vpc).id - time.sleep(30) - -boulder_preexists = False -boulder_servers = EC2.instances.filter(Filters=[ - {'Name': 'tag:Name', 'Values': ['le-boulderserver']}, - {'Name': 'instance-state-name', 'Values': ['running']}]) - -boulder_server = next(iter(boulder_servers), None) - -print("Requesting Instances...") -if boulder_server: - print("Found existing boulder server:", boulder_server) - boulder_preexists = True -else: - print("Can't find a boulder server, starting one...") - boulder_server = make_instance('le-boulderserver', - BOULDER_AMI, - KEYNAME, - machine_type='t2.micro', - #machine_type='t2.medium', - security_group_id=security_group_id, - subnet_id=subnet_id) - -try: - if not cl_args.boulderonly: - instances = create_client_instances(targetlist, security_group_id, subnet_id) - - # Configure and launch boulder server - #------------------------------------------------------------------------------- - print("Waiting on Boulder Server") - boulder_server = block_until_instance_ready(boulder_server) - print(" server %s"%boulder_server) - - - # env.host_string defines the ssh user and host for connection - env.host_string = "ubuntu@%s"%boulder_server.public_ip_address - print("Boulder Server at (SSH):", env.host_string) - if not boulder_preexists: - print("Configuring and Launching Boulder") - config_and_launch_boulder(boulder_server) - # blocking often unnecessary, but cheap EC2 VMs can get very slow - block_until_http_ready('http://%s:4000'%boulder_server.public_ip_address, - wait_time=10, timeout=500) - - boulder_url = "http://%s:4000/directory"%boulder_server.private_ip_address - print("Boulder Server at (public ip): http://%s:4000/directory"%boulder_server.public_ip_address) - print("Boulder Server at (EC2 private ip): %s"%boulder_url) - - if cl_args.boulderonly: - sys.exit(0) - - # Install and launch client scripts in parallel - #------------------------------------------------------------------------------- - print("Uploading and running test script in parallel: %s"%cl_args.test_script) - print("Output routed to log files in %s"%LOGDIR) - # (Advice: always use Manager.Queue, never regular multiprocessing.Queue - # the latter has implementation flaws that deadlock it in some circumstances) - manager = Manager() - outqueue = manager.Queue() - inqueue = manager.Queue() - SENTINEL = None #queue kill signal - - # launch as many processes as clients to test - num_processes = len(targetlist) - jobs = [] #keep a reference to current procs - - - # initiate process execution - for i in range(num_processes): - p = mp.Process(target=test_client_process, args=(inqueue, outqueue)) - jobs.append(p) - p.daemon = True # kills subprocesses if parent is killed - p.start() - - # fill up work queue - for ii, target in enumerate(targetlist): - inqueue.put((ii, target)) - - # add SENTINELs to end client processes - for i in range(num_processes): - inqueue.put(SENTINEL) - # wait on termination of client processes - for p in jobs: - p.join() - # add SENTINEL to output queue - outqueue.put(SENTINEL) - - # clean up - execute(local_repo_clean) - - # print and save summary results - results_file = open(LOGDIR+'/results', 'w') - outputs = [outq for outq in iter(outqueue.get, SENTINEL)] - outputs.sort(key=lambda x: x[0]) - for outq in outputs: - ii, target, status = outq - print('%d %s %s'%(ii, target['name'], status)) - results_file.write('%d %s %s\n'%(ii, target['name'], status)) - results_file.close() - -finally: - cleanup(cl_args, instances, targetlist) - - # kill any connections - fabric.network.disconnect_all() diff --git a/tests/letstest/scripts/boulder_config.sh b/tests/letstest/scripts/boulder_config.sh deleted file mode 100755 index 1ef63ca10c2..00000000000 --- a/tests/letstest/scripts/boulder_config.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash -x - -# Configures and Launches Boulder Server installed on -# us-east-1 ami-5f490b35 bouldertestserver (boulder commit 8b433f54dab) - -# fetch instance data from EC2 metadata service -public_host=$(curl -s http://169.254.169.254/2014-11-05/meta-data/public-hostname) -public_ip=$(curl -s http://169.254.169.254/2014-11-05/meta-data/public-ipv4) -private_ip=$(curl -s http://169.254.169.254/2014-11-05/meta-data/local-ipv4) - -# get local DNS resolver for VPC -resolver_ip=$(grep nameserver /etc/resolv.conf |cut -d" " -f2 |head -1) -resolver=$resolver_ip':53' - -# modifies integration testing boulder setup for local AWS VPC network -# connections instead of localhost -cd $GOPATH/src/github.com/letsencrypt/boulder -# configure boulder to receive outside connection on 4000 -sed -i '/listenAddress/ s/127.0.0.1:4000/'$private_ip':4000/' ./test/boulder-config.json -sed -i '/baseURL/ s/127.0.0.1:4000/'$private_ip':4000/' ./test/boulder-config.json -# change test ports to real -sed -i '/httpPort/ s/5002/80/' ./test/boulder-config.json -sed -i '/httpsPort/ s/5001/443/' ./test/boulder-config.json -sed -i '/tlsPort/ s/5001/443/' ./test/boulder-config.json -# set local dns resolver -sed -i '/dnsResolver/ s/127.0.0.1:8053/'$resolver'/' ./test/boulder-config.json - -# start rabbitMQ -#go run cmd/rabbitmq-setup/main.go -server amqp://localhost -# start acme services -#nohup ./start.py >& /dev/null < /dev/null & -#./start.py diff --git a/tests/letstest/scripts/boulder_install.sh b/tests/letstest/scripts/boulder_install.sh deleted file mode 100755 index f997268bdfc..00000000000 --- a/tests/letstest/scripts/boulder_install.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash -x - -# >>>> only tested on Ubuntu 14.04LTS <<<< - -# Check out special branch until latest docker changes land in Boulder master. -git clone -b docker-integration https://github.com/letsencrypt/boulder $BOULDERPATH -cd $BOULDERPATH -FAKE_DNS=$(ifconfig docker0 | grep "inet addr:" | cut -d: -f2 | awk '{ print $1}') -sed -i "s/FAKE_DNS: .*/FAKE_DNS: $FAKE_DNS/" docker-compose.yml -docker-compose up -d diff --git a/tests/letstest/scripts/test_apache2.sh b/tests/letstest/scripts/test_apache2.sh deleted file mode 100755 index d24de24582b..00000000000 --- a/tests/letstest/scripts/test_apache2.sh +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/bash -x - -# $OS_TYPE $PUBLIC_IP $PRIVATE_IP $PUBLIC_HOSTNAME $BOULDER_URL -# are dynamically set at execution - -if [ "$OS_TYPE" = "ubuntu" ] -then - CONFFILE=/etc/apache2/sites-available/000-default.conf - sudo apt-get update - sudo apt-get -y --no-upgrade install apache2 #curl - sudo apt-get -y install realpath # needed for test-apache-conf - # For apache 2.4, set up ServerName - sudo sed -i '/ServerName/ s/#ServerName/ServerName/' $CONFFILE - sudo sed -i '/ServerName/ s/www.example.com/'$PUBLIC_HOSTNAME'/' $CONFFILE -elif [ "$OS_TYPE" = "centos" ] -then - CONFFILE=/etc/httpd/conf/httpd.conf - sudo setenforce 0 || true #disable selinux - sudo yum -y install httpd - sudo yum -y install nghttp2 || echo this is probably ok but see https://bugzilla.redhat.com/show_bug.cgi?id=1358875 - sudo service httpd start - sudo mkdir -p /var/www/$PUBLIC_HOSTNAME/public_html - sudo chmod -R oug+rwx /var/www - sudo chmod -R oug+rw /etc/httpd - sudo echo 'foobar' > /var/www/$PUBLIC_HOSTNAME/public_html/index.html - sudo mkdir /etc/httpd/sites-available #certbot requires this... - sudo mkdir /etc/httpd/sites-enabled #certbot requires this... - #sudo echo "IncludeOptional sites-enabled/*.conf" >> /etc/httpd/conf/httpd.conf - sudo echo """ - - ServerName $PUBLIC_HOSTNAME - DocumentRoot /var/www/$PUBLIC_HOSTNAME/public_html - ErrorLog /var/www/$PUBLIC_HOSTNAME/error.log - CustomLog /var/www/$PUBLIC_HOSTNAME/requests.log combined -""" >> /etc/httpd/conf.d/$PUBLIC_HOSTNAME.conf - #sudo cp /etc/httpd/sites-available/$PUBLIC_HOSTNAME.conf /etc/httpd/sites-enabled/ -fi - -# Run certbot-apache2. -cd letsencrypt - -echo "Bootstrapping dependencies..." -letsencrypt-auto-source/letsencrypt-auto --os-packages-only -if [ $? -ne 0 ] ; then - exit 1 -fi - -python tools/_venv_common.py -e acme[dev] -e .[dev,docs] -e certbot-apache -sudo venv/bin/certbot -v --debug --text --agree-dev-preview --agree-tos \ - --renew-by-default --redirect --register-unsafely-without-email \ - --domain $PUBLIC_HOSTNAME --server $BOULDER_URL -if [ $? -ne 0 ] ; then - FAIL=1 -fi - -if [ "$OS_TYPE" = "ubuntu" ] ; then - export SERVER="$BOULDER_URL" - venv/bin/tox -e apacheconftest -else - echo Not running hackish apache tests on $OS_TYPE -fi - -if [ $? -ne 0 ] ; then - FAIL=1 -fi - -# return error if any of the subtests failed -if [ "$FAIL" = 1 ] ; then - exit 1 -fi diff --git a/tests/letstest/scripts/test_leauto_upgrades.sh b/tests/letstest/scripts/test_leauto_upgrades.sh deleted file mode 100755 index 355fead2e90..00000000000 --- a/tests/letstest/scripts/test_leauto_upgrades.sh +++ /dev/null @@ -1,123 +0,0 @@ -#!/bin/bash -xe -set -o pipefail - -# $OS_TYPE $PUBLIC_IP $PRIVATE_IP $PUBLIC_HOSTNAME $BOULDER_URL -# are dynamically set at execution - -cd letsencrypt - -if ! command -v git ; then - if [ "$OS_TYPE" = "ubuntu" ] ; then - sudo apt-get update - fi - if ! ( sudo apt-get install -y git || sudo yum install -y git-all || sudo yum install -y git || sudo dnf install -y git ) ; then - echo git installation failed! - exit 1 - fi -fi -# 0.5.0 is the oldest version of letsencrypt-auto that can be used because it's -# the first version that pins package versions, properly supports -# --no-self-upgrade, and works with newer versions of pip. -git checkout -f v0.5.0 letsencrypt-auto -if ! ./letsencrypt-auto -v --debug --version --no-self-upgrade 2>&1 | grep 0.5.0 ; then - echo initial installation appeared to fail - exit 1 -fi - -# Now that python and openssl have been installed, we can set up a fake server -# to provide a new version of letsencrypt-auto. First, we start the server and -# directory to be served. -MY_TEMP_DIR=$(mktemp -d) -PORT_FILE="$MY_TEMP_DIR/port" -SERVER_PATH=$(tools/readlink.py tools/simple_http_server.py) -cd "$MY_TEMP_DIR" -"$SERVER_PATH" 0 > $PORT_FILE & -SERVER_PID=$! -trap 'kill "$SERVER_PID" && rm -rf "$MY_TEMP_DIR"' EXIT -cd ~- - -# Then, we set up the files to be served. -FAKE_VERSION_NUM="99.99.99" -echo "{\"releases\": {\"$FAKE_VERSION_NUM\": null}}" > "$MY_TEMP_DIR/json" -LE_AUTO_SOURCE_DIR="$MY_TEMP_DIR/v$FAKE_VERSION_NUM" -NEW_LE_AUTO_PATH="$LE_AUTO_SOURCE_DIR/letsencrypt-auto" -mkdir "$LE_AUTO_SOURCE_DIR" -cp letsencrypt-auto-source/letsencrypt-auto "$LE_AUTO_SOURCE_DIR/letsencrypt-auto" -SIGNING_KEY="letsencrypt-auto-source/tests/signing.key" -openssl dgst -sha256 -sign "$SIGNING_KEY" -out "$NEW_LE_AUTO_PATH.sig" "$NEW_LE_AUTO_PATH" - -# Next, we wait for the server to start and get the port number. -sleep 5s -SERVER_PORT=$(sed -n 's/.*port \([0-9]\+\).*/\1/p' "$PORT_FILE") - -# Finally, we set the necessary certbot-auto environment variables. -export LE_AUTO_DIR_TEMPLATE="http://localhost:$SERVER_PORT/%s/" -export LE_AUTO_JSON_URL="http://localhost:$SERVER_PORT/json" -export LE_AUTO_PUBLIC_KEY="-----BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsMoSzLYQ7E1sdSOkwelg -tzKIh2qi3bpXuYtcfFC0XrvWig071NwIj+dZiT0OLZ2hPispEH0B7ISuuWg1ll7G -hFW0VdbxL6JdGzS2ShNWkX9hE9z+j8VqwDPOBn3ZHm03qwpYkBDwQib3KqOdYbTT -uUtJmmGcuk3a9Aq/sCT6DdfmTSdP5asdQYwIcaQreDrOosaS84DTWI3IU+UYJVgl -LsIVPBuy9IcgHidUQ96hJnoPsDCWsHwX62495QKEarauyKQrJzFes0EY95orDM47 -Z5o/NDiQB11m91yNB0MmPYY9QSbnOA9j7IaaC97AwRLuwXY+/R2ablTcxurWou68 -iQIDAQAB ------END PUBLIC KEY----- -" - -if [ $(python -V 2>&1 | cut -d" " -f 2 | cut -d. -f1,2 | sed 's/\.//') -eq 26 ]; then - RUN_PYTHON3_TESTS=1 - if command -v python3; then - echo "Didn't expect Python 3 to be installed!" - exit 1 - fi - cp letsencrypt-auto cb-auto - if ! ./cb-auto -v --debug --version 2>&1 | grep 0.5.0 ; then - echo "Certbot shouldn't have updated to a new version!" - exit 1 - fi - if [ -d "/opt/eff.org" ]; then - echo "New directory shouldn't have been created!" - exit 1 - fi - # Create a 2nd venv at the new path to ensure we properly handle this case - export VENV_PATH="/opt/eff.org/certbot/venv" - if ! sudo -E ./letsencrypt-auto -v --debug --version --no-self-upgrade 2>&1 | grep 0.5.0 ; then - echo second installation appeared to fail - exit 1 - fi - unset VENV_PATH -fi - -if ./letsencrypt-auto -v --debug --version | grep "WARNING: couldn't find Python" ; then - echo "Had problems checking for updates!" - exit 1 -fi - -EXPECTED_VERSION=$(grep -m1 LE_AUTO_VERSION certbot-auto | cut -d\" -f2) -if ! /opt/eff.org/certbot/venv/bin/letsencrypt --version 2>&1 | grep "$EXPECTED_VERSION" ; then - echo upgrade appeared to fail - exit 1 -fi - -if ! diff letsencrypt-auto letsencrypt-auto-source/letsencrypt-auto ; then - echo letsencrypt-auto and letsencrypt-auto-source/letsencrypt-auto differ - exit 1 -fi - -if [ "$RUN_PYTHON3_TESTS" = 1 ]; then - if ! command -v python3; then - echo "Python3 wasn't properly installed" - exit 1 - fi - if [ "$(/opt/eff.org/certbot/venv/bin/python -V 2>&1 | cut -d" " -f 2 | cut -d. -f1)" != 3 ]; then - echo "Python3 wasn't used in venv!" - exit 1 - fi -fi -echo upgrade appeared to be successful - -if [ "$(tools/readlink.py ${XDG_DATA_HOME:-~/.local/share}/letsencrypt)" != "/opt/eff.org/certbot/venv" ]; then - echo symlink from old venv path not properly created! - exit 1 -fi -echo symlink properly created diff --git a/tests/letstest/scripts/test_letsencrypt_auto_certonly_standalone.sh b/tests/letstest/scripts/test_letsencrypt_auto_certonly_standalone.sh deleted file mode 100755 index 2cbe66a833e..00000000000 --- a/tests/letstest/scripts/test_letsencrypt_auto_certonly_standalone.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash -x -set -eo pipefail - -# $PUBLIC_IP $PRIVATE_IP $PUBLIC_HOSTNAME $BOULDER_URL are dynamically set at execution - -# with curl, instance metadata available from EC2 metadata service: -#public_host=$(curl -s http://169.254.169.254/2014-11-05/meta-data/public-hostname) -#public_ip=$(curl -s http://169.254.169.254/2014-11-05/meta-data/public-ipv4) -#private_ip=$(curl -s http://169.254.169.254/2014-11-05/meta-data/local-ipv4) - -cd letsencrypt -export PATH="$PWD/letsencrypt-auto-source:$PATH" -letsencrypt-auto --os-packages-only --debug --version -letsencrypt-auto certonly --no-self-upgrade -v --standalone --debug \ - --text --agree-dev-preview --agree-tos \ - --renew-by-default --redirect \ - --register-unsafely-without-email \ - --domain $PUBLIC_HOSTNAME --server $BOULDER_URL - -# we have to jump through some hoops to cope with relative paths in renewal -# conf files ... -# 1. be in the right directory -cd tests/letstest/testdata/ - -# 2. refer to the config with the same level of relativity that it itself -# contains :/ -OUT=`letsencrypt-auto certificates --config-dir sample-config -v --no-self-upgrade` -TEST_CERTS=`echo "$OUT" | grep TEST_CERT | wc -l` -REVOKED=`echo "$OUT" | grep REVOKED | wc -l` - -if [ "$TEST_CERTS" != 2 ] ; then - echo "Did not find two test certs as expected ($TEST_CERTS)" - exit 1 -fi - -if [ "$REVOKED" != 1 ] ; then - echo "Did not find one revoked cert as expected ($REVOKED)" - exit 1 -fi - -if ! letsencrypt-auto --help --no-self-upgrade | grep -F "letsencrypt-auto [SUBCOMMAND]"; then - echo "letsencrypt-auto not included in help output!" - exit 1 -fi diff --git a/tests/letstest/scripts/test_letsencrypt_auto_venv_only.sh b/tests/letstest/scripts/test_letsencrypt_auto_venv_only.sh deleted file mode 100755 index c55e12e8b64..00000000000 --- a/tests/letstest/scripts/test_letsencrypt_auto_venv_only.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -x - -# $PUBLIC_IP $PRIVATE_IP $PUBLIC_HOSTNAME $BOULDER_URL are dynamically set at execution - -cd letsencrypt -# help installs virtualenv and does nothing else -./letsencrypt-auto-source/letsencrypt-auto -v --debug --help all diff --git a/tests/letstest/scripts/test_renew_standalone.sh b/tests/letstest/scripts/test_renew_standalone.sh deleted file mode 100755 index 31c38ea46f8..00000000000 --- a/tests/letstest/scripts/test_renew_standalone.sh +++ /dev/null @@ -1,55 +0,0 @@ -#!/bin/bash -x - -# $OS_TYPE $PUBLIC_IP $PRIVATE_IP $PUBLIC_HOSTNAME $BOULDER_URL -# are dynamically set at execution - -# run certbot-apache2 via letsencrypt-auto -cd letsencrypt - -export SUDO=sudo -if [ -f /etc/debian_version ] ; then - echo "Bootstrapping dependencies for Debian-based OSes..." - $SUDO bootstrap/_deb_common.sh -elif [ -f /etc/redhat-release ] ; then - echo "Bootstrapping dependencies for RedHat-based OSes..." - $SUDO bootstrap/_rpm_common.sh -else - echo "Don't have bootstrapping for this OS!" - exit 1 -fi - -bootstrap/dev/venv.sh -sudo venv/bin/certbot certonly --debug --standalone -t --agree-dev-preview --agree-tos \ - --renew-by-default --redirect --register-unsafely-without-email \ - --domain $PUBLIC_HOSTNAME --server $BOULDER_URL -v -if [ $? -ne 0 ] ; then - FAIL=1 -fi - -if [ "$OS_TYPE" = "ubuntu" ] ; then - venv/bin/tox -e apacheconftest -else - echo Not running hackish apache tests on $OS_TYPE -fi - -if [ $? -ne 0 ] ; then - FAIL=1 -fi - -sudo venv/bin/certbot renew --renew-by-default - -if [ $? -ne 0 ] ; then - FAIL=1 -fi - - -ls /etc/letsencrypt/archive/$PUBLIC_HOSTNAME | grep -q 2.pem - -if [ $? -ne 0 ] ; then - FAIL=1 -fi - -# return error if any of the subtests failed -if [ "$FAIL" = 1 ] ; then - exit 1 -fi diff --git a/tests/letstest/scripts/test_sdists.sh b/tests/letstest/scripts/test_sdists.sh deleted file mode 100755 index 0b9a91ffd25..00000000000 --- a/tests/letstest/scripts/test_sdists.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/sh -xe - -cd letsencrypt -./certbot-auto --os-packages-only -n --debug - -PLUGINS="certbot-apache certbot-nginx" -PYTHON=$(command -v python2.7 || command -v python27 || command -v python2 || command -v python) -TEMP_DIR=$(mktemp -d) -VERSION=$(letsencrypt-auto-source/version.py) -export VENV_ARGS="-p $PYTHON" - -# setup venv -tools/_venv_common.py --requirement letsencrypt-auto-source/pieces/dependency-requirements.txt -. ./venv/bin/activate - -# build sdists -for pkg_dir in acme . $PLUGINS; do - cd $pkg_dir - python setup.py clean - rm -rf build dist - python setup.py sdist - mv dist/* $TEMP_DIR - cd - -done - -# test sdists -cd $TEMP_DIR -for pkg in acme certbot $PLUGINS; do - tar -xvf "$pkg-$VERSION.tar.gz" - cd "$pkg-$VERSION" - python setup.py build - python setup.py test - python setup.py install - cd - -done diff --git a/tests/letstest/scripts/test_tests.sh b/tests/letstest/scripts/test_tests.sh deleted file mode 100755 index e6ab836b81f..00000000000 --- a/tests/letstest/scripts/test_tests.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -xe - -LE_AUTO="letsencrypt/letsencrypt-auto-source/letsencrypt-auto" -LE_AUTO="$LE_AUTO --debug --no-self-upgrade --non-interactive" -MODULES="acme certbot certbot_apache certbot_nginx" -VENV_NAME=venv - -# *-auto respects VENV_PATH -$LE_AUTO --os-packages-only -LE_AUTO_SUDO="" VENV_PATH="$VENV_NAME" $LE_AUTO --no-bootstrap --version -. $VENV_NAME/bin/activate - -# change to an empty directory to ensure CWD doesn't affect tests -cd $(mktemp -d) -pip install pytest==3.2.5 - -for module in $MODULES ; do - echo testing $module - pytest -v --pyargs $module -done diff --git a/tests/letstest/scripts/test_tox.sh b/tests/letstest/scripts/test_tox.sh deleted file mode 100755 index bb912667376..00000000000 --- a/tests/letstest/scripts/test_tox.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash -x -XDG_DATA_HOME=${XDG_DATA_HOME:-~/.local/share} -VENV_NAME="venv" -# The path to the letsencrypt-auto script. Everything that uses these might -# at some point be inlined... -LEA_PATH=./letsencrypt/ -VENV_PATH=${LEA_PATH/$VENV_NAME} -VENV_BIN=${VENV_PATH}/bin - - -# virtualenv call is not idempotent: it overwrites pip upgraded in -# later steps, causing "ImportError: cannot import name unpack_url" - -"$LEA_PATH/letsencrypt-auto" --os-packages-only - -cd letsencrypt -python tools/venv.py -venv/bin/tox -e py27 diff --git a/tests/letstest/targets.yaml b/tests/letstest/targets.yaml deleted file mode 100644 index 57ce4811a56..00000000000 --- a/tests/letstest/targets.yaml +++ /dev/null @@ -1,66 +0,0 @@ -targets: - #----------------------------------------------------------------------------- - #Ubuntu - - ami: ami-7b89cc11 - name: ubuntu14.04LTS - type: ubuntu - virt: hvm - user: ubuntu - - ami: ami-9295d0f8 - name: ubuntu14.04LTS_32bit - type: ubuntu - virt: pv - user: ubuntu - #----------------------------------------------------------------------------- - # Debian - - ami: ami-116d857a - name: debian8.1 - type: ubuntu - virt: hvm - user: admin - # userdata: | - # #cloud-init - # runcmd: - # - [ apt-get, install, -y, curl ] - #----------------------------------------------------------------------------- - # Other Redhat Distros - - ami: ami-60b6c60a - name: amazonlinux-2015.09.1 - type: centos - virt: hvm - user: ec2-user - - ami: ami-0d4cfd66 - name: amazonlinux-2015.03.1 - type: centos - virt: hvm - user: ec2-user - - ami: ami-a8d369c0 - name: RHEL7 - type: centos - virt: hvm - user: ec2-user - - ami: ami-518bfb3b - name: fedora23 - type: centos - virt: hvm - user: fedora - #----------------------------------------------------------------------------- - # CentOS - # These Marketplace AMIs must, irritatingly, have their terms manually - # agreed to on the AWS marketplace site for any new AWS account using them... - - ami: ami-9887c6e7 - name: centos7 - type: centos - virt: hvm - user: centos - # centos6 requires EPEL repo added - - ami: ami-1585c46a - name: centos6 - type: centos - virt: hvm - user: centos - userdata: | - #cloud-config - runcmd: - - yum install -y epel-release - - iptables -F diff --git a/tests/lock_test.py b/tests/lock_test.py deleted file mode 100644 index 0266cf0295a..00000000000 --- a/tests/lock_test.py +++ /dev/null @@ -1,245 +0,0 @@ -"""Tests to ensure the lock order is preserved.""" -from __future__ import print_function - -import atexit -import functools -import logging -import os -import re -import shutil -import subprocess -import sys -import tempfile - -from certbot import lock -from certbot import util - -from certbot.tests import util as test_util - - -logger = logging.getLogger(__name__) - - -def main(): - """Run the lock tests.""" - dirs, base_cmd = set_up() - for subcommand in ('certonly', 'install', 'renew', 'run',): - logger.info('Testing subcommand: %s', subcommand) - test_command(base_cmd + [subcommand], dirs) - logger.info('Lock test ran successfully.') - - -def set_up(): - """Prepare tests to be run. - - Logging is set up and temporary directories are set up to contain a - basic Certbot and Nginx configuration. The directories are returned - in the order they should be locked by Certbot. If the Nginx plugin - is expected to work on the system, the Nginx directory is included, - otherwise, it is not. - - A Certbot command is also created that uses the temporary - directories. The returned command can be used to test different - subcommands by appending the desired command to the end. - - :returns: directories and command - :rtype: `tuple` of `list` - - """ - logging.basicConfig(format='%(message)s', level=logging.INFO) - config_dir, logs_dir, work_dir, nginx_dir = set_up_dirs() - command = set_up_command(config_dir, logs_dir, work_dir, nginx_dir) - - dirs = [logs_dir, config_dir, work_dir] - # Travis and Circle CI set CI to true so we - # will always test Nginx's lock during CI - if os.environ.get('CI') == 'true' or util.exe_exists('nginx'): - dirs.append(nginx_dir) - else: - logger.warning('Skipping Nginx lock tests') - - return dirs, command - - -def set_up_dirs(): - """Set up directories for tests. - - A temporary directory is created to contain the config, log, work, - and nginx directories. A sample renewal configuration is created in - the config directory and a basic Nginx config is placed in the Nginx - directory. The temporary directory containing all of these - directories is deleted when the program exits. - - :return value: config, log, work, and nginx directories - :rtype: `tuple` of `str` - - """ - temp_dir = tempfile.mkdtemp() - logger.debug('Created temporary directory: %s', temp_dir) - atexit.register(functools.partial(shutil.rmtree, temp_dir)) - - config_dir = os.path.join(temp_dir, 'config') - logs_dir = os.path.join(temp_dir, 'logs') - work_dir = os.path.join(temp_dir, 'work') - nginx_dir = os.path.join(temp_dir, 'nginx') - - for directory in (config_dir, logs_dir, work_dir, nginx_dir,): - os.mkdir(directory) - - test_util.make_lineage(config_dir, 'sample-renewal.conf') - set_up_nginx_dir(nginx_dir) - - return config_dir, logs_dir, work_dir, nginx_dir - - -def set_up_nginx_dir(root_path): - """Create a basic Nginx configuration in nginx_dir. - - :param str root_path: where the Nginx server root should be placed - - """ - # Get the root of the git repository - repo_root = check_call('git rev-parse --show-toplevel'.split()).strip() - conf_script = os.path.join( - repo_root, 'certbot-nginx', 'tests', 'boulder-integration.conf.sh') - # boulder-integration.conf.sh uses the root environment variable as - # the Nginx server root when writing paths - os.environ['root'] = root_path - with open(os.path.join(root_path, 'nginx.conf'), 'w') as f: - f.write(check_call(['/bin/sh', conf_script])) - del os.environ['root'] - - -def set_up_command(config_dir, logs_dir, work_dir, nginx_dir): - """Build the Certbot command to run for testing. - - You can test different subcommands by appending the desired command - to the returned list. - - :param str config_dir: path to the configuration directory - :param str logs_dir: path to the logs directory - :param str work_dir: path to the work directory - :param str nginx_dir: path to the nginx directory - - :returns: certbot command to execute for testing - :rtype: `list` of `str` - - """ - return ( - 'certbot --cert-path {0} --key-path {1} --config-dir {2} ' - '--logs-dir {3} --work-dir {4} --nginx-server-root {5} --debug ' - '--force-renewal --nginx --verbose '.format( - test_util.vector_path('cert.pem'), - test_util.vector_path('rsa512_key.pem'), - config_dir, logs_dir, work_dir, nginx_dir).split()) - - -def test_command(command, directories): - """Assert Certbot acquires locks in a specific order. - - command is run repeatedly testing that Certbot acquires locks on - directories in the order they appear in the parameter directories. - - :param list command: Certbot command to execute - :param list directories: list of directories Certbot should fail - to acquire the lock on in sorted order - - """ - locks = [lock.lock_dir(directory) for directory in directories] - for dir_path, dir_lock in zip(directories, locks): - check_error(command, dir_path) - dir_lock.release() - - -def check_error(command, dir_path): - """Run command and verify it fails to acquire the lock for dir_path. - - :param str command: certbot command to run - :param str dir_path: path to directory containing the lock Certbot - should fail on - - """ - ret, out, err = subprocess_call(command) - if ret == 0: - report_failure("Certbot didn't exit with a nonzero status!", out, err) - - match = re.search("Please see the logfile '(.*)' for more details", err) - if match is not None: - # Get error output from more verbose logfile - with open(match.group(1)) as f: - err = f.read() - - pattern = 'A lock on {0}.* is held by another process'.format(dir_path) - if not re.search(pattern, err): - err_msg = 'Directory path {0} not in error output!'.format(dir_path) - report_failure(err_msg, out, err) - - -def check_call(args): - """Simple imitation of subprocess.check_call. - - This function is only available in subprocess in Python 2.7+. - - :param list args: program and it's arguments to be run - - :returns: stdout output - :rtype: str - - """ - exit_code, out, err = subprocess_call(args) - if exit_code: - report_failure('Command exited with a nonzero status!', out, err) - return out - - -def report_failure(err_msg, out, err): - """Report a subprocess failure and exit. - - :param str err_msg: error message to report - :param str out: stdout output - :param str err: stderr output - - """ - logger.critical(err_msg) - log_output(logging.INFO, out, err) - sys.exit(err_msg) - - -def subprocess_call(args): - """Run a command with subprocess and return the result. - - :param list args: program and it's arguments to be run - - :returns: return code, stdout output, stderr output - :rtype: tuple - - """ - process = subprocess.Popen(args, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, universal_newlines=True) - out, err = process.communicate() - logger.debug('Return code was %d', process.returncode) - log_output(logging.DEBUG, out, err) - return process.returncode, out, err - - -def log_output(level, out, err): - """Logs stdout and stderr output at the requested level. - - :param int level: logging level to use - :param str out: stdout output - :param str err: stderr output - - """ - if out: - logger.log(level, 'Stdout output was:\n%s', out) - if err: - logger.log(level, 'Stderr output was:\n%s', err) - - -if __name__ == "__main__": - if os.name != 'nt': - main() - else: - print( - 'Warning: lock_test cannot be executed on Windows, ' - 'as it relies on a Nginx distribution for Linux.') diff --git a/tests/manual-dns-auth.sh b/tests/manual-dns-auth.sh deleted file mode 100755 index febecf455fb..00000000000 --- a/tests/manual-dns-auth.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -# If domain begins with fail, fail the challenge by not completing it. -if [[ "$CERTBOT_DOMAIN" != fail* ]]; then - curl -X POST 'http://localhost:8055/set-txt' -d \ - "{\"host\": \"_acme-challenge.$CERTBOT_DOMAIN.\", \ - \"value\": \"$CERTBOT_VALIDATION\"}" -fi diff --git a/tests/manual-dns-cleanup.sh b/tests/manual-dns-cleanup.sh deleted file mode 100755 index 1c09e892cb1..00000000000 --- a/tests/manual-dns-cleanup.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -# If domain begins with fail, we didn't complete the challenge so there is -# nothing to clean up. -if [[ "$CERTBOT_DOMAIN" != fail* ]]; then - curl -X POST 'http://localhost:8055/clear-txt' -d \ - "{\"host\": \"_acme-challenge.$CERTBOT_DOMAIN.\"}" -fi diff --git a/tests/manual-http-auth.sh b/tests/manual-http-auth.sh deleted file mode 100755 index 48c33f04bb3..00000000000 --- a/tests/manual-http-auth.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/sh -uri_path=".well-known/acme-challenge/$CERTBOT_TOKEN" - -# This script should be run from the top level. e.g. ./tests/manual-http-auth.sh -source_dir="$(pwd)" -cd $(mktemp -d) -mkdir -p $(dirname $uri_path) -echo $CERTBOT_VALIDATION > $uri_path -python "$source_dir/tests/run_http_server.py" $http_01_port >/dev/null 2>&1 & -server_pid=$! -while ! curl "http://localhost:$http_01_port/$uri_path" >/dev/null 2>&1; do - sleep 1s -done -echo $server_pid diff --git a/tests/manual-http-cleanup.sh b/tests/manual-http-cleanup.sh deleted file mode 100755 index 5e437bf08c8..00000000000 --- a/tests/manual-http-cleanup.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -kill $CERTBOT_AUTH_OUTPUT diff --git a/tests/modification-check.py b/tests/modification-check.py index 8abc0fbfe5c..1d56d3442e9 100755 --- a/tests/modification-check.py +++ b/tests/modification-check.py @@ -1,124 +1,45 @@ #!/usr/bin/env python +"""Ensures there have been no changes to important certbot-auto files.""" -from __future__ import print_function - +import hashlib import os -import subprocess -import sys -import tempfile -import shutil -try: - from urllib.request import urlretrieve -except ImportError: - from urllib import urlretrieve -def find_repo_path(): +# Relative to the root of the Certbot repo, these files are expected to exist +# and have the SHA-256 hashes contained in this dictionary. These hashes were +# taken from our v1.14.0 tag which was the last release we intended to make +# changes to certbot-auto. +# +# We can delete this script and the files under letsencrypt-auto-source when +# we're comfortable breaking any old certbot-auto scripts that haven't updated +# to the last version of the script yet. See +# https://opensource.eff.org/eff-open-source/pl/65geri7c4tr6iqunc1rpb3mpna and +# letsencrypt-auto-source/README.md for more info. +EXPECTED_FILES = { + os.path.join('letsencrypt-auto-source', 'letsencrypt-auto'): + 'b997e3608526650a08e36e682fc3bf0c29903c06fa5ba4cc49308c43832450c2', + os.path.join('letsencrypt-auto-source', 'letsencrypt-auto.sig'): + '61c036aabf75da350b0633da1b2bef0260303921ecda993455ea5e6d3af3b2fe', +} + + +def find_repo_root(): return os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -# We do not use filecmp.cmp to take advantage of universal newlines -# handling in open() for Python 3.x and be insensitive to CRLF/LF when run on Windows. -# As a consequence, this function will not work correctly if executed by Python 2.x on Windows. -# But it will work correctly on Linux for any version, because every file tested will be LF. -def compare_files(path_1, path_2): - l1 = l2 = True - with open(path_1, 'r') as f1, open(path_2, 'r') as f2: - line = 1 - while l1 and l2: - line += 1 - l1 = f1.readline() - l2 = f2.readline() - if l1 != l2: - print('---') - print(( - 'While comparing {0} (1) and {1} (2), a difference was found at line {2}:' - .format(os.path.basename(path_1), os.path.basename(path_2), line))) - print('(1): {0}'.format(repr(l1))) - print('(2): {0}'.format(repr(l2))) - print('---') - return False - - return True - -def validate_scripts_content(repo_path, temp_cwd): - errors = False - - if not compare_files( - os.path.join(repo_path, 'certbot-auto'), - os.path.join(repo_path, 'letsencrypt-auto')): - print('Root certbot-auto and letsencrypt-auto differ.') - errors = True - else: - shutil.copyfile( - os.path.join(repo_path, 'certbot-auto'), - os.path.join(temp_cwd, 'local-auto')) - shutil.copy(os.path.normpath(os.path.join( - repo_path, - 'letsencrypt-auto-source/pieces/fetch.py')), temp_cwd) - # Compare file against current version in the target branch - branch = os.environ.get('TRAVIS_BRANCH', 'master') - url = ( - 'https://raw.githubusercontent.com/certbot/certbot/{0}/certbot-auto' - .format(branch)) - urlretrieve(url, os.path.join(temp_cwd, 'certbot-auto')) +def sha256_hash(filename): + hash_object = hashlib.sha256() + with open(filename, 'rb') as f: + hash_object.update(f.read()) + return hash_object.hexdigest() - if compare_files( - os.path.join(temp_cwd, 'certbot-auto'), - os.path.join(temp_cwd, 'local-auto')): - print('Root *-auto were unchanged') - else: - # Compare file against the latest released version - latest_version = subprocess.check_output( - [sys.executable, 'fetch.py', '--latest-version'], cwd=temp_cwd) - subprocess.check_call( - [sys.executable, 'fetch.py', '--le-auto-script', - 'v{0}'.format(latest_version.decode().strip())], cwd=temp_cwd) - if compare_files( - os.path.join(temp_cwd, 'letsencrypt-auto'), - os.path.join(temp_cwd, 'local-auto')): - print('Root *-auto were updated to the latest version.') - else: - print('Root *-auto have unexpected changes.') - errors = True - - return errors def main(): - repo_path = find_repo_path() - temp_cwd = tempfile.mkdtemp() - errors = False - - try: - errors = validate_scripts_content(repo_path, temp_cwd) - - shutil.copyfile( - os.path.normpath(os.path.join(repo_path, 'letsencrypt-auto-source/letsencrypt-auto')), - os.path.join(temp_cwd, 'original-lea') - ) - subprocess.check_call([sys.executable, os.path.normpath(os.path.join( - repo_path, 'letsencrypt-auto-source/build.py'))]) - shutil.copyfile( - os.path.normpath(os.path.join(repo_path, 'letsencrypt-auto-source/letsencrypt-auto')), - os.path.join(temp_cwd, 'build-lea') - ) - shutil.copyfile( - os.path.join(temp_cwd, 'original-lea'), - os.path.normpath(os.path.join(repo_path, 'letsencrypt-auto-source/letsencrypt-auto')) - ) - - if not compare_files( - os.path.join(temp_cwd, 'original-lea'), - os.path.join(temp_cwd, 'build-lea')): - print('Script letsencrypt-auto-source/letsencrypt-auto ' - 'doesn\'t match output of build.py.') - errors = True - else: - print('Script letsencrypt-auto-source/letsencrypt-auto matches output of build.py.') - finally: - shutil.rmtree(temp_cwd) + repo_root = find_repo_root() + for filename, expected_hash in EXPECTED_FILES.items(): + filepath = os.path.join(repo_root, filename) + assert sha256_hash(filepath) == expected_hash, f'unexpected changes to {filepath}' + print('All certbot-auto files have correct hashes.') - return errors if __name__ == '__main__': - if main(): - sys.exit(1) + main() diff --git a/tests/pebble-fetch.sh b/tests/pebble-fetch.sh deleted file mode 100755 index b0ba08961d7..00000000000 --- a/tests/pebble-fetch.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash -# Download and run Pebble instance for integration testing -set -xe - -PEBBLE_VERSION=2018-11-02 - -# We reuse the same GOPATH-style directory than for Boulder. -# Pebble does not need it, but it will make the installation consistent with Boulder's one. -export GOPATH=${GOPATH:-$HOME/gopath} -PEBBLEPATH=${PEBBLEPATH:-$GOPATH/src/github.com/letsencrypt/pebble} - -mkdir -p ${PEBBLEPATH} - -cat << UNLIKELY_EOF > "$PEBBLEPATH/docker-compose.yml" -version: '3' - -services: - pebble: - image: letsencrypt/pebble:${PEBBLE_VERSION} - command: pebble -strict ${PEBBLE_STRICT:-false} -dnsserver 10.77.77.1 - ports: - - 14000:14000 - environment: - - PEBBLE_VA_NOSLEEP=1 -UNLIKELY_EOF - -docker-compose -f "$PEBBLEPATH/docker-compose.yml" up -d pebble - -set +x # reduce verbosity while waiting for boulder -for n in `seq 1 150` ; do - if curl -k https://localhost:14000/dir 2>/dev/null; then - break - else - sleep 1 - fi -done - -if ! curl -k https://localhost:14000/dir 2>/dev/null; then - echo "timed out waiting for pebble to start" - exit 1 -fi diff --git a/tests/run_http_server.py b/tests/run_http_server.py deleted file mode 100644 index 0e4f8ac79ef..00000000000 --- a/tests/run_http_server.py +++ /dev/null @@ -1,11 +0,0 @@ -import runpy -import sys - -# Run Python's built-in HTTP server -# Usage: python ./tests/run_http_server.py port_num -# NOTE: This script should be compatible with 2.7, 3.4+ - -# sys.argv (port number) is passed as-is to the HTTP server module -runpy.run_module( - 'http.server' if sys.version_info[0] == 3 else 'SimpleHTTPServer', - run_name='__main__') diff --git a/tests/tox-boulder-integration.sh b/tests/tox-boulder-integration.sh deleted file mode 100755 index 8c8a967fd49..00000000000 --- a/tests/tox-boulder-integration.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -e -# A simple wrapper around tests/boulder-integration.sh that activates the tox -# virtual environment defined by the environment variable TOXENV before running -# integration tests. - -if [ -z "${TOXENV+x}" ]; then - echo "The environment variable TOXENV must be set to use this script!" >&2 - exit 1 -fi - -source .tox/$TOXENV/bin/activate -tests/boulder-integration.sh diff --git a/tools/_changelog_top.txt b/tools/_changelog_top.txt deleted file mode 100644 index 6983b3a43a0..00000000000 --- a/tools/_changelog_top.txt +++ /dev/null @@ -1,21 +0,0 @@ -## nextversion - master - -### Added - -* - -### Changed - -* - -### Fixed - -* - -Despite us having broken lockstep, we are continuing to release new versions of -all Certbot components during releases for the time being, however, the only -package with changes other than its version number was: - -* - -More details about these changes can be found on our GitHub repo. diff --git a/tools/_release.sh b/tools/_release.sh index ec2deda22ee..82e15924e1a 100755 --- a/tools/_release.sh +++ b/tools/_release.sh @@ -7,30 +7,79 @@ if [ "$RELEASE_DIR" = "" ]; then exit 1 fi +ExitWarning() { + exit_status="$?" + if [ "$exit_status" != 0 ]; then + # Don't print each command before executing it because it will disrupt + # the desired output. + set +x + echo '******************************' + echo '* *' + echo '* THE RELEASE SCRIPT FAILED! *' + echo '* *' + echo '******************************' + set -x + fi + exit "$exit_status" +} + +trap ExitWarning EXIT + version="$1" echo Releasing production version "$version"... nextversion="$2" RELEASE_BRANCH="candidate-$version" -if [ "$RELEASE_OPENSSL_PUBKEY" = "" ] ; then - RELEASE_OPENSSL_PUBKEY="`realpath \`dirname $0\``/eff-pubkey.pem" +if [ -n "$VIRTUAL_ENV" ]; then + if [[ "$PATH" != $VIRTUAL_ENV* ]]; then + echo "Unexpected PATH and VIRTUAL_ENV value. Please deactivate any" + echo "Python virtual environments and try running this script again." + exit 1 + fi + echo "Deactivating venv..." + export PATH="${PATH#*:}" + hash -r 2> /dev/null + unset VIRTUAL_ENV +fi + +if [ "$(git branch --show-current)" != "$RELEASE_BRANCH" ]; then + echo "Creating $RELEASE_BRANCH branch..." + git switch -c "$RELEASE_BRANCH" +fi + +# If RELEASE_GPG_KEY isn't set, determine the key to use. +if [ "$RELEASE_GPG_KEY" = "" ]; then + TRUSTED_KEYS=" + BF6BCFC89E90747B9A680FD7B6029E8500F7DB16 + 86379B4F0AF371B50CD9E5FF3402831161D1D280 + 20F201346BF8F3F455A73F9A780CC99432A28621 + F2871B4152AE13C49519111F447BF683AA3B26C3 + " + for key in $TRUSTED_KEYS; do + if gpg --with-colons --card-status | grep -q "$key"; then + RELEASE_GPG_KEY="$key" + break + fi + done + if [ "$RELEASE_GPG_KEY" = "" ]; then + echo A trusted PGP key was not found on your PGP card. + exit 1 + fi fi -RELEASE_GPG_KEY=${RELEASE_GPG_KEY:-A2CFB51FA275A7286234E7B24D17C995CD9775F2} + # Needed to fix problems with git signatures and pinentry export GPG_TTY=$(tty) # port for a local Python Package Index (used in testing) PORT=${PORT:-1234} -# subpackages to be released (the way developers think about them) -SUBPKGS_IN_AUTO_NO_CERTBOT="acme certbot-apache certbot-nginx" -SUBPKGS_NOT_IN_AUTO="certbot-dns-cloudflare certbot-dns-cloudxns certbot-dns-digitalocean certbot-dns-dnsimple certbot-dns-dnsmadeeasy certbot-dns-gehirn certbot-dns-google certbot-dns-linode certbot-dns-luadns certbot-dns-nsone certbot-dns-ovh certbot-dns-rfc2136 certbot-dns-route53 certbot-dns-sakuracloud" - # subpackages to be released (the way the script thinks about them) -SUBPKGS_IN_AUTO="certbot $SUBPKGS_IN_AUTO_NO_CERTBOT" -SUBPKGS_NO_CERTBOT="$SUBPKGS_IN_AUTO_NO_CERTBOT $SUBPKGS_NOT_IN_AUTO" -SUBPKGS="$SUBPKGS_IN_AUTO $SUBPKGS_NOT_IN_AUTO" -subpkgs_modules="$(echo $SUBPKGS | sed s/-/_/g)" +SUBPKGS_NO_CERTBOT="acme certbot-apache certbot-nginx certbot-dns-cloudflare \ + certbot-dns-digitalocean certbot-dns-dnsimple certbot-dns-dnsmadeeasy \ + certbot-dns-gehirn certbot-dns-google certbot-dns-linode certbot-dns-luadns \ + certbot-dns-nsone certbot-dns-ovh certbot-dns-rfc2136 certbot-dns-route53 \ + certbot-dns-sakuracloud" +SUBPKGS="certbot $SUBPKGS_NO_CERTBOT" # certbot_compatibility_test is not packaged because: # - it is not meant to be used by anyone else than Certbot devs # - it causes problems when running pytest - the latter tries to @@ -38,50 +87,39 @@ subpkgs_modules="$(echo $SUBPKGS | sed s/-/_/g)" # there tag="v$version" -mv "dist.$version" "dist.$version.$(date +%s).bak" || true +built_package_dir="packages" +if [ -d "$built_package_dir" ]; then + echo "there shouldn't already be a $built_package_dir directory!" + echo "if it's not important, maybe delete it and try running the script again?" + exit 1 +fi git tag --delete "$tag" || true tmpvenv=$(mktemp -d) -virtualenv --no-site-packages -p python2 $tmpvenv +python3 -m venv "$tmpvenv" . $tmpvenv/bin/activate -# update setuptools/pip just like in other places in the repo -pip install -U setuptools -pip install -U pip # latest pip => no --pre for dev releases -pip install -U wheel # setup.py bdist_wheel - -# newer versions of virtualenv inherit setuptools/pip/wheel versions -# from current env when creating a child env -pip install -U virtualenv +# update packaging tools to their pinned versions +tools/pip_install.py build towncrier uv virtualenv root_without_le="$version.$$" root="$RELEASE_DIR/le.$root_without_le" +REPO_ROOT="$(pwd)" echo "Cloning into fresh copy at $root" # clean repo = no artifacts git clone . $root git rev-parse HEAD cd $root -if [ "$RELEASE_BRANCH" != "candidate-$version" ] ; then - git branch -f "$RELEASE_BRANCH" -fi git checkout "$RELEASE_BRANCH" -# Update changelog -sed -i "s/master/$(date +'%Y-%m-%d')/" CHANGELOG.md -git add CHANGELOG.md -git diff --cached +# Update changelog. `--yes` automatically clears out older newsfragments, +# and all of towncrier's changes are staged for commit when it's done +towncrier build --version "$version" --yes git commit -m "Update changelog for $version release" -for pkg_dir in $SUBPKGS_NO_CERTBOT certbot-compatibility-test . -do - sed -i 's/\.dev0//' "$pkg_dir/setup.py" - git add "$pkg_dir/setup.py" -done - - SetVersion() { ver="$1" # bumping Certbot's version number is done differently - for pkg_dir in $SUBPKGS_NO_CERTBOT certbot-compatibility-test + for pkg_dir in $SUBPKGS_NO_CERTBOT certbot-compatibility-test certbot-ci letstest do setup_file="$pkg_dir/setup.py" if [ $(grep -c '^version' "$setup_file") != 1 ]; then @@ -90,191 +128,98 @@ SetVersion() { fi sed -i "s/^version.*/version = '$ver'/" $pkg_dir/setup.py done - init_file="certbot/__init__.py" + init_file="certbot/src/certbot/__init__.py" if [ $(grep -c '^__version' "$init_file") != 1 ]; then echo "Unexpected count of __version variables in $init_file" exit 1 fi sed -i "s/^__version.*/__version__ = '$ver'/" "$init_file" - git add $SUBPKGS certbot-compatibility-test + git add $SUBPKGS certbot-compatibility-test certbot-ci letstest } SetVersion "$version" +# Unset CERTBOT_OLDEST to prevent wheels from being built improperly due to +# conditionals like the one found in certbot-dns-dnsimple's setup.py file. +unset CERTBOT_OLDEST echo "Preparing sdists and wheels" -for pkg_dir in . $SUBPKGS_NO_CERTBOT +for pkg_dir in $SUBPKGS do cd $pkg_dir - python setup.py clean rm -rf build dist - python setup.py sdist - python setup.py bdist_wheel - - echo "Signing ($pkg_dir)" - for x in dist/*.tar.gz dist/*.whl - do - gpg2 -u "$RELEASE_GPG_KEY" --detach-sign --armor --sign --digest-algo sha256 $x - done + # It's not strictly necessary, but using uv to install build dependencies speeds things up a + # little bit. + python -m build --installer uv cd - done - -mkdir "dist.$version" -mv dist "dist.$version/certbot" -for pkg_dir in $SUBPKGS_NO_CERTBOT +mkdir "$built_package_dir" +for pkg_dir in $SUBPKGS do - mv $pkg_dir/dist "dist.$version/$pkg_dir/" + mv "$pkg_dir"/dist/* "$built_package_dir" done -echo "Testing packages" -cd "dist.$version" -# start local PyPI -python -m SimpleHTTPServer $PORT & + +cd "$built_package_dir" +echo "Generating checksum file and signing it" +sha256sum *.tar.gz > SHA256SUMS +gpg -u "$RELEASE_GPG_KEY" --detach-sign --armor --sign --digest-algo sha256 SHA256SUMS +git add *.tar.gz SHA256SUMS* + +echo "Installing packages to generate documentation" # cd .. is NOT done on purpose: we make sure that all subpackages are -# installed from local PyPI rather than current directory (repo root) -virtualenv --no-site-packages ../venv +# installed from local archives rather than current directory (repo root) +VIRTUALENV_NO_DOWNLOAD=1 virtualenv ../venv . ../venv/bin/activate pip install -U setuptools pip install -U pip -# Now, use our local PyPI. Disable cache so we get the correct KGS even if we -# (or our dependencies) have conditional dependencies implemented with if -# statements in setup.py and we have cached wheels lying around that would -# cause those ifs to not be evaluated. + +# This creates a string like "acme==a.b.c certbot==a.b.c ..." which can be used +# with pip to ensure the correct versions of our packages installed. +subpkgs_with_version="" +for pkg in $SUBPKGS; do + subpkgs_with_version="$subpkgs_with_version $pkg==$version" +done + +# Now, use our local archives. Disable cache so we get the correct packages even if +# we (or our dependencies) have conditional dependencies implemented with if +# statements in setup.py and we have cached wheels lying around that would cause +# those ifs to not be evaluated. python ../tools/pip_install.py \ --no-cache-dir \ - --extra-index-url http://localhost:$PORT \ - $SUBPKGS -# stop local PyPI -kill $! + --find-links . \ + $subpkgs_with_version cd ~- # get a snapshot of the CLI help for the docs # We set CERTBOT_DOCS to use dummy values in example user-agent string. -CERTBOT_DOCS=1 certbot --help all > docs/cli-help.txt +CERTBOT_DOCS=1 certbot --help all > certbot/docs/cli-help.txt jws --help > acme/docs/jws-help.txt -cd .. -# freeze before installing anything else, so that we know end-user KGS -# make sure "twine upload" doesn't catch "kgs" -if [ -d kgs ] ; then - echo Deleting old kgs... - rm -rf kgs -fi -mkdir kgs -kgs="kgs/$version" -pip freeze | tee $kgs -python ../tools/pip_install.py pytest -for module in $subpkgs_modules ; do - echo testing $module - # use an empty configuration file rather than the one in the repo root - pytest -c <(echo '') --pyargs $module -done -cd ~- - -# pin pip hashes of the things we just built -for pkg in $SUBPKGS_IN_AUTO ; do - echo $pkg==$version \\ - pip hash dist."$version/$pkg"/*.{whl,gz} | grep "^--hash" | python2 -c 'from sys import stdin; input = stdin.read(); print " ", input.replace("\n--hash", " \\\n --hash"),' -done > letsencrypt-auto-source/pieces/certbot-requirements.txt deactivate -# there should be one requirement specifier and two hashes for each subpackage -expected_count=$(expr $(echo $SUBPKGS_IN_AUTO | wc -w) \* 3) -if ! wc -l letsencrypt-auto-source/pieces/certbot-requirements.txt | grep -qE "^\s*$expected_count " ; then - echo Unexpected pip hash output - exit 1 -fi - -# ensure we have the latest built version of leauto -letsencrypt-auto-source/build.py - -# and that it's signed correctly -tools/offline-sigrequest.sh || true -while ! openssl dgst -sha256 -verify $RELEASE_OPENSSL_PUBKEY -signature \ - letsencrypt-auto-source/letsencrypt-auto.sig \ - letsencrypt-auto-source/letsencrypt-auto ; do - echo "The signature on letsencrypt-auto is not correct." - read -p "Would you like this script to try and sign it again [Y/n]?" response - case $response in - [yY][eE][sS]|[yY]|"") - tools/offline-sigrequest.sh || true;; - *) - ;; - esac -done - -# This signature is not quite as strong, but easier for people to verify out of band -while ! gpg2 -u "$RELEASE_GPG_KEY" --detach-sign --armor --sign --digest-algo sha256 letsencrypt-auto-source/letsencrypt-auto; do - echo "Unable to sign letsencrypt-auto using $RELEASE_KEY." - echo "Make sure your OpenPGP card is in your computer if you are using one." - echo "You may need to take the card out and put it back in again." - read -p "Press enter to try signing again." -done -# We can't rename the openssl letsencrypt-auto.sig for compatibility reasons, -# but we can use the right name for certbot-auto.asc from day one -mv letsencrypt-auto-source/letsencrypt-auto.asc letsencrypt-auto-source/certbot-auto.asc - -# copy leauto to the root, overwriting the previous release version -cp -p letsencrypt-auto-source/letsencrypt-auto certbot-auto -cp -p letsencrypt-auto-source/letsencrypt-auto letsencrypt-auto -git add certbot-auto letsencrypt-auto letsencrypt-auto-source docs/cli-help.txt -git diff --cached +git add certbot/docs/cli-help.txt while ! git commit --gpg-sign="$RELEASE_GPG_KEY" -m "Release $version"; do echo "Unable to sign the release commit using git." - echo "You may have to configure git to use gpg2 by running:" - echo 'git config --global gpg.program $(command -v gpg2)' + echo "You may have to configure git to use gpg by running:" + echo 'git config --global gpg.program $(command -v gpg)' read -p "Press enter to try signing again." done git tag --local-user "$RELEASE_GPG_KEY" --sign --message "Release $version" "$tag" -cd .. -echo Now in $PWD -name=${root_without_le%.*} -ext="${root_without_le##*.}" -rev="$(git rev-parse --short HEAD)" -echo tar cJvf $name.$rev.tar.xz $name.$rev -echo gpg2 -U $RELEASE_GPG_KEY --detach-sign --armor $name.$rev.tar.xz -cd ~- - -# Add master section to CHANGELOG.md -header=$(head -n 4 CHANGELOG.md) -body=$(sed s/nextversion/$nextversion/ tools/_changelog_top.txt) -footer=$(tail -n +5 CHANGELOG.md) -echo "$header - -$body - -$footer" > CHANGELOG.md -git add CHANGELOG.md -git diff --cached -git commit -m "Add contents to CHANGELOG.md for next version" - -echo "New root: $root" -echo "Test commands (in the letstest repo):" -echo 'python multitester.py targets.yaml $AWS_KEY $USERNAME scripts/test_leauto_upgrades.sh --alt_pip $YOUR_PIP_REPO --branch public-beta' -echo 'python multitester.py targets.yaml $AWK_KEY $USERNAME scripts/test_letsencrypt_auto_certonly_standalone.sh --branch candidate-0.1.1' -echo 'python multitester.py --saveinstances targets.yaml $AWS_KEY $USERNAME scripts/test_apache2.sh' -echo "In order to upload packages run the following command:" -echo twine upload "$root/dist.$version/*/*" +git rm --cached -r "$built_package_dir" +git commit -m "Remove built packages from git" if [ "$RELEASE_BRANCH" = candidate-"$version" ] ; then SetVersion "$nextversion".dev0 - letsencrypt-auto-source/build.py - git add letsencrypt-auto-source/letsencrypt-auto - for pkg_dir in $SUBPKGS_NO_CERTBOT . - do - if [ -f "$pkg_dir/local-oldest-requirements.txt" ]; then - sed -i "s/-e acme\[dev\]/acme[dev]==$version/" "$pkg_dir/local-oldest-requirements.txt" - sed -i "s/-e acme/acme[dev]==$version/" "$pkg_dir/local-oldest-requirements.txt" - sed -i "s/-e \.\[dev\]/certbot[dev]==$version/" "$pkg_dir/local-oldest-requirements.txt" - sed -i "s/-e \./certbot[dev]==$version/" "$pkg_dir/local-oldest-requirements.txt" - git add "$pkg_dir/local-oldest-requirements.txt" - fi - done - git diff + # If this message changes, it should also be changed in tools/finish_release.py git commit -m "Bump version to $nextversion" fi + +cd $REPO_ROOT +git remote rm temp || : +git remote add temp $root && git fetch temp diff --git a/tools/_venv_common.py b/tools/_venv_common.py deleted file mode 100755 index 5408427734a..00000000000 --- a/tools/_venv_common.py +++ /dev/null @@ -1,184 +0,0 @@ -#!/usr/bin/env python -"""Aids in creating a developer virtual environment for Certbot. - -When this module is run as a script, it takes the arguments that should -be passed to pip to install the Certbot packages as command line -arguments. The virtual environment will be created with the name "venv" -in the current working directory and will use the default version of -Python for the virtualenv executable in your PATH. You can change the -name of the virtual environment by setting the environment variable -VENV_NAME. -""" - -from __future__ import print_function - -import os -import shutil -import glob -import time -import subprocess -import sys -import re -import shlex - -VERSION_PATTERN = re.compile(r'^(\d+)\.(\d+).*$') - - -class PythonExecutableNotFoundError(Exception): - pass - - -def find_python_executable(python_major): - # type: (int) -> str - """ - Find the relevant python executable that is of the given python major version. - Will test, in decreasing priority order: - * the current Python interpreter - * 'pythonX' executable in PATH (with X the given major version) if available - * 'python' executable in PATH if available - * Windows Python launcher 'py' executable in PATH if available - Incompatible python versions for Certbot will be evicted (eg. Python < 3.5 on Windows) - :param int python_major: the Python major version to target (2 or 3) - :rtype: str - :return: the relevant python executable path - :raise RuntimeError: if no relevant python executable path could be found - """ - python_executable_path = None - - # First try, current python executable - if _check_version('{0}.{1}.{2}'.format( - sys.version_info[0], sys.version_info[1], sys.version_info[2]), python_major): - return sys.executable - - # Second try, with python executables in path - versions_to_test = ['2.7', '2', ''] if python_major == 2 else ['3', ''] - for one_version in versions_to_test: - try: - one_python = 'python{0}'.format(one_version) - output = subprocess.check_output([one_python, '--version'], - universal_newlines=True, stderr=subprocess.STDOUT) - if _check_version(output.strip().split()[1], python_major): - return subprocess.check_output([one_python, '-c', - 'import sys; sys.stdout.write(sys.executable);'], - universal_newlines=True) - except (subprocess.CalledProcessError, OSError): - pass - - # Last try, with Windows Python launcher - try: - env_arg = '-{0}'.format(python_major) - output_version = subprocess.check_output(['py', env_arg, '--version'], - universal_newlines=True, stderr=subprocess.STDOUT) - if _check_version(output_version.strip().split()[1], python_major): - return subprocess.check_output(['py', env_arg, '-c', - 'import sys; sys.stdout.write(sys.executable);'], - universal_newlines=True) - except (subprocess.CalledProcessError, OSError): - pass - - if not python_executable_path: - raise RuntimeError('Error, no compatible Python {0} executable for Certbot could be found.' - .format(python_major)) - - -def _check_version(version_str, major_version): - search = VERSION_PATTERN.search(version_str) - - if not search: - return False - - version = (int(search.group(1)), int(search.group(2))) - - minimal_version_supported = (2, 7) - if major_version == 3 and os.name == 'nt': - minimal_version_supported = (3, 5) - elif major_version == 3: - minimal_version_supported = (3, 4) - - if version >= minimal_version_supported: - return True - - print('Incompatible python version for Certbot found: {0}'.format(version_str)) - return False - - -def subprocess_with_print(cmd, env=os.environ, shell=False): - print('+ {0}'.format(subprocess.list2cmdline(cmd)) if isinstance(cmd, list) else cmd) - subprocess.check_call(cmd, env=env, shell=shell) - - -def get_venv_bin_path(venv_path): - python_linux = os.path.join(venv_path, 'bin/python') - if os.path.isfile(python_linux): - return os.path.abspath(os.path.dirname(python_linux)) - python_windows = os.path.join(venv_path, 'Scripts\\python.exe') - if os.path.isfile(python_windows): - return os.path.abspath(os.path.dirname(python_windows)) - - raise ValueError(( - 'Error, could not find python executable in venv path {0}: is it a valid venv ?' - .format(venv_path))) - - -def main(venv_name, venv_args, args): - """Creates a virtual environment and installs packages. - - :param str venv_name: The name or path at where the virtual - environment should be created. - :param str venv_args: Command line arguments for virtualenv - :param str args: Command line arguments that should be given to pip - to install packages - """ - - for path in glob.glob('*.egg-info'): - if os.path.isdir(path): - shutil.rmtree(path) - else: - os.remove(path) - - env_venv_name = os.environ.get('VENV_NAME') - if env_venv_name: - print('Creating venv at {0}' - ' as specified in VENV_NAME'.format(env_venv_name)) - venv_name = env_venv_name - - if os.path.isdir(venv_name): - os.rename(venv_name, '{0}.{1}.bak'.format(venv_name, int(time.time()))) - - command = [sys.executable, '-m', 'virtualenv', '--no-site-packages', '--setuptools', venv_name] - command.extend(shlex.split(venv_args)) - subprocess_with_print(command) - - # We execute the following commands in the context of the virtual environment, to install - # the packages in it. To do so, we append the venv binary to the PATH that will be used for - # these commands. With this trick, correct python executable will be selected. - new_environ = os.environ.copy() - new_environ['PATH'] = os.pathsep.join([get_venv_bin_path(venv_name), new_environ['PATH']]) - subprocess_with_print('python {0}'.format('./letsencrypt-auto-source/pieces/pipstrap.py'), - env=new_environ, shell=True) - subprocess_with_print('python -m pip install --upgrade "setuptools>=30.3"', - env=new_environ, shell=True) - subprocess_with_print('python {0} {1}'.format('./tools/pip_install.py', ' '.join(args)), - env=new_environ, shell=True) - - if os.path.isdir(os.path.join(venv_name, 'bin')): - # Linux/OSX specific - print('-------------------------------------------------------------------') - print('Please run the following command to activate developer environment:') - print('source {0}/bin/activate'.format(venv_name)) - print('-------------------------------------------------------------------') - elif os.path.isdir(os.path.join(venv_name, 'Scripts')): - # Windows specific - print('---------------------------------------------------------------------------') - print('Please run one of the following commands to activate developer environment:') - print('{0}\\Scripts\\activate.bat (for Batch)'.format(venv_name)) - print('.\\{0}\\Scripts\\Activate.ps1 (for Powershell)'.format(venv_name)) - print('---------------------------------------------------------------------------') - else: - raise ValueError('Error, directory {0} is not a valid venv.'.format(venv_name)) - - -if __name__ == '__main__': - main('venv', - '', - sys.argv[1:]) diff --git a/tools/deactivate.py b/tools/deactivate.py deleted file mode 100644 index d43b845523f..00000000000 --- a/tools/deactivate.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -Given an ACME account key as input, deactivate the account. - -This can be useful if you created an account with a non-Certbot client and now -want to deactivate it. - -Private key should be in PKCS#8 PEM form. - -To provide the URL for the ACME server you want to use, set it in the $DIRECTORY -environment variable, e.g.: - -DIRECTORY=https://acme-staging.api.letsencrypt.org/directory python \ - deactivate.py private_key.pem -""" -import os -import sys - -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives.asymmetric import rsa -from cryptography.hazmat.primitives import serialization -import josepy as jose - -from acme import client as acme_client -from acme import errors as acme_errors -from acme import messages - -DIRECTORY = os.getenv('DIRECTORY', 'http://localhost:4000/directory') - -if len(sys.argv) != 2: - print("Usage: python deactivate.py private_key.pem") - sys.exit(1) - -data = open(sys.argv[1], "r").read() -key = jose.JWKRSA(key=serialization.load_pem_private_key( - data, None, default_backend())) - -net = acme_client.ClientNetwork(key, verify_ssl=False, - user_agent="acme account deactivator") - -client = acme_client.Client(DIRECTORY, key=key, net=net) -try: - # We expect this to fail and give us a Conflict response with a Location - # header pointing at the account's URL. - client.register() -except acme_errors.ConflictError as e: - location = e.location -if location is None: - raise "Key was not previously registered (but now is)." -client.deactivate_registration(messages.RegistrationResource(uri=location)) diff --git a/tools/deps.sh b/tools/deps.sh deleted file mode 100755 index e12f201a5eb..00000000000 --- a/tools/deps.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# -# Find all Python imports. -# -# ./tools/deps.sh certbot -# ./tools/deps.sh acme -# ./tools/deps.sh certbot-apache -# ... -# -# Manually compare the output with deps in setup.py. - -git grep -h -E '^(import|from.*import)' $1/ | \ - awk '{print $2}' | \ - grep -vE "^$1" | \ - sort -u diff --git a/tools/dev_constraints.txt b/tools/dev_constraints.txt deleted file mode 100644 index 111dc549586..00000000000 --- a/tools/dev_constraints.txt +++ /dev/null @@ -1,75 +0,0 @@ -# Specifies Python package versions for packages not specified in -# letsencrypt-auto's requirements file. -alabaster==0.7.10 -apipkg==1.4 -asn1crypto==0.22.0 -astroid==1.3.5 -attrs==17.3.0 -Babel==2.5.1 -backports.shutil-get-terminal-size==1.0.0 -boto3==1.9.36 -botocore==1.12.36 -cloudflare==1.5.1 -coverage==4.4.2 -decorator==4.1.2 -dns-lexicon==3.0.8 -dnspython==1.15.0 -docutils==0.12 -execnet==1.5.0 -future==0.16.0 -futures==3.1.1 -google-api-python-client==1.5 -httplib2==0.10.3 -imagesize==0.7.1 -ipdb==0.10.2 -ipython==5.5.0 -ipython-genutils==0.2.0 -Jinja2==2.9.6 -jmespath==0.9.3 -josepy==1.1.0 -logger==1.4 -logilab-common==1.4.1 -MarkupSafe==1.0 -mypy==0.600 -ndg-httpsclient==0.3.2 -oauth2client==2.0.0 -pathlib2==2.3.0 -pexpect==4.2.1 -pickleshare==0.7.4 -pkginfo==1.4.2 -pluggy==0.5.2 -prompt-toolkit==1.0.15 -ptyprocess==0.5.2 -py==1.4.34 -pyasn1==0.1.9 -pyasn1-modules==0.0.10 -Pygments==2.2.0 -pylint==1.4.2 -pytest==3.2.5 -pytest-cov==2.5.1 -pytest-forked==0.2 -pytest-xdist==1.22.5 -python-dateutil==2.6.1 -python-digitalocean==1.11 -PyYAML==3.13 -repoze.sphinx.autointerface==0.8 -requests-file==1.4.2 -requests-toolbelt==0.8.0 -rsa==3.4.2 -s3transfer==0.1.11 -scandir==1.6 -simplegeneric==0.8.1 -snowballstemmer==1.2.1 -Sphinx==1.7.5 -sphinx-rtd-theme==0.2.4 -sphinxcontrib-websupport==1.0.1 -tldextract==2.2.0 -tox==2.9.1 -tqdm==4.19.4 -traitlets==4.3.2 -twine==1.11.0 -typed-ast==1.1.0 -typing==3.6.4 -uritemplate==0.6 -virtualenv==15.1.0 -wcwidth==0.1.7 diff --git a/tools/docker-warning.sh b/tools/docker-warning.sh deleted file mode 100755 index e4f5f40ee65..00000000000 --- a/tools/docker-warning.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -e -echo "Warning: This Docker image will soon be switching to Alpine Linux." >&2 -echo "You can switch now using the certbot/certbot repo on Docker Hub." >&2 -exec /opt/certbot/venv/bin/certbot $@ diff --git a/tools/docker/Dockerfile b/tools/docker/Dockerfile new file mode 100644 index 00000000000..67350a0d225 --- /dev/null +++ b/tools/docker/Dockerfile @@ -0,0 +1,56 @@ +#base image +FROM python:3.14-alpine3.23 AS certbot + +ENTRYPOINT [ "certbot" ] +EXPOSE 80 443 +VOLUME /etc/letsencrypt /var/lib/letsencrypt +WORKDIR /opt/certbot + +# Copy certbot code +COPY CHANGELOG.md README.rst src/ +COPY tools tools +COPY acme src/acme +COPY certbot src/certbot + +# Install certbot runtime dependencies +RUN apk add --no-cache --virtual .certbot-deps \ + libffi \ + libssl3 \ + openssl \ + ca-certificates \ + binutils + +# We set this environment variable and install git while building to try and +# increase the stability of fetching the rust crates needed to build the +# cryptography library +ARG CARGO_NET_GIT_FETCH_WITH_CLI=true +# Install certbot from sources +# +# For some reason, setting the CARGO_LOG and CARGO_TERM_VERBOSE environment +# variables and -v/--verbose flags on pip seems to help cryptography builds not +# hang when building Docker images for other architectures using QEMU. See +# https://github.com/certbot/certbot/issues/10020. This may hopefully also help +# us to get more information about the problem to aid further debugging. +RUN apk add --no-cache --virtual .build-deps \ + gcc \ + linux-headers \ + openssl-dev \ + musl-dev \ + libffi-dev \ + python3-dev \ + cargo \ + git \ + pkgconfig \ + && CARGO_LOG=trace CARGO_TERM_VERBOSE=true python tools/pip_install.py \ + --no-cache-dir -vvv \ + --editable src/acme \ + --editable src/certbot \ + && apk del .build-deps \ + && rm -rf ${HOME}/.cargo + +#static definition for making a plugin, but beware that +#using this layer definition will cause collisions if you make +#extensive use of the cache. +FROM certbot AS certbot-plugin +COPY --from=plugin-src . /opt/certbot/src/plugin +RUN python tools/pip_install.py --no-cache-dir --editable /opt/certbot/src/plugin diff --git a/tools/docker/README.md b/tools/docker/README.md new file mode 100644 index 00000000000..a081472aff8 --- /dev/null +++ b/tools/docker/README.md @@ -0,0 +1,49 @@ +Running Certbot in Docker +========================= + +Docker is an amazingly simple and quick way to obtain a certificate. However, this mode of operation is unable to install certificates automatically or configure your webserver, because our installer plugins cannot reach your webserver from inside the Docker container. + +**Most users should install Certbot by following the installation instructions at https://certbot.eff.org/instructions. You should only use Docker if you are sure you know what you are doing (you understand [volumes](https://docs.docker.com/storage/volumes/)) and have a good reason to do so, such as following the [one service per container rule](https://docs.docker.com/config/containers/multi-service_container/).** + +For more information, please read [Certbot - Alternative 1: Docker](https://eff-certbot.readthedocs.io/en/latest/install.html#alternative-1-docker). + + +Certbot Docker Tools +====================== + +Goal +---- + +This code is used to build and deploy new versions of the Certbot and Certbot +DNS plugin Docker images to Docker Hub. + +High-level behavior +------------------- + +Running `./build.sh all` causes the Docker images to be built for all +supported architectures. The generated images are stored in the local docker image cache. + +Running `./test.sh all` loads images from the docker image cache +and runs a test command to validate the image contents. + +Running `./deploy_images.sh all` will push the previously generated images +to Docker Hub. The `` argument is an identifier applied to all docker +images and manifests. It may be something like `nightly` or `v2.3.2`. If +the tag is a version stamp greater than `v2.0.0`, then a `latest` tag will +also be generated and pushed to the docker hub repo. + +Running `./deploy_manifests.sh all` will add multiarch manifests to +Docker Hub. This command assumes that `./deploy_images.sh all` has +been previously run with the same tag. + +Configuration +------------- + +To run these scripts you need: + +1. A computer with Docker installed and the Docker daemon running. You probably +don't want to use the docker snap as these scripts have failed when using that +in the past. +2. To be logged into Docker Hub with an account able to push to the Certbot and +Certbot DNS Docker images on Docker Hub. Altering the value of `DOCKER_HUB_ORG` +in `lib/common` will allow you to push to your own account for testing. diff --git a/tools/docker/build.sh b/tools/docker/build.sh new file mode 100755 index 00000000000..9182f43b336 --- /dev/null +++ b/tools/docker/build.sh @@ -0,0 +1,56 @@ +#!/bin/bash +set -euxo pipefail + +# This script builds docker images for certbot and each dns plugin from the +# local Certbot source files. Results are stored in the docker image cache + +# Usage: +# ./build.sh all +# ./build.sh +# The argument is used to identify the code version (e.g v2.3.1) or type of build +# (e.g. nightly). This will be used when saving images to the docker image cache. +# The argument "all" will build all known architectures. Alternatively, the +# user may provide a comma separated list of architectures drawn from the +# known architectures. Known architectures include amd64, arm32v6, and arm64v8. + +source "$(realpath "$(dirname "${BASH_SOURCE[0]}")")/lib/common" + +ParseArgs "$@" + +#jump to root, matching popd handed by Cleanup on EXIT via trap +pushd "${REPO_ROOT}" + +# Set trap here, as the popd won't work as expected if invoked prior to pushd +trap Cleanup EXIT +# Create the builder +CreateBuilder +InstallMultiarchSupport + + +BuildAndLoadByArch() { + TAG_ARCH=$1 + docker buildx build --target certbot --builder certbot_builder \ + --platform "$(arch2platform "$TAG_ARCH")" \ + -f "${WORK_DIR}/Dockerfile" \ + -t "${DOCKER_HUB_ORG}/certbot:${TAG_ARCH}-${TAG_VER}" \ + --load \ + . + for plugin in "${CERTBOT_PLUGINS[@]}"; do + docker buildx build --target certbot-plugin --builder certbot_builder \ + --platform "$(arch2platform "$TAG_ARCH")" \ + --build-context plugin-src="${REPO_ROOT}/certbot-${plugin}" \ + -f "${WORK_DIR}/Dockerfile" \ + -t "${DOCKER_HUB_ORG}/${plugin}:${TAG_ARCH}-${TAG_VER}" \ + --load \ + . + done +} + +# In principle, there is a better way to do with by using `docker buildx bake` +# instead of a for-loop. However, issues have been found in the results +# of such a build. See the branch buildx-bake and +# https://github.com/certbot/certbot/issues/9587. + +for ARCH in "${REQUESTED_ARCH_ARRAY[@]}"; do + BuildAndLoadByArch "$ARCH" +done \ No newline at end of file diff --git a/tools/docker/deploy_images.sh b/tools/docker/deploy_images.sh new file mode 100755 index 00000000000..d8766539dc9 --- /dev/null +++ b/tools/docker/deploy_images.sh @@ -0,0 +1,46 @@ +#!/bin/bash +set -euxo pipefail + +# This script takes docker images in the local docker cache and pushes them to +# Docker Hub. + +# Usage: +# ./deploy_images.sh all +# ./deploy_images.sh +# The argument is an identifier applied to all docker images and manifests. +# It may be something like `nightly` or `v2.3.2`. If the tag is a version +# stamp greater than v2.0.0, then a `latest` tag will also be generated and +# pushed to the docker hub repo. +# The argument "all" will push all known architectures. Alternatively, the +# user may provide a comma separated list of architectures drawn from the +# known architectures. Known architectures include amd64, arm32v6, and arm64v8. + +source "$(realpath $(dirname "${BASH_SOURCE[0]}"))/lib/common" + +ParseArgs "$@" + +#jump to root, matching popd handed by Cleanup on EXIT via trap +pushd "${REPO_ROOT}" + +# Set trap here, as the popd won't work as expected if invoked prior to pushd +trap popd EXIT + +REGISTRY_SPEC="${DOCKER_HUB_ORG}/" + +DeployImage() { + IMAGE_NAME=$1 + TAG_ARCH=$2 + docker push "${REGISTRY_SPEC}${IMAGE_NAME}:${TAG_ARCH}-${TAG_VER}" + if [[ "${TAG_VER}" =~ ^v([2-9]|[1-9][0-9]+)\.[0-9]+\.[0-9]+$ ]]; then + docker tag "${REGISTRY_SPEC}${IMAGE_NAME}:${TAG_ARCH}-${TAG_VER}" "${REGISTRY_SPEC}${IMAGE_NAME}:${TAG_ARCH}-latest" + docker push "${REGISTRY_SPEC}${IMAGE_NAME}:${TAG_ARCH}-latest" + fi +} + + +for TAG_ARCH in "${REQUESTED_ARCH_ARRAY[@]}"; do + DeployImage certbot "$TAG_ARCH" + for PLUGIN in "${CERTBOT_PLUGINS[@]}"; do + DeployImage "$PLUGIN" "$TAG_ARCH" + done +done \ No newline at end of file diff --git a/tools/docker/deploy_manifests.sh b/tools/docker/deploy_manifests.sh new file mode 100755 index 00000000000..87326753ed5 --- /dev/null +++ b/tools/docker/deploy_manifests.sh @@ -0,0 +1,48 @@ +#!/bin/bash +set -euxo pipefail + +# This script generates multi-arch manifests for images previously pushed to +# Docker Hub via deploy_images.sh + +# Usage: +# ./deploy_manifest.sh all +# ./deploy_manifest.sh +# The argument is an identifier applied to all docker images and manifests. +# It may be something like `nightly` or `v2.3.2`. If the tag is a version +# stamp greater than v2.0.0, then a `latest` tag will also be generated and +# pushed to the docker hub repo. +# The argument "all" will push all know architectures. Alternatively, the +# user may provide a comma separated list of architectures drawn from the +# known architectures. Know architectures include amd64, arm32v6, and arm64v8. + + +source "$(realpath $(dirname ${BASH_SOURCE[0]}))/lib/common" + +ParseArgs "$@" + +#jump to root, matching popd handed by Cleanup on EXIT via trap +pushd "${REPO_ROOT}" + +# Set trap here, as the popd won't work as expected if invoked prior to pushd +trap popd EXIT + +REGISTRY_SPEC="${DOCKER_HUB_ORG}/" + +DeployManifest() { + IMAGE_NAME=$1 + + SRC_IMAGES="" + for TAG_ARCH in "${REQUESTED_ARCH_ARRAY[@]}"; do + SRC_IMAGES+="${REGISTRY_SPEC}${IMAGE_NAME}:${TAG_ARCH}-${TAG_VER} " + done + docker buildx imagetools create -t "${REGISTRY_SPEC}${IMAGE_NAME}:${TAG_VER}" $SRC_IMAGES + + if [[ "${TAG_VER}" =~ ^v([2-9]|[1-9][0-9]+)\.[0-9]+\.[0-9]+$ ]]; then + docker buildx imagetools create -t "${REGISTRY_SPEC}${IMAGE_NAME}:latest" $SRC_IMAGES + fi +} + +DeployManifest certbot +for PLUGIN in "${CERTBOT_PLUGINS[@]}"; do + DeployManifest "$PLUGIN" +done \ No newline at end of file diff --git a/tools/docker/lib/common b/tools/docker/lib/common new file mode 100644 index 00000000000..9620360d79c --- /dev/null +++ b/tools/docker/lib/common @@ -0,0 +1,116 @@ +#!/bin/bash +set -euxo pipefail + +# Current supported architectures +export ALL_TARGET_ARCH=(amd64 arm32v6 arm64v8) + +# Name of the Certbot Docker organizaation on GitHub. After creating +# repositories with the same names (e.g. "certbot", "dns-dnsmadeeasy", etc.) +# using a different account on Docker Hub, you can change this value to have +# the scripts modify those Docker repositories rather than the repositories for +# the official Certbot Docker images. +export DOCKER_HUB_ORG="certbot" + +# List of Certbot plugins +export CERTBOT_PLUGINS=( + "dns-dnsmadeeasy" + "dns-dnsimple" + "dns-ovh" + "dns-cloudflare" + "dns-digitalocean" + "dns-google" + "dns-luadns" + "dns-nsone" + "dns-rfc2136" + "dns-route53" + "dns-gehirn" + "dns-linode" + "dns-sakuracloud" +) + +# WORK_DIR is two levels above this file +export WORK_DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")/..")" +# REPO_ROOT is two levels above that +export REPO_ROOT="$(realpath "${WORK_DIR}/../..")" + +# Converts input architecture identifier to the platform specification +# understood by `docker build buildx --platform `. +# Usage: arch2platform [arm64|arm32v6|arm64v8] +# If the input is not recognized, an error is returned +arch2platform() { + REQUESTED_ARCH="${1}" + case $REQUESTED_ARCH in + amd64) + echo "linux/amd64" + ;; + arm32v6) + echo "linux/arm/v6" + ;; + arm64v8) + echo "linux/arm64" + ;; + *) + return 1 + ;; + esac +} + +ParseArgs() { + export TAG_VER="$1" + if [ -z "$TAG_VER" ]; then + echo "We cannot tag Docker images with an empty string!" >&2 + exit 1 + fi + ARCH_LIST="$2" + if [ -z "$ARCH_LIST" ]; then + echo "Architectures must be specified!" >&2 + exit 1 + fi + + local IFS="," + # Handle the special value "all" + if [[ "${ARCH_LIST}" == "all" ]]; then + # Replace with comma separated + ARCH_LIST="${ALL_TARGET_ARCH[*]}" + fi + + # Turn arch list into an array + read -ra REQUESTED_ARCH_ARRAY <<< "$ARCH_LIST" + # And make sure all individual elements are in the list of all known architectures + for REQUESTED_ARCH in "${REQUESTED_ARCH_ARRAY[@]}"; do + local IFS=" " + if [[ ! " ${ALL_TARGET_ARCH[*]} " =~ " ${REQUESTED_ARCH} " ]]; then + echo "unknown architecture identifier: ${REQUESTED_ARCH}" >&2 + exit 1 + fi + done + export REQUESTED_ARCH_ARRAY +} + + +# Function for use with trap in the primary scripts to remove the +# docker builder and restore the original directory +Cleanup() { + docker buildx rm certbot_builder || true + popd +} + +# add binfmt tools to the docker environment, with integration into the new builder instance +InstallMultiarchSupport() { + docker run --privileged --rm tonistiigi/binfmt --install all +} + +# Function to create a docker builder using the buildkit docker-container +# driver +CreateBuilder() { + # just in case the env is not perfectly clean, remove any old instance of the builder + docker buildx rm certbot_builder || true + # create the builder instance + # + # BUILDKIT_STEP_LOG_MAX_* environment variables are set to prevent docker + # from truncating build logs that can be useful during debugging. See + # https://github.com/docker/buildx/issues/484#issuecomment-749352728 + docker buildx create --name certbot_builder --driver docker-container \ + --driver-opt=network=host --driver-opt env.BUILDKIT_STEP_LOG_MAX_SIZE=-1 \ + --driver-opt env.BUILDKIT_STEP_LOG_MAX_SPEED=-1 --bootstrap +} diff --git a/tools/docker/test.sh b/tools/docker/test.sh new file mode 100755 index 00000000000..702d75b7125 --- /dev/null +++ b/tools/docker/test.sh @@ -0,0 +1,43 @@ +#!/bin/bash +set -euxo pipefail + +# This script tests certbot docker and certbot dns plugin images. + +# Usage: +# ./test.sh all +# ./test.sh +# The argument is used to identify the code version (e.g v2.3.1) or type of build +# (e.g. nightly). This will be used when saving images to the docker image cache. +# The argument "all" will build all know architectures. Alternatively, the +# user may provide a comma separated list of architectures drawn from the +# known architectures. Known architectures include amd64, arm32v6, and arm64v8. + +source "$(realpath $(dirname "${BASH_SOURCE[0]}"))/lib/common" + +ParseArgs "$@" + +#jump to root, matching popd handed by Cleanup on EXIT via trap +pushd "${REPO_ROOT}" + +# Set trap here, as the popd won't work as expected if invoked prior to pushd +trap popd EXIT + +InstallMultiarchSupport + + +REGISTRY_SPEC="${DOCKER_HUB_ORG}/" + +TestImage() { + IMAGE_NAME=$1 + TAG_ARCH=$2 + TAG_VER=$3 + docker run --rm "${REGISTRY_SPEC}${IMAGE_NAME}:${TAG_ARCH}-${TAG_VER}" plugins --prepare +} + + +for TAG_ARCH in "${REQUESTED_ARCH_ARRAY[@]}"; do + TestImage certbot "$TAG_ARCH" "$TAG_VER" + for PLUGIN in "${CERTBOT_PLUGINS[@]}"; do + TestImage "$PLUGIN" "$TAG_ARCH" "$TAG_VER" + done +done \ No newline at end of file diff --git a/tools/eff-pubkey.pem b/tools/eff-pubkey.pem deleted file mode 100644 index fe6c2f5bb7d..00000000000 --- a/tools/eff-pubkey.pem +++ /dev/null @@ -1,9 +0,0 @@ ------BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6MR8W/galdxnpGqBsYbq -OzQb2eyW15YFjDDEMI0ZOzt8f504obNs920lDnpPD2/KqgsfjOgw2K7xWDJIj/18 -xUvWPk3LDkrnokNiRkA3KOx3W6fHycKL+zID7zy+xZYBuh2fLyQtWV1VGQ45iNRp -9+Zo7rH86cdfgkdnWTlNSHyTLW9NbXvyv/E12bppPcEvgCTAQXgnDVJ0/sqmeiij -n9tTFh03aM+R2V/21h8aTraAS24qiPCz6gkmYGC8yr6mglcnNoYbsLNYZ69zF1XH -cXPduCPdPdfLlzVlKK1/U7hkA28eG3BIAMh6uJYBRJTpiGgaGdPd7YekUB8S6cy+ -CQIDAQAB ------END PUBLIC KEY----- diff --git a/tools/extract_changelog.py b/tools/extract_changelog.py new file mode 100755 index 00000000000..e27ff7246ac --- /dev/null +++ b/tools/extract_changelog.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +from __future__ import print_function + +import os +import re +import sys + +CERTBOT_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + +NEW_SECTION_PATTERN = re.compile(r'^##\s*[\d.]+\s*-\s*[\d-]+$') + + +def main(): + version = sys.argv[1] + if version.endswith('.dev0'): + version = version[:-5] + + section_pattern = re.compile(r'^##\s*{0}\s*-\s*.*$' + .format(version.replace('.', '\\.'))) + + with open(os.path.join(CERTBOT_ROOT, 'certbot', 'CHANGELOG.md')) as file_h: + lines = file_h.read().splitlines() + + changelog = [] + + i = 0 + while i < len(lines): + if section_pattern.match(lines[i]): + i = i + 2 + while i < len(lines): + if NEW_SECTION_PATTERN.match(lines[i]): + break + changelog.append(lines[i]) + i = i + 1 + i = i + 1 + + print('\n'.join(changelog)) + + +if __name__ == '__main__': + main() diff --git a/tools/finish_release.py b/tools/finish_release.py new file mode 100755 index 00000000000..23858e4124e --- /dev/null +++ b/tools/finish_release.py @@ -0,0 +1,370 @@ +#!/usr/bin/env python +""" +Post-release script to publish artifacts created from GitHub Actions, synchronize the repo + on Github, and print a formatted changelog for the latest release. + +This currently includes: + +* Moving snaps from the beta channel to the stable channel +* Pushing the candidate branch to GitHub +* Creating a minor release branch if it's not a point release +* Opening PR(s) to merge the release changes back into `main` and/or the minor release branch +* Printing a formmated changelog for the latest release for distribution + +Setup: + - Install the `snapcraft` command line tool and log in to a privileged account. + - https://snapcraft.io/docs/installing-snapcraft + - Use the command `snapcraft login` to log in. + - Install the `gh` command line tool and log in to a privileged account. + - https://github.com/cli/cli#installation + - Use the command `gh auth login` to log in. + +Run: + +python tools/finish_release.py [--skip-snaps] [--skip-github-sync] [--test version ] + +Testing: + +This script can be safely run between releases. When this is done, the script +should execute successfully, if it has not run before. Promoting snaps from beta +and printing the changelog are idempotent, but synchronizing the github repo is not. + +To skip syncing with github, use the `--skip-github-sync` flag. +To skip promoting snaps, use the `--skip-snaps` flag. +To test with a version other than the latest release, use the `--test-version ` flag + with a version number formatted as . + +""" + +import argparse +import glob +import os.path +import re +import subprocess +import sys + +# Path to the root directory of the Certbot repository containing this script +REPO_ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +# This list contains the names of all Certbot DNS plugins. We used to have a +# CloudXNS plugin and since it's possible devs still have that directory +# locally, we filter it out here. If it's included in this list, this script +# will crash later when it fails to find a CloudXNS snap on the snap store with +# the current version since we no longer build it. +PLUGIN_SNAPS = [os.path.basename(path) + for path in glob.glob(os.path.join(REPO_ROOT, 'certbot-dns-*')) + if not path.endswith('certbot-dns-cloudxns')] +# This list contains the name of all Certbot snaps that should be published to +# the stable channel. +ALL_SNAPS = ['certbot'] + PLUGIN_SNAPS +# This is the count of the architectures currently supported by our snaps used +# for sanity checking. +SNAP_ARCH_COUNT = 3 +SKIP_SYNC_MESSAGE = ('To skip pushing updated branches to GitHub and creating PRs, ' + 'run this script with the `--skip-github-sync` flag.') + + +def parse_args(args): + """Parse command line arguments. + + :param args: command line arguments with the program name removed. This is + usually taken from sys.argv[1:]. + :type args: `list` of `str` + + :returns: parsed arguments + :rtype: argparse.Namespace + + """ + # Use the file's docstring for the help text and don't let argparse reformat it. + parser = argparse.ArgumentParser(description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('--test-version', type=str, default=None, + help='version in the form of 1.2.3, mainly for testing') + parser.add_argument('--skip-snaps', action='store_true', + help='don\'t promote snaps; used for testing') + parser.add_argument('--skip-github-sync', action='store_true', + help='don\'t synchronize branches to GitHub or create PRs') + return parser.parse_args(args) + + +def assert_logged_into_snapcraft(): + """Confirms that snapcraft is logged in to an account. + + :raises SystemExit: if the command snapcraft is unavailable or it + isn't logged into an account + + """ + cmd = 'snapcraft whoami'.split() + try: + subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, universal_newlines=True) + except (subprocess.CalledProcessError, OSError): + print("Please make sure that the command line tool snapcraft is") + print("installed and that you have logged in to an account by running") + print("'snapcraft login'. If that fails, your credentials may have expired") + print("and you should run `snapcraft logout` followed by 'snapcraft login'.") + sys.exit(1) + + +def get_snap_revisions(snap, channel, version): + """Finds the revisions for the snap and version in the given channel. + + If you call this function without being logged in with snapcraft, it + will hang with no output. + + :param str snap: the name of the snap on the snap store + :param str channel: snap channel to pull revisions from + :param str version: snap version number, e.g. 1.7.0 + + :returns: list of revision numbers + :rtype: `list` of `str` + + :raises subprocess.CalledProcessError: if the snapcraft command + fails + + :raises AssertionError: if the expected snaps are not found + + """ + print('Getting revision numbers for', snap, version) + cmd = ['snapcraft', 'status', snap] + process = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, universal_newlines=True) + pattern = f'\\s+{channel}\\s+{version}\\s+(\\d+)\\s*' + revisions = re.findall(pattern, process.stdout, re.MULTILINE) + assert len(revisions) == SNAP_ARCH_COUNT, f'Unexpected number of snaps found for {channel} {snap} {version} (expected {SNAP_ARCH_COUNT}, found {len(revisions)})' + return revisions + + +def promote_snaps(snaps, source_channel, version, progressive_percentage=None): + """Promotes the given snaps from source_channel to the stable channel. + + If the snaps have already been released to the stable channel, this + function will try to release them again which has no effect. + + :param snaps: snap package names to be promoted + :type snaps: `list` of `str` + :param str source_channel: snap channel to promote from + :param str version: the version number that should be found in the + candidate channel, e.g. 1.7.0 + :param progressive_percentage: specifies the percentage of a progressive + deployment + :type progressive_percentage: int or None + + :raises SystemExit: if the command snapcraft is unavailable or it + isn't logged into an account + + :raises subprocess.CalledProcessError: if a snapcraft command fails + for another reason + + """ + assert_logged_into_snapcraft() + for snap in snaps: + revisions = get_snap_revisions(snap, source_channel, version) + # The loop below is kind of slow, so let's print some output about what + # it is doing. + print('Releasing', snap, 'snaps to the stable channel') + for revision in revisions: + cmd = ['snapcraft', 'release', snap, revision, 'stable'] + if progressive_percentage: + cmd.extend(f'--progressive {progressive_percentage}'.split()) + try: + subprocess.run(cmd, check=True, stdout=subprocess.PIPE, universal_newlines=True) + except subprocess.CalledProcessError as e: + print("The command", f"'{' '.join(cmd)}'", "failed.") + print("The output printed to stdout was:") + print(e.stdout) + raise + +def fetch_version_number(): + """Retrieve latest release version number from GitHub + + :returns: version number + + """ + jq_arg = '.[] | select(.isLatest)|.name' + cmd = ['gh', 'release', 'list', '--json', 'name,isLatest', '--jq', jq_arg] + try: + process = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, universal_newlines=True) + except (subprocess.CalledProcessError, OSError): + print("Getting version number from GitHub release failed.") + sys.exit(1) + + name = process.stdout.rstrip().split(' ') + assert len(name) == 2 + version = name[-1] + assert len(version.split('.')) == 3 + return version + + +def _run_silent_except_error(cmd: list[str], message: str = None) -> subprocess.CompletedProcess: + # For some reason, git prints a bunch of non-error output to stderr. Let's keep this script + # quiet by capturing it and only printing it if we hit an error. + try: + process = subprocess.run(cmd, check=True, universal_newlines=True, capture_output=True) + except subprocess.CalledProcessError as e: + print(f'Error running `{" ".join(cmd)}`') + if message is not None: + print(message) + print(e.output) + print(e.stderr) + print(SKIP_SYNC_MESSAGE) + raise e + else: + return process + + +def _create_pr(title: str, body: str, description: str, other_opts: list[str] | None = None) -> str: + cmd = ['gh', 'pr', 'create', '--title', title, '--body', body] + if other_opts is not None: + cmd = cmd + other_opts + try: + proc = subprocess.run(cmd, check=True, universal_newlines=True, capture_output=True) + except subprocess.CalledProcessError as e: + if 'already exists' in e.stderr: + print(f'{description} already exists...skipping creation. ' + 'To create a new PR, delete the old one on GitHub.') + # The error message looks like: + # a pull request for branch "candidate-4.24.0" into branch "main" already exists: + # https://github.com/certbot/certbot/pull/10698 + last_e_word = e.stderr.split()[-1] + if 'https' in last_e_word: + output = last_e_word + else: + print(e.stderr) + print(SKIP_SYNC_MESSAGE) + raise e + else: + output = proc.stdout.rstrip() + return output + + +def _sync_candidate_from_temp_to_origin(version: str) -> None: + command_str = f'git pull temp candidate-{version}' + message = ('To run successfully, stash any changes you\'ve made to this branch. ' + 'Do not attempt to merge and continue, as that will fail.') + _run_silent_except_error(command_str.split(), message) + command_str = f'git push origin candidate-{version}' + message = ('To delete the branch on GitHub, run ' + f'`git push origin --delete candidate-{version}`.') + _run_silent_except_error(command_str.split(), message) + + +def _create_release_pr_to_main(version: str) -> None: + print(f'Creating PR to merge candidate-{version} into main...') + title = f'update files from {version} release' + body = 'this PR only needs 1 review and should be merged, not squashed' + result = _create_pr(title, body, 'PR to merge release changes into main') + print(f'PR location: {result}') + + +def _create_release_pr_to_minor_branch( + version: str, + branch_name:str, + point_x_branch_name: str) -> None: + print(f'Creating PR to merge {branch_name} into {point_x_branch_name}...') + title = f'update files from {version} release' + body = 'this PR only needs 1 review and should be merged, not squashed' + pr_opts = [ '--head', branch_name, + '--base', point_x_branch_name] + result = _create_pr(title, body, 'PR to merge release changes into .x branch', pr_opts) + print(f'PR location: {result}') + + +def _create_and_push_branch_without_version_bump(version: str, branch_name: str) -> None: + # Usually a branch of form 1.2.x + # When it's a point release, it'll be any name, and then merged back into 1.2.x + print(f'Creating branch without version bump commit named {branch_name}...') + # Check if there are uncommited changes, since reset will blow them away + + try: + msg = (f'Branch {branch_name} already exists. Delete it using ' + f'`git branch -D {branch_name}`.') + _run_silent_except_error(f'git branch {branch_name}'.split(), msg) + + _run_silent_except_error(f'git switch {branch_name}'.split()) + + # Make sure the last commit message is 'Bump version to {next version}' + output = _run_silent_except_error('git log -1 --pretty=%B'.split()).stdout + assert_msg = 'The most recent commit message should start with "Bump version to"' + assert output.startswith('Bump version to'), assert_msg + + _run_silent_except_error('git reset --hard HEAD~1'.split()) + + msg = ('You shouldn\'t be trying to re-push a minor version branch. If you really want ' + 'to, go into GitHub, turn off branch deletion protection, and delete it there. ') + _run_silent_except_error(f'git push origin {branch_name}'.split(), msg) + finally: + # Switching to the current branch exits 0 + _run_silent_except_error(f'git switch candidate-{version}'.split()) + print('Created.') + + +def _check_branch_matches_version(version: str) -> None: + # This function assumes we're on a branch like `candidate-1.2.0` or `candidate-1.2.3` + # where the number after candidate should be equal to the version number + process = _run_silent_except_error('git branch --show'.split()) + current_branch = process.stdout.rstrip() + if current_branch != f'candidate-{version}': + print(f'Unexpected branch name found. The current branch should be candidate-{version}.') + print(SKIP_SYNC_MESSAGE) + sys.exit(1) + + +def synchronize_github_repo(version: str): + _check_branch_matches_version(version) + message = ('You have uncommitted changes that will be deleted. ' + 'Stash your changes before rerunning this script.') + _run_silent_except_error('git diff --quiet HEAD'.split(), message) + + _sync_candidate_from_temp_to_origin(version) + _create_release_pr_to_main(version) + + # Check the last element of the version number to see if this is a point release + point_version = version.split('.')[-1] + point_release = point_version != '0' + point_x_branch_name = '.'.join(version.split('.')[:-1]) + '.x' + if not point_release: + branch_name = point_x_branch_name + else: + branch_name = f'point-candidate-{version}' + + _create_and_push_branch_without_version_bump(version, branch_name) + + if point_release: + _create_release_pr_to_minor_branch(version, branch_name, point_x_branch_name) + + +def generate_community_forum_post(version: str): + print('Generating announcement text for community forum post') + + cmd = f"gh release view v{version} --json body -t {{{{.body}}}}".split() + + try: + process = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, universal_newlines=True) + except (subprocess.CalledProcessError, OSError): + print("Generating announcement text failed.") + sys.exit(1) + + changelog = process.stdout + + print('Subject:') + print() + print(f'Certbot {version} Release') + print() + print('Contents:') + print() + print(f'Certbot {version} has just been released. The changelog for the release is:') + print() + print(changelog) + +def main(args): + parsed_args = parse_args(args) + version = parsed_args.test_version + if not version: + version = fetch_version_number() + if not parsed_args.skip_snaps: + promote_snaps(ALL_SNAPS, 'beta', version) + if not parsed_args.skip_github_sync: + synchronize_github_repo(version) + generate_community_forum_post(version) + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/tools/half-sign.c b/tools/half-sign.c deleted file mode 100644 index e56bc397cf3..00000000000 --- a/tools/half-sign.c +++ /dev/null @@ -1,123 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -// This program can be used to perform RSA public key signatures given only -// the hash of the file to be signed as input. - -// To compile: -// gcc half-sign.c -lssl -lcrypto -o half-sign - -// Sign with SHA256 -#define HASH_SIZE 32 - -void usage() { - printf("half-sign [binary hash file]\n"); - printf("\n"); - printf(" Computes and prints a binary RSA signature over data given the SHA256 hash of\n"); - printf(" the data as input.\n"); - printf("\n"); - printf(" should be PEM encoded.\n"); - printf("\n"); - printf(" The input SHA256 hash should be %d bytes in length. If no binary hash file is\n", HASH_SIZE); - printf(" specified, it will be read from stdin.\n"); - exit(1); -} - -void sign_hashed_data(EVP_PKEY *signing_key, unsigned char *md, size_t mdlen) { - // cribbed from the openssl EVP_PKEY_sign man page - EVP_PKEY_CTX *ctx; - unsigned char *sig; - size_t siglen; - - /* NB: assumes signing_key, md and mdlen are already set up - * and that signing_key is an RSA private key - */ - ctx = EVP_PKEY_CTX_new(signing_key, NULL); - if ((!ctx) - || (EVP_PKEY_sign_init(ctx) <= 0) - || (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0) - || (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0)) { - fprintf(stderr, "Failure establishing ctx for signature\n"); - exit(1); - } - - /* Determine buffer length */ - if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0) { - fprintf(stderr, "Unable to determine buffer length for signature\n"); - exit(1); - } - - sig = OPENSSL_malloc(siglen); - - if (!sig) { - fprintf(stderr, "Malloc failed\n"); - exit(1); - } - - if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0) { - fprintf(stderr, "Signature error\n"); - exit(1); - } - - /* Signature is siglen bytes written to buffer sig */ - fwrite(sig, siglen, 1, stdout); -} - -EVP_PKEY *read_private_key(char *filename) { - FILE *keyfile; - EVP_PKEY *privkey; - keyfile = fopen(filename, "r"); - if (!keyfile) { - fprintf(stderr, "Failed to open private key.pem file %s\n", filename); - exit(1); - } - privkey = PEM_read_PrivateKey(keyfile, NULL, NULL, NULL); - if (!privkey) { - fprintf(stderr, "Failed to read PEM private key from %s\n", filename); - exit(1); - } - if (EVP_PKEY_type(privkey->type) != EVP_PKEY_RSA) { - fprintf(stderr, "%s was a non-RSA key\n", filename); - exit(1); - } - return privkey; -} - -int main(int argc, char *argv[]) { - FILE *input; - unsigned char *buffer; - int test; - EVP_PKEY *privkey; - if (argc > 3 || argc < 2) - usage(); - if (argc < 3 || strcmp(argv[2],"-") == 0) - input = stdin; - else { - input = fopen(argv[2], "r"); - if (!input) usage(); - } - privkey = read_private_key(argv[1]); - buffer = malloc(HASH_SIZE); - if (!buffer) { - fprintf(stderr, "Argh, malloc failed\n"); - exit(1); - } - if (fread(buffer, HASH_SIZE, 1, input) != 1) { - perror("half-sign: Failed to read SHA256 from input\n"); - exit(1); - } - - test = fgetc(input); - if (test != EOF && test != '\n') { - fprintf(stderr,"Error, more than %d bytes fed to half-sign\n", HASH_SIZE); - fprintf(stderr,"Last byte was :%d\n" , (int) test); - exit(1); - } - sign_hashed_data(privkey, buffer, HASH_SIZE); - return 0; -} diff --git a/tools/install_and_test.py b/tools/install_and_test.py deleted file mode 100755 index 79a7c2264c9..00000000000 --- a/tools/install_and_test.py +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env python -# pip installs the requested packages in editable mode and runs unit tests on -# them. Each package is installed and tested in the order they are provided -# before the script moves on to the next package. If CERTBOT_NO_PIN is set not -# set to 1, packages are installed using pinned versions of all of our -# dependencies. See pip_install.py for more information on the versions pinned -# to. -from __future__ import print_function - -import os -import sys -import tempfile -import shutil -import subprocess -import re - -SKIP_PROJECTS_ON_WINDOWS = [ - 'certbot-apache', 'certbot-nginx', 'certbot-postfix', 'letshelp-certbot'] - -def call_with_print(command, cwd=None): - print(command) - subprocess.check_call(command, shell=True, cwd=cwd or os.getcwd()) - -def main(args): - if os.environ.get('CERTBOT_NO_PIN') == '1': - command = [sys.executable, '-m', 'pip', '-e'] - else: - script_dir = os.path.dirname(os.path.abspath(__file__)) - command = [sys.executable, os.path.join(script_dir, 'pip_install_editable.py')] - - new_args = [] - for arg in args: - if os.name == 'nt' and arg in SKIP_PROJECTS_ON_WINDOWS: - print(( - 'Info: currently {0} is not supported on Windows and will not be tested.' - .format(arg))) - else: - new_args.append(arg) - - for requirement in new_args: - current_command = command[:] - current_command.append(requirement) - call_with_print(' '.join(current_command)) - pkg = re.sub(r'\[\w+\]', '', requirement) - - if pkg == '.': - pkg = 'certbot' - - temp_cwd = tempfile.mkdtemp() - shutil.copy2("pytest.ini", temp_cwd) - try: - call_with_print(' '.join([ - sys.executable, '-m', 'pytest', pkg.replace('-', '_')]), cwd=temp_cwd) - finally: - shutil.rmtree(temp_cwd) - -if __name__ == '__main__': - main(sys.argv[1:]) diff --git a/tools/merge_requirements.py b/tools/merge_requirements.py deleted file mode 100755 index ad44a55d016..00000000000 --- a/tools/merge_requirements.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python -"""Merges multiple Python requirements files into one file. - -Requirements files specified later take precedence over earlier ones. Only -simple SomeProject==1.2.3 format is currently supported. - -""" - -from __future__ import print_function - -import sys - -def read_file(file_path): - """Reads in a Python requirements file. - - :param str file_path: path to requirements file - - :returns: mapping from a project to its pinned version - :rtype: dict - - """ - d = {} - with open(file_path) as f: - for line in f: - line = line.strip() - if line and not line.startswith('#'): - project, version = line.split('==') - if not version: - raise ValueError("Unexpected syntax '{0}'".format(line)) - d[project] = version - return d - - -def output_requirements(requirements): - """Prepare print requirements to stdout. - - :param dict requirements: mapping from a project to its pinned version - - """ - return '\n'.join('{0}=={1}'.format(k, v) - for k, v in sorted(requirements.items())) - - -def main(*files): - """Merges multiple requirements files together and prints the result. - - Requirement files specified later in the list take precedence over earlier - files. - - :param tuple files: paths to requirements files - - """ - d = {} - for f in files: - d.update(read_file(f)) - return output_requirements(d) - - -if __name__ == '__main__': - merged_requirements = main(*sys.argv[1:]) - print(merged_requirements) diff --git a/tools/offline-sigrequest.sh b/tools/offline-sigrequest.sh deleted file mode 100755 index 6443ae8af6f..00000000000 --- a/tools/offline-sigrequest.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash - -set -o errexit - -function sayhash { # $1 <-- HASH ; $2 <---SIGFILEBALL - while read -p "Press Enter to read the hash aloud or type 'done': " INP && [ "$INP" = "" ] ; do - if ! `which festival > /dev/null` ; then - echo \`festival\` is not installed! - echo Please install it to read the hash aloud - else - cat $1 | (echo "(Parameter.set 'Duration_Stretch 1.8)"; \ - echo -n '(SayText "'; \ - sha256sum | cut -c1-64 | fold -1 | sed 's/^a$/alpha/; s/^b$/bravo/; s/^c$/charlie/; s/^d$/delta/; s/^e$/echo/; s/^f$/foxtrot/'; \ - echo '")' ) | festival - fi - done - - echo 'Paste in the data from the QR code, then type Ctrl-D:' - cat > $2 -} - -function offlinesign { # $1 <-- INPFILE ; $2 <---SIGFILE - echo HASH FOR SIGNING: - SIGFILEBALL="$2.lzma.base64" - #echo "(place the resulting raw binary signature in $SIGFILEBALL)" - sha256sum $1 - echo metahash for confirmation only $(sha256sum $1 |cut -d' ' -f1 | tr -d '\n' | sha256sum | cut -c1-6) ... - echo - sayhash $1 $SIGFILEBALL -} - -function oncesigned { # $1 <-- INPFILE ; $2 <--SIGFILE - SIGFILEBALL="$2.lzma.base64" - cat $SIGFILEBALL | tr -d '\r' | base64 -d | unlzma -c > $2 || exit 1 - if ! [ -f $2 ] ; then - echo "Failed to find $2"'!' - exit 1 - fi - - if file $2 | grep -qv " data" ; then - echo "WARNING WARNING $2 does not look like a binary signature:" - echo `file $2` - exit 1 - fi -} - -HERE=`dirname $0` -LEAUTO="`realpath $HERE`/../letsencrypt-auto-source/letsencrypt-auto" -SIGFILE="$LEAUTO".sig -offlinesign $LEAUTO $SIGFILE -oncesigned $LEAUTO $SIGFILE diff --git a/tools/oldest_constraints.txt b/tools/oldest_constraints.txt index 642af660e08..e293fe304ff 100644 --- a/tools/oldest_constraints.txt +++ b/tools/oldest_constraints.txt @@ -1,63 +1,107 @@ -# This file contains the oldest versions of our dependencies we say we require -# in our packages or versions we need to support to maintain compatibility with -# the versions included in the various Linux distros where we are packaged. - -# CentOS/RHEL 7 EPEL constraints -cffi==1.6.0 -chardet==2.2.1 -configobj==4.7.2 -ipaddress==1.0.16 -mock==1.0.1 -ndg-httpsclient==0.3.2 -ply==3.4 -pyasn1==0.1.9 -pycparser==2.14 -pyOpenSSL==0.13.1 -pyparsing==1.5.6 -pyRFC3339==1.0 -python-augeas==0.5.0 -six==1.9.0 -# setuptools 0.9.8 is the actual version packaged, but some other dependencies -# in this file require setuptools>=1.0 and there are no relevant changes for us -# between these versions. -setuptools==1.0.0 -urllib3==1.10.2 -zope.component==4.1.0 -zope.event==4.0.3 -zope.interface==4.0.5 - -# Debian Jessie Backports constraints -PyICU==1.8 -colorama==0.3.2 -enum34==1.0.3 -html5lib==0.999 -idna==2.0 -pbr==1.8.0 -pytz==2012rc0 - -# Our setup.py constraints -cloudflare==1.5.1 -cryptography==1.2.3 -google-api-python-client==1.5 -oauth2client==2.0 -parsedatetime==1.3 -pyparsing==1.5.5 -python-digitalocean==1.11 -requests[security]==2.6.0 - -# Ubuntu Xenial constraints -ConfigArgParse==0.10.0 -funcsigs==0.4 -zope.hookable==4.0.4 - -# Ubuntu Bionic constraints -# Formerly only lexicon==2.2.1 is available on Bionic. But we cannot put that here because some -# DNS plugins require higher versions. We put to the least minimal Lexicon version to ensure -# that Lexicon 2.x works with Certbot. -dns-lexicon==2.7.14 - -# Plugin constraints -# These aren't necessarily the oldest versions we need to support -# Tracking at https://github.com/certbot/certbot/issues/6473 -boto3==1.4.7 -botocore==1.7.41 +# This file was generated by tools/pinning/oldest/repin.sh and can be updated using +# that script. +annotated-types==0.8.0 ; python_version == "3.10" +anyio==4.14.2 ; python_version == "3.10" +apacheconfig==0.3.2 ; python_version == "3.10" +asn1crypto==0.24.0 ; python_version == "3.10" +ast-serialize==0.6.0 ; python_version == "3.10" +astroid==4.0.4 ; python_version == "3.10" +beautifulsoup4==4.15.0 ; python_version == "3.10" +boto3==1.20.34 ; python_version == "3.10" +botocore==1.23.34 ; python_version == "3.10" +cachetools==5.5.2 ; python_version == "3.10" +certifi==2026.7.22 ; python_version == "3.10" +cffi==2.0.0 ; python_version == "3.10" +chardet==3.0.4 ; python_version == "3.10" +cloudflare==4.0.0 ; python_version == "3.10" +colorama==0.4.6 ; (sys_platform == "win32" or platform_system == "Windows") and python_version == "3.10" +configargparse==1.5.3 ; python_version == "3.10" +configobj==5.0.6 ; python_version == "3.10" +coverage==7.15.3 ; python_version == "3.10" +cryptography==47.0.0 ; python_version == "3.10" +cython==0.29.37 ; python_version == "3.10" +dill==0.4.1 ; python_version == "3.10" +distlib==0.4.3 ; python_version == "3.10" +distro==1.7.0 ; python_version == "3.10" +dns-lexicon==3.15.1 ; python_version == "3.10" +dnspython==2.6.1 ; python_version == "3.10" +exceptiongroup==1.3.1 ; python_version == "3.10" +execnet==2.1.2 ; python_version == "3.10" +filelock==3.32.2 ; python_version == "3.10" +funcsigs==0.4 ; python_version == "3.10" +google-api-python-client==1.6.5 ; python_version == "3.10" +google-auth==2.16.0 ; python_version == "3.10" +h11==0.16.0 ; python_version == "3.10" +httpcore==1.0.9 ; python_version == "3.10" +httplib2==0.9.2 ; python_version == "3.10" +httpx==0.28.1 ; python_version == "3.10" +idna==2.8 ; python_version == "3.10" +iniconfig==2.3.0 ; python_version == "3.10" +ipaddress==1.0.16 ; python_version == "3.10" +isort==8.0.1 ; python_version == "3.10" +jmespath==0.10.0 ; python_version == "3.10" +josepy==2.2.0 ; python_version == "3.10" +jsonpickle==4.1.2 ; python_version == "3.10" +librt==0.13.0 ; python_version == "3.10" and platform_python_implementation != "PyPy" +mccabe==0.7.0 ; python_version == "3.10" +mypy-extensions==1.1.0 ; python_version == "3.10" +mypy==2.3.0 ; python_version == "3.10" +ndg-httpsclient==0.3.2 ; python_version == "3.10" +oauth2client==4.1.3 ; python_version == "3.10" +packaging==26.3 ; python_version == "3.10" +parsedatetime==2.6 ; python_version == "3.10" +pathspec==1.1.1 ; python_version == "3.10" +pbr==1.8.0 ; python_version == "3.10" +pip==26.2.1 ; python_version == "3.10" +platformdirs==4.11.0 ; python_version == "3.10" +pluggy==1.6.0 ; python_version == "3.10" +ply==3.4 ; python_version == "3.10" +py==1.11.0 ; python_version == "3.10" +pyasn1-modules==0.4.1 ; python_version == "3.10" +pyasn1==0.4.8 ; python_version == "3.10" +pycparser==2.14 ; python_version == "3.10" +pydantic-core==2.46.4 ; python_version == "3.10" +pydantic==2.13.4 ; python_version == "3.10" +pygments==2.20.0 ; python_version == "3.10" +pylint==4.0.6 ; python_version == "3.10" +pyopenssl==26.1.0 ; python_version == "3.10" +pyotp==2.10.0 ; python_version == "3.10" +pyparsing==3.0.0 ; python_version == "3.10" +pyrfc3339==1.0 ; python_version == "3.10" +pytest-cov==7.1.0 ; python_version == "3.10" +pytest-xdist==3.8.0 ; python_version == "3.10" +pytest==9.1.1 ; python_version == "3.10" +python-augeas==0.5.0 ; python_version == "3.10" +python-dateutil==2.9.0.post0 ; python_version == "3.10" +python-digitalocean==1.15.0 ; python_version == "3.10" +python-discovery==1.5.1 ; python_version == "3.10" +pytz==2026.3.post1 ; python_version == "3.10" +pywin32==312 ; python_version == "3.10" and sys_platform == "win32" +pyyaml==6.0.3 ; python_version == "3.10" +requests-file==3.0.1 ; python_version == "3.10" +requests==2.25.1 ; python_version == "3.10" +rsa==4.9.1 ; python_version == "3.10" +ruff==0.16.1 ; python_version == "3.10" +s3transfer==0.5.2 ; python_version == "3.10" +setuptools==83.0.0 ; python_version == "3.10" +six==1.16.0 ; python_version == "3.10" +sniffio==1.3.1 ; python_version == "3.10" +soupsieve==2.9.1 ; python_version == "3.10" +tldextract==5.3.1 ; python_version == "3.10" +tomli==2.4.1 ; python_version == "3.10" +tomlkit==0.15.1 ; python_version == "3.10" +tox==3.28.0 ; python_version == "3.10" +types-httplib2==0.32.0.20260720 ; python_version == "3.10" +types-pyrfc3339==2.0.1.20250825 ; python_version == "3.10" +types-python-dateutil==2.9.0.20260716 ; python_version == "3.10" +types-pywin32==312.0.0.20260724 ; python_version == "3.10" +types-requests==2.31.0.6 ; python_version == "3.10" +types-setuptools==83.0.0.20260724 ; python_version == "3.10" +types-urllib3==1.26.25.14 ; python_version == "3.10" +typing-extensions==4.16.0 ; python_version == "3.10" +typing-inspection==0.4.2 ; python_version == "3.10" +uritemplate==3.0.1 ; python_version == "3.10" +urllib3==1.26.5 ; python_version == "3.10" +uv==0.12.2 ; python_version == "3.10" +virtualenv==21.7.1 ; python_version == "3.10" +wheel==0.47.0 ; python_version == "3.10" diff --git a/tools/pinning/DESIGN.md b/tools/pinning/DESIGN.md new file mode 100644 index 00000000000..64ab95d2d94 --- /dev/null +++ b/tools/pinning/DESIGN.md @@ -0,0 +1,133 @@ +# Certbot dependency pinning + +As described in the developer guide, we try to pin Certbot's dependencies to +well tested versions in almost all cases. Pinning Python dependencies across +different environments like this is actually quite tricky though and the files +under this directory make a somewhat reasonable, best effort approach to solve +this problem. + +## Python packaging background + +### Sdists and wheels + +Python projects are most commonly distributed on [PyPI](https://pypi.org/) as +either a [source +distribution](https://packaging.python.org/glossary/#term-Source-Distribution-or-sdist) +or a [wheel](https://packaging.python.org/glossary/#term-Wheel). Wheels don't +present a problem for us pinning dependencies because they offer a well defined +format where the dependencies of a package can be easily parsed. + +Source distributions or "sdists" are a problem though because they can contain +arbitrary Python code that must be run to determine the dependencies of the +package. This code could theoretically do anything, but it most commonly +inspects the environment it is running in and changes the project's +dependencies based the environment. As of writing this, we even do this in some +of Certbot's packages as you can see +[here](https://github.com/certbot/certbot/blob/8b610239bfcf7aac06f6e36d09a5abba3ba87047/certbot-dns-cloudflare/setup.py#L15-L27). +This is a problem because it means the environment an sdist was run in affects +the dependencies it declares making it difficult for us to determine Certbot's +dependencies for an arbitrary environment. Luckily, this is becoming less and +less of an issue with the increasing use of wheels, however, as of writing +this, some of Certbot's dependencies are still only available as sdists on some +platforms. + +### Environment markers and pyproject.toml + +Two other things have helped reduce the problems caused by sdists and are +relevant here. The first is the usage of [environment +markers](https://www.python.org/dev/peps/pep-0496/) which allows a package to +consistently declare its conditional dependencies with a static string +specifying the conditions where a dependency is required instead of dynamically +generating the list of required dependencies at runtime. This static string +keeps the package's declaration of its dependencies consistent across +environments. + +The other relatively recent change in Python packaging is the adoption of +[pyproject.toml files](https://www.python.org/dev/peps/pep-0518/) which allows +sdists to define their packages using a static file instead of a setup.py +file, which has historically been the norm. +Using a static file instead of arbitrary Python code makes it +much easier for package declarations to be reliably interpreted. The +introduction of pyproject.toml also allows for the use of build systems other +than setuptools which becomes relevant in the next section of this doc. + +## Our pinning system + +### Overview + +The files inside `tools/pinning` are used to generate Certbot's pinning files. +The files under `oldest` are used to generate the constraints file used for our +"oldest" tests while `current` is used to generate the constraints used +everywhere else. `common` includes shared files that are used for both sets of +pinnings. + +Under `current` and `oldest`, there are two files as of writing this. One is a +pyproject.toml file for use with [Poetry](https://python-poetry.org/) while +the other is a script that can be run to regenerate pinnings. The +pyproject.toml file defines a Python package that depends on everything we want +to pin. This file largely just depends on our own local packages, however, +extra dependencies can be declared to further constrain package versions or to +declare additional dependencies. + +The reason we use Poetry is that it is somewhat unique among Python packaging +tools in that when locking dependencies, it makes a best effort approach to do +this for all environments rather than just the current environment. This +includes recursively resolving dependencies declared through environment +markers that are not relevant for the current platform. It also includes +checking all wheels and sdists of a package for dependencies when picking a +specific version of a package from PyPI. You can see this in action through +the inclusion of dependencies like pywin32 which we only have a dependency on +for Windows. + +### Potential problems + +As of writing this, I'm aware of two potential problems with this pinning +system. The first is largely described earlier in the doc which is the problem +of sdists that use code to dynamically declare its dependencies. It's simply +not feasible to ensure this arbitrary Python code declares its dependencies in +the same way across all environments. Luckily, this is a largely a theoretical +problem and I'm aware of no issues with our current dependencies. + +The second problem with this approach is that build dependencies are not +tracked and pinned. To be clear, normal runtime dependencies like those +declared in "install_requires" in setup.py files and even optional runtime +dependencies like those declared in "extras_require" in setup.py are managed +properly. The problem here is with dependencies needed at build time when +initially installing a Python project. These are usually specified in +"setup_requires" in setup.py or under "requires" in the "build-system" section +of pyproject.toml files. + +Unfortunately, [tracking and pinning build dependencies seems to be a largely +unsolved problem in Python right +now](https://discuss.python.org/t/pinning-build-dependencies/8363). Our tooling +ensures that when installing build dependencies when using our pinning files +that versions from the pinning files are used (see +https://github.com/certbot/certbot/pull/8443 for more info about that), but I'm +not aware of any tool that automates the process of tracking and pinning down +build dependencies. For now, if we find any unpinned build dependencies, we can +declare a dependency on them in pyproject.toml. If a build dependency isn't +included in the constraints file, pip will use the latest version available on +PyPI. + +## Theoretical future work + +I think the system described above should work pretty well and I think it's +much better than the system we had before where how to update things like our +"oldest" pinnings was an open question. If we wanted to improve on this in the +future though, I think things to consider would be: + +1. We could require that wheels are used for all of our dependencies. If a + wheel is not available for one of our dependencies, we could try to work + with upstream to change that or build it and host it locally for ourselves. + (If we do the latter, how to pin build dependencies when building the wheel + remains an open question.) +2. We could only really try to pin our dependencies for certain environments. + This would be done by doing something like installing our packages in each + environment we care about and saving the output of a command like `pip + freeze`. With our use of snaps and Docker, this may be somewhat reasonable + because we could base them all on a common system like Ubuntu LTS, however, + it's not entirely trivial because we still have problems such as supporting + multiple CPU architectures and pinning dependencies for Windows. Alternative + development and test environments also wouldn't be fully supported. +3. We could help build better tooling that solves some of the problems with + this approach or adopts it when it becomes available. diff --git a/tools/pinning/common/export-pinned-dependencies.sh b/tools/pinning/common/export-pinned-dependencies.sh new file mode 100755 index 00000000000..02928d91cce --- /dev/null +++ b/tools/pinning/common/export-pinned-dependencies.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# This script accepts a directory containing a pyproject.toml file configured +# for use with poetry and generates and prints the pinned dependencies of that +# file. Any dependencies on acme or those referencing certbot will be removed +# from the output. The exported requirements are printed to stdout. +# +# For example, if a directory containing a pyproject.toml file for poetry is at +# ../current, you could activate Certbot's developer environment and then run a +# command like the following to generate requirements.txt for that environment: +# ./export-pinned-dependencies.sh ../current > requirements.txt +set -euo pipefail + +# If this script wasn't given a command line argument, print usage and exit. +if [ -z ${1+x} ]; then + echo "Usage:" >&2 + echo "$0 PYPROJECT_TOML_DIRECTORY [POETRY_ARGS]" >&2 + exit 1 +fi + +REPO_ROOT="$(git rev-parse --show-toplevel)" +WORK_DIR="$1" + +if ! command -v poetry >/dev/null || [ $(poetry --version | grep -oE '[0-9]+\.[0-9]+' | sed 's/\.//') -lt 12 ]; then + echo "Please install poetry 1.2+." >&2 + echo "You may need to recreate Certbot's virtual environment and activate it." >&2 + exit 1 +fi + +# Old eggs can cause outdated dependency information to be used by poetry so we +# delete them before generating the lock file. See +# https://github.com/python-poetry/poetry/issues/4103 for more info. +rm -rf ${REPO_ROOT}/*/*.egg-info +rm -rf ${REPO_ROOT}/*/src/*.egg-info + +cd "${WORK_DIR}" + +if [ -f poetry.lock ]; then + rm poetry.lock +fi + +echo "If this takes more than a few minutes, you may want to try clearing poetry's" >&2 +echo "caches with a command like the following and running this script again:" >&2 +echo >&2 +echo " poetry cache list | xargs -L1 poetry cache clear --all" >&2 +echo >&2 +echo "If that doesn't help, you can also try running this script with extra arguments" >&2 +echo 'for "poetry lock" on the command line such as -vvv to help see where poetry is' >&2 +echo "getting stuck." >&2 +extra_args="${*:2}" +poetry lock ${extra_args:+"$extra_args"} >&2 +trap 'rm poetry.lock' EXIT + +# POETRY_WARNINGS_EXPORT is set to remove warning output about +# poetry-plugin-export no longer being installed with poetry by default in the +# future which we can ignore because we explicitly depend on the plugin +# package. +# +# sed is then used to remove local packages from the output. +POETRY_WARNINGS_EXPORT=false poetry export --format constraints.txt --without-hashes | sed '/^acme @/d; /certbot/d;' diff --git a/tools/pinning/current/pyproject.toml b/tools/pinning/current/pyproject.toml new file mode 100644 index 00000000000..ce2be69fec6 --- /dev/null +++ b/tools/pinning/current/pyproject.toml @@ -0,0 +1,75 @@ +[tool.poetry] +name = "certbot-pinner" +version = "0.1.0" +description = "A simple project for pinning Certbot's dependencies using Poetry." +authors = ["Certbot Project"] +license = "Apache License 2.0" + +[tool.poetry.dependencies] +python = "^3.10" + +# Local dependencies +# Any local packages that have dependencies on other local packages must be +# listed below before the package it depends on. For instance, certbot depends +# on acme so certbot must be listed before acme. +certbot-ci = {path = "../../../certbot-ci"} +certbot-compatibility-test = {path = "../../../certbot-compatibility-test"} +certbot-dns-cloudflare = {path = "../../../certbot-dns-cloudflare", extras = ["docs"]} +certbot-dns-digitalocean = {path = "../../../certbot-dns-digitalocean", extras = ["docs"]} +certbot-dns-dnsimple = {path = "../../../certbot-dns-dnsimple", extras = ["docs"]} +certbot-dns-dnsmadeeasy = {path = "../../../certbot-dns-dnsmadeeasy", extras = ["docs"]} +certbot-dns-gehirn = {path = "../../../certbot-dns-gehirn", extras = ["docs"]} +certbot-dns-google = {path = "../../../certbot-dns-google", extras = ["docs"]} +certbot-dns-linode = {path = "../../../certbot-dns-linode", extras = ["docs"]} +certbot-dns-luadns = {path = "../../../certbot-dns-luadns", extras = ["docs"]} +certbot-dns-nsone = {path = "../../../certbot-dns-nsone", extras = ["docs"]} +certbot-dns-ovh = {path = "../../../certbot-dns-ovh", extras = ["docs"]} +certbot-dns-rfc2136 = {path = "../../../certbot-dns-rfc2136", extras = ["docs"]} +certbot-dns-route53 = {path = "../../../certbot-dns-route53", extras = ["docs"]} +certbot-dns-sakuracloud = {path = "../../../certbot-dns-sakuracloud", extras = ["docs"]} +certbot-nginx = {path = "../../../certbot-nginx"} +certbot-apache = {path = "../../../certbot-apache"} +certbot = {path = "../../../certbot", extras = ["all"]} +acme = {path = "../../../acme", extras = ["docs", "test"]} +letstest = {path = "../../../letstest"} + +# Extra dependencies +# As of writing this, cython is a build dependency of pyyaml. Since there +# doesn't appear to be a good way to automatically track down and pin build +# dependencies in Python (see +# https://discuss.python.org/t/how-to-pin-build-dependencies/8238), we list it +# as a dependency here to ensure a version of cython is pinned for extra +# stability. +# +# As of pyyaml 6.0.2, cython >= 3.0 is required for py >= 3.13, and < 3.0 for py < 3.13. +# See https://github.com/yaml/pyyaml/pull/808/files. +cython = [ + { version = "<3.0", python = "<3.13" }, + { version = ">=3.0", python = ">=3.13" } +] + +# setuptools-rust is a build dependency of cryptography, and since we don't have +# a great way of pinning build dependencies, we simply list it here to ensure a +# working version. Note: if build dependencies of setuptools-rust break at some +# point, it's probably worth enumerating and pinning them (and recursing to +# THEIR build dependencies) as well. +setuptools-rust = "*" +# pylint often adds new checks that we need to conform our code to when +# upgrading our dependencies. To help control when this needs to be done, we +# pin pylint to a compatible version here. +# +# If this pinning is removed, we may still need to add a lower bound for the +# pylint version. See https://github.com/certbot/certbot/pull/9229. +pylint = "3.3.3" +# mypy often adds new checks that we need to conform our code to when updating +# dependencies. To help control when this needs to be done, we pin mypy to a +# compatible version here. +mypy = "1.9.0" + +# Branch 4.x of tox introduces backward incompatibility changes, so require a newer +# version of tox to keep deterministic builds. +tox = ">=4" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/tools/pinning/current/repin.sh b/tools/pinning/current/repin.sh new file mode 100755 index 00000000000..5eedeea7079 --- /dev/null +++ b/tools/pinning/current/repin.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# This script automates the process of updating Certbot's dependencies +# including automatically updating the correct file. It is usually run without +# any arguments, but if any are provided, they will be passed to Poetry. +# Dependencies can be pinned to older versions by modifying pyproject.toml in +# the same directory as this file. +set -euo pipefail + +WORK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" +COMMON_DIR="$(dirname "${WORK_DIR}")/common" +REPO_ROOT="$(git rev-parse --show-toplevel)" +if ! RELATIVE_WORK_DIR="$(realpath --relative-to "$REPO_ROOT" "$WORK_DIR")"; then + echo this script needs GNU coreutils to be first in your PATH rather than macOS/BSD versions + exit 1 +fi +RELATIVE_SCRIPT_PATH="$RELATIVE_WORK_DIR/$(basename "${BASH_SOURCE[0]}")" +REQUIREMENTS_FILE="$REPO_ROOT/tools/requirements.txt" + +PINNINGS=$("${COMMON_DIR}/export-pinned-dependencies.sh" "${WORK_DIR}" "$@") +cat << EOF > "$REQUIREMENTS_FILE" +# This file was generated by $RELATIVE_SCRIPT_PATH and can be updated using +# that script. +# +# It is normally used as constraints to pip, however, it has the name +# requirements.txt so that is scanned by GitHub. See +# https://docs.github.com/en/github/visualizing-repository-data-with-graphs/about-the-dependency-graph#supported-package-ecosystems +# for more info. +EOF +echo "${PINNINGS}" >> "${REQUIREMENTS_FILE}" diff --git a/tools/pinning/oldest/pyproject.toml b/tools/pinning/oldest/pyproject.toml new file mode 100644 index 00000000000..1f6bc2b57a1 --- /dev/null +++ b/tools/pinning/oldest/pyproject.toml @@ -0,0 +1,112 @@ +# The purpose of this file is to help us test Certbot against the oldest +# versions of our dependencies which we claim to support in our setup.py files. +# +# Security alerts about vulnerable packages in this file can be ignored since +# they are only used during testing. +# +# Ideally, generating package pinnings based on our minimum allowed dependency +# versions would be done automatically by tooling, but as of writing this, both +# https://github.com/pypa/pip/issues/8085 and +# https://github.com/python-poetry/poetry/issues/3527 remain unresolved. +[tool.poetry] +name = "certbot-pinner" +version = "0.1.0" +description = "A simple project for pinning Certbot's dependencies using Poetry." +authors = ["Certbot Project"] +license = "Apache License 2.0" + +[tool.poetry.dependencies] +# The Python version here should be kept in sync with the one used in our +# oldest tests in tox.ini. +python = "<3.11,>=3.10" + +# Local dependencies +# Any local packages that have dependencies on other local packages must be +# listed below before the package it depends on. For instance, certbot depends +# on acme so certbot must be listed before acme. +certbot-ci = {path = "../../../certbot-ci"} +certbot-dns-cloudflare = {path = "../../../certbot-dns-cloudflare"} +certbot-dns-digitalocean = {path = "../../../certbot-dns-digitalocean"} +certbot-dns-dnsimple = {path = "../../../certbot-dns-dnsimple"} +certbot-dns-dnsmadeeasy = {path = "../../../certbot-dns-dnsmadeeasy"} +certbot-dns-gehirn = {path = "../../../certbot-dns-gehirn"} +certbot-dns-google = {path = "../../../certbot-dns-google"} +certbot-dns-linode = {path = "../../../certbot-dns-linode"} +certbot-dns-luadns = {path = "../../../certbot-dns-luadns"} +certbot-dns-nsone = {path = "../../../certbot-dns-nsone"} +certbot-dns-ovh = {path = "../../../certbot-dns-ovh"} +certbot-dns-rfc2136 = {path = "../../../certbot-dns-rfc2136"} +certbot-dns-route53 = {path = "../../../certbot-dns-route53"} +certbot-dns-sakuracloud = {path = "../../../certbot-dns-sakuracloud"} +certbot-nginx = {path = "../../../certbot-nginx"} +certbot-apache = {path = "../../../certbot-apache"} +certbot = {path = "../../../certbot", extras = ["test"]} +acme = {path = "../../../acme", extras = ["test"]} + +# Oldest dependencies +# We specify the oldest versions of our dependencies that we keep support for +# below. These dependencies can be updated as desired to simplify or improve +# Certbot or its development. If the dependency being updated is a direct +# dependency of one of our own packages, the minimum required version of that +# dependency should be updated in our setup.py files as well to communicate +# this information to our users. + +ConfigArgParse = "1.5.3" +apacheconfig = "0.3.2" +asn1crypto = "0.24.0" +# we were already ignoring debian 11 for our boto* versions; this uses the versions from ubuntu 22 +boto3 = "1.20.34" +botocore = "1.23.34" +cffi = "2.0.0" +chardet = "3.0.4" +cloudflare = "4.0.0" +configobj = "5.0.6" +cryptography = "47.0.0" +distro = "1.7.0" +dns-lexicon = "3.15.1" +dnspython = "2.6.1" +funcsigs = "0.4" +google-api-python-client = "1.6.5" +google-auth = "2.16.0" +httplib2 = "0.9.2" +idna = "2.8" +ipaddress = "1.0.16" +ndg-httpsclient = "0.3.2" +parsedatetime = "2.6" +pbr = "1.8.0" +ply = "3.4" +pyOpenSSL = "26.1.0" +pyRFC3339 = "1.0" +pyasn1 = "0.4.8" +pycparser = "2.14" +pyparsing = "3.0.0" +python-augeas = "0.5.0" +python-digitalocean = "1.15.0" +requests = "2.25.1" +six = "1.16.0" +urllib3 = "1.26.5" + +# Build dependencies +# Since there doesn't appear to +# doesn't appear to be a good way to automatically track down and pin build +# dependencies in Python (see +# https://discuss.python.org/t/how-to-pin-build-dependencies/8238), we list any +# build dependencies here to ensure they're pinned for extra stability. + +# cython is a build dependency of pyyaml +# +# +# As of pyyaml 6.0.2, cython >= 3.0 is required for py >= 3.13, and < 3.0 for py < 3.13. +# See https://github.com/yaml/pyyaml/pull/808/files. +cython = [ + { version = "<3.0", python = "<3.13" }, + { version = ">=3.0", python = ">=3.13" } +] + +# Other dependencies +# We add any dependencies that must be specified in this file for any another +# reason below. + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/tools/pinning/oldest/repin.sh b/tools/pinning/oldest/repin.sh new file mode 100755 index 00000000000..39af56d99d0 --- /dev/null +++ b/tools/pinning/oldest/repin.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# This script automates the process of updating Certbot's dependencies +# including automatically updating the correct file. It is usually run without +# any arguments, but if any are provided, they will be passed to Poetry. +# Dependencies can be pinned to older versions by modifying pyproject.toml in +# the same directory as this file. +set -euo pipefail + +WORK_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" +COMMON_DIR="$(dirname "${WORK_DIR}")/common" +REPO_ROOT="$(git rev-parse --show-toplevel)" +if ! RELATIVE_WORK_DIR="$(realpath --relative-to "$REPO_ROOT" "$WORK_DIR")"; then + echo this script needs GNU coreutils to be first in your PATH rather than macOS/BSD versions + exit 1 +fi +RELATIVE_SCRIPT_PATH="$RELATIVE_WORK_DIR/$(basename "${BASH_SOURCE[0]}")" +CONSTRAINTS_FILE="$REPO_ROOT/tools/oldest_constraints.txt" + +PINNINGS=$("${COMMON_DIR}/export-pinned-dependencies.sh" "${WORK_DIR}" "$@") +cat << EOF > "$CONSTRAINTS_FILE" +# This file was generated by $RELATIVE_SCRIPT_PATH and can be updated using +# that script. +EOF +echo "${PINNINGS}" >> "${CONSTRAINTS_FILE}" diff --git a/tools/pip_install.py b/tools/pip_install.py index 4466729e03a..dc74f2ed5d8 100755 --- a/tools/pip_install.py +++ b/tools/pip_install.py @@ -1,102 +1,73 @@ #!/usr/bin/env python # pip installs packages using pinned package versions. If CERTBOT_OLDEST is set -# to 1, a combination of tools/oldest_constraints.txt, -# tools/dev_constraints.txt, and local-oldest-requirements.txt contained in the -# top level of the package's directory is used, otherwise, a combination of -# certbot-auto's requirements file and tools/dev_constraints.txt is used. The -# other file always takes precedence over tools/dev_constraints.txt. If -# CERTBOT_OLDEST is set, this script must be run with `-e ` and -# no other arguments. +# to 1, tools/oldest_constraints.txt is used, otherwise, tools/requirements.txt +# is used. Before installing the requested packages, core Python packaging +# tools like pip, setuptools, and wheel are updated to pinned versions to +# increase stability of the install. +# +# cryptography is currently using this script in their CI at +# https://github.com/pyca/cryptography/blob/14d45c2259b01f1459eeab8bb7d85ce4cfb0841b/.github/downstream.d/certbot.sh#L8-L9. +# We should try to remember to keep their repo updated if we make any changes +# to this script which may break things for them. + +from __future__ import absolute_import +from __future__ import print_function -from __future__ import print_function, absolute_import - -import subprocess import os +import subprocess import sys -import re -import shutil -import tempfile - -import merge_requirements as merge_module -import readlink def find_tools_path(): - return os.path.dirname(readlink.main(__file__)) + return os.path.dirname(os.path.realpath(__file__)) + + +def call_with_print(command, env): + assert env is not None + print(command) + subprocess.check_call(command, shell=True, env=env) -def certbot_oldest_processing(tools_path, args, test_constraints): - if args[0] != '-e' or len(args) != 2: - raise ValueError('When CERTBOT_OLDEST is set, this script must be run ' - 'with a single -e argument.') - # remove any extras such as [dev] - pkg_dir = re.sub(r'\[\w+\]', '', args[1]) - requirements = os.path.join(pkg_dir, 'local-oldest-requirements.txt') - # packages like acme don't have any local oldest requirements - if not os.path.isfile(requirements): - requirements = None - shutil.copy(os.path.join(tools_path, 'oldest_constraints.txt'), test_constraints) +def uv_install_with_print(args_str, env): + command = ['"', sys.executable, '" -m uv pip install ', args_str] + call_with_print(''.join(command), env=env) - return requirements +def pip_install_with_print(args_str, env): + command = ['"', sys.executable, '" -m pip install --disable-pip-version-check --use-pep517 ', args_str] + call_with_print(''.join(command), env=env) -def certbot_normal_processing(tools_path, test_constraints): +def pip_constrained_environ(): + tools_path = find_tools_path() + repo_path = os.path.dirname(tools_path) - certbot_requirements = os.path.normpath(os.path.join( - repo_path, 'letsencrypt-auto-source/pieces/dependency-requirements.txt')) - with open(certbot_requirements, 'r') as fd: - data = fd.readlines() - with open(test_constraints, 'w') as fd: - for line in data: - search = re.search(r'^(\S*==\S*).*$', line) - if search: - fd.write('{0}{1}'.format(search.group(1), os.linesep)) - - -def merge_requirements(tools_path, test_constraints, all_constraints): - merged_requirements = merge_module.main( - os.path.join(tools_path, 'dev_constraints.txt'), - test_constraints - ) - with open(all_constraints, 'w') as fd: - fd.write(merged_requirements) - - -def call_with_print(command, cwd=None): - print(command) - subprocess.check_call(command, shell=True, cwd=cwd or os.getcwd()) + if os.environ.get('CERTBOT_OLDEST') == '1': + constraints_path = os.path.normpath(os.path.join( + repo_path, 'tools', 'oldest_constraints.txt')) + else: + constraints_path = os.path.normpath(os.path.join( + repo_path, 'tools', 'requirements.txt')) + + env = os.environ.copy() + # We set constraints for pip using an environment variable so that they + # are also used when installing build dependencies. See + # https://github.com/certbot/certbot/pull/8443 for more info. + env["PIP_CONSTRAINT"] = constraints_path + env["UV_CONSTRAINT"] = constraints_path + env["UV_BUILD_CONSTRAINT"] = constraints_path + return env + + +def pipstrap(env=None): + if env is None: + env = pip_constrained_environ() + pip_install_with_print('pip setuptools wheel uv', env=env) def main(args): - tools_path = find_tools_path() - working_dir = tempfile.mkdtemp() - - if os.environ.get('TRAVIS'): - # When this script is executed on Travis, the following print will make the log - # be folded until the end command is printed (see finally section). - print('travis_fold:start:install_certbot_deps') - - try: - test_constraints = os.path.join(working_dir, 'test_constraints.txt') - all_constraints = os.path.join(working_dir, 'all_constraints.txt') - - requirements = None - if os.environ.get('CERTBOT_OLDEST') == '1': - requirements = certbot_oldest_processing(tools_path, args, test_constraints) - else: - certbot_normal_processing(tools_path, test_constraints) - - merge_requirements(tools_path, test_constraints, all_constraints) - if requirements: - call_with_print('"{0}" -m pip install --constraint "{1}" --requirement "{2}"' - .format(sys.executable, all_constraints, requirements)) - - call_with_print('"{0}" -m pip install --constraint "{1}" {2}' - .format(sys.executable, all_constraints, ' '.join(args))) - finally: - if os.environ.get('TRAVIS'): - print('travis_fold:end:install_certbot_deps') - shutil.rmtree(working_dir) + env = pip_constrained_environ() + pipstrap(env) + uv_install_with_print(' '.join(args), env=env) if __name__ == '__main__': diff --git a/tools/pip_install_editable.py b/tools/pip_install_editable.py deleted file mode 100755 index 8eaf3a9fab0..00000000000 --- a/tools/pip_install_editable.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python -# pip installs packages in editable mode using certbot-auto's requirements file -# as constraints - -from __future__ import absolute_import - -import sys - -import pip_install - -def main(args): - new_args = [] - for arg in args: - new_args.append('-e') - new_args.append(arg) - - pip_install.main(new_args) - -if __name__ == '__main__': - main(sys.argv[1:]) diff --git a/tools/pipstrap.py b/tools/pipstrap.py new file mode 100755 index 00000000000..d74c50edd63 --- /dev/null +++ b/tools/pipstrap.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +"""Uses pip to upgrade Python packaging tools to pinned versions.""" +import pip_install + + +def main(): + pip_install.pipstrap() + + +if __name__ == '__main__': + main() diff --git a/tools/readlink.py b/tools/readlink.py deleted file mode 100755 index 0199ce18466..00000000000 --- a/tools/readlink.py +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env python -"""Canonicalizes a path and follows any symlinks. - -This is the equivalent of `readlink -f` on many Linux systems. This is -useful as there are often differences in readlink on different -platforms. - -""" -from __future__ import print_function - -import os -import sys - -def main(link): - return os.path.realpath(link) - -if __name__ == '__main__': - print(main(sys.argv[1])) diff --git a/tools/release.sh b/tools/release.sh index ae3e78dc1e3..a88b50e7e07 100755 --- a/tools/release.sh +++ b/tools/release.sh @@ -19,7 +19,7 @@ CheckVersion() { CheckVersion "$1" CheckVersion "$2" -if [ "$RELEASE_GPG_KEY" = "" ] && ! gpg2 --card-status >/dev/null 2>&1; then +if [ "$RELEASE_GPG_KEY" = "" ] && ! gpg --card-status >/dev/null 2>&1; then echo OpenPGP card not found! echo Please insert your PGP card and run this script again. exit 1 @@ -31,6 +31,15 @@ if ! command -v script >/dev/null 2>&1; then exit 1 fi +if [ -n "$SNAP_BUILD" ]; then + echo "Running the release script with the environment variable SNAP_BUILD" + echo "set will cause plugins' wheels to be built without dependencies" + echo "on Certbot. See https://github.com/certbot/certbot/pull/8091 for more" + echo "info. Please unset this environment variable and run this script" + echo "again." + exit 1 +fi + export RELEASE_DIR="./releases" mv "$RELEASE_DIR" "$RELEASE_DIR.$(date +%s).bak" || true LOG_PATH="log" diff --git a/tools/release_message.py b/tools/release_message.py new file mode 100755 index 00000000000..1fc579e9533 --- /dev/null +++ b/tools/release_message.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +""" +Script to generate a message notifying the person doing the release +of the result of the workflow run. + +Run: + +python tools/release_message.py GITHUB_AUTHOR_NAME SUCCESS + +where SUCCESS is True or False +""" +import os +import random +import sys + +server_url = os.environ['GITHUB_SERVER_URL'] +repo_name = os.environ['GITHUB_REPOSITORY'] +run_id = os.environ['GITHUB_RUN_ID'] + +def get_greeting(): + fun_greetings = [ + 'Hey', + 'Paging', + 'Hi', + 'Pinging', + ] + return random.choice(fun_greetings) + +def get_message(success: bool): + fun_success_messages = [ + 'the certbot release is ready to come out of the oven!', + "it's release-finishing go time!", + 'all certbot release systems are set for launch!', + ] + + if success: + message = random.choice(fun_success_messages) + else: + message = "the release pipeline has failed." + return message + +def get_content(requested_for: str, success: bool): + build_url = f'{server_url}/{repo_name}/actions/runs/{run_id}' + + # We use github author here because it's what we have access to. If the name sometimes + # changes, add any name it might be. Check the git log. + # This is a map of team member github author names to opensource mattermost username + usernames_map = { + 'Will Greenberg': 'willg', + 'Erica Portnoy': 'erica', + 'Brad Warren': 'brad', + 'ohemorange': 'erica', + } + + if requested_for in usernames_map: + text_body = f'{get_greeting()} @{usernames_map[requested_for]}, {get_message(success)}\n{build_url}' + else: + text_body = (f"{get_greeting()} {requested_for}, {get_message(success)}\nIf you'd like to get @ mentioned for " + "releases you do in the future, please modify tools/release_message.py with your " + f"git author name.\n{build_url}") + return text_body + +random.seed() +requested_for: str = sys.argv[1].rstrip() +success: bool = (sys.argv[2].rstrip().lower() == 'true') +print(get_content(requested_for, success)) diff --git a/tools/requirements.txt b/tools/requirements.txt new file mode 100644 index 00000000000..a1b230d1e2b --- /dev/null +++ b/tools/requirements.txt @@ -0,0 +1,198 @@ +# This file was generated by tools/pinning/current/repin.sh and can be updated using +# that script. +# +# It is normally used as constraints to pip, however, it has the name +# requirements.txt so that is scanned by GitHub. See +# https://docs.github.com/en/github/visualizing-repository-data-with-graphs/about-the-dependency-graph#supported-package-ecosystems +# for more info. +alabaster==1.0.0 ; python_version >= "3.10" and python_version < "4.0" +annotated-types==0.8.0 ; python_version >= "3.10" and python_version < "4.0" +anyio==4.14.2 ; python_version >= "3.10" and python_version < "4.0" +apacheconfig==0.3.2 ; python_version >= "3.10" and python_version < "4.0" +astroid==3.3.11 ; python_version >= "3.10" and python_version < "4.0" +asttokens==3.0.2 ; python_version >= "3.10" and python_version < "4.0" +babel==2.18.0 ; python_version >= "3.10" and python_version < "4.0" +backports-tarfile==1.2.0 ; python_version >= "3.10" and python_version < "3.12" +backports-zstd==1.6.0 ; python_version >= "3.10" and python_version < "3.14" +bcrypt==5.0.0 ; python_version >= "3.10" and python_version < "4.0" +beautifulsoup4==4.15.0 ; python_version >= "3.10" and python_version < "4.0" +boto3==1.43.65 ; python_version >= "3.10" and python_version < "4.0" +botocore==1.43.65 ; python_version >= "3.10" and python_version < "4.0" +build==1.5.0 ; python_version >= "3.10" and python_version < "4.0" +cachecontrol==0.14.4 ; python_version >= "3.10" and python_version < "4.0" +cachetools==7.1.7 ; python_version >= "3.10" and python_version < "4.0" +certifi==2026.7.22 ; python_version >= "3.10" and python_version < "4.0" +cffi==2.1.1 ; python_version >= "3.10" and python_version < "4.0" +charset-normalizer==3.4.9 ; python_version >= "3.10" and python_version < "4.0" +cleo==2.1.0 ; python_version >= "3.10" and python_version < "4.0" +click==8.4.2 ; python_version >= "3.10" and python_version < "4.0" +cloudflare==5.6.0 ; python_version >= "3.10" and python_version < "4.0" +colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" +configargparse==1.7.5 ; python_version >= "3.10" and python_version < "4.0" +configobj==5.0.9 ; python_version >= "3.10" and python_version < "4.0" +coverage==7.15.3 ; python_version >= "3.10" and python_version < "4.0" +crashtest==0.4.1 ; python_version >= "3.10" and python_version < "4.0" +cryptography==50.0.0 ; python_version >= "3.10" and python_version < "4.0" +cython==0.29.37 ; python_version >= "3.10" and python_version <= "3.12" +cython==3.2.9 ; python_version >= "3.13" and python_version < "4.0" +decorator==5.3.1 ; python_version >= "3.10" and python_version < "4.0" +deprecated==1.3.1 ; python_version >= "3.10" and python_version < "4.0" +dill==0.4.1 ; python_version >= "3.10" and python_version < "4.0" +distlib==0.4.3 ; python_version >= "3.10" and python_version < "4.0" +distro==1.9.0 ; python_version >= "3.10" and python_version < "4.0" +dns-lexicon==3.25.2 ; python_version >= "3.10" and python_version < "4.0" +dnspython==2.8.0 ; python_version >= "3.10" and python_version < "4.0" +docutils==0.21.2 ; python_version == "3.10" +docutils==0.22.4 ; python_version >= "3.11" and python_version < "4.0" +dulwich==1.2.12 ; python_version >= "3.10" and python_version < "4.0" +exceptiongroup==1.3.1 ; python_version == "3.10" +execnet==2.1.2 ; python_version >= "3.10" and python_version < "4.0" +executing==2.2.1 ; python_version >= "3.10" and python_version < "4.0" +fabric==3.2.3 ; python_version >= "3.10" and python_version < "4.0" +fastjsonschema==2.22.1 ; python_version >= "3.10" and python_version < "4.0" +filelock==3.32.2 ; python_version >= "3.10" and python_version < "4.0" +findpython==0.8.0 ; python_version >= "3.10" and python_version < "4.0" +google-api-core==2.33.0 ; python_version >= "3.10" and python_version < "4.0" +google-api-python-client==2.198.0 ; python_version >= "3.10" and python_version < "4.0" +google-auth-httplib2==0.4.0 ; python_version >= "3.10" and python_version < "4.0" +google-auth==2.56.2 ; python_version >= "3.10" and python_version < "4.0" +googleapis-common-protos==1.75.0 ; python_version >= "3.10" and python_version < "4.0" +h11==0.16.0 ; python_version >= "3.10" and python_version < "4.0" +httpcore==1.0.9 ; python_version >= "3.10" and python_version < "4.0" +httplib2==0.32.0 ; python_version >= "3.10" and python_version < "4.0" +httpx==0.28.1 ; python_version >= "3.10" and python_version < "4.0" +id==1.6.1 ; python_version >= "3.10" and python_version < "4.0" +idna==3.18 ; python_version >= "3.10" and python_version < "4.0" +imagesize==1.5.0 ; python_version >= "3.14" and python_version < "4.0" +imagesize==2.0.0 ; python_version >= "3.10" and python_version < "3.14" +importlib-metadata==9.0.0 ; python_version >= "3.10" and python_version < "3.12" +iniconfig==2.3.0 ; python_version >= "3.10" and python_version < "4.0" +installer==1.0.1 ; python_version >= "3.10" and python_version < "4.0" +invoke==2.2.1 ; python_version >= "3.10" and python_version < "4.0" +ipdb==0.13.13 ; python_version >= "3.10" and python_version < "4.0" +ipython-pygments-lexers==1.1.1 ; python_version >= "3.11" and python_version < "4.0" +ipython==8.39.0 ; python_version == "3.10" +ipython==9.16.1 ; python_version >= "3.11" and python_version < "4.0" +isort==5.13.2 ; python_version >= "3.10" and python_version < "4.0" +jaraco-classes==3.4.0 ; python_version >= "3.10" and python_version < "4.0" +jaraco-context==6.1.2 ; python_version >= "3.10" and python_version < "4.0" +jaraco-functools==4.6.0 ; python_version >= "3.10" and python_version < "4.0" +jedi==0.20.0 ; python_version >= "3.10" and python_version < "4.0" +jeepney==0.9.0 ; python_version >= "3.10" and python_version < "4.0" and sys_platform == "linux" +jinja2==3.1.6 ; python_version >= "3.10" and python_version < "4.0" +jmespath==1.1.0 ; python_version >= "3.10" and python_version < "4.0" +josepy==2.2.0 ; python_version >= "3.10" and python_version < "4.0" +jsonpickle==4.1.2 ; python_version >= "3.10" and python_version < "4.0" +keyring==25.7.0 ; python_version >= "3.10" and python_version < "4.0" +markdown-it-py==4.2.0 ; python_version >= "3.10" and python_version < "4.0" +markupsafe==3.0.3 ; python_version >= "3.10" and python_version < "4.0" +matplotlib-inline==0.2.2 ; python_version >= "3.10" and python_version < "4.0" +mccabe==0.7.0 ; python_version >= "3.10" and python_version < "4.0" +mdurl==0.1.2 ; python_version >= "3.10" and python_version < "4.0" +more-itertools==11.1.0 ; python_version >= "3.10" and python_version < "4.0" +msgpack==1.2.1 ; python_version >= "3.10" and python_version < "4.0" +mypy-extensions==1.1.0 ; python_version >= "3.10" and python_version < "4.0" +mypy==1.9.0 ; python_version >= "3.10" and python_version < "4.0" +nh3==0.3.6 ; python_version >= "3.10" and python_version < "4.0" +packaging==26.3 ; python_version >= "3.10" and python_version < "4.0" +paramiko==5.0.0 ; python_version >= "3.10" and python_version < "4.0" +parsedatetime==2.6 ; python_version >= "3.10" and python_version < "4.0" +parso==0.8.7 ; python_version >= "3.10" and python_version < "4.0" +pbs-installer==2026.7.28 ; python_version >= "3.10" and python_version < "4.0" +pexpect==4.9.0 ; python_version >= "3.10" and python_version < "4.0" and sys_platform != "win32" and sys_platform != "emscripten" +pip==26.2.1 ; python_version >= "3.10" and python_version < "4.0" +pkginfo==1.12.1.2 ; python_version >= "3.10" and python_version < "4.0" +platformdirs==4.11.0 ; python_version >= "3.10" and python_version < "4.0" +pluggy==1.6.0 ; python_version >= "3.10" and python_version < "4.0" +ply==3.11 ; python_version >= "3.10" and python_version < "4.0" +poetry-core==2.4.0 ; python_version >= "3.10" and python_version < "4.0" +poetry-plugin-export==1.10.0 ; python_version >= "3.10" and python_version < "4.0" +poetry==2.4.1 ; python_version >= "3.10" and python_version < "4.0" +prompt-toolkit==3.0.53 ; python_version >= "3.10" and python_version < "4.0" +proto-plus==1.28.2 ; python_version >= "3.10" and python_version < "4.0" +protobuf==7.35.1 ; python_version >= "3.10" and python_version < "4.0" +psutil==7.2.2 ; python_version >= "3.11" and python_version < "4.0" and sys_platform != "emscripten" and sys_platform != "cygwin" +ptyprocess==0.7.0 ; python_version >= "3.10" and python_version < "4.0" and sys_platform != "win32" and sys_platform != "emscripten" +pure-eval==0.2.3 ; python_version >= "3.10" and python_version < "4.0" +pyasn1-modules==0.4.2 ; python_version >= "3.10" and python_version < "4.0" +pyasn1==0.6.4 ; python_version >= "3.10" and python_version < "4.0" +pycparser==3.0 ; python_version >= "3.10" and python_version < "4.0" and implementation_name != "PyPy" +pydantic-core==2.46.4 ; python_version >= "3.10" and python_version < "4.0" +pydantic==2.13.4 ; python_version >= "3.10" and python_version < "4.0" +pygments==2.20.0 ; python_version >= "3.10" and python_version < "4.0" +pylint==3.3.3 ; python_version >= "3.10" and python_version < "4.0" +pynacl==1.6.2 ; python_version >= "3.10" and python_version < "4.0" +pyopenssl==26.4.0 ; python_version >= "3.10" and python_version < "4.0" +pyotp==2.10.0 ; python_version >= "3.10" and python_version < "4.0" +pyparsing==3.3.2 ; python_version >= "3.10" and python_version < "4.0" +pyproject-api==1.11.0 ; python_version >= "3.10" and python_version < "4.0" +pyproject-hooks==1.2.0 ; python_version >= "3.10" and python_version < "4.0" +pyrfc3339==2.1.0 ; python_version >= "3.10" and python_version < "4.0" +pytest-cov==7.1.0 ; python_version >= "3.10" and python_version < "4.0" +pytest-xdist==3.8.0 ; python_version >= "3.10" and python_version < "4.0" +pytest==9.1.1 ; python_version >= "3.10" and python_version < "4.0" +python-augeas==1.2.0 ; python_version >= "3.10" and python_version < "4.0" +python-dateutil==2.9.0.post0 ; python_version >= "3.10" and python_version < "4.0" +python-digitalocean==1.17.0 ; python_version >= "3.10" and python_version < "4.0" +python-discovery==1.5.1 ; python_version >= "3.10" and python_version < "4.0" +pywin32-ctypes==0.2.3 ; python_version >= "3.10" and python_version < "4.0" and sys_platform == "win32" +pywin32==312 ; python_version >= "3.10" and python_version < "4.0" and sys_platform == "win32" +pyyaml==6.0.3 ; python_version >= "3.10" and python_version < "4.0" +rapidfuzz==3.14.5 ; python_version >= "3.10" and python_version < "4.0" +readme-renderer==45.0 ; python_version >= "3.10" and python_version < "4.0" +requests-file==3.0.1 ; python_version >= "3.10" and python_version < "4.0" +requests-toolbelt==1.0.0 ; python_version >= "3.10" and python_version < "4.0" +requests-unixsocket==0.4.1 ; python_version >= "3.10" and python_version < "4.0" +requests==2.34.2 ; python_version >= "3.10" and python_version < "4.0" +rfc3986==2.0.0 ; python_version >= "3.10" and python_version < "4.0" +rich==15.0.0 ; python_version >= "3.10" and python_version < "4.0" +roman-numerals==4.1.0 ; python_version >= "3.11" and python_version < "4.0" +ruff==0.16.1 ; python_version >= "3.10" and python_version < "4.0" +s3transfer==0.19.2 ; python_version >= "3.10" and python_version < "4.0" +secretstorage==3.5.0 ; python_version >= "3.10" and python_version < "4.0" and sys_platform == "linux" +semantic-version==2.10.0 ; python_version >= "3.10" and python_version < "4.0" +setuptools-rust==1.13.0 ; python_version >= "3.10" and python_version < "4.0" +setuptools==83.0.0 ; python_version >= "3.10" and python_version < "4.0" +shellingham==1.5.4 ; python_version >= "3.10" and python_version < "4.0" +six==1.17.0 ; python_version >= "3.10" and python_version < "4.0" +sniffio==1.3.1 ; python_version >= "3.10" and python_version < "4.0" +snowballstemmer==3.1.1 ; python_version >= "3.10" and python_version < "4.0" +soupsieve==2.9.1 ; python_version >= "3.10" and python_version < "4.0" +sphinx-rtd-theme==3.1.0 ; python_version >= "3.10" and python_version < "4.0" +sphinx==8.1.3 ; python_version == "3.10" +sphinx==9.0.4 ; python_version == "3.11" +sphinx==9.1.0 ; python_version >= "3.12" and python_version < "4.0" +sphinxcontrib-applehelp==2.0.0 ; python_version >= "3.10" and python_version < "4.0" +sphinxcontrib-devhelp==2.0.0 ; python_version >= "3.10" and python_version < "4.0" +sphinxcontrib-htmlhelp==2.1.0 ; python_version >= "3.10" and python_version < "4.0" +sphinxcontrib-jquery==4.1 ; python_version >= "3.10" and python_version < "4.0" +sphinxcontrib-jsmath==1.0.1 ; python_version >= "3.10" and python_version < "4.0" +sphinxcontrib-qthelp==2.0.0 ; python_version >= "3.10" and python_version < "4.0" +sphinxcontrib-serializinghtml==2.0.0 ; python_version >= "3.10" and python_version < "4.0" +stack-data==0.6.3 ; python_version >= "3.10" and python_version < "4.0" +tldextract==5.3.1 ; python_version >= "3.10" and python_version < "4.0" +tomli-w==1.2.0 ; python_version >= "3.10" and python_version < "4.0" +tomli==2.4.1 ; python_version == "3.10" +tomlkit==0.15.1 ; python_version >= "3.10" and python_version < "4.0" +towncrier==25.8.0 ; python_version >= "3.10" and python_version < "4.0" +tox==4.58.0 ; python_version >= "3.10" and python_version < "4.0" +traitlets==5.16.1 ; python_version >= "3.10" and python_version < "4.0" +trove-classifiers==2026.6.1.19 ; python_version >= "3.10" and python_version < "4.0" +twine==7.0.0 ; python_version >= "3.10" and python_version < "4.0" +types-httplib2==0.32.0.20260720 ; python_version >= "3.10" and python_version < "4.0" +types-pyrfc3339==2.0.1.20250825 ; python_version >= "3.10" and python_version < "4.0" +types-python-dateutil==2.9.0.20260716 ; python_version >= "3.10" and python_version < "4.0" +types-pywin32==312.0.0.20260724 ; python_version >= "3.10" and python_version < "4.0" +types-requests==2.33.0.20260712 ; python_version >= "3.10" and python_version < "4.0" +types-setuptools==83.0.0.20260724 ; python_version >= "3.10" and python_version < "4.0" +typing-extensions==4.16.0 ; python_version >= "3.10" and python_version < "4.0" +typing-inspection==0.4.2 ; python_version >= "3.10" and python_version < "4.0" +uritemplate==4.2.0 ; python_version >= "3.10" and python_version < "4.0" +urllib3==2.7.0 ; python_version >= "3.10" and python_version < "4.0" +uv==0.12.2 ; python_version >= "3.10" and python_version < "4.0" +virtualenv==21.7.1 ; python_version >= "3.10" and python_version < "4.0" +wcwidth==0.8.2 ; python_version >= "3.10" and python_version < "4.0" +wheel==0.47.0 ; python_version >= "3.10" and python_version < "4.0" +wrapt==2.3.0 ; python_version >= "3.10" and python_version < "4.0" +xattr==1.3.0 ; python_version >= "3.10" and python_version < "4.0" and sys_platform == "darwin" +zipp==4.1.0 ; python_version >= "3.10" and python_version < "3.12" diff --git a/tools/retry.sh b/tools/retry.sh new file mode 100755 index 00000000000..964aab8eaad --- /dev/null +++ b/tools/retry.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# Retries a command if it fails. +# +# This is based on travis_retry.bash +# https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/bash/travis_retry.bash. +set -e +result=0 +count=1 +while [[ "${count}" -le 3 ]]; do + result=0 + "${@}" || result="${?}" + if [[ $result -eq 0 ]]; then break; fi + count="$((count + 1))" + sleep 1 +done + +exit "${result}" diff --git a/tools/simple_http_server.py b/tools/simple_http_server.py deleted file mode 100755 index 14ac9a3d37c..00000000000 --- a/tools/simple_http_server.py +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env python -"""A version of Python 2.x's SimpleHTTPServer that flushes its output.""" -from BaseHTTPServer import HTTPServer -from SimpleHTTPServer import SimpleHTTPRequestHandler -import sys - -def serve_forever(port=0): - """Spins up an HTTP server on all interfaces and the given port. - - A message is printed to stdout specifying the address and port being used - by the server. - - :param int port: port number to use. - - """ - server = HTTPServer(('', port), SimpleHTTPRequestHandler) - print('Serving HTTP on {0} port {1} ...'.format(*server.server_address)) - sys.stdout.flush() - server.serve_forever() - - -if __name__ == '__main__': - kwargs = {} - if len(sys.argv) > 1: - kwargs['port'] = int(sys.argv[1]) - serve_forever(**kwargs) diff --git a/tools/snap/README.md b/tools/snap/README.md new file mode 100644 index 00000000000..c4ae0558d17 --- /dev/null +++ b/tools/snap/README.md @@ -0,0 +1,100 @@ +# Building Certbot Snaps + +## Local Testing and Development + +These instructions are recommended when testing anything about the snap setup for ease of debugging. +The architecture of the built snap is limited to the architecture of the system it is built on. + +### Initial VM Set Up + +These steps need to be done once to set up your VM and do not need to be run again to rebuild the snap. + + 1. Start with a Focal VM. You need a full virtual machine using something like DigitalOcean, EC2, or VirtualBox. Docker won't work. Another version of Ubuntu can probably be used, but Focal was used when writing these instructions. + 2. Set up a user other than root with sudo privileges for use with snapcraft and run all of the following commands with it. A command to do this for a user named certbot looks like `adduser certbot && usermod -aG sudo certbot && su - certbot`. + 3. Install git and python with `sudo apt update && sudo apt install -y git python`. + 4. Set up lxd for use with snapcraft by running `sudo snap install lxd; sudo /snap/bin/lxd waitready && sudo /snap/bin/lxd init --auto` (errors here are ok; it may already + have been installed on your system). + 5. Add your current user to the lxd group and update your shell to have the new assignment by running `sudo usermod -a -G lxd ${USER} && newgrp lxd`. + 6. Install snapcraft with `sudo snap install --classic snapcraft`. + 7. `cd ~` (or any other directory where you want our source files to be) + 8. Run `git clone https://github.com/certbot/certbot` + 9. `cd certbot` (All further instructions are relative to this directory.) + +### Certbot Snap + +#### Reset the Environment + +If the snap has been built before, the instructions below clean up the build environment so it can reliably be used again. + + 1. `snapcraft clean --use-lxd` + 2. [Optional] `mv certbot_*_amd64.snap certbot_amd64.snap.bak` + +#### Build the Certbot Snap + +These are the steps to build and install the Certbot snap. If you have run these steps before, you may want to run the commands in the section above to clean things up or save a previous build before building the snap again (running `snapcraft` again will overwrite the previous snap). + + 1. Run `snapcraft --use-lxd`. + 2. Install the generated snap with `sudo snap install --dangerous --classic certbot_*_amd64.snap`. You can transfer the snap to a different machine to run it there instead if you prefer. + +#### Run + +Run Certbot as normal. For example, `certbot plugins` should display the Apache and Nginx plugins. + +### Certbot Plugin Snaps + +These instructions use the `certbot-dns-dnsimple` plugin as an example, but all of Certbot's other plugin snaps can be built in the same way. + +#### Reset the Environment + +If the plugin snap has been built before, the instructions below clean up the build environment so it can reliably be used again. + + 1. `cd certbot-dns-dnsimple` + 2. `snapcraft clean --use-lxd` + 3. [Optional] `mv certbot-dns-dnsimple_*_amd64.snap certbot-dns-simple_amd64.snap.bak` + 4. `cd ..` + +#### Build a Certbot Plugin Snap + +These are the steps to build and install the Certbot DNSimple plugin snap. If you have run these steps before, you may want to run the commands in the section above to clean things up or save a previous build before building the snap again (running `snapcraft` again will overwrite the previous snap). + + 1. Run `tools/snap/generate_dnsplugins_all.sh` to generate all necessary files for all plugin snaps. + 2. `cd certbot-dns-dnsimple` + 3. `snapcraft --use-lxd` + 4. Run `sudo snap set certbot trust-plugin-with-root=ok`. + 5. Install the generated snap with `sudo snap install --dangerous certbot-dns-dnsimple_*_amd64.snap`. Again, you can transfer the snap to a different machine to run it there instead if you prefer. + 6. Connect the plugin with `sudo snap connect certbot:plugin certbot-dns-dnsimple`. + 7. Connect the plugin metadata with `sudo snap connect certbot-dns-dnsimple:certbot-metadata certbot:certbot-metadata`. Install the plugin again to test refresh; if the plugin's hook creates any logs, they are at `/var/snap/certbot-dns-dnsimple/current/debuglog`. + +#### Run + +Run Certbot as normal. For example, `certbot plugins` should display the DNSimple plugin as installed. + +## Building for Other Architectures + +To build for an unavailable architecture or for multiple architectures simultaneously, we recommend using snapcraft's remote build feature. +It is easiest to run this from a local machine. + +### Initial Local Setup + + 1. Create or log into an Ubuntu One account [here](https://login.launchpad.net/). + 2. Install git and python with `sudo apt update && sudo apt install -y git python`. + 3. Install snapcraft with `sudo snap install --classic snapcraft`. + 4. `cd ~` (or any other directory where you want our source files to be) + 5. Run `git clone https://github.com/certbot/certbot` + 6. `cd certbot` (All further instructions are relative to this directory.) + 7. To trigger `snapcraft` to request access to your Launchpad account, run + `snapcraft remote-build --launchpad-accept-public-upload --status`. A URL where you need + to grant this access will be printed to your terminal and automatically open in your browser + if one is available. + +### Build Snaps Remotely + +Certbot provides a wrapper around snapcraft's remote build to make building all of our plugins easier. To see all available +options, run `python3 tools/snap/build_remote.py --help`. + +For example, to build all available snaps for all architectures, run `python3 tools/snap/build_remote.py ALL --archs amd64 arm64 armhf`. + +To build only the certbot snap on only amd64, run `python3 tools/snap/build_remote.py certbot --archs armhf`. + +The command will upload the entire contents of the working directory, so if the remote build +appears to hang, try using a clean clone of the `certbot` repository. diff --git a/tools/snap/build_remote.py b/tools/snap/build_remote.py new file mode 100755 index 00000000000..ba6f562ebc2 --- /dev/null +++ b/tools/snap/build_remote.py @@ -0,0 +1,327 @@ +#!/usr/bin/env python3 +import argparse +import datetime +import functools +import glob +from multiprocessing import Manager +from multiprocessing import Pool +from multiprocessing import Process +from multiprocessing.managers import SyncManager +import os +from os.path import basename +from os.path import dirname +from os.path import join +from os.path import realpath +import random +import re +import string +import subprocess +import sys +import tempfile +from threading import Lock +import time +from typing import Dict +from typing import List +from typing import Set +from typing import Tuple + +CERTBOT_DIR = dirname(dirname(dirname(realpath(__file__)))) +PLUGINS = [basename(path) for path in glob.glob(join(CERTBOT_DIR, 'certbot-dns-*'))] + + +# In Python, stdout and stderr are buffered in each process by default. When +# printing output from multiple processes, this can cause delays in printing +# output with lines from different processes being interleaved depending +# on when the output for that process is flushed. To prevent this, we override +# print so that it always flushes its output. Disabling output buffering can +# also be done through command line flags or environment variables set when the +# Python process starts, but this approach was taken instead to ensure +# consistent behavior regardless of how the script is invoked. +print = functools.partial(print, flush=True) + + +def _execute_build( + target: str, archs: Set[str], status: Dict[str, Dict[str, str]], + workspace: str, output_lock: Lock) -> Tuple[int, List[str]]: + # The implementation of remote-build recovery has changed over time. + # Currently, you cannot set a build-id, and the build-id is instead derived + # from a hash of the contents of the files in the directory: + # https://github.com/canonical/craft-application/blob/5b09ab3d9152a2b61ffcdf57691289023ed6ba26/craft_application/remote/utils.py#L64 + # + # We want a unique build ID so a fresh build is started for each run instead + # of potentially reusing an old build. See https://github.com/certbot/certbot/pull/8719 + # and https://github.com/snapcore/snapcraft/pull/3554 for more info. + # + # In the hope that one day you can again set a build ID, we will modify + # the directory by creating a file containing a build ID that conforms + # to the shape of snapcraft's build ID: using a MD5 hash represented as a + # 32 character hex string (we use a larger character set). + + random_string = ''.join(random.choice(string.ascii_lowercase + string.digits) + for _ in range(32)) + # place random string in build_id file inside `workspace` directory + with open(join(workspace, 'build_id'), 'w') as build_id_file: + build_id_file.write(random_string) + + with tempfile.TemporaryDirectory() as tempdir: + environ = os.environ.copy() + environ['XDG_CACHE_HOME'] = tempdir + process = subprocess.Popen([ + 'snapcraft', 'remote-build', '--launchpad-accept-public-upload', + '--build-for', ','.join(archs)], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + universal_newlines=True, env=environ, cwd=workspace) + + killed = False + process_output: List[str] = [] + for line in process.stdout: + process_output.append(line) + _extract_state(target, line, status) + + if not killed and any(state for state in status[target].values() if state == 'Chroot problem'): + # On this error the snapcraft process hangs. Let's finish it. + # + # killed is used to stop us from executing this code path + # multiple times per build that encounters "Chroot problem". + with output_lock: + print('Chroot problem encountered for build ' + f'{target} for {",".join(archs)}.\n' + 'Launchpad seems to be unable to recover from this ' + 'state so we are terminating the build.') + process.kill() + killed = True + + process_state = process.wait() + + return process_state, process_output + + +def _build_snap( + target: str, archs: Set[str], status: Dict[str, Dict[str, str]], + running: Dict[str, bool], output_lock: Lock) -> bool: + if target == 'certbot': + workspace = CERTBOT_DIR + else: + workspace = join(CERTBOT_DIR, target) + # Init and commit git repo in workspace. This is necessary starting in core24 + # as "Projects must be at the top level of a git repository" + # https://snapcraft.io/docs/migrate-core24#remote-build + subprocess.run(['git', 'init'], capture_output=True, check=True, cwd=workspace) + subprocess.run(['git', 'add', '-A'], capture_output=True, check=True, cwd=workspace) + subprocess.run(['git', 'commit', '-m', 'init'], capture_output=True, check=True, cwd=workspace) + + build_success = False + retry = 3 + while retry: + # Let's reset the status before each build so we're not starting with + # old state values. + status[target] = {arch: '...' for arch in archs} + exit_code, process_output = _execute_build(target, archs, status, workspace, output_lock) + with output_lock: + print(f'Build {target} for {",".join(archs)} (attempt {4-retry}/3) ended with ' + f'exit code {exit_code}.') + + # This output may change, and is set by + # https://github.com/canonical/snapcraft/blob/8ab7fd0c8a1d3f13045bec41a6e0158c063faa9b/snapcraft/commands/remote.py#L278 + failed_archs: set = set([arch for arch in archs if status[target][arch] != 'Succeeded']) + + # If the command failed or any architecture wasn't built + # successfully, let's try to print all the output about the problem + # that we can. + dump_output: bool = exit_code != 0 or bool(failed_archs) + + # Check snap list length + # We expect to have all target snaps available, or something bad happened. + if not dump_output: + snaps_list = glob.glob(join(workspace, '*.snap')) + if not len(snaps_list) == len(archs): + print('Some of the expected snaps for a successful build are missing ' + f'(current list: {snaps_list}).') + dump_output = True + + # Check if the snap file just contains html + if not dump_output: + for arch in archs: + snap_path_list = glob.glob(join(workspace, f'{target}_*_{arch}.snap')) + assert len(snap_path_list) == 1 + with open(snap_path_list[0], 'rb') as f: + first_block = f.read(10) + # 'hsqs' is the magic number that SquashFS files start with + if first_block.startswith(b'hsqs'): + continue + failed_archs.add(arch) + try: + first_block_str = first_block.decode() + except UnicodeDecodeError: + first_block_str = first_block.hex(sep=',') + print(f'The {target} {arch} snap file began with invalid data: {first_block_str}') + dump_output = bool(failed_archs) + + if not dump_output: + build_success = True + break + + print(f'Dumping snapcraft remote-build output build for {target}:') + print('\n'.join(process_output)) + + assert len(process_output) >= 1 + log_location = _extract_log_location(process_output[-1]) + _dump_failed_build_logs(target, log_location) + + # Retry the remote build if it has been interrupted (non zero status code) + # or if some builds have failed. + retry = retry - 1 + + running[target] = False + + return build_success + + +def _extract_log_location(line: str) -> str: + result = "" + pattern = r"^Full execution log: '(.+)'$" + match = re.match(pattern, line) + + if match: + result = match.group(1) + else: + print(f'warning: unable to extract log location, expected pattern "{pattern}", got "{line}"') + return result + + +def _extract_state(project: str, output: str, status: Dict[str, Dict[str, str]]) -> None: + state = status[project] + + # This output may change, and is set by + # https://github.com/canonical/snapcraft/blob/8ab7fd0c8a1d3f13045bec41a6e0158c063faa9b/snapcraft/commands/remote.py#L218 + if "Starting new build" in output: + for arch in state.keys(): + state[arch] = "Starting new build" + + match = re.match(r'^(\w+): (\w+)$', output) + if match: + arch = match.group(2) + state[arch] = match.group(1) + + # You need to reassign the value of status[project] here (rather than doing + # something like status[project][arch] = match.group(1)) for the state change + # to propagate to other processes. See + # https://docs.python.org/3.8/library/multiprocessing.html#proxy-objects for + # more info. + status[project] = state + + +def _dump_status_helper(archs: Set[str], status: Dict[str, Dict[str, str]]) -> None: + headers = ['project', *archs] + print(''.join(f'| {item:<25}' for item in headers)) + print(f'|{"-" * 26}' * len(headers)) + for project, states in sorted(status.items()): + print(''.join(f'| {item:<25}' for item in [project, *[states[arch] for arch in archs]])) + print(f'|{"-" * 26}' * len(headers)) + print() + + +def _dump_status( + archs: Set[str], status: Dict[str, Dict[str, str]], + running: Dict[str, bool], output_lock: Lock) -> None: + while any(running.values()): + with output_lock: + print(f'Remote build status at {datetime.datetime.now()}') + _dump_status_helper(archs, status) + time.sleep(10) + + +def _dump_failed_build_logs(target:str, build_output_path: str) -> None: + if not build_output_path: + build_output = 'Log location not extracted from output.' + else: + with open(build_output_path) as file_h: + build_output = file_h.read() + + print(f'Output for failed build target={target}') + print('-------------------------------------------') + print(build_output) + print('-------------------------------------------') + print() + + +def _dump_results(archs: Set[str], status: Dict[str, Dict[str, str]]) -> None: + print(f'Results for remote build finished at {datetime.datetime.now()}') + _dump_status_helper(archs, status) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('targets', nargs='+', choices=['ALL', 'DNS_PLUGINS', 'certbot', *PLUGINS], + help='the list of snaps to build') + parser.add_argument('--archs', nargs='+', choices=['amd64', 'arm64', 'armhf'], + default=['amd64'], help='the architectures for which snaps are built') + parser.add_argument('--timeout', type=int, default=None, + help='build process will fail after the provided timeout (in seconds)') + args = parser.parse_args() + + archs = set(args.archs) + targets = set(args.targets) + + if 'ALL' in targets: + targets.remove('ALL') + targets.update(['certbot', 'DNS_PLUGINS']) + + if 'DNS_PLUGINS' in targets: + targets.remove('DNS_PLUGINS') + targets.update(PLUGINS) + + # If we're building anything other than just Certbot, we need to + # generate the snapcraft files for the DNS plugins. + if targets != {'certbot'}: + subprocess.run(['tools/snap/generate_dnsplugins_all.sh'], + check=True, cwd=CERTBOT_DIR) + + print('Start remote snap builds...') + print(f' - archs: {", ".join(archs)}') + print(f' - projects: {", ".join(sorted(targets))}') + print() + + manager: SyncManager = Manager() + pool = Pool(processes=len(targets)) + with manager, pool: + status: Dict[str, Dict[str, str]] = manager.dict() + running = manager.dict({target: True for target in targets}) + # While multiple processes are running, this lock should be acquired + # before printing output. + output_lock = manager.Lock() + + async_results = [pool.apply_async(_build_snap, (target, archs, status, running, output_lock)) + for target in targets] + + process = Process(target=_dump_status, args=(archs, status, running, output_lock)) + process.start() + + try: + process.join(args.timeout) + + if process.is_alive(): + for target in targets: + if target == 'certbot': + workspace = CERTBOT_DIR + else: + workspace = join(CERTBOT_DIR, target) + raise ValueError(f"Timeout out reached ({args.timeout} seconds) during the build!") + + build_success = True + for async_result in async_results: + if not async_result.get(): + build_success = False + + _dump_results(archs, status) + if build_success: + print('All builds succeeded.') + else: + print('Some builds failed.') + raise ValueError("There were failures during the build!") + finally: + process.terminate() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/snap/generate_dnsplugins_all.sh b/tools/snap/generate_dnsplugins_all.sh new file mode 100755 index 00000000000..769b4ef4dee --- /dev/null +++ b/tools/snap/generate_dnsplugins_all.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# Generate all necessary files for building snaps for all DNS plugins +set -eu + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +CERTBOT_DIR="$(dirname "$(dirname "${DIR}")")" + +for PLUGIN_PATH in "${CERTBOT_DIR}"/certbot-dns-*; do + bash "${CERTBOT_DIR}"/tools/snap/generate_dnsplugins_snapcraft.sh $PLUGIN_PATH + bash "${CERTBOT_DIR}"/tools/snap/generate_dnsplugins_postrefreshhook.sh $PLUGIN_PATH + # Create constraints file + cp "${CERTBOT_DIR}"/tools/requirements.txt "${PLUGIN_PATH}"/snap-constraints.txt +done diff --git a/tools/snap/generate_dnsplugins_postrefreshhook.sh b/tools/snap/generate_dnsplugins_postrefreshhook.sh new file mode 100755 index 00000000000..cdccc3d9514 --- /dev/null +++ b/tools/snap/generate_dnsplugins_postrefreshhook.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# Generate the hooks/post-refresh file for a DNS plugin +# Usage: bash generate_dnsplugins_postrefreshhook.sh path/to/dns/plugin +# For example, from the certbot home directory: +# tools/snap/generate_dnsplugins_postrefreshhook.sh certbot-dns-dnsimple +set -eu + +PLUGIN_PATH=$1 +mkdir -p "${PLUGIN_PATH}/snap/hooks" +cat < "${PLUGIN_PATH}/snap/hooks/post-refresh" +#!/bin/sh -e +# This file is generated automatically and should not be edited manually. + +# get certbot version +if [ ! -f "\$SNAP/certbot-shared/certbot-version.txt" ]; then + echo "No certbot version available; not doing version comparison check" >> "\$SNAP_DATA/debuglog" + exit 0 +fi +cb_installed=\$(cat \$SNAP/certbot-shared/certbot-version.txt) + +# get required certbot version for plugin. certbot version must be at least the plugin's +# version. note that this is not the required version in setup.py, but the version number itself. +cb_required=\$(grep -oP "version = '\K.*(?=')" \$SNAP/setup.py) + + +\$SNAP/bin/python3 -c "import sys; from packaging import version; sys.exit(1) if\ + version.parse('\$cb_installed') < version.parse('\$cb_required') else sys.exit(0)" || exit_code=\$? +if [ "\$exit_code" = "1" ]; then + echo "Certbot is version \$cb_installed but needs to be at least \$cb_required before" \\ + "this plugin can be updated; will try again on next refresh." + exit 1 +fi +EOF diff --git a/tools/snap/generate_dnsplugins_snapcraft.sh b/tools/snap/generate_dnsplugins_snapcraft.sh new file mode 100755 index 00000000000..3730415734b --- /dev/null +++ b/tools/snap/generate_dnsplugins_snapcraft.sh @@ -0,0 +1,71 @@ +#!/bin/bash +# Generate the snapcraft.yaml file for a DNS plugins +# Usage: bash generate_dnsplugins_snapcraft.sh path/to/dns/plugin +# For example, from the certbot home directory: +# tools/snap/generate_dnsplugins_snapcraft.sh certbot-dns-dnsimple +set -e + +PLUGIN_PATH=$1 +PLUGIN=$(basename "${PLUGIN_PATH}") +DESCRIPTION=$(sed -E -n "/^description = / s/^description = ['\"](.*)['\"]/\1/ p" "${PLUGIN_PATH}/pyproject.toml") +mkdir -p "${PLUGIN_PATH}/snap" +cat < "${PLUGIN_PATH}/snap/snapcraft.yaml" +# This file is generated automatically and should not be edited manually. +name: ${PLUGIN} +summary: ${DESCRIPTION} +description: ${DESCRIPTION} +confinement: strict +grade: stable +base: core24 +adopt-info: ${PLUGIN} + +parts: + ${PLUGIN}: + plugin: python + source: . + override-pull: | + craftctl default + craftctl set version=\$(grep ^version \$SNAPCRAFT_PART_SRC/setup.py | cut -f2 -d= | tr -d "'[:space:]") + build-environment: + # We set this environment variable while building to try and increase the + # stability of fetching the rust crates needed to build the cryptography + # library. + - CARGO_NET_GIT_FETCH_WITH_CLI: "true" + # Constraints are passed through the environment variable PIP_CONSTRAINTS instead of using the + # parts.[part_name].constraints option available in snapcraft.yaml when the Python plugin is + # used. This is done to let these constraints be applied not only on the certbot package + # build, but also on any isolated build that pip could trigger when building wheels for + # dependencies. See https://github.com/certbot/certbot/pull/8443 for more info. + - PIP_CONSTRAINT: \$SNAPCRAFT_PART_SRC/snap-constraints.txt + - SNAP_BUILD: "True" + # To build cryptography and cffi if needed + build-packages: + - gcc + - git + - build-essential + - libssl-dev + - libffi-dev + - python3-dev + - cargo + - pkg-config + certbot-metadata: + plugin: dump + source: . + stage: [setup.py, certbot-shared] + override-pull: | + craftctl default + mkdir -p \$SNAPCRAFT_PART_SRC/certbot-shared + +slots: + certbot: + interface: content + content: certbot-1 + read: + - \$SNAP/lib/python3.12/site-packages + +plugs: + certbot-metadata: + interface: content + content: metadata-1 + target: \$SNAP/certbot-shared +EOF diff --git a/tools/sphinx-quickstart.sh b/tools/sphinx-quickstart.sh index 72dc9e200ef..8ac01435946 100755 --- a/tools/sphinx-quickstart.sh +++ b/tools/sphinx-quickstart.sh @@ -7,15 +7,16 @@ fi PROJECT=$1 -yes "n" | sphinx-quickstart --dot _ --project $PROJECT --author "Certbot Project" -v 0 --release 0 --language en --suffix .rst --master index --ext-autodoc --ext-intersphinx --ext-todo --ext-coverage --ext-viewcode --makefile --batchfile $PROJECT/docs +yes "n" | sphinx-quickstart --dot _ --project $PROJECT --author "Certbot Project" -v 0 --release 0 --language en --suffix .rst --master index --ext-autodoc --ext-intersphinx --ext-todo --ext-coverage --ext-viewcode --extensions sphinx_rtd_theme --makefile --batchfile $PROJECT/docs cd $PROJECT/docs -sed -i -e "s|\# import os|import os|" conf.py sed -i -e "s|\# needs_sphinx = '1.0'|needs_sphinx = '1.0'|" conf.py -sed -i -e "s|intersphinx_mapping = {'https://docs.python.org/': None}|intersphinx_mapping = {\n 'python': ('https://docs.python.org/', None),\n 'acme': ('https://acme-python.readthedocs.org/en/latest/', None),\n 'certbot': ('https://certbot.eff.org/docs/', None),\n}|" conf.py -sed -i -e "s|html_theme = 'alabaster'|\n# http://docs.readthedocs.org/en/latest/theme.html#how-do-i-use-this-locally-and-on-read-the-docs\n# on_rtd is whether we are on readthedocs.org\non_rtd = os.environ.get('READTHEDOCS', None) == 'True'\nif not on_rtd: # only import and set the theme if we're building docs locally\n import sphinx_rtd_theme\n html_theme = 'sphinx_rtd_theme'\n html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]\n# otherwise, readthedocs.org uses their theme by default, so no need to specify it|" conf.py -sed -i -e "s|# Add any paths that contain templates here, relative to this directory.|autodoc_member_order = 'bysource'\nautodoc_default_flags = ['show-inheritance', 'private-members']\n\n# Add any paths that contain templates here, relative to this directory.|" conf.py +sed -i -e "s|intersphinx_mapping = {'https://docs.python.org/': None}|intersphinx_mapping = {\n 'python': ('https://docs.python.org/', None),\n 'acme': ('https://acme-python.readthedocs.io/en/latest/', None),\n 'certbot': ('https://eff-certbot.readthedocs.io/en/stable/', None),\n}|" conf.py +sed -i -e "s|html_theme = 'alabaster'|html_theme = 'sphinx_rtd_theme'|" conf.py +sed -i -e "s|# Add any paths that contain templates here, relative to this directory.|autodoc_member_order = 'bysource'\nautodoc_default_flags = ['show-inheritance']\n\n# Add any paths that contain templates here, relative to this directory.|" conf.py sed -i -e "s|# The name of the Pygments (syntax highlighting) style to use.|default_role = 'py:obj'\n\n# The name of the Pygments (syntax highlighting) style to use.|" conf.py +# If the --ext-todo flag is removed from sphinx-quickstart, the line below can be removed. +sed -i -e "s|todo_include_todos = True|todo_include_todos = False|" conf.py echo "/_build/" >> .gitignore echo "================= API Documentation diff --git a/tools/venv.py b/tools/venv.py index 93b012e760b..668689ee122 100755 --- a/tools/venv.py +++ b/tools/venv.py @@ -1,15 +1,32 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Developer virtualenv setup for Certbot client -import os +"""Aids in creating a developer virtual environment for Certbot. + +When this module is run as a script, it takes the arguments that should +be passed to pip to install the Certbot packages as command line +arguments. If no arguments are provided, all Certbot packages and their +development dependencies are installed. The virtual environment will be +created with the name "venv" in the current working directory. You can +change the name of the virtual environment by setting the environment +variable VENV_NAME. -import _venv_common +""" + +from __future__ import print_function + +import glob +import os +import re +import shutil +import subprocess +import sys +import time REQUIREMENTS = [ - '-e acme[dev]', - '-e .[dev,docs]', + '-e acme[test]', + '-e certbot[all]', '-e certbot-apache', '-e certbot-dns-cloudflare', - '-e certbot-dns-cloudxns', '-e certbot-dns-digitalocean', '-e certbot-dns-dnsimple', '-e certbot-dns-dnsmadeeasy', @@ -23,19 +40,203 @@ '-e certbot-dns-route53', '-e certbot-dns-sakuracloud', '-e certbot-nginx', - '-e certbot-postfix', - '-e letshelp-certbot', '-e certbot-compatibility-test', + '-e certbot-ci', + '-e letstest', ] +if sys.platform == 'win32': + REQUIREMENTS.remove('-e certbot-apache') + REQUIREMENTS.remove('-e certbot-compatibility-test') + +VERSION_PATTERN = re.compile(r'^(\d+)\.(\d+).*$') + + +class PythonExecutableNotFoundError(Exception): + pass + + +def find_python_executable() -> str: + """ + Find the relevant python executable that is of the given python major version. + Will test, in decreasing priority order: + + * the current Python interpreter + * 'pythonX' executable in PATH (with X the given major version) if available + * 'python' executable in PATH if available + * Windows Python launcher 'py' executable in PATH if available + + Incompatible python versions for Certbot will be evicted (e.g. Python 3 + versions less than 3.7). + + :rtype: str + :return: the relevant python executable path + :raise RuntimeError: if no relevant python executable path could be found + """ + python_executable_path = None + + # First try, current python executable + if _check_version('{0}.{1}.{2}'.format( + sys.version_info[0], sys.version_info[1], sys.version_info[2])): + return sys.executable + + # Second try, with python executables in path + for one_version in ('3', '',): + try: + one_python = 'python{0}'.format(one_version) + output = subprocess.check_output([one_python, '--version'], + universal_newlines=True, stderr=subprocess.STDOUT) + if _check_version(output.strip().split()[1]): + return subprocess.check_output([one_python, '-c', + 'import sys; sys.stdout.write(sys.executable);'], + universal_newlines=True) + except (subprocess.CalledProcessError, OSError): + pass + + # Last try, with Windows Python launcher + try: + output_version = subprocess.check_output(['py', '-3', '--version'], + universal_newlines=True, stderr=subprocess.STDOUT) + if _check_version(output_version.strip().split()[1]): + return subprocess.check_output(['py', env_arg, '-c', + 'import sys; sys.stdout.write(sys.executable);'], + universal_newlines=True) + except (subprocess.CalledProcessError, OSError): + pass + + if not python_executable_path: + raise RuntimeError('Error, no compatible Python executable for Certbot could be found.') + + +def _check_version(version_str): + search = VERSION_PATTERN.search(version_str) + + if not search: + return False + + version = (int(search.group(1)), int(search.group(2))) + + if version >= (3, 10): + return True + + print('Incompatible python version for Certbot found: {0}'.format(version_str)) + return False + + +def subprocess_with_print(cmd, env=None, shell=False): + if env is None: + env = os.environ + print('+ {0}'.format(subprocess.list2cmdline(cmd)) if isinstance(cmd, list) else cmd) + subprocess.check_call(cmd, env=env, shell=shell) + + +def subprocess_output_with_print(cmd, env=None, shell=False): + if env is None: + env = os.environ + print('+ {0}'.format(subprocess.list2cmdline(cmd)) if isinstance(cmd, list) else cmd) + return subprocess.check_output(cmd, env=env, shell=shell) + + +def get_venv_python_path(venv_path): + python_linux = os.path.join(venv_path, 'bin/python') + if os.path.isfile(python_linux): + return os.path.abspath(python_linux) + python_windows = os.path.join(venv_path, 'Scripts\\python.exe') + if os.path.isfile(python_windows): + return os.path.abspath(python_windows) + + raise ValueError(( + 'Error, could not find python executable in venv path {0}: is it a valid venv ?' + .format(venv_path))) + + +def prepare_venv_path(venv_name): + """Determines the venv path and prepares it for use. + + This function cleans up any Python eggs in the current working directory + and ensures the venv path is available for use. The path used is the + VENV_NAME environment variable if it is set and venv_name otherwise. If + there is already a directory at the desired path, the existing directory is + renamed by appending a timestamp to the directory name. + + :param str venv_name: The name or path at where the virtual + environment should be created if VENV_NAME isn't set. + + :returns: path where the virtual environment should be created + :rtype: str + + """ + for path in glob.glob('*.egg-info'): + if os.path.isdir(path): + shutil.rmtree(path) + else: + os.remove(path) + + env_venv_name = os.environ.get('VENV_NAME') + if env_venv_name: + print('Creating venv at {0}' + ' as specified in VENV_NAME'.format(env_venv_name)) + venv_name = env_venv_name + + if os.path.isdir(venv_name): + os.rename(venv_name, '{0}.{1}.bak'.format(venv_name, int(time.time()))) + + return venv_name + + +def install_packages(venv_name, pip_args): + """Installs packages in the given venv. + + :param str venv_name: The name or path at where the virtual + environment should be created. + :param pip_args: Command line arguments that should be given to + pip to install packages + :type pip_args: `list` of `str` + + """ + # Using the python executable from venv, we ensure to execute following commands in this venv. + py_venv = get_venv_python_path(venv_name) + command = [py_venv, os.path.abspath('tools/pip_install.py')] + command.extend(pip_args) + subprocess_with_print(command) + + if os.path.isdir(os.path.join(venv_name, 'bin')): + # Linux/OSX specific + print('-------------------------------------------------------------------') + print('Please run the following command to activate developer environment:') + print('source {0}/bin/activate'.format(venv_name)) + print('-------------------------------------------------------------------') + elif os.path.isdir(os.path.join(venv_name, 'Scripts')): + # Windows specific + print('---------------------------------------------------------------------------') + print('Please run one of the following commands to activate developer environment:') + print('{0}\\Scripts\\activate.bat (for Batch)'.format(venv_name)) + print('.\\{0}\\Scripts\\Activate.ps1 (for Powershell)'.format(venv_name)) + print('---------------------------------------------------------------------------') + else: + raise ValueError('Error, directory {0} is not a valid venv.'.format(venv_name)) + + +def create_venv(venv_path): + """Create a Python virtual environment at venv_path. + + :param str venv_path: path where the venv should be created + + """ + python = find_python_executable() + command = [python, '-m', 'venv', venv_path] + subprocess_with_print(command) + + +def main(pip_args=None): + venv_path = prepare_venv_path('venv') + create_venv(venv_path) -def main(): - if os.name == 'nt': - raise ValueError('Certbot for Windows is not supported on Python 2.x.') + if not pip_args: + pip_args = REQUIREMENTS - venv_args = '--python "{0}"'.format(_venv_common.find_python_executable(2)) - _venv_common.main('venv', venv_args, REQUIREMENTS) + install_packages(venv_path, pip_args) if __name__ == '__main__': - main() + main(sys.argv[1:]) diff --git a/tools/venv3.py b/tools/venv3.py deleted file mode 100755 index c2374ba5ab9..00000000000 --- a/tools/venv3.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python -# Developer virtualenv setup for Certbot client -import _venv_common - -REQUIREMENTS = [ - '-e acme[dev]', - '-e .[dev,docs]', - '-e certbot-apache', - '-e certbot-dns-cloudflare', - '-e certbot-dns-cloudxns', - '-e certbot-dns-digitalocean', - '-e certbot-dns-dnsimple', - '-e certbot-dns-dnsmadeeasy', - '-e certbot-dns-gehirn', - '-e certbot-dns-google', - '-e certbot-dns-linode', - '-e certbot-dns-luadns', - '-e certbot-dns-nsone', - '-e certbot-dns-ovh', - '-e certbot-dns-rfc2136', - '-e certbot-dns-route53', - '-e certbot-dns-sakuracloud', - '-e certbot-nginx', - '-e certbot-postfix', - '-e letshelp-certbot', - '-e certbot-compatibility-test', -] - - -def main(): - venv_args = '--python "{0}"'.format(_venv_common.find_python_executable(3)) - _venv_common.main('venv3', venv_args, REQUIREMENTS) - - -if __name__ == '__main__': - main() diff --git a/towncrier.toml b/towncrier.toml new file mode 100644 index 00000000000..c9feac9bcad --- /dev/null +++ b/towncrier.toml @@ -0,0 +1,25 @@ +[tool.towncrier] +package = "certbot" +directory = "newsfragments" +filename = "certbot/CHANGELOG.md" +title_format = "## {version} - {project_date}" +issue_format = "[#{issue}](https://github.com/certbot/certbot/issues/{issue})" +issue_pattern = "\\d+" +ignore = [] + +# Our three custom news fragment types, declared in order of how we'd like them +# to appear in the changelog +[[tool.towncrier.type]] +directory = "added" +name = "Added" +showcontent = true + +[[tool.towncrier.type]] +directory = "changed" +name = "Changed" +showcontent = true + +[[tool.towncrier.type]] +directory = "fixed" +name = "Fixed" +showcontent = true diff --git a/tox.cover.py b/tox.cover.py deleted file mode 100755 index c8a45d82daa..00000000000 --- a/tox.cover.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python -import argparse -import subprocess -import os -import sys - -DEFAULT_PACKAGES = [ - 'certbot', 'acme', 'certbot_apache', 'certbot_dns_cloudflare', 'certbot_dns_cloudxns', - 'certbot_dns_digitalocean', 'certbot_dns_dnsimple', 'certbot_dns_dnsmadeeasy', - 'certbot_dns_gehirn', 'certbot_dns_google', 'certbot_dns_linode', 'certbot_dns_luadns', - 'certbot_dns_nsone', 'certbot_dns_ovh', 'certbot_dns_rfc2136', 'certbot_dns_route53', - 'certbot_dns_sakuracloud', 'certbot_nginx', 'certbot_postfix', 'letshelp_certbot'] - -COVER_THRESHOLDS = { - 'certbot': {'linux': 98, 'windows': 94}, - 'acme': {'linux': 100, 'windows': 99}, - 'certbot_apache': {'linux': 100, 'windows': 100}, - 'certbot_dns_cloudflare': {'linux': 98, 'windows': 98}, - 'certbot_dns_cloudxns': {'linux': 99, 'windows': 99}, - 'certbot_dns_digitalocean': {'linux': 98, 'windows': 98}, - 'certbot_dns_dnsimple': {'linux': 98, 'windows': 98}, - 'certbot_dns_dnsmadeeasy': {'linux': 99, 'windows': 99}, - 'certbot_dns_gehirn': {'linux': 97, 'windows': 97}, - 'certbot_dns_google': {'linux': 99, 'windows': 99}, - 'certbot_dns_linode': {'linux': 98, 'windows': 98}, - 'certbot_dns_luadns': {'linux': 98, 'windows': 98}, - 'certbot_dns_nsone': {'linux': 99, 'windows': 99}, - 'certbot_dns_ovh': {'linux': 97, 'windows': 97}, - 'certbot_dns_rfc2136': {'linux': 99, 'windows': 99}, - 'certbot_dns_route53': {'linux': 92, 'windows': 92}, - 'certbot_dns_sakuracloud': {'linux': 97, 'windows': 97}, - 'certbot_nginx': {'linux': 97, 'windows': 97}, - 'certbot_postfix': {'linux': 100, 'windows': 100}, - 'letshelp_certbot': {'linux': 100, 'windows': 100} -} - -SKIP_PROJECTS_ON_WINDOWS = [ - 'certbot-apache', 'certbot-nginx', 'certbot-postfix', 'letshelp-certbot'] - -def cover(package): - threshold = COVER_THRESHOLDS.get(package)['windows' if os.name == 'nt' else 'linux'] - - pkg_dir = package.replace('_', '-') - - if os.name == 'nt' and pkg_dir in SKIP_PROJECTS_ON_WINDOWS: - print(( - 'Info: currently {0} is not supported on Windows and will not be tested/covered.' - .format(pkg_dir))) - return - - subprocess.check_call([ - sys.executable, '-m', 'pytest', '--cov', pkg_dir, '--cov-append', '--cov-report=', package]) - subprocess.check_call([ - sys.executable, '-m', 'coverage', 'report', '--fail-under', str(threshold), '--include', - '{0}/*'.format(pkg_dir), '--show-missing']) - -def main(): - description = """ -This script is used by tox.ini (and thus by Travis CI and AppVeyor) in order -to generate separate stats for each package. It should be removed once those -packages are moved to a separate repo. - -Option -e makes sure we fail fast and don't submit to codecov.""" - parser = argparse.ArgumentParser(description=description) - parser.add_argument('--packages', nargs='+') - - args = parser.parse_args() - - packages = args.packages or DEFAULT_PACKAGES - - # --cov-append is on, make sure stats are correct - try: - os.remove('.coverage') - except OSError: - pass - - for package in packages: - cover(package) - -if __name__ == '__main__': - main() diff --git a/tox.ini b/tox.ini index 95b2f2c649a..94ffdac7bf1 100644 --- a/tox.ini +++ b/tox.ini @@ -1,252 +1,312 @@ -# Tox (http://tox.testrun.org/) is a tool for running tests in -# multiple virtualenvs. To use it, "pip install tox" and then run -# "tox" from this directory. - [tox] +envlist = {cover,lint}-posix,mypy + skipsdist = true -envlist = modification,py3,py27-cover,lint,mypy [base] -# pip installs the requested packages in editable mode -pip_install = python {toxinidir}/tools/pip_install_editable.py -# pip installs the requested packages in editable mode and runs unit tests on -# them. Each package is installed and tested in the order they are provided -# before the script moves on to the next package. All dependencies are pinned -# to a specific version for increased stability for developers. -install_and_test = python {toxinidir}/tools/install_and_test.py -dns_packages = - certbot-dns-cloudflare \ - certbot-dns-cloudxns \ - certbot-dns-digitalocean \ - certbot-dns-dnsimple \ - certbot-dns-dnsmadeeasy \ - certbot-dns-gehirn \ - certbot-dns-google \ - certbot-dns-linode \ - certbot-dns-luadns \ - certbot-dns-nsone \ - certbot-dns-ovh \ - certbot-dns-rfc2136 \ - certbot-dns-route53 \ - certbot-dns-sakuracloud -all_packages = - acme[dev] \ - .[dev] \ - certbot-apache \ - {[base]dns_packages} \ - certbot-nginx \ - certbot-postfix \ - letshelp-certbot -install_packages = - python {toxinidir}/tools/pip_install_editable.py {[base]all_packages} -source_paths = - acme/acme - certbot - certbot-apache/certbot_apache - certbot-compatibility-test/certbot_compatibility_test - certbot-dns-cloudflare/certbot_dns_cloudflare - certbot-dns-cloudxns/certbot_dns_cloudxns - certbot-dns-digitalocean/certbot_dns_digitalocean - certbot-dns-dnsimple/certbot_dns_dnsimple - certbot-dns-dnsmadeeasy/certbot_dns_dnsmadeeasy - certbot-dns-gehirn/certbot_dns_gehirn - certbot-dns-google/certbot_dns_google - certbot-dns-linode/certbot_dns_linode - certbot-dns-luadns/certbot_dns_luadns - certbot-dns-nsone/certbot_dns_nsone - certbot-dns-ovh/certbot_dns_ovh - certbot-dns-rfc2136/certbot_dns_rfc2136 - certbot-dns-route53/certbot_dns_route53 - certbot-dns-sakuracloud/certbot_dns_sakuracloud - certbot-nginx/certbot_nginx - certbot-postfix/certbot_postfix - letshelp-certbot/letshelp_certbot - tests/lock_test.py +pytest = python -m pytest {posargs} +# Paths are listed on one line because tox seems to have inconsistent +# behavior with substitutions that contain line continuations, see +# https://github.com/tox-dev/tox/issues/2069 for more info. +mypy_strict_source_paths = certbot-ci/src certbot-dns-cloudflare/src certbot-dns-digitalocean/src certbot-dns-dnsimple/src certbot-dns-dnsmadeeasy/src certbot-dns-gehirn/src certbot-dns-google/src certbot-dns-linode/src certbot-dns-luadns/src certbot-dns-nsone/src certbot-dns-ovh/src certbot-dns-rfc2136/src certbot-dns-route53/src certbot-dns-sakuracloud/src +mypy_no_strict_source_paths = acme/src certbot/src certbot-apache/src certbot-compatibility-test/src certbot-nginx/src +source_paths = {[base]mypy_strict_source_paths} {[base]mypy_no_strict_source_paths} [testenv] -commands = - {[base]install_and_test} {[base]all_packages} - python tests/lock_test.py +platform = + posix: ^(?!.*win32).*$ setenv = + PYTEST_ADDOPTS = {env:PYTEST_ADDOPTS:--numprocesses auto} PYTHONHASHSEED = 0 - -[testenv:py27-oldest] +# The default install command is python -I -m pip install {opts} {packages} +install_command = python -I {toxinidir}/tools/pip_install.py {opts} {packages} +deps = + -e acme[test] + -e certbot + -e certbot[test] + -e certbot-apache + -e certbot-dns-cloudflare + -e certbot-dns-digitalocean + -e certbot-dns-dnsimple + -e certbot-dns-dnsmadeeasy + -e certbot-dns-gehirn + -e certbot-dns-google + -e certbot-dns-linode + -e certbot-dns-luadns + -e certbot-dns-nsone + -e certbot-dns-ovh + -e certbot-dns-rfc2136 + -e certbot-dns-route53 + -e certbot-dns-sakuracloud + -e certbot-nginx +allowlist_externals = + echo + false +# This and the next few testenvs are a workaround for +# https://github.com/tox-dev/tox/issues/2858. commands = - {[testenv]commands} -setenv = - {[testenv]setenv} - CERTBOT_OLDEST=1 + echo "Unrecognized environment name {envname}" + false -[testenv:py27-acme-oldest] +[testenv:py{,-posix}] commands = - {[base]install_and_test} acme[dev] -setenv = - {[testenv:py27-oldest]setenv} + {[base]pytest} acme certbot certbot-dns-cloudflare certbot-dns-digitalocean certbot-dns-dnsimple certbot-dns-dnsmadeeasy certbot-dns-gehirn certbot-dns-google certbot-dns-linode certbot-dns-luadns certbot-dns-nsone certbot-dns-ovh certbot-dns-rfc2136 certbot-dns-route53 certbot-dns-sakuracloud -[testenv:py27-apache-oldest] -commands = - {[base]install_and_test} certbot-apache -setenv = - {[testenv:py27-oldest]setenv} +[testenv:py3{,10,11,12,13,14}] +commands = {[testenv:py]commands} -[testenv:py27-certbot-oldest] -commands = - {[base]install_and_test} .[dev] -setenv = - {[testenv:py27-oldest]setenv} +[testenv:py3.{10,11,12,13,14}] +commands = {[testenv:py]commands} -[testenv:py27-dns-oldest] -commands = - {[base]install_and_test} {[base]dns_packages} +[testenv:oldest] +# Setting basepython allows the tests to fail fast if that version of Python +# isn't available instead of potentially trying to use a newer version of +# Python which is unlikely to work. +# +# This version should be kept in sync with the one declared in +# tools/pinning/oldest/pyproject.toml. +basepython = python3.10 setenv = - {[testenv:py27-oldest]setenv} + {[testenv:py]setenv} + CERTBOT_OLDEST=1 +commands = {[testenv:py]commands} -[testenv:py27-nginx-oldest] +[testenv:cover] +coverage_report = python -m coverage report +common_coverage_report_commands = + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-cloudflare/* + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-digitalocean/* + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-dnsimple/* + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-dnsmadeeasy/* + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-gehirn/* + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-google/* + {[testenv:cover]coverage_report} --fail-under 100 --include certbot-dns-linode/* + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-luadns/* + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-nsone/* + {[testenv:cover]coverage_report} --fail-under 98 --include certbot-dns-ovh/* + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-rfc2136/* + {[testenv:cover]coverage_report} --fail-under 94 --include certbot-dns-route53/* + {[testenv:cover]coverage_report} --fail-under 99 --include certbot-dns-sakuracloud/* commands = - {[base]install_and_test} certbot-nginx - python tests/lock_test.py -setenv = - {[testenv:py27-oldest]setenv} + {[testenv:py]commands} --cov --cov-report= + {[testenv:cover]coverage_report} --fail-under 100 --include acme/* + {[testenv:cover]coverage_report} --fail-under 96 --include certbot/* + {[testenv:cover]common_coverage_report_commands} -[testenv:py27-postfix-oldest] -commands = - {[base]install_and_test} certbot-postfix -setenv = - {[testenv:py27-oldest]setenv} +# Another workaround for https://github.com/tox-dev/tox/issues/2858 in tox v4. +[testenv:cover-posix] +commands = {[testenv:cover]commands} -[testenv:py27_install] -basepython = python2.7 +[testenv:lint{,-posix}] commands = - {[base]install_packages} + python -m pylint --reports=n --rcfile=.pylintrc {[base]source_paths} + ruff check -[testenv:py27-cover] -basepython = python2.7 +[testenv:mypy] +deps = + {[testenv]deps} + -e certbot-ci commands = - {[base]install_packages} - python tox.cover.py + mypy {[base]mypy_no_strict_source_paths} + mypy {[base]mypy_strict_source_paths} --strict -[testenv:py37-cover] -basepython = python3.7 -commands = - {[base]install_packages} - python tox.cover.py +[testenv:isolated-acme] +description = Tests acme without any Certbot components installed +deps = -e acme[test] +commands = {[base]pytest} acme -[testenv:lint] -basepython = python2.7 -# separating into multiple invocations disables cross package -# duplicate code checking; if one of the commands fails, others will -# continue, but tox return code will reflect previous error -commands = - {[base]install_packages} - python -m pylint --reports=n --rcfile=.pylintrc {[base]source_paths} +[testenv:isolated-certbot] +description = Tests Certbot without any additional plugins installed +commands = {[base]pytest} certbot +deps = + {[testenv:isolated-acme]deps} + -e certbot[test] -[testenv:mypy] -basepython = python3 +[testenv:isolated-{apache,cloudflare,digitalocean,dnsimple,dnsmadeeasy,gehirn,google,linode,luadns,nsone,ovh,rfc2136,route53,sakuracloud,nginx}] +description = Tests the plugin without installing any other plugins +deps = + {[testenv:isolated-certbot]deps} + apache: -e certbot-apache + cloudflare: -e certbot-dns-cloudflare + digitalocean: -e certbot-dns-digitalocean + dnsimple: -e certbot-dns-dnsimple + dnsmadeeasy: -e certbot-dns-dnsmadeeasy + gehirn: -e certbot-dns-gehirn + google: -e certbot-dns-google + linode: -e certbot-dns-linode + luadns: -e certbot-dns-luadns + nsone: -e certbot-dns-nsone + ovh: -e certbot-dns-ovh + rfc2136: -e certbot-dns-rfc2136 + route53: -e certbot-dns-route53 + sakuracloud: -e certbot-dns-sakuracloud + nginx: -e certbot-nginx commands = - {[base]install_packages} - {[base]pip_install} .[dev3] - mypy {[base]source_paths} + apache: {[base]pytest} certbot/src/certbot/_internal/tests/plugins/apache + cloudflare: {[base]pytest} certbot-dns-cloudflare + digitalocean: {[base]pytest} certbot-dns-digitalocean + dnsimple: {[base]pytest} certbot-dns-dnsimple + dnsmadeeasy: {[base]pytest} certbot-dns-dnsmadeeasy + gehirn: {[base]pytest} certbot-dns-gehirn + google: {[base]pytest} certbot-dns-google + linode: {[base]pytest} certbot-dns-linode + luadns: {[base]pytest} certbot-dns-luadns + nsone: {[base]pytest} certbot-dns-nsone + ovh: {[base]pytest} certbot-dns-ovh + rfc2136: {[base]pytest} certbot-dns-rfc2136 + route53: {[base]pytest} certbot-dns-route53 + sakuracloud: {[base]pytest} certbot-dns-sakuracloud + nginx: {[base]pytest} certbot/src/certbot/_internal/tests/plugins/nginx [testenv:apacheconftest] -#basepython = python2.7 +deps = + -e acme + -e certbot + -e certbot-apache commands = - {[base]pip_install} acme . certbot-apache certbot-compatibility-test - {toxinidir}/certbot-apache/certbot_apache/tests/apache-conf-files/apache-conf-test --debian-modules + {toxinidir}/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test --debian-modules passenv = SERVER +allowlist_externals = + {toxinidir}/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test -[testenv:apacheconftest-with-pebble] +[testenv:apacheconftest-external-with-pebble] +description = Run apacheconftest with pebble and Certbot outside of the tox virtual environment. +deps = + -e certbot-ci +allowlist_externals = + {toxinidir}/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test-pebble.py commands = - {toxinidir}/tests/pebble-fetch.sh - {[testenv:apacheconftest]commands} -passenv = - HOME - GOPATH - PEBBLEPATH - PEBBLE_STRICT -setenv = - SERVER=https://localhost:14000/dir + {toxinidir}/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test-pebble.py --debian-modules + +[testenv:apacheconftest-with-pebble] +deps = + {[testenv:apacheconftest]deps} + {[testenv:apacheconftest-external-with-pebble]deps} +allowlist_externals = + {toxinidir}/certbot/src/certbot/_internal/tests/plugins/apache/apache-conf-files/apache-conf-test-pebble.py +commands = {[testenv:apacheconftest-external-with-pebble]commands} [testenv:nginxroundtrip] +deps = + -e acme + -e certbot + -e certbot-apache + -e certbot-nginx commands = - {[base]pip_install} acme . certbot-apache certbot-nginx python certbot-compatibility-test/nginx/roundtrip.py certbot-compatibility-test/nginx/nginx-roundtrip-testdata -# This is a duplication of the command line in testenv:le_auto to -# allow users to run the modification check by running `tox` [testenv:modification] +deps = commands = python {toxinidir}/tests/modification-check.py [testenv:apache_compat] +deps = commands = docker build -t certbot-compatibility-test -f certbot-compatibility-test/Dockerfile . docker build -t apache-compat -f certbot-compatibility-test/Dockerfile-apache . - docker run --rm -it apache-compat -c apache.tar.gz -vvvv -whitelist_externals = + docker run --rm -t apache-compat -c apache.tar.gz -vvvv +allowlist_externals = docker passenv = DOCKER_* [testenv:nginx_compat] +deps = commands = docker build -t certbot-compatibility-test -f certbot-compatibility-test/Dockerfile . docker build -t nginx-compat -f certbot-compatibility-test/Dockerfile-nginx . - docker run --rm -it nginx-compat -c nginx.tar.gz -vv -aie -whitelist_externals = + docker run --rm -t nginx-compat -c nginx.tar.gz -vv -aie +allowlist_externals = docker passenv = DOCKER_* -[testenv:le_auto_precise] -# At the moment, this tests under Python 2.7 only, as only that version is -# readily available on the Precise Docker image. +[testenv:integration] +deps = + -e acme + -e certbot + -e certbot-nginx + -e certbot-ci commands = - docker build -f letsencrypt-auto-source/Dockerfile.precise -t lea letsencrypt-auto-source - docker run --rm -t -i lea -whitelist_externals = - docker -passenv = - DOCKER_* + {[base]pytest} certbot-ci/src/certbot_integration_tests \ + --cov=acme --cov=certbot --cov=certbot_nginx --cov-report= \ + --cov-config=certbot-ci/src/certbot_integration_tests/.coveragerc + coverage report --include 'certbot/*' --show-missing --fail-under=71 +passenv = DOCKER_* -[testenv:le_auto_trusty] -# At the moment, this tests under Python 2.7 only, as only that version is -# readily available on the Trusty Docker image. +[testenv:integration-certbot] +deps = + -e acme + -e certbot + -e certbot-ci commands = - python {toxinidir}/tests/modification-check.py - docker build -f letsencrypt-auto-source/Dockerfile.trusty -t lea letsencrypt-auto-source - docker run --rm -t -i lea -whitelist_externals = - docker -passenv = - DOCKER_* - TRAVIS_BRANCH + {[base]pytest} certbot-ci/src/certbot_integration_tests/certbot_tests \ + --cov=acme --cov=certbot --cov-report= \ + --cov-config=certbot-ci/src/certbot_integration_tests/.coveragerc + coverage report --include 'certbot/*' --show-missing --fail-under=62 -[testenv:le_auto_wheezy] -# At the moment, this tests under Python 2.7 only, as only that version is -# readily available on the Wheezy Docker image. +[testenv:integration-dns-rfc2136] +deps = + -e acme + -e certbot + -e certbot-dns-rfc2136 + -e certbot-ci commands = - docker build -f letsencrypt-auto-source/Dockerfile.wheezy -t lea letsencrypt-auto-source - docker run --rm -t -i lea -whitelist_externals = - docker + {[base]pytest} certbot-ci/src/certbot_integration_tests/rfc2136_tests \ + --dns-server=bind \ + --numprocesses=1 \ + --cov=acme --cov=certbot --cov=certbot_dns_rfc2136 --cov-report= \ + --cov-config=certbot-ci/src/certbot_integration_tests/.coveragerc + coverage report --include 'certbot/*' --show-missing --fail-under=45 + coverage report --include 'certbot-dns-rfc2136/*' --show-missing --fail-under=86 + +[testenv:integration-external] +description = Run integration tests with Certbot outside of the tox virtual environment. +deps = + -e certbot-ci +commands = + {[base]pytest} certbot-ci/src/certbot_integration_tests passenv = DOCKER_* -[testenv:le_auto_centos6] -# At the moment, this tests under Python 2.6 only, as only that version is -# readily available on the CentOS 6 Docker image. +[testenv:integration-certbot-oldest] +deps = + -e acme + -e certbot + -e certbot-ci +basepython = + {[testenv:oldest]basepython} commands = - docker build -f letsencrypt-auto-source/Dockerfile.centos6 -t lea letsencrypt-auto-source - docker run --rm -t -i lea -whitelist_externals = - docker + {[base]pytest} certbot-ci/src/certbot_integration_tests/certbot_tests passenv = DOCKER_* +setenv = {[testenv:oldest]setenv} -[testenv:docker_dev] -# tests the Dockerfile-dev file to ensure development with it works -# as expected +[testenv:integration-nginx-oldest] +deps = + -e acme + -e certbot + -e certbot-nginx + -e certbot-ci +basepython = + {[testenv:oldest]basepython} commands = - docker-compose run --rm --service-ports development bash -c 'tox -e lint' -whitelist_externals = - docker-compose + {[base]pytest} certbot-ci/src/certbot_integration_tests/nginx_tests passenv = DOCKER_* +setenv = {[testenv:oldest]setenv} + +[testenv:test-farm-apache2] +passenv = + AWS_* +setenv = AWS_DEFAULT_REGION=us-east-1 +changedir = letstest +deps = -e {toxinidir}/letstest +commands = {toxinidir}/tools/retry.sh letstest targets/targets.yaml {env:AWS_EC2_PEM_FILE} SET_BY_ENV scripts/test_apache2.sh --repo {toxinidir} +allowlist_externals = + {toxinidir}/tools/retry.sh + +[testenv:validate-changelog] +deps = towncrier +# we set --version here so towncrier doesn't try to guess the certbot version which requires certbot +# to be installed +commands = towncrier build --draft --version 99.99.0