/
fetcher.go
196 lines (173 loc) · 6.65 KB
/
fetcher.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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 rights
import (
"context"
"go.thethings.network/lorawan-stack/pkg/errors"
"go.thethings.network/lorawan-stack/pkg/rpcmetadata"
"go.thethings.network/lorawan-stack/pkg/ttnpb"
"google.golang.org/grpc"
)
// Fetcher interface for rights fetching.
type Fetcher interface {
ApplicationRights(context.Context, ttnpb.ApplicationIdentifiers) (*ttnpb.Rights, error)
ClientRights(context.Context, ttnpb.ClientIdentifiers) (*ttnpb.Rights, error)
GatewayRights(context.Context, ttnpb.GatewayIdentifiers) (*ttnpb.Rights, error)
OrganizationRights(context.Context, ttnpb.OrganizationIdentifiers) (*ttnpb.Rights, error)
UserRights(context.Context, ttnpb.UserIdentifiers) (*ttnpb.Rights, error)
}
// FetcherFunc is a function that implements the Fetcher interface.
//
// A FetcherFunc that returns all Application rights for any Application,
// would look like this:
//
// fetcher := rights.FetcherFunc(func(ctx context.Context, ids ttnpb.Identifiers) (*ttnpb.Rights, error) {
// rights := ttnpb.AllApplicationRights // Instead this usually comes from an identity server or a database.
// return &rights, nil
// })
//
type FetcherFunc func(ctx context.Context, ids ttnpb.Identifiers) (*ttnpb.Rights, error)
// ApplicationRights implements the Fetcher interface.
func (f FetcherFunc) ApplicationRights(ctx context.Context, ids ttnpb.ApplicationIdentifiers) (*ttnpb.Rights, error) {
rights, err := f(ctx, ids)
registerRightsFetch(ctx, "application", rights, err)
return rights, err
}
// ClientRights implements the Fetcher interface.
func (f FetcherFunc) ClientRights(ctx context.Context, ids ttnpb.ClientIdentifiers) (*ttnpb.Rights, error) {
rights, err := f(ctx, ids)
registerRightsFetch(ctx, "client", rights, err)
return rights, err
}
// GatewayRights implements the Fetcher interface.
func (f FetcherFunc) GatewayRights(ctx context.Context, ids ttnpb.GatewayIdentifiers) (*ttnpb.Rights, error) {
rights, err := f(ctx, ids)
registerRightsFetch(ctx, "gateway", rights, err)
return rights, err
}
// OrganizationRights implements the Fetcher interface.
func (f FetcherFunc) OrganizationRights(ctx context.Context, ids ttnpb.OrganizationIdentifiers) (*ttnpb.Rights, error) {
rights, err := f(ctx, ids)
registerRightsFetch(ctx, "organization", rights, err)
return rights, err
}
// UserRights implements the Fetcher interface.
func (f FetcherFunc) UserRights(ctx context.Context, ids ttnpb.UserIdentifiers) (*ttnpb.Rights, error) {
rights, err := f(ctx, ids)
registerRightsFetch(ctx, "user", rights, err)
return rights, err
}
type fetcherKeyType struct{}
var fetcherKey fetcherKeyType
// NewContextWithFetcher returns a new context with the given rights fetcher.
func NewContextWithFetcher(ctx context.Context, fetcher Fetcher) context.Context {
return context.WithValue(ctx, fetcherKey, fetcher)
}
func fetcherFromContext(ctx context.Context) (Fetcher, bool) {
if fetcher, ok := ctx.Value(fetcherKey).(Fetcher); ok {
return fetcher, true
}
return nil, false
}
var errNoFetcher = errors.DefineInternal("no_fetcher", "no fetcher found in context")
// NewAccessFetcher returns a new rights fetcher that fetches from the Access role returned by getConn.
// The allowInsecure argument indicates whether it's allowed to send credentials over connections without TLS.
func NewAccessFetcher(getConn func(ctx context.Context) *grpc.ClientConn, allowInsecure bool) Fetcher {
return &accessFetcher{getConn: getConn, allowInsecure: allowInsecure}
}
type accessFetcher struct {
getConn func(ctx context.Context) *grpc.ClientConn
allowInsecure bool
}
var errNoISConn = errors.DefineUnavailable("no_identity_server_conn", "no connection to Identity Server")
func (f accessFetcher) ApplicationRights(ctx context.Context, appID ttnpb.ApplicationIdentifiers) (*ttnpb.Rights, error) {
cc := f.getConn(ctx)
if cc == nil {
return nil, errNoISConn.New()
}
callOpt, err := rpcmetadata.WithForwardedAuth(ctx, f.allowInsecure)
if err != nil {
return nil, err
}
rights, err := ttnpb.NewApplicationAccessClient(cc).ListRights(ctx, &appID, callOpt)
registerRightsFetch(ctx, "application", rights, err)
if err != nil {
return nil, err
}
return rights, nil
}
func (f accessFetcher) ClientRights(ctx context.Context, clientID ttnpb.ClientIdentifiers) (*ttnpb.Rights, error) {
cc := f.getConn(ctx)
if cc == nil {
return nil, errNoISConn.New()
}
callOpt, err := rpcmetadata.WithForwardedAuth(ctx, f.allowInsecure)
if err != nil {
return nil, err
}
rights, err := ttnpb.NewClientAccessClient(cc).ListRights(ctx, &clientID, callOpt)
registerRightsFetch(ctx, "client", rights, err)
if err != nil {
return nil, err
}
return rights, nil
}
func (f accessFetcher) GatewayRights(ctx context.Context, gtwID ttnpb.GatewayIdentifiers) (*ttnpb.Rights, error) {
cc := f.getConn(ctx)
if cc == nil {
return nil, errNoISConn.New()
}
callOpt, err := rpcmetadata.WithForwardedAuth(ctx, f.allowInsecure)
if err != nil {
return nil, err
}
rights, err := ttnpb.NewGatewayAccessClient(cc).ListRights(ctx, >wID, callOpt)
registerRightsFetch(ctx, "gateway", rights, err)
if err != nil {
return nil, err
}
return rights, nil
}
func (f accessFetcher) OrganizationRights(ctx context.Context, orgID ttnpb.OrganizationIdentifiers) (*ttnpb.Rights, error) {
cc := f.getConn(ctx)
if cc == nil {
return nil, errNoISConn.New()
}
callOpt, err := rpcmetadata.WithForwardedAuth(ctx, f.allowInsecure)
if err != nil {
return nil, err
}
rights, err := ttnpb.NewOrganizationAccessClient(cc).ListRights(ctx, &orgID, callOpt)
registerRightsFetch(ctx, "organization", rights, err)
if err != nil {
return nil, err
}
return rights, nil
}
func (f accessFetcher) UserRights(ctx context.Context, userID ttnpb.UserIdentifiers) (*ttnpb.Rights, error) {
cc := f.getConn(ctx)
if cc == nil {
return nil, errNoISConn.New()
}
callOpt, err := rpcmetadata.WithForwardedAuth(ctx, f.allowInsecure)
if err != nil {
return nil, err
}
rights, err := ttnpb.NewUserAccessClient(cc).ListRights(ctx, &userID, callOpt)
registerRightsFetch(ctx, "user", rights, err)
if err != nil {
return nil, err
}
return rights, nil
}