diff options
author | 无闻 <u@gogs.io> | 2015-02-01 16:56:34 -0500 |
---|---|---|
committer | 无闻 <u@gogs.io> | 2015-02-01 16:56:34 -0500 |
commit | cd6376f0930d6d0848936756675ffa4c96f6cea8 (patch) | |
tree | ef37bf1f30c88717d95838a1cdddce7eb54d71fc | |
parent | d4fc8880ebc1b05e19731f377c7b4d7dfc95ed56 (diff) | |
parent | 6643647687aa2ba8a41f8f95d404407ff6106d8c (diff) | |
download | gitea-cd6376f0930d6d0848936756675ffa4c96f6cea8.tar.gz gitea-cd6376f0930d6d0848936756675ffa4c96f6cea8.zip |
Merge pull request #818 from fzerorubigd/master
add a function to rewrite all public keys on admin request
-rw-r--r-- | conf/locale/locale_en-US.ini | 17 | ||||
-rw-r--r-- | models/publickey.go | 29 | ||||
-rw-r--r-- | routers/admin/admin.go | 4 | ||||
-rw-r--r-- | templates/admin/dashboard.tmpl | 5 |
4 files changed, 38 insertions, 17 deletions
diff --git a/conf/locale/locale_en-US.ini b/conf/locale/locale_en-US.ini index 534e8d3879..c0979d1bdb 100644 --- a/conf/locale/locale_en-US.ini +++ b/conf/locale/locale_en-US.ini @@ -66,7 +66,7 @@ app_url_helper = This affects HTTP/HTTPS clone URL and somewhere in e-mail. email_title = E-mail Service Settings (Optional) smtp_host = SMTP Host mailer_user = Sender E-mail -mailer_password = Sender Password +mailer_password = Sender Password notify_title = Notification Settings(Optional) register_confirm = Enable Register Confirmation mail_notify = Enable Mail Notification @@ -514,6 +514,8 @@ dashboard.delete_repo_archives = Delete all repositories archives dashboard.delete_repo_archives_success = All repositories archives have been deleted successfully. dashboard.git_gc_repos = Do garbage collection on repositories dashboard.git_gc_repos_success = All repositories have done garbage collection successfully. +dashboard.resync_all_sshkeys = Do resync .ssh/autorized_key file +dashboard.resync_all_sshkeys_success = All keys are synced again. dashboard.server_uptime = Server Uptime dashboard.current_goroutine = Current Goroutines dashboard.current_memory_usage = Current Memory Usage @@ -714,16 +716,3 @@ months = %d months %s years = %d years %s raw_seconds = seconds raw_minutes = minutes - - - - - - - - - - - - - diff --git a/models/publickey.go b/models/publickey.go index 566814e841..41233d0c39 100644 --- a/models/publickey.go +++ b/models/publickey.go @@ -244,7 +244,7 @@ func CheckPublicKeyString(content string) (bool, error) { } // saveAuthorizedKeyFile writes SSH key content to authorized_keys file. -func saveAuthorizedKeyFile(key *PublicKey) error { +func saveAuthorizedKeyFile(keys ...*PublicKey) error { sshOpLocker.Lock() defer sshOpLocker.Unlock() @@ -269,8 +269,13 @@ func saveAuthorizedKeyFile(key *PublicKey) error { } } - _, err = f.WriteString(key.GetAuthorizedString()) - return err + for _, key := range keys { + _, err = f.WriteString(key.GetAuthorizedString()) + if err != nil { + return err + } + } + return nil } // AddPublicKey adds new public key to database and authorized_keys file. @@ -422,3 +427,21 @@ func DeletePublicKey(key *PublicKey) error { } return os.Rename(tmpPath, fpath) } + +// RewriteAllPublicKeys remove any authorized key and re-write all key from database again +func RewriteAllPublicKeys() error { + keys := make([]*PublicKey, 0, 5) + err := x.Find(&keys) + if err != nil { + return err + } + + fpath := filepath.Join(SshPath, "authorized_keys") + if _, err := os.Stat(fpath); os.IsNotExist(err) { + return saveAuthorizedKeyFile(keys...) + } + if err := os.Remove(fpath); err != nil { + return err + } + return saveAuthorizedKeyFile(keys...) +} diff --git a/routers/admin/admin.go b/routers/admin/admin.go index 563a9cdc41..ea50d5c4cb 100644 --- a/routers/admin/admin.go +++ b/routers/admin/admin.go @@ -118,6 +118,7 @@ const ( CLEAN_INACTIVATE_USER CLEAN_REPO_ARCHIVES GIT_GC_REPOS + SYNC_SSH_AUTHORIZED_KEY ) func Dashboard(ctx *middleware.Context) { @@ -144,6 +145,9 @@ func Dashboard(ctx *middleware.Context) { case GIT_GC_REPOS: success = ctx.Tr("admin.dashboard.git_gc_repos_success") err = models.GitGcRepos() + case SYNC_SSH_AUTHORIZED_KEY: + success = ctx.Tr("admin.dashboard.resync_all_sshkeys_success") + err = models.RewriteAllPublicKeys() } if err != nil { diff --git a/templates/admin/dashboard.tmpl b/templates/admin/dashboard.tmpl index 8d17c360b0..b570517536 100644 --- a/templates/admin/dashboard.tmpl +++ b/templates/admin/dashboard.tmpl @@ -48,6 +48,11 @@ <td>{{.i18n.Tr "admin.dashboard.git_gc_repos"}}</td> <td><i class="fa fa-caret-square-o-right"></i> <a href="{{AppSubUrl}}/admin?op=4">{{.i18n.Tr "admin.dashboard.operation_run"}}</a></td> </tr> + <tr> + <td>{{.i18n.Tr "admin.dashboard.resync_all_sshkeys"}}</td> + <td><i class="fa fa-caret-square-o-right"></i> <a href="{{AppSubUrl}}/admin?op=5">{{.i18n.Tr "admin.dashboard.operation_run"}}</a></td> + </tr> + </tbody> </table> </div> |