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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
/*
* Copyright 2018-2024 The NATS Authors
* 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 jwt
import (
"fmt"
"testing"
"time"
"github.com/nats-io/nkeys"
)
func TestNewOperatorClaims(t *testing.T) {
ckp := createOperatorNKey(t)
uc := NewOperatorClaims(publicKey(ckp, t))
uc.Expires = time.Now().Add(time.Hour).Unix()
uJwt := encode(uc, ckp, t)
uc2, err := DecodeOperatorClaims(uJwt)
if err != nil {
t.Fatal("failed to decode", err)
}
AssertEquals(uc.String(), uc2.String(), t)
AssertEquals(uc.Claims() != nil, true, t)
AssertEquals(uc.Payload() != nil, true, t)
}
func TestOperatorSubjects(t *testing.T) {
type kpInputs struct {
name string
kp nkeys.KeyPair
ok bool
}
inputs := []kpInputs{
{"account", createAccountNKey(t), false},
{"cluster", createClusterNKey(t), false},
{"operator", createOperatorNKey(t), true},
{"server", createServerNKey(t), false},
{"user", createUserNKey(t), false},
}
for _, i := range inputs {
c := NewOperatorClaims(publicKey(i.kp, t))
_, err := c.Encode(createOperatorNKey(t))
if i.ok && err != nil {
t.Fatalf("unexpected error for %q: %v", i.name, err)
}
if !i.ok && err == nil {
t.Logf("should have failed to encode server with with %q subject", i.name)
t.Fail()
}
}
}
func TestInvalidOperatorClaimIssuer(t *testing.T) {
akp := createOperatorNKey(t)
ac := NewOperatorClaims(publicKey(akp, t))
ac.Expires = time.Now().Add(time.Hour).Unix()
aJwt := encode(ac, akp, t)
temp, err := DecodeGeneric(aJwt)
if err != nil {
t.Fatal("failed to decode", err)
}
type kpInputs struct {
name string
kp nkeys.KeyPair
ok bool
}
inputs := []kpInputs{
{"account", createAccountNKey(t), false},
{"user", createUserNKey(t), false},
{"operator", createOperatorNKey(t), true},
{"server", createServerNKey(t), false},
{"cluster", createClusterNKey(t), false},
}
for _, i := range inputs {
bad := encode(temp, i.kp, t)
_, err = DecodeOperatorClaims(bad)
if i.ok && err != nil {
t.Fatalf("unexpected error for %q: %v", i.name, err)
}
if !i.ok && err == nil {
t.Logf("should have failed to decode account signed by %q", i.name)
t.Fail()
}
}
}
func TestNewNilOperatorClaims(t *testing.T) {
v := NewOperatorClaims("")
if v != nil {
t.Fatal("expected nil user claim")
}
}
func TestOperatorType(t *testing.T) {
c := NewOperatorClaims(publicKey(createOperatorNKey(t), t))
s := encode(c, createOperatorNKey(t), t)
u, err := DecodeOperatorClaims(s)
if err != nil {
t.Fatalf("failed to decode operator claim: %v", err)
}
if OperatorClaim != u.Type {
t.Fatalf("type is unexpected %q (wanted operator)", u.Type)
}
}
func TestSigningKeyValidation(t *testing.T) {
ckp := createOperatorNKey(t)
ckp2 := createOperatorNKey(t)
uc := NewOperatorClaims(publicKey(ckp, t))
uc.Expires = time.Now().Add(time.Hour).Unix()
uc.SigningKeys.Add(publicKey(ckp2, t))
uJwt := encode(uc, ckp, t)
uc2, err := DecodeOperatorClaims(uJwt)
if err != nil {
t.Fatal("failed to decode", err)
}
AssertEquals(len(uc2.SigningKeys), 1, t)
AssertEquals(uc2.SigningKeys[0] == publicKey(ckp2, t), true, t)
vr := &ValidationResults{}
uc.Validate(vr)
if len(vr.Issues) != 0 {
t.Fatal("valid operator key should have no validation issues")
}
uc.SigningKeys.Add("") // add an invalid one
vr = &ValidationResults{}
uc.Validate(vr)
if len(vr.Issues) != 0 {
t.Fatal("should not be able to add empty values")
}
}
func TestSignedBy(t *testing.T) {
ckp := createOperatorNKey(t)
ckp2 := createOperatorNKey(t)
uc := NewOperatorClaims(publicKey(ckp, t))
uc2 := NewOperatorClaims(publicKey(ckp2, t))
akp := createAccountNKey(t)
ac := NewAccountClaims(publicKey(akp, t))
enc, err := ac.Encode(ckp) // sign with the operator key
if err != nil {
t.Fatal("failed to encode", err)
}
ac, err = DecodeAccountClaims(enc)
if err != nil {
t.Fatal("failed to decode", err)
}
AssertEquals(uc.DidSign(ac), true, t)
AssertEquals(uc2.DidSign(ac), false, t)
enc, err = ac.Encode(ckp2) // sign with the other operator key
if err != nil {
t.Fatal("failed to encode", err)
}
ac, err = DecodeAccountClaims(enc)
if err != nil {
t.Fatal("failed to decode", err)
}
AssertEquals(uc.DidSign(ac), false, t) // no signing key
AssertEquals(uc2.DidSign(ac), true, t) // actual key
uc.SigningKeys.Add(publicKey(ckp2, t))
AssertEquals(uc.DidSign(ac), true, t) // signing key
uc.StrictSigningKeyUsage = true
AssertEquals(uc.DidSign(uc), true, t)
AssertEquals(uc.DidSign(ac), true, t)
uc2.StrictSigningKeyUsage = true
AssertEquals(uc2.DidSign(uc2), true, t)
AssertEquals(uc2.DidSign(ac), false, t)
}
func testAccountWithAccountServerURL(t *testing.T, u string) error {
kp := createOperatorNKey(t)
pk := publicKey(kp, t)
oc := NewOperatorClaims(pk)
oc.AccountServerURL = u
s, err := oc.Encode(kp)
if err != nil {
return err
}
oc, err = DecodeOperatorClaims(s)
if err != nil {
t.Fatal(err)
}
AssertEquals(oc.AccountServerURL, u, t)
vr := ValidationResults{}
oc.Validate(&vr)
if !vr.IsEmpty() {
errs := vr.Errors()
return errs[0]
}
return nil
}
func Test_AccountServerURL(t *testing.T) {
var asuTests = []struct {
u string
shouldFail bool
}{
{"", false},
{"HTTP://foo.bar.com", false},
{"http://foo.bar.com/foo/bar", false},
{"http://user:pass@foo.bar.com/foo/bar", false},
{"https://foo.bar.com", false},
{"nats://foo.bar.com", false},
{"/hello", true},
}
for i, tt := range asuTests {
err := testAccountWithAccountServerURL(t, tt.u)
if err != nil && tt.shouldFail == false {
t.Fatalf("expected not to fail: %v", err)
} else if err == nil && tt.shouldFail {
t.Fatalf("test %s expected to fail but didn't", asuTests[i].u)
}
}
}
func Test_SystemAccount(t *testing.T) {
operatorWithSystemAcc := func(t *testing.T, u string) error {
kp := createOperatorNKey(t)
pk := publicKey(kp, t)
oc := NewOperatorClaims(pk)
oc.SystemAccount = u
s, err := oc.Encode(kp)
if err != nil {
return err
}
oc, err = DecodeOperatorClaims(s)
if err != nil {
t.Fatal(err)
}
AssertEquals(oc.SystemAccount, u, t)
vr := ValidationResults{}
oc.Validate(&vr)
if !vr.IsEmpty() {
return fmt.Errorf("%s", vr.Errors()[0])
}
return nil
}
var asuTests = []struct {
accKey string
shouldFail bool
}{
{"", false},
{"x", true},
{"ADZ547B24WHPLWOK7TMLNBSA7FQFXR6UM2NZ4HHNIB7RDFVZQFOZ4GQQ", false},
{"ADZ547B24WHPLWOK7TMLNBSA7FQFXR6UM2NZ4HHNIB7RDFVZQFOZ4777", true},
}
for i, tt := range asuTests {
err := operatorWithSystemAcc(t, tt.accKey)
if err != nil && tt.shouldFail == false {
t.Fatalf("expected not to fail: %v", err)
} else if err == nil && tt.shouldFail {
t.Fatalf("test %s expected to fail but didn't", asuTests[i].accKey)
}
}
}
func Test_AssertServerVersion(t *testing.T) {
operatorWithAssertServerVer := func(t *testing.T, v string) error {
kp := createOperatorNKey(t)
pk := publicKey(kp, t)
oc := NewOperatorClaims(pk)
oc.AssertServerVersion = v
s, err := oc.Encode(kp)
if err != nil {
return err
}
oc, err = DecodeOperatorClaims(s)
if err != nil {
t.Fatal(err)
}
AssertEquals(oc.AssertServerVersion, v, t)
vr := ValidationResults{}
oc.Validate(&vr)
if !vr.IsEmpty() {
return fmt.Errorf("%s", vr.Errors()[0])
}
return nil
}
var asuTests = []struct {
assertVer string
shouldFail bool
}{
{"1.2.3", false},
{"10.2.3", false},
{"1.20.3", false},
{"1.2.30", false},
{"10.20.30", false},
{"0.0.0", false},
{"0.0", true},
{"0", true},
{"a", true},
{"a.b.c", true},
{"1..1", true},
{"1a.b.c", true},
{"-1.0.0", true},
{"1.-1.0", true},
{"1.0.-1", true},
}
for i, tt := range asuTests {
err := operatorWithAssertServerVer(t, tt.assertVer)
if err != nil && tt.shouldFail == false {
t.Fatalf("expected not to fail: %v", err)
} else if err == nil && tt.shouldFail {
t.Fatalf("test %s expected to fail but didn't", asuTests[i].assertVer)
}
}
}
func testOperatorWithOperatorServiceURL(t *testing.T, u string) error {
kp := createOperatorNKey(t)
pk := publicKey(kp, t)
oc := NewOperatorClaims(pk)
oc.OperatorServiceURLs.Add(u)
s, err := oc.Encode(kp)
if err != nil {
return err
}
oc, err = DecodeOperatorClaims(s)
if err != nil {
t.Fatal(err)
}
if u != "" {
AssertEquals(oc.OperatorServiceURLs[0], u, t)
}
vr := ValidationResults{}
oc.Validate(&vr)
if !vr.IsEmpty() {
errs := vr.Errors()
return errs[0]
}
return nil
}
func Test_OperatorServiceURL(t *testing.T) {
var asuTests = []struct {
u string
shouldFail bool
}{
{"", false},
{"HTTP://foo.bar.com", true},
{"http://foo.bar.com/foo/bar", true},
{"nats://user:pass@foo.bar.com", true},
{"NATS://user:pass@foo.bar.com", true},
{"NATS://user@foo.bar.com", true},
{"nats://foo.bar.com/path", true},
{"tls://foo.bar.com/path", true},
{"/hello", true},
{"NATS://foo.bar.com", false},
{"TLS://foo.bar.com", false},
{"nats://foo.bar.com", false},
{"tls://foo.bar.com", false},
}
for i, tt := range asuTests {
err := testOperatorWithOperatorServiceURL(t, tt.u)
if err != nil && tt.shouldFail == false {
t.Fatalf("expected not to fail: %v", err)
} else if err == nil && tt.shouldFail {
t.Fatalf("test %s expected to fail but didn't", asuTests[i].u)
}
}
// now test all of them in a single jwt
kp := createOperatorNKey(t)
pk := publicKey(kp, t)
oc := NewOperatorClaims(pk)
encoded := 0
shouldFail := 0
for _, v := range asuTests {
oc.OperatorServiceURLs.Add(v.u)
// list won't encode empty strings
if v.u != "" {
encoded++
}
if v.shouldFail {
shouldFail++
}
}
s, err := oc.Encode(kp)
if err != nil {
t.Fatal(err)
}
oc, err = DecodeOperatorClaims(s)
if err != nil {
t.Fatal(err)
}
AssertEquals(len(oc.OperatorServiceURLs), encoded, t)
vr := ValidationResults{}
oc.Validate(&vr)
if vr.IsEmpty() {
t.Fatal("should have had errors")
}
errs := vr.Errors()
AssertEquals(len(errs), shouldFail, t)
}
func TestTags(t *testing.T) {
okp := createOperatorNKey(t)
opk := publicKey(okp, t)
oc := NewOperatorClaims(opk)
oc.Tags.Add("one")
oc.Tags.Add("one") // duplicated tags should be ignored
oc.Tags.Add("TWO") // should become lower case
oc.Tags.Add("three")
oJwt := encode(oc, okp, t)
oc2, err := DecodeOperatorClaims(oJwt)
if err != nil {
t.Fatal(err)
}
if len(oc2.GenericFields.Tags) != 3 {
t.Fatal("expected 3 tags")
}
for _, v := range oc.GenericFields.Tags {
AssertFalse(v == "TWO", t)
}
AssertTrue(oc.GenericFields.Tags.Contains("one"), t)
AssertTrue(oc.GenericFields.Tags.Contains("two"), t)
AssertTrue(oc.GenericFields.Tags.Contains("three"), t)
}
func TestOperatorClaims_GetTags(t *testing.T) {
okp := createOperatorNKey(t)
opk := publicKey(okp, t)
oc := NewOperatorClaims(opk)
oc.Operator.Tags.Add("foo", "bar")
tags := oc.GetTags()
if len(tags) != 2 {
t.Fatal("expected 2 tags")
}
if tags[0] != "foo" {
t.Fatal("expected tag foo")
}
if tags[1] != "bar" {
t.Fatal("expected tag bar")
}
token, err := oc.Encode(okp)
if err != nil {
t.Fatal("error encoding")
}
oc, err = DecodeOperatorClaims(token)
if err != nil {
t.Fatal("error decoding")
}
tags = oc.GetTags()
if len(tags) != 2 {
t.Fatal("expected 2 tags")
}
if tags[0] != "foo" {
t.Fatal("expected tag foo")
}
if tags[1] != "bar" {
t.Fatal("expected tag bar")
}
}
func TestNewOperatorClaimSignerFn(t *testing.T) {
kp := createOperatorNKey(t)
ok := false
oc := NewOperatorClaims(publicKey(kp, t))
token, err := oc.EncodeWithSigner(kp, func(pub string, data []byte) ([]byte, error) {
ok = true
return kp.Sign(data)
})
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("expected ok to be true")
}
oc, err = DecodeOperatorClaims(token)
if err != nil {
t.Fatal("failed to decode", err)
}
vr := CreateValidationResults()
oc.Validate(vr)
if !vr.IsEmpty() {
t.Fatalf("claims validation should not have failed, got %+v", vr.Issues)
}
}
|