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.

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