From ce8c8d31b3e107562b70354d5a2bb7f685f62847 Mon Sep 17 00:00:00 2001 From: Alberto Codutti Date: Mon, 24 Jul 2023 12:54:54 +0200 Subject: [PATCH] feat(job): Added JobServiceSetting 'job.step.property.value.length.max' as soft limit for JobStepProperty.propertyValue Signed-off-by: Alberto Codutti --- .../settings/JobServiceSettingKeys.java | 53 +++++++++++++++++ .../internal/settings/JobServiceSettings.java | 57 +++++++++++++++++++ .../job/step/internal/JobStepServiceImpl.java | 18 +++++- 3 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 service/job/internal/src/main/java/org/eclipse/kapua/service/job/internal/settings/JobServiceSettingKeys.java create mode 100644 service/job/internal/src/main/java/org/eclipse/kapua/service/job/internal/settings/JobServiceSettings.java diff --git a/service/job/internal/src/main/java/org/eclipse/kapua/service/job/internal/settings/JobServiceSettingKeys.java b/service/job/internal/src/main/java/org/eclipse/kapua/service/job/internal/settings/JobServiceSettingKeys.java new file mode 100644 index 00000000000..df98b56eca1 --- /dev/null +++ b/service/job/internal/src/main/java/org/eclipse/kapua/service/job/internal/settings/JobServiceSettingKeys.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2023, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.job.internal.settings; + +import org.eclipse.kapua.commons.setting.SettingKey; +import org.eclipse.kapua.service.job.step.definition.JobStepProperty; + +/** + * {@link SettingKey}s for {@link JobServiceSettings}. + * + * @since 2.0.0 + */ +public enum JobServiceSettingKeys implements SettingKey { + + /** + * Max length of {@link JobStepProperty#getPropertyValue()}. + * + * @since 2.0.0 + */ + JOB_STEP_PROPERTY_VALUE_LENGTH_MAX("job.step.property.value.length.max"); + + /** + * The key value of the {@link SettingKey}. + * + * @since 120.0 + */ + private final String key; + + /** + * Constructor. + * + * @param key The key value of the {@link SettingKey}. + * @since 2.0.0 + */ + private JobServiceSettingKeys(String key) { + this.key = key; + } + + @Override + public String key() { + return key; + } +} diff --git a/service/job/internal/src/main/java/org/eclipse/kapua/service/job/internal/settings/JobServiceSettings.java b/service/job/internal/src/main/java/org/eclipse/kapua/service/job/internal/settings/JobServiceSettings.java new file mode 100644 index 00000000000..d8f5bb77cf6 --- /dev/null +++ b/service/job/internal/src/main/java/org/eclipse/kapua/service/job/internal/settings/JobServiceSettings.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.job.internal.settings; + +import org.eclipse.kapua.commons.setting.AbstractKapuaSetting; + +/** + * {@link AbstractKapuaSetting} for {@code kapua-job-internal} module. + * + * @see AbstractKapuaSetting + * @since 2.0.0 + */ +public class JobServiceSettings extends AbstractKapuaSetting { + + /** + * Setting filename. + * + * @since 2.0.0 + */ + private static final String JOB_SERVICE_SETTING_RESOURCE = "job-service-settings.properties"; + + /** + * Singleton instance. + * + * @since 2.0.0 + */ + private static final JobServiceSettings INSTANCE = new JobServiceSettings(); + + /** + * Constructor. + * + * @since 2.0.0 + */ + private JobServiceSettings() { + super(JOB_SERVICE_SETTING_RESOURCE); + } + + /** + * Gets a singleton instance of {@link JobServiceSettings}. + * + * @return A singleton instance of {@link JobServiceSettings}. + * @since 2.0.0 + */ + public static JobServiceSettings getInstance() { + return INSTANCE; + } +} diff --git a/service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java b/service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java index 35046b0d4eb..b8b58c71eb7 100644 --- a/service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java +++ b/service/job/internal/src/main/java/org/eclipse/kapua/service/job/step/internal/JobStepServiceImpl.java @@ -38,6 +38,8 @@ import org.eclipse.kapua.service.job.execution.JobExecutionQuery; import org.eclipse.kapua.service.job.execution.JobExecutionService; import org.eclipse.kapua.service.job.internal.JobEntityManagerFactory; +import org.eclipse.kapua.service.job.internal.settings.JobServiceSettingKeys; +import org.eclipse.kapua.service.job.internal.settings.JobServiceSettings; import org.eclipse.kapua.service.job.step.JobStep; import org.eclipse.kapua.service.job.step.JobStepAttributes; import org.eclipse.kapua.service.job.step.JobStepCreator; @@ -75,6 +77,10 @@ public class JobStepServiceImpl extends AbstractKapuaService implements JobStepS private static final JobStepDefinitionService JOB_STEP_DEFINITION_SERVICE = LOCATOR.getService(JobStepDefinitionService.class); + private final JobServiceSettings jobServiceSettings = JobServiceSettings.getInstance(); + + private final int jobStepPropertyValueLengthMax = jobServiceSettings.getInt(JobServiceSettingKeys.JOB_STEP_PROPERTY_VALUE_LENGTH_MAX); + public JobStepServiceImpl() { super(JobEntityManagerFactory.getInstance(), null); } @@ -90,8 +96,8 @@ public JobStep create(JobStepCreator jobStepCreator) throws KapuaException { for (JobStepProperty jobStepProperty : jobStepCreator.getStepProperties()) { if (jobStepProperty.getPropertyValue() != null) { - Integer stepPropertyMaxLength = jobStepProperty.getMaxLength() != null ? jobStepProperty.getMaxLength() : 65535; - ArgumentValidator.lengthRange(jobStepProperty.getPropertyValue(), jobStepProperty.getMinLength(), stepPropertyMaxLength, "stepProperties[]." + jobStepProperty.getName()); + Integer stepPropertyMaxLength = jobStepProperty.getMaxLength() != null ? jobStepProperty.getMaxLength() : jobStepPropertyValueLengthMax; + ArgumentValidator.lengthRange(jobStepProperty.getPropertyValue(), jobStepProperty.getMinLength(), stepPropertyMaxLength, "jobStepCreator.stepProperties[]." + jobStepProperty.getName()); } } @@ -175,6 +181,14 @@ public JobStep update(JobStep jobStep) throws KapuaException { ArgumentValidator.notNull(jobStep.getScopeId(), "jobStep.scopeId"); ArgumentValidator.validateEntityName(jobStep.getName(), "jobStep.name"); ArgumentValidator.notNull(jobStep.getJobStepDefinitionId(), "jobStep.stepDefinitionId"); + + for (JobStepProperty jobStepProperty : jobStep.getStepProperties()) { + if (jobStepProperty.getPropertyValue() != null) { + Integer stepPropertyMaxLength = jobStepProperty.getMaxLength() != null ? jobStepProperty.getMaxLength() : jobStepPropertyValueLengthMax; + ArgumentValidator.lengthRange(jobStepProperty.getPropertyValue(), jobStepProperty.getMinLength(), stepPropertyMaxLength, "jobStep.stepProperties[]." + jobStepProperty.getName()); + } + } + if (jobStep.getDescription() != null) { ArgumentValidator.numRange(jobStep.getDescription().length(), 0, 8192, "jobStep.description"); }