This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (97 loc) · 2.5 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
package main
import (
"os"
"sync-secrets/pkg/aws"
"sync-secrets/pkg/secret"
"sync-secrets/pkg/vault"
log "github.com/sirupsen/logrus"
)
const (
PrefixDest = "DEST_"
PrefixSource = "SOURCE_"
EnvLogLevel = "LOG_LEVEL"
EnvSyncEnv = "ENVIRONMENT"
EnvSystem = "SYSTEM"
SystemAws = "aws"
SystemVault = "vault"
)
var SyncEnv secret.Environment
func init() {
SetLogLevel()
SetEnvironment()
}
func main() {
secrets := GetSourceSecrets()
UpdateDestinationSecrets(secrets)
}
// SetEnvironment reads the sync environment from environment variable and sets SyncEnv as correct
// secret.Environment.
func SetEnvironment() {
if v := os.Getenv(EnvSyncEnv); v != "" {
if e := secret.GetEnvFromString(v); e != nil {
SyncEnv = *e
} else {
log.Fatalf("%s not accepted value for %s", e, EnvSyncEnv)
}
} else {
log.Fatalf("Required env variable %s not defined", EnvSyncEnv)
}
}
// SetLogLevel reads desired logging level from the LOG_LEVEL env variable and sets it. Possible
// options are debug, info, warn, error, fatal, and panic. Defaults to logrus's default.
func SetLogLevel() {
var envLogLevel string
if v := os.Getenv(EnvLogLevel); v != "" {
envLogLevel = v
}
switch envLogLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "error":
log.SetLevel(log.ErrorLevel)
case "fatal":
log.SetLevel(log.FatalLevel)
}
}
// GetSourceSecrets returns a Slice of secrets from the source system.
func GetSourceSecrets() []*secret.Secret {
var system string
prefix := PrefixSource
if v := os.Getenv(prefix + EnvSystem); v != "" {
system = v
} else {
log.Fatalf("Required env variable %s not defined", prefix+EnvSystem)
}
switch system {
case SystemAws:
a := aws.New(prefix)
return a.GetSecrets(&SyncEnv)
case SystemVault:
v := vault.New(prefix)
return v.GetSecrets(&SyncEnv)
default:
log.Fatalf("%s should be one of: %s, %s", prefix+EnvSystem, SystemAws, SystemVault)
return nil // Will not execute
}
}
// UpdateDestinationSecrets sets secrets into the destination system.
func UpdateDestinationSecrets(secrets []*secret.Secret) {
var system string
prefix := PrefixDest
if v := os.Getenv(prefix + EnvSystem); v != "" {
system = v
} else {
log.Fatalf("Required env variable %s not defined", prefix+EnvSystem)
}
switch system {
case SystemVault:
v := vault.New(prefix)
v.UpdateSecrets(secrets)
default:
log.Fatalf("%s should be one of: %s", prefix+EnvSystem, SystemVault)
}
}