Skip to content

Commit a4c5be2

Browse files
committed
WEB-68 Service to return WebGoat Version and Build Number
1 parent fbc62a4 commit a4c5be2

File tree

7 files changed

+414
-240
lines changed

7 files changed

+414
-240
lines changed

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<name>WebGoat</name>
34
<modelVersion>4.0.0</modelVersion>
45
<groupId>WebGoat</groupId>
56
<artifactId>WebGoat</artifactId>
@@ -19,6 +20,8 @@
1920
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
2021
<spring.security.version>3.2.4.RELEASE</spring.security.version>
2122
<tiles.version>2.2.2</tiles.version>
23+
<!-- If run from Bamboo this will be replaced with the bamboo build number -->
24+
<build.number>local</build.number>
2225
</properties>
2326

2427
<build>
@@ -40,6 +43,22 @@
4043
<encoding>ISO-8859-1</encoding>
4144
</configuration>
4245
</plugin>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-war-plugin</artifactId>
49+
<configuration>
50+
<manifest>
51+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
52+
</manifest>
53+
<archive>
54+
<manifestEntries>
55+
<Specification-Title>${project.name}</Specification-Title>
56+
<Specification-Version>${project.version}</Specification-Version>
57+
<Implementation-Version>${build.number}</Implementation-Version>
58+
</manifestEntries>
59+
</archive>
60+
</configuration>
61+
</plugin>
4362
<plugin>
4463
<groupId>org.apache.tomcat.maven</groupId>
4564
<artifactId>tomcat7-maven-plugin</artifactId>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package org.owasp.webgoat.application;
7+
8+
import org.apache.commons.lang3.StringUtils;
9+
import org.apache.commons.lang3.builder.ToStringBuilder;
10+
11+
/**
12+
* Singleton which is created on context startup
13+
*
14+
* @author rlawson
15+
*/
16+
public class Application {
17+
18+
private static final Application INSTANCE = new Application();
19+
20+
private Application() {
21+
22+
}
23+
24+
public static final Application getInstance() {
25+
return INSTANCE;
26+
}
27+
28+
private String version = "local";
29+
private String build = "local";
30+
private String name = "WebGoat";
31+
32+
/**
33+
* @return the version
34+
*/
35+
public String getVersion() {
36+
return version;
37+
}
38+
39+
/**
40+
* @param version the version to set
41+
*/
42+
public void setVersion(String version) {
43+
if (StringUtils.isNotBlank(version)) {
44+
this.version = version;
45+
}
46+
}
47+
48+
/**
49+
* @return the build
50+
*/
51+
public String getBuild() {
52+
return build;
53+
}
54+
55+
/**
56+
* @param build the build to set
57+
*/
58+
public void setBuild(String build) {
59+
if (StringUtils.isNotBlank(build)) {
60+
this.build = build;
61+
}
62+
}
63+
64+
/**
65+
* @return the name
66+
*/
67+
public String getName() {
68+
return name;
69+
}
70+
71+
/**
72+
* @param name the name to set
73+
*/
74+
public void setName(String name) {
75+
if (StringUtils.isNotBlank(name)) {
76+
this.name = name;
77+
}
78+
}
79+
80+
@Override
81+
public String toString() {
82+
return new ToStringBuilder(this).
83+
append("name", name).
84+
append("version", version).
85+
append("build", build).
86+
toString();
87+
}
88+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package org.owasp.webgoat.application;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.jar.Attributes;
11+
import java.util.jar.Manifest;
12+
import javax.servlet.ServletContext;
13+
import javax.servlet.ServletContextEvent;
14+
import javax.servlet.ServletContextListener;
15+
16+
/**
17+
* Web application lifecycle listener.
18+
*
19+
* @author rlawson
20+
*/
21+
public class WebGoatServletListener implements ServletContextListener {
22+
23+
@Override
24+
public void contextInitialized(ServletContextEvent sce) {
25+
ServletContext context = sce.getServletContext();
26+
context.log("WebGoat is starting");
27+
setApplicationVariables(context);
28+
}
29+
30+
@Override
31+
public void contextDestroyed(ServletContextEvent sce) {
32+
ServletContext context = sce.getServletContext();
33+
context.log("WebGoat is stopping");
34+
}
35+
36+
private void setApplicationVariables(ServletContext context) {
37+
Application app = Application.getInstance();
38+
try {
39+
InputStream inputStream = context.getResourceAsStream("/META-INF/MANIFEST.MF");
40+
Manifest manifest = new Manifest(inputStream);
41+
Attributes attr = manifest.getMainAttributes();
42+
String name = attr.getValue("Specification-Title");
43+
String version = attr.getValue("Specification-Version");
44+
String build = attr.getValue("Implementation-Version");
45+
app.setName(name);
46+
app.setVersion(version);
47+
app.setBuild(build);
48+
} catch (IOException ioe) {
49+
context.log("Error setting application variables", ioe);
50+
}
51+
}
52+
}

src/main/java/org/owasp/webgoat/controller/Start.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import javax.servlet.http.HttpServletRequest;
1111
import javax.servlet.http.HttpSession;
1212
import org.apache.commons.lang3.StringUtils;
13+
import org.owasp.webgoat.application.Application;
1314
import org.owasp.webgoat.session.WebSession;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
@@ -55,6 +56,10 @@ public ModelAndView start(HttpServletRequest request,
5556

5657
String contactEmail = servletContext.getInitParameter("email");
5758
model.addObject("contactEmail", contactEmail);
59+
Application app = Application.getInstance();
60+
logger.info("Setting application properties: " + app);
61+
model.addObject("version", app.getVersion());
62+
model.addObject("build", app.getBuild());
5863

5964
// if everything ok then go to webgoat UI
6065
model.setViewName("main_new");
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* *************************************************************************************************
3+
*
4+
*
5+
* This file is part of WebGoat, an Open Web Application Security Project
6+
* utility. For details, please see http://www.owasp.org/
7+
*
8+
* Copyright (c) 2002 - 20014 Bruce Mayhew
9+
*
10+
* This program is free software; you can redistribute it and/or modify it under
11+
* the terms of the GNU General Public License as published by the Free Software
12+
* Foundation; either version 2 of the License, or (at your option) any later
13+
* version.
14+
*
15+
* This program is distributed in the hope that it will be useful, but WITHOUT
16+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18+
* details.
19+
*
20+
* You should have received a copy of the GNU General Public License along with
21+
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22+
* Place - Suite 330, Boston, MA 02111-1307, USA.
23+
*
24+
* Getting Source ==============
25+
*
26+
* Source for this application is maintained at
27+
* https://github.com/WebGoat/WebGoat, a repository for free software projects.
28+
*
29+
* For details, please see http://webgoat.github.io
30+
*/
31+
package org.owasp.webgoat.service;
32+
33+
import javax.servlet.http.HttpSession;
34+
import org.owasp.webgoat.application.Application;
35+
import org.springframework.stereotype.Controller;
36+
import org.springframework.web.bind.annotation.RequestMapping;
37+
import org.springframework.web.bind.annotation.ResponseBody;
38+
39+
/**
40+
*
41+
* @author rlawson
42+
*/
43+
@Controller
44+
public class ApplicationService extends BaseService {
45+
46+
/**
47+
* Returns global application info
48+
*
49+
* @param session
50+
* @return
51+
*/
52+
@RequestMapping(value = "/application.mvc", produces = "application/json")
53+
public @ResponseBody
54+
Application showApplication(HttpSession session) {
55+
Application app = Application.getInstance();
56+
return app;
57+
}
58+
59+
}

0 commit comments

Comments
 (0)