Skip to content

Commit

Permalink
Add filestorage
Browse files Browse the repository at this point in the history
  • Loading branch information
bunseokbot committed Feb 19, 2021
1 parent bd8b9b6 commit d7449ab
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 15 deletions.
1 change: 1 addition & 0 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sourceSets {

dependencies {
implementation("com.google.protobuf:protobuf-java:3.6.1")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.springframework.boot:spring-boot-starter-data-redis")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package project.namjun.kim.robster.analysis

import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import project.namjun.kim.robster.proto.RobsterGrpc
import org.springframework.web.multipart.MultipartFile
import project.namjun.kim.robster.proto.RobsterOuterClass
import java.lang.Exception
import java.nio.file.Path
import java.nio.file.Paths

@RestController
@RequestMapping("/analysis")
Expand All @@ -14,9 +15,18 @@ class AnalysisController {
@Autowired
lateinit var analysisService: AnalysisService

@Autowired
lateinit var fileStorage: FileStorage

private val rootLocation: Path = Paths.get("storage")

@PostMapping("/")
fun requestAnalysis(@RequestBody analysisDTO: AnalysisDTO): AnalysisMapping {
var analysisResult: RobsterOuterClass.AnalysisResponse = analysisService.executeAnalysis(analysisDTO)
fun requestAnalysis(@RequestParam("file") file: MultipartFile): AnalysisMapping {
val filePath: String = fileStorage.storeFile(file)
val analysisResult: RobsterOuterClass.AnalysisResponse = analysisService.executeAnalysis(
filePath = filePath,
fileType = "apk"
)
return AnalysisMapping(
id = analysisResult.id,
status = analysisResult.status,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package project.namjun.kim.robster.analysis

data class AnalysisMapping(
var id: String,
var id: String?,
var status: Boolean,
var message: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import java.util.*

@Service
class AnalysisService {
@Value("\${robster.analyzer.host}")
@Value("\${robster.engine.host}")
lateinit var analyzerHost: String

@Value("\${robster.analyzer.port}")
@Value("\${robster.engine.port}")
var analyzerPort: Int = 0
val uniqueId: String = UUID.randomUUID().toString()

fun executeAnalysis(analysisDTO: AnalysisDTO): RobsterOuterClass.AnalysisResponse {
fun executeAnalysis(filePath: String, fileType: String): RobsterOuterClass.AnalysisResponse {
var managedChannel: ManagedChannel = ManagedChannelBuilder.forAddress(
analyzerHost, analyzerPort
).usePlaintext().build()
var robsterStub: RobsterGrpc.RobsterBlockingStub = RobsterGrpc.newBlockingStub(managedChannel)
var analysisRequest: RobsterOuterClass.AnalysisRequest = RobsterOuterClass.AnalysisRequest.newBuilder()
.setId(uniqueId)
.setPath(analysisDTO.requestPath)
.setType(analysisDTO.requestType)
.setPath(filePath)
.setType(fileType)
.build()

var analysisResponse: RobsterOuterClass.AnalysisResponse = robsterStub.executeAnalysis(analysisRequest)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package project.namjun.kim.robster.analysis

import org.springframework.core.io.Resource
import org.springframework.web.multipart.MultipartFile

interface FileStorage {
fun storeFile(file: MultipartFile): String
fun loadFile(path: String): Resource
fun getFileHash(path: String): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package project.namjun.kim.robster.analysis

import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.Resource
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import java.io.InputStream
import java.nio.file.FileAlreadyExistsException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.security.MessageDigest

@Service
class FileStorageImpl: FileStorage {
@Value("\${robster.storage}")
lateinit var storage: String

override fun storeFile(file: MultipartFile): String {
val rootLocation: Path = Paths.get(this.storage)
val filename: String = calculateHash(file.inputStream)
try {
Files.copy(file.inputStream, rootLocation.resolve(filename))
} catch (e: FileAlreadyExistsException) {

}
return Paths.get(this.storage, filename).toString()
}

override fun loadFile(path: String): Resource {
TODO("Not yet implemented")
}

override fun getFileHash(path: String): String {
TODO("Not yet implemented")
}

fun calculateHash(stream: InputStream): String {
return MessageDigest
.getInstance("SHA-256")
.digest(stream.readAllBytes())
.fold("", { str, it -> str + "%02x".format(it) })
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package project.namjun.kim.robster.cluster

import org.springframework.data.mongodb.core.mapping.Field

data class Cluster (
@Field("_id")
var id: String,

@Field("time")
var time: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package project.namjun.kim.robster.cluster

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.find
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query
import org.springframework.data.mongodb.core.query.isEqualTo
import org.springframework.web.bind.annotation.*
import project.namjun.kim.robster.report.Report

@RestController
@RequestMapping("/cluster")
class ClusterController {

@Autowired
private lateinit var mongoTemplate: MongoTemplate

@GetMapping("/model/hash/{modelHash}")
fun clusterModelByHash(@PathVariable("modelHash") modelHash: String): List<Cluster>? {
val query: Query = Query()
.addCriteria(
Criteria.where("models").elemMatch(
Criteria.where("hash").isEqualTo(modelHash)
)
)
query.fields().include("_id", "time")

return this.mongoTemplate.find(query, "reports")
}

@GetMapping("/model/type/{modelType}")
fun clusterModelByType(@PathVariable("modelType") modelType: String): List<Cluster>? {
val query: Query = Query()
.addCriteria(
Criteria.where("models").elemMatch(
Criteria.where("type").isEqualTo(modelType)
)
)

return this.mongoTemplate.find(query, "reports")
}

@GetMapping("/method/type/{methodType}")
fun clusterMethodByType(@PathVariable("methodType") methodType: String): List<Cluster>? {
val query: Query = Query()
.addCriteria(
Criteria.where("methods").elemMatch(
Criteria.where("type").isEqualTo(methodType)
)
)

return this.mongoTemplate.find(query, "reports")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package project.namjun.kim.robster.report

import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.core.mapping.Field

@Document(collection="reports")
data class Report(
@Field("_id")
var id: String,

@Field("time")
var time: String,

@Field("models")
var models: List<Model>,

@Field("keywords")
var keywords: List<Keyword>,

@Field("methods")
var methods: List<Method>
)

data class Node(
var name: String,
var index: Int = 0
)

data class Model(
var type: String,
var path: String,
var hash: String,
var structure: List<Node>
)

data class Keyword(
var keyword: String,
var path: String
)

data class Method(
var type: String,
var method: String
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package project.namjun.kim.robster.report

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
Expand All @@ -8,9 +10,13 @@ import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/report")
class ReportController {

@Autowired
private lateinit var reportRepository: ReportRepository

@GetMapping("/{reportId}")
fun getReportById(@PathVariable("reportId") reportId: String): String {
return reportId
fun getReportById(@PathVariable("reportId") reportId: String): Report? {
return reportRepository.getReportById(reportId)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package project.namjun.kim.robster.report

import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Repository

@Repository
interface ReportRepository: MongoRepository<Report, String> {
fun getReportById(reportId: String): Report
}
11 changes: 9 additions & 2 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
robster.analyzer.host=localhost
robster.analyzer.port=50051
robster.engine.host=localhost
robster.engine.port=50051
robster.storage=D:\\storage

spring.data.mongodb.uri=mongodb://localhost:27017
spring.data.mongodb.database=robster

spring.servlet.multipart.max-file-size=2GB
spring.servlet.multipart.max-request-size=2GB

0 comments on commit d7449ab

Please sign in to comment.