/
list.go
87 lines (80 loc) · 2.6 KB
/
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
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
/*
Copyright 2020 The Magma Authors.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package handlers implements individual accessc commands as well as common
// across multiple commands functionality
package handlers
import (
"fmt"
"log"
"os"
"github.com/go-magma/magma/orc8r/cloud/go/services/accessd"
"github.com/go-magma/magma/orc8r/cloud/go/services/certifier"
"github.com/go-magma/magma/orc8r/cloud/go/tools/commands"
)
// List command - prints out all registered Operators and their attributes
func init() {
cmd := CommandRegistry.Add(
"list",
"List all Operators, their Certificate Serial Numbers and ACLs",
list)
cmd.Flags().Usage = func() {
fmt.Fprintf(os.Stderr, "\tUsage: %s %s\n", os.Args[0], cmd.Name())
}
}
func list(cmd *commands.Command, args []string) int {
operators, err := accessd.ListOperators()
if err != nil {
log.Fatalf("List Operators Error: %s", err)
}
acls, err := accessd.GetOperatorsACLs(operators)
if err != nil {
log.Fatalf("Get Operators Error: %s", err)
}
// Retrieve all certificate records
// It can be memory intensive, but Cert Info record is relatively small ~ 90 bytes on average
certMap, err := certifier.GetAll()
if err != nil {
log.Fatalf("Get Certificates Error: %s", err)
}
// operator hash to list of cert SNs map
// this map should be small, it will only include system's Operator entities
certSnsByOperMap := map[string][]string{}
// Create an empty list entry for each existing operator
for _, operator := range operators {
certSnsByOperMap[operator.HashString()] = []string{}
}
// Reverse mapping - add every found Cert SN to its operator's list
for sn, cinfo := range certMap {
op := cinfo.GetId()
hash := op.HashString()
if snlist, ok := certSnsByOperMap[hash]; ok {
certSnsByOperMap[hash] = append(snlist, sn)
}
}
// try to "free" certMap
certMap = nil
fmt.Println("Operators:")
for idx, acl := range acls {
if acl == nil {
log.Printf("ERROR: Nil ACL at %d index", idx)
continue
}
// Get Cert SNs for this operator
opname := acl.GetOperator().HashString()
certSNs, ok := certSnsByOperMap[opname]
if !ok {
certSNs = []string{}
log.Printf("Error Finding certificates for %s", opname)
}
PrintACL(acl, certSNs)
}
return 0
}