Skip to content

Commit

Permalink
feat: spacelift_stack save md5 hash of import_state to state and forc…
Browse files Browse the repository at this point in the history
…e new
  • Loading branch information
Apollorion committed Nov 25, 2024
1 parent 0b0d800 commit 9b4b756
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
6 changes: 6 additions & 0 deletions spacelift/resource_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spacelift

import (
"context"
"crypto/md5"

Check failure on line 5 in spacelift/resource_stack.go

View workflow job for this annotation

GitHub Actions / Run linter

G501: Blocklisted import crypto/md5: weak cryptographic primitive (gosec)

Check failure on line 5 in spacelift/resource_stack.go

View workflow job for this annotation

GitHub Actions / Run linter

G501: Blocklisted import crypto/md5: weak cryptographic primitive (gosec)

Check failure

Code scanning / gosec

Blocklisted import crypto/md5: weak cryptographic primitive Error

Blocklisted import crypto/md5: weak cryptographic primitive
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -405,6 +406,7 @@ func resourceStack() *schema.Resource {
Optional: true,
DiffSuppressFunc: ignoreOnceCreated,
Sensitive: true,
ForceNew: true,
},
"import_state_file": {
Type: schema.TypeString,
Expand Down Expand Up @@ -662,6 +664,10 @@ func resourceStackCreate(ctx context.Context, d *schema.ResourceData, meta inter
return diag.Errorf(`"import_state" requires "manage_state" to be true`)
} else if ok {
stateContent = content.(string)

// Save the hash to state instead of the content to prevent large state files and memory usage
hash := fmt.Sprintf("%x", md5.Sum([]byte(stateContent)))

Check failure on line 669 in spacelift/resource_stack.go

View workflow job for this annotation

GitHub Actions / Run linter

G401: Use of weak cryptographic primitive (gosec)

Check failure

Code scanning / gosec

Use of weak cryptographic primitive Error

Use of weak cryptographic primitive
d.Set("import_state", hash)
}

path, ok := d.GetOk("import_state_file")
Expand Down
50 changes: 48 additions & 2 deletions spacelift/resource_stack_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spacelift

import (
"crypto/md5"
"fmt"
"regexp"
"testing"
Expand Down Expand Up @@ -331,7 +332,7 @@ func TestStackResource(t *testing.T) {
name = "labelled-module-%s"
branch = "master"
labels = []
repository = "terraform-bacon-tasty"
repository = "demo"
}`, randomID),
Check: Resource(
"spacelift_stack.test",
Expand Down Expand Up @@ -1482,7 +1483,7 @@ func TestStackResourceSpace(t *testing.T) {
name = "labelled-module-%s"
branch = "master"
labels = []
repository = "terraform-bacon-tasty"
repository = "demo"
}`, randomID),
Check: Resource(
"spacelift_stack.test",
Expand Down Expand Up @@ -1637,6 +1638,51 @@ func TestStackResourceSpace(t *testing.T) {
},
})
})

t.Run("with import_state", func(t *testing.T) {

var originalId string
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
hash := fmt.Sprintf("%x", md5.Sum([]byte("{}")))

testSteps(t, []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "spacelift_stack" "state_import" {
branch = "master"
name = "Provider test stack workflow_tool default %s"
project_root = "root"
repository = "demo"
import_state = "{}"
}
`, randomID),
Check: Resource(
"spacelift_stack.state_import",
Attribute("import_state", Equals(hash)),

func(attributes map[string]string) error {
originalId = attributes["id"]
return nil
},
),
},
{
Config: fmt.Sprintf(`
resource "spacelift_stack" "state_import" {
branch = "master"
name = "Provider test stack workflow_tool default %s"
project_root = "root"
repository = "demo"
import_state = "{\"import_state\": \"changed\"}"
}
`, randomID),
Check: Resource(
"spacelift_stack.state_import",
Attribute("id", NotEquals(originalId)),
),
},
})
})
}

// getConfig returns a stack config with injected vendor config
Expand Down

0 comments on commit 9b4b756

Please sign in to comment.