go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/authdb/db.go (about) 1 // Copyright 2016 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package authdb 16 17 import ( 18 "context" 19 "net" 20 21 "go.chromium.org/luci/auth/identity" 22 23 "go.chromium.org/luci/server/auth/realms" 24 "go.chromium.org/luci/server/auth/service/protocol" 25 "go.chromium.org/luci/server/auth/signing" 26 ) 27 28 // AuthServiceAccessGroup members are allowed to see all groups. 29 const AuthServiceAccessGroup = "auth-service-access" 30 31 // DB is interface to access a database of authorization related information. 32 // 33 // It is static read only object that represent snapshot of auth data at some 34 // moment in time. 35 type DB interface { 36 // IsAllowedOAuthClientID returns true if given OAuth2 client_id can be used 37 // to authenticate access for given email. 38 IsAllowedOAuthClientID(ctx context.Context, email, clientID string) (bool, error) 39 40 // IsInternalService returns true if the given hostname belongs to a service 41 // that is a part of the current LUCI deployment. 42 // 43 // What hosts are internal is controlled by 'internal_service_regexp' setting 44 // in security.cfg in the Auth Service configs. 45 IsInternalService(ctx context.Context, hostname string) (bool, error) 46 47 // IsMember returns true if the given identity belongs to any of the groups. 48 // 49 // Unknown groups are considered empty (but logged as warnings). 50 // May return errors if underlying datastore has issues. 51 IsMember(ctx context.Context, id identity.Identity, groups []string) (bool, error) 52 53 // CheckMembership returns groups from the given list the identity belongs to. 54 // 55 // Unlike IsMember, it doesn't stop on the first hit but continues evaluating 56 // all groups. 57 // 58 // Unknown groups are considered empty. The order of groups in the result may 59 // be different from the order in 'groups'. 60 // 61 // May return errors if underlying datastore has issues. 62 CheckMembership(ctx context.Context, id identity.Identity, groups []string) ([]string, error) 63 64 // HasPermission returns true if the identity has the given permission in the 65 // realm. 66 // 67 // A non-existing realm is replaced with the corresponding root realm (e.g. if 68 // "projectA:some/realm" doesn't exist, "projectA:@root" will be used in its 69 // place). If the project doesn't exist or is not using realms yet, all its 70 // realms (including the root realm) are considered empty. HasPermission 71 // returns false in this case. 72 // 73 // Attributes are the context of this particular permission check and are used 74 // as inputs to `conditions` predicates in conditional bindings. If a service 75 // supports conditional bindings, it must document what attributes it passes 76 // with each permission it checks. 77 // 78 // Returns an error only if the check itself failed due to a misconfiguration 79 // or transient issues. This should usually result in an Internal error. 80 HasPermission(ctx context.Context, id identity.Identity, perm realms.Permission, realm string, attrs realms.Attrs) (bool, error) 81 82 // QueryRealms returns a list of realms where the identity has the given 83 // permission. 84 // 85 // If `project` is not empty, restricts the check only to the realms in this 86 // project, otherwise checks all realms across all projects. Either way, the 87 // returned realm names have form `<some-project>:<some-realm>`. The list is 88 // returned in some arbitrary order. 89 // 90 // Semantically it is equivalent to visiting all explicitly defined realms 91 // (plus "<project>:@root" and "<project>:@legacy") in the requested project 92 // or all projects, and calling HasPermission(id, perm, realm, attr) for each 93 // of them. 94 // 95 // The permission `perm` should be flagged in the process with 96 // UsedInQueryRealms flag, which lets the runtime know it must prepare indexes 97 // for the corresponding QueryRealms call. 98 // 99 // Returns an error only if the check itself failed due to a misconfiguration 100 // or transient issues. This should usually result in an Internal error. 101 QueryRealms(ctx context.Context, id identity.Identity, perm realms.Permission, project string, attrs realms.Attrs) ([]string, error) 102 103 // FilterKnownGroups filters the list of groups keeping only ones that exist. 104 // 105 // May return errors if underlying datastore has issues. If all groups are 106 // unknown, returns an empty list and no error. 107 FilterKnownGroups(ctx context.Context, groups []string) ([]string, error) 108 109 // GetCertificates returns a bundle with certificates of a trusted signer. 110 // 111 // Returns (nil, nil) if the given signer is not trusted. 112 // 113 // Returns errors (usually transient) if the bundle can't be fetched. 114 GetCertificates(ctx context.Context, id identity.Identity) (*signing.PublicCertificates, error) 115 116 // GetAllowlistForIdentity returns name of the IP allowlist to use to check 117 // IP of requests from given `ident`. 118 // 119 // It's used to restrict access for certain account to certain IP subnets. 120 // 121 // Returns ("", nil) if `ident` is not IP restricted. 122 GetAllowlistForIdentity(ctx context.Context, ident identity.Identity) (string, error) 123 124 // IsAllowedIP returns true if IP address belongs to given named IP allowlist. 125 // 126 // An IP allowlist is a set of IP subnets. Unknown allowlists are considered 127 // empty. May return errors if underlying datastore has issues. 128 IsAllowedIP(ctx context.Context, ip net.IP, allowlist string) (bool, error) 129 130 // GetAuthServiceURL returns root URL ("https://<host>") of the auth service. 131 // 132 // Returns an error if the DB implementation is not using an auth service. 133 GetAuthServiceURL(ctx context.Context) (string, error) 134 135 // GetTokenServiceURL returns root URL ("https://<host>") of the token server. 136 // 137 // Returns an error if the DB implementation doesn't know how to retrieve it. 138 // 139 // Returns ("", nil) if the token server URL is not configured. 140 GetTokenServiceURL(ctx context.Context) (string, error) 141 142 // GetRealmData returns data attached to a realm. 143 // 144 // Falls back to the "@root" realm if `realm` doesn't exist. Returns nil if 145 // the root realm doesn't exist either, which means that either project 146 // doesn't exist or it has no realms.cfg file. 147 // 148 // Returns an error only if the check itself failed due to a misconfiguration 149 // or transient issues. This should usually result in an Internal error. 150 GetRealmData(ctx context.Context, realm string) (*protocol.RealmData, error) 151 }