Skip to content

[BUG] Strip ANSI escape codes from Terraform/Ansible output in API and logs (fixes #1366, fixes #1367)#1401

Open
cray44 wants to merge 1 commit intosplunk:developfrom
cray44:bugfix/1366-strip-ansi-escape-codes
Open

[BUG] Strip ANSI escape codes from Terraform/Ansible output in API and logs (fixes #1366, fixes #1367)#1401
cray44 wants to merge 1 commit intosplunk:developfrom
cray44:bugfix/1366-strip-ansi-escape-codes

Conversation

@cray44
Copy link
Copy Markdown

@cray44 cray44 commented May 8, 2026

Description

Fixes #1366 and fixes #1367

Terminal color codes (ANSI escape sequences like \x1b[31m) from Terraform and Ansible output were flowing through to the web app logs and API error responses, making them unreadable. Additionally, colored Terraform output was not being properly captured as error text, causing the API to report "No error output from terraform" even when errors were visible in Docker logs.

Changes

Prevention (suppress ANSI at the source):

  • terraform_manager.py: Changed no_color=IsNotFlagged to no_color=IsFlagged in apply() and destroy() calls, which passes --no-color to Terraform
  • ansible_manager.py: Set ANSIBLE_NO_COLOR=1 in environment variables passed to ansible_runner.run()

Defense in depth (strip ANSI wherever output enters the system):

  • utils.py: Added strip_ansi() function using regex pattern \x1b\[[0-9;]*[a-zA-Z] to remove all ANSI escape sequences
  • terraform_manager.py: Strip ANSI from stdout/stderr after apply(), destroy(), and init() calls, and from retry outputs after state lock unlock
  • ansible_manager.py: Strip ANSI from stdout artifact files, event error messages, and Ansible event msg fields
  • app.py: Strip ANSI from exception messages and tracebacks at all three error-capture boundaries (build_vpn, build_lab, destroy)

Testing

  • Python syntax verified (py_compile passes for all 4 modified files)
  • strip_ansi() unit tested: correctly strips \x1b[31m and other ANSI codes from strings
  • All imports resolve correctly
  • Manual testing requires cloud credentials (AWS/Azure/GCP) — not tested end-to-end

Checklist

  • CLA signed
  • Branch based on develop
  • No hardcoded secrets or credentials
  • Commit message references issue numbers
  • Code follows existing style conventions

…d logs (fixes splunk#1366)

- Add strip_ansi() utility to attack_range/utils.py using regex
- Terraform: pass --no-color via no_color=IsFlagged (also fixes splunk#1367
  where colored output was not captured as error text)
- Terraform: strip ANSI from init/apply/destroy output before
  logging or raising RuntimeError
- Ansible: set ANSIBLE_NO_COLOR=1 via envvars in ansible_runner.run()
- Ansible: strip ANSI from stdout artifact files and event messages
  before logging
- API: strip ANSI from exception messages and tracebacks at all error
  capture points (build_vpn, build_lab, destroy)

Defense in depth: ANSI codes are suppressed at the source (flags) AND
stripped at every boundary where they enter the system.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Terrafrom error content not detected in API [BUG] Terminal colors encodings visible in app logs

1 participant