aboutsummaryrefslogtreecommitdiff
path: root/nvdmirror/sync.go
blob: 65a23cfbb4c08816ce70e0991a5164137b5704a3 (plain)
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
package nvdmirror

//
// Sync NVD data files to destination directory based on given sync
// configuration and cache.
//
// Returns an array of file names which have been updated in the
// destination directory.
//
// This function does the following:
//
// 1. Fetch the contents of the source meta URLs for CVE feeds and
//    CPE matches.  All source URLs are fetched concurrently.
//
// 2. Check the size and hash from meta files against the existing
//    CVE feed and CPE match files in the destination directory.  All
//    file sizes and hashes are checked concurrently.
//
// 3. Build a list of changed CVE feeds and CPE match files.
//
// 4. Append the CPE dictionary, CISA KEVC URL, and the CWE list URL.
//
// 5. Fetch the contents of changed URLs.  See note below about HTTP
//    request headers.
//
// 6. Return an array of updated files.
//
// Note: All HTTP requests in steps #1 and #5 above are sent with the
// following headers:
//
// * if-modified-since: The last-modified header from the previous
//   successful response, if it was set.
//
// * if-none-match: The etag header from the last successful response,
//   if it was set.
//
// * user-agent: User agent from config, or the default user agent if
//   the config value is unspecified.
//
func Sync(config SyncConfig, cache Cache, dstDir string) []Update {
  // build sync context
  ctx := newSyncContext(config, cache, dstDir)

  // fetch meta files, check for updates, append cpe dictionary URL,
  // fetch updated files, and then return a list of changed files
  return ctx.syncUrls(append(
    ctx.checkMetas(ctx.fetchMetas()),
    Update { Type: UpdateCpeDict, Url: config.GetCpeDictUrl() },
    Update { Type: UpdateCisaKevc, Url: config.GetCisaKevcUrl() },
    Update { Type: UpdateCweList, Url: config.GetCweListUrl() },
  ))
}