Easysearch との安全な接続:Java クライアントによる HTTPS 接続設定

はじめに

INFINI Easysearch は、ユーザビリティの向上を常に追求しています。このコンセプトに基づき、公式 Java クライアントがリリースされました。**easysearch-client:1.0.1** の登場により、開発者は Java アプリケーションから Easysearch クラスターとより直感的に連携できるようになりました。

このクライアントにより、開発者は従来の HTTP リクエストや JSON 処理に依存することなく、ネイティブな Java メソッドとデータ構造で操作が可能になります。これにより、データ管理とインデックス作成の効率が大幅に向上します。Java クライアントは、データ操作、クラスタ管理、セキュリティモジュールとの完全互換性を提供し、ロール、ユーザー、権限、ロールマッピング、アカウント管理のための豊富な API を実装しています。

Easysearch サーバーのセットアップ

まず、SSL 証明書で保護された Easysearch サーバーを設定します。既存のサーバーをお持ちの場合は、このステップをスキップしてください。

1. Easysearch のダウンロードとインストール

最新版の Easysearch を公式サイトからダウンロードします。ここではバージョン 1.6.1 を使用します。

wget https://dl-global.infinilabs.com/easysearch/stable/easysearch-1.6.1-214-mac-amd64.zip

システムに Java 11 以上がインストールされていることを確認してください。

unzip easysearch-1.6.1-214-mac-amd64.zip -d easysearch-1.6.1

2. 証明書の生成とプラグインのインストール

初期化スクリプトを実行して証明書を生成し、必要なプラグインをインストールします。

cd easysearch-1.6.1
bin/initialize.sh

スクリプトの実行後、ランダムに生成された管理者パスワードが表示されます。

3. Easysearch サーバーの起動

以下のコマンドでサーバーを起動します。

bin/easysearch

サーバーが正常に起動したか確認するには、ログファイルに表示された curl コマンドを使用します。

curl -ku admin:xxxxxxxxx https://localhost:9200

正常な応答として以下のような情報が表示されます。

{
  "name" : "MacBook-Pro.local",
  "cluster_name" : "easysearch",
  "cluster_uuid" : "1gRYQ6ssTiKGqcyuEN0Dbg",
  "version" : {
    "distribution" : "easysearch",
    "number" : "1.6.1",
    "distributor" : "INFINI Labs",
    "build_hash" : "14846e460e9976ba6d68c80bb9eca52af1179dcf",
    "build_date" : "2023-10-19T14:43:02.636639Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.2",
    "minimum_wire_lucene_version" : "7.7.0",
    "minimum_lucene_index_compatibility_version" : "7.7.0"
  },
  "tagline" : "You Know, For Easy Search!"
}

Java クライアントの設定

Easysearch Java クライアントは Maven Central リポジトリで利用可能です。Java アプリケーションに依存関係として追加します。

1. Gradle ビルドシステムでの依存関係設定

プロジェクトの build.gradle ファイルに以下の依存関係を追加します。

dependencies {
    implementation 'com.infinilabs:easysearch-client:1.0.1'
    implementation "org.apache.logging.log4j:log4j-api:2.19.0"
    implementation "org.apache.logging.log4j:log4j-core:2.19.0"
    implementation 'org.apache.httpcomponents:httpclient:4.5.10'
    implementation 'org.apache.httpcomponents:httpcore-nio:4.4.12'
    implementation 'org.apache.httpcomponents:httpasyncclient:4.1.4'
    implementation 'joda-time:joda-time:2.10.4'
    implementation ('org.apache.lucene:lucene-core:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-analyzers-common:8.11.2') {
        exclude group: '*', module: '*'
    }
    // ... 他の Lucene 依存関係 ...
}

2. Maven ビルドシステムでの依存関係設定

プロジェクトの pom.xml ファイルに以下の依存関係を追加します。

<dependencies>
    <dependency>
        <groupId>com.infinilabs</groupId>
        <artifactId>easysearch-client</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.10</version>
    </dependency>
    
</dependencies>

3. 信頼ストアの設定

Java クライアントを使用するには、サーバー証明書を署名した認証局(CA)の証明書をアプリケーションの信頼ストアに追加する必要があります。easysearch-1.6.1/config/ ディレクトリに ca.crt ファイルが生成されています。

まず、OpenSSL を使用して証明書を PEM 形式から DER 形式に変換します。

