-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
199 lines (185 loc) · 7.67 KB
/
setup.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
193
194
195
196
197
198
199
package ctfdsetup
import (
"context"
"net/http"
"github.com/ctfer-io/go-ctfd/api"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func Setup(ctx context.Context, url string, apiKey string, conf *Config) error {
nonce, session, err := api.GetNonceAndSession(url, api.WithContext(ctx))
if err != nil {
return errors.Wrap(err, "getting CTFd nonce and session")
}
client := api.NewClient(url, nonce, session, apiKey)
b, err := bare(ctx, url)
if err != nil {
return err
}
Log().Info("deciding on CTFd setup strategy",
zap.Bool("bare", b),
zap.Bool("login", apiKey == ""),
)
if b {
if err := bareSetup(ctx, client, conf); err != nil {
return err
}
} else if apiKey == "" {
if err := client.Login(&api.LoginParams{
Name: conf.Admin.Name,
Password: conf.Admin.Password.Content,
}, api.WithContext(ctx)); err != nil {
return &ErrClient{err: err}
}
}
return updateSetup(ctx, client, conf)
}
func bare(ctx context.Context, url string) (bool, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url+"/setup", nil)
if err != nil {
return false, err
}
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
res, err := client.Do(req)
if err != nil {
return false, &ErrClient{err: err}
}
return res.StatusCode == 200, nil // 302 if already setup
}
func bareSetup(ctx context.Context, client *api.Client, conf *Config) error {
// Flatten configuration and (basic) setup it
if err := client.Setup(&api.SetupParams{
CTFName: conf.Appearance.Name,
CTFDescription: conf.Appearance.Description,
UserMode: conf.Mode,
CTFTheme: conf.Theme.Name,
ChallengeVisibility: conf.Settings.ChallengeVisibility,
AccountVisibility: conf.Settings.AccountVisibility,
ScoreVisibility: conf.Settings.ScoreVisibility,
RegistrationVisibility: conf.Settings.RegistrationVisibility,
VerifyEmails: conf.Accounts.VerifyEmails,
TeamSize: conf.Accounts.TeamSize,
Name: conf.Admin.Name,
Email: conf.Admin.Email,
Password: conf.Admin.Password.Content,
}, api.WithContext(ctx)); err != nil {
return &ErrClient{err: err}
}
return nil
}
func updateSetup(ctx context.Context, client *api.Client, conf *Config) error {
// Push logo
if conf.Theme.Logo.Name != "" {
lf, err := client.PostFiles(&api.PostFilesParams{
Files: []*api.InputFile{
(*api.InputFile)(conf.Theme.Logo),
},
}, api.WithContext(ctx))
if err != nil {
return errors.Wrap(err, "pushing theme logo")
}
if _, err := client.PatchConfigsCTFLogo(&api.PatchConfigsCTFLogo{
Value: &lf[0].Location,
}, api.WithContext(ctx)); err != nil {
return errors.Wrap(err, "patching CTF logo")
}
}
// Push small icon
if conf.Theme.SmallIcon.Name != "" {
smf, err := client.PostFiles(&api.PostFilesParams{
Files: []*api.InputFile{
(*api.InputFile)(conf.Theme.SmallIcon),
},
}, api.WithContext(ctx))
if err != nil {
return errors.Wrap(err, "pushing theme small icon")
}
if _, err := client.PatchConfigsCTFSmallIcon(&api.PatchConfigsCTFLogo{
Value: &smf[0].Location,
}, api.WithContext(ctx)); err != nil {
return errors.Wrap(err, "patching CTF small icon")
}
}
// Update configs attributes
params := &api.PatchConfigsParams{
CTFDescription: &conf.Appearance.Description,
CTFName: &conf.Appearance.Name,
DefaultLocale: conf.Appearance.DefaultLocale,
CTFTheme: &conf.Theme.Name,
ThemeFooter: ptr(string(conf.Theme.Footer.Content)),
ThemeHeader: ptr(string(conf.Theme.Header.Content)),
ThemeSettings: ptr(string(conf.Theme.Settings.Content)),
DomainWhitelist: conf.Accounts.DomainWhitelist,
IncorrectSubmissionsPerMin: conf.Accounts.IncorrectSubmissionsPerMinute,
NameChanges: conf.Accounts.NameChanges,
NumTeams: conf.Accounts.NumTeams,
NumUsers: conf.Accounts.NumUsers,
TeamCreation: conf.Accounts.TeamCreation,
TeamDisbanding: conf.Accounts.TeamDisbanding,
TeamSize: conf.Accounts.TeamSize,
VerifyEmails: &conf.Accounts.VerifyEmails,
RobotsTxt: ptr(string(conf.Pages.RobotsTxt.Content)),
OauthClientID: conf.MajorLeagueCyber.ClientID,
OauthClientSecret: conf.MajorLeagueCyber.ClientSecret,
AccountVisibility: &conf.Settings.AccountVisibility,
ChallengeVisibility: &conf.Settings.ChallengeVisibility,
RegistrationVisibility: &conf.Settings.RegistrationVisibility,
ScoreVisibility: &conf.Settings.ScoreVisibility,
Paused: conf.Settings.Paused,
HTMLSanitization: conf.Security.HTMLSanitization,
RegistrationCode: conf.Security.RegistrationCode,
MailUseAuth: nil, // Handled later
MailUsername: nil, // Handled later
MailPassword: nil, // Handled later
MailFromAddr: nil, // Deprecated, set to nil for autocomplete
MailGunAPIKey: nil, // Deprecated, set to nil for autocomplete
MailGunBaseURL: nil, // Deprecated, set to nil for autocomplete
MailPort: conf.Email.Port,
MailServer: conf.Email.Server,
MailSSL: conf.Email.TLS_SSL,
MailTLS: conf.Email.STARTTLS,
SuccessfulRegistrationEmailSubject: conf.Email.Registration.Subject,
SuccessfulRegistrationEmailBody: conf.Email.Registration.Body,
VerificationEmailSubject: conf.Email.Confirmation.Subject,
VerificationEmailBody: conf.Email.Confirmation.Body,
UserCreationEmailSubject: conf.Email.NewAccount.Subject,
UserCreationEmailBody: conf.Email.NewAccount.Body,
PasswordChangeAlertSubject: conf.Email.PasswordReset.Subject,
PasswordChangeAlertBody: conf.Email.PasswordReset.Body,
PasswordResetSubject: conf.Email.PasswordResetConfirmation.Subject,
PasswordResetBody: conf.Email.PasswordResetConfirmation.Body,
Start: conf.Time.Start,
End: conf.Time.End,
Freeze: conf.Time.Freeze,
ViewAfterCTF: conf.Time.ViewAfter,
SocialShares: conf.Social.Shares,
PrivacyURL: conf.Legal.PrivacyPolicy.URL,
PrivacyText: ptr(string(conf.Legal.PrivacyPolicy.Content.Content)),
TOSURL: conf.Legal.TOS.URL,
TOSText: ptr(string(conf.Legal.TOS.Content.Content)),
UserMode: &conf.Mode,
}
// Handle mail server authentication
if conf.Email.Username != nil && conf.Email.Password != nil {
params.MailUseAuth = ptr(true)
params.MailUsername = conf.Email.Username
params.MailPassword = conf.Email.Password
}
if err := client.PatchConfigs(params, api.WithContext(ctx)); err != nil {
return &ErrClient{err: err}
}
// Handle additional pages configuration
if conf.Pages != nil && len(conf.Pages.Additional) != 0 {
if err := additionalPages(ctx, client, conf.Pages.Additional); err != nil {
return err
}
}
return nil
}
func ptr[T any](t T) *T {
return &t
}