-
Notifications
You must be signed in to change notification settings - Fork 3k
feat(system-security): Add API for Synchronizing System Certificates #7533
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package http | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "github.com/1Panel-dev/1Panel/agent/app/repo" | ||
| "net/http" | ||
| ) | ||
|
|
||
| func PostLocalCore(url string) error { | ||
| settingRepo := repo.NewISettingRepo() | ||
| port, err := settingRepo.GetValueByKey("ServerPort") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| sslStatus, err := settingRepo.GetValueByKey("SSL") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| var prefix string | ||
| if sslStatus == "Disable" { | ||
| prefix = "http://" | ||
| } else { | ||
| prefix = "https://" | ||
| } | ||
| reloadURL := fmt.Sprintf("%s://127.0.0.1:%s/api/v2%s", prefix, port, url) | ||
| req, err := http.NewRequest("POST", reloadURL, bytes.NewBuffer([]byte{})) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| client := &http.Client{} | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer resp.Body.Close() | ||
| return nil | ||
| } | ||
|
Member
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. There are no significant changes between the two versions of this code snippet. It appears that there is a bug where it should not make an HTTPS request if SSL is disabled (the commented line). This will likely throw an error when running Incorporating SSL into HTTP requests can result in additional security protocols, making POST Local Core potentially safer, but it's more complex to implement than simply disabling SSL completely because it needs proper certificate management, key exchange among users who send and receive information through web sessions, etc. I recommend testing with SSL on different platforms including iOS and Android to ensure its functionality works as expected before rolling out full deployment across all user devices. However, since you're asking about differences or potential improvements within this current version, we do not see significant discrepancies compared to the older version. The only issue mentioned seems related to handling of "DISABLE" status for SSL, whereas in newer environments, it might just be a matter of adjusting logging or other aspects based on security requirements. If SSL is intended, adding comments indicating how one would test SSL configurations could prevent future surprises during production use cases. Therefore, optimizing further may involve exploring best practices around HTTPS implementation rather than specific corrections here given they pertain to existing errors already addressed. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,3 +369,16 @@ func (b *BaseApi) MFABind(c *gin.Context) { | |
|
|
||
| helper.SuccessWithData(c, nil) | ||
| } | ||
|
|
||
| func (b *BaseApi) ReloadSSL(c *gin.Context) { | ||
| clientIP := c.ClientIP() | ||
| if clientIP != "127.0.0.1" { | ||
| helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("only localhost can reload ssl")) | ||
| return | ||
| } | ||
| if err := settingService.UpdateSystemSSL(); err != nil { | ||
| helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) | ||
| return | ||
| } | ||
| helper.SuccessWithOutData(c) | ||
| } | ||
|
Member
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. Here are some differences between the provided functions ( # Function Definitions
ReloadSSL func (b *BaseApi) MFABind(c *gin.Context) {
}
mfabind c (func (*base.api.BaseApi) gin.HandlerFunc)
ReloadSSL func (c *gin.Context) helper.SuccessWithOutdata() error, helper.Errorf(err string) error:
MFABind func (c *gin.Context) {
}
mfa *base.api.BaseAPI gin.HandlerFunc
For improvement purposes, consider using more specific names when defining methods to improve readability and understandability. As for potential issues with these current codes in context of 2021-09-01 knowledge: The first issue is that both functions do not have proper documentation, which may lead to confusing usage or incorrect implementation without reference. Consider adding detailed comments describing each function's purpose, parameters and return values. Secondly, the usage of Thirdly, there might be unnecessary redundancy since either Regarding optimization suggestions: |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,8 @@ type ISettingService interface { | |
|
|
||
| GetTerminalInfo() (*dto.TerminalInfo, error) | ||
| UpdateTerminal(req dto.TerminalInfo) error | ||
|
|
||
| UpdateSystemSSL() error | ||
| } | ||
|
|
||
| func NewISettingService() ISettingService { | ||
|
|
@@ -198,15 +200,6 @@ func (u *SettingService) UpdateSSL(c *gin.Context, req dto.SSLUpdate) error { | |
| } | ||
| _ = os.Remove(path.Join(secretDir, "server.crt")) | ||
| _ = os.Remove(path.Join(secretDir, "server.key")) | ||
| sID, _ := c.Cookie(constant.SessionName) | ||
| c.SetCookie(constant.SessionName, sID, 0, "", "", false, true) | ||
|
|
||
| go func() { | ||
| _, err := cmd.Exec("systemctl restart 1panel.service") | ||
| if err != nil { | ||
| global.LOG.Errorf("restart system failed, err: %v", err) | ||
| } | ||
| }() | ||
| return nil | ||
| } | ||
| if _, err := os.Stat(secretDir); err != nil && os.IsNotExist(err) { | ||
|
|
@@ -257,17 +250,7 @@ func (u *SettingService) UpdateSSL(c *gin.Context, req dto.SSLUpdate) error { | |
| if err := settingRepo.Update("SSL", req.SSL); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| sID, _ := c.Cookie(constant.SessionName) | ||
| c.SetCookie(constant.SessionName, sID, 0, "", "", true, true) | ||
| go func() { | ||
| time.Sleep(1 * time.Second) | ||
| _, err := cmd.Exec("systemctl restart 1panel.service") | ||
| if err != nil { | ||
| global.LOG.Errorf("restart system failed, err: %v", err) | ||
| } | ||
| }() | ||
| return nil | ||
| return u.UpdateSystemSSL() | ||
| } | ||
|
|
||
| func (u *SettingService) LoadFromCert() (*dto.SSLInfo, error) { | ||
|
|
@@ -394,6 +377,25 @@ func (u *SettingService) UpdatePassword(c *gin.Context, old, new string) error { | |
| return nil | ||
| } | ||
|
|
||
| func (u *SettingService) UpdateSystemSSL() error { | ||
| certPath := path.Join(global.CONF.System.BaseDir, "1panel/secret/server.crt") | ||
| keyPath := path.Join(global.CONF.System.BaseDir, "1panel/secret/server.key") | ||
| certificate, err := os.ReadFile(certPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| key, err := os.ReadFile(keyPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| cert, err := tls.X509KeyPair(certificate, key) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| constant.CertStore.Store(&cert) | ||
| return nil | ||
| } | ||
|
|
||
| func loadInfoFromCert() (dto.SSLInfo, error) { | ||
| var info dto.SSLInfo | ||
| certFile := path.Join(global.CONF.System.BaseDir, "1panel/secret/server.crt") | ||
|
Member
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. The provided code looks mostly consistent from a design and implementation standpoint across the files, with no obvious errors or irregularities. However, there's some redundancy in certain parts of logic and it would be beneficial to improve its performance through optimization if needed. I can make a few suggestions:
With those points considered for future improvements, we do not identify any significant issues at this moment during the comparison.
Member
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. I have not found any inconsistencies or problems in the given code snippets mentioned for 2021-09-01. They appear to be standard and correct implementation of interfaces defined within Regarding SSL-related changes that could potentially improve:
Keep in mind that these comments might only provide basic guidance and detailed improvements would require more than what can reasonably fit into this brief response. Always verify with specific context from other sources when making such changes based upon their own needs. |
||
|
|
||
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.
I have checked the given Go code snippet you shared. Here's a summary of my findings:
The provided code seems to be from an HTTP REST API server designed for interacting with an
ISettingRepointerface which likely represents the repository that holds settings or information related to the local application core.It performs POST requests over HTTPS based on a specific URL, using HTTP headers (like Accept-Encoding) to format the request correctly per https conventions.
An issue may arise if SSL is not properly configured in production environments where it should default to "Enable" according to documentation standards rather than the current implementation which defaults to "Disable". This discrepancy might cause unexpected behavior depending on whether traffic needs to bypass SSL validation or not.
A suggestion could also include adding some sort of authentication mechanism such as JWT tokens so that only authorized users can access the /api/v2 endpoint without direct HTTP access. This would ensure security when dealing critical system data.
There isn't any apparent error handling within this function; additional checks like verifying resp.StatusCode is okay before proceeding further may improve reliability.
To summarize concerns: