forked from google/exposure-notifications-server
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (169 loc) · 6.21 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2020 the Exposure Notifications Server authors
//
// 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.
// This tool displays the content of the export file.
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"time"
"github.com/google/exposure-notifications-server/internal/export"
exportpb "github.com/google/exposure-notifications-server/internal/pb/export"
"github.com/google/exposure-notifications-server/internal/publish/model"
"github.com/hashicorp/go-multierror"
)
var (
showSig = flag.Bool("sig", true, "show signature information from export bundle in json")
filePath = flag.String("file", "", "path to the export files, supports file globs")
printJSON = flag.Bool("json", true, "show the export in json")
quiet = flag.Bool("q", false, "run in quiet mode")
allowedTEKAge = flag.Duration("tek-age", 14*24*time.Hour, "max TEK age in checks")
symptomDayLimit = flag.Int("symptom-days", 14, "magnitude of expected symptom onset day range")
fileAge = flag.Duration("file-age", time.Duration(0), "file age is a positive duration that indicates how old a file is, this would be added to tek-age when validating the file and adjusts 'current time' for validing future keys.")
)
func main() {
if err := realMain(); err != nil {
printError("%s", err)
os.Exit(1)
}
}
func realMain() error {
flag.Parse()
if *filePath == "" {
return fmt.Errorf("--file is required")
}
if *allowedTEKAge < time.Duration(0) {
return fmt.Errorf("--tek-age must be a positive duration, got %q", *allowedTEKAge)
}
if *symptomDayLimit < 0 {
return fmt.Errorf("--symptom-days must be >=0, got %q", *symptomDayLimit)
}
if *fileAge < time.Duration(0) {
return fmt.Errorf("--file-age must be a positive duration, got %q", *fileAge)
}
matches, err := filepath.Glob(*filePath)
if err != nil {
return fmt.Errorf("failed to expand matches: %w", err)
}
if len(matches) == 0 {
return fmt.Errorf("%q produced no matches (shell escaping?)", *filePath)
}
var errors *multierror.Error
results := make([]*analysis, 0, len(matches))
for _, m := range matches {
result, err := analyzeOne(m, *showSig, *printJSON)
if err != nil {
errors = multierror.Append(errors, fmt.Errorf("%s: %w", m, err))
continue
}
results = append(results, result)
}
for _, result := range results {
printMsg("%s:\n", result.path)
if !*quiet && len(result.sig) > 0 {
printMsg("signature: %s", result.sig)
}
if !*quiet && len(result.export) > 0 {
printMsg("export: %s", result.export)
}
printMsg("\n")
}
return errors.ErrorOrNil()
}
type analysis struct {
path string
sig []byte
export []byte
}
func analyzeOne(pth string, includeSig, includeExport bool) (*analysis, error) {
blob, err := os.ReadFile(pth)
if err != nil {
return nil, fmt.Errorf("can't read export file: %w", err)
}
var result analysis
result.path = pth
if includeSig {
sigExport, err := export.UnmarshalSignatureFile(blob)
if err != nil {
return nil, fmt.Errorf("error unmarshaling export signature file: %w", err)
}
prettyJSON, err := json.MarshalIndent(sigExport, "", " ")
if err != nil {
return nil, fmt.Errorf("error pretty printing export signature: %w", err)
}
result.sig = prettyJSON
}
keyExport, _, err := export.UnmarshalExportFile(blob)
if err != nil {
return nil, fmt.Errorf("error unmarshaling export file: %w", err)
}
// Do some basic data validation.
if err := checkExportFile(keyExport); err != nil {
return nil, fmt.Errorf("export file contains errors: %w", err)
}
if includeExport {
prettyJSON, err := json.MarshalIndent(keyExport, "", " ")
if err != nil {
return nil, fmt.Errorf("error pretty printing export: %w", err)
}
result.export = prettyJSON
}
return &result, nil
}
func checkExportFile(export *exportpb.TemporaryExposureKeyExport) error {
now := time.Now().UTC().Add(-1 * *fileAge)
floor := model.IntervalNumber(now.Add(-1 * *allowedTEKAge))
ceiling := model.IntervalNumber(now)
var errors *multierror.Error
if err := checkKeys("keys", export.Keys, floor, ceiling); err != nil {
errors = multierror.Append(errors, err)
}
if err := checkKeys("revisedKeys", export.RevisedKeys, floor, ceiling); err != nil {
errors = multierror.Append(errors, err)
}
return errors.ErrorOrNil()
}
func checkKeys(typ string, keys []*exportpb.TemporaryExposureKey, floor, ceiling int32) error {
symptomDays := int32(*symptomDayLimit)
var errors *multierror.Error
for i, k := range keys {
if l := len(k.KeyData); l != 16 {
errors = multierror.Append(errors, fmt.Errorf("%s #%d: invald key length: want 16, got: %v", typ, i, l))
}
if s := k.GetRollingStartIntervalNumber(); s < floor {
errors = multierror.Append(errors, fmt.Errorf("%s #%d: rolling interval start number is > %v ago, want >= %d, got %d", typ, i, *allowedTEKAge, floor, s))
} else if s > ceiling {
errors = multierror.Append(errors, fmt.Errorf("%s #%d: rolling interval start number in the future, want < %d, got %d", typ, i, ceiling, s))
}
if r := k.GetRollingPeriod(); r < 1 || r > 144 {
errors = multierror.Append(errors, fmt.Errorf("%s #%d: rolling period invalid, want >= 1 && <= 144, got %d", typ, i, r))
}
if k.DaysSinceOnsetOfSymptoms != nil {
if d := k.GetDaysSinceOnsetOfSymptoms(); d < -symptomDays || d > symptomDays {
errors = multierror.Append(errors, fmt.Errorf("%s #%d: days_since_onset_of_symptoms is outside of expected range, -%d..%d, got: %d", typ, i, symptomDays, symptomDays, d))
}
}
}
return errors.ErrorOrNil()
}
func printMsg(msg string, args ...interface{}) {
msg = fmt.Sprintf("%s\n", msg)
fmt.Fprintf(os.Stdout, msg, args...)
}
func printError(msg string, args ...interface{}) {
msg = fmt.Sprintf("ERROR! %s\n", msg)
fmt.Fprintf(os.Stderr, msg, args...)
}