-
Notifications
You must be signed in to change notification settings - Fork 249
Handle parameter escaping internally in cf-create-service.ps1 #439
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
base: main
Are you sure you want to change the base?
Conversation
|
@copilot, the username will also commonly have a backslash as well, and I suppose there's no guarantee a password wouldn't either. Add the escaping to those two params as well. Also evaluate whether additional characters could break this script, like single or double-quotes - can those be handled as well? Assuming there are additional characters that need to be escaped, consider using a built-in powershell function (it would be acceptable to also adjust the script to require pwsh 7+ to minimize compatibility concerns) or refactoring to avoid duplicating the escape logic |
Done in commit f10b9b5. The script now uses PowerShell's
Added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR improves the usability of the cf-create-service.ps1 script by handling JSON parameter escaping internally instead of requiring users to manually escape special characters. The script now uses PowerShell's ConvertTo-Json cmdlet to automatically handle escaping of backslashes, quotes, and other special characters in UNC paths, usernames (including domain accounts), and passwords.
Key changes:
- Replaced manual string formatting with
ConvertTo-Jsonfor automatic JSON escaping - Updated help text and documentation to reflect simplified usage with natural input formats
- Added PowerShell 7.0+ requirement directive
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| FileShares/scripts/cf-create-service.ps1 | Implements automatic JSON escaping using ConvertTo-Json, updates parameter help text, and adds PowerShell 7.0 version requirement |
| FileShares/README.md | Updates documentation to show natural UNC path format, adds PowerShell 7+ requirement note, and documents domain username support |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
TimHess
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, here's the result of a test run:
PS C:\source\steeltoe-samples\FileShares\scripts> .\cf-create-service.ps1
cmdlet cf-create-service.ps1 at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
NetworkAddress: \\myserver\myshare
UserName: mydomain\useraccount
Password: some\ridulous"'passw\o/rd"""''"""''"''\'"
cf create-service credhub default sampleNetworkShare -c {"location":"\\\\myserver\\myshare","password":"some\\ridulous\"'passw\\o/rd\"\"\"''\"\"\"''\"''\\'\"","username":"mydomain\\useraccount"} -t sampleNetworkShare
Creating service instance sampleNetworkShare in org / space as <user>...
Service instance sampleNetworkShare created.
OK5f1b634 to
5b9701c
Compare
Co-authored-by: TimHess <3947063+TimHess@users.noreply.github.com>
Co-authored-by: TimHess <3947063+TimHess@users.noreply.github.com>
Co-authored-by: TimHess <3947063+TimHess@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
5b9701c to
88fc5c6
Compare
| @@ -1,15 +1,28 @@ | |||
| #Requires -Version 7.0 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need this? The script is only useful on Windows, so it would be nice if users could run it with the built-in Windows PowerShell instead of installing PowerShell Core first.
| $ParamJSON = [string]::Format('{{\"location\":\"{0}\",\"username\":\"{1}\",\"password\":\"{2}\"}}', $NetworkAddress, $UserName, $Password) | ||
| # Build parameter object and convert to JSON using PowerShell's built-in JSON serialization | ||
| # This automatically handles escaping of special characters including backslashes, quotes, etc. | ||
| $params = @{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable names are very inconsistent. Some are camelCase, others are PascalCase (all should be the former). The names range from $redactedParams (plural; the type of params before it) to $ParamJSON (singular, the type after it) and $ParamJSONRedacted (singular, the type before and after it).
Suggested:
$params (array)
$jsonParams (string)
$redactedParams (array)
$redactedJsonParams (string)
| * `-NetworkAddress \\\\<hostname>\\<sharename>` - escaped UNC path of the fileshare | ||
| * `-UserName <username>` - the username for accessing the fileshare | ||
| * `-NetworkAddress \\<hostname>\<sharename>` - UNC path of the fileshare | ||
| * `-UserName <username>` - the username for accessing the fileshare (can include domain, e.g., `DOMAIN\username`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explanations here and in the script itself should be synced up.
The script required users to manually double-escape backslashes in UNC paths (e.g.,
\\\\localhost\\shareinstead of\\localhost\share). This was error-prone and the help text explaining this was only visible on request. Additionally, usernames (especially domain accounts likeDOMAIN\user) and passwords could contain backslashes and special characters that weren't being escaped.Changes
cf-create-service.ps1: Uses PowerShell's built-in
ConvertTo-Jsonfor automatic JSON escaping#Requires -Version 7.0directive to ensure PowerShell 7+ compatibilityREADME.md: Updated documentation to show natural parameter format
DOMAIN\username)Example
Before:
After:
The script now uses
ConvertTo-Jsonto produce properly-escaped JSON for all parameters, handling backslashes, quotes, and other special characters automatically while accepting intuitive input.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.