Matt Kuhns
Matt Kuhns

Reputation: 1368

Suppressing STDOUT with node child_process

I am running the following code:

var exec = require('child_process').exec;
var command = "security set-key-partition-list -S apple-tool:,apple: -s -k password /path/to/keychain/login.keychain-db";
exec(serverConfig.securityCall, function (error, stdout, stderr) {
    if (error !== null) {
        console.log('exec error: ' + error);
        console.log('STDERR: ' + stderr);
        console.log('STDOUT: ' + stdout);
    }
});

I get the error: exec error: Error: stdout maxBuffer exceeded.

Is there a way to suppress the stdout? I don't need it. I saw this post: Stdout buffer issue using node child_process

So, I changed it to a spawn

var spawn = require('child_process').spawn;
var child = spawn('security', ['set-key-partition-list', '-S apple-tool:,apple: -s -k password /path/to/keychain/login.keychain-db'], {stdio:['ignore', 'ignore', 'pipe']});

child.stderr.on('data', function (data) {
   console.log('stderr: ' + data);
   stderr = 'stderr: ' + data
});

child.on('close', function (code) {
    console.log('child process exited with code ' + code);
    if (!code) { //0 = success 1= error
        console.log("SUCCESS");
    } else {
        console.log('STDERR: ' + stderr);
    }
 });

but I get this error:

stderr: password to unlock default: security: SecKeychainItemSetAccessWithPassword: The user name or passphrase you entered is not correct.

If I run this from command line it works, so i know my password is correct. (password and path to keychain have been redacted for security purposes).

How can I get this to work with spawn or exec?

Upvotes: 2

Views: 1217

Answers (1)

LEQADA
LEQADA

Reputation: 1982

The error you get is coming from your security application, not from Node. There is a tricky part of using spawn. Every single option should be a separate array element.

So this array element should be separated to multiple elements

'-S apple-tool:,apple: -s -k password /path/to/keychain/login.keychain-db'

Something like

['-S', 'apple-tool:,apple:', '-s', '-k', 'password', '/path/to/keychain/login.keychain-db']

Honestly I don't understand why it is not explained well in the documentation.

Upvotes: 2

Related Questions