You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.9 KiB

5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/security"
  10. "github.com/gorilla/mux"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/s3api"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. var (
  16. s3StandaloneOptions S3Options
  17. )
  18. type S3Options struct {
  19. filer *string
  20. port *int
  21. config *string
  22. domainName *string
  23. tlsPrivateKey *string
  24. tlsCertificate *string
  25. }
  26. func init() {
  27. cmdS3.Run = runS3 // break init cycle
  28. s3StandaloneOptions.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address")
  29. s3StandaloneOptions.port = cmdS3.Flag.Int("port", 8333, "s3 server http listen port")
  30. s3StandaloneOptions.domainName = cmdS3.Flag.String("domainName", "", "suffix of the host name, {bucket}.{domainName}")
  31. s3StandaloneOptions.config = cmdS3.Flag.String("config", "", "path to the config file")
  32. s3StandaloneOptions.tlsPrivateKey = cmdS3.Flag.String("key.file", "", "path to the TLS private key file")
  33. s3StandaloneOptions.tlsCertificate = cmdS3.Flag.String("cert.file", "", "path to the TLS certificate file")
  34. }
  35. var cmdS3 = &Command{
  36. UsageLine: "s3 [-port=8333] [-filer=<ip:port>] [-config=</path/to/config.json>]",
  37. Short: "start a s3 API compatible server that is backed by a filer",
  38. Long: `start a s3 API compatible server that is backed by a filer.
  39. By default, you can use any access key and secret key to access the S3 APIs.
  40. To enable credential based access, create a config.json file similar to this:
  41. {
  42. "identities": [
  43. {
  44. "name": "some_name",
  45. "credentials": [
  46. {
  47. "accessKey": "some_access_key1",
  48. "secretKey": "some_secret_key1"
  49. }
  50. ],
  51. "actions": [
  52. "Admin",
  53. "Read",
  54. "Write"
  55. ]
  56. },
  57. {
  58. "name": "some_read_only_user",
  59. "credentials": [
  60. {
  61. "accessKey": "some_access_key2",
  62. "secretKey": "some_secret_key2"
  63. }
  64. ],
  65. "actions": [
  66. "Read"
  67. ]
  68. },
  69. {
  70. "name": "some_normal_user",
  71. "credentials": [
  72. {
  73. "accessKey": "some_access_key3",
  74. "secretKey": "some_secret_key3"
  75. }
  76. ],
  77. "actions": [
  78. "Read",
  79. "Write"
  80. ]
  81. },
  82. {
  83. "name": "user_limited_to_bucket1",
  84. "credentials": [
  85. {
  86. "accessKey": "some_access_key4",
  87. "secretKey": "some_secret_key4"
  88. }
  89. ],
  90. "actions": [
  91. "Read:bucket1",
  92. "Write:bucket1"
  93. ]
  94. }
  95. ]
  96. }
  97. `,
  98. }
  99. func runS3(cmd *Command, args []string) bool {
  100. util.LoadConfiguration("security", false)
  101. return s3StandaloneOptions.startS3Server()
  102. }
  103. func (s3opt *S3Options) startS3Server() bool {
  104. filerGrpcAddress, err := pb.ParseFilerGrpcAddress(*s3opt.filer)
  105. if err != nil {
  106. glog.Fatal(err)
  107. return false
  108. }
  109. filerBucketsPath := "/buckets"
  110. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  111. for {
  112. err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  113. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  114. if err != nil {
  115. return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
  116. }
  117. filerBucketsPath = resp.DirBuckets
  118. glog.V(0).Infof("S3 read filer buckets dir: %s", filerBucketsPath)
  119. return nil
  120. })
  121. if err != nil {
  122. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *s3opt.filer, filerGrpcAddress)
  123. time.Sleep(time.Second)
  124. } else {
  125. glog.V(0).Infof("connected to filer %s grpc address %s", *s3opt.filer, filerGrpcAddress)
  126. break
  127. }
  128. }
  129. router := mux.NewRouter().SkipClean(true)
  130. _, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{
  131. Filer: *s3opt.filer,
  132. FilerGrpcAddress: filerGrpcAddress,
  133. Config: *s3opt.config,
  134. DomainName: *s3opt.domainName,
  135. BucketsPath: filerBucketsPath,
  136. GrpcDialOption: grpcDialOption,
  137. })
  138. if s3ApiServer_err != nil {
  139. glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
  140. }
  141. httpS := &http.Server{Handler: router}
  142. listenAddress := fmt.Sprintf(":%d", *s3opt.port)
  143. s3ApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  144. if err != nil {
  145. glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err)
  146. }
  147. if *s3opt.tlsPrivateKey != "" {
  148. glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.VERSION, *s3opt.port)
  149. if err = httpS.ServeTLS(s3ApiListener, *s3opt.tlsCertificate, *s3opt.tlsPrivateKey); err != nil {
  150. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  151. }
  152. } else {
  153. glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.VERSION, *s3opt.port)
  154. if err = httpS.Serve(s3ApiListener); err != nil {
  155. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  156. }
  157. }
  158. return true
  159. }