-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Adding TLS-HTTPS configuration #288
Changes from 6 commits
172e49b
62a96e0
d704276
376b466
e2215be
b09119f
8837774
619a8a3
324fd39
beaa4db
465b992
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ docs/package-lock.json | |
docs/.hugo_build.lock | ||
**.hugo_build.lock | ||
|
||
certs |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the refactor to an other branch |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package controller | ||
|
||
import ( | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
net_http "net/http" | ||
"os" | ||
"reflect" | ||
"soarca/internal/database/memory" | ||
|
@@ -178,7 +180,18 @@ func Initialize() error { | |
} | ||
|
||
port := utils.GetEnv("PORT", "8080") | ||
err = app.Run(":" + port) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. app.RunTLS |
||
enableTLS, _ := strconv.ParseBool(utils.GetEnv("ENABLE_TLS", "false")) | ||
certFile := utils.GetEnv("CERT_FILE", "./certs/server.crt") | ||
keyFile := utils.GetEnv("CERT_KEY_FILE", "./certs/server.key") | ||
|
||
if enableTLS { | ||
if certFile == "" || keyFile == "" { | ||
err := fmt.Errorf("TLS enabled but certificate or key file not provided") | ||
log.Error(err) | ||
return err | ||
} | ||
} | ||
err = runServer(app, port, enableTLS, certFile, keyFile) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to separate function |
||
if err != nil { | ||
log.Error("failed to run gin") | ||
} | ||
|
@@ -187,6 +200,44 @@ func Initialize() error { | |
return err | ||
} | ||
|
||
func createHTTPServer(app *gin.Engine, port string) *net_http.Server { | ||
return &net_http.Server{ | ||
Addr: ":" + port, | ||
Handler: app, | ||
TLSConfig: &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
}, | ||
} | ||
} | ||
|
||
func validateCertificates(certFile string, keyFile string) error { | ||
_, err := os.Stat(certFile) | ||
if os.IsNotExist(err) { | ||
return fmt.Errorf("certificate file not found: %s", certFile) | ||
} | ||
if os.IsNotExist(err) { | ||
_, err = os.Stat(keyFile) | ||
return fmt.Errorf("key file not found: %s", keyFile) | ||
} | ||
return nil | ||
} | ||
|
||
func runServer(app *gin.Engine, port string, enableTlS bool, certFile string, keyFile string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Limit to app and enableTLS |
||
server := createHTTPServer(app, port) | ||
if enableTlS { | ||
err := validateCertificates(certFile, keyFile) | ||
if err != nil { | ||
return fmt.Errorf("TLS configuration error: %w", err) | ||
} | ||
|
||
log.Infof("Starting HTTPS server on port %s", port) | ||
return server.ListenAndServeTLS(certFile, keyFile) | ||
} | ||
|
||
log.Infof("Starting HTTP server on port %s", port) | ||
return server.ListenAndServe() | ||
} | ||
|
||
func initializeCore(app *gin.Engine) error { | ||
origins := strings.Split(strings.ReplaceAll(utils.GetEnv("SOARCA_ALLOWED_ORIGINS", "*"), " ", ""), ",") | ||
routes.Cors(app, origins) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only minimal config in here