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.

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