Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(oauth): Fix issue where externally-terminated SSL deployments had… #390

Merged
merged 1 commit into from
May 9, 2017
Merged
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,28 @@ import com.netflix.spinnaker.gate.security.SpinnakerAuthConfig
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
import org.springframework.boot.autoconfigure.web.ServerProperties
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.cloud.security.oauth2.sso.EnableOAuth2Sso
import org.springframework.cloud.security.oauth2.sso.OAuth2SsoConfigurer
import org.springframework.cloud.security.oauth2.sso.OAuth2SsoConfigurerAdapter
import org.springframework.cloud.security.oauth2.sso.OAuth2SsoProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.context.annotation.Primary
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity
import org.springframework.security.core.AuthenticationException
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter
import org.springframework.stereotype.Component

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
* Each time a class extends WebSecurityConfigurerAdapter, a new set of matchers + filters gets added
* to the FilterChainProxy. This is not great when we want to protect the same URLs with different kinds of
Expand Down Expand Up @@ -97,6 +105,15 @@ class OAuth2SsoConfig {
@Autowired
ExternalAuthTokenFilter externalAuthTokenFilter

@Autowired
private OAuth2SsoProperties sso

@Autowired
private ServerProperties serverProperties

@Autowired
private AuthorizationCodeResourceDetails details

@Override
void match(OAuth2SsoConfigurer.RequestMatchers matchers) {
matchers.antMatchers('/**')
Expand All @@ -105,7 +122,30 @@ class OAuth2SsoConfig {
@Override
void configure(HttpSecurity http) throws Exception {
authConfig.configure(http)

// There isn't a good, built in way to get the redirect to /login be over https when the
// server itself doesn't terminate the SSL connection, so we have to override it.
if (!serverProperties.ssl?.enabled && details.preEstablishedRedirectUri?.startsWith("https")) {
http.exceptionHandling().authenticationEntryPoint(new ExternalSslEntryPoint(sso.getLoginPath()))
}

http.addFilterBefore(externalAuthTokenFilter, AbstractPreAuthenticatedProcessingFilter.class)
}
}

/**
* This class exists to change the login redirect (to /login) from http to https in instances
* where the SSL is terminated outside of this server.
*/
static class ExternalSslEntryPoint extends LoginUrlAuthenticationEntryPoint {

ExternalSslEntryPoint(String loginFormUrl) {
super(loginFormUrl)
}

@Override
protected String buildRedirectUrlToLoginPage(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
return super.buildRedirectUrlToLoginPage(request, response, authException).replaceFirst("http", "https")
}
}
}