Skip to content

Commit

Permalink
Refactor setup-agent.ps1 to update Wazuh agent installation script UR…
Browse files Browse the repository at this point in the history
…L, set default version to 4.8.1-1, and dynamically set environment variables Add wazuh agent install on Windows #3
  • Loading branch information
bengo237 committed Oct 10, 2024
1 parent 41f14e0 commit 89a0069
Showing 1 changed file with 59 additions and 115 deletions.
174 changes: 59 additions & 115 deletions scripts/install.ps1
Original file line number Diff line number Diff line change
@@ -1,126 +1,70 @@
# Set strict mode for script execution
Set-StrictMode -Version Latest

# Define default values for variables
[string]$WAZUH_AGENT_VERSION = "4.8.1-1" # Updated version
[string]$WAZUH_MANAGER = "master.dev.wazuh.adorsys.team" # Default manager

# Function to log information
function Log-Info {
param (
[string]$Message
)
Write-Host "[INFO] $Message"
}

# Function to log errors
function Log-Error {
param (
[string]$Message
)
Write-Host "[ERROR] $Message" -ForegroundColor Red
}

# Import GPG Key for Wazuh repository
function Import-Keys {
Log-Info "Importing Wazuh GPG key and setting up the repository for Windows"
$WazuhKeyUrl = "https://packages.wazuh.com/key/GPG-KEY-WAZUH"
$TEMP_DIR = [System.IO.Path]::GetTempPath()

# Download the GPG key
Invoke-WebRequest -Uri $WazuhKeyUrl -OutFile "$TEMP_DIR\WazuhGPGKey.asc"
Log-Info "Wazuh GPG key downloaded successfully."
}

# Install Wazuh agent on Windows
function Install-WazuhAgent {
Log-Info "Installing Wazuh agent version $WAZUH_AGENT_VERSION on Windows"
$TEMP_DIR = [System.IO.Path]::GetTempPath()

# Download the Wazuh Agent installer
$Arch = if ([System.Environment]::Is64BitOperatingSystem) { "win64" } else { "win32" }
$InstallerUrl = "https://packages.wazuh.com/4.x/windows/wazuh-agent-$WAZUH_AGENT_VERSION.$Arch.msi"
$InstallerPath = "$TEMP_DIR\wazuh-agent-$WAZUH_AGENT_VERSION.$Arch.msi"

Invoke-WebRequest -Uri $InstallerUrl -OutFile $InstallerPath
if ($?) {
Log-Info "Wazuh agent installer downloaded to $InstallerPath"
} else {
Log-Error "Failed to download Wazuh agent installer from $InstallerUrl"
exit 1
}

# Install the Wazuh Agent MSI package
Start-Process msiexec.exe -ArgumentList "/i `"$InstallerPath`" /quiet /norestart" -Wait
if ($?) {
Log-Info "Wazuh agent installed successfully."
} else {
Log-Error "Failed to install Wazuh agent."
exit 1
}
}

# Configure Wazuh agent to connect to the manager
function Configure-WazuhAgent {
Log-Info "Configuring Wazuh agent to connect to manager $WAZUH_MANAGER"
$ConfigFilePath = "C:\Program Files (x86)\ossec-agent\ossec.conf"
# Function to install Wazuh Agent
function Install-Agent {

# Global variables
$YARA_SH_PATH = "C:\Program Files (x86)\ossec-agent\active-response\bin\yara.bat"
$OSSEC_CONF_PATH = "C:\Program Files (x86)\ossec-agent\ossec.conf"

# Function to install Wazuh agent
function Install-WazuhAgent {
# Variables
$WAZUH_MANAGER = "master.wazuh.adorsys.team"
$WAZUH_AGENT_VERSION = "4.8.1-1"
$WAZUH_AGENT_MSI = "wazuh-agent-${WAZUH_AGENT_VERSION}.msi"
$TEMP_DIR = $env:TEMP

# Get the agent name from environment variable
$WAZUH_AGENT_NAME = $env:WAZUH_AGENT_NAME
if (-not $WAZUH_AGENT_NAME) {
Write-Error "WAZUH_AGENT_NAME environment variable is not set."
exit 1
}

if (Test-Path $ConfigFilePath) {
[xml]$Config = Get-Content $ConfigFilePath
$ManagerNode = $Config.ossec_config.client.server
# Determine package URL based on architecture
$ARCH = [System.Environment]::Is64BitOperatingSystem

if ($ManagerNode) {
$ManagerNode.address = $WAZUH_MANAGER
$Config.Save($ConfigFilePath)
Log-Info "Wazuh agent configuration updated successfully."
if ($ARCH) {
$PACKAGE_URL = "https://packages.wazuh.com/4.x/windows/wazuh-agent-${WAZUH_AGENT_VERSION}-1.msi"
} else {
Log-Error "Failed to find the server node in the configuration file."
Write-Output "Unsupported architecture"
exit 1
}
} else {
Log-Error "Configuration file not found at $ConfigFilePath"
exit 1
}
}

# Start Wazuh agent service
function Start-WazuhAgentService {
Log-Info "Starting Wazuh agent service"
$ServiceName = "WazuhSvc"

if (Get-Service -Name $ServiceName -ErrorAction SilentlyContinue) {
Start-Service -Name $ServiceName
if ($?) {
Log-Info "Wazuh agent service started successfully."
} else {
Log-Error "Failed to start Wazuh agent service."
# Download the package
Write-Output "Downloading Wazuh agent..."
try {
Invoke-WebRequest -Uri $PACKAGE_URL -OutFile ${env.tmp}\wazuh-agent
} catch {
Write-Error "Failed to download Wazuh agent: $_"
exit 1
}
} else {
Log-Error "Wazuh agent service does not exist."
exit 1
}
}

# Clean up temporary files
function Clean-Up {
Log-Info "Cleaning up temporary files."
$TEMP_DIR = [System.IO.Path]::GetTempPath()
Remove-Item -Path "$TEMP_DIR\WazuhGPGKey.asc" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$TEMP_DIR\wazuh-agent-$WAZUH_AGENT_VERSION.$Arch.msi" -Force -ErrorAction SilentlyContinue
Log-Info "Temporary files cleaned up."
}
# Define the path to the MSI file
$msiPath = "${env.tmp}\wazuh-agent"
$MSIArguments = @(
"/i"
"${env.tmp}\wazuh-agent"
"/q"
"WAZUH_MANAGER=${WAZUH_MANAGER}"
"WAZUH_AGENT_NAME=${WAZUH_AGENT_NAME}"
)

# Install the package
Write-Output "Installing Wazuh agent..."
try {
Start-Process msiexec.exe -ArgumentList $MSIArguments -Wait
} catch {
Write-Error "Failed to install Wazuh agent: $_"
exit 1
}

# Main execution
try {
Import-Keys
Install-WazuhAgent
Configure-WazuhAgent
Start-WazuhAgentService
Clean-Up
} catch {
Log-Error "An error occurred: $_"
} finally {
# Pause to allow the user to see the output
Read-Host -Prompt "Press Enter to exit"
# Clean up
Write-Output "Cleaning up..."
try {
Remove-Item -Path ${env.tmp}\wazuh-agent -ErrorAction Stop
} catch {
Write-Warning "Failed to clean up the downloaded MSI file: $_"
}
Write-Output "Wazuh agent installed successfully!"
}
}

0 comments on commit 89a0069

Please sign in to comment.