Skip to content
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

Setting to disable authorized_keys backup #1856

Merged
merged 12 commits into from Jun 28, 2017

Conversation

dnmgns
Copy link
Contributor

@dnmgns dnmgns commented Jun 2, 2017

When using LDAP User Synchronization (#1478) with LDAP Public SSH Keys synchronization (#1844), the authorized_keys backup might be running with a quite high frequency (as it's called every time when rewriting public keys).

The system could end up with lots of backup-files causing inode issue and running out of free space.

This PR addresses this issue by giving the administrator the option to disable the authorized_keys backup when rewriting the public keys. This enables the administrator to perform the backup on his own outside of Gitea instead, or simply skip the backup as the keys are already stored in LDAP.

Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
@dnmgns dnmgns changed the title Setting to disable authorized key backup Setting to disable authorized_keys backup Jun 2, 2017
@sapk
Copy link
Member

sapk commented Jun 4, 2017

It's seems that the disabled part also have a part that recopy line from old version (that kind of weird but maybe to keep manually added keys). I am not against also disabling that but that should be explicitly describe in description of VAR (ex: this will override manually added keys). https://github.com/dnmgns/gitea/blob/78314cc1cdbcf2ba88bcf124de578c3b830af085/models/ssh_key.go#L591

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jun 4, 2017
@lunny lunny added this to the 1.3.0 milestone Jun 5, 2017
@dnmgns
Copy link
Contributor Author

dnmgns commented Jun 8, 2017

@sapk Yeah, seems that it has that part to save keys that has been added manually (written directly to authorized_keys file). As there's a tmp-file being created, we could just use that one for this purpose instead of the backup-file (old file). I've made some changes now, use the tmp-file for saving old keys instead (authorized_keys => tmp-file). And if backup is enabled, simply just copy the current authorized_keys file to a backup file.

Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking 🙂

os.Remove(tmpPath)
}()

if com.IsExist(fPath) && !setting.SSH.DisableAuthorizedKeysBackup {
bakPath := fPath + fmt.Sprintf("_%d.gitea_bak", time.Now().Unix())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're moving this line, mind changing it to

  bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix())

}

f.Close()
if err = os.Rename(tmpPath, fpath); err != nil {
t.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're closing this twice

Copy link
Contributor Author

@dnmgns dnmgns Jun 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I thought it looked like it but I also thought that maybe it was there for a reason that I didn't know about. FYI: This wasn't introduced in this PR, closing twice also exists in master (first and last row here: https://github.com/go-gitea/gitea/blob/master/models/ssh_key.go#L568-L606)

But I can fix it in this PR.

Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
@dnmgns
Copy link
Contributor Author

dnmgns commented Jun 13, 2017

@bkcsoft - Sent in some new commits that addresses the issues you found, also I'm changing the casing for vars from mixed to lowercase only. This seems to better follow the variable style from other functions.

Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
@bkcsoft
Copy link
Member

bkcsoft commented Jun 15, 2017

Question: why did you change the casing? tmpPath => tmppath. The go way of naming things is tmpPath (and is used throughout the rest of the code-base)

Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
@dnmgns
Copy link
Contributor Author

dnmgns commented Jun 15, 2017

Thanks @bkcsoft - Temporary confusion I believe :)

Copy link
Member

@bkcsoft bkcsoft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from this question you have my LGTM 🙂

conf/app.ini Outdated
@@ -120,6 +120,8 @@ SSH_ROOT_PATH =
SSH_KEY_TEST_PATH =
; Path to ssh-keygen, default is 'ssh-keygen' and let shell find out which one to call.
SSH_KEYGEN_PATH = ssh-keygen
; Disable SSH Authorized Key Backup when rewriting all keys, default is false
SSH_DISABLE_AUTHORIZED_KEYS_BACKUP = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like negations in configs. Would not SSH_ENABLE_AUTHORIZED_KEYS_BACKUP = true make more sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some other negations in the config, that's simply why I choose that style. But I don't mind changing this to enable/true instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer enable, reading negations is sometimes hard for non-native speakers 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And actually that would just be SSH_BACKUP_AUTHORIZED_KEYS = true

Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
os.Remove(tmpPath)
}()

if com.IsExist(fPath) && setting.SSH.AuthorizedKeysBackup {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better: if setting.SSH.AuthorizedKeysBackup && com.IsExist(fPath) { to not check for directory existence if backup is disabled anyway, disk i/o operations are not cheap especially on rpi

Signed-off-by: Magnus Lindvall <magnus@dnmgns.com>
@bkcsoft
Copy link
Member

bkcsoft commented Jun 26, 2017

LGTM actually I found more things 😛

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jun 26, 2017
f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
tmpPath := fPath + ".tmp"
t, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need a temp file? Since we're locking SSH access (line 324) we shouldn't need to do that :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it seems that the lock was introduced 3 years ago in ab747f2 and temp file has been there since then. This PR doesn't introduce any new tmp file, it just updates the variable name for the already existing one. While I may investigate if the tmp file is really needed (I also guess it isn't) and make the change for this in this PR, I believe it should be part of another PR intended to fix that specific issue. Your thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temp file could actually be needed because generating new one is more time consuming than file move operation so that it would not disrupt ssh service

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree that it should not be part of this one. Just noting that we're still trashing disk here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool, I've gotten so much great feedback here so I just thought that this was feedback as well :)

@bkcsoft
Copy link
Member

bkcsoft commented Jun 27, 2017

LGTM

@cez81
Copy link
Contributor

cez81 commented Jun 27, 2017

LGTM

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jun 27, 2017
@lunny lunny modified the milestones: 1.2.0, 1.3.0 Jun 28, 2017
@lunny lunny merged commit 79daf31 into go-gitea:master Jun 28, 2017
@lunny lunny added the type/enhancement An improvement of existing functionality label Aug 25, 2017
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
@dnmgns dnmgns deleted the disable_authorized_key_backup branch January 20, 2023 11:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants