Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication setup #316

Merged

Conversation

Josephine-Rutten
Copy link
Collaborator

@Josephine-Rutten Josephine-Rutten commented Sep 19, 2023

Based on ticket: #310

We implemented the OAuth login in the backend.

We still need to make changes on the frontend. We also need to make a unit test for some functions.

@Josephine-Rutten Josephine-Rutten changed the title auth setup Authentication setup Sep 19, 2023
return oauth_client.connext.authorize_redirect(redirect_uri)
except Exception as e:
logger.error("Exception during authorized redirect: {}".format(str(e)))
return empty_result(status="error", data="Exception during authorized redirect."), 500
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to return a 401 and/or a 403 if you receive it from the auth service and otherwise a 503 temp unavailable as there is no real backend ISE that we can fix.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubled checked and we can't get to this exception, unless our env variables are wrong so for now didn't catch the exception at all.

token = oauth_client.connext.authorize_access_token()
except Exception as e:
logger.error("Exception during authorization of the access token: {}".format(str(e)))
return empty_result(status="error", data="Exception during authorization of the access token."), 500
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, probably best to handle the 401 and 403 case transparently and otherwise return a 503 as the auth service is probably down.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't ever get a specific error code with this. The only error I could find is the MismatchingStateError which comes when the state is not matching (I can trigger this by calling this api call directly). I catched that specific error.

OIDC_CONF_WELL_KNOWN_URL: str = "well-known-openid-configuration-endpoint"
OIDC_CLIENT_SECRET: str = "xxx"
OIDC_CLIENT_ID: str = "client-id"
FE_CALLBACK_URL: str = "http://localhost/callback"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FRONTEND_CALLBACK_URL

except exceptions.JWKError:
try:
# with this exception, we first try to reload the keys
logger.info('JWT.decode didnt work. Get the keys and retry.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move to debug level?

try:
# with this exception, the call was too fast or the computer too slow
logger.info('Checked token too fast (or computer is too slow), retry')
sleep(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably your local machine ;) So I think we can remove this.

if not api_settings.JWT_ENABLED:
return "admin"
# request the userinfo
url = auth_settings.OIDC_CONF_WELL_KNOWN_URL.split('.well-known')[0] + 'oidc/userinfo'
Copy link
Contributor

@pboers1988 pboers1988 Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might not be oidc/userinfo in all cases. This is retrievable by reading the .well-know configuration. so probably something likt this:

`
metadata = requests.get(settings.OIDC_WELL_KNOWN_ENDPOINT)
user_info_endpoint = metadata.json()["userinfo_endpoint"]

`

try:
# with this exception, we first try to reload the keys
logger.info('JWT.decode didnt work. Get the keys and retry.')
response = requests.get(url=auth_settings.OIDC_CONF_WELL_KNOWN_URL.split('.well-known')[0] + 'oidc/certs')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as below, should be dynamic

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed it to follow your recommendation.

@Josephine-Rutten Josephine-Rutten marked this pull request as ready for review November 6, 2023 08:58
Copy link

codecov bot commented Nov 15, 2023

Codecov Report

Attention: 141 lines in your changes are missing coverage. Please review.

Comparison is base (2a2a173) 65.46% compared to head (308c0b5) 64.61%.
Report is 8 commits behind head on develop.

Files Patch % Lines
src/cnaas_nms/tools/security.py 20.58% 81 Missing ⚠️
src/cnaas_nms/api/app.py 38.29% 29 Missing ⚠️
src/cnaas_nms/api/auth.py 48.78% 21 Missing ⚠️
src/cnaas_nms/api/mgmtdomain.py 66.66% 4 Missing ⚠️
src/cnaas_nms/api/device.py 92.30% 2 Missing ⚠️
src/cnaas_nms/api/linknet.py 84.61% 2 Missing ⚠️
src/cnaas_nms/api/jobs.py 85.71% 1 Missing ⚠️
src/cnaas_nms/app_settings.py 95.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #316      +/-   ##
===========================================
- Coverage    65.46%   64.61%   -0.85%     
===========================================
  Files           66       67       +1     
  Lines         7325     7509     +184     
===========================================
+ Hits          4795     4852      +57     
- Misses        2530     2657     +127     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot E 1 Security Hotspot
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

src/cnaas_nms/tools/security.py Dismissed Show dismissed Hide dismissed
@Josephine-Rutten Josephine-Rutten self-assigned this Dec 21, 2023
@indy-independence
Copy link
Member

I added two comments here, but I got this working with our Sunet SATOSA setup now :)

server_metadata_url=auth_settings.OIDC_CONF_WELL_KNOWN_URL,
client_id=auth_settings.OIDC_CLIENT_ID,
client_secret=auth_settings.OIDC_CLIENT_SECRET,
client_kwargs={"scope": "openid"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For our satosa server to work with this we seem to need to modify this from just "openid" to "openid email" so we can get the email for logging etc later

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I've added it to the app_setting, so it can be set as an env variable or in the auth_config.yaml

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah great! Can we add something about the new config options on this documentation page also? docs/configuration/index.rst

if not auth_settings.OIDC_ENABLED:
return "Admin"
# Request the userinfo
metadata = requests.get(auth_settings.OIDC_CONF_WELL_KNOWN_URL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one more error that I think it would be good if we catch, if the metadata is unavailable when doing any API request the client will get back message: "Internal server error", if we catch requests.exceptions.ConnectionError here and return something like "OIDC metadata unavailable: ConnectionError" (or whatever error from str(e) )

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had troubles reproducing the error, so I had troubles testing it. I catched the error you mentoined and another error I got instead. Let me know how you produced this error if the current changes are not catching the error you meant.

Copy link

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@indy-independence
Copy link
Member

Tested with SATOSA builtin OIDC with opaque access_token and new https://github.com/UniversitaDellaCalabria/SATOSA-oidcop/tree/main using JWT access_token both working!

@indy-independence indy-independence merged commit 0e22aec into SUNET:develop Jan 16, 2024
3 of 5 checks passed
@indy-independence indy-independence added this to the v1.6.0 milestone Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants