Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hash outputs of file content #142

Merged
merged 17 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/data-sources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ resource "aws_s3_object" "shared_zip" {
- `content` (String) Raw content of the file that was read, as UTF-8 encoded string. Files that do not contain UTF-8 text will have invalid UTF-8 sequences in `content`
replaced with the Unicode replacement character.
- `content_base64` (String) Base64 encoded version of the file content (use this when dealing with binary data).
- `id` (String) The hexadecimal encoding of the checksum of the file content.
- `content_base64sha256` (String) Base64 encoded SHA256 checksum of file content.
- `content_base64sha512` (String) Base64 encoded SHA512 checksum of file content.
- `content_md5` (String) MD5 checksum of file content.
- `content_sha1` (String) SHA1 checksum of file content.
- `content_sha256` (String) SHA256 checksum of file content.
- `content_sha512` (String) SHA512 checksum of file content.
- `id` (String) The hexadecimal encoding of the SHA1 checksum of the file content.
8 changes: 7 additions & 1 deletion docs/data-sources/sensitive_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ resource "aws_s3_object" "shared_zip" {
- `content` (String, Sensitive) Raw content of the file that was read, as UTF-8 encoded string. Files that do not contain UTF-8 text will have invalid UTF-8 sequences in `content`
replaced with the Unicode replacement character.
- `content_base64` (String, Sensitive) Base64 encoded version of the file content (use this when dealing with binary data).
- `id` (String) The hexadecimal encoding of the checksum of the file content
- `content_base64sha256` (String) Base64 encoded SHA256 checksum of file content.
- `content_base64sha512` (String) Base64 encoded SHA512 checksum of file content.
- `content_md5` (String) MD5 checksum of file content.
- `content_sha1` (String) SHA1 checksum of file content.
- `content_sha256` (String) SHA256 checksum of file content.
- `content_sha512` (String) SHA512 checksum of file content.
- `id` (String) The hexadecimal encoding of the SHA1 checksum of the file content.
8 changes: 7 additions & 1 deletion docs/resources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ resource "local_file" "foo" {

### Read-Only

- `id` (String) The hexadecimal encoding of the checksum of the file content
- `content_base64sha256` (String) Base64 encoded SHA256 checksum of file content.
- `content_base64sha512` (String) Base64 encoded SHA512 checksum of file content.
- `content_md5` (String) MD5 checksum of file content.
- `content_sha1` (String) SHA1 checksum of file content.
- `content_sha256` (String) SHA256 checksum of file content.
- `content_sha512` (String) SHA512 checksum of file content.
- `id` (String) The hexadecimal encoding of the SHA1 checksum of the file content.
8 changes: 7 additions & 1 deletion docs/resources/sensitive_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ resource "local_sensitive_file" "foo" {

### Read-Only

- `id` (String) The hexadecimal encoding of the checksum of the file content
- `content_base64sha256` (String) Base64 encoded SHA256 checksum of file content.
- `content_base64sha512` (String) Base64 encoded SHA512 checksum of file content.
- `content_md5` (String) MD5 checksum of file content.
- `content_sha1` (String) SHA1 checksum of file content.
- `content_sha256` (String) SHA256 checksum of file content.
- `content_sha512` (String) SHA512 checksum of file content.
- `id` (String) The hexadecimal encoding of the SHA1 checksum of the file content.
60 changes: 47 additions & 13 deletions internal/provider/data_source_local_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package provider

import (
"context"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"os"

Expand Down Expand Up @@ -41,7 +39,31 @@ func (n *localFileDataSource) Schema(ctx context.Context, req datasource.SchemaR
Computed: true,
},
"id": schema.StringAttribute{
Description: "The hexadecimal encoding of the checksum of the file content.",
Description: "The hexadecimal encoding of the SHA1 checksum of the file content.",
Computed: true,
},
"content_md5": schema.StringAttribute{
Description: "MD5 checksum of file content.",
Computed: true,
},
"content_sha1": schema.StringAttribute{
Description: "SHA1 checksum of file content.",
Computed: true,
},
"content_sha256": schema.StringAttribute{
Description: "SHA256 checksum of file content.",
Computed: true,
},
"content_base64sha256": schema.StringAttribute{
Description: "Base64 encoded SHA256 checksum of file content.",
Computed: true,
},
"content_sha512": schema.StringAttribute{
Description: "SHA512 checksum of file content.",
Computed: true,
},
"content_base64sha512": schema.StringAttribute{
Description: "Base64 encoded SHA512 checksum of file content.",
Computed: true,
},
},
Expand Down Expand Up @@ -75,22 +97,34 @@ func (n *localFileDataSource) Read(ctx context.Context, req datasource.ReadReque
return
}

//calculate the checksum of file content
checksum := sha1.Sum(content)
//calculate the checksums of file content
checksums := genFileChecksums(content)

state := localFileDataSourceModelV0{
Filename: config.Filename,
Content: types.StringValue(string(content)),
ContentBase64: types.StringValue(base64.StdEncoding.EncodeToString(content)),
ID: types.StringValue(hex.EncodeToString(checksum[:])),
Filename: config.Filename,
Content: types.StringValue(string(content)),
ContentBase64: types.StringValue(base64.StdEncoding.EncodeToString(content)),
ID: types.StringValue(checksums.sha1Hex),
ContentMd5: types.StringValue(checksums.md5Hex),
ContentSha1: types.StringValue(checksums.sha1Hex),
ContentSha256: types.StringValue(checksums.sha256Hex),
ContentBase64sha256: types.StringValue(checksums.sha256Base64),
ContentSha512: types.StringValue(checksums.sha512Hex),
ContentBase64sha512: types.StringValue(checksums.sha512Base64),
}
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}

type localFileDataSourceModelV0 struct {
Filename types.String `tfsdk:"filename"`
Content types.String `tfsdk:"content"`
ContentBase64 types.String `tfsdk:"content_base64"`
ID types.String `tfsdk:"id"`
Filename types.String `tfsdk:"filename"`
Content types.String `tfsdk:"content"`
ContentBase64 types.String `tfsdk:"content_base64"`
ID types.String `tfsdk:"id"`
ContentMd5 types.String `tfsdk:"content_md5"`
ContentSha1 types.String `tfsdk:"content_sha1"`
ContentSha256 types.String `tfsdk:"content_sha256"`
ContentBase64sha256 types.String `tfsdk:"content_base64sha256"`
ContentSha512 types.String `tfsdk:"content_sha512"`
ContentBase64sha512 types.String `tfsdk:"content_base64sha512"`
}
7 changes: 7 additions & 0 deletions internal/provider/data_source_local_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func TestLocalFileDataSource(t *testing.T) {
content := "This is some content"
checkSums := genFileChecksums([]byte(content))

config := `
data "local_file" "file" {
Expand All @@ -24,6 +25,12 @@ func TestLocalFileDataSource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.local_file.file", "content", content),
resource.TestCheckResourceAttr("data.local_file.file", "content_base64", base64.StdEncoding.EncodeToString([]byte(content))),
resource.TestCheckResourceAttr("data.local_file.file", "content_md5", checkSums.md5Hex),
resource.TestCheckResourceAttr("data.local_file.file", "content_sha1", checkSums.sha1Hex),
resource.TestCheckResourceAttr("data.local_file.file", "content_sha256", checkSums.sha256Hex),
resource.TestCheckResourceAttr("data.local_file.file", "content_base64sha256", checkSums.sha256Base64),
resource.TestCheckResourceAttr("data.local_file.file", "content_sha512", checkSums.sha512Hex),
resource.TestCheckResourceAttr("data.local_file.file", "content_base64sha512", checkSums.sha512Base64),
),
},
},
Expand Down
26 changes: 25 additions & 1 deletion internal/provider/data_source_local_sensitive_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,31 @@ func (n *localSensitiveFileDataSource) Schema(ctx context.Context, req datasourc
Computed: true,
},
"id": schema.StringAttribute{
Description: "The hexadecimal encoding of the checksum of the file content",
Description: "The hexadecimal encoding of the SHA1 checksum of the file content.",
Computed: true,
},
"content_md5": schema.StringAttribute{
Description: "MD5 checksum of file content.",
Computed: true,
},
"content_sha1": schema.StringAttribute{
Description: "SHA1 checksum of file content.",
Computed: true,
},
"content_sha256": schema.StringAttribute{
Description: "SHA256 checksum of file content.",
Computed: true,
},
"content_base64sha256": schema.StringAttribute{
Description: "Base64 encoded SHA256 checksum of file content.",
Computed: true,
},
"content_sha512": schema.StringAttribute{
Description: "SHA512 checksum of file content.",
Computed: true,
},
"content_base64sha512": schema.StringAttribute{
Description: "Base64 encoded SHA512 checksum of file content.",
Computed: true,
},
},
Expand Down
35 changes: 35 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package provider

import (
"context"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
Expand Down Expand Up @@ -44,3 +50,32 @@ func (p *localProvider) Resources(ctx context.Context) []func() resource.Resourc
func (p *localProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{}
}

type fileChecksums struct {
md5Hex string
sha1Hex string
sha256Hex string
sha256Base64 string
sha512Hex string
sha512Base64 string
}

func genFileChecksums(data []byte) fileChecksums {
var checksums fileChecksums

md5Sum := md5.Sum(data)
checksums.md5Hex = hex.EncodeToString(md5Sum[:])

sha1Sum := sha1.Sum(data)
checksums.sha1Hex = hex.EncodeToString(sha1Sum[:])

sha256Sum := sha256.Sum256(data)
checksums.sha256Hex = hex.EncodeToString(sha256Sum[:])
checksums.sha256Base64 = base64.StdEncoding.EncodeToString(sha256Sum[:])

sha512Sum := sha512.Sum512(data)
checksums.sha512Hex = hex.EncodeToString(sha512Sum[:])
checksums.sha512Base64 = base64.StdEncoding.EncodeToString(sha512Sum[:])

return checksums
}
42 changes: 39 additions & 3 deletions internal/provider/resource_local_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (n *localFileResource) Schema(ctx context.Context, req resource.SchemaReque
},
},
"id": schema.StringAttribute{
Description: "The hexadecimal encoding of the checksum of the file content",
Description: "The hexadecimal encoding of the SHA1 checksum of the file content.",
Computed: true,
},
"sensitive_content": schema.StringAttribute{
Expand All @@ -139,6 +139,30 @@ func (n *localFileResource) Schema(ctx context.Context, req resource.SchemaReque
path.MatchRoot("source")),
},
},
"content_md5": schema.StringAttribute{
Description: "MD5 checksum of file content.",
Computed: true,
},
"content_sha1": schema.StringAttribute{
Description: "SHA1 checksum of file content.",
Computed: true,
},
"content_sha256": schema.StringAttribute{
Description: "SHA256 checksum of file content.",
Computed: true,
},
"content_base64sha256": schema.StringAttribute{
Description: "Base64 encoded SHA256 checksum of file content.",
Computed: true,
},
"content_sha512": schema.StringAttribute{
Description: "SHA512 checksum of file content.",
Computed: true,
},
"content_base64sha512": schema.StringAttribute{
Description: "Base64 encoded SHA512 checksum of file content.",
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -196,9 +220,15 @@ func (n *localFileResource) Create(ctx context.Context, req resource.CreateReque
return
}

checksum := sha1.Sum(content)
checksums := genFileChecksums(content)
plan.ContentMd5 = types.StringValue(checksums.md5Hex)
plan.ContentSha1 = types.StringValue(checksums.sha1Hex)
plan.ContentSha256 = types.StringValue(checksums.sha256Hex)
plan.ContentBase64sha256 = types.StringValue(checksums.sha256Base64)
plan.ContentSha512 = types.StringValue(checksums.sha512Hex)
plan.ContentBase64sha512 = types.StringValue(checksums.sha512Base64)

plan.ID = types.StringValue(hex.EncodeToString(checksum[:]))
plan.ID = types.StringValue(checksums.sha1Hex)
diags = resp.State.Set(ctx, &plan)
resp.Diagnostics.Append(diags...)
}
Expand Down Expand Up @@ -283,4 +313,10 @@ type localFileResourceModelV0 struct {
DirectoryPermission types.String `tfsdk:"directory_permission"`
ID types.String `tfsdk:"id"`
SensitiveContent types.String `tfsdk:"sensitive_content"`
ContentMd5 types.String `tfsdk:"content_md5"`
ContentSha1 types.String `tfsdk:"content_sha1"`
ContentSha256 types.String `tfsdk:"content_sha256"`
ContentBase64sha256 types.String `tfsdk:"content_base64sha256"`
ContentSha512 types.String `tfsdk:"content_sha512"`
ContentBase64sha512 types.String `tfsdk:"content_base64sha512"`
}
42 changes: 39 additions & 3 deletions internal/provider/resource_local_sensitive_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,31 @@ func (n *localSensitiveFileResource) Schema(ctx context.Context, req resource.Sc
},
},
"id": schema.StringAttribute{
Description: "The hexadecimal encoding of the checksum of the file content",
Description: "The hexadecimal encoding of the SHA1 checksum of the file content.",
Computed: true,
},
"content_md5": schema.StringAttribute{
Description: "MD5 checksum of file content.",
Computed: true,
},
"content_sha1": schema.StringAttribute{
Description: "SHA1 checksum of file content.",
Computed: true,
},
"content_sha256": schema.StringAttribute{
Description: "SHA256 checksum of file content.",
Computed: true,
},
"content_base64sha256": schema.StringAttribute{
Description: "Base64 encoded SHA256 checksum of file content.",
Computed: true,
},
"content_sha512": schema.StringAttribute{
Description: "SHA512 checksum of file content.",
Computed: true,
},
"content_base64sha512": schema.StringAttribute{
Description: "Base64 encoded SHA512 checksum of file content.",
Computed: true,
},
},
Expand Down Expand Up @@ -176,9 +200,15 @@ func (n *localSensitiveFileResource) Create(ctx context.Context, req resource.Cr
return
}

checksum := sha1.Sum(content)
checksums := genFileChecksums(content)
plan.ContentMd5 = types.StringValue(checksums.md5Hex)
plan.ContentSha1 = types.StringValue(checksums.sha1Hex)
plan.ContentSha256 = types.StringValue(checksums.sha256Hex)
plan.ContentBase64sha256 = types.StringValue(checksums.sha256Base64)
plan.ContentSha512 = types.StringValue(checksums.sha512Hex)
plan.ContentBase64sha512 = types.StringValue(checksums.sha512Base64)

plan.ID = types.StringValue(hex.EncodeToString(checksum[:]))
plan.ID = types.StringValue(checksums.sha1Hex)
diags = resp.State.Set(ctx, &plan)
resp.Diagnostics.Append(diags...)
}
Expand Down Expand Up @@ -259,4 +289,10 @@ type localSensitiveFileResourceModelV0 struct {
FilePermission types.String `tfsdk:"file_permission"`
DirectoryPermission types.String `tfsdk:"directory_permission"`
ID types.String `tfsdk:"id"`
ContentMd5 types.String `tfsdk:"content_md5"`
ContentSha1 types.String `tfsdk:"content_sha1"`
ContentSha256 types.String `tfsdk:"content_sha256"`
ContentBase64sha256 types.String `tfsdk:"content_base64sha256"`
ContentSha512 types.String `tfsdk:"content_sha512"`
ContentBase64sha512 types.String `tfsdk:"content_base64sha512"`
}