Skip to content

Commit 7ba383d

Browse files
authoredApr 18, 2024··
feat: Add cerbosctl inspect policies command (#2101)
``` POLICY ID ACTIONS resource.leave_request.vdefault *,create,submit,view resource.leave_request.vdefault/regional *,create,submit,view principal.donald_duck.v20210210 create,delete ``` Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
1 parent 2e335d5 commit 7ba383d

File tree

8 files changed

+115
-1
lines changed

8 files changed

+115
-1
lines changed
 

‎cmd/cerbosctl/inspect/inspect.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2021-2024 Zenauth Ltd.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package inspect
5+
6+
type Cmd struct {
7+
Policies PoliciesCmd `cmd:"" name:"policies" aliases:"p" help:"Inspect policies in the store"`
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2021-2024 Zenauth Ltd.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package flagset
5+
6+
type Filters struct {
7+
NameRegexp string `help:"Filter policies by name, using regular expression"`
8+
VersionRegexp string `help:"Filter policies by version, using regular expression"`
9+
ScopeRegexp string `help:"Filter policies by scope, using regular expression"`
10+
IncludeDisabled bool `help:"Include disabled policies"`
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2021-2024 Zenauth Ltd.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package flagset
5+
6+
type Format struct {
7+
NoHeaders bool `help:"Do not output headers"`
8+
}

‎cmd/cerbosctl/inspect/policies.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2021-2024 Zenauth Ltd.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package inspect
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"strings"
10+
11+
"github.com/alecthomas/kong"
12+
"github.com/cerbos/cerbos-sdk-go/cerbos"
13+
14+
"github.com/cerbos/cerbos/cmd/cerbosctl/inspect/internal/flagset"
15+
"github.com/cerbos/cerbos/cmd/cerbosctl/internal/client"
16+
"github.com/cerbos/cerbos/cmd/cerbosctl/internal/printer"
17+
)
18+
19+
const (
20+
help = `# Inspect policies
21+
22+
cerbosctl inspect policies
23+
24+
# Inspect policies, print no headers
25+
26+
cerbosctl inspect policies`
27+
separator = ","
28+
)
29+
30+
type PoliciesCmd struct {
31+
flagset.Filters
32+
flagset.Format
33+
}
34+
35+
func (c *PoliciesCmd) Run(k *kong.Kong, cctx *client.Context) error {
36+
var opts []cerbos.FilterOption
37+
if c.Filters.IncludeDisabled {
38+
opts = append(opts, cerbos.WithIncludeDisabled())
39+
}
40+
if c.Filters.NameRegexp != "" {
41+
opts = append(opts, cerbos.WithNameRegexp(c.Filters.NameRegexp))
42+
}
43+
if c.Filters.ScopeRegexp != "" {
44+
opts = append(opts, cerbos.WithScopeRegexp(c.Filters.ScopeRegexp))
45+
}
46+
if c.Filters.VersionRegexp != "" {
47+
opts = append(opts, cerbos.WithVersionRegexp(c.Filters.VersionRegexp))
48+
}
49+
50+
response, err := cctx.AdminClient.InspectPolicies(context.Background(), opts...)
51+
if err != nil {
52+
return fmt.Errorf("error while inspecting policies: %w", err)
53+
}
54+
55+
tw := printer.NewTableWriter(k.Stdout)
56+
if !c.Format.NoHeaders {
57+
tw.SetHeader([]string{"POLICY ID", "ACTIONS"})
58+
}
59+
60+
for policyKey, result := range response.Results {
61+
actions := strings.Join(result.Actions, separator)
62+
tw.Append([]string{policyKey, actions})
63+
}
64+
65+
tw.Render()
66+
return nil
67+
}
68+
69+
func (c *PoliciesCmd) Help() string {
70+
return help
71+
}

‎cmd/cerbosctl/root/root.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/cerbos/cerbos/cmd/cerbosctl/enable"
1212
"github.com/cerbos/cerbos/cmd/cerbosctl/get"
1313
"github.com/cerbos/cerbos/cmd/cerbosctl/hub"
14+
"github.com/cerbos/cerbos/cmd/cerbosctl/inspect"
1415
"github.com/cerbos/cerbos/cmd/cerbosctl/internal/flagset"
1516
"github.com/cerbos/cerbos/cmd/cerbosctl/put"
1617
"github.com/cerbos/cerbos/cmd/cerbosctl/store"
@@ -21,6 +22,7 @@ type Cli struct {
2122
Get get.Cmd `cmd:"" help:"List or view policies and schemas"`
2223
Hub hub.Cmd `cmd:"" help:"Cerbos Hub operations"`
2324
flagset.Globals
25+
Inspect inspect.Cmd `cmd:"" help:"Inspect policies"`
2426
Store store.Cmd `cmd:"" help:"Store operations"`
2527
Delete del.Cmd `cmd:"" help:"Delete schemas"`
2628
Disable disable.Cmd `cmd:"" help:"Disable policies"`

‎docs/modules/cli/pages/cerbosctl.adoc

+10
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ embedded PDP bundle. If none of the policies in the repo are annotated, they are
372372
cerbosctl hub epdp list-candidates ./path/to/repository
373373
----
374374

375+
[#inspect-policies]
376+
== `inspect policies`
377+
378+
This command is to inspect policies in the store. Currently, it lists actions defined in the policies.
379+
380+
.Inspect policies
381+
----
382+
cerbosctl inspect policies
383+
----
384+
375385
[#put]
376386
== `put`
377387

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/bluele/gcache v0.0.2
1313
github.com/bufbuild/protovalidate-go v0.6.1
1414
github.com/cenkalti/backoff/v4 v4.3.0
15-
github.com/cerbos/cerbos-sdk-go v0.2.4
15+
github.com/cerbos/cerbos-sdk-go v0.2.5-0.20240415074433-be5e6dca0cce
1616
github.com/cerbos/cerbos/api/genpb v0.35.1
1717
github.com/cerbos/cloud-api v0.1.18
1818
github.com/cespare/xxhash v1.1.0

‎go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMr
157157
github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
158158
github.com/cerbos/cerbos-sdk-go v0.2.4 h1:OE0T6728Ry4acFd9pb8vRxizBGDJFvluwxxIHPjLQQg=
159159
github.com/cerbos/cerbos-sdk-go v0.2.4/go.mod h1:q+ORcpV5KLvubtRlPFB39hi5b+pvDE3lC6ZEgNks6rw=
160+
github.com/cerbos/cerbos-sdk-go v0.2.5-0.20240408141506-00afe19d074b h1:a2fSjB/48GDJPZA+qDIP0NHfEUrTiIlVqsGYac/qFEk=
161+
github.com/cerbos/cerbos-sdk-go v0.2.5-0.20240408141506-00afe19d074b/go.mod h1:E5rynfR0AHdvdz69CEk1/LbUH7apq/a3ybjjK65TXw4=
162+
github.com/cerbos/cerbos-sdk-go v0.2.5-0.20240415074433-be5e6dca0cce h1:Bmn1LQpcZklykycAmSE+Bv62lECPBXoLQaAUAB3BcxM=
163+
github.com/cerbos/cerbos-sdk-go v0.2.5-0.20240415074433-be5e6dca0cce/go.mod h1:Utzg7mpDNkJ01MG6pdlinsgwrzU/XIzuQ9MFvYQskcs=
160164
github.com/cerbos/cerbos/api/genpb v0.35.1 h1:IgmvEUg+FXachWUT9f2b7NQ650kogwAEoeyMpx8kQYg=
161165
github.com/cerbos/cerbos/api/genpb v0.35.1/go.mod h1:DcozdAIUztxXwtVs88gGgdyCITru7WCTF9vGA6j+H8k=
162166
github.com/cerbos/cloud-api v0.1.18 h1:psD9psLgNq/u1IEOsp8L2mwwvB4umGeA4C4YHw19G9g=

0 commit comments

Comments
 (0)
Please sign in to comment.