Refactor: merge fields from EligibilityApiConfig into EligibilityApiVerificationRequest - #3441
Conversation
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
c68c4f1 to
9a273fb
Compare
| api_auth_key_secret_name = SecretNameField( | ||
| help_text="The name of a secret containing the value of the auth header to send in Eligibility API requests.", | ||
| ) | ||
| api_client_private_key = models.ForeignKey( |
There was a problem hiding this comment.
Regarding the naming, I tried to keep the 3 key-related fields sort of consistent.
There was a problem hiding this comment.
Take it or leave it, but these two fields could be simplified to just client_*_key (e.g. dropping the api_ prefix).
Might help differentiate from the fields that are more about the server (API) config vs. Benefits (client) config.
There was a problem hiding this comment.
Ah thanks for this suggestion! I like this simplification and I made the change in 4504964 instead of going back to the original commit to hopefully make reviewing this change easier.
| agency = kwargs.get("agency") | ||
| return HttpResponse(agency.eligibility_api_public_key_data, content_type="text/plain") | ||
| eligibility_api_public_key_data = EligibilityApiVerificationRequest.objects.first().api_client_public_key_data | ||
| return HttpResponse(eligibility_api_public_key_data, content_type="text/plain") |
There was a problem hiding this comment.
It's interesting how the change in this PR affects this view/URL.
First, as seen from this code, eligibility_api_public_key_data is not agency-related, so the URL <agency:agency>/publickey does not really need the agency argument anymore. I didn't remove it (yet) because I wanted to double check if there are any other systems (like eligibility server GH Actions, for example) that would still expect to include the agency argument. If there are none, the URL can be simplified.
The second thing, I think that this indirectly also shows that the api_client_public_key (and api_client_private_key) fields can even be removed from EligibilityApiVerificationRequest because they don't depend on the EV server instance Benefits communicates with, these 2 fields are used by the whole Benefits application, so maybe we can just use the PemData model directly? If it makes sense, it could be a followup refactor.
There was a problem hiding this comment.
These are both great points and I think we can simplify this significantly. I'd argue a follow-up issue/PR probably makes the most sense but open to doing it here as well.
We can see in the config files on the MST and SBMTD EV servers this is a simple path:
# In MST's case
CLIENT_KEY_PATH = "https://benefits.calitp.org/mst/publickey"
# In SBMTD's case
CLIENT_KEY_PATH = "https://benefits.calitp.org/sbmtd/publickey"And going to a simpler top-level approach seems pretty easy to do, and also reduces code
CLIENT_KEY_PATH = "https://benefits.calitp.org/publickey"Since we don't have an "app configuration" model (something we have kicked around in the past, but not totally necessary at least for this) we'll just want to be careful with how the query for PemData works, to ensure we always get exactly just the public key version.
There was a problem hiding this comment.
If this feels more related to the merging of those 2 models into EligibilityApiVerificationRequest it definitely feels in scope to me to do it as part of this PR. FWIW.
There was a problem hiding this comment.
Thanks for talking through these points with me @thekaveman. We agreed that addressing the two observations from the comment above (#3441 (comment)) wouldn't do much good at this point, we can keep the organization as-is.
On observation 1, it's not wrong to keep the agency argument in the URL. Conceptually this is still correct, it's just that the client public key is shared among all agencies, so it's ok to request a key to a particular agency, even if the view associated with the URL does not end up using the agency argument. A small clarification was added as an in-line comment on this point.
On observation 2, an "app configuration" model is not necessary at this point since it would only hold 2 fields, and it seems to add more complication and not much benefit. Without this extra model, we couldn't identify a way to select the correct PemData instance corresponding to the encryption key we need to fetch. Because of this, we should keep the client API encryption keys in EligibilityApiVerificationRequest along with the EV server's public key. They are neatly organized under the same model.
|
I note the autofix run from I wouldn't have expected that, was your devcontainer fully rebuilt on the latest |
2bfd2c3 to
f19735e
Compare
Thanks for pointing this out! For some reason my dev container is not applying |
thekaveman
left a comment
There was a problem hiding this comment.
Some comments, looking good overall.
| api_auth_key_secret_name = SecretNameField( | ||
| help_text="The name of a secret containing the value of the auth header to send in Eligibility API requests.", | ||
| ) | ||
| api_client_private_key = models.ForeignKey( |
There was a problem hiding this comment.
Take it or leave it, but these two fields could be simplified to just client_*_key (e.g. dropping the api_ prefix).
Might help differentiate from the fields that are more about the server (API) config vs. Benefits (client) config.
move api_private_key and api_public_key fields from EligibilityApiConfig to EligibilityApiVerificationRequest
move api_private_key and api_public_key data getters from TransitAgency to EligibilityApiVerificationRequest. Also update usage of the moved fields.
use TransitAgency.slug instead
remove the field since it is not used anymore
since the DeleteModel step already deletes the table from the database, this step is redundant.
drop the "api_" prefix from EligibilityApiVerificationRequest fields to help differentiate fields that are more about the server (API) config vs. Benefits (client) config.
since EligibilityApiConfig will be deleted, these steps are redundant and can be removed to clean up the migration.
4504964 to
d832b65
Compare
thekaveman
left a comment
There was a problem hiding this comment.
Nice work @lalver1! I went through all the steps and everything worked as expected (except for In-Person, since I couldn't configure agency_card for that and didn't want to mess around with why).
Closes #3397
This PR moves fields that are not specific to a transit agency to
EligibilityApiVerificationRequestand deletesEligibilityApiConfigsince it is no longer needed.Follow-ups
Reviewing
main, set yourdevenvironment to use either the Littlepay or Switchiodevfixturesbin/reset_db.sh) to simulate a running Benefits instancebin/init.shto simulate what a running Benefits instance will do when the code in this PR is deployedapi_private_keyandapi_public_keywere moved toEligibilityApiVerificationRequestand that they have the correct data, i.e.,api_client_private_keypoints to theBenefits client private keyPemDatainstance andapi_client_public_keypoints to theBenefits client public keyPemDatainstanceeligibility_api_configfield was removed fromTransitAgencyand that theEligibilityApiConfiginstances were deleted from the databaseEligibilityApiConfigmodel was removed and that the fields in steps 5 and 6 show up in the correct modelhttp://localhost/cst/publickeyURL and ensure that you can still see Benefits' public keyTo review the dev container
local_fixtures.jsonfixturesbin/reset_db.sh) and ensure the database is successfully recreatedlocal_fixtures.jsonare not set up for enrollment)