Skip to content

Commit f76be90

Browse files
Validate SMB mount endpoint fields
Add stricter validation and credential handling for StorageMover SMB mount endpoints. Host now must be a valid IPv4 address or hostname/FQDN; share_name is limited to 1-80 characters and forbids control characters and a set of punctuation; description is limited to 1024 characters. Update Update() logic to require both username_key_vault_secret_id and password_key_vault_secret_id to be set together (or both empty) and to set/clear credentials accordingly. Adjust tests formatting and update R docs to describe the new validation rules.
1 parent 779123e commit f76be90

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

internal/services/storagemover/storage_mover_smb_mount_endpoint_resource.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,29 @@ func (r StorageMoverSmbMountEndpointResource) Arguments() map[string]*pluginsdk.
7373
},
7474

7575
"host": {
76-
Type: pluginsdk.TypeString,
77-
Required: true,
78-
ForceNew: true,
79-
ValidateFunc: validation.StringIsNotEmpty,
76+
Type: pluginsdk.TypeString,
77+
Required: true,
78+
ForceNew: true,
79+
ValidateFunc: validation.Any(
80+
validation.IsIPv4Address,
81+
validation.StringMatch(
82+
regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])?$`),
83+
"Host must be a valid IPv4 address or hostname/FQDN (letters, numbers, dots, hyphens only).",
84+
),
85+
),
8086
},
8187

8288
"share_name": {
83-
Type: pluginsdk.TypeString,
84-
Required: true,
85-
ForceNew: true,
86-
ValidateFunc: validation.StringIsNotEmpty,
89+
Type: pluginsdk.TypeString,
90+
Required: true,
91+
ForceNew: true,
92+
ValidateFunc: validation.All(
93+
validation.StringLenBetween(1, 80),
94+
validation.StringMatch(
95+
regexp.MustCompile(`^[^\\/\[\]:<> +=;,*?\x00-\x1f\x7f]+$`),
96+
"Share name must be 1-80 characters and cannot contain: \\ / [ ] : < > + = ; , * ? or control characters.",
97+
),
98+
),
8799
},
88100

89101
"username_key_vault_secret_id": {
@@ -104,7 +116,7 @@ func (r StorageMoverSmbMountEndpointResource) Arguments() map[string]*pluginsdk.
104116
"description": {
105117
Type: pluginsdk.TypeString,
106118
Optional: true,
107-
ValidateFunc: validation.StringIsNotEmpty,
119+
ValidateFunc: validation.StringLenBetween(0, 1024),
108120
},
109121
}
110122
}
@@ -207,15 +219,18 @@ func (r StorageMoverSmbMountEndpointResource) Update() sdk.ResourceFunc {
207219
}
208220

209221
if metadata.ResourceData.HasChange("username_key_vault_secret_id") || metadata.ResourceData.HasChange("password_key_vault_secret_id") {
210-
if model.UsernameKeyVaultSecretId != "" && model.PasswordKeyVaultSecretId != "" {
222+
bothSet := model.UsernameKeyVaultSecretId != "" && model.PasswordKeyVaultSecretId != ""
223+
bothEmpty := model.UsernameKeyVaultSecretId == "" && model.PasswordKeyVaultSecretId == ""
224+
switch {
225+
case bothSet:
211226
v.Credentials = &endpoints.AzureKeyVaultSmbCredentials{
212227
Type: endpoints.CredentialTypeAzureKeyVaultSmb,
213228
UsernameUri: pointer.To(model.UsernameKeyVaultSecretId),
214229
PasswordUri: pointer.To(model.PasswordKeyVaultSecretId),
215230
}
216-
} else if model.UsernameKeyVaultSecretId == "" && model.PasswordKeyVaultSecretId == "" {
231+
case bothEmpty:
217232
v.Credentials = nil
218-
} else {
233+
default:
219234
return fmt.Errorf("both `username_key_vault_secret_id` and `password_key_vault_secret_id` must be specified together")
220235
}
221236
}

internal/services/storagemover/storage_mover_smb_mount_endpoint_resource_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ resource "azurerm_key_vault_secret" "password" {
191191
}
192192
193193
resource "azurerm_storage_mover_smb_mount_endpoint" "test" {
194-
name = "acctest-smse-%d"
195-
storage_mover_id = azurerm_storage_mover.test.id
196-
host = "192.168.0.1"
197-
share_name = "testshare"
194+
name = "acctest-smse-%d"
195+
storage_mover_id = azurerm_storage_mover.test.id
196+
host = "192.168.0.1"
197+
share_name = "testshare"
198198
username_key_vault_secret_id = azurerm_key_vault_secret.username.versionless_id
199199
password_key_vault_secret_id = azurerm_key_vault_secret.password.versionless_id
200200
description = "Example SMB Mount Endpoint Description"
@@ -247,10 +247,10 @@ resource "azurerm_key_vault_secret" "password" {
247247
}
248248
249249
resource "azurerm_storage_mover_smb_mount_endpoint" "test" {
250-
name = "acctest-smse-%d"
251-
storage_mover_id = azurerm_storage_mover.test.id
252-
host = "192.168.0.1"
253-
share_name = "testshare"
250+
name = "acctest-smse-%d"
251+
storage_mover_id = azurerm_storage_mover.test.id
252+
host = "192.168.0.1"
253+
share_name = "testshare"
254254
username_key_vault_secret_id = azurerm_key_vault_secret.username.versionless_id
255255
password_key_vault_secret_id = azurerm_key_vault_secret.password.versionless_id
256256
description = "Updated SMB Mount Endpoint Description"

website/docs/r/storage_mover_smb_mount_endpoint.html.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ The following arguments are supported:
6666

6767
* `storage_mover_id` - (Required) Specifies the ID of the Storage Mover for this SMB Mount Endpoint. Changing this forces a new resource to be created.
6868

69-
* `host` - (Required) Specifies the host name or IP address of the SMB server. Changing this forces a new resource to be created.
69+
* `host` - (Required) Specifies the host name or IP address of the SMB server. Must be a valid IPv4 address or hostname/FQDN (letters, numbers, dots, hyphens only). Changing this forces a new resource to be created.
7070

71-
* `share_name` - (Required) Specifies the name of the SMB share. Changing this forces a new resource to be created.
71+
* `share_name` - (Required) Specifies the name of the SMB share. Must be 1-80 characters and cannot contain: `\` `/` `[` `]` `:` `<` `>` `+` `=` `;` `,` `*` `?` or control characters. Changing this forces a new resource to be created.
7272

7373
* `username_key_vault_secret_id` - (Optional) Specifies the Azure Key Vault secret ID for the username to use for authentication. Must be specified together with `password_key_vault_secret_id`. You can use `azurerm_key_vault_secret.<name>.id` or `azurerm_key_vault_secret.<name>.versionless_id` (recommended to avoid pinning to a specific secret version).
7474

7575
* `password_key_vault_secret_id` - (Optional) Specifies the Azure Key Vault secret ID for the password to use for authentication. Must be specified together with `username_key_vault_secret_id`. You can use `azurerm_key_vault_secret.<name>.id` or `azurerm_key_vault_secret.<name>.versionless_id` (recommended).
7676

77-
* `description` - (Optional) Specifies a description for the Storage Mover SMB Mount Endpoint.
77+
* `description` - (Optional) Specifies a description for the Storage Mover SMB Mount Endpoint. Maximum length 1024 characters.
7878

7979
## Attributes Reference
8080

0 commit comments

Comments
 (0)