|
1 |
| -:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/master |
2 |
| - |
3 |
| -:github: https://github.com/elastic/elasticsearch-net |
4 |
| - |
5 |
| -:nuget: https://www.nuget.org/packages |
6 |
| - |
7 |
| -//// |
| 1 | +:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/master |
| 2 | + |
| 3 | +:github: https://github.com/elastic/elasticsearch-net |
| 4 | + |
| 5 | +:nuget: https://www.nuget.org/packages |
| 6 | + |
| 7 | +//// |
8 | 8 | IMPORTANT NOTE
|
9 | 9 | ==============
|
10 | 10 | This file has been generated from https://github.com/elastic/elasticsearch-net/tree/master/src/Tests/Tests/ClientConcepts/Certificates/WorkingWithCertificates.doc.cs.
|
11 | 11 | If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,
|
12 |
| -please modify the original csharp file found at the link and submit the PR with that change. Thanks! |
13 |
| -//// |
14 |
| - |
15 |
| -[[working-with-certificates]] |
16 |
| -=== Working with certificates |
17 |
| - |
18 |
| -If you've enabled SSL on Elasticsearch with https://www.elastic.co/products/elastic-stack[Elastic Stack Security features], or through a |
19 |
| -proxy in front of Elasticsearch, and the Certificate Authority (CA) |
20 |
| -that generated the certificate is trusted by the machine running the client code, there should be nothing for you to do to talk |
21 |
| -to the cluster over HTTPS with the client. |
22 |
| - |
23 |
| -If you are using your own CA which is not trusted however, .NET won't allow you to make HTTPS calls to that endpoint by default. |
24 |
| -With .NET Framework, you can pre-empt this though a custom validation callback on the global static |
25 |
| -`ServicePointManager.ServerCertificateValidationCallback`. Most examples you will find doing this this will simply return `true` from the |
26 |
| -validation callback and merrily whistle off into the sunset. **This is not advisable** as it allows *any* HTTPS traffic through in the |
27 |
| -current `AppDomain` *without* any validation. Here's a concrete example: |
28 |
| - |
29 |
| -Imagine you deploy a web application that talks to Elasticsearch over HTTPS using NEST, and also uses some third party SOAP/WSDL endpoint. |
30 |
| -By setting the following |
31 |
| - |
32 |
| -[source,csharp] |
33 |
| ----- |
34 |
| -ServicePointManager.ServerCertificateValidationCallback += |
35 |
| -(sender, cert, chain, errors) => true |
36 |
| ----- |
37 |
| - |
38 |
| -validation will not be performed for HTTPS connections to *both* Elasticsearch *and* that external web service. |
39 |
| - |
40 |
| -==== Validation configuration |
41 |
| - |
42 |
| -It's possible to also set a callback per service endpoint with .NET, and both Elasticsearch.NET and NEST expose this through |
43 |
| -connection settings (`ConnectionConfiguration` with Elasticsearch.Net and `ConnectionSettings` with NEST). You can do |
44 |
| -your own validation in that handler or use one of the baked in handlers that we ship with out of the box, on the static class |
45 |
| -`CertificateValidations`. |
46 |
| - |
47 |
| -The two most basic ones are `AllowAll` and `DenyAll`, which accept or deny all SSL traffic to our nodes, respectively. Here's |
48 |
| -a couple of examples. |
49 |
| - |
50 |
| -===== Denying all certificate validation |
51 |
| - |
52 |
| -Here we set up `ConnectionSettings` with a validation callback that denies all certificate validation |
53 |
| - |
54 |
| -[source,csharp] |
55 |
| ----- |
56 |
| -[IntegrationOnly] |
57 |
| -public class DenyAllCertificatesCluster : SslAndKpiXPackCluster |
58 |
| -{ |
59 |
| - protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) => s |
60 |
| - .ServerCertificateValidationCallback((o, certificate, chain, errors) => false) |
| 12 | +please modify the original csharp file found at the link and submit the PR with that change. Thanks! |
| 13 | +//// |
| 14 | + |
| 15 | +[[working-with-certificates]] |
| 16 | +=== Working with certificates |
| 17 | + |
| 18 | +If you've enabled SSL on Elasticsearch with https://www.elastic.co/products/elastic-stack[Elastic Stack Security features], or through a |
| 19 | +proxy in front of Elasticsearch, and the Certificate Authority (CA) |
| 20 | +that generated the certificate is trusted by the machine running the client code, there should be nothing for you to do to talk |
| 21 | +to the cluster over HTTPS with the client. |
| 22 | + |
| 23 | +If you are using your own CA which is not trusted however, .NET won't allow you to make HTTPS calls to that endpoint by default. |
| 24 | +With .NET Framework, you can pre-empt this though a custom validation callback on the global static |
| 25 | +`ServicePointManager.ServerCertificateValidationCallback`. Most examples you will find doing this this will simply return `true` from the |
| 26 | +validation callback and merrily whistle off into the sunset. **This is not advisable** as it allows *any* HTTPS traffic through in the |
| 27 | +current `AppDomain` *without* any validation. Here's a concrete example: |
| 28 | + |
| 29 | +Imagine you deploy a web application that talks to Elasticsearch over HTTPS using NEST, and also uses some third party SOAP/WSDL endpoint. |
| 30 | +By setting the following |
| 31 | + |
| 32 | +[source,csharp] |
| 33 | +---- |
| 34 | +ServicePointManager.ServerCertificateValidationCallback += |
| 35 | +(sender, cert, chain, errors) => true |
| 36 | +---- |
| 37 | + |
| 38 | +validation will not be performed for HTTPS connections to *both* Elasticsearch *and* that external web service. |
| 39 | + |
| 40 | +==== Validation configuration |
| 41 | + |
| 42 | +It's possible to also set a callback per service endpoint with .NET, and both Elasticsearch.NET and NEST expose this through |
| 43 | +connection settings (`ConnectionConfiguration` with Elasticsearch.Net and `ConnectionSettings` with NEST). You can do |
| 44 | +your own validation in that handler or use one of the baked in handlers that we ship with out of the box, on the static class |
| 45 | +`CertificateValidations`. |
| 46 | + |
| 47 | +The two most basic ones are `AllowAll` and `DenyAll`, which accept or deny all SSL traffic to our nodes, respectively. Here's |
| 48 | +a couple of examples. |
| 49 | + |
| 50 | +===== Denying all certificate validation |
| 51 | + |
| 52 | +Here we set up `ConnectionSettings` with a validation callback that denies all certificate validation |
| 53 | + |
| 54 | +[source,csharp] |
| 55 | +---- |
| 56 | +[IntegrationOnly] |
| 57 | +public class DenyAllCertificatesCluster : SslAndKpiXPackCluster |
| 58 | +{ |
| 59 | + protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) => s |
| 60 | + .ServerCertificateValidationCallback((o, certificate, chain, errors) => false) |
61 | 61 | .ServerCertificateValidationCallback(CertificateValidations.DenyAll); <1>
|
62 |
| -} |
63 |
| ----- |
64 |
| -<1> use a lambda expression or `CertificateValidations.DenyAll` to deny all validation |
65 |
| - |
66 |
| -===== Allowing all certificate validation |
67 |
| - |
68 |
| -Here we set up `ConnectionSettings` with a validation callback that allows all certificate validation |
69 |
| - |
70 |
| -[source,csharp] |
71 |
| ----- |
72 |
| -public class AllowAllCertificatesCluster : SslAndKpiXPackCluster |
73 |
| -{ |
74 |
| - protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) => s |
| 62 | +} |
| 63 | +---- |
| 64 | +<1> use a lambda expression or `CertificateValidations.DenyAll` to deny all validation |
| 65 | + |
| 66 | +===== Allowing all certificate validation |
| 67 | + |
| 68 | +Here we set up `ConnectionSettings` with a validation callback that allows all certificate validation |
| 69 | + |
| 70 | +[source,csharp] |
| 71 | +---- |
| 72 | +public class AllowAllCertificatesCluster : SslAndKpiXPackCluster |
| 73 | +{ |
| 74 | + protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) => s |
75 | 75 | .ServerCertificateValidationCallback((o, certificate, chain, errors) => true) <1>
|
76 | 76 | .ServerCertificateValidationCallback(CertificateValidations.AllowAll); <1>
|
77 |
| -} |
78 |
| ----- |
79 |
| -<1> use a lambda expression or `CertificateValidations.AllowAll` to allow all validation |
80 |
| - |
81 |
| -===== Allowing certificates from a Certificate Authority |
82 |
| - |
83 |
| -If your client application has access to the public CA certificate locally, Elasticsearch.NET and NEST ship with some handy helpers |
84 |
| -that can assert that a certificate the server presents is one that came from the local CA. |
85 |
| - |
86 |
| -If you use {ref_current}/certutil.html[`elasticsearch-certutil` tool] to generate SSL certificates, the generated node certificate |
87 |
| -does not include the CA in the certificate chain, in order to cut down on SSL handshake size. In those case you can use |
88 |
| -`CertificateValidations.AuthorityIsRoot` and pass it your local copy of the CA public key to assert that |
89 |
| -the certificate the server presented was generated using it |
90 |
| - |
91 |
| -[source,csharp] |
92 |
| ----- |
93 |
| -[IntegrationOnly] |
94 |
| -public class CertgenCaCluster : SslAndKpiXPackCluster |
95 |
| -{ |
96 |
| - public CertgenCaCluster() : base() { } |
97 |
| - public CertgenCaCluster(SslAndKpiClusterConfiguration configuration) : base(configuration) { } |
98 |
| - protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) => s |
99 |
| - .ServerCertificateValidationCallback( |
100 |
| - CertificateValidations.AuthorityIsRoot(new X509Certificate(this.ClusterConfiguration.FileSystem.CaCertificate)) |
101 |
| - ); |
102 |
| -} |
103 |
| ----- |
104 |
| - |
105 |
| -If your local copy does not match the server's CA, the client will fail to connect |
106 |
| - |
107 |
| -[source,csharp] |
108 |
| ----- |
109 |
| -[IntegrationOnly] |
110 |
| -public class BadCertgenCaCluster : SslAndKpiXPackCluster |
111 |
| -{ |
112 |
| - protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) => s |
113 |
| - .ServerCertificateValidationCallback( |
114 |
| - CertificateValidations.AuthorityPartOfChain(new X509Certificate(this.ClusterConfiguration.FileSystem.UnusedCaCertificate)) |
115 |
| - ); |
116 |
| -} |
117 |
| ----- |
118 |
| - |
119 |
| -If you go for a vendor generated SSL certificate, it's common practice for the certificate to include the CA _and_ any intermediary CAs |
120 |
| -in the certificate chain. When using such a certificate, use `CertificateValidations.AuthorityPartOfChain` which validates that |
121 |
| -the local CA certificate is part of the chain that was used to generate the servers key. |
122 |
| - |
| 77 | +} |
| 78 | +---- |
| 79 | +<1> use a lambda expression or `CertificateValidations.AllowAll` to allow all validation |
| 80 | + |
| 81 | +===== Allowing certificates from a Certificate Authority |
| 82 | + |
| 83 | +If your client application has access to the public CA certificate locally, Elasticsearch.NET and NEST ship with some handy helpers |
| 84 | +that can assert that a certificate the server presents is one that came from the local CA. |
| 85 | + |
| 86 | +If you use {ref_current}/certutil.html[`elasticsearch-certutil` tool] to generate SSL certificates, the generated node certificate |
| 87 | +does not include the CA in the certificate chain, in order to cut down on SSL handshake size. In those case you can use |
| 88 | +`CertificateValidations.AuthorityIsRoot` and pass it your local copy of the CA public key to assert that |
| 89 | +the certificate the server presented was generated using it |
| 90 | + |
| 91 | +[source,csharp] |
| 92 | +---- |
| 93 | +[IntegrationOnly] |
| 94 | +public class CertgenCaCluster : SslAndKpiXPackCluster |
| 95 | +{ |
| 96 | + public CertgenCaCluster() : base() { } |
| 97 | + public CertgenCaCluster(SslAndKpiClusterConfiguration configuration) : base(configuration) { } |
| 98 | + protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) => s |
| 99 | + .ServerCertificateValidationCallback( |
| 100 | + CertificateValidations.AuthorityIsRoot(new X509Certificate(this.ClusterConfiguration.FileSystem.CaCertificate)) |
| 101 | + ); |
| 102 | +} |
| 103 | +---- |
| 104 | + |
| 105 | +If your local copy does not match the server's CA, the client will fail to connect |
| 106 | + |
| 107 | +[source,csharp] |
| 108 | +---- |
| 109 | +[IntegrationOnly] |
| 110 | +public class BadCertgenCaCluster : SslAndKpiXPackCluster |
| 111 | +{ |
| 112 | + protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) => s |
| 113 | + .ServerCertificateValidationCallback( |
| 114 | + CertificateValidations.AuthorityPartOfChain(new X509Certificate(this.ClusterConfiguration.FileSystem.UnusedCaCertificate)) |
| 115 | + ); |
| 116 | +} |
| 117 | +---- |
| 118 | + |
| 119 | +If you go for a vendor generated SSL certificate, it's common practice for the certificate to include the CA _and_ any intermediary CAs |
| 120 | +in the certificate chain. When using such a certificate, use `CertificateValidations.AuthorityPartOfChain` which validates that |
| 121 | +the local CA certificate is part of the chain that was used to generate the servers key. |
| 122 | + |
0 commit comments