From e06cd0800c98a34f01bfd460a239d9ccf53127d3 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Mon, 21 Nov 2016 16:19:00 -0800 Subject: [PATCH] [client-deregistration] validate client deregistration and registration status --- lib/sensu/settings/validators/client.rb | 32 ++++++++++++++++ spec/validator_spec.rb | 49 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/lib/sensu/settings/validators/client.rb b/lib/sensu/settings/validators/client.rb index fea262f..8001198 100644 --- a/lib/sensu/settings/validators/client.rb +++ b/lib/sensu/settings/validators/client.rb @@ -138,6 +138,37 @@ def validate_client_registration(client) invalid(client, "client registration must be a hash") if is_a_hash?(client[:registration]) validate_client_registration_handlers(client) + must_be_an_integer_if_set(client[:registration][:status]) || + invalid(client, "client registration status must be an integer") + end + end + + # Validate client deregistration handlers. + # Validates: deregistration (handler, handlers) + # + # @param client [Hash] sensu client definition. + def validate_client_deregistration_handlers(client) + must_be_a_string_if_set(client[:deregistration][:handler]) || + invalid(client, "client deregistration handler must be a string") + must_be_an_array_if_set(client[:deregistration][:handlers]) || + invalid(client, "client deregistration handlers must be an array") + if is_an_array?(client[:deregistration][:handlers]) + items_must_be_strings(client[:deregistration][:handlers]) || + invalid(client, "client deregistration handlers must each be a string") + end + end + + # Validate client deregistration. + # Validates: deregistration + # + # @param client [Hash] sensu client definition. + def validate_client_deregistration(client) + must_be_a_hash_if_set(client[:deregistration]) || + invalid(client, "client deregistration must be a hash") + if is_a_hash?(client[:deregistration]) + validate_client_deregistration_handlers(client) + must_be_an_integer_if_set(client[:deregistration][:status]) || + invalid(client, "client deregistration status must be an integer") end end @@ -163,6 +194,7 @@ def validate_client(client) validate_client_redact(client) validate_client_signature(client) validate_client_registration(client) + validate_client_deregistration(client) end end end diff --git a/spec/validator_spec.rb b/spec/validator_spec.rb index c421622..5a1a2bf 100644 --- a/spec/validator_spec.rb +++ b/spec/validator_spec.rb @@ -844,6 +844,55 @@ client[:registration][:handlers] = ["foo"] @validator.validate_client(client) expect(@validator.reset).to eq(0) + client[:registration][:status] = true + @validator.validate_client(client) + expect(@validator.reset).to eq(1) + client[:registration][:status] = "3" + @validator.validate_client(client) + expect(@validator.reset).to eq(1) + client[:registration][:status] = 3 + @validator.validate_client(client) + expect(@validator.reset).to eq(0) + end + + it "can validate client deregistration" do + client = { + :name => "foo", + :address => "127.0.0.1", + :subscriptions => ["bar"] + } + @validator.validate_client(client) + expect(@validator.reset).to eq(0) + client[:deregistration] = true + @validator.validate_client(client) + expect(@validator.reset).to eq(1) + client[:deregistration] = {} + @validator.validate_client(client) + expect(@validator.reset).to eq(0) + client[:deregistration][:handler] = 1 + @validator.validate_client(client) + expect(@validator.reset).to eq(1) + client[:deregistration][:handler] = "foo" + @validator.validate_client(client) + expect(@validator.reset).to eq(0) + client[:deregistration][:handlers] = 1 + @validator.validate_client(client) + expect(@validator.reset).to eq(1) + client[:deregistration][:handlers] = [1] + @validator.validate_client(client) + expect(@validator.reset).to eq(1) + client[:deregistration][:handlers] = ["foo"] + @validator.validate_client(client) + expect(@validator.reset).to eq(0) + client[:deregistration][:status] = true + @validator.validate_client(client) + expect(@validator.reset).to eq(1) + client[:deregistration][:status] = "3" + @validator.validate_client(client) + expect(@validator.reset).to eq(1) + client[:deregistration][:status] = 3 + @validator.validate_client(client) + expect(@validator.reset).to eq(0) end it "can run, validating client" do