-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
145 lines (127 loc) · 3.88 KB
/
metadata.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
// Copyright 2018 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ui
import (
"context"
"sort"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"go.chromium.org/luci/auth/identity"
"go.chromium.org/luci/server/auth"
api "go.chromium.org/luci/cipd/api/cipd/v1"
"go.chromium.org/luci/cipd/appengine/impl"
)
// metadataBlock is passed to the templates as Metadata arg.
type metadataBlock struct {
// CanView is true if the caller is able to see all prefix metadata.
CanView bool
// ACLs is per-principal acls sorted by prefix length and role.
//
// Populated only if CanView is true.
ACLs []metadataACL
// CallerRoles is roles of the current caller.
//
// Populated only if CanView is false.
CallerRoles []string
}
type metadataACL struct {
RolePb api.Role // original role enum, for sorting
Role string // e.g "Reader"
Who string // either an email or a group name
WhoHref string // for groups, link to a group definition
Prefix string // via what prefix this role is granted
PrefixHref string // link to the corresponding prefix page
}
// fetchMetadata fetches and formats for UI metadata of the given prefix.
//
// It recognizes PermissionDenied errors and falls back to only displaying what
// roles the caller has instead of the full metadata.
func fetchMetadata(c context.Context, pfx string) (*metadataBlock, error) {
meta, err := impl.PublicRepo.GetInheritedPrefixMetadata(c, &api.PrefixRequest{
Prefix: pfx,
})
switch grpc.Code(err) {
case codes.OK:
break // handled below
case codes.PermissionDenied:
return fetchCallerRoles(c, pfx)
default:
return nil, err
}
// Grab URL of an auth server with the groups, if available.
groupsURL := ""
if url, err := auth.GetState(c).DB().GetAuthServiceURL(c); err == nil {
groupsURL = url + "/auth/groups/"
}
out := &metadataBlock{CanView: true}
for _, m := range meta.PerPrefixMetadata {
for _, a := range m.Acls {
role := strings.Title(strings.ToLower(a.Role.String()))
prefix := m.Prefix
if prefix == "" {
prefix = "[root]"
}
for _, p := range a.Principals {
whoHref := ""
switch {
case strings.HasPrefix(p, "group:"):
p = strings.TrimPrefix(p, "group:")
if groupsURL != "" {
whoHref = groupsURL + p
}
case p == string(identity.AnonymousIdentity):
p = "anonymous"
default:
p = strings.TrimPrefix(p, "user:")
}
out.ACLs = append(out.ACLs, metadataACL{
RolePb: a.Role,
Role: role,
Who: p,
WhoHref: whoHref,
Prefix: prefix,
PrefixHref: prefixPageURL(m.Prefix),
})
}
}
}
sort.Slice(out.ACLs, func(i, j int) bool {
l, r := out.ACLs[i], out.ACLs[j]
if l.RolePb != r.RolePb {
return l.RolePb > r.RolePb // "stronger" role (e.g. OWNERS) first
}
if l.Prefix != r.Prefix {
return l.Prefix > r.Prefix // longer prefix first
}
return l.Who < r.Who // alphabetically
})
return out, nil
}
func fetchCallerRoles(c context.Context, pfx string) (*metadataBlock, error) {
roles, err := impl.PublicRepo.GetRolesInPrefix(c, &api.PrefixRequest{
Prefix: pfx,
})
if err != nil {
return nil, err
}
out := &metadataBlock{
CanView: false,
CallerRoles: make([]string, len(roles.Roles)),
}
for i, r := range roles.Roles {
out.CallerRoles[i] = strings.Title(strings.ToLower(r.Role.String()))
}
return out, nil
}