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

refactor: extract bookkeeper admin common #13

Merged
merged 1 commit into from
Aug 30, 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
@@ -0,0 +1,34 @@
package io.github.protocol.bookkeeper.admin.api;

public class Configuration {
public String host = "localhost";

public int port;

public boolean tlsEnabled;

public TlsConfig tlsConfig;

public Configuration() {
}

public Configuration host(String host) {
this.host = host;
return this;
}

public Configuration port(int port) {
this.port = port;
return this;
}

public Configuration tlsEnabled(boolean tlsEnabled) {
this.tlsEnabled = tlsEnabled;
return this;
}

public Configuration tlsConfig(TlsConfig tlsConfig) {
this.tlsConfig = tlsConfig;
return this;
}
}
22 changes: 22 additions & 0 deletions bookkeeper-admin-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.protocol-laboratory</groupId>
<artifactId>bookkeeper-admin-parent</artifactId>
<version>0.0.3</version>
</parent>

<artifactId>bookkeeper-admin-common</artifactId>

<dependencies>
<dependency>
<groupId>io.github.protocol-laboratory</groupId>
<artifactId>bookkeeper-admin-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
/*
* 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 io.github.protocol.bookkeeper.admin.jdk;
package io.github.protocol.bookkeeper.admin.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package io.github.protocol.bookkeeper.admin.common;
2 changes: 1 addition & 1 deletion bookkeeper-admin-jdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<dependencies>
<dependency>
<groupId>io.github.protocol-laboratory</groupId>
<artifactId>bookkeeper-admin-api</artifactId>
<artifactId>bookkeeper-admin-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.protocol.bookkeeper.admin.jdk;

import com.fasterxml.jackson.core.type.TypeReference;
import io.github.protocol.bookkeeper.admin.common.JacksonService;

import java.io.IOException;
import java.net.http.HttpResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.protocol.bookkeeper.admin.jdk;

import com.fasterxml.jackson.core.type.TypeReference;
import io.github.protocol.bookkeeper.admin.common.JacksonService;

import java.io.IOException;
import java.net.http.HttpResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.protocol.bookkeeper.admin.jdk;

import io.github.protocol.bookkeeper.admin.api.Configuration;

public class BookkeeperAdminBuilderImpl implements BookkeeperAdminBuilder {
private final Configuration conf;

Expand All @@ -14,13 +16,13 @@ public BookkeeperAdmin build() {

@Override
public BookkeeperAdminBuilder host(String host) {
this.conf.setHost(host);
this.conf.host(host);
return this;
}

@Override
public BookkeeperAdminBuilder port(int port) {
this.conf.setPort(port);
this.conf.port(port);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.protocol.bookkeeper.admin.jdk;

import io.github.protocol.bookkeeper.admin.api.Configuration;

public class BookkeeperAdminImpl implements BookkeeperAdmin {
private final BookiesImpl bookies;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.protocol.bookkeeper.admin.jdk;


import io.github.protocol.bookkeeper.admin.common.JacksonService;

import java.io.IOException;
import java.net.http.HttpResponse;
import java.util.Map;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.protocol.bookkeeper.admin.jdk;

import com.fasterxml.jackson.core.JsonProcessingException;
import io.github.protocol.bookkeeper.admin.api.Configuration;
import io.github.protocol.bookkeeper.admin.common.JacksonService;

import java.io.IOException;
import java.net.URI;
Expand All @@ -22,7 +24,7 @@ public InnerHttpClient(Configuration conf) {
this.client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build();
this.httpPrefix = "http://" + conf.getHost() + ":" + conf.getPort();
this.httpPrefix = "http://" + conf.host + ":" + conf.port;
}

public HttpResponse<String> get(String urlSuffix) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.protocol.bookkeeper.admin.jdk;

import io.github.embedded.bookkeeper.core.EmbeddedBookkeeperServer;
import io.github.protocol.bookkeeper.admin.api.Configuration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -18,8 +19,8 @@ public class AutoRecoveryImplTest {
public static void setup() throws Exception {
SERVER.start();
Configuration conf = new Configuration();
conf.setHost("localhost");
conf.setPort(SERVER.getBkWebPort());
conf.host("localhost");
conf.port(SERVER.getBkWebPort());
autoRecovery = new AutoRecoveryImpl(new InnerHttpClient(conf));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.protocol.bookkeeper.admin.jdk;

import io.github.embedded.bookkeeper.core.EmbeddedBookkeeperServer;
import io.github.protocol.bookkeeper.admin.api.Configuration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -19,8 +20,8 @@ public class BookiesImplTest {
public static void setup() throws Exception {
SERVER.start();
Configuration conf = new Configuration();
conf.setHost("localhost");
conf.setPort(SERVER.getBkWebPort());
conf.host("localhost");
conf.port(SERVER.getBkWebPort());
bookiesImpl = new BookiesImpl(new InnerHttpClient(conf));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.protocol.bookkeeper.admin.jdk;

import io.github.embedded.bookkeeper.core.EmbeddedBookkeeperServer;
import io.github.protocol.bookkeeper.admin.api.Configuration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -20,8 +21,8 @@ public class ConfigsImplTest {
public static void setup() throws Exception {
SERVER.start();
Configuration conf = new Configuration();
conf.setHost("localhost");
conf.setPort(SERVER.getBkWebPort());
conf.host("localhost");
conf.port(SERVER.getBkWebPort());
configsImpl = new ConfigsImpl(new InnerHttpClient(conf));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<version>0.0.3</version>
</parent>

<artifactId>bookkeeper-admin-reactor</artifactId>
<artifactId>bookkeeper-admin-reactive</artifactId>

<dependencies>
<dependency>
<groupId>io.github.protocol-laboratory</groupId>
<artifactId>bookkeeper-admin-api</artifactId>
<artifactId>bookkeeper-admin-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.protocol.bookkeeper.admin.reactive;

import io.github.protocol.bookkeeper.admin.api.Configuration;
import io.netty.handler.ssl.SslContext;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClientSecurityUtils;

public class InnerReactiveClient {
private final HttpClient httpClient;

private final String httpPrefix;

public InnerReactiveClient(Configuration conf) {
HttpClient client = HttpClient.create();

if (conf.tlsEnabled) {
client = client.secure(spec -> {
SslContext context = SslContextUtil.build(conf.tlsConfig);
if (conf.tlsConfig.hostnameVerifyDisabled) {
spec.sslContext(context)
.handlerConfigurator(HttpClientSecurityUtils.HOSTNAME_VERIFICATION_CONFIGURER);
} else {
spec.sslContext(context);
}
});
this.httpPrefix = "https://" + conf.host + ":" + conf.port;
} else {
this.httpPrefix = "http://" + conf.host + ":" + conf.port;
}

this.httpClient = client;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.github.protocol.bookkeeper.admin.reactive;

import io.github.protocol.bookkeeper.admin.api.TlsConfig;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

import java.io.FileInputStream;
import java.security.KeyStore;
import java.util.Arrays;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;

public class SslContextUtil {

public static SslContext build(TlsConfig config) {
try {
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();

if (config.keyStorePath != null && config.keyStorePassword != null) {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
try (FileInputStream keyStoreInputStream = new FileInputStream(config.keyStorePath)) {
keyStore.load(keyStoreInputStream, config.keyStorePassword);
}
String defaultKeyAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(defaultKeyAlgorithm);
keyManagerFactory.init(keyStore, config.keyStorePassword);
sslContextBuilder.keyManager(keyManagerFactory);
}

if (config.verifyDisabled) {
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
} else if (config.trustStorePath != null && config.trustStorePassword != null) {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
try (FileInputStream trustStoreInputStream = new FileInputStream(config.trustStorePath)) {
trustStore.load(trustStoreInputStream, config.trustStorePassword);
}
String defaultTrustAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(defaultTrustAlgorithm);
trustManagerFactory.init(trustStore);
sslContextBuilder.trustManager(trustManagerFactory);
}

if (config.versions != null) {
sslContextBuilder.protocols(config.versions);
}

if (config.cipherSuites != null) {
sslContextBuilder.ciphers(Arrays.asList(config.cipherSuites));
}

return sslContextBuilder.build();
} catch (Exception e) {
throw new RuntimeException("Error setting up SSL configuration", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package io.github.protocol.bookkeeper.admin.reactive;

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading