-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for TLS encryption between manager and addons
Signed-off-by: Bipul Adhikari <[email protected]>
- Loading branch information
Showing
8 changed files
with
208 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
Copyright 2024 The Kubernetes-CSI-Addons Authors. | ||
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 token | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/status" | ||
authv1 "k8s.io/api/authentication/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
const bearerPrefix = "Bearer " | ||
|
||
func WithServiceAccountToken() grpc.DialOption { | ||
return grpc.WithUnaryInterceptor(addAuthorizationHeader) | ||
} | ||
|
||
func addAuthorizationHeader(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
token, err := getToken() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
authCtx := metadata.AppendToOutgoingContext(ctx, "Authorization", "Bearer "+token) | ||
return invoker(authCtx, method, req, reply, cc, opts...) | ||
} | ||
|
||
func getToken() (string, error) { | ||
return readFile("/var/run/secrets/kubernetes.io/serviceaccount/token") | ||
} | ||
|
||
func AuthorizationInterceptor(kubeclient kubernetes.Clientset) grpc.UnaryServerInterceptor { | ||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
if err := authorizeConnection(ctx, kubeclient); err != nil { | ||
return nil, err | ||
} | ||
return handler(ctx, req) | ||
} | ||
} | ||
|
||
func authorizeConnection(ctx context.Context, kubeclient kubernetes.Clientset) error { | ||
|
||
md, ok := metadata.FromIncomingContext(ctx) | ||
if !ok { | ||
return status.Error(codes.Unauthenticated, "missing metadata") | ||
} | ||
|
||
authHeader, ok := md["authorization"] | ||
if !ok || len(authHeader) == 0 { | ||
return status.Error(codes.Unauthenticated, "missing authorization token") | ||
} | ||
|
||
token := authHeader[0] | ||
isValidated, err := validateBearerToken(ctx, token, kubeclient) | ||
if !isValidated || (err != nil) { | ||
return status.Error(codes.Unauthenticated, fmt.Sprint("invalid token: %w", err)) | ||
} | ||
return nil | ||
} | ||
|
||
func parseToken(authHeader string) string { | ||
return strings.TrimPrefix(authHeader, bearerPrefix) | ||
} | ||
|
||
func validateBearerToken(ctx context.Context, token string, kubeclient kubernetes.Clientset) (bool, error) { | ||
tokenReview := &authv1.TokenReview{ | ||
Spec: authv1.TokenReviewSpec{ | ||
Token: parseToken(token), | ||
}, | ||
} | ||
result, err := kubeclient.AuthenticationV1().TokenReviews().Create(ctx, tokenReview, metav1.CreateOptions{}) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to review token %w", err) | ||
} | ||
|
||
if result.Status.Authenticated { | ||
return true, nil | ||
} | ||
return false, nil | ||
} | ||
|
||
func readFile(filePath string) (string, error) { | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer file.Close() | ||
|
||
data, err := io.ReadAll(file) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(data), nil | ||
} | ||
|
||
func GetCACert() (string, error) { | ||
return readFile("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters