Skip to content
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

feat: adds a MongoDB connector (closes smallstep/certificates#141) #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

elasticspoon
Copy link

This commit adds a MongoDB connector and closes smallstep/certificates#141

Note: To enable transactions the database must a replica set, I made the connection fail if the condition was not met.
I am not sure if this is the best way to go about it. But I am also not sure how often the basic CRUD operations are used outside of transactions.

MongoDB is a document database, thus concepts translate as:
bucket/table = collection
key = document
value = document value

Name of feature:

MongoDB connector

Pain or issue this feature alleviates:

Closes smallstep/certificates#141

Why is this important to the project (if not answered above):

Closes smallstep/certificates#141

Is there documentation on how to use this feature? If so, where?

Implements the database interface

In what environments or workflows is this feature supported?

Same as all the rest of the connectors. Transactions require Mongo to be configured as a replica set.

In what environments or workflows is this feature explicitly NOT supported (if any)?

Not supported in CI tests.

Supporting links/other PRs/issues:

This commit adds a MongoDB connector.

A couple of things to note:
- To enable transactions the database must a replica set,
I made the connection fail if the condition was not met.

MongoDB is a document database, thus concepts translate as:
bucket/table = collection
key = document
value = document value

Note: mongo will automatically create a collection
if a operation is attempted on one that does not exist.
For example: if you delete a table, then call list to
check for the contents of the table it will be recreated.

closes smallstep/certificates#141
@elasticspoon elasticspoon requested a review from a team as a code owner January 11, 2024 03:18
@CLAassistant
Copy link

CLAassistant commented Jan 11, 2024

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the needs triage Waiting for discussion / prioritization by team label Jan 11, 2024
mongo/mongo.go Outdated Show resolved Hide resolved
go.sum Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
nosql_test.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
@hslatman
Copy link
Member

Thank you, @elasticspoon! We've done a first pass of review.

@elasticspoon elasticspoon force-pushed the adds-mongodb-connector branch 2 times, most recently from 7652cc6 to ae85109 Compare January 31, 2024 20:25
removes use of github/errors package, uses fmt.Errorf instead
changes syntax of error messages
changes order of context as a parameter
makes sure session is passed down where possible
renames bucket to collection
other minor fixes for review
Copy link
Member

@hslatman hslatman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the improvements, @elasticspoon! 🙂

I've done another pass. It may seem like much, but it's mostly many cases of the same thing. Most things are just textual too.

We're currently working on a new version of nosql that changes some things in terms of the exposed interface. It should be available fairly soon. We aim for no / minimal breaking changes, but at this time I can't say for sure what it'll look like. I don't expect big changes to happen to the core of your implementation. It's probably good to await its release and adapt this PR, so that it starts of in a nice way.

mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
Co-authored-by: Herman Slatman <[email protected]>
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated Show resolved Hide resolved
mongo/mongo.go Outdated
Comment on lines 144 to 146
if err = session.StartTransaction(txnOptions); err != nil {
return fmt.Errorf("failed to pending transaction: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"failed to 'what' pending ..."

@elasticspoon elasticspoon force-pushed the adds-mongodb-connector branch 4 times, most recently from b36b3e2 to 515e0c6 Compare February 22, 2024 22:38
Comment on lines +304 to +326
if err := db.CreateTable(op.Bucket); err != nil {
return abort(ctx, session, fmt.Errorf("failed creating table %s: %w", op.Bucket, err))
}
case database.DeleteTable:
if err := db.DeleteTable(op.Bucket); err != nil {
return abort(ctx, session, fmt.Errorf("failed deleting table %s: %w", op.Bucket, err))
}
case database.Get:
if op.Result, err = db.get(ctx, op.Bucket, op.Key); err != nil {
return abort(ctx, session, fmt.Errorf("failed getting %s/%s: %w", op.Bucket, op.Key, err))
}
case database.Set:
if err := db.set(ctx, op.Bucket, op.Key, op.Value); err != nil {
return abort(ctx, session, fmt.Errorf("failed setting %s/%s: %w", op.Bucket, op.Key, err))
}
case database.Delete:
if err := db.del(ctx, op.Bucket, op.Key); err != nil {
return abort(ctx, session, fmt.Errorf("failed deleting %s/%s: %w", op.Bucket, op.Key, err))
}
case database.CmpAndSwap:
op.Result, op.Swapped, err = db.cmpAndSwap(ctx, op.Bucket, op.Key, op.CmpValue, op.Value)
if err != nil {
return abort(ctx, session, fmt.Errorf("failed load-or-store on %s/%s: %w", op.Bucket, op.Key, err))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is better in terms of error message clarity. Errors will now say what operation an Update failed to followed by the reason that operation failed.

Changes to error syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Waiting for discussion / prioritization by team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MongoDB Database Connector
4 participants