Skip to content

Commit

Permalink
feat: add support of ctfd_challenge.function
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Dec 17, 2023
1 parent c94b2ea commit 2e2c21f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 33 deletions.
1 change: 1 addition & 0 deletions examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ resource "ctfd_challenge" "http" {
decay = 17
minimum = 50
state = "visible"
function = "logarithmic"

flags = [{
content = "24HIUT{Http_1s_n0t_s3cuR3}"
Expand Down
10 changes: 10 additions & 0 deletions internal/provider/challenge/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file is related to the challenge.function attribute

package challenge

import "github.com/hashicorp/terraform-plugin-framework/types"

var (
FunctionLinear = types.StringValue("linear")
FunctionLogarithmic = types.StringValue("logarithmic")
)
43 changes: 28 additions & 15 deletions internal/provider/challenge_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (ch *challengeDataSource) Schema(ctx context.Context, req datasource.Schema
"description": schema.StringAttribute{
Computed: true,
},
"connection_info": schema.StringAttribute{
Computed: true,
},
"value": schema.Int64Attribute{
Computed: true,
},
Expand All @@ -69,6 +72,13 @@ func (ch *challengeDataSource) Schema(ctx context.Context, req datasource.Schema
"minimum": schema.Int64Attribute{
Computed: true,
},
"max_attempts": schema.StringAttribute{
Computed: true,
},
"function": schema.StringAttribute{
Computed: true,
},
// TODO add support of next + requirements
"state": schema.StringAttribute{
Computed: true,
},
Expand Down Expand Up @@ -240,21 +250,24 @@ func (ch *challengeDataSource) Read(ctx context.Context, req datasource.ReadRequ
}

challState := challengeResourceModel{
ID: types.StringValue(strconv.Itoa(chall.ID)),
Name: types.StringValue(chall.Name),
Category: types.StringValue(chall.Category),
Description: types.StringValue(chall.Description),
Value: types.Int64Value(int64(chall.Value)),
Initial: utils.ToTFInt64(chall.Initial),
Decay: utils.ToTFInt64(chall.Decay),
Minimum: utils.ToTFInt64(chall.Minimum),
State: types.StringValue(chall.State),
Type: types.StringValue(chall.Type),
Files: challFiles,
Flags: challFlags,
Tags: challTags,
Topics: challTopics,
Hints: challHints,
ID: types.StringValue(strconv.Itoa(chall.ID)),
Name: types.StringValue(chall.Name),
Category: types.StringValue(chall.Category),
Description: types.StringValue(chall.Description),
ConnectionInfo: utils.ToTFString(chall.ConnectionInfo),
MaxAttempts: utils.ToTFInt64(chall.MaxAttempts),
Function: types.StringValue(chall.Function),
Value: types.Int64Value(int64(chall.Value)),
Initial: utils.ToTFInt64(chall.Initial),
Decay: utils.ToTFInt64(chall.Decay),
Minimum: utils.ToTFInt64(chall.Minimum),
State: types.StringValue(chall.State),
Type: types.StringValue(chall.Type),
Files: challFiles,
Flags: challFlags,
Tags: challTags,
Topics: challTopics,
Hints: challHints,
}

state.ID = types.StringValue("placeholder")
Expand Down
55 changes: 37 additions & 18 deletions internal/provider/challenge_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
Expand All @@ -37,24 +38,26 @@ type challengeResource struct {
}

type challengeResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Category types.String `tfsdk:"category"`
Description types.String `tfsdk:"description"`
ConnectionInfo types.String `tfsdk:"connection_info"`
MaxAttempts types.Int64 `tfsdk:"max_attempts"`
Value types.Int64 `tfsdk:"value"`
Initial types.Int64 `tfsdk:"initial"`
Decay types.Int64 `tfsdk:"decay"`
Minimum types.Int64 `tfsdk:"minimum"`
State types.String `tfsdk:"state"`
Type types.String `tfsdk:"type"`
Requirements *challenge.RequirementsSubresourceModel `tfsdk:"requirements"`
Flags []challenge.FlagSubresourceModel `tfsdk:"flags"`
Tags []types.String `tfsdk:"tags"`
Topics []types.String `tfsdk:"topics"`
Hints []challenge.HintSubresourceModel `tfsdk:"hints"`
Files []challenge.FileSubresourceModel `tfsdk:"files"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Category types.String `tfsdk:"category"`
Description types.String `tfsdk:"description"`
ConnectionInfo types.String `tfsdk:"connection_info"`
MaxAttempts types.Int64 `tfsdk:"max_attempts"`
Function types.String `tfsdk:"function"`
Value types.Int64 `tfsdk:"value"`
Initial types.Int64 `tfsdk:"initial"`
Decay types.Int64 `tfsdk:"decay"`
Minimum types.Int64 `tfsdk:"minimum"`
State types.String `tfsdk:"state"`
Type types.String `tfsdk:"type"`
// TODO add support of Next challenges
Requirements *challenge.RequirementsSubresourceModel `tfsdk:"requirements"`
Flags []challenge.FlagSubresourceModel `tfsdk:"flags"`
Tags []types.String `tfsdk:"tags"`
Topics []types.String `tfsdk:"topics"`
Hints []challenge.HintSubresourceModel `tfsdk:"hints"`
Files []challenge.FileSubresourceModel `tfsdk:"files"`
}

func (r *challengeResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -91,6 +94,19 @@ func (r *challengeResource) Schema(ctx context.Context, req resource.SchemaReque
Computed: true,
Default: int64default.StaticInt64(0),
},
"function": schema.StringAttribute{
Optional: true,
Computed: true,
Default: defaults.String(stringdefault.StaticString("linear")),
Description: "Decay function to define how the challenge's value will change through time.",
MarkdownDescription: "Decay function to define how the challenge's value will change through time.",
Validators: []validator.String{
validators.NewStringEnumValidator([]basetypes.StringValue{
challenge.FunctionLinear,
challenge.FunctionLogarithmic,
}),
},
},
// TODO value can't be set side to <initial,decay,minimum>, depends on .type value (respectively standard and dynamic)
"value": schema.Int64Attribute{
Optional: true,
Expand Down Expand Up @@ -214,6 +230,7 @@ func (r *challengeResource) Create(ctx context.Context, req resource.CreateReque
Description: data.Description.ValueString(),
ConnectionInfo: data.ConnectionInfo.ValueStringPointer(),
MaxAttempts: utils.ToInt(data.MaxAttempts),
Function: data.Function.ValueString(),
Value: int(data.Value.ValueInt64()),
Initial: utils.ToInt(data.Initial),
Decay: utils.ToInt(data.Decay),
Expand Down Expand Up @@ -330,6 +347,7 @@ func (r *challengeResource) Read(ctx context.Context, req resource.ReadRequest,
data.Description = types.StringValue(res.Description)
data.ConnectionInfo = utils.ToTFString(res.ConnectionInfo)
data.MaxAttempts = utils.ToTFInt64(res.MaxAttempts)
data.Function = types.StringValue(res.Function)
data.Value = types.Int64Value(int64(res.Value))
data.Initial = utils.ToTFInt64(res.Initial)
data.Decay = utils.ToTFInt64(res.Decay)
Expand Down Expand Up @@ -500,6 +518,7 @@ func (r *challengeResource) Update(ctx context.Context, req resource.UpdateReque
Description: data.Description.ValueString(),
ConnectionInfo: data.ConnectionInfo.ValueStringPointer(),
MaxAttempts: utils.ToInt(data.MaxAttempts),
Function: data.Function.ValueString(),
// Value: int(data.Value.ToInt64Value()), // TODO add support of .value in PATCH /challenges
Initial: utils.ToInt(data.Initial),
Decay: utils.ToInt(data.Decay),
Expand Down

0 comments on commit 2e2c21f

Please sign in to comment.