Skip to content

Commit f5552bc

Browse files
author
Adam Kroon
authored
Merge pull request #15 from bcgov/claim-av-starter
Adding ClamAv Lib
2 parents 7e777e4 + df713b8 commit f5552bc

File tree

16 files changed

+455
-12
lines changed

16 files changed

+455
-12
lines changed

src/pom.xml

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
<groupId>ca.bc.gov.open</groupId>
77
<artifactId>spring-starters</artifactId>
8-
<version>0.1.3</version>
9-
<modules>
10-
11-
</modules>
8+
<version>0.1.4</version>
129

1310
<name>spring-starters</name>
1411
<packaging>pom</packaging>
@@ -25,25 +22,34 @@
2522
<module>spring-sftp-starter</module>
2623
<module>spring-bceid-starter</module>
2724
<module>spring-starters-bom</module>
25+
<module>spring-clamav-starter</module>
2826
</modules>
2927
</profile>
3028

3129
<profile>
32-
<id>spring-sftp-starter</id>
30+
<id>sftp</id>
3331
<modules>
3432
<module>spring-starters-bom</module>
3533
<module>spring-sftp-starter</module>
3634
</modules>
3735
</profile>
3836

3937
<profile>
40-
<id>spring-bceid-starter</id>
38+
<id>bceid</id>
4139
<modules>
4240
<module>spring-starters-bom</module>
4341
<module>spring-bceid-starter</module>
4442
</modules>
4543
</profile>
4644

45+
<profile>
46+
<id>clamav</id>
47+
<modules>
48+
<module>spring-starters-bom</module>
49+
<module>spring-clamav-starter</module>
50+
</modules>
51+
</profile>
52+
4753
</profiles>
4854

4955
</project>

