This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
/
authorization_test.go
178 lines (170 loc) · 4.87 KB
/
authorization_test.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
// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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 authorization contains the authorization module implementation.
package authorization
import (
"context"
"testing"
"github.com/google/keytransparency/impl/authentication"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/google/keytransparency/core/api/v1/keytransparency_go_proto"
authzpb "github.com/google/keytransparency/impl/authorization/authz_go_proto"
)
const (
testUser = "[email protected]"
l1 = "r1"
l2 = "r2"
l3 = "r3"
l4 = "r4"
l5 = "r5"
admin1 = "[email protected]"
admin2 = "[email protected]"
admin3 = "[email protected]"
admin4 = "[email protected]"
res1 = "directories/1"
res2 = "directories/2"
res3 = "directories/3"
res4 = "directories/4"
)
var authz = AuthzPolicy{
Policy: &authzpb.AuthorizationPolicy{
Roles: map[string]*authzpb.AuthorizationPolicy_Role{
l1: {
Principals: []string{admin1},
},
l2: {
Principals: []string{admin1, admin2},
},
l3: {
Principals: []string{admin3},
},
l4: {},
},
ResourceToRoleLabels: map[string]*authzpb.AuthorizationPolicy_RoleLabels{
res1: {
Labels: []string{l1, l2},
},
res2: {
Labels: []string{l3},
},
res3: {
Labels: []string{l4},
},
res4: {
Labels: []string{l5},
},
},
},
}
func TestIsAuthorized(t *testing.T) {
ctx := context.Background()
for _, tc := range []struct {
description string
ctx context.Context
directoryID string
userID string
wantCode codes.Code
}{
{
description: "self updating own profile",
ctx: authentication.WithOutgoingFakeAuth(ctx, testUser),
directoryID: "1",
userID: testUser,
},
{
description: "other accessing profile, authorized with one role",
ctx: authentication.WithOutgoingFakeAuth(ctx, admin2),
directoryID: "1",
userID: "",
},
{
description: "other accessing profile, authorized with multiple roles",
ctx: authentication.WithOutgoingFakeAuth(ctx, admin1),
directoryID: "1",
userID: "",
},
{
description: "other accessing profile, authorized second resource",
ctx: authentication.WithOutgoingFakeAuth(ctx, admin3),
directoryID: "2",
userID: "",
},
{
description: "not authorized, no resource label",
ctx: authentication.WithOutgoingFakeAuth(ctx, admin1),
directoryID: "5",
userID: "",
wantCode: codes.PermissionDenied,
},
{
description: "not authorized, no label_to_role defined",
ctx: authentication.WithOutgoingFakeAuth(ctx, admin1),
directoryID: "3",
userID: "",
wantCode: codes.PermissionDenied,
},
{
description: "not authorized, empty role definition",
ctx: authentication.WithOutgoingFakeAuth(ctx, admin1),
directoryID: "4",
userID: "",
wantCode: codes.PermissionDenied,
},
{
description: "not authorized principal",
ctx: authentication.WithOutgoingFakeAuth(ctx, admin4),
directoryID: "1",
userID: "",
wantCode: codes.PermissionDenied,
},
} {
t.Run(tc.description, func(t *testing.T) {
// Convert outgoing context to incoming context.
inCtx := metautils.ExtractOutgoing(tc.ctx).ToIncoming(ctx)
sctx, err := authentication.FakeAuthFunc(inCtx)
if err != nil {
t.Fatalf("FakeAuthFunc(): %v", err)
}
req := &pb.UpdateEntryRequest{
DirectoryId: tc.directoryID,
EntryUpdate: &pb.EntryUpdate{UserId: tc.userID},
}
err = authz.Authorize(sctx, req)
if got, want := status.Code(err), tc.wantCode; got != want {
t.Errorf("IsAuthorized(): %v, want %v", err, want)
}
})
}
}
func TestResouceLabel(t *testing.T) {
for _, tc := range []struct {
directoryID string
out string
wantCode codes.Code
}{
{directoryID: "1", out: "directories/1"},
{directoryID: "111", out: "directories/111"},
{directoryID: "1/1", wantCode: codes.InvalidArgument},
} {
label, err := resourceLabel(tc.directoryID)
if got, want := label, tc.out; got != want {
t.Errorf("resourceLabel(%v): %v, want %v", tc.directoryID, got, want)
}
if got, want := status.Code(err), tc.wantCode; got != want {
t.Errorf("resourceLabel(%v): %v, want %v", tc.directoryID, err, want)
}
}
}