Does anyone have any ideas of an easy path to load certificate and key files from a string rather than a file?
Use Case:
1. traditionally we all put a cleartext file on disk with our private key and public certificate. If the server is breached, we just regenerate all the things and move on.
2. I would like to store my certificates and keys in a more secure location (AWS SSM Param store, Hashicorp Vault, etc.).
3. The certificate is only read from file at startup as best I can tell, and relocating certificates and keys to an encrypted store would (a) allow better auditing when the content is accessed, (b) restrict access to only authorized processes and (c) make rotating keys and certificates a much easier process.
Analysis:
Current Functionality:
- We setup a server using ListenAndServeTLS() and pass in a filename for the certificate and key.
- In go1.17.1/src/net/http/server.go at 3066, tls.LoadX509KeyPair() loads is called.
- LoadX509KeyPair() exists at 230 in src/crypto/tls/tls.go and
- It calls os.ReadFile() at 231 and 235.
Possible Solution:
- We cannot break existing things, and within the limitations of golang, it is probably the least-disruptive solution to add a new ListenAndServeTLSFromVar() which would functionally do everything ListenAndServeTLS() does, but instead of reading a file, it would instead accept the input string as the certificate/key content.
- Alternatively ListenAndServeTLSFromVar() would accept a boolean parameter which would determine if certificate and key parameters are filenames or content strings. in this case, ListenAndServeTLSFromVar() would support both filenames and content string use cases and provide a path to unifying the approach if the community begins to adopt the use case identified above in large numbers.
You can already do this by creating an http.Server{} with a tls.Config initialized from the certificates you have. You have to decode and parse the certificates from strings to create the tls.Config.