Skip to content

Commit

Permalink
[ares-package] Add --remainPlainIPK option
Browse files Browse the repository at this point in the history
  • Loading branch information
THIEM VAN NGUYEN/LGEDV webOS FRAMEWORK Thiem.nguyen SERVICE PART authored and nguyen-van-quang committed Jan 2, 2025
1 parent 4f3e326 commit 4d06e62
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
15 changes: 13 additions & 2 deletions bin/ares-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ function PalmPackage() {
"pkgversion": String,
"pkginfofile": String,
"info": String,
"info-detail": String
"info-detail": String,
"remainplainipk": Boolean
};

const shortHands = {
Expand All @@ -76,7 +77,8 @@ function PalmPackage() {
"pf": "--pkginfofile",
"i": "--info",
"I": "--info-detail",
"v": ["--level", "verbose"]
"v": ["--level", "verbose"],
"rpi": "--remainplainipk"
};

this.argv = nopt(knownOpts, shortHands, process.argv, 2 /* drop 'node' & basename*/);
Expand Down Expand Up @@ -163,6 +165,11 @@ PalmPackage.prototype = {
}
}

const lowerCaseKeyArgv = {};
for (const key in this.argv) {
lowerCaseKeyArgv[key.toLocaleLowerCase()] = this.argv[key];
}

if (Object.hasOwnProperty.call(this.argv, 'minify')) {
this.options.minify = this.argv.minify;
} else {
Expand Down Expand Up @@ -212,6 +219,10 @@ PalmPackage.prototype = {
if (Object.hasOwnProperty.call(this.argv, 'info')) {
this.options.info = this.argv.info;
}

if (Object.hasOwnProperty.call(lowerCaseKeyArgv, 'remainplainipk')) {
this.options.remainPlainIPK = lowerCaseKeyArgv.remainplainipk;
}
},

setOutputDir: function(next) {
Expand Down
29 changes: 27 additions & 2 deletions lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const ar = require('ar-async'),
this.pkgResourceNames = [];
this.pkgId;
this.pkginfofile;
this.remainPlainIPK = false;
// To check app signing
this.dataHash = '';
this.certificateHash = '';
Expand Down Expand Up @@ -131,6 +132,10 @@ const ar = require('ar-async'),
this.encrypt = options.encrypt;
}

if (options && Object.prototype.hasOwnProperty.call(options, 'remainPlainIPK')) {
this.remainPlainIPK = options.remainPlainIPK;
}

if (options && Object.prototype.hasOwnProperty.call(options, 'sign')) {
if (!fs.existsSync(path.resolve(options.sign))) {
return next(errHndl.getErrMsg("NOT_EXIST_PATH", options.sign));
Expand Down Expand Up @@ -1564,6 +1569,7 @@ const ar = require('ar-async'),
filename = filename.concat(".ipk");
}
this.ipkFileName = filename;
this.plainIpkFileName = filename + '_plain';
setImmediate(next);
}

Expand Down Expand Up @@ -2185,14 +2191,26 @@ const ar = require('ar-async'),
}

function copyOutputToDst(destination, middleCb, next) {
let self = this;
// copy data directory to destination
if (this.rom) {
middleCb("Create output directory to " + destination);
copySrcToDst.call(this, path.join(this.tempDir, 'data'), destination, next);
} else if (this.encrypt) {
// copy encrypted ipk to destination
middleCb("Create encrypted " + this.ipkFileName + " to " + destination);
copySrcToDst.call(this, path.join(this.encryptDir, this.ipkFileName), destination, next);
if (this.remainPlainIPK) {
log.verbose("Remain Plain IPK file from : " + self.tempDir, self.plainIpkFileName);
log.verbose(" to : " + destination);
middleCb("Remain Plain IPK file " + self.plainIpkFileName + " to " + destination);
copySrcToDst.call(self, path.join(self.tempDir, self.plainIpkFileName), destination, function() {
middleCb("Create encrypted " + self.ipkFileName + " to " + destination);
copySrcToDst.call(self, path.join(self.encryptDir, self.ipkFileName), destination, next);
});

} else {
middleCb("Create encrypted " + this.ipkFileName + " to " + destination);
copySrcToDst.call(this, path.join(this.encryptDir, this.ipkFileName), destination, next);
}
} else {
// copy plain ipk to destination
let outmsg = "Create ";
Expand All @@ -2209,13 +2227,20 @@ const ar = require('ar-async'),
const plainIpkPath = path.join(this.tempDir, this.ipkFileName),
encrypedIpkPath = path.join(dstPath, this.ipkFileName);

let self = this;

try {
const input = fs.createReadStream(plainIpkPath),
output = fs.createWriteStream(encrypedIpkPath),
cipher = crypto.createCipheriv('aes-256-cbc', this.key, this.iv);

output.on('close', function() {
log.verbose("package#encryptPackage()#encrypIpk()", "encrypted Ipk to " + encrypedIpkPath);
if (self.remainPlainIPK === true) {
log.verbose("Remain plain IPK file name : " + path.join(plainIpkPath));
log.verbose("** to : " + path.join(self.tempDir, self.plainIpkFileName));
fs.renameSync(plainIpkPath, path.join(self.tempDir, self.plainIpkFileName));
}
setImmediate(next);
}).
on('error', function(err) {
Expand Down
19 changes: 19 additions & 0 deletions spec/jsSpecs/ares-package.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const tempDirPath = path.join(__dirname, "..", "tempFiles"),
outputPath = path.join(tempDirPath, "output"),
appPathByRom = path.join(outputPath, "usr/palm/applications"),
appPkgPath = path.join(outputPath, "com.webos.sample.app_1.0.0_all.ipk"),
plainAppPkgPath = path.join(outputPath, "com.webos.sample.app_1.0.0_all.ipk_plain"),
svcPkgPath = path.join(outputPath, "com.webos.sample_1.0.0_all.ipk"),
appinfoPath = path.join(sampleAppPath, "appinfo.json"),
signKeyPath = path.join(tempDirPath, "sign/signPriv.key"),
Expand Down Expand Up @@ -494,6 +495,24 @@ describe(aresCmd + ' --encrypt(-enc)', function() {
done();
});
});

it('Encrypted ipk and keep plain IPK', function(done) {
exec(cmd + ` -enc -rpi ${sampleAppPath} ${sampleServicePaths[0]} -o ${outputPath}`, function(error, stdout, stderr) {
if (stderr && stderr.length > 0) {
common.detectNodeMessage(stderr);
expect(stderr).toContain("ares-package ERR! [syscall failure]: ENOENT: no such file or directory, open", error);
expect(stderr).toContain("ares-package ERR! [Tips]: Please check if the path is valid", error);
}
if(options.profile && options.profile === 'signage'){
expect(fs.existsSync(appPkgPath)).toBe(true);
expect(fs.existsSync(plainAppPkgPath)).toBe(true);
} else {
expect(fs.existsSync(appPkgPath)).toBe(false);
expect(fs.existsSync(plainAppPkgPath)).toBe(false);
}
done();
});
});
});

describe(aresCmd + ' --sign(-s) & --certificate(-crt)', function() {
Expand Down

0 comments on commit 4d06e62

Please sign in to comment.