-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathquery_noc_certificates.go
121 lines (99 loc) · 3.2 KB
/
query_noc_certificates.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cli
import (
"context"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/types/pki"
"github.com/zigbee-alliance/distributed-compliance-ledger/utils/cli"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
)
func CmdListNocCertificates() *cobra.Command {
cmd := &cobra.Command{
Use: "all-noc-x509-certs",
Short: "Gets all noc certificates (root and ica)",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryNocCertificatesRequest{
Pagination: pageReq,
}
res, err := queryClient.NocCertificatesAll(context.Background(), params)
if cli.IsKeyNotFoundRPCError(err) {
return clientCtx.PrintString(cli.LightClientProxyForListQueries)
}
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func CmdShowNocCertificates() *cobra.Command {
var (
vid int32
subject string
subjectKeyID string
)
cmd := &cobra.Command{
Use: "noc-x509-cert",
Short: "Gets NOC certificates (either root or ica) by one of property combinations: " +
"'subject + subject-key-id' or 'VID and subject-key-id' or just 'subject-key-id'",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
if vid != 0 && subject != "" && subjectKeyID != "" {
return clientCtx.PrintString("Incorrect parameters combination. " +
"You must provide '--subject, --subject-key-id' or '--vid , --subject-key-id' or " +
"just '--subject-key-id'")
}
if subject != "" && subjectKeyID != "" {
var res types.NocCertificates
return cli.QueryWithProof(
clientCtx,
pkitypes.StoreKey,
types.NocCertificatesKeyPrefix,
types.NocCertificatesKey(subject, subjectKeyID),
&res,
)
}
if vid != 0 && subjectKeyID != "" {
var res types.NocCertificatesByVidAndSkid
return cli.QueryWithProof(
clientCtx,
pkitypes.StoreKey,
types.NocCertificatesByVidAndSkidKeyPrefix,
types.NocCertificatesByVidAndSkidKey(vid, subjectKeyID),
&res,
)
}
var res types.NocCertificatesBySubjectKeyID
return cli.QueryWithProof(
clientCtx,
pkitypes.StoreKey,
types.NocCertificatesBySubjectKeyIDKeyPrefix,
types.NocCertificatesBySubjectKeyIDKey(subjectKeyID),
&res,
)
},
}
cmd.Flags().Int32Var(&vid, FlagVid, 0, "Vendor ID (positive non-zero) - optional")
cmd.Flags().StringVarP(&subject, FlagSubject, FlagSubjectShortcut, "", "Certificate's subject - optional")
cmd.Flags().StringVarP(&subjectKeyID, FlagSubjectKeyID, FlagSubjectKeyIDShortcut, "", "Certificate's subject key id (hex) - required")
flags.AddQueryFlagsToCmd(cmd)
_ = cmd.MarkFlagRequired(FlagSubjectKeyID)
return cmd
}