Skip to content

Commit

Permalink
Merge pull request #15 from bcgov/claim-av-starter
Browse files Browse the repository at this point in the history
Adding ClamAv Lib
  • Loading branch information
Adam Kroon authored Aug 18, 2020
2 parents 7e777e4 + df713b8 commit f5552bc
Show file tree
Hide file tree
Showing 16 changed files with 455 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

<groupId>ca.bc.gov.open</groupId>
<artifactId>spring-starters</artifactId>
<version>0.1.3</version>
<modules>

</modules>
<version>0.1.4</version>

<name>spring-starters</name>
<packaging>pom</packaging>
Expand All @@ -25,25 +22,34 @@
<module>spring-sftp-starter</module>
<module>spring-bceid-starter</module>
<module>spring-starters-bom</module>
<module>spring-clamav-starter</module>
</modules>
</profile>

<profile>
<id>spring-sftp-starter</id>
<id>sftp</id>
<modules>
<module>spring-starters-bom</module>
<module>spring-sftp-starter</module>
</modules>
</profile>

<profile>
<id>spring-bceid-starter</id>
<id>bceid</id>
<modules>
<module>spring-starters-bom</module>
<module>spring-bceid-starter</module>
</modules>
</profile>

<profile>
<id>clamav</id>
<modules>
<module>spring-starters-bom</module>
<module>spring-clamav-starter</module>
</modules>
</profile>

</profiles>

</project>
2 changes: 1 addition & 1 deletion src/spring-bceid-starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Add spring-bceid-starter as a maven dependency
<dependency>
<groupId>ca.bc.gov.open</groupId>
<artifactId>spring-bceid-starter</artifactId>
<version>0.1.2</version>
<version>0.1.4</version>
</dependency>
</dependencies>
```
Expand Down
4 changes: 2 additions & 2 deletions src/spring-bceid-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ca.bc.gov.open</groupId>
<artifactId>spring-bceid-starter</artifactId>
<version>0.1.3</version>
<version>0.1.4</version>

<properties>
<java.version>1.8</java.version>
Expand Down Expand Up @@ -86,7 +86,7 @@
<dependency>
<groupId>ca.bc.gov.open</groupId>
<artifactId>spring-starters-bom</artifactId>
<version>0.1.3</version>
<version>0.1.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
75 changes: 75 additions & 0 deletions src/spring-clamav-starter/README.md
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
102 changes: 102 additions & 0 deletions src/spring-clamav-starter/pom.xml
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>
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);
}

}
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);
}
}
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;
}
}
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;

}
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();
}
}
Loading

0 comments on commit f5552bc

Please sign in to comment.