2022-01-03 15:21:56 +01:00
package parser
import (
"errors"
2023-01-12 16:09:34 +01:00
"fmt"
2022-01-03 15:21:56 +01:00
2023-03-30 17:31:14 +02:00
"github.com/go-ldap/ldap/v3"
2024-08-04 13:04:52 +02:00
2022-05-04 14:49:59 +02:00
ociscfg "github.com/owncloud/ocis/v2/ocis-pkg/config"
2023-01-12 16:09:34 +01:00
defaults2 "github.com/owncloud/ocis/v2/ocis-pkg/config/defaults"
2024-08-06 17:16:14 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/config/envdecode"
2022-05-04 14:49:59 +02:00
"github.com/owncloud/ocis/v2/ocis-pkg/shared"
2022-06-20 16:49:06 +02:00
"github.com/owncloud/ocis/v2/services/graph/pkg/config"
"github.com/owncloud/ocis/v2/services/graph/pkg/config/defaults"
2024-08-04 13:04:52 +02:00
"github.com/owncloud/ocis/v2/services/graph/pkg/unifiedrole"
2022-01-03 15:21:56 +01:00
)
2022-04-29 20:21:14 +00:00
// ParseConfig loads configuration from known paths.
2022-01-03 15:21:56 +01:00
func ParseConfig ( cfg * config . Config ) error {
2024-04-30 18:18:31 +02:00
err := ociscfg . BindSourcesToStructs ( cfg . Service . Name , cfg )
2022-01-03 15:21:56 +01:00
if err != nil {
return err
}
2022-03-08 17:35:55 +01:00
defaults . EnsureDefaults ( cfg )
2022-01-03 15:21:56 +01: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-03-08 17:35:55 +01:00
defaults . Sanitize ( cfg )
2022-01-03 15:21:56 +01:00
2022-04-28 11:10:39 +02:00
return Validate ( cfg )
}
func Validate ( cfg * config . Config ) error {
2022-04-29 13:07:25 +02:00
if cfg . TokenManager . JWTSecret == "" {
return shared . MissingJWTTokenError ( cfg . Service . Name )
}
2023-03-30 17:31:14 +02:00
if cfg . Identity . Backend == "ldap" {
if err := validateLDAPSettings ( cfg ); err != nil {
return err
}
2022-04-29 16:10:21 +02:00
}
2023-01-12 16:09:34 +01:00
if cfg . Application . ID == "" {
return fmt . Errorf ( "The application ID has not been configured for %s. " +
"Make sure your %s config contains the proper values " +
"(e.g. by running ocis init or setting it manually in " +
"the config/corresponding environment variable)." ,
"graph" , defaults2 . BaseConfigPath ())
}
2023-01-13 15:23:40 +07:00
switch cfg . API . UsernameMatch {
case "default" , "none" :
default :
return fmt . Errorf ( "The username match validator is invalid for %s. " +
"Make sure your %s config contains the proper values " +
"(e.g. by running ocis init or setting it manually in " +
"the config/corresponding environment variable)." ,
"graph" , defaults2 . BaseConfigPath ())
}
2024-02-29 11:19:01 +01:00
if cfg . ServiceAccount . ServiceAccountID == "" {
return shared . MissingServiceAccountID ( cfg . Service . Name )
}
if cfg . ServiceAccount . ServiceAccountSecret == "" {
return shared . MissingServiceAccountSecret ( cfg . Service . Name )
}
2024-08-04 13:04:52 +02:00
// validate unified roles
{
var err error
for _ , uid := range cfg . UnifiedRoles . AvailableRoles {
// check if the role is known
2024-08-08 09:42:20 +02:00
if len ( unifiedrole . GetRoles ( unifiedrole . RoleFilterIDs ( uid ))) == 0 {
2024-08-04 13:04:52 +02:00
// collect all possible errors to return them all at once
2024-08-08 09:42:20 +02:00
err = errors . Join ( err , fmt . Errorf ( "%w: %s" , unifiedrole . ErrUnknownRole , uid ))
2024-08-04 13:04:52 +02:00
}
}
if err != nil {
return err
}
}
2022-01-03 15:21:56 +01:00
return nil
}
2023-03-30 17:31:14 +02:00
func validateLDAPSettings ( cfg * config . Config ) error {
if cfg . Identity . LDAP . BindPassword == "" {
return shared . MissingLDAPBindPassword ( cfg . Service . Name )
}
// ensure that "GroupBaseDN" is below "GroupBaseDN"
if cfg . Identity . LDAP . WriteEnabled && cfg . Identity . LDAP . GroupCreateBaseDN != cfg . Identity . LDAP . GroupBaseDN {
baseDN , err := ldap . ParseDN ( cfg . Identity . LDAP . GroupBaseDN )
if err != nil {
return fmt . Errorf ( "Unable to parse the LDAP Group Base DN '%s': %w " , cfg . Identity . LDAP . GroupBaseDN , err )
}
createBaseDN , err := ldap . ParseDN ( cfg . Identity . LDAP . GroupCreateBaseDN )
if err != nil {
return fmt . Errorf ( "Unable to parse the LDAP Group Create Base DN '%s': %w " , cfg . Identity . LDAP . GroupCreateBaseDN , err )
}
if ! baseDN . AncestorOfFold ( createBaseDN ) {
return fmt . Errorf ( "The LDAP Group Create Base DN (%s) must be subordinate to the LDAP Group Base DN (%s)" , cfg . Identity . LDAP . GroupCreateBaseDN , cfg . Identity . LDAP . GroupBaseDN )
}
}
return nil
}