Skip to content

Commit

Permalink
fix(challenge.file): manual or default location and SHA1 sum
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Mar 2, 2024
1 parent e36c79a commit 080ad43
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion provider/challenge/file_subresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package challenge

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

Expand Down Expand Up @@ -39,6 +41,8 @@ func FileSubresourceAttributes() map[string]schema.Attribute {
},
"location": schema.StringAttribute{
MarkdownDescription: "Location where the file is stored on the CTFd instance, for download purposes.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Expand Down Expand Up @@ -84,8 +88,18 @@ func (file *FileSubresourceModel) Read(ctx context.Context, diags diag.Diagnosti
}

file.Content = types.StringValue(string(content))
// TODO fetch sha1sum
file.PropagateContent(ctx, diags)

h := sha1.New()
_, err = h.Write(content)
if err != nil {
diags.AddError(
"Internal Error",
fmt.Sprintf("Failed to compute SHA1 sum, got error: %s", err),
)
}
sum := h.Sum(nil)
file.SHA1Sum = types.StringValue(hex.EncodeToString(sum))
}

func (data *FileSubresourceModel) Create(ctx context.Context, diags diag.Diagnostics, client *api.Client, challengeID int) {
Expand Down
12 changes: 12 additions & 0 deletions provider/challenge_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package provider

import (
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"strconv"

Expand Down Expand Up @@ -112,6 +114,16 @@ func (chall *challengeResourceModel) Read(ctx context.Context, diags diag.Diagno
Content: types.StringValue(string(c)),
}
nf.PropagateContent(ctx, diags)
h := sha1.New()
_, err = h.Write(c)
if err != nil {
diags.AddError(
"Internal Error",
fmt.Sprintf("Failed to compute SHA1 sum, got error: %s", err),
)
}
sum := h.Sum(nil)
nf.SHA1Sum = types.StringValue(hex.EncodeToString(sum))
chall.Files = append(chall.Files, nf)
}

Expand Down

0 comments on commit 080ad43

Please sign in to comment.