File: roles.go

package info (click to toggle)
golang-github-theupdateframework-go-tuf 0.5.2-5~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 7,596 kB
  • sloc: python: 163; sh: 37; makefile: 12
file content (48 lines) | stat: -rw-r--r-- 929 bytes parent folder | download | duplicates (2)
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
package roles

import (
	"strconv"
	"strings"
)

var TopLevelRoles = map[string]struct{}{
	"root":      {},
	"targets":   {},
	"snapshot":  {},
	"timestamp": {},
}

func IsTopLevelRole(name string) bool {
	_, ok := TopLevelRoles[name]
	return ok
}

func IsDelegatedTargetsRole(name string) bool {
	return !IsTopLevelRole(name)
}

func IsTopLevelManifest(name string) bool {
	if IsVersionedManifest(name) {
		var found bool
		_, name, found = strings.Cut(name, ".")
		if !found {
			panic("expected a versioned manifest of the form x.role.json")
		}
	}
	return IsTopLevelRole(strings.TrimSuffix(name, ".json"))
}

func IsDelegatedTargetsManifest(name string) bool {
	return !IsTopLevelManifest(name)
}

func IsVersionedManifest(name string) bool {
	parts := strings.Split(name, ".")
	// Versioned manifests have the form "x.role.json"
	if len(parts) < 3 {
		return false
	}

	_, err := strconv.Atoi(parts[0])
	return err == nil
}