Skip to content
Merged
Show file tree
Hide file tree
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 @@ -4,9 +4,11 @@
import com.styra.opa.OPAClient;
import com.styra.opa.OPAException;
import com.styra.opa.springboot.autoconfigure.OPAProperties;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.AuthorizationManager;
Expand Down Expand Up @@ -39,10 +41,14 @@ public class OPAAuthorizationManager
OPAAuthorizationManager.class
);

// If opaPath is null, then we assume the user wants to use the default path.
private String opaPath;

private ContextDataProvider ctxProvider;

private OPAClient opa;

@Autowired
private OPAProperties opaProperties;

/**
Expand Down Expand Up @@ -113,10 +119,9 @@ public OPAAuthorizationManager(OPAClient opa, ContextDataProvider newProvider) {
* @param newProvider
*/
public OPAAuthorizationManager(OPAClient opa, String newOpaPath, ContextDataProvider newProvider) {
// If newOpaPath is null, then we assume the user wants to use the default path.
opaProperties = new OPAProperties();
opaProperties.setPath(newOpaPath);
this.opa = opa != null ? opa : defaultOPAClient(opaProperties);
this.opaPath = newOpaPath;
this.opa = opa != null ? opa : defaultOPAClient();
this.ctxProvider = newProvider;
}

Expand All @@ -131,8 +136,8 @@ public OPAAuthorizationManager(String newOpaPath, ContextDataProvider newProvide
this(null, newOpaPath, newProvider);
}

private static OPAClient defaultOPAClient(OPAProperties opaProperties) {
String opaURL = opaProperties.getUrl();
private static OPAClient defaultOPAClient() {
String opaURL = OPAProperties.DEFAULT_URL;
String opaURLEnv = System.getenv("OPA_URL");
if (opaURLEnv != null) {
opaURL = opaURLEnv;
Expand All @@ -141,6 +146,13 @@ private static OPAClient defaultOPAClient(OPAProperties opaProperties) {
return opac;
}

@PostConstruct
private void init() {
if (opaPath == null) {
opaPath = opaProperties.getPath();
}
}

public String getReasonKey() {
return opaProperties.getResponse().getContext().getReasonKey();
}
Expand Down Expand Up @@ -258,10 +270,10 @@ public OPAResponse opaRequest(
logger.trace("OPA input for request: {}", iMap);
OPAResponse resp = null;
try {
if (opaProperties.getPath() != null) {
logger.trace("OPA path is {}", opaProperties.getPath());
if (opaPath != null) {
logger.trace("OPA path is {}", opaPath);
resp = opa.evaluate(
opaProperties.getPath(),
opaPath,
iMap,
new TypeReference<OPAResponse>() {}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.TestPropertySource;

import java.util.Map;

Expand All @@ -18,6 +19,7 @@
@SpringBootTest(classes = OPAAutoConfiguration.class)
public class OPAAutoConfigurationTest {

@TestPropertySource(properties = { "opa.response.context.reason-key=fr" })
@Nested
public class DefaultOPAAutoConfigurationTest {

Expand All @@ -34,6 +36,15 @@ public void testDefaultBeansExistence() {
assertNotNull(opaClient);
assertNotNull(opaAuthorizationManager);
}

/**
* Make sure that {@link #opaProperties} bean is autowired in {@link #opaAuthorizationManager}.
*/
@Test
public void testOPAPropertiesBeanAutowiring() {
assertEquals("fr", opaProperties.getResponse().getContext().getReasonKey());
assertEquals("fr", opaAuthorizationManager.getReasonKey());
}
}

@Import(OPAAutoConfigurationTestWithCustomOPAClient.CustomOPAClientConfiguration.class)
Expand Down
Loading