2022-12-30 09:25:21 -05:00
|
|
|
package backend
|
2022-01-14 13:44:37 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-12-30 09:25:21 -05:00
|
|
|
"github.com/dgryski/dgoogauth"
|
2022-08-02 10:19:27 -04:00
|
|
|
"github.com/google/uuid"
|
2022-12-30 09:25:21 -05:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2022-01-14 13:44:37 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-08-02 10:19:27 -04:00
|
|
|
"strings"
|
2022-01-14 13:44:37 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
secretCA = "openvpn-pki-ca"
|
|
|
|
secretServer = "openvpn-pki-server"
|
2022-07-21 11:17:53 -04:00
|
|
|
secretClientTmpl = "openvpn-pki-%d"
|
2022-01-14 13:44:37 -05:00
|
|
|
secretCRL = "openvpn-pki-crl"
|
|
|
|
secretIndexTxt = "openvpn-pki-index-txt"
|
|
|
|
secretDHandTA = "openvpn-pki-dh-and-ta"
|
|
|
|
certFileName = "tls.crt"
|
|
|
|
privKeyFileName = "tls.key"
|
|
|
|
)
|
|
|
|
|
|
|
|
//<year><month><day><hour><minute><second>Z
|
|
|
|
const indexTxtDateFormat = "060102150405Z"
|
|
|
|
|
|
|
|
var namespace = "default"
|
|
|
|
|
|
|
|
type OpenVPNPKI struct {
|
|
|
|
CAPrivKeyRSA *rsa.PrivateKey
|
|
|
|
CAPrivKeyPEM *bytes.Buffer
|
|
|
|
CACert *x509.Certificate
|
|
|
|
CACertPEM *bytes.Buffer
|
|
|
|
ServerPrivKeyRSA *rsa.PrivateKey
|
|
|
|
ServerPrivKeyPEM *bytes.Buffer
|
|
|
|
ServerCert *x509.Certificate
|
|
|
|
ServerCertPEM *bytes.Buffer
|
|
|
|
ClientCerts []ClientCert
|
|
|
|
RevokedCerts []RevokedCert
|
|
|
|
KubeClient *kubernetes.Clientset
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientCert struct {
|
|
|
|
PrivKeyRSA *rsa.PrivateKey
|
|
|
|
PrivKeyPEM *bytes.Buffer
|
|
|
|
Cert *x509.Certificate
|
|
|
|
CertPEM *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
type RevokedCert struct {
|
|
|
|
RevokedTime time.Time `json:"revokedTime"`
|
|
|
|
CommonName string `json:"commonName"`
|
|
|
|
Cert *x509.Certificate `json:"cert"`
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) Run() (err error) {
|
|
|
|
if _, err := os.Stat(KubeNamespaceFilePath); err == nil {
|
|
|
|
file, err := ioutil.ReadFile(KubeNamespaceFilePath)
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
namespace = string(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.initKubeClient()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.initPKI()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.indexTxtUpdate()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.easyrsaGenCRL()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res, _ := openVPNPKI.secretCheckExists(secretDHandTA); !res {
|
|
|
|
err := openVPNPKI.secretGenTaKeyAndDHParam()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateFilesFromSecrets()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateCRLOnDisk()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateIndexTxtOnDisk()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateCcdOnDisk()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) initKubeClient() (err error) {
|
|
|
|
config, _ := rest.InClusterConfig()
|
|
|
|
openVPNPKI.KubeClient, err = kubernetes.NewForConfig(config)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) initPKI() (err error) {
|
|
|
|
if res, _ := openVPNPKI.secretCheckExists(secretCA); res {
|
|
|
|
cert, err := openVPNPKI.secretGetClientCert(secretCA)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
openVPNPKI.CAPrivKeyPEM = cert.PrivKeyPEM
|
|
|
|
openVPNPKI.CAPrivKeyRSA = cert.PrivKeyRSA
|
|
|
|
openVPNPKI.CACertPEM = cert.CertPEM
|
|
|
|
openVPNPKI.CACert = cert.Cert
|
|
|
|
} else {
|
2022-12-30 09:25:21 -05:00
|
|
|
openVPNPKI.CAPrivKeyPEM, err = GenPrivKey()
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-12-30 09:25:21 -05:00
|
|
|
openVPNPKI.CAPrivKeyRSA, err = DecodePrivKey(openVPNPKI.CAPrivKeyPEM.Bytes())
|
2022-01-14 13:44:37 -05:00
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
openVPNPKI.CACertPEM, _ = GenCA(openVPNPKI.CAPrivKeyRSA)
|
|
|
|
openVPNPKI.CACert, err = DecodeCert(openVPNPKI.CACertPEM.Bytes())
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secretMetaData := metav1.ObjectMeta{Name: secretCA}
|
|
|
|
|
|
|
|
secretData := map[string][]byte{
|
|
|
|
certFileName: openVPNPKI.CACertPEM.Bytes(),
|
|
|
|
privKeyFileName: openVPNPKI.CAPrivKeyPEM.Bytes(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.secretCreate(secretMetaData, secretData, v1.SecretTypeTLS)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if res, _ := openVPNPKI.secretCheckExists(secretServer); res {
|
|
|
|
cert, err := openVPNPKI.secretGetClientCert(secretServer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
openVPNPKI.ServerPrivKeyPEM = cert.PrivKeyPEM
|
|
|
|
openVPNPKI.ServerPrivKeyRSA = cert.PrivKeyRSA
|
|
|
|
openVPNPKI.ServerCertPEM = cert.CertPEM
|
|
|
|
openVPNPKI.ServerCert = cert.Cert
|
|
|
|
} else {
|
2022-12-30 09:25:21 -05:00
|
|
|
openVPNPKI.ServerPrivKeyPEM, err = GenPrivKey()
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
openVPNPKI.ServerPrivKeyRSA, err = DecodePrivKey(openVPNPKI.ServerPrivKeyPEM.Bytes())
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
openVPNPKI.ServerCertPEM, _ = GenServerCert(openVPNPKI.ServerPrivKeyRSA, openVPNPKI.CAPrivKeyRSA, openVPNPKI.CACert, "server")
|
|
|
|
openVPNPKI.ServerCert, err = DecodeCert(openVPNPKI.ServerCertPEM.Bytes())
|
2022-01-14 13:44:37 -05:00
|
|
|
|
|
|
|
secretMetaData := metav1.ObjectMeta{
|
|
|
|
Name: secretServer,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"index.txt": "",
|
2022-09-01 06:15:22 -04:00
|
|
|
"name": "server",
|
2022-01-14 13:44:37 -05:00
|
|
|
"type": "serverAuth",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
secretData := map[string][]byte{
|
|
|
|
certFileName: openVPNPKI.ServerCertPEM.Bytes(),
|
|
|
|
privKeyFileName: openVPNPKI.ServerPrivKeyPEM.Bytes(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.secretCreate(secretMetaData, secretData, v1.SecretTypeTLS)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) indexTxtUpdate() (err error) {
|
|
|
|
secrets, err := openVPNPKI.secretsGetByLabels("index.txt=")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var indexTxt string
|
|
|
|
for _, secret := range secrets.Items {
|
|
|
|
certPEM := bytes.NewBuffer(secret.Data[certFileName])
|
|
|
|
log.Trace("indexTxtUpdate:" + secret.Name)
|
2022-12-30 09:25:21 -05:00
|
|
|
cert, err := DecodeCert(certPEM.Bytes())
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace(cert.Subject.CommonName)
|
|
|
|
|
|
|
|
if secret.Annotations["revokedAt"] == "" {
|
2022-09-01 06:15:22 -04:00
|
|
|
indexTxt += fmt.Sprintf("%s\t%s\t\t%s\t%s\t%s\n", "V", cert.NotAfter.Format(indexTxtDateFormat), fmt.Sprintf("%d", cert.SerialNumber), "unknown", "/CN="+secret.Labels["name"])
|
2022-01-14 13:44:37 -05:00
|
|
|
} else if cert.NotAfter.Before(time.Now()) {
|
2022-09-01 06:15:22 -04:00
|
|
|
indexTxt += fmt.Sprintf("%s\t%s\t\t%s\t%s\t%s\n", "E", cert.NotAfter.Format(indexTxtDateFormat), fmt.Sprintf("%d", cert.SerialNumber), "unknown", "/CN="+secret.Labels["name"])
|
2022-01-14 13:44:37 -05:00
|
|
|
} else {
|
2022-09-01 06:15:22 -04:00
|
|
|
indexTxt += fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s\n", "R", cert.NotAfter.Format(indexTxtDateFormat), secret.Annotations["revokedAt"], fmt.Sprintf("%d", cert.SerialNumber), "unknown", "/CN="+secret.Labels["name"])
|
2022-01-14 13:44:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
secretMetaData := metav1.ObjectMeta{Name: secretIndexTxt}
|
|
|
|
|
|
|
|
secretData := map[string][]byte{"index.txt": []byte(indexTxt)}
|
|
|
|
|
|
|
|
if res, _ := openVPNPKI.secretCheckExists(secretIndexTxt); !res {
|
|
|
|
err = openVPNPKI.secretCreate(secretMetaData, secretData, v1.SecretTypeOpaque)
|
|
|
|
} else {
|
|
|
|
err = openVPNPKI.secretUpdate(secretMetaData, secretData, v1.SecretTypeOpaque)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) updateIndexTxtOnDisk() (err error) {
|
|
|
|
secret, err := openVPNPKI.secretGetByName(secretIndexTxt)
|
|
|
|
indexTxt := secret.Data["index.txt"]
|
2022-12-30 09:25:21 -05:00
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/pki/index.txt", *EasyrsaDirPath), indexTxt, 0600)
|
2022-01-14 13:44:37 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) easyrsaGenCRL() (err error) {
|
|
|
|
err = openVPNPKI.indexTxtUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secrets, err := openVPNPKI.secretsGetByLabels("index.txt=,type=clientAuth")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var revoked []*RevokedCert
|
|
|
|
|
|
|
|
for _, secret := range secrets.Items {
|
|
|
|
if secret.Annotations["revokedAt"] != "" {
|
|
|
|
revokedAt, err := time.Parse(indexTxtDateFormat, secret.Annotations["revokedAt"])
|
|
|
|
if err != nil {
|
|
|
|
log.Warning(err)
|
|
|
|
}
|
2022-12-30 09:25:21 -05:00
|
|
|
cert, err := DecodeCert(secret.Data[certFileName])
|
2022-01-14 13:44:37 -05:00
|
|
|
revoked = append(revoked, &RevokedCert{RevokedTime: revokedAt, Cert: cert})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
crl, err := GenCRL(revoked, openVPNPKI.CACert, openVPNPKI.CAPrivKeyRSA)
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secretMetaData := metav1.ObjectMeta{Name: secretCRL}
|
|
|
|
|
|
|
|
secretData := map[string][]byte{
|
|
|
|
"crl.pem": crl.Bytes(),
|
|
|
|
}
|
|
|
|
|
|
|
|
//err = openVPNPKI.secretCreate(secretMetaData, secretData)
|
|
|
|
|
|
|
|
if res, _ := openVPNPKI.secretCheckExists(secretCRL); !res {
|
|
|
|
err = openVPNPKI.secretCreate(secretMetaData, secretData, v1.SecretTypeOpaque)
|
|
|
|
} else {
|
|
|
|
err = openVPNPKI.secretUpdate(secretMetaData, secretData, v1.SecretTypeOpaque)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) EasyrsaBuildClient(commonName string) (err error) {
|
2022-01-14 13:44:37 -05:00
|
|
|
// check certificate exists
|
|
|
|
_, err = openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err == nil {
|
|
|
|
return errors.New(fmt.Sprintf("certificate for user (%s) already exists", commonName))
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
clientPrivKeyPEM, err := GenPrivKey()
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
clientPrivKeyRSA, err := DecodePrivKey(clientPrivKeyPEM.Bytes())
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
clientCertPEM, _ := GenClientCert(clientPrivKeyRSA, openVPNPKI.CAPrivKeyRSA, openVPNPKI.CACert, commonName)
|
|
|
|
clientCert, err := DecodeCert(clientCertPEM.Bytes())
|
2022-01-14 13:44:37 -05:00
|
|
|
|
|
|
|
secretMetaData := metav1.ObjectMeta{
|
|
|
|
Name: fmt.Sprintf(secretClientTmpl, clientCert.SerialNumber),
|
|
|
|
Labels: map[string]string{
|
|
|
|
"index.txt": "",
|
|
|
|
"type": "clientAuth",
|
|
|
|
"name": commonName,
|
|
|
|
"app.kubernetes.io/managed-by": "ovpn-admin",
|
|
|
|
},
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"commonName": commonName,
|
|
|
|
"notBefore": clientCert.NotBefore.Format(indexTxtDateFormat),
|
|
|
|
"notAfter": clientCert.NotAfter.Format(indexTxtDateFormat),
|
|
|
|
"revokedAt": "",
|
2022-07-21 11:17:53 -04:00
|
|
|
"serialNumber": fmt.Sprintf("%d", clientCert.SerialNumber),
|
2022-01-14 13:44:37 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
secretData := map[string][]byte{
|
|
|
|
certFileName: clientCertPEM.Bytes(),
|
|
|
|
privKeyFileName: clientPrivKeyPEM.Bytes(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.secretCreate(secretMetaData, secretData, v1.SecretTypeTLS)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.indexTxtUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateIndexTxtOnDisk()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) easyrsaGetCACert() string {
|
|
|
|
return openVPNPKI.CACertPEM.String()
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) EasyrsaGetClientCert(commonName string) (cert, key string) {
|
2022-01-14 13:44:37 -05:00
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert = string(secret.Data[certFileName])
|
|
|
|
key = string(secret.Data[privKeyFileName])
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) EasyrsaRevoke(commonName string) (err error) {
|
2022-01-14 13:44:37 -05:00
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if secret.Annotations["revokedAt"] != "" {
|
|
|
|
log.Warnf("user (%s) already revoked", commonName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secret.Annotations["revokedAt"] = time.Now().Format(indexTxtDateFormat)
|
|
|
|
|
|
|
|
_, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.indexTxtUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateIndexTxtOnDisk()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.easyrsaGenCRL()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateCRLOnDisk()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) EasyrsaUnrevoke(commonName string) (err error) {
|
2022-01-14 13:44:37 -05:00
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret.Annotations["revokedAt"] = ""
|
|
|
|
|
|
|
|
_, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.indexTxtUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateIndexTxtOnDisk()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.easyrsaGenCRL()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateCRLOnDisk()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) EasyrsaRotate(commonName string) (err error) {
|
2022-07-21 11:17:53 -04:00
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2022-08-02 10:19:27 -04:00
|
|
|
uniqHash := strings.Replace(uuid.New().String(), "-", "", -1)
|
|
|
|
secret.Annotations["commonName"] = "REVOKED-" + commonName + "-" + uniqHash
|
2022-07-21 11:17:53 -04:00
|
|
|
secret.Labels["name"] = "REVOKED" + commonName
|
|
|
|
secret.Labels["revokedForever"] = "true"
|
|
|
|
|
|
|
|
_, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
err = openVPNPKI.EasyrsaBuildClient(commonName)
|
2022-07-21 11:17:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.indexTxtUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateIndexTxtOnDisk()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.easyrsaGenCRL()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateCRLOnDisk()
|
|
|
|
return
|
|
|
|
}
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) EasyrsaDelete(commonName string) (err error) {
|
2022-07-21 11:17:53 -04:00
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2022-08-02 10:19:27 -04:00
|
|
|
uniqHash := strings.Replace(uuid.New().String(), "-", "", -1)
|
|
|
|
secret.Annotations["commonName"] = "REVOKED-" + commonName + "-" + uniqHash
|
|
|
|
secret.Labels["name"] = "REVOKED-" + commonName + "-" + uniqHash
|
2022-07-21 11:17:53 -04:00
|
|
|
secret.Labels["revokedForever"] = "true"
|
|
|
|
|
|
|
|
_, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.indexTxtUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateIndexTxtOnDisk()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.easyrsaGenCRL()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateCRLOnDisk()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-14 13:44:37 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) secretGetClientCert(name string) (cert ClientCert, err error) {
|
|
|
|
secret, err := openVPNPKI.secretGetByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cert.CertPEM = bytes.NewBuffer(secret.Data[certFileName])
|
2022-12-30 09:25:21 -05:00
|
|
|
cert.Cert, err = DecodeCert(cert.CertPEM.Bytes())
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cert.PrivKeyPEM = bytes.NewBuffer(secret.Data[privKeyFileName])
|
2022-12-30 09:25:21 -05:00
|
|
|
cert.PrivKeyRSA, err = DecodePrivKey(cert.PrivKeyPEM.Bytes())
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) updateFilesFromSecrets() (err error) {
|
|
|
|
ca, err := openVPNPKI.secretGetClientCert(secretCA)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := openVPNPKI.secretGetClientCert(secretServer)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := openVPNPKI.secretGetByName(secretDHandTA)
|
|
|
|
takey := secret.Data["ta.key"]
|
|
|
|
dhparam := secret.Data["dh.pem"]
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s/pki/issued", *EasyrsaDirPath)); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(fmt.Sprintf("%s/pki/issued", *EasyrsaDirPath), 0755)
|
2022-01-14 13:44:37 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if _, err := os.Stat(fmt.Sprintf("%s/pki/private", *EasyrsaDirPath)); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(fmt.Sprintf("%s/pki/private", *EasyrsaDirPath), 0755)
|
2022-01-14 13:44:37 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/pki/ca.crt", *EasyrsaDirPath), ca.CertPEM.Bytes(), 0600)
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/pki/issued/server.crt", *EasyrsaDirPath), server.CertPEM.Bytes(), 0600)
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/pki/private/server.key", *EasyrsaDirPath), server.PrivKeyPEM.Bytes(), 0600)
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/pki/ta.key", *EasyrsaDirPath), takey, 0600)
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/pki/dh.pem", *EasyrsaDirPath), dhparam, 0600)
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateCRLOnDisk()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) updateCRLOnDisk() (err error) {
|
|
|
|
secret, err := openVPNPKI.secretGetByName(secretCRL)
|
|
|
|
crl := secret.Data["crl.pem"]
|
2022-12-30 09:25:21 -05:00
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/pki/crl.pem", *EasyrsaDirPath), crl, 0644)
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error write crl.pem:%s", err.Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) secretGenTaKeyAndDHParam() (err error) {
|
|
|
|
taKeyPath := "/tmp/ta.key"
|
|
|
|
cmd := exec.Command("bash", "-c", fmt.Sprintf("/usr/sbin/openvpn --genkey --secret %s", taKeyPath))
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
|
|
|
log.Info(fmt.Sprintf("/usr/sbin/openvpn --genkey --secret %s: %s", taKeyPath, string(stdout)))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
taKey, err := ioutil.ReadFile(taKeyPath)
|
|
|
|
|
|
|
|
dhparamPath := "/tmp/dh.pem"
|
|
|
|
cmd = exec.Command("bash", "-c", fmt.Sprintf("openssl dhparam -out %s 2048", dhparamPath))
|
|
|
|
_, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dhparam, err := ioutil.ReadFile(dhparamPath)
|
|
|
|
|
|
|
|
secretMetaData := metav1.ObjectMeta{Name: secretDHandTA}
|
|
|
|
|
|
|
|
secretData := map[string][]byte{
|
|
|
|
"ta.key": taKey,
|
|
|
|
"dh.pem": dhparam,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.secretCreate(secretMetaData, secretData, v1.SecretTypeOpaque)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ccd
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) SecretGetCcd(commonName string) (ccd string) {
|
2022-01-14 13:44:37 -05:00
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
for k := range secret.Data {
|
2022-01-14 13:44:37 -05:00
|
|
|
if k == "ccd" {
|
|
|
|
ccd = string(secret.Data["ccd"])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) SecretUpdateCcd(commonName string, ccd []byte) {
|
2022-01-14 13:44:37 -05:00
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
secret.Data["ccd"] = ccd
|
|
|
|
|
|
|
|
err = openVPNPKI.secretUpdate(secret.ObjectMeta, secret.Data, v1.SecretTypeTLS)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("secret (%s) update error: %s", secret.Name, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = openVPNPKI.updateCcdOnDisk()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
func (openVPNPKI *OpenVPNPKI) updateCcdOnDisk() error {
|
2022-01-14 13:44:37 -05:00
|
|
|
secrets, err := openVPNPKI.secretsGetByLabels("index.txt=,type=clientAuth")
|
|
|
|
if err != nil {
|
2022-12-30 09:25:21 -05:00
|
|
|
return err
|
2022-01-14 13:44:37 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
if _, err := os.Stat(*CcdDir); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(*CcdDir, 0755)
|
2022-01-14 13:44:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, secret := range secrets.Items {
|
|
|
|
ccd := secret.Data["ccd"]
|
|
|
|
if len(ccd) > 0 {
|
2022-12-30 09:25:21 -05:00
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/%s", *CcdDir, secret.Labels["name"]), ccd, 0644)
|
2022-01-14 13:44:37 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 09:25:21 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// auth
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) authByTOTP(commonName, token string) (authOK bool, err error) {
|
|
|
|
secret, err := openVPNPKI.secondFactorSecret(commonName)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
otpConfig := &dgoogauth.OTPConfig{
|
|
|
|
Secret: strings.TrimSpace(secret),
|
|
|
|
WindowSize: 3,
|
|
|
|
HotpCounter: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, authErr := otpConfig.Authenticate(strings.TrimSpace(token))
|
|
|
|
|
|
|
|
if authErr != nil {
|
|
|
|
return false, authErr
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
return true, nil
|
|
|
|
} else {
|
|
|
|
return false, tokenMismatchedError
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) authByPassword(commonName, password string) (authOK bool, err error) {
|
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, _ := range secret.Data {
|
|
|
|
if k == "passwordHash" {
|
|
|
|
err = bcrypt.CompareHashAndPassword(secret.Data["passwordHash"], []byte(password))
|
|
|
|
if err != nil {
|
|
|
|
return false, passwordMismatchedError
|
|
|
|
} else {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("can`t get user password")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) updatePasswordSecret(commonName string, newPassword []byte) (err error) {
|
|
|
|
|
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hash, _ := bcrypt.GenerateFromPassword(newPassword, bcrypt.MinCost)
|
|
|
|
secret.Data["passwordHash"] = hash
|
|
|
|
|
|
|
|
err = openVPNPKI.secretUpdate(secret.ObjectMeta, secret.Data, v1.SecretTypeTLS)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("secret (%s) update error: %s", secret.Name, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) secondFactorSecret(commonName string) (secondFactorSecret string, err error) {
|
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, _ := range secret.Data {
|
|
|
|
if k == "secondFactorSecret" {
|
|
|
|
secondFactorSecret = string(secret.Data["secondFactorSecret"])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) updateSecondFactorSecret(commonName string, secondFactorSecret []byte) (err error) {
|
|
|
|
|
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
secret.Data["secondFactorSecret"] = secondFactorSecret
|
|
|
|
|
|
|
|
err = openVPNPKI.secretUpdate(secret.ObjectMeta, secret.Data, v1.SecretTypeTLS)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("secret (%s) update error: %s", secret.Name, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) SecondFactorEnabled(commonName string) (enabled bool, err error) {
|
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
if _, ok := secret.Labels["secondFactorEnabled"]; ok {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) addSecondFactorEnabledLabel(commonName string) (err error) {
|
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
secret.Labels["secondFactorEnabled"] = ""
|
|
|
|
|
|
|
|
_, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) deleteSecondFactorEnabledLabel(commonName string) (err error) {
|
|
|
|
secret, err := openVPNPKI.secretGetByLabels("name=" + commonName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
if _, ok := secret.Labels["secondFactorEnabled"]; ok {
|
|
|
|
delete(secret.Labels, "secondFactorEnabled")
|
|
|
|
_, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-01-14 13:44:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) secretCreate(objectMeta metav1.ObjectMeta, data map[string][]byte, secretType v1.SecretType) (err error) {
|
|
|
|
if objectMeta.Name == "nil" {
|
|
|
|
err = errors.New("secret name not defined")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secret := &v1.Secret{
|
|
|
|
TypeMeta: metav1.TypeMeta{},
|
|
|
|
ObjectMeta: objectMeta,
|
|
|
|
Data: data,
|
|
|
|
Type: secretType,
|
|
|
|
}
|
|
|
|
_, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Create(context.TODO(), secret, metav1.CreateOptions{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) secretUpdate(objectMeta metav1.ObjectMeta, data map[string][]byte, secretType v1.SecretType) (err error) {
|
|
|
|
secret := &v1.Secret{
|
|
|
|
TypeMeta: metav1.TypeMeta{},
|
|
|
|
ObjectMeta: objectMeta,
|
|
|
|
Data: data,
|
|
|
|
Type: secretType,
|
|
|
|
}
|
|
|
|
_, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) secretGetByName(name string) (secret *v1.Secret, err error) {
|
|
|
|
secret, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Get(context.TODO(), name, metav1.GetOptions{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) secretsGetByLabels(labels string) (secrets *v1.SecretList, err error) {
|
|
|
|
secrets, err = openVPNPKI.KubeClient.CoreV1().Secrets(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labels})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(secrets.Items) == 0 {
|
|
|
|
log.Debugf("secrets with labels %s not found", labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) secretGetByLabels(labels string) (secret *v1.Secret, err error) {
|
|
|
|
secrets, err := openVPNPKI.secretsGetByLabels(labels)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(secrets.Items) > 1 {
|
|
|
|
err = errors.New(fmt.Sprintf("found more than one secret with labels %s", labels))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(secrets.Items) == 0 {
|
|
|
|
err = errors.New(fmt.Sprintf("secret not found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secret = &secrets.Items[0]
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (openVPNPKI *OpenVPNPKI) secretCheckExists(name string) (bool, string) {
|
|
|
|
secret, err := openVPNPKI.KubeClient.CoreV1().Secrets(namespace).Get(context.TODO(), name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
log.Debug(err)
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
return true, secret.ResourceVersion
|
|
|
|
}
|