Skip to content

Commit

Permalink
feat(file): make file non-challenge related by default
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Jun 2, 2024
1 parent cc2d56f commit 089540e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/resources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ resource "ctfd_file" "http_file" {

### Required

- `challenge_id` (String) Challenge of the file.
- `name` (String) Name of the file as displayed to end-users.

### Optional

- `challenge_id` (String) Challenge of the file.
- `content` (String, Sensitive) Raw content of the file, perfectly fit the use-cases of a .txt document or anything with a simple binary content. You could provide it from the file-system using `file("${path.module}/...")`.
- `contentb64` (String, Sensitive) Base 64 content of the file, perfectly fit the use-cases of complex binaries. You could provide it from the file-system using `filebase64("${path.module}/...")`.
- `location` (String) Location where the file is stored on the CTFd instance, for download purposes.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ctfer-io/terraform-provider-ctfd
go 1.22

require (
github.com/ctfer-io/go-ctfd v0.8.0
github.com/ctfer-io/go-ctfd v0.8.1
github.com/hashicorp/terraform-plugin-docs v0.19.2
github.com/hashicorp/terraform-plugin-framework v1.8.0
github.com/hashicorp/terraform-plugin-go v0.23.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZ
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/ctfer-io/go-ctfd v0.8.0 h1:as4TWW0OFI/4oZnJi8ad1pEeVqcZWVw2HiQl0rMDWNk=
github.com/ctfer-io/go-ctfd v0.8.0/go.mod h1:zOOgs1LmKEVW3rilcog0jT921vjShmR3avJbSMtvNyM=
github.com/ctfer-io/go-ctfd v0.8.1 h1:/cXBR6rJ6S4Q2w7HS5otQvyQ4GK9pzDTsrqCxOl69oc=
github.com/ctfer-io/go-ctfd v0.8.1/go.mod h1:zOOgs1LmKEVW3rilcog0jT921vjShmR3avJbSMtvNyM=
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
15 changes: 7 additions & 8 deletions provider/file_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *fileResource) Schema(ctx context.Context, req resource.SchemaRequest, r
},
"challenge_id": schema.StringAttribute{
MarkdownDescription: "Challenge of the file.",
Required: true,
Optional: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Name of the file as displayed to end-users.",
Expand Down Expand Up @@ -138,16 +138,19 @@ func (r *fileResource) Create(ctx context.Context, req resource.CreateRequest, r
}

// Create file
res, err := r.client.PostFiles(&api.PostFilesParams{
Challenge: utils.Ptr(utils.Atoi(data.ChallengeID.ValueString())),
params := &api.PostFilesParams{
Files: []*api.InputFile{
{
Name: data.Name.ValueString(),
Content: []byte(data.Content.ValueString()),
},
},
Location: data.Location.ValueStringPointer(),
}, api.WithContext(ctx))
}
if !data.ChallengeID.IsNull() {
params.Challenge = utils.Ptr(utils.Atoi(data.ChallengeID.ValueString()))
}
res, err := r.client.PostFiles(params, api.WithContext(ctx))
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
Expand Down Expand Up @@ -291,9 +294,5 @@ func lookForChallengeId(ctx context.Context, client *api.Client, fileID int, dia
}
}
}
diags.AddError(
"Provider Error",
fmt.Sprintf("Unable to find challenge of file %d", fileID),
)
return types.StringNull()
}
10 changes: 10 additions & 0 deletions provider/file_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ resource "ctfd_file" "pouet" {
name = "pouet.txt"
content = "Pouet is a clown cat"
}
resource "ctfd_file" "pouet_2" {
name = "pouet-2.txt"
content = "Pouet is a clown cat, but has not challenge"
}
`,
},
// ImportState testing
Expand All @@ -48,6 +53,11 @@ resource "ctfd_file" "pouet" {
name = "pouet.txt"
content = "Pouet the 2nd is the clowniest cat ever"
}
resource "ctfd_file" "pouet_2" {
name = "pouet-2.txt"
content = "Pouet is a clown cat, but has not challenge"
}
`,
},
// Delete testing automatically occurs in TestCase
Expand Down

0 comments on commit 089540e

Please sign in to comment.