mirrored from https://chromium.googlesource.com/infra/luci/luci-go
-
Notifications
You must be signed in to change notification settings - Fork 43
/
auth.go
118 lines (105 loc) · 3.33 KB
/
auth.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
// Copyright 2015 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 coordinator
import (
"context"
"fmt"
"strings"
"go.chromium.org/gae/service/info"
"go.chromium.org/luci/auth/identity"
log "go.chromium.org/luci/common/logging"
"go.chromium.org/luci/logdog/api/config/svcconfig"
"go.chromium.org/luci/logdog/server/config"
"go.chromium.org/luci/server/auth"
)
// IsAdminUser tests whether the current user belongs to the administrative
// users group.
//
// If the user is not, a MembershipError will be returned.
func IsAdminUser(c context.Context) error {
cfg, err := config.Config(c)
if err != nil {
return err
}
return checkMember(c, cfg.Coordinator.AdminAuthGroup)
}
// IsServiceUser tests whether the current user belongs to the backend services
// users group.
//
// If the user is not, a MembershipError will be returned.
func IsServiceUser(c context.Context) error {
cfg, err := config.Config(c)
if err != nil {
return err
}
return checkMember(c, cfg.Coordinator.ServiceAuthGroup)
}
// IsProjectReader tests whether the current user belongs to one of the
// project's declared reader groups.
//
// If the user is not, a MembershipError will be returned.
func IsProjectReader(c context.Context, pcfg *svcconfig.ProjectConfig) error {
return checkMember(c, pcfg.ReaderAuthGroups...)
}
// IsProjectWriter tests whether the current user belongs to one of the
// project's declared writer groups.
//
// If the user is not a member of any of the groups, a MembershipError will be
// returned.
func IsProjectWriter(c context.Context, pcfg *svcconfig.ProjectConfig) error {
return checkMember(c, pcfg.WriterAuthGroups...)
}
func checkMember(c context.Context, groups ...string) error {
// On dev-appserver, the superuser has implicit group membership to
// everything.
if info.IsDevAppServer(c) {
if u := auth.CurrentUser(c); u.Superuser {
log.Fields{
"identity": u.Identity,
"groups": groups,
}.Infof(c, "Granting superuser implicit group membership on development server.")
return nil
}
}
id := auth.CurrentIdentity(c)
is, err := auth.IsMember(c, groups...)
if err != nil {
return err
}
if is {
log.Fields{
"identity": id,
"group": groups,
}.Debugf(c, "User access granted.")
return nil
}
return &MembershipError{
Identity: id,
Groups: groups,
}
}
// MembershipError is an error returned by group membership checking functions
// if the current identity is not a member of the requested group.
type MembershipError struct {
Identity identity.Identity
Groups []string
}
func (e *MembershipError) Error() string {
return fmt.Sprintf("user %q is not a member of [%s]", e.Identity, strings.Join(e.Groups, ", "))
}
// IsMembershipError returns whether a given error is a membership error.
func IsMembershipError(e error) bool {
_, ok := e.(*MembershipError)
return ok
}