-
Notifications
You must be signed in to change notification settings - Fork 63
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
feat(ssl): Add API interfaces for managing SSL certificates #139
Open
eust-w
wants to merge
1
commit into
qiniu:master
Choose a base branch
from
eust-w:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -352,3 +352,224 @@ func postRequest(mac *auth.Credentials, path string, body interface{}) (resData | |
|
||
return | ||
} | ||
|
||
// CertListReq 获取ssl证书列表请求内容 | ||
type CertListReq struct { | ||
Marker string `json:"marker"` | ||
Limit int `json:"limit"` | ||
} | ||
|
||
// CertListResp 获取ssl证书列表响应内容 | ||
type CertListResp struct { | ||
Marker string `json:"marker"` | ||
Certs []struct { | ||
CertID string `json:"certid"` | ||
Name string `json:"name"` | ||
CommonName string `json:"common_name"` | ||
DNSNames []string `json:"dnsnames"` | ||
NotBefore int `json:"not_before"` | ||
NotAfter int `json:"not_after"` | ||
CreateTime int `json:"create_time"` | ||
} `json:"certs"` | ||
} | ||
|
||
// GetCertList 获取ssl证书列表 | ||
func (m *CdnManager) GetCertList(marker string, limit int) (certList CertListResp, err error) { | ||
reqParams := fmt.Sprintf("marker=%s&limit=%d", marker, limit) | ||
urlStr := fmt.Sprintf("%s/sslcert?%s", FusionHost, reqParams) | ||
req, reqErr := http.NewRequest("GET", urlStr, nil) | ||
if reqErr != nil { | ||
err = reqErr | ||
return | ||
} | ||
accessToken, signErr := m.mac.SignRequest(req) | ||
if signErr != nil { | ||
err = signErr | ||
return | ||
} | ||
req.Header.Add("Authorization", "QBox "+accessToken) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
resp, respErr := http.DefaultClient.Do(req) | ||
if respErr != nil { | ||
err = respErr | ||
return | ||
} | ||
defer resp.Body.Close() | ||
resData, ioErr := ioutil.ReadAll(resp.Body) | ||
if ioErr != nil { | ||
err = ioErr | ||
return | ||
} | ||
umErr := json.Unmarshal(resData, &certList) | ||
if umErr != nil { | ||
err = umErr | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
// CertDetailResp 获取单个ssl证书响应内容 | ||
type CertDetailResp struct { | ||
Name string `json:"name"` | ||
CommonName string `json:"common_name"` | ||
DNSNames []string `json:"dnsnames"` | ||
NotBefore int `json:"not_before"` | ||
NotAfter int `json:"not_after"` | ||
Pri string `json:"pri"` | ||
Ca string `json:"ca"` | ||
CreateTime int `json:"create_time"` | ||
} | ||
|
||
// RealCertDetailResp 当前的api返回与官方文档有差异 | ||
type RealCertDetailResp struct { | ||
CertID string `json:"certid"` | ||
Name string `json:"name"` | ||
UID int `json:"uid"` | ||
CommonName string `json:"common_name"` | ||
DNSNames []string `json:"dnsnames"` | ||
CreateTime int `json:"create_time"` | ||
NotBefore int `json:"not_before"` | ||
NotAfter int `json:"not_after"` | ||
OrderID string `json:"orderid"` | ||
ProductShortName string `json:"product_short_name"` | ||
ProductType string `json:"product_type"` | ||
CertType string `json:"cert_type"` | ||
Encrypt string `json:"encrypt"` | ||
EncryptParameter string `json:"encryptParameter"` | ||
Enable bool `json:"enable"` | ||
ChildOrderID string `json:"child_order_id"` | ||
State string `json:"state"` | ||
AutoRenew bool `json:"auto_renew"` | ||
Renewable bool `json:"renewable"` | ||
CA string `json:"ca"` | ||
} | ||
|
||
// GetCertDetail 获取单个ssl证书的详细信息 | ||
func (m *CdnManager) GetCertDetail(certID string) (certDetail CertDetailResp, err error) { | ||
urlStr := fmt.Sprintf("%s/sslcert/%s", FusionHost, certID) | ||
req, reqErr := http.NewRequest("GET", urlStr, nil) | ||
if reqErr != nil { | ||
err = reqErr | ||
return | ||
} | ||
accessToken, signErr := m.mac.SignRequest(req) | ||
if signErr != nil { | ||
err = signErr | ||
return | ||
} | ||
req.Header.Add("Authorization", "QBox "+accessToken) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
resp, respErr := http.DefaultClient.Do(req) | ||
if respErr != nil { | ||
err = respErr | ||
return | ||
} | ||
defer resp.Body.Close() | ||
resData, ioErr := ioutil.ReadAll(resp.Body) | ||
if ioErr != nil { | ||
err = ioErr | ||
return | ||
} | ||
var resJson = struct { | ||
Code int | ||
Error string | ||
Cert RealCertDetailResp | ||
}{} | ||
umErr := json.Unmarshal(resData, &resJson) | ||
certDetail.Ca = resJson.Cert.CA | ||
certDetail.CommonName = resJson.Cert.Name | ||
certDetail.DNSNames = resJson.Cert.DNSNames | ||
certDetail.Name = resJson.Cert.Name | ||
certDetail.NotAfter = resJson.Cert.NotAfter | ||
certDetail.NotBefore = resJson.Cert.NotBefore | ||
certDetail.CreateTime = resJson.Cert.CreateTime | ||
if umErr != nil { | ||
err = umErr | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
// UploadCertReq 上传ssl证书请求内容 | ||
type UploadCertReq struct { | ||
Name string `json:"name"` | ||
CommonName string `json:"common_name"` | ||
Pri string `json:"pri"` | ||
Ca string `json:"ca"` | ||
} | ||
|
||
// UploadCertResp 上传ssl证书响应内容 | ||
type UploadCertResp struct { | ||
CertID string `json:"certID"` | ||
} | ||
|
||
// UploadCert 上传ssl证书 | ||
func (m *CdnManager) UploadCert(name, commonName, pri, ca string) (resp UploadCertResp, err error) { | ||
reqBody := UploadCertReq{ | ||
Name: name, | ||
CommonName: commonName, | ||
Pri: pri, | ||
Ca: ca, | ||
} | ||
urlStr := fmt.Sprintf("%s/sslcert", FusionHost) | ||
reqData, _ := json.Marshal(reqBody) | ||
req, reqErr := http.NewRequest("POST", urlStr, bytes.NewReader(reqData)) | ||
if reqErr != nil { | ||
err = reqErr | ||
return | ||
} | ||
accessToken, signErr := m.mac.SignRequest(req) | ||
if signErr != nil { | ||
err = signErr | ||
return | ||
} | ||
req.Header.Add("Authorization", "QBox "+accessToken) | ||
req.Header.Add("Content-Type", "application/json") | ||
httpResp, respErr := http.DefaultClient.Do(req) | ||
if respErr != nil { | ||
err = respErr | ||
return | ||
} | ||
defer httpResp.Body.Close() | ||
resData, ioErr := ioutil.ReadAll(httpResp.Body) | ||
if ioErr != nil { | ||
err = ioErr | ||
return | ||
} | ||
umErr := json.Unmarshal(resData, &resp) | ||
if umErr != nil { | ||
err = umErr | ||
return | ||
} | ||
return | ||
} | ||
|
||
// DeleteCert 删除ssl证书 | ||
func (m *CdnManager) DeleteCert(certID string) (err error) { | ||
urlStr := fmt.Sprintf("%s/sslcert/%s", FusionHost, certID) | ||
req, reqErr := http.NewRequest("DELETE", urlStr, nil) | ||
if reqErr != nil { | ||
err = reqErr | ||
return | ||
} | ||
accessToken, signErr := m.mac.SignRequest(req) | ||
if signErr != nil { | ||
err = signErr | ||
return | ||
} | ||
req.Header.Add("Authorization", "QBox "+accessToken) | ||
req.Header.Add("Content-Type", "application/json") | ||
resp, respErr := http.DefaultClient.Do(req) | ||
if respErr != nil { | ||
err = respErr | ||
return | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
err = fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
return | ||
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. 另外,针对新增的接口需要补充单元测试 |
||
} | ||
return | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
每个接口都调用go标准包的接口,拿到结果后做unmarshal,建议封装一下,参考一下上面的postRequest