/
challenge_data_source.go
192 lines (174 loc) · 6.65 KB
/
challenge_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package provider
import (
"context"
"fmt"
"strconv"
"github.com/ctfer-io/go-ctfd/api"
"github.com/ctfer-io/terraform-provider-ctfd/provider/challenge"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var (
_ datasource.DataSource = (*challengeDataSource)(nil)
_ datasource.DataSourceWithConfigure = (*challengeDataSource)(nil)
)
func NewChallengeDataSource() datasource.DataSource {
return &challengeDataSource{}
}
type challengeDataSource struct {
client *api.Client
}
type challengesDataSourceModel struct {
ID types.String `tfsdk:"id"`
Challenges []challengeResourceModel `tfsdk:"challenges"`
}
func (ch *challengeDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_challenges"
}
func (ch *challengeDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"challenges": schema.ListNestedAttribute{
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "Identifier of the challenge.",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Name of the challenge, displayed as it.",
Computed: true,
},
"category": schema.StringAttribute{
MarkdownDescription: "Category of the challenge that CTFd groups by on the web UI.",
Computed: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "Description of the challenge, consider using multiline descriptions for better style.",
Computed: true,
},
"connection_info": schema.StringAttribute{
MarkdownDescription: "Connection Information to connect to the challenge instance, useful for pwn or web pentest.",
Computed: true,
},
"max_attempts": schema.Int64Attribute{
MarkdownDescription: "Maximum amount of attempts before being unable to flag the challenge.",
Computed: true,
},
"function": schema.StringAttribute{
MarkdownDescription: "Decay function to define how the challenge value evolve through solves, either linear or logarithmic.",
Computed: true,
},
"value": schema.Int64Attribute{
Computed: true,
},
"decay": schema.Int64Attribute{
Computed: true,
},
"minimum": schema.Int64Attribute{
Computed: true,
},
"state": schema.StringAttribute{
MarkdownDescription: "State of the challenge, either hidden or visible.",
Computed: true,
},
"type": schema.StringAttribute{
MarkdownDescription: "Type of the challenge defining its layout, either standard or dynamic.",
Computed: true,
},
"next": schema.Int64Attribute{
MarkdownDescription: "Suggestion for the end-user as next challenge to work on.",
Computed: true,
},
"requirements": schema.SingleNestedAttribute{
MarkdownDescription: "List of required challenges that needs to get flagged before this one being accessible. Useful for skill-trees-like strategy CTF.",
Computed: true,
Attributes: map[string]schema.Attribute{
"behavior": schema.StringAttribute{
MarkdownDescription: "Behavior if not unlocked, either hidden or anonymized.",
Computed: true,
},
"prerequisites": schema.ListAttribute{
MarkdownDescription: "List of the challenges ID.",
Computed: true,
ElementType: types.StringType,
},
},
},
"flags": schema.ListNestedAttribute{
MarkdownDescription: "List of challenge flags that solves it.",
NestedObject: schema.NestedAttributeObject{
Attributes: challenge.FlagSubdatasourceAttributes(),
},
Computed: true,
},
"tags": schema.ListAttribute{
MarkdownDescription: "List of challenge tags that will be displayed to the end-user. You could use them to give some quick insights of what a challenge involves.",
ElementType: types.StringType,
Computed: true,
},
"topics": schema.ListAttribute{
MarkdownDescription: "List of challenge topics that are displayed to the administrators for maintenance and planification.",
ElementType: types.StringType,
Computed: true,
},
"hints": schema.ListNestedAttribute{
MarkdownDescription: "List of hints about the challenge displayed to the end-user.",
NestedObject: schema.NestedAttributeObject{
Attributes: challenge.HintSubdatasourceAttributes(),
},
Computed: true,
},
"files": schema.ListNestedAttribute{
MarkdownDescription: "List of files given to players to flag the challenge.",
NestedObject: schema.NestedAttributeObject{
Attributes: challenge.FileSubdatasourceAttributes(),
},
Computed: true,
},
},
},
},
},
}
}
func (ch *challengeDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*api.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *github.com/ctfer-io/go-ctfd/api.Client, got: %T. Please open an issue at https://github.com/ctfer-io/terraform-provider-ctfd", req.ProviderData),
)
return
}
ch.client = client
}
func (ch *challengeDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state challengesDataSourceModel
challs, err := ch.client.GetChallenges(&api.GetChallengesParams{}, api.WithContext(ctx))
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read CTFd Challenges",
err.Error(),
)
return
}
state.Challenges = make([]challengeResourceModel, 0, len(challs))
for _, c := range challs {
chall := challengeResourceModel{
ID: types.StringValue(strconv.Itoa(c.ID)),
}
chall.Read(ctx, resp.Diagnostics, ch.client)
state.Challenges = append(state.Challenges, chall)
}
state.ID = types.StringValue("placeholder")
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}