Commit Graph

8371 Commits

Author SHA1 Message Date
Ben Thorner
023a06d5fb Start dual running with "areas" and "names"
For the public API we actually receive a "name" instead of an ID,
which we also want to start sending from the Admin app.

Unlike IDs, which aren't really used anywhere, we want the names
to display the alerts on gov.uk/alerts.
2021-08-26 15:34:25 +01:00
Ben Thorner
8f39d476bd Start dual running with "areas" and (area) "ids"
This is necessary until:

- The Admin app is using the new "areas(_2)" format to store and
retrieve data.

- We've migrated all existing broadcast messages to use the new
format.

Note that "areas" / "ids" isn't actually used for anything except
printing out the PagerDuty message - it's not sent to the proxy [1].

[1]: 6edc6c70aa/app/celery/broadcast_message_tasks.py (L190-L193)
2021-08-26 15:34:24 +01:00
Ben Thorner
fd7ebbebb0 Introduce "areas_2" so we can repurpose "areas"
Currently we have:

- An "areas" column in the DB that stores a JSON blob.
- An "areas" field inside the "areas" JSON that stores area IDs.
- Each field has to be manually copied into the JSON column.

We want to move to:

- An "areas" column in the DB (unchanged).
- An "ids" field inside the "areas" JSON (to replace "areas").
- The Admin app sending other data inside an "areas" JSON field.

The API design for areas is confusing and difficult to extend.
Here we duplicate the current API functionality using an "areas_2"
field. Once the Admin app is using this field, we'll be able to
rename it to just "areas", which is where we want to get to.

In the next commits we'll build on this to support the migration
from "areas"."areas" to "areas"."ids".
2021-08-26 15:34:23 +01:00
Ben Thorner
277db4e6c7 Merge pull request #3313 from alphagov/force-override-broadcast-178986763
Allow old broadcasts to be overridden
2021-08-26 14:25:41 +01:00
Ben Thorner
44b388dc69 Allow old broadcasts to be overridden
This is a temporary feature to make it easy to migrate the format
of the "areas" column and backfill extra data for it.

It's not possible to use this feature to update the status of an
old broadcast message, so the risk from this override is minimal.
2021-08-26 12:40:43 +01:00
Chris Hill-Scott
6edc6c70aa Merge pull request #3310 from alphagov/revert-3309-update-utils-coordinate-transformation
Revert "Update utils to bring in coordinate transformation"
2021-08-20 16:31:47 +01:00
Chris Hill-Scott
f3e6d92046 Revert "Update utils to bring in coordinate transformation" 2021-08-20 16:05:39 +01:00
Chris Hill-Scott
c50ef90d53 Merge pull request #3309 from alphagov/update-utils-coordinate-transformation
Update utils to bring in coordinate transformation
2021-08-20 15:43:51 +01:00
Chris Hill-Scott
e92be8b034 Pass polygons through if they’re small already
If a polygon is smaller than the largest polygon in our dataset of
simplified polygons then we’re only throwing away useful detail by
simplifying it.

We should still simplify larger polygons as a fallback, to avoid sending
anything to the CBC that we’re not sure it will like.

The thresholds here are low: we can raise them as we test and experiment
more.

Here’s some data about the Flood Warning Service polygons

Percentile | 80% | 90%   | 95%    | 98%     | 99%     | 99.9%
-----------|-----|-------|--------|---------|---------|---------
Point count| 226 | 401.9 | 640.45 | 1015.38 | 1389.07 | 3008.609

Percentile    | 80% | 90%   | 95%    | 98%     | 99%     | 99.9%
--------------|-----|-------|--------|---------|---------|---------
Polygon count |2----|3------|5-------|8--------|10-------|40.469
2021-08-19 11:08:53 +01:00
Chris Hill-Scott
a6135fb8ab Bump utils
This new version of utils implements the transformation of our polygons
to a Cartesian plane. In other words, it converts them from being
defined in spherical degrees to metres.

