Skip to content

Commit

Permalink
[apache#768] feat(cli): Cli method for blacklist update
Browse files Browse the repository at this point in the history
  • Loading branch information
beryllw committed Jun 5, 2023
1 parent 3e58805 commit 74fcb9b
Show file tree
Hide file tree
Showing 25 changed files with 881 additions and 63 deletions.
8 changes: 8 additions & 0 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
<dependency>
<groupId>org.apache.uniffle</groupId>
<artifactId>rss-internal-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
</project>
47 changes: 47 additions & 0 deletions cli/src/main/java/org/apache/uniffle/api/AdminRestApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.uniffle.api;

import org.apache.uniffle.client.RestClient;
import org.apache.uniffle.client.UniffleRestClient;

import java.util.HashMap;
import java.util.Map;

public class AdminRestApi {
private UniffleRestClient client;

private static final String API_BASE_PATH = "admin";

private AdminRestApi() {}

public AdminRestApi(UniffleRestClient client) {
this.client = client;
}

public String refreshAccessChecker(String checker) {
String path = String.format("/api/server/%s/%s", API_BASE_PATH, "refresh/accessChecker");
Map<String, Object> params = new HashMap<>();
params.put("class", checker);
return this.getClient().get(path, params, null);
}

private RestClient getClient() {
return this.client.getHttpClient();
}
}
158 changes: 100 additions & 58 deletions cli/src/main/java/org/apache/uniffle/cli/UniffleCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.uniffle.api.AdminRestApi;
import org.apache.uniffle.client.UniffleRestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,69 +30,109 @@

public class UniffleCLI extends AbstractCustomCommandLine {

private static final Logger LOG = LoggerFactory.getLogger(UniffleCLI.class);
private final Options allOptions;
private final Option uniffleClientCli;
private final Option uniffleAdminCli;
private final Option help;

public UniffleCLI(String shortPrefix, String longPrefix) {
allOptions = new Options();
uniffleClientCli = new Option(shortPrefix + "c", longPrefix + "cli",
true, "This is an client cli command that will print args.");
uniffleAdminCli = new Option(shortPrefix + "a", longPrefix + "admin",
true, "This is an admin command that will print args.");
help = new Option(shortPrefix + "h", longPrefix + "help",
false, "Help for the Uniffle CLI.");
allOptions.addOption(uniffleClientCli);
allOptions.addOption(uniffleAdminCli);
allOptions.addOption(help);
}

public int run(String[] args) throws UniffleCliArgsException {
final CommandLine cmd = parseCommandLineOptions(args, true);

if (cmd.hasOption(help.getOpt())) {
printUsage();
return 0;
private static final Logger LOG = LoggerFactory.getLogger(UniffleCLI.class);
private final Options allOptions;
private final Option uniffleClientCli;
private final Option uniffleAdminCli;
private final Option checkerClass;
private final Option help;
private final Option coordServer;
private final Option coordPort;
private final Option refreshAccessCli;
protected UniffleRestClient client;

public UniffleCLI(String shortPrefix, String longPrefix) {
allOptions = new Options();
uniffleClientCli = new Option(shortPrefix + "c", longPrefix + "cli",
true, "This is an client cli command that will print args.");
uniffleAdminCli = new Option(shortPrefix + "a", longPrefix + "admin",
true, "This is an admin command that will print args.");
refreshAccessCli = new Option(shortPrefix + "rc", longPrefix + "refreshChecker",
false, "This is an admin command that will refresh access checker.");
checkerClass = new Option(shortPrefix + "ac", longPrefix + "checkerClass",
true, "This is an admin command that will refresh access checker.");
help = new Option(shortPrefix + "h", longPrefix + "help",
false, "Help for the Uniffle CLI.");
coordServer = new Option(shortPrefix + "s", longPrefix + "host",
true, "This is coordinator server host.");
coordPort = new Option(shortPrefix + "p", longPrefix + "port",
true, "This is coordinator server port.");
allOptions.addOption(uniffleClientCli);
allOptions.addOption(uniffleAdminCli);
allOptions.addOption(coordServer);
allOptions.addOption(coordPort);
allOptions.addOption(refreshAccessCli);
allOptions.addOption(help);
}

public int run(String[] args) throws UniffleCliArgsException {
final CommandLine cmd = parseCommandLineOptions(args, true);

if (cmd.hasOption(help.getOpt())) {
printUsage();
return 0;
}

if (cmd.hasOption(uniffleClientCli.getOpt())) {
String cliArgs = cmd.getOptionValue(uniffleClientCli.getOpt());
System.out.println("uniffle-client-cli : " + cliArgs);
return 0;
}

if (cmd.hasOption(uniffleAdminCli.getOpt())) {
String cliArgs = cmd.getOptionValue(uniffleAdminCli.getOpt());
System.out.println("uniffle-admin-cli : " + cliArgs);
return 0;
}
if (cmd.hasOption(coordServer.getOpt()) && cmd.hasOption(coordPort.getOpt())) {
String host = cmd.getOptionValue(coordServer.getOpt()).trim();
int port = Integer.parseInt(cmd.getOptionValue(coordPort.getOpt()).trim());
String hostUrl = String.format("http://%s:%d", host, port);
client = UniffleRestClient.builder(hostUrl).build();
}

if (cmd.hasOption(refreshAccessCli.getOpt())) {
String checker = cmd.getOptionValue(checkerClass.getOpt());
LOG.info(refreshAccessChecker(checker));
return 0;
}

return 1;
}

private String refreshAccessChecker(String checker) throws UniffleCliArgsException {
if (client == null) {
throw new UniffleCliArgsException("Missing Coordinator host address and grpc port parameters.");
}
AdminRestApi adminRestApi = new AdminRestApi(client);
return adminRestApi.refreshAccessChecker(checker);
}

if (cmd.hasOption(uniffleClientCli.getOpt())) {
String cliArgs = cmd.getOptionValue(uniffleClientCli.getOpt());
System.out.println("uniffle-client-cli : " + cliArgs);
return 0;
@Override
public void addRunOptions(Options baseOptions) {
baseOptions.addOption(uniffleClientCli);
baseOptions.addOption(uniffleAdminCli);
baseOptions.addOption(refreshAccessCli);
baseOptions.addOption(checkerClass);
baseOptions.addOption(coordServer);
baseOptions.addOption(coordPort);
}

if (cmd.hasOption(uniffleAdminCli.getOpt())) {
String cliArgs = cmd.getOptionValue(uniffleAdminCli.getOpt());
System.out.println("uniffle-admin-cli : " + cliArgs);
return 0;
@Override
public void addGeneralOptions(Options baseOptions) {
baseOptions.addOption(help);
}

return 1;
}

@Override
public void addRunOptions(Options baseOptions) {
baseOptions.addOption(uniffleClientCli);
baseOptions.addOption(uniffleAdminCli);
}

@Override
public void addGeneralOptions(Options baseOptions) {
baseOptions.addOption(help);
}

public static void main(String[] args) {
int retCode;
try {
final UniffleCLI cli = new UniffleCLI("", "");
retCode = cli.run(args);
} catch (UniffleCliArgsException e) {
retCode = AbstractCustomCommandLine.handleCliArgsException(e, LOG);
} catch (Exception e) {
retCode = AbstractCustomCommandLine.handleError(e, LOG);
public static void main(String[] args) {
int retCode;
try {
final UniffleCLI cli = new UniffleCLI("", "");
retCode = cli.run(args);
} catch (UniffleCliArgsException e) {
retCode = AbstractCustomCommandLine.handleCliArgsException(e, LOG);
} catch (Exception e) {
retCode = AbstractCustomCommandLine.handleError(e, LOG);
}
System.exit(retCode);
}
System.exit(retCode);
}
}
58 changes: 58 additions & 0 deletions cli/src/main/java/org/apache/uniffle/client/HttpClientFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.uniffle.client;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.uniffle.client.exception.UniffleRestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;

public class HttpClientFactory {
private static final Logger LOG = LoggerFactory.getLogger(HttpClientFactory.class);

public static CloseableHttpClient createHttpClient(RestClientConf conf) {
RequestConfig requestConfig =
RequestConfig.custom()
.setSocketTimeout(conf.getSocketTimeout())
.setConnectTimeout(conf.getConnectTimeout())
.build();
SSLConnectionSocketFactory sslSocketFactory;
try {
TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
SSLContext sslContext =
SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
} catch (Exception e) {
LOG.error("Error: ", e);
throw new UniffleRestException("Failed to create HttpClient", e);
}

return HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(sslSocketFactory)
.build();
}
}
28 changes: 28 additions & 0 deletions cli/src/main/java/org/apache/uniffle/client/RestClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.uniffle.client;

import java.util.Map;

/** A underlying http client interface for common rest request. */
public interface RestClient extends AutoCloseable {
<T> T get(String path, Map<String, Object> params, Class<T> type, String authHeader);

String get(String path, Map<String, Object> params, String authHeader);

}
57 changes: 57 additions & 0 deletions cli/src/main/java/org/apache/uniffle/client/RestClientConf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.uniffle.client;

public class RestClientConf {
private int maxAttempts;
private int attemptWaitTime;
private int socketTimeout;
private int connectTimeout;

public int getMaxAttempts() {
return maxAttempts;
}

public void setMaxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
}

public int getAttemptWaitTime() {
return attemptWaitTime;
}

public void setAttemptWaitTime(int attemptWaitTime) {
this.attemptWaitTime = attemptWaitTime;
}

public int getSocketTimeout() {
return socketTimeout;
}

public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}

public int getConnectTimeout() {
return connectTimeout;
}

public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
}

0 comments on commit 74fcb9b

Please sign in to comment.