Skip to content

CDRIVER-4454 add integration tests for Azure KMS #1124

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 32 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ba846d4
WIP: add testazurekms_task_group to create and destroy an Azure vm
kevinAlbs Oct 17, 2022
c89ecb4
TEMP: add xtrace
kevinAlbs Oct 17, 2022
f65610a
fix spelling
kevinAlbs Oct 17, 2022
787a93e
remove .json suffix
kevinAlbs Oct 17, 2022
904cd56
use chmod 600 on private key file
kevinAlbs Oct 18, 2022
b49378d
add test-azurekms
kevinAlbs Oct 18, 2022
6ccb1d8
add compile script
kevinAlbs Oct 18, 2022
73afe98
remove additional optional dependencies
kevinAlbs Oct 18, 2022
e47105a
clone libmongocrypt 1.6.0
kevinAlbs Oct 18, 2022
6e9b916
use type:test in testazurekms-task
kevinAlbs Oct 18, 2022
c92c001
parameterize the keyName and keyVaultEndpoint
kevinAlbs Oct 18, 2022
dbff90c
test running on remote
kevinAlbs Oct 18, 2022
d6475d6
TEMP: add intentional failure to test task failure
kevinAlbs Oct 18, 2022
502f0ed
fix placement of setup and test
kevinAlbs Oct 19, 2022
6a0ce51
Remove intentional error
kevinAlbs Oct 19, 2022
c67b5c8
add testazurekms-fail-task
kevinAlbs Oct 19, 2022
60f4f8f
update expected error
kevinAlbs Oct 19, 2022
6a80d31
add required AZUREKMS_VMNAME_PREFIX
kevinAlbs Oct 19, 2022
80fa14c
remove xtrace
kevinAlbs Oct 19, 2022
ba7db5b
add minimal support for TaskGroup
kevinAlbs Oct 19, 2022
61807f7
move Azure KMS config into python scripts
kevinAlbs Oct 19, 2022
2521f5d
update comment
kevinAlbs Oct 19, 2022
eb8d691
Merge branch 'master' into DRIVERS-2411
kevinAlbs Oct 19, 2022
eb16079
rename DRIVER_TOOLS to DRIVERS_TOOLS
kevinAlbs Oct 20, 2022
c671361
use debian 10
kevinAlbs Oct 20, 2022
3fed994
pass paths rather than relying on the working directory
kevinAlbs Nov 2, 2022
f31f4c2
remove cmake options soon to be superceded
kevinAlbs Nov 2, 2022
77c5fb7
use . instead of ~
kevinAlbs Nov 2, 2022
453d3d3
update git clone of drivers-evergreen-tools
kevinAlbs Nov 2, 2022
38864a1
use $PWD in LD_LIBRARY_PATH
kevinAlbs Nov 2, 2022
1adac5c
check KEY_NAME and KEY_VAULT_ENDPOINT env vars
kevinAlbs Nov 2, 2022
4d9869a
merge decl+init
kevinAlbs Nov 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .evergreen/compile-test-azurekms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset

# Working directory is expected to be mongo-c-driver repo.
ROOT=$(pwd)
INSTALL_DIR=$ROOT/install
. .evergreen/find-cmake.sh
echo "Installing libmongocrypt ... begin"
git clone https://github.com/mongodb/libmongocrypt --branch 1.6.0
$CMAKE -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \
-DBUILD_TESTING=OFF \
"-H$ROOT/libmongocrypt" \
"-B$ROOT/libmongocrypt"
$CMAKE --build "$ROOT/libmongocrypt" --target install
echo "Installing libmongocrypt ... end"

echo "Compile test-azurekms ... begin"
# Disable unnecessary dependencies. test-azurekms is copied to a remote host for testing, which may not have all dependent libraries.
$CMAKE \
-DENABLE_SASL=OFF \
-DENABLE_SNAPPY=OFF \
-DENABLE_ZSTD=OFF \
-DENABLE_ZLIB=OFF \
-DENABLE_ICU=OFF \
-DENABLE_SRV=OFF \
-DENABLE_CLIENT_SIDE_ENCRYPTION=ON \
-DCMAKE_PREFIX_PATH=$INSTALL_DIR \
.
$CMAKE --build . --target test-azurekms
echo "Compile test-azurekms ... end"
112 changes: 112 additions & 0 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24116,6 +24116,111 @@ tasks:
AUTH: noauth
LOADBALANCED: loadbalanced
SSL: nossl
- name: testazurekms-task
commands:
- func: fetch source
- command: shell.exec
params:
shell: bash
script: |-
set -o errexit
echo "Building test-azurekms ... begin"
pushd mongoc
./.evergreen/compile-test-azurekms.sh
popd
echo "Building test-azurekms ... end"
echo "Copying files ... begin"
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup}
export AZUREKMS_VMNAME=${AZUREKMS_VMNAME}
export AZUREKMS_PRIVATEKEYPATH=/tmp/testazurekms_privatekey
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools

