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.

185 lines
4.9 KiB

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