Commit Graph

82 Commits

Author SHA1 Message Date
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
venusbb
f4d005c7fb initial logging for route protection 2017-11-03 14:43:56 +00:00
venusbb
9ad75ff726 Tests modified 2017-09-27 10:15:29 +01:00
venusbb
6a7013fa7a Enable Inbound sms IP blocking 2017-09-26 10:59:09 +01:00
venusbb
160b878745 Minor change in how we inteprete Incoming IP 2017-09-13 17:23:23 +01:00
venusbb
9efc17a941 Use ipaddress library for the masked bits 2017-09-13 14:08:23 +01:00
venusbb
c285ab0b45 inbound sms monitoring 24bit mask 2017-09-13 11:29:11 +01:00
venusbb
a5cf8ff60f put more log messages to view what env returns 2017-07-12 13:49:20 +01:00
venusbb
5d57189187 changed name of test fixture 2017-07-11 09:50:09 +01:00
venusbb
5089a4d53b retrieve sms ip whitelist from credentials on paas 2017-07-10 17:03:43 +01:00
venusbb
1d8d6b1ef1 Add list of IP address that X-Forwarded-For return to add more visibility to the traffic 2017-07-06 12:31:00 +01:00
venusbb
a307fa6dcd Check ip unit test and modify ways to parse IP address 2017-07-06 12:30:09 +01:00
venusbb
c182ceca90 Check ip unit test and modify ways to parse IP address 2017-07-06 12:30:08 +01:00
Rebecca Law
78242812ef Register a before_request event for all blueprints, that defines the authentication requirement.
There are three authentication methods:
 - requires_no_auth - public endpoint that does not require an Authorisation header
 - requires_auth - public endpoints that need an API key in the Authorisation header
 - requires_admin_auth - private endpoint that requires an Authorisation header which contains the API key for the defined as the client admin user
2017-03-16 18:15:49 +00:00
Rebecca Law
f880604c85 First attempt at securing the endpoints.
Started with adding a before_request event to the service_blueprint, which executes the requires_admin_auth method rather than the require_auth method.

Obviously this is not done but want to get this in front of people to get an opinion.
2017-03-16 10:42:45 +00:00
Chris Hill-Scott
b6b9b3b225 Give a more helpful error when token has expired
We’ve seen quite a few developers encounter the `Invalid token: expired`
error message when they’re getting started using the Notify API. When
this happens they either raise a support ticket or ask for help on
Slack.

In every case this has been because the clock on their
machine/environment/container isn’t accurate. The error message doesn’t
help them figure this out.

This commit adds extra detail to the error message so they can fix the
problem without having to come to us for help.
2017-01-17 10:44:00 +00:00
Leo Hemsted
17cf582502 Merge branch 'master' into active-service 2016-11-10 13:54:02 +00:00
Leo Hemsted
abecb5ff98 Merge pull request #726 from alphagov/auth-500
update python client to 2.0.0
2016-11-10 13:47:34 +00:00
Leo Hemsted
e8c3a5cdde add check for inactive services to auth handler
cleaned up some auth code to marginally improve efficiency of error checking
and hopefully make it easier to read

fixed some incorrect auth headers in the deactivate tests
2016-11-10 11:07:12 +00:00
Chris Hill-Scott
9248e72c50 Make bearer prefix on auth header case insensitive
From a support ticket:

> the "Bearer" prefix on the auth header is case sensitive. Can this be
> made case-insensitive?

Sure can 🙃
2016-11-07 10:49:05 +00:00
Leo Hemsted
f089b75129 update python client to 2.0.0
this is to prevent 500 errors because <2.0.0 raised AssertionError
if supplied JWT tokens were incorrectly formatted

tests added
2016-11-03 17:05:25 +00:00