forked from apache/incubator-uniffle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apache#768] feat(cli): Cli method for blacklist update
- Loading branch information
wangjunbo
committed
Jun 17, 2023
1 parent
fb8f4cf
commit 14f44fd
Showing
20 changed files
with
871 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
cli/src/main/java/org/apache/uniffle/api/AdminRestApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.uniffle.client.RestClient; | ||
import org.apache.uniffle.client.UniffleRestClient; | ||
|
||
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 path = String.format("/api/server/%s/%s", API_BASE_PATH, "refreshChecker"); | ||
Map<String, Object> params = new HashMap<>(); | ||
return this.getClient().get(path, params, null); | ||
} | ||
|
||
private RestClient getClient() { | ||
return this.client.getHttpClient(); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
cli/src/main/java/org/apache/uniffle/cli/UniffleAdminCLI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* 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.cli; | ||
|
||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.cli.Option; | ||
import org.apache.commons.cli.Options; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.apache.uniffle.AbstractCustomCommandLine; | ||
import org.apache.uniffle.UniffleCliArgsException; | ||
import org.apache.uniffle.api.AdminRestApi; | ||
import org.apache.uniffle.client.UniffleRestClient; | ||
|
||
public class UniffleAdminCLI extends AbstractCustomCommandLine { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(UniffleAdminCLI.class); | ||
|
||
private final Options allOptions; | ||
private final Option refreshAccessCli; | ||
private final Option coordinatorHost; | ||
private final Option coordPort; | ||
private final Option ssl; | ||
|
||
private final Option help; | ||
protected UniffleRestClient client; | ||
|
||
public UniffleAdminCLI(String shortPrefix, String longPrefix) { | ||
allOptions = new Options(); | ||
refreshAccessCli = new Option(shortPrefix + "r", longPrefix + "refreshChecker", | ||
false, "This is an admin command that will refresh access checker."); | ||
help = new Option(shortPrefix + "h", longPrefix + "help", | ||
false, "Help for the Uniffle Admin CLI."); | ||
coordinatorHost = new Option(shortPrefix + "s", longPrefix + "coordinatorHost", | ||
true, "This is coordinator server host."); | ||
coordPort = new Option(shortPrefix + "p", longPrefix + "port", | ||
true, "This is coordinator server port."); | ||
ssl = new Option(null, longPrefix + "ssl", false, "use SSL"); | ||
|
||
allOptions.addOption(refreshAccessCli); | ||
allOptions.addOption(coordinatorHost); | ||
allOptions.addOption(coordPort); | ||
allOptions.addOption(ssl); | ||
allOptions.addOption(help); | ||
} | ||
|
||
public UniffleAdminCLI(String shortPrefix, String longPrefix, UniffleRestClient client) { | ||
this(shortPrefix, longPrefix); | ||
this.client = client; | ||
} | ||
|
||
public int run(String[] args) throws UniffleCliArgsException { | ||
final CommandLine cmd = parseCommandLineOptions(args, true); | ||
|
||
if (cmd.hasOption(help.getOpt())) { | ||
printUsage(); | ||
return 0; | ||
} | ||
|
||
if (cmd.hasOption(coordinatorHost.getOpt()) && cmd.hasOption(coordPort.getOpt())) { | ||
String host = cmd.getOptionValue(coordinatorHost.getOpt()).trim(); | ||
int port = Integer.parseInt(cmd.getOptionValue(coordPort.getOpt()).trim()); | ||
String hostUrl; | ||
if (cmd.hasOption(ssl.getOpt())) { | ||
hostUrl = String.format("https://%s:%d", host, port); | ||
} else { | ||
hostUrl = String.format("http://%s:%d", host, port); | ||
} | ||
LOG.info("connected to coordinator: {}.", hostUrl); | ||
client = UniffleRestClient.builder(hostUrl).build(); | ||
} | ||
|
||
if (cmd.hasOption(refreshAccessCli.getOpt())) { | ||
LOG.info("uniffle-admin-cli : refresh coordinator access checker!"); | ||
refreshAccessChecker(); | ||
return 0; | ||
} | ||
return 1; | ||
} | ||
|
||
private String refreshAccessChecker() throws UniffleCliArgsException { | ||
if (client == null) { | ||
throw new UniffleCliArgsException("Missing Coordinator host address and grpc port parameters."); | ||
} | ||
AdminRestApi adminRestApi = new AdminRestApi(client); | ||
return adminRestApi.refreshAccessChecker(); | ||
} | ||
|
||
@Override | ||
public void addRunOptions(Options baseOptions) { | ||
baseOptions.addOption(refreshAccessCli); | ||
baseOptions.addOption(coordinatorHost); | ||
baseOptions.addOption(coordPort); | ||
} | ||
|
||
@Override | ||
public void addGeneralOptions(Options baseOptions) { | ||
baseOptions.addOption(help); | ||
} | ||
|
||
public static void main(String[] args) { | ||
int retCode; | ||
try { | ||
final UniffleAdminCLI cli = new UniffleAdminCLI("", ""); | ||
retCode = cli.run(args); | ||
} catch (UniffleCliArgsException e) { | ||
retCode = AbstractCustomCommandLine.handleCliArgsException(e, LOG); | ||
} catch (Exception e) { | ||
retCode = AbstractCustomCommandLine.handleError(e, LOG); | ||
} | ||
System.exit(retCode); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
cli/src/main/java/org/apache/uniffle/client/HttpClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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 javax.net.ssl.SSLContext; | ||
|
||
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.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.apache.uniffle.client.exception.UniffleRestException; | ||
|
||
|
||
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(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
cli/src/main/java/org/apache/uniffle/client/RestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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, Cloneable { | ||
|
||
String get(String path, Map<String, Object> params, String authHeader); | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
cli/src/main/java/org/apache/uniffle/client/RestClientConf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.