This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
secret.go
91 lines (80 loc) · 2.7 KB
/
secret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package k8sProvider
import (
"context"
"errors"
"fmt"
"time"
"github.com/camaeel/vault-k8s-helper/pkg/certificates"
log "github.com/sirupsen/logrus"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/applyconfigurations/core/v1"
applyMetaV1 "k8s.io/client-go/applyconfigurations/meta/v1"
"k8s.io/client-go/kubernetes"
)
var MINVALIDDAYS int = 30
func CheckSecretValidity(k8s *kubernetes.Clientset, ctx context.Context, name *string, namespace *string) (bool, error) {
minCertificateValid, err := time.ParseDuration(fmt.Sprintf("%dh", MINVALIDDAYS*24))
if err != nil {
panic(err)
}
secret, err := k8s.CoreV1().Secrets(*namespace).Get(ctx, *name, metav1.GetOptions{})
if err != nil {
if apiErrors.IsNotFound(err) {
log.Infof("Secret %s not found in namespace %s, so it will be created", *name, *namespace)
return false, nil
}
return false, err
}
log.Infof("Secret %s found in namespace %s", *name, *namespace)
if crt, ok := secret.Data["vault.crt"]; ok {
validBefore, err := certificates.GetValidNotAfter(crt)
if err != nil {
log.Warnf("Couldn't parse certificate from secret, will recreate it")
return false, nil
} else if time.Until(*validBefore) < minCertificateValid {
log.Infof("Certificate is valid until %s, will be recreated", *validBefore)
return false, nil
} else {
log.Infof("Certificate is valid until %s, won't be recreated", *validBefore)
return true, nil
}
}
log.Warnf("Secret found, but without certificate data. Secret will be recreated")
return false, nil
}
func CreateOrReplaceSecret(k8s *kubernetes.Clientset, ctx context.Context, name *string, namespace *string, data map[string][]byte) error {
kind := "Secret"
apiVersion := "v1"
secretData := v1.SecretApplyConfiguration{
Data: data,
// Immutable: &immutable,
// Type: &secretType,
ObjectMetaApplyConfiguration: &applyMetaV1.ObjectMetaApplyConfiguration{
Namespace: namespace,
Name: name,
},
TypeMetaApplyConfiguration: applyMetaV1.TypeMetaApplyConfiguration{
Kind: &kind,
APIVersion: &apiVersion,
},
}
// options := metav1.PatchOptions{
// // Force: &o.ForceConflicts,
// }
_, err := k8s.CoreV1().Secrets(*namespace).Apply(ctx, &secretData, metav1.ApplyOptions{FieldManager: FIELDMANAGER})
if err != nil {
panic(err)
}
return nil
}
func GetSecretContents(k8s *kubernetes.Clientset, ctx context.Context, name *string, namespace *string) (map[string][]byte, error) {
secret, err := k8s.CoreV1().Secrets(*namespace).Get(ctx, *name, metav1.GetOptions{})
if err != nil {
return nil, err
}
if secret.Data == nil {
return nil, errors.New("secret data not found")
}
return secret.Data, nil
}