Repository navigation
lib/syncthing: Clean up / refactor LoadOrGenerateCertificate() utility function. - #8025
Conversation
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
left a comment
There was a problem hiding this comment.
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?
|
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. |
I wouldn't necessarily move it, so that the
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.
|
It's not quite easy to persist the key and cert to PEM files once they have been converted to a If you like this approach, I can continue and remove the use of temporary files in tests where not needed. |
| } | ||
|
|
||
| return tls.LoadX509KeyPair(certFile, keyFile) | ||
| return tls.X509KeyPair(pem.EncodeToMemory(certBlock), pem.EncodeToMemory(keyBlock)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
|
Thanks @imsodin. |
* 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 ...
Just some cleanup around the
LoadOrGenerateCertificate()function. It should adhere to the given arguments instead of delegating to thelocationspackage. 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
locationspackage already provides the desired paths. Many other functions here take a path as argument and mix it with somelocations.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.