-
Notifications
You must be signed in to change notification settings - Fork 107
/
trivy_provider_registry_secrets.go
191 lines (163 loc) · 6.17 KB
/
trivy_provider_registry_secrets.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 2023 Flant JSC
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 hooks
import (
"context"
"errors"
"fmt"
"time"
"github.com/clarketm/json"
"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
"github.com/flant/shell-operator/pkg/kube_events_manager/types"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/tidwall/gjson"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/deckhouse/deckhouse/go_lib/dependency"
"github.com/deckhouse/deckhouse/go_lib/dependency/k8s"
)
var _ = sdk.RegisterFunc(&go_hook.HookConfig{
Queue: "/modules/admission-policy-engine/trivy_provider_secrets",
OnBeforeHelm: &go_hook.OrderedConfig{
Order: 30,
},
Kubernetes: []go_hook.KubernetesConfig{
{
Name: "trivy_provider_secrets",
ApiVersion: "v1",
Kind: "Secret",
NamespaceSelector: &types.NamespaceSelector{
NameSelector: &types.NameSelector{
MatchNames: []string{"d8-admission-policy-engine"},
},
},
NameSelector: &types.NameSelector{
MatchNames: []string{"deckhouse-registry"},
},
FilterFunc: fileterTrivyProviderSecrets,
},
},
}, dependency.WithExternalDependencies(handleTrivyProviderSecrets))
type dockerConfig struct {
Auths map[string]authn.AuthConfig `json:"auths"`
}
func fileterTrivyProviderSecrets(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
var secret corev1.Secret
err := sdk.FromUnstructured(obj, &secret)
if err != nil {
return nil, err
}
config, err := registrySecretToAuthnConfig(&secret)
if err != nil {
if errors.Is(err, ErrSecretWithNoData) || errors.Is(err, ErrNotDockerCfgJSONType) || errors.Is(err, ErrNoDockerConfigJSONKey) {
return nil, nil
}
return nil, err
}
return config.Auths, nil
}
var (
ErrSecretWithNoData = errors.New("secret has nil data")
ErrNotDockerCfgJSONType = fmt.Errorf("secret is not '%s' type", corev1.SecretTypeDockerConfigJson)
ErrNoDockerConfigJSONKey = fmt.Errorf("secret doesn't have '%s' key", corev1.DockerConfigJsonKey)
)
func registrySecretToAuthnConfig(secret *corev1.Secret) (*dockerConfig, error) {
if secret.Type != corev1.SecretTypeDockerConfigJson {
return nil, fmt.Errorf("%w: name=%s, namespace=%s", ErrNotDockerCfgJSONType, secret.GetName(), secret.GetNamespace())
}
if secret.Data == nil {
return nil, fmt.Errorf("%w: name=%s, namespace=%s", ErrSecretWithNoData, secret.GetName(), secret.GetNamespace())
}
rawCreds, ok := secret.Data[corev1.DockerConfigJsonKey]
if !ok {
return nil, fmt.Errorf("%w: name=%s, namespace=%s", ErrNoDockerConfigJSONKey, secret.GetName(), secret.GetNamespace())
}
config := new(dockerConfig)
if err := json.Unmarshal(rawCreds, config); err != nil {
return nil, fmt.Errorf("cannot decode docker config JSON: %v", err)
}
return config, nil
}
func handleTrivyProviderSecrets(input *go_hook.HookInput, dc dependency.Container) error {
if !input.Values.Get("admissionPolicyEngine.denyVulnerableImages.enabled").Bool() {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
defer cancel()
resultCfg := dockerConfig{Auths: make(map[string]authn.AuthConfig)}
for _, authSnap := range input.Snapshots["trivy_provider_secrets"] {
err := convertSnapToAuthnConfig(authSnap, &resultCfg)
if err != nil && !errors.Is(err, ErrNilSnapshot) {
return err
}
}
k8sClient, err := dc.GetK8sClient()
if err != nil {
return fmt.Errorf("can't get k8s client for retrieving registry secrets: %w", err)
}
registrySecretsValues := input.Values.Get("admissionPolicyEngine.denyVulnerableImages.registrySecrets").Array()
for _, registrySecretValue := range registrySecretsValues {
registrySecret, err := registrySecretValueToAuthnConfig(ctx, registrySecretValue, k8sClient)
if err != nil {
return fmt.Errorf("can't get registry secret from module values: %w", err)
}
err = convertSnapToAuthnConfig(registrySecret.Auths, &resultCfg)
if err != nil && !errors.Is(err, ErrNilSnapshot) {
return err
}
}
input.Values.Set("admissionPolicyEngine.internal.denyVulnerableImages.dockerConfigJson", resultCfg)
return nil
}
var ErrNilSnapshot = errors.New("nil snapshot")
func convertSnapToAuthnConfig(authSnap interface{}, resultCfg *dockerConfig) error {
if authSnap == nil {
return fmt.Errorf("%w: %v", ErrNilSnapshot, authSnap)
}
auths, ok := authSnap.(map[string]authn.AuthConfig)
if !ok {
return fmt.Errorf("can't convert auths snaphsot to map[string]authn.AuthConfig{}: %v", authSnap)
}
for registry, config := range auths {
resultCfg.Auths[registry] = config
}
return nil
}
func registrySecretValueToAuthnConfig(ctx context.Context, registrySecretValue gjson.Result, k8sClient k8s.Client) (*dockerConfig, error) {
name, namespace, err := registrySecretValueToNamespaceName(registrySecretValue)
if err != nil {
return nil, fmt.Errorf("can't retrieve name and namespace from registry secret entry: %w", err)
}
registrySecret, err := k8sClient.CoreV1().Secrets(namespace).Get(ctx, name, v1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("can't get registry secret from namespace '%s': %w", namespace, err)
}
return registrySecretToAuthnConfig(registrySecret)
}
func registrySecretValueToNamespaceName(registrySecretValue gjson.Result) (string, string, error) {
data := registrySecretValue.Map()
if len(data) == 0 {
return "", "", fmt.Errorf("no data found from registrySecret value")
}
name, ok := data["name"]
if !ok {
return "", "", fmt.Errorf("no name found for registry secret")
}
namespace, ok := data["namespace"]
if !ok {
return "", "", fmt.Errorf("no namespace found for registry secret")
}
return name.String(), namespace.String(), nil
}