Skip to content

Commit 6f02139

Browse files
authored
Merge pull request #468 from vrtdev/feature/mongodb_database_provider
add gsub to replace invalid json values with a 1
2 parents c2066d5 + 53b8793 commit 6f02139

5 files changed

Lines changed: 87 additions & 12 deletions

File tree

lib/puppet/provider/mongodb.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
2+
require 'puppet/util/mongodb_output'
3+
14
require 'yaml'
25
require 'json'
36
class Puppet::Provider::Mongodb < Puppet::Provider
@@ -173,13 +176,7 @@ def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
173176
raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}"
174177
end
175178

176-
%w[ObjectId NumberLong].each do |data_type|
177-
out.gsub!(%r{#{data_type}\(([^)]*)\)}, '\1')
178-
end
179-
out.gsub!(%r{^Error\:.+}, '')
180-
out.gsub!(%r{^.*warning\:.+}, '') # remove warnings if sslAllowInvalidHostnames is true
181-
out.gsub!(%r{^.*The server certificate does not match the host name.+}, '') # remove warnings if sslAllowInvalidHostnames is true mongo 3.x
182-
out
179+
Puppet::Util::MongodbOutput.sanitize(out)
183180
end
184181

185182
def mongo_eval(cmd, db = 'admin', retries = 10, host = nil)

lib/puppet/provider/mongodb_replset/mongo.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,6 @@ def self.mongo_command(command, host = nil, retries = 4)
388388
raise
389389
end
390390

391-
# Dirty hack to remove JavaScript objects
392-
output.gsub!(%r{\w+\((\d+).+?\)}, '\1') # Remove extra parameters from 'Timestamp(1462971623, 1)' Objects
393-
output.gsub!(%r{\w+\((.+?)\)}, '\1')
394-
395391
# Hack to avoid non-json empty sets
396392
output = '{}' if output == "null\n"
397393
output = '{}' if output == "\nnull\n"

lib/puppet/util/mongodb_output.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Puppet
2+
module Util
3+
module MongodbOutput
4+
def self.sanitize(data)
5+
# Dirty hack to remove JavaScript objects
6+
data.gsub!(%r{\w+\((\d+).+?\)}, '\1') # Remove extra parameters from 'Timestamp(1462971623, 1)' Objects
7+
data.gsub!(%r{\w+\((.+?)\)}, '\1')
8+
9+
data.gsub!(%r{^Error\:.+}, '')
10+
data.gsub!(%r{^.*warning\:.+}, '') # remove warnings if sslAllowInvalidHostnames is true
11+
data.gsub!(%r{^.*The server certificate does not match the host name.+}, '') # remove warnings if sslAllowInvalidHostnames is true mongo 3.x
12+
data
13+
end
14+
end
15+
end
16+
end

spec/unit/puppet/provider/mongodb_replset/mongodb_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
"me" : "mongo1:27017",
136136
"maxBsonObjectSize" : 16777216,
137137
"maxMessageSizeBytes" : 48000000,
138-
"localTime" : ISODate("2014-01-10T19:31:51.281Z"),
138+
"localTime" : "2014-01-10T19:31:51.281Z",
139139
"ok" : 1
140140
}
141141
EOT
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'spec_helper'
2+
require 'puppet/util/mongodb_output'
3+
require 'json'
4+
5+
describe Puppet::Util::MongodbOutput do
6+
let(:bson_data) do
7+
<<-EOT
8+
{
9+
"setName": "rs_test",
10+
"ismaster": true,
11+
"secondary": false,
12+
"hosts": [
13+
"mongo1:27017"
14+
],
15+
"primary": "mongo1:27017",
16+
"me": "mongo1:27017",
17+
"maxBsonObjectSize": 16777216,
18+
"maxMessageSizeBytes": 48000000,
19+
"hash": BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
20+
"keyId": NumberLong(0),
21+
"clusterTime": Timestamp(1538381287, 1),
22+
"replicaSetId": ObjectId("5bb1d270137a581ebd3d61f2"),
23+
"slaveDelay": NumberLong(-1),
24+
"majorityWriteDate": ISODate("2018-10-01T08:08:01Z"),
25+
"lastHeartbeat": ISODate("2018-10-01T08:08:05.859Z"),
26+
"ok": 1
27+
}
28+
EOT
29+
end
30+
31+
let(:json_data) do
32+
<<-EOT
33+
{
34+
"setName": "rs_test",
35+
"ismaster": true,
36+
"secondary": false,
37+
"hosts": [
38+
"mongo1:27017"
39+
],
40+
"primary": "mongo1:27017",
41+
"me": "mongo1:27017",
42+
"maxBsonObjectSize": 16777216,
43+
"maxMessageSizeBytes": 48000000,
44+
"hash": 0,
45+
"keyId": 0,
46+
"clusterTime": 1538381287,
47+
"replicaSetId": "5bb1d270137a581ebd3d61f2",
48+
"slaveDelay": -1,
49+
"majorityWriteDate": "2018-10-01T08:08:01Z",
50+
"lastHeartbeat": "2018-10-01T08:08:05.859Z",
51+
"ok": 1
52+
}
53+
EOT
54+
end
55+
56+
describe '.sanitize' do
57+
it 'returns a valid json' do
58+
sanitized_json = described_class.sanitize(bson_data)
59+
expect { JSON.parse(sanitized_json) }.not_to raise_error
60+
end
61+
it 'replaces data types' do
62+
sanitized_json = described_class.sanitize(bson_data)
63+
expect(JSON.parse(sanitized_json)).to include(JSON.parse(json_data))
64+
end
65+
end
66+
end

0 commit comments

Comments
 (0)