OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package config | 5 package config |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "strings" | 9 "strings" |
10 | 10 |
11 "github.com/golang/protobuf/proto" | 11 "github.com/golang/protobuf/proto" |
| 12 "github.com/luci/gae/service/info" |
12 "github.com/luci/luci-go/common/config" | 13 "github.com/luci/luci-go/common/config" |
13 "github.com/luci/luci-go/common/errors" | 14 "github.com/luci/luci-go/common/errors" |
14 log "github.com/luci/luci-go/common/logging" | 15 log "github.com/luci/luci-go/common/logging" |
15 "github.com/luci/luci-go/common/parallel" | 16 "github.com/luci/luci-go/common/parallel" |
16 configProto "github.com/luci/luci-go/common/proto/config" | 17 configProto "github.com/luci/luci-go/common/proto/config" |
| 18 "github.com/luci/luci-go/common/proto/logdog/svcconfig" |
17 "github.com/luci/luci-go/server/auth" | 19 "github.com/luci/luci-go/server/auth" |
18 "github.com/luci/luci-go/server/auth/identity" | 20 "github.com/luci/luci-go/server/auth/identity" |
19 "golang.org/x/net/context" | 21 "golang.org/x/net/context" |
20 ) | 22 ) |
21 | 23 |
22 const maxProjectWorkers = 32 | 24 const maxProjectWorkers = 32 |
23 | 25 |
24 // ErrNoAccess is returned if the user has no access to the requested project. | 26 // ErrNoAccess is returned if the user has no access to the requested project. |
25 var ErrNoAccess = errors.New("no access") | 27 var ErrNoAccess = errors.New("no access") |
26 | 28 |
| 29 // ProjectConfigPath returns the config set and path for project-specific |
| 30 // configuration. |
| 31 // |
| 32 // A given project's configuration is named after the current App ID. |
| 33 func ProjectConfigPath(c context.Context, project config.ProjectName) (string, s
tring) { |
| 34 return fmt.Sprintf("projects/%s", project), fmt.Sprintf("%s.cfg", info.G
et(c).AppID()) |
| 35 } |
| 36 |
| 37 // ProjectConfig loads the project config protobuf from the config service. |
| 38 // |
| 39 // If the configuration was not present, config.ErrNoConfig will be returned. |
| 40 func ProjectConfig(c context.Context, project config.ProjectName) (*svcconfig.Pr
ojectConfig, error) { |
| 41 // TODO(dnj): When empty project is disabled, make this return |
| 42 // config.ErrNoConfig. |
| 43 if project == "" { |
| 44 return &svcconfig.ProjectConfig{ |
| 45 ReaderAuthGroups: []string{"all"}, |
| 46 }, nil |
| 47 } |
| 48 |
| 49 configSet, configPath := ProjectConfigPath(c, project) |
| 50 cfg, err := config.Get(c).GetConfig(configSet, configPath, false) |
| 51 if err != nil { |
| 52 return nil, err |
| 53 } |
| 54 |
| 55 var pcfg svcconfig.ProjectConfig |
| 56 if err := proto.UnmarshalText(cfg.Content, &pcfg); err != nil { |
| 57 log.Fields{ |
| 58 log.ErrorKey: err, |
| 59 "configSet": cfg.ConfigSet, |
| 60 "path": cfg.Path, |
| 61 "contentHash": cfg.ContentHash, |
| 62 }.Errorf(c, "Failed to unmarshal project configuration.") |
| 63 return nil, ErrInvalidConfig |
| 64 } |
| 65 |
| 66 return &pcfg, nil |
| 67 } |
| 68 |
27 // Projects lists the registered LogDog projects. | 69 // Projects lists the registered LogDog projects. |
28 func Projects(c context.Context) ([]string, error) { | 70 func Projects(c context.Context) ([]string, error) { |
29 projects, err := config.Get(c).GetProjects() | 71 projects, err := config.Get(c).GetProjects() |
30 if err != nil { | 72 if err != nil { |
31 log.WithError(err).Errorf(c, "Failed to list 'luci-config' proje
cts.") | 73 log.WithError(err).Errorf(c, "Failed to list 'luci-config' proje
cts.") |
32 return nil, err | 74 return nil, err |
33 } | 75 } |
34 | 76 |
35 // TODO(dnj): Filter this list to projects with active LogDog configs, o
nce we | 77 // TODO(dnj): Filter this list to projects with active LogDog configs, o
nce we |
36 // move to project-specific configurations. | 78 // move to project-specific configurations. |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 for i, proj := range allProjects { | 195 for i, proj := range allProjects { |
154 if access[i] { | 196 if access[i] { |
155 result = append(result, config.ProjectName(proj.ID)) | 197 result = append(result, config.ProjectName(proj.ID)) |
156 } | 198 } |
157 } | 199 } |
158 return result, nil | 200 return result, nil |
159 } | 201 } |
160 | 202 |
161 func checkProjectAccess(c context.Context, ci config.Interface, proj config.Proj
ectName, st auth.State) (bool, error) { | 203 func checkProjectAccess(c context.Context, ci config.Interface, proj config.Proj
ectName, st auth.State) (bool, error) { |
162 // Load the configuration for this project. | 204 // Load the configuration for this project. |
163 » cfg, err := ci.GetConfig(fmt.Sprintf("projects/%s", proj), "project.cfg"
, false) | 205 » configSet, configPath := ProjectConfigPath(c, proj) |
| 206 » cfg, err := ci.GetConfig(configSet, configPath, false) |
164 if err != nil { | 207 if err != nil { |
165 if err == config.ErrNoConfig { | 208 if err == config.ErrNoConfig { |
166 // If the configuration is missing, report no access. | 209 // If the configuration is missing, report no access. |
167 return false, nil | 210 return false, nil |
168 } | 211 } |
169 return false, err | 212 return false, err |
170 } | 213 } |
171 | 214 |
172 var pcfg configProto.ProjectCfg | 215 var pcfg configProto.ProjectCfg |
173 if err := proto.UnmarshalText(cfg.Content, &pcfg); err != nil { | 216 if err := proto.UnmarshalText(cfg.Content, &pcfg); err != nil { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 264 |
222 return false, nil | 265 return false, nil |
223 } | 266 } |
224 | 267 |
225 func trimPrefix(s, p string) (string, bool) { | 268 func trimPrefix(s, p string) (string, bool) { |
226 if strings.HasPrefix(s, p) { | 269 if strings.HasPrefix(s, p) { |
227 return s[len(p):], true | 270 return s[len(p):], true |
228 } | 271 } |
229 return s, false | 272 return s, false |
230 } | 273 } |
OLD | NEW |