/
sign.go
272 lines (236 loc) · 8.99 KB
/
sign.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
// Copyright 2020 IBM Corporation
//
// 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 sign
import (
"encoding/json"
"fmt"
"strings"
vrsig "github.com/IBM/integrity-enforcer/enforcer/pkg/apis/resourcesignature/v1alpha1"
"github.com/IBM/integrity-enforcer/enforcer/pkg/config"
common "github.com/IBM/integrity-enforcer/enforcer/pkg/control/common"
logger "github.com/IBM/integrity-enforcer/enforcer/pkg/logger"
helm "github.com/IBM/integrity-enforcer/enforcer/pkg/plugins/helm"
policy "github.com/IBM/integrity-enforcer/enforcer/pkg/policy"
"github.com/IBM/integrity-enforcer/enforcer/pkg/protect"
)
type VerifyType string
type SignatureType string
const (
VerifyTypeX509 VerifyType = "x509"
VerifyTypePGP VerifyType = "pgp"
)
const (
SignatureTypeUnknown SignatureType = ""
SignatureTypeResource SignatureType = "Resource"
SignatureTypeApplyingResource SignatureType = "ApplyingResource"
SignatureTypePatch SignatureType = "Patch"
SignatureTypeHelm SignatureType = "Helm"
)
/**********************************************
GeneralSignature
***********************************************/
type GeneralSignature struct {
SignType SignatureType
data map[string]string
option map[string]bool
}
/**********************************************
SignPolicy
***********************************************/
type SignPolicyEvaluator interface {
Eval(reqc *common.ReqContext, resSigList *vrsig.ResourceSignatureList, protectAttrs, unprotectAttrs []*protect.AttrsPattern) (*common.SignPolicyEvalResult, error)
}
type ConcreteSignPolicyEvaluator struct {
config *config.EnforcerConfig
policy *policy.SignPolicy
plugins map[string]bool
}
func NewSignPolicyEvaluator(config *config.EnforcerConfig, policy *policy.SignPolicy, plugins map[string]bool) (SignPolicyEvaluator, error) {
return &ConcreteSignPolicyEvaluator{
config: config,
policy: policy,
plugins: plugins,
}, nil
}
func (self *ConcreteSignPolicyEvaluator) GetResourceSignature(ref *common.ResourceRef, reqc *common.ReqContext, resSigList *vrsig.ResourceSignatureList, protectAttrs, unprotectAttrs []*protect.AttrsPattern) *GeneralSignature {
sigAnnotations := reqc.ClaimedMetadata.Annotations.SignatureAnnotations()
matchedProtectAttrs := strings.Join(findAttrsPattern(reqc, protectAttrs), ",")
matchedUnprotectAttrs := strings.Join(findAttrsPattern(reqc, unprotectAttrs), ",")
//1. pick ResourceSignature from metadata.annotation if available
if sigAnnotations.Signature != "" {
message := base64decode(sigAnnotations.Message)
messageScope := sigAnnotations.MessageScope
mutableAttrs := sigAnnotations.MutableAttrs
matchRequired := true
scopedSignature := false
if message == "" && messageScope != "" {
message = GenerateMessageFromRawObj(reqc.RawObject, messageScope, mutableAttrs)
matchRequired = false // skip matching because the message is generated from Requested Object
scopedSignature = true // enable checking if the signature is for patch
}
signature := base64decode(sigAnnotations.Signature)
certificate := base64decode(sigAnnotations.Certificate)
signType := SignatureTypeResource
if sigAnnotations.SignatureType == vrsig.SignatureTypeApplyingResource {
signType = SignatureTypeApplyingResource
} else if sigAnnotations.SignatureType == vrsig.SignatureTypePatch {
signType = SignatureTypePatch
}
return &GeneralSignature{
SignType: signType,
data: map[string]string{"signature": signature, "message": message, "certificate": certificate, "scope": messageScope, "protectAttrs": matchedProtectAttrs, "unprotectAttrs": matchedUnprotectAttrs},
option: map[string]bool{"matchRequired": matchRequired, "scopedSignature": scopedSignature},
}
}
//2. pick ResourceSignature from custom resource if available
if resSigList != nil && len(resSigList.Items) > 0 {
si, yamlBytes, found := resSigList.FindSignItem(ref.ApiVersion, ref.Kind, ref.Name, ref.Namespace)
if found {
signature := base64decode(si.Signature)
certificate := base64decode(si.Certificate)
message := base64decode(si.Message)
mutableAttrs := si.MutableAttrs
matchRequired := true
scopedSignature := false
if si.Message == "" && si.MessageScope != "" {
message = GenerateMessageFromRawObj(reqc.RawObject, si.MessageScope, mutableAttrs)
matchRequired = false // skip matching because the message is generated from Requested Object
scopedSignature = true // enable checking if the signature is for patch
}
signType := SignatureTypeResource
if si.Type == vrsig.SignatureTypeApplyingResource {
signType = SignatureTypeApplyingResource
} else if si.Type == vrsig.SignatureTypePatch {
signType = SignatureTypePatch
}
return &GeneralSignature{
SignType: signType,
data: map[string]string{"signature": signature, "message": message, "certificate": certificate, "yamlBytes": string(yamlBytes), "scope": si.MessageScope, "protectAttrs": matchedProtectAttrs, "unprotectAttrs": matchedUnprotectAttrs},
option: map[string]bool{"matchRequired": matchRequired, "scopedSignature": scopedSignature},
}
}
}
//3. pick ResourceSignature from external store if available
//4. helm resource (release secret, helm cahrt resources)
if ok := self.plugins["helm"]; ok {
rsecBytes, err := helm.FindReleaseSecret(reqc.Namespace, reqc.Kind, reqc.Name, reqc.RawObject)
if err != nil {
logger.Error(fmt.Sprintf("Error occured in finding helm release secret; %s", err.Error()))
return nil
}
if rsecBytes != nil {
hrmSigs, err := helm.GetHelmReleaseMetadata(rsecBytes)
if err == nil && len(hrmSigs) == 2 {
rls := hrmSigs[0]
hrm := hrmSigs[1]
eCfg := true
return &GeneralSignature{
SignType: SignatureTypeHelm,
data: map[string]string{"releaseSecret": rls, "helmReleaseMetadata": hrm},
option: map[string]bool{"emptyConfig": eCfg, "matchRequired": true},
}
} else {
logger.Error(fmt.Sprintf("Error occured in getting signature from helm release metadata; %s", err.Error()))
return nil
}
}
}
return nil
//5. return nil if no signature found
// return nil
}
func (self *ConcreteSignPolicyEvaluator) Eval(reqc *common.ReqContext, resSigList *vrsig.ResourceSignatureList, protectAttrs, unprotectAttrs []*protect.AttrsPattern) (*common.SignPolicyEvalResult, error) {
// eval sign policy
ref := reqc.ResourceRef()
// find signature
rsig := self.GetResourceSignature(ref, reqc, resSigList, protectAttrs, unprotectAttrs)
if rsig == nil {
return &common.SignPolicyEvalResult{
Allow: false,
Checked: true,
Error: &common.CheckError{
Reason: "No signature found",
},
}, nil
}
verifyType := VerifyType(self.config.VerifyType)
// create verifier
verifier := NewVerifier(verifyType, rsig.SignType, self.config.Namespace, self.config.CertPoolPath, self.config.KeyringPath)
// verify signature
sigVerifyResult, err := verifier.Verify(rsig, reqc)
if err != nil {
return &common.SignPolicyEvalResult{
Allow: false,
Checked: true,
Error: &common.CheckError{
Error: err,
Reason: "Error during signature verification",
},
}, nil
}
if sigVerifyResult == nil || sigVerifyResult.Signer == nil {
msg := ""
if sigVerifyResult != nil && sigVerifyResult.Error != nil {
msg = sigVerifyResult.Error.Reason
}
return &common.SignPolicyEvalResult{
Allow: false,
Checked: true,
Error: &common.CheckError{
Reason: fmt.Sprintf("Failed to verify signature; %s", msg),
},
}, nil
}
// signer
signer := sigVerifyResult.Signer
// check signer policy
signerMatched, matchedPolicy := self.policy.Match(reqc.Namespace, signer)
matchedPolicyStr := ""
if matchedPolicy != nil {
tmpMatchedPolicy, _ := json.Marshal(matchedPolicy)
matchedPolicyStr = string(tmpMatchedPolicy)
}
if signerMatched {
return &common.SignPolicyEvalResult{
Signer: signer,
SignerName: signer.GetName(),
Allow: true,
Checked: true,
MatchedPolicy: matchedPolicyStr,
Error: nil,
}, nil
} else {
return &common.SignPolicyEvalResult{
Signer: signer,
SignerName: signer.GetName(),
Allow: false,
Checked: true,
Error: &common.CheckError{
Reason: fmt.Sprintf("No signer policies met this resource. this resource is signed by %s", signer.GetName()),
},
}, nil
}
}
func findAttrsPattern(reqc *common.ReqContext, attrs []*protect.AttrsPattern) []string {
reqFields := reqc.Map()
masks := []string{}
for _, attr := range attrs {
if attr.Match.Match(reqFields) {
masks = append(masks, attr.Attrs...)
}
}
return masks
}