/
tools.go
141 lines (119 loc) · 4.28 KB
/
tools.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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 v1alpha1
import (
"context"
"encoding/json"
"fmt"
"net"
"strings"
"github.com/apache/dubbo-admin/pkg/authority/jwt"
"google.golang.org/grpc/credentials"
"github.com/apache/dubbo-admin/pkg/authority/cert"
"github.com/apache/dubbo-admin/pkg/authority/config"
"github.com/apache/dubbo-admin/pkg/authority/k8s"
"github.com/apache/dubbo-admin/pkg/authority/rule"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
)
func ExactEndpoint(c context.Context, certStorage cert.Storage, options *config.Options, kubeClient k8s.Client) (*rule.Endpoint, error) {
if c == nil {
return nil, fmt.Errorf("context is nil")
}
p, ok := peer.FromContext(c)
if !ok {
return nil, fmt.Errorf("failed to get peer from context")
}
endpoint, endpointErr := tryFromHeader(c, certStorage, options, kubeClient)
if endpointErr == nil {
return endpoint, nil
}
endpoint, connectionErr := tryFromConnection(p)
if connectionErr == nil {
return endpoint, nil
}
if !options.IsTrustAnyone && connectionErr != nil {
return nil, fmt.Errorf("Failed to get endpoint from header: %s. Failed to get endpoint from connection: %s. RemoteAddr: %s",
endpointErr.Error(), connectionErr.Error(), p.Addr.String())
}
host, _, err := net.SplitHostPort(p.Addr.String())
if err != nil {
return nil, err
}
return &rule.Endpoint{
ID: p.Addr.String(),
Ips: []string{host},
}, nil
}
func tryFromHeader(c context.Context, certStorage cert.Storage, options *config.Options, kubeClient k8s.Client) (*rule.Endpoint, error) {
// TODO refactor as coreos/go-oidc
authorization := metadata.ValueFromIncomingContext(c, "authorization")
if len(authorization) != 1 {
return nil, fmt.Errorf("failed to get Authorization header from context")
}
if !strings.HasPrefix(authorization[0], "Bearer ") {
return nil, fmt.Errorf("failed to get Authorization header from context")
}
token := strings.ReplaceAll(authorization[0], "Bearer ", "")
authorizationTypes := metadata.ValueFromIncomingContext(c, "authorization-type")
authorizationType := "kubernetes"
if len(authorizationTypes) == 1 {
authorizationType = authorizationTypes[0]
}
if authorizationType == "dubbo-jwt" {
for _, c := range certStorage.GetTrustedCerts() {
claims, err := jwt.Verify(&c.PrivateKey.PublicKey, token)
if err != nil {
continue
}
endpoint := &rule.Endpoint{SpiffeID: claims.Subject}
err = json.Unmarshal([]byte(claims.Extensions), endpoint)
if err != nil {
continue
}
return endpoint, nil
}
return nil, fmt.Errorf("failed to verify Authorization header from dubbo-jwt")
}
if options.IsKubernetesConnected && options.EnableOIDCCheck {
endpoint, ok := kubeClient.VerifyServiceAccount(token, authorizationType)
if !ok {
return nil, fmt.Errorf("failed to verify Authorization header from kubernetes")
}
return endpoint, nil
}
return nil, fmt.Errorf("failed to verify Authorization header")
}
func tryFromConnection(p *peer.Peer) (*rule.Endpoint, error) {
if p.AuthInfo != nil && p.AuthInfo.AuthType() == "tls" {
tlsInfo, ok := p.AuthInfo.(credentials.TLSInfo)
if !ok {
return nil, fmt.Errorf("failed to get TLSInfo from peer")
}
host, _, err := net.SplitHostPort(p.Addr.String())
if err != nil {
return nil, err
}
if tlsInfo.SPIFFEID == nil {
return nil, fmt.Errorf("failed to get SPIFFE ID from peer")
}
return &rule.Endpoint{
ID: p.Addr.String(),
SpiffeID: tlsInfo.SPIFFEID.String(),
Ips: []string{host},
}, nil
}
return nil, fmt.Errorf("failed to get TLSInfo from peer")
}