Skip to content

Commit

Permalink
fix: liqoctl retrieve nonce only from tenant namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiolor committed Feb 5, 2025
1 parent 6025920 commit 74be854
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 34 deletions.
4 changes: 2 additions & 2 deletions cmd/liqoctl/cmd/unpeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ offloaded workloads to be rescheduled. The Identity and Tenant are respectively
removed from the consumer and provider clusters, and the networking between the
two clusters is destroyed.
The reverse peering, if any, is preserved, and the remote cluster can continue
The reverse peering, if any, is preserved, and the remote cluster can continue
offloading workloads to its virtual node representing the local cluster.
Examples:
Expand Down Expand Up @@ -66,7 +66,7 @@ func newUnpeerCommand(ctx context.Context, f *factory.Factory) *cobra.Command {

cmd.PersistentFlags().DurationVar(&options.Timeout, "timeout", 120*time.Second, "Timeout for unpeering completion")
cmd.PersistentFlags().BoolVar(&options.Wait, "wait", true, "Wait for resource to be deleted before returning")
cmd.PersistentFlags().BoolVar(&options.KeepNamespaces, "keep-namespaces", false, "Keep tenant namespaces after unpeering")
cmd.PersistentFlags().BoolVar(&options.DeleteNamespace, "delete-namespaces", false, "Delete the tenant namespace after unpeering")

options.LocalFactory.AddFlags(cmd.PersistentFlags(), cmd.RegisterFlagCompletionFunc)
options.RemoteFactory.AddFlags(cmd.PersistentFlags(), cmd.RegisterFlagCompletionFunc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (r *TenantReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
if authv1beta1.GetAuthzPolicyValue(tenant.Spec.AuthzPolicy) != authv1beta1.TolerateNoHandshake {
// get the nonce for the tenant

nonceSecret, err := getters.GetNonceSecretByClusterID(ctx, r.Client, clusterID)
nonceSecret, err := getters.GetNonceSecretByClusterID(ctx, r.Client, clusterID, corev1.NamespaceAll)
if err != nil {
klog.Errorf("Unable to get the nonce for the Tenant %q: %s", req.Name, err)
r.EventRecorder.Event(tenant, corev1.EventTypeWarning, "NonceNotFound", err.Error())
Expand Down
10 changes: 5 additions & 5 deletions pkg/liqo-controller-manager/authentication/utils/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func EnsureNonceSecret(ctx context.Context, cl client.Client,
// already a nonce secret in the tenant namespace.
func EnsureSignedNonceSecret(ctx context.Context, cl client.Client,
remoteClusterID liqov1beta1.ClusterID, tenantNamespace string, nonce *string) error {
nonceSecret, err := getters.GetSignedNonceSecretByClusterID(ctx, cl, remoteClusterID)
nonceSecret, err := getters.GetSignedNonceSecretByClusterID(ctx, cl, remoteClusterID, tenantNamespace)
switch {
case errors.IsNotFound(err):
// Secret not found. Create it given the provided nonce.
Expand Down Expand Up @@ -80,8 +80,8 @@ func EnsureSignedNonceSecret(ctx context.Context, cl client.Client,
}

// RetrieveNonce retrieves the nonce from the secret in the tenant namespace.
func RetrieveNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID) ([]byte, error) {
nonce, err := getters.GetNonceSecretByClusterID(ctx, cl, remoteClusterID)
func RetrieveNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID, tenantNs string) ([]byte, error) {
nonce, err := getters.GetNonceSecretByClusterID(ctx, cl, remoteClusterID, tenantNs)
if err != nil {
return nil, fmt.Errorf("unable to get nonce secret: %w", err)
}
Expand All @@ -90,8 +90,8 @@ func RetrieveNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1
}

// RetrieveSignedNonce retrieves the signed nonce from the secret in the tenant namespace.
func RetrieveSignedNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID) ([]byte, error) {
secret, err := getters.GetSignedNonceSecretByClusterID(ctx, cl, remoteClusterID)
func RetrieveSignedNonce(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID, tenantNs string) ([]byte, error) {
secret, err := getters.GetSignedNonceSecretByClusterID(ctx, cl, remoteClusterID, tenantNs)
if err != nil {
return nil, fmt.Errorf("unable to get signed nonce secret: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/liqoctl/authenticate/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ func (c *Cluster) EnsureNonce(ctx context.Context) ([]byte, error) {
s.Success("Nonce secret ensured")

// Wait for secret to be filled with the nonce.
if err := c.waiter.ForNonce(ctx, c.RemoteClusterID, false); err != nil {
if err := c.waiter.ForNonce(ctx, c.RemoteClusterID, c.TenantNamespace, false); err != nil {
return nil, err
}

// Retrieve nonce from secret.
s = c.local.Printer.StartSpinner("Retrieving nonce")
nonceValue, err := authutils.RetrieveNonce(ctx, c.local.CRClient, c.RemoteClusterID)
nonceValue, err := authutils.RetrieveNonce(ctx, c.local.CRClient, c.RemoteClusterID, c.TenantNamespace)
if err != nil {
s.Fail(fmt.Sprintf("Unable to retrieve nonce: %v", output.PrettyErr(err)))
return nil, err
Expand All @@ -135,13 +135,13 @@ func (c *Cluster) EnsureSignedNonce(ctx context.Context, nonce []byte) ([]byte,
s.Success("Signed nonce secret ensured")

// Wait for secret to be filled with the signed nonce.
if err := c.waiter.ForSignedNonce(ctx, c.RemoteClusterID, false); err != nil {
if err := c.waiter.ForSignedNonce(ctx, c.RemoteClusterID, false, c.TenantNamespace); err != nil {
return nil, err
}

// Retrieve signed nonce from secret.
s = c.local.Printer.StartSpinner("Retrieving signed nonce")
signedNonceValue, err := authutils.RetrieveSignedNonce(ctx, c.local.CRClient, c.RemoteClusterID)
signedNonceValue, err := authutils.RetrieveSignedNonce(ctx, c.local.CRClient, c.RemoteClusterID, c.TenantNamespace)
if err != nil {
s.Fail(fmt.Sprintf("Unable to retrieve signed nonce: %v", output.PrettyErr(err)))
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/liqoctl/rest/nonce/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ func (o *Options) handleCreate(ctx context.Context) error {
s.Success("Nonce created")

// Wait for secret to be filled with the nonce.
if err := waiter.ForNonce(ctx, o.clusterID.GetClusterID(), false); err != nil {
if err := waiter.ForNonce(ctx, o.clusterID.GetClusterID(), tenantNs.GetName(), false); err != nil {
return err
}

// Retrieve nonce from secret.
s = opts.Printer.StartSpinner("Retrieving nonce")
nonceValue, err := authutils.RetrieveNonce(ctx, opts.CRClient, o.clusterID.GetClusterID())
nonceValue, err := authutils.RetrieveNonce(ctx, opts.CRClient, o.clusterID.GetClusterID(), tenantNs.GetName())
if err != nil {
s.Fail(fmt.Sprintf("Unable to retrieve nonce: %v", output.PrettyErr(err)))
return err
Expand Down
3 changes: 2 additions & 1 deletion pkg/liqoctl/rest/nonce/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/runtime"

authutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/authentication/utils"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (o *Options) Get(ctx context.Context, options *rest.GetOptions) *cobra.Comm
func (o *Options) handleGet(ctx context.Context) error {
opts := o.getOptions

nonceValue, err := authutils.RetrieveNonce(ctx, opts.CRClient, o.clusterID.GetClusterID())
nonceValue, err := authutils.RetrieveNonce(ctx, opts.CRClient, o.clusterID.GetClusterID(), corev1.NamespaceAll)
if err != nil {
opts.Printer.CheckErr(fmt.Errorf("unable to retrieve nonce: %v", output.PrettyErr(err)))
return err
Expand Down
6 changes: 3 additions & 3 deletions pkg/liqoctl/rest/tenant/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const liqoctlGenerateConfigHelp = `Generate the Tenant resource to be applied on
This commands generates a Tenant filled with all the authentication parameters needed to authenticate with the remote cluster.
It signs the nonce provided by the remote cluster and generates the CSR.
The Nonce can be provided as a flag or it can be retrieved from the secret in the tenant namespace (if existing).
The Nonce can be provided as a flag or it can be retrieved from the secret in the tenant namespace (if existing).
Examples:
$ {{ .Executable }} generate tenant --remote-cluster-id remote-cluster-id`
Expand Down Expand Up @@ -98,13 +98,13 @@ func (o *Options) handleGenerate(ctx context.Context) error {
}

// Wait for secret to be filled with the signed nonce.
if err := waiter.ForSignedNonce(ctx, o.remoteClusterID.GetClusterID(), true); err != nil {
if err := waiter.ForSignedNonce(ctx, o.remoteClusterID.GetClusterID(), true, tenantNs.GetName()); err != nil {
opts.Printer.CheckErr(fmt.Errorf("unable to wait for nonce to be signed: %w", err))
return err
}

// Retrieve signed nonce from secret.
signedNonce, err := authutils.RetrieveSignedNonce(ctx, opts.CRClient, o.remoteClusterID.GetClusterID())
signedNonce, err := authutils.RetrieveSignedNonce(ctx, opts.CRClient, o.remoteClusterID.GetClusterID(), tenantNs.GetName())
if err != nil {
opts.Printer.CheckErr(fmt.Errorf("unable to retrieve signed nonce: %w", err))
return err
Expand Down
12 changes: 6 additions & 6 deletions pkg/liqoctl/unpeer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type Options struct {
RemoteFactory *factory.Factory
waiter *wait.Waiter

Timeout time.Duration
Wait bool
KeepNamespaces bool
Timeout time.Duration
Wait bool
DeleteNamespace bool

consumerClusterID liqov1beta1.ClusterID
providerClusterID liqov1beta1.ClusterID
Expand Down Expand Up @@ -85,8 +85,8 @@ func (o *Options) RunUnpeer(ctx context.Context) error {
o.LocalFactory.Printer.CheckErr(fmt.Errorf("an error occurred while checking bidirectional peering: %v", output.PrettyErr(err)))
return err
}
if bidirectional && !o.KeepNamespaces {
err = fmt.Errorf("cannot unpeer bidirectional peering without keeping namespaces, please set the --keep-namespaces flag")
if bidirectional && o.DeleteNamespace {
err = fmt.Errorf("cannot delete the tenant namespace when a bidirectional is enabled, please remote the --delete-namespaces flag")
o.LocalFactory.Printer.CheckErr(err)
return err
}
Expand All @@ -111,7 +111,7 @@ func (o *Options) RunUnpeer(ctx context.Context) error {
}
}

if !o.KeepNamespaces {
if o.DeleteNamespace {
consumer := unauthenticate.NewCluster(o.LocalFactory)
provider := unauthenticate.NewCluster(o.RemoteFactory)

Expand Down
8 changes: 4 additions & 4 deletions pkg/liqoctl/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ func (w *Waiter) ForConnectionEstablished(ctx context.Context, conn *networkingv
}

// ForNonce waits until the secret containing the nonce has been created or the timeout expires.
func (w *Waiter) ForNonce(ctx context.Context, remoteClusterID liqov1beta1.ClusterID, silent bool) error {
func (w *Waiter) ForNonce(ctx context.Context, remoteClusterID liqov1beta1.ClusterID, tenantNamespace string, silent bool) error {
var s *pterm.SpinnerPrinter

if !silent {
s = w.Printer.StartSpinner("Waiting for nonce to be generated")
}

err := wait.PollUntilContextCancel(ctx, 1*time.Second, true, func(ctx context.Context) (done bool, err error) {
secret, err := getters.GetNonceSecretByClusterID(ctx, w.CRClient, remoteClusterID)
secret, err := getters.GetNonceSecretByClusterID(ctx, w.CRClient, remoteClusterID, tenantNamespace)
if err != nil {
return false, client.IgnoreNotFound(err)
}
Expand All @@ -366,15 +366,15 @@ func (w *Waiter) ForNonce(ctx context.Context, remoteClusterID liqov1beta1.Clust
}

// ForSignedNonce waits until the signed nonce secret has been signed and returns the signature.
func (w *Waiter) ForSignedNonce(ctx context.Context, remoteClusterID liqov1beta1.ClusterID, silent bool) error {
func (w *Waiter) ForSignedNonce(ctx context.Context, remoteClusterID liqov1beta1.ClusterID, silent bool, tenantNs string) error {
var s *pterm.SpinnerPrinter

if !silent {
s = w.Printer.StartSpinner("Waiting for nonce to be signed")
}

err := wait.PollUntilContextCancel(ctx, 1*time.Second, true, func(ctx context.Context) (done bool, err error) {
secret, err := getters.GetSignedNonceSecretByClusterID(ctx, w.CRClient, remoteClusterID)
secret, err := getters.GetSignedNonceSecretByClusterID(ctx, w.CRClient, remoteClusterID, tenantNs)
if err != nil {
return false, client.IgnoreNotFound(err)
}
Expand Down
18 changes: 12 additions & 6 deletions pkg/utils/getters/k8sGetters.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ func ListNodesByClusterID(ctx context.Context, cl client.Client, clusterID liqov
}

// GetNonceSecretByClusterID returns the secret containing the nonce to be signed by the consumer cluster.
func GetNonceSecretByClusterID(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID) (*corev1.Secret, error) {
func GetNonceSecretByClusterID(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID,
tenantNs string) (*corev1.Secret, error) {
var secrets corev1.SecretList
if err := cl.List(ctx, &secrets, &client.ListOptions{
Namespace: tenantNs,
LabelSelector: labels.SelectorFromSet(map[string]string{
consts.RemoteClusterID: string(remoteClusterID),
consts.NonceSecretLabelKey: "true",
Expand All @@ -237,12 +239,16 @@ func GetNonceSecretByClusterID(ctx context.Context, cl client.Client, remoteClus
}

// GetSignedNonceSecretByClusterID returns the secret containing the nonce signed by the consumer cluster.
func GetSignedNonceSecretByClusterID(ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID) (*corev1.Secret, error) {
func GetSignedNonceSecretByClusterID(
ctx context.Context, cl client.Client, remoteClusterID liqov1beta1.ClusterID, tentantNs string) (*corev1.Secret, error) {
var secrets corev1.SecretList
if err := cl.List(ctx, &secrets, client.MatchingLabels{
consts.RemoteClusterID: string(remoteClusterID),
consts.SignedNonceSecretLabelKey: "true",
}); err != nil {
if err := cl.List(ctx, &secrets,
client.MatchingLabels{
consts.RemoteClusterID: string(remoteClusterID),
consts.SignedNonceSecretLabelKey: "true",
},
&client.ListOptions{Namespace: tentantNs},
); err != nil {
return nil, err
}

Expand Down

0 comments on commit 74be854

Please sign in to comment.