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

[Improve] Refactor update setting interface #3579

Closed
wants to merge 4 commits into from
Closed
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
@@ -0,0 +1,98 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.streampark.console.core.bean;

import org.apache.streampark.console.core.entity.Setting;

import lombok.Data;

import java.util.Objects;
import java.util.regex.Pattern;

@Data
public class SettingAlertEmailConfigParams {
private Setting host;
private Setting port;
private Setting from;
private Setting username;
private Setting password;
private Setting ssl;
/* 只允许英文字母、数字、下划线、英文句号、以及中划线组成 */
private final String EMAIL_ADDRESS_REGEXP = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
/* The port value must be between 0-65535 */
private final String PORT_REGEXP =
"^([1-9]\\d{0,3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$";
private final String FROM_REGEXP = "";
/*字母开头,允许5-16字节,允许字母数字下划线*/
private final String USER_NAME_REGEXP = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
/*字母开头,允许5-16字节,允许字母数字下划线*/
private final String PASS_WORD_REGEXP = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
/* Whether SSL is enabled or not can only be true false */
private final String SSL_REGEXP = "^(true|false)$";

public static boolean verifyParams(SettingAlertEmailConfigParams params) {
return params.verifyHost()
&& params.verifyPort()
&& params.verifyFrom()
&& params.verifyUserName()
&& params.verifyPassWord()
&& params.verifySSL();
}

private boolean verifyHost() {
if (Objects.isNull(getHost()) || Objects.isNull(getHost().getSettingValue())) {
return false;
}
return Pattern.matches(EMAIL_ADDRESS_REGEXP, getHost().getSettingValue());
}

private boolean verifyPort() {
if (Objects.isNull(getPort()) || Objects.isNull(getPort().getSettingValue())) {
return false;
}
return Pattern.matches(PORT_REGEXP, getPort().getSettingValue());
}

private boolean verifyFrom() {
if (Objects.isNull(getFrom()) || Objects.isNull(getFrom().getSettingValue())) {
return false;
}
return Pattern.matches(FROM_REGEXP, getFrom().getSettingValue());
}

private boolean verifyUserName() {
if (Objects.isNull(getUsername()) || Objects.isNull(getUsername().getSettingValue())) {
return false;
}
return Pattern.matches(USER_NAME_REGEXP, getUsername().getSettingValue());
}

private boolean verifyPassWord() {
if (Objects.isNull(getPassword()) || Objects.isNull(getPassword().getSettingValue())) {
return false;
}
return Pattern.matches(PASS_WORD_REGEXP, getPassword().getSettingValue());
}

private boolean verifySSL() {
if (Objects.isNull(getSsl()) || Objects.isNull(getSsl().getSettingValue())) {
return false;
}
return Pattern.matches(SSL_REGEXP, getSsl().getSettingValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.streampark.console.core.bean;

import org.apache.streampark.console.core.entity.Setting;

import lombok.Data;

import java.util.Objects;
import java.util.regex.Pattern;

@Data
public class SettingDockerConfigParams {
private Setting username;
private Setting password;
private Setting address;
private Setting namespace;

/*字母开头,允许5-16字节,允许字母数字下划线*/
private final String USER_NAME_REGEXP = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
/*字母开头,允许5-16字节,允许字母数字下划线*/
private final String PASSWORD_REGEXP = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
/*ipv4 match rule */
private final String IPV4_ADDRESS_REGEX =
"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
/*ipv6 match rule */
private final String IPV6_ADDRESS_REGEX = "^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)$";
/* domain match rule */
private final String DOMAIN_NAME_REGEX =
"^((http:\\/\\/)|(https:\\/\\/))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(\\/)";
/* Docker namespace rules based on the format of the domain name */
String NAMESPACE_REGEX = "^(?!-)[a-zA-Z0-9-]{1,253}(?<!-)$";

public static boolean verifyParams(SettingDockerConfigParams params) {
return params.verifyUserName()
&& params.verifyPassWord()
&& params.verifyAddress()
&& params.verifyNameSpace();
}

private boolean verifyUserName() {
if (Objects.isNull(getUsername()) || Objects.isNull(getUsername().getSettingValue())) {
return false;
}
return Pattern.matches(USER_NAME_REGEXP, getUsername().getSettingValue());
}

private boolean verifyPassWord() {
if (Objects.isNull(getPassword()) || Objects.isNull(getPassword().getSettingValue())) {
return false;
}
return Pattern.matches(PASSWORD_REGEXP, getPassword().getSettingValue());
}

private boolean verifyAddress() {
if (Objects.isNull(getAddress()) || Objects.isNull(getAddress().getSettingValue())) {
return false;
}

return Pattern.matches(IPV4_ADDRESS_REGEX, getAddress().getSettingValue())
|| Pattern.matches(IPV6_ADDRESS_REGEX, getAddress().getSettingValue())
|| Pattern.matches(DOMAIN_NAME_REGEX, getAddress().getSettingValue());
}

private boolean verifyNameSpace() {
if (Objects.isNull(getNamespace()) || Objects.isNull(getNamespace().getSettingValue())) {
return false;
}
return Pattern.matches(NAMESPACE_REGEX, getNamespace().getSettingValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package org.apache.streampark.console.core.controller;

import org.apache.streampark.common.util.HadoopUtils;
import org.apache.streampark.console.base.domain.ResponseCode;
import org.apache.streampark.console.base.domain.RestResponse;
import org.apache.streampark.console.core.bean.SettingAlertEmailConfigParams;
import org.apache.streampark.console.core.bean.SettingDockerConfigParams;
import org.apache.streampark.console.core.entity.Setting;
import org.apache.streampark.console.core.service.SettingService;

Expand All @@ -31,9 +34,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@Tag(name = "SETTING_TAG")
Expand Down Expand Up @@ -77,6 +82,42 @@ public RestResponse update(Setting setting) {
return RestResponse.success(updated);
}

@Operation(summary = "Update docker setting")
@PostMapping("update/docker")
@RequiresPermissions("setting:update")
public RestResponse updateDocker(@RequestBody SettingDockerConfigParams params) {
if (!SettingDockerConfigParams.verifyParams(params)) {
return RestResponse.fail(ResponseCode.CODE_FAIL, "The parameter is incorrect, please check!");
}

List<Setting> settings =
Arrays.asList(
params.getAddress(), params.getNamespace(),
params.getUsername(), params.getPassword());
boolean updated = settingService.updateSettings(settings);
return RestResponse.success(updated);
}

@Operation(summary = "Update alert email")
@PostMapping("update/alert/email")
@RequiresPermissions("setting:update")
public RestResponse updateAlertEmail(@RequestBody SettingAlertEmailConfigParams params) {
if (!SettingAlertEmailConfigParams.verifyParams(params)) {
return RestResponse.fail(ResponseCode.CODE_FAIL, "The parameter is incorrect, please check!");
}

List<Setting> settings =
Arrays.asList(
params.getHost(),
params.getPort(),
params.getFrom(),
params.getUsername(),
params.getPassword(),
params.getSsl());
boolean updated = settingService.updateSettings(settings);
return RestResponse.success(updated);
}

@Operation(summary = "Check hadoop status")
@PostMapping("checkHadoop")
public RestResponse checkHadoop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -65,6 +66,14 @@ public interface SettingService extends IService<Setting> {
*/
boolean update(Setting setting);

/**
* * Updates the specified Settings.
*
* @param settings list of setting object to update
* @return true if the update is successful, false otherwise
*/
boolean updateSettings(List<Setting> settings);

/**
* Retrieves the Maven configuration settings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import javax.annotation.PostConstruct;

import java.util.List;
import java.util.Optional;

@Slf4j
Expand Down Expand Up @@ -78,6 +79,16 @@ public boolean update(Setting setting) {
}
}

public boolean updateSettings(List<Setting> settings) {
for (Setting each : settings) {
boolean result = update(each);
if (!result) {
return false;
}
}
return true;
}

@Override
public MavenConfig getMavenConfig() {
return MavenConfig.fromSetting();
Expand Down
Loading
Loading