Skip to content

lib/syncthing: Clean up / refactor LoadOrGenerateCertificate() utility function. - #8025

Merged
acolomb merged 8 commits into
syncthing:mainfrom
acolomb:fix-unused-cert-paths
Nov 7, 2021
Merged

acolomb merged 8 commits into
syncthing:mainfrom
acolomb:fix-unused-cert-paths

Conversation

@acolomb

@acolomb acolomb commented Oct 29, 2021

Copy link
Copy Markdown
Member

Just some cleanup around the LoadOrGenerateCertificate() function. It should adhere to the given arguments instead of delegating to the locations package. This is technically a no-op change, because the only place calling this function passes exactly the same values.

I've chosen to keep the arguments although in principle the locations package already provides the desired paths. Many other functions here take a path as argument and mix it with some locations.Get() values. If you think the latter should be used consistently, I will switch over to that method as far as possible.

Also a no-op is to factor out the second half of that function and reuse it in cmd/syncthing. That previously had its own independent (but matching) values for certificate name and lifetime, which is prone to get out of sync.

LoadOrGenerateCertificate() takes two file path arguments, but then
uses the locations package to determine the actual path.  Fix that
with a minimally invasive change, by using the arguments instead.

The only caller of this function is cmd/syncthing, which passes the
same values, so this is technically a no-op.
For possible future use of this function in other packages, split off
the basic function GenerateCertificate() which just passes the right
arguments to the tlsutil package.

The intention is to keep the tlsDefaultCommonName and
deviceCertLifetimeDays constants "hidden" in one central place,
instead of other code using their own values directly (namely
cmd/syncthing/main.go).

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

Good fix on the arguments, that's a bug. However, this seems like it could live in tlsutil as it's just a thin wrapper around another tls / tlsutil call?

@AudriusButkevicius

Copy link
Copy Markdown
Member

Also, while we are at it, could we separate the generation from persisting?

There was a number of times when I needed certs in tests, and the only way to get them, is to persist them on disk. There is no reason why this can't generate certs in memory, and then if needed, pass them to a different function to persist to disk.

@acolomb

acolomb commented Oct 30, 2021

Copy link
Copy Markdown
Member Author

Good fix on the arguments, that's a bug. However, this seems like it could live in tlsutil as it's just a thin wrapper around another tls / tlsutil call?

I wouldn't necessarily move it, so that the tlsDefaultCommonName and deviceCertLifetimeDays constants can stay unexported members of the lib/syncthing package. The same tlsutil function is used for the API cert, but with different lifetime and commonName settings. So it makes sense to keep the wrappers in the corresponding package.

Also, while we are at it, could we separate the generation from persisting?

Will look into it. That makes the wrapper functions just a tiny bit less thin, then I'd keep them.

Factor out the key / cert generation from the part of saving them to
files.  To keep the API compatible, allow passing empty names for
certFile and keyFile to skip writing them.
@acolomb

acolomb commented Oct 31, 2021

Copy link
Copy Markdown
Member Author

It's not quite easy to persist the key and cert to PEM files once they have been converted to a tls.Certificate structure AFAICS. And passing around other data structures like pem.Block would make the utility functions less useful because they don't actually create a TLS certificate. So I opted to just make the saving part optional by passing empty strings for keyFile and / or certFile and thus keep the NewCertificate() function API compatible.

If you like this approach, I can continue and remove the use of temporary files in tests where not needed.

Comment thread lib/tlsutil/tlsutil.go
}

return tls.LoadX509KeyPair(certFile, keyFile)
return tls.X509KeyPair(pem.EncodeToMemory(certBlock), pem.EncodeToMemory(keyBlock))

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.

I dislike the pass empty string to not save. Why can't it just be two methods? I guess you can't go from tls.Cert back to pem blocks?

@acolomb acolomb Nov 1, 2021 •

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.

Yes, at least I couldn't figure out how to do that. Something happens with certificate chains and parsing and you cannot recover the original form. If you find a way, I'm all ears.

@imsodin imsodin Nov 6, 2021 •

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's about the call signature, where both empty strings and two methods isn't particularly nice, but I too would prefer the latter. Something like e.g.

func NewCertificate(certFile, keyFile string, commonName string, lifetimeDays int) (tls.Certificate, error)

func NewCertificateMemoryOnly(commonName string, lifetimeDays int) (tls.Certificate, error)

Or alternatively export tlsutil.generateCertificate, and have

func NewCertificate(certBlock, keyBlock *pem.Block) (tls.Certificate, error)

func SaveCertificate(certBlock, keyBlock *pem.Block) error

or possibly encapsulate the two *pem.Block variables in a type in tlsutil (RawCertificate, CertificateBlocks ?) to not even expose that "implementation detail". However that's just some futher thought, the main point is the "empty string means don't save" mechanism.

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 find it strange if a caller needs to call GenerateCertificate() and then NewCertificate() to actually use it. Especially when the tlsutil package is obviously meant to make the process easier.

So I went with a separate function NewCertificateInMemory(). OK?

Comment thread lib/syncthing/utils.go Resolved
@acolomb
acolomb merged commit ec8a748 into syncthing:main Nov 7, 2021
@acolomb
acolomb deleted the fix-unused-cert-paths branch November 7, 2021 23:00
@acolomb

acolomb commented Nov 7, 2021

Copy link
Copy Markdown
Member Author

Thanks @imsodin.

@calmh calmh added this to the v1.18.5 milestone Nov 10, 2021
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
  ...
@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.

5 participants