pull/12149/head
Phillip Webb 7 years ago
parent cdb5677375
commit 05faac2b09

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -54,19 +54,16 @@ public class DataSourcePoolMetricsAutoConfigurationTests {
@Test
public void autoConfiguredDataSourceIsInstrumented() {
this.contextRunner
.run((context) -> {
context.getBean(DataSource.class).getConnection().getMetaData();
MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("jdbc.connections.max").tags("name", "dataSource")
.meter();
});
this.contextRunner.run((context) -> {
context.getBean(DataSource.class).getConnection().getMetaData();
MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("jdbc.connections.max").tags("name", "dataSource").meter();
});
}
@Test
public void dataSourceInstrumentationCanBeDisabled() {
this.contextRunner
.withPropertyValues("management.metrics.enable.jdbc=false")
this.contextRunner.withPropertyValues("management.metrics.enable.jdbc=false")
.run((context) -> {
context.getBean(DataSource.class).getConnection().getMetaData();
MeterRegistry registry = context.getBean(MeterRegistry.class);
@ -92,23 +89,20 @@ public class DataSourcePoolMetricsAutoConfigurationTests {
@Test
public void autoConfiguredHikariDataSourceIsInstrumented() {
this.contextRunner
.run((context) -> {
context.getBean(DataSource.class).getConnection();
MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("hikaricp.connections").meter();
});
this.contextRunner.run((context) -> {
context.getBean(DataSource.class).getConnection();
MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("hikaricp.connections").meter();
});
}
@Test
public void hikariDataSourceInstrumentationCanBeDisabled() {
this.contextRunner
.withPropertyValues("management.metrics.enable.hikaricp=false")
this.contextRunner.withPropertyValues("management.metrics.enable.hikaricp=false")
.run((context) -> {
context.getBean(DataSource.class).getConnection();
MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.find("hikaricp.connections").meter())
.isNull();
assertThat(registry.find("hikaricp.connections").meter()).isNull();
});
}
@ -135,7 +129,7 @@ public class DataSourcePoolMetricsAutoConfigurationTests {
MeterRegistry registry = context.getBean(MeterRegistry.class);
assertThat(registry.get("hikaricp.connections").meter().getId()
.getTags())
.containsExactly(Tag.of("pool", "firstDataSource"));
.containsExactly(Tag.of("pool", "firstDataSource"));
});
}
@ -215,8 +209,7 @@ public class DataSourcePoolMetricsAutoConfigurationTests {
private org.apache.tomcat.jdbc.pool.DataSource createTomcatDataSource() {
String url = "jdbc:hsqldb:mem:test-" + UUID.randomUUID();
return DataSourceBuilder
.create().url(url)
return DataSourceBuilder.create().url(url)
.type(org.apache.tomcat.jdbc.pool.DataSource.class).build();
}

@ -26,6 +26,20 @@ import java.security.Principal;
*/
public interface SecurityContext {
public static SecurityContext NONE = new SecurityContext() {
@Override
public Principal getPrincipal() {
return null;
}
@Override
public boolean isUserInRole(String role) {
return false;
}
};
/**
* Return the currently authenticated {@link Principal} or {@code null}.
* @return the principal or {@code null}

@ -16,7 +16,6 @@
package org.springframework.boot.actuate.endpoint.jmx;
import java.security.Principal;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -100,7 +99,7 @@ public class EndpointMBean implements DynamicMBean {
.map(JmxOperationParameter::getName).toArray(String[]::new);
Map<String, Object> arguments = getArguments(parameterNames, params);
Object result = operation
.invoke(new InvocationContext(new JmxSecurityContext(), arguments));
.invoke(new InvocationContext(SecurityContext.NONE, arguments));
if (REACTOR_PRESENT) {
result = ReactiveHandler.handle(result);
}
@ -152,18 +151,4 @@ public class EndpointMBean implements DynamicMBean {
}
private static final class JmxSecurityContext implements SecurityContext {
@Override
public Principal getPrincipal() {
return null;
}
@Override
public boolean isUserInRole(String role) {
return false;
}
}
}

@ -271,14 +271,16 @@ public abstract class AbstractWebFluxEndpointHandlerMapping
private ReactiveWebOperationAdapter(OperationInvoker invoker) {
this.invoker = invoker;
this.securityContextSupplier = getSecurityContextSupplier();
}
private Supplier<Mono<? extends SecurityContext>> getSecurityContextSupplier() {
if (ClassUtils.isPresent(
"org.springframework.security.core.context.ReactiveSecurityContextHolder",
getClass().getClassLoader())) {
this.securityContextSupplier = this::springSecurityContext;
}
else {
this.securityContextSupplier = this::emptySecurityContext;
return this::springSecurityContext;
}
return this::emptySecurityContext;
}
public Mono<? extends SecurityContext> springSecurityContext() {
@ -289,19 +291,7 @@ public abstract class AbstractWebFluxEndpointHandlerMapping
}
public Mono<SecurityContext> emptySecurityContext() {
return Mono.just(new SecurityContext() {
@Override
public Principal getPrincipal() {
return null;
}
@Override
public boolean isUserInRole(String role) {
return false;
}
});
return Mono.just(SecurityContext.NONE);
}
@Override

@ -84,8 +84,8 @@ public class DataSourcePoolMetrics implements MeterBinder {
private <N extends Number> void bindDataSource(MeterRegistry registry,
String metricName, Function<DataSource, N> function) {
if (function.apply(this.dataSource) != null) {
registry.gauge("jdbc.connections." + metricName, this.tags,
this.dataSource, (m) -> function.apply(m).doubleValue());
registry.gauge("jdbc.connections." + metricName, this.tags, this.dataSource,
(m) -> function.apply(m).doubleValue());
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -79,10 +79,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@ -989,6 +989,7 @@ public class ConfigurationPropertiesTests {
}
return null;
}
}
@Configuration

Loading…
Cancel
Save