Browse Source

add afunction to rewrite all public keys on admin request

refs #763
pull/818/head
fzerorubigd 10 years ago
parent
commit
6643647687
No known key found for this signature in database
GPG Key ID: D6EE858AF9D2999A
  1. 15
      conf/locale/locale_en-US.ini
  2. 25
      models/publickey.go
  3. 4
      routers/admin/admin.go
  4. 5
      templates/admin/dashboard.tmpl

15
conf/locale/locale_en-US.ini

@ -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.delete_repo_archives_success = All repositories archives have been deleted successfully.
dashboard.git_gc_repos = Do garbage collection on repositories dashboard.git_gc_repos = Do garbage collection on repositories
dashboard.git_gc_repos_success = All repositories have done garbage collection successfully. 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.server_uptime = Server Uptime
dashboard.current_goroutine = Current Goroutines dashboard.current_goroutine = Current Goroutines
dashboard.current_memory_usage = Current Memory Usage dashboard.current_memory_usage = Current Memory Usage
@ -711,16 +713,3 @@ months = %d months %s
years = %d years %s years = %d years %s
raw_seconds = seconds raw_seconds = seconds
raw_minutes = minutes raw_minutes = minutes

25
models/publickey.go

@ -163,7 +163,7 @@ func CheckPublicKeyString(content string) (bool, error) {
} }
// saveAuthorizedKeyFile writes SSH key content to authorized_keys file. // saveAuthorizedKeyFile writes SSH key content to authorized_keys file.
func saveAuthorizedKeyFile(key *PublicKey) error { func saveAuthorizedKeyFile(keys ...*PublicKey) error {
sshOpLocker.Lock() sshOpLocker.Lock()
defer sshOpLocker.Unlock() defer sshOpLocker.Unlock()
@ -188,8 +188,13 @@ func saveAuthorizedKeyFile(key *PublicKey) error {
} }
} }
for _, key := range keys {
_, err = f.WriteString(key.GetAuthorizedString()) _, err = f.WriteString(key.GetAuthorizedString())
if err != nil {
return err return err
}
}
return nil
} }
// AddPublicKey adds new public key to database and authorized_keys file. // 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) 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...)
}

4
routers/admin/admin.go

@ -118,6 +118,7 @@ const (
CLEAN_INACTIVATE_USER CLEAN_INACTIVATE_USER
CLEAN_REPO_ARCHIVES CLEAN_REPO_ARCHIVES
GIT_GC_REPOS GIT_GC_REPOS
SYNC_SSH_AUTHORIZED_KEY
) )
func Dashboard(ctx *middleware.Context) { func Dashboard(ctx *middleware.Context) {
@ -144,6 +145,9 @@ func Dashboard(ctx *middleware.Context) {
case GIT_GC_REPOS: case GIT_GC_REPOS:
success = ctx.Tr("admin.dashboard.git_gc_repos_success") success = ctx.Tr("admin.dashboard.git_gc_repos_success")
err = models.GitGcRepos() err = models.GitGcRepos()
case SYNC_SSH_AUTHORIZED_KEY:
success = ctx.Tr("admin.dashboard.resync_all_sshkeys_success")
err = models.RewriteAllPublicKeys()
} }
if err != nil { if err != nil {

5
templates/admin/dashboard.tmpl

@ -48,6 +48,11 @@
<td>{{.i18n.Tr "admin.dashboard.git_gc_repos"}}</td> <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> <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>
<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> </tbody>
</table> </table>
</div> </div>

Loading…
Cancel
Save