2020-11-25 20:30:11 +00:00
|
|
|
package s3iam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/iam_pb"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
proto "github.com/golang/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
iamConfigPrefix = "/etc/iam"
|
|
|
|
iamIdentityFile = "identity.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IAMFilerStore struct {
|
|
|
|
client *filer_pb.SeaweedFilerClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIAMFilerStore(client *filer_pb.SeaweedFilerClient) *IAMFilerStore {
|
|
|
|
return &IAMFilerStore{client: client}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ifs *IAMFilerStore) getIAMConfigRequest() *filer_pb.LookupDirectoryEntryRequest {
|
|
|
|
return &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Directory: iamConfigPrefix,
|
|
|
|
Name: iamIdentityFile,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ifs *IAMFilerStore) LoadIAMConfig(config *iam_pb.S3ApiConfiguration) error {
|
|
|
|
resp, err := filer_pb.LookupEntry(*ifs.client, ifs.getIAMConfigRequest())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 11:12:13 +00:00
|
|
|
err = ifs.loadIAMConfigFromEntry(resp.Entry.Content, config)
|
2020-11-25 20:30:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ifs *IAMFilerStore) SaveIAMConfig(config *iam_pb.S3ApiConfiguration) error {
|
|
|
|
entry := &filer_pb.Entry{
|
|
|
|
Name: iamIdentityFile,
|
|
|
|
IsDirectory: false,
|
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
FileMode: uint32(0644),
|
|
|
|
Collection: "",
|
|
|
|
Replication: "",
|
|
|
|
},
|
2020-12-02 11:12:13 +00:00
|
|
|
Content: []byte{},
|
2020-11-25 20:30:11 +00:00
|
|
|
}
|
2020-12-02 11:12:13 +00:00
|
|
|
err := ifs.saveIAMConfigToEntry(entry.Content, config)
|
2020-11-25 20:30:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = filer_pb.LookupEntry(*ifs.client, ifs.getIAMConfigRequest())
|
|
|
|
if err == filer_pb.ErrNotFound {
|
|
|
|
err = filer_pb.CreateEntry(*ifs.client, &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: iamConfigPrefix,
|
|
|
|
Entry: entry,
|
|
|
|
IsFromOtherCluster: false,
|
|
|
|
Signatures: nil,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
err = filer_pb.UpdateEntry(*ifs.client, &filer_pb.UpdateEntryRequest{
|
|
|
|
Directory: iamConfigPrefix,
|
|
|
|
Entry: entry,
|
|
|
|
IsFromOtherCluster: false,
|
|
|
|
Signatures: nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 11:12:13 +00:00
|
|
|
func (ifs *IAMFilerStore) loadIAMConfigFromEntry(content []byte, config *iam_pb.S3ApiConfiguration) error {
|
|
|
|
if err := proto.Unmarshal(content, config); err != nil {
|
|
|
|
return err
|
2020-11-25 20:30:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 11:12:13 +00:00
|
|
|
func (ifs *IAMFilerStore) saveIAMConfigToEntry(content []byte, config *iam_pb.S3ApiConfiguration) error {
|
|
|
|
content, err := proto.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-25 20:30:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|