For the API this means our simplification will be slightly more
accurate.
2021-08-19 11:08:18 +01:00
Leo Hemsted
5cb97253ac Merge pull request #3244 from alphagov/allowlist
Allowlist
2021-08-17 14:43:33 +01:00
Leo Hemsted
543bff1c30 bump utils to fix sending to international numbers via allowlist 2021-08-16 10:10:24 +01:00
Pea Tyczynska
9334204cd2 Merge pull request #3305 from alphagov/fetch-alerts-list-for-govuk-alerts
get_broadcasts returns a list of alerts for gov.uk/alerts
2021-08-12 14:33:46 +01:00
Pea Tyczynska
9d2f8347b2 get_broadcasts returns a list of broadcasts for gov.uk/alerts 2021-08-12 14:03:33 +01:00
Pea Tyczynska
0f7f219a55 dao_get_all_broadcast_messages returns just fields govuk alerts need 2021-08-11 14:43:27 +01:00
Pea Tyczynska
74c9ca2bf6 Fetch all broadcast messages that are or were transmitted
Regardless of channel.
Do not include:
- broadcasts older than 25.05.2021
- stubbed broadcasts
- broadcasts that were not transmitted. So only broadcasting,
cancelled and completed make the list;
2021-08-11 14:43:27 +01:00
Ben Thorner
cae429dadc Merge pull request #3303 from alphagov/refactor-test-auth-headers-179039225
Follow-up refactorings to authorization code
2021-08-11 13:54:27 +01:00
Ben Thorner
e8ad12f016 Merge pull request #3304 from alphagov/switch-to-per-app-api-keys-179039225
Switch to per-app secrets from internal APIs
2021-08-11 11:22:10 +01:00
Ben Thorner
e1dec3f9b8 Switch to per-app secrets from internal APIs
Relates to: [1]

[1]: https://github.com/alphagov/notifications-credentials/pull/231
2021-08-05 17:24:56 +01:00
Ben Thorner
96c527038c Only log authorization for public requests
This is the original behaviour [1]. Since all internal requests will
have corresponding logs from public-facing apps that are making them,
there's little value in logging them.

Logging internal requests doesn't lead to a significant increase in
our overall log ingestion: a rough estimate is its an extra 5000 logs
per minute, out of about 900K per minute.

[1]: e08d726f05/app/authentication/auth.py (L153)
2021-08-04 16:16:31 +01:00
Ben Thorner
09e3ba6836 DRY-up creating auth headers for requests
The rest of the tests need to construct the header directly so they
can pass custom tokens. But for the three tests that actually make
a request to prove the auth functions work as wrappers, we can use
the same factory functions we use everywhere else in the tests.
2021-08-04 16:06:23 +01:00
Ben Thorner
0312e2a528 Split generating authorization headers by type
In response to [1].

[1]: https://github.com/alphagov/notifications-api/pull/3300#discussion_r681653248
2021-08-04 15:13:52 +01:00
Ben Thorner
5a1636e41f Merge pull request #3300 from alphagov/multi-internal-auth-179039225
Split up authentication for internal API endpoint
2021-08-04 14:35:54 +01:00
Ben Thorner
a3cbea218f Rename "key" to "secret" for consistency
While "key" is the term used by the JWT library, all the rest of
our code - the ApiKey model, the Python client - all use the term
"secret" instead. Although "secret" is less precise than "key", it
does help avoid confusion with (api) key (as a model object).
2021-08-03 15:58:29 +01:00
Ben Thorner
4b7ad89f6a Add pretend authenticated API for govuk-alerts
We can define the API properly in future work. I've used a separate
blueprint from "broadcasts" since this API is purely internal, and
it's helpful to make it clear it's specific to govuk-alerts.
2021-08-03 15:58:28 +01:00
Ben Thorner
3e32fc99b8 Rename ADMIN_CLIENT_USER_NAME to say CLIENT_ID
"user name" implies we're doing basic auth, which we're not. We
should use the standard terminology for bearer tokens.
2021-08-03 15:58:27 +01:00
Ben Thorner
323feedb1f Backfill missing tests for requires_auth function 2021-08-03 15:58:26 +01:00
Ben Thorner
cd0a950571 Colocate requires_auth test with the others
This is to help minimise the diff for the next commit.
2021-08-03 15:58:25 +01:00
Ben Thorner
f5d2edaa0a Rewrite requires_admin_auth tests to be generic
This adds previously missing tests and changes the existing ones
to test the "requires_internal_auth" function directly. In order
to make the tests generic, we have a fake auth function and an
associated fixture.

