| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package trustpolicy |
| 15 |
|
| 16 |
import ( |
| 17 |
"encoding/json" |
| 18 |
"fmt" |
| 19 |
"os" |
| 20 |
"path/filepath" |
| 21 |
"runtime" |
| 22 |
"strconv" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
|
| 26 |
"github.com/notaryproject/notation-go/dir" |
| 27 |
) |
| 28 |
|
| 29 |
func dummyPolicyDocument() Document { |
| 30 |
return Document{ |
| 31 |
Version: "1.0", |
| 32 |
TrustPolicies: []TrustPolicy{ |
| 33 |
{ |
| 34 |
Name: "test-statement-name", |
| 35 |
RegistryScopes: []string{"registry.acme-rockets.io/software/net-monitor"}, |
| 36 |
SignatureVerification: SignatureVerification{VerificationLevel: "strict"}, |
| 37 |
TrustStores: []string{"ca:valid-trust-store", "signingAuthority:valid-trust-store"}, |
| 38 |
TrustedIdentities: []string{"x509.subject:CN=Notation Test Root,O=Notary,L=Seattle,ST=WA,C=US"}, |
| 39 |
}, |
| 40 |
}, |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// create testcase for validatePolicyCore method |
| 45 |
func TestValidatePolicyCore(t *testing.T) { |
| 46 |
policyName := "test-statement-name" |
| 47 |
sigVerification := SignatureVerification{VerificationLevel: "strict"} |
| 48 |
// valid policy |
| 49 |
if err := validatePolicyCore(policyName, sigVerification, []string{"ca:valid-ts"}, []string{"*"}); err != nil { |
| 50 |
t.Errorf("validatePolicyCore returned error: '%v'", err) |
| 51 |
} |
| 52 |
|
| 53 |
// check valid skip SignatureVerification |
| 54 |
if err := validatePolicyCore(policyName, SignatureVerification{VerificationLevel: "skip"}, []string{}, []string{}); err != nil { |
| 55 |
t.Errorf("validatePolicyCore returned error: '%v'", err) |
| 56 |
} |
| 57 |
|
| 58 |
// check skip SignatureVerification doesn't has trust store and trusted identity |
| 59 |
expectedErr := "trust policy statement \"test-statement-name\" is set to skip signature verification but configured with trust stores and/or trusted identities, remove them if signature verification needs to be skipped" |
| 60 |
if err := validatePolicyCore(policyName, SignatureVerification{VerificationLevel: "skip"}, []string{"ca:valid-ts"}, []string{}); err == nil || err.Error() != expectedErr { |
| 61 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 62 |
} |
| 63 |
if err := validatePolicyCore(policyName, SignatureVerification{VerificationLevel: "skip"}, []string{}, []string{"x509:zoop"}); err == nil || err.Error() != expectedErr { |
| 64 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 65 |
} |
| 66 |
|
| 67 |
// empty policy name |
| 68 |
expectedErr = "a trust policy statement is missing a name, every statement requires a name" |
| 69 |
if err := validatePolicyCore("", sigVerification, []string{"ca:valid-ts"}, []string{"*"}); err == nil || err.Error() != expectedErr { |
| 70 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 71 |
} |
| 72 |
|
| 73 |
// invalid SignatureVerification |
| 74 |
expectedErr = "trust policy statement \"test-statement-name\" has invalid signatureVerification: signature verification level is empty or missing in the trust policy statement" |
| 75 |
if err := validatePolicyCore(policyName, SignatureVerification{}, []string{"ca:valid-ts"}, []string{"*"}); err == nil || err.Error() != expectedErr { |
| 76 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 77 |
} |
| 78 |
|
| 79 |
// invalid trust-store or trust-policy |
| 80 |
expectedErr = "trust policy statement \"test-statement-name\" is either missing trust stores or trusted identities, both must be specified" |
| 81 |
if err := validatePolicyCore(policyName, sigVerification, []string{}, []string{}); err == nil || err.Error() != expectedErr { |
| 82 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 83 |
} |
| 84 |
if err := validatePolicyCore(policyName, sigVerification, []string{"ca:valid-ts"}, []string{}); err == nil || err.Error() != expectedErr { |
| 85 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 86 |
} |
| 87 |
|
| 88 |
expectedErr = "trust policy statement \"test-statement-name\" uses an unsupported trust store type \"hola\" in trust store value \"hola:valid-ts\"" |
| 89 |
if err := validatePolicyCore(policyName, sigVerification, []string{"hola:valid-ts"}, []string{"hola"}); err == nil || err.Error() != expectedErr { |
| 90 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 91 |
} |
| 92 |
|
| 93 |
expectedErr = "trust policy statement \"test-statement-name\" has trusted identity \"x509.subject\" missing separator" |
| 94 |
if err := validatePolicyCore(policyName, sigVerification, []string{"ca:valid-ts"}, []string{"x509.subject"}); err == nil || err.Error() != expectedErr { |
| 95 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// TestValidateTrustedIdentities tests only valid x509.subjects are accepted |
| 100 |
func TestValidateTrustStore(t *testing.T) { |
| 101 |
// valid trust-store |
| 102 |
if err := validateTrustStore("test-statement-name", []string{"ca:my-ts"}); err != nil { |
| 103 |
t.Errorf("validateTrustStore returned error: '%v", err) |
| 104 |
} |
| 105 |
|
| 106 |
// empty trust-store |
| 107 |
expectedErr := "trust policy statement \"test-statement-name\" has malformed trust store value \"\". The required format is <TrustStoreType>:<TrustStoreName>" |
| 108 |
if err := validateTrustStore("test-statement-name", []string{""}); err == nil || err.Error() != expectedErr { |
| 109 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 110 |
} |
| 111 |
|
| 112 |
// invalid trust-store type |
| 113 |
expectedErr = "trust policy statement \"test-statement-name\" uses an unsupported trust store type \"unknown\" in trust store value \"unknown:my-ts\"" |
| 114 |
if err := validateTrustStore("test-statement-name", []string{"unknown:my-ts"}); err == nil || err.Error() != expectedErr { |
| 115 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 116 |
} |
| 117 |
|
| 118 |
// invalid trust-store directory name |
| 119 |
expectedErr = "trust policy statement \"test-statement-name\" uses an unsupported trust store name \"#@$@$\" in trust store value \"ca:#@$@$\". Named store name needs to follow [a-zA-Z0-9_.-]+ format" |
| 120 |
if err := validateTrustStore("test-statement-name", []string{"ca:#@$@$"}); err == nil || err.Error() != expectedErr { |
| 121 |
t.Errorf("expected error '%s' but not found", expectedErr) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// TestValidateTrustedIdentities tests only valid x509.subjects are accepted |
| 126 |
func TestValidateTrustedIdentities(t *testing.T) { |
| 127 |
// wildcard present with specific trusted identity throws error. |
| 128 |
err := validateTrustedIdentities("test-statement-name", []string{"*", "C=US, ST=WA, O=wabbit-network.io"}) |
| 129 |
if err == nil || err.Error() != "trust policy statement \"test-statement-name\" uses a wildcard trusted identity '*', a wildcard identity cannot be used in conjunction with other values" { |
| 130 |
t.Fatalf("trusted identities with wildcard and specific identityshould return error") |
| 131 |
} |
| 132 |
|
| 133 |
// If empty trust policy throws error. |
| 134 |
err = validateTrustedIdentities("test-statement-name", []string{""}) |
| 135 |
if err == nil || err.Error() != "trust policy statement \"test-statement-name\" has an empty trusted identity" { |
| 136 |
t.Fatalf("empty trusted identity should return error") |
| 137 |
} |
| 138 |
|
| 139 |
// No trusted identity prefix throws error |
| 140 |
err = validateTrustedIdentities("test-statement-name", []string{"C=US, ST=WA, O=wabbit-network.io, OU=org1"}) |
| 141 |
if err == nil || err.Error() != "trust policy statement \"test-statement-name\" has trusted identity \"C=US, ST=WA, O=wabbit-network.io, OU=org1\" missing separator" { |
| 142 |
t.Fatalf("trusted identity without separator should return error") |
| 143 |
} |
| 144 |
|
| 145 |
// Accept unknown identity prefixes |
| 146 |
err = validateTrustedIdentities("test-statement-name", []string{"unknown:my-trusted-identity"}) |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("unknown identity prefix should not return an error. Error: %q", err) |
| 149 |
} |
| 150 |
|
| 151 |
// Validate x509.subject identities |
| 152 |
invalidDN := "x509.subject:,,," |
| 153 |
err = validateTrustedIdentities("test-statement-name", []string{invalidDN}) |
| 154 |
if err == nil || err.Error() != "trust policy statement \"test-statement-name\" has trusted identity \"x509.subject:,,,\" with invalid identity value: parsing distinguished name (DN) \",,,\" failed with err: incomplete type, value pair. A valid DN must contain 'C', 'ST' or 'S', and 'O' RDN attributes at a minimum, and follow RFC 4514 standard" { |
| 155 |
t.Fatalf("invalid x509.subject identity should return error. Error : %q", err) |
| 156 |
} |
| 157 |
|
| 158 |
// Validate x509.subject with no value |
| 159 |
err = validateTrustedIdentities("test-statement-name", []string{"x509.subject:"}) |
| 160 |
if err == nil || err.Error() != "trust policy statement \"test-statement-name\" has trusted identity \"x509.subject:\" without an identity value" { |
| 161 |
t.Fatalf("x509.subject identity without value should return error. Error : %q", err) |
| 162 |
} |
| 163 |
|
| 164 |
// Validate duplicate RDNs |
| 165 |
invalidDN = "x509.subject:C=US,C=IN" |
| 166 |
err = validateTrustedIdentities("test-statement-name", []string{invalidDN}) |
| 167 |
if err == nil || err.Error() != "trust policy statement \"test-statement-name\" has trusted identity \"x509.subject:C=US,C=IN\" with invalid identity value: distinguished name (DN) \"C=US,C=IN\" has duplicate RDN attribute for \"C\", DN can only have unique RDN attributes" { |
| 168 |
t.Fatalf("invalid x509.subject identity should return error. Error : %q", err) |
| 169 |
} |
| 170 |
|
| 171 |
// Validate mandatory RDNs |
| 172 |
invalidDN = "x509.subject:C=US,ST=WA" |
| 173 |
err = validateTrustedIdentities("test-statement-name", []string{invalidDN}) |
| 174 |
if err == nil || err.Error() != "trust policy statement \"test-statement-name\" has trusted identity \"x509.subject:C=US,ST=WA\" with invalid identity value: distinguished name (DN) \"C=US,ST=WA\" has no mandatory RDN attribute for \"O\", it must contain 'C', 'ST' or 'S', and 'O' RDN attributes at a minimum" { |
| 175 |
t.Fatalf("invalid x509.subject identity should return error. Error : %q", err) |
| 176 |
} |
| 177 |
|
| 178 |
// DN may have optional RDNs |
| 179 |
validDN := "x509.subject:C=US,ST=WA,O=MyOrg,CustomRDN=CustomValue" |
| 180 |
err = validateTrustedIdentities("test-statement-name", []string{validDN}) |
| 181 |
if err != nil { |
| 182 |
t.Fatalf("valid x509.subject identity should not return error. Error : %q", err) |
| 183 |
} |
| 184 |
|
| 185 |
// Validate rfc4514 DNs |
| 186 |
validDN1 := "x509.subject:C=US,ST=WA,O=MyOrg" |
| 187 |
validDN2 := "x509.subject:C=US,ST=WA,O= My. Org" |
| 188 |
validDN3 := "x509.subject:C=US,ST=WA,O=My \"special\" Org \\, \\; \\\\ others" |
| 189 |
err = validateTrustedIdentities("test-statement-name", []string{validDN1, validDN2, validDN3}) |
| 190 |
if err != nil { |
| 191 |
t.Fatalf("valid x509.subject identity should not return error. Error : %q", err) |
| 192 |
} |
| 193 |
|
| 194 |
// Validate overlapping DNs |
| 195 |
validDN1 = "x509.subject:C=US,ST=WA,O=MyOrg" |
| 196 |
validDN2 = "x509.subject:C=US,ST=WA,O=MyOrg,X=Y" |
| 197 |
err = validateTrustedIdentities("test-statement-name", []string{validDN1, validDN2}) |
| 198 |
if err == nil || err.Error() != "trust policy statement \"test-statement-name\" has overlapping x509 trustedIdentities, \"x509.subject:C=US,ST=WA,O=MyOrg\" overlaps with \"x509.subject:C=US,ST=WA,O=MyOrg,X=Y\"" { |
| 199 |
t.Fatalf("overlapping DNs should return error") |
| 200 |
} |
| 201 |
|
| 202 |
// Validate multi-valued RDNs |
| 203 |
multiValuedRUN := "x509.subject:C=US+ST=WA,O=MyOrg" |
| 204 |
err = validateTrustedIdentities("test-statement-name", []string{multiValuedRUN}) |
| 205 |
if err == nil || err.Error() != "trust policy statement \"test-statement-name\" has trusted identity \"x509.subject:C=US+ST=WA,O=MyOrg\" with invalid identity value: distinguished name (DN) \"C=US+ST=WA,O=MyOrg\" has multi-valued RDN attributes, remove multi-valued RDN attributes as they are not supported" { |
| 206 |
t.Fatalf("multi-valued RDN should return error. Error : %q", err) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestGetVerificationLevel(t *testing.T) { |
| 211 |
tests := []struct { |
| 212 |
verificationLevel SignatureVerification |
| 213 |
wantErr bool |
| 214 |
verificationActions []ValidationAction |
| 215 |
}{ |
| 216 |
{SignatureVerification{VerificationLevel: "strict"}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionEnforce, ActionEnforce, ActionEnforce}}, |
| 217 |
{SignatureVerification{VerificationLevel: "permissive"}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionLog, ActionLog, ActionLog}}, |
| 218 |
{SignatureVerification{VerificationLevel: "audit"}, false, []ValidationAction{ActionEnforce, ActionLog, ActionLog, ActionLog, ActionLog}}, |
| 219 |
{SignatureVerification{VerificationLevel: "skip"}, false, []ValidationAction{ActionSkip, ActionSkip, ActionSkip, ActionSkip, ActionSkip}}, |
| 220 |
{SignatureVerification{VerificationLevel: "invalid"}, true, []ValidationAction{}}, |
| 221 |
} |
| 222 |
for i, tt := range tests { |
| 223 |
t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 224 |
|
| 225 |
level, err := tt.verificationLevel.GetVerificationLevel() |
| 226 |
|
| 227 |
if tt.wantErr != (err != nil) { |
| 228 |
t.Fatalf("TestFindVerificationLevel Error: %q WantErr: %v", err, tt.wantErr) |
| 229 |
} else { |
| 230 |
for index, action := range tt.verificationActions { |
| 231 |
if action != level.Enforcement[ValidationTypes[index]] { |
| 232 |
t.Errorf("%q verification action should be %q for Verification Level %v", ValidationTypes[index], action, tt.verificationLevel) |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
}) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func TestCustomVerificationLevel(t *testing.T) { |
| 241 |
tests := []struct { |
| 242 |
customVerification SignatureVerification |
| 243 |
wantErr bool |
| 244 |
verificationActions []ValidationAction |
| 245 |
}{ |
| 246 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"integrity": "log"}}, true, []ValidationAction{}}, |
| 247 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"authenticity": "skip"}}, true, []ValidationAction{}}, |
| 248 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"authenticTimestamp": "skip"}}, true, []ValidationAction{}}, |
| 249 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"expiry": "skip"}}, true, []ValidationAction{}}, |
| 250 |
{SignatureVerification{VerificationLevel: "skip", Override: map[ValidationType]ValidationAction{"authenticity": "log"}}, true, []ValidationAction{}}, |
| 251 |
{SignatureVerification{VerificationLevel: "invalid", Override: map[ValidationType]ValidationAction{"authenticity": "log"}}, true, []ValidationAction{}}, |
| 252 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"invalid": "log"}}, true, []ValidationAction{}}, |
| 253 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"authenticity": "invalid"}}, true, []ValidationAction{}}, |
| 254 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"authenticity": "log"}}, false, []ValidationAction{ActionEnforce, ActionLog, ActionEnforce, ActionEnforce, ActionEnforce}}, |
| 255 |
{SignatureVerification{VerificationLevel: "permissive", Override: map[ValidationType]ValidationAction{"authenticity": "log"}}, false, []ValidationAction{ActionEnforce, ActionLog, ActionLog, ActionLog, ActionLog}}, |
| 256 |
{SignatureVerification{VerificationLevel: "audit", Override: map[ValidationType]ValidationAction{"authenticity": "log"}}, false, []ValidationAction{ActionEnforce, ActionLog, ActionLog, ActionLog, ActionLog}}, |
| 257 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"expiry": "log"}}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionEnforce, ActionLog, ActionEnforce}}, |
| 258 |
{SignatureVerification{VerificationLevel: "permissive", Override: map[ValidationType]ValidationAction{"expiry": "log"}}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionLog, ActionLog, ActionLog}}, |
| 259 |
{SignatureVerification{VerificationLevel: "audit", Override: map[ValidationType]ValidationAction{"expiry": "log"}}, false, []ValidationAction{ActionEnforce, ActionLog, ActionLog, ActionLog, ActionLog}}, |
| 260 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"revocation": "log"}}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionEnforce, ActionEnforce, ActionLog}}, |
| 261 |
{SignatureVerification{VerificationLevel: "permissive", Override: map[ValidationType]ValidationAction{"revocation": "log"}}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionLog, ActionLog, ActionLog}}, |
| 262 |
{SignatureVerification{VerificationLevel: "audit", Override: map[ValidationType]ValidationAction{"revocation": "log"}}, false, []ValidationAction{ActionEnforce, ActionLog, ActionLog, ActionLog, ActionLog}}, |
| 263 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"revocation": "skip"}}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionEnforce, ActionEnforce, ActionSkip}}, |
| 264 |
{SignatureVerification{VerificationLevel: "permissive", Override: map[ValidationType]ValidationAction{"revocation": "skip"}}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionLog, ActionLog, ActionSkip}}, |
| 265 |
{SignatureVerification{VerificationLevel: "audit", Override: map[ValidationType]ValidationAction{"revocation": "skip"}}, false, []ValidationAction{ActionEnforce, ActionLog, ActionLog, ActionLog, ActionSkip}}, |
| 266 |
{SignatureVerification{VerificationLevel: "permissive", Override: map[ValidationType]ValidationAction{"authenticTimestamp": "log"}}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionLog, ActionLog, ActionLog}}, |
| 267 |
{SignatureVerification{VerificationLevel: "audit", Override: map[ValidationType]ValidationAction{"authenticTimestamp": "log"}}, false, []ValidationAction{ActionEnforce, ActionLog, ActionLog, ActionLog, ActionLog}}, |
| 268 |
{SignatureVerification{VerificationLevel: "strict", Override: map[ValidationType]ValidationAction{"authenticTimestamp": "log"}}, false, []ValidationAction{ActionEnforce, ActionEnforce, ActionLog, ActionEnforce, ActionEnforce}}, |
| 269 |
} |
| 270 |
for i, tt := range tests { |
| 271 |
t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 272 |
level, err := tt.customVerification.GetVerificationLevel() |
| 273 |
|
| 274 |
if tt.wantErr != (err != nil) { |
| 275 |
t.Fatalf("TestCustomVerificationLevel Error: %q WantErr: %v", err, tt.wantErr) |
| 276 |
} else { |
| 277 |
if !tt.wantErr && len(tt.verificationActions) == 0 { |
| 278 |
t.Errorf("test case isn't configured with VerificationActions") |
| 279 |
} |
| 280 |
for index, action := range tt.verificationActions { |
| 281 |
if action != level.Enforcement[ValidationTypes[index]] { |
| 282 |
t.Errorf("%q verification action should be %q for custom verification %v", ValidationTypes[index], action, tt.customVerification) |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
}) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func TestGetDocument(t *testing.T) { |
| 291 |
if runtime.GOOS == "windows" { |
| 292 |
t.Skip("skipping test on Windows") |
| 293 |
} |
| 294 |
dir.UserConfigDir = "/" |
| 295 |
var doc Document |
| 296 |
tests := []struct { |
| 297 |
name string |
| 298 |
expectedDocument any |
| 299 |
actualDocument any |
| 300 |
}{ |
| 301 |
{ |
| 302 |
name: "valid policy file", |
| 303 |
expectedDocument: dummyPolicyDocument(), |
| 304 |
actualDocument: &doc, |
| 305 |
}, |
| 306 |
} |
| 307 |
|
| 308 |
for _, tt := range tests { |
| 309 |
t.Run(tt.name, func(t *testing.T) { |
| 310 |
tempRoot := t.TempDir() |
| 311 |
path := filepath.Join(tempRoot, "trustpolicy.json") |
| 312 |
policyJson, _ := json.Marshal(tt.expectedDocument) |
| 313 |
if err := os.WriteFile(path, policyJson, 0600); err != nil { |
| 314 |
t.Fatalf("TestGetDocument write policy file failed. Error: %v", err) |
| 315 |
} |
| 316 |
t.Cleanup(func() { os.RemoveAll(tempRoot) }) |
| 317 |
|
| 318 |
if err := getDocument(path, tt.actualDocument); err != nil { |
| 319 |
t.Fatalf("getDocument() should not throw error for an existing policy file. Error: %v", err) |
| 320 |
} |
| 321 |
}) |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
func TestGetDocumentErrors(t *testing.T) { |
| 326 |
dir.UserConfigDir = "/" |
| 327 |
t.Run("non-existing policy file", func(t *testing.T) { |
| 328 |
var doc Document |
| 329 |
if err := getDocument("blaah", &doc); err == nil || err.Error() != fmt.Sprintf("trust policy is not present. To create a trust policy, see: %s", trustPolicyLink) { |
| 330 |
t.Fatalf("getDocument() should throw error for non existent policy") |
| 331 |
} |
| 332 |
}) |
| 333 |
|
| 334 |
t.Run("invalid json file", func(t *testing.T) { |
| 335 |
if runtime.GOOS == "windows" { |
| 336 |
t.Skip("skipping test on Windows") |
| 337 |
} |
| 338 |
tempRoot := t.TempDir() |
| 339 |
path := filepath.Join(tempRoot, "invalid.json") |
| 340 |
if err := os.WriteFile(path, []byte(`{"invalid`), 0600); err != nil { |
| 341 |
t.Fatalf("creation of invalid policy file failed. Error: %v", err) |
| 342 |
} |
| 343 |
t.Cleanup(func() { os.RemoveAll(tempRoot) }) |
| 344 |
|
| 345 |
var doc Document |
| 346 |
if err := getDocument(path, &doc); err == nil || err.Error() != fmt.Sprintf("malformed trust policy. To create a trust policy, see: %s", trustPolicyLink) { |
| 347 |
t.Fatalf("getDocument() should throw error for invalid policy file. Error: %v", err) |
| 348 |
} |
| 349 |
}) |
| 350 |
|
| 351 |
t.Run("policy file with bad permissions", func(t *testing.T) { |
| 352 |
if runtime.GOOS == "windows" { |
| 353 |
t.Skip("skipping test on Windows") |
| 354 |
} |
| 355 |
tempRoot := t.TempDir() |
| 356 |
policyJson, _ := json.Marshal([]byte("Some String")) |
| 357 |
path := filepath.Join(tempRoot, "trustpolicy.json") |
| 358 |
if err := os.WriteFile(path, policyJson, 0000); err != nil { |
| 359 |
t.Fatalf("creation of invalid permission policy file failed. Error: %v", err) |
| 360 |
} |
| 361 |
expectedErrMsg := fmt.Sprintf("unable to read trust policy due to file permissions, please verify the permissions of %s", path) |
| 362 |
var doc Document |
| 363 |
if err := getDocument(path, &doc); err == nil || err.Error() != expectedErrMsg { |
| 364 |
t.Errorf("getDocument() should throw error for a policy file with bad permissions. "+ |
| 365 |
"Expected error: '%v'qq but found '%v'", expectedErrMsg, err.Error()) |
| 366 |
} |
| 367 |
}) |
| 368 |
|
| 369 |
t.Run("symlink policy file", func(t *testing.T) { |
| 370 |
if runtime.GOOS == "windows" { |
| 371 |
t.Skip("skipping test on Windows") |
| 372 |
} |
| 373 |
tempRoot := t.TempDir() |
| 374 |
path := filepath.Join(tempRoot, "trustpolicy.json") |
| 375 |
if err := os.WriteFile(path, []byte(`{"invalid`), 0600); err != nil { |
| 376 |
t.Fatalf("creation of policy file failed. Error: %v", err) |
| 377 |
} |
| 378 |
|
| 379 |
symlinkPath := filepath.Join(tempRoot, "invalid.json") |
| 380 |
if err := os.Symlink(path, symlinkPath); err != nil { |
| 381 |
t.Fatalf("creation of symlink for policy file failed. Error: %v", err) |
| 382 |
} |
| 383 |
var doc Document |
| 384 |
if err := getDocument(symlinkPath, &doc); err == nil || !strings.HasPrefix(err.Error(), "trust policy is not a regular file (symlinks are not supported)") { |
| 385 |
t.Fatalf("getDocument() should throw error for a symlink policy file. Error: %v", err) |
| 386 |
} |
| 387 |
}) |
| 388 |
} |