-
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
1c5a1b6
commit cc0e23a
Showing
49 changed files
with
3,049 additions
and
13 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,97 @@ | ||
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/authorization" | ||
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 | ||
|
||
token, err := authorization.CreateToken() | ||
if err != nil { | ||
logger.Info("failed to create bearer token", "message", err) | ||
token = "" | ||
} | ||
|
||
if found.Data["token"] == nil || string(found.Data["token"]) != token { | ||
found.StringData = map[string]string{ | ||
"token": token, | ||
} | ||
modified = true | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
return found, modified, nil | ||
} | ||
|
||
func NewSecret(namespace string, owner metav1.OwnerReference) *corev1.Secret { | ||
token, err := authorization.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package authorization_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestAuthorization(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Authorization Suite") | ||
} |
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,55 @@ | ||
package authorization | ||
|
||
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) (any, 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.