(t *testing.T)
| 30 | ) |
| 31 | |
| 32 | func TestVerifySource(t *testing.T) { |
| 33 | ctx := log.NewContext(test.Context(), test.GetLogger(t)) |
| 34 | |
| 35 | a := assertions.New(t) |
| 36 | |
| 37 | key := []byte{0x2A, 0x9C, 0x2C, 0x3C, 0x2A, 0x9C, 0x2A, 0x9C, 0x2A, 0x9C, 0x2A, 0x9C, 0x2A, 0x9C, 0x2A, 0x9C} |
| 38 | |
| 39 | c, err := New(ctx, &Config{ |
| 40 | Keys: []string{ |
| 41 | hex.EncodeToString(key), |
| 42 | }, |
| 43 | }) |
| 44 | a.So(err, should.BeNil) |
| 45 | |
| 46 | t.Run("empty secret", func(t *testing.T) { |
| 47 | a := assertions.New(t) |
| 48 | |
| 49 | ctx := c.WithVerifiedSource(ctx) |
| 50 | a.So(errors.IsUnauthenticated(clusterauth.Authorized(ctx)), should.BeTrue) |
| 51 | }) |
| 52 | |
| 53 | t.Run("invalid secret type", func(t *testing.T) { |
| 54 | a := assertions.New(t) |
| 55 | |
| 56 | md := metadata.Pairs("authorization", "Basic invalid-secret") |
| 57 | ctx := metadata.NewIncomingContext(ctx, md) |
| 58 | |
| 59 | ctx = c.WithVerifiedSource(ctx) |
| 60 | a.So(errors.IsInvalidArgument(clusterauth.Authorized(ctx)), should.BeTrue) |
| 61 | }) |
| 62 | |
| 63 | t.Run("valid secret", func(t *testing.T) { |
| 64 | a := assertions.New(t) |
| 65 | |
| 66 | md := metadata.Pairs("authorization", fmt.Sprintf("ClusterKey %s", hex.EncodeToString(key))) |
| 67 | ctx := metadata.NewIncomingContext(ctx, md) |
| 68 | |
| 69 | ctx = c.WithVerifiedSource(ctx) |
| 70 | a.So(clusterauth.Authorized(ctx), should.BeNil) |
| 71 | }) |
| 72 | |
| 73 | t.Run("wrong secret", func(t *testing.T) { |
| 74 | a := assertions.New(t) |
| 75 | |
| 76 | md := metadata.Pairs("authorization", "ClusterKey 0102030405060708") |
| 77 | ctx := metadata.NewIncomingContext(ctx, md) |
| 78 | |
| 79 | ctx = c.WithVerifiedSource(ctx) |
| 80 | a.So(errors.IsPermissionDenied(clusterauth.Authorized(ctx)), should.BeTrue) |
| 81 | }) |
| 82 | } |
nothing calls this directly
no test coverage detected