Skip to content
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

storage: fixing the validation for ip_rules to include the value in the error #21178

Merged
merged 2 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func resourceStorageAccountNetworkRulesSchema() map[string]*pluginsdk.Schema {
Computed: true,
ConfigMode: pluginsdk.SchemaConfigModeAttr,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Type: pluginsdk.TypeString,
ValidateFunc: validate.StorageAccountIpRule,
},
Set: pluginsdk.HashString,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ func StorageAccountIpRule(v interface{}, k string) (warnings []string, errors []
value := v.(string)

if !regexp.MustCompile(`^([0-9]{1,3}\.){3}[0-9]{1,3}(/([0-9]|[1-2][0-9]|30))?$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q must start with IPV4 address and/or slash, number of bits (0-30) as prefix. Example: 23.45.1.0/30.", k))
errors = append(errors, fmt.Errorf("%q must start with IPV4 address and/or slash, number of bits (0-30) as prefix. Example: 23.45.1.0/30 but got %q.", k, value))
return warnings, errors
}

ipParts := strings.Split(v.(string), ".")
firstIPPart := ipParts[0]
secondIPPart, _ := strconv.Atoi(ipParts[1])
if (firstIPPart == "10") || (firstIPPart == "172" && secondIPPart >= 16 && secondIPPart <= 31) || (firstIPPart == "192" && secondIPPart == 168) {
errors = append(errors, fmt.Errorf("%q must be public ip address", k))
errors = append(errors, fmt.Errorf("%q must be public ip address but got %q", k, value))
return warnings, errors
}

Expand Down