Having generic tests for internal auth will make it easier to add
other "requires..." functions in future.
2021-08-03 15:58:24 +01:00
Ben Thorner
193e7e0004 Rewrite other requires_auth tests to call function
This makes the tests easier to read by avoiding request boilerplate
and making a clearer link between the name of the test and that we
are actually testing that specific function.
2021-08-03 15:58:23 +01:00
Ben Thorner
4fc820c0cc Remove redundant test about no auth proxy checks
This is a lot of code to check we haven't written a single line,
which we can just visually see isn't there. We should avoid having
tests that check code _isn't_ there, as such testing is infinite.
2021-08-03 15:58:22 +01:00
Ben Thorner
65d0dc43f0 Move happy path request-based tests to top of file
We can simplify the rest of the tests to avoid the boilerplate of
making an actual request. But it's worth keeping these two to prove
the wrapper work correctly for an arbitrary route.
2021-08-03 15:58:21 +01:00
Ben Thorner
9da937ab1d DRY-up tests for decoding JWT tokens
Previously we had a lot of duplicate tests inconsistently checking
each of the "requires_" functions. Since both of them now use the
same "_decode_jwt_token" helper, we can consolidate all the tests
onto that. In future commits we'll look at testing the top-level
functions in terms of what they do specifically.
2021-08-03 15:58:20 +01:00
Ben Thorner
2c568698d1 Simplify tests for get auth token / issuer
This switches to testing the two functions directly as trying to
test them through the top-level "requires_..." functions or calls
to endpoints doesn't scale as we add more of them.

While this has a slight risk that a "requires_..." function might
not be using these helpers, it seems unlikely and we can always
add a mock to check this if we're concerned in future.
2021-08-03 15:42:40 +01:00
Ben Thorner
1d806d65eb Standardise auth checks for both kinds of API auth
Previously "requires_auth" and "requires_admin_auth" had similar
but different ways of checking their keys. This switches them to
use the same checks, with the admin / internal auth passing in a
fake / stub set of "api keys" to check.

Pulling out the logic this way will make it easier to unpick the
tests, so we can focus on testing what's unique to each kind of
API auth and avoid future duplication when we start calling the
"requires_internal_auth" method with other client_ids.

Note that a couple of error messages / response codes have changed
for admin / internal auth. None of these occur in practice, so we
can make them consistent with the behaviour for the public API.
2021-08-03 15:42:39 +01:00
Ben Thorner
e08d726f05 DRY-up and simplify creating JWT tokens in tests
Previously this was heavily duplicated but with the odd test using
a __create_token method. This adds some fixtures to remove all the
boilerplate and standardise how tokens are created in each test.
2021-08-03 15:42:37 +01:00
Ben Thorner
36afd4210f Rename tests to match the function under test
This makes it easier to see what we're missing. I also tweaked one
test where a parameter wasn't necessary.
2021-08-03 15:41:40 +01:00
Pea Tyczynska
82e7724c56 Merge pull request #3296 from alphagov/separate_error_for_cancelling_cancelled_letters
Show separate error for when user tries to cancel letter
2021-08-03 10:29:47 +01:00
Chris Hill-Scott
17a046683f Merge pull request #3299 from alphagov/revoke-api-keys-when-service-becomes-broadcast
Revoke API keys when changing broadcast settings
2021-08-02 10:48:16 +01:00
Chris Hill-Scott
132411be24 Don’t re-expire old keys
If a key has already been expired we don’t want to lose the information
about when that happened by giving it a new expiry date.
2021-07-30 11:56:51 +01:00
Sakis
9eb06836cc Merge pull request #3301 from alphagov/jobs-worker-memory-bump
Set jobs worker memory to 2GB
2021-07-30 13:08:06 +03:00
Sakis
4d73a22d48 Set jobs worker memory to 2GB
This is to see if the worker requires slightly more memory than it has access to or to determine if there is a memory  leak somewhere in the code that needs to be further investigated.

