-
Notifications
You must be signed in to change notification settings - Fork 0
/
authz.go
189 lines (162 loc) · 4.75 KB
/
authz.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
//go:generate $REPO_ROOT/bin/mixer_codegen.sh -a mixer/adapter/authzopa/config/config.proto -x "-s=false -n authzopa-adapter -t authorization"
package authzopa
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net"
stdjwt "github.com/dgrijalva/jwt-go"
"google.golang.org/grpc"
"istio.io/api/mixer/adapter/model/v1beta1"
policy "istio.io/api/policy/v1beta1"
"istio.io/istio/mixer/adapter/authzopa/config"
"istio.io/istio/mixer/pkg/status"
"istio.io/istio/mixer/template/authorization"
"istio.io/pkg/log"
pb "istio.io/istio/mixer/adapter/authzopa/pb/authz"
)
type (
// Server is basic server interface
Server interface {
Addr() string
Close() error
Run(shutdown chan error)
}
// AuthzAdapter supports authorization template.
AuthzAdapter struct {
listener net.Listener
server *grpc.Server
}
)
var _ authorization.HandleAuthorizationServiceServer = &AuthzAdapter{}
func decode(enc []byte) ([]byte, error) {
// create new buffer from enc
// you can also use bytes.NewBuffer(enc)
r := bytes.NewReader(enc)
// pass it to NewDecoder so that it can read data
dec := base64.NewDecoder(base64.RawURLEncoding, r)
// read decoded data from dec to res
res, err := ioutil.ReadAll(dec)
return res, err
}
func decodeValue(in interface{}) interface{} {
switch t := in.(type) {
case *policy.Value_StringValue:
return t.StringValue
case *policy.Value_Int64Value:
return t.Int64Value
case *policy.Value_DoubleValue:
return t.DoubleValue
default:
return fmt.Sprintf("%v", in)
}
}
func decodeValueMap(in map[string]*policy.Value) map[string]interface{} {
out := make(map[string]interface{}, len(in))
for k, v := range in {
out[k] = decodeValue(v.GetValue())
}
return out
}
func (s *AuthzAdapter) HandleAuthorization(ctx context.Context, req *authorization.HandleAuthorizationRequest) (*v1beta1.CheckResult, error) {
cfg := &config.Params{}
if req.AdapterConfig != nil {
if err := cfg.Unmarshal(req.AdapterConfig.Value); err != nil {
log.Errorf("error unmarshalling adapter config: %v", err)
return nil, err
}
}
props := decodeValueMap(req.Instance.Subject.Properties)
// get x jwt payload
xjp := props["x_jwt_playload"].(string)
if xjp == "" {
return &v1beta1.CheckResult{
Status: status.WithPermissionDenied("Unauthorized: x-jwt-playload Missing"),
}, nil
}
method := props["request_method"].(string)
path := props["request_path"].(string)
fmt.Println("xjp", xjp)
fmt.Println("method", method)
xp, err := decode([]byte(xjp))
if err != nil {
errMsg := fmt.Sprintf("Unauthorized: x-jwt-playload base64 decode fail... %v", err)
return &v1beta1.CheckResult{
Status: status.WithPermissionDenied(errMsg),
}, nil
}
var cl stdjwt.MapClaims
if err = json.NewDecoder(bytes.NewReader(xp)).Decode(&cl); err != nil {
return &v1beta1.CheckResult{
Status: status.WithPermissionDenied("Unauthorized: x-jwt-playload claim decode fail"),
}, nil
}
conn, err := grpc.DialContext(ctx, cfg.AuthzServerUrl, grpc.WithInsecure())
if err != nil {
return &v1beta1.CheckResult{
Status: status.WithPermissionDenied("Unauthorized: authz server connect fai"),
}, nil
}
ac := pb.NewAuthzClient(conn)
res, err := ac.IsAuthorizedReq(context.Background(), &pb.IsAuthorizedReqRequest{
User: cl["userId"].(string),
Path: path,
Method: method,
})
if err != nil {
errMsg := fmt.Sprintf("Unauthorized: authz server evaluate fail... %v", err)
return &v1beta1.CheckResult{
Status: status.WithPermissionDenied(errMsg),
}, nil
}
fmt.Println("res", res.IsAuthorized)
if res.IsAuthorized {
return &v1beta1.CheckResult{
Status: status.OK,
// ValidDuration: time.Second * 3,
// ValidUseCount: 3,
}, nil
} else {
return &v1beta1.CheckResult{
Status: status.WithPermissionDenied("Unauthorized: permission deny"),
}, nil
}
}
// Addr returns the listening address of the server
func (s *AuthzAdapter) Addr() string {
return s.listener.Addr().String()
}
// Run starts the server run
func (s *AuthzAdapter) Run(shutdown chan error) {
shutdown <- s.server.Serve(s.listener)
}
// Close gracefully shuts down the server; used for testing
func (s *AuthzAdapter) Close() error {
if s.server != nil {
s.server.GracefulStop()
}
if s.listener != nil {
_ = s.listener.Close()
}
return nil
}
// NewAuthzAdapter creates a new IBP adapter that listens at provided port.
func NewAuthzAdapter(addr string) (Server, error) {
if addr == "" {
addr = "0"
}
listener, err := net.Listen("tcp", fmt.Sprintf(":%s", addr))
if err != nil {
return nil, fmt.Errorf("unable to listen on socket: %v", err)
}
s := &AuthzAdapter{
listener: listener,
}
fmt.Printf("listening on \"%v\"\n", s.Addr())
s.server = grpc.NewServer()
authorization.RegisterHandleAuthorizationServiceServer(s.server, s)
return s, nil
}