]> source.dussan.org Git - gitea.git/commitdiff
add afunction to rewrite all public keys on admin request
authorfzerorubigd <fzerorubigd@gmail.com>
Wed, 31 Dec 2014 18:07:51 +0000 (21:37 +0330)
committerfzerorubigd <fzerorubigd@gmail.com>
Wed, 31 Dec 2014 22:45:52 +0000 (02:15 +0330)
refs #763

conf/locale/locale_en-US.ini
models/publickey.go
routers/admin/admin.go
templates/admin/dashboard.tmpl

index 34f9febc1ccee9a1dd2923aa6b697018b1ea88c8..deedbefab018b14b09832ff5840a232e880766eb 100644 (file)
@@ -64,7 +64,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
@@ -511,6 +511,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
@@ -711,16 +713,3 @@ months = %d months %s
 years = %d years %s
 raw_seconds = seconds
 raw_minutes = minutes
-
-
-
-
-
-
-
-
-
-
-
-
-
index ba15ca455336586041647702eebf41e2be2bbd67..fbba691abc04325b13b79f37093997a49ee551c8 100644 (file)
@@ -163,7 +163,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()
 
@@ -188,8 +188,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.
@@ -341,3 +346,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...)
+}
index 563a9cdc41f98b24954f6e0950a5c60fc5335fb5..ea50d5c4cbeac2de837956b8ed6bc4f36728e51c 100644 (file)
@@ -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 {
index 8d17c360b0889b911c4ad91a34addf6d2d2a50a1..b5705175360340d340db4e402c35d5afec272b7d 100644 (file)
                                                 <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>