Skip to content

Commit

Permalink
further improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten committed Feb 7, 2024
1 parent df09748 commit 69824f2
Show file tree
Hide file tree
Showing 19 changed files with 392 additions and 409 deletions.
34 changes: 2 additions & 32 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
woodpeckerGrpcServer "go.woodpecker-ci.org/woodpecker/v2/server/grpc"
"go.woodpecker-ci.org/woodpecker/v2/server/logging"
"go.woodpecker-ci.org/woodpecker/v2/server/model"

// "go.woodpecker-ci.org/woodpecker/v2/server/plugins/encryption"
// encryptedStore "go.woodpecker-ci.org/woodpecker/v2/server/plugins/encryption/wrapper/store"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/permissions"
Expand Down Expand Up @@ -280,39 +281,8 @@ func setupEvilGlobals(c *cli.Context, v store.Store, f forge.Forge) error {
server.Config.Services.Queue = setupQueue(c, v)
server.Config.Services.Logs = logging.New()
server.Config.Services.Pubsub = pubsub.New()
var err error
server.Config.Services.Registries, err = setupRegistryService(c, v)
if err != nil {
return err
}

// TODO(1544): fix encrypted store
// // encryption
// encryptedSecretStore := encryptedStore.NewSecretStore(v)
// err := encryption.Encryption(c, v).WithClient(encryptedSecretStore).Build()
// if err != nil {
// log.Fatal().Err(err).Msg("could not create encryption service")
// }
// server.Config.Services.Secrets = setupSecretService(c, encryptedSecretStore)
server.Config.Services.Secrets, err = setupSecretService(c, v)
if err != nil {
return err
}
server.Config.Services.Environ, err = setupEnvironService(c, v)
if err != nil {
return err
}
server.Config.Services.Membership = setupMembershipService(c, f)

server.Config.Services.SignaturePrivateKey, server.Config.Services.SignaturePublicKey, err = setupSignatureKeys(v)
if err != nil {
return err
}

server.Config.Services.ConfigService, err = setupConfigService(c)
if err != nil {
return err
}
// TODO: setup addon manager

// authentication
server.Config.Pipeline.AuthenticatePublicRepos = c.Bool("authenticate-public-repos")
Expand Down
63 changes: 0 additions & 63 deletions cmd/server/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/server/forge/gitea"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/github"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/gitlab"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/config"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/environments"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/registry"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/secrets"
"go.woodpecker-ci.org/woodpecker/v2/server/queue"
"go.woodpecker-ci.org/woodpecker/v2/server/store"
"go.woodpecker-ci.org/woodpecker/v2/server/store/datastore"
Expand Down Expand Up @@ -105,48 +100,6 @@ func setupQueue(c *cli.Context, s store.Store) queue.Queue {
return queue.WithTaskStore(queue.New(c.Context), s)
}

func setupSecretService(c *cli.Context, s model.SecretStore) (model.SecretService, error) {
addonService, err := addon.Load[model.SecretService](c.StringSlice("addons"), addonTypes.TypeSecretService)
if err != nil {
return nil, err
}
if addonService != nil {
return addonService.Value, nil
}

return secrets.New(c.Context, s), nil
}

func setupRegistryService(c *cli.Context, s store.Store) (model.RegistryService, error) {
addonService, err := addon.Load[model.RegistryService](c.StringSlice("addons"), addonTypes.TypeRegistryService)
if err != nil {
return nil, err
}
if addonService != nil {
return addonService.Value, nil
}

if c.String("docker-config") != "" {
return registry.Combined(
registry.New(s),
registry.Filesystem(c.String("docker-config")),
), nil
}
return registry.New(s), nil
}

func setupEnvironService(c *cli.Context, _ store.Store) (model.EnvironService, error) {
addonService, err := addon.Load[model.EnvironService](c.StringSlice("addons"), addonTypes.TypeEnvironmentService)
if err != nil {
return nil, err
}
if addonService != nil {
return addonService.Value, nil
}

return environments.Parse(c.StringSlice("environment")), nil
}

func setupMembershipService(_ *cli.Context, r forge.Forge) cache.MembershipService {
return cache.NewMembershipService(r)
}
Expand Down Expand Up @@ -286,19 +239,3 @@ func setupMetrics(g *errgroup.Group, _store store.Store) {
}
})
}

func setupConfigService(c *cli.Context) (config.Extension, error) {
addonExt, err := addon.Load[config.Extension](c.StringSlice("addons"), addonTypes.TypeConfigService)
if err != nil {
return nil, err
}
if addonExt != nil {
return addonExt.Value, nil
}

if endpoint := c.String("config-service-endpoint"); endpoint != "" {
return config.NewHTTP(endpoint, server.Config.Services.SignaturePrivateKey), nil
}

return nil, nil
}
20 changes: 6 additions & 14 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,25 @@
package server

