Commit Graph

103 Commits

Author SHA1 Message Date
Kenneth Kehl
00fd3a72bb code review feedback, fix setup.cfg and reformat 2023-08-25 08:10:33 -07:00
Kenneth Kehl
026dc14021 notify-api-412 use black to enforce python style standards 2023-08-23 10:35:43 -07:00
stvnrlly
637fbdb891 broadcast flake8 cleanup 2022-10-25 11:53:24 -04:00
stvnrlly
57f4df8ed1 remove broadcast-related code, except migrations 2022-10-04 15:28:27 +00:00
Christa Hartsock
af6495cd4c Get tests passing locally
When we cloned the repository and started making modifications, we
didn't initially keep tests in step. This commit tries to get us to a
clean test run by skipping tests that are failing and removing some
that we no longer expect to use (MMG, Firetext), with the intention that
we will come back in future and update or remove them as appropriate.

To find all tests skipped, search for `@pytest.mark.skip(reason="Needs
updating for TTS:`. There will be a brief description of the work that
needs to be done to get them passing, if known. Delete that line to make
them run in a standard test run (`make test`).
2022-07-07 15:41:15 -07:00
Ben Thorner
6a53871455 Restructure govuk-alerts endpoint to be internal
In response to: https://github.com/alphagov/notifications-api/pull/3305#pullrequestreview-726672421

Previously this was added among the public /v2 endpoints, but it's
only meant for internal use. While only the govuk-alerts app would
be able to access it, the location and /v2 URL suggested otherwise.
This restructures the endpoint so it resembles other internal ones.
2021-09-15 15:36:17 +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
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
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
Leo Hemsted
c1b08e4cbc make sure all non-uuid service ids 403 in api keys
previously 'invalid-strings' would be handled, but integers would just
return 500.
2021-05-19 08:57:31 +01:00
Ben Thorner
a91fde2fda Run auto-correct on app/ and tests/ 2021-03-12 11:45:45 +00:00
Leo Hemsted
156c7aa32a bump python client
brings in jwt2.0 compat
2020-12-31 13:56:04 +00:00
Chris Hill-Scott
3b0b96834d Do extra code style checks with flake8-bugbear
Flake8 Bugbear checks for some extra things that aren’t code style
errors, but are likely to introduce bugs or unexpected behaviour. A
good example is having mutable default function arguments, which get
shared between every call to the function and therefore mutating a value
in one place can unexpectedly cause it to change in another.

This commit enables all the extra warnings provided by Flake8 Bugbear,
except for:
- the line length one (because we already lint for that separately)
- B903 Data class should either be immutable or use `__slots__` because
  this seems to false-positive on some of our custom exceptions
- B902 Invalid first argument 'cls' used for instance method because
  some SQLAlchemy decorators (eg `declared_attr`) make things that
  aren’t formally class methods take a class not an instance as their
  first argument

It disables:
- _B306: BaseException.message is removed in Python 3_ because I think
  our exceptions have a custom structure that means the `.message`
  attribute is still present

Matches the work done in other repos:
- https://github.com/alphagov/notifications-admin/pull/3172/files
2020-12-22 16:26:45 +00:00
Leo Hemsted
9502f17d84 flake8 fixes
a stricter flake8 bump. mostly things around f strings and format
strings, but a couple of bad placeholder names in loops
2020-12-07 15:24:02 +00:00
Chris Hill-Scott
3ffdb3093b Revert "Revert "Merge pull request #2887 from alphagov/cache-the-serialised-things""
This reverts commit 7e85e37e1d.
2020-06-26 14:10:12 +01:00
Chris Hill-Scott
7e85e37e1d Revert "Merge pull request #2887 from alphagov/cache-the-serialised-things"
This reverts commit b8c2c6b291, reversing
changes made to 351aca2c5a.
2020-06-26 13:42:44 +01:00
Chris Hill-Scott
6a9818b5fd Cache services and API keys in memory
Same as we’ve done for templates.

For high volume services this should mean avoiding calls to external
services, either the database or Redis.

TTL is set to 2 seconds, so that’s the maximum time it will take for
revoking an API key or renaming a service to propagate.

