| 1 |
#!/usr/bin/env python |
| 2 |
# |
| 3 |
# Copyright 2009-present MongoDB, Inc. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
|
| 18 |
from collections import OrderedDict as OD |
| 19 |
from typing import MutableSequence |
| 20 |
|
| 21 |
from evergreen_config_generator.functions import func, shell_exec |
| 22 |
from evergreen_config_generator.taskgroups import TaskGroup |
| 23 |
from evergreen_config_generator.tasks import NamedTask |
| 24 |
from evergreen_config_generator.variants import Variant |
| 25 |
|
| 26 |
|
| 27 |
def _create_tasks(): |
| 28 |
# passtask is expected to run on a remote Azure VM and succeed at obtaining credentials. |
| 29 |
passtask = NamedTask(task_name='testazurekms-task') |
| 30 |
passtask.commands = [ |
| 31 |
func('fetch-source'), |
| 32 |
shell_exec( |
| 33 |
r""" |
| 34 |
echo "Building test-azurekms ... begin" |
| 35 |
pushd mongoc |
| 36 |
.evergreen/scripts/compile-test-azurekms.sh |
| 37 |
popd |
| 38 |
echo "Building test-azurekms ... end" |
| 39 |
|
| 40 |
echo "Copying files ... begin" |
| 41 |
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup} |
| 42 |
export AZUREKMS_VMNAME=${AZUREKMS_VMNAME} |
| 43 |
export AZUREKMS_PRIVATEKEYPATH=/tmp/testazurekms_privatekey |
| 44 |
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools |
| 45 |
mkdir testazurekms |
| 46 |
cp ./mongoc/src/libmongoc/test-azurekms ./mongoc/install/lib/libmongocrypt.* testazurekms |
| 47 |
tar czf testazurekms.tgz testazurekms/* |
| 48 |
AZUREKMS_SRC="testazurekms.tgz" \ |
| 49 |
AZUREKMS_DST="./" \ |
| 50 |
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/copy-file.sh |
| 51 |
echo "Copying files ... end" |
| 52 |
|
| 53 |
echo "Untarring file ... begin" |
| 54 |
AZUREKMS_CMD="tar xf testazurekms.tgz" \ |
| 55 |
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/run-command.sh |
| 56 |
echo "Untarring file ... end" |
| 57 |
""", |
| 58 |
test=False, |
| 59 |
add_expansions_to_env=True, |
| 60 |
), |
| 61 |
shell_exec( |
| 62 |
r""" |
| 63 |
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup} |
| 64 |
export AZUREKMS_VMNAME=${AZUREKMS_VMNAME} |
| 65 |
export AZUREKMS_PRIVATEKEYPATH=/tmp/testazurekms_privatekey |
| 66 |
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools |
| 67 |
AZUREKMS_CMD="LD_LIBRARY_PATH=./testazurekms MONGODB_URI='mongodb://localhost:27017' KEY_NAME='${testazurekms_keyname}' KEY_VAULT_ENDPOINT='${testazurekms_keyvaultendpoint}' ./testazurekms/test-azurekms" \ |
| 68 |
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/run-command.sh |
| 69 |
""" |
| 70 |
), |
| 71 |
] |
| 72 |
|
| 73 |
failtask = NamedTask(task_name='testazurekms-fail-task') |
| 74 |
failtask.commands = [ |
| 75 |
func('fetch-source'), |
| 76 |
shell_exec( |
| 77 |
r""" |
| 78 |
pushd mongoc |
| 79 |
.evergreen/scripts/compile-test-azurekms.sh |
| 80 |
popd |
| 81 |
""", |
| 82 |
test=False, |
| 83 |
add_expansions_to_env=True, |
| 84 |
), |
| 85 |
shell_exec( |
| 86 |
r""" |
| 87 |
LD_LIBRARY_PATH=$PWD/install \ |
| 88 |
MONGODB_URI='mongodb://localhost:27017' \ |
| 89 |
KEY_NAME='${testazurekms_keyname}' \ |
| 90 |
KEY_VAULT_ENDPOINT='${testazurekms_keyvaultendpoint}' \ |
| 91 |
EXPECT_ERROR='Error from Azure IMDS server' \ |
| 92 |
./mongoc/src/libmongoc/test-azurekms |
| 93 |
""" |
| 94 |
), |
| 95 |
] |
| 96 |
return [passtask, failtask] |
| 97 |
|
| 98 |
|
| 99 |
def _create_variant(): |
| 100 |
return Variant( |
| 101 |
name='testazurekms-variant', |
| 102 |
display_name='Azure KMS', |
| 103 |
# Azure Virtual Machine created is Debian 11. |
| 104 |
run_on='debian11-small', # TODO: switch to 'debian11-latest-small' after DEVPROD-23011 fixed. |
| 105 |
tasks=['testazurekms_task_group', 'testazurekms-fail-task'], |
| 106 |
batchtime=20160, |
| 107 |
) # Use a batchtime of 14 days as suggested by the CSFLE test README |
| 108 |
|
| 109 |
|
| 110 |
def _create_task_group(): |
| 111 |
task_group = TaskGroup(name='testazurekms_task_group') |
| 112 |
task_group.setup_group_can_fail_task = True |
| 113 |
task_group.setup_group_timeout_secs = 1800 # 30 minutes |
| 114 |
task_group.setup_group = [ |
| 115 |
func('fetch-det'), |
| 116 |
# Assume role to get AWS secrets. |
| 117 |
{'command': 'ec2.assume_role', 'params': {'role_arn': '${aws_test_secrets_role}'}}, |
| 118 |
shell_exec( |
| 119 |
r""" |
| 120 |
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools |
| 121 |
echo '${testazurekms_publickey}' > /tmp/testazurekms_publickey |
| 122 |
echo '${testazurekms_privatekey}' > /tmp/testazurekms_privatekey |
| 123 |
# Set 600 permissions on private key file. Otherwise ssh / scp may error with permissions "are too open". |
| 124 |
chmod 600 /tmp/testazurekms_privatekey |
| 125 |
export AZUREKMS_CLIENTID=${testazurekms_clientid} |
| 126 |
export AZUREKMS_TENANTID=${testazurekms_tenantid} |
| 127 |
export AZUREKMS_SECRET=${testazurekms_secret} |
| 128 |
export AZUREKMS_DRIVERS_TOOLS=$DRIVERS_TOOLS |
| 129 |
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup} |
| 130 |
export AZUREKMS_PUBLICKEYPATH=/tmp/testazurekms_publickey |
| 131 |
export AZUREKMS_PRIVATEKEYPATH=/tmp/testazurekms_privatekey |
| 132 |
export AZUREKMS_SCOPE=${testazurekms_scope} |
| 133 |
export AZUREKMS_VMNAME_PREFIX=CDRIVER |
| 134 |
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/create-and-setup-vm.sh |
| 135 |
""", |
| 136 |
test=False, |
| 137 |
include_expansions_in_env=['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN'], |
| 138 |
), |
| 139 |
# Load the AZUREKMS_VMNAME expansion. |
| 140 |
OD( |
| 141 |
[ |
| 142 |
('command', 'expansions.update'), |
| 143 |
( |
| 144 |
'params', |
| 145 |
OD( |
| 146 |
[ |
| 147 |
('file', 'testazurekms-expansions.yml'), |
| 148 |
] |
| 149 |
), |
| 150 |
), |
| 151 |
] |
| 152 |
), |
| 153 |
] |
| 154 |
|
| 155 |
task_group.teardown_group = [ |
| 156 |
# Load expansions again. The setup task may have failed before running `expansions.update`. |
| 157 |
OD( |
| 158 |
[ |
| 159 |
('command', 'expansions.update'), |
| 160 |
( |
| 161 |
'params', |
| 162 |
OD( |
| 163 |
[ |
| 164 |
('file', 'testazurekms-expansions.yml'), |
| 165 |
] |
| 166 |
), |
| 167 |
), |
| 168 |
] |
| 169 |
), |
| 170 |
shell_exec( |
| 171 |
r""" |
| 172 |
DRIVERS_TOOLS=$(pwd)/drivers-evergreen-tools |
| 173 |
export AZUREKMS_VMNAME=${AZUREKMS_VMNAME} |
| 174 |
export AZUREKMS_RESOURCEGROUP=${testazurekms_resourcegroup} |
| 175 |
$DRIVERS_TOOLS/.evergreen/csfle/azurekms/delete-vm.sh |
| 176 |
""", |
| 177 |
test=False, |
| 178 |
), |
| 179 |
] |
| 180 |
task_group.tasks = ['testazurekms-task'] |
| 181 |
return task_group |
| 182 |
|
| 183 |
|
| 184 |
def testazurekms_generate( |
| 185 |
all_tasks: MutableSequence[NamedTask], |
| 186 |
all_variants: MutableSequence[Variant], |
| 187 |
all_task_groups: MutableSequence[TaskGroup], |
| 188 |
): |
| 189 |
all_tasks.extend(_create_tasks()) |
| 190 |
all_variants.append(_create_variant()) |
| 191 |
all_task_groups.append(_create_task_group()) |