mirror of
https://github.com/oneclickvirt/backtrace.git
synced 2026-11-24 23:39:41 +08:00
feat: validate synchronized ASN prefix snapshots
This commit is contained in:
1 parent
a32a1067d3
commit
577871a7df
20 files changed
+8987
-2983
No files matched your search
@@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
@@ -87,17 +89,109 @@ func updatePrefixes(ctx context.Context, client *http.Client, config updateConfi
|
||||
valuesByASN[asn] = values
|
||||
}
|
||||
for _, asn := range knownASNs {
|
||||
if err := writePrefixFile(filepath.Join(config.OutputDir, strings.ToLower(asn)+".txt"), valuesByASN[asn]); err != nil {
|
||||
path := filepath.Join(config.OutputDir, strings.ToLower(asn)+".txt")
|
||||
if err := writePrefixFile(path, valuesByASN[asn]); err != nil {
|
||||
if errors.Is(err, errPrefixCountDrop) {
|
||||
fmt.Fprintf(os.Stderr, "skip %s: %v\n", asn, err)
|
||||
continue
|
||||
current, readErr := os.ReadFile(path)
|
||||
if readErr != nil {
|
||||
return readErr
|
||||
}
|
||||
valuesByASN[asn] = prefixValuesFromFile(current)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := writePrefixManifest(path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type prefixManifest struct {
|
||||
Schema string `json:"schema"`
|
||||
File string `json:"file"`
|
||||
Count int `json:"count"`
|
||||
SHA256 string `json:"sha256"`
|
||||
GeneratedAt string `json:"generated_at"`
|
||||
}
|
||||
|
||||
func prefixValuesFromFile(data []byte) []string {
|
||||
values := make([]string, 0)
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
if value := strings.TrimSpace(line); value != "" {
|
||||
values = append(values, value)
|
||||
}
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func writePrefixManifest(snapshotPath string) error {
|
||||
snapshot, err := os.ReadFile(snapshotPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
values := prefixValuesFromFile(snapshot)
|
||||
if len(values) == 0 {
|
||||
return errors.New("prefix snapshot is empty")
|
||||
}
|
||||
hash := sha256.Sum256(snapshot)
|
||||
fileName := filepath.Base(snapshotPath)
|
||||
sha256Value := hex.EncodeToString(hash[:])
|
||||
manifestPath := snapshotPath + ".manifest.json"
|
||||
if current, readErr := os.ReadFile(manifestPath); readErr == nil {
|
||||
var existing prefixManifest
|
||||
decoder := json.NewDecoder(bytes.NewReader(current))
|
||||
decoder.DisallowUnknownFields()
|
||||
var extra any
|
||||
if decoder.Decode(&existing) == nil && decoder.Decode(&extra) == io.EOF &&
|
||||
existing.Schema == "backtrace.asn-prefixes/v1" && existing.File == fileName &&
|
||||
existing.Count == len(values) && strings.EqualFold(existing.SHA256, sha256Value) {
|
||||
if _, timeErr := time.Parse(time.RFC3339, existing.GeneratedAt); timeErr == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
} else if !errors.Is(readErr, os.ErrNotExist) {
|
||||
return readErr
|
||||
}
|
||||
manifest := prefixManifest{Schema: "backtrace.asn-prefixes/v1", File: fileName, Count: len(values), SHA256: sha256Value, GeneratedAt: time.Now().UTC().Format(time.RFC3339)}
|
||||
data, err := json.MarshalIndent(manifest, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data = append(data, '\n')
|
||||
return writeAtomicFile(manifestPath, data)
|
||||
}
|
||||
|
||||
func writeAtomicFile(output string, data []byte) error {
|
||||
if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
temporary, err := os.CreateTemp(filepath.Dir(output), ".prefix-manifest-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
temporaryName := temporary.Name()
|
||||
defer os.Remove(temporaryName)
|
||||
if err := temporary.Chmod(0o644); err != nil {
|
||||
_ = temporary.Close()
|
||||
return err
|
||||
}
|
||||
if _, err := temporary.Write(data); err != nil {
|
||||
_ = temporary.Close()
|
||||
return err
|
||||
}
|
||||
if err := temporary.Sync(); err != nil {
|
||||
_ = temporary.Close()
|
||||
return err
|
||||
}
|
||||
if err := temporary.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(temporaryName, output)
|
||||
}
|
||||
|
||||
func extractIPv6Prefixes(data []byte) ([]string, error) {
|
||||
var response struct {
|
||||
Status string `json:"status"`
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -30,3 +34,37 @@ func TestWritePrefixFileProtectsLargeCountDrop(t *testing.T) {
|
||||
t.Fatalf("count drop error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWritePrefixManifestPreservesGeneratedAtWhenSnapshotIsUnchanged(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
snapshotPath := filepath.Join(directory, "as64500.txt")
|
||||
if err := os.WriteFile(snapshotPath, []byte("2001:db8::/48\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifestPath := snapshotPath + ".manifest.json"
|
||||
old := prefixManifest{Schema: "backtrace.asn-prefixes/v1", File: "as64500.txt", Count: 1, SHA256: "", GeneratedAt: "2020-01-02T03:04:05Z"}
|
||||
data, err := os.ReadFile(snapshotPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Seed a valid manifest with an old timestamp. The updater must retain it.
|
||||
hash := sha256.Sum256(data)
|
||||
old.SHA256 = hex.EncodeToString(hash[:])
|
||||
encoded, err := json.Marshal(old)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(manifestPath, append(encoded, '\n'), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writePrefixManifest(snapshotPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual, err := os.ReadFile(manifestPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Contains(actual, []byte(`"generated_at":"2020-01-02T03:04:05Z"`)) {
|
||||
t.Fatalf("generated_at was not preserved: %s", actual)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user