From 29184b2a73ad23a9308c18ea78a2df956988882a Mon Sep 17 00:00:00 2001 From: Lucas TESSON Date: Tue, 16 Apr 2024 00:10:29 +0200 Subject: [PATCH] feat(cd): improve continuous setup support --- errors.go | 13 +++++++++++++ go.mod | 2 +- go.sum | 4 ++-- setup.go | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 errors.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..97abee2 --- /dev/null +++ b/errors.go @@ -0,0 +1,13 @@ +package ctfdsetup + +import "github.com/pkg/errors" + +type ErrClient struct { + err error +} + +var _ error = (*ErrClient)(nil) + +func (err ErrClient) Error() string { + return errors.Wrap(err.err, "client error").Error() +} diff --git a/go.mod b/go.mod index 3471898..e837643 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ctfer-io/ctfd-setup go 1.22.2 require ( - github.com/ctfer-io/go-ctfd v0.6.0 + github.com/ctfer-io/go-ctfd v0.6.2 github.com/pkg/errors v0.9.1 github.com/urfave/cli/v2 v2.27.1 go.uber.org/multierr v1.10.0 diff --git a/go.sum b/go.sum index 4cebac9..c85c965 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/ctfer-io/go-ctfd v0.6.0 h1:6sV4pFthZeNoCKJfsPOJ5Hr1drDoiY7KVWILGjRpQAU= -github.com/ctfer-io/go-ctfd v0.6.0/go.mod h1:zOOgs1LmKEVW3rilcog0jT921vjShmR3avJbSMtvNyM= +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/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= diff --git a/setup.go b/setup.go index 8d1c1a4..dbfb06f 100644 --- a/setup.go +++ b/setup.go @@ -3,6 +3,7 @@ package ctfdsetup import ( "context" "net/http" + "strconv" "github.com/ctfer-io/go-ctfd/api" "github.com/pkg/errors" @@ -41,7 +42,7 @@ func bare(ctx context.Context, url string) (bool, error) { } res, err := client.Do(req) if err != nil { - return false, err + return false, &ErrClient{err: err} } return res.StatusCode == 200, nil // 302 if already setup } @@ -63,7 +64,8 @@ func bareSetup(ctx context.Context, client *api.Client, conf *Config) error { } // Flatten configuration and setup it - return client.Setup(&api.SetupParams{ + // TODO basic setup only, will be updated in the upcoming API calls + if err := client.Setup(&api.SetupParams{ CTFName: conf.Global.Name, CTFDescription: conf.Global.Description, UserMode: conf.Global.Mode, @@ -83,12 +85,49 @@ func bareSetup(ctx context.Context, client *api.Client, conf *Config) error { ThemeColor: conf.Front.ThemeColor, Start: conf.Global.Start, End: conf.Global.End, - }, api.WithContext(ctx)) + }, api.WithContext(ctx)); err != nil { + return &ErrClient{err: err} + } + return nil } -func updateSetup(_ context.Context, _ *api.Client, _ *Config) error { +func updateSetup(ctx context.Context, client *api.Client, conf *Config) error { + Log().Info("logging in") + + if err := client.Login(&api.LoginParams{ + Name: conf.Admin.Name, + Password: conf.Admin.Password, + }, api.WithContext(ctx)); err != nil { + return &ErrClient{err: err} + } + Log().Info("updating existing CTFd instance") - // TODO implement - return errors.New("not implemented yet") + if err := client.PatchConfigs(&api.PatchConfigsParams{ + CTFName: &conf.Global.Name, + CTFDescription: &conf.Global.Description, + UserMode: &conf.Global.Mode, + ChallengeVisibility: &conf.Visibilities.Challenge, + AccountVisibility: &conf.Visibilities.Account, + ScoreVisibility: &conf.Visibilities.Score, + RegistrationVisibility: &conf.Visibilities.Registration, + VerifyEmails: &conf.Global.VerifyEmails, + TeamSize: itoa(conf.Global.TeamSize), + // Admin configuration won't be updated + // TODO add support of front group + // TODO add support of other settings + Start: &conf.Global.Start, + End: &conf.Global.End, + }, api.WithContext(ctx)); err != nil { + return &ErrClient{err: err} + } + return nil +} + +func itoa(i *int) *string { + if i == nil { + return nil + } + s := strconv.Itoa(*i) + return &s }