Skip to content

Commit daa7464

Browse files
authored
update libs and use go 1.16 (#7)
1 parent 5ea3c81 commit daa7464

14 files changed

Lines changed: 420 additions & 242 deletions

File tree

api/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
to generate use a protoc version that is atleast >= 3.12.3 and make sure you have `protoc-gen-go` plugin installed at `${GOPATH}/bin/`
44

55
```bash
6-
protoc -I. -I /usr/local/include/google/protobuf/ --go_out=paths=source_relative,plugins="grpc:$PWD" --plugin=protoc-gen-go="${GOPATH}/bin/protoc-gen-go" api/*.proto
6+
protoc -I. -I /usr/local/include/google/protobuf/ --go_out "." --go_opt paths=source_relative --plugin=protoc-gen-go="${GOPATH}/bin/protoc-gen-go" api/*.proto
77
```

api/annotations.pb.go

Lines changed: 9 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/authz.pb.go

Lines changed: 6 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auth/context.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,61 @@ func (c contextKey) String() string {
1212
}
1313

1414
var (
15-
// contextKeyAuthtoken = contextKey("auth-token")
16-
contextKeyCurrentUser = contextKey("current-user")
15+
contextKeyAuthenticated = contextKey("authn")
16+
contextKeyAuthorized = contextKey("authz")
17+
18+
userName = "current-user"
19+
userClaims = "claims"
20+
userRoles = "roles"
1721
)
1822

1923
// CurrentUser for the request
2024
func CurrentUser(ctx context.Context) string {
2125

22-
if v, ok := ctx.Value(contextKeyCurrentUser).(string); ok {
23-
return v
26+
if v, ok := ctx.Value(contextKeyAuthenticated).(map[string]interface{}); ok {
27+
if u, ok := v[userName]; ok {
28+
return u.(string)
29+
}
2430
}
2531

2632
return Anonymous
2733
}
2834

29-
// newContext returns a new context with the given user attached
30-
func newContext(parent context.Context, user string) context.Context {
31-
return context.WithValue(parent, contextKeyCurrentUser, user)
35+
// CurrentUserClaims for the request
36+
func CurrentUserClaims(ctx context.Context) Claims {
37+
38+
if v, ok := ctx.Value(contextKeyAuthenticated).(map[string]interface{}); ok {
39+
if u, ok := v[userClaims]; ok {
40+
return u.(Claims)
41+
}
42+
}
43+
44+
return nil
45+
}
46+
47+
// CurrentUserRoles for the request
48+
func CurrentUserRoles(ctx context.Context) []string {
49+
50+
if v, ok := ctx.Value(contextKeyAuthorized).(map[string]interface{}); ok {
51+
if u, ok := v[userRoles]; ok {
52+
return u.([]string)
53+
}
54+
}
55+
56+
return nil
57+
}
58+
59+
// returns a new context with the given user and the claims attached
60+
func newAuthenticatedContext(parent context.Context, user string, cl Claims) context.Context {
61+
return context.WithValue(parent, contextKeyAuthenticated, map[string]interface{}{
62+
userName: user,
63+
userClaims: cl,
64+
})
65+
}
66+
67+
// returns a new context with the given authz result attached
68+
func newAuthorizedContext(parent context.Context, roles []string) context.Context {
69+
return context.WithValue(parent, contextKeyAuthorized, map[string]interface{}{
70+
userRoles: roles,
71+
})
3272
}

auth/runtime.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ func NewRuntime(ctx context.Context, options ...Option) (Runtime, error) {
111111
opt.apply(r)
112112
}
113113
if r.logger == nil {
114-
logger, err := log.NewNop()
115-
if err != nil {
116-
return nil, err
117-
}
118-
r.logger = logger
114+
r.logger = log.NewNop()
119115
}
120116

121117
verifier, err := newOIDCVerifier(ctx, r.issuer, r.aud)
@@ -199,8 +195,7 @@ func (r *runtime) Authorize(ctx context.Context, claims Claims, resource string,
199195

200196
ar, err = r.authorizer(ctx, authzReq)
201197

202-
// TODO copy certain information into context and cache the authz result with a TTL
203-
return ctx, ar, err
198+
return newAuthorizedContext(ctx, roles), ar, err
204199
}
205200

206201
func (r *runtime) Verify(ctx context.Context, token string) (context.Context, Claims, error) {
@@ -223,7 +218,7 @@ func (r *runtime) Verify(ctx context.Context, token string) (context.Context, Cl
223218
cl.AdditionalClaims = additionalClaims
224219
}
225220

226-
return newContext(ctx, r.idResolver(cl)), cl, nil
221+
return newAuthenticatedContext(ctx, r.idResolver(cl), cl), cl, nil
227222
}
228223

229224
func newOIDCVerifier(ctx context.Context, issuer, audience string) (*oidc.IDTokenVerifier, error) {

go.mod

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
module github.com/cnative/pkg
22

3-
go 1.15
3+
go 1.16
44

55
require (
66
contrib.go.opencensus.io/exporter/ocagent v0.7.0
7-
contrib.go.opencensus.io/exporter/prometheus v0.2.0
7+
contrib.go.opencensus.io/exporter/prometheus v0.3.0
8+
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 // indirect
89
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
910
github.com/coreos/go-oidc v2.2.1+incompatible
10-
github.com/golang/protobuf v1.4.3
11-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.0
12-
github.com/jhump/protoreflect v1.7.0
11+
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
12+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.4.0
13+
github.com/jhump/protoreflect v1.8.2
1314
github.com/pkg/errors v0.9.1
14-
github.com/pquerna/cachecontrol v0.0.0-20200921180117-858c6e7e6b7e // indirect
15-
github.com/prometheus/client_golang v1.8.0 // indirect
16-
github.com/prometheus/statsd_exporter v0.18.0 // indirect
17-
github.com/rollbar/rollbar-go v1.2.0
18-
github.com/sirupsen/logrus v1.7.0 // indirect
19-
github.com/soheilhy/cmux v0.1.4
20-
go.opencensus.io v0.22.5
21-
go.uber.org/multierr v1.6.0 // indirect
15+
github.com/pquerna/cachecontrol v0.1.0 // indirect
16+
github.com/prometheus/client_golang v1.10.0 // indirect
17+
github.com/prometheus/common v0.25.0 // indirect
18+
github.com/prometheus/statsd_exporter v0.20.2 // indirect
19+
github.com/rollbar/rollbar-go v1.4.0
20+
github.com/sirupsen/logrus v1.8.1 // indirect
21+
github.com/soheilhy/cmux v0.1.5
22+
go.opencensus.io v0.23.0
23+
go.uber.org/multierr v1.7.0 // indirect
2224
go.uber.org/zap v1.16.0
23-
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee
24-
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0
25-
golang.org/x/sync v0.0.0-20201008141435-b3e1573b7520 // indirect
26-
golang.org/x/sys v0.0.0-20201016160150-f659759dc4ca // indirect
27-
google.golang.org/api v0.33.0 // indirect
28-
google.golang.org/appengine v1.6.7 // indirect
29-
google.golang.org/genproto v0.0.0-20201015140912-32ed001d685c // indirect
30-
google.golang.org/grpc v1.33.0
31-
google.golang.org/grpc/examples v0.0.0-20200807164945-d3e3e7a46f57 // indirect
32-
google.golang.org/protobuf v1.25.0
25+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
26+
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
27+
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
28+
google.golang.org/api v0.47.0 // indirect
29+
google.golang.org/genproto v0.0.0-20210520160233-290a1ae68a05 // indirect
30+
google.golang.org/grpc v1.38.0
31+
google.golang.org/protobuf v1.26.0
3332
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
3433
)

0 commit comments

Comments
 (0)