ssl.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package system
  2. import (
  3. "os"
  4. "github.com/0xJacky/Nginx-UI/internal/helper"
  5. "github.com/0xJacky/Nginx-UI/internal/nginx"
  6. "github.com/uozi-tech/cosy"
  7. )
  8. // ValidateSSLCertificates checks if SSL certificate and key files exist and are under Nginx config directory
  9. // Returns nil if valid, or a CosyError if invalid
  10. func ValidateSSLCertificates(sslCert, sslKey string) error {
  11. // Check if both paths are provided
  12. if sslCert == "" {
  13. return ErrSSLCertRequired
  14. }
  15. if sslKey == "" {
  16. return ErrSSLKeyRequired
  17. }
  18. // Get Nginx configuration directory
  19. nginxConfPath := nginx.GetConfPath()
  20. // Check if certificate file exists and is under Nginx config directory
  21. if !helper.IsUnderDirectory(sslCert, nginxConfPath) {
  22. return cosy.WrapErrorWithParams(ErrSSLCertNotUnderConf, nginxConfPath)
  23. }
  24. // Check if certificate file exists
  25. if _, err := os.Stat(sslCert); os.IsNotExist(err) {
  26. return ErrSSLCertNotFound
  27. }
  28. // Check if key file is under Nginx config directory
  29. if !helper.IsUnderDirectory(sslKey, nginxConfPath) {
  30. return cosy.WrapErrorWithParams(ErrSSLKeyNotUnderConf, nginxConfPath)
  31. }
  32. // Check if key file exists
  33. if _, err := os.Stat(sslKey); os.IsNotExist(err) {
  34. return ErrSSLKeyNotFound
  35. }
  36. return nil
  37. }