|
| 1 | +/* |
| 2 | + * Copyright 2023 CloudWeGo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package auth |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/cloudwego/kitex/pkg/klog" |
| 25 | + "github.com/cloudwego/kitex/pkg/remote" |
| 26 | + "github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/metadata" |
| 27 | + "github.com/cloudwego/kitex/pkg/rpcinfo" |
| 28 | + "github.com/cloudwego/kitex/transport" |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + jwtTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" // token path in the pod. |
| 33 | + |
| 34 | + jwtTokenKey = "Authorization" // Istiod gets the jwt from meta using this key. |
| 35 | + |
| 36 | + // If Istio is deployed in a different cluster, set this env as the cluster id of this service. |
| 37 | + // Usually, we can get the value using "kubectl config get-clusters". |
| 38 | + clusterIDEnvKey = "ISTIO_META_CLUSTER_ID" |
| 39 | + |
| 40 | + clusterIDMetadataKey = "clusterid" // Istiod retrieves clusterid and use it for auth of JWT. |
| 41 | +) |
| 42 | + |
| 43 | +var ( |
| 44 | + clusterID string |
| 45 | + jwtToken string |
| 46 | +) |
| 47 | + |
| 48 | +func init() { |
| 49 | + // init clusterID |
| 50 | + clusterID = os.Getenv(clusterIDEnvKey) |
| 51 | + // watch jwtToken file |
| 52 | + if token, err := getJWTToken(); err != nil { |
| 53 | + klog.Warnf("[XDS] Auth, getJWTToken error=%s. Ignore this log if not deploying on multiple clusters.\n", err.Error()) |
| 54 | + } else { |
| 55 | + jwtToken = token |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// ClientHTTP2JwtHandler is used to set jwt token to http2 header |
| 60 | +var ClientHTTP2JwtHandler = &clientHTTP2JwtHandler{} |
| 61 | + |
| 62 | +type clientHTTP2JwtHandler struct{} |
| 63 | + |
| 64 | +var ( |
| 65 | + _ remote.MetaHandler = ClientHTTP2JwtHandler |
| 66 | + _ remote.StreamingMetaHandler = ClientHTTP2JwtHandler |
| 67 | +) |
| 68 | + |
| 69 | +func (*clientHTTP2JwtHandler) OnConnectStream(ctx context.Context) (context.Context, error) { |
| 70 | + ri := rpcinfo.GetRPCInfo(ctx) |
| 71 | + if !isGRPC(ri) { |
| 72 | + return ctx, nil |
| 73 | + } |
| 74 | + var md metadata.MD |
| 75 | + md, ok := metadata.FromOutgoingContext(ctx) |
| 76 | + if !ok { |
| 77 | + md = metadata.MD{} |
| 78 | + } |
| 79 | + // Set JWT and clusterID for auth |
| 80 | + md.Set(jwtTokenKey, jwtTokenValueFmt(jwtToken)) |
| 81 | + md.Set(clusterIDMetadataKey, clusterID) |
| 82 | + return metadata.NewOutgoingContext(ctx, md), nil |
| 83 | +} |
| 84 | + |
| 85 | +func (*clientHTTP2JwtHandler) OnReadStream(ctx context.Context) (context.Context, error) { |
| 86 | + return ctx, nil |
| 87 | +} |
| 88 | + |
| 89 | +func (ch *clientHTTP2JwtHandler) WriteMeta(ctx context.Context, msg remote.Message) (context.Context, error) { |
| 90 | + return ctx, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (ch *clientHTTP2JwtHandler) ReadMeta(ctx context.Context, msg remote.Message) (context.Context, error) { |
| 94 | + return ctx, nil |
| 95 | +} |
| 96 | + |
| 97 | +func isGRPC(ri rpcinfo.RPCInfo) bool { |
| 98 | + return ri.Config().TransportProtocol()&transport.GRPC == transport.GRPC |
| 99 | +} |
| 100 | + |
| 101 | +var jwtTokenValueFmt = func(jwtToken string) string { |
| 102 | + return fmt.Sprintf("Bearer %s", jwtToken) |
| 103 | +} |
| 104 | + |
| 105 | +func getJWTToken() (string, error) { |
| 106 | + saToken := jwtTokenPath |
| 107 | + |
| 108 | + token, err := os.ReadFile(saToken) |
| 109 | + if err != nil { |
| 110 | + return "", err |
| 111 | + } |
| 112 | + |
| 113 | + return string(token), nil |
| 114 | +} |
0 commit comments