-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/tls 49 #51
Open
bobcat1506
wants to merge
2
commits into
loresoft:master
Choose a base branch
from
bobcat1506:feature/tls-49
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/tls 49 #51
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: '3.1' | ||
|
||
services: | ||
|
||
mongo: | ||
image: mongo | ||
restart: always | ||
volumes: | ||
- ./tls/:/etc/tls | ||
ports: | ||
- "27017" | ||
command: --tlsMode requireTLS --tlsCertificateKeyFile /etc/tls/mongodb.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using MongoDB.Bson; | ||
|
@@ -161,6 +163,26 @@ public string CollectionName | |
/// </summary> | ||
public bool IncludeEventProperties { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets if TLS should be used when connecting to MongoDB | ||
/// </summary> | ||
public bool UseTls { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the client certificate to use when connecting to MongoDB | ||
/// </summary> | ||
public string ClientCertificate { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the client certificate password to use when connecting to MongoDB | ||
/// </summary> | ||
public string ClientCertificatePassword { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommended to use NLog |
||
|
||
/// <summary> | ||
/// Gets or sets the Check Certificate Revocation when connecting to MongoDB | ||
/// </summary> | ||
public bool? CheckCertificateRevocation { get; set; } | ||
|
||
/// <summary> | ||
/// Initializes the target. Can be used by inheriting classes | ||
/// to initialize logging. | ||
|
@@ -404,8 +426,26 @@ private IMongoCollection<BsonDocument> GetCollection() | |
databaseName = !string.IsNullOrEmpty(databaseName) ? databaseName : (mongoUrl.DatabaseName ?? "NLog"); | ||
collectionName = !string.IsNullOrEmpty(collectionName) ? collectionName : "Log"; | ||
InternalLogger.Info("Connecting to MongoDB collection {0} in database {1}", collectionName, databaseName); | ||
|
||
var settings = MongoClientSettings.FromUrl(mongoUrl); | ||
|
||
if (UseTls) | ||
{ | ||
var cert = new X509Certificate2(ClientCertificate, ClientCertificatePassword); | ||
|
||
if (cert == null) | ||
{ | ||
throw new InvalidOperationException("Unable to load certificate"); | ||
} | ||
|
||
settings.SslSettings = new SslSettings | ||
{ | ||
ClientCertificates = new[] { cert }, | ||
}; | ||
UseTls = true; | ||
} | ||
|
||
var client = new MongoClient(mongoUrl); | ||
var client = new MongoClient(settings); | ||
|
||
// Database name overrides connection string | ||
var database = client.GetDatabase(databaseName); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# https://medium.com/@rajanmaharjan/secure-your-mongodb-connections-ssl-tls-92e2addb3c89 | ||
|
||
# root ca | ||
openssl genrsa -out rootCA.key 2048 | ||
openssl genrsa -des3 -out rootCA.key 2048 # password is mongo | ||
openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.pem | ||
|
||
# per device | ||
openssl genrsa -out mongodb.key 2048 | ||
# Whatever you see in the address field in your browser when you go to your device | ||
# must be what you put under common name, even if it’s an IP address. | ||
openssl req -new -key mongodb.key -out mongodb.csr # answer Common Name (eg, YOUR name) []: localhost | ||
|
||
openssl x509 -req -in mongodb.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out mongodb.crt -days 500 -sha256 | ||
cat mongodb.key mongodb.crt > mongodb.pem | ||
|
||
# run mongo using docker-compose or the following command: mongod --tlsMode requireTLS --tlsCertificateKeyFile tls/mongodb.pem |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommended to use NLog
Layout
instead ofstring
. IfClientCertificate
is a file-path, then maybe include that in the property-name.