/
server.go
191 lines (164 loc) · 6.13 KB
/
server.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
// Copyright 2020 Google LLC
//
// 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
//
// https://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 server implements a grpc server to receive mount events
package server
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"sync"
"github.com/GoogleCloudPlatform/secrets-store-csi-driver-provider-gcp/auth"
"github.com/GoogleCloudPlatform/secrets-store-csi-driver-provider-gcp/config"
"github.com/googleapis/gax-go/v2"
secretmanager "cloud.google.com/go/secretmanager/apiv1"
"cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/oauth"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/anypb"
"k8s.io/klog/v2"
"sigs.k8s.io/secrets-store-csi-driver/provider/v1alpha1"
)
type Server struct {
RuntimeVersion string
AuthClient *auth.Client
SecretClient *secretmanager.Client
}
var _ v1alpha1.CSIDriverProviderServer = &Server{}
// Mount implements provider csi-provider method
func (s *Server) Mount(ctx context.Context, req *v1alpha1.MountRequest) (*v1alpha1.MountResponse, error) {
p, err := strconv.ParseUint(req.GetPermission(), 10, 32)
if err != nil {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("Unable to parse permissions: %s", req.GetPermission()))
}
params := &config.MountParams{
Attributes: req.GetAttributes(),
KubeSecrets: req.GetSecrets(),
TargetPath: req.GetTargetPath(),
Permissions: os.FileMode(p),
}
cfg, err := config.Parse(params)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
ts, err := s.AuthClient.TokenSource(ctx, cfg)
if err != nil {
klog.ErrorS(err, "unable to obtain auth for mount", "pod", klog.ObjectRef{Namespace: cfg.PodInfo.Namespace, Name: cfg.PodInfo.Name})
return nil, status.Error(codes.PermissionDenied, fmt.Sprintf("unable to obtain auth for mount: %v", err))
}
// Build a grpc credentials.PerRPCCredentials using
// the grpc google.golang.org/grpc/credentials/oauth package, not to be
// confused with the oauth2.TokenSource that it wraps.
gts := oauth.TokenSource{TokenSource: ts}
// Fetch the secrets from the secretmanager API based on the
// SecretProviderClass configuration.
return handleMountEvent(ctx, s.SecretClient, gts, cfg)
}
// Version implements provider csi-provider method
func (s *Server) Version(ctx context.Context, req *v1alpha1.VersionRequest) (*v1alpha1.VersionResponse, error) {
return &v1alpha1.VersionResponse{
Version: "v1alpha1",
RuntimeName: "secrets-store-csi-driver-provider-gcp",
RuntimeVersion: s.RuntimeVersion,
}, nil
}
// handleMountEvent fetches the secrets from the secretmanager API and
// include them in the MountResponse based on the SecretProviderClass
// configuration.
func handleMountEvent(ctx context.Context, client *secretmanager.Client, creds credentials.PerRPCCredentials, cfg *config.MountConfig) (*v1alpha1.MountResponse, error) {
results := make([]*secretmanagerpb.AccessSecretVersionResponse, len(cfg.Secrets))
errs := make([]error, len(cfg.Secrets))
// need to build a per-rpc call option based of the tokensource
callAuth := gax.WithGRPCOptions(grpc.PerRPCCredentials(creds))
// In parallel fetch all secrets needed for the mount
wg := sync.WaitGroup{}
for i, secret := range cfg.Secrets {
wg.Add(1)
i, secret := i, secret
go func() {
defer wg.Done()
req := &secretmanagerpb.AccessSecretVersionRequest{
Name: secret.ResourceName,
}
resp, err := client.AccessSecretVersion(ctx, req, callAuth)
results[i] = resp
errs[i] = err
}()
}
wg.Wait()
// If any access failed, return a grpc status error that includes each
// individual status error in the Details field.
//
// If there are any failures then there will be no changes to the
// filesystem. Initial mount events will fail (preventing pod start) and
// the secrets-store-csi-driver will emit pod events on rotation failures.
// By erroring out on any failures we prevent partial rotations (i.e. the
// username file was updated to a new value but the corresponding password
// field was not).
if err := buildErr(errs); err != nil {
return nil, err
}
out := &v1alpha1.MountResponse{}
// Add secrets to response.
ovs := make([]*v1alpha1.ObjectVersion, len(cfg.Secrets))
for i, secret := range cfg.Secrets {
mode := int32(cfg.Permissions)
if secret.Mode != nil {
mode = *secret.Mode
}
result := results[i]
out.Files = append(out.Files, &v1alpha1.File{
Path: secret.PathString(),
Mode: mode,
Contents: result.Payload.Data,
})
klog.V(5).InfoS("added secret to response", "resource_name", secret.ResourceName, "file_name", secret.FileName, "pod", klog.ObjectRef{Namespace: cfg.PodInfo.Namespace, Name: cfg.PodInfo.Name})
ovs[i] = &v1alpha1.ObjectVersion{
Id: secret.ResourceName,
Version: result.GetName(),
}
}
out.ObjectVersion = ovs
return out, nil
}
// buildErr consolidates many errors into a single Status protobuf error message
// with each individual error included into the status Details any proto. The
// consolidated proto is converted to a general error.
func buildErr(errs []error) error {
msgs := make([]string, 0, len(errs))
hasErr := false
s := &spb.Status{
Code: int32(codes.Internal),
Details: make([]*anypb.Any, 0),
}
for i := range errs {
if errs[i] == nil {
continue
}
hasErr = true
msgs = append(msgs, errs[i].Error())
any, _ := anypb.New(status.Convert(errs[i]).Proto())
s.Details = append(s.Details, any)
}
if !hasErr {
return nil
}
s.Message = strings.Join(msgs, ",")
return status.FromProto(s).Err()
}