Unverified Commit 2067c488 authored by Adrien's avatar Adrien Committed by GitHub

feat(codesign) improve robusteness (#12)

* use temp directories to support concurent acccess
parent 45a0700f
......@@ -25,16 +25,15 @@ import org.springframework.web.multipart.MultipartFile
import java.io.File
import java.io.FileInputStream
import java.nio.file.Files
import javax.servlet.http.HttpServletResponse
import java.nio.file.Path
import java.nio.file.Paths
import java.io.FileOutputStream
import javax.servlet.http.HttpServletResponse
@RestController
class CodeSigningRestController {
val logger: Log = LogFactory.getLog(javaClass)
val macOs: String = "Mac OS X"
var windowsCodeSign = WindowsCodeSign()
var macCodeSign = MacCodeSign()
......@@ -48,9 +47,9 @@ class CodeSigningRestController {
response.outputStream.close()
return
}
val tempDirectory: File = Files.createDirectory(Paths.get("codesign" + System.currentTimeMillis())).toFile().absoluteFile
try {
val exeCopy: File = Files.createFile(Paths.get(file.originalFilename+System.currentTimeMillis())).toFile().absoluteFile
val exeCopy: File = Files.createFile(tempDirectory.toPath().resolve(file.originalFilename)).toFile().absoluteFile
logger.info("Copy of input file as $exeCopy....")
file.transferTo(exeCopy)
......@@ -62,9 +61,9 @@ class CodeSigningRestController {
} else {
fileSigned = windowsCodeSign.signWindowsProduct(exeCopy)
}
response.setContentType("application/octet-stream")
response.setContentType("application/octet-stream")
response.setHeader("Content-Disposition", "attachment; filename='${fileSigned.name}'")
FileInputStream(fileSigned.canonicalFile).use { inputStream ->
inputStream.copyTo(response.outputStream)
response.outputStream.close()
......@@ -76,11 +75,11 @@ class CodeSigningRestController {
response.outputStream.write("An error occured while signing file.".toByteArray())
response.outputStream.close()
return
}finally{
if (!exeCopy.delete()) {
logger.error("Failed to delete temp file $exeCopy")
} finally {
if (!tempDirectory.deleteRecursively()) {
logger.error("$tempDirectory has not been deleted")
}
}
}
} catch (e: Throwable) {
logger.error("An error occured", e)
......@@ -100,8 +99,9 @@ class CodeSigningRestController {
response.outputStream.close()
return
}
val tempDirectory: File = Files.createDirectory(Paths.get("codesign" + System.currentTimeMillis())).toFile().absoluteFile
try {
val exeCopy: File = Files.createTempFile(file.originalFilename, "").toFile()
val exeCopy: File = Files.createFile(tempDirectory.toPath().resolve(file.originalFilename)).toFile().absoluteFile
logger.info("Copy of input file as $exeCopy....")
file.transferTo(exeCopy)
......@@ -115,10 +115,7 @@ class CodeSigningRestController {
inputStream.copyTo(response.outputStream)
response.outputStream.close()
}
if (!fileSigned.delete()) {
logger.error("Failed to delete temp file $fileSigned")
}
return
return
} catch (e: SignException) {
logger.error(e)
response.setStatus(500);
......@@ -134,6 +131,10 @@ class CodeSigningRestController {
}
} catch (e: Throwable) {
logger.error("An error occured", e)
} finally {
if (!tempDirectory.deleteRecursively()) {
logger.error("$tempDirectory has not been deleted")
}
}
response.outputStream.write("An error occured server side. Check logs.".toByteArray())
......
......@@ -19,8 +19,8 @@ import org.apache.commons.logging.LogFactory
import org.bonitasoft.exception.BuildDmgException
import org.bonitasoft.exception.SignException
import java.io.File
import java.io.IOException
import java.io.FileNotFoundException
import java.io.IOException
class MacCodeSign {
val logger: Log = LogFactory.getLog(javaClass)
......@@ -29,8 +29,7 @@ class MacCodeSign {
fun signMacProduct(zipFile: File): File {
val file: File = unzip(zipFile);
if (!zipFile.delete()) {
logger.error("Failed to delete temp file $zipFile")
throw Throwable()
throw IOException("Failed to delete temp file $zipFile")
}
logger.info("Signing osx application...")
......@@ -45,7 +44,6 @@ class MacCodeSign {
}
var newZipFile = zip(file);
deleteApplication(file)
logger.info("Osx application has been signed successfully")
return newZipFile
}
......@@ -76,7 +74,7 @@ class MacCodeSign {
.command("hdiutil", "create", "-volname", dmgName, "-srcfolder", file.name, "-ov", "-format", "UDZO", dmgName + ".dmg")
.start()
.waitFor()
var dmg = File(dmgPath);
var dmg = File(dmgPath)
if (buildDmgResult != 0 || !dmg.exists()) {
throw BuildDmgException("An error occured while building dmg")
}
......@@ -91,9 +89,6 @@ class MacCodeSign {
if (signResult != 0) {
throw SignException("An error occured while signing dmg.")
}
deleteApplication(file)
logger.info("dmg has been created and signed successfully")
return dmg
}
......@@ -105,39 +100,27 @@ class MacCodeSign {
.command("unzip", zipFile.name)
.start()
.waitFor()
if (result != 0) {
if (result != 0) {
throw IOException("failed to unzip $zipFile. 'unzip' command return code: $result")
}
var file: File = File(zipFile.parentFile.canonicalPath + separator + zipFile.nameWithoutExtension)
if(!file.exists()){
throw FileNotFoundException("$zipFile.nameWithoutExtension not found in $zipFile.parentFile.canonicalPath")
}
if (!file.exists()) {
throw FileNotFoundException("${zipFile.nameWithoutExtension} not found in ${zipFile.parentFile.canonicalPath}")
}
return file
}
fun zip(file: File): File {
ProcessBuilder()
val res = ProcessBuilder()
.inheritIO()
.directory(file.parentFile)
.command("zip", file.nameWithoutExtension + ".zip", "-r", file.name)
.start()
.waitFor()
var zipFile = File(file.parentFile.canonicalPath + separator + file.nameWithoutExtension + ".zip")
if (!zipFile.exists()) {
throw Throwable("failed to zip $file")
if (res != 0) {
throw IOException("failed to zip $file")
}
return zipFile
return File(file.parentFile.canonicalPath + separator + file.nameWithoutExtension + ".zip")
}
fun deleteApplication(application: File) {
ProcessBuilder()
.inheritIO()
.directory(application.parentFile)
.command("rm", "-r", application.name)
.start()
.waitFor()
}
}
\ No newline at end of file
......@@ -32,7 +32,6 @@ class WindowsCodeSign {
if (signResult == 1) {
throw SignException("An error occured while signing file.")
}
return 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