Skip to content

Commit 465b7c7

Browse files
committed
Add more hash outputs of archive content
Per hashicorp/terraform-provider-local#137 this brings the provider up to feature parity with all of Terraform's built-in file-reading functions The following outputs are added to `data_archive_file`: * `output_sha256` - SHA256 checksum of archive content. * `output_sha512` - SHA512 checksum of archive content. * `output_base64sha512` - Base64 encoded SHA512 checksum of archive content.
1 parent 7ce0088 commit 465b7c7

File tree

2 files changed

+88
-26
lines changed

2 files changed

+88
-26
lines changed

internal/provider/data_source_archive_file.go

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/md5"
66
"crypto/sha1"
77
"crypto/sha256"
8+
"crypto/sha512"
89
"encoding/base64"
910
"encoding/hex"
1011
"fmt"
@@ -95,23 +96,41 @@ func dataSourceFile() *schema.Resource {
9596
Computed: true,
9697
ForceNew: true,
9798
},
99+
"output_md5": {
100+
Type: schema.TypeString,
101+
Computed: true,
102+
ForceNew: true,
103+
Description: "MD5 of output file",
104+
},
98105
"output_sha": {
99106
Type: schema.TypeString,
100107
Computed: true,
101108
ForceNew: true,
102109
Description: "SHA1 checksum of output file",
103110
},
111+
"output_sha256": {
112+
Type: schema.TypeString,
113+
Computed: true,
114+
ForceNew: true,
115+
Description: "SHA256 checksum of output file",
116+
},
104117
"output_base64sha256": {
105118
Type: schema.TypeString,
106119
Computed: true,
107120
ForceNew: true,
108121
Description: "Base64 Encoded SHA256 checksum of output file",
109122
},
110-
"output_md5": {
123+
"output_sha512": {
111124
Type: schema.TypeString,
112125
Computed: true,
113126
ForceNew: true,
114-
Description: "MD5 of output file",
127+
Description: "SHA512 checksum of output file",
128+
},
129+
"output_base64sha512": {
130+
Type: schema.TypeString,
131+
Computed: true,
132+
ForceNew: true,
133+
Description: "Base64 Encoded SHA512 checksum of output file",
115134
},
116135
"output_file_mode": {
117136
Type: schema.TypeString,
@@ -145,14 +164,17 @@ func dataSourceFileRead(d *schema.ResourceData, meta interface{}) error {
145164
return err
146165
}
147166

148-
sha1, base64sha256, md5, err := genFileShas(outputPath)
167+
checksums, err := genFileChecksums(outputPath)
149168
if err != nil {
150-
return fmt.Errorf("could not generate file checksum sha256: %s", err)
169+
return fmt.Errorf("could not generate file checksums: %s", err)
151170
}
152171

153-
d.Set("output_sha", sha1)
154-
d.Set("output_base64sha256", base64sha256)
155-
d.Set("output_md5", md5)
172+
d.Set("output_md5", checksums.md5Hex)
173+
d.Set("output_sha", checksums.sha1Hex)
174+
d.Set("output_sha256", checksums.sha256Hex)
175+
d.Set("output_base64sha256", checksums.sha256Base64)
176+
d.Set("output_sha512", checksums.sha512Hex)
177+
d.Set("output_base64sha512", checksums.sha512Base64)
156178
d.Set("output_size", fi.Size())
157179
d.SetId(d.Get("output_sha").(string))
158180

@@ -218,23 +240,36 @@ func archive(d *schema.ResourceData) error {
218240
return nil
219241
}
220242

221-
func genFileShas(filename string) (string, string, string, error) {
243+
type fileChecksums struct {
244+
md5Hex string
245+
sha1Hex string
246+
sha256Hex string
247+
sha256Base64 string
248+
sha512Hex string
249+
sha512Base64 string
250+
}
251+
252+
func genFileChecksums(filename string) (fileChecksums, error) {
253+
checksums := fileChecksums{}
254+
222255
data, err := ioutil.ReadFile(filename)
223256
if err != nil {
224-
return "", "", "", fmt.Errorf("could not compute file '%s' checksum: %s", filename, err)
257+
return checksums, fmt.Errorf("could not compute file '%s' checksum: %s", filename, err)
225258
}
226-
h := sha1.New()
227-
h.Write([]byte(data))
228-
sha1 := hex.EncodeToString(h.Sum(nil))
229259

230-
h256 := sha256.New()
231-
h256.Write([]byte(data))
232-
shaSum := h256.Sum(nil)
233-
sha256base64 := base64.StdEncoding.EncodeToString(shaSum[:])
260+
md5Sum := md5.Sum(data)
261+
checksums.md5Hex = hex.EncodeToString(md5Sum[:])
262+
263+
sha1Sum := sha1.Sum(data)
264+
checksums.sha1Hex = hex.EncodeToString(sha1Sum[:])
265+
266+
sha256Sum := sha256.Sum256(data)
267+
checksums.sha256Hex = hex.EncodeToString(sha256Sum[:])
268+
checksums.sha256Base64 = base64.StdEncoding.EncodeToString(sha256Sum[:])
234269

235-
md5 := md5.New()
236-
md5.Write([]byte(data))
237-
md5Sum := hex.EncodeToString(md5.Sum(nil))
270+
sha512Sum := sha512.Sum512(data)
271+
checksums.sha512Hex = hex.EncodeToString(sha512Sum[:])
272+
checksums.sha512Base64 = base64.StdEncoding.EncodeToString(sha512Sum[:])
238273

239-
return sha1, sha256base64, md5Sum, nil
274+
return checksums, nil
240275
}

internal/provider/data_source_archive_file_test.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,23 @@ func TestAccArchiveFile_Basic(t *testing.T) {
2727
testAccArchiveFileExists(f, &fileSize),
2828
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
2929

30+
r.TestCheckResourceAttr(
31+
"data.archive_file.foo", "output_md5", "ea35f0444ea9a3d5641d8760bc2815cc",
32+
),
33+
r.TestCheckResourceAttr(
34+
"data.archive_file.foo", "output_sha", "019c79c4dc14dbe1edb3e467b2de6a6aad148717",
35+
),
36+
r.TestCheckResourceAttr(
37+
"data.archive_file.foo", "output_sha256", "3fb55c931a048943b8d7558dde7c2e4bfc8e04be33b1b55691053d1352391fa7",
38+
),
3039
r.TestCheckResourceAttr(
3140
"data.archive_file.foo", "output_base64sha256", "P7VckxoEiUO411WN3nwuS/yOBL4zsbVWkQU9E1I5H6c=",
3241
),
3342
r.TestCheckResourceAttr(
34-
"data.archive_file.foo", "output_md5", "ea35f0444ea9a3d5641d8760bc2815cc",
43+
"data.archive_file.foo", "output_sha512", "57e2d073dce214609bd61113b90b0b2b7c75034047224d56e35f363c8f2662e3acd561eebf94826a67453411181eca7e1cbf15db1f2fdd496cf13df46b7848c3",
3544
),
3645
r.TestCheckResourceAttr(
37-
"data.archive_file.foo", "output_sha", "019c79c4dc14dbe1edb3e467b2de6a6aad148717",
46+
"data.archive_file.foo", "output_base64sha512", "V+LQc9ziFGCb1hETuQsLK3x1A0BHIk1W4182PI8mYuOs1WHuv5SCamdFNBEYHsp+HL8V2x8v3Uls8T30a3hIww==",
3847
),
3948
),
4049
},
@@ -43,14 +52,23 @@ func TestAccArchiveFile_Basic(t *testing.T) {
4352
Check: r.ComposeTestCheckFunc(
4453
testAccArchiveFileExists(f, &fileSize),
4554
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
55+
r.TestCheckResourceAttr(
56+
"data.archive_file.foo", "output_md5", "59fbc9e62af3cbc2f588f97498240dae",
57+
),
58+
r.TestCheckResourceAttr(
59+
"data.archive_file.foo", "output_sha", "ce4ee1450ab93ac86e11446649e44cea907b6568",
60+
),
61+
r.TestCheckResourceAttr(
62+
"data.archive_file.foo", "output_sha256", "5131387f97167da47aa741df3ab2c82f182f17c514c222538d34708d04e0756b",
63+
),
4664
r.TestCheckResourceAttr(
4765
"data.archive_file.foo", "output_base64sha256", "UTE4f5cWfaR6p0HfOrLILxgvF8UUwiJTjTRwjQTgdWs=",
4866
),
4967
r.TestCheckResourceAttr(
50-
"data.archive_file.foo", "output_md5", "59fbc9e62af3cbc2f588f97498240dae",
68+
"data.archive_file.foo", "output_sha512", "eb33eb0f8cd8efe1a5a0b99acbd22ed22dbebb80817f8de6e8fed15c21c52240838d9bb46fb0938846c74f694425551ba60829a6396f91fcfe49d21a1e3bb409",
5169
),
5270
r.TestCheckResourceAttr(
53-
"data.archive_file.foo", "output_sha", "ce4ee1450ab93ac86e11446649e44cea907b6568",
71+
"data.archive_file.foo", "output_base64sha512", "6zPrD4zY7+GloLmay9Iu0i2+u4CBf43m6P7RXCHFIkCDjZu0b7CTiEbHT2lEJVUbpggppjlvkfz+SdIaHju0CQ==",
5472
),
5573
),
5674
},
@@ -59,14 +77,23 @@ func TestAccArchiveFile_Basic(t *testing.T) {
5977
Check: r.ComposeTestCheckFunc(
6078
testAccArchiveFileExists(f, &fileSize),
6179
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
80+
r.TestCheckResourceAttr(
81+
"data.archive_file.foo", "output_md5", "b73f64a383716070aa4a29563b8b14d4",
82+
),
83+
r.TestCheckResourceAttr(
84+
"data.archive_file.foo", "output_sha", "76d20a402eefd1cfbdc47886abd4e0909616c191",
85+
),
86+
r.TestCheckResourceAttr(
87+
"data.archive_file.foo", "output_sha256", "c9d07cc2dabc9caf6f43bed51fa613c281e6ca58cae3a8d6fae2094b00b3369a",
88+
),
6289
r.TestCheckResourceAttr(
6390
"data.archive_file.foo", "output_base64sha256", "ydB8wtq8nK9vQ77VH6YTwoHmyljK46jW+uIJSwCzNpo=",
6491
),
6592
r.TestCheckResourceAttr(
66-
"data.archive_file.foo", "output_md5", "b73f64a383716070aa4a29563b8b14d4",
93+
"data.archive_file.foo", "output_sha512", "b96ac6b9554a04473a733be36f190422bf7162b4afdb211a0f551713eadf4092459426750646c70383ce6c8b89171b88582a608e5841bfaaafa17004a2a2ca0a",
6794
),
6895
r.TestCheckResourceAttr(
69-
"data.archive_file.foo", "output_sha", "76d20a402eefd1cfbdc47886abd4e0909616c191",
96+
"data.archive_file.foo", "output_base64sha512", "uWrGuVVKBEc6czvjbxkEIr9xYrSv2yEaD1UXE+rfQJJFlCZ1BkbHA4PObIuJFxuIWCpgjlhBv6qvoXAEoqLKCg==",
7097
),
7198
),
7299
},

0 commit comments

Comments
 (0)