-
Notifications
You must be signed in to change notification settings - Fork 1
/
listing.go
158 lines (140 loc) · 3.93 KB
/
listing.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
package listing
import (
"embed"
"flag"
"html/template"
"time"
"github.com/distribution/distribution/v3/registry/auth/token"
"github.com/gorilla/mux"
"github.com/greboid/registryauth/auth"
log "github.com/sirupsen/logrus"
)
var (
ShowIndex = flag.Bool("show-index", false, "Show an index page, rather than just a 200 response")
ShowListings = flag.Bool("show-listings", true, "Index page lists public repositories")
PullHostname = flag.String("pull-hostname", "", "Hostname to show on listings and info page, will default to the request hostname")
RegistryHost = flag.String("registry-host", "http://localhost:8080", "The URL of the registry being listed")
RefreshInterval = flag.Duration("refresh-interval", 60*time.Second, "The time between registry refreshes")
)
//go:embed templates
var templates embed.FS
type Lister struct {
templates *template.Template
TokenProvider TokenProvider
PublicPrefixes []string
repositories *RepositoryList
lastPoll time.Time
}
type TokenProvider func(...string) (string, error)
type RepositoryList struct {
Repositories []*Repository
}
type Repository struct {
Name string
Tags []Tag
}
type Tag struct {
Name string
SHA string
Size int
}
type ListingIndex struct {
Title string
Repositories *RepositoryList
LastPolled time.Time
}
type Index struct {
Title string
}
func NewLister(publicPrefixes []string, getFullToken func(repository ...string) (string, error)) *Lister {
lister := &Lister{
TokenProvider: getFullToken,
PublicPrefixes: publicPrefixes,
}
return lister
}
func (s *Lister) Initialise(router *mux.Router) {
s.addRoutes(router)
}
func (s *Lister) start() {
go func() {
log.Infof("Refreshing repositories")
s.repositories = s.getRepositories()
s.lastPoll = time.Now()
log.Infof("Repository list refreshed")
for range time.Tick(*RefreshInterval) {
log.Infof("Refreshing repositories")
s.repositories = s.getRepositories()
s.lastPoll = time.Now()
log.Infof("Repository list refreshed")
}
}()
}
func (s *Lister) getRepositories() *RepositoryList {
publicRepositories, err := s.getPublicRepositories()
if err != nil {
log.Printf("Error: %s", err)
return nil
}
repositoryList := &RepositoryList{}
for index := range publicRepositories {
repoInfo, err := s.getRepoInfo(publicRepositories[index])
if err == nil {
repositoryList.Repositories = append(repositoryList.Repositories, repoInfo)
} else {
log.Infof("Unable to update repository list: %s", err.Error())
}
}
return repositoryList
}
func (s *Lister) getRepoInfo(repository string) (*Repository, error) {
distRepo, err := getTagList(repository, s.TokenProvider)
if err != nil {
return nil, err
}
taggedRepository, err := s.getTaggedRepository(distRepo)
if err != nil {
return nil, err
}
return taggedRepository, nil
}
func (s *Lister) getTaggedRepository(repository *DistributionRepository) (*Repository, error) {
repo := &Repository{
Name: repository.Name,
}
for index := range repository.Tags {
manifest, err := getRepositoryManifest(repository.Name, repository.Tags[index], s.TokenProvider)
if err != nil {
log.Printf("Unable to get manifest for tag: %s", err.Error())
repo.Tags = append(repo.Tags, Tag{
Name: repository.Tags[index],
SHA: "error",
Size: manifest.Size,
})
continue
}
repo.Tags = append(repo.Tags, Tag{
Name: repository.Tags[index],
SHA: manifest.SHA,
Size: manifest.Size,
})
}
return repo, nil
}
func (s *Lister) getPublicRepositories() ([]string, error) {
catalog, err := getCatalog(s.TokenProvider)
if err != nil {
return nil, err
}
var publicRepositories []string
for index := range catalog.Repositories {
if auth.IsScopePublic(s.PublicPrefixes, &token.ResourceActions{
Type: "repository",
Name: catalog.Repositories[index],
Actions: []string{"pull"},
}) {
publicRepositories = append(publicRepositories, catalog.Repositories[index])
}
}
return publicRepositories, nil
}