Qiao-yq commented on code in PR #1592: URL: https://github.com/apache/dubbo-admin/pull/1592#discussion_r4336693604
########## ai/security/security.go: ########## @@ -0,0 +1,183 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package security + +import ( + "context" + "errors" + "net/http" + "strings" +) + +const ( + AuthMethodStatic = "static" + AuthMethodSystem = "system" + + ScopeSessionAdmin = "ai:sessions:admin" +) + +var ( + ErrAuthenticationRequired = errors.New("authentication required") + ErrResourceNotAccessible = errors.New("resource not accessible") +) + +// Subject is the authenticated identity supplied by a trusted server-side +// authenticator. Request payloads and ordinary client headers are not identity +// sources. +type Subject struct { + TenantID string + UserID string + Roles []string + Scopes []string + AuthMethod string +} + +func (s Subject) Validate() error { + if strings.TrimSpace(s.TenantID) == "" || strings.TrimSpace(s.UserID) == "" || strings.TrimSpace(s.AuthMethod) == "" { + return ErrAuthenticationRequired + } + return nil +} + +func (s Subject) HasScope(scope string) bool { + for _, candidate := range s.Scopes { + if candidate == scope { + return true + } + } + return false +} + +// ResourceReference is an untrusted resource requested by a caller. Its +// presence in AccessContext is not proof that access has been authorized. +type ResourceReference struct { + Kind string + ID string +} + +// AccessContext is passed explicitly to every Store operation that can read or +// mutate user-owned data. +type AccessContext struct { + Subject Subject + ResourceReferences []ResourceReference + systemCapability bool +} + +func NewAccessContext(subject Subject, references ...ResourceReference) (AccessContext, error) { + if err := subject.Validate(); err != nil { + return AccessContext{}, err + } + subject.Roles = append([]string(nil), subject.Roles...) + subject.Scopes = append([]string(nil), subject.Scopes...) + return AccessContext{Subject: subject, ResourceReferences: append([]ResourceReference(nil), references...)}, nil +} + +func MustAccessContext(subject Subject, references ...ResourceReference) AccessContext { + access, err := NewAccessContext(subject, references...) + if err != nil { + panic(err) + } + return access +} + +func SystemAccessContext() AccessContext { + return AccessContext{ + Subject: Subject{TenantID: "system", UserID: "system", AuthMethod: AuthMethodSystem}, + systemCapability: true, + } +} + +func (a AccessContext) Validate() error { return a.Subject.Validate() } + +func (a AccessContext) IsSystem() bool { return a.systemCapability } + +// CanAccessOwner intentionally grants no cross-tenant access. The reserved +// admin scope permits managing other owners only within the same tenant. +func (a AccessContext) CanAccessOwner(tenantID, ownerID string) bool { + if a.Validate() != nil { + return false + } + if a.IsSystem() { + return true + } + if a.Subject.TenantID != tenantID { + return false + } + return a.Subject.UserID == ownerID || a.Subject.HasScope(ScopeSessionAdmin) Review Comment: Fixed in 4c6bbad. Business access now rejects sessions with an empty tenant or owner, including access by same-tenant admins. Legacy adoption still only claims sessions where both fields are absent. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
