-
Notifications
You must be signed in to change notification settings - Fork 332
/
auth_users_policies_list.go
57 lines (48 loc) · 1.83 KB
/
auth_users_policies_list.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
package cmd
import (
"net/http"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/treeverse/lakefs/pkg/api/apigen"
"github.com/treeverse/lakefs/pkg/api/apiutil"
)
var authUsersPoliciesList = &cobra.Command{
Use: "list",
Short: "List policies for the given user",
Run: func(cmd *cobra.Command, args []string) {
id := Must(cmd.Flags().GetString("id"))
amount := Must(cmd.Flags().GetInt("amount"))
after := Must(cmd.Flags().GetString("after"))
effective := Must(cmd.Flags().GetBool("effective"))
clt := getClient()
resp, err := clt.ListUserPoliciesWithResponse(cmd.Context(), id, &apigen.ListUserPoliciesParams{
After: apiutil.Ptr(apigen.PaginationAfter(after)),
Amount: apiutil.Ptr(apigen.PaginationAmount(amount)),
Effective: &effective,
})
DieOnErrorOrUnexpectedStatusCode(resp, err, http.StatusOK)
if resp.JSON200 == nil {
Die("Bad response from server", 1)
}
policies := resp.JSON200.Results
rows := make([][]interface{}, 0)
for _, policy := range policies {
for i, statement := range policy.Statement {
ts := time.Unix(*policy.CreationDate, 0).String()
rows = append(rows, []interface{}{policy.Id, ts, i, statement.Resource, statement.Effect, strings.Join(statement.Action, ", ")})
}
}
pagination := resp.JSON200.Pagination
PrintTable(rows, []interface{}{"Policy ID", "Creation Date", "Statement #", "Resource", "Effect", "Actions"}, &pagination, amount)
},
}
//nolint:gochecknoinits
func init() {
authUsersPoliciesList.Flags().Bool("effective", false,
"List all distinct policies attached to the user, including by group memberships")
authUsersPoliciesList.Flags().String("id", "", "Username (email for password-based users)")
_ = authUsersPoliciesList.MarkFlagRequired("id")
addPaginationFlags(authUsersPoliciesList)
authUsersPolicies.AddCommand(authUsersPoliciesList)
}