Skip to content

Conversation

@KshitijaChoudhari
Copy link
Collaborator

@KshitijaChoudhari KshitijaChoudhari commented Jan 21, 2026

feat(run-tasks): add run tasks integration support

Description

This PR adds comprehensive support for Terraform Cloud/Enterprise Run Tasks Integration to the python-tfe SDK. This feature allows developers to create webhook servers that can validate Terraform runs and send results back to TFC/TFE, enabling custom policy enforcement, cost validation, security scanning, and approval workflows.

Testing plan

  • Unit Tests: 22 run tasks integration tests covering all models and callback functionality
  • Local Testing: Mock TFC/TFE server for development testing without real infrastructure
  • Integration Testing: Validated end-to-end flow with real HCP Terraform Cloud
  • Production Testing: Deployed to AWS EC2 and successfully processed real webhook callbacks
  • All Tests: 339/339 unit tests passing, full test suite validates implementation

External links

  • ** [API documentation]**

  • Run Tasks Integration API

  • Run Tasks Overview

  • Run Tasks API Reference

  • [Related PR]

  • [HashiCorp go-tfe SDK Run Tasks]
    (vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html)

  • [Terraform Cloud Run Tasks Documentation]
    (vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html)

Output from tests

Including output from tests may require access to a TFE instance. Ignore this section if you have no environment to test against.

Unit Tests (All Passing):

$ pytest tests/units/ -v
platform darwin -- Python 3.14.0, pytest-9.0.1, pluggy-1.6.0
collected 339 items

tests/units/test_run_tasks_integration.py::TestRunTaskRequest::test_run_task_request_minimal PASSED
tests/units/test_run_tasks_integration.py::TestRunTaskRequest::test_run_task_request_complete PASSED
tests/units/test_run_tasks_integration.py::TestTaskResultTag::test_tag_with_level PASSED
tests/units/test_run_tasks_integration.py::TestTaskResultTag::test_tag_without_level PASSED
tests/units/test_run_tasks_integration.py::TestTaskResultOutcome::test_outcome_complete PASSED
tests/units/test_run_tasks_integration.py::TestTaskResultOutcome::test_outcome_minimal PASSED
tests/units/test_run_tasks_integration.py::TestTaskResultCallbackOptions::test_callback_options_passed PASSED
tests/units/test_run_tasks_integration.py::TestTaskResultCallbackOptions::test_callback_options_with_outcomes PASSED
tests/units/test_run_tasks_integration.py::TestTaskResultCallbackOptions::test_validate_invalid_status PASSED
tests/units/test_run_tasks_integration.py::TestTaskResultCallbackOptions::test_validate_valid_statuses PASSED
tests/units/test_run_tasks_integration.py::TestRunTasksIntegration::test_callback_success PASSED
tests/units/test_run_tasks_integration.py::TestRunTasksIntegration::test_callback_empty_url PASSED
tests/units/test_run_tasks_integration.py::TestRunTasksIntegration::test_callback_empty_token PASSED
tests/units/test_run_tasks_integration.py::TestRunTasksIntegration::test_callback_invalid_status PASSED
tests/units/test_run_tasks_integration.py::TestRunTasksIntegration::test_callback_with_outcomes PASSED

Linting and Type Checks (All Passing):
$ make check
.venv/bin/python -m ruff format --check .
134 files already formatted
.venv/bin/python -m ruff check .
All checks passed!
.venv/bin/python -m mypy src/pytfe
Success: no issues found in 82 source files

Production Testing on AWS EC2 (PASSED):

Deployed to AWS EC2: :8888

$ python examples/run_tasks_integration.py --port 8888

Run Tasks Integration Callback Server

Listening on http://localhost:8888
Waiting for requests from TFC/TFE...
Received Run Task Request
Run ID: run-abc123def
Organization: my-test-org
Workspace: test-workspace
Stage: pre_plan
Callback URL: https://app.terraform.io/api/v2/task-results/...

[OK] Verification request detected - responding with 200 OK

Initializing TFEClient...
Access token from webhook: AtH4Xk2Y9Z***
Client initialized successfully
Sending callback to: https://app.terraform.io/api/v2/task-results...

[SUCCESS] Callback sent successfully: passed

Including output from tests may require access to a TFE instance. Ignore this section if you have no environment to test against.

Real HCP Terraform Cloud Integration (PASSED):

Run task created and configured successfully
Webhook received from actual TFC run trigger
Callback sent and acknowledged by TFC API
Results displayed correctly in TFC UI as "Advisory - Passed"
Complete end-to-end flow verified with production TFC instance

Local Mock Testing (PASSED):
Mock TFC/TFE received callback!
Status: passed
Message: All checks passed successfully
All tests completed successfully

Test Coverage Summary:

Unit Tests: 22/22 run tasks integration tests passing (100%)
Integration Tests: Local mock server validation successful
Production Testing: AWS EC2 deployment with real webhooks successful
End-to-End: Complete TFC/TFE integration flow validated
Code Quality: All linting, formatting, and type checks passing

