Skip to content

Commit 55bc1f6

Browse files
authored
Merge pull request #165 from zachwhaley/checksums
Add more hash outputs of archive content
2 parents b54c6dc + 7f8946a commit 55bc1f6

File tree

6 files changed

+174
-69
lines changed

6 files changed

+174
-69
lines changed

docs/data-sources/file.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ data "archive_file" "lambda_my_function" {
8080
### Read-Only
8181

8282
- `id` (String) The sha1 checksum hash of the output.
83-
- `output_base64sha256` (String) The base64-encoded SHA256 checksum of output archive file.
84-
- `output_md5` (String) The MD5 checksum of output archive file.
85-
- `output_sha` (String) The SHA1 checksum of output archive file.
83+
- `output_base64sha256` (String) Base64 Encoded SHA256 checksum of output file
84+
- `output_base64sha512` (String) Base64 Encoded SHA512 checksum of output file
85+
- `output_md5` (String) MD5 of output file
86+
- `output_sha` (String) SHA1 checksum of output file
87+
- `output_sha256` (String) SHA256 checksum of output file
88+
- `output_sha512` (String) SHA512 checksum of output file
8689
- `output_size` (Number) The byte size of the output archive file.
8790

8891
<a id="nestedblock--source"></a>

docs/resources/file.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ description: |-
3333
### Read-Only
3434

3535
- `id` (String) The sha1 checksum hash of the output.
36-
- `output_base64sha256` (String) The base64-encoded SHA256 checksum of output archive file.
37-
- `output_md5` (String) The MD5 checksum of output archive file.
38-
- `output_sha` (String) The SHA1 checksum of output archive file.
36+
- `output_base64sha256` (String) Base64 Encoded SHA256 checksum of output file
37+
- `output_base64sha512` (String) Base64 Encoded SHA512 checksum of output file
38+
- `output_md5` (String) MD5 of output file
39+
- `output_sha` (String) SHA1 checksum of output file
40+
- `output_sha256` (String) SHA256 checksum of output file
41+
- `output_sha512` (String) SHA512 checksum of output file
3942
- `output_size` (Number) The byte size of the output archive file.
4043

4144
<a id="nestedblock--source"></a>

internal/provider/data_source_archive_file.go

Lines changed: 63 additions & 32 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"
@@ -149,23 +150,35 @@ func (d *archiveFileDataSource) Schema(ctx context.Context, req datasource.Schem
149150
Description: "The byte size of the output archive file.",
150151
Computed: true,
151152
},
153+
"output_file_mode": schema.StringAttribute{
154+
Description: "String that specifies the octal file mode for all archived files. For example: `\"0666\"`. " +
155+
"Setting this will ensure that cross platform usage of this module will not vary the modes of archived " +
156+
"files (and ultimately checksums) resulting in more deterministic behavior.",
157+
Optional: true,
158+
},
159+
"output_md5": schema.StringAttribute{
160+
Description: "MD5 of output file",
161+
Computed: true,
162+
},
152163
"output_sha": schema.StringAttribute{
153-
Description: "The SHA1 checksum of output archive file.",
164+
Description: "SHA1 checksum of output file",
165+
Computed: true,
166+
},
167+
"output_sha256": schema.StringAttribute{
168+
Description: "SHA256 checksum of output file",
154169
Computed: true,
155170
},
156171
"output_base64sha256": schema.StringAttribute{
157-
Description: "The base64-encoded SHA256 checksum of output archive file.",
172+
Description: "Base64 Encoded SHA256 checksum of output file",
158173
Computed: true,
159174
},
160-
"output_md5": schema.StringAttribute{
161-
Description: "The MD5 checksum of output archive file.",
175+
"output_sha512": schema.StringAttribute{
176+
Description: "SHA512 checksum of output file",
162177
Computed: true,
163178
},
164-
"output_file_mode": schema.StringAttribute{
165-
Description: "String that specifies the octal file mode for all archived files. For example: `\"0666\"`. " +
166-
"Setting this will ensure that cross platform usage of this module will not vary the modes of archived " +
167-
"files (and ultimately checksums) resulting in more deterministic behavior.",
168-
Optional: true,
179+
"output_base64sha512": schema.StringAttribute{
180+
Description: "Base64 Encoded SHA512 checksum of output file",
181+
Computed: true,
169182
},
170183
},
171184
}
@@ -269,21 +282,23 @@ func (d *archiveFileDataSource) Read(ctx context.Context, req datasource.ReadReq
269282
)
270283
return
271284
}
285+
model.OutputSize = types.Int64Value(fi.Size())
272286

273-
sha1, base64sha256, md5, err := genFileShas(outputPath)
287+
checksums, err := genFileChecksums(outputPath)
274288
if err != nil {
275289
resp.Diagnostics.AddError(
276290
"Hash generation error",
277-
fmt.Sprintf("error generating hashed: %s", err),
291+
fmt.Sprintf("error generating checksums: %s", err),
278292
)
279293
}
294+
model.OutputMd5 = types.StringValue(checksums.md5Hex)
295+
model.OutputSha = types.StringValue(checksums.sha1Hex)
296+
model.OutputSha256 = types.StringValue(checksums.sha256Hex)
297+
model.OutputBase64Sha256 = types.StringValue(checksums.sha256Base64)
298+
model.OutputSha512 = types.StringValue(checksums.sha512Hex)
299+
model.OutputBase64Sha512 = types.StringValue(checksums.sha512Base64)
280300

281-
model.OutputSha = types.StringValue(sha1)
282-
model.OutputBase64Sha256 = types.StringValue(base64sha256)
283-
model.OutputMd5 = types.StringValue(md5)
284-
model.OutputSize = types.Int64Value(fi.Size())
285-
286-
model.ID = types.StringValue(sha1)
301+
model.ID = types.StringValue(checksums.sha1Hex)
287302

288303
diags = resp.State.Set(ctx, model)
289304
resp.Diagnostics.Append(diags...)
@@ -304,34 +319,50 @@ type fileModel struct {
304319
Excludes types.Set `tfsdk:"excludes"`
305320
OutputPath types.String `tfsdk:"output_path"`
306321
OutputSize types.Int64 `tfsdk:"output_size"`
322+
OutputFileMode types.String `tfsdk:"output_file_mode"`
323+
OutputMd5 types.String `tfsdk:"output_md5"`
307324
OutputSha types.String `tfsdk:"output_sha"`
325+
OutputSha256 types.String `tfsdk:"output_sha256"`
308326
OutputBase64Sha256 types.String `tfsdk:"output_base64sha256"`
309-
OutputMd5 types.String `tfsdk:"output_md5"`
310-
OutputFileMode types.String `tfsdk:"output_file_mode"`
327+
OutputSha512 types.String `tfsdk:"output_sha512"`
328+
OutputBase64Sha512 types.String `tfsdk:"output_base64sha512"`
311329
}
312330

313331
type sourceModel struct {
314332
Content types.String `tfsdk:"content"`
315333
Filename types.String `tfsdk:"filename"`
316334
}
317335

318-
func genFileShas(filename string) (string, string, string, error) {
336+
type fileChecksums struct {
337+
md5Hex string
338+
sha1Hex string
339+
sha256Hex string
340+
sha256Base64 string
341+
sha512Hex string
342+
sha512Base64 string
343+
}
344+
345+
func genFileChecksums(filename string) (fileChecksums, error) {
346+
var checksums fileChecksums
347+
319348
data, err := os.ReadFile(filename)
320349
if err != nil {
321-
return "", "", "", fmt.Errorf("could not compute file '%s' checksum: %s", filename, err)
350+
return checksums, fmt.Errorf("could not compute file '%s' checksum: %s", filename, err)
322351
}
323-
h := sha1.New()
324-
h.Write(data)
325-
sha1 := hex.EncodeToString(h.Sum(nil))
326352

327-
h256 := sha256.New()
328-
h256.Write(data)
329-
shaSum := h256.Sum(nil)
330-
sha256base64 := base64.StdEncoding.EncodeToString(shaSum[:])
353+
md5Sum := md5.Sum(data)
354+
checksums.md5Hex = hex.EncodeToString(md5Sum[:])
355+
356+
sha1Sum := sha1.Sum(data)
357+
checksums.sha1Hex = hex.EncodeToString(sha1Sum[:])
358+
359+
sha256Sum := sha256.Sum256(data)
360+
checksums.sha256Hex = hex.EncodeToString(sha256Sum[:])
361+
checksums.sha256Base64 = base64.StdEncoding.EncodeToString(sha256Sum[:])
331362

332-
md5 := md5.New()
333-
md5.Write(data)
334-
md5Sum := hex.EncodeToString(md5.Sum(nil))
363+
sha512Sum := sha512.Sum512(data)
364+
checksums.sha512Hex = hex.EncodeToString(sha512Sum[:])
365+
checksums.sha512Base64 = base64.StdEncoding.EncodeToString(sha512Sum[:])
335366

336-
return sha1, sha256base64, md5Sum, nil
367+
return checksums, nil
337368
}

internal/provider/data_source_archive_file_test.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,23 @@ func TestAccArchiveFile_Basic(t *testing.T) {
2525
Check: r.ComposeTestCheckFunc(
2626
testAccArchiveFileSize(f, &fileSize),
2727
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
28+
r.TestCheckResourceAttr(
29+
"data.archive_file.foo", "output_md5", "ea35f0444ea9a3d5641d8760bc2815cc",
30+
),
31+
r.TestCheckResourceAttr(
32+
"data.archive_file.foo", "output_sha", "019c79c4dc14dbe1edb3e467b2de6a6aad148717",
33+
),
34+
r.TestCheckResourceAttr(
35+
"data.archive_file.foo", "output_sha256", "3fb55c931a048943b8d7558dde7c2e4bfc8e04be33b1b55691053d1352391fa7",
36+
),
2837
r.TestCheckResourceAttr(
2938
"data.archive_file.foo", "output_base64sha256", "P7VckxoEiUO411WN3nwuS/yOBL4zsbVWkQU9E1I5H6c=",
3039
),
3140
r.TestCheckResourceAttr(
32-
"data.archive_file.foo", "output_md5", "ea35f0444ea9a3d5641d8760bc2815cc",
41+
"data.archive_file.foo", "output_sha512", "57e2d073dce214609bd61113b90b0b2b7c75034047224d56e35f363c8f2662e3acd561eebf94826a67453411181eca7e1cbf15db1f2fdd496cf13df46b7848c3",
3342
),
3443
r.TestCheckResourceAttr(
35-
"data.archive_file.foo", "output_sha", "019c79c4dc14dbe1edb3e467b2de6a6aad148717",
44+
"data.archive_file.foo", "output_base64sha512", "V+LQc9ziFGCb1hETuQsLK3x1A0BHIk1W4182PI8mYuOs1WHuv5SCamdFNBEYHsp+HL8V2x8v3Uls8T30a3hIww==",
3645
),
3746
),
3847
},
@@ -41,14 +50,23 @@ func TestAccArchiveFile_Basic(t *testing.T) {
4150
Check: r.ComposeTestCheckFunc(
4251
testAccArchiveFileSize(f, &fileSize),
4352
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
53+
r.TestCheckResourceAttr(
54+
"data.archive_file.foo", "output_md5", "59fbc9e62af3cbc2f588f97498240dae",
55+
),
56+
r.TestCheckResourceAttr(
57+
"data.archive_file.foo", "output_sha", "ce4ee1450ab93ac86e11446649e44cea907b6568",
58+
),
59+
r.TestCheckResourceAttr(
60+
"data.archive_file.foo", "output_sha256", "5131387f97167da47aa741df3ab2c82f182f17c514c222538d34708d04e0756b",
61+
),
4462
r.TestCheckResourceAttr(
4563
"data.archive_file.foo", "output_base64sha256", "UTE4f5cWfaR6p0HfOrLILxgvF8UUwiJTjTRwjQTgdWs=",
4664
),
4765
r.TestCheckResourceAttr(
48-
"data.archive_file.foo", "output_md5", "59fbc9e62af3cbc2f588f97498240dae",
66+
"data.archive_file.foo", "output_sha512", "eb33eb0f8cd8efe1a5a0b99acbd22ed22dbebb80817f8de6e8fed15c21c52240838d9bb46fb0938846c74f694425551ba60829a6396f91fcfe49d21a1e3bb409",
4967
),
5068
r.TestCheckResourceAttr(
51-
"data.archive_file.foo", "output_sha", "ce4ee1450ab93ac86e11446649e44cea907b6568",
69+
"data.archive_file.foo", "output_base64sha512", "6zPrD4zY7+GloLmay9Iu0i2+u4CBf43m6P7RXCHFIkCDjZu0b7CTiEbHT2lEJVUbpggppjlvkfz+SdIaHju0CQ==",
5270
),
5371
),
5472
},
@@ -57,14 +75,23 @@ func TestAccArchiveFile_Basic(t *testing.T) {
5775
Check: r.ComposeTestCheckFunc(
5876
testAccArchiveFileSize(f, &fileSize),
5977
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
78+
r.TestCheckResourceAttr(
79+
"data.archive_file.foo", "output_md5", "b73f64a383716070aa4a29563b8b14d4",
80+
),
81+
r.TestCheckResourceAttr(
82+
"data.archive_file.foo", "output_sha", "76d20a402eefd1cfbdc47886abd4e0909616c191",
83+
),
84+
r.TestCheckResourceAttr(
85+
"data.archive_file.foo", "output_sha256", "c9d07cc2dabc9caf6f43bed51fa613c281e6ca58cae3a8d6fae2094b00b3369a",
86+
),
6087
r.TestCheckResourceAttr(
6188
"data.archive_file.foo", "output_base64sha256", "ydB8wtq8nK9vQ77VH6YTwoHmyljK46jW+uIJSwCzNpo=",
6289
),
6390
r.TestCheckResourceAttr(
64-
"data.archive_file.foo", "output_md5", "b73f64a383716070aa4a29563b8b14d4",
91+
"data.archive_file.foo", "output_sha512", "b96ac6b9554a04473a733be36f190422bf7162b4afdb211a0f551713eadf4092459426750646c70383ce6c8b89171b88582a608e5841bfaaafa17004a2a2ca0a",
6592
),
6693
r.TestCheckResourceAttr(
67-
"data.archive_file.foo", "output_sha", "76d20a402eefd1cfbdc47886abd4e0909616c191",
94+
"data.archive_file.foo", "output_base64sha512", "uWrGuVVKBEc6czvjbxkEIr9xYrSv2yEaD1UXE+rfQJJFlCZ1BkbHA4PObIuJFxuIWCpgjlhBv6qvoXAEoqLKCg==",
6895
),
6996
),
7097
},

internal/provider/resource_archive_file.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,6 @@ func (d *archiveFileResource) Schema(ctx context.Context, req resource.SchemaReq
176176
Description: "The byte size of the output archive file.",
177177
Computed: true,
178178
},
179-
"output_sha": schema.StringAttribute{
180-
Description: "The SHA1 checksum of output archive file.",
181-
Computed: true,
182-
},
183-
"output_base64sha256": schema.StringAttribute{
184-
Description: "The base64-encoded SHA256 checksum of output archive file.",
185-
Computed: true,
186-
},
187-
"output_md5": schema.StringAttribute{
188-
Description: "The MD5 checksum of output archive file.",
189-
Computed: true,
190-
},
191179
"output_file_mode": schema.StringAttribute{
192180
Description: "String that specifies the octal file mode for all archived files. For example: `\"0666\"`. " +
193181
"Setting this will ensure that cross platform usage of this module will not vary the modes of archived " +
@@ -197,6 +185,30 @@ func (d *archiveFileResource) Schema(ctx context.Context, req resource.SchemaReq
197185
stringplanmodifier.RequiresReplace(),
198186
},
199187
},
188+
"output_md5": schema.StringAttribute{
189+
Description: "MD5 of output file",
190+
Computed: true,
191+
},
192+
"output_sha": schema.StringAttribute{
193+
Description: "SHA1 checksum of output file",
194+
Computed: true,
195+
},
196+
"output_sha256": schema.StringAttribute{
197+
Description: "SHA256 checksum of output file",
198+
Computed: true,
199+
},
200+
"output_base64sha256": schema.StringAttribute{
201+
Description: "Base64 Encoded SHA256 checksum of output file",
202+
Computed: true,
203+
},
204+
"output_sha512": schema.StringAttribute{
205+
Description: "SHA512 checksum of output file",
206+
Computed: true,
207+
},
208+
"output_base64sha512": schema.StringAttribute{
209+
Description: "Base64 Encoded SHA512 checksum of output file",
210+
Computed: true,
211+
},
200212
},
201213
}
202214
}
@@ -263,22 +275,24 @@ func updateModel(ctx context.Context, model *fileModel) diag.Diagnostics {
263275
)
264276
return diags
265277
}
278+
model.OutputSize = types.Int64Value(fi.Size())
266279

267-
sha1, base64sha256, md5, err := genFileShas(outputPath)
280+
checksums, err := genFileChecksums(outputPath)
268281
if err != nil {
269282
diags.AddError(
270283
"Hash generation error",
271284
fmt.Sprintf("error generating hashed: %s", err),
272285
)
273286
return diags
274287
}
288+
model.OutputMd5 = types.StringValue(checksums.md5Hex)
289+
model.OutputSha = types.StringValue(checksums.sha1Hex)
290+
model.OutputSha256 = types.StringValue(checksums.sha256Hex)
291+
model.OutputBase64Sha256 = types.StringValue(checksums.sha256Base64)
292+
model.OutputSha512 = types.StringValue(checksums.sha512Hex)
293+
model.OutputBase64Sha512 = types.StringValue(checksums.sha512Base64)
275294

276-
model.OutputSha = types.StringValue(sha1)
277-
model.OutputBase64Sha256 = types.StringValue(base64sha256)
278-
model.OutputMd5 = types.StringValue(md5)
279-
model.OutputSize = types.Int64Value(fi.Size())
280-
281-
model.ID = types.StringValue(sha1)
295+
model.ID = types.StringValue(checksums.sha1Hex)
282296

283297
return diags
284298
}

0 commit comments

Comments
 (0)