import (
"crypto"
"time"

"go.woodpecker-ci.org/woodpecker/v2/server/cache"
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
"go.woodpecker-ci.org/woodpecker/v2/server/logging"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/config"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/permissions"
"go.woodpecker-ci.org/woodpecker/v2/server/pubsub"
"go.woodpecker-ci.org/woodpecker/v2/server/queue"
)

var Config = struct {
Services struct {
Pubsub *pubsub.Publisher
Queue queue.Queue
Logs logging.Log
Secrets model.SecretService
Registries model.RegistryService
Environ model.EnvironService
Forge forge.Forge
Timeout time.Duration
Membership cache.MembershipService
ConfigService config.Extension
SignaturePrivateKey crypto.PrivateKey
SignaturePublicKey crypto.PublicKey
Pubsub *pubsub.Publisher
Queue queue.Queue
Logs logging.Log
Forge forge.Forge
Timeout time.Duration
Membership cache.MembershipService
}
Storage struct {
// Users model.UserStore
Expand Down
5 changes: 0 additions & 5 deletions server/model/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ var (
errEnvironValueInvalid = errors.New("invalid Environment Variable Value")
)

// EnvironService defines a service for managing environment variables.
type EnvironService interface {
EnvironList(*Repo) ([]*Environ, error)
}

// EnvironStore persists environment information to storage.
type EnvironStore interface {
EnvironList(*Repo) ([]*Environ, error)
Expand Down
2 changes: 1 addition & 1 deletion server/plugins/config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type responseStructure struct {
Configs []config `json:"configs"`
}

func NewHTTP(endpoint string, privateKey crypto.PrivateKey) Extension {
func NewHTTP(endpoint string, privateKey crypto.PrivateKey) Service {
return &http{endpoint, privateKey}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/server/model"
)

type Extension interface {
type Service interface {
FetchConfig(ctx context.Context, repo *model.Repo, pipeline *model.Pipeline, currentFileMeta []*forge_types.FileMeta, netrc *model.Netrc) (configData []*forge_types.FileMeta, useOld bool, err error)
}
2 changes: 1 addition & 1 deletion server/plugins/environments/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type builtin struct {
}

// Parse returns a model.EnvironService based on a string slice where key and value are separated by a ":" delimiter.
func Parse(params []string) model.EnvironService {
func Parse(params []string) Service {
var globals []*model.Environ

for _, item := range params {
Expand Down
8 changes: 8 additions & 0 deletions server/plugins/environments/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package environments

import "go.woodpecker-ci.org/woodpecker/v2/server/model"

// Service defines a service for managing environment variables.
type Service interface {
EnvironList(*model.Repo) ([]*model.Environ, error)
}
76 changes: 76 additions & 0 deletions server/plugins/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package extensions

import (
"crypto"

"github.com/urfave/cli/v2"
"go.woodpecker-ci.org/woodpecker/v2/server/forge"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/config"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/environments"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/registry"
"go.woodpecker-ci.org/woodpecker/v2/server/plugins/secret"
"go.woodpecker-ci.org/woodpecker/v2/server/store"
)

type Manager struct {
secret secret.Service
registry registry.Service
config config.Service
environ environments.Service
signaturePrivateKey crypto.PrivateKey
signaturePublicKey crypto.PublicKey
}

func NewManager(store store.Store, forge forge.Forge, c *cli.Context) (*Manager, error) {
signaturePrivateKey, signaturePublicKey, err := setupSignatureKeys(store)
if err != nil {
return nil, err
}

config, err := setupConfigService(c, store, signaturePrivateKey)
if err != nil {
return nil, err
}

return &Manager{
signaturePrivateKey: signaturePrivateKey,
signaturePublicKey: signaturePublicKey,
secret: setupSecretExtension(store),
registry: setupRegistryExtension(store, c.String("docker-config")),
config: config,
environ: environments.Parse(c.StringSlice("environment")),
}, nil
}

func (e *Manager) SignaturePublicKey() crypto.PublicKey {
return e.signaturePublicKey
}

func (e *Manager) SecretAddonFromRepo(repo *model.Repo) secret.Service {
// if repo.SecretEndpoint != "" {
// return secret.NewHTTP(repo.SecretEndpoint, e.signaturePrivateKey)
// }

return e.secret
}

func (e *Manager) RegistryAddonFromRepo(repo *model.Repo) registry.Service {
// if repo.SecretEndpoint != "" {
// return registry.NewHTTP(repo.SecretEndpoint, e.signaturePrivateKey)
// }

return e.registry
}

func (e *Manager) ConfigAddonFromRepo(repo *model.Repo) config.Service {
// if repo.ConfigEndpoint != "" {
// return config.NewHTTP(repo.ConfigEndpoint, e.signaturePrivateKey)
// }

return e.config
}

func (e *Manager) EnvironAddonFromRepo(repo *model.Repo) environments.Service {
return e.environ
}
Loading

0 comments on commit 69824f2

Please sign in to comment.