forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkicmn.go
171 lines (157 loc) · 4.55 KB
/
pkicmn.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2018 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 pkicmn contains some commonly used functionality and definitions.
package pkicmn
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
)
const (
CertNameFmt = "ISD%d-AS%s-V%d.crt"
CoreCertNameFmt = "ISD%d-AS%s-V%d-core.crt"
TrcNameFmt = "ISD%d-V%d.trc"
ErrInvalidSelector = "Invalid selector."
ErrNoISDDirFound = "No ISD directories found"
ErrNoASDirFound = "No AS directories found"
TRCsDir = "trcs"
CertsDir = "certs"
KeysDir = "keys"
)
var (
RootDir string
OutDir string
Force bool
Quiet bool
)
// ProcessSelector processes the given selector and returns a mapping from ISD id to ASes
// of that ISD. In case of an ISD-only selector, i.e., a '*' or any number the lists of
// ASes will be empty.
func ProcessSelector(selector string) (map[addr.ISD][]addr.IA, error) {
toks := strings.Split(selector, "-")
if len(toks) > 2 {
return nil, common.NewBasicError(ErrInvalidSelector, nil, "selector", selector)
}
isdTok := toks[0]
asTok := "*"
if len(toks) == 2 {
asTok = toks[1]
}
// Validate selectors.
if isdTok == "*" && asTok != "*" {
return nil, common.NewBasicError(ErrInvalidSelector, nil, "selector", selector)
}
if isdTok != "*" {
if _, err := addr.ISDFromString(isdTok); err != nil {
return nil, common.NewBasicError(ErrInvalidSelector, err, "selector", selector)
}
}
if asTok != "*" {
as, err := addr.ASFromString(asTok)
if err != nil {
return nil, common.NewBasicError(ErrInvalidSelector, err, "selector", selector)
}
asTok = as.FileFmt()
}
isdDirs, err := filepath.Glob(filepath.Join(RootDir, fmt.Sprintf("ISD%s", isdTok)))
if err != nil {
return nil, err
}
if len(isdDirs) == 0 {
return nil, common.NewBasicError(ErrNoISDDirFound, nil, "selector", selector)
}
res := make(map[addr.ISD][]addr.IA)
for _, dir := range isdDirs {
isd, err := isdFromDir(dir)
if err != nil {
return nil, err
}
dirs, err := filepath.Glob(filepath.Join(dir, fmt.Sprintf("AS%s", asTok)))
if err != nil {
return nil, err
}
if len(dirs) == 0 {
return nil, common.NewBasicError(ErrNoASDirFound, nil, "selector", selector)
}
ases := make([]addr.IA, len(dirs))
for i, asDir := range dirs {
as, err := asFromDir(asDir)
if err != nil {
return nil, err
}
ases[i] = addr.IA{I: addr.ISD(isd), A: addr.AS(as)}
}
res[isd] = ases
}
return res, nil
}
func isdFromDir(dir string) (addr.ISD, error) {
isd, err := addr.ISDFromFileFmt(filepath.Base(dir), true)
if err != nil {
return 0, common.NewBasicError("Unable to parse ISD number from dir", err, "dir", dir)
}
return isd, nil
}
func asFromDir(dir string) (addr.AS, error) {
as, err := addr.ASFromFileFmt(filepath.Base(dir), true)
if err != nil {
return 0, common.NewBasicError("Unable to parse AS number from dir", err, "dir", dir)
}
return as, nil
}
func Contains(ases []addr.IA, as addr.IA) bool {
for _, ia := range ases {
if ia.Eq(as) {
return true
}
}
return false
}
func WriteToFile(raw common.RawBytes, path string, perm os.FileMode) error {
// Check if file already exists.
if _, err := os.Stat(path); err == nil {
if !Force {
QuietPrint("%s already exists. Use -f to overwrite.\n", path)
return nil
}
// Nuke file to ensure correct permissions.
if err = os.Remove(path); err != nil {
return err
}
}
if err := ioutil.WriteFile(path, append(raw, "\n"...), perm); err != nil {
return err
}
QuietPrint("Successfully written %s\n", path)
return nil
}
func GetAsPath(baseDir string, ia addr.IA) string {
return filepath.Join(baseDir, fmt.Sprintf("ISD%d/AS%s", ia.I, ia.A.FileFmt()))
}
func GetIsdPath(baseDir string, isd addr.ISD) string {
return filepath.Join(baseDir, fmt.Sprintf("ISD%d", isd))
}
func ErrorAndExit(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(2)
}
func QuietPrint(format string, a ...interface{}) {
if !Quiet {
fmt.Printf(format, a...)
}
}