elastic / elastic/harp-plugins

harp-terraformer --out truncates incorrectly when overwriting existing file

Open
#57 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
2
Forks
6
Avg merge
7d 21h
Merged PRs (30d)
3

Description

## Bug

`harp-terraformer service --out ` overwrites the file in place without truncating first. If the new output is shorter than the existing file content, the leftover tail of the old content remains, producing corrupted Terraform output.

This affects **all** harp SDK commands that use `--out` on an existing file, not just `harp-terraformer`.

## Root cause

In `elastic/harp` — `pkg/sdk/cmdutil/io.go` (~line 97):

```go
os.OpenFile(name, os.O_CREATE|os.O_WRONLY, 0o400) // missing O_TRUNC
```

## Fix

```go
os.OpenFile(name, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o400)
```

## Steps to reproduce

1. Generate a large `.tf` file with `harp-terraformer service --out output.tf`
2. Modify the spec to produce a smaller output
3. Re-run with the same `--out output.tf`
4. Observe that the tail of the previous file content remains appended to the new output

## Workaround

Skip `--out` and use shell redirection instead (which truncates by default):
```bash
harp-terraformer service --spec spec.yaml --env production > output.tf
```

## Notes

- The fix is in `elastic/harp` (the SDK), but filing here per maintainer guidance — this repo is the appropriate place to track validation and testing.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.