summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2019-02-03 23:56:53 +0000
committerGitHub <noreply@github.com>2019-02-03 23:56:53 +0000
commit01c10a951b9db0b9f020ef657ca71eb5e5882a84 (patch)
tree6cb29db94edda9d90e9a509028bada2a61ade928 /modules
parent634cbaad2b8d091a2a3065de81e0b2b76daa5ef7 (diff)
downloadgitea-01c10a951b9db0b9f020ef657ca71eb5e5882a84.tar.gz
gitea-01c10a951b9db0b9f020ef657ca71eb5e5882a84.zip
Fix ssh deploy and user key constraints (#1357) (#5939)
1. A key can either be an ssh user key or a deploy key. It cannot be both. 2. If a key is a user key - it can only be associated with one user. 3. If a key is a deploy key - it can be used in multiple repositories and the permissions it has on those repositories can be different. 4. If a repository is deleted, its deploy keys must be deleted too. We currently don't enforce any of this and multiple repositories access with different permissions doesn't work at all. This PR enforces the following constraints: - [x] You should not be able to add the same user key as another user - [x] You should not be able to add a ssh user key which is being used as a deploy key - [x] You should not be able to add a ssh deploy key which is being used as a user key - [x] If you add an ssh deploy key to another repository you should be able to use it in different modes without losing the ability to use it in the other mode. - [x] If you delete a repository you must delete all its deploy keys. Fix #1357
Diffstat (limited to 'modules')
-rw-r--r--modules/private/key.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/modules/private/key.go b/modules/private/key.go
index 86d0a730d1..1c6511846b 100644
--- a/modules/private/key.go
+++ b/modules/private/key.go
@@ -32,6 +32,31 @@ func UpdateDeployKeyUpdated(keyID int64, repoID int64) error {
return nil
}
+// GetDeployKey check if repo has deploy key
+func GetDeployKey(keyID, repoID int64) (*models.DeployKey, error) {
+ reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/keys/%d", repoID, keyID)
+ log.GitLogger.Trace("GetDeployKey: %s", reqURL)
+
+ resp, err := newInternalRequest(reqURL, "GET").Response()
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ switch resp.StatusCode {
+ case 404:
+ return nil, nil
+ case 200:
+ var dKey models.DeployKey
+ if err := json.NewDecoder(resp.Body).Decode(&dKey); err != nil {
+ return nil, err
+ }
+ return &dKey, nil
+ default:
+ return nil, fmt.Errorf("Failed to get deploy key: %s", decodeJSONError(resp).Err)
+ }
+}
+
// HasDeployKey check if repo has deploy key
func HasDeployKey(keyID, repoID int64) (bool, error) {
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/repositories/%d/has-keys/%d", repoID, keyID)