2020-12-09 12:11:49 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/iamapi"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2020-12-09 12:11:49 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
iamStandaloneOptions IamOptions
|
|
|
|
)
|
|
|
|
|
|
|
|
type IamOptions struct {
|
|
|
|
filer *string
|
|
|
|
masters *string
|
2022-03-16 05:28:18 +00:00
|
|
|
ip *string
|
2020-12-09 12:11:49 +00:00
|
|
|
port *int
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdIam.Run = runIam // break init cycle
|
|
|
|
iamStandaloneOptions.filer = cmdIam.Flag.String("filer", "localhost:8888", "filer server address")
|
|
|
|
iamStandaloneOptions.masters = cmdIam.Flag.String("master", "localhost:9333", "comma-separated master servers")
|
2022-03-16 05:28:18 +00:00
|
|
|
iamStandaloneOptions.ip = cmdIam.Flag.String("ip", util.DetectedHostAddress(), "iam server http listen ip address")
|
2020-12-09 12:11:49 +00:00
|
|
|
iamStandaloneOptions.port = cmdIam.Flag.Int("port", 8111, "iam server http listen port")
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdIam = &Command{
|
|
|
|
UsageLine: "iam [-port=8111] [-filer=<ip:port>] [-masters=<ip:port>,<ip:port>]",
|
|
|
|
Short: "start a iam API compatible server",
|
|
|
|
Long: "start a iam API compatible server.",
|
|
|
|
}
|
|
|
|
|
|
|
|
func runIam(cmd *Command, args []string) bool {
|
|
|
|
return iamStandaloneOptions.startIamServer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iamopt *IamOptions) startIamServer() bool {
|
2021-09-13 05:47:52 +00:00
|
|
|
filerAddress := pb.ServerAddress(*iamopt.filer)
|
2020-12-09 12:11:49 +00:00
|
|
|
|
2021-08-03 08:30:35 +00:00
|
|
|
util.LoadConfiguration("security", false)
|
2020-12-09 12:11:49 +00:00
|
|
|
grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
|
|
|
|
for {
|
2023-01-20 09:48:12 +00:00
|
|
|
err := pb.WithGrpcFilerClient(false, 0, filerAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
|
2020-12-09 12:11:49 +00:00
|
|
|
resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
|
|
|
|
if err != nil {
|
2021-09-13 05:47:52 +00:00
|
|
|
return fmt.Errorf("get filer %s configuration: %v", filerAddress, err)
|
2020-12-09 12:11:49 +00:00
|
|
|
}
|
|
|
|
glog.V(0).Infof("IAM read filer configuration: %s", resp)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-09-13 05:47:52 +00:00
|
|
|
glog.V(0).Infof("wait to connect to filer %s grpc address %s", *iamopt.filer, filerAddress.ToGrpcAddress())
|
2020-12-09 12:11:49 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
} else {
|
2021-09-13 05:47:52 +00:00
|
|
|
glog.V(0).Infof("connected to filer %s grpc address %s", *iamopt.filer, filerAddress.ToGrpcAddress())
|
2020-12-09 12:11:49 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 20:33:17 +00:00
|
|
|
masters := pb.ServerAddresses(*iamopt.masters).ToAddressMap()
|
2020-12-09 12:11:49 +00:00
|
|
|
router := mux.NewRouter().SkipClean(true)
|
|
|
|
_, iamApiServer_err := iamapi.NewIamApiServer(router, &iamapi.IamServerOption{
|
2021-09-13 05:47:52 +00:00
|
|
|
Masters: masters,
|
|
|
|
Filer: filerAddress,
|
|
|
|
Port: *iamopt.port,
|
|
|
|
GrpcDialOption: grpcDialOption,
|
2020-12-09 12:11:49 +00:00
|
|
|
})
|
|
|
|
glog.V(0).Info("NewIamApiServer created")
|
|
|
|
if iamApiServer_err != nil {
|
|
|
|
glog.Fatalf("IAM API Server startup error: %v", iamApiServer_err)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpS := &http.Server{Handler: router}
|
|
|
|
|
|
|
|
listenAddress := fmt.Sprintf(":%d", *iamopt.port)
|
2022-03-16 05:28:18 +00:00
|
|
|
iamApiListener, iamApiLocalListener, err := util.NewIpAndLocalListeners(*iamopt.ip, *iamopt.port, time.Duration(10)*time.Second)
|
2020-12-09 12:11:49 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("IAM API Server listener on %s error: %v", listenAddress, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(0).Infof("Start Seaweed IAM API Server %s at http port %d", util.Version(), *iamopt.port)
|
2022-03-16 05:28:18 +00:00
|
|
|
if iamApiLocalListener != nil {
|
|
|
|
go func() {
|
|
|
|
if err = httpS.Serve(iamApiLocalListener); err != nil {
|
|
|
|
glog.Errorf("IAM API Server Fail to serve: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-12-09 12:11:49 +00:00
|
|
|
if err = httpS.Serve(iamApiListener); err != nil {
|
|
|
|
glog.Fatalf("IAM API Server Fail to serve: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|