| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package authdb | 5 package authdb |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "net" | 9 "net" |
| 10 "strings" | 10 "strings" |
| 11 "time" | 11 "time" |
| 12 | 12 |
| 13 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 14 | 14 |
| 15 "github.com/luci/luci-go/common/clock" | 15 "github.com/luci/luci-go/common/clock" |
| 16 "github.com/luci/luci-go/common/data/caching/lazyslot" | 16 "github.com/luci/luci-go/common/data/caching/lazyslot" |
| 17 "github.com/luci/luci-go/common/logging" | 17 "github.com/luci/luci-go/common/logging" |
| 18 "github.com/luci/luci-go/server/auth/identity" | 18 "github.com/luci/luci-go/server/auth/identity" |
| 19 "github.com/luci/luci-go/server/auth/service/protocol" | 19 "github.com/luci/luci-go/server/auth/service/protocol" |
| 20 "github.com/luci/luci-go/server/auth/signing" | 20 "github.com/luci/luci-go/server/auth/signing" |
| 21 "github.com/luci/luci-go/server/secrets" | |
| 22 ) | 21 ) |
| 23 | 22 |
| 24 // OAuth client_id of https://apis-explorer.appspot.com/. | 23 // OAuth client_id of https://apis-explorer.appspot.com/. |
| 25 const googleAPIExplorerClientID = "292824132082.apps.googleusercontent.com" | 24 const googleAPIExplorerClientID = "292824132082.apps.googleusercontent.com" |
| 26 | 25 |
| 27 // SnapshotDB implements DB using AuthDB proto message. | 26 // SnapshotDB implements DB using AuthDB proto message. |
| 28 // | 27 // |
| 29 // Use NewSnapshotDB to create new instances. Don't touch public fields | 28 // Use NewSnapshotDB to create new instances. Don't touch public fields |
| 30 // of existing instances. | 29 // of existing instances. |
| 31 type SnapshotDB struct { | 30 type SnapshotDB struct { |
| 32 AuthServiceURL string // where it was fetched from | 31 AuthServiceURL string // where it was fetched from |
| 33 Rev int64 // its revision number | 32 Rev int64 // its revision number |
| 34 | 33 |
| 35 tokenServiceURL string // URL of the token server as provided by Auth se
rvice | 34 tokenServiceURL string // URL of the token server as provided by Auth se
rvice |
| 36 | 35 |
| 37 clientIDs map[string]struct{} // set of allowed client IDs | 36 clientIDs map[string]struct{} // set of allowed client IDs |
| 38 groups map[string]*group // map of all known groups | 37 groups map[string]*group // map of all known groups |
| 39 secrets secrets.StaticStore // secrets shared by all service with this
DB | |
| 40 | 38 |
| 41 assignments map[identity.Identity]string // IP whitelist assignements | 39 assignments map[identity.Identity]string // IP whitelist assignements |
| 42 whitelists map[string][]net.IPNet // IP whitelists | 40 whitelists map[string][]net.IPNet // IP whitelists |
| 43 | 41 |
| 44 // Certs are loaded lazily in GetCertificates since they are used only w
hen | 42 // Certs are loaded lazily in GetCertificates since they are used only w
hen |
| 45 // checking delegation tokens, which is relatively rare. | 43 // checking delegation tokens, which is relatively rare. |
| 46 certs lazyslot.Slot | 44 certs lazyslot.Slot |
| 47 } | 45 } |
| 48 | 46 |
| 49 var _ DB = &SnapshotDB{} | 47 var _ DB = &SnapshotDB{} |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 // Second pass: fill in `nested` with pointers, now that we have them. | 121 // Second pass: fill in `nested` with pointers, now that we have them. |
| 124 for _, g := range authDB.GetGroups() { | 122 for _, g := range authDB.GetGroups() { |
| 125 gr := db.groups[g.GetName()] | 123 gr := db.groups[g.GetName()] |
| 126 for _, nestedName := range g.GetNested() { | 124 for _, nestedName := range g.GetNested() { |
| 127 if nestedGroup := db.groups[nestedName]; nestedGroup !=
nil { | 125 if nestedGroup := db.groups[nestedName]; nestedGroup !=
nil { |
| 128 gr.nested = append(gr.nested, nestedGroup) | 126 gr.nested = append(gr.nested, nestedGroup) |
| 129 } | 127 } |
| 130 } | 128 } |
| 131 } | 129 } |
| 132 | 130 |
| 133 // Load all shared secrets. | |
| 134 db.secrets = make(secrets.StaticStore, len(authDB.GetSecrets())) | |
| 135 for _, s := range authDB.GetSecrets() { | |
| 136 values := s.GetValues() | |
| 137 if len(values) == 0 { | |
| 138 continue | |
| 139 } | |
| 140 secret := secrets.Secret{ | |
| 141 Current: secrets.NamedBlob{Blob: values[0]}, // most rec
ent on top | |
| 142 } | |
| 143 if len(values) > 1 { | |
| 144 secret.Previous = make([]secrets.NamedBlob, len(values)-
1) | |
| 145 for i := 1; i < len(values); i++ { | |
| 146 secret.Previous[i-1] = secrets.NamedBlob{Blob: v
alues[i]} | |
| 147 } | |
| 148 } | |
| 149 db.secrets[secrets.Key(s.GetName())] = secret | |
| 150 } | |
| 151 | |
| 152 // Build map of IP whitelist assignments. | 131 // Build map of IP whitelist assignments. |
| 153 db.assignments = make(map[identity.Identity]string, len(authDB.GetIpWhit
elistAssignments())) | 132 db.assignments = make(map[identity.Identity]string, len(authDB.GetIpWhit
elistAssignments())) |
| 154 for _, a := range authDB.GetIpWhitelistAssignments() { | 133 for _, a := range authDB.GetIpWhitelistAssignments() { |
| 155 db.assignments[identity.Identity(a.GetIdentity())] = a.GetIpWhit
elist() | 134 db.assignments[identity.Identity(a.GetIdentity())] = a.GetIpWhit
elist() |
| 156 } | 135 } |
| 157 | 136 |
| 158 // Parse all subnets into IPNet objects. | 137 // Parse all subnets into IPNet objects. |
| 159 db.whitelists = make(map[string][]net.IPNet, len(authDB.GetIpWhitelists(
))) | 138 db.whitelists = make(map[string][]net.IPNet, len(authDB.GetIpWhitelists(
))) |
| 160 for _, w := range authDB.GetIpWhitelists() { | 139 for _, w := range authDB.GetIpWhitelists() { |
| 161 if len(w.GetSubnets()) == 0 { | 140 if len(w.GetSubnets()) == 0 { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 259 |
| 281 return found | 260 return found |
| 282 } | 261 } |
| 283 | 262 |
| 284 if gr := db.groups[groupName]; gr != nil { | 263 if gr := db.groups[groupName]; gr != nil { |
| 285 return isMember(gr), nil | 264 return isMember(gr), nil |
| 286 } | 265 } |
| 287 return false, nil | 266 return false, nil |
| 288 } | 267 } |
| 289 | 268 |
| 290 // SharedSecrets is secrets.Store with secrets in Auth DB. | |
| 291 // | |
| 292 // Such secrets are usually generated on central Auth Service and are known | |
| 293 // to all trusted services (so that they can use them to exchange data). | |
| 294 func (db *SnapshotDB) SharedSecrets(c context.Context) (secrets.Store, error) { | |
| 295 return db.secrets, nil | |
| 296 } | |
| 297 | |
| 298 // GetCertificates returns a bundle with certificates of a trusted signer. | 269 // GetCertificates returns a bundle with certificates of a trusted signer. |
| 299 func (db *SnapshotDB) GetCertificates(c context.Context, signerID identity.Ident
ity) (*signing.PublicCertificates, error) { | 270 func (db *SnapshotDB) GetCertificates(c context.Context, signerID identity.Ident
ity) (*signing.PublicCertificates, error) { |
| 300 val, err := db.certs.Get(c) | 271 val, err := db.certs.Get(c) |
| 301 if err != nil { | 272 if err != nil { |
| 302 return nil, err | 273 return nil, err |
| 303 } | 274 } |
| 304 trustedCertsMap := val.Value.(certMap) | 275 trustedCertsMap := val.Value.(certMap) |
| 305 return trustedCertsMap[signerID], nil | 276 return trustedCertsMap[signerID], nil |
| 306 } | 277 } |
| 307 | 278 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 return nil, fmt.Errorf("the token server %s didn't provide its s
ervice account name", db.tokenServiceURL) | 335 return nil, fmt.Errorf("the token server %s didn't provide its s
ervice account name", db.tokenServiceURL) |
| 365 } | 336 } |
| 366 | 337 |
| 367 id, err := identity.MakeIdentity("user:" + certs.ServiceAccountName) | 338 id, err := identity.MakeIdentity("user:" + certs.ServiceAccountName) |
| 368 if err != nil { | 339 if err != nil { |
| 369 return nil, fmt.Errorf("invalid service_account_name %q in fetch
ed certificates bundle - %s", certs.ServiceAccountName, err) | 340 return nil, fmt.Errorf("invalid service_account_name %q in fetch
ed certificates bundle - %s", certs.ServiceAccountName, err) |
| 370 } | 341 } |
| 371 | 342 |
| 372 return certMap{id: certs}, nil | 343 return certMap{id: certs}, nil |
| 373 } | 344 } |
| OLD | NEW |