@@ -33,6 +33,7 @@ import (
3333 "github.com/cozy/cozy-stack/pkg/tlsclient"
3434 "github.com/cozy/cozy-stack/pkg/utils"
3535 "github.com/cozy/gomail"
36+ "github.com/mitchellh/mapstructure"
3637 "github.com/redis/go-redis/v9"
3738 "github.com/spf13/viper"
3839)
@@ -131,8 +132,6 @@ type Config struct {
131132 Notifications Notifications
132133 Flagship Flagship
133134
134- Sharing SharingConfig
135-
136135 Lock lock.Getter
137136 Limiter * limits.RateLimiter
138137 SessionStorage redis.UniversalClient
@@ -319,47 +318,52 @@ type Flagship struct {
319318 AppleAppIDs []string
320319}
321320
322- // SharingConfig contains configuration for cozy-to-cozy sharing trust rules .
321+ // SharingConfig contains sharing trust settings for a specific context .
323322type SharingConfig struct {
324- AutoAcceptTrusted bool `mapstructure:"auto_accept_trusted"`
325- AutoAcceptTrustedContacts bool `mapstructure:"auto_accept_trusted_contacts"`
326- Contexts map [string ]SharingContext `mapstructure:"contexts"`
327- }
328-
329- // SharingContext allows overriding sharing trust settings for a specific context.
330- type SharingContext struct {
331- AutoAcceptTrusted * bool `mapstructure:"auto_accept_trusted"`
332- AutoAcceptTrustedContacts * bool `mapstructure:"auto_accept_trusted_contacts"`
323+ AutoAcceptTrusted bool `mapstructure:"auto_accept_trusted"`
324+ AutoAcceptTrustedContacts bool `mapstructure:"auto_accept_trusted_contacts"`
333325 TrustedDomains []string `mapstructure:"trusted_domains"`
334326}
335327
336- // OptionsForContext returns the effective sharing configuration for a given context,
337- // after applying context-specific overrides to the global settings.
338- func (c SharingConfig ) OptionsForContext (contextName string ) SharingContext {
339- // Start with an empty context
340- var result SharingContext
328+ // GetSharingConfig returns the sharing configuration for a given context.
329+ // It reads from contexts.<contextName>.sharing and falls back to contexts.default.sharing
330+ // if the specific context doesn't have sharing configuration.
331+ func GetSharingConfig (contextName string ) SharingConfig {
332+ // Default configuration with safe defaults
333+ defaultConfig := SharingConfig {
334+ AutoAcceptTrusted : false ,
335+ AutoAcceptTrustedContacts : false ,
336+ TrustedDomains : []string {},
337+ }
341338
342- // Try to get specific context, fallback to default context, or create empty
343- if contextName != "" {
344- if ctx , ok := c .Contexts [contextName ]; ok {
345- result = ctx
346- } else if defaultCtx , ok := c .Contexts [DefaultInstanceContext ]; ok {
347- result = defaultCtx
348- }
349- } else if defaultCtx , ok := c .Contexts [DefaultInstanceContext ]; ok {
350- result = defaultCtx
339+ if config == nil || config .Contexts == nil {
340+ return defaultConfig
351341 }
352342
353- // Apply global AutoAcceptTrusted if not set in context
354- if result .AutoAcceptTrusted == nil {
355- result .AutoAcceptTrusted = & c .AutoAcceptTrusted
343+ // Try to get sharing config from the specific context
344+ if contextName != "" {
345+ if ctxData , ok := config .Contexts [contextName ].(map [string ]interface {}); ok {
346+ if sharingData , ok := ctxData ["sharing" ].(map [string ]interface {}); ok {
347+ var cfg SharingConfig
348+ if err := mapstructure .Decode (sharingData , & cfg ); err == nil {
349+ return cfg
350+ }
351+ }
352+ }
356353 }
357354
358- if result .AutoAcceptTrustedContacts == nil {
359- result .AutoAcceptTrustedContacts = & c .AutoAcceptTrustedContacts
355+ // Fall back to default context
356+ if ctxData , ok := config .Contexts [DefaultInstanceContext ].(map [string ]interface {}); ok {
357+ if sharingData , ok := ctxData ["sharing" ].(map [string ]interface {}); ok {
358+ var cfg SharingConfig
359+ if err := mapstructure .Decode (sharingData , & cfg ); err == nil {
360+ return cfg
361+ }
362+ }
360363 }
361364
362- return result
365+ // Return empty config with safe defaults
366+ return defaultConfig
363367}
364368
365369// SMS contains the configuration to send notifications by SMS.
@@ -606,7 +610,6 @@ func applyDefaults(v *viper.Viper) {
606610 v .SetDefault ("assets_polling_interval" , 2 * time .Minute )
607611 v .SetDefault ("fs.versioning.max_number_of_versions_to_keep" , 20 )
608612 v .SetDefault ("fs.versioning.min_delay_between_two_versions" , 15 * time .Minute )
609- v .SetDefault ("sharing.auto_accept_trusted" , false )
610613}
611614
612615func envMap () map [string ]string {
@@ -688,11 +691,6 @@ func UseViper(v *viper.Viper) error {
688691 return err
689692 }
690693
691- sharingCfg , err := makeSharingConfig (v )
692- if err != nil {
693- return err
694- }
695-
696694 var subdomains SubdomainType
697695 if subs := v .GetString ("subdomains" ); subs != "" {
698696 switch subs {
@@ -1002,7 +1000,6 @@ func UseViper(v *viper.Viper) error {
10021000 PlayIntegrityVerificationKeys : v .GetStringSlice ("flagship.play_integrity_verification_keys" ),
10031001 AppleAppIDs : v .GetStringSlice ("flagship.apple_app_ids" ),
10041002 },
1005- Sharing : sharingCfg ,
10061003 Lock : lock .New (lockRedis ),
10071004 SessionStorage : sessionsRedis ,
10081005 DownloadStorage : downloadRedis ,
@@ -1255,22 +1252,6 @@ func makeSMS(raw map[string]interface{}) map[string]SMS {
12551252 return sms
12561253}
12571254
1258- func makeSharingConfig (v * viper.Viper ) (SharingConfig , error ) {
1259- var cfg SharingConfig
1260-
1261- // Use UnmarshalKey to automatically handle type conversions
1262- if err := v .UnmarshalKey ("sharing" , & cfg ); err != nil {
1263- return SharingConfig {}, fmt .Errorf ("config: failed to unmarshal sharing config: %w" , err )
1264- }
1265-
1266- // Ensure Contexts map is initialized even if not present in config
1267- if cfg .Contexts == nil {
1268- cfg .Contexts = map [string ]SharingContext {}
1269- }
1270-
1271- return cfg , nil
1272- }
1273-
12741255func makeCommonSettings (v * viper.Viper ) (map [string ]CommonSettings , error ) {
12751256 settings := make (map [string ]CommonSettings )
12761257
0 commit comments