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

Onboarding MDE for Linux to LISA #3113

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
44 changes: 43 additions & 1 deletion lisa/sut_orchestrator/azure/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
# Licensed under the MIT license.

import re
from pathlib import PurePath
import os
import sys
import json
from pathlib import PurePath, Path
from typing import Any, Dict, List, Optional, Tuple, Type

from assertpy import assert_that
Expand All @@ -20,6 +23,9 @@
get_matched_str,
)
from lisa.util.process import ExecutableResult
from typing import Optional, cast
from lisa.operating_system import Posix



class Waagent(Tool):
Expand Down Expand Up @@ -561,3 +567,39 @@ def get_pool_records(self, pool_id: int, force_run: bool = False) -> Dict[str, s
records[content_split[i]] = content_split[i + 1]

return records

class mdatp(Tool):
@property
def command(self) -> str:
self._log.info(f"Inside the func: [{sys._getframe().f_code.co_name}]")
return "mdatp"

@property
def can_install(self) -> bool:
self._log.info(f"Inside the func: [{sys._getframe().f_code.co_name}]")
return False

def _install(self) -> bool:
return self._check_exists()

def get_result(
self,
arg: str,
json_out: bool = False,
sudo: bool = False,
) -> Any:
self._log.info(f"Inside the func: [{sys._getframe().f_code.co_name}]")
if json_out:
arg += ' --output json'
result = self.run(
arg,
sudo=sudo,
shell=True,
force_run=True,
)

result.assert_exit_code()
if json_out:
return json.loads(result.stdout)
return result.stdout.split()

58 changes: 58 additions & 0 deletions microsoft/runbook/my.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: azure default
Copy link
Author

Choose a reason for hiding this comment

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

This is just for testing purpose. Will remove it once incorporate all the review comments

include:
- path: ./debug.yml
variable:
- name: origin
value: tiers/tier.yml
- name: case
value: verify_cpu_count
- name: location
value: "westus3"
- name: keep_environment
value: "no"
- name: resource_group_name
value: ""
- name: marketplace_image
value: ""
- name: vhd
value: ""
- name: vm_size
value: ""
- name: deploy
value: true
- name: wait_delete
value: false
- name: concurrency
value: 5
- name: admin_private_key_file
value: ""
is_secret: true
- name: admin_password
value: ""
is_secret: true
- name: case
value: verify_cpu_count
concurrency: $(concurrency)
notifier:
- type: html
- type: env_stats
platform:
- type: azure
admin_private_key_file: $(admin_private_key_file)
admin_password: $(admin_password)
keep_environment: $(keep_environment)
azure:
resource_group_name: "lisa-test-zakhter"
deploy: $(deploy)
subscription_id: $(subscription_id)
wait_delete: $(wait_delete)
requirement:
core_count:
min: 2
azure:
#marketplace: "Debian:debian-10-daily:10-gen2:0.20231218.1599" #$(marketplace_image)
marketplace: $(marketplace_image)
vhd: $(vhd)
location: $(location)
vm_size: $(vm_size)

150 changes: 150 additions & 0 deletions microsoft/testsuites/vm_extensions/mde.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import os
import time
import requests

from typing import Any
from pathlib import Path, PurePath

from assertpy import assert_that

from lisa import (
Logger,
Node,
TestCaseMetadata,
TestSuite,
TestSuiteMetadata,
simple_requirement,
)
from lisa.operating_system import BSD
from lisa.sut_orchestrator.azure.tools import mdatp
from lisa.testsuite import TestResult
from lisa.tools import RemoteCopy, Whoami, Curl
from lisa import CustomScriptBuilder, CustomScript
from lisa.util import LisaException


@TestSuiteMetadata(
area="vm_extension",
category="functional",
description="""
MDE Test Suite
zeeshan1995 marked this conversation as resolved.
Show resolved Hide resolved
""",
)
class MDE(TestSuite):

def before_case(self, log: Logger, **kwargs: Any) -> None:
response = requests.get("https://raw.githubusercontent.com/microsoft/mdatp-xplat/master/linux/installation/mde_installer.sh")
if response.ok:
script = response.text
import tempfile
_, self.mde_installer = tempfile.mkstemp(prefix='mde_installer', suffix='.sh')
with open(self.mde_installer, 'w') as writer:
writer.write(script)
self._echo_script = CustomScriptBuilder(Path(os.path.dirname(self.mde_installer)),
[os.path.basename(self.mde_installer)])
else:
log.error('Unable to download mde_installer.sh script')

@TestCaseMetadata(
description="""
Verify MDE installation
""",
priority=1,
requirement=simple_requirement(min_core_count=2,
min_memory_mb=1024,
unsupported_os=[BSD])
)
def verify_install(self, node: Node, log: Logger, result: TestResult) -> None:
script: CustomScript = node.tools[self._echo_script]
log.info('Installing MDE')
result1 = script.run(parameters="--install", sudo=True)
log.info(result1)

try:
output = node.tools[mdatp]._check_exists()
zeeshan1995 marked this conversation as resolved.
Show resolved Hide resolved
except LisaException as e:
log.error(e)
output = False

assert_that(output).described_as('Unable to install MDE').is_equal_to(True)

Choose a reason for hiding this comment

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

Is the description (described_as) supposed to be opposite of what is being checked (is_equal_to) here and elsewhere?


@TestCaseMetadata(
description="""
Verify if MDE is healthy
""",
priority=1,
requirement=simple_requirement(min_core_count=2,
min_memory_mb=1024,
unsupported_os=[BSD])
)
def verify_onboard(self, node: Node, log: Logger, result: TestResult) -> None:
username = node.tools[Whoami].get_username()

remote_copy = node.tools[RemoteCopy]
remote_copy.copy_to_remote(
PurePath("/home/zakhter/projects/lab/MicrosoftDefenderATPOnboardingLinuxServer.py"), PurePath(f"/home/{username}/MicrosoftDefenderATPOnboardingLinuxServer.py"))
zeeshan1995 marked this conversation as resolved.
Show resolved Hide resolved

script: CustomScript = node.tools[self._echo_script]

log.info('Onboarding MDE')
result1 = script.run(parameters=f"--onboard /home/{username}/MicrosoftDefenderATPOnboardingLinuxServer.py/MicrosoftDefenderATPOnboardingLinuxServer.py", sudo=True)
zeeshan1995 marked this conversation as resolved.
Show resolved Hide resolved
log.info(result1)

output = node.tools[mdatp].get_result('health --field licensed')

log.info(output)

assert_that(output).is_equal_to(['true'])
zeeshan1995 marked this conversation as resolved.
Show resolved Hide resolved

@TestCaseMetadata(
description="""
Verify if MDE is healthy
""",
priority=1,
requirement=simple_requirement(min_core_count=2,
min_memory_mb=1024,
unsupported_os=[BSD])
)
def verify_health(self, node: Node, log: Logger, result: TestResult) -> None:
output = node.tools[mdatp].get_result('health', json_out=True)

log.info(output)

assert_that(output['healthy']).is_equal_to(True)

@TestCaseMetadata(
description="""
Verify if MDE is healthy
""",
priority=1,
requirement=simple_requirement(min_core_count=2,
min_memory_mb=1024,
unsupported_os=[BSD])
)
def eicar_test(self, node: Node, log: Logger, result: TestResult) -> None:
log.info('Running EICAR test')

output = node.tools[mdatp].get_result('health --field real_time_protection_enabled')
if output == ['false']:
output = node.tools[mdatp].get_result('config real-time-protection --value enabled', sudo=True)
assert_that(' '.join(output)).is_equal_to('Configuration property updated.')

current_threat_list= node.tools[mdatp].get_result('threat list')
log.info(current_threat_list)

node.tools[Curl].fetch(arg="-o /tmp/eicar.com.txt",
execute_arg="",
url="https://secure.eicar.org/eicar.com.txt")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I hope it won't trigger any security alert?


time.sleep(5) #Wait for remediation

new_threat_list = node.tools[mdatp].get_result('threat list')
log.info(new_threat_list)

eicar_detect = ' '.join(new_threat_list).replace(' '.join(current_threat_list), '')

log.info(eicar_detect)
log.info(eicar_detect.find('Name: Virus:DOS/EICAR_Test_File'))
assert_that('Name: Virus:DOS/EICAR_Test_File' in eicar_detect).is_equal_to(True)


Loading