-
Notifications
You must be signed in to change notification settings - Fork 25
/
query.go
96 lines (84 loc) · 2.57 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package authorizer
import (
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2"
"github.com/aserto-dev/go-authorizer/aserto/authorizer/v2/api"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/aserto-dev/topaz/pkg/cli/clients"
"github.com/aserto-dev/topaz/pkg/cli/edit"
"github.com/aserto-dev/topaz/pkg/cli/fflag"
"github.com/aserto-dev/topaz/pkg/cli/jsonx"
"github.com/aserto-dev/topaz/pkg/cli/prompter"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
)
type QueryCmd struct {
Request string `arg:"" type:"string" name:"request" optional:"" help:"json request or file path to check permission request or '-' to read from stdin"`
Template bool `name:"template" short:"t" help:"prints a check permission request template on stdout"`
Editor bool `name:"edit" short:"e" help:"edit config file" hidden:"" type:"fflag.Editor"`
clients.AuthorizerConfig
}
func (cmd *QueryCmd) Run(c *cc.CommonCtx) error {
if cmd.Template {
return jsonx.OutputJSONPB(c.UI.Output(), cmd.template())
}
client, err := clients.NewAuthorizerClient(c, &cmd.AuthorizerConfig)
if err != nil {
return errors.Wrap(err, "failed to get authorizer client")
}
if cmd.Request == "" && cmd.Editor && fflag.Enabled(fflag.Editor) {
req, err := edit.Msg(cmd.template())
if err != nil {
return err
}
cmd.Request = req
}
if cmd.Request == "" && fflag.Enabled(fflag.Prompter) {
p := prompter.New(cmd.template())
if err := p.Show(); err != nil {
return err
}
cmd.Request = jsonx.MaskedMarshalOpts().Format(p.Req())
}
if cmd.Request == "" {
return errors.New("request argument is required")
}
if cmd.Request == "" {
return errors.New("request argument is required")
}
var req authorizer.QueryRequest
err = clients.UnmarshalRequest(cmd.Request, &req)
if err != nil {
return err
}
resp, err := client.Query(c.Context, &req)
if err != nil {
return err
}
return jsonx.OutputJSONPB(c.UI.Output(), resp)
}
func (cmd *QueryCmd) template() proto.Message {
return &authorizer.QueryRequest{
Query: "",
Input: "",
Options: &authorizer.QueryOptions{
Metrics: false,
Instrument: false,
Trace: authorizer.TraceLevel_TRACE_LEVEL_FAILS,
TraceSummary: false,
},
PolicyContext: &api.PolicyContext{
Path: "",
Decisions: []string{"allowed"},
},
IdentityContext: &api.IdentityContext{
Identity: "",
Type: api.IdentityType_IDENTITY_TYPE_NONE,
},
ResourceContext: &structpb.Struct{},
PolicyInstance: &api.PolicyInstance{
Name: "",
InstanceLabel: "",
},
}
}