37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
common.go 2.11 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package pipelineexecution
import (
cryptorand "crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"math"
"math/big"
"time"
"github.com/rancher/rancher/pkg/controllers/user/nslabels"
"github.com/rancher/rancher/pkg/pipeline/utils"
v3 "github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/util/cert"
)
func getProjectID(ns *corev1.Namespace) string {
if ns.Annotations != nil {
return ns.Annotations[nslabels.ProjectIDFieldLabel]
}
return ""
}
func getSigningDuration(lister v3.PipelineSettingLister, projectID string) time.Duration {
defaultDuration, _ := time.ParseDuration(utils.SettingSigningDurationDefault)
setting, err := lister.Get(projectID, utils.SettingSigningDuration)
if err != nil {
logrus.Warn(err)
return defaultDuration
}
duration, err := time.ParseDuration(setting.Value)
if err != nil {
logrus.Warn(err)
return defaultDuration
}
return duration
}
// newSignedCert creates a signed certificate using the given CA certificate and key
func newSignedCertWithDuration(cfg cert.Config, duration time.Duration, key *rsa.PrivateKey, caCert *x509.Certificate, caKey *rsa.PrivateKey) (*x509.Certificate, error) {
serial, err := cryptorand.Int(cryptorand.Reader, new(big.Int).SetInt64(math.MaxInt64))
if err != nil {
return nil, err
}
if len(cfg.CommonName) == 0 {
return nil, errors.New("must specify a CommonName")
}
if len(cfg.Usages) == 0 {
return nil, errors.New("must specify at least one ExtKeyUsage")
}
certTmpl := x509.Certificate{
Subject: pkix.Name{
CommonName: cfg.CommonName,
Organization: cfg.Organization,
},
DNSNames: cfg.AltNames.DNSNames,
IPAddresses: cfg.AltNames.IPs,
SerialNumber: serial,
NotBefore: time.Now(),
NotAfter: time.Now().Add(duration), //.UTC(),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: cfg.Usages,
}
certDERBytes, err := x509.CreateCertificate(cryptorand.Reader, &certTmpl, caCert, key.Public(), caKey)
if err != nil {
return nil, err
}
return x509.ParseCertificate(certDERBytes)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.5-rc10

搜索帮助

0d507c66 1850385 C8b1a773 1850385