go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/engine/permissions.go (about) 1 // Copyright 2020 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 engine 16 17 import ( 18 "context" 19 20 "go.chromium.org/luci/common/logging" 21 "go.chromium.org/luci/server/auth" 22 "go.chromium.org/luci/server/auth/realms" 23 ) 24 25 const adminGroup = "administrators" 26 27 var ( 28 PermJobsGet = realms.RegisterPermission("scheduler.jobs.get") 29 PermJobsPause = realms.RegisterPermission("scheduler.jobs.pause") 30 PermJobsResume = realms.RegisterPermission("scheduler.jobs.resume") 31 PermJobsAbort = realms.RegisterPermission("scheduler.jobs.abort") 32 PermJobsTrigger = realms.RegisterPermission("scheduler.jobs.trigger") 33 ) 34 35 // CheckPermission returns nil if the caller has the given permission for the 36 // job or ErrNoPermission otherwise. 37 // 38 // May also return transient errors. 39 func CheckPermission(ctx context.Context, job *Job, perm realms.Permission) error { 40 ctx = logging.SetField(ctx, "JobID", job.JobID) 41 42 // Fallback to the @legacy realm if the job entity isn't updated yet. 43 realm := job.RealmID 44 if realm == "" { 45 realm = realms.Join(job.ProjectID, realms.LegacyRealm) 46 } 47 48 // Check realm's bindings (including conditional ones). 49 attrs := realms.Attrs{ 50 "scheduler.job.name": job.JobName(), 51 } 52 switch yes, err := auth.HasPermission(ctx, perm, realm, attrs); { 53 case err != nil: 54 return err 55 case yes: 56 return nil 57 } 58 59 // Admins have implicit access to everything. 60 // TODO(vadimsh): We should probably remove this. 61 switch yes, err := auth.IsMember(ctx, adminGroup); { 62 case err != nil: 63 return err 64 case yes: 65 logging.Warningf(ctx, "ADMIN_FALLBACK: perm=%q job=%q caller=%q", 66 perm, job.JobID, auth.CurrentIdentity(ctx)) 67 return nil 68 default: 69 return ErrNoPermission 70 } 71 }