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.

244 lines
7.3 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. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  6. "net/http"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/pb"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/gorilla/mux"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/s3api"
  14. stats_collect "github.com/chrislusf/seaweedfs/weed/stats"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. var (
  18. s3StandaloneOptions S3Options
  19. )
  20. type S3Options struct {
  21. filer *string
  22. bindIp *string
  23. port *int
  24. config *string
  25. domainName *string
  26. tlsPrivateKey *string
  27. tlsCertificate *string
  28. metricsHttpPort *int
  29. allowEmptyFolder *bool
  30. allowDeleteBucketNotEmpty *bool
  31. auditLogConfig *string
  32. localFilerSocket *string
  33. }
  34. func init() {
  35. cmdS3.Run = runS3 // break init cycle
  36. s3StandaloneOptions.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address")
  37. s3StandaloneOptions.bindIp = cmdS3.Flag.String("ip.bind", "", "ip address to bind to. Default to localhost.")
  38. s3StandaloneOptions.port = cmdS3.Flag.Int("port", 8333, "s3 server http listen port")
  39. s3StandaloneOptions.domainName = cmdS3.Flag.String("domainName", "", "suffix of the host name in comma separated list, {bucket}.{domainName}")
  40. s3StandaloneOptions.config = cmdS3.Flag.String("config", "", "path to the config file")
  41. s3StandaloneOptions.auditLogConfig = cmdS3.Flag.String("auditLogConfig", "", "path to the audit log config file")
  42. s3StandaloneOptions.tlsPrivateKey = cmdS3.Flag.String("key.file", "", "path to the TLS private key file")
  43. s3StandaloneOptions.tlsCertificate = cmdS3.Flag.String("cert.file", "", "path to the TLS certificate file")
  44. s3StandaloneOptions.metricsHttpPort = cmdS3.Flag.Int("metricsPort", 0, "Prometheus metrics listen port")
  45. s3StandaloneOptions.allowEmptyFolder = cmdS3.Flag.Bool("allowEmptyFolder", true, "allow empty folders")
  46. s3StandaloneOptions.allowDeleteBucketNotEmpty = cmdS3.Flag.Bool("allowDeleteBucketNotEmpty", true, "allow recursive deleting all entries along with bucket")
  47. }
  48. var cmdS3 = &Command{
  49. UsageLine: "s3 [-port=8333] [-filer=<ip:port>] [-config=</path/to/config.json>]",
  50. Short: "start a s3 API compatible server that is backed by a filer",
  51. Long: `start a s3 API compatible server that is backed by a filer.
  52. By default, you can use any access key and secret key to access the S3 APIs.
  53. To enable credential based access, create a config.json file similar to this:
  54. {
  55. "identities": [
  56. {
  57. "name": "anonymous",
  58. "actions": [
  59. "Read"
  60. ]
  61. },
  62. {
  63. "name": "some_admin_user",
  64. "credentials": [
  65. {
  66. "accessKey": "some_access_key1",
  67. "secretKey": "some_secret_key1"
  68. }
  69. ],
  70. "actions": [
  71. "Admin",
  72. "Read",
  73. "List",
  74. "Tagging",
  75. "Write"
  76. ]
  77. },
  78. {
  79. "name": "some_read_only_user",
  80. "credentials": [
  81. {
  82. "accessKey": "some_access_key2",
  83. "secretKey": "some_secret_key2"
  84. }
  85. ],
  86. "actions": [
  87. "Read"
  88. ]
  89. },
  90. {
  91. "name": "some_normal_user",
  92. "credentials": [
  93. {
  94. "accessKey": "some_access_key3",
  95. "secretKey": "some_secret_key3"
  96. }
  97. ],
  98. "actions": [
  99. "Read",
  100. "List",
  101. "Tagging",
  102. "Write"
  103. ]
  104. },
  105. {
  106. "name": "user_limited_to_bucket1",
  107. "credentials": [
  108. {
  109. "accessKey": "some_access_key4",
  110. "secretKey": "some_secret_key4"
  111. }
  112. ],
  113. "actions": [
  114. "Read:bucket1",
  115. "List:bucket1",
  116. "Tagging:bucket1",
  117. "Write:bucket1"
  118. ]
  119. }
  120. ]
  121. }
  122. `,
  123. }
  124. func runS3(cmd *Command, args []string) bool {
  125. util.LoadConfiguration("security", false)
  126. go stats_collect.StartMetricsServer(*s3StandaloneOptions.metricsHttpPort)
  127. return s3StandaloneOptions.startS3Server()
  128. }
  129. func (s3opt *S3Options) startS3Server() bool {
  130. filerAddress := pb.ServerAddress(*s3opt.filer)
  131. filerBucketsPath := "/buckets"
  132. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  133. // metrics read from the filer
  134. var metricsAddress string
  135. var metricsIntervalSec int
  136. for {
  137. err := pb.WithGrpcFilerClient(false, filerAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  138. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  139. if err != nil {
  140. return fmt.Errorf("get filer %s configuration: %v", filerAddress, err)
  141. }
  142. filerBucketsPath = resp.DirBuckets
  143. metricsAddress, metricsIntervalSec = resp.MetricsAddress, int(resp.MetricsIntervalSec)
  144. glog.V(0).Infof("S3 read filer buckets dir: %s", filerBucketsPath)
  145. return nil
  146. })
  147. if err != nil {
  148. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *s3opt.filer, filerAddress.ToGrpcAddress())
  149. time.Sleep(time.Second)
  150. } else {
  151. glog.V(0).Infof("connected to filer %s grpc address %s", *s3opt.filer, filerAddress.ToGrpcAddress())
  152. break
  153. }
  154. }
  155. go stats_collect.LoopPushingMetric("s3", stats_collect.SourceName(uint32(*s3opt.port)), metricsAddress, metricsIntervalSec)
  156. router := mux.NewRouter().SkipClean(true)
  157. _, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{
  158. Filer: filerAddress,
  159. Port: *s3opt.port,
  160. Config: *s3opt.config,
  161. DomainName: *s3opt.domainName,
  162. BucketsPath: filerBucketsPath,
  163. GrpcDialOption: grpcDialOption,
  164. AllowEmptyFolder: *s3opt.allowEmptyFolder,
  165. AllowDeleteBucketNotEmpty: *s3opt.allowDeleteBucketNotEmpty,
  166. LocalFilerSocket: s3opt.localFilerSocket,
  167. })
  168. if s3ApiServer_err != nil {
  169. glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
  170. }
  171. httpS := &http.Server{Handler: router}
  172. if *s3opt.bindIp == "" {
  173. *s3opt.bindIp = "localhost"
  174. }
  175. listenAddress := fmt.Sprintf("%s:%d", *s3opt.bindIp, *s3opt.port)
  176. s3ApiListener, s3ApiLocalListner, err := util.NewIpAndLocalListeners(*s3opt.bindIp, *s3opt.port, time.Duration(10)*time.Second)
  177. if err != nil {
  178. glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err)
  179. }
  180. if len(*s3opt.auditLogConfig) > 0 {
  181. s3err.InitAuditLog(*s3opt.auditLogConfig)
  182. if s3err.Logger != nil {
  183. defer s3err.Logger.Close()
  184. }
  185. }
  186. if *s3opt.tlsPrivateKey != "" {
  187. glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.Version(), *s3opt.port)
  188. if s3ApiLocalListner != nil {
  189. go func() {
  190. if err = httpS.ServeTLS(s3ApiLocalListner, *s3opt.tlsCertificate, *s3opt.tlsPrivateKey); err != nil {
  191. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  192. }
  193. }()
  194. }
  195. if err = httpS.ServeTLS(s3ApiListener, *s3opt.tlsCertificate, *s3opt.tlsPrivateKey); err != nil {
  196. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  197. }
  198. } else {
  199. glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.Version(), *s3opt.port)
  200. if s3ApiLocalListner != nil {
  201. go func() {
  202. if err = httpS.Serve(s3ApiLocalListner); err != nil {
  203. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  204. }
  205. }()
  206. }
  207. if err = httpS.Serve(s3ApiListener); err != nil {
  208. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  209. }
  210. }
  211. return true
  212. }