Files
kopia/cli/command_acl_add.go
Jarek Kowalski 044db7593b feat(repository): apply retention policies server-side (#3249)
* feat(repository): apply retention policies server-side

This allows append-only snapshots where the client can never delete
arbitrary manifests and policies are maintained on the server.

The client only needs permissions to create snapshots in a given, which
automatically gives them permission to invoke the server-side method
for their own snapshots only.

* Update cli/command_acl_add.go

Co-authored-by: Guillaume <Gui13@users.noreply.github.com>

* Update internal/server/api_manifest.go

Co-authored-by: Guillaume <Gui13@users.noreply.github.com>

* Update internal/server/api_manifest.go

Co-authored-by: Guillaume <Gui13@users.noreply.github.com>

* Update internal/server/grpc_session.go

Co-authored-by: Guillaume <Gui13@users.noreply.github.com>

---------

Co-authored-by: Guillaume <Gui13@users.noreply.github.com>
2023-09-02 18:23:21 -07:00

54 lines
1.4 KiB
Go

package cli
import (
"context"
"strings"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/acl"
"github.com/kopia/kopia/repo"
)
type commandACLAdd struct {
user string
target string
level string
overwrite bool
}
func (c *commandACLAdd) setup(svc appServices, parent commandParent) {
cmd := parent.Command("add", "Add ACL entry")
cmd.Flag("user", "User the ACL targets").Required().StringVar(&c.user)
cmd.Flag("target", "Manifests targeted by the rule (type:T,key1:value1,...,keyN:valueN)").Required().StringVar(&c.target)
cmd.Flag("access", "Access the user gets to subject").Required().EnumVar(&c.level, acl.SupportedAccessLevels()...)
cmd.Flag("overwrite", "Overwrite existing rule with the same user and target").BoolVar(&c.overwrite)
cmd.Action(svc.repositoryWriterAction(c.run))
}
func (c *commandACLAdd) run(ctx context.Context, rep repo.RepositoryWriter) error {
r := acl.TargetRule{}
for _, v := range strings.Split(c.target, ",") {
parts := strings.SplitN(v, "=", 2) //nolint:gomnd
if len(parts) != 2 { //nolint:gomnd
return errors.Errorf("invalid target labels %q, must be key=value", v)
}
r[parts[0]] = parts[1]
}
al, err := acl.ParseAccessLevel(c.level)
if err != nil {
return errors.Wrap(err, "invalid access level")
}
e := &acl.Entry{
User: c.user,
Target: r,
Access: al,
}
return errors.Wrap(acl.AddACL(ctx, rep, e, c.overwrite), "error adding ACL entry")
}