agola/internal/services/config/config.go

422 lines
11 KiB
Go
Raw Normal View History

2019-02-21 15:05:06 +00:00
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"io/ioutil"
"time"
"agola.io/agola/internal/errors"
2019-07-01 09:40:20 +00:00
"agola.io/agola/internal/util"
2019-02-21 15:05:06 +00:00
yaml "gopkg.in/yaml.v2"
)
2019-04-30 10:08:59 +00:00
const (
maxIDLength = 20
)
2019-02-21 15:05:06 +00:00
type Config struct {
2019-04-30 10:08:59 +00:00
// ID defines the agola installation id. It's used inside the
// various services to uniquely distinguish it from other installations
// Defaults to "agola"
ID string `yaml:"id"`
Gateway Gateway `yaml:"gateway"`
Scheduler Scheduler `yaml:"scheduler"`
Notification Notification `yaml:"notification"`
Runservice Runservice `yaml:"runservice"`
Executor Executor `yaml:"executor"`
Configstore Configstore `yaml:"configstore"`
Gitserver Gitserver `yaml:"gitserver"`
2019-02-21 15:05:06 +00:00
}
type Gateway struct {
Debug bool `yaml:"debug"`
// APIExposedURL is the gateway API exposed url i.e. https://myagola.example.com
APIExposedURL string `yaml:"apiExposedURL"`
// WebExposedURL is the web interface exposed url i.e. https://myagola.example.com
2019-02-21 15:05:06 +00:00
// This is used for generating the redirect_url in oauth2 redirects
WebExposedURL string `yaml:"webExposedURL"`
RunserviceURL string `yaml:"runserviceURL"`
2019-05-07 21:42:42 +00:00
ConfigstoreURL string `yaml:"configstoreURL"`
2019-05-08 13:23:13 +00:00
GitserverURL string `yaml:"gitserverURL"`
2019-02-21 15:05:06 +00:00
Web Web `yaml:"web"`
Etcd Etcd `yaml:"etcd"`
ObjectStorage ObjectStorage `yaml:"objectStorage"`
2019-02-21 15:05:06 +00:00
TokenSigning TokenSigning `yaml:"tokenSigning"`
AdminToken string `yaml:"adminToken"`
}
type Scheduler struct {
Debug bool `yaml:"debug"`
RunserviceURL string `yaml:"runserviceURL"`
2019-02-21 15:05:06 +00:00
}
type Notification struct {
Debug bool `yaml:"debug"`
// WebExposedURL is the web interface exposed url i.e. https://myagola.example.com
// This is used for generating the redirect_url in oauth2 redirects
WebExposedURL string `yaml:"webExposedURL"`
RunserviceURL string `yaml:"runserviceURL"`
ConfigstoreURL string `yaml:"configstoreURL"`
Etcd Etcd `yaml:"etcd"`
}
type Runservice struct {
2019-02-21 15:05:06 +00:00
Debug bool `yaml:"debug"`
DataDir string `yaml:"dataDir"`
Web Web `yaml:"web"`
Etcd Etcd `yaml:"etcd"`
ObjectStorage ObjectStorage `yaml:"objectStorage"`
RunCacheExpireInterval time.Duration `yaml:"runCacheExpireInterval"`
RunWorkspaceExpireInterval time.Duration `yaml:"runWorkspaceExpireInterval"`
2019-02-21 15:05:06 +00:00
}
type Executor struct {
2019-02-21 15:05:06 +00:00
Debug bool `yaml:"debug"`
DataDir string `yaml:"dataDir"`
RunserviceURL string `yaml:"runserviceURL"`
2019-02-21 15:05:06 +00:00
ToolboxPath string `yaml:"toolboxPath"`
Web Web `yaml:"web"`
2019-04-17 13:25:11 +00:00
2019-04-22 15:54:24 +00:00
Driver Driver `yaml:"driver"`
InitImage InitImage `yaml:"initImage"`
2019-04-17 13:25:11 +00:00
Labels map[string]string `yaml:"labels"`
// ActiveTasksLimit is the max number of concurrent active tasks
ActiveTasksLimit int `yaml:"activeTasksLimit"`
AllowPrivilegedContainers bool `yaml:"allowPrivilegedContainers"`
2019-02-21 15:05:06 +00:00
}
type InitImage struct {
Image string `yaml:"image"`
Auth *DockerRegistryAuth `yaml:"auth"`
}
type DockerRegistryAuthType string
const (
DockerRegistryAuthTypeBasic DockerRegistryAuthType = "basic"
DockerRegistryAuthTypeEncodedAuth DockerRegistryAuthType = "encodedauth"
)
type DockerRegistryAuth struct {
Type DockerRegistryAuthType `json:"type"`
// basic auth
Username string `json:"username"`
Password string `json:"password"`
// encoded auth string
Auth string `json:"auth"`
// future auths like aws ecr auth
}
2019-05-07 21:42:42 +00:00
type Configstore struct {
2019-02-21 15:05:06 +00:00
Debug bool `yaml:"debug"`
DataDir string `yaml:"dataDir"`
Web Web `yaml:"web"`
Etcd Etcd `yaml:"etcd"`
ObjectStorage ObjectStorage `yaml:"objectStorage"`
2019-02-21 15:05:06 +00:00
}
2019-05-08 13:23:13 +00:00
type Gitserver struct {
2019-02-21 15:05:06 +00:00
Debug bool `yaml:"debug"`
DataDir string `yaml:"dataDir"`
Web Web `yaml:"web"`
Etcd Etcd `yaml:"etcd"`
ObjectStorage ObjectStorage `yaml:"objectStorage"`
RepositoryCleanupInterval time.Duration `yaml:"repositoryCleanupInterval"`
RepositoryRefsExpireInterval time.Duration `yaml:"repositoryRefsExpireInterval"`
2019-02-21 15:05:06 +00:00
}
type Web struct {
// http listen addess
ListenAddress string `yaml:"listenAddress"`
// use TLS (https)
TLS bool `yaml:"tls"`
// TLSCert is the path to the pem formatted server certificate. If the
// certificate is signed by a certificate authority, the certFile should be
// the concatenation of the server's certificate, any intermediates, and the
// CA's certificate.
TLSCertFile string `yaml:"tlsCertFile"`
// Server cert private key
// TODO(sgotti) support encrypted private keys (add a private key password config entry)
TLSKeyFile string `yaml:"tlsKeyFile"`
// CORS allowed origins
AllowedOrigins []string `yaml:"allowedOrigins"`
}
type ObjectStorageType string
2019-02-21 15:05:06 +00:00
const (
ObjectStorageTypePosix ObjectStorageType = "posix"
ObjectStorageTypeS3 ObjectStorageType = "s3"
2019-02-21 15:05:06 +00:00
)
type ObjectStorage struct {
Type ObjectStorageType `yaml:"type"`
2019-02-21 15:05:06 +00:00
// Posix
Path string `yaml:"path"`
// S3
Endpoint string `yaml:"endpoint"`
Bucket string `yaml:"bucket"`
Location string `yaml:"location"`
AccessKey string `yaml:"accessKey"`
SecretAccessKey string `yaml:"secretAccessKey"`
DisableTLS bool `yaml:"disableTLS"`
}
type Etcd struct {
Endpoints string `yaml:"endpoints"`
// TODO(sgotti) support encrypted private keys (add a private key password config entry)
TLSCertFile string `yaml:"tlsCertFile"`
TLSKeyFile string `yaml:"tlsKeyFile"`
TLSCAFile string `yaml:"tlsCAFile"`
TLSSkipVerify bool `yaml:"tlsSkipVerify"`
}
2019-04-22 15:54:24 +00:00
type DriverType string
const (
DriverTypeDocker DriverType = "docker"
DriverTypeK8s DriverType = "kubernetes"
)
type Driver struct {
Type DriverType `yaml:"type"`
// docker fields
// k8s fields
}
2019-02-21 15:05:06 +00:00
type TokenSigning struct {
// token duration (defaults to 12 hours)
Duration time.Duration `yaml:"duration"`
// signing method: "hmac" or "rsa"
Method string `yaml:"method"`
// signing key. Used only with HMAC signing method
Key string `yaml:"key"`
// path to a file containing a pem encoded private key. Used only with RSA signing method
PrivateKeyPath string `yaml:"privateKeyPath"`
// path to a file containing a pem encoded public key. Used only with RSA signing method
PublicKeyPath string `yaml:"publicKeyPath"`
}
var defaultConfig = Config{
2019-04-30 10:08:59 +00:00
ID: "agola",
2019-02-21 15:05:06 +00:00
Gateway: Gateway{
TokenSigning: TokenSigning{
Duration: 12 * time.Hour,
},
},
Runservice: Runservice{
RunCacheExpireInterval: 7 * 24 * time.Hour,
RunWorkspaceExpireInterval: 7 * 24 * time.Hour,
},
Executor: Executor{
InitImage: InitImage{
Image: "busybox:stable",
},
ActiveTasksLimit: 2,
},
Gitserver: Gitserver{
RepositoryCleanupInterval: 24 * time.Hour,
RepositoryRefsExpireInterval: 30 * 24 * time.Hour,
},
2019-02-21 15:05:06 +00:00
}
func Parse(configFile string, componentsNames []string) (*Config, error) {
2019-02-21 15:05:06 +00:00
configData, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, errors.WithStack(err)
2019-02-21 15:05:06 +00:00
}
c := &defaultConfig
if err := yaml.Unmarshal(configData, &c); err != nil {
return nil, errors.WithStack(err)
2019-02-21 15:05:06 +00:00
}
return c, Validate(c, componentsNames)
2019-02-21 15:05:06 +00:00
}
func validateWeb(w *Web) error {
if w.ListenAddress == "" {
return errors.Errorf("listen address undefined")
}
if w.TLS {
if w.TLSKeyFile == "" {
return errors.Errorf("no tls key file specified")
}
if w.TLSCertFile == "" {
return errors.Errorf("no tls cert file specified")
}
}
return nil
}
func validateInitImage(i *InitImage) error {
if i.Image == "" {
return errors.Errorf("image is empty")
}
return nil
}
func Validate(c *Config, componentsNames []string) error {
2019-04-30 10:08:59 +00:00
// Global
if len(c.ID) > maxIDLength {
return errors.Errorf("id too long")
}
if !util.ValidateName(c.ID) {
return errors.Errorf("invalid id")
}
2019-02-21 15:05:06 +00:00
// Gateway
if isComponentEnabled(componentsNames, "gateway") {
if c.Gateway.APIExposedURL == "" {
return errors.Errorf("gateway apiExposedURL is empty")
}
if c.Gateway.WebExposedURL == "" {
return errors.Errorf("gateway webExposedURL is empty")
}
if c.Gateway.ConfigstoreURL == "" {
return errors.Errorf("gateway configstoreURL is empty")
}
if c.Gateway.RunserviceURL == "" {
return errors.Errorf("gateway runserviceURL is empty")
}
if err := validateWeb(&c.Gateway.Web); err != nil {
return errors.Wrapf(err, "gateway web configuration error")
}
2019-02-21 15:05:06 +00:00
}
// Configstore
if isComponentEnabled(componentsNames, "configstore") {
if c.Configstore.DataDir == "" {
return errors.Errorf("configstore dataDir is empty")
}
if err := validateWeb(&c.Configstore.Web); err != nil {
return errors.Wrapf(err, "configstore web configuration error")
}
2019-02-21 15:05:06 +00:00
}
// Runservice
if isComponentEnabled(componentsNames, "runservice") {
if c.Runservice.DataDir == "" {
return errors.Errorf("runservice dataDir is empty")
}
if err := validateWeb(&c.Runservice.Web); err != nil {
return errors.Wrapf(err, "runservice web configuration error")
}
2019-02-21 15:05:06 +00:00
}
// Executor
if isComponentEnabled(componentsNames, "executor") {
if c.Executor.DataDir == "" {
return errors.Errorf("executor dataDir is empty")
}
if c.Executor.ToolboxPath == "" {
return errors.Errorf("git server toolboxPath is empty")
}
if c.Executor.RunserviceURL == "" {
return errors.Errorf("executor runserviceURL is empty")
}
if c.Executor.Driver.Type == "" {
return errors.Errorf("executor driver type is empty")
}
switch c.Executor.Driver.Type {
case DriverTypeDocker:
case DriverTypeK8s:
default:
return errors.Errorf("executor driver type %q unknown", c.Executor.Driver.Type)
}
if err := validateInitImage(&c.Executor.InitImage); err != nil {
return errors.Wrapf(err, "executor initImage configuration error")
}
2019-04-22 15:54:24 +00:00
}
2019-02-21 15:05:06 +00:00
// Scheduler
if isComponentEnabled(componentsNames, "scheduler") {
if c.Scheduler.RunserviceURL == "" {
return errors.Errorf("scheduler runserviceURL is empty")
}
2019-02-21 15:05:06 +00:00
}
// Notification
if isComponentEnabled(componentsNames, "notification") {
if c.Notification.WebExposedURL == "" {
return errors.Errorf("notification webExposedURL is empty")
}
if c.Notification.ConfigstoreURL == "" {
return errors.Errorf("notification configstoreURL is empty")
}
if c.Notification.RunserviceURL == "" {
return errors.Errorf("notification runserviceURL is empty")
}
}
2019-02-21 15:05:06 +00:00
// Git server
if isComponentEnabled(componentsNames, "gitserver") {
if c.Gitserver.DataDir == "" {
return errors.Errorf("git server dataDir is empty")
}
2019-02-21 15:05:06 +00:00
}
return nil
}
func isComponentEnabled(componentsNames []string, name string) bool {
if util.StringInSlice(componentsNames, "all-base") && name != "executor" {
return true
}
return util.StringInSlice(componentsNames, name)
}