TLSExample.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.v1;
  20. import io.milvus.client.MilvusServiceClient;
  21. import io.milvus.grpc.CheckHealthResponse;
  22. import io.milvus.param.ConnectParam;
  23. import io.milvus.param.R;
  24. import java.io.File;
  25. import java.net.URL;
  26. // Note: read the following description before running this example
  27. // 1. cmd into the "tls" folder, generate certificate by the following commands.
  28. // (more details read the https://milvus.io/docs/tls.md)
  29. // chmod +x gen.sh
  30. // ./gen.sh
  31. //
  32. // 2. Configure the file paths of server.pem, server.key, and ca.pem for the server in config/milvus.yaml.
  33. // Set tlsMode to 1 for one-way authentication. Set tlsMode to 2 for two-way authentication.
  34. // (read the doc to know how to config milvus: https://milvus.io/docs/configure-docker.md)
  35. // tls:
  36. // serverPemPath: [path_to_tls]/tls/server.pem
  37. // serverKeyPath: [path_to_tls]/tls/server.key
  38. // caPemPath: [path_to_tls]/tls/ca.pem
  39. //
  40. // common:
  41. // security:
  42. // tlsMode: 2
  43. //
  44. // 3. Start milvus server
  45. // 4. Run this example.
  46. // Connect server by oneWayAuth() if the server tlsMode=1, connect server by twoWayAuth() if the server tlsMode=2.
  47. //
  48. public class TLSExample {
  49. private static void oneWayAuth() {
  50. ClassLoader classLoader = BulkWriterExample.class.getClassLoader();
  51. URL resourceUrl = classLoader.getResource("tls");
  52. String path = new File(resourceUrl.getFile()).getAbsolutePath();
  53. ConnectParam connectParam = ConnectParam.newBuilder()
  54. .withHost("localhost")
  55. .withPort(19530)
  56. .withServerName("localhost")
  57. .withServerPemPath(path + "/server.pem")
  58. .build();
  59. MilvusServiceClient milvusClient = new MilvusServiceClient(connectParam);
  60. R<CheckHealthResponse> health = milvusClient.checkHealth();
  61. if (health.getStatus() != R.Status.Success.getCode()) {
  62. throw new RuntimeException(health.getMessage());
  63. } else {
  64. System.out.println(health);
  65. }
  66. }
  67. private static void twoWayAuth() {
  68. ClassLoader classLoader = BulkWriterExample.class.getClassLoader();
  69. URL resourceUrl = classLoader.getResource("tls");
  70. String path = new File(resourceUrl.getFile()).getAbsolutePath();
  71. ConnectParam connectParam = ConnectParam.newBuilder()
  72. .withHost("localhost")
  73. .withPort(19530)
  74. .withServerName("localhost")
  75. .withCaPemPath(path + "/ca.pem")
  76. .withClientKeyPath(path + "/client.key")
  77. .withClientPemPath(path + "/client.pem")
  78. .build();
  79. MilvusServiceClient milvusClient = new MilvusServiceClient(connectParam);
  80. R<CheckHealthResponse> health = milvusClient.checkHealth();
  81. if (health.getStatus() != R.Status.Success.getCode()) {
  82. throw new RuntimeException(health.getMessage());
  83. } else {
  84. System.out.println(health);
  85. }
  86. }
  87. // tlsMode=1, set oneWay=true
  88. // tlsMode=2, set oneWay=false
  89. private static final boolean oneWay = false;
  90. public static void main(String[] args) {
  91. if (oneWay) {
  92. oneWayAuth();
  93. } else {
  94. twoWayAuth();
  95. }
  96. }
  97. }