-
Notifications
You must be signed in to change notification settings - Fork 98
/
importer.go
110 lines (94 loc) · 2.81 KB
/
importer.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
// Copyright 2019 The Berglas Authors
//
// 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 auto
import (
"context"
"log"
"os"
"strconv"
"time"
"github.com/GoogleCloudPlatform/berglas/pkg/berglas"
"github.com/GoogleCloudPlatform/berglas/pkg/retry"
"github.com/pkg/errors"
"google.golang.org/api/googleapi"
)
var (
retryBase = 500 * time.Millisecond
retryTries = 5
// continueOnError controls whether Berglas should continue on error or panic.
// The default behavior is to panic.
continueOnError, _ = strconv.ParseBool(os.Getenv("BERGLAS_CONTINUE_ON_ERROR"))
)
func init() {
ctx := context.Background()
runtimeEnv, err := berglas.DetectRuntimeEnvironment()
if err != nil {
handleError(errors.Wrap(err, "failed to detect environment"))
return
}
envvarRefs, err := Resolve(ctx, runtimeEnv)
if err != nil {
handleError(errors.Wrap(err, "failed to resolve environment variables"))
return
}
if len(envvarRefs) == 0 {
log.Printf("[WARN] berglas auto was included, but no secrets were found in the environment")
return
}
client, err := berglas.New(ctx)
if err != nil {
handleError(errors.Wrap(err, "failed to initialize berglas client"))
return
}
for k := range envvarRefs {
if err := client.Replace(ctx, k); err != nil {
handleError(errors.Wrapf(err, "failed to set %s", k))
}
}
}
// Resolve resolves the environment variables. Importing the package calls
// Resolve. It's a separate method primarily for testing. Implementers should
// not call this method.
func Resolve(ctx context.Context, runtimeEnv berglas.RuntimeEnvironment) (map[string]string, error) {
var envvars map[string]string
var err error
if err := retry.RetryFib(ctx, retryBase, retryTries, func() error {
envvars, err = runtimeEnv.EnvVars(ctx)
if err != nil {
if terr, ok := errors.Cause(err).(*googleapi.Error); ok {
// Do not retry 400-level errors
if terr.Code >= 400 && terr.Code <= 499 {
return terr
}
}
return retry.RetryableError(err)
}
return nil
}); err != nil {
return nil, errors.Wrap(err, "failed to find environment variables")
}
envvarRefs := make(map[string]string)
for k, v := range envvars {
if berglas.IsReference(v) {
envvarRefs[k] = v
}
}
return envvarRefs, nil
}
func handleError(err error) {
log.Printf("%s\n", err)
if !continueOnError {
panic(err)
}
}