Problem/Motivation
I have Key and Shield modules installed for a project and I want to use Shield with credentials stored outside the database (in a .key file). The configured .key file's location is set as "private://my_key_file.key" (stream wrapper is allowed here). When getKeyValue() is invoked (in src/Plugin/KeyProvider/FileKeyProvider.php), the key file gets checked for existence and readability. But it always returns FALSE since it can't recognise the path "private://..." (bootstrap still not loaded since this is HTTP Auth provided by Shield). When absolute path is used (like /var/www/files-private), it works. But I need the stream wrapper path for my case.
Steps to reproduce
Add a key in the Key module (Section "Type settings" > Key type > "User/password"; Section "Provider Settings" > Key provider > "File" and for "File location", add "private://..." or "public://..."), and use the same key when configuring Shield (Credentials > Credential Provider > "Key module (User/password)" and under "User/password", select your key).
Proposed resolution
We can get the file's real path by just adding \Drupal::service('file_system')->realpath($file) when the stream wrapper is used.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | Screenshot 2025-04-25 at 11.02.17 AM.png | 109.51 KB | rajeshreeputra |
| #8 | 3300224-key-file-location-stream-wrapper-3.patch | 1.01 KB | kmakaveev |
| #2 | 3300224-key-file-location-stream-wrapper.patch | 702 bytes | kmakaveev |
Issue fork key-3300224
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3300224-add-stream-wrapper
changes, plain diff MR !47
- 3300224-key-file-not
changes, plain diff MR !6
Comments
Comment #2
kmakaveev commentedComment #3
angel.hWe should not limit this to the public and private schemas. Maybe you should use something like:
Comment #6
anoopsingh92Hi @angel.h, I have updated your changes in the MR6.
I have raised an MR please review if it is fine then please merge it.
MR: https://git.drupalcode.org/project/key/-/merge_requests/6
Thanks
Comment #7
kmakaveev commentedComment #8
kmakaveev commentedSorry, I missed to use the class StreamWrapperManager in the patch. Fixed and updated.
Comment #9
kmakaveev commentedComment #10
anoopsingh92Thanks for informing @kmakaveev, I am updating your changes in MR. Thanks
Comment #11
anoopsingh92MR #6 updated. Thanks
Comment #12
cmlaraI will note that the use of realPath() is discouraged. See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21File%21Fi...
I'm not sure how often a site may want a key file on a remote streamWrapper, however if this change is accepted as-is it could break existing deployments.
At a minimal I believe this may need some work to ensure it does not impact remote streamWrappers.
Adjusting title to capture that this should only an issue before bootstrap completes.
Comment #14
rajeshreeputraWe can use the
realpath()method of FileSystem.I have validated the MR changes with shield module and it work as expected. Hence requesting review.
Comment #15
cmlaraGiven the following documentation of the API Method can you provide more justification?
— FileSystemInterface::realpath()
Does the key module know that a path is a local filesystem? Is this a rare case? If so why? Does the module need to pass an absolute path to a function? If so which function only accepts absolute paths?
Comment #16
ankitv18 commentedMinor suggestion ~~ Please check, moving into NW
Comment #17
rajeshreeputraHello @cmlara,
I am currently experiencing an issue when provided file path located on a different server or outside the Drupal project scope, as it is returning an following error. Hence I mentioned that we can use the
realpath()method of FileSystem.Could you please let me know if there's anything I'm missing that would enable me to provide a file path located on a different server or outside the Drupal project scope?
Comment #18
cmlaraUsing the S3fs module with the file at s3://test.txt would be an example that works key when this patch is not applied. Once this patch enforces realpath I would expect the following to no longer function.
Comment #19
rajeshreeputraHello @cmlara, updated to work with realpath, stream wrapper and remote urls. Could you please validate with s3 once. Will keep this in needs work status.
Comment #20
rajeshreeputraI have not yet utilized the s3fs module in Drupal, but I will give it a try. Then accordingly update here.
Comment #21
vishalkhode commentedIn !MR 6 Looks like there's a complete new feature has been introduced i.e Provide support for external file url for key. I'm not sure if this should be supported or not. In any case, this should be implemented and discussed separately. We should limit this ticket to fix the bug only.
Comment #23
rajeshreeputraWith the understanding how the
public://andprivate://stream wrappers function with S3FS. Will close the MR!6(keeping for future reference) and create new one.Comment #25
rajeshreeputraCleanup completed, requesting review.
Referencing existing issue to address PHPStan related warnings - #3520663: Fix PHPStan warnings.
Comment #26
cmlaraFrom MR comments it appears this should be Postponed on #3356052: KeyProviderInterface::getKeyValue() doesn't always return a string as there is question if NULL is an acceptable return type.
It is used in one location in existing code however is not documented in the API to be allowed.
Until a decison is made to update the API new deviations should likely not be added. (Avoid creating more work for others if the API is correct.)
Comment #27
rajeshreeputraSure @cmlara, will start work and discussion on #3356052: KeyProviderInterface::getKeyValue() doesn't always return a string.
Comment #28
rajeshreeputraBased on the ongoing discussions in the referenced issues, will proceed with the exact ask of this issue without introducing new validations.
Requesting review.
Comment #29
chandu7929 commentedOverall, the changes in MR!47 look good to me. I validated the behavior using both private://myfile.key and public://myfile.key paths, and was able to retrieve the key value when using this patch. However, without the patch, the value returns null. Therefore, marking this as RTBC.
Comment #30
mxr576Since constructor arguments changed on a public class, I think this deserves a change record.
Comment #31
rajeshreeputraComment #32
rajeshreeputraDraft CR - #3559966: FileKeyProvider now uses dependency injection for FileSystemInterface and improved stream wrapper handling please review.
Comment #33
mxr576I think the CR is too lengthy compared with other announcements about new constructor arguments - I assume the LLM went too far here :)
I expected nothing more than
* https://www.drupal.org/node/3404140
* https://www.drupal.org/node/3440755
* https://www.drupal.org/node/3419963
Comment #34
mxr576The code looks for me.