-
Notifications
You must be signed in to change notification settings - Fork 5
Add OPAAutoConfiguration to define common beans
#28
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.styra.opa.springboot.autoconfigure; | ||
|
|
||
| import com.styra.opa.OPAClient; | ||
| import com.styra.opa.springboot.OPAAuthorizationManager; | ||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
| import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
|
|
||
| /** | ||
| * {@link EnableAutoConfiguration Auto-configuration} for OPA authorization support. | ||
| */ | ||
| @AutoConfiguration | ||
| @EnableConfigurationProperties(OPAProperties.class) | ||
| @AutoConfigureBefore(SecurityAutoConfiguration.class) | ||
| @ConditionalOnClass(OPAClient.class) | ||
| public class OPAAutoConfiguration { | ||
|
|
||
| /** | ||
| * Create an {@link OPAClient} bean using {@link OPAProperties#getUrl()}. | ||
| */ | ||
| @Bean | ||
| @ConditionalOnMissingBean(OPAClient.class) | ||
| public OPAClient opaClient(OPAProperties opaProperties) { | ||
| return new OPAClient(opaProperties.getUrl()); | ||
| } | ||
|
|
||
| /** | ||
| * Create an {@link OPAAuthorizationManager} bean using {@link OPAClient} bean and {@link OPAProperties#getPath()}. | ||
| */ | ||
| @Bean | ||
| @ConditionalOnMissingBean(OPAAuthorizationManager.class) | ||
| public OPAAuthorizationManager opaAuthorizationManager(OPAClient opaClient, OPAProperties opaProperties) { | ||
| return new OPAAuthorizationManager(opaClient, opaProperties.getPath()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| com.styra.opa.springboot.autoconfigure.OPAAutoConfiguration |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package com.styra.opa.springboot.autoconfigure; | ||
|
|
||
| import com.styra.opa.OPAClient; | ||
| import com.styra.opa.springboot.OPAAuthorizationManager; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| 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 java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| @SpringBootTest(classes = OPAAutoConfiguration.class) | ||
| public class OPAAutoConfigurationTest { | ||
|
|
||
| @Nested | ||
| public class DefaultOPAAutoConfigurationTest { | ||
|
|
||
| @Autowired(required = false) | ||
| private OPAProperties opaProperties; | ||
| @Autowired(required = false) | ||
| private OPAClient opaClient; | ||
| @Autowired(required = false) | ||
| private OPAAuthorizationManager opaAuthorizationManager; | ||
|
|
||
| @Test | ||
| public void test() { | ||
| assertNotNull(opaProperties); | ||
| assertNotNull(opaClient); | ||
| assertNotNull(opaAuthorizationManager); | ||
| } | ||
| } | ||
|
|
||
| @Import(OPAAutoConfigurationTestWithCustomOPAClient.CustomOPAClientConfiguration.class) | ||
| @Nested | ||
| public class OPAAutoConfigurationTestWithCustomOPAClient { | ||
|
|
||
| @Autowired(required = false) | ||
| private Map<String, OPAClient> opaClients; | ||
|
|
||
| @Test | ||
| public void test() { | ||
| assertNotNull(opaClients); | ||
| assertEquals(1, opaClients.size()); | ||
| assertNotNull(opaClients.get("customOPAClient")); | ||
| } | ||
|
|
||
| @Configuration | ||
| public static class CustomOPAClientConfiguration { | ||
|
|
||
| @Bean | ||
| public OPAClient customOPAClient() { | ||
| return new OPAClient("http://localhost:8182"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Import(OPAAutoConfigurationTestWithCustomOPAAuthorizationManager.CustomOPAAuthorizationManagerConfiguration.class) | ||
| @Nested | ||
| public class OPAAutoConfigurationTestWithCustomOPAAuthorizationManager { | ||
|
|
||
| @Autowired(required = false) | ||
| private Map<String, OPAAuthorizationManager> opaAuthorizationManagers; | ||
|
|
||
| @Test | ||
| public void test() { | ||
|
||
| assertNotNull(opaAuthorizationManagers); | ||
| assertEquals(1, opaAuthorizationManagers.size()); | ||
| assertNotNull(opaAuthorizationManagers.get("customOPAAuthorizationManager")); | ||
| } | ||
|
|
||
| @Configuration | ||
| public static class CustomOPAAuthorizationManagerConfiguration { | ||
|
|
||
| @Bean | ||
| public OPAAuthorizationManager customOPAAuthorizationManager() { | ||
| return new OPAAuthorizationManager("foo/bar2"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!