Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mongo output handling #610

Closed

Conversation

ZloeSabo
Copy link
Contributor

Improve mongo output handling

This PR contains multiple changes to improve stability of parsing mongo output:

changes printjson to JSON.stringify to avoid internal types

In many cases printjson is adding internal types (ObjectId, etc) to the output, which results in an invalid JSON that is not parseable by ruby.

For example, printjson(db.isMaster()) prints something like this:

{
	"topologyVersion" : {
		"processId" : ObjectId("60995bb5e421c84948b94087"),
		"counter" : NumberLong(6)
	},
	...
}

adds logic to extract error object from a corrupted output

Currently, the handling of exceptions is broken because the existing logic is expecting something parseable in the output, but it's not a valid JSON even after sanitization:

    Error: Authentication failed.
    2021-05-11T15:35:19.647+0200 E QUERY    [thread1] Error: Could not retrieve replica set config: {
    	"ok" : 0,
    	"errmsg" : "not authorized on admin to execute command { replSetGetConfig: 1.0, $clusterTime: { clusterTime: Timestamp(0, 0), signature: { hash: BinData(0, 0000000000000000000000000000000000000000), keyId: 0 } }, $readPreference: { mode: \"secondaryPreferred\" }, $db: \"admin\" }",
    	"code" : 13,
    	"codeName" : "Unauthorized",
    	"$clusterTime" : {
    		"clusterTime" : Timestamp(0, 0),
    		"signature" : {
    			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
    			"keyId" : NumberLong(0)
    		}
    	}
    } :
    rs.conf@src/mongo/shell/utils.js:1323:11
    @(shell eval):1:43'

Because of this, replica set provider (that relies on an error data) can't detect that the host is online and fails to properly initialize the replica set.

I've added code that attempts to extract an object in case an output is not a valid JSON.

adds check for error code when detecting failed authorization

The existing logic relies on an error message that potentially may change (because of i18n, etc), which may break it in a long run. I've added a check for a error code name, that is less likely to change.

ZloeSabo added 3 commits May 11, 2021 14:18
This changes the json conversion logic to JSON.stringify that
always produce a valid JSON, which is parseable by ruby.
The printjson sometimes adds internal mongo types to the result,
so that ruby JSON library is not able to parse it.
This changes the way the module handles mongodb output:
1. it would not do any sanitization if the output already contains
   a valid json.
2. it would try to extract a json object from a corrupted output
3. it would fallback to the previous logic if the json extraction failed
The existing logic was only relying on a error message, that was
changing over time. This change adds a check for the codeName
property that supposed to be less volatile.
@vox-pupuli-tasks
Copy link

Dear @ZloeSabo, thanks for the PR!

This is Vox Pupuli Tasks, your friendly Vox Pupuli GitHub Bot. I noticed that your pull request contains merge conflict. Can you please rebase?

You can find my sourcecode at voxpupuli/vox-pupuli-tasks

@witjoh
Copy link
Contributor

witjoh commented Mar 29, 2024

This should be fixed with #703

@witjoh witjoh closed this Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants