Skip to content

Commit 40c5bc9

Browse files
committed
feat: Added Argon2 and Scrypt hashing with Base64 output.
1 parent 17f1088 commit 40c5bc9

File tree

65 files changed

+2217
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2217
-197
lines changed

acc-coverage.png

-7.21 KB
Loading

acc-coverage.svg

Lines changed: 231 additions & 153 deletions
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bats
2+
# https://bats-core.readthedocs.io/en/stable/writing-tests.html
3+
4+
@test "corefunc_hash_argon2id_base64: attrs" {
5+
run bash -c "tfschema data show -format=json corefunc_hash_argon2id_base64 | jq -Mrc '.attributes[]'"
6+
7+
[[ ${status} -eq 0 ]]
8+
[[ ${lines[0]} == '{"name":"input","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
9+
[[ ${lines[1]} == '{"name":"salt","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
10+
[[ ${lines[2]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
11+
}

bats/ds_hash_scrypt.bats.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bats
2+
# https://bats-core.readthedocs.io/en/stable/writing-tests.html
3+
4+
@test "corefunc_hash_scrypt: attrs" {
5+
run bash -c "tfschema data show -format=json corefunc_hash_scrypt | jq -Mrc '.attributes[]'"
6+
7+
[[ ${status} -eq 0 ]]
8+
[[ ${lines[0]} == '{"name":"input","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
9+
[[ ${lines[1]} == '{"name":"salt","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
10+
[[ ${lines[2]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
11+
}

bats/ds_hash_scrypt_base64.bats.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bats
2+
# https://bats-core.readthedocs.io/en/stable/writing-tests.html
3+
4+
@test "corefunc_hash_scrypt_base64: attrs" {
5+
run bash -c "tfschema data show -format=json corefunc_hash_scrypt_base64 | jq -Mrc '.attributes[]'"
6+
7+
[[ ${status} -eq 0 ]]
8+
[[ ${lines[0]} == '{"name":"input","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
9+
[[ ${lines[1]} == '{"name":"salt","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
10+
[[ ${lines[2]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
11+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright 2024-2025, Northwood Labs, LLC <license@northwood-labs.com>
2+
// Copyright 2023-2025, Ryan Parman <rparman@northwood-labs.com>
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package corefuncprovider // lint:no_dupe
17+
18+
import (
19+
"context"
20+
"strings"
21+
22+
"github.com/hashicorp/terraform-plugin-framework/datasource"
23+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
24+
"github.com/hashicorp/terraform-plugin-framework/resource"
25+
"github.com/hashicorp/terraform-plugin-framework/types"
26+
"github.com/hashicorp/terraform-plugin-log/tflog"
27+
"github.com/lithammer/dedent"
28+
29+
"github.com/northwood-labs/terraform-provider-corefunc/v2/corefunc"
30+
)
31+
32+
// Ensure the implementation satisfies the expected interfaces.
33+
var (
34+
_ datasource.DataSource = &hashArgon2idBase64DataSource{}
35+
_ datasource.DataSourceWithConfigure = &hashArgon2idBase64DataSource{}
36+
)
37+
38+
// hashArgon2idBase64DataSource is the data source implementation.
39+
type (
40+
hashArgon2idBase64DataSource struct{}
41+
42+
// hashArgon2idBase64DataSourceModel maps the data source schema data.
43+
hashArgon2idBase64DataSourceModel struct {
44+
Input types.String `tfsdk:"input"`
45+
Salt types.String `tfsdk:"salt"`
46+
Value types.String `tfsdk:"value"`
47+
}
48+
)
49+
50+
// HashArgon2idBase64DataSource is a method that exposes its paired Go function as a
51+
// Terraform Data Source.
52+
func HashArgon2idBase64DataSource() datasource.DataSource { // lint:allow_return_interface
53+
return &hashArgon2idBase64DataSource{}
54+
}
55+
56+
// Metadata returns the data source type name.
57+
func (d *hashArgon2idBase64DataSource) Metadata(
58+
ctx context.Context,
59+
req datasource.MetadataRequest,
60+
resp *datasource.MetadataResponse,
61+
) {
62+
tflog.Debug(ctx, "Starting HashArgon2idBase64 DataSource Metadata method.")
63+
64+
resp.TypeName = req.ProviderTypeName + "_hash_argon2id_base64"
65+
66+
tflog.Debug(ctx, "req.ProviderTypeName = "+req.ProviderTypeName)
67+
tflog.Debug(ctx, "resp.TypeName = "+resp.TypeName)
68+
69+
tflog.Debug(ctx, "Ending HashArgon2idBase64 DataSource Metadata method.")
70+
}
71+
72+
// Schema defines the schema for the data source.
73+
func (d *hashArgon2idBase64DataSource) Schema(
74+
ctx context.Context,
75+
_ datasource.SchemaRequest,
76+
resp *datasource.SchemaResponse,
77+
) {
78+
tflog.Debug(ctx, "Starting HashArgon2idBase64 DataSource Schema method.")
79+
80+
resp.Schema = schema.Schema{
81+
MarkdownDescription: strings.TrimSpace(dedent.Dedent(`
82+
Generates the Argon2id hash of a string with its associated salt value. Returns a Base64
83+
value instead of a hexadecimal string.
84+
85+
Maps to the ` + linkPackage("Base64HashArgon2id") + ` Go method, which can be used in ` + Terratest + `.
86+
`)),
87+
Attributes: map[string]schema.Attribute{
88+
"input": schema.StringAttribute{
89+
MarkdownDescription: "The string to generate the Argon2id hash for.",
90+
Required: true,
91+
},
92+
"salt": schema.StringAttribute{
93+
MarkdownDescription: "A random value to provide additional entropy in the calculation.",
94+
Required: true,
95+
},
96+
"value": schema.StringAttribute{
97+
MarkdownDescription: "The result of the hashing function.",
98+
Computed: true,
99+
},
100+
},
101+
}
102+
103+
tflog.Debug(ctx, "Ending HashArgon2idBase64 DataSource Schema method.")
104+
}
105+
106+
// Configure adds the provider configured client to the data source.
107+
func (d *hashArgon2idBase64DataSource) Configure(
108+
ctx context.Context,
109+
req datasource.ConfigureRequest,
110+
_ *datasource.ConfigureResponse,
111+
) {
112+
tflog.Debug(ctx, "Starting HashArgon2idBase64 DataSource Configure method.")
113+
114+
if req.ProviderData == nil {
115+
return
116+
}
117+
118+
tflog.Debug(ctx, "Ending HashArgon2idBase64 DataSource Configure method.")
119+
}
120+
121+
func (d *hashArgon2idBase64DataSource) Create(
122+
ctx context.Context,
123+
req resource.CreateRequest, // lint:allow_large_memory
124+
resp *resource.CreateResponse,
125+
) {
126+
tflog.Debug(ctx, "Starting HashArgon2idBase64 DataSource Create method.")
127+
128+
var plan hashArgon2idBase64DataSourceModel
129+
130+
diags := req.Plan.Get(ctx, &plan)
131+
resp.Diagnostics.Append(diags...)
132+
133+
if resp.Diagnostics.HasError() {
134+
return
135+
}
136+
137+
tflog.Debug(ctx, "Ending HashArgon2idBase64 DataSource Create method.")
138+
}
139+
140+
// Read refreshes the Terraform state with the latest data.
141+
func (d *hashArgon2idBase64DataSource) Read( // lint:no_dupe
142+
ctx context.Context,
143+
_ datasource.ReadRequest, // lint:allow_large_memory
144+
resp *datasource.ReadResponse,
145+
) {
146+
tflog.Debug(ctx, "Starting HashArgon2idBase64 DataSource Read method.")
147+
148+
var state hashArgon2idBase64DataSourceModel
149+
150+
diags := resp.State.Get(ctx, &state)
151+
resp.Diagnostics.Append(diags...)
152+
153+
value, err := corefunc.Base64HashArgon2id(
154+
state.Input.ValueString(),
155+
[]byte(state.Salt.ValueString()),
156+
)
157+
if err != nil {
158+
resp.Diagnostics.AddError(
159+
"Problem with calculating the Argon2id hash.",
160+
err.Error(),
161+
)
162+
163+
return
164+
}
165+
166+
state.Value = types.StringValue(value)
167+
168+
diags = resp.State.Set(ctx, &state)
169+
resp.Diagnostics.Append(diags...)
170+
171+
if resp.Diagnostics.HasError() {
172+
return
173+
}
174+
175+
tflog.Debug(ctx, "Ending HashArgon2idBase64 DataSource Read method.")
176+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{{ if contains .Input "\n" -}}
2+
locals {
3+
input = <<EOT
4+
{{ .Input }}EOT
5+
}
6+
7+
{{ end -}}
8+
data "corefunc_hash_argon2id_base64" "hash_argon2id_base64" {
9+
{{- if contains .Input "\n" }}
10+
input = local.input
11+
{{- else }}
12+
input = "{{ .Input }}"
13+
{{- end }}
14+
salt = "somesaltvalue"
15+
}
16+
17+
#=> {{ .Expected }}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2024-2025, Northwood Labs, LLC <license@northwood-labs.com>
2+
// Copyright 2023-2025, Ryan Parman <rparman@northwood-labs.com>
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package corefuncprovider // lint:no_dupe
17+
18+
import (
19+
"bytes"
20+
"fmt"
21+
"log"
22+
"os"
23+
"strings"
24+
"testing"
25+
"text/template"
26+
27+
"github.com/northwood-labs/terraform-provider-corefunc/v2/testfixtures"
28+
29+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
30+
)
31+
32+
func TestAccHashArgon2idBase64DataSource(t *testing.T) {
33+
t.Parallel()
34+
35+
funcName := traceFuncName()
36+
37+
for name, tc := range testfixtures.Base64HashArgon2idTestTable { // lint:no_dupe
38+
fmt.Printf(
39+
"=== RUN %s/%s\n",
40+
strings.TrimSpace(funcName),
41+
strings.TrimSpace(name),
42+
)
43+
44+
buf := &bytes.Buffer{}
45+
tmpl := template.Must(
46+
template.New("hash_argon2id_base64_data_source_fixture.tftpl").
47+
Funcs(FuncMap()).
48+
ParseFiles("hash_argon2id_base64_data_source_fixture.tftpl"),
49+
)
50+
51+
err := tmpl.Execute(buf, tc)
52+
if err != nil {
53+
log.Fatalln(err)
54+
}
55+
56+
if os.Getenv("PROVIDER_DEBUG") != "" {
57+
fmt.Fprintln(os.Stderr, buf.String())
58+
}
59+
60+
resource.Test(t, resource.TestCase{
61+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
62+
Steps: []resource.TestStep{
63+
{
64+
Config: providerConfig + buf.String(),
65+
Check: resource.ComposeAggregateTestCheckFunc(
66+
resource.TestCheckResourceAttr(
67+
"data.corefunc_hash_argon2id_base64.hash_argon2id_base64",
68+
"value",
69+
tc.Expected,
70+
),
71+
),
72+
},
73+
},
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)