Commit Graph

8827 Commits

Author SHA1 Message Date
Katie Smith
1f705f3c29 Update flask from 2.1.0 to 2.1.1 2022-04-14 10:17:20 +01:00
Katie Smith
0cd06dba62 Update celery[sqs] from 5.2.3 to 5.2.6. 2022-04-14 10:13:30 +01:00
Katie Smith
c3829da864 Bump all test dependencies 2022-04-14 09:07:39 +01:00
Sakis
153ffd52c4 Merge pull request #3506 from alphagov/add-internal-routes
Add internal routes for api and api-sms-receipts
2022-04-13 10:16:26 +01:00
sakisv
d6b78e6373 Add internal routes for api and api-sms-receipts
These routes will be used by prometheus to scrape the `/metrics` endpoint.

Currently:

The shared prometheus scrapes the `/metrics` endpoint using
the public routes.

The `/metrics` endpoint is provided by the [gds_metrics_python][] which
comes with [bearer-token authentication][] where the token is expected
to be equal to the paas app id.

Each app is configured as a separate target in the shared prometheus
with its app id configured as a GET parameter (e.g.
http://notify-api-production.cloudapps.digital/metrics?cf_app_guid=69c87503-6b53-4c35-XXXX-XXXXXXXXXXXX&cf_app_instance=69c87503-6b53-4c35-XXXX-XXXXXXXXXXXX%3A1&cf_app_instance_index=1)

Each scrape request goes through an nginx proxy which retrieves this
argument from the query string and sets it as a header [[source][]]. This way it
passes the authentication and also is able to instruct the gorouter to
target a specific instance of the app.

In the future:

Since we're moving away from the shared prometheus and towards an
approach where we [run our own prometheus on PaaS][] we can skip the
need for having an nginx proxy and use the internal routes instead, and
have a [preshared-token][] for authentication if we need to.

[gds_metrics_python]: https://github.com/Crown-Commercial-Service/gds_metrics_python
[bearer-token authentication]: https://github.com/Crown-Commercial-Service/gds_metrics_python/blob/master/gds_metrics/__init__.py#L47-L52
[source]: https://github.com/alphagov/prometheus-aws-configuration-beta/blob/master/terraform/modules/prom-ec2/prometheus/cloud.conf#L111-L123
[run our own prometheus on PaaS]: https://github.com/alphagov/notifications-cf-monitoring/pull/1
[preshared-token]: https://github.com/Crown-Commercial-Service/gds_metrics_python/pull/18
2022-04-13 11:01:24 +03:00
Ben Thorner
8c7ad16452 Merge pull request #3503 from alphagov/allow-repeat-send-letter
Don't error sending a letter that's sent already
2022-04-12 16:04:54 +01:00
Ben Thorner
413c6c4c26 Move check for existing letter earlier in endpoint
In response to: [^1].

[^1]: https://github.com/alphagov/notifications-api/pull/3503#discussion_r848426047
2022-04-12 15:51:06 +01:00
Leo Hemsted
91200a2088 Merge pull request #3502 from alphagov/provider-report
add new daily sms provider volume report
2022-04-12 15:48:24 +01:00
Ben Thorner
a5e0fd6104 Merge pull request #3508 from alphagov/redis-ssl-181796569
Prepare to switch to Redis on PaaS
2022-04-12 15:47:46 +01:00
Ben Thorner
5eeb74b267 Merge pull request #3509 from alphagov/remove-redundant-log-181665654
Remove redundant ternary on SMS client FROM_NUMBER
2022-04-12 15:43:33 +01:00
Ben Thorner
29fffc406c Merge pull request #3507 from alphagov/bump-utils-55-1-4
Bump utils to 55.1.4 (no changes)
2022-04-12 15:43:09 +01:00
Ben Thorner
44d90b0a4f Remove redundant ternary on SMS client FROM_NUMBER
Logs over the past 14 days confirm we never call this code with
None as the sender, so it's safe to remove the ternary.
2022-04-12 14:59:21 +01:00
Leo Hemsted
a2cbe20325 fix rediss ssl eventlet sslerror bug
eventlet works by monkey-patching core IO libraries (such as ssl) to be
non-blocking. However, there's currently a bug: In the normal socket
library it may throw a timeout error as a `socket.timeout` exception.
However eventlet.green.ssl's patch raises an ssl.SSLError('timed out',)
instead. redispy handles socket.timeout but not ssl.SSLError, so we
solve this by monkey patching the monkey patching code to raise the
correct exception type 😱

Note: This code should _only_ be called when we're using eventlets, or
we'll run into issues with regular code failing with max recursion
errors. With that in mind we put this code in gunicorn_config, as that
isn't imported when we run celery or run flask locally.
2022-04-12 14:50:36 +01:00
Ben Thorner
fb405977fa Allow REDIS_URL to optionally come from PaaS
This is to support a migration from Redislabs to PaaS native Redis,
allowing us to toggle between old and new using the env vars for
the instance - without needing to change the code.
2022-04-12 14:48:08 +01:00
Ben Thorner
1f83113e74 Move setting VCAP_SERVICES out of fixture
This was inconsistent with the source data for the fixture being
overidden in some of the tests. We only need to set it in the env
once, so it makes sense to just put the code there.
2022-04-12 14:46:47 +01:00
Ben Thorner
06aba23adb Remove redundant postgres CloudFoundry fixture 2022-04-12 14:45:16 +01:00
Ben Thorner
f393ca4638 Bump utils to 55.1.4 (no changes) 2022-04-12 14:13:53 +01:00
Leo Hemsted
259d4a0569 add new daily sms provider volume report
code generally lifted almost exactly from the daily_volumes_report, but
per provider and only for SMS.
2022-04-11 13:42:40 +01:00
Ben Thorner
385be77d67 Merge pull request #3504 from alphagov/adjust-sms-resting
Further tweaks to SMS provider resting points
2022-04-11 10:54:42 +01:00
Ben Thorner
3f5a811e8f Further tweaks to SMS provider resting points 2022-04-11 10:44:57 +01:00
Ben Thorner
70430f10ea Co-locate tests for sending a notification
I found the send letter tests hard to find as the name of the file
didn't match the name of the one containing the code under test.
2022-04-08 17:37:33 +01:00
Ben Thorner
fa10ec77ab DRY-up test send letter test data into fixture
This makes it easier to see what's different in each test.
2022-04-08 17:37:31 +01:00
Ben Thorner
5810d46d35 Don't error sending a letter that's sent already
Fixes this error (in Admin):

      File "/home/vcap/app/app/notify_client/notification_api_client.py", line 74, in send_precompiled_letter
        return self.post(url='/service/{}/send-pdf-letter'.format(service_id), data=data)
      File "/home/vcap/app/app/notify_client/__init__.py", line 59, in post
        return super().post(*args, **kwargs)
      File "/home/vcap/deps/0/python/lib/python3.9/site-packages/notifications_python_client/base.py", line 48, in post
        return self.request("POST", url, data=data)
      File "/home/vcap/deps/0/python/lib/python3.9/site-packages/notifications_python_client/base.py", line 64, in request
        response = self._perform_request(method, url, kwargs)
      File "/home/vcap/deps/0/python/lib/python3.9/site-packages/notifications_python_client/base.py", line 118, in _perform_request
        raise api_error
    notifications_python_client.errors.HTTPError: 500 - Internal server error

Due to this error (in API):

      File "/home/vcap/app/app/service/send_notification.py", line 178, in send_pdf_letter_notification
        raise e
      File "/home/vcap/app/app/service/send_notification.py", line 173, in send_pdf_letter_notification
        letter = utils_s3download(current_app.config['TRANSIENT_UPLOADED_LETTERS'], file_location)
      File "/home/vcap/deps/0/python/lib/python3.9/site-packages/notifications_utils/s3.py", line 53, in s3download
        raise S3ObjectNotFound(error.response, error.operation_name)
    notifications_utils.s3.S3ObjectNotFound: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.

I checked the DB to verify the letter does actually exist i.e. it
is an instance of the problem we're fixing here.
2022-04-08 17:20:44 +01:00
Ben Thorner
6ca550a1a6 Merge pull request #3497 from alphagov/broadcast-alert-earlier-181423293
Alert earlier when a broadcast is approved
2022-04-07 10:34:23 +01:00
Ben Thorner
190c2b5122 Fix broadcast alert not triggering PagerDuty
We previously decided against this [^1] but the lambda alerts are
not a replacement for this one.

Even if both sets of alerts go off, we expect PagerDuty to aggregate
them into a single notification.

[^1]: 65c21f694c
2022-04-06 14:06:03 +01:00
Ben Thorner
cf66d7c344 Merge pull request #3500 from alphagov/limit-provider-history
Limit provider history to 100 results
2022-04-06 12:09:35 +01:00
Ben Thorner
13b01579c8 Limit provider history to 100 results
This stops the pages being slow to load and defers the need to add
any pagination. Only the most recent data is likely to be relevant.
2022-04-06 11:53:01 +01:00
Ben Thorner
a513e534f7 Merge pull request #3499 from alphagov/adjust-sms-provider-priorities
Adjust resting positions for SMS providers
2022-04-06 10:49:32 +01:00
Ben Thorner
a8ce4cabc3 Adjust resting positions for SMS providers
It's worth noting this will break the Admin page to edit provider
priorities, which currently works in units of 10. We already have
work planned to fix this [^1] and it's not an immediate problem.

The automatic adjustment algorithm will continue to work properly
as it can cope with increments smaller than 10 [^2].

[^1]: https://www.pivotaltracker.com/story/show/181681739
[^2]: b145a29935/app/dao/provider_details_dao.py (L122)
2022-04-06 10:35:21 +01:00
Katie Smith
5915f422d9 Merge pull request #3498 from alphagov/bump-dependencies
Bump Flask and itsdangerous
2022-04-06 09:13:55 +01:00
Katie Smith
badd0e0894 Bump Flask and itsdangerous
This bumps Flask to version 2.1.0, which requires some minor changes to
the app code and itsdangerous to also be bumped.
2022-04-05 17:06:08 +01:00
Ben Thorner
f4cc87dc77 Remove excess indentation in broadcast alert
In response to: [^1].

[^1]: https://github.com/alphagov/notifications-api/pull/3497#discussion_r842747749
2022-04-05 14:42:24 +01:00
Ben Thorner
e84014b86d Update broadcast approval alert content
This now links to the new runbook for the alert.
2022-04-05 12:57:09 +01:00
Ben Thorner
779b8e941f Rewrite broadcast Zendesk alert at approval time
The new alert happens earlier but is otherwise the same:

- We only create a ticket in Production.
- We only create a ticket on approval.

I took this opportunity to refactor the alert as a private function
and test this specifically in detail to avoid lots of repetitive
mocks, which are required when calling the main "update" function.

One test I haven't preserved was for when the "names" array is empty,
as this was added for a legacy data integrity scenario [^1].

[^1]: bf0bf4e31c
2022-04-05 12:57:08 +01:00
Ben Thorner
9902ddfc22 Merge pull request #3496 from alphagov/fix-send-sms-log
Fix log for sending SMS to be generic per client
2022-04-05 11:49:59 +01:00
Ben Thorner
c7c5793da4 Shorten name of broadcast utility function
This is doing more than just validating and updating an is about to
do even more. Saying "update" is broad enough to cover the others.
2022-04-05 11:35:33 +01:00
Ben Thorner
3b705a780a Extract broadcast validations into separate fn
This makes it easier to see the key stages of the function, which
we're about to make a bit bigger.
2022-04-05 11:29:36 +01:00
Ben Thorner
4a40b169a2 Fix flakey test for notification status
This was creating data in the Notifications table but the funcetion
under test was - now the timestamps are in the past - looking in the
NotificationHistory table. Freezing the time of the test fixes that.
2022-04-05 11:07:05 +01:00
Ben Thorner
0d07220923 Fix log for sending SMS to be generic per client
This was a mistake in [^1].

[^1]: 3b082477f0 (diff-95316b574f974237b3b7ce453fec09628bd1062087154789ff4d0176ea23c460R52)
2022-04-05 10:46:14 +01:00
Ben Thorner
7631e9abc9 Merge pull request #3495 from alphagov/fix-fy21-tests
Fix tests breaking in new financial year
2022-04-01 10:33:05 +01:00
Ben Thorner
6f632b3c4b Fix tests breaking in new financial year
We should lock these tests to run in a particular year [^1].

Fixes e.g.

      >       assert annual_billing[0].free_sms_fragment_limit == 150000
      E       assert 40000 == 150000
      E        +  where 40000 = <AnnualBilling 8851fd4b-6316-4a53-b0e0-8202c803ae97>.free_sms_fragment_limit

[^1]: 8402e7c97b (diff-adcd90bfdc6b7777fdf309037ca6948bef4f4b858e22f8b2e46b71865d580fbaR60)
2022-04-01 10:22:07 +01:00
Ben Thorner
b6a52ec606 Merge pull request #3493 from alphagov/reach-send-181665654
Add boilerplate for sending SMS via Reach
2022-04-01 10:10:01 +01:00
Ben Thorner
e6fffc00da Add temporary log to check if code is in use
In response to: [^1].

[^1]: https://github.com/alphagov/notifications-api/pull/3493#discussion_r838477599
2022-03-31 10:32:18 +01:00
Ben Thorner
7d92a0869a Remove per-client SMS exception classes
In response to: [^1].

The stacktrace conveys the same and more information. We don't do
anything different for each exception class, so there's no value
in having three of them over one exception.

I did think about DRYing-up the duplicate exception behaviour into
the base class one. This isn't ideal because the base class would
be making assumptions about how inheriting classes make requests,
which might change with future providers. Although it might be nice
to have more info in the top-level message, we'll still get it in
the stacktrace e.g.

    ValueError: Expected 'code' to be '0'
    During handling of the above exception, another exception occurred:
    app.clients.sms.SmsClientResponseException: SMS client error (Invalid response JSON)

    requests.exceptions.ReadTimeout
    During handling of the above exception, another exception occurred:
    app.clients.sms.SmsClientResponseException: SMS client error (Request failed)

[^1]: https://github.com/alphagov/notifications-api/pull/3493#discussion_r837363717
2022-03-30 13:38:50 +01:00
Ben Thorner
8432be4fc1 Add missing test for invalid Firetext JSON
This is already tested for MMG (and Reach).
2022-03-30 13:38:49 +01:00
Ben Thorner
a2e1d03009 Require "sender" argument to send_sms method
In response to [^1].

[^1]: https://github.com/alphagov/notifications-api/pull/3493#discussion_r836616675
2022-03-30 13:38:48 +01:00
Ben Thorner
015152bab2 Add boilerplate for sending SMS via Reach
This works in conjunction with the new SMS provider stub [^1].

Local testing:

- Run the migrations to add Reach as an inactive provider.
- Activate the Reach provider locally and deactivate the others.

      update provider_details set priority = 100, active = false where notification_type = 'sms';
      update provider_details set active = true where identifier = 'reach';

- Tweak your local environment to point at the SMS stub.

      export REACH_URL="http://host.docker.internal:6300/reach"

- Start / restart Celery to pick up the config change.
- Send a SMS via the Admin app and see the stub log it.
- Reset your environment so you can send normal SMS.

      update provider_details set active = true where notification_type = 'sms';
      update provider_details set active = false where identifier = 'reach';

[^1]: https://github.com/alphagov/notifications-sms-provider-stub/pull/10
2022-03-30 13:38:46 +01:00
Ben Thorner
27ddc4501e DRY-up overriding shortcode with sender
This avoids duplicating the logic when we add a new provider.
2022-03-30 13:37:01 +01:00
Ben Thorner
3b082477f0 DRY-up logging and metrics for sending SMS
This avoids duplicating it as we add a new provider and means we
can test it all in one place (although it wasn't tested before).

I'm not sure why the previous code did "super(..)__init__" in a
non-init function - it's a bit late! - so I've just replaced it
with a call to the new "init_app" function in the parent class.
2022-03-30 13:37:00 +01:00
Ben Thorner
84578e8a1d Make provider tests agnostic to actual data
The provider tests are coupled to actual data in the DB, but we
shouldn't have to overhaul the tests when this changes.

Assuming we don't delete old providers, just testing a subset of
the fixture data should give us enough confidence in the code.
2022-03-30 13:36:59 +01:00