-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
68 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,58 @@ | ||
package systemapi | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"time" | ||
|
||
"github.com/flashbots/go-utils/tls" | ||
) | ||
|
||
func (s *Server) loadOrCreateTLSCert() error { | ||
_, _, err := tls.GenerateTLS(time.Hour*24*365, []string{}) | ||
return err | ||
// createTLSCertIfNotExists created a cert and key file if it doesn't exist yet | ||
func (s *Server) createTLSCertIfNotExists() error { | ||
log := s.log.With("cert", s.cfg.General.TLSCertPath, "key", s.cfg.General.TLSKeyPath) | ||
_, err1 := os.Stat(s.cfg.General.TLSCertPath) | ||
if err1 != nil && !os.IsNotExist(err1) { | ||
return err1 | ||
} | ||
|
||
_, err2 := os.Stat(s.cfg.General.TLSKeyPath) | ||
if err2 != nil && !os.IsNotExist(err2) { | ||
return err2 | ||
} | ||
|
||
certFileExists := err1 == nil | ||
keyFileExists := err2 == nil | ||
if certFileExists && keyFileExists { | ||
// Files exist, use them | ||
log.Info("TLS cert and key found, using them") | ||
return nil | ||
} else if certFileExists || keyFileExists { | ||
// Only one of the files exist, should not happen | ||
return errors.New("both TLS cert and key files are required, but only one exists") | ||
} | ||
|
||
// Files do not exist, should create them | ||
if !s.cfg.General.TLSCreateIfMissing { | ||
return errors.New("TLS cert and key files do not exist, but config is set to not create them") | ||
} | ||
|
||
// Create them | ||
cert, key, err := tls.GenerateTLS(time.Hour*24*365, s.cfg.General.TLSHosts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = os.WriteFile(s.cfg.General.TLSCertPath, cert, 0o600) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = os.WriteFile(s.cfg.General.TLSKeyPath, key, 0o600) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.With("hosts", s.cfg.General.TLSHosts).Info("TLS cert and key files created") | ||
return nil | ||
} |