s3.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. package service
  2. import (
  3. "fmt"
  4. "io"
  5. "time"
  6. "aws-sts-mock/internal/kvdb"
  7. "aws-sts-mock/internal/storage"
  8. "aws-sts-mock/pkg/s3"
  9. )
  10. // S3Service handles S3 business logic with user isolation
  11. type S3Service struct {
  12. storage *storage.UserAwareStorage
  13. db *kvdb.BoltKVDB
  14. }
  15. // NewS3Service creates a new S3 service
  16. func NewS3Service(storage *storage.UserAwareStorage, db *kvdb.BoltKVDB) *S3Service {
  17. return &S3Service{
  18. storage: storage,
  19. db: db,
  20. }
  21. }
  22. // ListBuckets returns all buckets for a specific user
  23. func (s *S3Service) ListBuckets(accountID, accessKeyID string) (*s3.ListAllMyBucketsResult, error) {
  24. // Get bucket configs from database to get actual bucket names
  25. configs, err := s.db.ListBucketConfigs(accountID)
  26. if err != nil {
  27. return nil, err
  28. }
  29. var s3Buckets []s3.Bucket
  30. for _, config := range configs {
  31. // Parse the creation date
  32. createdAt, err := time.Parse(time.RFC3339, config.CreatedAt)
  33. if err != nil {
  34. createdAt = time.Now()
  35. }
  36. s3Buckets = append(s3Buckets, s3.Bucket{
  37. Name: config.BucketName, // Use actual bucket name, not ID
  38. CreationDate: createdAt.UTC().Format(time.RFC3339),
  39. })
  40. }
  41. return &s3.ListAllMyBucketsResult{
  42. Owner: s3.Owner{
  43. ID: accountID,
  44. DisplayName: accessKeyID,
  45. },
  46. Buckets: s3Buckets,
  47. }, nil
  48. }
  49. // CreateBucket creates a new bucket for a specific user
  50. func (s *S3Service) CreateBucket(accountID, name string) error {
  51. if name == "" {
  52. return fmt.Errorf("bucket name is required")
  53. }
  54. // Validate bucket name (basic validation)
  55. if len(name) < 3 || len(name) > 63 {
  56. return fmt.Errorf("bucket name must be between 3 and 63 characters")
  57. }
  58. // Create bucket in storage (returns bucket ID)
  59. bucketID, err := s.storage.CreateBucketForUser(accountID, name)
  60. if err != nil {
  61. return err
  62. }
  63. // Store bucket config in database
  64. config := &kvdb.BucketConfig{
  65. BucketID: bucketID,
  66. AccountID: accountID,
  67. BucketName: name,
  68. PublicViewing: false, // Default to private
  69. CreatedAt: time.Now().UTC().Format(time.RFC3339),
  70. }
  71. return s.db.SetBucketConfig(config)
  72. }
  73. // DeleteBucket deletes a bucket for a specific user
  74. func (s *S3Service) DeleteBucket(accountID, name string) error {
  75. // Get bucket ID first
  76. bucketID, err := s.storage.GetBucketIDForUser(accountID, name)
  77. if err != nil {
  78. return err
  79. }
  80. // Delete from storage
  81. if err := s.storage.DeleteBucketForUser(accountID, name); err != nil {
  82. return err
  83. }
  84. // Delete config from database
  85. return s.db.DeleteBucketConfig(accountID, bucketID)
  86. }
  87. // BucketExists checks if a bucket exists for a specific user
  88. func (s *S3Service) BucketExists(accountID, name string) (bool, error) {
  89. return s.storage.BucketExistsForUser(accountID, name)
  90. }
  91. // BucketIsEmpty checks if a bucket has no objects
  92. func (s *S3Service) BucketIsEmpty(accountID, bucketName string) (bool, error) {
  93. objects, err := s.storage.ListObjectsForUser(accountID, bucketName)
  94. if err != nil {
  95. return false, err
  96. }
  97. return len(objects) == 0, nil
  98. }
  99. // ListObjects returns all objects in a bucket for a specific user
  100. func (s *S3Service) ListObjects(accountID, bucketName string) ([]storage.ObjectInfo, error) {
  101. return s.storage.ListObjectsForUser(accountID, bucketName)
  102. }
  103. // PutObject stores an object for a specific user
  104. func (s *S3Service) PutObject(accountID, bucket, key string, reader io.Reader) error {
  105. return s.storage.PutObjectForUser(accountID, bucket, key, reader)
  106. }
  107. // GetObject retrieves an object for a specific user
  108. func (s *S3Service) GetObject(accountID, bucket, key string) (io.ReadCloser, *storage.ObjectInfo, error) {
  109. return s.storage.GetObjectForUser(accountID, bucket, key)
  110. }
  111. // DeleteObject removes an object for a specific user
  112. func (s *S3Service) DeleteObject(accountID, bucket, key string) error {
  113. return s.storage.DeleteObjectForUser(accountID, bucket, key)
  114. }
  115. // DeleteObjects removes multiple objects for a specific user
  116. // Returns a map of key -> error for failed deletions
  117. func (s *S3Service) DeleteObjects(accountID, bucket string, keys []string) map[string]error {
  118. errors := make(map[string]error)
  119. for _, key := range keys {
  120. if err := s.storage.DeleteObjectForUser(accountID, bucket, key); err != nil {
  121. // Only add to errors if it's not "object not found"
  122. // S3 behavior: deleting non-existent objects succeeds
  123. if err != storage.ErrObjectNotFound {
  124. errors[key] = err
  125. }
  126. }
  127. }
  128. return errors
  129. }
  130. // GetObjectInfo retrieves object metadata for a specific user
  131. func (s *S3Service) GetObjectInfo(accountID, bucket, key string) (*storage.ObjectInfo, error) {
  132. return s.storage.GetObjectInfoForUser(accountID, bucket, key)
  133. }
  134. // GetStorageStats returns storage statistics for a user
  135. func (s *S3Service) GetStorageStats(accountID string) (*storage.StorageStats, error) {
  136. return s.storage.GetStorageStatsForUser(accountID)
  137. }
  138. // SetBucketPublicViewing enables or disables public viewing for a bucket
  139. func (s *S3Service) SetBucketPublicViewing(accountID, bucketName string, enabled bool) error {
  140. // Get bucket ID
  141. bucketID, err := s.storage.GetBucketIDForUser(accountID, bucketName)
  142. if err != nil {
  143. return err
  144. }
  145. // Get existing config
  146. config, err := s.db.GetBucketConfig(accountID, bucketID)
  147. if err != nil {
  148. return fmt.Errorf("bucket config not found: %w", err)
  149. }
  150. // Update public viewing setting
  151. config.PublicViewing = enabled
  152. return s.db.SetBucketConfig(config)
  153. }
  154. // GetBucketConfig retrieves bucket configuration
  155. func (s *S3Service) GetBucketConfig(accountID, bucketName string) (*kvdb.BucketConfig, error) {
  156. bucketID, err := s.storage.GetBucketIDForUser(accountID, bucketName)
  157. if err != nil {
  158. return nil, err
  159. }
  160. return s.db.GetBucketConfig(accountID, bucketID)
  161. }
  162. // GetBucketByID retrieves bucket configuration by bucket ID
  163. func (s *S3Service) GetBucketByID(accountID, bucketID string) (*kvdb.BucketConfig, error) {
  164. return s.db.GetBucketConfig(accountID, bucketID)
  165. }
  166. // UpdateBucketConfig updates bucket configuration
  167. func (s *S3Service) UpdateBucketConfig(config *kvdb.BucketConfig) error {
  168. return s.db.SetBucketConfig(config)
  169. }
  170. // CopyObject copies an object within the same bucket or to another bucket
  171. func (s *S3Service) CopyObject(accountID, srcBucket, srcKey, dstBucket, dstKey string) error {
  172. // Get source object
  173. reader, info, err := s.storage.GetObjectForUser(accountID, srcBucket, srcKey)
  174. if err != nil {
  175. return fmt.Errorf("failed to get source object: %w", err)
  176. }
  177. defer reader.Close()
  178. // Put to destination
  179. if err := s.storage.PutObjectForUser(accountID, dstBucket, dstKey, reader); err != nil {
  180. return fmt.Errorf("failed to put destination object: %w", err)
  181. }
  182. // Note: Metadata is not copied in this simple implementation
  183. // You might want to extend storage to support metadata copying
  184. _ = info // Metadata could be used here if storage interface supported it
  185. return nil
  186. }
  187. // ObjectExists checks if an object exists
  188. func (s *S3Service) ObjectExists(accountID, bucket, key string) (bool, error) {
  189. _, err := s.storage.GetObjectInfoForUser(accountID, bucket, key)
  190. if err != nil {
  191. if err == storage.ErrObjectNotFound {
  192. return false, nil
  193. }
  194. return false, err
  195. }
  196. return true, nil
  197. }
  198. // GetBucketSize returns the total size of all objects in a bucket
  199. func (s *S3Service) GetBucketSize(accountID, bucketName string) (int64, error) {
  200. objects, err := s.storage.ListObjectsForUser(accountID, bucketName)
  201. if err != nil {
  202. return 0, err
  203. }
  204. var totalSize int64
  205. for _, obj := range objects {
  206. totalSize += obj.Size
  207. }
  208. return totalSize, nil
  209. }
  210. // GetBucketObjectCount returns the number of objects in a bucket
  211. func (s *S3Service) GetBucketObjectCount(accountID, bucketName string) (int, error) {
  212. objects, err := s.storage.ListObjectsForUser(accountID, bucketName)
  213. if err != nil {
  214. return 0, err
  215. }
  216. return len(objects), nil
  217. }
  218. // ListObjectsWithPrefix returns objects with a specific prefix
  219. func (s *S3Service) ListObjectsWithPrefix(accountID, bucketName, prefix string) ([]storage.ObjectInfo, error) {
  220. allObjects, err := s.storage.ListObjectsForUser(accountID, bucketName)
  221. if err != nil {
  222. return nil, err
  223. }
  224. var filtered []storage.ObjectInfo
  225. for _, obj := range allObjects {
  226. if len(obj.Key) >= len(prefix) && obj.Key[:len(prefix)] == prefix {
  227. filtered = append(filtered, obj)
  228. }
  229. }
  230. return filtered, nil
  231. }
  232. // DeleteObjectsWithPrefix deletes all objects with a specific prefix
  233. func (s *S3Service) DeleteObjectsWithPrefix(accountID, bucketName, prefix string) (int, error) {
  234. objects, err := s.ListObjectsWithPrefix(accountID, bucketName, prefix)
  235. if err != nil {
  236. return 0, err
  237. }
  238. count := 0
  239. for _, obj := range objects {
  240. if err := s.storage.DeleteObjectForUser(accountID, bucketName, obj.Key); err != nil {
  241. // Log error but continue
  242. continue
  243. }
  244. count++
  245. }
  246. return count, nil
  247. }
  248. // ValidateBucketName validates bucket name according to S3 rules
  249. func (s *S3Service) ValidateBucketName(name string) error {
  250. if len(name) < 3 || len(name) > 63 {
  251. return fmt.Errorf("bucket name must be between 3 and 63 characters")
  252. }
  253. // Check for valid characters (simplified version)
  254. for i, c := range name {
  255. if !((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '.') {
  256. return fmt.Errorf("bucket name contains invalid character: %c", c)
  257. }
  258. // Must start and end with letter or number
  259. if i == 0 || i == len(name)-1 {
  260. if !((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
  261. return fmt.Errorf("bucket name must start and end with a letter or number")
  262. }
  263. }
  264. }
  265. // Cannot be formatted as IP address
  266. if isIPAddress(name) {
  267. return fmt.Errorf("bucket name cannot be formatted as IP address")
  268. }
  269. return nil
  270. }
  271. // isIPAddress checks if string looks like an IP address
  272. func isIPAddress(s string) bool {
  273. parts := splitString(s, '.')
  274. if len(parts) != 4 {
  275. return false
  276. }
  277. for _, part := range parts {
  278. if len(part) == 0 || len(part) > 3 {
  279. return false
  280. }
  281. num := 0
  282. for _, c := range part {
  283. if c < '0' || c > '9' {
  284. return false
  285. }
  286. num = num*10 + int(c-'0')
  287. }
  288. if num > 255 {
  289. return false
  290. }
  291. }
  292. return true
  293. }
  294. // splitString splits string by delimiter
  295. func splitString(s string, delim rune) []string {
  296. var parts []string
  297. var current string
  298. for _, c := range s {
  299. if c == delim {
  300. parts = append(parts, current)
  301. current = ""
  302. } else {
  303. current += string(c)
  304. }
  305. }
  306. parts = append(parts, current)
  307. return parts
  308. }
  309. // GetAllUserBuckets returns all bucket names for a user
  310. func (s *S3Service) GetAllUserBuckets(accountID string) ([]string, error) {
  311. configs, err := s.db.ListBucketConfigs(accountID)
  312. if err != nil {
  313. return nil, err
  314. }
  315. bucketNames := make([]string, len(configs))
  316. for i, config := range configs {
  317. bucketNames[i] = config.BucketName
  318. }
  319. return bucketNames, nil
  320. }
  321. // DeleteAllBuckets deletes all buckets for a user (useful for cleanup/testing)
  322. func (s *S3Service) DeleteAllBuckets(accountID string) error {
  323. buckets, err := s.GetAllUserBuckets(accountID)
  324. if err != nil {
  325. return err
  326. }
  327. for _, bucket := range buckets {
  328. // Delete all objects first
  329. objects, err := s.ListObjects(accountID, bucket)
  330. if err != nil {
  331. continue
  332. }
  333. for _, obj := range objects {
  334. _ = s.DeleteObject(accountID, bucket, obj.Key)
  335. }
  336. // Delete bucket
  337. _ = s.DeleteBucket(accountID, bucket)
  338. }
  339. return nil
  340. }
  341. // RenameObject renames an object (copy + delete)
  342. func (s *S3Service) RenameObject(accountID, bucket, oldKey, newKey string) error {
  343. // Copy object
  344. if err := s.CopyObject(accountID, bucket, oldKey, bucket, newKey); err != nil {
  345. return err
  346. }
  347. // Delete old object
  348. return s.DeleteObject(accountID, bucket, oldKey)
  349. }
  350. // MoveObject moves an object to another bucket (copy + delete)
  351. func (s *S3Service) MoveObject(accountID, srcBucket, srcKey, dstBucket, dstKey string) error {
  352. // Copy object
  353. if err := s.CopyObject(accountID, srcBucket, srcKey, dstBucket, dstKey); err != nil {
  354. return err
  355. }
  356. // Delete source object
  357. return s.DeleteObject(accountID, srcBucket, srcKey)
  358. }
  359. // CreateMultipartUpload initiates a multipart upload
  360. func (s *S3Service) CreateMultipartUpload(accountID, bucket, key string) (string, error) {
  361. // Check if bucket exists
  362. exists, err := s.storage.BucketExistsForUser(accountID, bucket)
  363. if err != nil {
  364. return "", err
  365. }
  366. if !exists {
  367. return "", storage.ErrBucketNotFound
  368. }
  369. return s.storage.CreateMultipartUpload(accountID, bucket, key)
  370. }
  371. // UploadPart uploads a part for a multipart upload
  372. func (s *S3Service) UploadPart(uploadID string, partNumber int, data io.Reader) (string, error) {
  373. // Validate part number (1-10000)
  374. if partNumber < 1 || partNumber > 10000 {
  375. return "", fmt.Errorf("part number must be between 1 and 10000")
  376. }
  377. return s.storage.UploadPart(uploadID, partNumber, data)
  378. }
  379. // ListParts lists parts of a multipart upload
  380. func (s *S3Service) ListParts(uploadID string, maxParts, partNumberMarker int) ([]storage.PartInfo, int, bool, error) {
  381. if maxParts <= 0 {
  382. maxParts = 1000 // Default max parts
  383. }
  384. if maxParts > 1000 {
  385. maxParts = 1000 // S3 limit
  386. }
  387. return s.storage.ListParts(uploadID, maxParts, partNumberMarker)
  388. }
  389. // GetMultipartUpload gets multipart upload information
  390. func (s *S3Service) GetMultipartUpload(uploadID string) (*storage.MultipartUploadInfo, error) {
  391. return s.storage.GetMultipartUpload(uploadID)
  392. }
  393. // AbortMultipartUpload cancels a multipart upload
  394. func (s *S3Service) AbortMultipartUpload(uploadID string) error {
  395. return s.storage.AbortMultipartUpload(uploadID)
  396. }
  397. // CompleteMultipartUpload finalizes a multipart upload
  398. func (s *S3Service) CompleteMultipartUpload(uploadID string, parts []int) error {
  399. // Validate parts are in order
  400. for i := 1; i < len(parts); i++ {
  401. if parts[i] <= parts[i-1] {
  402. return fmt.Errorf("parts must be in ascending order")
  403. }
  404. }
  405. return s.storage.CompleteMultipartUpload(uploadID, parts)
  406. }
  407. // ListMultipartUploads lists in-progress multipart uploads for a bucket
  408. func (s *S3Service) ListMultipartUploads(accountID, bucket string, maxUploads int, keyMarker, uploadIDMarker string) ([]storage.MultipartUploadInfo, string, string, bool, error) {
  409. // Check if bucket exists
  410. exists, err := s.storage.BucketExistsForUser(accountID, bucket)
  411. if err != nil {
  412. return nil, "", "", false, err
  413. }
  414. if !exists {
  415. return nil, "", "", false, storage.ErrBucketNotFound
  416. }
  417. if maxUploads <= 0 {
  418. maxUploads = 1000
  419. }
  420. if maxUploads > 1000 {
  421. maxUploads = 1000
  422. }
  423. return s.storage.ListMultipartUploads(accountID, bucket, maxUploads, keyMarker, uploadIDMarker)
  424. }