Commit Graph

8333 Commits

Author SHA1 Message Date
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
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
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
Ben Thorner
d24b45bb67 Constrain log length for CBC proxy payload
This avoids any issues due to large payloads (e.g. with a lot of
polygons in the 'areas' field). While we may miss part of the log
in such cases, this is more than we get already anyway.
2021-07-20 16:06:48 +01:00
Richard Baker
d1791c6bfe Remove "link test" from cbc-proxy log output
The `_invoke_lambda` function is called when sending  both link test and real message types.

Signed-off-by: Richard Baker <richard.baker@digital.cabinet-office.gov.uk>
2021-07-20 15:35:50 +01:00
Katie Smith
1bce636b77 Merge pull request #3290 from alphagov/keep-view-activity
Always keep view activity permissions for broadcast users
2021-07-20 13:05:50 +01:00
Katie Smith
fbe79591ef Merge pull request #3284 from alphagov/update-bcast-permissions-data
Remove original permissions from broadcast users
2021-07-20 11:47:08 +01:00
Ben Thorner
cdc150de1b Change link test task to trigger both lambda
This modifies the previous "(_)send_link_test" method to trigger a
link test for a specific lambda. We then call the method with both
the primary and failover lambda in new orchestrator method.

Since the _invoke_lambda function doesn't raise exceptions if it
fails, there's no need to rescue anything in order to ensure the
second link test / invocation runs as well. It doesn't testing for
this, since it boils to an absence of code to raise any exception.

Note that, like the other parent tests, we only check the new method
works with a specific proxy client instance.
2021-07-19 16:00:56 +01:00
Ben Thorner
08f48379b4 Move ID generation into link test method
Unlike the other IDs which are stored in the DB, this isn't relevant
for the Celery task as it invokes a link test. Moving it into the
proxy client will also enable us to generate a second ID in the next
commits, where we start doing a link test for the failover lambda.
2021-07-19 16:00:55 +01:00
Ben Thorner
7fb65761f9 Always log when a lambda is invoked
This replaces the previous single-purpose log about a link test
with a more informative and generic one for all invocations.
2021-07-19 15:45:43 +01:00
Ben Thorner
1e8eda0d15 Avoid failover when running link tests
We want to get the point where we're running link tests for each
lambda independently. The tests weren't checking for the failover
mechanism for link tests, so we can just remove it.
2021-07-19 15:45:42 +01:00
Ben Thorner
b6774bf0f7 Generate Vodafone link test sequence nos in proxy
Previously the Celery task to trigger a link test had to know about
the special case of a sequence number for Vodafone. Since we're about
to change the client to perform multiple tests it makes sense to give
it the knowledge of how to generate number itself.

Note that we have to import the db inline to avoid a circular import,
since this module is itself imported by app/__init__.py.

Other invocations of the Vodafone client use stored sequence numbers
from the DB, which are called "message numbers" in that context. Since
the two use cases are very different (even the names are different!),
having them in two places shouldn't cause any confusion.
2021-07-19 15:43:36 +01:00
Katie Smith
d708b64dd4 Remove original permissions from broadcast users
The broadcast user permissions are changing, so to avoid confusion and
permissions which exist in the database but don't display on the
frontend we are going to remove all existing permissions for users of
broadcast services. This also updates the permissions of invited users
who are still pending.

