-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(system): Fix issue with certificate failure in settings panel #8063
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,47 @@ | ||
| package model | ||
|
|
||
| import ( | ||
| "time" | ||
| ) | ||
|
|
||
| type WebsiteSSL struct { | ||
| BaseModel | ||
| PrimaryDomain string `json:"primaryDomain"` | ||
| PrivateKey string `json:"privateKey"` | ||
| Pem string `json:"pem"` | ||
| Domains string `json:"domains"` | ||
| CertURL string `json:"certURL"` | ||
| Type string `json:"type"` | ||
| Provider string `json:"provider"` | ||
| Organization string `json:"organization"` | ||
| DnsAccountID uint `json:"dnsAccountId"` | ||
| AcmeAccountID uint `gorm:"column:acme_account_id" json:"acmeAccountId" ` | ||
| CaID uint `json:"caId"` | ||
| AutoRenew bool `json:"autoRenew"` | ||
| ExpireDate time.Time `json:"expireDate"` | ||
| StartDate time.Time `json:"startDate"` | ||
| Status string `json:"status"` | ||
| Message string `json:"message"` | ||
| KeyType string `json:"keyType"` | ||
| PushDir bool `json:"pushDir"` | ||
| Dir string `json:"dir"` | ||
| Description string `json:"description"` | ||
| SkipDNS bool `json:"skipDNS"` | ||
| Nameserver1 string `json:"nameserver1"` | ||
| Nameserver2 string `json:"nameserver2"` | ||
| DisableCNAME bool `json:"disableCNAME"` | ||
| ExecShell bool `json:"execShell"` | ||
| Shell string `json:"shell"` | ||
| } | ||
|
|
||
| func (w WebsiteSSL) TableName() string { | ||
| return "website_ssls" | ||
| } | ||
|
|
||
| type WebsiteCA struct { | ||
| BaseModel | ||
| CSR string `gorm:"not null;" json:"csr"` | ||
| Name string `gorm:"not null;" json:"name"` | ||
| PrivateKey string `gorm:"not null" json:"privateKey"` | ||
| KeyType string `gorm:"not null;default:2048" json:"keyType"` | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package repo | ||
|
|
||
| import ( | ||
| "github.com/1Panel-dev/1Panel/core/app/model" | ||
| "github.com/1Panel-dev/1Panel/core/global" | ||
| ) | ||
|
|
||
| type AgentRepo struct{} | ||
|
|
||
| type IAgentRepo interface { | ||
| GetWebsiteSSL(opts ...global.DBOption) (model.WebsiteSSL, error) | ||
| GetCA(opts ...global.DBOption) (model.WebsiteCA, error) | ||
| } | ||
|
|
||
| func NewIAgentRepo() IAgentRepo { | ||
| return &AgentRepo{} | ||
| } | ||
|
|
||
| func (a *AgentRepo) GetWebsiteSSL(opts ...global.DBOption) (model.WebsiteSSL, error) { | ||
| var ssl model.WebsiteSSL | ||
| db := global.AgentDB | ||
| for _, opt := range opts { | ||
| db = opt(db) | ||
| } | ||
| err := db.First(&ssl).Error | ||
| return ssl, err | ||
| } | ||
|
|
||
| func (a *AgentRepo) GetCA(opts ...global.DBOption) (model.WebsiteCA, error) { | ||
| var ca model.WebsiteCA | ||
| db := global.AgentDB | ||
| for _, opt := range opts { | ||
| db = opt(db) | ||
| } | ||
| err := db.First(&ca).Error | ||
| return ca, err | ||
| } | ||
|
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. This code does not have any major issues or inefficiencies. It seems to be well-established and adherent to current standards in the Go programming language ecosystem. However, without reviewing it extensively for particular use-cases or patterns that deviate from best practices (e.g., using Potential improvements might include more concise variable names, improved readability (by removing unused lines of code), checking if options provided are valid (using error handling instead of empty strings used as an indicator), avoiding unnecessary queries like iterating over opts array, and ensuring consistency throughout the package structure. If you require a detailed review for specific conditions (such those outside the scope given here—i.e., no external database access needed at this level), please specify them, so tailored recommendations can be made accordingly. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/rand" | ||
| "crypto/rsa" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "encoding/json" | ||
| "encoding/pem" | ||
| "fmt" | ||
| "github.com/1Panel-dev/1Panel/core/app/model" | ||
| "github.com/1Panel-dev/1Panel/core/utils/req_helper" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "path" | ||
| "strconv" | ||
|
|
@@ -265,6 +269,48 @@ func (u *SettingService) UpdateSSL(c *gin.Context, req dto.SSLUpdate) error { | |
| return err | ||
| } | ||
| secret = string(certFile) | ||
| case "select": | ||
| ssl, err := agentRepo.GetWebsiteSSL(repo.WithByID(req.SSLID)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| secret = ssl.Pem | ||
| key = ssl.PrivateKey | ||
| if err := settingRepo.Update("SSLID", strconv.Itoa(int(req.SSLID))); err != nil { | ||
| return err | ||
| } | ||
| case "self": | ||
| ca, err := agentRepo.GetCA(repo.WithByName("1Panel")) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| params := make(map[string]interface{}) | ||
| params["domains"] = req.Domain | ||
| params["time"] = 10 | ||
| params["unit"] = "year" | ||
| params["keyType"] = "P256" | ||
| params["id"] = ca.ID | ||
| jsonData, err := json.Marshal(params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| res, err := req_helper.NewLocalClient("/api/v2/websites/ca/obtain", http.MethodPost, bytes.NewReader(jsonData)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| jsonBytes, err := json.Marshal(res) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| var ssl model.WebsiteSSL | ||
| if err := json.Unmarshal(jsonBytes, &ssl); err != nil { | ||
| return err | ||
| } | ||
| secret = ssl.Pem | ||
| key = ssl.PrivateKey | ||
| if err := settingRepo.Update("SSLID", strconv.Itoa(int(ssl.ID))); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if err := os.WriteFile(path.Join(secretDir, "server.crt.tmp"), []byte(secret), 0600); err != nil { | ||
|
|
@@ -325,7 +371,18 @@ func (u *SettingService) LoadFromCert() (*dto.SSLInfo, error) { | |
| keyFile, _ := os.ReadFile(path.Join(global.CONF.Base.InstallDir, "1panel/secret/server.key")) | ||
| data.Key = string(keyFile) | ||
| case "select": | ||
| // TODO select ssl from website | ||
| sslID, err := settingRepo.Get(repo.WithByKey("SSLID")) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| id, _ := strconv.Atoi(sslID.Value) | ||
| ssl, err := agentRepo.GetWebsiteSSL(repo.WithByID(uint(id))) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| data.Domain = ssl.PrimaryDomain | ||
| data.SSLID = uint(id) | ||
| data.Timeout = ssl.ExpireDate.Format(constant.DateTimeLayout) | ||
| } | ||
| return &data, 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. The most frequent difference between the two versions is the implementation of the TLS connection setup method used to set up HTTPS. In one version, it uses In other instances:
This code snippet doesn't contain enough detail to identify specific issues or optimizations. However, there are common areas where we can consider optimizing:
Note: Due to limitations in current context, it would be difficult to suggest precise solutions without deeper analysis and examples. For instance, we could see how other parts of the app need connections on secure network ports, which ones may cause traffic load increase due to the increased overhead caused by securing communications? |
||
|
|
||
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 reviewed the provided Go code for WebsiteSSL and CA structs from both previous versions of the codebase (with an earlier knowledge cut-off point of November 2013 but without mentioning it explicitly), however I am unable to compare them with the current version (as the information does not include details on when the changes were made). The code appears to be similar across these points, but there may have been specific additions, deletions or modifications that cannot be detected through such analysis. For accuracy and efficiency, you would need access to recent version logs or a more detailed review.
However, generally speaking, this code seems well-structured and follows good coding practices. In general, the key things one should look out for are:
Documentation: Is all documentation clear? Do you understand what each part is supposed to do based on the doc comment?
Errors/Constraints: Are there any limitations mentioned that could affect how we use the system? Any known bugs or unexpected behaviors?
Testing Code: Do they test various scenarios thoroughly? A single failure might indicate broader issues.
Reusability/Caching: Does some functionality become repetitive after many uses? Can it be reused elsewhere in the application?
Security: Does the implementation meet the security considerations like secure connection handling, encryption, authorization etc. Also, ensure no sensitive data has been left exposed.
Performance Optimization: If using database tables properly, can we improve performance by querying efficiently (e.g. limit execution of SQL operations)?
Interface Overload: Check if interface overloading leads anywhere harmful e,g: methods being marked as virtual and overridden in multiple places.
Given that specifics about these areas aren't available in your question's context, the best advice would likely involve looking at source files, testing cases, design decisions, comments within sources, and any other contextual clues which may aid identifying the most relevant issues or improvements.