openssl x509 -in easysearch-1.6.1/config/ca.crt -inform pem -out ca.der --outform der

次に、Java の keytool を使用してカスタム信頼ストアを作成し、証明書をインポートします。

keytool -import -file ca.der -alias easysearch-trust -keystore customTrustStore

プロンプトが表示されたら、信頼ストアのパスワードを入力します(ここでは例として "securePass" を使用)。

証明書が正常に追加されたことを確認します。

keytool -keystore customTrustStore -storepass securePass -list

4. Java アプリケーションでのクライアント設定

以下に、カスタム信頼ストアを使用して HTTPS 経由で Easysearch クラスターに接続し、インデックスを作成してデータを挿入する例を示します。

// 信頼ストアのシステムプロパティを設定
System.setProperty("javax.net.ssl.trustStore", "/full/path/to/customTrustStore");
System.setProperty("javax.net.ssl.trustStorePassword", "securePass");

// 接続先ホストの設定
HttpHost[] targetHosts = new HttpHost[1];
targetHosts[0] = new HttpHost("search.example.com", 9200, "https");

// 認証情報の設定
AuthProvider authProvider = new BasicCredentialsProvider();
authProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "adminPassword"));

// クライアントの作成
SearchClient searchClient = new SearchClient(SearchClient.builder(targetHosts)
    .setHttpClientConfigCallback((AsyncClientBuilder clientBuilder) -> {
        clientBuilder.setDefaultCredentialsProvider(authProvider);
        return clientBuilder;
    }));

// インデックスの作成
IndexCreationRequest indexRequest = new IndexCreationRequest("product-catalog");
indexRequest.settings(Settings.builder()
    .put("index.shards", 1)
    .put("index.replicas", 1)
);

searchClient.indices().create(indexRequest, RequestOptions.DEFAULT);

// バルク操作によるデータ挿入
BulkInsertOperation bulkOperation = new BulkInsertOperation();
for (int i = 0; i < 10; i++) {
    DocumentRequest docRequest = new DocumentRequest("product-catalog")
        .id(Integer.toString(i))
        .source("{\"name\":\"Product " + i + "\", \"price\":" + (i * 10) + "}", JsonContentType.JSON);
    bulkOperation.add(docRequest);
}

BulkResponse bulkResult = searchClient.bulk(bulkOperation, RequestOptions.DEFAULT);
System.out.println(bulkResult.toString());

5. PEM 形式の証明書を使用する代替方法

PEM 形式の証明書が直接利用可能な場合、以下の方法で SSL コンテキストを設定することもできます。

Path certificatePath = Paths.get("/path/to/ca.crt");
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate trustedCertificate;
try (InputStream certStream = Files.newInputStream(certificatePath)) {
    trustedCertificate = certFactory.generateCertificate(certStream);
}

KeyStore trustStore = KeyStore.getInstance("pkcs12");
trustStore.load(null, null);
trustStore.setCertificateEntry("trusted-ca", trustedCertificate);

SSLContextBuilder contextBuilder = SSLContexts.custom()
    .loadTrustMaterial(trustStore, null);
SSLContext sslContext = contextBuilder.build();

// クライアントの作成
SearchClient searchClient = new SearchClient(SearchClient.builder(targetHosts)
    .setHttpClientConfigCallback((AsyncClientBuilder clientBuilder) -> {
        clientBuilder.setDefaultCredentialsProvider(authProvider);
        clientBuilder.setSSLContext(sslContext);
        return clientBuilder;
    }));

これで、Java クライアントを使用して HTTPS 経由で Easysearch クラスターに安全に接続できました。さらに、クライアントには強力なアクセス制御管理 API も実装されています。

Easysearch について

INFINI Easysearch は、分散型のリアルタイム検索・分析エンジンで、コアエンジンはオープンソースの Apache Lucene をベースにしています。Easysearch は、軽量な Elasticsearch の代替ソリューションを目指し、企業向け機能の追加とサポートを継続的に行っています。Elasticsearch と比較して、Easysearch は検索業務の最適化に重点を置き、製品のシンプルさと使いやすさを維持しています。

詳細な公式ドキュメントは https://www.infinilabs.com/docs/latest/easysearch で確認できます。

タグ: Easysearch Javaクライアント HTTPS SSL証明書 検索エンジン

5月13日 19:03 投稿