2022-01-03 15:21:56 +01:00
|
|
|
package parser
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
|
2022-05-04 14:49:59 +02:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/config"
|
|
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/config/envdecode"
|
|
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/shared"
|
2023-03-03 00:39:00 +01:00
|
|
|
"github.com/owncloud/ocis/v2/ocis-pkg/structs"
|
2022-01-03 15:21:56 +01:00
|
|
|
)
|
|
|
|
|
|
2022-04-28 15:34:31 +02:00
|
|
|
// ParseConfig loads the ocis configuration and
|
|
|
|
|
// copies applicable parts into the commons part, from
|
2022-08-17 15:36:52 +02:00
|
|
|
// where the services can copy it into their own config
|
2022-06-02 09:55:56 +02:00
|
|
|
func ParseConfig(cfg *config.Config, skipValidate bool) error {
|
2024-04-30 18:18:31 +02:00
|
|
|
err := config.BindSourcesToStructs("ocis", cfg)
|
2022-01-03 15:21:56 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 16:16:06 +02:00
|
|
|
EnsureDefaults(cfg)
|
2022-04-28 15:34:31 +02:00
|
|
|
|
|
|
|
|
// load all env variables relevant to the config in the current context.
|
|
|
|
|
if err := envdecode.Decode(cfg); err != nil {
|
|
|
|
|
// no environment variable set for this config is an expected "error"
|
|
|
|
|
if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 16:16:06 +02:00
|
|
|
EnsureCommons(cfg)
|
|
|
|
|
|
2022-06-02 09:55:56 +02:00
|
|
|
if skipValidate {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 15:34:31 +02:00
|
|
|
return Validate(cfg)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 00:39:00 +01:00
|
|
|
// EnsureDefaults ensures that all pointers in the
|
2022-08-17 15:36:52 +02:00
|
|
|
// oCIS config (not the services configs) are initialized
|
2022-04-28 16:16:06 +02:00
|
|
|
func EnsureDefaults(cfg *config.Config) {
|
|
|
|
|
if cfg.Tracing == nil {
|
|
|
|
|
cfg.Tracing = &shared.Tracing{}
|
|
|
|
|
}
|
|
|
|
|
if cfg.Log == nil {
|
|
|
|
|
cfg.Log = &shared.Log{}
|
|
|
|
|
}
|
|
|
|
|
if cfg.TokenManager == nil {
|
|
|
|
|
cfg.TokenManager = &shared.TokenManager{}
|
|
|
|
|
}
|
2023-03-22 15:21:57 +01:00
|
|
|
if cfg.Cache == nil {
|
|
|
|
|
cfg.Cache = &shared.Cache{}
|
2022-09-16 15:42:47 +02:00
|
|
|
}
|
2022-11-03 10:17:08 +01:00
|
|
|
if cfg.GRPCClientTLS == nil {
|
|
|
|
|
cfg.GRPCClientTLS = &shared.GRPCClientTLS{}
|
|
|
|
|
}
|
2026-10-06 17:21:46 +02:00
|
|
|
if cfg.GRPCClientOptions == nil {
|
|
|
|
|
cfg.GRPCClientOptions = &shared.GRPCClientOptions{}
|
|
|
|
|
}
|
2022-11-03 10:17:08 +01:00
|
|
|
if cfg.GRPCServiceTLS == nil {
|
|
|
|
|
cfg.GRPCServiceTLS = &shared.GRPCServiceTLS{}
|
|
|
|
|
}
|
2024-06-28 17:25:40 +02:00
|
|
|
if cfg.Reva == nil {
|
|
|
|
|
cfg.Reva = &shared.Reva{}
|
|
|
|
|
}
|
2022-04-28 16:16:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EnsureCommons copies applicable parts of the oCIS config into the commons part
|
|
|
|
|
func EnsureCommons(cfg *config.Config) {
|
2022-04-28 15:34:31 +02:00
|
|
|
// ensure the commons part is initialized
|
2022-04-22 14:32:41 +02:00
|
|
|
if cfg.Commons == nil {
|
|
|
|
|
cfg.Commons = &shared.Commons{}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 00:39:00 +01:00
|
|
|
cfg.Commons.Log = structs.CopyOrZeroValue(cfg.Log)
|
|
|
|
|
cfg.Commons.Tracing = structs.CopyOrZeroValue(cfg.Tracing)
|
2023-03-22 15:21:57 +01:00
|
|
|
cfg.Commons.Cache = structs.CopyOrZeroValue(cfg.Cache)
|
2022-09-16 15:42:47 +02:00
|
|
|
|
2022-11-03 10:17:08 +01:00
|
|
|
if cfg.GRPCClientTLS != nil {
|
|
|
|
|
cfg.Commons.GRPCClientTLS = cfg.GRPCClientTLS
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 17:21:46 +02:00
|
|
|
if cfg.GRPCClientOptions != nil {
|
|
|
|
|
cfg.Commons.GRPCClientOptions = cfg.GRPCClientOptions
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 10:17:08 +01:00
|
|
|
if cfg.GRPCServiceTLS != nil {
|
|
|
|
|
cfg.Commons.GRPCServiceTLS = cfg.GRPCServiceTLS
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 15:50:32 +01:00
|
|
|
cfg.Commons.HTTPServiceTLS = cfg.HTTPServiceTLS
|
|
|
|
|
|
2023-03-03 00:39:00 +01:00
|
|
|
cfg.Commons.TokenManager = structs.CopyOrZeroValue(cfg.TokenManager)
|
2022-04-22 14:32:41 +02:00
|
|
|
|
2022-04-28 15:34:31 +02:00
|
|
|
// copy machine auth api key to the commons part if set
|
2022-04-26 10:18:12 +02:00
|
|
|
if cfg.MachineAuthAPIKey != "" {
|
|
|
|
|
cfg.Commons.MachineAuthAPIKey = cfg.MachineAuthAPIKey
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 15:48:02 +02:00
|
|
|
if cfg.SystemUserAPIKey != "" {
|
|
|
|
|
cfg.Commons.SystemUserAPIKey = cfg.SystemUserAPIKey
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 15:34:31 +02:00
|
|
|
// copy transfer secret to the commons part if set
|
2022-04-26 14:09:29 +02:00
|
|
|
if cfg.TransferSecret != "" {
|
|
|
|
|
cfg.Commons.TransferSecret = cfg.TransferSecret
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 12:36:30 +00:00
|
|
|
// copy metadata user id to the commons part if set
|
2022-05-04 11:41:37 +02:00
|
|
|
if cfg.SystemUserID != "" {
|
|
|
|
|
cfg.Commons.SystemUserID = cfg.SystemUserID
|
2022-05-02 12:36:30 +00:00
|
|
|
}
|
2022-05-03 12:31:53 +02:00
|
|
|
|
|
|
|
|
// copy admin user id to the commons part if set
|
|
|
|
|
if cfg.AdminUserID != "" {
|
|
|
|
|
cfg.Commons.AdminUserID = cfg.AdminUserID
|
|
|
|
|
}
|
2022-09-29 14:59:16 +02:00
|
|
|
|
|
|
|
|
if cfg.OcisURL != "" {
|
|
|
|
|
cfg.Commons.OcisURL = cfg.OcisURL
|
|
|
|
|
}
|
2024-06-28 17:25:40 +02:00
|
|
|
|
|
|
|
|
cfg.Commons.Reva = structs.CopyOrZeroValue(cfg.Reva)
|
2022-04-28 15:34:31 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-03 00:39:00 +01:00
|
|
|
// Validate checks that all required configs are set. If a required config value
|
|
|
|
|
// is missing an error will be returned.
|
2022-04-28 15:34:31 +02:00
|
|
|
func Validate(cfg *config.Config) error {
|
|
|
|
|
if cfg.TokenManager.JWTSecret == "" {
|
2022-04-29 11:15:13 +02:00
|
|
|
return shared.MissingJWTTokenError("ocis")
|
2022-04-28 15:34:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cfg.TransferSecret == "" {
|
2022-04-29 11:15:13 +02:00
|
|
|
return shared.MissingRevaTransferSecretError("ocis")
|
2022-04-28 15:34:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cfg.MachineAuthAPIKey == "" {
|
2022-04-29 11:15:13 +02:00
|
|
|
return shared.MissingMachineAuthApiKeyError("ocis")
|
2022-01-03 15:21:56 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-04 11:41:37 +02:00
|
|
|
if cfg.SystemUserID == "" {
|
|
|
|
|
return shared.MissingSystemUserID("ocis")
|
2022-05-02 12:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-03 15:21:56 +01:00
|
|
|
return nil
|
|
|
|
|
}
|