Skip to content

Commit

Permalink
feat!: add of all CTFd settings, change API
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Apr 29, 2024
1 parent 4f690dd commit b227aae
Show file tree
Hide file tree
Showing 8 changed files with 722 additions and 177 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Thanks to this, you can integrate it using **GitHub Actions**, **Drone CI** or e
For the CLI configuration, please refer to the binary's specific API through `ctfd-setup --help`.
In use of IaC provisionning scenario, the corresponding environment variables are also mapped to the output, so please refer to it.

### GitHub Actions
### GitHub Actions

To improve our own workflows and share knownledges and tooling, we built a GitHub Action: `ctfer-io/ctfd-setup`.
You can use it given the following example.
Expand Down
523 changes: 455 additions & 68 deletions cmd/ctfd-setup/main.go

Large diffs are not rendered by default.

134 changes: 106 additions & 28 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,113 @@ import (

type (
Config struct {
Global Global `yaml:"global"`
Visibilities Visibilities `yaml:"visibilities"`
Front Front `yaml:"front"`
Admin Admin `yaml:"admin"`
Appearance Appearance `yaml:"appearance"`
Theme Theme `yaml:"theme"`
Accounts Accounts `yaml:"accounts"`
Pages Pages `yaml:"pages"`
// Don't handle brackets here, should not be part of those settings but CRUD objects
// CustomFields are not handled as they are not predictable and would be hard to handle + bad practice (API changes on the fly)
MajorLeagueCyber MajorLeagueCyber `yaml:"major_league_cyber"`
Settings Settings `yaml:"settings"`
Security Security `yaml:"security"`
Email Email `yaml:"email"`
Time Time `yaml:"time"`
Social Social `yaml:"social"`
Legal Legal `yaml:"legal"`
Mode string `yaml:"mode"`

Admin Admin `yaml:"admin"`
}

Appearance struct {
Name string `yaml:"name"` // required
Description string `yaml:"description"` // required
}

Theme struct {
Logo *File `yaml:"logo"`
SmallIcon *File `yaml:"small_icon"`
Name string `yaml:"name"`
Color string `yaml:"color"`
// Banner is only supported by bare setup, need to be at least support by PatchConfigs
Header *File `yaml:"header"`
Footer *File `yaml:"footer"`
Settings *File `yaml:"settings"`
}

Accounts struct {
DomainWhitelist *string `yaml:"domain_whitelist"`
VerifyEmails bool `yaml:"verify_emails"`
TeamCreation *bool `yaml:"team_creation"`
TeamSize *int `yaml:"team_size"`
NumTeams *int `yaml:"num_teams"`
NumUsers *int `yaml:"num_users"`
TeamDisbanding *string `yaml:"team_disbanding"`
IncorrectSubmissionsPerMinute *int `yaml:"incorrect_submissions_per_minutes"`
NameChanges *bool `yaml:"name_changes"`
}

Pages struct {
RobotsTxt *File `yaml:"robots_txt"`
}

MajorLeagueCyber struct {
ClientID *string `yaml:"client_id"`
ClientSecret *string `yaml:"client_secret"`
}

Settings struct {
ChallengeVisibility string `yaml:"challenge_visibility"`
AccountVisibility string `yaml:"account_visibility"`
ScoreVisibility string `yaml:"score_visibility"`
RegistrationVisibility string `yaml:"registration_visibility"`
Paused *bool `yaml:"paused"`
}

Security struct {
HTMLSanitization *bool `yaml:"html_sanitization"`
RegistrationCode *string `yaml:"registration_code"`
}

Email struct {
Registration EmailContent `yaml:"registration"`
Confirmation EmailContent `yaml:"confirmation"`
NewAccount EmailContent `yaml:"new_account"`
PasswordReset EmailContent `yaml:"password_reset"`
PasswordResetConfirmation EmailContent `yaml:"password_reset_confirmation"`
From *string `yaml:"from"`
Server *string `yaml:"server"`
Port *string `yaml:"port"`
Username *string `yaml:"username"`
Password *string `yaml:"password"`
TLS_SSL *bool `yaml:"tls_ssl"`
STARTTLS *bool `yaml:"starttls"`
}

EmailContent struct {
Subject *string `yaml:"subject"`
Body *string `yaml:"body"`
}

Time struct {
Start *string `yaml:"start"`
End *string `yaml:"end"`
Freeze *string `yaml:"freeze"`
ViewAfter *bool `yaml:"view_after"`
}

Global struct {
Name string `yaml:"name"` // required
Description string `yaml:"description"` // required
Mode string `yaml:"mode"`
TeamSize *int `yaml:"team_size"`
VerifyEmails bool `yaml:"verify_emails"`
Start string `yaml:"start"`
End string `yaml:"end"`
Social struct {
Shares *bool `yaml:"shares"`
}

Visibilities struct {
Challenge string `yaml:"challenge"`
Account string `yaml:"account"`
Score string `yaml:"score"`
Registration string `yaml:"registration"`
Legal struct {
TOS ExternalReference `yaml:"tos"`
PrivacyPolicy ExternalReference `yaml:"privacy_policy"`
}

Front struct {
Theme string `yaml:"theme"`
ThemeColor string `yaml:"theme_color"`
Logo *string `yaml:"logo"`
Banner *string `yaml:"banner"`
SmallIcon *string `yaml:"small_icon"`
ExternalReference struct {
URL *string `yaml:"url"`
Content *string `yaml:"content"`
}

Admin struct {
Expand All @@ -48,11 +126,11 @@ type (

func (conf Config) Validate() error {
var merr error
if conf.Global.Name == "" {
merr = multierr.Append(merr, &ErrRequired{Attribute: "global.name"})
if conf.Appearance.Name == "" {
merr = multierr.Append(merr, &ErrRequired{Attribute: "appearance.name"})
}
if conf.Global.Description == "" {
merr = multierr.Append(merr, &ErrRequired{Attribute: "global.description"})
if conf.Appearance.Description == "" {
merr = multierr.Append(merr, &ErrRequired{Attribute: "appearance.description"})
}
if conf.Admin.Name == "" {
merr = multierr.Append(merr, &ErrRequired{Attribute: "admin.name"})
Expand All @@ -68,7 +146,7 @@ func (conf Config) Validate() error {
}

// Does not validate attributes content, let CTFd deal with
// that and provide a meaningful error message
// that and provide a meaningful error message... if it can :)

return nil
}
Expand Down
8 changes: 8 additions & 0 deletions examples/minimal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global:
name: 'MyCTF 20XX'
description: 'MyCTF description'

admin:
name: 'my-name'
email: '[email protected]'
password: 'dont put password in a config file, use the varenv'
38 changes: 27 additions & 11 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,37 @@ package ctfdsetup

import (
"os"
"path"

"github.com/ctfer-io/go-ctfd/api"
"gopkg.in/yaml.v3"
)

func File(loc *string) (*api.InputFile, error) {
if loc == nil || *loc == "" {
return nil, nil
type File struct {
Name string
Content []byte
}

var _ yaml.Unmarshaler = (*File)(nil)

func (file *File) UnmarshalYAML(node *yaml.Node) error {
if node.Value != "" {
file.Content = []byte(node.Value)
}
type lfi struct {
FromFile *string `yaml:"from_file"`
}
b, err := os.ReadFile(*loc)
var lfiv lfi
if err := node.Decode(&lfiv); err != nil {
return err
}

if lfiv.FromFile == nil {
return nil
}

fc, err := os.ReadFile(*lfiv.FromFile)
if err != nil {
return nil, err
return err
}
return &api.InputFile{
Name: path.Base(*loc),
Content: b,
}, nil
file.Content = fc
return nil
}
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/ctfd-setup
go 1.22.2

require (
github.com/ctfer-io/go-ctfd v0.6.2
github.com/ctfer-io/go-ctfd v0.6.5
github.com/pkg/errors v0.9.1
github.com/urfave/cli/v2 v2.27.2
go.uber.org/multierr v1.11.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/ctfer-io/go-ctfd v0.6.2 h1:dRTWiyi9LSimOaUGuShqg3oYXN0Ur3h7bt2lHlB5zok=
github.com/ctfer-io/go-ctfd v0.6.2/go.mod h1:zOOgs1LmKEVW3rilcog0jT921vjShmR3avJbSMtvNyM=
github.com/ctfer-io/go-ctfd v0.6.5 h1:9Pdg+oft9SLQlt4/gvOBlX8bW85vsedeOIF7Pb6Z8hM=
github.com/ctfer-io/go-ctfd v0.6.5/go.mod h1:zOOgs1LmKEVW3rilcog0jT921vjShmR3avJbSMtvNyM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/schema v1.3.0 h1:rbciOzXAx3IB8stEFnfTwO3sYa6EWlQk79XdyustPDA=
Expand Down
Loading

0 comments on commit b227aae

Please sign in to comment.