-
Notifications
You must be signed in to change notification settings - Fork 279
/
sensu_helpers.rb
151 lines (140 loc) · 4.93 KB
/
sensu_helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require "openssl"
module Sensu
class Helpers
extend ChefVaultCookbook if Kernel.const_defined?("ChefVaultCookbook")
class << self
def select_attributes(attributes, keys)
attributes.to_hash.reject do |key, value|
!Array(keys).include?(key.to_s) || value.nil?
end
end
def sanitize(raw_hash)
sanitized = Hash.new
raw_hash.each do |key, value|
# Expand Chef::DelayedEvaluator (lazy)
value = value.call if value.respond_to?(:call)
case value
when Hash
sanitized[key] = sanitize(value) unless value.empty?
when nil
# noop
else
sanitized[key] = value
end
end
sanitized
end
def gem_binary
if File.exists?("/opt/sensu/embedded/bin/gem")
"/opt/sensu/embedded/bin/gem"
elsif File.exists?('c:\opt\sensu\embedded\bin\gem.bat')
'c:\opt\sensu\embedded\bin\gem.bat'
else
"gem"
end
end
def data_bag_item(item, missing_ok=false, data_bag_name="sensu")
raw_hash = Chef::DataBagItem.load(data_bag_name, item).delete_if { |k,v| k == "id" }
encrypted = raw_hash.detect do |key, value|
if value.is_a?(Hash)
value.has_key?("encrypted_data")
end
end
if encrypted
if Chef::DataBag.load(data_bag_name).key? "#{item}_keys"
chef_vault_item(data_bag_name, item)
else
secret = Chef::EncryptedDataBagItem.load_secret
Chef::EncryptedDataBagItem.new(raw_hash, secret)
end
else
raw_hash
end
rescue Chef::Exceptions::ValidationFailed,
Chef::Exceptions::InvalidDataBagPath,
Net::HTTPServerException => error
missing_ok ? nil : raise(error)
end
def random_password(length=20, number=false, upper=false, lower=false, special=false)
password = ""
requiredOffset = 0
requiredOffset += 1 if number
requiredOffset += 1 if upper
requiredOffset += 1 if lower
requiredOffset += 1 if special
length = requiredOffset if length < requiredOffset
limit = password.length < (length - requiredOffset)
while limit || requiredOffset > 0
push = false
c = ::OpenSSL::Random.random_bytes(1).gsub(/\W/, '')
if c != ""
if c =~ /[[:digit:]]/
requiredOffset -= 1 if number
number = false
elsif c >= 'a' && c <= 'z'
requiredOffset -= 1 if lower
lower = false
elsif c >= 'A' && c <= 'Z'
requiredOffset -= 1 if upper
upper = false
else
requiredOffset -= 1 if special
special = false
end
end
limit = password.length < (length - requiredOffset)
if limit
password << c
end
end
password
end
# Wraps the Chef::Util::Windows::NetUser, returning false if the Win32 constant
# is undefined, or returning false if the user does not exist. This indirection
# seems like the most expedient way to make the sensu::_windows recipe testable
# via chefspec on non-windows platforms.
#
# @param [String] the name of the user to test for
# @return [TrueClass, FalseClass]
def windows_user_exists?(user)
if defined?(Win32)
net_user = Chef::Util::Windows::NetUser.new(user)
!!net_user.get_info rescue false
else
false
end
end
# Wraps Win32::Service, returning false if the Win32 constant
# is undefined, or returning false if the user does not exist. This indirection
# seems like the most expedient way to make the sensu::_windows recipe testable
# via chefspec on non-windows platforms.
#
# @param [String] the name of the service to test for
# @return [TrueClass, FalseClass]
def windows_service_exists?(service)
if defined?(Win32)
::Win32::Service.exists?(service)
else
false
end
end
# Derives Sensu package version strings for Redhat platforms.
# When the desired Sensu version is '0.27.0' or later, the package
# requires a '.elX' suffix.
#
# @param [String] Sensu version string
# @param [String] Platform version
# @param [String,NilClass] Suffix to override default '.elX'
def redhat_version_string(sensu_version, platform_version, suffix_override = nil)
bare_version = sensu_version.split('-').first
if Gem::Version.new(bare_version) < Gem::Version.new('0.27.0')
sensu_version
else
platform_major = Gem::Version.new(platform_version).segments.first
suffix = suffix_override || ".el#{platform_major}"
[sensu_version, suffix].join
end
end
end
end
end