Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7bbbe08
ref: fix test linting (#491)
marandaneto Jul 23, 2020
95cc5f4
fix: event.level fatal is not a crashed session (#492)
marandaneto Jul 23, 2020
50a2165
feat: drop store endpoint in favor of envelope (#490)
marandaneto Jul 24, 2020
2f3b563
fix: ktlint for kts files (#493)
marandaneto Jul 24, 2020
29fbcc7
sentry-java readme
bruno-garcia Jul 27, 2020
14a5e29
feat: rotate cache folder (#494)
marandaneto Jul 28, 2020
db5345b
Merge remote-tracking branch 'origin' into feat/sentry-java
maciejwalkowiak Jul 29, 2020
ad2e0d8
make EventProcessor.process Nullable (#507)
davemt Aug 5, 2020
707015f
Merge remote-tracking branch 'origin/master' into feat/sentry-java
maciejwalkowiak Aug 11, 2020
acfb973
Merge remote-tracking branch 'origin/master' into feat/sentry-java
maciejwalkowiak Aug 12, 2020
9607fea
fix merge conflict
marandaneto Aug 13, 2020
d18da36
Add Logback integration. (#511)
maciejwalkowiak Aug 20, 2020
d14445a
Merge branch 'master' into 3.0.0
marandaneto Aug 26, 2020
68c7ceb
Sentry Spring Boot Starter. (#517)
maciejwalkowiak Aug 27, 2020
2d6e215
Refactor binding options (#530)
maciejwalkowiak Aug 29, 2020
0a8bde6
Add `sendDefaultPii` flag to `SentryOptions`. (#531)
maciejwalkowiak Aug 30, 2020
82e4efe
Attach breadcrumbs to events triggered in Logback integration. (#532)
maciejwalkowiak Aug 31, 2020
944f478
Bump to 3.0.0-alpha.1 (#535)
marandaneto Sep 1, 2020
eefdd31
feat: enable release health by default (#534)
marandaneto Sep 1, 2020
df08324
Merge branch 'master' into 3.0.0
marandaneto Sep 2, 2020
35750dd
Add Log4j2 integration. (#537)
maciejwalkowiak Sep 2, 2020
3737aab
feat: session updates along with the event (#514)
marandaneto Sep 3, 2020
cd6f526
Merge remote-tracking branch 'origin/master' into feat/sentry-java
maciejwalkowiak Sep 3, 2020
3915ca4
fix: enable session tracking by default on sample (#538)
marandaneto Sep 3, 2020
9aad2cb
chore 3.0.0-alpha.1
marandaneto Sep 3, 2020
e767af6
prepare: 3.0.0-alpha.2
marandaneto Sep 3, 2020
b754303
Bump Gradle to 6.6.1
marandaneto Sep 3, 2020
60af521
Extract minimum required coverage by Jacoco plugin. (#541)
maciejwalkowiak Sep 4, 2020
799e581
Merge branch 'master' into 3.0.0
marandaneto Sep 4, 2020
923f9fc
Add Spring Integration. (#539)
maciejwalkowiak Sep 8, 2020
7845eb8
Merge branch '3.0.0' into feat/sentry-java
bruno-garcia Sep 8, 2020
89b7606
fix conflicts
marandaneto Sep 9, 2020
ed1b133
Merge branch 'master' into 3.0.0
marandaneto Sep 9, 2020
33766f4
ref: move init flag (#542)
marandaneto Sep 9, 2020
c891d6a
fix: Asserting events on Envelope sent to Transport (#548)
maciejwalkowiak Sep 9, 2020
ae0219d
Add option to set user information via `SentryUserProvider` (#549)
maciejwalkowiak Sep 9, 2020
cdb4530
Merge branch '3.0.0' into feat/sentry-java
bruno-garcia Sep 9, 2020
6d02f98
upgrade cache id
marandaneto Sep 10, 2020
dd0e7d8
Feat/sentry java fix kotlin (#552)
maciejwalkowiak Sep 10, 2020
188ef14
ref: sentry-core to sentry (#553)
bruno-garcia Sep 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add option to set user information via SentryUserProvider (#549)
  • Loading branch information
maciejwalkowiak authored Sep 9, 2020
commit ae0219da369bb605c264b79c74f6741275ad083a
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package io.sentry.samples.spring.boot;

import io.sentry.core.IHub;
import io.sentry.core.SentryOptions;
import io.sentry.spring.SentrySecurityFilter;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -14,32 +11,15 @@
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

private final @NotNull IHub hub;
private final @NotNull SentryOptions options;

public SecurityConfiguration(final @NotNull IHub hub, final @NotNull SentryOptions options) {
this.hub = hub;
this.options = options;
}

// this API is meant to be consumed by non-browser clients thus the CSRF protection is not needed.
@Override
@SuppressWarnings("lgtm[java/spring-disabled-csrf-protection]")
protected void configure(final @NotNull HttpSecurity http) throws Exception {
// register SentrySecurityFilter to attach user information to SentryEvents
http.addFilterAfter(new SentrySecurityFilter(hub, options), AnonymousAuthenticationFilter.class)
.csrf()
.disable()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package io.sentry.samples.spring;

import io.sentry.core.IHub;
import io.sentry.core.SentryOptions;
import io.sentry.spring.SentrySecurityFilter;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -14,32 +11,15 @@
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

private final @NotNull IHub hub;
private final @NotNull SentryOptions options;

public SecurityConfiguration(final @NotNull IHub hub, final @NotNull SentryOptions options) {
this.hub = hub;
this.options = options;
}

// this API is meant to be consumed by non-browser clients thus the CSRF protection is not needed.
@Override
@SuppressWarnings("lgtm[java/spring-disabled-csrf-protection]")
protected void configure(final @NotNull HttpSecurity http) throws Exception {
// register SentrySecurityFilter to attach user information to SentryEvents
http.addFilterAfter(new SentrySecurityFilter(hub, options), AnonymousAuthenticationFilter.class)
.csrf()
.disable()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import io.sentry.core.protocol.SdkVersion;
import io.sentry.core.transport.ITransport;
import io.sentry.core.transport.ITransportGate;
import io.sentry.spring.SentryUserProvider;
import io.sentry.spring.SentryUserProviderEventProcessor;
import io.sentry.spring.SentryWebConfiguration;
import java.util.List;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -43,12 +45,17 @@ static class HubConfiguration {
final @NotNull List<EventProcessor> eventProcessors,
final @NotNull List<Integration> integrations,
final @NotNull ObjectProvider<ITransportGate> transportGate,
final @NotNull ObjectProvider<SentryUserProvider> sentryUserProviders,
final @NotNull ObjectProvider<ITransport> transport) {
return options -> {
beforeSendCallback.ifAvailable(options::setBeforeSend);
beforeBreadcrumbCallback.ifAvailable(options::setBeforeBreadcrumb);
eventProcessors.forEach(options::addEventProcessor);
integrations.forEach(options::addIntegration);
sentryUserProviders.forEach(
sentryUserProvider ->
options.addEventProcessor(
new SentryUserProviderEventProcessor(sentryUserProvider)));
transportGate.ifAvailable(options::setTransportGate);
transport.ifAvailable(options::setTransport);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package io.sentry.spring.boot

import com.nhaarman.mockitokotlin2.verify
import io.sentry.core.IHub
import io.sentry.core.Sentry
import io.sentry.core.SentryOptions
import io.sentry.core.transport.ITransport
import io.sentry.spring.SentrySecurityFilter
import io.sentry.test.checkEvent
import java.lang.RuntimeException
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -30,7 +27,6 @@ import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.crypto.factory.PasswordEncoderFactories
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.provisioning.InMemoryUserDetailsManager
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
Expand Down Expand Up @@ -120,15 +116,10 @@ class HelloController {
}

@Configuration
open class SecurityConfiguration(
private val hub: IHub,
private val options: SentryOptions
) : WebSecurityConfigurerAdapter() {
open class SecurityConfiguration : WebSecurityConfigurerAdapter() {

override fun configure(http: HttpSecurity) {
http
.addFilterAfter(SentrySecurityFilter(hub, options), AnonymousAuthenticationFilter::class.java)
.csrf().disable()
http.csrf().disable()
.authorizeRequests().anyRequest().authenticated()
.and()
.httpBasic()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.sentry.spring;

import io.sentry.core.SentryOptions;
import io.sentry.core.protocol.User;
import io.sentry.core.util.Objects;
import javax.servlet.http.HttpServletRequest;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
* Resolves user information from {@link HttpServletRequest} obtained via {@link
* RequestContextHolder}.
*/
public final class HttpServletRequestSentryUserProvider implements SentryUserProvider {
private final @NotNull SentryOptions options;

public HttpServletRequestSentryUserProvider(final @NotNull SentryOptions options) {
this.options = Objects.requireNonNull(options, "options are required");
}

@Override
public @Nullable User provideUser() {
if (options.isSendDefaultPii()) {
final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof ServletRequestAttributes) {
final ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes) requestAttributes;
final HttpServletRequest request = servletRequestAttributes.getRequest();

final User user = new User();
user.setIpAddress(toIpAddress(request));
if (request.getUserPrincipal() != null) {
user.setUsername(request.getUserPrincipal().getName());
}
return user;
}
}
return null;
}

// it is advised to not use `String#split` method but since we do not have 3rd party libraries
// this is our only option.
@SuppressWarnings("StringSplitter")
private static @NotNull String toIpAddress(final @NotNull HttpServletRequest request) {
final String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress != null) {
return ipAddress.contains(",") ? ipAddress.split(",")[0].trim() : ipAddress;
} else {
return request.getRemoteAddr();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,39 @@
import io.sentry.core.Sentry;
import io.sentry.core.SentryOptions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/** Initializes Sentry after all beans are registered. */
@Open
public class SentryInitBeanPostProcessor implements BeanPostProcessor {
public class SentryInitBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
private @Nullable ApplicationContext applicationContext;

@Override
public Object postProcessAfterInitialization(
final @NotNull Object bean, @NotNull final String beanName) throws BeansException {
if (bean instanceof SentryOptions) {
Sentry.init((SentryOptions) bean);
final SentryOptions options = (SentryOptions) bean;

if (applicationContext != null) {
applicationContext
.getBeanProvider(SentryUserProvider.class)
.forEach(
sentryUserProvider ->
options.addEventProcessor(
new SentryUserProviderEventProcessor(sentryUserProvider)));
}
Sentry.init(options);
}
return bean;
}

@Override
public void setApplicationContext(final @NotNull ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.sentry.spring;

import io.sentry.core.protocol.User;
import org.jetbrains.annotations.Nullable;

/**
* Provides user information that's set on {@link io.sentry.core.SentryEvent} using {@link
* SentryUserProviderEventProcessor}.
*
* <p>Out of the box Spring integration configures single {@link SentryUserProvider} - {@link
* HttpServletRequestSentryUserProvider}.
*/
@FunctionalInterface
public interface SentryUserProvider {
@Nullable
User provideUser();
}
Loading