-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (137 loc) · 4.01 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/nlopes/slack"
"github.com/rayanebel/kubeseal-backuper/pkg/config"
slackclient "github.com/rayanebel/kubeseal-backuper/pkg/notifiers/slack"
k8sutils "github.com/rayanebel/kubeseal-backuper/pkg/utils/kube"
s3utils "github.com/rayanebel/kubeseal-backuper/pkg/utils/s3"
slackutils "github.com/rayanebel/kubeseal-backuper/pkg/utils/slack"
"github.com/rayanebel/kubeseal-backuper/pkg/utils/kubeseal"
"github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
const (
kubesealSecretLabel = "sealedsecrets.bitnami.com/sealed-secrets-key"
)
var state *config.State
// run - will execute all steps to backup and clean sealed secret key.
func run() {
k8sutils.SetKubernetesclient(state)
processBackup(state)
k8sutils.CleanSecret(state)
msgTxt := fmt.Sprintf("*Kubeseal controller*: `%s` has generated a new encryption key."+
" This Key has been upload to S3."+
" The old encryption keys have all been *decommissioned*."+
" Please *re-encrypt* all your secret using the new key.", state.Config.KubesealControllerName)
switch state.Config.Notifier {
case "slack":
slackMsg := slackclient.SlackMessage{
Message: "",
ChannelID: state.Config.SlackChannelName,
Attachement: slack.Attachment{
Title: ":robot_face: Kubeseal Operator",
Color: "#00FF00",
Text: msgTxt,
}}
slackutils.InitSlack(state)
slackutils.NotifySlack(state, slackMsg)
default:
log.WithFields(log.Fields{
"notifier": state.Config.Notifier,
}).Error("Unsupported notifier backend")
os.Exit(1)
}
}
// processBackup - will process the backup of sealedsecret and export it into an external storage endpoint.
func processBackup(state *config.State) {
labelSelector := kubesealSecretLabel
opts := metav1.ListOptions{
LabelSelector: labelSelector,
}
secrets, err := state.K8s.ListSecrets(state.Config.KubesealControllerNamespace, opts)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"namespace": state.Config.KubesealControllerNamespace,
}).Error("Unable to list secrets")
os.Exit(1)
}
secret, err := kubeseal.FindSecretByPrefix(secrets, state.Config.KubesealKeyPrefix)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Unable to find kubeseal secret")
os.Exit(1)
}
k8sutils.SetGVKForObject(&secret)
var obj unstructured.Unstructured
objByte, err := json.Marshal(secret)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Serialization error")
os.Exit(1)
}
err = json.Unmarshal(objByte, &obj)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Deserialization error")
os.Exit(1)
}
k8sutils.CleanCommonKubernetesFields(&obj)
err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &secret)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Unable to convert unstructured object into v1.Secret")
os.Exit(1)
}
fileName, err := k8sutils.KubernetesJson2Yaml(&secret)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Unable to convert secret object into yaml")
os.Exit(1)
}
kubesealYamlfile, err := os.Open(fileName)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"filename": fileName,
}).Error("Unable to open temporary file")
os.Exit(1)
}
defer kubesealYamlfile.Close()
s3utils.StoreSecretKeyToS3(state, kubesealYamlfile)
}
// main program
func main() {
conf := &config.Config{}
err := envconfig.Process("", conf)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("Config error")
os.Exit(1)
}
config.NewState()
state = config.GetState()
state.Config = conf
run()
}
// init function
func init() {
log.SetFormatter(&log.TextFormatter{
DisableColors: false,
FullTimestamp: true,
})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
}