Choose a reason for hiding this comment

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

NOTE: if you use DRIVERS_TOOLS, this value should already be assigned, at least this is what I saw in the c# driver (same in other places)

Choose a reason for hiding this comment

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

Missed your explanation above. It's a bit strange, but ok :)

mkdir testazurekms
cp ./mongoc/src/libmongoc/test-azurekms ./mongoc/install/lib/libmongocrypt.* testazurekms
tar czf testazurekms.tgz testazurekms/*
AZUREKMS_SRC="testazurekms.tgz" \
AZUREKMS_DST="./" \
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/copy-file.sh
echo "Copying files ... end"
echo "Untarring file ... begin"
AZUREKMS_CMD="tar xf testazurekms.tgz" \
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/run-command.sh
echo "Untarring file ... end"
- command: shell.exec
type: test
params:
shell: bash
script: |-
set -o errexit
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup}
export AZUREKMS_VMNAME=${AZUREKMS_VMNAME}
export AZUREKMS_PRIVATEKEYPATH=/tmp/testazurekms_privatekey
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools
AZUREKMS_CMD="LD_LIBRARY_PATH=./testazurekms MONGODB_URI='mongodb://localhost:27017' KEY_NAME='${testazurekms_keyname}' KEY_VAULT_ENDPOINT='${testazurekms_keyvaultendpoint}' ./testazurekms/test-azurekms" \
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/run-command.sh
- name: testazurekms-fail-task
commands:
- func: fetch source
- command: shell.exec
params:
shell: bash
script: |-
set -o errexit
pushd mongoc
./.evergreen/compile-test-azurekms.sh
popd
- command: shell.exec
type: test
params:
shell: bash
script: |-
set -o errexit
LD_LIBRARY_PATH=$PWD/install \
MONGODB_URI='mongodb://localhost:27017' \
KEY_NAME='${testazurekms_keyname}' \
KEY_VAULT_ENDPOINT='${testazurekms_keyvaultendpoint}' \
EXPECT_ERROR='Error from Azure IMDS server' \
./mongoc/src/libmongoc/test-azurekms
task_groups:
- name: testazurekms_task_group
setup_group:
- command: shell.exec
params:
shell: bash
script: |-
set -o errexit
git clone https://github.com/mongodb-labs/drivers-evergreen-tools.git --depth=1 drivers-evergreen-tools
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools
echo '${testazurekms_publickey}' > /tmp/testazurekms_publickey
echo '${testazurekms_privatekey}' > /tmp/testazurekms_privatekey
# Set 600 permissions on private key file. Otherwise ssh / scp may error with permissions "are too open".
chmod 600 /tmp/testazurekms_privatekey
export AZUREKMS_CLIENTID=${testazurekms_clientid}
export AZUREKMS_TENANTID=${testazurekms_tenantid}
export AZUREKMS_SECRET=${testazurekms_secret}
export AZUREKMS_DRIVERS_TOOLS=$DRIVERS_TOOLS
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup}
export AZUREKMS_PUBLICKEYPATH=/tmp/testazurekms_publickey
export AZUREKMS_PRIVATEKEYPATH=/tmp/testazurekms_privatekey
export AZUREKMS_SCOPE=${testazurekms_scope}
export AZUREKMS_VMNAME_PREFIX=CDRIVER
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/create-and-setup-vm.sh
- command: expansions.update
params:
file: testazurekms-expansions.yml
teardown_group:
- command: shell.exec
params:
shell: bash
script: |-
set -o errexit
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools
export AZUREKMS_VMNAME=${AZUREKMS_VMNAME}
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup}
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/delete-vm.sh
setup_group_can_fail_task: true
setup_group_timeout_secs: 1800
tasks:
- testazurekms-task
buildvariants:
- name: releng
display_name: '**Release Archive Creator'
Expand Down Expand Up @@ -24878,3 +24983,10 @@ buildvariants:
run_on: macos-1100-arm64
tasks:
- debug-compile-sasl-darwinssl
- name: testazurekms-variant
display_name: Azure KMS
run_on: debian10-small
tasks:
- testazurekms_task_group
- testazurekms-fail-task
batchtime: 20160
2 changes: 2 additions & 0 deletions .evergreen/find-cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ set -o errexit # Exit the script with error if any of the commands fail

find_cmake ()
{
CMAKE=${CMAKE:-}
IGNORE_SYSTEM_CMAKE=${IGNORE_SYSTEM_CMAKE:-}
if [ ! -z "$CMAKE" ]; then
return 0
elif [ -f "/Applications/cmake-3.2.2-Darwin-x86_64/CMake.app/Contents/bin/cmake" ]; then
Expand Down
48 changes: 48 additions & 0 deletions build/evergreen_config_generator/taskgroups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2018-present MongoDB, 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.

from evergreen_config_generator import ConfigObject


class TaskGroup(ConfigObject):
def __init__(self, name):
super(TaskGroup, self).__init__()
self._task_group_name = name

@property
def name(self):
return self._task_group_name

def to_dict(self):
v = super(TaskGroup, self).to_dict()
# See possible TaskGroup attributes from the Evergreen wiki:
# https://github.com/evergreen-ci/evergreen/wiki/Project-Configuration-Files#task-groups
attrs = [
'setup_group',
'teardown_group',
'setup_task',
'teardown_task',
'max_hosts',
'timeout',
'setup_group_can_fail_task',
'setup_group_timeout_secs',
'setup_group_can_fail_task',
'share_processes',
'tasks'
]

for i in attrs:
if getattr(self, i, None):
v[i] = getattr(self, i)
return v
15 changes: 15 additions & 0 deletions build/evergreen_config_lib/taskgroups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2018-present MongoDB, 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.

all_task_groups = []
141 changes: 141 additions & 0 deletions build/evergreen_config_lib/testazurekms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/usr/bin/env python
#
# Copyright 2022-present MongoDB, 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.


from collections import OrderedDict as OD

from evergreen_config_generator.functions import (shell_exec, func)
from evergreen_config_generator.tasks import (NamedTask)
from evergreen_config_generator.variants import (Variant)
from evergreen_config_generator.taskgroups import (TaskGroup)

def _create_tasks():
# passtask is expected to run on a remote Azure VM and succeed at obtaining credentials.
passtask = NamedTask (task_name="testazurekms-task")
passtask.commands = [
func("fetch source"),
shell_exec (r'''
echo "Building test-azurekms ... begin"
pushd mongoc
./.evergreen/compile-test-azurekms.sh
popd
echo "Building test-azurekms ... end"

echo "Copying files ... begin"
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup}
export AZUREKMS_VMNAME=${AZUREKMS_VMNAME}
export AZUREKMS_PRIVATEKEYPATH=/tmp/testazurekms_privatekey
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools
mkdir testazurekms
cp ./mongoc/src/libmongoc/test-azurekms ./mongoc/install/lib/libmongocrypt.* testazurekms
tar czf testazurekms.tgz testazurekms/*
AZUREKMS_SRC="testazurekms.tgz" \
AZUREKMS_DST="./" \
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/copy-file.sh
echo "Copying files ... end"

echo "Untarring file ... begin"
AZUREKMS_CMD="tar xf testazurekms.tgz" \
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/run-command.sh
echo "Untarring file ... end"
''', test=False),
shell_exec (r'''
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup}
export AZUREKMS_VMNAME=${AZUREKMS_VMNAME}
export AZUREKMS_PRIVATEKEYPATH=/tmp/testazurekms_privatekey
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools
AZUREKMS_CMD="LD_LIBRARY_PATH=./testazurekms MONGODB_URI='mongodb://localhost:27017' KEY_NAME='${testazurekms_keyname}' KEY_VAULT_ENDPOINT='${testazurekms_keyvaultendpoint}' ./testazurekms/test-azurekms" \
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/run-command.sh
''')
]

failtask = NamedTask (task_name="testazurekms-fail-task")
failtask.commands = [
func("fetch source"),
shell_exec (r'''
pushd mongoc
./.evergreen/compile-test-azurekms.sh
popd
''', test=False),
shell_exec (r'''
LD_LIBRARY_PATH=$PWD/install \
MONGODB_URI='mongodb://localhost:27017' \
KEY_NAME='${testazurekms_keyname}' \
KEY_VAULT_ENDPOINT='${testazurekms_keyvaultendpoint}' \
EXPECT_ERROR='Error from Azure IMDS server' \
./mongoc/src/libmongoc/test-azurekms
''')
]
return [passtask, failtask]


def _create_variant():
return Variant(
name="testazurekms-variant",
display_name="Azure KMS",
# Azure Virtual Machine created is Debian 10.
run_on="debian10-small", tasks=[
"testazurekms_task_group",
"testazurekms-fail-task"
], batchtime=20160) # Use a batchtime of 14 days as suggested by the CSFLE test README


def _create_task_group():
task_group = TaskGroup(name="testazurekms_task_group")
task_group.setup_group_can_fail_task = True
task_group.setup_group_timeout_secs = 1800 # 30 minutes
task_group.setup_group = [
shell_exec(r'''
git clone https://github.com/mongodb-labs/drivers-evergreen-tools.git --depth=1 drivers-evergreen-tools
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools
echo '${testazurekms_publickey}' > /tmp/testazurekms_publickey
echo '${testazurekms_privatekey}' > /tmp/testazurekms_privatekey
# Set 600 permissions on private key file. Otherwise ssh / scp may error with permissions "are too open".
chmod 600 /tmp/testazurekms_privatekey
export AZUREKMS_CLIENTID=${testazurekms_clientid}
export AZUREKMS_TENANTID=${testazurekms_tenantid}
export AZUREKMS_SECRET=${testazurekms_secret}
export AZUREKMS_DRIVERS_TOOLS=$DRIVERS_TOOLS
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup}
export AZUREKMS_PUBLICKEYPATH=/tmp/testazurekms_publickey
export AZUREKMS_PRIVATEKEYPATH=/tmp/testazurekms_privatekey
export AZUREKMS_SCOPE=${testazurekms_scope}
export AZUREKMS_VMNAME_PREFIX=CDRIVER
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/create-and-setup-vm.sh
''', test=False),
# Load the AZUREKMS_VMNAME expansion.
OD([('command', 'expansions.update'),
('params', OD([
('file', 'testazurekms-expansions.yml'),
]))])
]

task_group.teardown_group = [
shell_exec(r'''
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools
export AZUREKMS_VMNAME=${AZUREKMS_VMNAME}
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup}
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/delete-vm.sh
''', test=False)
]
task_group.tasks = ["testazurekms-task"]
return task_group


def testazurekms_generate(all_tasks, all_variants, all_task_groups):
all_tasks.extend(_create_tasks())
all_variants.append(_create_variant())
all_task_groups.append(_create_task_group())
5 changes: 5 additions & 0 deletions build/generate-evergreen-config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
from evergreen_config_lib.functions import all_functions
from evergreen_config_lib.tasks import all_tasks
from evergreen_config_lib.variants import all_variants
from evergreen_config_lib.taskgroups import all_task_groups
from evergreen_config_lib.testazurekms import testazurekms_generate

testazurekms_generate (all_tasks, all_variants, all_task_groups)

config = OD([
('stepback', True),
Expand All @@ -66,6 +70,7 @@
OD([('func', 'backtrace')])
]),
('tasks', all_tasks),
('task_groups', all_task_groups),
('buildvariants', all_variants),
])

Expand Down
1 change: 1 addition & 0 deletions src/libmongoc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ endif ()
mongoc_add_test (test-libmongoc FALSE ${test-libmongoc-sources})
mongoc_add_test (test-mongoc-gssapi FALSE ${PROJECT_SOURCE_DIR}/tests/test-mongoc-gssapi.c)
mongoc_add_test (test-mongoc-cache FALSE ${PROJECT_SOURCE_DIR}/tests/test-mongoc-cache.c)
mongoc_add_test (test-azurekms FALSE ${PROJECT_SOURCE_DIR}/tests/test-azurekms.c)

if (ENABLE_TESTS)
# "make test" doesn't compile tests, so we create "make check" which compiles
Expand Down
Loading