TLSExample.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package io.milvus;
  20. import com.alibaba.fastjson.JSONObject;
  21. import io.milvus.client.MilvusServiceClient;
  22. import io.milvus.grpc.*;
  23. import io.milvus.param.*;
  24. import io.milvus.param.collection.*;
  25. import io.milvus.param.dml.*;
  26. import io.milvus.param.index.*;
  27. import io.milvus.response.*;
  28. import java.util.*;
  29. // Note: read the following description before running this example
  30. // 1. cmd into the "tls" folder, generate certificate by the following commands.
  31. // (more details read the https://milvus.io/docs/tls.md)
  32. // chmod +x gen.sh
  33. // ./gen.sh
  34. //
  35. // 2. Configure the file paths of server.pem, server.key, and ca.pem for the server in config/milvus.yaml.
  36. // Set tlsMode to 1 for one-way authentication. Set tlsMode to 2 for two-way authentication.
  37. // (read the doc to know how to config milvus: https://milvus.io/docs/configure-docker.md)
  38. // tls:
  39. // serverPemPath: [path_to_tls]/tls/server.pem
  40. // serverKeyPath: [path_to_tls]/tls/server.key
  41. // caPemPath: [path_to_tls]/tls/ca.pem
  42. //
  43. // common:
  44. // security:
  45. // tlsMode: 2
  46. //
  47. // 3. Start milvus server
  48. // 4. Run this example.
  49. // Connect server by oneWayAuth() if the server tlsMode=1, connect server by twoWayAuth() if the server tlsMode=2.
  50. //
  51. public class TLSExample {
  52. private static void oneWayAuth() {
  53. String path = ClassLoader.getSystemResource("").getPath();
  54. ConnectParam connectParam = ConnectParam.newBuilder()
  55. .withHost("localhost")
  56. .withPort(19530)
  57. .withServerName("localhost")
  58. .withServerPemPath(path + "/tls/server.pem")
  59. .build();
  60. MilvusServiceClient milvusClient = new MilvusServiceClient(connectParam);
  61. R<CheckHealthResponse> health = milvusClient.checkHealth();
  62. if (health.getStatus() != R.Status.Success.getCode()) {
  63. throw new RuntimeException(health.getMessage());
  64. } else {
  65. System.out.println(health);
  66. }
  67. }
  68. private static void twoWayAuth() {
  69. String path = ClassLoader.getSystemResource("").getPath();
  70. ConnectParam connectParam = ConnectParam.newBuilder()
  71. .withHost("localhost")
  72. .withPort(19530)
  73. .withServerName("localhost")
  74. .withCaPemPath(path + "/tls/ca.pem")
  75. .withClientKeyPath(path + "/tls/client.key")
  76. .withClientPemPath(path + "/tls/client.pem")
  77. .build();
  78. MilvusServiceClient milvusClient = new MilvusServiceClient(connectParam);
  79. R<CheckHealthResponse> health = milvusClient.checkHealth();
  80. if (health.getStatus() != R.Status.Success.getCode()) {
  81. throw new RuntimeException(health.getMessage());
  82. } else {
  83. System.out.println(health);
  84. }
  85. }
  86. // tlsMode=1, set oneWay=true
  87. // tlsMode=2, set oneWay=false
  88. private static final boolean oneWay = false;
  89. public static void main(String[] args) {
  90. if (oneWay) {
  91. oneWayAuth();
  92. } else {
  93. twoWayAuth();
  94. }
  95. }
  96. }