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

Support DwC - Avoid superflous WARN log messages #1415

Merged
merged 4 commits into from
Jan 4, 2024
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
10 changes: 5 additions & 5 deletions java-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency> <!-- check if it's still needed when slf4j-api 1.8 is available -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sap.cloud.security.xsuaa</groupId>
<artifactId>spring-xsuaa</artifactId>
Expand All @@ -125,6 +120,11 @@
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javax.annotation.Nonnull;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

import static com.sap.cloud.security.token.TokenClaims.XSUAA.EXTERNAL_ATTRIBUTE;
Expand All @@ -31,8 +32,8 @@
public class HybridTokenFactory implements TokenFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(HybridTokenFactory.class);
private static String xsAppId;
private static ScopeConverter xsScopeConverter;
static Optional<String> xsAppId;
static ScopeConverter xsScopeConverter;

/**
* Determines whether the JWT token is issued by XSUAA or IAS identity service,
Expand Down Expand Up @@ -66,25 +67,28 @@ public Token create(String jwtToken) {
*/
static void withXsuaaAppId(@Nonnull String xsAppId) {
LOGGER.debug("XSUAA app id = {}", xsAppId);
HybridTokenFactory.xsAppId = xsAppId;
HybridTokenFactory.xsAppId = Optional.of(xsAppId);
getOrCreateScopeConverter();
}

private static ScopeConverter getOrCreateScopeConverter() {
if (xsScopeConverter == null && getXsAppId() != null) {
xsScopeConverter = new XsuaaScopeConverter(getXsAppId());
if (xsScopeConverter == null && getXsAppId().isPresent()) {
xsScopeConverter = new XsuaaScopeConverter(getXsAppId().get());
}
return xsScopeConverter;
}

private static String getXsAppId() {
if (xsAppId == null) {
OAuth2ServiceConfiguration serviceConfiguration = Environments.getCurrent().getXsuaaConfiguration();
if (serviceConfiguration == null) {
LOGGER.warn("There is no xsuaa service configuration: no local scope check possible.");
} else {
xsAppId = serviceConfiguration.getProperty(ServiceConstants.XSUAA.APP_ID);
}
private static Optional<String> getXsAppId() {
if (Objects.nonNull(xsAppId)) {
return xsAppId;
}
OAuth2ServiceConfiguration serviceConfiguration = Environments.getCurrent().getXsuaaConfiguration();
if (serviceConfiguration != null) {
xsAppId = Optional.of(serviceConfiguration.getProperty(ServiceConstants.XSUAA.APP_ID));
} else {
LOGGER.warn(
"There is no xsuaa service configuration with 'xsappname' property: no local scope check possible.");
xsAppId = Optional.empty();
}
return xsAppId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.sap.cloud.security.servlet;

import ch.qos.logback.core.read.ListAppender;
import com.sap.cloud.security.token.XsuaaToken;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.Logger;

import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class HybridTokenFactoryTest {

private ListAppender<ILoggingEvent> logWatcher;
private HybridTokenFactory cut;

@BeforeEach
public void setup() {
cut = new HybridTokenFactory();
cut.xsAppId = null;
cut.xsScopeConverter = null;
logWatcher = new ListAppender<>();
logWatcher.start();
((Logger) LoggerFactory.getLogger(HybridTokenFactory.class)).addAppender(logWatcher);
}

@AfterEach
void teardown() {
((Logger) LoggerFactory.getLogger(HybridTokenFactory.class)).detachAndStopAllAppenders();
}

@Test
void oneWarningMessageIncaseSecurityConfigIsMissing() throws IOException {
String jwt = IOUtils.resourceToString("/xsuaaJwtBearerTokenRSA256.txt", UTF_8);
XsuaaToken token = (XsuaaToken) cut.create(jwt);
cut.create(jwt);

assertThat(token.getIssuer()).isEqualTo("http://auth.com");
assertThrows(IllegalArgumentException.class, () -> token.hasLocalScope("abc"));
assertThat(logWatcher.list).isNotNull().hasSize(1);
assertThat(logWatcher.list.get(0).getMessage()).contains("There is no xsuaa service configuration");
}
}
Loading