| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 // Package access implements a config service access check against a project |
| 6 // config client. |
| 7 // |
| 8 // Note that this is a soft check, as the true access authority is the config |
| 9 // service, and this check is not hitting that service. |
| 10 // |
| 11 // If access is granted, this function will return nil. If access is explicitly |
| 12 // denied, this will return ErrNoAccess. |
| 13 // |
| 14 // This is a port of the ACL implementation from the config service: |
| 15 // https://chromium.googlesource.com/external/github.com/luci/luci-py/+/e3fbb1f5
dafa59a2c57cf3a9fe3708f4309ab653/appengine/components/components/config/api.py |
| 16 package access |
| 17 |
| 18 import ( |
| 19 "strings" |
| 20 |
| 21 "github.com/luci/luci-go/common/errors" |
| 22 configPB "github.com/luci/luci-go/common/proto/config" |
| 23 "github.com/luci/luci-go/luci_config/common/cfgtypes" |
| 24 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 25 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" |
| 26 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" |
| 27 "github.com/luci/luci-go/server/auth" |
| 28 "github.com/luci/luci-go/server/auth/identity" |
| 29 |
| 30 "golang.org/x/net/context" |
| 31 ) |
| 32 |
| 33 // ErrNoAccess is an error returned by CheckAccess if the supplied Authority |
| 34 // does not have access to the supplied config set. |
| 35 var ErrNoAccess = errors.New("no access") |
| 36 |
| 37 // Check tests if a given Authority can access the named config set. |
| 38 func Check(c context.Context, a backend.Authority, configSet cfgtypes.ConfigSet)
error { |
| 39 if a == backend.AsService { |
| 40 return nil |
| 41 } |
| 42 |
| 43 _, projectConfigSet, _ := configSet.SplitProject() |
| 44 if projectConfigSet == "" { |
| 45 // Not a project config set, so neither remaining Authority can
access. |
| 46 return ErrNoAccess |
| 47 } |
| 48 |
| 49 // Load the project config. We execute this RPC as the service, not the
user, |
| 50 // so while this will recurse (and hopefully take advantage of the cache
), it |
| 51 // will not trigger an infinite access check loop. |
| 52 var pcfg configPB.ProjectCfg |
| 53 if err := cfgclient.Get(c, cfgclient.AsService, projectConfigSet, cfgcli
ent.ProjectConfigPath, |
| 54 textproto.Message(&pcfg), nil); err != nil { |
| 55 return errors.Annotate(err).Reason("failed to load %(path)q in %
(configSet)q"). |
| 56 D("path", cfgclient.ProjectConfigPath).D("configSet", pr
ojectConfigSet).Err() |
| 57 } |
| 58 |
| 59 id := identity.AnonymousIdentity |
| 60 if a == backend.AsUser { |
| 61 id = auth.CurrentIdentity(c) |
| 62 } |
| 63 checkGroups := make([]string, 0, len(pcfg.Access)) |
| 64 for _, access := range pcfg.Access { |
| 65 if group, ok := trimPrefix(access, "group:"); ok { |
| 66 // Check group membership. |
| 67 checkGroups = append(checkGroups, group) |
| 68 } else { |
| 69 // If there is no ":" in the access string, this is a us
er ACL. |
| 70 if strings.IndexRune(access, ':') < 0 { |
| 71 access = "user:" + access |
| 72 } |
| 73 if identity.Identity(access) == id { |
| 74 return nil |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 // No individual accesses, check groups. |
| 80 if len(checkGroups) > 0 { |
| 81 switch canAccess, err := auth.IsMember(c, checkGroups...); { |
| 82 case err != nil: |
| 83 return errors.Annotate(err).Reason("failed to check grou
p membership").Err() |
| 84 case canAccess: |
| 85 return nil |
| 86 } |
| 87 } |
| 88 return ErrNoAccess |
| 89 } |
| 90 |
| 91 func trimPrefix(v, pfx string) (string, bool) { |
| 92 if strings.HasPrefix(v, pfx) { |
| 93 return v[len(pfx):], true |
| 94 } |
| 95 return v, false |
| 96 } |
| OLD | NEW |