-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityOptions.cs
More file actions
172 lines (155 loc) · 6.08 KB
/
SecurityOptions.cs
File metadata and controls
172 lines (155 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#region License
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2025 Couchbase, Inc.
*
* Licensed 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.
*
* ************************************************************/
#endregion
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using Couchbase.AnalyticsClient.Certificates;
namespace Couchbase.AnalyticsClient.Options;
public record SecurityOptions
{
/// <summary>
/// The certificate trust mode that determines which certificates to trust.
/// </summary>
internal CertificateTrustMode TrustMode { get; init; } = CertificateTrustMode.Default;
/// <summary>
/// Path to the PEM file (used when TrustMode is PemFile).
/// </summary>
internal string? PemFilePath { get; init; }
/// <summary>
/// PEM-encoded certificate string (used when TrustMode is PemString).
/// </summary>
internal string? PemString { get; init; }
/// <summary>
/// Certificate collection (used when TrustMode is Certificates).
/// </summary>
internal X509Certificate2Collection? Certificates { get; init; }
/// <summary>
/// If true, the SDK trusts ANY certificate regardless of validity.
/// </summary>
/// <remarks>The default is false. Use with caution in production environments.</remarks>
internal bool DisableServerCertificateValidation { get; init; }
/// <summary>
/// SSL protocols the SDK is allowed to use when negotiating TLS settings.
/// </summary>
internal SslProtocols SslProtocols { get; init; } = SslProtocols.Tls13 | SslProtocols.Tls12;
/// <summary>
/// The certificate revocation checking mode used during chain validation.
/// </summary>
/// <remarks>
/// The default is <see cref="X509RevocationMode.NoCheck"/> because custom and private CAs
/// typically do not provide CRL distribution points or OCSP responders. Set to
/// <see cref="X509RevocationMode.Online"/> or <see cref="X509RevocationMode.Offline"/>
/// if your CA infrastructure supports revocation checking.
/// </remarks>
internal X509RevocationMode RevocationMode { get; init; } = X509RevocationMode.NoCheck;
internal string? PathToPemFileValue => PemFilePath;
internal string? CertificateValue => PemString;
internal X509Certificate2Collection? CertificatesValue => Certificates;
/// <summary>
/// Clears any existing trust settings, and tells the SDK
/// to trust only the Capella CA certificate(s) bundled with
/// the SDK.
/// </summary>
public SecurityOptions WithTrustOnlyCapella()
{
return this with
{
TrustMode = CertificateTrustMode.CapellaOnly,
PemFilePath = null,
PemString = null,
Certificates = null
};
}
/// <summary>
/// Clears any existing trust settings, and tells the SDK to
/// trust only the PEM-encoded certificate(s) in the file at
/// the given FS path.
/// </summary>
public SecurityOptions WithTrustOnlyPemFile(string pathToPemFile)
{
return this with
{
TrustMode = CertificateTrustMode.PemFilePath,
PemFilePath = pathToPemFile,
PemString = null,
Certificates = null
};
}
/// <summary>
/// Clears any existing trust settings, and tells the SDK to
/// trust only the PEM-encoded certificate(s) in the given
/// string.
/// </summary>
public SecurityOptions WithTrustOnlyPemString(string certificate)
{
return this with
{
TrustMode = CertificateTrustMode.PemString,
PemString = certificate,
PemFilePath = null,
Certificates = null
};
}
/// <summary>
/// Clears any existing trust settings, and tells the SDK to
/// trust only the specified certificates.
/// </summary>
public SecurityOptions WithTrustOnlyCertificates(X509Certificate2Collection certificates)
{
return this with
{
TrustMode = CertificateTrustMode.CertificatesOnly,
Certificates = certificates,
PemFilePath = null,
PemString = null
};
}
/// <summary>
/// If true, the SDK trusts ANY certificate regardless of
/// validity.
/// Impl Note: This is a separate property because a user
/// should be able to set this to false and then back to
/// true without losing the previous certificate trust
/// settings.
/// <remarks>The default is false. If disabled an error will be logged.</remarks>
/// </summary>
public SecurityOptions WithDisableCertificateVerification(bool disable = false)
{
return this with { DisableServerCertificateValidation = disable };
}
/// <summary>
/// Sets the SSL protocols the SDK is allowed to use when negotiating TLS settings.
/// </summary>
public SecurityOptions WithSslProtocols(SslProtocols protocols)
{
return this with { SslProtocols = protocols };
}
/// <summary>
/// Sets the certificate revocation mode used when validating server certificate chains.
/// </summary>
/// <remarks>
/// The default is <see cref="X509RevocationMode.NoCheck"/>. Use <see cref="X509RevocationMode.Online"/>
/// or <see cref="X509RevocationMode.Offline"/> if your CA infrastructure provides CRL/OCSP endpoints.
/// </remarks>
public SecurityOptions WithRevocationMode(X509RevocationMode revocationMode)
{
return this with { RevocationMode = revocationMode };
}
}