How to drop any fields containing the word "PublicKey" from logstash

My original answer just tests the top-level fields. If you want to remove fields nested inside of fields then try

    ruby {
        init => '
            def doSomething(object, name, event)
                if object   # Remove this if your use-case needs to process nil objects
                    # If we need to handle non-leaf nodes then test this first
                    if name.include?("PublicKey")
                        event.remove(name)
                    elsif object.kind_of?(Hash) and object != {}
                        object.each { |k, v| doSomething(v, "#{name}[#{k}]", event) }
                    elsif object.kind_of?(Array) and object != []
                        object.each_index { |i|
                            doSomething(object[i], "#{name}[#{i}]", event)
                        }
                    end
                end
            end

        '
        code => '
            event.to_hash.each { |k, v|
                doSomething(v, "[#{k}]", event)
            }
        '
    }

It seems like half the times I use this type of loop I find something that breaks my original version and variants. This time it was the need to run the test against non-leaf nodes :rofl: