-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from bcgov/claim-av-starter
Adding ClamAv Lib
- Loading branch information
Showing
16 changed files
with
455 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# spring-clamav-starter | ||
|
||
A stater to facilitate usage of ClamAv | ||
|
||
## Usage | ||
|
||
Add spring-bceid-starter as a maven dependency | ||
|
||
```xml | ||
<dependencies> | ||
<dependency> | ||
<groupId>ca.bc.gov.open</groupId> | ||
<artifactId>spring-clamav-starter</artifactId> | ||
<version>0.1.4</version> | ||
</dependency> | ||
</dependencies> | ||
``` | ||
|
||
## How to use the service | ||
|
||
The following code should be used to implement this component: | ||
|
||
```java | ||
|
||
@autowired | ||
private ClamAvService clamAvService | ||
|
||
@Service | ||
public class FileScan { | ||
|
||
public void Scan(InputStream inputStream) { | ||
|
||
try { | ||
clamAvService.scan(inputStream); | ||
} catch (VirusDetectedException e) { | ||
// file is infected | ||
} | ||
|
||
} | ||
} | ||
|
||
``` | ||
|
||
|
||
## Configuration | ||
|
||
| name | definition | required | | ||
| --- | --- | --- | | ||
| [bcgov.clamav.service.host](#bcgovclamavservicehost) | String | No | | ||
| [bcgov.clamav.service.port](#bcgovclamavserviceport) | String | No | | ||
| [bcgov.clamav.service.timeout](#bcgovclamavservicetimeout) | String | No | | ||
|
||
#### cgov.clamav.service.host | ||
|
||
* Value type is String | ||
|
||
Default value is **localhost** | ||
|
||
Sets the port of the host client | ||
|
||
#### bcgov.clamav.service.port | ||
|
||
* Value type is Integer | ||
|
||
Default value is **3310** | ||
|
||
Sets the port of the clamAv client | ||
|
||
#### bcgov.clamav.service.timeout | ||
|
||
* Value type is Integer | ||
|
||
Default value is **500** | ||
|
||
Sets the timeout of the clamAv client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>ca.bc.gov.open</groupId> | ||
<artifactId>spring-clamav-starter</artifactId> | ||
<version>0.1.4</version> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<spring-boot.version>2.2.4.RELEASE</spring-boot.version> | ||
<org.apache.maven.plugins.version.version>2.22.0</org.apache.maven.plugins.version.version> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>fi.solita.clamav</groupId> | ||
<artifactId>clamav-client</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>${spring-boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>ca.bc.gov.open</groupId> | ||
<artifactId>spring-starters-bom</artifactId> | ||
<version>0.1.4</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>${org.apache.maven.plugins.version.version}</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<version>${org.apache.maven.plugins.version.version}</version> | ||
</plugin> | ||
<!--jacoco code coverage--> | ||
<plugin> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<version>0.8.2</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>prepare-agent</goal> | ||
</goals> | ||
</execution> | ||
<!-- attached to Maven test phase --> | ||
<execution> | ||
<id>report</id> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>report</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
40 changes: 40 additions & 0 deletions
40
src/spring-clamav-starter/src/main/java/ca/bc/gov/open/clamav/starter/AutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ca.bc.gov.open.clamav.starter; | ||
|
||
import fi.solita.clamav.ClamAVClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
@EnableConfigurationProperties(ClamAvProperties.class) | ||
public class AutoConfiguration { | ||
|
||
private final ClamAvProperties clamAvProperties; | ||
|
||
Logger logger = LoggerFactory.getLogger(AutoConfiguration.class); | ||
|
||
public AutoConfiguration(ClamAvProperties clamAvProperties) { | ||
this.clamAvProperties = clamAvProperties; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(ClamAVClient.class) | ||
public ClamAVClient clamAVClient() { | ||
|
||
logger.debug("Configuring ClamAv Client"); | ||
logger.debug("ClamAv host: [{}]", clamAvProperties.getHost()); | ||
logger.debug("ClamAv port: [{}]", clamAvProperties.getPort()); | ||
logger.debug("ClamAv timeout: [{}]", clamAvProperties.getTimeout()); | ||
|
||
return new ClamAVClient(clamAvProperties.getHost(), clamAvProperties.getPort()); | ||
|
||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(ClamAvService.class) | ||
public ClamAvService clamAvService(ClamAVClient clamAVClient) { | ||
return new ClamAvServiceImpl(clamAVClient); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/spring-clamav-starter/src/main/java/ca/bc/gov/open/clamav/starter/ClamAvException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ca.bc.gov.open.clamav.starter; | ||
|
||
public class ClamAvException extends RuntimeException { | ||
|
||
public ClamAvException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/spring-clamav-starter/src/main/java/ca/bc/gov/open/clamav/starter/ClamAvProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ca.bc.gov.open.clamav.starter; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "bcgov.clamav") | ||
public class ClamAvProperties { | ||
|
||
private String host = "localhost"; | ||
private int port = 3310; | ||
private int timeout = 500; | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public void setHost(String host) { | ||
this.host = host; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public void setPort(int port) { | ||
this.port = port; | ||
} | ||
|
||
public int getTimeout() { | ||
return timeout; | ||
} | ||
|
||
public void setTimeout(int timeout) { | ||
this.timeout = timeout; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/spring-clamav-starter/src/main/java/ca/bc/gov/open/clamav/starter/ClamAvService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ca.bc.gov.open.clamav.starter; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public interface ClamAvService { | ||
|
||
void scan(InputStream inputStream) throws VirusDetectedException; | ||
|
||
boolean ping() throws IOException; | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/spring-clamav-starter/src/main/java/ca/bc/gov/open/clamav/starter/ClamAvServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ca.bc.gov.open.clamav.starter; | ||
|
||
import fi.solita.clamav.ClamAVClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class ClamAvServiceImpl implements ClamAvService { | ||
|
||
private Logger logger = LoggerFactory.getLogger(AutoConfiguration.class); | ||
|
||
private final ClamAVClient clamAVClient; | ||
|
||
public ClamAvServiceImpl(ClamAVClient clamAVClient) { | ||
this.clamAVClient = clamAVClient; | ||
} | ||
|
||
@Override | ||
public void scan(InputStream inputStream) throws VirusDetectedException { | ||
|
||
byte[] reply; | ||
try { | ||
reply = clamAVClient.scan(inputStream); | ||
} catch (IOException e) { | ||
logger.error("ClamAv Service could not scan the input"); | ||
throw new ClamAvException("Could not scan the input", e); | ||
} | ||
if (!ClamAVClient.isCleanReply(reply)) { | ||
logger.error("Virus Detected using ClamAv server"); | ||
throw new VirusDetectedException("ClamAv has detected a virus in the input"); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean ping() throws IOException { | ||
return clamAVClient.ping(); | ||
} | ||
} |
Oops, something went wrong.