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

Fix crash related to empty block in lb_vs resource #1463

Merged
merged 1 commit into from
Dec 9, 2024

Conversation

annakhm
Copy link
Collaborator

@annakhm annakhm commented Nov 6, 2024

Empty block can be configured when no required attributes are defined in the block. In this case nil protection is required, since empty block is presented as nil rather than empty map in the schema structure.

Empty block can be configured when no required attributes are
defined in the block. In this case nil protection is required,
since empty block is presented as nil rather than empty map in the
schema structure.

Signed-off-by: Anna Khmelnitsky <akhmelnitsky@vmware.com>
@@ -903,6 +903,10 @@ func getPolicyLbRuleVariablePersistenceLearnActionSchema() *schema.Schema {
func getPolicyClientSSLBindingFromSchema(d *schema.ResourceData) *model.LBClientSslProfileBinding {
bindings := d.Get("client_ssl").([]interface{})
for _, binding := range bindings {
if binding == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unsafe as d.Get() result can be nil as well and casting it will result a crash.
As we have multiple instances in the scope of this resource and multiple others in other resources, can we use a func instead of the range which will do all the validations, e.g check that input (d.Get() result) is not null, that members aren't null as well etc?
e.g:

func iterateSchemaSlice(s interface{}, f func(interface{})) {
    if s == nil {
        return
    }
    for _, i := range s.([]interface{}) {
        if i == nil {
            continue
        }
        f(i)
    }
}

And call it as:

  iterateSchemaSlice(d.Get("client_ssl"), func(binding interface{}){
		data := binding.(map[string]interface{})
		chainDepth := int64(data["certificate_chain_depth"].(int))    
  })

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the key exists in schema, then nil is not expected to be returned, as stated here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification Anna.
A pedant note. Do you think it might be better to rewrite this to:

if binding != nil {
		// maximum count is one
		data := binding.(map[string]interface{})
		chainDepth := int64(data["certificate_chain_depth"].(int))
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases that are addressed in this patch, the code block that would need to be indented is rather big, so I would leave it as is for the sake of readability

Copy link
Member

@salv-orlando salv-orlando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, no additional comment from me

@annakhm annakhm merged commit e126f93 into master Dec 9, 2024
3 checks passed
@annakhm annakhm deleted the fix-lb-empty-block branch December 9, 2024 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

While creating nsxt_policy_lb_virtual_server along with dynamic rule set, getting error.
3 participants