| 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 testconfig |
| 6 |
| 7 import ( |
| 8 "net/url" |
| 9 |
| 10 ccfg "github.com/luci/luci-go/common/config" |
| 11 "github.com/luci/luci-go/luci_config/common/cfgtypes" |
| 12 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 13 "github.com/luci/luci-go/luci_config/server/cfgclient/access" |
| 14 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" |
| 15 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/client" |
| 16 |
| 17 "golang.org/x/net/context" |
| 18 ) |
| 19 |
| 20 // WithCommonClient installs a backend.B into the supplied Context that |
| 21 // loads its configuration from base. |
| 22 func WithCommonClient(c context.Context, base ccfg.Interface) context.Context { |
| 23 return backend.WithBackend(c, &client.Backend{ |
| 24 Provider: &Provider{ |
| 25 Base: base, |
| 26 }, |
| 27 }) |
| 28 } |
| 29 |
| 30 // Provider is a client.Provider that is backed by a config interface. |
| 31 // This differs from client.RemoteProvider in that it takes an arbitrary |
| 32 // config interface instance rather than generating a remote one. |
| 33 // |
| 34 // Its intended use is for testing setups. |
| 35 // |
| 36 // Provider performs access checks locally using access.CheckAccess. |
| 37 type Provider struct { |
| 38 // Base is the base interface. |
| 39 Base ccfg.Interface |
| 40 } |
| 41 |
| 42 var _ client.Provider = (*Provider)(nil) |
| 43 |
| 44 // GetConfigClient implements ClientProvider. |
| 45 func (p *Provider) GetConfigClient(c context.Context, a backend.Authority) ccfg.
Interface { |
| 46 return &boundLocalInterface{ |
| 47 Context: c, |
| 48 Interface: p.Base, |
| 49 lsp: p, |
| 50 a: a, |
| 51 } |
| 52 } |
| 53 |
| 54 // boundLocalInterface is a ccfg.Interface implementation that adds ACL |
| 55 // pruning to the results based on the bound authority. |
| 56 type boundLocalInterface struct { |
| 57 context.Context |
| 58 |
| 59 // Interface is the config interface implementation. |
| 60 ccfg.Interface |
| 61 |
| 62 lsp *Provider |
| 63 a backend.Authority |
| 64 } |
| 65 |
| 66 func (bli *boundLocalInterface) GetConfig(ctx context.Context, configSet, path s
tring, hashOnly bool) (*ccfg.Config, error) { |
| 67 if err := access.Check(bli, bli.a, cfgtypes.ConfigSet(configSet)); err !
= nil { |
| 68 return nil, ccfg.ErrNoConfig |
| 69 } |
| 70 return bli.Interface.GetConfig(ctx, configSet, path, hashOnly) |
| 71 } |
| 72 |
| 73 func (bli *boundLocalInterface) GetProjectConfigs(ctx context.Context, path stri
ng, hashesOnly bool) ([]ccfg.Config, error) { |
| 74 cfgs, err := bli.Interface.GetProjectConfigs(ctx, path, hashesOnly) |
| 75 if err != nil { |
| 76 return nil, err |
| 77 } |
| 78 return bli.pruneConfigList(cfgs), nil |
| 79 } |
| 80 |
| 81 func (bli *boundLocalInterface) GetRefConfigs(ctx context.Context, path string,
hashesOnly bool) ([]ccfg.Config, error) { |
| 82 cfgs, err := bli.Interface.GetRefConfigs(ctx, path, hashesOnly) |
| 83 if err != nil { |
| 84 return nil, err |
| 85 } |
| 86 return bli.pruneConfigList(cfgs), nil |
| 87 } |
| 88 |
| 89 func (bli *boundLocalInterface) GetConfigSetLocation(ctx context.Context, config
Set string) (*url.URL, error) { |
| 90 if err := access.Check(ctx, bli.a, cfgtypes.ConfigSet(configSet)); err !
= nil { |
| 91 return nil, cfgclient.ErrNoConfig |
| 92 } |
| 93 return bli.Interface.GetConfigSetLocation(ctx, configSet) |
| 94 } |
| 95 |
| 96 func (bli *boundLocalInterface) pruneConfigList(cl []ccfg.Config) []ccfg.Config
{ |
| 97 ptr := 0 |
| 98 for i := range cl { |
| 99 if err := access.Check(bli, bli.a, cfgtypes.ConfigSet(cl[i].Conf
igSet)); err == nil { |
| 100 cl[ptr] = cl[i] |
| 101 ptr++ |
| 102 } |
| 103 } |
| 104 return cl[:ptr] |
| 105 } |
| OLD | NEW |