filesystem.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package sdk
  2. import "github.com/drakkan/sftpgo/v2/sdk/kms"
  3. // FilesystemProvider defines the supported storage filesystems
  4. type FilesystemProvider int
  5. // supported values for FilesystemProvider
  6. const (
  7. LocalFilesystemProvider FilesystemProvider = iota // Local
  8. S3FilesystemProvider // AWS S3 compatible
  9. GCSFilesystemProvider // Google Cloud Storage
  10. AzureBlobFilesystemProvider // Azure Blob Storage
  11. CryptedFilesystemProvider // Local encrypted
  12. SFTPFilesystemProvider // SFTP
  13. )
  14. // GetProviderByName returns the FilesystemProvider matching a given name
  15. // to provide backwards compatibility, numeric strings are accepted as well
  16. func GetProviderByName(name string) FilesystemProvider {
  17. switch name {
  18. case "0", "osfs":
  19. return LocalFilesystemProvider
  20. case "1", "s3fs":
  21. return S3FilesystemProvider
  22. case "2", "gcsfs":
  23. return GCSFilesystemProvider
  24. case "3", "azblobfs":
  25. return AzureBlobFilesystemProvider
  26. case "4", "cryptfs":
  27. return CryptedFilesystemProvider
  28. case "5", "sftpfs":
  29. return SFTPFilesystemProvider
  30. }
  31. // TODO think about returning an error value instead of silently defaulting to LocalFilesystemProvider
  32. return LocalFilesystemProvider
  33. }
  34. // Name returns the Provider's unique name
  35. func (p FilesystemProvider) Name() string {
  36. switch p {
  37. case LocalFilesystemProvider:
  38. return "osfs"
  39. case S3FilesystemProvider:
  40. return "s3fs"
  41. case GCSFilesystemProvider:
  42. return "gcsfs"
  43. case AzureBlobFilesystemProvider:
  44. return "azblobfs"
  45. case CryptedFilesystemProvider:
  46. return "cryptfs"
  47. case SFTPFilesystemProvider:
  48. return "sftpfs"
  49. }
  50. return "" // let's not claim to be
  51. }
  52. // ShortInfo returns a human readable, short description for the given FilesystemProvider
  53. func (p FilesystemProvider) ShortInfo() string {
  54. switch p {
  55. case LocalFilesystemProvider:
  56. return "Local"
  57. case S3FilesystemProvider:
  58. return "AWS S3 (Compatible)"
  59. case GCSFilesystemProvider:
  60. return "Google Cloud Storage"
  61. case AzureBlobFilesystemProvider:
  62. return "Azure Blob Storage"
  63. case CryptedFilesystemProvider:
  64. return "Local encrypted"
  65. case SFTPFilesystemProvider:
  66. return "SFTP"
  67. }
  68. return ""
  69. }
  70. // ListProviders returns a list of available FilesystemProviders.
  71. func ListProviders() []FilesystemProvider {
  72. return []FilesystemProvider{
  73. LocalFilesystemProvider, S3FilesystemProvider,
  74. GCSFilesystemProvider, AzureBlobFilesystemProvider,
  75. CryptedFilesystemProvider, SFTPFilesystemProvider,
  76. }
  77. }
  78. // BaseS3FsConfig defines the base configuration for S3 based filesystems
  79. type BaseS3FsConfig struct {
  80. Bucket string `json:"bucket,omitempty"`
  81. // KeyPrefix is similar to a chroot directory for local filesystem.
  82. // If specified then the SFTP user will only see objects that starts
  83. // with this prefix and so you can restrict access to a specific
  84. // folder. The prefix, if not empty, must not start with "/" and must
  85. // end with "/".
  86. // If empty the whole bucket contents will be available
  87. KeyPrefix string `json:"key_prefix,omitempty"`
  88. Region string `json:"region,omitempty"`
  89. AccessKey string `json:"access_key,omitempty"`
  90. Endpoint string `json:"endpoint,omitempty"`
  91. StorageClass string `json:"storage_class,omitempty"`
  92. // The canned ACL to apply to uploaded objects. Leave empty to use the default ACL.
  93. // For more information and available ACLs, see here:
  94. // https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl
  95. ACL string `json:"acl,omitempty"`
  96. // The buffer size (in MB) to use for multipart uploads. The minimum allowed part size is 5MB,
  97. // and if this value is set to zero, the default value (5MB) for the AWS SDK will be used.
  98. // The minimum allowed value is 5.
  99. // Please note that if the upload bandwidth between the SFTP client and SFTPGo is greater than
  100. // the upload bandwidth between SFTPGo and S3 then the SFTP client have to wait for the upload
  101. // of the last parts to S3 after it ends the file upload to SFTPGo, and it may time out.
  102. // Keep this in mind if you customize these parameters.
  103. UploadPartSize int64 `json:"upload_part_size,omitempty"`
  104. // How many parts are uploaded in parallel
  105. UploadConcurrency int `json:"upload_concurrency,omitempty"`
  106. // The buffer size (in MB) to use for multipart downloads. The minimum allowed part size is 5MB,
  107. // and if this value is set to zero, the default value (5MB) for the AWS SDK will be used.
  108. // The minimum allowed value is 5. Ignored for partial downloads.
  109. DownloadPartSize int64 `json:"download_part_size,omitempty"`
  110. // How many parts are downloaded in parallel. Ignored for partial downloads.
  111. DownloadConcurrency int `json:"download_concurrency,omitempty"`
  112. // DownloadPartMaxTime defines the maximum time allowed, in seconds, to download a single chunk (5MB).
  113. // 0 means no timeout. Ignored for partial downloads.
  114. DownloadPartMaxTime int `json:"download_part_max_time,omitempty"`
  115. // Set this to `true` to force the request to use path-style addressing,
  116. // i.e., `http://s3.amazonaws.com/BUCKET/KEY`. By default, the S3 client
  117. // will use virtual hosted bucket addressing when possible
  118. // (`http://BUCKET.s3.amazonaws.com/KEY`)
  119. ForcePathStyle bool `json:"force_path_style,omitempty"`
  120. }
  121. // S3FsConfig defines the base configuration for S3 based filesystems
  122. type S3FsConfig struct {
  123. BaseS3FsConfig
  124. AccessSecret kms.BaseSecret `json:"access_secret,omitempty"`
  125. }
  126. // BaseGCSFsConfig defines the base configuration for Google Cloud Storage based filesystems
  127. type BaseGCSFsConfig struct {
  128. Bucket string `json:"bucket,omitempty"`
  129. // KeyPrefix is similar to a chroot directory for local filesystem.
  130. // If specified then the SFTP user will only see objects that starts
  131. // with this prefix and so you can restrict access to a specific
  132. // folder. The prefix, if not empty, must not start with "/" and must
  133. // end with "/".
  134. // If empty the whole bucket contents will be available
  135. KeyPrefix string `json:"key_prefix,omitempty"`
  136. CredentialFile string `json:"-"`
  137. // 0 explicit, 1 automatic
  138. AutomaticCredentials int `json:"automatic_credentials,omitempty"`
  139. StorageClass string `json:"storage_class,omitempty"`
  140. // The ACL to apply to uploaded objects. Leave empty to use the default ACL.
  141. // For more information and available ACLs, refer to the JSON API here:
  142. // https://cloud.google.com/storage/docs/access-control/lists#predefined-acl
  143. ACL string `json:"acl,omitempty"`
  144. }
  145. // GCSFsConfig defines the configuration for Google Cloud Storage based filesystems
  146. type GCSFsConfig struct {
  147. BaseGCSFsConfig
  148. Credentials kms.BaseSecret `json:"credentials,omitempty"`
  149. }
  150. // BaseAzBlobFsConfig defines the base configuration for Azure Blob Storage based filesystem
  151. type BaseAzBlobFsConfig struct {
  152. Container string `json:"container,omitempty"`
  153. // Storage Account Name, leave blank to use SAS URL
  154. AccountName string `json:"account_name,omitempty"`
  155. // Optional endpoint. Default is "blob.core.windows.net".
  156. // If you use the emulator the endpoint must include the protocol,
  157. // for example "http://127.0.0.1:10000"
  158. Endpoint string `json:"endpoint,omitempty"`
  159. // KeyPrefix is similar to a chroot directory for local filesystem.
  160. // If specified then the SFTPGo user will only see objects that starts
  161. // with this prefix and so you can restrict access to a specific
  162. // folder. The prefix, if not empty, must not start with "/" and must
  163. // end with "/".
  164. // If empty the whole bucket contents will be available
  165. KeyPrefix string `json:"key_prefix,omitempty"`
  166. // The buffer size (in MB) to use for multipart uploads.
  167. // If this value is set to zero, the default value (1MB) for the Azure SDK will be used.
  168. // Please note that if the upload bandwidth between the SFTPGo client and SFTPGo server is
  169. // greater than the upload bandwidth between SFTPGo and Azure then the SFTP client have
  170. // to wait for the upload of the last parts to Azure after it ends the file upload to SFTPGo,
  171. // and it may time out.
  172. // Keep this in mind if you customize these parameters.
  173. UploadPartSize int64 `json:"upload_part_size,omitempty"`
  174. // How many parts are uploaded in parallel
  175. UploadConcurrency int `json:"upload_concurrency,omitempty"`
  176. // Set to true if you use an Azure emulator such as Azurite
  177. UseEmulator bool `json:"use_emulator,omitempty"`
  178. // Blob Access Tier
  179. AccessTier string `json:"access_tier,omitempty"`
  180. }
  181. // AzBlobFsConfig defines the configuration for Azure Blob Storage based filesystem
  182. type AzBlobFsConfig struct {
  183. BaseAzBlobFsConfig
  184. // Storage Account Key leave blank to use SAS URL.
  185. // The access key is stored encrypted based on the kms configuration
  186. AccountKey kms.BaseSecret `json:"account_key,omitempty"`
  187. // Shared access signature URL, leave blank if using account/key
  188. SASURL kms.BaseSecret `json:"sas_url,omitempty"`
  189. }
  190. // CryptFsConfig defines the configuration to store local files as encrypted
  191. type CryptFsConfig struct {
  192. Passphrase kms.BaseSecret `json:"passphrase,omitempty"`
  193. }
  194. // BaseSFTPFsConfig defines the base configuration for SFTP based filesystem
  195. type BaseSFTPFsConfig struct {
  196. Endpoint string `json:"endpoint,omitempty"`
  197. Username string `json:"username,omitempty"`
  198. Fingerprints []string `json:"fingerprints,omitempty"`
  199. // Prefix is the path prefix to strip from SFTP resource paths.
  200. Prefix string `json:"prefix,omitempty"`
  201. // Concurrent reads are safe to use and disabling them will degrade performance.
  202. // Some servers automatically delete files once they are downloaded.
  203. // Using concurrent reads is problematic with such servers.
  204. DisableCouncurrentReads bool `json:"disable_concurrent_reads,omitempty"`
  205. // The buffer size (in MB) to use for transfers.
  206. // Buffering could improve performance for high latency networks.
  207. // With buffering enabled upload resume is not supported and a file
  208. // cannot be opened for both reading and writing at the same time
  209. // 0 means disabled.
  210. BufferSize int64 `json:"buffer_size,omitempty"`
  211. }
  212. // SFTPFsConfig defines the configuration for SFTP based filesystem
  213. type SFTPFsConfig struct {
  214. BaseSFTPFsConfig
  215. Password kms.BaseSecret `json:"password,omitempty"`
  216. PrivateKey kms.BaseSecret `json:"private_key,omitempty"`
  217. }
  218. // Filesystem defines filesystem details
  219. type Filesystem struct {
  220. Provider FilesystemProvider `json:"provider"`
  221. S3Config S3FsConfig `json:"s3config,omitempty"`
  222. GCSConfig GCSFsConfig `json:"gcsconfig,omitempty"`
  223. AzBlobConfig AzBlobFsConfig `json:"azblobconfig,omitempty"`
  224. CryptConfig CryptFsConfig `json:"cryptconfig,omitempty"`
  225. SFTPConfig SFTPFsConfig `json:"sftpconfig,omitempty"`
  226. }