The exception to this is the `view_activity` permission, which we always
add for broadcast users even if they have no other permissions.
(aad017a184/app/main/forms.py (L1043))
2021-07-19 14:38:48 +01:00
Sakis
e166156fad Merge pull request #3292 from alphagov/revert-werkzeug
Revert werkzeug to the last non-2.0.0 version
2021-07-19 16:14:26 +03:00
sakisv
7ff43939bc Revert werkzeug to the last non-2.0.0 version
We observe high memory usage since we bumped it (along with other
things) and because it only appears on the API and not on the workers
the hypothesis is that Werkzeug is responsible for it.
2021-07-19 16:06:56 +03:00
Katie Smith
0c7982fd84 Always keep view_activity permissions for broadcast users
We made a change to remove all permissions from users and invited users
when the broadcast service settings form is submitted
(https://github.com/alphagov/notifications-api/pull/3284). However, when the
form is submitted, notifications-admin always adds the `view_activity`
permission even if no permission boxes are ticked, so we don't want to
remove that one permission
(256c840b46/app/main/forms.py (L1042))
2021-07-14 16:39:38 +01:00
Rebecca Law
d781da2981 Merge pull request #3261 from alphagov/remove-scheduled_notifications
Remove scheduled_notifications
2021-07-14 10:56:42 +01:00
Leo Hemsted
1975f3664a Merge pull request #3289 from alphagov/retry-429-callback
retry service callbacks on 429
2021-07-13 17:04:45 +01:00
Leo Hemsted
2ad9a3a380 retry service callbacks on 429
if we're served a 429, put the item on the retry queue and retry the
same as if the service returned a 5xx. 429 is commonly returned for rate
limit exceeding, and retrying on a delay is a typical response to that.
2021-07-13 16:09:17 +01:00
Pea Tyczynska
719ba698e2 Merge pull request #3288 from alphagov/bump-utils-to-revert-changes
Bump utils to revert changes to placeholders that introduced a bug
2021-07-09 14:58:02 +01:00
Pea Tyczynska
e82b8bc33c Bump utils to revert changes to placeholders that introduced
a bug.
2021-07-09 14:45:41 +01:00
Pea Tyczynska
96f34bbd45 Merge pull request #3286 from alphagov/bump-utils-fix-placeholder-bug
Bump utils to bring in fix for optional placeholder bug
2021-07-09 11:24:42 +01:00
Katie Smith
18bb1c2ac4 Merge pull request #3277 from alphagov/bcast-service-permissions
Remove user permissions if service becomes a broadcast service
2021-07-09 09:12:45 +01:00
Pea Tyczynska
c28e9451d4 Bump moto version to try solve dependencies version conflict
Also update mock import statements in some test files as they
stopped working with this dependency update.
2021-07-08 15:37:19 +01:00
Pea Tyczynska
9e8682ac29 Bump utils to bring in fix for optional placeholder bug
See https://github.com/alphagov/notifications-utils/pull/878 for
details.

Changes we had to make for our app and tests to work correctly
after the dependency updates:

1. Update emergency alerts polygons test because we changed
how exact we are with locations of the points on the map.

2. Use Flask's g object to set additional request attributes

So far we have been storing them in _request_ctx_stack which is
an innard for Flask's request context.

Because of major update to Werkzeug dependency, which Flask relies
on, the way we were using it stopped working, so we had a new
way to set those values.
The way we set those values now, by using g object, seems to also
be favoured in Flask documentation:
https://flask.palletsprojects.com/en/1.1.x/reqcontext/#how-the-context-works
2021-07-08 12:18:09 +01:00
Rebecca Law
5fcf65422c Fix version conflicts. 2021-07-08 08:14:03 +01:00
Rebecca Law
bcd8d85569 Fix merge conflicts 2021-07-08 08:12:18 +01:00
Rebecca Law
ae29ae28e5 Adding CONCURRENTLY to the drop index statement.
Drop index concurrently will drop the index without locking out concurrent selects, inserts, updates, and deletes on the index's table namely on notifications.
2021-07-08 08:12:18 +01:00
Rebecca Law
94c4a8f238 Remove scheduled_notifications
All code has been removed for ScheduledNotifications. This PR just
removes the table, it has never been used.
2021-07-08 08:12:18 +01:00
Katie Smith
fc0b9736eb Remove user permissions if service becomes a broadcast service
The "normal" service permissions and broadcast service permissions are
going to be different with no overlap. This means that if you were
viewing the team members page, there might be permissions in the
database that are not visible on the frontend if a service has changed
type. For example, someone could have the 'manage_api_keys' permission,
which would not show up on the team members page of a broadcast service.
To avoid people having permissions which aren't visible in admin, we now
remove all permissions from users when their service is converted to a
broadcast service.

Permisions for invited users are also removed.

It's not possible to convert a broadcast service to a normal service, so
we don't need to cover for this scenario.
2021-07-07 16:13:35 +01:00
Katie Smith
29a13a8fae Merge pull request #3274 from alphagov/new-permissions
Add new broadcast related permissions
2021-07-07 15:31:21 +01:00
Katie Smith
e5fdd8ee1f Add new broadcast related permissions
We want to have new permissions which will be used specifically for
broadcasts:
- `create_broadcasts`
- `approve_broadcasts`
- `reject_broadcasts`
- `cancel_broadcasts`

Cancel and reject will always go together, but having separate database
permissions makes things easier to change in the future.

The permission column of the permissions table is an enum. We can add values
in the alembic upgrade script, but removing individual values from an
enum is not supported by Postgres. To remove values, we have to recreate
the enum with the old values.
2021-07-07 14:54:13 +01:00
Ben Thorner
273d14fbe4 Merge pull request #3280 from alphagov/webauthn-user-flag
Add flag to say if user is eligible for WebAuthn
2021-07-01 11:10:06 +01:00
Ben Thorner
6cf24899dd Let existing WebAuthn users continue using it
It's not a big deal if a user is no longer eligible to register a
security key, so we may as well let them continue using it. This
avoids putting them in a limbo state if we don't immediately change
their auth type when they're no longer eligible to use the feature.
2021-06-30 15:41:43 +01:00
Rebecca Law
f4c0f1f1df Merge pull request #3282 from alphagov/fix-bug-check_service_over_daily_limit
Convert value from redis to an int.
2021-06-29 14:11:28 +01:00
Rebecca Law
c501c92bf3 Convert value from redis to an int.
This has been properly tested locally with redis enabled.
2021-06-29 14:00:25 +01:00
Rebecca Law
864554f125 Merge pull request #3281 from alphagov/fix-bug-check_service_over_daily_limit
Fix bug in check_service_over_daily_limit
2021-06-29 13:20:14 +01:00
Rebecca Law
ed788cb0bd Fix bug in check_service_over_daily_limit
I forgot to return service_stats if the cache exists. And fixed the tests to check service_stats value.
2021-06-29 12:59:20 +01:00
Rebecca Law
310e1cb4e2 Merge pull request #3276 from alphagov/daily-limit-redis-cache
Correct the daily limits cache.
2021-06-29 12:06:35 +01:00
Ben Thorner
2fa6327efb Add flag to say if user is eligible for WebAuthn
Currently we have some data-driven roles to say who can use this
feature. Adding a flag in the API means we can avoid API calls in
the Admin app to determine the same.

Allowing members of the GOV.UK Notify service to use the feature
is a workaround, so we can avoid making someone a Platform Admin
before they've protected their account with it.
2021-06-28 13:35:24 +01:00
Rebecca Law
18dd9050a4 - make sure when processing a job that we check the total_sent + job.notification_count against the service.message_limit. 2021-06-28 13:07:48 +01:00