diff --git a/changelog/unreleased/change-remove-owncloudsql-driver.md b/changelog/unreleased/change-remove-owncloudsql-driver.md new file mode 100644 index 00000000000..bd3460c214e --- /dev/null +++ b/changelog/unreleased/change-remove-owncloudsql-driver.md @@ -0,0 +1,9 @@ +Change: Remove the owncloudsql driver + +We removed the deprecated `owncloudsql` driver from oCIS. + +The services `storage-users`, `auth-basic`, `users`, `groups`, and `sharing` no +longer accept the value `owncloudsql` for their driver. The related +configuration sections and environment variables are removed. + +https://github.com/owncloud/ocis/pull/13189 diff --git a/go.mod b/go.mod index ae63c0d51e9..c314fe422c8 100644 --- a/go.mod +++ b/go.mod @@ -67,7 +67,7 @@ require ( github.com/open-policy-agent/opa v1.21.1 github.com/orcaman/concurrent-map v1.0.0 github.com/owncloud/libre-graph-api-go v1.0.5-0.20260216101009-eeac018af245 - github.com/owncloud/reva/v2 v2.0.0-20261012083840-8b649a4c3899 + github.com/owncloud/reva/v2 v2.0.0-20261019151321-d079c7843edc github.com/pkg/errors v0.9.1 github.com/pkg/xattr v0.4.12 github.com/prometheus/client_golang v1.24.1 diff --git a/go.sum b/go.sum index 96f9b98c0c5..3aa43433228 100644 --- a/go.sum +++ b/go.sum @@ -736,8 +736,8 @@ github.com/orcaman/concurrent-map v1.0.0 h1:I/2A2XPCb4IuQWcQhBhSwGfiuybl/J0ev9HD github.com/orcaman/concurrent-map v1.0.0/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI= github.com/owncloud/libre-graph-api-go v1.0.5-0.20260216101009-eeac018af245 h1:JRidLTAKhnvyLMRtVtSF4lhBa0NSAOs6fof+d6JnKII= github.com/owncloud/libre-graph-api-go v1.0.5-0.20260216101009-eeac018af245/go.mod h1:z61VMGAJRtR1nbgXWiNoCkxUXP1B3Je9rMuJbnGd+Og= -github.com/owncloud/reva/v2 v2.0.0-20261012083840-8b649a4c3899 h1:IMu076AtnFEdabpBbowcGDYOeFLtm9ycNY1Z43YPYZU= -github.com/owncloud/reva/v2 v2.0.0-20261012083840-8b649a4c3899/go.mod h1:cgqg+UTe4ekjMbJj8hgoXSTxQ9fQramKcOUdvsFUrzc= +github.com/owncloud/reva/v2 v2.0.0-20261019151321-d079c7843edc h1:Cah6Vr9rYMVswYdkwKhMBEkChdwPLJvtI1vrYLeUVS0= +github.com/owncloud/reva/v2 v2.0.0-20261019151321-d079c7843edc/go.mod h1:cgqg+UTe4ekjMbJj8hgoXSTxQ9fQramKcOUdvsFUrzc= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= github.com/pablodz/inotifywaitgo v0.0.12 h1:DVJJTFMDlSAdO46yisPF2vzozs/r+4k6ih8MVghq78M= diff --git a/ocis/pkg/command/migrate.go b/ocis/pkg/command/migrate.go index a12a4d486e9..bbfea341089 100644 --- a/ocis/pkg/command/migrate.go +++ b/ocis/pkg/command/migrate.go @@ -501,15 +501,6 @@ func revaShareConfig(cfg *sharing.Config) map[string]interface{} { "enable_expired_shares_cleanup": cfg.UserSharingDrivers.SQL.EnableExpiredSharesCleanup, "janitor_run_interval": cfg.UserSharingDrivers.SQL.JanitorRunInterval, }, - "owncloudsql": map[string]interface{}{ - "gateway_addr": cfg.Reva.Address, - "storage_mount_id": cfg.UserSharingDrivers.OwnCloudSQL.UserStorageMountID, - "db_username": cfg.UserSharingDrivers.OwnCloudSQL.DBUsername, - "db_password": cfg.UserSharingDrivers.OwnCloudSQL.DBPassword, - "db_host": cfg.UserSharingDrivers.OwnCloudSQL.DBHost, - "db_port": cfg.UserSharingDrivers.OwnCloudSQL.DBPort, - "db_name": cfg.UserSharingDrivers.OwnCloudSQL.DBName, - }, "cs3": map[string]interface{}{ "gateway_addr": cfg.UserSharingDrivers.CS3.ProviderAddr, "provider_addr": cfg.UserSharingDrivers.CS3.ProviderAddr, diff --git a/services/auth-basic/pkg/config/config.go b/services/auth-basic/pkg/config/config.go index d73642b5be5..700f90563d5 100644 --- a/services/auth-basic/pkg/config/config.go +++ b/services/auth-basic/pkg/config/config.go @@ -52,9 +52,8 @@ type GRPCConfig struct { } type AuthProviders struct { - LDAP LDAPProvider `yaml:"ldap"` - OwnCloudSQL OwnCloudSQLProvider `yaml:"owncloudsql"` - JSON JSONProvider `yaml:"json,omitempty"` // not supported by the oCIS product, therefore not part of docs + LDAP LDAPProvider `yaml:"ldap"` + JSON JSONProvider `yaml:"json,omitempty"` // not supported by the oCIS product, therefore not part of docs } type JSONProvider struct { @@ -110,15 +109,3 @@ type LDAPGroupSchema struct { Groupname string `yaml:"group_name" env:"OCIS_LDAP_GROUP_SCHEMA_GROUPNAME;AUTH_BASIC_LDAP_GROUP_SCHEMA_GROUPNAME" desc:"LDAP Attribute to use for the name of groups." introductionVersion:"pre5.0"` Member string `yaml:"member" env:"OCIS_LDAP_GROUP_SCHEMA_MEMBER;AUTH_BASIC_LDAP_GROUP_SCHEMA_MEMBER" desc:"LDAP Attribute that is used for group members." introductionVersion:"pre5.0"` } - -type OwnCloudSQLProvider struct { - DBUsername string `yaml:"db_username" env:"AUTH_BASIC_OWNCLOUDSQL_DB_USERNAME" desc:"Database user to use for authenticating with the owncloud database." introductionVersion:"pre5.0"` - DBPassword string `yaml:"db_password" env:"AUTH_BASIC_OWNCLOUDSQL_DB_PASSWORD" desc:"Password for the database user." introductionVersion:"pre5.0"` - DBHost string `yaml:"db_host" env:"AUTH_BASIC_OWNCLOUDSQL_DB_HOST" desc:"Hostname of the database server." introductionVersion:"pre5.0"` - DBPort int `yaml:"db_port" env:"AUTH_BASIC_OWNCLOUDSQL_DB_PORT" desc:"Network port to use for the database connection." introductionVersion:"pre5.0"` - DBName string `yaml:"db_name" env:"AUTH_BASIC_OWNCLOUDSQL_DB_NAME" desc:"Name of the owncloud database." introductionVersion:"pre5.0"` - IDP string `yaml:"idp" env:"AUTH_BASIC_OWNCLOUDSQL_IDP" desc:"The identity provider value to set in the userids of the CS3 user objects for users returned by this user provider." introductionVersion:"pre5.0"` - Nobody int64 `yaml:"nobody" env:"AUTH_BASIC_OWNCLOUDSQL_NOBODY" desc:"Fallback number if no numeric UID and GID properties are provided." introductionVersion:"pre5.0"` - JoinUsername bool `yaml:"join_username" env:"AUTH_BASIC_OWNCLOUDSQL_JOIN_USERNAME" desc:"Join the user properties table to read usernames" introductionVersion:"pre5.0"` - JoinOwnCloudUUID bool `yaml:"join_owncloud_uuid" env:"AUTH_BASIC_OWNCLOUDSQL_JOIN_OWNCLOUD_UUID" desc:"Join the user properties table to read user ID's." introductionVersion:"pre5.0"` -} diff --git a/services/auth-basic/pkg/config/defaults/defaultconfig.go b/services/auth-basic/pkg/config/defaults/defaultconfig.go index d73394ccc25..6efd52c52d7 100644 --- a/services/auth-basic/pkg/config/defaults/defaultconfig.go +++ b/services/auth-basic/pkg/config/defaults/defaultconfig.go @@ -76,16 +76,6 @@ func DefaultConfig() *config.Config { }, }, JSON: config.JSONProvider{}, - OwnCloudSQL: config.OwnCloudSQLProvider{ - DBUsername: "owncloud", - DBHost: "mysql", - DBPort: 3306, - DBName: "owncloud", - IDP: "https://localhost:9200", - Nobody: 90, - JoinUsername: false, - JoinOwnCloudUUID: false, - }, }, } } diff --git a/services/auth-basic/pkg/revaconfig/config.go b/services/auth-basic/pkg/revaconfig/config.go index fd22a03d85a..e9f1a7c4ace 100644 --- a/services/auth-basic/pkg/revaconfig/config.go +++ b/services/auth-basic/pkg/revaconfig/config.go @@ -30,17 +30,6 @@ func AuthBasicConfigFromStruct(cfg *config.Config) map[string]interface{} { "users": cfg.AuthProviders.JSON.File, }, "ldap": ldapConfigFromString(cfg.AuthProviders.LDAP), - "owncloudsql": map[string]interface{}{ - "dbusername": cfg.AuthProviders.OwnCloudSQL.DBUsername, - "dbpassword": cfg.AuthProviders.OwnCloudSQL.DBPassword, - "dbhost": cfg.AuthProviders.OwnCloudSQL.DBHost, - "dbport": cfg.AuthProviders.OwnCloudSQL.DBPort, - "dbname": cfg.AuthProviders.OwnCloudSQL.DBName, - "idp": cfg.AuthProviders.OwnCloudSQL.IDP, - "nobody": cfg.AuthProviders.OwnCloudSQL.Nobody, - "join_username": cfg.AuthProviders.OwnCloudSQL.JoinUsername, - "join_ownclouduuid": cfg.AuthProviders.OwnCloudSQL.JoinOwnCloudUUID, - }, }, }, }, diff --git a/services/groups/pkg/config/config.go b/services/groups/pkg/config/config.go index a2225acfd1f..d07bf6d2eca 100644 --- a/services/groups/pkg/config/config.go +++ b/services/groups/pkg/config/config.go @@ -21,7 +21,7 @@ type Config struct { SkipUserGroupsInToken bool `yaml:"skip_user_groups_in_token" env:"GROUPS_SKIP_USER_GROUPS_IN_TOKEN" desc:"Disables the loading of user's group memberships from the reva access token." introductionVersion:"pre5.0"` - Driver string `yaml:"driver" env:"GROUPS_DRIVER" desc:"The driver which should be used by the groups service. Supported values are 'ldap' and 'owncloudsql'." introductionVersion:"pre5.0"` + Driver string `yaml:"driver" env:"GROUPS_DRIVER" desc:"The driver which should be used by the groups service. Supported value is 'ldap'." introductionVersion:"pre5.0"` Drivers Drivers `yaml:"drivers"` Context context.Context `yaml:"-"` @@ -53,8 +53,7 @@ type GRPCConfig struct { } type Drivers struct { - LDAP LDAPDriver `yaml:"ldap"` - OwnCloudSQL OwnCloudSQLDriver `yaml:"owncloudsql"` + LDAP LDAPDriver `yaml:"ldap"` JSON JSONDriver `yaml:"json,omitempty"` // not supported by the oCIS product, therefore not part of docs REST RESTProvider `yaml:"rest,omitempty"` // not supported by the oCIS product, therefore not part of docs @@ -107,19 +106,6 @@ type LDAPGroupSchema struct { Member string `yaml:"member" env:"OCIS_LDAP_GROUP_SCHEMA_MEMBER;GROUPS_LDAP_GROUP_SCHEMA_MEMBER" desc:"LDAP Attribute that is used for group members." introductionVersion:"pre5.0"` } -type OwnCloudSQLDriver struct { - DBUsername string `yaml:"db_username" env:"GROUPS_OWNCLOUDSQL_DB_USERNAME" desc:"Database user to use for authenticating with the owncloud database." introductionVersion:"pre5.0"` - DBPassword string `yaml:"db_password" env:"GROUPS_OWNCLOUDSQL_DB_PASSWORD" desc:"Password for the database user." introductionVersion:"pre5.0"` - DBHost string `yaml:"db_host" env:"GROUPS_OWNCLOUDSQL_DB_HOST" desc:"Hostname of the database server." introductionVersion:"pre5.0"` - DBPort int `yaml:"db_port" env:"GROUPS_OWNCLOUDSQL_DB_PORT" desc:"Network port to use for the database connection." introductionVersion:"pre5.0"` - DBName string `yaml:"db_name" env:"GROUPS_OWNCLOUDSQL_DB_NAME" desc:"Name of the owncloud database." introductionVersion:"pre5.0"` - IDP string `yaml:"idp" env:"GROUPS_OWNCLOUDSQL_IDP" desc:"The identity provider value to set in the userids of the CS3 user objects for users returned by this user provider." introductionVersion:"pre5.0"` - Nobody int64 `yaml:"nobody" env:"GROUPS_OWNCLOUDSQL_NOBODY" desc:"Fallback number if no numeric UID and GID properties are provided." introductionVersion:"pre5.0"` - JoinUsername bool `yaml:"join_username" env:"GROUPS_OWNCLOUDSQL_JOIN_USERNAME" desc:"Join the user properties table to read usernames." introductionVersion:"pre5.0"` - JoinOwnCloudUUID bool `yaml:"join_owncloud_uuid" env:"GROUPS_OWNCLOUDSQL_JOIN_OWNCLOUD_UUID" desc:"Join the user properties table to read user IDs." introductionVersion:"pre5.0"` - EnableMedialSearch bool `yaml:"enable_medial_search" env:"GROUPS_OWNCLOUDSQL_ENABLE_MEDIAL_SEARCH" desc:"Allow 'medial search' when searching for users instead of just doing a prefix search. This allows finding 'Alice' when searching for 'lic'." introductionVersion:"pre5.0"` -} - type JSONDriver struct { File string } diff --git a/services/groups/pkg/config/defaults/defaultconfig.go b/services/groups/pkg/config/defaults/defaultconfig.go index f40ef3b3345..0f77525900f 100644 --- a/services/groups/pkg/config/defaults/defaultconfig.go +++ b/services/groups/pkg/config/defaults/defaultconfig.go @@ -72,18 +72,6 @@ func DefaultConfig() *config.Config { Member: "member", }, }, - OwnCloudSQL: config.OwnCloudSQLDriver{ - DBUsername: "owncloud", - DBPassword: "", - DBHost: "mysql", - DBPort: 3306, - DBName: "owncloud", - IDP: "https://localhost:9200", - Nobody: 90, - JoinUsername: false, - JoinOwnCloudUUID: false, - EnableMedialSearch: false, - }, }, } } diff --git a/services/sharing/pkg/config/config.go b/services/sharing/pkg/config/config.go index b1d6aecd654..f44ffdf59ad 100644 --- a/services/sharing/pkg/config/config.go +++ b/services/sharing/pkg/config/config.go @@ -21,7 +21,7 @@ type Config struct { SkipUserGroupsInToken bool `yaml:"skip_user_groups_in_token" env:"SHARING_SKIP_USER_GROUPS_IN_TOKEN" desc:"Disables the loading of user's group memberships from the reva access token." introductionVersion:"pre5.0"` - UserSharingDriver string `yaml:"user_sharing_driver" env:"SHARING_USER_DRIVER" desc:"Driver to be used to persist shares. Supported values are 'jsoncs3', 'json', 'cs3' (deprecated) and 'owncloudsql'." introductionVersion:"pre5.0"` + UserSharingDriver string `yaml:"user_sharing_driver" env:"SHARING_USER_DRIVER" desc:"Driver to be used to persist shares. Supported values are 'jsoncs3', 'json' and 'cs3' (deprecated)." introductionVersion:"pre5.0"` UserSharingDrivers UserSharingDrivers `yaml:"user_sharing_drivers"` PublicSharingDriver string `yaml:"public_sharing_driver" env:"SHARING_PUBLIC_DRIVER" desc:"Driver to be used to persist public shares. Supported values are 'jsoncs3', 'json' and 'cs3' (deprecated)." introductionVersion:"pre5.0"` PublicSharingDrivers PublicSharingDrivers `yaml:"public_sharing_drivers"` @@ -62,10 +62,9 @@ type GRPCConfig struct { } type UserSharingDrivers struct { - JSONCS3 UserSharingJSONCS3Driver `yaml:"jsoncs3"` - JSON UserSharingJSONDriver `yaml:"json"` - CS3 UserSharingCS3Driver `yaml:"cs3"` - OwnCloudSQL UserSharingOwnCloudSQLDriver `yaml:"owncloudsql"` + JSONCS3 UserSharingJSONCS3Driver `yaml:"jsoncs3"` + JSON UserSharingJSONDriver `yaml:"json"` + CS3 UserSharingCS3Driver `yaml:"cs3"` SQL UserSharingSQLDriver `yaml:"sql,omitempty"` // not supported by the oCIS product, therefore not part of docs } @@ -86,15 +85,6 @@ type UserSharingSQLDriver struct { UserStorageMountID string `yaml:"user_storage_mount_id"` } -type UserSharingOwnCloudSQLDriver struct { - DBUsername string `yaml:"db_username" env:"SHARING_USER_OWNCLOUDSQL_DB_USERNAME" desc:"Username for the database." introductionVersion:"pre5.0"` - DBPassword string `yaml:"db_password" env:"SHARING_USER_OWNCLOUDSQL_DB_PASSWORD" desc:"Password for the database." introductionVersion:"pre5.0"` - DBHost string `yaml:"db_host" env:"SHARING_USER_OWNCLOUDSQL_DB_HOST" desc:"Hostname or IP of the database server." introductionVersion:"pre5.0"` - DBPort int `yaml:"db_port" env:"SHARING_USER_OWNCLOUDSQL_DB_PORT" desc:"Port that the database server is listening on." introductionVersion:"pre5.0"` - DBName string `yaml:"db_name" env:"SHARING_USER_OWNCLOUDSQL_DB_NAME" desc:"Name of the database to be used." introductionVersion:"pre5.0"` - UserStorageMountID string `yaml:"user_storage_mount_id" env:"SHARING_USER_OWNCLOUDSQL_USER_STORAGE_MOUNT_ID" desc:"Mount ID of the ownCloudSQL users storage for mapping ownCloud 10 shares." introductionVersion:"pre5.0"` -} - type UserSharingCS3Driver struct { ProviderAddr string `yaml:"provider_addr" env:"SHARING_USER_CS3_PROVIDER_ADDR" desc:"GRPC address of the STORAGE-SYSTEM service." introductionVersion:"pre5.0"` SystemUserID string `yaml:"system_user_id" env:"OCIS_SYSTEM_USER_ID;SHARING_USER_CS3_SYSTEM_USER_ID" desc:"ID of the oCIS STORAGE-SYSTEM system user. Admins need to set the ID for the STORAGE-SYSTEM system user in this config option which is then used to reference the user. Any reasonable long string is possible, preferably this would be an UUIDv4 format." introductionVersion:"pre5.0"` diff --git a/services/sharing/pkg/config/defaults/defaultconfig.go b/services/sharing/pkg/config/defaults/defaultconfig.go index 10a76ef89eb..fec9b3a6526 100644 --- a/services/sharing/pkg/config/defaults/defaultconfig.go +++ b/services/sharing/pkg/config/defaults/defaultconfig.go @@ -49,12 +49,6 @@ func DefaultConfig() *config.Config { SystemUserIDP: "internal", MaxConcurrency: 1, }, - OwnCloudSQL: config.UserSharingOwnCloudSQLDriver{ - DBUsername: "owncloud", - DBHost: "mysql", - DBPort: 3306, - DBName: "owncloud", - }, }, PublicSharingDriver: "jsoncs3", PublicSharingDrivers: config.PublicSharingDrivers{ @@ -69,7 +63,6 @@ func DefaultConfig() *config.Config { ProviderAddr: "com.owncloud.api.storage-system", // system storage SystemUserIDP: "internal", }, - // TODO implement and add owncloudsql publicshare driver }, Events: config.Events{ Addr: "127.0.0.1:9233", diff --git a/services/sharing/pkg/revaconfig/config.go b/services/sharing/pkg/revaconfig/config.go index d5021ee21d6..d68a7e44c7d 100644 --- a/services/sharing/pkg/revaconfig/config.go +++ b/services/sharing/pkg/revaconfig/config.go @@ -54,15 +54,6 @@ func SharingConfigFromStruct(cfg *config.Config, logger log.Logger) (map[string] "enable_expired_shares_cleanup": cfg.UserSharingDrivers.SQL.EnableExpiredSharesCleanup, "janitor_run_interval": cfg.UserSharingDrivers.SQL.JanitorRunInterval, }, - "owncloudsql": map[string]interface{}{ - "gateway_addr": cfg.Reva.Address, - "storage_mount_id": cfg.UserSharingDrivers.OwnCloudSQL.UserStorageMountID, - "db_username": cfg.UserSharingDrivers.OwnCloudSQL.DBUsername, - "db_password": cfg.UserSharingDrivers.OwnCloudSQL.DBPassword, - "db_host": cfg.UserSharingDrivers.OwnCloudSQL.DBHost, - "db_port": cfg.UserSharingDrivers.OwnCloudSQL.DBPort, - "db_name": cfg.UserSharingDrivers.OwnCloudSQL.DBName, - }, "cs3": map[string]interface{}{ "gateway_addr": cfg.UserSharingDrivers.CS3.ProviderAddr, "provider_addr": cfg.UserSharingDrivers.CS3.ProviderAddr, diff --git a/services/storage-users/pkg/config/config.go b/services/storage-users/pkg/config/config.go index f16e9ad322d..72391411a7a 100644 --- a/services/storage-users/pkg/config/config.go +++ b/services/storage-users/pkg/config/config.go @@ -24,7 +24,7 @@ type Config struct { SkipUserGroupsInToken bool `yaml:"skip_user_groups_in_token" env:"STORAGE_USERS_SKIP_USER_GROUPS_IN_TOKEN" desc:"Disables the loading of user's group memberships from the reva access token." introductionVersion:"pre5.0"` GracefulShutdownTimeout int `yaml:"graceful_shutdown_timeout" env:"STORAGE_USERS_GRACEFUL_SHUTDOWN_TIMEOUT" desc:"The number of seconds to wait for the 'storage-users' service to shutdown cleanly before exiting with an error that gets logged. Note: This setting is only applicable when running the 'storage-users' service as a standalone service. See the text description for more details." introductionVersion:"pre5.0"` - Driver string `yaml:"driver" env:"STORAGE_USERS_DRIVER" desc:"The storage driver which should be used by the service. Defaults to 'ocis', Supported values are: 'ocis', 's3ng' and 'owncloudsql' (deprecated). The 'ocis' driver stores all data (blob and meta data) in an POSIX compliant volume. The 's3ng' driver stores metadata in a POSIX compliant volume and uploads blobs to the s3 bucket." introductionVersion:"pre5.0"` + Driver string `yaml:"driver" env:"STORAGE_USERS_DRIVER" desc:"The storage driver which should be used by the service. Defaults to 'ocis', Supported values are: 'ocis' and 's3ng'. The 'ocis' driver stores all data (blob and meta data) in an POSIX compliant volume. The 's3ng' driver stores metadata in a POSIX compliant volume and uploads blobs to the s3 bucket." introductionVersion:"pre5.0"` Drivers Drivers `yaml:"drivers"` DataServerURL string `yaml:"data_server_url" env:"STORAGE_USERS_DATA_SERVER_URL" desc:"URL of the data server, needs to be reachable by the data gateway provided by the frontend service or the user if directly exposed." introductionVersion:"pre5.0"` DataGatewayURL string `yaml:"data_gateway_url" env:"STORAGE_USERS_DATA_GATEWAY_URL" desc:"URL of the data gateway server" introductionVersion:"pre5.0"` @@ -100,10 +100,9 @@ type CORS struct { // Drivers combine all storage driver configurations type Drivers struct { - OCIS OCISDriver `yaml:"ocis"` - S3NG S3NGDriver `yaml:"s3ng"` - OwnCloudSQL OwnCloudSQLDriver `yaml:"owncloudsql"` - Posix PosixDriver `yaml:"posix"` + OCIS OCISDriver `yaml:"ocis"` + S3NG S3NGDriver `yaml:"s3ng"` + Posix PosixDriver `yaml:"posix"` S3 S3Driver `yaml:",omitempty"` // not supported by the oCIS product, therefore not part of docs EOS EOSDriver `yaml:",omitempty"` // not supported by the oCIS product, therefore not part of docs @@ -176,22 +175,6 @@ type S3NGDriver struct { DisableVersioning bool `yaml:"disable_versioning" env:"OCIS_DISABLE_VERSIONING" desc:"Disables versioning of files. When set to true, new uploads with the same filename will overwrite existing files instead of creating a new version." introductionVersion:"7.0.0"` } -// OwnCloudSQLDriver is the storage driver configuration when using 'owncloudsql' storage driver -type OwnCloudSQLDriver struct { - // Root is the absolute path to the location of the data - Root string `yaml:"root" env:"STORAGE_USERS_OWNCLOUDSQL_DATADIR" desc:"The directory where the filesystem storage will store SQL migration data. If not defined, the root directory derives from $OCIS_BASE_DATA_PATH/storage/owncloud." introductionVersion:"pre5.0"` - // ShareFolder defines the name of the folder jailing all shares - ShareFolder string `yaml:"share_folder" env:"STORAGE_USERS_OWNCLOUDSQL_SHARE_FOLDER" desc:"Name of the folder jailing all shares." introductionVersion:"pre5.0"` - UserLayout string `yaml:"user_layout" env:"STORAGE_USERS_OWNCLOUDSQL_LAYOUT" desc:"Path layout to use to navigate into a users folder in an owncloud data directory" introductionVersion:"pre5.0"` - UploadInfoDir string `yaml:"upload_info_dir" env:"STORAGE_USERS_OWNCLOUDSQL_UPLOADINFO_DIR" desc:"The directory where the filesystem will store uploads temporarily. If not defined, the root directory derives from $OCIS_BASE_DATA_PATH/storage/uploadinfo." introductionVersion:"pre5.0"` - DBUsername string `yaml:"db_username" env:"STORAGE_USERS_OWNCLOUDSQL_DB_USERNAME" desc:"Username for the database." introductionVersion:"pre5.0"` - DBPassword string `yaml:"db_password" env:"STORAGE_USERS_OWNCLOUDSQL_DB_PASSWORD" desc:"Password for the database." introductionVersion:"pre5.0"` - DBHost string `yaml:"db_host" env:"STORAGE_USERS_OWNCLOUDSQL_DB_HOST" desc:"Hostname or IP of the database server." introductionVersion:"pre5.0"` - DBPort int `yaml:"db_port" env:"STORAGE_USERS_OWNCLOUDSQL_DB_PORT" desc:"Port that the database server is listening on." introductionVersion:"pre5.0"` - DBName string `yaml:"db_name" env:"STORAGE_USERS_OWNCLOUDSQL_DB_NAME" desc:"Name of the database to be used." introductionVersion:"pre5.0"` - UsersProviderEndpoint string `yaml:"users_provider_endpoint" env:"STORAGE_USERS_OWNCLOUDSQL_USERS_PROVIDER_ENDPOINT" desc:"Endpoint of the users provider." introductionVersion:"pre5.0"` -} - // PosixDriver is the storage driver configuration when using 'posix' storage driver type PosixDriver struct { // Root is the absolute path to the location of the data diff --git a/services/storage-users/pkg/config/defaults/defaultconfig.go b/services/storage-users/pkg/config/defaults/defaultconfig.go index f77ef6372f5..78d9136dd16 100644 --- a/services/storage-users/pkg/config/defaults/defaultconfig.go +++ b/services/storage-users/pkg/config/defaults/defaultconfig.go @@ -94,18 +94,6 @@ func DefaultConfig() *config.Config { GracefulShutdownTimeout: 30, Driver: "ocis", Drivers: config.Drivers{ - OwnCloudSQL: config.OwnCloudSQLDriver{ - Root: filepath.Join(defaults.BaseDataPath(), "storage", "owncloud"), - ShareFolder: "/Shares", - UserLayout: "{{.Username}}", - UploadInfoDir: filepath.Join(defaults.BaseDataPath(), "storage", "uploadinfo"), - DBUsername: "owncloud", - DBPassword: "owncloud", - DBHost: "", - DBPort: 3306, - DBName: "owncloud", - UsersProviderEndpoint: "com.owncloud.api.users", - }, S3NG: config.S3NGDriver{ Propagator: "sync", Root: filepath.Join(defaults.BaseDataPath(), "storage", "users"), diff --git a/services/storage-users/pkg/revaconfig/drivers.go b/services/storage-users/pkg/revaconfig/drivers.go index 0c59be695b6..c4a6cb1461f 100644 --- a/services/storage-users/pkg/revaconfig/drivers.go +++ b/services/storage-users/pkg/revaconfig/drivers.go @@ -137,29 +137,6 @@ func LocalHome(cfg *config.Config) map[string]interface{} { } } -// OwnCloudSQL is the config mapping for the OwnCloudSQL storage driver -func OwnCloudSQL(cfg *config.Config) map[string]interface{} { - return map[string]interface{}{ - "datadirectory": cfg.Drivers.OwnCloudSQL.Root, - "upload_info_dir": cfg.Drivers.OwnCloudSQL.UploadInfoDir, - "share_folder": cfg.Drivers.OwnCloudSQL.ShareFolder, - "user_layout": cfg.Drivers.OwnCloudSQL.UserLayout, - "enable_home": false, - "dbusername": cfg.Drivers.OwnCloudSQL.DBUsername, - "dbpassword": cfg.Drivers.OwnCloudSQL.DBPassword, - "dbhost": cfg.Drivers.OwnCloudSQL.DBHost, - "dbport": cfg.Drivers.OwnCloudSQL.DBPort, - "dbname": cfg.Drivers.OwnCloudSQL.DBName, - "userprovidersvc": cfg.Drivers.OwnCloudSQL.UsersProviderEndpoint, - "tokens": map[string]interface{}{ - "download_endpoint": cfg.DataServerURL, - "datagateway_endpoint": cfg.DataGatewayURL, - "transfer_shared_secret": cfg.Commons.TransferSecret, - "transfer_expires": cfg.TransferExpires, - }, - } -} - // Ocis is the config mapping for the Ocis storage driver func Ocis(cfg *config.Config) map[string]interface{} { return map[string]interface{}{ diff --git a/services/storage-users/pkg/revaconfig/user.go b/services/storage-users/pkg/revaconfig/user.go index a40ba3d64e6..faa31cc5ecd 100644 --- a/services/storage-users/pkg/revaconfig/user.go +++ b/services/storage-users/pkg/revaconfig/user.go @@ -5,31 +5,29 @@ import "github.com/owncloud/ocis/v2/services/storage-users/pkg/config" // StorageProviderDrivers are the drivers for the storage provider func StorageProviderDrivers(cfg *config.Config) map[string]interface{} { return map[string]interface{}{ - "eos": EOS(cfg), - "eoshome": EOSHome(cfg), - "eosgrpc": EOSGRPC(cfg), - "local": Local(cfg), - "localhome": LocalHome(cfg), - "owncloudsql": OwnCloudSQL(cfg), - "ocis": OcisNoEvents(cfg), - "s3": S3(cfg), - "s3ng": S3NGNoEvents(cfg), - "posix": Posix(cfg, true), + "eos": EOS(cfg), + "eoshome": EOSHome(cfg), + "eosgrpc": EOSGRPC(cfg), + "local": Local(cfg), + "localhome": LocalHome(cfg), + "ocis": OcisNoEvents(cfg), + "s3": S3(cfg), + "s3ng": S3NGNoEvents(cfg), + "posix": Posix(cfg, true), } } // DataProviderDrivers are the drivers for the storage provider func DataProviderDrivers(cfg *config.Config) map[string]interface{} { return map[string]interface{}{ - "eos": EOS(cfg), - "eoshome": EOSHome(cfg), - "eosgrpc": EOSGRPC(cfg), - "local": Local(cfg), - "localhome": LocalHome(cfg), - "owncloudsql": OwnCloudSQL(cfg), - "ocis": Ocis(cfg), - "s3": S3(cfg), - "s3ng": S3NG(cfg), - "posix": Posix(cfg, false), + "eos": EOS(cfg), + "eoshome": EOSHome(cfg), + "eosgrpc": EOSGRPC(cfg), + "local": Local(cfg), + "localhome": LocalHome(cfg), + "ocis": Ocis(cfg), + "s3": S3(cfg), + "s3ng": S3NG(cfg), + "posix": Posix(cfg, false), } } diff --git a/services/users/pkg/config/config.go b/services/users/pkg/config/config.go index 923d0f45caa..4876ec23656 100644 --- a/services/users/pkg/config/config.go +++ b/services/users/pkg/config/config.go @@ -21,7 +21,7 @@ type Config struct { SkipUserGroupsInToken bool `yaml:"skip_user_groups_in_token" env:"USERS_SKIP_USER_GROUPS_IN_TOKEN" desc:"Disables the loading of user's group memberships from the reva access token." introductionVersion:"pre5.0"` - Driver string `yaml:"driver" env:"USERS_DRIVER" desc:"The driver which should be used by the users service. Supported values are 'ldap' and 'owncloudsql'." introductionVersion:"pre5.0"` + Driver string `yaml:"driver" env:"USERS_DRIVER" desc:"The driver which should be used by the users service. Supported value is 'ldap'." introductionVersion:"pre5.0"` Drivers Drivers `yaml:"drivers"` Context context.Context `yaml:"-"` @@ -52,8 +52,7 @@ type GRPCConfig struct { } type Drivers struct { - LDAP LDAPDriver `yaml:"ldap"` - OwnCloudSQL OwnCloudSQLDriver `yaml:"owncloudsql"` + LDAP LDAPDriver `yaml:"ldap"` JSON JSONDriver `yaml:"json,omitempty"` // not supported by the oCIS product, therefore not part of docs REST RESTProvider `yaml:"rest,omitempty"` // not supported by the oCIS product, therefore not part of docs @@ -113,18 +112,6 @@ type LDAPGroupSchema struct { Member string `yaml:"member" env:"OCIS_LDAP_GROUP_SCHEMA_MEMBER;USERS_LDAP_GROUP_SCHEMA_MEMBER" desc:"LDAP Attribute that is used for group members." introductionVersion:"pre5.0"` } -type OwnCloudSQLDriver struct { - DBUsername string `yaml:"db_username" env:"USERS_OWNCLOUDSQL_DB_USERNAME" desc:"Database user to use for authenticating with the owncloud database." introductionVersion:"pre5.0"` - DBPassword string `yaml:"db_password" env:"USERS_OWNCLOUDSQL_DB_PASSWORD" desc:"Password for the database user." introductionVersion:"pre5.0"` - DBHost string `yaml:"db_host" env:"USERS_OWNCLOUDSQL_DB_HOST" desc:"Hostname of the database server." introductionVersion:"pre5.0"` - DBPort int `yaml:"db_port" env:"USERS_OWNCLOUDSQL_DB_PORT" desc:"Network port to use for the database connection." introductionVersion:"pre5.0"` - DBName string `yaml:"db_name" env:"USERS_OWNCLOUDSQL_DB_NAME" desc:"Name of the owncloud database." introductionVersion:"pre5.0"` - IDP string `yaml:"idp" env:"USERS_OWNCLOUDSQL_IDP" desc:"The identity provider value to set in the userids of the CS3 user objects for users returned by this user provider." introductionVersion:"pre5.0"` - Nobody int64 `yaml:"nobody" env:"USERS_OWNCLOUDSQL_NOBODY" desc:"Fallback number if no numeric UID and GID properties are provided." introductionVersion:"pre5.0"` - JoinUsername bool `yaml:"join_username" env:"USERS_OWNCLOUDSQL_JOIN_USERNAME" desc:"Join the user properties table to read usernames" introductionVersion:"pre5.0"` - JoinOwnCloudUUID bool `yaml:"join_owncloud_uuid" env:"USERS_OWNCLOUDSQL_JOIN_OWNCLOUD_UUID" desc:"Join the user properties table to read user IDs." introductionVersion:"pre5.0"` - EnableMedialSearch bool `yaml:"enable_medial_search" env:"USERS_OWNCLOUDSQL_ENABLE_MEDIAL_SEARCH" desc:"Allow 'medial search' when searching for users instead of just doing a prefix search. This allows finding 'Alice' when searching for 'lic'." introductionVersion:"pre5.0"` -} type RESTProvider struct { ClientID string ClientSecret string diff --git a/services/users/pkg/config/defaults/defaultconfig.go b/services/users/pkg/config/defaults/defaultconfig.go index 28933e2ad3a..6313f88efd3 100644 --- a/services/users/pkg/config/defaults/defaultconfig.go +++ b/services/users/pkg/config/defaults/defaultconfig.go @@ -77,18 +77,6 @@ func DefaultConfig() *config.Config { }, }, JSON: config.JSONDriver{}, - OwnCloudSQL: config.OwnCloudSQLDriver{ - DBUsername: "owncloud", - DBPassword: "secret", - DBHost: "mysql", - DBPort: 3306, - DBName: "owncloud", - IDP: "https://localhost:9200", - Nobody: 90, - JoinUsername: false, - JoinOwnCloudUUID: false, - EnableMedialSearch: false, - }, }, } } diff --git a/services/users/pkg/revaconfig/config.go b/services/users/pkg/revaconfig/config.go index 93398571779..7dc9d142d3f 100644 --- a/services/users/pkg/revaconfig/config.go +++ b/services/users/pkg/revaconfig/config.go @@ -30,18 +30,6 @@ func UsersConfigFromStruct(cfg *config.Config) map[string]interface{} { "users": cfg.Drivers.JSON.File, }, "ldap": ldapConfigFromString(cfg.Drivers.LDAP), - "owncloudsql": map[string]interface{}{ - "dbusername": cfg.Drivers.OwnCloudSQL.DBUsername, - "dbpassword": cfg.Drivers.OwnCloudSQL.DBPassword, - "dbhost": cfg.Drivers.OwnCloudSQL.DBHost, - "dbport": cfg.Drivers.OwnCloudSQL.DBPort, - "dbname": cfg.Drivers.OwnCloudSQL.DBName, - "idp": cfg.Drivers.OwnCloudSQL.IDP, - "nobody": cfg.Drivers.OwnCloudSQL.Nobody, - "join_username": cfg.Drivers.OwnCloudSQL.JoinUsername, - "join_ownclouduuid": cfg.Drivers.OwnCloudSQL.JoinOwnCloudUUID, - "enable_medial_search": cfg.Drivers.OwnCloudSQL.EnableMedialSearch, - }, }, }, }, diff --git a/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/loader/loader.go b/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/loader/loader.go index 843c9bda9ba..9c5714ace3b 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/loader/loader.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/loader/loader.go @@ -29,7 +29,6 @@ import ( _ "github.com/owncloud/reva/v2/pkg/auth/manager/nextcloud" _ "github.com/owncloud/reva/v2/pkg/auth/manager/ocmshares" _ "github.com/owncloud/reva/v2/pkg/auth/manager/oidc" - _ "github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql" _ "github.com/owncloud/reva/v2/pkg/auth/manager/publicshares" _ "github.com/owncloud/reva/v2/pkg/auth/manager/serviceaccounts" // Add your own here diff --git a/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/accounts/accounts.go b/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/accounts/accounts.go deleted file mode 100644 index 11eb70cefd2..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/accounts/accounts.go +++ /dev/null @@ -1,165 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package accounts - -import ( - "context" - "database/sql" - "strings" - "time" - - "github.com/owncloud/reva/v2/pkg/appctx" - "github.com/pkg/errors" -) - -// Accounts represents oc10-style Accounts -type Accounts struct { - driver string - db *sql.DB - joinUsername, joinUUID, enableMedialSearch bool - selectSQL string -} - -// NewMysql returns a new accounts instance connecting to a MySQL database -func NewMysql(dsn string, joinUsername, joinUUID, enableMedialSearch bool) (*Accounts, error) { - sqldb, err := sql.Open("mysql", dsn) - if err != nil { - return nil, errors.Wrap(err, "error connecting to the database") - } - - // FIXME make configurable - sqldb.SetConnMaxLifetime(time.Minute * 3) - sqldb.SetConnMaxIdleTime(time.Second * 30) - sqldb.SetMaxOpenConns(100) - sqldb.SetMaxIdleConns(10) - - err = sqldb.Ping() - if err != nil { - return nil, errors.Wrap(err, "error connecting to the database") - } - - return New("mysql", sqldb, joinUsername, joinUUID, enableMedialSearch) -} - -// New returns a new accounts instance connecting to the given sql.DB -func New(driver string, sqldb *sql.DB, joinUsername, joinUUID, enableMedialSearch bool) (*Accounts, error) { - - sel := "SELECT id, email, user_id, display_name, quota, last_login, backend, home, state, password" - from := ` - FROM oc_accounts a - LEFT JOIN oc_users u - ON a.user_id=u.uid - ` - if joinUsername { - sel += ", p.configvalue AS username" - from += `LEFT JOIN oc_preferences p - ON a.user_id=p.userid - AND p.appid='core' - AND p.configkey='username'` - } else { - // fallback to user_id as username - sel += ", user_id AS username" - } - if joinUUID { - sel += ", p2.configvalue AS ownclouduuid" - from += `LEFT JOIN oc_preferences p2 - ON a.user_id=p2.userid - AND p2.appid='core' - AND p2.configkey='ownclouduuid'` - } else { - // fallback to user_id as ownclouduuid - sel += ", user_id AS ownclouduuid" - } - - return &Accounts{ - driver: driver, - db: sqldb, - joinUsername: joinUsername, - joinUUID: joinUUID, - enableMedialSearch: enableMedialSearch, - selectSQL: sel + from, - }, nil -} - -// Account stores information about accounts. -type Account struct { - ID uint64 - Email sql.NullString - UserID string - DisplayName sql.NullString - Quota sql.NullString - LastLogin int - Backend string - Home string - State int8 - PasswordHash string // from oc_users - Username sql.NullString // optional comes from the oc_preferences - OwnCloudUUID sql.NullString // optional comes from the oc_preferences -} - -func (as *Accounts) rowToAccount(ctx context.Context, row Scannable) (*Account, error) { - a := Account{} - if err := row.Scan(&a.ID, &a.Email, &a.UserID, &a.DisplayName, &a.Quota, &a.LastLogin, &a.Backend, &a.Home, &a.State, &a.PasswordHash, &a.Username, &a.OwnCloudUUID); err != nil { - appctx.GetLogger(ctx).Error().Err(err).Msg("could not scan row, skipping") - return nil, err - } - - return &a, nil -} - -// Scannable describes the interface providing a Scan method -type Scannable interface { - Scan(...interface{}) error -} - -// GetAccountByLogin fetches an account by mail or username -func (as *Accounts) GetAccountByLogin(ctx context.Context, login string) (*Account, error) { - var row *sql.Row - username := strings.ToLower(login) // usernames are lowercased in owncloud classic - if as.joinUsername { - row = as.db.QueryRowContext(ctx, as.selectSQL+" WHERE a.email=? OR a.lower_user_id=? OR p.configvalue=?", login, username, login) - } else { - row = as.db.QueryRowContext(ctx, as.selectSQL+" WHERE a.email=? OR a.lower_user_id=?", login, username) - } - - return as.rowToAccount(ctx, row) -} - -// GetAccountGroups reads the groups for an account -func (as *Accounts) GetAccountGroups(ctx context.Context, uid string) ([]string, error) { - rows, err := as.db.QueryContext(ctx, "SELECT gid FROM oc_group_user WHERE uid=?", uid) - if err != nil { - return nil, err - } - defer rows.Close() - - var group string - groups := []string{} - for rows.Next() { - if err := rows.Scan(&group); err != nil { - appctx.GetLogger(ctx).Error().Err(err).Msg("could not scan row, skipping") - continue - } - groups = append(groups, group) - } - if err = rows.Err(); err != nil { - return nil, err - } - return groups, nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/accounts/test.sqlite b/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/accounts/test.sqlite deleted file mode 100644 index c68bb753774..00000000000 Binary files a/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/accounts/test.sqlite and /dev/null differ diff --git a/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/owncloudsql.go b/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/owncloudsql.go deleted file mode 100644 index 2d7010667c9..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/owncloudsql.go +++ /dev/null @@ -1,192 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package owncloudsql - -import ( - "context" - "crypto/hmac" - "crypto/sha1" - "encoding/hex" - "fmt" - "strings" - - "golang.org/x/crypto/bcrypt" - - authpb "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1" - user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - "github.com/owncloud/reva/v2/pkg/appctx" - "github.com/owncloud/reva/v2/pkg/auth" - "github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/accounts" - "github.com/owncloud/reva/v2/pkg/auth/manager/registry" - "github.com/owncloud/reva/v2/pkg/auth/scope" - "github.com/owncloud/reva/v2/pkg/errtypes" - "github.com/mitchellh/mapstructure" - "github.com/pkg/errors" - - // Provides mysql drivers - _ "github.com/go-sql-driver/mysql" -) - -func init() { - registry.Register("owncloudsql", NewMysql) -} - -type manager struct { - c *config - db *accounts.Accounts -} - -type config struct { - DbUsername string `mapstructure:"dbusername"` - DbPassword string `mapstructure:"dbpassword"` - DbHost string `mapstructure:"dbhost"` - DbPort int `mapstructure:"dbport"` - DbName string `mapstructure:"dbname"` - Idp string `mapstructure:"idp"` - Nobody int64 `mapstructure:"nobody"` - LegacySalt string `mapstructure:"legacy_salt"` - JoinUsername bool `mapstructure:"join_username"` - JoinOwnCloudUUID bool `mapstructure:"join_ownclouduuid"` -} - -// NewMysql returns a new auth manager connection to an owncloud mysql database -func NewMysql(m map[string]interface{}) (auth.Manager, error) { - mgr := &manager{} - err := mgr.Configure(m) - if err != nil { - err = errors.Wrap(err, "error creating a new auth manager") - return nil, err - } - - mgr.db, err = accounts.NewMysql( - fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", mgr.c.DbUsername, mgr.c.DbPassword, mgr.c.DbHost, mgr.c.DbPort, mgr.c.DbName), - mgr.c.JoinUsername, - mgr.c.JoinOwnCloudUUID, - false, - ) - if err != nil { - return nil, err - } - - return mgr, nil -} - -func (m *manager) Configure(ml map[string]interface{}) error { - c, err := parseConfig(ml) - if err != nil { - return err - } - - if c.Nobody == 0 { - c.Nobody = 99 - } - - m.c = c - return nil -} - -func parseConfig(m map[string]interface{}) (*config, error) { - c := &config{} - if err := mapstructure.Decode(m, &c); err != nil { - return nil, err - } - return c, nil -} - -func (m *manager) Authenticate(ctx context.Context, login, clientSecret string) (*user.User, map[string]*authpb.Scope, error) { - log := appctx.GetLogger(ctx) - - // 1. find user by login - - account, err := m.db.GetAccountByLogin(ctx, login) - if err != nil { - return nil, nil, errtypes.NotFound(login) - } - // 2. verify the user password - if !m.verify(clientSecret, account.PasswordHash) { - return nil, nil, errtypes.InvalidCredentials(login) - } - - userID := &user.UserId{ - Idp: m.c.Idp, - OpaqueId: account.OwnCloudUUID.String, - Type: user.UserType_USER_TYPE_PRIMARY, // TODO: assign the appropriate user type for guest accounts - } - - u := &user.User{ - Id: userID, - // TODO add more claims from the StandardClaims, eg EmailVerified and lastlogin - Username: account.Username.String, - Mail: account.Email.String, - DisplayName: account.DisplayName.String, - //UidNumber: uidNumber, - //GidNumber: gidNumber, - } - - if u.Groups, err = m.db.GetAccountGroups(ctx, account.UserID); err != nil { - return nil, nil, err - } - - var scopes map[string]*authpb.Scope - if userID != nil && (userID.Type == user.UserType_USER_TYPE_LIGHTWEIGHT || userID.Type == user.UserType_USER_TYPE_FEDERATED) { - scopes, err = scope.AddLightweightAccountScope(authpb.Role_ROLE_OWNER, nil) - if err != nil { - return nil, nil, err - } - } else { - scopes, err = scope.AddOwnerScope(nil) - if err != nil { - return nil, nil, err - } - } - // do not log password hash - account.PasswordHash = "***redacted***" - log.Debug().Interface("account", account).Interface("user", u).Msg("authenticated user") - - return u, scopes, nil -} - -func (m *manager) verify(password, hash string) bool { - splitHash := strings.SplitN(hash, "|", 2) - switch len(splitHash) { - case 2: - if splitHash[0] == "1" { - return m.verifyHashV1(password, splitHash[1]) - } - case 1: - return m.legacyHashVerify(password, hash) - } - return false -} - -func (m *manager) legacyHashVerify(password, hash string) bool { - // TODO rehash $newHash = $this->hash($message); - switch len(hash) { - case 60: // legacy PHPass hash - return nil == bcrypt.CompareHashAndPassword([]byte(hash), []byte(password+m.c.LegacySalt)) - case 40: // legacy sha1 hash - h := sha1.Sum([]byte(password)) - return hmac.Equal([]byte(hash), []byte(hex.EncodeToString(h[:]))) - } - return false -} -func (m *manager) verifyHashV1(password, hash string) bool { - // TODO implement password_needs_rehash - return nil == bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/loader/loader.go b/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/loader/loader.go index cc92c0e9670..d39d39b636e 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/loader/loader.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/loader/loader.go @@ -23,6 +23,5 @@ import ( _ "github.com/owncloud/reva/v2/pkg/publicshare/manager/cs3" _ "github.com/owncloud/reva/v2/pkg/publicshare/manager/json" _ "github.com/owncloud/reva/v2/pkg/publicshare/manager/memory" - _ "github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql" // Add your own here ) diff --git a/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql/conversions.go b/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql/conversions.go deleted file mode 100644 index 84abf766003..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql/conversions.go +++ /dev/null @@ -1,226 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package owncloudsql - -import ( - "context" - "time" - - userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" - link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1" - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" - "github.com/owncloud/reva/v2/pkg/conversions" - "github.com/owncloud/reva/v2/pkg/rgrpc/status" - "github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool" - "github.com/jellydator/ttlcache/v2" -) - -// DBShare stores information about user and public shares. -type DBShare struct { - ID string - UIDOwner string - UIDInitiator string - ItemStorage string - FileSource string - ItemType string // 'file' or 'folder' - ShareWith string - Token string - Expiration string - Permissions int - ShareType int - ShareName string - STime int - FileTarget string - RejectedBy string - State int - Parent int -} - -// UserConverter describes an interface for converting user ids to names and back -type UserConverter interface { - UserNameToUserID(ctx context.Context, username string) (*userpb.UserId, error) - UserIDToUserName(ctx context.Context, userid *userpb.UserId) (string, error) -} - -// GatewayUserConverter converts usernames and ids using the gateway -type GatewayUserConverter struct { - gwAddr string - - IDCache *ttlcache.Cache - NameCache *ttlcache.Cache -} - -// NewGatewayUserConverter returns a instance of GatewayUserConverter -func NewGatewayUserConverter(gwAddr string) *GatewayUserConverter { - IDCache := ttlcache.NewCache() - _ = IDCache.SetTTL(30 * time.Second) - IDCache.SkipTTLExtensionOnHit(true) - NameCache := ttlcache.NewCache() - _ = NameCache.SetTTL(30 * time.Second) - NameCache.SkipTTLExtensionOnHit(true) - - return &GatewayUserConverter{ - gwAddr: gwAddr, - IDCache: IDCache, - NameCache: NameCache, - } -} - -// UserIDToUserName converts a user ID to an username -func (c *GatewayUserConverter) UserIDToUserName(ctx context.Context, userid *userpb.UserId) (string, error) { - username, err := c.NameCache.Get(userid.String()) - if err == nil { - return username.(string), nil - } - - gwConn, err := pool.GetGatewayServiceClient(c.gwAddr) - if err != nil { - return "", err - } - getUserResponse, err := gwConn.GetUser(ctx, &userpb.GetUserRequest{ - UserId: userid, - SkipFetchingUserGroups: true, - }) - if err != nil { - return "", err - } - if getUserResponse.Status.Code != rpc.Code_CODE_OK { - return "", status.NewErrorFromCode(getUserResponse.Status.Code, "gateway") - } - _ = c.NameCache.Set(userid.String(), getUserResponse.User.Username) - return getUserResponse.User.Username, nil -} - -// UserNameToUserID converts a username to an user ID -func (c *GatewayUserConverter) UserNameToUserID(ctx context.Context, username string) (*userpb.UserId, error) { - id, err := c.IDCache.Get(username) - if err == nil { - return id.(*userpb.UserId), nil - } - - gwConn, err := pool.GetGatewayServiceClient(c.gwAddr) - if err != nil { - return nil, err - } - getUserResponse, err := gwConn.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{ - Claim: "username", - Value: username, - SkipFetchingUserGroups: true, - }) - if err != nil { - return nil, err - } - if getUserResponse.Status.Code != rpc.Code_CODE_OK { - return nil, status.NewErrorFromCode(getUserResponse.Status.Code, "gateway") - } - _ = c.IDCache.Set(username, getUserResponse.User.Id) - return getUserResponse.User.Id, nil -} - -func resourceTypeToItem(r provider.ResourceType) string { - switch r { - case provider.ResourceType_RESOURCE_TYPE_FILE: - return "file" - case provider.ResourceType_RESOURCE_TYPE_CONTAINER: - return "folder" - case provider.ResourceType_RESOURCE_TYPE_REFERENCE: - return "reference" - case provider.ResourceType_RESOURCE_TYPE_SYMLINK: - return "symlink" - default: - return "" - } -} - -func sharePermToInt(p *provider.ResourcePermissions) int { - return int(conversions.RoleFromResourcePermissions(p, true).OCSPermissions()) -} - -func intTosharePerm(p int) (*provider.ResourcePermissions, error) { - perms, err := conversions.NewPermissions(p) - if err != nil { - return nil, err - } - - return conversions.RoleFromOCSPermissions(perms, nil).CS3ResourcePermissions(), nil -} - -func formatUserID(u *userpb.UserId) string { - return u.OpaqueId -} - -// ConvertToCS3PublicShare converts a DBShare to a CS3API public share -func (m *mgr) ConvertToCS3PublicShare(ctx context.Context, s DBShare) (*link.PublicShare, error) { - ts := &typespb.Timestamp{ - Seconds: uint64(s.STime), - } - permissions, err := intTosharePerm(s.Permissions) - if err != nil { - return nil, err - } - owner, err := m.userConverter.UserNameToUserID(ctx, s.UIDOwner) - if err != nil { - return nil, err - } - var creator *userpb.UserId - if s.UIDOwner == s.UIDInitiator { - creator = owner - } else { - creator, err = m.userConverter.UserNameToUserID(ctx, s.UIDOwner) - if err != nil { - return nil, err - } - } - pwd := false - if s.ShareWith != "" { - pwd = true - } - var expires *typespb.Timestamp - if s.Expiration != "" { - t, err := time.Parse("2006-01-02 15:04:05", s.Expiration) - if err != nil { - t, err = time.Parse("2006-01-02 15:04:05-07:00", s.Expiration) - } - if err == nil { - expires = &typespb.Timestamp{ - Seconds: uint64(t.Unix()), - } - } - } - return &link.PublicShare{ - Id: &link.PublicShareId{ - OpaqueId: s.ID, - }, - ResourceId: &provider.ResourceId{ - SpaceId: s.ItemStorage, - OpaqueId: s.FileSource, - }, - Permissions: &link.PublicSharePermissions{Permissions: permissions}, - Owner: owner, - Creator: creator, - Token: s.Token, - DisplayName: s.ShareName, - PasswordProtected: pwd, - Expiration: expires, - Ctime: ts, - Mtime: ts, - }, nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql/owncloudsql.go b/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql/owncloudsql.go deleted file mode 100644 index ad9de7aa6fd..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql/owncloudsql.go +++ /dev/null @@ -1,533 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -// Package owncloudsql implements a publiclink share manager backed by an existing ownCloud 10 database -// -// The SQL queries use `coalesce({column_identifier}, ”) as {column_identifier}` to read an emptystring -// instead of null values, which better fits the golang default values. -package owncloudsql - -import ( - "context" - "database/sql" - "fmt" - "strconv" - "strings" - "time" - - user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1" - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" - "github.com/owncloud/reva/v2/pkg/errtypes" - "github.com/owncloud/reva/v2/pkg/publicshare" - "github.com/owncloud/reva/v2/pkg/publicshare/manager/registry" - "github.com/owncloud/reva/v2/pkg/sharedconf" - "github.com/owncloud/reva/v2/pkg/utils" - "github.com/mitchellh/mapstructure" - "github.com/pkg/errors" - "golang.org/x/crypto/bcrypt" - "google.golang.org/protobuf/proto" - - // Provides mysql drivers - _ "github.com/go-sql-driver/mysql" -) - -const ( - publicShareType = 3 -) - -func init() { - registry.Register("owncloudsql", NewMysql) -} - -// Config configures an owncloudsql publicshare manager -type Config struct { - GatewayAddr string `mapstructure:"gateway_addr"` - DbUsername string `mapstructure:"db_username"` - DbPassword string `mapstructure:"db_password"` - DbHost string `mapstructure:"db_host"` - DbPort int `mapstructure:"db_port"` - DbName string `mapstructure:"db_name"` - EnableExpiredSharesCleanup bool `mapstructure:"enable_expired_shares_cleanup"` - SharePasswordHashCost int `mapstructure:"password_hash_cost"` -} - -type mgr struct { - driver string - db *sql.DB - c Config - userConverter UserConverter -} - -// NewMysql returns a new publicshare manager connection to a mysql database -func NewMysql(m map[string]interface{}) (publicshare.Manager, error) { - c, err := parseConfig(m) - if err != nil { - err = errors.Wrap(err, "error creating a new manager") - return nil, err - } - - db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.DbUsername, c.DbPassword, c.DbHost, c.DbPort, c.DbName)) - if err != nil { - return nil, err - } - - userConverter := NewGatewayUserConverter(sharedconf.GetGatewaySVC(c.GatewayAddr)) - - return New("mysql", db, *c, userConverter) -} - -// New returns a new Cache instance connecting to the given sql.DB -func New(driver string, db *sql.DB, c Config, userConverter UserConverter) (publicshare.Manager, error) { - if c.SharePasswordHashCost == 0 { - c.SharePasswordHashCost = bcrypt.DefaultCost - } - return &mgr{ - driver: driver, - db: db, - c: c, - userConverter: userConverter, - }, nil -} - -func parseConfig(m map[string]interface{}) (*Config, error) { - c := &Config{} - if err := mapstructure.Decode(m, c); err != nil { - return nil, err - } - return c, nil -} - -func (m *mgr) CreatePublicShare(ctx context.Context, u *user.User, rInfo *provider.ResourceInfo, g *link.Grant) (*link.PublicShare, error) { - - tkn := utils.RandString(15) - now := time.Now().Unix() - - displayName := tkn - if rInfo.ArbitraryMetadata != nil && rInfo.ArbitraryMetadata.Metadata["name"] != "" { - displayName = rInfo.ArbitraryMetadata.Metadata["name"] - } - createdAt := &typespb.Timestamp{ - Seconds: uint64(now), - } - - creator := u.Username - owner, err := m.userConverter.UserIDToUserName(ctx, rInfo.Owner) - if err != nil { - return nil, err - } - permissions := sharePermToInt(g.Permissions.Permissions) - itemType := resourceTypeToItem(rInfo.Type) - - itemSource := rInfo.Id.OpaqueId - fileSource, err := strconv.ParseUint(itemSource, 10, 64) - if err != nil { - // it can be the case that the item source may be a character string - // we leave fileSource blank in that case - fileSource = 0 - } - - columns := "share_type,uid_owner,uid_initiator,item_type,item_source,file_source,permissions,stime,token,share_name" - placeholders := "?,?,?,?,?,?,?,?,?,?" - params := []interface{}{publicShareType, owner, creator, itemType, itemSource, fileSource, permissions, now, tkn, displayName} - - var passwordProtected bool - password := g.Password - if password != "" { - password, err = hashPassword(password, m.c.SharePasswordHashCost) - if err != nil { - return nil, errors.Wrap(err, "could not hash share password") - } - passwordProtected = true - - columns += ",share_with" - placeholders += ",?" - params = append(params, password) - } - - if g.Expiration != nil && g.Expiration.Seconds != 0 { - t := time.Unix(int64(g.Expiration.Seconds), 0) - columns += ",expiration" - placeholders += ",?" - params = append(params, t) - } - - query := "INSERT INTO oc_share (" + columns + ") VALUES (" + placeholders + ")" - stmt, err := m.db.Prepare(query) - if err != nil { - return nil, err - } - result, err := stmt.Exec(params...) - if err != nil { - return nil, err - } - lastID, err := result.LastInsertId() - if err != nil { - return nil, err - } - - return &link.PublicShare{ - Id: &link.PublicShareId{ - OpaqueId: strconv.FormatInt(lastID, 10), - }, - Owner: rInfo.GetOwner(), - Creator: u.Id, - ResourceId: rInfo.Id, - Token: tkn, - Permissions: g.Permissions, - Ctime: createdAt, - Mtime: createdAt, - PasswordProtected: passwordProtected, - Expiration: g.Expiration, - DisplayName: displayName, - }, nil -} - -// owncloud 10 prefixes the hash with `1|` -func hashPassword(password string, cost int) (string, error) { - bytes, err := bcrypt.GenerateFromPassword([]byte(password), cost) - return "1|" + string(bytes), err -} - -// UpdatePublicShare updates the expiration date, permissions and Mtime -func (m *mgr) UpdatePublicShare(ctx context.Context, u *user.User, req *link.UpdatePublicShareRequest) (*link.PublicShare, error) { - query := "update oc_share set " - paramsMap := map[string]interface{}{} - params := []interface{}{} - - now := time.Now().Unix() - uid := u.Username - - switch req.GetUpdate().GetType() { - case link.UpdatePublicShareRequest_Update_TYPE_DISPLAYNAME: - paramsMap["share_name"] = req.Update.GetDisplayName() - case link.UpdatePublicShareRequest_Update_TYPE_PERMISSIONS: - paramsMap["permissions"] = sharePermToInt(req.Update.GetGrant().GetPermissions().Permissions) - case link.UpdatePublicShareRequest_Update_TYPE_EXPIRATION: - paramsMap["expiration"] = time.Unix(int64(req.Update.GetGrant().Expiration.Seconds), 0) - case link.UpdatePublicShareRequest_Update_TYPE_PASSWORD: - if req.Update.GetGrant().Password == "" { - paramsMap["share_with"] = "" - } else { - h, err := hashPassword(req.Update.GetGrant().Password, m.c.SharePasswordHashCost) - if err != nil { - return nil, errors.Wrap(err, "could not hash share password") - } - paramsMap["share_with"] = h - } - default: - return nil, fmt.Errorf("invalid update type: %v", req.GetUpdate().GetType()) - } - - for k, v := range paramsMap { - query += k + "=?" - params = append(params, v) - } - - switch { - case req.Ref.GetId() != nil: - query += ",stime=? where id=? AND (uid_owner=? or uid_initiator=?)" - params = append(params, now, req.Ref.GetId().OpaqueId, uid, uid) - case req.Ref.GetToken() != "": - query += ",stime=? where token=? AND (uid_owner=? or uid_initiator=?)" - params = append(params, now, req.Ref.GetToken(), uid, uid) - default: - return nil, errtypes.NotFound(req.Ref.String()) - } - - stmt, err := m.db.Prepare(query) - if err != nil { - return nil, err - } - if _, err = stmt.Exec(params...); err != nil { - return nil, err - } - - return m.GetPublicShare(ctx, u, req.Ref, false) -} - -func (m *mgr) GetPublicShare(ctx context.Context, u *user.User, ref *link.PublicShareReference, sign bool) (share *link.PublicShare, err error) { - - ps, err := m.getWithPassword(ctx, ref) - if err != nil { - return nil, err - } - - if publicshare.IsExpired(&ps.PublicShare) { - if err := m.cleanupExpiredShares(); err != nil { - return nil, err - } - return nil, errtypes.NotFound("public share has expired") - } - - if ps.PublicShare.PasswordProtected && sign { - err = publicshare.AddSignature(&ps.PublicShare, ps.Password) - if err != nil { - return nil, err - } - } - - return &ps.PublicShare, nil -} - -func (m *mgr) getWithPassword(ctx context.Context, ref *link.PublicShareReference) (*publicshare.WithPassword, error) { - switch { - case ref.GetToken() != "": - return m.getByToken(ctx, ref.GetToken()) - case ref.GetId().GetOpaqueId() != "": - return m.getByID(ctx, ref.GetId().GetOpaqueId()) - default: - return nil, errtypes.BadRequest("neither id nor token given") - } -} - -func (m *mgr) getByToken(ctx context.Context, token string) (*publicshare.WithPassword, error) { - s, err := getByToken(m.db, token) - if err != nil { - return nil, err - } - ps, err := m.ConvertToCS3PublicShare(ctx, s) - if err != nil { - return nil, err - } - ret := &publicshare.WithPassword{ - Password: strings.TrimPrefix(s.ShareWith, "1|"), - } - proto.Merge(&ret.PublicShare, ps) - return ret, nil -} - -func getByToken(db *sql.DB, token string) (DBShare, error) { - s := DBShare{Token: token} - query := `SELECT - coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, - coalesce(share_with, '') as share_with, coalesce(file_source, '') as file_source, - coalesce(item_type, '') as item_type, - coalesce(expiration, '') as expiration, coalesce(share_name, '') as share_name, - s.id, s.stime, s.permissions, fc.storage as storage - FROM oc_share s - LEFT JOIN oc_filecache fc ON fc.fileid = file_source - WHERE share_type=? AND token=?` - if err := db.QueryRow(query, publicShareType, token).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.ItemType, &s.Expiration, &s.ShareName, &s.ID, &s.STime, &s.Permissions, &s.ItemStorage); err != nil { - if err == sql.ErrNoRows { - return s, errtypes.NotFound(token) - } - return s, err - } - return s, nil -} - -func (m *mgr) getByID(ctx context.Context, id string) (*publicshare.WithPassword, error) { - s := DBShare{ID: id} - query := `SELECT - coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, - coalesce(share_with, '') as share_with, coalesce(file_source, '') as file_source, - coalesce(item_type, '') as item_type, coalesce(token,'') as token, - coalesce(expiration, '') as expiration, coalesce(share_name, '') as share_name, - s.stime, s.permissions, fc.storage as storage - FROM oc_share s - LEFT JOIN oc_filecache fc ON fc.fileid = file_source - WHERE share_type=? AND id=?` - if err := m.db.QueryRow(query, publicShareType, id).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.ItemType, &s.Token, &s.Expiration, &s.ShareName, &s.STime, &s.Permissions, &s.ItemStorage); err != nil { - if err == sql.ErrNoRows { - return nil, errtypes.NotFound(id) - } - return nil, err - } - ps, err := m.ConvertToCS3PublicShare(ctx, s) - if err != nil { - return nil, err - } - ret := &publicshare.WithPassword{ - Password: strings.TrimPrefix(s.ShareWith, "1|"), - } - proto.Merge(&ret.PublicShare, ps) - return ret, nil -} - -func (m *mgr) ListPublicShares(ctx context.Context, u *user.User, filters []*link.ListPublicSharesRequest_Filter, sign bool) ([]*link.PublicShare, error) { - uid := u.Username - // FIXME instead of joining we may want to have to do a stat call ... if we want to store shares from other providers? or just Dump()? and be done with migration? - query := `SELECT - coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, - coalesce(share_with, '') as share_with, coalesce(file_source, '') as file_source, - coalesce(item_type, '') as item_type, coalesce(token,'') as token, - coalesce(expiration, '') as expiration, coalesce(share_name, '') as share_name, - s.id, s.stime, s.permissions, fc.storage as storage - FROM oc_share s - LEFT JOIN oc_filecache fc ON fc.fileid = file_source - WHERE (uid_owner=? or uid_initiator=?) - AND (share_type=?)` - var resourceFilters, ownerFilters, creatorFilters, storageFilters string - var resourceParams, ownerParams, creatorParams, storageParams []interface{} - params := []interface{}{uid, uid, publicShareType} - - for _, f := range filters { - switch f.Type { - case link.ListPublicSharesRequest_Filter_TYPE_RESOURCE_ID: - if len(resourceFilters) != 0 { - resourceFilters += " OR " - } - resourceFilters += "item_source=?" - resourceParams = append(resourceParams, f.GetResourceId().GetOpaqueId()) - case link.ListPublicSharesRequest_Filter_TYPE_OWNER: - if len(ownerFilters) != 0 { - ownerFilters += " OR " - } - ownerFilters += "(uid_owner=?)" - ownerParams = append(ownerParams, formatUserID(f.GetOwner())) - case link.ListPublicSharesRequest_Filter_TYPE_CREATOR: - if len(creatorFilters) != 0 { - creatorFilters += " OR " - } - creatorFilters += "(uid_initiator=?)" - creatorParams = append(creatorParams, formatUserID(f.GetCreator())) - case publicshare.StorageIDFilterType: - if len(storageFilters) != 0 { - storageFilters += " OR " - } - storageFilters += "(storage=?)" - storageParams = append(storageParams, f.GetResourceId().GetStorageId()) - } - } - if resourceFilters != "" { - query = fmt.Sprintf("%s AND (%s)", query, resourceFilters) - params = append(params, resourceParams...) - } - if ownerFilters != "" { - query = fmt.Sprintf("%s AND (%s)", query, ownerFilters) - params = append(params, ownerParams...) - } - if creatorFilters != "" { - query = fmt.Sprintf("%s AND (%s)", query, creatorFilters) - params = append(params, creatorParams...) - } - if storageFilters != "" { - query = fmt.Sprintf("%s AND (%s)", query, storageFilters) - params = append(params, storageParams...) - } - - rows, err := m.db.Query(query, params...) - if err != nil { - return nil, err - } - defer rows.Close() - - var s DBShare - shares := []*link.PublicShare{} - for rows.Next() { - if err := rows.Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.ItemType, &s.Token, &s.Expiration, &s.ShareName, &s.ID, &s.STime, &s.Permissions, &s.ItemStorage); err != nil { - continue - } - var cs3Share *link.PublicShare - if cs3Share, err = m.ConvertToCS3PublicShare(ctx, s); err != nil { - return nil, err - } - if publicshare.IsExpired(cs3Share) { - _ = m.cleanupExpiredShares() - } else { - if cs3Share.PasswordProtected && sign { - if err := publicshare.AddSignature(cs3Share, strings.TrimPrefix(s.ShareWith, "1|")); err != nil { - return nil, err - } - } - shares = append(shares, cs3Share) - } - } - if err = rows.Err(); err != nil { - return nil, err - } - - return shares, nil -} - -func (m *mgr) RevokePublicShare(ctx context.Context, u *user.User, ref *link.PublicShareReference) error { - uid := u.Username - query := "delete from oc_share where " - params := []interface{}{} - - switch { - case ref.GetId() != nil && ref.GetId().OpaqueId != "": - query += "id=? AND (uid_owner=? or uid_initiator=?)" - params = append(params, ref.GetId().OpaqueId, uid, uid) - case ref.GetToken() != "": - query += "token=? AND (uid_owner=? or uid_initiator=?)" - params = append(params, ref.GetToken(), uid, uid) - default: - return errtypes.NotFound(ref.String()) - } - - stmt, err := m.db.Prepare(query) - if err != nil { - return err - } - res, err := stmt.Exec(params...) - if err != nil { - return err - } - - rowCnt, err := res.RowsAffected() - if err != nil { - return err - } - if rowCnt == 0 { - return errtypes.NotFound(ref.String()) - } - return nil -} - -func (m *mgr) GetPublicShareByToken(ctx context.Context, token string, auth *link.PublicShareAuthentication, sign bool) (*link.PublicShare, error) { - ps, err := m.getByToken(ctx, token) - if err != nil { - return nil, err - } - - if publicshare.IsExpired(&ps.PublicShare) { - if err := m.cleanupExpiredShares(); err != nil { - return nil, err - } - return nil, errtypes.NotFound("public share has expired") - } - - if ps.PublicShare.PasswordProtected { - if !publicshare.Authenticate(&ps.PublicShare, ps.Password, auth) { - return nil, errtypes.InvalidCredentials("access denied") - } - } - - return &ps.PublicShare, nil -} - -func (m *mgr) cleanupExpiredShares() error { - if !m.c.EnableExpiredSharesCleanup { - return nil - } - - query := "DELETE FROM oc_share WHERE expiration IS NOT NULL AND expiration < ?" - params := []interface{}{time.Now().Format("2006-01-02 03:04:05")} - - stmt, err := m.db.Prepare(query) - if err != nil { - return err - } - if _, err = stmt.Exec(params...); err != nil { - return err - } - return nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql/test.db b/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql/test.db deleted file mode 100644 index fba76fdcc4f..00000000000 Binary files a/vendor/github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql/test.db and /dev/null differ diff --git a/vendor/github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool/selector.go b/vendor/github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool/selector.go index eb364130955..27fba088c37 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool/selector.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool/selector.go @@ -20,6 +20,7 @@ package pool import ( "fmt" + "math/rand" "strings" "sync" @@ -45,7 +46,9 @@ import ( "github.com/owncloud/reva/v2/pkg/registry" "github.com/pkg/errors" "github.com/sercand/kuberesolver/v5" + mRegistry "go-micro.dev/v4/registry" "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" ) func init() { @@ -82,6 +85,27 @@ func GetSelector[T any](k string, id string, f func(cc grpc.ClientConnInterface) return newSelector } +// cachedClient pairs a grpc client with the connection backing it, so Next +// can check the connection's health before handing the client back out. +type cachedClient[T any] struct { + conn *grpc.ClientConn + client T +} + +// healthy reports whether the cached connection is safe to hand out again. +// A connection only becomes TransientFailure or Shutdown after a real RPC on +// it has already failed - a connection that was never used for an RPC stays +// Idle, so it is treated as healthy here (there is no way to probe +// reachability without using it). +func (c cachedClient[T]) healthy() bool { + switch c.conn.GetState() { + case connectivity.TransientFailure, connectivity.Connecting, connectivity.Shutdown: + return false + default: + return true + } +} + type Selector[T any] struct { id string clientFactory func(cc grpc.ClientConnInterface) T @@ -101,47 +125,99 @@ func (s *Selector[T]) Next(opts ...Option) (T, error) { opt(&options) } - target := s.id // if the target is given as a recognized gRPC URI, skip registry lookup // see https://github.com/grpc/grpc/blob/master/doc/naming.md#name-syntax prefix := strings.SplitN(s.id, ":", 2)[0] switch { - case prefix == "dns": - fallthrough - case prefix == "unix": - fallthrough - case prefix == "kubernetes": - // use target as is and skip registry lookup + case prefix == "dns", prefix == "unix", prefix == "kubernetes": + // use the id as is and skip registry lookup + return s.loadOrNewClient(s.id, allOpts) case options.registry != nil: - // use service registry to look up address + // use the service registry to look up every known node, and pick + // the first one that is ready services, err := options.registry.GetService(s.id) if err != nil { return *new(T), fmt.Errorf("%s: %w", s.id, err) } - nodeAddress, err := registry.GetNodeAddress(services) - if err != nil { - return *new(T), fmt.Errorf("%s: %w", s.id, err) - } - target = nodeAddress + targets := shuffledTargets(services) + return s.nextHealthy(targets, allOpts) default: - // if no registry is available, use the target as is + // if no registry is available, use the id as is + return s.loadOrNewClient(s.id, allOpts) + } +} + +// nextHealthy walks targets in the given order. It returns the client for +// the first target that is ready. A target with no cache entry is also a +// candidate, since Next has no way to know its state without dialing it. If +// no target is ready, nextHealthy reuses the first one found, so the caller +// gets a real error from a real attempt instead of a fabricated one. +func (s *Selector[T]) nextHealthy(targets []string, opts []Option) (T, error) { + if len(targets) == 0 { + return *new(T), fmt.Errorf("%s: no nodes found in registry", s.id) + } + var fallback *cachedClient[T] + for _, target := range targets { + cached, ok := s.clientMap.Load(target) + if !ok { + return s.newClient(target, opts) + } + + cc := cached.(cachedClient[T]) + if cc.healthy() { + return cc.client, nil + } + if fallback == nil { + fallback = &cc + } } - existingClient, ok := s.clientMap.Load(target) - if ok { - return existingClient.(T), nil - } + return fallback.client, nil +} - conn, err := NewConn(target, allOpts...) +// loadOrNewClient returns the cached client for target, creating a new one if +// none exists yet. It does not check health: target was not looked up +// through the registry, so there is no alternative node to fall back to. +func (s *Selector[T]) loadOrNewClient(target string, opts []Option) (T, error) { + if cached, ok := s.clientMap.Load(target); ok { + return cached.(cachedClient[T]).client, nil + } + return s.newClient(target, opts) +} + +// newClient creates a new connection to target, wraps it in a client and caches +// both under target. +func (s *Selector[T]) newClient(target string, opts []Option) (T, error) { + conn, err := NewConn(target, opts...) if err != nil { return *new(T), errors.Wrap(err, fmt.Sprintf("could not create connection for %s to %s", s.id, target)) } - newClient := s.clientFactory(conn) - s.clientMap.Store(target, newClient) + newClient := cachedClient[T]{conn: conn, client: s.clientFactory(conn)} + // if another goroutine already created a client for this target, use it instead + if existing, loaded := s.clientMap.LoadOrStore(target, newClient); loaded { + _ = conn.Close() + return existing.(cachedClient[T]).client, nil + } - return newClient, nil + return newClient.client, nil +} + +// shuffledTargets flattens every node address out of services and returns +// them in random order, so repeated calls spread load across all known +// nodes instead of always preferring the same one. +func shuffledTargets(services []*mRegistry.Service) []string { + var targets []string + for _, svc := range services { + for _, node := range svc.Nodes { + targets = append(targets, node.Address) + } + } + rand.Shuffle(len(targets), func(i, j int) { + targets[i], targets[j] = targets[j], targets[i] + }) + return targets } // GatewaySelector returns a Selector[gateway.GatewayAPIClient]. diff --git a/vendor/github.com/owncloud/reva/v2/pkg/share/manager/loader/loader.go b/vendor/github.com/owncloud/reva/v2/pkg/share/manager/loader/loader.go index c8ae078eb73..6961bef5adb 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/share/manager/loader/loader.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/share/manager/loader/loader.go @@ -24,6 +24,5 @@ import ( _ "github.com/owncloud/reva/v2/pkg/share/manager/json" _ "github.com/owncloud/reva/v2/pkg/share/manager/jsoncs3" _ "github.com/owncloud/reva/v2/pkg/share/manager/memory" - _ "github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql" // Add your own here ) diff --git a/vendor/github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql/conversions.go b/vendor/github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql/conversions.go deleted file mode 100644 index 3a96b4d2b16..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql/conversions.go +++ /dev/null @@ -1,301 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package owncloudsql - -import ( - "context" - "strings" - "time" - - grouppb "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1" - userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - userprovider "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" - collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1" - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" - "github.com/owncloud/reva/v2/pkg/conversions" - "github.com/owncloud/reva/v2/pkg/rgrpc/status" - "github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool" - "github.com/owncloud/reva/v2/pkg/utils" - "github.com/jellydator/ttlcache/v2" -) - -// DBShare stores information about user and public shares. -type DBShare struct { - ID string - UIDOwner string - UIDInitiator string - ItemStorage string - FileSource string - ShareWith string - Token string - Expiration string - Permissions int - ShareType int - ShareName string - STime int - FileTarget string - RejectedBy string - State int - Parent int -} - -// UserConverter describes an interface for converting user ids to names and back -type UserConverter interface { - UserNameToUserID(ctx context.Context, username string) (*userpb.UserId, error) - UserIDToUserName(ctx context.Context, userid *userpb.UserId) (string, error) - GetUser(userid *userpb.UserId) (*userpb.User, error) -} - -// GatewayUserConverter converts usernames and ids using the gateway -type GatewayUserConverter struct { - gwAddr string - - IDCache *ttlcache.Cache - NameCache *ttlcache.Cache -} - -// NewGatewayUserConverter returns a instance of GatewayUserConverter -func NewGatewayUserConverter(gwAddr string) *GatewayUserConverter { - IDCache := ttlcache.NewCache() - _ = IDCache.SetTTL(30 * time.Second) - IDCache.SkipTTLExtensionOnHit(true) - NameCache := ttlcache.NewCache() - _ = NameCache.SetTTL(30 * time.Second) - NameCache.SkipTTLExtensionOnHit(true) - - return &GatewayUserConverter{ - gwAddr: gwAddr, - IDCache: IDCache, - NameCache: NameCache, - } -} - -// UserIDToUserName converts a user ID to an username -func (c *GatewayUserConverter) UserIDToUserName(ctx context.Context, userid *userpb.UserId) (string, error) { - username, err := c.NameCache.Get(userid.String()) - if err == nil { - return username.(string), nil - } - - gwConn, err := pool.GetGatewayServiceClient(c.gwAddr) - if err != nil { - return "", err - } - getUserResponse, err := gwConn.GetUser(ctx, &userprovider.GetUserRequest{ - UserId: userid, - SkipFetchingUserGroups: true, - }) - if err != nil { - return "", err - } - if getUserResponse.Status.Code != rpc.Code_CODE_OK { - return "", status.NewErrorFromCode(getUserResponse.Status.Code, "gateway") - } - _ = c.NameCache.Set(userid.String(), getUserResponse.User.Username) - return getUserResponse.User.Username, nil -} - -// UserNameToUserID converts a username to an user ID -func (c *GatewayUserConverter) UserNameToUserID(ctx context.Context, username string) (*userpb.UserId, error) { - id, err := c.IDCache.Get(username) - if err == nil { - return id.(*userpb.UserId), nil - } - - gwConn, err := pool.GetGatewayServiceClient(c.gwAddr) - if err != nil { - return nil, err - } - getUserResponse, err := gwConn.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{ - Claim: "username", - Value: username, - SkipFetchingUserGroups: true, - }) - if err != nil { - return nil, err - } - if getUserResponse.Status.Code != rpc.Code_CODE_OK { - return nil, status.NewErrorFromCode(getUserResponse.Status.Code, "gateway") - } - _ = c.IDCache.Set(username, getUserResponse.User.Id) - return getUserResponse.User.Id, nil -} - -// GetUser gets the user -func (c *GatewayUserConverter) GetUser(userid *userpb.UserId) (*userpb.User, error) { - gwc, err := pool.GetGatewayServiceClient(c.gwAddr) - if err != nil { - return nil, err - } - return utils.GetUser(userid, gwc) -} - -func (m *mgr) formatGrantee(ctx context.Context, g *provider.Grantee) (int, string, error) { - var granteeType int - var formattedID string - switch g.Type { - case provider.GranteeType_GRANTEE_TYPE_USER: - granteeType = 0 - var err error - formattedID, err = m.userConverter.UserIDToUserName(ctx, g.GetUserId()) - if err != nil { - return 0, "", err - } - case provider.GranteeType_GRANTEE_TYPE_GROUP: - granteeType = 1 - formattedID = formatGroupID(g.GetGroupId()) - default: - granteeType = -1 - } - return granteeType, formattedID, nil -} - -func (m *mgr) extractGrantee(ctx context.Context, t int, g string) (*provider.Grantee, error) { - var grantee provider.Grantee - switch t { - case 0: - userid, err := m.userConverter.UserNameToUserID(ctx, g) - if err != nil { - return nil, err - } - grantee.Type = provider.GranteeType_GRANTEE_TYPE_USER - grantee.Id = &provider.Grantee_UserId{UserId: userid} - case 1, 2: - grantee.Type = provider.GranteeType_GRANTEE_TYPE_GROUP - grantee.Id = &provider.Grantee_GroupId{GroupId: extractGroupID(g)} - default: - grantee.Type = provider.GranteeType_GRANTEE_TYPE_INVALID - } - return &grantee, nil -} - -func resourceTypeToItem(r provider.ResourceType) string { - switch r { - case provider.ResourceType_RESOURCE_TYPE_FILE: - return "file" - case provider.ResourceType_RESOURCE_TYPE_CONTAINER: - return "folder" - case provider.ResourceType_RESOURCE_TYPE_REFERENCE: - return "reference" - case provider.ResourceType_RESOURCE_TYPE_SYMLINK: - return "symlink" - default: - return "" - } -} - -func sharePermToInt(p *provider.ResourcePermissions) int { - return int(conversions.RoleFromResourcePermissions(p, false).OCSPermissions()) -} - -func intTosharePerm(p int) (*provider.ResourcePermissions, error) { - perms, err := conversions.NewPermissions(p) - if err != nil { - return nil, err - } - - return conversions.RoleFromOCSPermissions(perms, nil).CS3ResourcePermissions(), nil -} - -func intToShareState(g int) collaboration.ShareState { - switch g { - case 0: - return collaboration.ShareState_SHARE_STATE_ACCEPTED - case 1: - return collaboration.ShareState_SHARE_STATE_PENDING - case 2: - return collaboration.ShareState_SHARE_STATE_REJECTED - default: - return collaboration.ShareState_SHARE_STATE_INVALID - } -} - -func formatUserID(u *userpb.UserId) string { - return u.OpaqueId -} - -func formatGroupID(u *grouppb.GroupId) string { - return u.OpaqueId -} - -func extractGroupID(u string) *grouppb.GroupId { - return &grouppb.GroupId{OpaqueId: u} -} - -func (m *mgr) convertToCS3Share(ctx context.Context, s DBShare, storageMountID string) (*collaboration.Share, error) { - ts := &typespb.Timestamp{ - Seconds: uint64(s.STime), - } - permissions, err := intTosharePerm(s.Permissions) - if err != nil { - return nil, err - } - grantee, err := m.extractGrantee(ctx, s.ShareType, s.ShareWith) - if err != nil { - return nil, err - } - owner, err := m.userConverter.UserNameToUserID(ctx, s.UIDOwner) - if err != nil { - return nil, err - } - var creator *userpb.UserId - if s.UIDOwner == s.UIDInitiator { - creator = owner - } else { - creator, err = m.userConverter.UserNameToUserID(ctx, s.UIDOwner) - if err != nil { - return nil, err - } - } - return &collaboration.Share{ - Id: &collaboration.ShareId{ - OpaqueId: s.ID, - }, - ResourceId: &provider.ResourceId{ - SpaceId: s.ItemStorage, - OpaqueId: s.FileSource, - }, - Permissions: &collaboration.SharePermissions{Permissions: permissions}, - Grantee: grantee, - Owner: owner, - Creator: creator, - Ctime: ts, - Mtime: ts, - }, nil -} - -func (m *mgr) convertToCS3ReceivedShare(ctx context.Context, s DBShare, storageMountID string) (*collaboration.ReceivedShare, error) { - share, err := m.convertToCS3Share(ctx, s, storageMountID) - if err != nil { - return nil, err - } - var state collaboration.ShareState - if s.RejectedBy != "" { - state = collaboration.ShareState_SHARE_STATE_REJECTED - } else { - state = intToShareState(s.State) - } - return &collaboration.ReceivedShare{ - Share: share, - State: state, - MountPoint: &provider.Reference{Path: strings.TrimLeft(s.FileTarget, "/")}, - }, nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql/owncloudsql.go b/vendor/github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql/owncloudsql.go deleted file mode 100644 index e0dbc019bdb..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql/owncloudsql.go +++ /dev/null @@ -1,674 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package owncloudsql - -import ( - "context" - "database/sql" - "fmt" - "path" - "strconv" - "strings" - "time" - - userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1" - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" - ctxpkg "github.com/owncloud/reva/v2/pkg/ctx" - "github.com/owncloud/reva/v2/pkg/errtypes" - "github.com/owncloud/reva/v2/pkg/share" - "github.com/owncloud/reva/v2/pkg/share/manager/registry" - "github.com/owncloud/reva/v2/pkg/utils" - "github.com/mitchellh/mapstructure" - "github.com/pkg/errors" - "google.golang.org/genproto/protobuf/field_mask" - - // Provides mysql drivers - _ "github.com/go-sql-driver/mysql" -) - -const ( - shareTypeUser = 0 - shareTypeGroup = 1 -) - -func init() { - registry.Register("owncloudsql", NewMysql) -} - -type config struct { - GatewayAddr string `mapstructure:"gateway_addr"` - StorageMountID string `mapstructure:"storage_mount_id"` - DbUsername string `mapstructure:"db_username"` - DbPassword string `mapstructure:"db_password"` - DbHost string `mapstructure:"db_host"` - DbPort int `mapstructure:"db_port"` - DbName string `mapstructure:"db_name"` -} - -type mgr struct { - driver string - db *sql.DB - storageMountID string - userConverter UserConverter -} - -// NewMysql returns a new share manager connection to a mysql database -func NewMysql(m map[string]interface{}) (share.Manager, error) { - c, err := parseConfig(m) - if err != nil { - err = errors.Wrap(err, "error creating a new manager") - return nil, err - } - - db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.DbUsername, c.DbPassword, c.DbHost, c.DbPort, c.DbName)) - if err != nil { - return nil, err - } - - userConverter := NewGatewayUserConverter(c.GatewayAddr) - - return New("mysql", db, c.StorageMountID, userConverter) -} - -// New returns a new Cache instance connecting to the given sql.DB -func New(driver string, db *sql.DB, storageMountID string, userConverter UserConverter) (share.Manager, error) { - return &mgr{ - driver: driver, - db: db, - storageMountID: storageMountID, - userConverter: userConverter, - }, nil -} - -func parseConfig(m map[string]interface{}) (*config, error) { - c := &config{} - if err := mapstructure.Decode(m, c); err != nil { - return nil, err - } - return c, nil -} - -func (m *mgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collaboration.ShareGrant) (*collaboration.Share, error) { - user := ctxpkg.ContextMustGetUser(ctx) - - // do not allow share to myself or the owner if share is for a user - // TODO(labkode): should not this be caught already at the gw level? - if g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_USER && - (utils.UserEqual(g.Grantee.GetUserId(), user.Id) || utils.UserEqual(g.Grantee.GetUserId(), md.Owner)) { - return nil, errtypes.BadRequest("owncloudsql: owner/creator and grantee are the same") - } - - // check if share already exists. - key := &collaboration.ShareKey{ - Owner: md.Owner, - ResourceId: md.Id, - Grantee: g.Grantee, - } - _, err := m.getByKey(ctx, key) - - // share already exists - if err == nil { - return nil, errtypes.AlreadyExists(key.String()) - } - - now := time.Now().Unix() - ts := &typespb.Timestamp{ - Seconds: uint64(now), - } - - owner, err := m.userConverter.UserIDToUserName(ctx, md.Owner) - if err != nil { - return nil, err - } - shareType, shareWith, err := m.formatGrantee(ctx, g.Grantee) - if err != nil { - return nil, err - } - itemType := resourceTypeToItem(md.Type) - targetPath := path.Join("/", path.Base(md.Path)) - permissions := sharePermToInt(g.Permissions.Permissions) - itemSource := md.Id.OpaqueId - fileSource, err := strconv.ParseUint(itemSource, 10, 64) - if err != nil { - // it can be the case that the item source may be a character string - // we leave fileSource blank in that case - fileSource = 0 - } - - stmtString := "INSERT INTO oc_share (share_type,uid_owner,uid_initiator,item_type,item_source,file_source,permissions,stime,share_with,file_target) VALUES (?,?,?,?,?,?,?,?,?,?)" - stmtValues := []interface{}{shareType, owner, user.Username, itemType, itemSource, fileSource, permissions, now, shareWith, targetPath} - - stmt, err := m.db.Prepare(stmtString) - if err != nil { - return nil, err - } - result, err := stmt.ExecContext(ctx, stmtValues...) - if err != nil { - return nil, err - } - lastID, err := result.LastInsertId() - if err != nil { - return nil, err - } - - return &collaboration.Share{ - Id: &collaboration.ShareId{ - OpaqueId: strconv.FormatInt(lastID, 10), - }, - ResourceId: md.Id, - Permissions: g.Permissions, - Grantee: g.Grantee, - Owner: md.Owner, - Creator: user.Id, - Ctime: ts, - Mtime: ts, - }, nil -} - -func (m *mgr) GetShare(ctx context.Context, ref *collaboration.ShareReference) (*collaboration.Share, error) { - var s *collaboration.Share - var err error - switch { - case ref.GetId() != nil: - s, err = m.getByID(ctx, ref.GetId()) - case ref.GetKey() != nil: - s, err = m.getByKey(ctx, ref.GetKey()) - default: - err = errtypes.NotFound(ref.String()) - } - - if err != nil { - return nil, err - } - - return s, nil -} - -func (m *mgr) Unshare(ctx context.Context, ref *collaboration.ShareReference) error { - uid := ctxpkg.ContextMustGetUser(ctx).Username - var query string - params := []interface{}{} - switch { - case ref.GetId() != nil: - query = "DELETE FROM oc_share where id=? AND (uid_owner=? or uid_initiator=?)" - params = append(params, ref.GetId().OpaqueId, uid, uid) - case ref.GetKey() != nil: - key := ref.GetKey() - shareType, shareWith, err := m.formatGrantee(ctx, key.Grantee) - if err != nil { - return err - } - owner := formatUserID(key.Owner) - query = "DELETE FROM oc_share WHERE uid_owner=? AND file_source=? AND share_type=? AND share_with=? AND (uid_owner=? or uid_initiator=?)" - params = append(params, owner, key.ResourceId.OpaqueId, shareType, shareWith, uid, uid) - default: - return errtypes.NotFound(ref.String()) - } - - stmt, err := m.db.Prepare(query) - if err != nil { - return err - } - res, err := stmt.ExecContext(ctx, params...) - if err != nil { - return err - } - - rowCnt, err := res.RowsAffected() - if err != nil { - return err - } - if rowCnt == 0 { - return errtypes.NotFound(ref.String()) - } - return nil -} - -func (m *mgr) UpdateShare(ctx context.Context, ref *collaboration.ShareReference, p *collaboration.SharePermissions, updated *collaboration.Share, fieldMask *field_mask.FieldMask) (*collaboration.Share, error) { - permissions := sharePermToInt(p.Permissions) - uid := ctxpkg.ContextMustGetUser(ctx).Username - - var query string - params := []interface{}{} - switch { - case ref.GetId() != nil: - query = "update oc_share set permissions=?,stime=? where id=? AND (uid_owner=? or uid_initiator=?)" - params = append(params, permissions, time.Now().Unix(), ref.GetId().OpaqueId, uid, uid) - case ref.GetKey() != nil: - key := ref.GetKey() - shareType, shareWith, err := m.formatGrantee(ctx, key.Grantee) - if err != nil { - return nil, err - } - owner := formatUserID(key.Owner) - query = "update oc_share set permissions=?,stime=? where (uid_owner=? or uid_initiator=?) AND file_source=? AND share_type=? AND share_with=? AND (uid_owner=? or uid_initiator=?)" - params = append(params, permissions, time.Now().Unix(), owner, owner, key.ResourceId.OpaqueId, shareType, shareWith, uid, uid) - default: - return nil, errtypes.NotFound(ref.String()) - } - - stmt, err := m.db.Prepare(query) - if err != nil { - return nil, err - } - if _, err = stmt.ExecContext(ctx, params...); err != nil { - return nil, err - } - - return m.GetShare(ctx, ref) -} - -func (m *mgr) ListShares(ctx context.Context, filters []*collaboration.Filter) ([]*collaboration.Share, error) { - uid := ctxpkg.ContextMustGetUser(ctx).Username - query := ` - SELECT - coalesce(s.uid_owner, '') as uid_owner, coalesce(s.uid_initiator, '') as uid_initiator, - coalesce(s.share_with, '') as share_with, coalesce(s.file_source, '') as file_source, - s.file_target, s.id, s.stime, s.permissions, s.share_type, fc.storage as storage - FROM oc_share s - LEFT JOIN oc_filecache fc ON fc.fileid = file_source - WHERE (uid_owner=? or uid_initiator=?) - ` - params := []interface{}{uid, uid} - - var ( - filterQuery string - filterParams []interface{} - err error - ) - if len(filters) == 0 { - filterQuery += "(share_type=? OR share_type=?)" - params = append(params, shareTypeUser) - params = append(params, shareTypeGroup) - } else { - filterQuery, filterParams, err = translateFilters(filters) - if err != nil { - return nil, err - } - params = append(params, filterParams...) - } - - if filterQuery != "" { - query = fmt.Sprintf("%s AND (%s)", query, filterQuery) - } - - rows, err := m.db.QueryContext(ctx, query, params...) - if err != nil { - return nil, err - } - defer rows.Close() - - var s DBShare - shares := []*collaboration.Share{} - for rows.Next() { - if err := rows.Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.ItemStorage); err != nil { - continue - } - share, err := m.convertToCS3Share(ctx, s, m.storageMountID) - if err != nil { - return nil, err - } - shares = append(shares, share) - } - if err = rows.Err(); err != nil { - return nil, err - } - - return shares, nil -} - -// we list the shares that are targeted to the user in context or to the user groups. -func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.Filter, forUser *userpb.UserId) ([]*collaboration.ReceivedShare, error) { - user := ctxpkg.ContextMustGetUser(ctx) - if user.GetId().GetType() == userpb.UserType_USER_TYPE_SERVICE { - u, err := m.userConverter.GetUser(forUser) - if err != nil { - return nil, errtypes.BadRequest("user not found") - } - user = u - } - uid := user.Username - - params := []interface{}{uid, uid, uid} - for _, v := range user.Groups { - params = append(params, v) - } - - homeConcat := "" - if m.driver == "mysql" { // mysql concat - homeConcat = "storages.id = CONCAT('home::', s.uid_owner)" - } else { // sqlite3 concat - homeConcat = "storages.id = 'home::' || s.uid_owner" - } - userSelect := "" - if len(user.Groups) > 0 { - userSelect = "AND ((share_type != 1 AND share_with=?) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))" - } else { - userSelect = "AND (share_type != 1 AND share_with=?)" - } - query := ` - WITH results AS - ( - SELECT s.*, storages.numeric_id FROM oc_share s - LEFT JOIN oc_storages storages ON ` + homeConcat + ` - WHERE (uid_owner != ? AND uid_initiator != ?) ` + userSelect + ` - ) - SELECT COALESCE(r.uid_owner, '') AS uid_owner, COALESCE(r.uid_initiator, '') AS uid_initiator, COALESCE(r.share_with, '') - AS share_with, COALESCE(r.file_source, '') AS file_source, COALESCE(r2.file_target, r.file_target), r.id, r.stime, r.permissions, r.share_type, COALESCE(r2.accepted, r.accepted), - r.numeric_id, COALESCE(r.parent, -1) AS parent FROM results r LEFT JOIN results r2 ON r.id = r2.parent WHERE r.parent IS NULL` - - filterQuery, filterParams, err := translateFilters(filters) - if err != nil { - return nil, err - } - params = append(params, filterParams...) - - if filterQuery != "" { - query = fmt.Sprintf("%s AND (%s)", query, filterQuery) - } - query += ";" - rows, err := m.db.QueryContext(ctx, query, params...) - if err != nil { - return nil, err - } - defer rows.Close() - - var s DBShare - shares := []*collaboration.ReceivedShare{} - for rows.Next() { - if err := rows.Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State, &s.ItemStorage, &s.Parent); err != nil { - continue - } - share, err := m.convertToCS3ReceivedShare(ctx, s, m.storageMountID) - if err != nil { - return nil, err - } - shares = append(shares, share) - } - if err = rows.Err(); err != nil { - return nil, err - } - - return shares, nil -} - -func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareReference) (*collaboration.ReceivedShare, error) { - var s *collaboration.ReceivedShare - var err error - switch { - case ref.GetId() != nil: - s, err = m.getReceivedByID(ctx, ref.GetId()) - case ref.GetKey() != nil: - s, err = m.getReceivedByKey(ctx, ref.GetKey()) - default: - err = errtypes.NotFound(ref.String()) - } - - if err != nil { - return nil, err - } - - return s, nil - -} - -func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, _ *userpb.UserId) (*collaboration.ReceivedShare, error) { - // TODO: How to inject the uid when a UserId is set? override it in the ctx? Add parameter to GetReceivedShare? - rs, err := m.GetReceivedShare(ctx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: receivedShare.Share.Id}}) - if err != nil { - return nil, err - } - - fields := []string{} - params := []interface{}{} - for i := range fieldMask.Paths { - switch fieldMask.Paths[i] { - case "state": - rs.State = receivedShare.State - fields = append(fields, "accepted=?") - switch rs.State { - case collaboration.ShareState_SHARE_STATE_REJECTED: - params = append(params, 2) - case collaboration.ShareState_SHARE_STATE_ACCEPTED: - params = append(params, 0) - } - case "mount_point": - fields = append(fields, "file_target=?") - rs.MountPoint = receivedShare.MountPoint - params = append(params, rs.MountPoint.Path) - case "hidden": - continue - default: - return nil, errtypes.NotSupported("updating " + fieldMask.Paths[i] + " is not supported") - } - } - - if len(fields) == 0 { - return nil, fmt.Errorf("no valid field provided in the fieldmask") - } - - updateReceivedShare := func(column string) error { - query := "update oc_share set " - query += strings.Join(fields, ",") - query += fmt.Sprintf(" where %s=?", column) - params := append(params, rs.Share.Id.OpaqueId) - - stmt, err := m.db.Prepare(query) - if err != nil { - return err - } - res, err := stmt.ExecContext(ctx, params...) - if err != nil { - return err - } - affected, err := res.RowsAffected() - if err != nil { - return err - } - if affected < 1 { - return fmt.Errorf("No rows updated") - } - return nil - } - err = updateReceivedShare("parent") // Try to update the child state in case of group shares first - if err != nil { - err = updateReceivedShare("id") - } - if err != nil { - return nil, err - } - - return rs, nil -} - -func (m *mgr) getByID(ctx context.Context, id *collaboration.ShareId) (*collaboration.Share, error) { - uid := ctxpkg.ContextMustGetUser(ctx).Username - s := DBShare{ID: id.OpaqueId} - query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with, coalesce(file_source, '') as file_source, file_target, stime, permissions, share_type FROM oc_share WHERE id=? AND (uid_owner=? or uid_initiator=?)" - if err := m.db.QueryRowContext(ctx, query, id.OpaqueId, uid, uid).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.STime, &s.Permissions, &s.ShareType); err != nil { - if err == sql.ErrNoRows { - return nil, errtypes.NotFound(id.OpaqueId) - } - return nil, err - } - return m.convertToCS3Share(ctx, s, m.storageMountID) -} - -func (m *mgr) getByKey(ctx context.Context, key *collaboration.ShareKey) (*collaboration.Share, error) { - owner, err := m.userConverter.UserIDToUserName(ctx, key.Owner) - if err != nil { - return nil, err - } - uid := ctxpkg.ContextMustGetUser(ctx).Username - - s := DBShare{} - shareType, shareWith, err := m.formatGrantee(ctx, key.Grantee) - if err != nil { - return nil, err - } - query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with, coalesce(file_source, '') as file_source, file_target, id, stime, permissions, share_type FROM oc_share WHERE uid_owner=? AND file_source=? AND share_type=? AND share_with=? AND (uid_owner=? or uid_initiator=?)" - if err = m.db.QueryRowContext(ctx, query, owner, key.ResourceId.StorageId, shareType, shareWith, uid, uid).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType); err != nil { - if err == sql.ErrNoRows { - return nil, errtypes.NotFound(key.String()) - } - return nil, err - } - return m.convertToCS3Share(ctx, s, m.storageMountID) -} - -func (m *mgr) getReceivedByID(ctx context.Context, id *collaboration.ShareId) (*collaboration.ReceivedShare, error) { - user := ctxpkg.ContextMustGetUser(ctx) - uid := user.Username - - params := []interface{}{id.OpaqueId, id.OpaqueId, uid} - for _, v := range user.Groups { - params = append(params, v) - } - - homeConcat := "" - if m.driver == "mysql" { // mysql concat - homeConcat = "storages.id = CONCAT('home::', s.uid_owner)" - } else { // sqlite3 concat - homeConcat = "storages.id = 'home::' || s.uid_owner" - } - userSelect := "" - if len(user.Groups) > 0 { - userSelect = "AND ((share_type != 1 AND share_with=?) OR (share_type = 1 AND share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + ")))" - } else { - userSelect = "AND (share_type != 1 AND share_with=?)" - } - - query := ` - WITH results AS - ( - SELECT s.*, storages.numeric_id - FROM oc_share s - LEFT JOIN oc_storages storages ON ` + homeConcat + ` - WHERE s.id=? OR s.parent=? ` + userSelect + ` - ) - SELECT COALESCE(r.uid_owner, '') AS uid_owner, COALESCE(r.uid_initiator, '') AS uid_initiator, COALESCE(r.share_with, '') - AS share_with, COALESCE(r.file_source, '') AS file_source, COALESCE(r2.file_target, r.file_target), r.id, r.stime, r.permissions, r.share_type, COALESCE(r2.accepted, r.accepted), - r.numeric_id, COALESCE(r.parent, -1) AS parent - FROM results r - LEFT JOIN results r2 ON r.id = r2.parent - WHERE r.parent IS NULL; - ` - - s := DBShare{} - if err := m.db.QueryRowContext(ctx, query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State, &s.ItemStorage, &s.Parent); err != nil { - if err == sql.ErrNoRows { - return nil, errtypes.NotFound(id.OpaqueId) - } - return nil, err - } - return m.convertToCS3ReceivedShare(ctx, s, m.storageMountID) -} - -func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey) (*collaboration.ReceivedShare, error) { - user := ctxpkg.ContextMustGetUser(ctx) - uid := user.Username - - shareType, shareWith, err := m.formatGrantee(ctx, key.Grantee) - if err != nil { - return nil, err - } - params := []interface{}{uid, formatUserID(key.Owner), key.ResourceId.StorageId, key.ResourceId.OpaqueId, shareType, shareWith, shareWith} - for _, v := range user.Groups { - params = append(params, v) - } - - s := DBShare{} - query := "select coalesce(uid_owner, '') as uid_owner, coalesce(uid_initiator, '') as uid_initiator, coalesce(share_with, '') as share_with, coalesce(file_source, '') as file_source, file_target, ts.id, stime, permissions, share_type, accepted FROM oc_share ts WHERE uid_owner=? AND file_source=? AND share_type=? AND share_with=? " - if len(user.Groups) > 0 { - query += "AND (share_with=? OR share_with in (?" + strings.Repeat(",?", len(user.Groups)-1) + "))" - } else { - query += "AND (share_with=?)" - } - - if err := m.db.QueryRowContext(ctx, query, params...).Scan(&s.UIDOwner, &s.UIDInitiator, &s.ShareWith, &s.FileSource, &s.FileTarget, &s.ID, &s.STime, &s.Permissions, &s.ShareType, &s.State); err != nil { - if err == sql.ErrNoRows { - return nil, errtypes.NotFound(key.String()) - } - return nil, err - } - return m.convertToCS3ReceivedShare(ctx, s, m.storageMountID) -} - -func granteeTypeToShareType(granteeType provider.GranteeType) int { - switch granteeType { - case provider.GranteeType_GRANTEE_TYPE_USER: - return shareTypeUser - case provider.GranteeType_GRANTEE_TYPE_GROUP: - return shareTypeGroup - } - return -1 -} - -// translateFilters translates the filters to sql queries -func translateFilters(filters []*collaboration.Filter) (string, []interface{}, error) { - var ( - filterQuery string - params []interface{} - ) - - groupedFilters := share.GroupFiltersByType(filters) - // If multiple filters of the same type are passed to this function, they need to be combined with the `OR` operator. - // That is why the filters got grouped by type. - // For every given filter type, iterate over the filters and if there are more than one combine them. - // Combine the different filter types using `AND` - var filterCounter = 0 - for filterType, filters := range groupedFilters { - switch filterType { - case collaboration.Filter_TYPE_RESOURCE_ID: - filterQuery += "(" - for i, f := range filters { - filterQuery += "file_source=?" - params = append(params, f.GetResourceId().OpaqueId) - - if i != len(filters)-1 { - filterQuery += " OR " - } - } - filterQuery += ")" - case collaboration.Filter_TYPE_GRANTEE_TYPE: - filterQuery += "(" - for i, f := range filters { - filterQuery += "r.share_type=?" - params = append(params, granteeTypeToShareType(f.GetGranteeType())) - - if i != len(filters)-1 { - filterQuery += " OR " - } - } - filterQuery += ")" - case collaboration.Filter_TYPE_EXCLUDE_DENIALS: - // TODO this may change once the mapping of permission to share types is completed (cf. pkg/cbox/utils/conversions.go) - filterQuery += "r.permissions > 0" - default: - return "", nil, fmt.Errorf("filter type is not supported") - } - if filterCounter != len(groupedFilters)-1 { - filterQuery += " AND " - } - filterCounter++ - } - return filterQuery, params, nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql/test.db b/vendor/github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql/test.db deleted file mode 100644 index fba76fdcc4f..00000000000 Binary files a/vendor/github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql/test.db and /dev/null differ diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/loader/loader.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/loader/loader.go index ae836ff2da6..e73168385e0 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/loader/loader.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/loader/loader.go @@ -32,7 +32,6 @@ import ( _ "github.com/owncloud/reva/v2/pkg/storage/fs/localhome" _ "github.com/owncloud/reva/v2/pkg/storage/fs/nextcloud" _ "github.com/owncloud/reva/v2/pkg/storage/fs/ocis" - _ "github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql" _ "github.com/owncloud/reva/v2/pkg/storage/fs/s3" _ "github.com/owncloud/reva/v2/pkg/storage/fs/s3ng" // Add your own here diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/filecache/filecache.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/filecache/filecache.go deleted file mode 100644 index 42f441d8a37..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/filecache/filecache.go +++ /dev/null @@ -1,761 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package filecache - -import ( - "context" - "crypto/md5" - "database/sql" - "encoding/hex" - "fmt" - "path/filepath" - "regexp" - "strconv" - "strings" - "time" - - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - "github.com/owncloud/reva/v2/pkg/conversions" - - "github.com/pkg/errors" - "github.com/rs/zerolog/log" - - // Provides mysql drivers - _ "github.com/go-sql-driver/mysql" -) - -// Cache represents a oc10-style file cache -type Cache struct { - driver string - db *sql.DB -} - -// Storage represents a storage entry in the database -type Storage struct { - ID string - NumericID int -} - -// File represents an entry of the file cache -type File struct { - ID int - Storage int - Parent int - MimePart int - MimeType int - MimeTypeString string - Size int - MTime int - StorageMTime int - UnencryptedSize int - Permissions int - Encrypted bool - Path string - Name string - Etag string - Checksum string -} - -// TrashItem represents a trash item of the file cache -type TrashItem struct { - ID int - Name string - User string - Path string - Timestamp int -} - -// Scannable describes the interface providing a Scan method -type Scannable interface { - Scan(...interface{}) error -} - -// NewMysql returns a new Cache instance connecting to a MySQL database -func NewMysql(dsn string) (*Cache, error) { - sqldb, err := sql.Open("mysql", dsn) - if err != nil { - return nil, errors.Wrap(err, "error connecting to the database") - } - - // FIXME make configurable - sqldb.SetConnMaxLifetime(time.Minute * 3) - sqldb.SetConnMaxIdleTime(time.Second * 30) - sqldb.SetMaxOpenConns(100) - sqldb.SetMaxIdleConns(10) - - err = sqldb.Ping() - if err != nil { - return nil, errors.Wrap(err, "error connecting to the database") - } - - return New("mysql", sqldb) -} - -// New returns a new Cache instance connecting to the given sql.DB -func New(driver string, sqldb *sql.DB) (*Cache, error) { - return &Cache{ - driver: driver, - db: sqldb, - }, nil -} - -// ListStorages returns the list of numeric ids of all storages -// Optionally only home storages are considered -func (c *Cache) ListStorages(ctx context.Context, onlyHome bool) ([]*Storage, error) { - query := "" - if onlyHome { - mountPointConcat := "" - if c.driver == "mysql" { - mountPointConcat = "m.mount_point = CONCAT('/', m.user_id, '/')" - } else { // sqlite3 - mountPointConcat = "m.mount_point = '/' || m.user_id || '/'" - } - - query = "SELECT s.id, s.numeric_id FROM oc_storages s JOIN oc_mounts m ON s.numeric_id = m.storage_id WHERE " + mountPointConcat - } else { - query = "SELECT id, numeric_id FROM oc_storages" - } - rows, err := c.db.QueryContext(ctx, query) - if err != nil { - return nil, err - } - defer rows.Close() - - storages := []*Storage{} - for rows.Next() { - storage := &Storage{} - err := rows.Scan(&storage.ID, &storage.NumericID) - if err != nil { - return nil, err - } - storages = append(storages, storage) - } - return storages, nil -} - -// GetStorage returns the storage with the given numeric id -func (c *Cache) GetStorage(ctx context.Context, numeridID interface{}) (*Storage, error) { - numericID, err := toIntID(numeridID) - if err != nil { - return nil, err - } - row := c.db.QueryRowContext(ctx, "SELECT id, numeric_id FROM oc_storages WHERE numeric_id = ?", numericID) - s := &Storage{} - switch err := row.Scan(&s.ID, &s.NumericID); err { - case nil: - return s, nil - default: - return nil, err - } -} - -// GetNumericStorageID returns the database id for the given storage -func (c *Cache) GetNumericStorageID(ctx context.Context, id string) (int, error) { - row := c.db.QueryRowContext(ctx, "SELECT numeric_id FROM oc_storages WHERE id = ?", id) - var nid int - switch err := row.Scan(&nid); err { - case nil: - return nid, nil - default: - return -1, err - } -} - -// CreateStorage creates a new storage and returns its numeric id -func (c *Cache) CreateStorage(ctx context.Context, id string) (int, error) { - tx, err := c.db.Begin() - if err != nil { - return -1, err - } - defer func() { _ = tx.Rollback() }() - - stmt, err := tx.PrepareContext(ctx, "INSERT INTO oc_storages(id) VALUES(?)") - if err != nil { - return -1, err - } - defer stmt.Close() - - res, err := stmt.ExecContext(ctx, id) - if err != nil { - return -1, err - } - insertedID, err := res.LastInsertId() - if err != nil { - return -1, err - } - - data := map[string]interface{}{ - "path": "", - "etag": "", - "mimetype": "httpd/unix-directory", - } - _, err = c.doInsertOrUpdate(ctx, tx, int(insertedID), data, true) - if err != nil { - return -1, err - } - - err = tx.Commit() - if err != nil { - return -1, err - } - - return int(insertedID), err -} - -// GetStorageOwner returns the username of the owner of the given storage -func (c *Cache) GetStorageOwner(ctx context.Context, numericID interface{}) (string, error) { - numericID, err := toIntID(numericID) - if err != nil { - return "", err - } - row := c.db.QueryRowContext(ctx, "SELECT id FROM oc_storages WHERE numeric_id = ?", numericID) - var id string - switch err := row.Scan(&id); err { - case nil: - return strings.TrimPrefix(id, "home::"), nil - default: - return "", err - } -} - -// GetStorageOwnerByFileID returns the username of the owner of the given entry -func (c *Cache) GetStorageOwnerByFileID(ctx context.Context, numericID interface{}) (string, error) { - numericID, err := toIntID(numericID) - if err != nil { - return "", err - } - row := c.db.QueryRowContext(ctx, "SELECT id FROM oc_storages storages, oc_filecache cache WHERE storages.numeric_id = cache.storage AND cache.fileid = ?", numericID) - var id string - switch err := row.Scan(&id); err { - case nil: - return strings.TrimPrefix(id, "home::"), nil - default: - return "", err - } -} - -func (c *Cache) rowToFile(row Scannable) (*File, error) { - var fileid, storage, parent, mimetype, mimepart, size, mtime, storageMtime, encrypted, unencryptedSize int - var permissions sql.NullInt32 - var path, name, etag, checksum, mimetypestring sql.NullString - err := row.Scan(&fileid, &storage, &path, &parent, &permissions, &mimetype, &mimepart, &mimetypestring, &size, &mtime, &storageMtime, &encrypted, &unencryptedSize, &name, &etag, &checksum) - if err != nil { - return nil, err - } - - return &File{ - ID: fileid, - Storage: storage, - Path: path.String, - Parent: parent, - Permissions: int(permissions.Int32), - MimeType: mimetype, - MimeTypeString: mimetypestring.String, - MimePart: mimepart, - Size: size, - MTime: mtime, - StorageMTime: storageMtime, - Encrypted: encrypted == 1, - UnencryptedSize: unencryptedSize, - Name: name.String, - Etag: etag.String, - Checksum: checksum.String, - }, nil -} - -// Get returns the cache entry for the specified storage/path -func (c *Cache) Get(ctx context.Context, s interface{}, p string) (*File, error) { - storageID, err := toIntID(s) - if err != nil { - return nil, err - } - - phashBytes := md5.Sum([]byte(p)) - phash := hex.EncodeToString(phashBytes[:]) - - row := c.db.QueryRowContext(ctx, ` - SELECT - fc.fileid, fc.storage, fc.path, fc.parent, fc.permissions, fc.mimetype, fc.mimepart, - mt.mimetype, fc.size, fc.mtime, fc.storage_mtime, fc.encrypted, fc.unencrypted_size, - fc.name, fc.etag, fc.checksum - FROM oc_filecache fc - LEFT JOIN oc_mimetypes mt ON fc.mimetype = mt.id - WHERE path_hash = ? AND storage = ?`, phash, storageID) - return c.rowToFile(row) -} - -// Path returns the path for the specified entry -func (c *Cache) Path(ctx context.Context, id interface{}) (string, error) { - id, err := toIntID(id) - if err != nil { - return "", err - } - - row := c.db.QueryRowContext(ctx, "SELECT path FROM oc_filecache WHERE fileid = ?", id) - var path string - err = row.Scan(&path) - if err != nil { - return "", err - } - return path, nil -} - -// List returns the list of entries below the given path -func (c *Cache) List(ctx context.Context, storage interface{}, p string) ([]*File, error) { - storageID, err := toIntID(storage) - if err != nil { - return nil, err - } - - var rows *sql.Rows - phash := fmt.Sprintf("%x", md5.Sum([]byte(strings.Trim(p, "/")))) - rows, err = c.db.QueryContext(ctx, ` - SELECT - fc.fileid, fc.storage, fc.path, fc.parent, fc.permissions, fc.mimetype, fc.mimepart, - mt.mimetype, fc.size, fc.mtime, fc.storage_mtime, fc.encrypted, fc.unencrypted_size, - fc.name, fc.etag, fc.checksum - FROM oc_filecache fc - LEFT JOIN oc_mimetypes mt ON fc.mimetype = mt.id - WHERE storage = ? AND parent = (SELECT fileid FROM oc_filecache WHERE storage = ? AND path_hash=?) AND name IS NOT NULL - `, storageID, storageID, phash) - if err != nil { - return nil, err - } - defer rows.Close() - entries := []*File{} - for rows.Next() { - entry, err := c.rowToFile(rows) - if err != nil { - return nil, err - } - entries = append(entries, entry) - } - - return entries, nil -} - -// Permissions returns the permissions for the specified storage/path -func (c *Cache) Permissions(ctx context.Context, storage interface{}, p string) (*provider.ResourcePermissions, error) { - entry, err := c.Get(ctx, storage, p) - if err != nil { - return nil, err - } - - perms, err := conversions.NewPermissions(entry.Permissions) - if err != nil { - return nil, err - } - - return conversions.RoleFromOCSPermissions(perms, nil).CS3ResourcePermissions(), nil -} - -// InsertOrUpdate creates or updates a cache entry -func (c *Cache) InsertOrUpdate(ctx context.Context, storage interface{}, data map[string]interface{}, allowEmptyParent bool) (int, error) { - tx, err := c.db.Begin() - if err != nil { - return -1, err - } - defer func() { _ = tx.Rollback() }() - - id, err := c.doInsertOrUpdate(ctx, tx, storage, data, allowEmptyParent) - if err != nil { - return -1, err - } - - err = tx.Commit() - if err != nil { - return -1, err - } - - return id, err -} - -func (c *Cache) doInsertOrUpdate(ctx context.Context, tx *sql.Tx, storage interface{}, data map[string]interface{}, allowEmptyParent bool) (int, error) { - storageID, err := toIntID(storage) - if err != nil { - return -1, err - } - - columns := []string{"storage"} - placeholders := []string{"?"} - values := []interface{}{storage} - - for _, key := range []string{"path", "mimetype", "etag"} { - if _, exists := data[key]; !exists { - return -1, fmt.Errorf("missing required data") - } - } - - path := data["path"].(string) - data["name"] = filepath.Base(path) - if data["name"] == "." { - data["name"] = "" - } - - parentPath := strings.TrimRight(filepath.Dir(path), "/") - if parentPath == "." { - parentPath = "" - } - if path == "" { - data["parent"] = -1 - } else { - parent, err := c.Get(ctx, storageID, parentPath) - if err == nil { - data["parent"] = parent.ID - } else { - if allowEmptyParent { - data["parent"] = -1 - } else { - return -1, fmt.Errorf("could not find parent %s, %s, %v, %w", parentPath, path, parent, err) - } - } - } - - if _, exists := data["checksum"]; !exists { - data["checksum"] = "" - } - - for k, v := range data { - switch k { - case "path": - phashBytes := md5.Sum([]byte(v.(string))) - phash := hex.EncodeToString(phashBytes[:]) - columns = append(columns, "path_hash") - values = append(values, phash) - placeholders = append(placeholders, "?") - case "storage_mtime": - if _, exists := data["mtime"]; !exists { - columns = append(columns, "mtime") - values = append(values, v) - placeholders = append(placeholders, "?") - } - case "mimetype": - parts := strings.Split(v.(string), "/") - columns = append(columns, "mimetype") - values = append(values, v) - placeholders = append(placeholders, "(SELECT id FROM oc_mimetypes WHERE mimetype=?)") - columns = append(columns, "mimepart") - values = append(values, parts[0]) - placeholders = append(placeholders, "(SELECT id FROM oc_mimetypes WHERE mimetype=?)") - continue - } - - columns = append(columns, k) - values = append(values, v) - placeholders = append(placeholders, "?") - } - - err = c.insertMimetype(ctx, tx, data["mimetype"].(string)) - if err != nil { - return -1, err - } - - query := "INSERT INTO oc_filecache( " + strings.Join(columns, ", ") + ") VALUES(" + strings.Join(placeholders, ",") + ")" - - updates := []string{} - for i, column := range columns { - if column != "path" && column != "path_hash" && column != "storage" { - updates = append(updates, column+"="+placeholders[i]) - values = append(values, values[i]) - } - } - if c.driver == "mysql" { // mysql upsert - query += " ON DUPLICATE KEY UPDATE " - } else { // sqlite3 upsert - query += " ON CONFLICT(storage,path_hash) DO UPDATE SET " - } - query += strings.Join(updates, ",") - - stmt, err := tx.PrepareContext(ctx, query) - if err != nil { - return -1, err - } - - res, err := stmt.ExecContext(ctx, values...) - if err != nil { - log.Err(err).Msg("could not store filecache item") - return -1, err - } - id, err := res.LastInsertId() - if err != nil { - return -1, err - } - return int(id), nil -} - -// Copy creates a copy of the specified entry at the target path -func (c *Cache) Copy(ctx context.Context, storage interface{}, sourcePath, targetPath string) (int, error) { - storageID, err := toIntID(storage) - if err != nil { - return -1, err - } - source, err := c.Get(ctx, storageID, sourcePath) - if err != nil { - return -1, errors.Wrap(err, "could not find source") - } - - row := c.db.QueryRowContext(ctx, "SELECT mimetype FROM oc_mimetypes WHERE id=?", source.MimeType) - var mimetype string - err = row.Scan(&mimetype) - if err != nil { - return -1, errors.Wrap(err, "could not find source mimetype") - } - - data := map[string]interface{}{ - "path": targetPath, - "checksum": source.Checksum, - "mimetype": mimetype, - "permissions": source.Permissions, - "etag": source.Etag, - "size": source.Size, - "mtime": source.MTime, - "storage_mtime": source.StorageMTime, - "encrypted": source.Encrypted, - "unencrypted_size": source.UnencryptedSize, - } - return c.InsertOrUpdate(ctx, storage, data, false) -} - -// Move moves the specified entry to the target path -func (c *Cache) Move(ctx context.Context, storage interface{}, sourcePath, targetPath string) error { - storageID, err := toIntID(storage) - if err != nil { - return err - } - source, err := c.Get(ctx, storageID, sourcePath) - if err != nil { - return errors.Wrap(err, "could not find source") - } - newParentPath := strings.TrimRight(filepath.Dir(targetPath), "/") - newParent, err := c.Get(ctx, storageID, newParentPath) - if err != nil { - return errors.Wrap(err, "could not find new parent") - } - - tx, err := c.db.Begin() - if err != nil { - return err - } - defer func() { _ = tx.Rollback() }() - stmt, err := tx.Prepare("UPDATE oc_filecache SET parent=?, path=?, name=?, path_hash=? WHERE storage = ? AND fileid=?") - if err != nil { - return err - } - defer stmt.Close() - phashBytes := md5.Sum([]byte(targetPath)) - _, err = stmt.ExecContext(ctx, newParent.ID, targetPath, filepath.Base(targetPath), hex.EncodeToString(phashBytes[:]), storageID, source.ID) - if err != nil { - return err - } - - childRows, err := tx.QueryContext(ctx, "SELECT fileid, path FROM oc_filecache WHERE parent = ?", source.ID) - if err != nil { - return err - } - defer childRows.Close() - children := map[int]string{} - for childRows.Next() { - var ( - id int - path string - ) - err = childRows.Scan(&id, &path) - if err != nil { - return err - } - - children[id] = path - } - for id, path := range children { - path = strings.ReplaceAll(path, sourcePath, targetPath) - phashBytes = md5.Sum([]byte(path)) - _, err = stmt.ExecContext(ctx, source.ID, path, filepath.Base(path), hex.EncodeToString(phashBytes[:]), storageID, id) - if err != nil { - return err - } - } - - return tx.Commit() -} - -// Purge removes the specified storage/path from the cache without putting it into the trash -func (c *Cache) Purge(ctx context.Context, storage interface{}, path string) error { - storageID, err := toIntID(storage) - if err != nil { - return err - } - phashBytes := md5.Sum([]byte(path)) - phash := hex.EncodeToString(phashBytes[:]) - _, err = c.db.ExecContext(ctx, "DELETE FROM oc_filecache WHERE storage = ? and path_hash = ?", storageID, phash) - return err -} - -// Delete removes the specified storage/path from the cache -func (c *Cache) Delete(ctx context.Context, storage interface{}, user, path, trashPath string) error { - err := c.Move(ctx, storage, path, trashPath) - if err != nil { - return err - } - - re := regexp.MustCompile(`(.*)\.d(\d+)$`) - parts := re.FindStringSubmatch(filepath.Base(trashPath)) - - query := "INSERT INTO oc_files_trash(user,id,timestamp,location) VALUES(?,?,?,?)" - stmt, err := c.db.PrepareContext(ctx, query) - if err != nil { - return err - } - - relativeLocation, err := filepath.Rel("files/", filepath.Dir(path)) - if err != nil { - return err - } - _, err = stmt.ExecContext(ctx, user, filepath.Base(parts[1]), parts[2], relativeLocation) - if err != nil { - log.Err(err).Msg("could not store filecache item") - return err - } - - return nil -} - -// GetRecycleItem returns the specified recycle item -func (c *Cache) GetRecycleItem(ctx context.Context, user, path string, timestamp int) (*TrashItem, error) { - row := c.db.QueryRowContext(ctx, "SELECT auto_id, id, location FROM oc_files_trash WHERE id = ? AND user = ? AND timestamp = ?", path, user, timestamp) - var autoID int - var id, location string - err := row.Scan(&autoID, &id, &location) - if err != nil { - return nil, err - } - - return &TrashItem{ - ID: autoID, - Name: id, - User: user, - Path: location, - Timestamp: timestamp, - }, nil -} - -// EmptyRecycle clears the recycle bin for the given user -func (c *Cache) EmptyRecycle(ctx context.Context, user string) error { - _, err := c.db.ExecContext(ctx, "DELETE FROM oc_files_trash WHERE user = ?", user) - if err != nil { - return err - } - - storage, err := c.GetNumericStorageID(ctx, "home::"+user) - if err != nil { - return err - } - - _, err = c.db.ExecContext(ctx, "DELETE FROM oc_filecache WHERE storage = ? AND PATH LIKE ?", storage, "files_trashbin/%") - return err -} - -// DeleteRecycleItem deletes the specified item from the trash -func (c *Cache) DeleteRecycleItem(ctx context.Context, user, path string, timestamp int) error { - _, err := c.db.ExecContext(ctx, "DELETE FROM oc_files_trash WHERE id = ? AND user = ? AND timestamp = ?", path, user, timestamp) - return err -} - -// PurgeRecycleItem deletes the specified item from the filecache and the trash -func (c *Cache) PurgeRecycleItem(ctx context.Context, user, path string, timestamp int, isVersionFile bool) error { - row := c.db.QueryRowContext(ctx, "SELECT auto_id, location FROM oc_files_trash WHERE id = ? AND user = ? AND timestamp = ?", path, user, timestamp) - var autoID int - var location string - err := row.Scan(&autoID, &location) - if err != nil { - return err - } - - _, err = c.db.ExecContext(ctx, "DELETE FROM oc_files_trash WHERE auto_id=?", autoID) - if err != nil { - return err - } - - storage, err := c.GetNumericStorageID(ctx, "home::"+user) - if err != nil { - return err - } - trashType := "files" - if isVersionFile { - trashType = "versions" - } - item, err := c.Get(ctx, storage, filepath.Join("files_trashbin", trashType, path+".d"+strconv.Itoa(timestamp))) - if err != nil { - return err - } - _, err = c.db.ExecContext(ctx, "DELETE FROM oc_filecache WHERE fileid=? OR parent=?", item.ID, item.ID) - - return err -} - -// SetEtag set a new etag for the specified item -func (c *Cache) SetEtag(ctx context.Context, storage interface{}, path, etag string) error { - storageID, err := toIntID(storage) - if err != nil { - return err - } - source, err := c.Get(ctx, storageID, path) - if err != nil { - return errors.Wrap(err, "could not find source") - } - stmt, err := c.db.PrepareContext(ctx, "UPDATE oc_filecache SET etag=? WHERE storage = ? AND fileid=?") - if err != nil { - return err - } - _, err = stmt.ExecContext(ctx, etag, storageID, source.ID) - return err -} - -func (c *Cache) insertMimetype(ctx context.Context, tx *sql.Tx, mimetype string) error { - insertPart := func(v string) error { - stmt, err := tx.PrepareContext(ctx, "INSERT INTO oc_mimetypes(mimetype) VALUES(?)") - if err != nil { - return err - } - _, err = stmt.ExecContext(ctx, v) - if err != nil { - if strings.Contains(err.Error(), "UNIQUE") || strings.Contains(err.Error(), "Error 1062") { - return nil // Already exists - } - return err - } - return nil - } - parts := strings.Split(mimetype, "/") - err := insertPart(parts[0]) - if err != nil { - return err - } - return insertPart(mimetype) -} - -func toIntID(rid interface{}) (int, error) { - switch t := rid.(type) { - case int: - return t, nil - case string: - return strconv.Atoi(t) - default: - return -1, fmt.Errorf("invalid type") - } -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/filecache/test.db b/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/filecache/test.db deleted file mode 100644 index 913b70f8607..00000000000 Binary files a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/filecache/test.db and /dev/null differ diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/owncloudsql.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/owncloudsql.go deleted file mode 100644 index 80563f572f6..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/owncloudsql.go +++ /dev/null @@ -1,2149 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package owncloudsql - -import ( - "context" - "crypto/md5" - "crypto/sha1" - "database/sql" - "fmt" - "hash/adler32" - "io" - "net/url" - "os" - "path/filepath" - "regexp" - "strconv" - "strings" - "syscall" - "time" - - userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" - "github.com/mitchellh/mapstructure" - "github.com/pkg/errors" - "github.com/pkg/xattr" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" - - "github.com/owncloud/reva/v2/internal/grpc/services/storageprovider" - "github.com/owncloud/reva/v2/pkg/appctx" - "github.com/owncloud/reva/v2/pkg/conversions" - ctxpkg "github.com/owncloud/reva/v2/pkg/ctx" - "github.com/owncloud/reva/v2/pkg/errtypes" - "github.com/owncloud/reva/v2/pkg/events" - "github.com/owncloud/reva/v2/pkg/logger" - "github.com/owncloud/reva/v2/pkg/mime" - "github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool" - "github.com/owncloud/reva/v2/pkg/sharedconf" - "github.com/owncloud/reva/v2/pkg/storage" - "github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/filecache" - "github.com/owncloud/reva/v2/pkg/storage/fs/registry" - "github.com/owncloud/reva/v2/pkg/storage/utils/chunking" - "github.com/owncloud/reva/v2/pkg/storage/utils/templates" -) - -const ( - // Currently,extended file attributes have four separated - // namespaces (user, trusted, security and system) followed by a dot. - // A non root user can only manipulate the user. namespace, which is what - // we will use to store ownCloud specific metadata. To prevent name - // collisions with other apps We are going to introduce a sub namespace - // "user.oc." - ocPrefix string = "user.oc." - - mdPrefix string = ocPrefix + "md." // arbitrary metadata - favPrefix string = ocPrefix + "fav." // favorite flag, per user - etagPrefix string = ocPrefix + "etag." // allow overriding a calculated etag with one from the extended attributes - checksumsKey string = "http://owncloud.org/ns/checksums" -) - -var defaultPermissions *provider.ResourcePermissions = &provider.ResourcePermissions{ - // no permissions -} -var ownerPermissions *provider.ResourcePermissions = &provider.ResourcePermissions{ - // all permissions - AddGrant: true, - CreateContainer: true, - Delete: true, - GetPath: true, - GetQuota: true, - InitiateFileDownload: true, - InitiateFileUpload: true, - ListContainer: true, - ListFileVersions: true, - ListGrants: true, - ListRecycle: true, - Move: true, - PurgeRecycle: true, - RemoveGrant: true, - RestoreFileVersion: true, - RestoreRecycleItem: true, - Stat: true, - UpdateGrant: true, - DenyGrant: true, -} - -func init() { - registry.Register("owncloudsql", New) -} - -type config struct { - DataDirectory string `mapstructure:"datadirectory"` - UploadInfoDir string `mapstructure:"upload_info_dir"` - DeprecatedShareDirectory string `mapstructure:"sharedirectory"` - ShareFolder string `mapstructure:"share_folder"` - UserLayout string `mapstructure:"user_layout"` - EnableHome bool `mapstructure:"enable_home"` - UserProviderEndpoint string `mapstructure:"userprovidersvc"` - DbUsername string `mapstructure:"dbusername"` - DbPassword string `mapstructure:"dbpassword"` - DbHost string `mapstructure:"dbhost"` - DbPort int `mapstructure:"dbport"` - DbName string `mapstructure:"dbname"` -} - -func parseConfig(m map[string]interface{}) (*config, error) { - c := &config{} - if err := mapstructure.Decode(m, c); err != nil { - err = errors.Wrap(err, "error decoding conf") - return nil, err - } - return c, nil -} - -func (c *config) init(m map[string]interface{}) { - if c.UserLayout == "" { - c.UserLayout = "{{.Username}}" - } - if c.UploadInfoDir == "" { - c.UploadInfoDir = "/var/tmp/reva/uploadinfo" - } - // fallback for old config - if c.DeprecatedShareDirectory != "" { - c.ShareFolder = c.DeprecatedShareDirectory - } - if c.ShareFolder == "" { - c.ShareFolder = "/Shares" - } - // ensure share folder always starts with slash - c.ShareFolder = filepath.Join("/", c.ShareFolder) - - c.UserProviderEndpoint = sharedconf.GetGatewaySVC(c.UserProviderEndpoint) -} - -// New returns an implementation to of the storage.FS interface that talk to -// a local filesystem. -func New(m map[string]interface{}, _ events.Stream, _ *zerolog.Logger) (storage.FS, error) { - c, err := parseConfig(m) - if err != nil { - return nil, err - } - c.init(m) - - // c.DataDirectory should never end in / unless it is the root? - c.DataDirectory = filepath.Clean(c.DataDirectory) - - // create datadir if it does not exist - err = os.MkdirAll(c.DataDirectory, 0700) - if err != nil { - logger.New().Error().Err(err). - Str("path", c.DataDirectory). - Msg("could not create datadir") - } - - err = os.MkdirAll(c.UploadInfoDir, 0700) - if err != nil { - logger.New().Error().Err(err). - Str("path", c.UploadInfoDir). - Msg("could not create uploadinfo dir") - } - - dbSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", c.DbUsername, c.DbPassword, c.DbHost, c.DbPort, c.DbName) - filecache, err := filecache.NewMysql(dbSource) - if err != nil { - return nil, err - } - - return &owncloudsqlfs{ - c: c, - chunkHandler: chunking.NewChunkHandler(c.UploadInfoDir), - filecache: filecache, - }, nil -} - -type owncloudsqlfs struct { - c *config - chunkHandler *chunking.ChunkHandler - filecache *filecache.Cache -} - -func (fs *owncloudsqlfs) Shutdown(ctx context.Context) error { - return nil -} - -// owncloudsql stores files in the files subfolder -// the incoming path starts with /, so we need to insert the files subfolder into the path -// and prefix the data directory -// TODO the path handed to a storage provider should not contain the username -func (fs *owncloudsqlfs) toInternalPath(ctx context.Context, sp string) (ip string) { - if fs.c.EnableHome { - u := ctxpkg.ContextMustGetUser(ctx) - layout := templates.WithUser(u, fs.c.UserLayout) - ip = filepath.Join(fs.c.DataDirectory, layout, "files", sp) - } else { - // trim all / - sp = strings.Trim(sp, "/") - // p = "" or - // p = or - // p = /foo/bar.txt - segments := strings.SplitN(sp, "/", 2) - - if len(segments) == 1 && segments[0] == "" { - ip = fs.c.DataDirectory - return - } - - // parts[0] contains the username or userid. - u, err := fs.getUser(ctx, segments[0]) - if err != nil { - // TODO return invalid internal path? - return - } - layout := templates.WithUser(u, fs.c.UserLayout) - - if len(segments) == 1 { - // parts = "" - ip = filepath.Join(fs.c.DataDirectory, layout, "files") - } else { - // parts = "", "foo/bar.txt" - ip = filepath.Join(fs.c.DataDirectory, layout, "files", segments[1]) - } - - } - return -} - -// owncloudsql stores versions in the files_versions subfolder -// the incoming path starts with /, so we need to insert the files subfolder into the path -// and prefix the data directory -// TODO the path handed to a storage provider should not contain the username -func (fs *owncloudsqlfs) getVersionsPath(ctx context.Context, ip string) string { - // ip = /path/to/data//files/foo/bar.txt - // remove data dir - if fs.c.DataDirectory != "/" { - // fs.c.DataDirectory is a clean path, so it never ends in / - ip = strings.TrimPrefix(ip, fs.c.DataDirectory) - } - // ip = //files/foo/bar.txt - parts := strings.SplitN(ip, "/", 4) - - // parts[1] contains the username or userid. - u, err := fs.getUser(ctx, parts[1]) - if err != nil { - // TODO return invalid internal path? - return "" - } - layout := templates.WithUser(u, fs.c.UserLayout) - - switch len(parts) { - case 3: - // parts = "", "" - return filepath.Join(fs.c.DataDirectory, layout, "files_versions") - case 4: - // parts = "", "", "foo/bar.txt" - return filepath.Join(fs.c.DataDirectory, layout, "files_versions", parts[3]) - default: - return "" // TODO Must not happen? - } - -} - -// owncloudsql stores trashed items in the files_trashbin subfolder of a users home -func (fs *owncloudsqlfs) getRecyclePath(ctx context.Context) (string, error) { - u, ok := ctxpkg.ContextGetUser(ctx) - if !ok { - err := errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx") - return "", err - } - layout := templates.WithUser(u, fs.c.UserLayout) - return fs.getRecyclePathForUser(layout) -} - -func (fs *owncloudsqlfs) getRecyclePathForUser(user string) (string, error) { - return filepath.Join(fs.c.DataDirectory, user, "files_trashbin/files"), nil -} - -func (fs *owncloudsqlfs) getVersionRecyclePath(ctx context.Context) (string, error) { - u, ok := ctxpkg.ContextGetUser(ctx) - if !ok { - err := errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx") - return "", err - } - layout := templates.WithUser(u, fs.c.UserLayout) - return filepath.Join(fs.c.DataDirectory, layout, "files_trashbin/versions"), nil -} - -func (fs *owncloudsqlfs) toDatabasePath(ip string) string { - owner := fs.getOwner(ip) - trim := filepath.Join(fs.c.DataDirectory, owner) - p := strings.TrimPrefix(ip, trim) - p = strings.TrimPrefix(p, "/") - return p -} - -func (fs *owncloudsqlfs) toResourcePath(ip, owner string) string { - trim := filepath.Join(fs.c.DataDirectory, owner, "files") - p := strings.TrimPrefix(ip, trim) - p = strings.TrimPrefix(p, "/") - // root directory - if p == "" { - p = "." - } - return p -} - -func (fs *owncloudsqlfs) toStoragePath(ctx context.Context, ip string) (sp string) { - if fs.c.EnableHome { - u := ctxpkg.ContextMustGetUser(ctx) - layout := templates.WithUser(u, fs.c.UserLayout) - trim := filepath.Join(fs.c.DataDirectory, layout, "files") - sp = strings.TrimPrefix(ip, trim) - // root directory - if sp == "" { - sp = "/" - } - } else { - // ip = /data//files/foo/bar.txt - // remove data dir - if fs.c.DataDirectory != "/" { - // fs.c.DataDirectory is a clean path, so it never ends in / - ip = strings.TrimPrefix(ip, fs.c.DataDirectory) - // ip = //files/foo/bar.txt - } - - segments := strings.SplitN(ip, "/", 4) - // parts = "", "", "files", "foo/bar.txt" - switch len(segments) { - case 1: - sp = "/" - case 2: - sp = filepath.Join("/", segments[1]) - case 3: - sp = filepath.Join("/", segments[1]) - default: - sp = filepath.Join(segments[1], segments[3]) - } - } - log := appctx.GetLogger(ctx) - log.Debug().Str("driver", "owncloudsql").Str("ipath", ip).Str("spath", sp).Msg("toStoragePath") - return -} - -// TODO the owner needs to come from a different place -func (fs *owncloudsqlfs) getOwner(ip string) string { - ip = strings.TrimPrefix(ip, fs.c.DataDirectory) - parts := strings.SplitN(ip, "/", 3) - if len(parts) > 1 { - return parts[1] - } - return "" -} - -// TODO cache user lookup -func (fs *owncloudsqlfs) getUser(ctx context.Context, usernameOrID string) (id *userpb.User, err error) { - u := ctxpkg.ContextMustGetUser(ctx) - // check if username matches and id is set - if u.Username == usernameOrID && u.Id != nil && u.Id.OpaqueId != "" { - return u, nil - } - // check if userid matches and username is set - if u.Id != nil && u.Id.OpaqueId == usernameOrID && u.Username != "" { - return u, nil - } - // look up at the userprovider - - // parts[0] contains the username or userid. use user service to look up id - c, err := pool.GetUserProviderServiceClient(fs.c.UserProviderEndpoint) - if err != nil { - appctx.GetLogger(ctx). - Error().Err(err). - Str("userprovidersvc", fs.c.UserProviderEndpoint). - Str("usernameOrID", usernameOrID). - Msg("could not get user provider client") - return nil, err - } - res, err := c.GetUser(ctx, &userpb.GetUserRequest{ - UserId: &userpb.UserId{OpaqueId: usernameOrID}, - }) - if err != nil { - appctx.GetLogger(ctx). - Error().Err(err). - Str("userprovidersvc", fs.c.UserProviderEndpoint). - Str("usernameOrID", usernameOrID). - Msg("could not get user") - return nil, err - } - - if res.Status.Code == rpc.Code_CODE_NOT_FOUND { - appctx.GetLogger(ctx). - Error(). - Str("userprovidersvc", fs.c.UserProviderEndpoint). - Str("usernameOrID", usernameOrID). - Interface("status", res.Status). - Msg("user not found by id. Trying by name") - - var cres *userpb.GetUserByClaimResponse - cres, err = c.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{ - Claim: "username", - Value: usernameOrID, - }) - if err != nil { - appctx.GetLogger(ctx). - Error().Err(err). - Str("userprovidersvc", fs.c.UserProviderEndpoint). - Str("usernameOrID", usernameOrID). - Msg("could not get user by username") - return nil, err - } - if cres.Status.Code == rpc.Code_CODE_NOT_FOUND { - appctx.GetLogger(ctx). - Error(). - Str("userprovidersvc", fs.c.UserProviderEndpoint). - Str("usernameOrID", usernameOrID). - Interface("status", cres.Status). - Msg("user not found by username") - return nil, fmt.Errorf("user not found") - } - res.User = cres.User - res.Status = cres.Status - } - - if res.Status.Code != rpc.Code_CODE_OK { - appctx.GetLogger(ctx). - Error(). - Str("userprovidersvc", fs.c.UserProviderEndpoint). - Str("usernameOrID", usernameOrID). - Interface("status", res.Status). - Msg("user lookup failed") - return nil, fmt.Errorf("user lookup failed") - } - return res.User, nil -} - -// permissionSet returns the permission set for the current user -func (fs *owncloudsqlfs) permissionSet(ctx context.Context, owner *userpb.UserId) *provider.ResourcePermissions { - if owner == nil { - return &provider.ResourcePermissions{ - Stat: true, - } - } - u, ok := ctxpkg.ContextGetUser(ctx) - if !ok { - return &provider.ResourcePermissions{ - // no permissions - } - } - if u.Id == nil { - return &provider.ResourcePermissions{ - // no permissions - } - } - if u.Id.OpaqueId == owner.OpaqueId && u.Id.Idp == owner.Idp { - return &provider.ResourcePermissions{ - // owner has all permissions - AddGrant: true, - CreateContainer: true, - Delete: true, - GetPath: true, - GetQuota: true, - InitiateFileDownload: true, - InitiateFileUpload: true, - ListContainer: true, - ListFileVersions: true, - ListGrants: true, - ListRecycle: true, - Move: true, - PurgeRecycle: true, - RemoveGrant: true, - RestoreFileVersion: true, - RestoreRecycleItem: true, - Stat: true, - UpdateGrant: true, - } - } - // TODO fix permissions for share recipients by traversing reading acls up to the root? cache acls for the parent node and reuse it - return &provider.ResourcePermissions{ - AddGrant: true, - CreateContainer: true, - Delete: true, - GetPath: true, - GetQuota: true, - InitiateFileDownload: true, - InitiateFileUpload: true, - ListContainer: true, - ListFileVersions: true, - ListGrants: true, - ListRecycle: true, - Move: true, - PurgeRecycle: true, - RemoveGrant: true, - RestoreFileVersion: true, - RestoreRecycleItem: true, - Stat: true, - UpdateGrant: true, - } -} - -func (fs *owncloudsqlfs) getStorage(ctx context.Context, ip string) (int, error) { - return fs.filecache.GetNumericStorageID(ctx, "home::"+fs.getOwner(ip)) -} - -func (fs *owncloudsqlfs) getUserStorage(ctx context.Context, user string) (int, error) { - id, err := fs.filecache.GetNumericStorageID(ctx, "home::"+user) - if err != nil { - id, err = fs.filecache.CreateStorage(ctx, "home::"+user) - } - return id, err -} - -func (fs *owncloudsqlfs) convertToResourceInfo(ctx context.Context, entry *filecache.File, ip string, mdKeys []string) (*provider.ResourceInfo, error) { - mdKeysMap := make(map[string]struct{}) - for _, k := range mdKeys { - mdKeysMap[k] = struct{}{} - } - - var returnAllKeys bool - if _, ok := mdKeysMap["*"]; len(mdKeys) == 0 || ok { - returnAllKeys = true - } - owner := fs.getOwner(ip) - path := fs.toResourcePath(ip, owner) - isDir := entry.MimeTypeString == "httpd/unix-directory" - ri := &provider.ResourceInfo{ - Id: &provider.ResourceId{ - // return ownclouds numeric storage id as the space id! - SpaceId: strconv.Itoa(entry.Storage), OpaqueId: strconv.Itoa(entry.ID), - }, - Path: filepath.Base(path), // we currently only return the name, decomposedfs returns the path if the request was path based. is that even still possible? - Type: getResourceType(isDir), - Etag: entry.Etag, - MimeType: entry.MimeTypeString, - Size: uint64(entry.Size), - Mtime: &types.Timestamp{ - Seconds: uint64(entry.MTime), - }, - ArbitraryMetadata: &provider.ArbitraryMetadata{ - Metadata: map[string]string{}, // TODO aduffeck: which metadata needs to go in here? - }, - } - // omit parentid for root - if path != "." { - ri.Name = entry.Name - ri.ParentId = &provider.ResourceId{ - // return ownclouds numeric storage id as the space id! - SpaceId: strconv.Itoa(entry.Storage), OpaqueId: strconv.Itoa(entry.Parent), - } - } - - if owner, err := fs.getUser(ctx, owner); err == nil { - ri.Owner = owner.Id - } else { - appctx.GetLogger(ctx).Error().Err(err).Msg("error getting owner") - } - - ri.PermissionSet = fs.permissionSet(ctx, ri.Owner) - - // checksums - if !isDir { - if _, checksumRequested := mdKeysMap[checksumsKey]; returnAllKeys || checksumRequested { - // TODO which checksum was requested? sha1 adler32 or md5? for now hardcode sha1? - readChecksumIntoResourceChecksum(ctx, entry.Checksum, storageprovider.XSSHA1, ri) - readChecksumIntoOpaque(ctx, entry.Checksum, storageprovider.XSMD5, ri) - readChecksumIntoOpaque(ctx, ip, storageprovider.XSAdler32, ri) - } - } - - return ri, nil -} - -// GetPathByID returns the storage relative path for the file id, without the internal namespace -func (fs *owncloudsqlfs) GetPathByID(ctx context.Context, id *provider.ResourceId) (string, error) { - ip, err := fs.resolve(ctx, &provider.Reference{ResourceId: id}) - if err != nil { - return "", err - } - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.GetPath { - return "", errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return "", errtypes.NotFound(fs.toStoragePath(ctx, ip)) - } - return "", errors.Wrap(err, "owncloudsql: error reading permissions") - } - - return fs.toStoragePath(ctx, ip), nil -} - -// resolve takes in a request path or request id and converts it to an internal path. -func (fs *owncloudsqlfs) resolve(ctx context.Context, ref *provider.Reference) (string, error) { - - if ref.GetResourceId() != nil { - p, err := fs.filecache.Path(ctx, ref.GetResourceId().OpaqueId) - if err != nil { - return "", err - } - p = strings.TrimPrefix(p, "files/") - if !fs.c.EnableHome { - owner, err := fs.filecache.GetStorageOwnerByFileID(ctx, ref.GetResourceId().OpaqueId) - if err != nil { - return "", err - } - p = filepath.Join(owner, p) - } - if ref.GetPath() != "" { - p = filepath.Join(p, ref.GetPath()) - } - return fs.toInternalPath(ctx, p), nil - } - - if ref.GetPath() != "" { - return fs.toInternalPath(ctx, ref.GetPath()), nil - } - - // reference is invalid - return "", fmt.Errorf("invalid reference %+v", ref) -} - -func (fs *owncloudsqlfs) DenyGrant(ctx context.Context, ref *provider.Reference, g *provider.Grantee) error { - return errtypes.NotSupported("owncloudsqlfs: deny grant not supported") -} - -func (fs *owncloudsqlfs) AddGrant(ctx context.Context, ref *provider.Reference, g *provider.Grant) error { - return errtypes.NotSupported("owncloudsqlfs: add grant not supported") -} - -func (fs *owncloudsqlfs) readPermissions(ctx context.Context, ip string) (p *provider.ResourcePermissions, err error) { - u, ok := ctxpkg.ContextGetUser(ctx) - if !ok { - appctx.GetLogger(ctx).Debug().Str("ipath", ip).Msg("no user in context, returning default permissions") - return defaultPermissions, nil - } - // check if the current user is the owner - owner := fs.getOwner(ip) - if owner == u.Username { - appctx.GetLogger(ctx).Debug().Str("ipath", ip).Msg("user is owner, returning owner permissions") - return ownerPermissions, nil - } - - // otherwise this is a share - ownerStorageID, err := fs.filecache.GetNumericStorageID(ctx, "home::"+owner) - if err != nil { - return nil, err - } - entry, err := fs.filecache.Get(ctx, ownerStorageID, fs.toDatabasePath(ip)) - if err != nil { - return nil, err - } - perms, err := conversions.NewPermissions(entry.Permissions) - if err != nil { - return nil, err - } - return conversions.RoleFromOCSPermissions(perms, nil).CS3ResourcePermissions(), nil -} - -// The os not exists error is buried inside the xattr error, -// so we cannot just use os.IsNotExists(). -func isNotFound(err error) bool { - if xerr, ok := err.(*xattr.Error); ok { - if serr, ok2 := xerr.Err.(syscall.Errno); ok2 { - return serr == syscall.ENOENT - } - } - return false -} - -func (fs *owncloudsqlfs) ListGrants(ctx context.Context, ref *provider.Reference) (grants []*provider.Grant, err error) { - return []*provider.Grant{}, nil // nop -} - -func (fs *owncloudsqlfs) RemoveGrant(ctx context.Context, ref *provider.Reference, g *provider.Grant) (err error) { - return nil // nop -} - -func (fs *owncloudsqlfs) UpdateGrant(ctx context.Context, ref *provider.Reference, g *provider.Grant) error { - return nil // nop -} - -func (fs *owncloudsqlfs) CreateHome(ctx context.Context) error { - u, ok := ctxpkg.ContextGetUser(ctx) - if !ok { - err := errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx") - return err - } - return fs.createHomeForUser(ctx, templates.WithUser(u, fs.c.UserLayout)) -} - -func (fs *owncloudsqlfs) createHomeForUser(ctx context.Context, user string) error { - homePaths := []string{ - filepath.Join(fs.c.DataDirectory, user), - filepath.Join(fs.c.DataDirectory, user, "files"), - filepath.Join(fs.c.DataDirectory, user, "files_trashbin"), - filepath.Join(fs.c.DataDirectory, user, "files_trashbin/files"), - filepath.Join(fs.c.DataDirectory, user, "files_trashbin/versions"), - filepath.Join(fs.c.DataDirectory, user, "uploads"), - } - - storageID, err := fs.getUserStorage(ctx, user) - if err != nil { - return err - } - for _, v := range homePaths { - if err := os.MkdirAll(v, 0755); err != nil { - return errors.Wrap(err, "owncloudsql: error creating home path: "+v) - } - - fi, err := os.Stat(v) - if err != nil { - return err - } - data := map[string]interface{}{ - "path": fs.toDatabasePath(v), - "etag": calcEtag(ctx, fi), - "mimetype": "httpd/unix-directory", - "permissions": 31, // 1: READ, 2: UPDATE, 4: CREATE, 8: DELETE, 16: SHARE - } - - allowEmptyParent := v == filepath.Join(fs.c.DataDirectory, user) // the root doesn't have a parent - _, err = fs.filecache.InsertOrUpdate(ctx, storageID, data, allowEmptyParent) - if err != nil { - return err - } - } - return nil -} - -// If home is enabled, the relative home is always the empty string -func (fs *owncloudsqlfs) GetHome(ctx context.Context) (string, error) { - if !fs.c.EnableHome { - return "", errtypes.NotSupported("owncloudsql: get home not supported") - } - return "", nil -} - -func (fs *owncloudsqlfs) CreateDir(ctx context.Context, ref *provider.Reference) (_ *storage.CreateDirResult, err error) { - - ip, err := fs.resolve(ctx, ref) - if err != nil { - return nil, err - } - - // check permissions of parent dir - if perm, err := fs.readPermissions(ctx, filepath.Dir(ip)); err == nil { - if !perm.CreateContainer { - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.PreconditionFailed(ref.Path) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - if err = os.Mkdir(ip, 0700); err != nil { - if os.IsNotExist(err) { - return nil, errtypes.PreconditionFailed(ref.Path) - } - if os.IsExist(err) { - return nil, errtypes.AlreadyExists(ref.Path) - } - return nil, errors.Wrap(err, "owncloudsql: error creating dir "+fs.toStoragePath(ctx, filepath.Dir(ip))) - } - - fi, err := os.Stat(ip) - if err != nil { - return nil, err - } - mtime := time.Now().Unix() - - permissions := 31 // 1: READ, 2: UPDATE, 4: CREATE, 8: DELETE, 16: SHARE - if perm, err := fs.readPermissions(ctx, filepath.Dir(ip)); err == nil { - permissions = int(conversions.RoleFromResourcePermissions(perm, false).OCSPermissions()) // inherit permissions of parent - } - data := map[string]interface{}{ - "path": fs.toDatabasePath(ip), - "etag": calcEtag(ctx, fi), - "mimetype": "httpd/unix-directory", - "permissions": permissions, - "mtime": mtime, - "storage_mtime": mtime, - } - storageID, err := fs.getStorage(ctx, ip) - if err != nil { - return nil, err - } - _, err = fs.filecache.InsertOrUpdate(ctx, storageID, data, false) - if err != nil { - return nil, err - } - - if err := fs.propagate(ctx, filepath.Dir(ip)); err != nil { - return nil, err - } - return &storage.CreateDirResult{}, nil -} - -// TouchFile as defined in the storage.FS interface -func (fs *owncloudsqlfs) TouchFile(ctx context.Context, ref *provider.Reference, markprocessing bool, mtime string) (*storage.TouchFileResult, error) { - ip, err := fs.resolve(ctx, ref) - if err != nil { - return nil, err - } - - // check permissions of parent dir - parentPerms, err := fs.readPermissions(ctx, filepath.Dir(ip)) - if err == nil { - if !parentPerms.InitiateFileUpload { - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.NotFound(ref.Path) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - _, err = os.Create(ip) - if err != nil { - if os.IsNotExist(err) { - return nil, errtypes.NotFound(ref.Path) - } - // FIXME we also need already exists error, webdav expects 405 MethodNotAllowed - return nil, errors.Wrap(err, "owncloudsql: error creating file "+fs.toStoragePath(ctx, filepath.Dir(ip))) - } - - if err = os.Chmod(ip, 0700); err != nil { - return nil, errors.Wrap(err, "owncloudsql: error setting file permissions on "+fs.toStoragePath(ctx, filepath.Dir(ip))) - } - - fi, err := os.Stat(ip) - if err != nil { - return nil, err - } - storageMtime := time.Now().Unix() - mt := storageMtime - if mtime != "" { - t, err := strconv.Atoi(mtime) - if err != nil { - log.Info(). - Str("owncloudsql", ip). - Msg("error mtime conversion. mtine set to system time") - } - mt = time.Unix(int64(t), 0).Unix() - } - - data := map[string]interface{}{ - "path": fs.toDatabasePath(ip), - "etag": calcEtag(ctx, fi), - "mimetype": mime.Detect(false, ip), - "permissions": int(conversions.RoleFromResourcePermissions(parentPerms, false).OCSPermissions()), // inherit permissions of parent - "mtime": mt, - "storage_mtime": storageMtime, - } - storageID, err := fs.getStorage(ctx, ip) - if err != nil { - return nil, err - } - _, err = fs.filecache.InsertOrUpdate(ctx, storageID, data, false) - if err != nil { - return nil, err - } - - if err := fs.propagate(ctx, filepath.Dir(ip)); err != nil { - return nil, err - } - return &storage.TouchFileResult{}, nil -} - -func (fs *owncloudsqlfs) CreateReference(ctx context.Context, sp string, targetURI *url.URL) error { - return errtypes.NotSupported("owncloudsql: operation not supported") -} - -func (fs *owncloudsqlfs) setMtime(ctx context.Context, ip string, mtime string) error { - log := appctx.GetLogger(ctx) - if mt, err := parseMTime(mtime); err == nil { - // updating mtime also updates atime - if err := os.Chtimes(ip, mt, mt); err != nil { - log.Error().Err(err). - Str("ipath", ip). - Time("mtime", mt). - Msg("could not set mtime") - return errors.Wrap(err, "could not set mtime") - } - } else { - log.Error().Err(err). - Str("ipath", ip). - Str("mtime", mtime). - Msg("could not parse mtime") - return errors.Wrap(err, "could not parse mtime") - } - return nil -} -func (fs *owncloudsqlfs) SetArbitraryMetadata(ctx context.Context, ref *provider.Reference, md *provider.ArbitraryMetadata) (err error) { - log := appctx.GetLogger(ctx) - - var ip string - if ip, err = fs.resolve(ctx, ref); err != nil { - return errors.Wrap(err, "owncloudsql: error resolving reference") - } - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.InitiateFileUpload { // TODO add dedicated permission? - return errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - } - return errors.Wrap(err, "owncloudsql: error reading permissions") - } - - var fi os.FileInfo - fi, err = os.Stat(ip) - if err != nil { - if os.IsNotExist(err) { - return errtypes.NotFound(fs.toStoragePath(ctx, ip)) - } - return errors.Wrap(err, "owncloudsql: error stating "+ip) - } - - errs := []error{} - - if md.Metadata != nil { - if val, ok := md.Metadata["mtime"]; ok { - err := fs.setMtime(ctx, ip, val) - if err != nil { - errs = append(errs, errors.Wrap(err, "could not set mtime")) - } - // remove from metadata - delete(md.Metadata, "mtime") - } - // TODO(jfd) special handling for atime? - // TODO(jfd) allow setting birth time (btime)? - // TODO(jfd) any other metadata that is interesting? fileid? - if val, ok := md.Metadata["etag"]; ok { - etag := calcEtag(ctx, fi) - val = fmt.Sprintf("\"%s\"", strings.Trim(val, "\"")) - if etag == val { - log.Debug(). - Str("ipath", ip). - Str("etag", val). - Msg("ignoring request to update identical etag") - } else - // etag is only valid until the calculated etag changes - // TODO(jfd) cleanup in a batch job - if err := xattr.Set(ip, etagPrefix+etag, []byte(val)); err != nil { - log.Error().Err(err). - Str("ipath", ip). - Str("calcetag", etag). - Str("etag", val). - Msg("could not set etag") - errs = append(errs, errors.Wrap(err, "could not set etag")) - } - delete(md.Metadata, "etag") - } - if val, ok := md.Metadata["http://owncloud.org/ns/favorite"]; ok { - // TODO we should not mess with the user here ... the favorites is now a user specific property for a file - // that cannot be mapped to extended attributes without leaking who has marked a file as a favorite - // it is a specific case of a tag, which is user individual as well - // TODO there are different types of tags - // 1. public that are managed by everyone - // 2. private tags that are only visible to the user - // 3. system tags that are only visible to the system - // 4. group tags that are only visible to a group ... - // urgh ... well this can be solved using different namespaces - // 1. public = p: - // 2. private = u:: for user specific - // 3. system = s: for system - // 4. group = g:: - // 5. app? = a:: for apps? - // obviously this only is secure when the u/s/g/a namespaces are not accessible by users in the filesystem - // public tags can be mapped to extended attributes - if u, ok := ctxpkg.ContextGetUser(ctx); ok { - // the favorite flag is specific to the user, so we need to incorporate the userid - if uid := u.GetId(); uid != nil { - fa := fmt.Sprintf("%s%s@%s", favPrefix, uid.GetOpaqueId(), uid.GetIdp()) - if err := xattr.Set(ip, fa, []byte(val)); err != nil { - log.Error().Err(err). - Str("ipath", ip). - Interface("user", u). - Str("key", fa). - Msg("could not set favorite flag") - errs = append(errs, errors.Wrap(err, "could not set favorite flag")) - } - } else { - log.Error(). - Str("ipath", ip). - Interface("user", u). - Msg("user has no id") - errs = append(errs, errors.Wrap(errtypes.UserRequired("userrequired"), "user has no id")) - } - } else { - log.Error(). - Str("ipath", ip). - Interface("user", u). - Msg("error getting user from ctx") - errs = append(errs, errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx")) - } - // remove from metadata - delete(md.Metadata, "http://owncloud.org/ns/favorite") - } - } - for k, v := range md.Metadata { - if err := xattr.Set(ip, mdPrefix+k, []byte(v)); err != nil { - log.Error().Err(err). - Str("ipath", ip). - Str("key", k). - Str("val", v). - Msg("could not set metadata") - errs = append(errs, errors.Wrap(err, "could not set metadata")) - } - } - switch len(errs) { - case 0: - return fs.propagate(ctx, ip) - case 1: - return errs[0] - default: - // TODO how to return multiple errors? - return errors.New("multiple errors occurred, see log for details") - } -} - -func parseMTime(v string) (t time.Time, err error) { - p := strings.SplitN(v, ".", 2) - var sec, nsec int64 - if sec, err = strconv.ParseInt(p[0], 10, 64); err == nil { - if len(p) > 1 { - nsec, err = strconv.ParseInt(p[1], 10, 64) - } - } - return time.Unix(sec, nsec), err -} - -func (fs *owncloudsqlfs) UnsetArbitraryMetadata(ctx context.Context, ref *provider.Reference, keys []string) (err error) { - log := appctx.GetLogger(ctx) - - var ip string - if ip, err = fs.resolve(ctx, ref); err != nil { - return errors.Wrap(err, "owncloudsql: error resolving reference") - } - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.InitiateFileUpload { // TODO add dedicated permission? - return errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return errtypes.NotFound(fs.toStoragePath(ctx, ip)) - } - return errors.Wrap(err, "owncloudsql: error reading permissions") - } - - _, err = os.Stat(ip) - if err != nil { - if os.IsNotExist(err) { - return errtypes.NotFound(fs.toStoragePath(ctx, ip)) - } - return errors.Wrap(err, "owncloudsql: error stating "+ip) - } - - errs := []error{} - for _, k := range keys { - switch k { - case "http://owncloud.org/ns/favorite": - if u, ok := ctxpkg.ContextGetUser(ctx); ok { - // the favorite flag is specific to the user, so we need to incorporate the userid - if uid := u.GetId(); uid != nil { - fa := fmt.Sprintf("%s%s@%s", favPrefix, uid.GetOpaqueId(), uid.GetIdp()) - if err := xattr.Remove(ip, fa); err != nil { - log.Error().Err(err). - Str("ipath", ip). - Interface("user", u). - Str("key", fa). - Msg("could not unset favorite flag") - errs = append(errs, errors.Wrap(err, "could not unset favorite flag")) - } - } else { - log.Error(). - Str("ipath", ip). - Interface("user", u). - Msg("user has no id") - errs = append(errs, errors.Wrap(errtypes.UserRequired("userrequired"), "user has no id")) - } - } else { - log.Error(). - Str("ipath", ip). - Interface("user", u). - Msg("error getting user from ctx") - errs = append(errs, errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx")) - } - default: - if err = xattr.Remove(ip, mdPrefix+k); err != nil { - // a non-existing attribute will return an error, which we can ignore - // (using string compare because the error type is syscall.Errno and not wrapped/recognizable) - if e, ok := err.(*xattr.Error); !ok || !(e.Err.Error() == "no data available" || - // darwin - e.Err.Error() == "attribute not found") { - log.Error().Err(err). - Str("ipath", ip). - Str("key", k). - Msg("could not unset metadata") - errs = append(errs, errors.Wrap(err, "could not unset metadata")) - } - } - } - } - - switch len(errs) { - case 0: - return fs.propagate(ctx, ip) - case 1: - return errs[0] - default: - // TODO how to return multiple errors? - return errors.New("multiple errors occurred, see log for details") - } -} - -// GetLock returns an existing lock on the given reference -func (fs *owncloudsqlfs) GetLock(ctx context.Context, ref *provider.Reference) (*provider.Lock, error) { - return nil, errtypes.NotSupported("unimplemented") -} - -// SetLock puts a lock on the given reference -func (fs *owncloudsqlfs) SetLock(ctx context.Context, ref *provider.Reference, lock *provider.Lock) (*storage.SetLockResult, error) { - return nil, errtypes.NotSupported("unimplemented") -} - -// RefreshLock refreshes an existing lock on the given reference -func (fs *owncloudsqlfs) RefreshLock(ctx context.Context, ref *provider.Reference, lock *provider.Lock, existingLockID string) error { - return errtypes.NotSupported("unimplemented") -} - -// Unlock removes an existing lock from the given reference -func (fs *owncloudsqlfs) Unlock(ctx context.Context, ref *provider.Reference, lock *provider.Lock) (*storage.UnlockResult, error) { - return nil, errtypes.NotSupported("unimplemented") -} - -// Delete is actually only a move to trash -// -// This is a first optimistic approach. -// When a file has versions and we want to delete the file it could happen that -// the service crashes before all moves are finished. -// That would result in invalid state like the main files was moved but the -// versions were not. -// We will live with that compromise since this storage driver will be -// deprecated soon. -func (fs *owncloudsqlfs) Delete(ctx context.Context, ref *provider.Reference) (_ *storage.DeleteResult, err error) { - var ip string - if ip, err = fs.resolve(ctx, ref); err != nil { - return nil, errors.Wrap(err, "owncloudsql: error resolving reference") - } - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.Delete { - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - _, err = os.Stat(ip) - if err != nil { - if os.IsNotExist(err) { - return nil, errtypes.NotFound(fs.toStoragePath(ctx, ip)) - } - return nil, errors.Wrap(err, "owncloudsql: error stating "+ip) - } - - // Delete file into the owner's trash, not the user's (in case of shares) - rp, err := fs.getRecyclePathForUser(fs.getOwner(ip)) - if err != nil { - return nil, errors.Wrap(err, "owncloudsql: error resolving recycle path") - } - - if err := os.MkdirAll(rp, 0700); err != nil { - return nil, errors.Wrap(err, "owncloudsql: error creating trashbin dir "+rp) - } - - // ip is the path on disk ... we need only the path relative to root - origin := filepath.Dir(fs.toStoragePath(ctx, ip)) - - err = fs.trash(ctx, ip, rp, origin) - if err != nil { - return nil, errors.Wrapf(err, "owncloudsql: error deleting file %s", ip) - } - return &storage.DeleteResult{}, nil -} - -func (fs *owncloudsqlfs) trash(ctx context.Context, ip string, rp string, origin string) error { - // move to trash location - dtime := time.Now().Unix() - tgt := filepath.Join(rp, fmt.Sprintf("%s.d%d", filepath.Base(ip), dtime)) - if err := os.Rename(ip, tgt); err != nil { - if os.IsExist(err) { - // timestamp collision, try again with higher value: - dtime++ - tgt := filepath.Join(rp, fmt.Sprintf("%s.d%d", filepath.Base(ip), dtime)) - if err := os.Rename(ip, tgt); err != nil { - return errors.Wrap(err, "owncloudsql: could not move item to trash") - } - } - } - - storage, err := fs.getStorage(ctx, ip) - if err != nil { - return err - } - - tryDelete := func() error { - return fs.filecache.Delete(ctx, storage, fs.getOwner(ip), fs.toDatabasePath(ip), fs.toDatabasePath(tgt)) - } - err = tryDelete() - if err != nil { - err = fs.createHomeForUser(ctx, fs.getOwner(ip)) // Try setting up the owner's home (incl. trash) to fix the problem - if err != nil { - return err - } - err = tryDelete() - if err != nil { - return err - } - } - - err = fs.trashVersions(ctx, ip, origin, dtime) - if err != nil { - return errors.Wrapf(err, "owncloudsql: error deleting versions of file %s", ip) - } - - return fs.propagate(ctx, filepath.Dir(ip)) -} - -func (fs *owncloudsqlfs) trashVersions(ctx context.Context, ip string, origin string, dtime int64) error { - vp := fs.getVersionsPath(ctx, ip) - vrp, err := fs.getVersionRecyclePath(ctx) - if err != nil { - return errors.Wrap(err, "error resolving versions recycle path") - } - - if err := os.MkdirAll(vrp, 0700); err != nil { - return errors.Wrap(err, "owncloudsql: error creating trashbin dir "+vrp) - } - - // Ignore error since the only possible error is malformed pattern. - versions, _ := filepath.Glob(vp + ".v*") - storage, err := fs.getStorage(ctx, ip) - if err != nil { - return err - } - for _, v := range versions { - tgt := filepath.Join(vrp, fmt.Sprintf("%s.d%d", filepath.Base(v), dtime)) - if err := os.Rename(v, tgt); err != nil { - if os.IsExist(err) { - // timestamp collision, try again with higher value: - dtime++ - tgt := filepath.Join(vrp, fmt.Sprintf("%s.d%d", filepath.Base(ip), dtime)) - if err := os.Rename(ip, tgt); err != nil { - return errors.Wrap(err, "owncloudsql: could not move item to trash") - } - } - } - if err != nil { - return errors.Wrap(err, "owncloudsql: error deleting file "+v) - } - err = fs.filecache.Move(ctx, storage, fs.toDatabasePath(v), fs.toDatabasePath(tgt)) - if err != nil { - return errors.Wrap(err, "owncloudsql: error deleting file "+v) - } - } - return nil -} - -func (fs *owncloudsqlfs) Move(ctx context.Context, oldRef, newRef *provider.Reference) (*storage.MoveResult, error) { - oldIP, err := fs.resolve(ctx, oldRef) - if err != nil { - return nil, errors.Wrap(err, "owncloudsql: error resolving reference") - } - - // check permissions - if perm, err := fs.readPermissions(ctx, oldIP); err == nil { - if !perm.Move { // TODO add dedicated permission? - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(oldIP))) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - newIP, err := fs.resolve(ctx, newRef) - if err != nil { - return nil, errors.Wrap(err, "owncloudsql: error resolving reference") - } - - // TODO check target permissions ... if it exists - stor, err := fs.getStorage(ctx, oldIP) - if err != nil { - return nil, err - } - err = fs.filecache.Move(ctx, stor, fs.toDatabasePath(oldIP), fs.toDatabasePath(newIP)) - if err != nil { - return nil, err - } - if err = os.Rename(oldIP, newIP); err != nil { - return nil, errors.Wrap(err, "owncloudsql: error moving "+oldIP+" to "+newIP) - } - - if err := fs.propagate(ctx, newIP); err != nil { - return nil, err - } - if filepath.Dir(newIP) != filepath.Dir(oldIP) { - if err := fs.propagate(ctx, filepath.Dir(oldIP)); err != nil { - return nil, err - } - } - return &storage.MoveResult{}, nil -} - -func (fs *owncloudsqlfs) GetMD(ctx context.Context, ref *provider.Reference, mdKeys []string, fieldMask []string) (*provider.ResourceInfo, error) { - ip, err := fs.resolve(ctx, ref) - if err != nil { - // TODO return correct errtype - if _, ok := err.(errtypes.IsNotFound); ok { - return nil, err - } - return nil, errors.Wrap(err, "owncloudsql: error resolving reference") - } - p := fs.toStoragePath(ctx, ip) - - // If GetMD is called for a path shared with the user then the path is - // already wrapped. (fs.resolve wraps the path) - if strings.HasPrefix(p, fs.c.DataDirectory) { - ip = p - } - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.Stat { - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - ownerStorageID, err := fs.filecache.GetNumericStorageID(ctx, "home::"+fs.getOwner(ip)) - if err != nil { - return nil, err - } - entry, err := fs.filecache.Get(ctx, ownerStorageID, fs.toDatabasePath(ip)) - switch { - case err == sql.ErrNoRows: - return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - case err != nil: - return nil, err - } - - return fs.convertToResourceInfo(ctx, entry, ip, mdKeys) -} - -func (fs *owncloudsqlfs) ListFolder(ctx context.Context, ref *provider.Reference, mdKeys, fieldMask []string) ([]*provider.ResourceInfo, error) { - log := appctx.GetLogger(ctx) - - ip, err := fs.resolve(ctx, ref) - if err != nil { - return nil, errors.Wrap(err, "owncloudsql: error resolving reference") - } - sp := fs.toStoragePath(ctx, ip) - - if fs.c.EnableHome { - log.Debug().Msg("home enabled") - if strings.HasPrefix(sp, "/") { - // permissions checked in listWithHome - return fs.listWithHome(ctx, "/", sp, mdKeys) - } - } - - log.Debug().Msg("list with nominal home") - // permissions checked in listWithNominalHome - return fs.listWithNominalHome(ctx, sp, mdKeys) -} - -func (fs *owncloudsqlfs) listWithNominalHome(ctx context.Context, ip string, mdKeys []string) ([]*provider.ResourceInfo, error) { - - // If a user wants to list a folder shared with him the path will already - // be wrapped with the files directory path of the share owner. - // In that case we don't want to wrap the path again. - if !strings.HasPrefix(ip, fs.c.DataDirectory) { - ip = fs.toInternalPath(ctx, ip) - } - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.ListContainer { - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - storage, err := fs.getStorage(ctx, ip) - if err != nil { - return nil, err - } - entries, err := fs.filecache.List(ctx, storage, fs.toDatabasePath(ip)+"/") - if err != nil { - return nil, errors.Wrapf(err, "owncloudsql: error listing %s", ip) - } - owner := fs.getOwner(ip) - finfos := []*provider.ResourceInfo{} - for _, entry := range entries { - cp := filepath.Join(fs.c.DataDirectory, owner, entry.Path) - m, err := fs.convertToResourceInfo(ctx, entry, cp, mdKeys) - if err != nil { - appctx.GetLogger(ctx).Error().Err(err).Str("path", cp).Msg("error converting to a resource info") - } - finfos = append(finfos, m) - } - return finfos, nil -} - -func (fs *owncloudsqlfs) listWithHome(ctx context.Context, home, p string, mdKeys []string) ([]*provider.ResourceInfo, error) { - log := appctx.GetLogger(ctx) - if p == home { - log.Debug().Msg("listing home") - return fs.listHome(ctx, home, mdKeys) - } - - log.Debug().Msg("listing nominal home") - return fs.listWithNominalHome(ctx, p, mdKeys) -} - -func (fs *owncloudsqlfs) listHome(ctx context.Context, home string, mdKeys []string) ([]*provider.ResourceInfo, error) { - // list files - ip := fs.toInternalPath(ctx, home) - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.ListContainer { - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - storage, err := fs.getStorage(ctx, ip) - if err != nil { - return nil, err - } - entries, err := fs.filecache.List(ctx, storage, fs.toDatabasePath(ip)+"/") - if err != nil { - return nil, errors.Wrapf(err, "owncloudsql: error listing %s", ip) - } - owner := fs.getOwner(ip) - finfos := []*provider.ResourceInfo{} - for _, entry := range entries { - cp := filepath.Join(fs.c.DataDirectory, owner, entry.Path) - m, err := fs.convertToResourceInfo(ctx, entry, cp, mdKeys) - if err != nil { - appctx.GetLogger(ctx).Error().Err(err).Str("path", cp).Msg("error converting to a resource info") - } - finfos = append(finfos, m) - } - return finfos, nil -} - -func (fs *owncloudsqlfs) archiveRevision(ctx context.Context, vbp string, ip string) error { - // move existing file to versions dir - vp := fmt.Sprintf("%s.v%d", vbp, time.Now().Unix()) - if err := os.MkdirAll(filepath.Dir(vp), 0700); err != nil { - return errors.Wrap(err, "owncloudsql: error creating versions dir "+vp) - } - - // TODO(jfd): make sure rename is atomic, missing fsync ... - if err := os.Rename(ip, vp); err != nil { - return errors.Wrap(err, "owncloudsql: error renaming from "+ip+" to "+vp) - } - - storage, err := fs.getStorage(ctx, ip) - if err != nil { - return err - } - - vdp := fs.toDatabasePath(vp) - basePath := strings.TrimSuffix(vp, vdp) - parts := strings.Split(filepath.Dir(vdp), "/") - walkPath := "" - for i := 0; i < len(parts); i++ { - walkPath = filepath.Join(walkPath, parts[i]) - _, err := fs.filecache.Get(ctx, storage, walkPath) - if err == nil { - continue - } - - fi, err := os.Stat(filepath.Join(basePath, walkPath)) - if err != nil { - return errors.Wrap(err, "could not stat parent version directory") - } - data := map[string]interface{}{ - "path": walkPath, - "mimetype": "httpd/unix-directory", - "etag": calcEtag(ctx, fi), - "permissions": 31, // 1: READ, 2: UPDATE, 4: CREATE, 8: DELETE, 16: SHARE - } - - _, err = fs.filecache.InsertOrUpdate(ctx, storage, data, false) - if err != nil { - return errors.Wrap(err, "could not create parent version directory") - } - } - _, err = fs.filecache.Copy(ctx, storage, fs.toDatabasePath(ip), vdp) - return err -} - -func (fs *owncloudsqlfs) Download(ctx context.Context, ref *provider.Reference, openReaderfunc func(*provider.ResourceInfo) bool) (*provider.ResourceInfo, io.ReadCloser, error) { - ip, err := fs.resolve(ctx, ref) - if err != nil { - return nil, nil, errors.Wrap(err, "owncloudsql: error resolving reference") - } - p := fs.toStoragePath(ctx, ip) - - // If Download is called for a path shared with the user then the path is - // already wrapped. (fs.resolve wraps the path) - if strings.HasPrefix(p, fs.c.DataDirectory) { - ip = p - } - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.InitiateFileDownload { - return nil, nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - } - return nil, nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - ownerStorageID, err := fs.filecache.GetNumericStorageID(ctx, "home::"+fs.getOwner(ip)) - if err != nil { - return nil, nil, err - } - entry, err := fs.filecache.Get(ctx, ownerStorageID, fs.toDatabasePath(ip)) - switch { - case err == sql.ErrNoRows: - return nil, nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - case err != nil: - return nil, nil, err - } - - md, err := fs.convertToResourceInfo(ctx, entry, ip, nil) - if err != nil { - return nil, nil, err - } - - if !openReaderfunc(md) { - return md, nil, nil - } - - r, err := os.Open(ip) - if err != nil { - if os.IsNotExist(err) { - return nil, nil, errtypes.NotFound(fs.toStoragePath(ctx, ip)) - } - return nil, nil, errors.Wrap(err, "owncloudsql: error reading "+ip) - } - return md, r, nil -} - -func (fs *owncloudsqlfs) ListRevisions(ctx context.Context, ref *provider.Reference) ([]*provider.FileVersion, error) { - ip, err := fs.resolve(ctx, ref) - if err != nil { - return nil, errors.Wrap(err, "owncloudsql: error resolving reference") - } - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.ListFileVersions { - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - vp := fs.getVersionsPath(ctx, ip) - bn := filepath.Base(ip) - storageID, err := fs.getStorage(ctx, ip) - if err != nil { - return nil, err - } - entries, err := fs.filecache.List(ctx, storageID, filepath.Dir(fs.toDatabasePath(vp))+"/") - if err != nil { - return nil, err - } - revisions := []*provider.FileVersion{} - for _, entry := range entries { - if strings.HasPrefix(entry.Name, bn) { - // versions have filename.ext.v12345678 - version := entry.Name[len(bn)+2:] // truncate ".v" to get version mtime - mtime, err := strconv.Atoi(version) - if err != nil { - log := appctx.GetLogger(ctx) - log.Error().Err(err).Str("path", entry.Name).Msg("invalid version mtime") - return nil, err - } - revisions = append(revisions, &provider.FileVersion{ - Key: version, - Size: uint64(entry.Size), - Mtime: uint64(mtime), - Etag: entry.Etag, - }) - } - } - - return revisions, nil -} - -func (fs *owncloudsqlfs) DownloadRevision(ctx context.Context, ref *provider.Reference, revisionKey string, openReaderfunc func(*provider.ResourceInfo) bool) (*provider.ResourceInfo, io.ReadCloser, error) { - return nil, nil, errtypes.NotSupported("download revision") -} - -func (fs *owncloudsqlfs) RestoreRevision(ctx context.Context, ref *provider.Reference, revisionKey string) (*storage.RestoreRevisionResult, error) { - ip, err := fs.resolve(ctx, ref) - if err != nil { - return nil, errors.Wrap(err, "owncloudsql: error resolving reference") - } - - // check permissions - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.RestoreFileVersion { - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.NotFound(fs.toStoragePath(ctx, filepath.Dir(ip))) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - - vp := fs.getVersionsPath(ctx, ip) - rp := vp + ".v" + revisionKey - - // check revision exists - rs, err := os.Stat(rp) - if err != nil { - return nil, err - } - - if !rs.Mode().IsRegular() { - return nil, fmt.Errorf("%s is not a regular file", rp) - } - - source, err := os.Open(rp) - if err != nil { - return nil, err - } - defer source.Close() - - // destination should be available, otherwise we could not have navigated to its revisions - if err := fs.archiveRevision(ctx, fs.getVersionsPath(ctx, ip), ip); err != nil { - return nil, err - } - - destination, err := os.Create(ip) - if err != nil { - // TODO(jfd) bring back revision in case sth goes wrong? - return nil, err - } - defer destination.Close() - - _, err = io.Copy(destination, source) - if err != nil { - return nil, err - } - - sha1h, md5h, adler32h, err := fs.HashFile(ip) - if err != nil { - log.Err(err).Msg("owncloudsql: could not open file for checksumming") - } - fi, err := os.Stat(ip) - if err != nil { - return nil, err - } - mtime := time.Now().Unix() - data := map[string]interface{}{ - "path": fs.toDatabasePath(ip), - "checksum": fmt.Sprintf("SHA1:%032x MD5:%032x ADLER32:%032x", sha1h, md5h, adler32h), - "etag": calcEtag(ctx, fi), - "size": fi.Size(), - "mimetype": mime.Detect(false, ip), - "mtime": mtime, - "storage_mtime": mtime, - } - storageID, err := fs.getStorage(ctx, ip) - if err != nil { - return nil, err - } - _, err = fs.filecache.InsertOrUpdate(ctx, storageID, data, false) - if err != nil { - return nil, err - } - - // TODO(jfd) bring back revision in case sth goes wrong? - if err := fs.propagate(ctx, ip); err != nil { - return nil, err - } - return &storage.RestoreRevisionResult{}, nil -} - -func (fs *owncloudsqlfs) PurgeRecycleItem(ctx context.Context, ref *provider.Reference, key, relativePath string) error { - rp, err := fs.getRecyclePath(ctx) - if err != nil { - return errors.Wrap(err, "owncloudsql: error resolving recycle path") - } - vp := filepath.Join(filepath.Dir(rp), "versions") - ip := filepath.Join(rp, filepath.Clean(key)) - // TODO check permission? - - // check permissions - /* are they stored in the trash? - if perm, err := fs.readPermissions(ctx, ip); err == nil { - if !perm.ListContainer { - return nil, errtypes.PermissionDenied("") - } - } else { - if isNotFound(err) { - return nil, errtypes.NotFound(fs.unwrap(ctx, filepath.Dir(ip))) - } - return nil, errors.Wrap(err, "owncloudsql: error reading permissions") - } - */ - - err = os.RemoveAll(ip) - if err != nil { - return errors.Wrap(err, "owncloudsql: error deleting recycle item") - } - base, ttime, err := splitTrashKey(key) - if err != nil { - return err - } - err = fs.filecache.PurgeRecycleItem(ctx, ctxpkg.ContextMustGetUser(ctx).Username, base, ttime, false) - if err != nil { - return err - } - - versionsGlob := filepath.Join(vp, base+".v*.d"+strconv.Itoa(ttime)) - versionFiles, err := filepath.Glob(versionsGlob) - if err != nil { - return errors.Wrap(err, "owncloudsql: error listing recycle item versions") - } - storageID, err := fs.getStorage(ctx, ip) - if err != nil { - return err - } - for _, versionFile := range versionFiles { - err = os.Remove(versionFile) - if err != nil { - return errors.Wrap(err, "owncloudsql: error deleting recycle item versions") - } - err = fs.filecache.Purge(ctx, storageID, fs.toDatabasePath(versionFile)) - if err != nil { - return err - } - } - - // TODO delete keyfiles, keys, share-keys - return nil -} - -func (fs *owncloudsqlfs) EmptyRecycle(ctx context.Context, ref *provider.Reference) error { - // TODO check permission? on what? user must be the owner - rp, err := fs.getRecyclePath(ctx) - if err != nil { - return errors.Wrap(err, "owncloudsql: error resolving recycle path") - } - err = os.RemoveAll(rp) - if err != nil { - return errors.Wrap(err, "owncloudsql: error deleting recycle files") - } - err = os.RemoveAll(filepath.Join(filepath.Dir(rp), "versions")) - if err != nil { - return errors.Wrap(err, "owncloudsql: error deleting recycle files versions") - } - - u := ctxpkg.ContextMustGetUser(ctx) - err = fs.filecache.EmptyRecycle(ctx, u.Username) - if err != nil { - return errors.Wrap(err, "owncloudsql: error deleting recycle items from the database") - } - - // TODO delete keyfiles, keys, share-keys ... or just everything? - return nil -} - -func splitTrashKey(key string) (string, int, error) { - // trashbin items have filename.ext.d12345678 - suffix := filepath.Ext(key) - if len(suffix) == 0 || !strings.HasPrefix(suffix, ".d") { - return "", -1, fmt.Errorf("invalid suffix") - } - trashtime := suffix[2:] // truncate "d" to get trashbin time - ttime, err := strconv.Atoi(trashtime) - if err != nil { - return "", -1, fmt.Errorf("invalid suffix") - } - return strings.TrimSuffix(filepath.Base(key), suffix), ttime, nil -} - -func (fs *owncloudsqlfs) convertToRecycleItem(ctx context.Context, md os.FileInfo) *provider.RecycleItem { - base, ttime, err := splitTrashKey(md.Name()) - if err != nil { - log := appctx.GetLogger(ctx) - log.Error().Str("path", md.Name()).Msg("invalid trash item key") - } - - u := ctxpkg.ContextMustGetUser(ctx) - item, err := fs.filecache.GetRecycleItem(ctx, u.Username, base, ttime) - if err != nil { - log := appctx.GetLogger(ctx) - log.Error().Err(err).Str("path", md.Name()).Msg("could not get trash item") - return nil - } - - // ownCloud 10 stores the parent dir of the deleted item as the location in the oc_files_trashbin table - // we use extended attributes for original location, but also only the parent location, which is why - // we need to join and trim the path when listing it - originalPath := filepath.Join(item.Path, base) - - return &provider.RecycleItem{ - Type: getResourceType(md.IsDir()), - Key: md.Name(), - // TODO do we need to prefix the path? it should be relative to this storage root, right? - Ref: &provider.Reference{Path: originalPath}, - Size: uint64(md.Size()), - DeletionTime: &types.Timestamp{ - Seconds: uint64(ttime), - // no nanos available - }, - } -} - -func (fs *owncloudsqlfs) ListRecycle(ctx context.Context, ref *provider.Reference, key, relativePath string) ([]*provider.RecycleItem, error) { - // TODO check permission? on what? user must be the owner? - rp, err := fs.getRecyclePath(ctx) - if err != nil { - return nil, errors.Wrap(err, "owncloudsql: error resolving recycle path") - } - - // list files folder - mds, err := os.ReadDir(rp) - if err != nil { - log := appctx.GetLogger(ctx) - log.Debug().Err(err).Str("path", rp).Msg("trash not readable") - // TODO jfd only ignore not found errors - return []*provider.RecycleItem{}, nil - } - // TODO (jfd) limit and offset - items := []*provider.RecycleItem{} - for i := range mds { - mdsInfo, _ := mds[i].Info() - ri := fs.convertToRecycleItem(ctx, mdsInfo) - if ri != nil { - items = append(items, ri) - } - - } - return items, nil -} - -func (fs *owncloudsqlfs) RestoreRecycleItem(ctx context.Context, ref *provider.Reference, key, relativePath string, restoreRef *provider.Reference) (*storage.RestoreRecycleItemResult, error) { - log := appctx.GetLogger(ctx) - - base, ttime, err := splitTrashKey(key) - if err != nil { - log.Error().Str("path", key).Msg("invalid trash item key") - return nil, fmt.Errorf("invalid trash item suffix") - } - - recyclePath, err := fs.getRecyclePath(ctx) - if err != nil { - return nil, errors.Wrap(err, "owncloudsql: error resolving recycle path") - } - src := filepath.Join(recyclePath, filepath.Clean(key)) - - if restoreRef.Path == "" { - u := ctxpkg.ContextMustGetUser(ctx) - item, err := fs.filecache.GetRecycleItem(ctx, u.Username, base, ttime) - if err != nil { - log := appctx.GetLogger(ctx) - log.Error().Err(err).Str("path", key).Msg("could not get trash item") - return nil, err - } - restoreRef.Path = filepath.Join(item.Path, item.Name) - } - - tgt := fs.toInternalPath(ctx, restoreRef.Path) - // move back to original location - if err := os.Rename(src, tgt); err != nil { - log.Error().Err(err).Str("key", key).Str("restorePath", restoreRef.Path).Str("src", src).Str("tgt", tgt).Msg("could not restore item") - return nil, errors.Wrap(err, "owncloudsql: could not restore item") - } - - storageID, err := fs.getStorage(ctx, src) - if err != nil { - return nil, err - } - err = fs.filecache.Move(ctx, storageID, fs.toDatabasePath(src), fs.toDatabasePath(tgt)) - if err != nil { - return nil, err - } - err = fs.filecache.DeleteRecycleItem(ctx, ctxpkg.ContextMustGetUser(ctx).Username, base, ttime) - if err != nil { - return nil, err - } - err = fs.RestoreRecycleItemVersions(ctx, key, tgt) - if err != nil { - return nil, err - } - - if err := fs.propagate(ctx, tgt); err != nil { - return nil, err - } - return &storage.RestoreRecycleItemResult{}, nil -} - -func (fs *owncloudsqlfs) RestoreRecycleItemVersions(ctx context.Context, key, target string) error { - base, ttime, err := splitTrashKey(key) - if err != nil { - return fmt.Errorf("invalid trash item suffix") - } - storage, err := fs.getStorage(ctx, target) - if err != nil { - return err - } - - recyclePath, err := fs.getRecyclePath(ctx) - if err != nil { - return errors.Wrap(err, "owncloudsql: error resolving recycle path") - } - versionsRecyclePath := filepath.Join(filepath.Dir(recyclePath), "versions") - - // Restore versions - deleteSuffix := ".d" + strconv.Itoa(ttime) - versionsGlob := filepath.Join(versionsRecyclePath, base+".v*"+deleteSuffix) - versionFiles, err := filepath.Glob(versionsGlob) - versionsRoot := filepath.Dir(fs.getVersionsPath(ctx, target)) - - if err != nil { - return errors.Wrap(err, "owncloudsql: error listing recycle item versions") - } - for _, versionFile := range versionFiles { - versionBase := strings.TrimSuffix(filepath.Base(versionFile), deleteSuffix) - versionsRestorePath := filepath.Join(versionsRoot, versionBase) - if err = os.Rename(versionFile, versionsRestorePath); err != nil { - return errors.Wrap(err, "owncloudsql: could not restore version file") - } - err = fs.filecache.Move(ctx, storage, fs.toDatabasePath(versionFile), fs.toDatabasePath(versionsRestorePath)) - if err != nil { - return err - } - } - return nil -} - -func (fs *owncloudsqlfs) propagate(ctx context.Context, leafPath string) error { - var root string - if fs.c.EnableHome { - root = filepath.Clean(fs.toInternalPath(ctx, "/")) - } else { - owner := fs.getOwner(leafPath) - root = filepath.Clean(fs.toInternalPath(ctx, owner)) - } - versionsRoot := filepath.Join(filepath.Dir(root), "files_versions") - if !strings.HasPrefix(leafPath, root) { - err := errors.New("internal path outside root") - appctx.GetLogger(ctx).Error(). - Err(err). - Str("leafPath", leafPath). - Str("root", root). - Msg("could not propagate change") - return err - } - - fi, err := os.Stat(leafPath) - if err != nil { - appctx.GetLogger(ctx).Error(). - Err(err). - Str("leafPath", leafPath). - Str("root", root). - Msg("could not propagate change") - return err - } - - storageID, err := fs.getStorage(ctx, leafPath) - if err != nil { - return err - } - - currentPath := filepath.Clean(leafPath) - for currentPath != root && currentPath != versionsRoot { - appctx.GetLogger(ctx).Debug(). - Str("leafPath", leafPath). - Str("currentPath", currentPath). - Msg("propagating change") - parentFi, err := os.Stat(currentPath) - if err != nil { - return err - } - if fi.ModTime().UnixNano() > parentFi.ModTime().UnixNano() { - if err := os.Chtimes(currentPath, fi.ModTime(), fi.ModTime()); err != nil { - appctx.GetLogger(ctx).Error(). - Err(err). - Str("leafPath", leafPath). - Str("currentPath", currentPath). - Msg("could not propagate change") - return err - } - } - fi, err = os.Stat(currentPath) - if err != nil { - return err - } - etag := calcEtag(ctx, fi) - if err := fs.filecache.SetEtag(ctx, storageID, fs.toDatabasePath(currentPath), etag); err != nil { - appctx.GetLogger(ctx).Error(). - Err(err). - Str("leafPath", leafPath). - Str("currentPath", currentPath). - Msg("could not set etag") - return err - } - - currentPath = filepath.Dir(currentPath) - } - return nil -} - -func (fs *owncloudsqlfs) HashFile(path string) (string, string, string, error) { - sha1h := sha1.New() - md5h := md5.New() - adler32h := adler32.New() - { - f, err := os.Open(path) - if err != nil { - return "", "", "", errors.Wrap(err, "owncloudsql: could not copy bytes for checksumming") - } - defer f.Close() - - r1 := io.TeeReader(f, sha1h) - r2 := io.TeeReader(r1, md5h) - - if _, err := io.Copy(adler32h, r2); err != nil { - return "", "", "", errors.Wrap(err, "owncloudsql: could not copy bytes for checksumming") - } - - return string(sha1h.Sum(nil)), string(md5h.Sum(nil)), string(adler32h.Sum(nil)), nil - } -} - -func readChecksumIntoResourceChecksum(ctx context.Context, checksums, algo string, ri *provider.ResourceInfo) { - re := regexp.MustCompile(strings.ToUpper(algo) + `:(.*)`) - matches := re.FindStringSubmatch(checksums) - if len(matches) < 2 { - appctx.GetLogger(ctx). - Debug(). - Str("nodepath", checksums). - Str("algorithm", algo). - Msg("checksum not set") - } else { - ri.Checksum = &provider.ResourceChecksum{ - Type: storageprovider.PKG2GRPCXS(algo), - Sum: matches[1], - } - } -} - -func readChecksumIntoOpaque(ctx context.Context, checksums, algo string, ri *provider.ResourceInfo) { - re := regexp.MustCompile(strings.ToUpper(algo) + `:(.*)`) - matches := re.FindStringSubmatch(checksums) - if len(matches) < 2 { - appctx.GetLogger(ctx). - Debug(). - Str("nodepath", checksums). - Str("algorithm", algo). - Msg("checksum not set") - } else { - if ri.Opaque == nil { - ri.Opaque = &types.Opaque{ - Map: map[string]*types.OpaqueEntry{}, - } - } - ri.Opaque.Map[algo] = &types.OpaqueEntry{ - Decoder: "plain", - Value: []byte(matches[1]), - } - } -} - -func getResourceType(isDir bool) provider.ResourceType { - if isDir { - return provider.ResourceType_RESOURCE_TYPE_CONTAINER - } - return provider.ResourceType_RESOURCE_TYPE_FILE -} - -// TODO propagate etag and mtime or append event to history? propagate on disk ... -// - but propagation is a separate task. only if upload was successful ... diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/owncloudsql_unix.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/owncloudsql_unix.go deleted file mode 100644 index 06750bd6440..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/owncloudsql_unix.go +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -//go:build !windows -// +build !windows - -package owncloudsql - -import ( - "context" - "crypto/md5" - "encoding/binary" - "fmt" - "os" - "strings" - "syscall" - - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - "github.com/owncloud/reva/v2/pkg/appctx" -) - -// TODO(jfd) get rid of the differences between unix and windows. the inode and dev should never be used for the etag because it interferes with backups - -// calcEtag will create an etag based on the md5 of -// - mtime, -// - inode (if available), -// - device (if available) and -// - size. -// errors are logged, but an etag will still be returned -func calcEtag(ctx context.Context, fi os.FileInfo) string { - log := appctx.GetLogger(ctx) - h := md5.New() - err := binary.Write(h, binary.BigEndian, fi.ModTime().UnixNano()) - if err != nil { - log.Error().Err(err).Msg("error writing mtime") - } - stat, ok := fi.Sys().(*syscall.Stat_t) - if ok { - // take device and inode into account - err = binary.Write(h, binary.BigEndian, stat.Ino) - if err != nil { - log.Error().Err(err).Msg("error writing inode") - } - err = binary.Write(h, binary.BigEndian, stat.Dev) - if err != nil { - log.Error().Err(err).Msg("error writing device") - } - } - err = binary.Write(h, binary.BigEndian, fi.Size()) - if err != nil { - log.Error().Err(err).Msg("error writing size") - } - etag := fmt.Sprintf("%x", h.Sum(nil)) - return strings.Trim(etag, "\"") -} - -func (fs *owncloudsqlfs) GetQuota(ctx context.Context, ref *provider.Reference) (uint64, uint64, uint64, error) { - // TODO quota of which storage space? - // we could use the logged in user, but when a user has access to multiple storages this falls short - // for now return quota of root - stat := syscall.Statfs_t{} - err := syscall.Statfs(fs.toInternalPath(ctx, "/"), &stat) - if err != nil { - return 0, 0, 0, err - } - // Total data blocks in filesystem - total := stat.Blocks * uint64(stat.Bsize) - // Free blocks available to unprivileged user - used := (stat.Blocks - uint64(stat.Bavail)) * uint64(stat.Bsize) //nolint:unconvert - remaining := total - used - return total, used, remaining, nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/owncloudsql_windows.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/owncloudsql_windows.go deleted file mode 100644 index e9a8fb31251..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/owncloudsql_windows.go +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -//go:build windows -// +build windows - -package owncloudsql - -import ( - "context" - "crypto/md5" - "encoding/binary" - "fmt" - "os" - "strings" - - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - "github.com/owncloud/reva/v2/pkg/appctx" - "golang.org/x/sys/windows" -) - -// calcEtag will create an etag based on the md5 of -// - mtime, -// - inode (if available), -// - device (if available) and -// - size. -// errors are logged, but an etag will still be returned -func calcEtag(ctx context.Context, fi os.FileInfo) string { - log := appctx.GetLogger(ctx) - h := md5.New() - err := binary.Write(h, binary.BigEndian, fi.ModTime().UnixNano()) - if err != nil { - log.Error().Err(err).Msg("error writing mtime") - } - // device and inode have no meaning on windows - err = binary.Write(h, binary.BigEndian, fi.Size()) - if err != nil { - log.Error().Err(err).Msg("error writing size") - } - etag := fmt.Sprintf(`"%x"`, h.Sum(nil)) - return fmt.Sprintf("\"%s\"", strings.Trim(etag, "\"")) -} - -func (fs *owncloudsqlfs) GetQuota(ctx context.Context, ref *provider.Reference) (uint64, uint64, uint64, error) { - // TODO quota of which storage space? - // we could use the logged in user, but when a user has access to multiple storages this falls short - // for now return quota of root - var free, total, avail uint64 - - pathPtr, err := windows.UTF16PtrFromString(fs.toInternalPath(ctx, "/")) - if err != nil { - return 0, 0, 0, err - } - err = windows.GetDiskFreeSpaceEx(pathPtr, &avail, &total, &free) - if err != nil { - return 0, 0, 0, err - } - - used := total - free - return total, used, free, nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/spaces.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/spaces.go deleted file mode 100644 index 389359ebc2a..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/spaces.go +++ /dev/null @@ -1,193 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package owncloudsql - -import ( - "context" - "fmt" - "strconv" - "strings" - - userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" - ctxpkg "github.com/owncloud/reva/v2/pkg/ctx" - "github.com/owncloud/reva/v2/pkg/errtypes" - "github.com/owncloud/reva/v2/pkg/storage" - "github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/filecache" - "github.com/owncloud/reva/v2/pkg/storagespace" - "github.com/owncloud/reva/v2/pkg/utils" -) - -// ListStorageSpaces lists storage spaces according to the provided filters -func (fs *owncloudsqlfs) ListStorageSpaces(ctx context.Context, filter []*provider.ListStorageSpacesRequest_Filter, unrestricted bool) ([]*provider.StorageSpace, error) { - var ( - spaceID = "*" - // uid *userpb.UserId - ) - - filteringUnsupportedSpaceTypes := false - - for i := range filter { - switch filter[i].Type { - case provider.ListStorageSpacesRequest_Filter_TYPE_SPACE_TYPE: - t := filter[i].GetSpaceType() - filteringUnsupportedSpaceTypes = (t != "personal" && !strings.HasPrefix(t, "+")) - case provider.ListStorageSpacesRequest_Filter_TYPE_ID: - _, spaceID, _, _ = storagespace.SplitID(filter[i].GetId().OpaqueId) - case provider.ListStorageSpacesRequest_Filter_TYPE_USER: - // uid = filter[i].GetUser() - } - } - if filteringUnsupportedSpaceTypes { - // owncloudsql only supports personal spaces, no need to search for something else - return []*provider.StorageSpace{}, nil - } - - spaces := []*provider.StorageSpace{} - if spaceID == "*" { - u, ok := ctxpkg.ContextGetUser(ctx) - if !ok { - return nil, errtypes.UserRequired("error getting user from context") - } - // if uid != nil && utils.UserIDEqual(uid, u.Id) { - // return nil, errtypes.PermissionDenied("cannot access space of other user?") - // } - space, err := fs.getPersonalSpace(ctx, u) - if err != nil { - return nil, err - } - spaces = append(spaces, space) - } else { - id, err := strconv.Atoi(spaceID) - if err != nil { - // non-numeric space id -> this request is not for us - return []*provider.StorageSpace{}, nil - } - space, err := fs.getSpaceByNumericID(ctx, id) - if err != nil { - return nil, err - } - spaces = append(spaces, space) - } - return spaces, nil -} - -// CreateStorageSpace creates a storage space -func (fs *owncloudsqlfs) CreateStorageSpace(ctx context.Context, req *provider.CreateStorageSpaceRequest) (*provider.CreateStorageSpaceResponse, error) { - return nil, errtypes.NotSupported("unimplemented: CreateStorageSpace") -} - -// UpdateStorageSpace updates a storage space -func (fs *owncloudsqlfs) UpdateStorageSpace(ctx context.Context, req *provider.UpdateStorageSpaceRequest) (*provider.UpdateStorageSpaceResponse, error) { - return nil, errtypes.NotSupported("update storage space") -} - -// DeleteStorageSpace deletes a storage space -func (fs *owncloudsqlfs) DeleteStorageSpace(ctx context.Context, req *provider.DeleteStorageSpaceRequest) (*storage.DeleteStorageSpaceResult, error) { - return nil, errtypes.NotSupported("delete storage space") -} - -// Note: currently unused but will be used later -// func (fs *owncloudsqlfs) listAllPersonalSpaces(ctx context.Context) ([]*provider.StorageSpace, error) { -// storages, err := fs.filecache.ListStorages(true) -// if err != nil { -// return nil, err -// } -// spaces := []*provider.StorageSpace{} -// for _, storage := range storages { -// space, err := fs.storageToSpace(ctx, storage) -// if err != nil { -// return nil, err -// } -// spaces = append(spaces, space) -// } -// return spaces, nil -// } - -func (fs *owncloudsqlfs) getPersonalSpace(ctx context.Context, owner *userpb.User) (*provider.StorageSpace, error) { - storageID, err := fs.filecache.GetNumericStorageID(ctx, "home::"+owner.Username) - if err != nil { - return nil, err - } - storage, err := fs.filecache.GetStorage(ctx, storageID) - if err != nil { - return nil, err - } - root, err := fs.filecache.Get(ctx, storage.NumericID, "") - if err != nil { - return nil, err - } - - space := &provider.StorageSpace{ - Id: &provider.StorageSpaceId{OpaqueId: strconv.Itoa(storage.NumericID)}, - Root: &provider.ResourceId{ - // return ownclouds numeric storage id as the space id! - SpaceId: strconv.Itoa(storage.NumericID), - OpaqueId: strconv.Itoa(root.ID), - }, - Name: owner.Username, - SpaceType: "personal", - Mtime: &types.Timestamp{Seconds: uint64(root.MTime)}, - Owner: owner, - } - space.Opaque = utils.AppendPlainToOpaque(space.Opaque, "spaceAlias", "personal/"+owner.Username) - space.Opaque = utils.AppendPlainToOpaque(space.Opaque, "etag", fmt.Sprintf(`"%s"`, root.Etag)) - return space, nil -} - -func (fs *owncloudsqlfs) getSpaceByNumericID(ctx context.Context, spaceID int) (*provider.StorageSpace, error) { - storage, err := fs.filecache.GetStorage(ctx, spaceID) - if err != nil { - return nil, err - } - if !strings.HasPrefix(storage.ID, "home::") { - return nil, fmt.Errorf("only personal spaces are supported") - } - - return fs.storageToSpace(ctx, storage) -} - -func (fs *owncloudsqlfs) storageToSpace(ctx context.Context, storage *filecache.Storage) (*provider.StorageSpace, error) { - root, err := fs.filecache.Get(ctx, storage.NumericID, "") - if err != nil { - return nil, err - } - ownerName := strings.TrimPrefix(storage.ID, "home::") - owner, err := fs.getUser(ctx, ownerName) - if err != nil { - return nil, err - } - - space := &provider.StorageSpace{ - Id: &provider.StorageSpaceId{OpaqueId: strconv.Itoa(storage.NumericID)}, - Root: &provider.ResourceId{ - // return ownclouds numeric storage id as the space id! - SpaceId: strconv.Itoa(storage.NumericID), - OpaqueId: strconv.Itoa(root.ID), - }, - Name: owner.Username, - SpaceType: "personal", - Mtime: &types.Timestamp{Seconds: uint64(root.MTime)}, - Owner: owner, - } - space.Opaque = utils.AppendPlainToOpaque(space.Opaque, "spaceAlias", "personal/"+owner.Username) - space.Opaque = utils.AppendPlainToOpaque(space.Opaque, "etag", fmt.Sprintf(`"%s"`, root.Etag)) - return space, nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/upload.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/upload.go deleted file mode 100644 index 358d35e45c0..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/upload.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package owncloudsql - -import ( - "context" - - provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" - "github.com/owncloud/reva/v2/pkg/errtypes" - "github.com/owncloud/reva/v2/pkg/storage" -) - -func (fs *owncloudsqlfs) MarkProcessing(ctx context.Context, ref *provider.Reference, processing bool, sessionID string) error { - return errtypes.NotSupported("op not supported") -} - -func (fs *owncloudsqlfs) CommitUpload(_ context.Context, _ *provider.Reference, _ string, _ storage.UploadSource) error { - return errtypes.NotSupported("op not supported") -} - -func (fs *owncloudsqlfs) PrepareUpload(_ context.Context, _ *provider.Reference, _ string, info storage.UploadInfo) (*storage.PrepareUploadResult, error) { - return &storage.PrepareUploadResult{VersionCreated: info.NodeExisted}, nil -} - -func (fs *owncloudsqlfs) RollbackUpload(_ context.Context, _ *provider.Reference, _ string, _ storage.RollbackInfo) error { - return nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/user/manager/loader/loader.go b/vendor/github.com/owncloud/reva/v2/pkg/user/manager/loader/loader.go index 943c51b4fc4..9afab19d514 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/user/manager/loader/loader.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/user/manager/loader/loader.go @@ -25,6 +25,5 @@ import ( _ "github.com/owncloud/reva/v2/pkg/user/manager/ldap" _ "github.com/owncloud/reva/v2/pkg/user/manager/memory" _ "github.com/owncloud/reva/v2/pkg/user/manager/nextcloud" - _ "github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql" // Add your own here ) diff --git a/vendor/github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/accounts/accounts.go b/vendor/github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/accounts/accounts.go deleted file mode 100644 index 3848bfb6848..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/accounts/accounts.go +++ /dev/null @@ -1,228 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package accounts - -import ( - "context" - "database/sql" - "strings" - "time" - - "github.com/owncloud/reva/v2/pkg/appctx" - "github.com/pkg/errors" -) - -// Accounts represents oc10-style Accounts -type Accounts struct { - driver string - db *sql.DB - joinUsername, joinUUID, enableMedialSearch bool - selectSQL string -} - -// NewMysql returns a new Cache instance connecting to a MySQL database -func NewMysql(dsn string, joinUsername, joinUUID, enableMedialSearch bool) (*Accounts, error) { - sqldb, err := sql.Open("mysql", dsn) - if err != nil { - return nil, errors.Wrap(err, "error connecting to the database") - } - - // FIXME make configurable - sqldb.SetConnMaxLifetime(time.Minute * 3) - sqldb.SetConnMaxIdleTime(time.Second * 30) - sqldb.SetMaxOpenConns(100) - sqldb.SetMaxIdleConns(10) - - err = sqldb.Ping() - if err != nil { - return nil, errors.Wrap(err, "error connecting to the database") - } - - return New("mysql", sqldb, joinUsername, joinUUID, enableMedialSearch) -} - -// New returns a new Cache instance connecting to the given sql.DB -func New(driver string, sqldb *sql.DB, joinUsername, joinUUID, enableMedialSearch bool) (*Accounts, error) { - - sel := "SELECT id, email, user_id, display_name, quota, last_login, backend, home, state" - from := ` - FROM oc_accounts a - ` - if joinUsername { - sel += ", p.configvalue AS username" - from += `LEFT JOIN oc_preferences p - ON a.user_id=p.userid - AND p.appid='core' - AND p.configkey='username'` - } else { - // fallback to user_id as username - sel += ", user_id AS username" - } - if joinUUID { - sel += ", p2.configvalue AS ownclouduuid" - from += `LEFT JOIN oc_preferences p2 - ON a.user_id=p2.userid - AND p2.appid='core' - AND p2.configkey='ownclouduuid'` - } else { - // fallback to user_id as ownclouduuid - sel += ", user_id AS ownclouduuid" - } - - return &Accounts{ - driver: driver, - db: sqldb, - joinUsername: joinUsername, - joinUUID: joinUUID, - enableMedialSearch: enableMedialSearch, - selectSQL: sel + from, - }, nil -} - -// Account stores information about accounts. -type Account struct { - ID uint64 - Email sql.NullString - UserID string - DisplayName sql.NullString - Quota sql.NullString - LastLogin int - Backend string - Home string - State int8 - Username sql.NullString // optional comes from the oc_preferences - OwnCloudUUID sql.NullString // optional comes from the oc_preferences -} - -func (as *Accounts) rowToAccount(ctx context.Context, row Scannable) (*Account, error) { - a := Account{} - if err := row.Scan(&a.ID, &a.Email, &a.UserID, &a.DisplayName, &a.Quota, &a.LastLogin, &a.Backend, &a.Home, &a.State, &a.Username, &a.OwnCloudUUID); err != nil { - appctx.GetLogger(ctx).Error().Err(err).Msg("could not scan row, skipping") - return nil, err - } - - return &a, nil -} - -// Scannable describes the interface providing a Scan method -type Scannable interface { - Scan(...interface{}) error -} - -// GetAccountByClaim fetches an account by mail, username or userid -func (as *Accounts) GetAccountByClaim(ctx context.Context, claim, value string) (*Account, error) { - // TODO align supported claims with rest driver and the others, maybe refactor into common mapping - var row *sql.Row - var where string - switch claim { - case "mail": - where = "WHERE a.email=?" - // case "uid": - // claim = m.c.Schema.UIDNumber - // case "gid": - // claim = m.c.Schema.GIDNumber - case "username": - if as.joinUsername { - where = "WHERE p.configvalue=?" - } else { - // use user_id as username - where = "WHERE a.user_id=?" - } - case "userid": - if as.joinUUID { - where = "WHERE p2.configvalue=?" - } else { - // use user_id as uuid - where = "WHERE a.user_id=?" - } - default: - return nil, errors.New("owncloudsql: invalid field " + claim) - } - - row = as.db.QueryRowContext(ctx, as.selectSQL+where, value) - - return as.rowToAccount(ctx, row) -} - -func sanitizeWildcards(q string) string { - return strings.ReplaceAll(strings.ReplaceAll(q, "%", `\%`), "_", `\_`) -} - -// FindAccounts searches userid, displayname and email using the given query. The Wildcard caracters % and _ are escaped. -func (as *Accounts) FindAccounts(ctx context.Context, query string) ([]Account, error) { - if as.enableMedialSearch { - query = "%" + sanitizeWildcards(query) + "%" - } - // TODO join oc_account_terms - where := "WHERE a.user_id LIKE ? OR a.display_name LIKE ? OR a.email LIKE ?" - args := []interface{}{query, query, query} - - if as.joinUsername { - where += " OR p.configvalue LIKE ?" - args = append(args, query) - } - if as.joinUUID { - where += " OR p2.configvalue LIKE ?" - args = append(args, query) - } - - rows, err := as.db.QueryContext(ctx, as.selectSQL+where, args...) - if err != nil { - return nil, err - } - defer rows.Close() - - accounts := []Account{} - for rows.Next() { - a := Account{} - if err := rows.Scan(&a.ID, &a.Email, &a.UserID, &a.DisplayName, &a.Quota, &a.LastLogin, &a.Backend, &a.Home, &a.State, &a.Username, &a.OwnCloudUUID); err != nil { - appctx.GetLogger(ctx).Error().Err(err).Msg("could not scan row, skipping") - continue - } - accounts = append(accounts, a) - } - if err = rows.Err(); err != nil { - return nil, err - } - - return accounts, nil -} - -// GetAccountGroups lasts the groups for an account -func (as *Accounts) GetAccountGroups(ctx context.Context, uid string) ([]string, error) { - rows, err := as.db.QueryContext(ctx, "SELECT gid FROM oc_group_user WHERE uid=?", uid) - if err != nil { - return nil, err - } - defer rows.Close() - - groups := []string{} - for rows.Next() { - var group string - if err := rows.Scan(&group); err != nil { - appctx.GetLogger(ctx).Error().Err(err).Msg("could not scan row, skipping") - continue - } - groups = append(groups, group) - } - if err = rows.Err(); err != nil { - return nil, err - } - return groups, nil -} diff --git a/vendor/github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/accounts/test.sqlite b/vendor/github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/accounts/test.sqlite deleted file mode 100644 index c68bb753774..00000000000 Binary files a/vendor/github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/accounts/test.sqlite and /dev/null differ diff --git a/vendor/github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/owncloudsql.go b/vendor/github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/owncloudsql.go deleted file mode 100644 index a968a509ec4..00000000000 --- a/vendor/github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/owncloudsql.go +++ /dev/null @@ -1,189 +0,0 @@ -// Copyright 2018-2021 CERN -// -// 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. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package owncloudsql - -import ( - "context" - "database/sql" - "fmt" - - userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" - "github.com/owncloud/reva/v2/pkg/appctx" - "github.com/owncloud/reva/v2/pkg/errtypes" - "github.com/owncloud/reva/v2/pkg/user" - "github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/accounts" - "github.com/owncloud/reva/v2/pkg/user/manager/registry" - "github.com/mitchellh/mapstructure" - "github.com/pkg/errors" - - // Provides mysql drivers - _ "github.com/go-sql-driver/mysql" -) - -func init() { - registry.Register("owncloudsql", NewMysql) -} - -type manager struct { - c *config - db *accounts.Accounts -} - -type config struct { - DbUsername string `mapstructure:"dbusername"` - DbPassword string `mapstructure:"dbpassword"` - DbHost string `mapstructure:"dbhost"` - DbPort int `mapstructure:"dbport"` - DbName string `mapstructure:"dbname"` - Idp string `mapstructure:"idp"` - Nobody int64 `mapstructure:"nobody"` - JoinUsername bool `mapstructure:"join_username"` - JoinOwnCloudUUID bool `mapstructure:"join_ownclouduuid"` - EnableMedialSearch bool `mapstructure:"enable_medial_search"` -} - -// NewMysql returns a new user manager connection to an owncloud mysql database -func NewMysql(m map[string]interface{}) (user.Manager, error) { - mgr := &manager{} - err := mgr.Configure(m) - if err != nil { - err = errors.Wrap(err, "error creating a new manager") - return nil, err - } - - mgr.db, err = accounts.NewMysql( - fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", mgr.c.DbUsername, mgr.c.DbPassword, mgr.c.DbHost, mgr.c.DbPort, mgr.c.DbName), - mgr.c.JoinUsername, - mgr.c.JoinOwnCloudUUID, - mgr.c.EnableMedialSearch, - ) - if err != nil { - return nil, err - } - - return mgr, nil -} - -func (m *manager) Configure(ml map[string]interface{}) error { - c, err := parseConfig(ml) - if err != nil { - return err - } - - if c.Nobody == 0 { - c.Nobody = 99 - } - - m.c = c - return nil -} - -func parseConfig(m map[string]interface{}) (*config, error) { - c := &config{} - if err := mapstructure.Decode(m, &c); err != nil { - return nil, err - } - return c, nil -} - -func (m *manager) GetUser(ctx context.Context, uid *userpb.UserId, skipFetchingGroups bool) (*userpb.User, error) { - // search via the user_id - a, err := m.db.GetAccountByClaim(ctx, "userid", uid.OpaqueId) - if err == sql.ErrNoRows { - return nil, errtypes.NotFound(uid.OpaqueId) - } - return m.convertToCS3User(ctx, a, skipFetchingGroups) -} - -func (m *manager) GetUserByClaim(ctx context.Context, claim, value string, skipFetchingGroups bool) (*userpb.User, error) { - a, err := m.db.GetAccountByClaim(ctx, claim, value) - if err == sql.ErrNoRows { - return nil, errtypes.NotFound(claim + "=" + value) - } else if err != nil { - return nil, err - } - return m.convertToCS3User(ctx, a, skipFetchingGroups) -} - -func (m *manager) FindUsers(ctx context.Context, query string, skipFetchingGroups bool) ([]*userpb.User, error) { - - accounts, err := m.db.FindAccounts(ctx, query) - if err == sql.ErrNoRows { - return nil, errtypes.NotFound("no users found for " + query) - } else if err != nil { - return nil, err - } - - users := make([]*userpb.User, 0, len(accounts)) - for i := range accounts { - u, err := m.convertToCS3User(ctx, &accounts[i], skipFetchingGroups) - if err != nil { - appctx.GetLogger(ctx).Error().Err(err).Interface("account", accounts[i]).Msg("could not convert account, skipping") - continue - } - users = append(users, u) - } - - return users, nil -} - -func (m *manager) GetUserGroups(ctx context.Context, uid *userpb.UserId) ([]string, error) { - groups, err := m.db.GetAccountGroups(ctx, uid.OpaqueId) - if err == sql.ErrNoRows { - return nil, errtypes.NotFound("no groups found for uid " + uid.OpaqueId) - } else if err != nil { - return nil, err - } - return groups, nil -} - -func (m *manager) convertToCS3User(ctx context.Context, a *accounts.Account, skipFetchingGroups bool) (*userpb.User, error) { - u := &userpb.User{ - Id: &userpb.UserId{ - Idp: m.c.Idp, - OpaqueId: a.OwnCloudUUID.String, - Type: userpb.UserType_USER_TYPE_PRIMARY, - }, - Username: a.Username.String, - Mail: a.Email.String, - DisplayName: a.DisplayName.String, - //Groups: groups, - GidNumber: m.c.Nobody, - UidNumber: m.c.Nobody, - } - // https://github.com/owncloud/reva/pull/4135 - // fall back to userid - if u.Id.OpaqueId == "" { - u.Id.OpaqueId = a.UserID - } - if u.Username == "" { - u.Username = u.Id.OpaqueId - } - if u.DisplayName == "" { - u.DisplayName = u.Id.OpaqueId - } - - if !skipFetchingGroups { - var err error - if u.Groups, err = m.GetUserGroups(ctx, u.Id); err != nil { - return nil, err - } - } - return u, nil -} diff --git a/vendor/modules.txt b/vendor/modules.txt index e673e6e9316..2fb4e2bead2 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1321,7 +1321,7 @@ github.com/orcaman/concurrent-map # github.com/owncloud/libre-graph-api-go v1.0.5-0.20260216101009-eeac018af245 ## explicit; go 1.18 github.com/owncloud/libre-graph-api-go -# github.com/owncloud/reva/v2 v2.0.0-20261012083840-8b649a4c3899 +# github.com/owncloud/reva/v2 v2.0.0-20261019151321-d079c7843edc ## explicit; go 1.26.0 github.com/owncloud/reva/v2/cmd/revad/internal/grace github.com/owncloud/reva/v2/cmd/revad/runtime @@ -1439,8 +1439,6 @@ github.com/owncloud/reva/v2/pkg/auth/manager/machine github.com/owncloud/reva/v2/pkg/auth/manager/nextcloud github.com/owncloud/reva/v2/pkg/auth/manager/ocmshares github.com/owncloud/reva/v2/pkg/auth/manager/oidc -github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql -github.com/owncloud/reva/v2/pkg/auth/manager/owncloudsql/accounts github.com/owncloud/reva/v2/pkg/auth/manager/publicshares github.com/owncloud/reva/v2/pkg/auth/manager/registry github.com/owncloud/reva/v2/pkg/auth/manager/serviceaccounts @@ -1546,7 +1544,6 @@ github.com/owncloud/reva/v2/pkg/publicshare/manager/json/persistence/file github.com/owncloud/reva/v2/pkg/publicshare/manager/json/persistence/memory github.com/owncloud/reva/v2/pkg/publicshare/manager/loader github.com/owncloud/reva/v2/pkg/publicshare/manager/memory -github.com/owncloud/reva/v2/pkg/publicshare/manager/owncloudsql github.com/owncloud/reva/v2/pkg/publicshare/manager/registry github.com/owncloud/reva/v2/pkg/registry github.com/owncloud/reva/v2/pkg/rgrpc @@ -1578,7 +1575,6 @@ github.com/owncloud/reva/v2/pkg/share/manager/jsoncs3/sharecache github.com/owncloud/reva/v2/pkg/share/manager/jsoncs3/shareid github.com/owncloud/reva/v2/pkg/share/manager/loader github.com/owncloud/reva/v2/pkg/share/manager/memory -github.com/owncloud/reva/v2/pkg/share/manager/owncloudsql github.com/owncloud/reva/v2/pkg/share/manager/registry github.com/owncloud/reva/v2/pkg/sharedconf github.com/owncloud/reva/v2/pkg/siteacc @@ -1621,8 +1617,6 @@ github.com/owncloud/reva/v2/pkg/storage/fs/localhome github.com/owncloud/reva/v2/pkg/storage/fs/nextcloud github.com/owncloud/reva/v2/pkg/storage/fs/ocis github.com/owncloud/reva/v2/pkg/storage/fs/ocis/blobstore -github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql -github.com/owncloud/reva/v2/pkg/storage/fs/owncloudsql/filecache github.com/owncloud/reva/v2/pkg/storage/fs/posix github.com/owncloud/reva/v2/pkg/storage/fs/posix/blobstore github.com/owncloud/reva/v2/pkg/storage/fs/posix/lookup @@ -1691,8 +1685,6 @@ github.com/owncloud/reva/v2/pkg/user/manager/ldap github.com/owncloud/reva/v2/pkg/user/manager/loader github.com/owncloud/reva/v2/pkg/user/manager/memory github.com/owncloud/reva/v2/pkg/user/manager/nextcloud -github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql -github.com/owncloud/reva/v2/pkg/user/manager/owncloudsql/accounts github.com/owncloud/reva/v2/pkg/user/manager/registry github.com/owncloud/reva/v2/pkg/utils github.com/owncloud/reva/v2/pkg/utils/cfg