-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Fix SSH auth lfs locks #3152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix SSH auth lfs locks #3152
Changes from 4 commits
ac9e714
bad1991
ef09252
faf2196
5064568
c8fce15
2036540
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,28 +11,39 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
api "code.gitea.io/sdk/gitea" | ||
) | ||
|
||
// LFSLock represents a git lfs lock of repository. | ||
type LFSLock struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"INDEX NOT NULL"` | ||
Owner *User `xorm:"-"` | ||
OwnerID int64 `xorm:"INDEX NOT NULL"` | ||
Path string `xorm:"TEXT"` | ||
Created time.Time `xorm:"created"` | ||
ID int64 `xorm:"pk autoincr"` | ||
Repo *Repository `xorm:"-"` | ||
RepoID int64 `xorm:"INDEX NOT NULL"` | ||
Owner *User `xorm:"-"` | ||
OwnerID int64 `xorm:"INDEX NOT NULL"` | ||
Path string `xorm:"TEXT"` | ||
Created time.Time `xorm:"created"` | ||
} | ||
|
||
// BeforeInsert is invoked from XORM before inserting an object of this type. | ||
func (l *LFSLock) BeforeInsert() { | ||
l.OwnerID = l.Owner.ID | ||
l.RepoID = l.Repo.ID | ||
l.Path = cleanPath(l.Path) | ||
} | ||
|
||
// AfterLoad is invoked from XORM after setting the values of all fields of this object. | ||
func (l *LFSLock) AfterLoad() { | ||
l.Owner, _ = GetUserByID(l.OwnerID) | ||
var err error | ||
l.Owner, err = GetUserByID(l.OwnerID) | ||
if err != nil { | ||
log.Error(2, "LFS lock AfterLoad failed OwnerId[%d] not found: %v", l.OwnerID, err) | ||
} | ||
l.Repo, err = GetRepositoryByID(l.RepoID) | ||
if err != nil { | ||
log.Error(2, "LFS lock AfterLoad failed RepoId[%d] not found: %v", l.RepoID, err) | ||
} | ||
} | ||
|
||
func cleanPath(p string) string { | ||
|
@@ -53,12 +64,12 @@ func (l *LFSLock) APIFormat() *api.LFSLock { | |
|
||
// CreateLFSLock creates a new lock. | ||
func CreateLFSLock(lock *LFSLock) (*LFSLock, error) { | ||
err := CheckLFSAccessForRepo(lock.Owner, lock.RepoID, "create") | ||
err := CheckLFSAccessForRepo(lock.Owner, lock.Repo, AccessModeWrite) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
l, err := GetLFSLock(lock.RepoID, lock.Path) | ||
l, err := GetLFSLock(lock.Repo, lock.Path) | ||
if err == nil { | ||
return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path} | ||
} | ||
|
@@ -71,15 +82,15 @@ func CreateLFSLock(lock *LFSLock) (*LFSLock, error) { | |
} | ||
|
||
// GetLFSLock returns release by given path. | ||
func GetLFSLock(repoID int64, path string) (*LFSLock, error) { | ||
func GetLFSLock(repo *Repository, path string) (*LFSLock, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? Is it really worth passing the repository if the only thing we care about it the id? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I remember it is to use HasAccess that need a repo reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I could make a func accessLevel and hasAccess that use repoID more generally for better perf ? (I think in a other PR ?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I must have missed the call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact after reviewing code of accessLevel, it use repo.IsPrivate that need a repo reference so. |
||
path = cleanPath(path) | ||
rel := &LFSLock{RepoID: repoID} | ||
rel := &LFSLock{RepoID: repo.ID} | ||
has, err := x.Where("lower(path) = ?", strings.ToLower(path)).Get(rel) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !has { | ||
return nil, ErrLFSLockNotExist{0, repoID, path} | ||
return nil, ErrLFSLockNotExist{0, repo.ID, path} | ||
} | ||
return rel, nil | ||
} | ||
|
@@ -109,7 +120,7 @@ func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) { | |
return nil, err | ||
} | ||
|
||
err = CheckLFSAccessForRepo(u, lock.RepoID, "delete") | ||
err = CheckLFSAccessForRepo(u, lock.Repo, AccessModeWrite) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -123,24 +134,15 @@ func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) { | |
} | ||
|
||
//CheckLFSAccessForRepo check needed access mode base on action | ||
func CheckLFSAccessForRepo(u *User, repoID int64, action string) error { | ||
func CheckLFSAccessForRepo(u *User, repo *Repository, mode AccessMode) error { | ||
if u == nil { | ||
return ErrLFSLockUnauthorizedAction{repoID, "undefined", action} | ||
} | ||
mode := AccessModeRead | ||
if action == "create" || action == "delete" || action == "verify" { | ||
mode = AccessModeWrite | ||
} | ||
|
||
repo, err := GetRepositoryByID(repoID) | ||
if err != nil { | ||
return err | ||
return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode} | ||
} | ||
has, err := HasAccess(u.ID, repo, mode) | ||
if err != nil { | ||
return err | ||
} else if !has { | ||
return ErrLFSLockUnauthorizedAction{repo.ID, u.DisplayName(), action} | ||
return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode} | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will performing database queries inside
AfterLoad
lead to deadlock problems if the query that triggeredAfterLoad
is part of a transaction? See #1813There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ethantkoenig queries should not be affected by locking and will load last committed data even if update/insert is in progress
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. In #1813 the offending SQL was a write which blocked on an ongoing transaction, but if you're sure that reading won't block on ongoing transactions then this is okay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use
to avoid that.