Skip to content

Commit 3e1658f

Browse files
CopilotTimHessCopilot
authored
Handle parameter escaping internally in cf-create-service.ps1 (#439)
* Handle parameter escaping internally in cf-create-service.ps1 * Use ConvertTo-Json for robust escaping of all parameters * PascalParams, camelVariables, enhance consistency in readme & scripts --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: TimHess <[email protected]> Co-authored-by: Tim Hess <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 5cb0623 commit 3e1658f

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

FileShares/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ You can also delete files by clicking the "Delete file" button in the same row a
4949
> [!TIP]
5050
> The sample uses credentials different from those of your Windows user account. If you've opened the file share in Windows Explorer before running the sample, it fails because a file share can't be accessed by one user using multiple credentials. To recover, run `klist purge` to make Windows forget the connection from Windows Explorer.
5151

52-
5352
### Removing the local user account and file share
5453

5554
> [!CAUTION]
@@ -71,10 +70,16 @@ Before deploying the app, you must create an entry in CredHub to contain the cre
7170

7271
### Store credentials in CredHub
7372

73+
> [!NOTE]
74+
> The [cf-create-service.ps1](scripts/cf-create-service.ps1) script requires PowerShell 7 or later.
75+
7476
1. Run [cf-create-service.ps1](scripts/cf-create-service.ps1) to create a service instance in CredHub, using parameters to set the required values:
75-
* `-NetworkAddress \\\\<hostname>\\<sharename>` - escaped UNC path of the fileshare
76-
* `-UserName <username>` - the username for accessing the fileshare
77-
* `-Password <password>` - the password for accessing the fileshare
77+
* `-NetworkAddress \\<hostname>\<sharename>` - UNC path to the network share (required). For example: `\\localhost\steeltoe_network_share`
78+
* `-UserName <username>` - the username for accessing the file share, can include domain (e.g., `DOMAIN\username`) (required)
79+
* `-Password <password>` - the password for accessing the file share (required)
80+
* `-ServiceName credhub` - the name of the service for storing credentials
81+
* `-ServicePlan default` - the service plan to use
82+
* `-ServiceInstanceName sampleNetworkShare` - the name of the service instance
7883

7984
### Deploy the app
8085

FileShares/scripts/add-user-and-share.ps1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
#Requires -Modules Microsoft.PowerShell.LocalAccounts, SmbShare
33

44
Param(
5-
[string]$ShareName = "steeltoe_network_share",
6-
[string]$SharePath = "c:\steeltoe_network_share",
7-
[string]$UserName = "shareWriteUser",
8-
[string]$Password = "thisIs1Pass!"
5+
[Parameter(Mandatory = $false, HelpMessage = "The name of the share")][string]$ShareName = "steeltoe_network_share",
6+
[Parameter(Mandatory = $false, HelpMessage = "The path to the share. For example: 'c:\steeltoe_network_share'")][string]$SharePath = "c:\steeltoe_network_share",
7+
[Parameter(Mandatory = $false, HelpMessage = "The name of the user")][string]$UserName = "shareWriteUser",
8+
[Parameter(Mandatory = $false, HelpMessage = "The password for the user")][string]$Password = "thisIs1Pass!"
99
)
1010
$ErrorActionPreference = "Stop"
11+
1112
if ($PSVersionTable.PSVersion.Major -lt 6)
1213
{
1314
Write-Output "Running in Windows PowerShell (version < 6)"
@@ -18,7 +19,7 @@ else
1819
Add-Type -AssemblyName System.Management.Automation
1920
Import-Module Microsoft.PowerShell.LocalAccounts -SkipEditionCheck
2021
}
21-
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
22+
$securePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
2223

2324
if (Get-LocalUser -Name $UserName -ErrorAction SilentlyContinue)
2425
{
@@ -28,7 +29,7 @@ else
2829
{
2930
Write-Host "Creating local user $UserName..."
3031
New-LocalUser $UserName `
31-
-Password $SecurePassword `
32+
-Password $securePassword `
3233
-FullName "SMB ReadWrite" `
3334
-Description "For write access to $ShareName" | Out-Null
3435
Write-Host "Done creating user."
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
#Requires -Version 7.0
2+
13
Param(
2-
[Parameter(Mandatory = $true, HelpMessage = "Escaped UNC path. For example, if the path is '\\localhost\steeltoe_network_share', use '\\\\localhost\\steeltoe_network_share'.")][string]$NetworkAddress,
3-
[Parameter(Mandatory=$true)][string]$UserName,
4-
[Parameter(Mandatory=$true)][string]$Password,
5-
[string]$ServiceName = "credhub",
6-
[string]$ServicePlan = "default",
7-
[string]$ServiceInstanceName = "sampleNetworkShare"
4+
[Parameter(Mandatory = $true, HelpMessage = "UNC path to the network share. For example: '\\localhost\steeltoe_network_share'")][string]$NetworkAddress,
5+
[Parameter(Mandatory = $true, HelpMessage = "The username for accessing the file share, can include domain. For example: 'DOMAIN\username'")][string]$UserName,
6+
[Parameter(Mandatory = $true, HelpMessage = "The password for accessing the file share.")][string]$Password,
7+
[Parameter(Mandatory = $false, HelpMessage = "The name of the service for storing credentials")][string]$ServiceName = "credhub",
8+
[Parameter(Mandatory = $false, HelpMessage = "The service plan to use")][string]$ServicePlan = "default",
9+
[Parameter(Mandatory = $false, HelpMessage = "The name of the service instance")][string]$ServiceInstanceName = "sampleNetworkShare"
810
)
911
$ErrorActionPreference = "Stop"
1012

11-
$ParamJSON = [string]::Format('{{\"location\":\"{0}\",\"username\":\"{1}\",\"password\":\"{2}\"}}', $NetworkAddress, $UserName, $Password)
13+
# Build parameter object and convert to JSON using PowerShell's built-in JSON serialization
14+
# This automatically handles escaping of special characters including backslashes, quotes, etc.
15+
$params = @{
16+
location = $NetworkAddress
17+
username = $UserName
18+
password = $Password
19+
}
20+
$jsonParams = $params | ConvertTo-Json -Compress
1221

13-
Write-Host "cf create-service $ServiceName $ServicePlan $ServiceInstanceName -c $ParamJSON -t $ServiceInstanceName"
22+
# Create a redacted copy of the parameters for logging so the password is not exposed
23+
$redactedParams = $params.Clone()
24+
$redactedParams['password'] = 'REDACTED'
25+
$redactedJsonParams = $redactedParams | ConvertTo-Json -Compress
1426

15-
cf create-service $ServiceName $ServicePlan $ServiceInstanceName -c $ParamJSON -t $ServiceInstanceName
27+
Write-Host "cf create-service $ServiceName $ServicePlan $ServiceInstanceName -c $redactedJsonParams -t $ServiceInstanceName"
28+
cf create-service $ServiceName $ServicePlan $ServiceInstanceName -c $jsonParams -t $ServiceInstanceName

FileShares/scripts/remove-user-and-share.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
#Requires -Modules Microsoft.PowerShell.LocalAccounts, SmbShare
33

44
Param(
5-
[string]$ShareName = "steeltoe_network_share",
6-
[string]$SharePath = "c:\steeltoe_network_share",
7-
[string]$UserName = "shareWriteUser"
5+
[Parameter(Mandatory = $false, HelpMessage = "The name of the share")][string]$ShareName = "steeltoe_network_share",
6+
[Parameter(Mandatory = $false, HelpMessage = "The path to the share. For example: 'c:\steeltoe_network_share'")][string]$SharePath = "c:\steeltoe_network_share",
7+
[Parameter(Mandatory = $false, HelpMessage = "The name of the user")][string]$UserName = "shareWriteUser"
88
)
99
$ErrorActionPreference = "Stop"
10+
1011
if ($PSVersionTable.PSVersion.Major -lt 6)
1112
{
1213
Write-Output "Running in Windows PowerShell (version < 6)"
@@ -17,6 +18,7 @@ else
1718
Add-Type -AssemblyName System.Management.Automation
1819
Import-Module Microsoft.PowerShell.LocalAccounts -SkipEditionCheck
1920
}
21+
2022
if (Get-SmbShare $ShareName -ErrorAction SilentlyContinue)
2123
{
2224
Remove-SmbShare -Name $ShareName

0 commit comments

Comments
 (0)