Some of the tests created services with the same service ID. This
caused intermittent failures because the cache relies on unique service
IDs (like we have in the real world) to key itself.
2020-06-24 08:46:13 +01:00
Chris Hill-Scott
320bca70f7 Serialise service, API keys and permissions
By serialising these straight away we can:
- not go back to the database later, potentially closing the connection
  sooner
- potentially cache the serialised data, meaning we don’t touch the
  database at all
2020-06-23 16:00:41 +01:00
David McDonald
2dc5550159 Change variable name to make more descriptive
Also remove unnecessary if statement
Also add manifest change to make sure relevant environment variables
makes it into the app
2020-02-20 15:48:15 +00:00
David McDonald
2967fdce08 Make test more accurate
So we are really testing the functionality the test says it is, rather
than potentially being misled by using an incorrect key as the secret
2020-02-20 13:49:31 +00:00
David McDonald
7246306447 Support multiple secrets for ADMIN_CLIENT_SECRETS
This will allow us to accept two different ones and therefore allow us
to rotate the secret that the admin client is sending to the API

Due to how the notifications-python-client throws exceptions, we run
into exactly the same issue with not being able to distinguish if a
`TokenDecodeError` is thrown because the token was encrypted with a
different secret key or if because there was a different error when
decoding. I've copied the TODO from `requires_auth` as this is exactly
the same issue.

I've also added a test case for functionality that was missing for an
out of date admin token (old IAT).
2020-02-20 13:47:39 +00:00
David McDonald
52d3df49d4 Make ADMIN_CLIENT_SECRET a list of a single secret
And support this change across our code. Note, this is a halfway step
where it is not a list rather than a string but still only supports a
single secret, ie one item in the list.
2020-02-20 13:43:10 +00:00
David McDonald
f861da1843 Improve text for error messages 2020-02-14 14:15:41 +00:00
David McDonald
ba21d04080 Add test case for uncaught jwt exception 2020-02-14 14:10:12 +00:00
David McDonald
946ba993b5 Catch TokenAlgorithmError
Instead of letting it go uncaught and causing an error, we now show the
user an appropriate error message.
2019-12-12 10:23:28 +00:00
Athanasios Voutsadakis
463f1eefaf Move proxy header check to auth-requiring endpoints
The main drive behind this is to allow us to enable http healthchecks on
the `/_status` endpoint. The healthcheck requests are happening directly
on the instances without going to the proxy to get the header properly
set.

In any case, endpoints like `/_status` should be generally accessible by
anything without requiring any form of authorization.
2018-03-27 17:37:09 +01:00
Leo Hemsted
a0b87396ef change token expiry err msg for clarity 2017-12-20 13:57:34 +00:00
Leo Hemsted
32a6f44d3a update tests to use new response messages
while it doesn't strictly make sense for the error situations, these
are not typical end user errors - they're about malformed requests.
The typical use case is "api key was revoked" or similar - so that
should be the default error message
2017-12-20 13:57:34 +00:00
Leo Hemsted
687cf8526b log service id and api key id during auth
example log line:
```
API AuthError: AuthError({'token': ['Invalid token: signature, api token is not valid']}, 403, service_id=3e1ed7ea-8a05-4b4e-93ec-d7bebfea6cae, api_key_id=None)"
```
2017-12-20 13:57:34 +00:00
venusbb
568dcaa63d remove ip whitelist inbound sms codes 2017-12-18 10:25:37 +00:00
Leo Hemsted
08494ef206 more flake8. lots of unused imports and variables that didn't get used. i tried to preserve old variable names as comments when it looked like they were useful (eg when they were describing timestamps) 2017-11-28 17:23:09 +00:00
Athanasios Voutsadakis
26ba4dad06 Parametrize tests 2017-11-16 17:00:38 +00:00
Athanasios Voutsadakis
5d687d87e7 Enable header checking on preview and staging, add test 2017-11-16 12:02:09 +00:00
Athanasios Voutsadakis
7a24cd8753 Typos 2017-11-14 16:04:04 +00:00
Athanasios Voutsadakis
0f696aa3e8 Use function from utils to check secret header value
This adds a before_request handler to check whether all incoming
requests have the proxy header configured.
2017-11-14 14:26:00 +00:00
venusbb
03fc781b8c create new method to validate secret header, new tests 2017-11-06 11:56:57 +00:00