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

Fix constance being accessed before database is ready and other fixes #341

Merged
merged 9 commits into from
Nov 15, 2024

Conversation

gsnider2195
Copy link
Contributor

@gsnider2195 gsnider2195 commented Nov 13, 2024

Closes #340

What's Changed

  • Remove conditional logic for urlpatterns based on constance settings
  • Update existing API views that were affected by the above change to return a 404 if the integration is not enabled
  • Remove conditional logic for adding grafana navigation menu items
    • We can't do this at init because it needs to access the database
    • We can't modify the nav menu during runtime because core doesn't support removing nav menu entries dynamically
  • Add a "grafana disabled" view in case a user clicks on a grafana nav menu item when the grafana integration is disabled
  • Fix multiple bugs in the grafana views
    • Incorrect view names
    • Incorrectly using a reference to the model instead of the model itself
    • Delete all grafana API files since there we no API views provided by grafana integration
    • Fixed Nautobot v2.3 incompatibility caused by saved views not being able to determine the models' table classes
  • Fix dev environment nautobot_config.py to fall back to constance if environment variable is not used
  • Fix mattermost, msteams, slack and webex integration API views that were not requiring user to be logged in
  • Add exception handling for cases when diffsync is not installed, since it's marked as optional

Screenshots

image

@glennmatthews
Copy link
Contributor

I'm curious about:

Fix mattermost, msteams, slack and webex integration API views that were not requiring user to be logged in

These are the endpoints for receiving messages from the chat platform, aren't they? I wouldn't expect them to require Nautobot user authentication.

Comment on lines +58 to +61
return TemplateResponse(
request=self.request,
template="nautobot_chatops_grafana/grafana_disabled.html",
context={},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should probably be a redirect to another view but this works.

@@ -22,6 +28,9 @@ def run_dashboard_sync(overwrite: bool = False) -> Union[str, None]:
Args:
overwrite (bool): Overwrite Nautobot data and delete records that are no longer in Grafana.
"""
if not DIFFSYNC_INSTALLED:
raise ImportError("DiffSync is not installed. Please install DiffSync to use the grafana integration.")
Copy link
Contributor

Choose a reason for hiding this comment

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

ImportError for these calls feels weird to me - shouldn't it be something like RuntimeError?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was going to use RuntimeError but then I decided to replicate the exception you would get if you tried to import diffsync in this function. It seemed more appropriate to use ImportError here but I'm happy to change it to RuntimeError if you think that's better here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would be my preference, but I don't feel strongly about it.

Might be good either way to add a Raises: section to the docstring documenting this behavior?

@smk4664
Copy link
Contributor

smk4664 commented Nov 13, 2024

I'm curious about:

Fix mattermost, msteams, slack and webex integration API views that were not requiring user to be logged in

These are the endpoints for receiving messages from the chat platform, aren't they? I wouldn't expect them to require Nautobot user authentication.

Yeah, these need to be reverted. The API for these views is for the chat platform to communicate with Nautobot. Each of these views have their own way to authenticate the system. For instance, Slack signs their requests.

@gsnider2195
Copy link
Contributor Author

I'm curious about:

Fix mattermost, msteams, slack and webex integration API views that were not requiring user to be logged in

These are the endpoints for receiving messages from the chat platform, aren't they? I wouldn't expect them to require Nautobot user authentication.

Yeah, these need to be reverted. The API for these views is for the chat platform to communicate with Nautobot. Each of these views have their own way to authenticate the system. For instance, Slack signs their requests.

That's simple enough to do. I didn't have the context for why these were left wide open. These should probably also be changed to rest_framework views at some point instead of django views?

changes/341.housekeeping Show resolved Hide resolved
Comment on lines -32 to -35
except Exception:
grafana_urlpatterns = []
logger = logging.getLogger(__name__)
logger.warning("Grafana ChatOps integration is not available.", exc_info=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering - were there cases where importing the grafana URLs might fail here (missing optional dependency, etc.) that we need to handle still?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably. Our dockerfile installs with --extras all. I'll test without that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like I need to add more import guardrails 😞

If I just put this exception handling back in we get a broken nav menu.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is fixed now. Discussed the implementation in DSU and decided not to spend too much time on it since ideally nobody will ever encounter this.

@glennmatthews
Copy link
Contributor

I'm curious about:

Fix mattermost, msteams, slack and webex integration API views that were not requiring user to be logged in

These are the endpoints for receiving messages from the chat platform, aren't they? I wouldn't expect them to require Nautobot user authentication.

Yeah, these need to be reverted. The API for these views is for the chat platform to communicate with Nautobot. Each of these views have their own way to authenticate the system. For instance, Slack signs their requests.

That's simple enough to do. I didn't have the context for why these were left wide open. These should probably also be changed to rest_framework views at some point instead of django views?

They don't use DRF serializers AFAIK so I'm not sure why they would be changed over?

@gsnider2195
Copy link
Contributor Author

I'm curious about:

Fix mattermost, msteams, slack and webex integration API views that were not requiring user to be logged in

These are the endpoints for receiving messages from the chat platform, aren't they? I wouldn't expect them to require Nautobot user authentication.

Yeah, these need to be reverted. The API for these views is for the chat platform to communicate with Nautobot. Each of these views have their own way to authenticate the system. For instance, Slack signs their requests.

That's simple enough to do. I didn't have the context for why these were left wide open. These should probably also be changed to rest_framework views at some point instead of django views?

They don't use DRF serializers AFAIK so I'm not sure why they would be changed over?

Just so they don't have to manually disable csrf checks. This is the first place I've seen @method_decorator(csrf_exempt, name="dispatch") on an API view.

from .integrations.grafana.models import Dashboard as GrafanaDashboard
from .integrations.grafana.models import Panel as GrafanaPanel
from .integrations.grafana.models import PanelVariable as GrafanaPanelVariable
from .integrations.grafana.models import GrafanaDashboard, GrafanaPanel, GrafanaPanelVariable
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh good, I am glad that we can get rid of the rename. I am not sure why this wasn't done when originally implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to leave it in for the diffsync code because the diffsync models are called GrafanaDashboard and GrafanaPanel and I didn't want to go down another rabbit hole trying to fix those...

Copy link
Contributor

@smk4664 smk4664 left a comment

Choose a reason for hiding this comment

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

Thank you for the fixes!

@gsnider2195 gsnider2195 merged commit 4761790 into develop Nov 15, 2024
14 checks passed
@gsnider2195 gsnider2195 deleted the u/gas-340-constance-fix branch November 15, 2024 16:15
@gsnider2195 gsnider2195 mentioned this pull request Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Constance is being accessed before database is ready
3 participants