-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgcp.go
164 lines (146 loc) · 4.65 KB
/
gcp.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
/*
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 gcp
import (
"context"
"encoding/base64"
"fmt"
"strings"
"github.com/go-logr/logr"
smmeta "github.com/itscontained/secret-manager/pkg/apis/meta/v1"
smv1alpha1 "github.com/itscontained/secret-manager/pkg/apis/secretmanager/v1alpha1"
"github.com/itscontained/secret-manager/pkg/internal/store"
"google.golang.org/api/option"
"google.golang.org/api/secretmanager/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)
var _ store.Client = &GCP{}
type GCP struct {
kubeClient ctrlclient.Client
store smv1alpha1.GenericStore
log logr.Logger
client *secretmanager.Service
}
func New(ctx context.Context, log logr.Logger, kubeClient ctrlclient.Client, store smv1alpha1.GenericStore) (store.Client, error) {
g := &GCP{
kubeClient: kubeClient,
store: store,
log: log,
}
err := g.newClient(ctx)
if err != nil {
log.Error(err, "could not create new gcp client")
return nil, err
}
return g, nil
}
func (g *GCP) GetSecret(_ context.Context, ref smv1alpha1.RemoteReference) ([]byte, error) {
version := "latest"
if ref.Version != nil {
version = *ref.Version
}
data, err := g.readSecret(ref.Name, version)
if err != nil {
return nil, err
}
return data[ref.Name], nil
}
func (g *GCP) GetSecretMap(_ context.Context, ref smv1alpha1.RemoteReference) (map[string][]byte, error) {
version := "latest"
if ref.Version != nil {
version = *ref.Version
}
return g.readSecret(ref.Name, version)
}
func (g *GCP) readSecret(id, version string) (map[string][]byte, error) {
projectID := g.store.GetSpec().GCP.ProjectID
name := id
if !strings.HasPrefix(id, "projects/") && projectID != nil {
name = fmt.Sprintf("projects/%s/secrets/%s/versions/%s", *projectID, id, version)
}
resp, err := g.client.Projects.Secrets.Versions.Access(name).Do()
if err != nil {
return nil, err
}
data, err := base64.URLEncoding.DecodeString(resp.Payload.Data)
if err != nil {
return nil, err
}
return map[string][]byte{id: data}, nil
}
func (g *GCP) newClient(ctx context.Context) error {
g.log.V(1).Info("creating new gcp api client")
var err error
var clientOption option.ClientOption
spec := g.store.GetSpec().GCP
if spec.AuthSecretRef == nil {
g.log.V(1).Info("no authentication defined. using environment variables")
g.client, err = secretmanager.NewService(ctx)
if err != nil {
return err
}
return nil
}
// TODO: Validating Webhook Candidate
if spec.AuthSecretRef.JSON != nil && spec.AuthSecretRef.FilePath != nil {
return fmt.Errorf("multiple authentication methods configured")
}
if spec.AuthSecretRef.FilePath != nil {
g.log.V(1).Info("file authentication defined. using %s", *spec.AuthSecretRef.FilePath)
clientOption = option.WithCredentialsFile(*spec.AuthSecretRef.FilePath)
}
scoped := true
if g.store.GetTypeMeta().Kind == smv1alpha1.ClusterSecretStoreKind {
g.log.V(1).Info("removing namespace scope restriction")
scoped = false
}
if spec.AuthSecretRef.JSON != nil {
g.log.V(1).Info("JSON authentication defined")
namespace := g.store.GetNamespace()
if !scoped {
if spec.AuthSecretRef.JSON.Namespace == nil {
return fmt.Errorf("authsecretref namespace required when cluster-scoped")
}
namespace = *spec.AuthSecretRef.JSON.Namespace
}
data, e := g.secretKeyRef(ctx, namespace, *spec.AuthSecretRef.JSON)
if e != nil {
return err
}
clientOption = option.WithCredentialsJSON([]byte(data))
}
g.client, err = secretmanager.NewService(ctx, clientOption)
if err != nil {
return err
}
return nil
}
func (g *GCP) secretKeyRef(ctx context.Context, namespace string, secretRef smmeta.SecretKeySelector) (string, error) {
g.log.V(1).Info("retrieving kubernetes secret", "name", secretRef.Name)
var secret corev1.Secret
ref := types.NamespacedName{
Namespace: namespace,
Name: secretRef.Name,
}
err := g.kubeClient.Get(ctx, ref, &secret)
if err != nil {
return "", err
}
keyBytes, ok := secret.Data[secretRef.Key]
if !ok {
return "", fmt.Errorf("no data for %q in secret '%s/%s'", secretRef.Key, secretRef.Name, namespace)
}
value := strings.TrimSpace(string(keyBytes))
return value, nil
}