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.

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