-
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.
Signed-off-by: Bipul Adhikari <[email protected]>
- Loading branch information
Showing
11 changed files
with
272 additions
and
21 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
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,21 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
func GetNamespace() (string, error) { | ||
namespaceFile := "/var/run/secrets/kubernetes.io/serviceaccount/namespace" | ||
file, err := os.Open(namespaceFile) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer file.Close() | ||
|
||
data, err := io.ReadAll(file) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(data), nil | ||
} |
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,151 @@ | ||
/* | ||
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" | ||
|
||
"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" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type tokenResolver struct { | ||
kubeclient client.Client | ||
namespace string | ||
serviceAccount string | ||
|
||
token string | ||
expiration metav1.Time | ||
} | ||
|
||
func WithServiceAccountToken(client client.Client, namespace, serviceAccount string) grpc.DialOption { | ||
tr := tokenResolver{ | ||
kubeclient: client, | ||
namespace: namespace, | ||
serviceAccount: serviceAccount, | ||
expiration: metav1.Now(), | ||
} | ||
|
||
return grpc.WithUnaryInterceptor(tr.addAuthorizationHeader) | ||
} | ||
|
||
func (tr *tokenResolver) addAuthorizationHeader(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
token, err := tr.getToken(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
authCtx := metadata.AppendToOutgoingContext(ctx, "Authorization", "Bearer "+token) | ||
return invoker(authCtx, method, req, reply, cc, opts...) | ||
} | ||
|
||
func (tr *tokenResolver) getToken(ctx context.Context) (string, error) { | ||
now := metav1.Now() | ||
if tr.expiration.Before(&now) { | ||
// token expired | ||
return tr.refreshToken(ctx) | ||
} | ||
|
||
return tr.token, nil | ||
} | ||
|
||
func (tr *tokenResolver) refreshToken(ctx context.Context) (string, error) { | ||
treq := &authv1.TokenRequest{ | ||
Spec: authv1.TokenRequestSpec{ | ||
Audiences: []string{"csi-addons"}, | ||
}, | ||
} | ||
|
||
err := tr.kubeclient.Create(ctx, treq) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
tr.token = treq.Status.Token | ||
tr.expiration = treq.Status.ExpirationTimestamp | ||
|
||
return tr.token, nil | ||
} | ||
|
||
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(kubeclient, ctx); err != nil { | ||
return nil, err | ||
} | ||
return handler(ctx, req) | ||
} | ||
} | ||
|
||
func authorizeConnection(kubeclient kubernetes.Clientset, ctx context.Context) error { | ||
|
||
md, ok := metadata.FromIncomingContext(ctx) | ||
if !ok { | ||
return status.Errorf(codes.Unauthenticated, "missing metadata") | ||
} | ||
|
||
authHeader, ok := md["authorization"] | ||
if !ok || len(authHeader) == 0 { | ||
return status.Errorf(codes.Unauthenticated, "missing authorization token") | ||
} | ||
|
||
token := authHeader[0] | ||
isValidated, err := validateBearerToken(token, kubeclient) | ||
if !isValidated || err != nil { | ||
return status.Errorf(codes.Unauthenticated, "invalid token") | ||
} | ||
return nil | ||
} | ||
|
||
func validateBearerToken(token string, kubeclient kubernetes.Clientset) (bool, error) { | ||
tokenReview := &authv1.TokenReview{ | ||
Spec: authv1.TokenReviewSpec{ | ||
Token: token, | ||
}, | ||
} | ||
result, err := kubeclient.AuthenticationV1().TokenReviews().Create(context.TODO(), tokenReview, metav1.CreateOptions{}) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to review token %v", err) | ||
} | ||
|
||
if result.Status.Authenticated { | ||
return true, nil | ||
} | ||
return false, nil | ||
} | ||
|
||
func GetServerCert() (string, error) { | ||
certFile := "/etc/tls/ca.crt" | ||
file, err := os.Open(certFile) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer file.Close() | ||
|
||
data, err := io.ReadAll(file) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(data), nil | ||
} |
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
Oops, something went wrong.