mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-07-30 20:13:23 +00:00
When jwt.filer_signing.key is set, the filer's IamGrpcServer requires a Bearer token on every IAM RPC. The shell's s3.* IAM commands dialed without that header and failed with Unauthenticated. Route them through a small helper that mints a token from the same key viper-loaded from security.toml and appends it as outgoing metadata, matching the credential grpc_store pattern.
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package shell
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
|
|
)
|
|
|
|
func init() {
|
|
Commands = append(Commands, &commandS3PolicyDetach{})
|
|
}
|
|
|
|
type commandS3PolicyDetach struct {
|
|
}
|
|
|
|
func (c *commandS3PolicyDetach) Name() string {
|
|
return "s3.policy.detach"
|
|
}
|
|
|
|
func (c *commandS3PolicyDetach) Help() string {
|
|
return `detach a policy from an S3 IAM user
|
|
|
|
s3.policy.detach -policy <policy_name> -user <username>
|
|
`
|
|
}
|
|
|
|
func (c *commandS3PolicyDetach) HasTag(CommandTag) bool {
|
|
return false
|
|
}
|
|
|
|
func (c *commandS3PolicyDetach) Do(args []string, commandEnv *CommandEnv, writer io.Writer) error {
|
|
f := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
policy := f.String("policy", "", "policy name")
|
|
user := f.String("user", "", "user name")
|
|
if err := f.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
|
|
if *policy == "" {
|
|
return fmt.Errorf("-policy is required")
|
|
}
|
|
if *user == "" {
|
|
return fmt.Errorf("-user is required")
|
|
}
|
|
|
|
return commandEnv.withIamClient(func(ctx context.Context, client iam_pb.SeaweedIdentityAccessManagementClient) error {
|
|
resp, err := client.GetUser(ctx, &iam_pb.GetUserRequest{Username: *user})
|
|
if err != nil {
|
|
return fmt.Errorf("get user %q: %w", *user, err)
|
|
}
|
|
if resp.Identity == nil {
|
|
return fmt.Errorf("user %q returned empty identity", *user)
|
|
}
|
|
|
|
found := false
|
|
var kept []string
|
|
for _, p := range resp.Identity.PolicyNames {
|
|
if p == *policy {
|
|
found = true
|
|
} else {
|
|
kept = append(kept, p)
|
|
}
|
|
}
|
|
if !found {
|
|
return fmt.Errorf("policy %q is not attached to user %q", *policy, *user)
|
|
}
|
|
|
|
resp.Identity.PolicyNames = kept
|
|
_, err = client.UpdateUser(ctx, &iam_pb.UpdateUserRequest{
|
|
Username: *user,
|
|
Identity: resp.Identity,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return json.NewEncoder(writer).Encode(map[string]string{"policy": *policy, "user": *user})
|
|
})
|
|
}
|