-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutator.go
144 lines (120 loc) · 4.05 KB
/
mutator.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
// Copyright © 2024 Bank-Vaults Maintainers
//
// 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 bao
import (
"context"
"crypto/x509"
"log/slog"
"net/http"
"emperror.dev/errors"
bao "github.com/bank-vaults/vault-sdk/vault"
baoapi "github.com/hashicorp/vault/api"
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type mutator struct {
client *bao.Client
config *Config
logger *slog.Logger
}
func (m *mutator) newClient(ctx context.Context, k8sClient kubernetes.Interface, k8sNamespace string) error {
clientConfig := baoapi.DefaultConfig()
if clientConfig.Error != nil {
return clientConfig.Error
}
clientConfig.Address = m.config.Addr
tlsConfig := baoapi.TLSConfig{Insecure: m.config.SkipVerify}
err := clientConfig.ConfigureTLS(&tlsConfig)
if err != nil {
return err
}
if m.config.TLSSecret != "" {
tlsSecret, err := k8sClient.CoreV1().Secrets(k8sNamespace).Get(
ctx,
m.config.TLSSecret,
metav1.GetOptions{},
)
if err != nil {
return errors.Wrap(err, "failed to read Bao TLS Secret")
}
clientTLSConfig := clientConfig.HttpClient.Transport.(*http.Transport).TLSClientConfig
pool := x509.NewCertPool()
ok := pool.AppendCertsFromPEM(tlsSecret.Data["ca.crt"])
if !ok {
return errors.Errorf("error loading Bao CA PEM from TLS Secret: %s", tlsSecret.Name)
}
clientTLSConfig.RootCAs = pool
}
if m.config.BaoServiceAccount != "" {
sa, err := k8sClient.CoreV1().ServiceAccounts(m.config.ObjectNamespace).Get(ctx, m.config.BaoServiceAccount, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "Failed to retrieve specified service account on namespace "+m.config.ObjectNamespace)
}
saToken := ""
if len(sa.Secrets) > 0 {
secret, err := k8sClient.CoreV1().Secrets(m.config.ObjectNamespace).Get(ctx, sa.Secrets[0].Name, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, "Failed to retrieve secret for service account "+sa.Secrets[0].Name+" in namespace "+m.config.ObjectNamespace)
}
saToken = string(secret.Data["token"])
}
if saToken == "" {
tokenTTL := int64(600) // min allowed duration is 10 mins
tokenRequest := &authenticationv1.TokenRequest{
Spec: authenticationv1.TokenRequestSpec{
Audiences: []string{"https://kubernetes.default.svc"},
ExpirationSeconds: &tokenTTL,
},
}
token, err := k8sClient.CoreV1().ServiceAccounts(m.config.ObjectNamespace).CreateToken(
ctx,
m.config.BaoServiceAccount,
tokenRequest,
metav1.CreateOptions{},
)
if err != nil {
return errors.Wrap(err, "Failed to create a token for the specified service account "+m.config.BaoServiceAccount+" on namespace "+m.config.ObjectNamespace)
}
saToken = token.Status.Token
}
baoClient, err := bao.NewClientFromConfig(
clientConfig,
bao.ClientRole(m.config.Role),
bao.ClientAuthPath(m.config.Path),
bao.NamespacedSecretAuthMethod,
bao.ClientLogger(&ClientLogger{Logger: m.logger}),
bao.ExistingSecret(saToken),
bao.VaultNamespace(m.config.BaoNamespace),
)
if err != nil {
return errors.Wrap(err, "failed to create Bao client")
}
m.client = baoClient
return nil
}
baoClient, err := bao.NewClientFromConfig(
clientConfig,
bao.ClientRole(m.config.Role),
bao.ClientAuthPath(m.config.Path),
bao.ClientAuthMethod(m.config.AuthMethod),
bao.ClientLogger(&ClientLogger{Logger: m.logger}),
bao.VaultNamespace(m.config.BaoNamespace),
)
if err != nil {
return errors.Wrap(err, "failed to create Bao client")
}
m.client = baoClient
return nil
}