-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
aptsigning.go
232 lines (215 loc) · 6.81 KB
/
aptsigning.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// 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 2016-present Datadog, Inc.
package packagesigningimpl
import (
"bufio"
"encoding/xml"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/DataDog/datadog-agent/comp/core/log"
pkgUtils "github.com/DataDog/datadog-agent/comp/metadata/packagesigning/utils"
)
const (
trustedFolder = "/etc/apt/trusted.gpg.d/"
trustedFile = "/etc/apt/trusted.gpg"
mainSourceList = "/etc/apt/sources.list"
sourceList = "/etc/apt/sources.list.d/"
)
var (
sourceListRegexp = regexp.MustCompile(`^([^\s]+)\s?(\[.*\]\s)?(.*)$`)
signedBy = regexp.MustCompile(`signed-by=([A-Za-z0-9_\-\.\/]+)`)
trusted = regexp.MustCompile(`trusted=yes`)
debsigPolicies = "/etc/debsig/policies/"
debsigKeyring = "/usr/share/debsig/keyrings/"
)
// getAPTSignatureKeys returns the list of debian signature keys
func getAPTSignatureKeys(client *http.Client, logger log.Component) []signingKey {
cacheKeys := make(map[string]signingKey)
// debian 11 and ubuntu 22.04 will be the last using legacy trusted.gpg.d folder and trusted.gpg file
err := updateWithTrustedKeys(cacheKeys, client)
if err != nil {
logger.Debugf("Error while reading trusted keys: %s", err)
}
// Regular files are referenced in the sources.list file by signed-by=filename
err = updateWithSignedByKeys(cacheKeys, client)
if err != nil {
logger.Debugf("Error while reading signed-by keys: %s", err)
}
// In APT we can also sign packages with debsig
keyPaths, err := getDebsigKeyPaths()
if err != nil {
logger.Debugf("Error while reading debsig keys: %s", err)
}
for _, keyPath := range keyPaths {
err = readGPGFile(cacheKeys, repoFile{filename: keyPath, repositories: nil}, "debsig", client)
if err != nil {
logger.Debugf("Error while reading debsig key %s: %s", keyPath, err)
continue
}
}
// Extract signingKeys from the cache in a list
var keyList []signingKey
for _, key := range cacheKeys {
keyList = append(keyList, key)
}
return keyList
}
func updateWithTrustedKeys(cacheKeys map[string]signingKey, client *http.Client) error {
// debian 11 and ubuntu 22.04 will be the last using legacy trusted.gpg.d folder and trusted.gpg file
if _, err := os.Stat(trustedFolder); err != nil {
return err
}
if files, err := os.ReadDir(trustedFolder); err == nil {
for _, file := range files {
trustedFileName := filepath.Join(trustedFolder, file.Name())
err = readGPGFile(cacheKeys, repoFile{trustedFileName, nil}, "trusted", client)
if err != nil {
return err
}
}
}
if _, err := os.Stat(trustedFile); err != nil {
return err
}
return readGPGFile(cacheKeys, repoFile{trustedFile, nil}, "trusted", client)
}
func updateWithSignedByKeys(cacheKeys map[string]signingKey, client *http.Client) error {
gpgcheck, err := pkgUtils.IsPackageSigningEnabled()
if err != nil {
return err
}
if _, err := os.Stat(mainSourceList); err != nil {
return err
}
reposPerKey := parseSourceListFile(mainSourceList, gpgcheck)
for name, repos := range reposPerKey {
err := readGPGFile(cacheKeys, repoFile{name, repos}, "signed-by", client)
if err != nil {
return err
}
}
if _, err := os.Stat(sourceList); err != nil {
return err
}
if files, err := os.ReadDir(sourceList); err == nil {
for _, file := range files {
reposPerKey := parseSourceListFile(filepath.Join(sourceList, file.Name()), gpgcheck)
for name, repos := range reposPerKey {
err = readGPGFile(cacheKeys, repoFile{name, repos}, "signed-by", client)
if err != nil {
return err
}
}
}
}
return nil
}
func parseSourceListFile(filePath string, gpgcheck bool) map[string][]pkgUtils.Repository {
reposPerKey := make(map[string][]pkgUtils.Repository)
file, err := os.Open(filePath)
if err != nil {
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
splitLine := sourceListRegexp.FindStringSubmatch(line)
if len(splitLine) > 1 {
options := splitLine[2]
keyURI := signedBy.FindStringSubmatch(options)
repoGpgcheck := true
repoCheck := trusted.FindStringSubmatch(options)
if len(repoCheck) > 0 {
repoGpgcheck = false
}
keyPath := "nokey" // Track repositories without gpgkey
if len(keyURI) > 1 {
keyPath = keyURI[1]
}
if _, ok := reposPerKey[keyPath]; !ok {
reposPerKey[keyPath] = []pkgUtils.Repository{{Name: splitLine[3], Enabled: true, GPGCheck: gpgcheck, RepoGPGCheck: repoGpgcheck}}
} else {
reposPerKey[keyPath] = append(reposPerKey[keyPath], pkgUtils.Repository{Name: splitLine[3], Enabled: true, GPGCheck: gpgcheck, RepoGPGCheck: repoGpgcheck})
}
}
}
return reposPerKey
}
func getDebsigKeyPaths() ([]string, error) {
filePaths := make(map[string]struct{})
// Search in the policy files
if _, err := os.Stat(debsigPolicies); err != nil {
return nil, err
}
if debsigDirs, err := os.ReadDir(debsigPolicies); err == nil {
for _, debsigDir := range debsigDirs {
if debsigDir.IsDir() {
if policyFiles, err := os.ReadDir(filepath.Join(debsigPolicies, debsigDir.Name())); err == nil {
for _, policyFile := range policyFiles {
// Get the gpg file name from policy files
if debsigFile := getDebsigFileFromPolicy(filepath.Join(debsigPolicies, debsigDir.Name(), policyFile.Name())); debsigFile != "" {
debsigFilePath := filepath.Join(debsigKeyring, debsigDir.Name(), debsigFile)
if _, err := os.Stat(debsigFilePath); err == nil {
filePaths[debsigFilePath] = struct{}{}
} else {
return nil, err
}
}
}
} else {
return nil, err
}
}
}
} else {
return nil, err
}
// Denormalise the map
filePathsSlice := make([]string, 0)
for k := range filePaths {
filePathsSlice = append(filePathsSlice, k)
}
return filePathsSlice, nil
}
// policy structure to unmarshall the policy files
type policy struct {
XMLName xml.Name `xml:"Policy"`
Origin origin `xml:"Origin"`
Selection selection
Verification verification
}
type origin struct {
Name string `xml:"Name,attr"`
ID string `xml:"id,attr"`
Description string `xml:"Description,attr"`
}
type selection struct {
Required required `xml:"Required"`
}
type required struct {
Type string `xml:"Type,attr"`
File string `xml:"File,attr"`
ID string `xml:"id,attr"`
}
type verification struct {
MinOptional int `xml:"MinOptional,attr"`
Required required `xml:"Required"`
}
func getDebsigFileFromPolicy(policyFile string) string {
if xmlData, err := os.ReadFile(policyFile); err == nil {
var policy policy
if err = xml.Unmarshal(xmlData, &policy); err == nil {
return policy.Verification.Required.File
}
}
return ""
}