src/spring-bceid-starter/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Add spring-bceid-starter as a maven dependency
1111
<dependency>
1212
<groupId>ca.bc.gov.open</groupId>
1313
<artifactId>spring-bceid-starter</artifactId>
14-
<version>0.1.2</version>
14+
<version>0.1.4</version>
1515
</dependency>
1616
</dependencies>
1717
```

src/spring-bceid-starter/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>ca.bc.gov.open</groupId>
88
<artifactId>spring-bceid-starter</artifactId>
9-
<version>0.1.3</version>
9+
<version>0.1.4</version>
1010

1111
<properties>
1212
<java.version>1.8</java.version>
@@ -86,7 +86,7 @@
8686
<dependency>
8787
<groupId>ca.bc.gov.open</groupId>
8888
<artifactId>spring-starters-bom</artifactId>
89-
<version>0.1.3</version>
89+
<version>0.1.4</version>
9090
<type>pom</type>
9191
<scope>import</scope>
9292
</dependency>

src/spring-clamav-starter/README.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# spring-clamav-starter
2+
3+
A stater to facilitate usage of ClamAv
4+
5+
## Usage
6+
7+
Add spring-bceid-starter as a maven dependency
8+
9+
```xml
10+
<dependencies>
11+
<dependency>
12+
<groupId>ca.bc.gov.open</groupId>
13+
<artifactId>spring-clamav-starter</artifactId>
14+
<version>0.1.4</version>
15+
</dependency>
16+
</dependencies>
17+
```
18+
19+
## How to use the service
20+
21+
The following code should be used to implement this component:
22+
23+
```java
24+
25+
@autowired
26+
private ClamAvService clamAvService
27+
28+
@Service
29+
public class FileScan {
30+
31+
public void Scan(InputStream inputStream) {
32+
33+
try {
34+
clamAvService.scan(inputStream);
35+
} catch (VirusDetectedException e) {
36+
// file is infected
37+
}
38+
39+
}
40+
}
41+
42+
```
43+
44+
45+
## Configuration
46+
47+
| name | definition | required |
48+
| --- | --- | --- |
49+
| [bcgov.clamav.service.host](#bcgovclamavservicehost) | String | No |
50+
| [bcgov.clamav.service.port](#bcgovclamavserviceport) | String | No |
51+
| [bcgov.clamav.service.timeout](#bcgovclamavservicetimeout) | String | No |
52+
53+
#### cgov.clamav.service.host
54+
55+
* Value type is String
56+
57+
Default value is **localhost**
58+
59+
Sets the port of the host client
60+
61+
#### bcgov.clamav.service.port
62+
63+
* Value type is Integer
64+
65+
Default value is **3310**
66+
67+
Sets the port of the clamAv client
68+
69+
#### bcgov.clamav.service.timeout
70+
71+
* Value type is Integer
72+
73+
Default value is **500**
74+
75+
Sets the timeout of the clamAv client

src/spring-clamav-starter/pom.xml

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ca.bc.gov.open</groupId>
8+
<artifactId>spring-clamav-starter</artifactId>
9+
<version>0.1.4</version>
10+
11+
<properties>
12+
<java.version>1.8</java.version>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
<spring-boot.version>2.2.4.RELEASE</spring-boot.version>
16+
<org.apache.maven.plugins.version.version>2.22.0</org.apache.maven.plugins.version.version>
17+
</properties>
18+
19+
<dependencies>
20+
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter</artifactId>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>fi.solita.clamav</groupId>
28+
<artifactId>clamav-client</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
<exclusions>
36+
<exclusion>
37+
<groupId>org.junit.vintage</groupId>
38+
<artifactId>junit-vintage-engine</artifactId>
39+
</exclusion>
40+
</exclusions>
41+
</dependency>
42+
43+
</dependencies>
44+
45+
<dependencyManagement>
46+
<dependencies>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-dependencies</artifactId>
50+
<version>${spring-boot.version}</version>
51+
<type>pom</type>
52+
<scope>import</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>ca.bc.gov.open</groupId>
56+
<artifactId>spring-starters-bom</artifactId>
57+
<version>0.1.4</version>
58+
<type>pom</type>
59+
<scope>import</scope>
60+
</dependency>
61+
</dependencies>
62+
</dependencyManagement>
63+
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-surefire-plugin</artifactId>
70+
<version>${org.apache.maven.plugins.version.version}</version>
71+
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-failsafe-plugin</artifactId>
75+
<version>${org.apache.maven.plugins.version.version}</version>
76+
</plugin>
77+
<!--jacoco code coverage-->
78+
<plugin>
79+
<groupId>org.jacoco</groupId>
80+
<artifactId>jacoco-maven-plugin</artifactId>
81+
<version>0.8.2</version>
82+
<executions>
83+
<execution>
84+
<goals>
85+
<goal>prepare-agent</goal>
86+
</goals>
87+
</execution>
88+
<!-- attached to Maven test phase -->
89+
<execution>
90+
<id>report</id>
91+
<phase>test</phase>
92+
<goals>
93+
<goal>report</goal>
94+
</goals>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
101+
102+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ca.bc.gov.open.clamav.starter;
2+
3+
import fi.solita.clamav.ClamAVClient;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
7+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
8+
import org.springframework.context.annotation.Bean;
9+
10+
@EnableConfigurationProperties(ClamAvProperties.class)
11+
public class AutoConfiguration {
12+
13+
private final ClamAvProperties clamAvProperties;
14+
15+
Logger logger = LoggerFactory.getLogger(AutoConfiguration.class);
16+
17+
public AutoConfiguration(ClamAvProperties clamAvProperties) {
18+
this.clamAvProperties = clamAvProperties;
19+
}
20+
21+
@Bean
22+
@ConditionalOnMissingBean(ClamAVClient.class)
23+
public ClamAVClient clamAVClient() {
24+
25+
logger.debug("Configuring ClamAv Client");
26+
logger.debug("ClamAv host: [{}]", clamAvProperties.getHost());
27+
logger.debug("ClamAv port: [{}]", clamAvProperties.getPort());
28+
logger.debug("ClamAv timeout: [{}]", clamAvProperties.getTimeout());
29+
30+
return new ClamAVClient(clamAvProperties.getHost(), clamAvProperties.getPort());
31+
32+
}
33+
34+
@Bean
35+
@ConditionalOnMissingBean(ClamAvService.class)
36+
public ClamAvService clamAvService(ClamAVClient clamAVClient) {
37+
return new ClamAvServiceImpl(clamAVClient);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ca.bc.gov.open.clamav.starter;
2+
3+
public class ClamAvException extends RuntimeException {
4+
5+
public ClamAvException(String message, Throwable cause) {
6+
super(message, cause);
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ca.bc.gov.open.clamav.starter;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
5+
@ConfigurationProperties(prefix = "bcgov.clamav")
6+
public class ClamAvProperties {
7+
8+
private String host = "localhost";
9+
private int port = 3310;
10+
private int timeout = 500;
11+
12+
public String getHost() {
13+
return host;
14+
}
15+
16+
public void setHost(String host) {
17+
this.host = host;
18+
}
19+
20+
public int getPort() {
21+
return port;
22+
}
23+
24+
public void setPort(int port) {
25+
this.port = port;
26+
}
27+
28+
public int getTimeout() {
29+
return timeout;
30+
}
31+
32+
public void setTimeout(int timeout) {
33+
this.timeout = timeout;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package ca.bc.gov.open.clamav.starter;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
public interface ClamAvService {
7+
8+
void scan(InputStream inputStream) throws VirusDetectedException;
9+
10+
boolean ping() throws IOException;
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ca.bc.gov.open.clamav.starter;
2+
3+
import fi.solita.clamav.ClamAVClient;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
10+
public class ClamAvServiceImpl implements ClamAvService {
11+
12+
private Logger logger = LoggerFactory.getLogger(AutoConfiguration.class);
13+
14+
private final ClamAVClient clamAVClient;
15+
16+
public ClamAvServiceImpl(ClamAVClient clamAVClient) {
17+
this.clamAVClient = clamAVClient;
18+
}
19+
20+
@Override
21+
public void scan(InputStream inputStream) throws VirusDetectedException {
22+
23+
byte[] reply;
24+
try {
25+
reply = clamAVClient.scan(inputStream);
26+
} catch (IOException e) {
27+
logger.error("ClamAv Service could not scan the input");
28+
throw new ClamAvException("Could not scan the input", e);
29+
}
30+
if (!ClamAVClient.isCleanReply(reply)) {
31+
logger.error("Virus Detected using ClamAv server");
32+
throw new VirusDetectedException("ClamAv has detected a virus in the input");
33+
}
34+
35+
}
36+
37+
@Override
38+
public boolean ping() throws IOException {
39+
return clamAVClient.ping();
40+
}
41+
}

0 commit comments

Comments
 (0)