Skip to content

PHPC-2093: Key Management API #1339

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

Merged
merged 13 commits into from
Aug 18, 2022
Merged

PHPC-2093: Key Management API #1339

merged 13 commits into from
Aug 18, 2022

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Jul 29, 2022

@@ -492,6 +493,78 @@ void php_phongo_cursor_init_ce(INIT_FUNC_ARGS) /* {{{ */
php_phongo_handler_cursor.offset = XtOffsetOf(php_phongo_cursor_t, std);
} /* }}} */

static void phongo_cursor_init(zval* return_value, zval* manager, mongoc_cursor_t* cursor, zval* readPreference, zval* session) /* {{{ */
Copy link
Member

Choose a reason for hiding this comment

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

Noted that this logic was moved from phongo_execute.c

@jmikola jmikola force-pushed the phpc-2093 branch 2 times, most recently from 8158830 to 0999c83 Compare August 5, 2022 20:50
@jmikola jmikola marked this pull request as ready for review August 5, 2022 20:51
}

if (bson_empty(&reply)) {
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "mongoc_client_encryption_delete_key returned an empty document");
Copy link
Member Author

Choose a reason for hiding this comment

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

@alcaeus: this was added per your earlier comment in #1339 (comment)

* key vault collection. The collection has a majority read concern, but the
* query itself specifies no filter or options. Create an empty query object
* and attach it to the cursor for the benefit of debugging. */
if (!phongo_query_init(&query, NULL, NULL)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The side effect here is that the dumped Cursor will not show a majority read concern as it might otherwise if we were to execute a query through PHPLIB with a collection-level read concern. I don't think this is worth addressing, though, and the current behavior isn't much different if PHPC were to execute a query and inherit a client-level read concern (enforced internally by libmongoc).

*
* This function will fall back to a modifier in the absence of a top-level
* option (where applicable). */
bool phongo_query_init(zval* return_value, zval* filter, zval* options) /* {{{ */
Copy link
Member Author

Choose a reason for hiding this comment

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

This function previously initialized the php_phongo_query_t struct. Since we now call this from two places (Query constructor and ClientEncryption::getKeys()), I needed to change it to initialize the entire zval.

We still have some branch logic below in the event that the zval was already initialized, which covers the original Query constructor use case. And we also have an instanceof check after that as an added sanity check before we fetch the internal struct.

goto cleanup;
}

if (!phongo_cursor_init_for_query(return_value, &intern->key_vault_client_manager, cursor, intern->key_vault_namespace, &query, NULL, NULL)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

phongo_cursor_init_for_query was changed to handle this use case. See my later comment in Query.c.

/* {{{ proto object MongoDB\Driver\ClientEncryption::deleteKey(MongoDB\BSON\Binary $id)
Removes the key document with the given UUID (BSON binary subtype 0x04) from
the key vault collection. Returns the result of the internal deleteOne()
operation on the key vault collection. */
Copy link
Member Author

Choose a reason for hiding this comment

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

Since DeleteResult only exists in PHPLIB, returning the result object as a plain document made the most sense. Constructing a BulkWriteResult isn't feasible for the same reason as rewrapManyDataKeys(), as we don't have a server hint available and thus cannot satisfy BulkWriteResult::getServer().

/* {{{ proto object MongoDB\Driver\ClientEncryption::rewrapManyDataKey(array|object $filter[, array $options = array()])
Decrypts multiple data keys and (re-)encrypts them with a new masterKey, or
with their current masterKey if a new one is not given. Returns an object
corresponding to the internal libmongoc result. */
Copy link
Member Author

Choose a reason for hiding this comment

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

As previously discussed in the C/C++ Slack channel, I opted not to model the return value here. As I mentioned earlier in deleteKey(), we don't have a server hint available and can't create a BulkWriteResult. I didn't see any reason to model RewrapManyDataKeyResult since it just has an optional bulkWriteResult field.

bulk_write_result = mongoc_client_encryption_rewrap_many_datakey_result_get_bulk_write_result(result);

if (bson_empty0(bulk_write_result)) {
BSON_APPEND_NULL(&reply, "bulkWriteResult");
Copy link
Member Author

Choose a reason for hiding this comment

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

I think always having the bulkWriteResult field exist is preferable to leaving it unset.

@@ -305,6 +656,23 @@ void phongo_clientencryption_init(php_phongo_clientencryption_t* intern, zval* o
ZVAL_ZVAL(&intern->key_vault_client_manager, key_vault_client_manager, 1, 0);
}

/* Copy the key vault namespace, since it may be referenced later by
* ClientEncryption::getKeys(). The namespace will already have been
* validated by phongo_clientencryption_opts_from_zval. */
Copy link
Member Author

Choose a reason for hiding this comment

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

This exists purely for Query debug info.

define('CSFLE_KEY_VAULT_NS', 'encryption.__keyVault');
define('CSFLE_KEY_VAULT_DATABASE_NAME', 'encryption');
define('CSFLE_KEY_VAULT_COLLECTION_NAME', '__keyVault');
define('CSFLE_KEY_VAULT_NS', CSFLE_KEY_VAULT_DATABASE_NAME . '.' . CSFLE_KEY_VAULT_COLLECTION_NAME);
Copy link
Member Author

Choose a reason for hiding this comment

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

Splitting these was necessary to utilize skip_if_not_clean in tests. I originally created a skip_if_ns_not_clean method but this seemed more straightforward.

Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -0,0 +1,31 @@
--TEST--
MongoDB\Driver\ClientEncryption::rewrapManyDataKey() accepts null for $options
Copy link
Member Author

Choose a reason for hiding this comment

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

FYI: this test covers the changes from #1347 for PHPC-2124. I don't believe we had existing tests for passing null, as we probably would have caught the stub conflict sooner if so.

@jmikola
Copy link
Member Author

jmikola commented Aug 17, 2022

@alcaeus: Feel free to review my last commit. I'll wait on #1347 to be merged before merging this.

One question: when merging generated arginfo.h files, I assume we always want to regenerate those after merging to ensure the "Stub hash" is consistent. Is that correct?

@alcaeus
Copy link
Member

alcaeus commented Aug 18, 2022

One question: when merging generated arginfo.h files, I assume we always want to regenerate those after merging to ensure the "Stub hash" is consistent. Is that correct?

That is correct. Luckily, the stub hash in the arginfo file will conflict if the file was changed in both head and base branch of a PR, requiring a rebase and re-generation of the arginfo file.

@jmikola jmikola merged commit c99a856 into mongodb:master Aug 18, 2022
@jmikola jmikola deleted the phpc-2093 branch August 18, 2022 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants