Skip to content

Turn SAML year into a path parameter (LG-7554)#7153

Merged
zachmargolis merged 25 commits intomainfrom
margolis-year-path-param
Apr 13, 2023
Merged

Turn SAML year into a path parameter (LG-7554)#7153
zachmargolis merged 25 commits intomainfrom
margolis-year-path-param

Conversation

@zachmargolis
Copy link
Contributor

🎫 Ticket

LG-7554

🛠 Summary of changes

By using Rails router's constraints and path params syntax, we can simplify parsing of URLs and also simplify the defintion of our routes

📜 Testing Plan

Specs pass

@zachmargolis zachmargolis requested a review from a team October 17, 2022 15:41
get "/api/saml/metadata#{suffix}" => 'saml_idp#metadata', format: false
match "/api/saml/logout#{suffix}" => 'saml_idp#logout', via: %i[get post delete]
match "/api/saml/remotelogout#{suffix}" => 'saml_idp#remotelogout', via: %i[get post]
constraints(path_year: SamlEndpoint.suffixes) do
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a pretty cool feature I wasn't aware of, but I'm at a loss for how it even works. I don't see any documentation for it, and the closest I got to thinking I understood it was thinking it was implicitly calling Array#matches?, but matches? doesn't appear to be a method on an array.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the Rails Routing guide, I pieced this together from segment constraints and the constraints block form in

So the block form is just a way to avoid repeating the segment constraints for each line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and in terms of implementation, I bet that it's using our favorite === which for strings and regexes is matches, but for arrays is "contains"

Conflicts:
	app/controllers/saml_completion_controller.rb
	spec/controllers/saml_completion_controller_spec.rb
	spec/services/saml_endpoint_spec.rb
	spec/support/saml_auth_helper.rb
@zachmargolis zachmargolis reopened this Apr 12, 2023
@zachmargolis
Copy link
Contributor Author

img
giving this another shot!

@zachmargolis zachmargolis requested a review from aduth April 12, 2023 23:49
@julialeague
Copy link
Contributor

Excellent!

Suggestion: Can we add a test to the SAML request spec file to check for a 404 being rendered when an unsupported path year is passed for a POST request?

And maybe the same kind of test for a SAML GET auth request, to see that the routing constraint works as expected?

Copy link
Contributor

@julialeague julialeague left a comment

Choose a reason for hiding this comment

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

👍 outside of my suggestion!

@zachmargolis
Copy link
Contributor Author

Excellent!

Suggestion: Can we add a test to the SAML request spec file to check for a 404 being rendered when an unsupported path year is passed for a POST request?

And maybe the same kind of test for a SAML GET auth request, to see that the routing constraint works as expected?

Great ideas, added in ce4dfda and be810ff

Copy link
Contributor

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Solid improvement 👍

prepend_before_action :skip_session_expiration, only: [:metadata, :remotelogout]

skip_before_action :verify_authenticity_token
before_action :require_path_year
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary? Given the defined constraints, is it possible that a path would be matched without a path year?

If it is necessary, do we have any spec coverage for this?

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 forget

I think that it's basically only required in specs, because in controller tests you can route directly to an action and skip the routers constraints, but in real life the router handles them.

I'll toy around with removing it and see what happens

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Confirmed, it was required because the controller specs let us route directly.

query_params = Rack::Utils.parse_nested_query url.query
url = URI.parse(request.original_url)

# we need to grab just the .path, in case the full value includes query params like ?locale=
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have spec coverage for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the reason I added it was that feature specs in other locales ran into exceptions here

@zachmargolis zachmargolis merged commit 32739ac into main Apr 13, 2023
@zachmargolis zachmargolis deleted the margolis-year-path-param branch April 13, 2023 16:40
@solipet solipet mentioned this pull request Apr 13, 2023
jc-gsa pushed a commit that referenced this pull request Apr 19, 2023
* Turn SAML year into a path parameter (LG-7554)
* Clean up SamlIdpController to use parsed param instead of parsing URL directly
* Add before filter to catch missing path year
* Move path_year to a constant, reference it
* Fix generated url that includes query params
* Review feedback: update saml_requests_spec.rb to check for out-of-bounds years
* Review feedback: add specs for get request too
* Remove unused method
* Bring back stricter route check
* Fix usage of domain/host with url helper methods
* Fix url helper method call
* Simplify auth helper methods

changelog: Internal, SAML, Update SAML API URLs to make annual certificate rotation easier

---------

Co-authored-by: Andrew Duthie <andrew.duthie@gsa.gov>
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.

4 participants