| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package azure
- import (
- "bytes"
- "crypto/sha1"
- "crypto/x509"
- "encoding/pem"
- "fmt"
- "net/url"
- "strings"
- "time"
- "yunion.io/x/cloudmux/pkg/apis"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud"
- "yunion.io/x/pkg/errors"
- )
- type SLoadbalancerCert struct {
- multicloud.SResourceBase
- AzureTags
- region *SRegion
- Name string `json:"name"`
- Id string `json:"id"`
- Properties struct {
- PublicCertData string
- HttpListeners []struct {
- Id string
- }
- }
- }
- func (self *SLoadbalancerCert) GetId() string {
- return self.Id
- }
- func (self *SLoadbalancerCert) GetName() string {
- return self.Name
- }
- func (self *SLoadbalancerCert) GetGlobalId() string {
- return strings.ToLower(self.GetId())
- }
- func (self *SLoadbalancerCert) GetStatus() string {
- return apis.STATUS_AVAILABLE
- }
- func (self *SLoadbalancerCert) GetProjectId() string {
- return getResourceGroup(self.GetId())
- }
- func (self *SLoadbalancerCert) Delete() error {
- return errors.Wrap(cloudprovider.ErrNotImplemented, "Delete")
- }
- func (self *SLoadbalancerCert) ParsePublicKey() (*x509.Certificate, error) {
- block, _ := pem.Decode([]byte(self.GetPublickKey()))
- cert, err := x509.ParseCertificate(block.Bytes)
- if err != nil {
- return nil, errors.Wrap(err, "ParseCertificate")
- }
- return cert, nil
- }
- func (self *SLoadbalancerCert) GetCommonName() string {
- cert, err := self.ParsePublicKey()
- if err != nil {
- return ""
- }
- return cert.Issuer.CommonName
- }
- func (self *SLoadbalancerCert) GetSubjectAlternativeNames() string {
- _, err := self.ParsePublicKey()
- if err != nil {
- return ""
- }
- return ""
- }
- func (self *SLoadbalancerCert) GetFingerprint() string {
- publicKey := self.GetPublickKey()
- if len(publicKey) == 0 {
- return ""
- }
- _fp := sha1.Sum([]byte(publicKey))
- fp := fmt.Sprintf("sha1:% x", _fp)
- return strings.Replace(fp, " ", ":", -1)
- }
- func (self *SLoadbalancerCert) GetExpireTime() time.Time {
- cert, err := self.ParsePublicKey()
- if err != nil {
- return time.Time{}
- }
- return cert.NotAfter
- }
- func (self *SLoadbalancerCert) GetPublickKey() string {
- if len(self.Properties.PublicCertData) > 0 {
- var pk bytes.Buffer
- pk.WriteString("-----BEGIN CERTIFICATE-----\r\n")
- content := bytes.NewBufferString(self.Properties.PublicCertData)
- for {
- l := content.Next(64)
- if len(l) == 64 {
- pk.WriteString(fmt.Sprintf("%s\r\n", l))
- } else {
- pk.WriteString(fmt.Sprintf("%s\r\n", l))
- break
- }
- }
- pk.WriteString("-----END CERTIFICATE-----")
- return pk.String()
- }
- return ""
- }
- func (self *SLoadbalancerCert) GetPrivateKey() string {
- return ""
- }
- func (self *SRegion) GetLoadbalancerCertificates() ([]SLoadbalancerCert, error) {
- params := url.Values{}
- resp, err := self.list_resources("Microsoft.Network/applicationGateways", "2023-09-01", params)
- if err != nil {
- return nil, err
- }
- ret := []SLoadbalancer{}
- err = resp.Unmarshal(&ret, "value")
- if err != nil {
- return nil, err
- }
- result := []SLoadbalancerCert{}
- for i := range ret {
- if ret[i].Location != self.Name {
- continue
- }
- result = append(result, ret[i].Properties.SSLCertificates...)
- }
- return result, nil
- }
|