Skip to content

Commit

Permalink
apply formatter
Browse files Browse the repository at this point in the history
Signed-off-by: liga-oz <liga.ozolina@sap.com>
  • Loading branch information
liga-oz committed Jan 5, 2024
1 parent 2430469 commit e8607e7
Show file tree
Hide file tree
Showing 58 changed files with 755 additions and 629 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public ServiceBindingEnvironment withEnvironmentVariableReader(UnaryOperator<Str
@Nullable
@Override
public OAuth2ServiceConfiguration getXsuaaConfiguration() {
List<ServiceConstants.Plan> orderedServicePlans = List.of(ServiceConstants.Plan.APPLICATION, ServiceConstants.Plan.BROKER,
List<ServiceConstants.Plan> orderedServicePlans = List.of(ServiceConstants.Plan.APPLICATION,
ServiceConstants.Plan.BROKER,
ServiceConstants.Plan.SPACE, ServiceConstants.Plan.DEFAULT);
List<OAuth2ServiceConfiguration> xsuaaConfigurations = getServiceConfigurationsAsList().get(XSUAA);

Expand Down Expand Up @@ -135,11 +136,14 @@ public Map<Service, List<OAuth2ServiceConfiguration>> getServiceConfigurationsAs
* Gives access to all service configurations parsed from the environment. The
* service configurations are parsed on the first access, then cached.
*
* Note that the result contains only one service configuration per service plan and does not contain configurations
* with a service plan other than those from {@link ServiceConstants}#Plan.
* Use {@link ServiceBindingEnvironment#getServiceConfigurationsAsList()} to get a complete list of configurations.
* Note that the result contains only one service configuration per service plan
* and does not contain configurations with a service plan other than those from
* {@link ServiceConstants}#Plan. Use
* {@link ServiceBindingEnvironment#getServiceConfigurationsAsList()} to get a
* complete list of configurations.
*
* @return the service configurations grouped first by service, then by service plan.
* @return the service configurations grouped first by service, then by service
* plan.
*/
@Override
public Map<Service, Map<ServiceConstants.Plan, OAuth2ServiceConfiguration>> getServiceConfigurations() {
Expand All @@ -158,8 +162,7 @@ public Map<Service, Map<ServiceConstants.Plan, OAuth2ServiceConfiguration>> getS
.collect(Collectors.toMap(
config -> ServiceConstants.Plan.from(config.getProperty(SERVICE_PLAN)),
Function.identity(),
(a, b) -> a
));
(a, b) -> a));

result.put(service, planConfigurations);
}
Expand Down Expand Up @@ -194,7 +197,7 @@ private void clearServiceConfigurations() {
private ServiceConstants.Plan getServicePlan(OAuth2ServiceConfiguration config) {
try {
return ServiceConstants.Plan.from(config.getProperty(SERVICE_PLAN));
} catch(IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public static OAuth2ServiceConfigurationBuilder mapToOAuth2ServiceConfigurationB
OAuth2ServiceConfigurationBuilder builder = OAuth2ServiceConfigurationBuilder.forService(service)
.withProperties(credentials.getEntries(String.class))
.withProperty(NAME, b.getName().orElse(""))
.withProperty(SERVICE_PLAN, b.getServicePlan().orElse(ServiceConstants.Plan.APPLICATION.name()).toUpperCase());
.withProperty(SERVICE_PLAN,
b.getServicePlan().orElse(ServiceConstants.Plan.APPLICATION.name()).toUpperCase());

if (IAS.equals(service)) {
parseDomains(builder, credentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class ServiceBindingEnvironmentTest {
static void setUp() throws IOException {
String singleXsuaaConfiguration = IOUtils.resourceToString("/vcapXsuaaServiceSingleBinding.json", UTF_8);
String multipleXsuaaConfigurations = IOUtils.resourceToString("/vcapXsuaaServiceMultipleBindings.json", UTF_8);
String multipleXsuaaApplicationPlanConfigurations = IOUtils.resourceToString("/vcapXsuaaServiceMultipleApplicationPlanBindings.json", UTF_8);
String multipleXsuaaApplicationPlanConfigurations = IOUtils
.resourceToString("/vcapXsuaaServiceMultipleApplicationPlanBindings.json", UTF_8);
String singleIasConfiguration = IOUtils.resourceToString("/vcapIasServiceSingleBinding.json", UTF_8);
String unknownXsuaaPlanConfig = IOUtils.resourceToString("/vcapUnknownServicePlan.json", UTF_8);
vcapXsa = IOUtils.resourceToString("/vcapXsuaaXsaSingleBinding.json", UTF_8);
Expand Down Expand Up @@ -73,7 +74,9 @@ void getXsuaaConfigurationForTokenExchange() {
assertNotSame(cutMultipleXsuaa.getXsuaaConfigurationForTokenExchange(),
cutMultipleXsuaa.getXsuaaConfiguration());

assertThat(cutMultipleApplicationPlanXsuaa.getXsuaaConfigurationForTokenExchange().getProperty(ServiceConstants.SERVICE_PLAN),
assertThat(
cutMultipleApplicationPlanXsuaa.getXsuaaConfigurationForTokenExchange()
.getProperty(ServiceConstants.SERVICE_PLAN),
equalToIgnoringCase(ServiceConstants.Plan.BROKER.toString()));
assertNotSame(cutMultipleApplicationPlanXsuaa.getXsuaaConfigurationForTokenExchange(),
cutMultipleXsuaa.getXsuaaConfiguration());
Expand Down Expand Up @@ -138,7 +141,8 @@ void getServiceConfigurations() {
configs = cutMultipleApplicationPlanXsuaa.getServiceConfigurations();
assertThat(configs.get(Service.XSUAA).entrySet(), hasSize(2));
assertThat(configs.get(Service.IAS).entrySet(), is(empty()));
assertThat(configs.get(Service.XSUAA).get(ServiceConstants.Plan.APPLICATION).getProperty(ServiceConstants.XSUAA.APP_ID), equalTo("na-d6a3278d-5e07-40e9-92ae-546bbfd9cdde!t8066"));
assertThat(configs.get(Service.XSUAA).get(ServiceConstants.Plan.APPLICATION)
.getProperty(ServiceConstants.XSUAA.APP_ID), equalTo("na-d6a3278d-5e07-40e9-92ae-546bbfd9cdde!t8066"));
assertNotNull(configs.get(Service.XSUAA).get(ServiceConstants.Plan.BROKER));
assertNotNull(configs.get(Service.XSUAA).get(ServiceConstants.Plan.APPLICATION));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
*/
package com.sap.cloud.security.config;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;



/**
* Central entry point to access the OAuth configuration
* ({@link OAuth2ServiceConfiguration}) of a supported identity {@link Service}.
*/
public interface Environment {
/**
* Return the primary OAuth service configuration of Xsuaa identity service instance.
*
* Return the primary OAuth service configuration of Xsuaa identity service
* instance.
*
* @return the OAuth service configuration or null, in case there is no instance
*/
@Nullable
Expand All @@ -43,8 +41,8 @@ public interface Environment {

/**
* In case there is only one Xsuaa identity service instance, this one gets
* returned. In case there are multiple bindings the primary one of plan "broker" gets
* returned.
* returned. In case there are multiple bindings the primary one of plan
* "broker" gets returned.
*
* @return the service configuration to be used for token exchange
*
Expand All @@ -53,9 +51,9 @@ public interface Environment {
*/
@Nullable
OAuth2ServiceConfiguration getXsuaaConfigurationForTokenExchange();

/**
* Gives access to all service configurations parsed from the environment.
* Gives access to all service configurations parsed from the environment.
*
* @return the service configurations grouped by service
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ default String getZoneId() {
*
* @return the unique application tenant identifier.
*/
default String getAppTid(){
return hasClaim(SAP_GLOBAL_APP_TID) ? getClaimAsString(SAP_GLOBAL_APP_TID) : getClaimAsString(SAP_GLOBAL_ZONE_ID);
default String getAppTid() {
return hasClaim(SAP_GLOBAL_APP_TID) ? getClaimAsString(SAP_GLOBAL_APP_TID)
: getClaimAsString(SAP_GLOBAL_ZONE_ID);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ private TokenClaims() {
public static final String SAP_GLOBAL_SCIM_ID = "scim_id";
public static final String SAP_GLOBAL_USER_ID = "user_uuid";


/**
* @deprecated Use {@link TokenClaims#SAP_GLOBAL_APP_TID} instead.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.sap.cloud.security.token.validation;

/**
* This interface is for INTERNAL usage only to add backward-compatibility for test credentials with trusted domain 'localhost' to the issuer validation.
* This interface is for INTERNAL usage only to add backward-compatibility for
* test credentials with trusted domain 'localhost' to the issuer validation.
*/
public interface TestIssuerValidator {
boolean isValidIssuer(String issuer);
boolean isValidIssuer(String issuer);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.sap.cloud.security.token.validation;

/**
* This interface is for INTERNAL usage only to add backward-compatibility for test credentials with uaadomain 'localhost' during JKU construction.
* This interface is for INTERNAL usage only to add backward-compatibility for
* test credentials with uaadomain 'localhost' during JKU construction.
*/
public interface XsuaaJkuFactory {
String create(String token);
String create(String token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public static ApplicationServerOptions forService(Service service, int jwksPort)

public static ApplicationServerOptions forService(Service service) {
return switch (service) {
case XSUAA -> forXsuaaService(SecurityTestRule.DEFAULT_APP_ID, SecurityTestRule.DEFAULT_CLIENT_ID);
case IAS -> new ApplicationServerOptions(new IasTokenAuthenticator()
.withServiceConfiguration(OAuth2ServiceConfigurationBuilder.forService(Service.IAS)
.withClientId(SecurityTestRule.DEFAULT_CLIENT_ID)
.withUrl("http://localhost")
.withDomains("localhost")
.build()));
default ->
throw new UnsupportedOperationException("Identity Service " + service + " is not yet supported.");
case XSUAA -> forXsuaaService(SecurityTestRule.DEFAULT_APP_ID, SecurityTestRule.DEFAULT_CLIENT_ID);
case IAS -> new ApplicationServerOptions(new IasTokenAuthenticator()
.withServiceConfiguration(OAuth2ServiceConfigurationBuilder.forService(Service.IAS)
.withClientId(SecurityTestRule.DEFAULT_CLIENT_ID)
.withUrl("http://localhost")
.withDomains("localhost")
.build()));
default ->
throw new UnsupportedOperationException("Identity Service " + service + " is not yet supported.");
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ private JwtGenerator(Service service, SignatureCalculator signatureCalculator) {
}

/**
* Creates a new JSONObject object with LinkedHashMap with predictable iteration order.
* Creates a new JSONObject object with LinkedHashMap with predictable iteration
* order.
*
* @return JSONObject
*/
private static JSONObject newPredictableOrderingJSONObject() {
Expand Down Expand Up @@ -166,7 +168,8 @@ private void setDefaultsForNewToken(String azp) {
withClaimValue(TokenClaims.XSUAA.CLIENT_ID, azp); // Client Id left for backward compatibility
if (service == Service.IAS) {
jsonPayload.put(TokenClaims.AUDIENCE, azp);
jsonPayload.put(TokenClaims.SAP_GLOBAL_ZONE_ID, DEFAULT_ZONE_ID); //TODO to be removed once fallback is not supported
jsonPayload.put(TokenClaims.SAP_GLOBAL_ZONE_ID, DEFAULT_ZONE_ID); // TODO to be removed once fallback is not
// supported
jsonPayload.put(TokenClaims.SAP_GLOBAL_APP_TID, DEFAULT_APP_TID);
jsonPayload.put(TokenClaims.SAP_GLOBAL_USER_ID, DEFAULT_USER_ID);
jsonPayload.put(TokenClaims.SAP_GLOBAL_SCIM_ID, DEFAULT_USER_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,6 @@
*/
package com.sap.cloud.security.test;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static com.sap.cloud.security.config.Service.IAS;
import static com.sap.cloud.security.config.Service.XSUAA;
import static com.sap.cloud.security.xsuaa.client.OidcConfigurationService.DISCOVERY_ENDPOINT_DEFAULT;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Nullable;

import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.ee10.servlet.FilterHolder;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.servlet.security.ConstraintSecurityHandler;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.eclipse.jetty.server.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.sap.cloud.environment.servicebinding.SapVcapServicesServiceBindingAccessor;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
Expand All @@ -59,10 +25,32 @@
import com.sap.cloud.security.xsuaa.client.XsuaaDefaultEndpoints;
import com.sap.cloud.security.xsuaa.http.HttpHeaders;
import com.sap.cloud.security.xsuaa.http.MediaType;

import jakarta.servlet.DispatcherType;
import jakarta.servlet.Filter;
import jakarta.servlet.Servlet;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.ee10.servlet.FilterHolder;
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.eclipse.jetty.ee10.servlet.security.ConstraintSecurityHandler;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.eclipse.jetty.server.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.util.*;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static com.sap.cloud.security.config.Service.IAS;
import static com.sap.cloud.security.config.Service.XSUAA;
import static com.sap.cloud.security.xsuaa.client.OidcConfigurationService.DISCOVERY_ENDPOINT_DEFAULT;

public class SecurityTest
implements SecurityTestContext, ServiceMockConfiguration, ApplicationServerConfiguration {
Expand Down Expand Up @@ -200,12 +188,13 @@ public OAuth2ServiceConfigurationBuilder getOAuth2ServiceConfigurationBuilderFro
}

ServiceBinding binding = serviceBindings.get(0);
OAuth2ServiceConfigurationBuilder builder = ServiceBindingMapper.mapToOAuth2ServiceConfigurationBuilder(binding);
OAuth2ServiceConfigurationBuilder builder = ServiceBindingMapper
.mapToOAuth2ServiceConfigurationBuilder(binding);
if (builder != null) {
// adjust domain and URL of the config to fit the mocked service instance
builder = builder.withDomains(URI.create(issuerUrl).getHost()).withUrl(issuerUrl);

if(Objects.equals(Service.from(binding.getServiceName().get()), XSUAA)) {
if (Objects.equals(Service.from(binding.getServiceName().get()), XSUAA)) {
builder.withProperty(ServiceConstants.XSUAA.UAA_DOMAIN, wireMockServer.baseUrl());
}
}
Expand Down Expand Up @@ -290,7 +279,7 @@ public void setup() throws Exception {
wireMockServer.resetAll();
}
if (useApplicationServer && (applicationServer == null || !applicationServer.isStarted())) {
if (applicationServerOptions == null){
if (applicationServerOptions == null) {
this.applicationServerOptions = ApplicationServerOptions.forService(service, wireMockServer.port());
}
startApplicationServer();
Expand Down
Loading

0 comments on commit e8607e7

Please sign in to comment.