Commit 2f4e9a00 authored by Adrien's avatar Adrien

split windows and macos logic and implements installer buil

parent 5038d0dd
/**
* Copyright (C) 2018 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.exception
class BuildDmgException : Throwable() {
}
\ No newline at end of file
/**
* Copyright (C) 2018 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.exception
class SignException() : Throwable(){
}
\ No newline at end of file
......@@ -16,6 +16,8 @@ package org.bonitasoft.releng
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.bonitasoft.exception.BuildDmgException
import org.bonitasoft.exception.SignException
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestParam
......@@ -33,6 +35,9 @@ class CodeSigningRestController {
val logger: Log = LogFactory.getLog(javaClass)
val macOs: String = "Mac OS X"
var windowsCodeSign = WindowsCodeSign()
var macCodeSign = MacCodeSign()
@PostMapping("/sign")
fun signProduct(@RequestParam("exeFile") file: MultipartFile,
response: HttpServletResponse): StreamingResponseBody {
......@@ -50,39 +55,32 @@ class CodeSigningRestController {
logger.info("Copy of input file as $exeCopy....")
file.transferTo(exeCopy)
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename='${file.originalFilename}'");
var signResult = 1
if (System.getProperty("os.name") == macOs) { // macOS
// TODO exeFile is a zip...
val processBuilder = ProcessBuilder("echo", "bonitasoft", "|", "sudo", "-S", "codesign", "--deep", "-s", "Bonitasoft", exeCopy.canonicalFile.absolutePath)
logger.info("Signing macOS file...")
signResult = processBuilder.inheritIO().start().waitFor()
} else { // windows
val processBuilder = ProcessBuilder("signtool", "sign", "/tr", "http://timestamp.digicert.com",
"/td", "sha256", "/fd", "sha256", "/a", exeCopy.canonicalFile.absolutePath)
logger.info("Signing windows file...")
signResult = processBuilder.inheritIO().start().waitFor()
}
val fileSigned : File
if (signResult != 0) {
logger.error("An error occured while signing file.")
try {
if (System.getProperty("os.name") == macOs) {
fileSigned = macCodeSign.signMacProduct(exeCopy)
} else {
fileSigned = windowsCodeSign.signWindowsProduct(exeCopy)
}
} catch (e: SignException) {
response.setStatus(500);
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured while signing file.".toByteArray())
}
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename='${fileSigned.name}'");
return StreamingResponseBody { outputStream ->
FileInputStream(exeCopy.canonicalFile).use { inputStream ->
FileInputStream(fileSigned.canonicalFile).use { inputStream ->
inputStream.copyTo(outputStream)
outputStream.flush()
outputStream.close()
}
if (!exeCopy.delete()) {
logger.error("Failed to delete temp file $exeCopy")
if (!fileSigned.delete()) {
logger.error("Failed to delete temp file $fileSigned")
}
}
} catch (e: Throwable) {
......@@ -97,8 +95,6 @@ class CodeSigningRestController {
@PostMapping( "/buildAndSignMacInstaller")
fun buildAndSignOsxInstaller(@RequestParam("exeFile") file: MultipartFile,
response: HttpServletResponse): StreamingResponseBody {
if (System.getProperty("os.name") == macOs) {
// TODO exeFile is a zip...
if (file.isEmpty) {
logger.error("File '%s' is empty".format(file.originalFilename))
response.setStatus(400)
......@@ -112,64 +108,33 @@ class CodeSigningRestController {
logger.info("Copy of input file as $exeCopy....")
file.transferTo(exeCopy)
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename='${file.originalFilename}'");
var processResult = 1
var processBuilder = ProcessBuilder("echo", "bonitasoft", "|", "sudo", "-S", "codesign", "--deep", "-s", "Bonitasoft", exeCopy.canonicalFile.absolutePath)
logger.info("Signing macOS installer...")
processResult = processBuilder.inheritIO().start().waitFor()
if (processResult != 0) {
logger.error("An error occured while signing installer.")
response.setStatus(500);
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured while signing file.".toByteArray())
}
}
logger.info("Building dmg...")
var dmgName = exeCopy.name.substringBefore('.', exeCopy.name) + ".dmg"
processBuilder = ProcessBuilder("echo", "bonitasoft", "|", "sudo", "-S", "hdiutil", "create", "-volname", dmgName, "-srcfolder", exeCopy.canonicalFile.absolutePath, "-ov", "-format", "UDZO", dmgName)
processResult = processBuilder.inheritIO().start().waitFor()
if (processResult != 0) {
logger.error("An error occured while creating dmg.")
response.setStatus(500);
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured while creating dmg.".toByteArray())
}
}
var dmgPath = exeCopy.canonicalFile.absolutePath.substringBefore('.', exeCopy.name) + ".dmg"
processBuilder = ProcessBuilder("codesign", "-s", "Bonitasoft", dmgPath)
logger.info("Signing dmg...")
processResult = processBuilder.inheritIO().start().waitFor()
if (processResult != 0) {
logger.error("An error occured while signing dmg.")
response.setStatus(500);
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured while signin dmg.".toByteArray())
}
}
return StreamingResponseBody { outputStream ->
val dmgFile : File = File(dmgPath);
FileInputStream(dmgFile).use { inputStream ->
inputStream.copyTo(outputStream)
outputStream.flush()
outputStream.close()
}
if (!exeCopy.delete()) {
logger.error("Failed to delete temp file $exeCopy")
}
if (!dmgFile.delete()) {
logger.error("Failed to delete temp file $dmgFile")
}
}
try {
val fileSigned: File = macCodeSign.buildAndSignMacInstaller(exeCopy)
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename='${fileSigned.name}'");
return StreamingResponseBody { outputStream ->
FileInputStream(fileSigned).use { inputStream ->
inputStream.copyTo(outputStream)
outputStream.flush()
outputStream.close()
}
if (!fileSigned.delete()) {
logger.error("Failed to delete temp file $fileSigned")
}
}
} catch (e : SignException) {
response.setStatus(500);
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured while signing file.".toByteArray())
}
} catch (e : BuildDmgException) {
response.setStatus(500);
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured while building dmg.".toByteArray())
}
}
} catch (e: Throwable) {
logger.error("An error occured", e)
}
......@@ -177,9 +142,7 @@ class CodeSigningRestController {
return StreamingResponseBody { outputStream ->
outputStream.write("An error occured server side. Check logs.".toByteArray())
}
}
return StreamingResponseBody { outputStream ->
outputStream.write("Endpoint only available on macOS".toByteArray())
}
}
}
\ No newline at end of file
/**
* Copyright (C) 2018 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.releng
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.bonitasoft.exception.BuildDmgException
import org.bonitasoft.exception.SignException
import java.io.File
class MacCodeSign {
val logger: Log = LogFactory.getLog(javaClass)
val separator = System.getProperty("file.separator")
fun signMacProduct(zipFile: File) : File {
val file : File = unzip(zipFile);
if (!zipFile.delete()) {
logger.error("Failed to delete temp file $zipFile")
throw Throwable()
}
val processBuilder = ProcessBuilder("echo", "bonitasoft", "|", "sudo", "-S", "codesign", "--deep", "-s", "Bonitasoft", file.canonicalPath)
logger.info("Signing macOS file...")
val signResult = processBuilder.inheritIO().start().waitFor()
if (signResult != 0) {
logger.error("An error occured while signing file.")
throw SignException()
}
return zip(file);
}
fun buildAndSignMacInstaller(zipFile: File) : File {
val file : File = unzip(zipFile);
if (!zipFile.delete()) {
logger.error("Failed to delete temp file $zipFile")
throw Throwable()
}
logger.info("Signing macOS installer...")
var processBuilder = ProcessBuilder("echo", "bonitasoft", "|", "sudo", "-S", "codesign", "--deep", "-s", "Bonitasoft", file.canonicalPath)
var signResult = processBuilder.inheritIO().start().waitFor()
if (signResult != 0) {
logger.error("An error occured while signing installer.")
throw SignException()
}
logger.info("Building dmg...")
var dmgName = file.nameWithoutExtension
var dmgPath = file.parentFile.canonicalPath + separator + file.nameWithoutExtension + ".dmg"
processBuilder = ProcessBuilder("echo", "bonitasoft", "|", "sudo", "-S", "hdiutil", "create", "-volname", dmgName, "-srcfolder", file.canonicalPath, "-ov", "-format", "UDZO", dmgPath)
val buildDmgResult = processBuilder.inheritIO().start().waitFor()
if (buildDmgResult != 0) {
logger.error("An error occured while building dmg.")
throw BuildDmgException()
}
if (!file.delete()) {
throw Throwable("Failed to delete temp file $file")
}
logger.info("Signing macOS dmg...")
processBuilder = ProcessBuilder("codesign", "-s", "Bonitasoft", dmgPath)
signResult = processBuilder.inheritIO().start().waitFor()
if (signResult != 0) {
logger.error("An error occured while signing dmg.")
throw SignException()
}
return File(dmgPath)
}
fun unzip(zipFile: File) : File {
ProcessBuilder()
.command("unzip", zipFile.canonicalPath)
.start()
.waitFor()
var file :File = File(zipFile.parentFile.canonicalPath + separator + zipFile.nameWithoutExtension + ".app")
if (!file.exists()) {
logger.error("failed to unzip $zipFile")
throw Throwable()
}
return file
}
fun zip(file: File) : File {
val zipPath : String = file.parentFile.canonicalPath + separator + file.nameWithoutExtension + ".zip"
ProcessBuilder()
.command("zip", zipPath, file.canonicalPath)
.start()
.waitFor()
var zipFile = File(zipPath)
if (!zipFile.exists()) {
logger.error("failed to unzip $file")
throw Throwable()
}
return zipFile
}
}
\ No newline at end of file
/**
* Copyright (C) 2018 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.releng
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.bonitasoft.exception.SignException
import org.springframework.web.multipart.MultipartFile
import java.io.File
import javax.servlet.http.HttpServletResponse
class WindowsCodeSign {
val logger: Log = LogFactory.getLog(javaClass)
fun signWindowsProduct(file: File) : File {
val processBuilder = ProcessBuilder("signtool", "sign", "/tr", "http://timestamp.digicert.com",
"/td", "sha256", "/fd", "sha256", "/a", file.canonicalFile.absolutePath)
logger.info("Signing windows file...")
val signResult = processBuilder.inheritIO().start().waitFor()
if (signResult != 0) {
logger.error("An error occured while signing file.")
throw SignException()
}
return file;
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment