Skip to content

cmd/syncthing: Implement generate as a subcommand with optional API credential setting (fixes #8021) - #8043

Merged
acolomb merged 13 commits into
syncthing:mainfrom
acolomb:cli-generate-gui-auth
Nov 18, 2021
Merged

acolomb merged 13 commits into
syncthing:mainfrom
acolomb:cli-generate-gui-auth

Conversation

@acolomb

@acolomb acolomb commented Nov 8, 2021 •

Copy link
Copy Markdown
Member

Purpose

The --generate option for the syncthing serve subcommand allows creating an initial configuration and keypair given a home / config directory. This functionality is mirrored to a new subcommand syncthing serve with the following extensions:

  1. Do not require a directory to be specified, but use the default location.
  2. Accept the standard --home or --config options to override the default location, as used in the serve subcommand.
  3. Accept --gui-user=... argument to preconfigure the generated config for GUI authentication. This overwrites the setting should the config.xml file already exist.
  4. Accept --gui-password=... argument to preconfigure the generated config for GUI authentication. This overwrites the setting should the config.xml file 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 --generate option still works unmodified. Verified correct usage / help messages for both syncthing serve and syncthing generate.

Screenshots

$ ./bin/syncthing generate --help
Usage: syncthing generate

Generate key and config, then exit

Flags:
  -h, --help                   Show context-sensitive help.

      --config=PATH            Set configuration directory (config and keys)
      --home=PATH              Set configuration and data directory
      --no-default-folder      Don't create the "default" folder on first startup ($STNODEFAULTFOLDER)
      --gui-user=STRING        Override GUI authentication user name
      --gui-password=STRING    Override GUI authentication password (use - to read from standard input)

Documentation

See syncthing/docs#694.

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.
@acolomb acolomb linked an issue Nov 8, 2021 that may be closed by this pull request

@calmh calmh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good

Comment thread cmd/syncthing/generate/generate.go Resolved
Comment thread cmd/syncthing/generate/generate.go Resolved
Comment thread cmd/syncthing/generate/generate.go Resolved
@acolomb

acolomb commented Nov 8, 2021

Copy link
Copy Markdown
Member Author

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is comparing a hash against a plain-text password, isn't it?
Probably the field should be named Password but PasswordHash.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I see your concern and that makes sense to avoid. My thought was about the opposite case, supplying an already hashed password.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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...

Comment thread cmd/syncthing/generate/generate.go Outdated

if guiPassword != "" && guiCfg.Password != guiPassword {
if err := guiCfg.HashAndSetPassword(guiPassword); err != nil {
log.Fatal("Failed to set GUI authentication password.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The calling function returns errors, so I'd expect this to return an error too to be passed upwards.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm. Maybe I just don't get the timing of it. Should I check for that passed-out error after the Wait() call then?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Committed a simplified closure, please check if that's more to your taste.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread lib/syncthing/utils.go Resolved
@acolomb

acolomb commented Nov 16, 2021

Copy link
Copy Markdown
Member Author

Anything else I can do to get this across the finish line? Currently waiting for some comment replies from @imsodin.

Comment thread cmd/syncthing/generate/generate.go Resolved
@acolomb
acolomb merged commit 100870e into syncthing:main Nov 18, 2021
@acolomb

acolomb commented Nov 18, 2021

Copy link
Copy Markdown
Member Author

Oh I just noticed merging this when the RC is already out maybe wasn't the best idea. Sorry.

calmh added a commit to calmh/syncthing that referenced this pull request Nov 22, 2021
* 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
  ...
calmh added a commit to calmh/syncthing that referenced this pull request Nov 22, 2021
* 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
  ...
@acolomb
acolomb deleted the cli-generate-gui-auth branch November 22, 2021 20:59
@calmh calmh added this to the v1.18.5 milestone Nov 23, 2021
@st-review st-review added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Mar 10, 2023
@syncthing syncthing locked and limited conversation to collaborators Mar 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a way to preset GUI credentials with password hashing

4 participants