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.

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