Rollback Plan

Low Risk Assessment: This is an additive feature that introduces new functionality without modifying existing SDK behavior. No breaking changes to current users.

Rollback Strategy:

  1. Immediate Rollback (if critical issues discovered):

    • Revert commit 35d2296 from main branch
    • Remove files: examples/run_tasks_integration.py and docs/RUN_TASKS_INTEGRATION_EXAMPLE.md
    • Remove exports from src/pytfe/models/__init__.py for run tasks models
    • Timeline: Within 2 hours of issue detection
  2. Partial Rollback (if example issues only):

    • Remove example file only: examples/run_tasks_integration.py
    • Keep core SDK functionality intact
    • Update documentation to indicate feature is available but example is being revised
    • Timeline: Within 24 hours
  3. Monitoring Plan:

    • Monitor SDK installation/import success rates
    • Check for new GitHub issues related to run tasks integration
    • Verify existing functionality remains unaffected
    • Review package build and distribution processes
  4. Communication Plan:

    • Update GitHub issue/PR with rollback notice
    • Post notice in project documentation if needed
    • Notify users via release notes of any temporary removal

Risk Mitigation: Since this feature is purely additive and doesn't modify existing code paths, rollback risk is minimal. Existing users will not be affected by reverting these changes.

Changes to Security Controls

Breaking Changes

  • No breaking changes

All new functionality is additive and does not modify existing SDK behavior. Existing code continues to work unchanged.

Changes to Security Controls

No changes to existing security controls. This feature leverages existing SDK security patterns without modification.

Security Implementation Details:

  1. Token Handling:

    • Uses webhook-provided access tokens for authentication (not client tokens)
    • Follows existing SDK patterns for secure token transmission
    • Example code demonstrates proper token masking in logs (token[:10]***)
  2. Input Validation:

    • All webhook payloads validated using Pydantic models with strict typing
    • Prevents injection attacks through structured data validation
    • Invalid payloads are rejected with proper error handling
  3. Transport Security:

    • Leverages existing HTTPTransport with HTTPS encryption
    • No changes to TLS/SSL configuration or certificate handling
    • Maintains existing connection security standards
  4. Access Controls:

    • No modifications to existing authentication or authorization mechanisms
    • Feature is additive and doesn't alter existing access patterns
    • Uses same permission model as other SDK resources
  5. Logging Security:

    • Example demonstrates secure logging practices (token masking)
    • No sensitive data logged in plain text
    • Follows existing SDK logging patterns

Risk Assessment: Low security risk. Feature uses established, tested security controls from the existing SDK without introducing new attack vectors or modifying existing security mechanisms.

PCI review checklist

  • I have documented a clear reason for, and description of, the change I am making.

  • If applicable, I've documented a plan to revert these changes if they require more than reverting the pull request.

  • If applicable, I've documented the impact of any changes to security controls.

    Examples of changes to security controls include using new access control methods, adding or removing logging pipelines, etc.

If you have any questions, please contact your direct supervisor, GRC (#team-grc), or the PCI working group (#proj-pci-reboot). You can also find more information at PCI Compliance.

- Add RunTasksIntegration resource with callback method
- Add RunTaskRequest model for webhook payload parsing
- Add TaskResultCallbackOptions, TaskResultOutcome, TaskResultStatus models
- Add example Flask server for run tasks webhooks
- Add 15 unit tests for run tasks integration
- Update client to include run_tasks_integration property
- Export RunTaskRequest model
Flask dependency removed completely from the project
- Add task_result.py with TaskResult, TaskResultStatus, TaskEnforcementLevel models
- Add task_stages.py with TaskStage, Stage, TaskStageStatus, Actions, Permissions models
- Update run_task.py to import Stage and TaskEnforcementLevel from new modules (remove duplicates)
- Update run_tasks_integration.py to use TaskResultStatus enum from task_result
- Update run_task_request.py to add model_config for proper serialization
- Export all new models in __init__.py
- All 22 unit tests passing
- Matches go-tfe implementation structure
@KshitijaChoudhari KshitijaChoudhari requested a review from a team as a code owner January 21, 2026 06:49
- Add all new run tasks integration models to __all__ exports
- Fix trailing whitespace issues across multiple files
- Run ruff format to ensure consistent code style
- All 22 unit tests passing
- All linting checks pass
This commit adds comprehensive support for Terraform Cloud/Enterprise Run Tasks Integration to the python-tfe SDK. This feature allows developers to create webhook servers that can validate Terraform runs and send results back to TFC/TFE.

Key additions:
- Production-ready webhook server example with deployment instructions
- Complete documentation explaining architecture and flow
- Support for multiple cloud deployment platforms (AWS EC2, Heroku, GCP, etc.)
- Comprehensive validation examples (cost control, security, compliance)
- Clean implementation following HashiCorp patterns
@KshitijaChoudhari KshitijaChoudhari changed the title Feat/run tasks integration feat(run-tasks): add run tasks integration support Jan 27, 2026
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.

1 participant