Repository navigation
cmd/syncthing: Implement generate as a subcommand with optional API credential setting (fixes #8021) - #8043
Conversation
The function is generic enough to live in the utils section of the syncthing package. It uses the targeted logging infrastructure, which is easier to reuse when found in a library package.
Instead of manually joining paths with "magic" file names, get them from the centralized locations helper lib.
Using the app-wide debug logger mechanism in generate() is not very useful, as the --logfile argument is interpreted by the monitor process and that cannot be in use when generate() is called. Switching to basic log.Println() calls allows to factor out this function into its own package for a subcommand, which won't need its own logger instance. Visible change: No more timestamp prefixes.
Accept a subcommand as an alternative to the --generate option. It accepts a custom config directory through either the --home or --config options, using the default location if neither is given.
Add the options --gui-user and --gui-password to "generate", but not the "serve --generate" option form. If either is given, an existing config will not abort the command, but rather load, modify and save it with the new credentials. The password can be read from standard input by passing only a single dash as argument. Config modification is skipped if the value matches what's already in the config. Especially passing in the hashed password will not cause a modification, but giving the same (plaintext) password again will result in varying hashes.
|
Regarding documentation, I could pack that into syncthing/docs#693, or wait for that to be merged and make a separate PR. |
| changed = true | ||
| } | ||
|
|
||
| if guiPassword != "" && guiCfg.Password != guiPassword { |
There was a problem hiding this comment.
This is comparing a hash against a plain-text password, isn't it?
Probably the field should be named Password but PasswordHash.
There was a problem hiding this comment.
You're right on both points. I personally think that renaming is not worth the effort.
The comparison is a special case, because I wanted to skip writing if the password didn't actually change. But the hash changes randomly (salted), so this is the only case we can actually detect. If you think it's not useful, we can drop it though.
There was a problem hiding this comment.
What would you detect? The hash being equal to the plain-text password isn't something that should ever happen, and if it does then because someone manually added it to the config, in which case it should also be replaced.
There was a problem hiding this comment.
I see your concern and that makes sense to avoid. My thought was about the opposite case, supplying an already hashed password.
There was a problem hiding this comment.
Ah right, now I get it - if for some arcane reason the user gets the hash from config and passes it to this function, it should just leave the password alone. Yeah I guess why not.
There was a problem hiding this comment.
and if it does then because someone manually added it to the config, in which case it should also be replaced.
That case is really not nice though. I'm undecided whether leaving a (wrongfully) unhashed password in the config when setting it again is wiser than trying to cater for this very hypothetical corner case...
|
|
||
| if guiPassword != "" && guiCfg.Password != guiPassword { | ||
| if err := guiCfg.HashAndSetPassword(guiPassword); err != nil { | ||
| log.Fatal("Failed to set GUI authentication password.") |
There was a problem hiding this comment.
The calling function returns errors, so I'd expect this to return an error too to be passed upwards.
There was a problem hiding this comment.
No, the ModifyFunction type in lib/config has no return value. I'm not sure it's worth the effort to pass an *Error pointer to it somehow? The boolean here is just used to optimize whether the GUIConfiguration replacement can be skipped.
There was a problem hiding this comment.
That's just a closure passed in. Getting the error out of that is as simple as declaring the error outside of the function and then assigning inside it.
There was a problem hiding this comment.
Hm. Maybe I just don't get the timing of it. Should I check for that passed-out error after the Wait() call then?
There was a problem hiding this comment.
Committed a simplified closure, please check if that's more to your taste.
There was a problem hiding this comment.
It doesn't matter where you do it - Wait returns immediately if there's no change. I would have probably checked it right after checking err.
There was a problem hiding this comment.
I'm sure Wait() will return immediately, but Modify() may do so as well, before the Serve() goroutine has finished? That's why I thought checking after Wait() is the only way to make sure the async function has completed running.
|
Anything else I can do to get this across the finish line? Currently waiting for some comment replies from @imsodin. |
|
Oh I just noticed merging this when the RC is already out maybe wasn't the best idea. Sorry. |
* main: (32 commits) cmd/syncthing: Implement generate as a subcommand with optional API credential setting (fixes syncthing#8021) (syncthing#8043) lib/model: Correct "reverting folder" log entry lib/model: Correct handling of fakefs cache gui, lib: Fix tracking deleted locally-changed on encrypted (fixes syncthing#7715) (syncthing#7726) lib/config: Move the bcrypt password hashing to GUIConfiguration (syncthing#8028) lib/syncthing: Clean up / refactor LoadOrGenerateCertificate() utility function. (syncthing#8025) lib/api: http.Request.BasicAuth instead of custom code (syncthing#8039) Normalize CLI options to always use two dashes. (syncthing#8037) gui: Display identicons for discovered device IDs. (syncthing#8022) cmd/syncthing/cli: indexDumpSize doesn't need a heap (syncthing#8024) lib/model: Optimize jobQueue performance and memory use (syncthing#8023) lib/model: Limit the number of default hashers on Android (ref syncthing#2220) lib/model: Set mod. time after writing trailer in shortcut (ref syncthing#7992) lib/protocol: Simplify codeToError, errorToCode lib/protocol: Eliminate nativeModel on Unix gui: Add direct link to Ignore Patterns from folder panel (fixes syncthing#4293) (syncthing#7993) gui: Translate theme names in settings (syncthing#8006) lib/model: Pull when a new connection is established (fixes syncthing#8012) (syncthing#8013) gui, man, authors: Update docs, translations, and contributors gui, man, authors: Update docs, translations, and contributors ...
* main: (32 commits) cmd/syncthing: Implement generate as a subcommand with optional API credential setting (fixes syncthing#8021) (syncthing#8043) lib/model: Correct "reverting folder" log entry lib/model: Correct handling of fakefs cache gui, lib: Fix tracking deleted locally-changed on encrypted (fixes syncthing#7715) (syncthing#7726) lib/config: Move the bcrypt password hashing to GUIConfiguration (syncthing#8028) lib/syncthing: Clean up / refactor LoadOrGenerateCertificate() utility function. (syncthing#8025) lib/api: http.Request.BasicAuth instead of custom code (syncthing#8039) Normalize CLI options to always use two dashes. (syncthing#8037) gui: Display identicons for discovered device IDs. (syncthing#8022) cmd/syncthing/cli: indexDumpSize doesn't need a heap (syncthing#8024) lib/model: Optimize jobQueue performance and memory use (syncthing#8023) lib/model: Limit the number of default hashers on Android (ref syncthing#2220) lib/model: Set mod. time after writing trailer in shortcut (ref syncthing#7992) lib/protocol: Simplify codeToError, errorToCode lib/protocol: Eliminate nativeModel on Unix gui: Add direct link to Ignore Patterns from folder panel (fixes syncthing#4293) (syncthing#7993) gui: Translate theme names in settings (syncthing#8006) lib/model: Pull when a new connection is established (fixes syncthing#8012) (syncthing#8013) gui, man, authors: Update docs, translations, and contributors gui, man, authors: Update docs, translations, and contributors ...
Purpose
The
--generateoption for thesyncthing servesubcommand allows creating an initial configuration and keypair given a home / config directory. This functionality is mirrored to a new subcommandsyncthing servewith the following extensions:--homeor--configoptions to override the default location, as used in theservesubcommand.--gui-user=...argument to preconfigure the generated config for GUI authentication. This overwrites the setting should theconfig.xmlfile already exist.--gui-password=...argument to preconfigure the generated config for GUI authentication. This overwrites the setting should theconfig.xmlfile already exist. The password can be read from the standard input stream by specifying-as value. Hashing the password is automatically taken care of.Besides tidying up the command line structure, the main use case for this is setting up GUI credentials from an installation wizard on headless systems (like NAS), which otherwise would start out with the GUI wide open.
Testing
Manual testing only: Generated new cert and config in empty home dir. Adjusted GUI user and password. Password read from pipe, standard input (terminated via EOF or line break), fails when empty password is piped in. Existing config stays untouched if neither
--gui-...option is given.Giving the existing password hash as password again skips the modification.Old--generateoption still works unmodified. Verified correct usage / help messages for bothsyncthing serveandsyncthing generate.Screenshots
Documentation
See syncthing/docs#694.