37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
setup.go 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
package tls
import (
"bytes"
"crypto/x509"
"encoding/pem"
"github.com/rancher/types/apis/management.cattle.io/v3"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/cert"
)
type Storage interface {
Create(*v3.ListenConfig) (*v3.ListenConfig, error)
Get(name string, opts metav1.GetOptions) (*v3.ListenConfig, error)
Update(*v3.ListenConfig) (*v3.ListenConfig, error)
}
func SetupListenConfig(storage Storage, noCACerts bool, lc *v3.ListenConfig) error {
userCACerts := lc.CACerts
existing, err := storage.Get(lc.Name, metav1.GetOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
}
if apierrors.IsNotFound(err) {
existing = nil
}
if existing != nil {
if lc.Cert == "" {
lc.Cert = existing.Cert
lc.CACerts = existing.CACerts
lc.Key = existing.Key
lc.CAKey = existing.CAKey
lc.CACert = existing.CACert
lc.KnownIPs = existing.KnownIPs
lc.GeneratedCerts = existing.GeneratedCerts
}
}
if (lc.Key == "" || lc.Cert == "") && lc.CACert == "" && lc.Mode != "acme" {
caKey, err := cert.NewPrivateKey()
if err != nil {
return err
}
caCert, err := cert.NewSelfSignedCACert(cert.Config{
CommonName: "cattle-ca",
Organization: []string{"the-ranch"},
}, caKey)
if err != nil {
return err
}
caCertBuffer := bytes.Buffer{}
if err := pem.Encode(&caCertBuffer, &pem.Block{
Type: cert.CertificateBlockType,
Bytes: caCert.Raw,
}); err != nil {
return err
}
caKeyBuffer := bytes.Buffer{}
if err := pem.Encode(&caKeyBuffer, &pem.Block{
Type: cert.RSAPrivateKeyBlockType,
Bytes: x509.MarshalPKCS1PrivateKey(caKey),
}); err != nil {
return err
}
lc.CACert = string(caCertBuffer.Bytes())
lc.CACerts = lc.CACert
lc.CAKey = string(caKeyBuffer.Bytes())
}
if noCACerts || lc.Mode == "acme" {
lc.CACerts = ""
} else if userCACerts != "" {
lc.CACerts = userCACerts
}
if existing == nil {
_, err := storage.Create(lc)
return err
}
lc.ResourceVersion = existing.ResourceVersion
_, err = storage.Update(lc)
return err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.2-rc15

搜索帮助

0d507c66 1850385 C8b1a773 1850385