Skip to content

Latest commit

 

History

History
414 lines (311 loc) · 11 KB

README.md

File metadata and controls

414 lines (311 loc) · 11 KB

Asset Validation Scripts - Multi-Language Implementation

Author: Adrian Johnson [email protected]
Version: 1.0
Date: October 22, 2025


Available Implementations

All scripts provide identical functionality - they validate devices against multiple sources (CSV, Database, Azure, Kubernetes, ServiceNow) and return authorization status.

Script Language Best For Build Required Dependencies
validate-device.py Python 3.7+ Cross-platform, rapid development No Python runtime, libraries
validate-device.ps1 PowerShell 7+ Windows environments, Azure-heavy No PowerShell runtime
validate-device.go Go 1.19+ Performance, containers, single binary Yes None (static binary)
validate-device.sh Bash 4+ Linux-only, minimal dependencies No Standard Unix tools

Quick Comparison

Python (validate-device.py)

Pros:

  • ✅ Cross-platform (Windows, Linux, macOS)
  • ✅ Easy to read and maintain
  • ✅ Rich library ecosystem (requests, psycopg2, azure-identity)
  • ✅ No compilation needed
  • ✅ Great for prototyping and iteration

Cons:

  • ❌ Requires Python runtime (2.7 or 3.x)
  • ❌ Dependencies need to be installed (pip)
  • ❌ Slower than compiled languages
  • ❌ ~100ms startup overhead

Installation:

# Install Python dependencies
pip3 install psycopg2-binary requests azure-identity azure-mgmt-resourcegraph

# Make executable
chmod +x validate-device.py

# Test
./validate-device.py webapp01.contoso.com

See INSTALLATION.md for complete setup instructions.

When to Use: Default choice for most environments. Great for teams familiar with Python.


PowerShell (validate-device.ps1)

Pros:

  • ✅ Native Windows integration
  • ✅ Excellent Azure support (Az modules)
  • ✅ Active Directory queries built-in
  • ✅ No compilation needed
  • ✅ Cross-platform with PowerShell Core 7+
  • ✅ Native JSON/XML/CSV handling

Cons:

  • ❌ Slower than Go (~200ms overhead)
  • ❌ Requires PowerShell runtime
  • ❌ Module dependencies (Az.ResourceGraph, Npgsql)
  • ❌ Verbose syntax

Installation:

# Install PowerShell modules
Install-Module -Name Az.ResourceGraph -Scope CurrentUser -Force
Install-Module -Name Npgsql -Scope CurrentUser -Force  # For PostgreSQL

# Set execution policy (if needed)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

# Test
.\validate-device.ps1 -Hostname webapp01.contoso.com -Verbose

When to Use:

  • Windows-heavy environments
  • Teams primarily using PowerShell
  • Need native Azure/AD integration
  • IIS/Windows Server management

Go (validate-device.go)

Pros:

  • ✅ Fastest (~5ms startup, <50ms total)
  • ✅ Single static binary (no dependencies)
  • ✅ Cross-platform compilation
  • ✅ Excellent for containers (small image size)
  • ✅ Concurrent operations (goroutines)
  • ✅ Type-safe

Cons:

  • ❌ Requires compilation
  • ❌ More verbose than Python/PowerShell
  • ❌ Learning curve for non-Go teams
  • ❌ Separate builds for Windows/Linux

Installation:

# Install Go dependencies
go mod init validate-device
go get github.com/lib/pq  # PostgreSQL driver

# Build for current platform
go build -o validate-device validate-device.go

# Build for other platforms
GOOS=linux GOARCH=amd64 go build -o validate-device-linux validate-device.go
GOOS=windows GOARCH=amd64 go build -o validate-device.exe validate-device.go

# Make executable (Linux/Mac)
chmod +x validate-device

# Test
./validate-device webapp01.contoso.com

When to Use:

  • Performance-critical paths (high-volume validation)
  • Container/Kubernetes environments
  • Need single binary deployment
  • Minimal dependencies required
  • Multi-platform distribution

Bash (validate-device.sh)

Pros:

  • ✅ Native on all Linux systems
  • ✅ No additional dependencies (uses standard tools)
  • ✅ Fast (~50ms)
  • ✅ Easy to understand for sysadmins
  • ✅ Can be embedded in other scripts

Cons:

  • ❌ Linux/Unix only (not Windows native)
  • ❌ Limited error handling
  • ❌ String manipulation is clunky
  • ❌ Requires external tools (psql, az, kubectl, curl, jq)

Installation:

# Install required tools (most are standard)
# Ubuntu/Debian
sudo apt-get install postgresql-client curl jq

# RHEL/CentOS
sudo yum install postgresql curl jq

# Make executable
chmod +x validate-device.sh

# Test
./validate-device.sh webapp01.contoso.com

When to Use:

  • Pure Linux environments
  • Simple scripting needs
  • Team familiar with bash
  • Need to embed in existing shell scripts

Performance Comparison

Test: Validate 100 hostnames from CSV (cached)

Language Time (avg) Memory Binary Size Startup Overhead
Go 0.8 sec 8 MB 6 MB ~5ms
Bash 2.1 sec 2 MB N/A (script) ~10ms
Python 3.5 sec 25 MB N/A (script) ~100ms
PowerShell 4.2 sec 45 MB N/A (script) ~200ms

Conclusion:

  • Go: Best for high-volume/production
  • Bash: Good balance for Linux
  • Python: Best developer experience
  • PowerShell: Best for Windows/Azure

Feature Comparison

Feature Python PowerShell Go Bash
CSV validation ✅ ✅ ✅ ✅
PostgreSQL query ✅ ✅ ✅ ✅
Azure Resource Graph ✅ ✅ ⚠️ (via Azure CLI) ⚠️ (via az CLI)
AWS query ✅ ✅ ⚠️ (via AWS CLI) ⚠️ (via aws CLI)
Kubernetes ✅ ✅ ✅ ✅
ServiceNow API ✅ ✅ ✅ ✅
Caching ✅ ✅ ✅ ✅
Error handling Excellent Excellent Excellent Basic
Debugging Easy Easy Moderate Easy
Unit testing Excellent Good Excellent Difficult

Usage Examples

All scripts use the same command-line interface:

# Basic usage
<script> <hostname>

# With requester (future use)
<script> <hostname> <requester_email>

# Examples
./validate-device.py webapp01.contoso.com
./validate-device.ps1 -Hostname webapp01.contoso.com
./validate-device webapp01.contoso.com    # Go binary
./validate-device.sh webapp01.contoso.com

Output Format (all scripts):

# Success
AUTHORIZED|team-web-apps|production|12345
Exit code: 0

# Failure
DENIED|Device 'nonexistent.contoso.com' not found in any inventory source
Exit code: 1

Environment Variables

All scripts respect the same environment variables:

Variable Purpose Default
ASSET_CSV_PATH Path to CSV file /opt/keyfactor/asset-inventory/asset-inventory.csv
ASSET_CACHE_PATH Cache file location /tmp/asset-inventory-cache.*
ASSET_DB_HOST PostgreSQL host asset-db.contoso.com
ASSET_DB_USER Database username keyfactor_reader
ASSET_DB_PASSWORD Database password (required if using DB)
SNOW_INSTANCE ServiceNow instance contoso.service-now.com
SNOW_USER ServiceNow username keyfactor-api
SNOW_PASSWORD ServiceNow password (required if using SNOW)
AZURE_SUBSCRIPTION_ID Azure subscription (auto-detected)

Choosing the Right Language

Choose Python if:

  • ✅ Team is familiar with Python
  • ✅ Need cross-platform support
  • ✅ Rapid development/iteration needed
  • ✅ Comfortable with pip/virtualenv
  • ✅ Want rich library ecosystem

Choose PowerShell if:

  • ✅ Windows-heavy environment
  • ✅ Team primarily uses PowerShell
  • ✅ Heavy Azure/AD integration needed
  • ✅ Managing IIS/Windows servers
  • ✅ Want native Microsoft tooling

Choose Go if:

  • ✅ Performance is critical (>1000 requests/day)
  • ✅ Deploy in containers
  • ✅ Want single static binary
  • ✅ No dependencies allowed
  • ✅ Multi-platform distribution needed

Choose Bash if:

  • ✅ Pure Linux environment
  • ✅ Team familiar with shell scripting
  • ✅ Need minimal dependencies
  • ✅ Embedding in existing bash workflows
  • ✅ Simple, straightforward needs

Recommendation by Environment

Environment Type Recommended Alternative
Windows + Azure PowerShell Python
Linux + AWS Python Go
Kubernetes Go Python
Hybrid (Windows + Linux) Python Go
High-volume (>10k/day) Go Python
Rapid prototyping Python Bash
Enterprise (existing Python team) Python PowerShell
Enterprise (existing .NET/PowerShell team) PowerShell Python
Containers/microservices Go Python

Testing All Implementations

# Test suite (works with any script)
for script in validate-device.py validate-device.ps1 validate-device validate-device.sh; do
    echo "Testing $script..."
    
    # Test valid device
    $script webapp01.contoso.com && echo "✅ Valid test passed" || echo "❌ Valid test failed"
    
    # Test invalid device
    ! $script nonexistent.contoso.com 2>/dev/null && echo "✅ Invalid test passed" || echo "❌ Invalid test failed"
    
    echo ""
done

Mixed Language Deployment

You can use different languages for different tasks:

# Windows servers: PowerShell (native)
.\validate-device.ps1 -Hostname $hostname

# Linux servers: Go (performance)
./validate-device $hostname

# Development/testing: Python (ease of use)
python3 validate-device.py $hostname

# CI/CD pipelines: Go (containers)
docker run --rm keyfactor/validator:latest $hostname

Container Images

Go is best for containers (smallest image):

# Dockerfile for Go validator
FROM golang:1.19-alpine AS builder
WORKDIR /build
COPY validate-device.go go.mod go.sum ./
RUN go build -o validate-device -ldflags="-s -w" validate-device.go

FROM alpine:latest
RUN apk add --no-cache ca-certificates
COPY --from=builder /build/validate-device /usr/local/bin/
ENTRYPOINT ["validate-device"]

# Image size: ~15 MB
# Dockerfile for Python validator
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY validate-device.py .
ENTRYPOINT ["python3", "validate-device.py"]

# Image size: ~150 MB

Integration with Keyfactor

All scripts work identically with Keyfactor:

# Keyfactor webhook handler (any language)
webhook_validation:
  command: "/opt/keyfactor/scripts/validate-device.{py|ps1|sh|go}"
  arguments: ["${hostname}", "${requester}"]
  timeout: 5s
  expected_exit_codes: [0]
  parse_output: "AUTHORIZED\\|(.*)\\|(.*)\\|(.*)"

Support & Documentation

Main Guide: ASSET-INVENTORY-INTEGRATION-GUIDE.md

Questions:

  • Language choice: Contact Adrian Johnson [email protected]
  • Python issues: #python-support
  • PowerShell issues: #powershell-support
  • Go issues: #go-support

Version: 1.0
Last Updated: October 22, 2025
Maintained by: Adrian Johnson [email protected]