Skip to content

Added new parameters for namespaces #1492

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

Merged
merged 1 commit into from
Sep 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ public final class Messages {
public static final String LIST_OPERATION_ACTIONS_AUDIT_LOG_MESSAGE = "List operation action in space with id: {0}";
public static final String EXECUTE_OPERATION_AUDIT_LOG_MESSAGE = "Execute operation in space with id: {0}";
public static final String GET_OPERATION_LOGS_AUDIT_LOG_MESSAGE = "Get operation logs in space with id: {0}";
public static final String GET_OPERATION_LOG_CONTENT_AUDIT_LOG_MESSAGE = "Get operation log content in space with id: {0}";
public static final String GET_OPERATION_LOG_CONTENT_AUDIT_LOG_MESSAGE = "Get operation log content in space with id: {0}";
public static final String START_OPERATION_AUDIT_LOG_MESSAGE = "Start {0} operation in space with id: {1}";
public static final String GET_INFO_FOR_OPERATION = "Get info for operation in space with id: {0}";

public static final String LIST_MTA_AUDIT_LOG_MESSAGE = "List MTA in space with id: {0}";
public static final String LIST_MTA_AUDIT_LOG_MESSAGE = "List MTA in space with id: {0}";
public static final String GET_MTA_AUDIT_LOG_MESSAGE = "Get MTA in space with id: {0}";

public static final String GET_INFO_FOR_API_AUDIT_LOG_CONFIG = "Get information for api";
Expand All @@ -207,7 +207,6 @@ public final class Messages {

public static final String FETCH_TOKEN_AUDIT_LOG_CONFIG = "Access token fetch";


// Audit log configuration
public static final String GET_CSRF_TOKEN_AUDIT_LOG_CONFIG = "CSRF token get ";

Expand All @@ -227,10 +226,12 @@ public final class Messages {
public static final String START_OPERATION_AUDIT_LOG_CONFIG = "Operation start";
public static final String GET_OPERATION_INFO_AUDIT_LOG_CONFIG = "Operation info";

public static final String MTA_INFO_AUDIT_LOG_CONFIG= "MTA info";
public static final String MTA_INFO_AUDIT_LOG_CONFIG = "MTA info";
public static final String MTA_LIST_AUDIT_LOG_CONFIG = "MTA list";

public static final String API_INFO_AUDIT_LOG_CONFIG= "Api info";
public static final String API_INFO_AUDIT_LOG_CONFIG = "Api info";
public static final String IGNORING_NAMESPACE_PARAMETERS = "Ignoring parameter \"{0}\" , as the MTA is not deployed with namespace!";
public static final String NAMESPACE_PARSING_ERROR_MESSAGE = "Cannot parse \"{0}\" flag - expected a boolean format.";

private Messages() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ public MtaDescriptorPropertiesResolver(MtaDescriptorPropertiesResolverContext co
}

public List<ParameterValidator> getValidatorsList() {
return List.of(new HostValidator(), new DomainValidator(), new RoutesValidator(context.getNamespace(), context.applyNamespace()),
new IdleRoutesValidator(context.getNamespace(), context.applyNamespace()), new TasksValidator(),
new VisibilityValidator(), new RestartOnEnvChangeValidator());
return List.of(new HostValidator(), new DomainValidator(),
new RoutesValidator(context.getNamespace(),
context.applyNamespaceAppRoutesGlobalLevel(),
context.applyNamespaceAppRoutesProcessVariable()),
new IdleRoutesValidator(context.getNamespace(),
context.applyNamespaceAppRoutesGlobalLevel(),
context.applyNamespaceAppRoutesProcessVariable()),
new TasksValidator(), new VisibilityValidator(), new RestartOnEnvChangeValidator());
}

public DeploymentDescriptor resolve(DeploymentDescriptor descriptor) {
Expand Down Expand Up @@ -113,8 +118,12 @@ private void unescapeEscapedReferences(DeploymentDescriptor descriptor) {
}

private DeploymentDescriptor correctEntityNames(DeploymentDescriptor descriptor) {
List<ParameterValidator> correctors = Arrays.asList(new ApplicationNameValidator(context.getNamespace(), context.applyNamespace()),
new ServiceNameValidator(context.getNamespace(), context.applyNamespace()));
List<ParameterValidator> correctors = Arrays.asList(new ApplicationNameValidator(context.getNamespace(),
context.applyNamespaceAppNamesGlobalLevel(),
context.applyNamespaceAppNamesProcessVariable()),
new ServiceNameValidator(context.getNamespace(),
context.applyNamespaceServiceNamesGlobalLevel(),
context.applyNamespaceServiceNamesProcessVariable()));
return context.getHandlerFactory()
.getDescriptorParametersValidator(descriptor, correctors)
.validate();
Expand All @@ -128,7 +137,7 @@ private void editRoutesSetTemporaryPlaceholders(DeploymentDescriptor descriptor)

for (Map<String, Object> routeMap : routes) {
Object routeValue = routeMap.get(SupportedParameters.ROUTE);
boolean noHostname = MapUtil.parseBooleanFlag(routeMap, SupportedParameters.NO_HOSTNAME, false);
Boolean noHostname = MapUtil.parseBooleanFlag(routeMap, SupportedParameters.NO_HOSTNAME, false);
String protocol = (String) routeMap.get(SupportedParameters.ROUTE_PROTOCOL);
if (routeValue instanceof String) {
routeMap.put(SupportedParameters.ROUTE, replacePartsWithIdlePlaceholders((String) routeValue, noHostname, protocol));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ public interface MtaDescriptorPropertiesResolverContext {
@Nullable
String getNamespace();

boolean applyNamespace();
boolean applyNamespaceAppNamesGlobalLevel();

boolean applyNamespaceServiceNamesGlobalLevel();

boolean applyNamespaceAppRoutesGlobalLevel();

@Nullable
Boolean applyNamespaceAppNamesProcessVariable();

@Nullable
Boolean applyNamespaceServiceNamesProcessVariable();

@Nullable
Boolean applyNamespaceAppRoutesProcessVariable();

boolean shouldReserveTemporaryRoute();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class SupportedParameters {
public static final String APPLY_NAMESPACE = "apply-namespace";
public static final String MTA_VERSION = "mta-version";
public static final String MTA_ID = "mta-id";
public static final String APPLY_NAMESPACE_APPS = "app-names";
public static final String APPLY_NAMESPACE_SERVICES = "service-names";
public static final String APPLY_NAMESPACE_ROUTES = "app-routes";

// Module / module type parameters:
public static final String APP_NAME = "app-name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public CloudRoute parseIdleRouteMap(Map<String, Object> routeMap) {
if (routeString == null) {
return null;
}
boolean noHostname = MapUtil.parseBooleanFlag(routeMap, SupportedParameters.NO_HOSTNAME, false);
Boolean noHostname = MapUtil.parseBooleanFlag(routeMap, SupportedParameters.NO_HOSTNAME, false);
String protocol = (String) routeMap.get(SupportedParameters.ROUTE_PROTOCOL);
return new ApplicationURI(routeString, noHostname, protocol).toCloudRoute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public CloudRoute parseRouteMap(Map<String, Object> routeMap) {
if (routeString == null) {
return null;
}
boolean noHostname = MapUtil.parseBooleanFlag(routeMap, SupportedParameters.NO_HOSTNAME, false);
Boolean noHostname = MapUtil.parseBooleanFlag(routeMap, SupportedParameters.NO_HOSTNAME, false);
String protocol = (String) routeMap.get(SupportedParameters.ROUTE_PROTOCOL);
return new ApplicationURI(routeString, noHostname, protocol).toCloudRoute();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.cloudfoundry.multiapps.controller.core.util;

import org.cloudfoundry.multiapps.common.util.MapUtil;
import org.cloudfoundry.multiapps.controller.core.Messages;
import org.cloudfoundry.multiapps.controller.core.model.SupportedParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Objects;

import static java.text.MessageFormat.format;

public class NamespaceValidationUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(NamespaceValidationUtil.class);
private String namespace;
private boolean applyNamespaceGlobalLevel;
private Boolean applyNamespaceProcessVariable;

public NamespaceValidationUtil() {
}

public NamespaceValidationUtil(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
this.namespace = namespace;
this.applyNamespaceGlobalLevel = applyNamespaceGlobalLevel;
this.applyNamespaceProcessVariable = applyNamespaceProcessVariable;
}

public String getNamespace() {
return namespace;
}

public boolean shouldApplyNamespaceResultValue(final Map<String, Object> relatedParameters) {
Boolean applyNamespaceModuleLevel = MapUtil.parseBooleanFlag(relatedParameters, SupportedParameters.APPLY_NAMESPACE, null);
boolean applyNamespaceResult = Objects.requireNonNullElse(applyNamespaceProcessVariable,
Objects.requireNonNullElse(applyNamespaceModuleLevel,
applyNamespaceGlobalLevel));
if (namespace == null && applyNamespaceResult) {
LOGGER.warn(format(Messages.IGNORING_NAMESPACE_PARAMETERS, SupportedParameters.NAMESPACE));
}
return applyNamespaceResult;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
import java.util.Set;

import org.cloudfoundry.multiapps.common.ContentException;
import org.cloudfoundry.multiapps.common.util.MapUtil;
import org.cloudfoundry.multiapps.controller.core.Messages;
import org.cloudfoundry.multiapps.controller.core.model.SupportedParameters;
import org.cloudfoundry.multiapps.controller.core.util.NameUtil;
import org.cloudfoundry.multiapps.controller.core.util.NamespaceValidationUtil;
import org.cloudfoundry.multiapps.mta.model.Module;

public class ApplicationNameValidator implements ParameterValidator {
public class ApplicationNameValidator extends NamespaceValidationUtil implements ParameterValidator {

private final String namespace;
private final boolean applyNamespaceGlobal;

public ApplicationNameValidator(String namespace, boolean applyNamespace) {
this.namespace = namespace;
this.applyNamespaceGlobal = applyNamespace;
public ApplicationNameValidator(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
super(namespace, applyNamespaceGlobalLevel, applyNamespaceProcessVariable);
}

@Override
Expand Down Expand Up @@ -47,11 +43,8 @@ public Object attemptToCorrect(Object applicationName, final Map<String, Object>
if (!(applicationName instanceof String)) {
throw new ContentException(Messages.COULD_NOT_CREATE_VALID_APPLICATION_NAME_FROM_0, applicationName);
}

boolean applyNamespaceLocal = MapUtil.parseBooleanFlag(relatedParameters, SupportedParameters.APPLY_NAMESPACE, true);
boolean applyNamespace = applyNamespaceGlobal && applyNamespaceLocal;

return NameUtil.computeValidApplicationName((String) applicationName, namespace, applyNamespace);
boolean applyNamespaceResult = shouldApplyNamespaceResultValue(relatedParameters);
return NameUtil.computeValidApplicationName((String) applicationName, getNamespace(), applyNamespaceResult);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,26 @@
import java.util.Map;
import java.util.Set;

import org.cloudfoundry.multiapps.common.util.MapUtil;
import org.cloudfoundry.multiapps.controller.core.Messages;
import org.cloudfoundry.multiapps.controller.core.model.SupportedParameters;
import org.cloudfoundry.multiapps.controller.core.util.NameUtil;

public class HostValidator extends RoutePartValidator {

private final String namespace;
private final boolean applyNamespaceGlobal;

public HostValidator() {
this.namespace = null;
this.applyNamespaceGlobal = false;
super(null, false, false);
}

public HostValidator(String namespace, boolean applyNamespaceGlobal) {
this.namespace = namespace;
this.applyNamespaceGlobal = applyNamespaceGlobal;
public HostValidator(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
super(namespace, applyNamespaceGlobalLevel, applyNamespaceProcessVariable);
}

@Override
public boolean isValid(Object routePart, final Map<String, Object> context) {
if (!super.isValid(routePart, context)) {
return false;
}
if (shouldApplyNamespace(context)) {
return ((String) routePart).startsWith(namespace);
if (shouldApplyNamespace(context) && getNamespace() != null) {
return ((String) routePart).startsWith(getNamespace());
}
return true;
}
Expand Down Expand Up @@ -62,12 +55,11 @@ protected String getRoutePartPattern() {

@Override
protected String modifyAndShortenRoutePart(String routePart, Map<String, Object> context) {
return NameUtil.computeNamespacedNameWithLength(routePart, namespace, shouldApplyNamespace(context), getRoutePartMaxLength());
return NameUtil.computeNamespacedNameWithLength(routePart, getNamespace(), shouldApplyNamespace(context), getRoutePartMaxLength());
}

private boolean shouldApplyNamespace(Map<String, Object> context) {
boolean applyNamespaceLocal = MapUtil.parseBooleanFlag(context, SupportedParameters.APPLY_NAMESPACE, true);
return applyNamespaceGlobal && applyNamespaceLocal;
return shouldApplyNamespaceResultValue(context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

public class IdleRouteValidator extends RouteValidator {

public IdleRouteValidator(String namespace, boolean applyNamespaceGlobal) {
super(namespace, applyNamespaceGlobal);
public IdleRouteValidator(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
super(namespace, applyNamespaceGlobalLevel, applyNamespaceProcessVariable);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

public class IdleRoutesValidator extends RoutesValidator {

public IdleRoutesValidator(String namespace, boolean applyNamespaceGlobal) {
super(namespace, applyNamespaceGlobal);
public IdleRoutesValidator(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
super(namespace, applyNamespaceGlobalLevel, applyNamespaceProcessVariable);
}

@Override
protected void initRoutesValidators(String namespace, boolean applyNamespaceGlobal) {
ParameterValidator idleRouteValidator = new IdleRouteValidator(namespace, applyNamespaceGlobal);
protected void initRoutesValidators(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
ParameterValidator idleRouteValidator = new IdleRouteValidator(namespace, applyNamespaceGlobalLevel, applyNamespaceProcessVariable);
this.validators = Map.of(idleRouteValidator.getParameterName(), idleRouteValidator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@

import org.cloudfoundry.multiapps.common.SLException;
import org.cloudfoundry.multiapps.controller.core.util.NameUtil;
import org.cloudfoundry.multiapps.controller.core.util.NamespaceValidationUtil;
import org.cloudfoundry.multiapps.mta.model.Module;

public abstract class RoutePartValidator implements ParameterValidator {
public abstract class RoutePartValidator extends NamespaceValidationUtil implements ParameterValidator {
protected RoutePartValidator() {
}

protected RoutePartValidator(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
super(namespace, applyNamespaceGlobalLevel, applyNamespaceProcessVariable);
}

@Override
public String attemptToCorrect(Object routePart, final Map<String, Object> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public class RouteValidator implements ParameterValidator {

private final List<ParameterValidator> validators;

public RouteValidator(String namespace, boolean applyNamespaceGlobal) {
this.validators = List.of(new HostValidator(namespace, applyNamespaceGlobal), new DomainValidator());
public RouteValidator(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
this.validators = List.of(new HostValidator(namespace, applyNamespaceGlobalLevel, applyNamespaceProcessVariable),
new DomainValidator());
}

@Override
Expand All @@ -26,7 +27,7 @@ public String attemptToCorrect(Object route, final Map<String, Object> context)
throw new SLException(Messages.COULD_NOT_CREATE_VALID_ROUTE, route);
}
String routeString = (String) route;
boolean noHostname = MapUtil.parseBooleanFlag(context, SupportedParameters.NO_HOSTNAME, false);
Boolean noHostname = MapUtil.parseBooleanFlag(context, SupportedParameters.NO_HOSTNAME, false);
String protocol = (String) context.get(SupportedParameters.ROUTE_PROTOCOL);
ApplicationURI uri = new ApplicationURI(routeString, noHostname, protocol);
try {
Expand Down Expand Up @@ -71,7 +72,7 @@ public boolean isValid(Object route, final Map<String, Object> context) {
return false;
}

boolean noHostname = MapUtil.parseBooleanFlag(context, SupportedParameters.NO_HOSTNAME, false);
Boolean noHostname = MapUtil.parseBooleanFlag(context, SupportedParameters.NO_HOSTNAME, false);
String protocol = (String) context.get(SupportedParameters.ROUTE_PROTOCOL);
ApplicationURI uri = new ApplicationURI(routeString, noHostname, protocol);
Map<String, Object> uriParts = uri.getURIParts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public class RoutesValidator implements ParameterValidator {

protected Map<String, ParameterValidator> validators;

public RoutesValidator(String namespace, boolean applyNamespaceGlobal) {
initRoutesValidators(namespace, applyNamespaceGlobal);
public RoutesValidator(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
initRoutesValidators(namespace, applyNamespaceGlobalLevel, applyNamespaceProcessVariable);
}

protected void initRoutesValidators(String namespace, boolean applyNamespaceGlobal) {
ParameterValidator routeValidator = new RouteValidator(namespace, applyNamespaceGlobal);
protected void initRoutesValidators(String namespace, boolean applyNamespaceGlobalLevel, Boolean applyNamespaceProcessVariable) {
ParameterValidator routeValidator = new RouteValidator(namespace, applyNamespaceGlobalLevel, applyNamespaceProcessVariable);
this.validators = Map.of(routeValidator.getParameterName(), routeValidator);
}

Expand Down
Loading
Loading