|
| 1 | +package rpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/go-chassis/cari/pkg/errsvc" |
| 11 | + "github.com/go-chassis/cari/rbac" |
| 12 | + "github.com/go-chassis/go-chassis/v2/security/authr" |
| 13 | + "github.com/go-chassis/go-chassis/v2/server/restful" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "google.golang.org/grpc/metadata" |
| 16 | + |
| 17 | + "github.com/apache/servicecomb-service-center/syncer/config" |
| 18 | +) |
| 19 | + |
| 20 | +type testAuth struct{} |
| 21 | + |
| 22 | +func (testAuth) Login(ctx context.Context, user string, password string, opts ...authr.LoginOption) (string, error) { |
| 23 | + return "", nil |
| 24 | +} |
| 25 | + |
| 26 | +func (testAuth) Authenticate(ctx context.Context, token string) (interface{}, error) { |
| 27 | + var claim map[string]interface{} |
| 28 | + return claim, json.Unmarshal([]byte(token), &claim) |
| 29 | +} |
| 30 | + |
| 31 | +func Test_auth(t *testing.T) { |
| 32 | + // use the custom auth plugin |
| 33 | + authr.Install("test", func(opts *authr.Options) (authr.Authenticator, error) { |
| 34 | + return testAuth{}, nil |
| 35 | + }) |
| 36 | + assert.NoError(t, authr.Init(authr.WithPlugin("test"))) |
| 37 | + |
| 38 | + type args struct { |
| 39 | + ctx context.Context |
| 40 | + } |
| 41 | + tests := []struct { |
| 42 | + name string |
| 43 | + preDo func() |
| 44 | + args args |
| 45 | + wantErr assert.ErrorAssertionFunc |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "sync rbac disables", |
| 49 | + preDo: func() { |
| 50 | + config.SetConfig(config.Config{ |
| 51 | + Sync: &config.Sync{ |
| 52 | + RbacEnabled: false, |
| 53 | + }}) |
| 54 | + }, |
| 55 | + args: args{ |
| 56 | + ctx: context.Background(), // rbac disabled, empty ctx should pass the auth |
| 57 | + }, |
| 58 | + wantErr: assert.NoError, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "no header", |
| 62 | + preDo: func() { |
| 63 | + config.SetConfig(config.Config{ |
| 64 | + Sync: &config.Sync{ |
| 65 | + RbacEnabled: true, |
| 66 | + }}) |
| 67 | + }, |
| 68 | + args: args{ |
| 69 | + ctx: context.Background(), // rbac enabled, empty ctx should not pass the auth |
| 70 | + }, |
| 71 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 72 | + var errSvcErr *errsvc.Error |
| 73 | + ok := errors.As(err, &errSvcErr) |
| 74 | + assert.True(t, ok) |
| 75 | + |
| 76 | + return assert.Equal(t, rbac.ErrNoAuthHeader, errSvcErr.Code) |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "with header but no auth header", |
| 81 | + args: args{ |
| 82 | + ctx: metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{"fake": "fake"})), |
| 83 | + }, |
| 84 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 85 | + var errSvcErr *errsvc.Error |
| 86 | + ok := errors.As(err, &errSvcErr) |
| 87 | + assert.True(t, ok) |
| 88 | + |
| 89 | + return assert.Equal(t, rbac.ErrNoAuthHeader, errSvcErr.Code) |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "auth header format error", |
| 94 | + args: args{ |
| 95 | + ctx: metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{restful.HeaderAuth: "fake"})), |
| 96 | + }, |
| 97 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 98 | + return assert.Equal(t, rbac.ErrInvalidHeader, err) |
| 99 | + }, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "wrong account nor role", |
| 103 | + args: args{ |
| 104 | + ctx: metadata.NewIncomingContext(context.Background(), |
| 105 | + metadata.New(map[string]string{restful.HeaderAuth: `Bear {"account":"x","roles":["x"]}`})), |
| 106 | + }, |
| 107 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 108 | + return assert.Equal(t, errWrongAccountNorRole, err) |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "valid token", |
| 113 | + args: args{ |
| 114 | + ctx: metadata.NewIncomingContext(context.Background(), |
| 115 | + metadata.New(map[string]string{restful.HeaderAuth: `Bear {"account":"sync-user","roles":["sync-admin"]}`})), |
| 116 | + }, |
| 117 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 118 | + return assert.NoError(t, err) |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | + for _, tt := range tests { |
| 123 | + t.Run(tt.name, func(t *testing.T) { |
| 124 | + if tt.preDo != nil { |
| 125 | + tt.preDo() |
| 126 | + } |
| 127 | + tt.wantErr(t, auth(tt.args.ctx), fmt.Sprintf("auth(%v)", tt.args.ctx)) |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments