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.
72 lines
2.0 KiB
72 lines
2.0 KiB
package grpc
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/credential"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
func init() {
|
|
credential.Stores = append(credential.Stores, &IamGrpcStore{})
|
|
}
|
|
|
|
// IamGrpcStore implements CredentialStore using SeaweedFS IAM gRPC service
|
|
type IamGrpcStore struct {
|
|
filerAddressFunc func() pb.ServerAddress // Function to get current active filer
|
|
grpcDialOption grpc.DialOption
|
|
mu sync.RWMutex // Protects filerAddressFunc and grpcDialOption
|
|
}
|
|
|
|
func (store *IamGrpcStore) GetName() credential.CredentialStoreTypeName {
|
|
return credential.StoreTypeGrpc
|
|
}
|
|
|
|
func (store *IamGrpcStore) Initialize(configuration util.Configuration, prefix string) error {
|
|
if configuration != nil {
|
|
filerAddr := configuration.GetString(prefix + "filer")
|
|
if filerAddr != "" {
|
|
store.mu.Lock()
|
|
store.filerAddressFunc = func() pb.ServerAddress {
|
|
return pb.ServerAddress(filerAddr)
|
|
}
|
|
store.mu.Unlock()
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *IamGrpcStore) SetFilerAddressFunc(getFiler func() pb.ServerAddress, grpcDialOption grpc.DialOption) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.filerAddressFunc = getFiler
|
|
store.grpcDialOption = grpcDialOption
|
|
}
|
|
|
|
func (store *IamGrpcStore) withIamClient(fn func(client iam_pb.SeaweedIdentityAccessManagementClient) error) error {
|
|
store.mu.RLock()
|
|
if store.filerAddressFunc == nil {
|
|
store.mu.RUnlock()
|
|
return fmt.Errorf("iam_grpc: filer not yet available")
|
|
}
|
|
|
|
filerAddress := store.filerAddressFunc()
|
|
dialOption := store.grpcDialOption
|
|
store.mu.RUnlock()
|
|
|
|
if filerAddress == "" {
|
|
return fmt.Errorf("iam_grpc: no filer discovered yet")
|
|
}
|
|
|
|
return pb.WithGrpcClient(false, 0, func(conn *grpc.ClientConn) error {
|
|
client := iam_pb.NewSeaweedIdentityAccessManagementClient(conn)
|
|
return fn(client)
|
|
}, filerAddress.ToGrpcAddress(), false, dialOption)
|
|
}
|
|
|
|
func (store *IamGrpcStore) Shutdown() {
|
|
}
|