Skip to content

Commit

Permalink
Merge pull request #747 from particle-iot/feature/remove-open-disable
Browse files Browse the repository at this point in the history
Remove `--open` open from protection disable command
  • Loading branch information
monkbroc authored Jun 27, 2024
2 parents b6e34e7 + fc5a97b commit fa277b4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 52 deletions.
18 changes: 6 additions & 12 deletions src/cli/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ module.exports = ({ commandProcessor, root }) => {
});

commandProcessor.createCommand(deviceProtection, 'disable', 'Disables device protection', {
options: {
'open': {
boolean: true,
description: 'Turns a protected device into an open device'
}
},
handler: (args) => {

handler: () => {
const DeviceProtectionCommands = require('../cmd/device-protection');
return new DeviceProtectionCommands().disableProtection(args);
return new DeviceProtectionCommands().disableProtection();
},
examples: {
'$0 $command': 'Puts a protected device into service mode',
'$0 $command --open': 'Turns a protected device into an open device'
'$0 $command': 'Puts a Protected Device to Service Mode',
},
epilogue: 'A protected device in service mode allows any command to be performed on it that can be performed on an open device like flashing firmware or serial monitor.'
epilogue: 'A Protected Device in Service Mode allows any command to be performed on it that can be performed on an open device like flashing firmware or serial monitor.'
});

commandProcessor.createCommand(deviceProtection, 'enable', 'Enables device protection', {
Expand All @@ -40,7 +34,7 @@ module.exports = ({ commandProcessor, root }) => {
return new DeviceProtectionCommands().enableProtection(args);
},
examples: {
'$0 $command': 'Turns an open device into a protected device'
'$0 $command': 'Turns an Open Device into a Protected Device'
}
});

Expand Down
19 changes: 3 additions & 16 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
* @returns {Promise<void>}
* @throws {Error} - Throws an error if any of the async operations fail.
*/
async disableProtection({ open } = {}) {
async disableProtection() {
let addToOutput = [];

await this.ui.showBusySpinnerUntilResolved('Disabling device protection', this._withDevice(true, async () => {
Expand All @@ -97,7 +97,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
let s = await this._getDeviceProtection();

if (!s.protected && !s.overridden) {
addToOutput.push(`${deviceStr} is not a protected device.${os.EOL}`);
addToOutput.push(`${deviceStr} is not a Protected Device.${os.EOL}`);
return;
}

Expand All @@ -121,21 +121,8 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {

await this.device.unprotectDevice({ action: 'confirm', serverSignature, serverPublicKeyFingerprint });

if (!open) {
addToOutput.push(`${deviceStr} is now in service mode.${os.EOL}A protected device stays in service mode for a total of 20 reboots or 24 hours.${os.EOL}`);
return;
}

const localBootloaderPath = await this._downloadBootloader();
await this._flashBootloader(localBootloaderPath);
addToOutput.push(`${deviceStr} is now an open device.${os.EOL}`);
addToOutput.push(`${deviceStr} is now in Service Mode.${os.EOL}A Protected Device stays in Service Mode for a total of 20 reboots or 24 hours.${os.EOL}`);

const success = await this._markAsDevelopmentDevice(true);
addToOutput.push(success ?
// TODO: Improve these lines
`Device placed in development mode to maintain current settings.${os.EOL}` :
`Failed to mark device as development device. Device protection may be enabled on next cloud connection.${os.EOL}`
);
} catch (error) {
throw new Error(`Failed to disable device protection: ${error.message}${os.EOL}`);
}
Expand Down
24 changes: 0 additions & 24 deletions src/cmd/device-protection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,6 @@ describe('DeviceProtectionCommands', () => {
expect(deviceProtectionCommands.api.unprotectDevice).to.have.been.calledTwice;
});

it('should disable protection on the device and turns to an open device', async () => {
sinon.stub(deviceProtectionCommands, '_getDeviceProtection')
.onFirstCall().resolves({ protected: true, overridden: false })
.onSecondCall().resolves({ protected: true, overridden: false });
sinon.stub(deviceProtectionCommands, '_getDeviceString').resolves('[123456789abcdef] (Product 12345)');
deviceProtectionCommands.device = {
unprotectDevice: sinon.stub().resolves({ protected: true, deviceNonce: '11111', deviceSignature: '22222', devicePublicKeyFingerprint: '33333' })
};
deviceProtectionCommands.api = {
unprotectDevice: sinon.stub().resolves({ server_nonce: '44444', server_signature: '55555', server_public_key_fingerprint: '66666' })
};
sinon.stub(deviceProtectionCommands, '_downloadBootloader').resolves();
sinon.stub(deviceProtectionCommands, '_flashBootloader').resolves();
sinon.stub(deviceProtectionCommands,'_markAsDevelopmentDevice').resolves(true);


await deviceProtectionCommands.disableProtection({ open: true });

expect(deviceProtectionCommands._getDeviceProtection).to.have.been.calledOnce;
expect(deviceProtectionCommands.device.unprotectDevice).to.have.been.calledTwice;
expect(deviceProtectionCommands.api.unprotectDevice).to.have.been.calledTwice;
expect(deviceProtectionCommands._markAsDevelopmentDevice).to.have.been.calledOnce;
});

it('handles already open devices', async () => {
sinon.stub(deviceProtectionCommands, '_getDeviceProtection')
.onFirstCall().resolves({ protected: false, overridden: false });
Expand Down
Binary file not shown.

0 comments on commit fa277b4

Please sign in to comment.