38 changed files with 781 additions and 112 deletions
@ -0,0 +1,90 @@
@@ -0,0 +1,90 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.endpoint.jmx; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
||||
import org.springframework.boot.actuate.endpoint.Endpoint; |
||||
import org.springframework.boot.actuate.endpoint.EndpointProperties; |
||||
import org.springframework.context.EnvironmentAware; |
||||
import org.springframework.core.env.Environment; |
||||
import org.springframework.jmx.export.annotation.ManagedResource; |
||||
import org.springframework.util.ObjectUtils; |
||||
|
||||
/** |
||||
* Abstract base class for {@link JmxEndpoint} implementations without a backing |
||||
* {@link Endpoint}. |
||||
* |
||||
* @author Vedran Pavic |
||||
* @author Phillip Webb |
||||
* @since 1.5.0 |
||||
*/ |
||||
@ManagedResource |
||||
public abstract class AbstractJmxEndpoint implements JmxEndpoint, EnvironmentAware { |
||||
|
||||
private final DataConverter dataConverter; |
||||
|
||||
private Environment environment; |
||||
|
||||
/** |
||||
* Enable the endpoint. |
||||
*/ |
||||
private Boolean enabled; |
||||
|
||||
public AbstractJmxEndpoint(ObjectMapper objectMapper) { |
||||
this.dataConverter = new DataConverter(objectMapper); |
||||
} |
||||
|
||||
@Override |
||||
public void setEnvironment(Environment environment) { |
||||
this.environment = environment; |
||||
} |
||||
|
||||
protected final Environment getEnvironment() { |
||||
return this.environment; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isEnabled() { |
||||
return EndpointProperties.isEnabled(this.environment, this.enabled); |
||||
} |
||||
|
||||
public void setEnabled(Boolean enabled) { |
||||
this.enabled = enabled; |
||||
} |
||||
|
||||
@Override |
||||
public String getIdentity() { |
||||
return ObjectUtils.getIdentityHexString(this); |
||||
} |
||||
|
||||
@Override |
||||
@SuppressWarnings("rawtypes") |
||||
public Class<? extends Endpoint> getEndpointType() { |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* Convert the given data into JSON. |
||||
* @param data the source data |
||||
* @return the JSON representation |
||||
*/ |
||||
protected Object convert(Object data) { |
||||
return this.dataConverter.convert(data); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.endpoint.jmx; |
||||
|
||||
import java.text.ParseException; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
||||
import org.springframework.boot.actuate.audit.AuditEvent; |
||||
import org.springframework.boot.actuate.audit.AuditEventRepository; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.jmx.export.annotation.ManagedOperation; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* {@link JmxEndpoint} for {@link AuditEventRepository}. |
||||
* |
||||
* @author Vedran Pavic |
||||
* @since 1.5.0 |
||||
*/ |
||||
@ConfigurationProperties(prefix = "endpoints.auditevents") |
||||
public class AuditEventsJmxEndpoint extends AbstractJmxEndpoint { |
||||
|
||||
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ"; |
||||
|
||||
private final AuditEventRepository auditEventRepository; |
||||
|
||||
public AuditEventsJmxEndpoint(ObjectMapper objectMapper, |
||||
AuditEventRepository auditEventRepository) { |
||||
super(objectMapper); |
||||
Assert.notNull(auditEventRepository, "AuditEventRepository must not be null"); |
||||
this.auditEventRepository = auditEventRepository; |
||||
} |
||||
|
||||
@ManagedOperation(description = "Retrieves a list of audit events meeting the given criteria") |
||||
public Object getData(String dateAfter) { |
||||
List<AuditEvent> auditEvents = this.auditEventRepository |
||||
.find(parseDate(dateAfter)); |
||||
return convert(auditEvents); |
||||
} |
||||
|
||||
@ManagedOperation(description = "Retrieves a list of audit events meeting the given criteria") |
||||
public Object getData(String dateAfter, String principal) { |
||||
List<AuditEvent> auditEvents = this.auditEventRepository.find(principal, |
||||
parseDate(dateAfter)); |
||||
return convert(auditEvents); |
||||
} |
||||
|
||||
@ManagedOperation(description = "Retrieves a list of audit events meeting the given criteria") |
||||
public Object getData(String principal, String dateAfter, String type) { |
||||
List<AuditEvent> auditEvents = this.auditEventRepository.find(principal, |
||||
parseDate(dateAfter), type); |
||||
return convert(auditEvents); |
||||
} |
||||
|
||||
private Date parseDate(String date) { |
||||
try { |
||||
return new SimpleDateFormat(DATE_FORMAT).parse(date); |
||||
} |
||||
catch (ParseException ex) { |
||||
throw new IllegalArgumentException(ex); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.endpoint.jmx; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import com.fasterxml.jackson.databind.JavaType; |
||||
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
||||
/** |
||||
* Internal converter that uses an {@link ObjectMapper} to convert to JSON. |
||||
* |
||||
* @author Christian Dupuis |
||||
* @author Andy Wilkinson |
||||
* @author Phillip Webb |
||||
*/ |
||||
class DataConverter { |
||||
|
||||
private final ObjectMapper objectMapper; |
||||
|
||||
private final JavaType listObject; |
||||
|
||||
private final JavaType mapStringObject; |
||||
|
||||
DataConverter(ObjectMapper objectMapper) { |
||||
this.objectMapper = (objectMapper == null ? new ObjectMapper() : objectMapper); |
||||
this.listObject = this.objectMapper.getTypeFactory() |
||||
.constructParametricType(List.class, Object.class); |
||||
this.mapStringObject = this.objectMapper.getTypeFactory() |
||||
.constructParametricType(Map.class, String.class, Object.class); |
||||
|
||||
} |
||||
|
||||
public Object convert(Object data) { |
||||
if (data == null) { |
||||
return null; |
||||
} |
||||
if (data instanceof String) { |
||||
return data; |
||||
} |
||||
if (data.getClass().isArray() || data instanceof List) { |
||||
return this.objectMapper.convertValue(data, this.listObject); |
||||
} |
||||
return this.objectMapper.convertValue(data, this.mapStringObject); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.endpoint.jmx; |
||||
|
||||
import org.springframework.boot.actuate.endpoint.Endpoint; |
||||
|
||||
/** |
||||
* A strategy for the JMX layer on top of an {@link Endpoint}. Implementations are allowed |
||||
* to use {@code @ManagedAttribute} and the full Spring JMX machinery. Implementations may |
||||
* be backed by an actual {@link Endpoint} or may be specifically designed for JMX only. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 1.5.0 |
||||
* @see EndpointMBean |
||||
* @see AbstractJmxEndpoint |
||||
*/ |
||||
public interface JmxEndpoint { |
||||
|
||||
/** |
||||
* Return if the JMX endpoint is enabled. |
||||
* @return if the endpoint is enabled |
||||
*/ |
||||
boolean isEnabled(); |
||||
|
||||
/** |
||||
* Return the MBean identity for this endpoint. |
||||
* @return the MBean identity. |
||||
*/ |
||||
String getIdentity(); |
||||
|
||||
/** |
||||
* Return the type of {@link Endpoint} exposed, or {@code null} if this |
||||
* {@link JmxEndpoint} exposes information that cannot be represented as a traditional |
||||
* {@link Endpoint}. |
||||
* @return the endpoint type |
||||
*/ |
||||
@SuppressWarnings("rawtypes") |
||||
Class<? extends Endpoint> getEndpointType(); |
||||
|
||||
} |
||||
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.endpoint.mvc; |
||||
|
||||
import java.util.Date; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.springframework.boot.actuate.audit.AuditEvent; |
||||
import org.springframework.boot.actuate.audit.AuditEventRepository; |
||||
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
import org.springframework.format.annotation.DateTimeFormat; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
|
||||
/** |
||||
* {@link MvcEndpoint} to expose {@link AuditEvent}s. |
||||
* |
||||
* @author Vedran Pavic |
||||
* @author Phillip Webb |
||||
* @since 1.5.0 |
||||
*/ |
||||
@ConfigurationProperties(prefix = "endpoints.auditevents") |
||||
public class AuditEventsMvcEndpoint extends AbstractNamedMvcEndpoint { |
||||
|
||||
private final AuditEventRepository auditEventRepository; |
||||
|
||||
public AuditEventsMvcEndpoint(AuditEventRepository auditEventRepository) { |
||||
super("auditevents", "/auditevents", true); |
||||
Assert.notNull(auditEventRepository, "AuditEventRepository must not be null"); |
||||
this.auditEventRepository = auditEventRepository; |
||||
} |
||||
|
||||
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) |
||||
@ResponseBody |
||||
public ResponseEntity<?> findByPrincipalAndAfterAndType( |
||||
@RequestParam(required = false) String principal, |
||||
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ") Date after, |
||||
@RequestParam(required = false) String type) { |
||||
if (!isEnabled()) { |
||||
return DISABLED_RESPONSE; |
||||
} |
||||
Map<Object, Object> result = new LinkedHashMap<Object, Object>(); |
||||
result.put("events", this.auditEventRepository.find(principal, after, type)); |
||||
return ResponseEntity.ok(result); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,130 @@
@@ -0,0 +1,130 @@
|
||||
/* |
||||
* Copyright 2012-2016 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.endpoint.mvc; |
||||
|
||||
import java.time.Instant; |
||||
import java.util.Collections; |
||||
import java.util.Date; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.actuate.audit.AuditEvent; |
||||
import org.springframework.boot.actuate.audit.AuditEventRepository; |
||||
import org.springframework.boot.actuate.audit.InMemoryAuditEventRepository; |
||||
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration; |
||||
import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.test.context.TestPropertySource; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
import org.springframework.test.web.servlet.MockMvc; |
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
||||
import org.springframework.web.context.WebApplicationContext; |
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString; |
||||
import static org.hamcrest.CoreMatchers.not; |
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
||||
|
||||
/** |
||||
* Tests for {@link AuditEventsMvcEndpoint}. |
||||
* |
||||
* @author Vedran Pavic |
||||
*/ |
||||
@SpringBootTest |
||||
@RunWith(SpringRunner.class) |
||||
@TestPropertySource(properties = "management.security.enabled=false") |
||||
public class AuditEventsMvcEndpointTests { |
||||
|
||||
@Autowired |
||||
private WebApplicationContext context; |
||||
|
||||
private MockMvc mvc; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
this.context.getBean(AuditEventsMvcEndpoint.class).setEnabled(true); |
||||
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build(); |
||||
} |
||||
|
||||
@Test |
||||
public void invokeWhenDisabledShouldReturnNotFoundStatus() throws Exception { |
||||
this.context.getBean(AuditEventsMvcEndpoint.class).setEnabled(false); |
||||
this.mvc.perform(get("/auditevents").param("after", "2016-11-01T10:00:00+0000")) |
||||
.andExpect(status().isNotFound()); |
||||
} |
||||
|
||||
@Test |
||||
public void invokeFilterByDateAfter() throws Exception { |
||||
this.mvc.perform(get("/auditevents").param("after", "2016-11-01T13:00:00+0000")) |
||||
.andExpect(status().isOk()) |
||||
.andExpect(content().string("{\"events\":[]}")); |
||||
} |
||||
|
||||
@Test |
||||
public void invokeFilterByPrincipalAndDateAfter() throws Exception { |
||||
this.mvc.perform(get("/auditevents").param("principal", "user").param("after", |
||||
"2016-11-01T10:00:00+0000")) |
||||
.andExpect(status().isOk()) |
||||
.andExpect(content().string( |
||||
containsString("\"principal\":\"user\",\"type\":\"login\""))) |
||||
.andExpect(content().string(not(containsString("admin")))); |
||||
} |
||||
|
||||
@Test |
||||
public void invokeFilterByPrincipalAndDateAfterAndType() throws Exception { |
||||
this.mvc.perform(get("/auditevents").param("principal", "admin") |
||||
.param("after", "2016-11-01T10:00:00+0000").param("type", "logout")) |
||||
.andExpect(status().isOk()) |
||||
.andExpect(content().string( |
||||
containsString("\"principal\":\"admin\",\"type\":\"logout\""))) |
||||
.andExpect(content().string(not(containsString("login")))); |
||||
} |
||||
|
||||
@Import({ JacksonAutoConfiguration.class, |
||||
HttpMessageConvertersAutoConfiguration.class, |
||||
EndpointWebMvcAutoConfiguration.class, WebMvcAutoConfiguration.class, |
||||
ManagementServerPropertiesAutoConfiguration.class }) |
||||
@Configuration |
||||
protected static class TestConfiguration { |
||||
|
||||
@Bean |
||||
public AuditEventRepository auditEventsRepository() { |
||||
AuditEventRepository repository = new InMemoryAuditEventRepository(3); |
||||
repository.add(createEvent("2016-11-01T11:00:00Z", "admin", "login")); |
||||
repository.add(createEvent("2016-11-01T12:00:00Z", "admin", "logout")); |
||||
repository.add(createEvent("2016-11-01T12:00:00Z", "user", "login")); |
||||
return repository; |
||||
} |
||||
|
||||
private AuditEvent createEvent(String instant, String principal, String type) { |
||||
return new AuditEvent(Date.from(Instant.parse(instant)), principal, type, |
||||
Collections.<String, Object>emptyMap()); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue