-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
configs.go
123 lines (105 loc) · 2.99 KB
/
configs.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022-present Datadog, Inc.
package state
import (
"encoding/json"
"errors"
"fmt"
"github.com/DataDog/go-tuf/data"
)
// ErrNoConfigVersion occurs when a target file's custom meta is missing the config version
var ErrNoConfigVersion = errors.New("version missing in custom file meta")
func parseConfig(product string, raw []byte, metadata Metadata) (interface{}, error) {
if _, validProduct := validProducts[product]; !validProduct {
return nil, fmt.Errorf("unknown product: %s", product)
}
switch product {
// ASM products are parsed directly in this client
case ProductASMFeatures:
return parseASMFeaturesConfig(raw, metadata)
case ProductASMDD:
return parseConfigASMDD(raw, metadata)
case ProductASMData:
return parseConfigASMData(raw, metadata)
// case ProductAgentTask:
// return ParseConfigAgentTask(raw, metadata)
// Other products are parsed separately
default:
return RawConfig{
Config: raw,
Metadata: metadata,
}, nil
}
}
// RawConfig holds a config that will be parsed separately
type RawConfig struct {
Config []byte
Metadata Metadata
}
// GetConfigs returns the current configs of a given product
func (r *Repository) GetConfigs(product string) map[string]RawConfig {
typedConfigs := make(map[string]RawConfig)
configs := r.getConfigs(product)
for path, conf := range configs {
// We control this, so if this has gone wrong something has gone horribly wrong
typed, ok := conf.(RawConfig)
if !ok {
panic("unexpected config stored as RawConfig")
}
typedConfigs[path] = typed
}
return typedConfigs
}
// Metadata stores remote config metadata for a given configuration
type Metadata struct {
Product string
ID string
Name string
Version uint64
RawLength uint64
Hashes map[string][]byte
ApplyStatus ApplyStatus
}
func newConfigMetadata(parsedPath configPath, tfm data.TargetFileMeta) (Metadata, error) {
var m Metadata
m.ID = parsedPath.ConfigID
m.Product = parsedPath.Product
m.Name = parsedPath.Name
m.RawLength = uint64(tfm.Length)
m.Hashes = make(map[string][]byte)
for k, v := range tfm.Hashes {
m.Hashes[k] = []byte(v)
}
v, err := fileMetaVersion(tfm)
if err != nil {
return Metadata{}, err
}
m.Version = v
return m, nil
}
type fileMetaCustom struct {
Version *uint64 `json:"v"`
}
func fileMetaVersion(fm data.TargetFileMeta) (uint64, error) {
if fm.Custom == nil {
return 0, ErrNoConfigVersion
}
fmc, err := parseFileMetaCustom(*fm.Custom)
if err != nil {
return 0, err
}
return *fmc.Version, nil
}
func parseFileMetaCustom(rawCustom []byte) (fileMetaCustom, error) {
var custom fileMetaCustom
err := json.Unmarshal(rawCustom, &custom)
if err != nil {
return fileMetaCustom{}, err
}
if custom.Version == nil {
return fileMetaCustom{}, ErrNoConfigVersion
}
return custom, nil
}