-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
217 lines (182 loc) · 6.28 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"os"
"strconv"
"time"
"github.com/contentful-labs/kube-secret-syncer/pkg/k8snamespace"
"github.com/aws/aws-sdk-go/aws"
awsclient "github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/secretsmanager/secretsmanageriface"
secretsv1 "github.com/contentful-labs/kube-secret-syncer/api/v1"
"github.com/contentful-labs/kube-secret-syncer/controllers"
"github.com/contentful-labs/kube-secret-syncer/pkg/iam"
"github.com/contentful-labs/kube-secret-syncer/pkg/rolevalidator"
uzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = secretsv1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
type SMSVCFactory struct {
session *session.Session
arns iam.ARNGetter
SMSVC secretsmanageriface.SecretsManagerAPI // Main, default SM service - used when no IAM Role is specified in the secret
AssumedSMSVCs map[string]secretsmanageriface.SecretsManagerAPI // SM Service for each IAM Role
}
func getDurationFromEnv(envVar string, defaultDuration time.Duration) (time.Duration, error) {
value, ok := os.LookupEnv(envVar)
if ok {
if value == "" {
return defaultDuration, nil
}
valueInt, err := strconv.Atoi(value)
if err == nil {
interval := time.Second * time.Duration(valueInt)
return interval, nil
}
return 0 * time.Second, fmt.Errorf("%s invalid: %s", envVar, value)
}
return defaultDuration, nil
}
func (s SMSVCFactory) getSMSVC(iamRole string) (secretsmanageriface.SecretsManagerAPI, error) {
var smsvc secretsmanageriface.SecretsManagerAPI
var err error
// No iamRole specified, we use the default service
if iamRole == "" {
return s.SMSVC, nil
}
// ensure specified iamRole is an ARN
iamGetARN, err := s.arns.GetARN(iamRole)
if err != nil {
return nil, err
}
var ok bool
smsvc, ok = s.AssumedSMSVCs[iamGetARN]
if !ok {
creds := stscreds.NewCredentials(s.session, iamGetARN)
smsvc = secretsmanager.New(s.session, &aws.Config{Credentials: creds})
s.AssumedSMSVCs[iamGetARN] = smsvc
}
return smsvc, nil
}
func newSMSVCFactory(sess *session.Session, arnGetter iam.ARNGetter) *SMSVCFactory {
return &SMSVCFactory{
session: sess,
arns: arnGetter,
SMSVC: secretsmanager.New(sess),
AssumedSMSVCs: map[string]secretsmanageriface.SecretsManagerAPI{},
}
}
func realMain() int {
metricsAddr := os.Getenv("METRICS_LISTEN")
if metricsAddr == "" {
metricsAddr = ":8080"
}
// empty string default is what we want
defaultSearchRole := os.Getenv("POLL_DEFAULT_SEARCH_ROLE")
annotationName := os.Getenv("NS_ANNOTATION")
if annotationName == "" {
annotationName = "iam.amazonaws.com/allowed-roles"
}
syncPeriod, err := getDurationFromEnv("SYNC_INTERVAL_SEC", 120*time.Second)
if err != nil {
setupLog.Error(err, "failed parsing SYNC_INTERVAL_SEC: should be an integer")
return 1
}
pollInterval, err := getDurationFromEnv("POLL_INTERVAL_SEC", 300*time.Second)
if err != nil {
setupLog.Error(err, "failed parsing POLL_INTERVAL_SEC: should be an integer")
return 1
}
logCfg := zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
logger := zap.New(zap.Encoder(zapcore.NewJSONEncoder(logCfg)), zap.StacktraceLevel(uzap.PanicLevel))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: true,
LeaderElectionID: "5a48bfe8.contentful.com",
SyncPeriod: &syncPeriod,
Logger: logger,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
return 1
}
ctx := context.Background()
Retry5Cfg := request.WithRetryer(aws.NewConfig(), awsclient.DefaultRetryer{NumMaxRetries: 5})
arnClient := iam.NewARNClientWithCache(iam.GetARN)
smsvcfactory := newSMSVCFactory(session.Must(session.NewSession(Retry5Cfg)), arnClient)
nsCache, err := k8snamespace.NewWatcher(ctx)
if err != nil {
setupLog.Error(err, "unable to start namespace watcher")
return 1
}
roleValidator := rolevalidator.NewRoleValidator(arnClient, nsCache, annotationName)
r := &controllers.SyncedSecretReconciler{
Client: mgr.GetClient(),
Log: logger.WithName("controllers").WithName("SyncedSecret"),
Sess: session.New(Retry5Cfg),
GetSMClient: smsvcfactory.getSMSVC,
DefaultSearchRole: defaultSearchRole,
RoleValidator: roleValidator,
PollInterval: pollInterval,
}
if err = r.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "SyncedSecret")
return 1
}
defer r.Quit()
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err = mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
return 1
}
return 0
}
func main() {
// Call realMain so that defers work properly, since os.Exit won't
// call defers.
os.Exit(realMain())
}