Skip to content

Commit

Permalink
Merge pull request #468 from vrtdev/feature/mongodb_database_provider
Browse files Browse the repository at this point in the history
add gsub to replace invalid json values with a 1
  • Loading branch information
vStone committed Oct 2, 2018
2 parents c2066d5 + 53b8793 commit 6f02139
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
11 changes: 4 additions & 7 deletions lib/puppet/provider/mongodb.rb
@@ -1,3 +1,6 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
require 'puppet/util/mongodb_output'

require 'yaml'
require 'json'
class Puppet::Provider::Mongodb < Puppet::Provider
Expand Down Expand Up @@ -173,13 +176,7 @@ def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}"
end

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

def mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
Expand Down
4 changes: 0 additions & 4 deletions lib/puppet/provider/mongodb_replset/mongo.rb
Expand Up @@ -388,10 +388,6 @@ def self.mongo_command(command, host = nil, retries = 4)
raise
end

# Dirty hack to remove JavaScript objects
output.gsub!(%r{\w+\((\d+).+?\)}, '\1') # Remove extra parameters from 'Timestamp(1462971623, 1)' Objects
output.gsub!(%r{\w+\((.+?)\)}, '\1')

# Hack to avoid non-json empty sets
output = '{}' if output == "null\n"
output = '{}' if output == "\nnull\n"
Expand Down
16 changes: 16 additions & 0 deletions lib/puppet/util/mongodb_output.rb
@@ -0,0 +1,16 @@
module Puppet
module Util
module MongodbOutput
def self.sanitize(data)
# Dirty hack to remove JavaScript objects
data.gsub!(%r{\w+\((\d+).+?\)}, '\1') # Remove extra parameters from 'Timestamp(1462971623, 1)' Objects
data.gsub!(%r{\w+\((.+?)\)}, '\1')

data.gsub!(%r{^Error\:.+}, '')
data.gsub!(%r{^.*warning\:.+}, '') # remove warnings if sslAllowInvalidHostnames is true
data.gsub!(%r{^.*The server certificate does not match the host name.+}, '') # remove warnings if sslAllowInvalidHostnames is true mongo 3.x
data
end
end
end
end
2 changes: 1 addition & 1 deletion spec/unit/puppet/provider/mongodb_replset/mongodb_spec.rb
Expand Up @@ -135,7 +135,7 @@
"me" : "mongo1:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2014-01-10T19:31:51.281Z"),
"localTime" : "2014-01-10T19:31:51.281Z",
"ok" : 1
}
EOT
Expand Down
66 changes: 66 additions & 0 deletions spec/unit/puppet/util/mongodb_output_spec.rb
@@ -0,0 +1,66 @@
require 'spec_helper'
require 'puppet/util/mongodb_output'
require 'json'

describe Puppet::Util::MongodbOutput do
let(:bson_data) do
<<-EOT
{
"setName": "rs_test",
"ismaster": true,
"secondary": false,
"hosts": [
"mongo1:27017"
],
"primary": "mongo1:27017",
"me": "mongo1:27017",
"maxBsonObjectSize": 16777216,
"maxMessageSizeBytes": 48000000,
"hash": BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId": NumberLong(0),
"clusterTime": Timestamp(1538381287, 1),
"replicaSetId": ObjectId("5bb1d270137a581ebd3d61f2"),
"slaveDelay": NumberLong(-1),
"majorityWriteDate": ISODate("2018-10-01T08:08:01Z"),
"lastHeartbeat": ISODate("2018-10-01T08:08:05.859Z"),
"ok": 1
}
EOT
end

let(:json_data) do
<<-EOT
{
"setName": "rs_test",
"ismaster": true,
"secondary": false,
"hosts": [
"mongo1:27017"
],
"primary": "mongo1:27017",
"me": "mongo1:27017",
"maxBsonObjectSize": 16777216,
"maxMessageSizeBytes": 48000000,
"hash": 0,
"keyId": 0,
"clusterTime": 1538381287,
"replicaSetId": "5bb1d270137a581ebd3d61f2",
"slaveDelay": -1,
"majorityWriteDate": "2018-10-01T08:08:01Z",
"lastHeartbeat": "2018-10-01T08:08:05.859Z",
"ok": 1
}
EOT
end

describe '.sanitize' do
it 'returns a valid json' do
sanitized_json = described_class.sanitize(bson_data)
expect { JSON.parse(sanitized_json) }.not_to raise_error
end
it 'replaces data types' do
sanitized_json = described_class.sanitize(bson_data)
expect(JSON.parse(sanitized_json)).to include(JSON.parse(json_data))
end
end
end

0 comments on commit 6f02139

Please sign in to comment.