-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Bearer token for /metrics endpoint
Signed-off-by: João Vilaça <[email protected]>
- Loading branch information
1 parent
8aa2052
commit 4a2f431
Showing
49 changed files
with
3,062 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package alerts | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/kubevirt/hyperconverged-cluster-operator/pkg/jwt" | ||
hcoutil "github.com/kubevirt/hyperconverged-cluster-operator/pkg/util" | ||
) | ||
|
||
const ( | ||
secretName = "hco-bearer-auth" | ||
) | ||
|
||
type secretReconciler struct { | ||
theSecret *corev1.Secret | ||
} | ||
|
||
func newSecretReconciler(namespace string, owner metav1.OwnerReference) *secretReconciler { | ||
return &secretReconciler{ | ||
theSecret: NewSecret(namespace, owner), | ||
} | ||
} | ||
|
||
func (r *secretReconciler) Kind() string { | ||
return "Secret" | ||
} | ||
|
||
func (r *secretReconciler) ResourceName() string { | ||
return secretName | ||
} | ||
|
||
func (r *secretReconciler) GetFullResource() client.Object { | ||
return r.theSecret.DeepCopy() | ||
} | ||
|
||
func (r *secretReconciler) EmptyObject() client.Object { | ||
return &corev1.Secret{} | ||
} | ||
|
||
func (r *secretReconciler) UpdateExistingResource(ctx context.Context, cl client.Client, resource client.Object, logger logr.Logger) (client.Object, bool, error) { | ||
found := resource.(*corev1.Secret) | ||
modified := false | ||
|
||
logger.Info("creating bearer token") | ||
token, err := jwt.CreateToken() | ||
if err != nil { | ||
logger.Info("failed to create bearer token", "message", err) | ||
token = "" | ||
} | ||
|
||
logger.Info("bearer token created", "token", token) | ||
|
||
if found.Data["token"] == nil || string(found.Data["token"]) != token { | ||
logger.Info("updating bearer token") | ||
found.StringData = map[string]string{ | ||
"token": token, | ||
} | ||
modified = true | ||
} | ||
|
||
logger.Info("updating common details") | ||
|
||
modified = updateCommonDetails(&r.theSecret.ObjectMeta, &found.ObjectMeta) || modified | ||
|
||
if modified { | ||
err := cl.Update(ctx, found) | ||
if err != nil { | ||
logger.Error(err, "failed to update the Secret") | ||
return nil, false, err | ||
} | ||
logger.Info("successfully updated the Secret") | ||
} | ||
|
||
return found, modified, nil | ||
} | ||
|
||
func NewSecret(namespace string, owner metav1.OwnerReference) *corev1.Secret { | ||
token, err := jwt.CreateToken() | ||
if err != nil { | ||
logger.Info("failed to create bearer token", "message", err) | ||
token = "" | ||
} | ||
|
||
return &corev1.Secret{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Secret", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secretName, | ||
Namespace: namespace, | ||
Labels: hcoutil.GetLabels(hcoutil.HyperConvergedName, hcoutil.AppComponentMonitoring), | ||
OwnerReferences: []metav1.OwnerReference{owner}, | ||
}, | ||
StringData: map[string]string{ | ||
"token": token, | ||
}, | ||
} | ||
} |
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
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,54 @@ | ||
package jwt | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
const ( | ||
tokenPathEnvVar = "KUBERNETES_SERVICE_TOKEN_PATH" | ||
defaultTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token" | ||
) | ||
|
||
func CreateToken() (string, error) { | ||
token := jwt.New(jwt.SigningMethodHS256) | ||
|
||
secretKey, err := getSecretKey() | ||
if err != nil { | ||
return "", fmt.Errorf("error getting secret key: %v", err) | ||
} | ||
|
||
tokenString, err := token.SignedString(secretKey) | ||
if err != nil { | ||
return "", fmt.Errorf("error signing token: %v", err) | ||
} | ||
|
||
return tokenString, nil | ||
} | ||
|
||
func ValidateToken(tokenString string) (bool, error) { | ||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { | ||
return getSecretKey() | ||
}) | ||
if err != nil { | ||
return false, fmt.Errorf("error parsing token: %v", err) | ||
} | ||
|
||
return token.Valid, nil | ||
} | ||
|
||
func getSecretKey() ([]byte, error) { | ||
tokenPath := os.Getenv(tokenPathEnvVar) | ||
if tokenPath == "" { | ||
tokenPath = defaultTokenPath | ||
} | ||
|
||
token, err := os.ReadFile(tokenPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading token: %v", err) | ||
} | ||
|
||
return token, nil | ||
} |
Oops, something went wrong.