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.

149 lines
4.0 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. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/security"
  7. "github.com/gorilla/mux"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/s3api"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. var (
  13. s3StandaloneOptions S3Options
  14. )
  15. type S3Options struct {
  16. filer *string
  17. filerBucketsPath *string
  18. port *int
  19. config *string
  20. domainName *string
  21. tlsPrivateKey *string
  22. tlsCertificate *string
  23. }
  24. func init() {
  25. cmdS3.Run = runS3 // break init cycle
  26. s3StandaloneOptions.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address")
  27. s3StandaloneOptions.filerBucketsPath = cmdS3.Flag.String("filer.dir.buckets", "/buckets", "folder on filer to store all buckets")
  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. }
  83. `,
  84. }
  85. func runS3(cmd *Command, args []string) bool {
  86. util.LoadConfiguration("security", false)
  87. return s3StandaloneOptions.startS3Server()
  88. }
  89. func (s3opt *S3Options) startS3Server() bool {
  90. filerGrpcAddress, err := parseFilerGrpcAddress(*s3opt.filer)
  91. if err != nil {
  92. glog.Fatal(err)
  93. return false
  94. }
  95. router := mux.NewRouter().SkipClean(true)
  96. _, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{
  97. Filer: *s3opt.filer,
  98. FilerGrpcAddress: filerGrpcAddress,
  99. Config: *s3opt.config,
  100. DomainName: *s3opt.domainName,
  101. BucketsPath: *s3opt.filerBucketsPath,
  102. GrpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.client"),
  103. })
  104. if s3ApiServer_err != nil {
  105. glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
  106. }
  107. httpS := &http.Server{Handler: router}
  108. listenAddress := fmt.Sprintf(":%d", *s3opt.port)
  109. s3ApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  110. if err != nil {
  111. glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err)
  112. }
  113. if *s3opt.tlsPrivateKey != "" {
  114. glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.VERSION, *s3opt.port)
  115. if err = httpS.ServeTLS(s3ApiListener, *s3opt.tlsCertificate, *s3opt.tlsPrivateKey); err != nil {
  116. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  117. }
  118. } else {
  119. glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.VERSION, *s3opt.port)
  120. if err = httpS.Serve(s3ApiListener); err != nil {
  121. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  122. }
  123. }
  124. return true
  125. }