Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1093 from daniellavoie/hystrix-security
Expose security context to any Hystrix command.
- Loading branch information
Showing
16 changed files
with
501 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
.../org/springframework/cloud/netflix/hystrix/security/HystrixSecurityAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2013-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.cloud.netflix.hystrix.security; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.AllNestedConditions; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.cloud.netflix.hystrix.security.HystrixSecurityAutoConfiguration.HystrixSecurityCondition; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.core.context.SecurityContext; | ||
|
||
import com.netflix.hystrix.Hystrix; | ||
import com.netflix.hystrix.strategy.HystrixPlugins; | ||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy; | ||
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier; | ||
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook; | ||
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher; | ||
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy; | ||
|
||
/** | ||
* @author Daniel Lavoie | ||
*/ | ||
@Configuration | ||
@Conditional(HystrixSecurityCondition.class) | ||
@ConditionalOnClass({ Hystrix.class, SecurityContext.class }) | ||
public class HystrixSecurityAutoConfiguration { | ||
@Autowired(required = false) | ||
private HystrixConcurrencyStrategy existingConcurrencyStrategy; | ||
|
||
@PostConstruct | ||
public void init() { | ||
// Keeps references of existing Hystrix plugins. | ||
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance() | ||
.getEventNotifier(); | ||
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance() | ||
.getMetricsPublisher(); | ||
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance() | ||
.getPropertiesStrategy(); | ||
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance() | ||
.getCommandExecutionHook(); | ||
|
||
HystrixPlugins.reset(); | ||
|
||
// Registers existing plugins excepts the Concurrent Strategy plugin. | ||
HystrixPlugins.getInstance().registerConcurrencyStrategy( | ||
new SecurityContextConcurrencyStrategy(existingConcurrencyStrategy)); | ||
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier); | ||
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher); | ||
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy); | ||
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook); | ||
} | ||
|
||
static class HystrixSecurityCondition extends AllNestedConditions { | ||
|
||
public HystrixSecurityCondition() { | ||
super(ConfigurationPhase.REGISTER_BEAN); | ||
} | ||
|
||
@ConditionalOnProperty(name = "feign.hystrix.enabled", matchIfMissing = true) | ||
static class HystrixEnabled { | ||
|
||
} | ||
|
||
@ConditionalOnProperty(name = "hystrix.shareSecurityContext") | ||
static class ShareSecurityContext { | ||
|
||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...rg/springframework/cloud/netflix/hystrix/security/SecurityContextConcurrencyStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2013-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.cloud.netflix.hystrix.security; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.springframework.security.concurrent.DelegatingSecurityContextCallable; | ||
|
||
import com.netflix.hystrix.HystrixThreadPoolKey; | ||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy; | ||
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariable; | ||
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle; | ||
import com.netflix.hystrix.strategy.properties.HystrixProperty; | ||
|
||
/** | ||
* @author daniellavoie | ||
*/ | ||
public class SecurityContextConcurrencyStrategy extends HystrixConcurrencyStrategy { | ||
private HystrixConcurrencyStrategy existingConcurrencyStrategy; | ||
|
||
public SecurityContextConcurrencyStrategy( | ||
HystrixConcurrencyStrategy existingConcurrencyStrategy) { | ||
this.existingConcurrencyStrategy = existingConcurrencyStrategy; | ||
} | ||
|
||
@Override | ||
public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) { | ||
return existingConcurrencyStrategy != null | ||
? existingConcurrencyStrategy.getBlockingQueue(maxQueueSize) | ||
: super.getBlockingQueue(maxQueueSize); | ||
} | ||
|
||
@Override | ||
public <T> HystrixRequestVariable<T> getRequestVariable( | ||
HystrixRequestVariableLifecycle<T> rv) { | ||
return existingConcurrencyStrategy != null | ||
? existingConcurrencyStrategy.getRequestVariable(rv) | ||
: super.getRequestVariable(rv); | ||
} | ||
|
||
@Override | ||
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey, | ||
HystrixProperty<Integer> corePoolSize, | ||
HystrixProperty<Integer> maximumPoolSize, | ||
HystrixProperty<Integer> keepAliveTime, TimeUnit unit, | ||
BlockingQueue<Runnable> workQueue) { | ||
return existingConcurrencyStrategy != null | ||
? existingConcurrencyStrategy.getThreadPool(threadPoolKey, corePoolSize, | ||
maximumPoolSize, keepAliveTime, unit, workQueue) | ||
: super.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, | ||
keepAliveTime, unit, workQueue); | ||
} | ||
|
||
@Override | ||
public <T> Callable<T> wrapCallable(Callable<T> callable) { | ||
return existingConcurrencyStrategy != null | ||
? existingConcurrencyStrategy | ||
.wrapCallable(new DelegatingSecurityContextCallable<T>(callable)) | ||
: super.wrapCallable(new DelegatingSecurityContextCallable<T>(callable)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...t/java/org/springframework/cloud/netflix/hystrix/security/HystrixSecurityApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2013-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.cloud.netflix.hystrix.security; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.netflix.feign.EnableFeignClients; | ||
import org.springframework.cloud.netflix.hystrix.security.app.UsernameClient; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author Daniel Lavoie | ||
*/ | ||
@Configuration | ||
@SpringBootApplication | ||
@EnableFeignClients(clients = UsernameClient.class) | ||
public class HystrixSecurityApplication { | ||
|
||
} |
Oops, something went wrong.