This comes at the heels of yesterday's issue that we could not process the CSVs the users uploaded, where the memory graph for this worker showed that it was using almost all of its available memory, so a redeploy fixed the problem.
2021-07-30 12:49:42 +03:00
Ben Thorner
2788682b0a DRY-up creating JWT tokens manually in auth tests
This makes it a bit easier to see what tests are missing.
2021-07-29 12:53:03 +01:00
Ben Thorner
49455d9890 Support granular API auth for internal apps
Previously we just had a single array of API keys / secrets, any of
which could be used to get past the "requires_admin_auth" check.
While multiple keys are necessary to allow for rotation, we should
avoid giving other apps access this way (too much privilege).

This converts the existing config vars into a new dictionary, keyed
by client_id. We can then use the dictionary to scope auth for new
API consumers like gov.uk/alerts to just the endpoints they need to
access, while maintaining existing access for the Admin app.

Once the new dictionary is available as a JSON environment variable,
we'll be able to remove the old credentials / config. In the next
commits, we'll look at more tests for the new functionality.
2021-07-29 12:53:02 +01:00
Pea Tyczynska
0c8dd247f9 Show separate error for when user tries to cancel letter
that is already cancelled vs when it is too late to
cancel letter vs when we don't know what's the cause
of failure.

This is so we could show useful error messages to the users
and also for better debugging.
2021-07-29 11:32:49 +01:00
Chris Hill-Scott
43bcb56ff4 Revoke API keys when changing broadcast settings
On a regular Notify service anyone with permission can create an API
key. If this service then is given permission to send emergency alerts
it will have an API key which can create emergency alerts. This feels
dangerous.

Secondly, if a service which legitimately has an API key for sending
alerts in training mode is changed to live mode you now have an API key
which people will think isn’t going to create a real alert but actually
will. This feels really dangerous.

Neither of these scenarios are things we should be doing, but having
them possible still makes me feel uncomfortable.

This commit revokes all API keys for a service when its broadcast
settings change, same way we remove all permissions for its users.
2021-07-29 10:11:38 +01:00
Pea Tyczynska
2b2c240bee Update utils version to bring in too_late_to_cancel_letter
We need that method to show right errors to the user
when cancelling letter fails

Update dependencies
2021-07-28 16:33:01 +01:00
Ben Thorner
312a895822 Merge pull request #3294 from alphagov/auto-expire-alerts-178926353
Auto expire old broadcast messages
2021-07-22 09:53:41 +01:00
Ben Thorner
5e9d8e5fa0 Auto expire old broadcast messages
Since the expiry is sent as part of the message payload, we don't
need to invoke the CBC proxies (and indeed there's no way to do so
for an expired alert). In future we plan to extend this task so it
triggers the regeneration of content on gov.uk/alerts.

It's worth noting that 'finishes_at' can theoretically be None, in
which case it's unclear when the alert should expire. While alerts
from the Admin app should always have an expiry [1], we have many
in the DB that don't, so it's worth checking for this scenario.

[1]: 078ac10c8d/app/models/broadcast_message.py (L255)
2021-07-21 13:05:11 +01:00
Ben Thorner
03d438b0cc Merge pull request #3293 from alphagov/site-b-link-test-178738544
Trigger separate link test for failover lambda
2021-07-20 16:44:59 +01:00