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.

265 lines
8.1 KiB

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