Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add labels and annotations field to SubNamespace resource #19

Merged
merged 28 commits into from
Dec 6, 2021

Conversation

bells17
Copy link
Contributor

@bells17 bells17 commented Sep 23, 2021

#7

@bells17 bells17 marked this pull request as ready for review September 24, 2021 06:44
api/v1/subnamespace_types.go Show resolved Hide resolved
api/v1/subnamespace_types.go Outdated Show resolved Hide resolved
controllers/subnamespace_controller.go Outdated Show resolved Hide resolved
Copy link
Member

@zoetrope zoetrope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this feature should be used with caution, because tenant users may get privileges by setting labels or annotations.
For example, PodSecurity Admission will control policies using namespace labels.

So, please write a note in the documentation like the following:
If you are using something that controls permissions by labels or annotations, such as PodSecurity Admission, an administrator should set root namespaces appropriate labels and annotations.

@ymmt2005
Copy link
Member

@zoetrope
Accurate only sets the labels and annotations that are explicitly permitted
in the configuration file. So, admins have full control over what a tenant
user can do.

@ymmt2005
Copy link
Member

@bells17 @zoetrope
One thing that I haven't determined is that whether we should have a different set of
label/annotation keys for this feature than the keys for propagation.

What do you think?

@zoetrope
Copy link
Member

I understand.

I prefer a simple configuration.
I think we should not have the different set.

@bells17
Copy link
Contributor Author

bells17 commented Sep 28, 2021

I agree with @zoetrope.

@bells17
Copy link
Contributor Author

bells17 commented Sep 28, 2021

By the way, should we prioritize SubNamespace labels/annotations over parental Namespace labels/annotations if there have the same labels/annotations key?

@ymmt2005
Copy link
Member

OK, let's go with the current proposal.

@ymmt2005
Copy link
Member

@bells17

By the way, should we prioritize SubNamespace labels/annotations over parental Namespace labels/annotations if there have the same labels/annotations key?

Definitely not. And even if they would conflict, the parent Namespace labels/annotations win because they are propagated later on.

@bells17
Copy link
Contributor Author

bells17 commented Oct 6, 2021

Thank you @ymmt2005 @zoetrope .
I've corrected the points you commented on.
Would you review this PR again?

Copy link
Member

@ymmt2005 ymmt2005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a section for this feature to the user manual, too.

api/v1/subnamespace_types.go Outdated Show resolved Hide resolved
api/v1/subnamespace_types.go Outdated Show resolved Hide resolved
controllers/subnamespace_controller.go Outdated Show resolved Hide resolved
@bells17 bells17 requested a review from ymmt2005 October 13, 2021 06:41
controllers/subnamespace_controller.go Outdated Show resolved Hide resolved
@bells17 bells17 requested a review from ymmt2005 October 19, 2021 16:55
Copy link
Member

@ymmt2005 ymmt2005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approving, but could you add a section to the user manual for this feature?
@bells17

Copy link
Member

@ymmt2005 ymmt2005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bells17
@zoetrope and I have concluded that the current implementation is vulnerable
for sensitive labels/annotations like one for MetaLB address pools or one for
pod-security-admission policy.

As mentioned in #19 (comment) ,
we should have a separate set of label/annotation keys that SubNamespace can specify to
protect from malicious use.

Please update the design and fix the implementation.

@bells17 bells17 force-pushed the add-labels-and-annotations-field2 branch from 91aee10 to 8fac21a Compare November 1, 2021 08:03
@bells17
Copy link
Contributor Author

bells17 commented Nov 17, 2021

@ymmt2005 @zoetrope The following points have been updated:

  • SubNamespace spec.labels/spec.annotations is now propagated to descendants namespaces.
  • Added --labels and --annotations options to the kubectl-accurate sub create command.
  • Wrote docs.

Would you review these points?

subNSHandler(ev.ObjectOld, q)
},
DeleteFunc: func(ev event.DeleteEvent, q workqueue.RateLimitingInterface) {
subNSHandler(ev.Object, q)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary, because when SubNamespace is deleted, the target Namespace will be also deleted.

Copy link
Member

@zoetrope zoetrope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the code.
I think the following spec is fine.

  • SubNamespace spec.labels/spec.annotations is now propagated to descendants namespaces.
  • SubNamespace spec.labels/spec.annotations values are preferred more than parent namespace labels/annotations values.

zoetrope
zoetrope previously approved these changes Nov 19, 2021
Copy link
Member

@zoetrope zoetrope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -36,6 +38,8 @@ This effectively creates a namespace named NAME as a sub-namespace of NS.`,
},
}

cmd.Flags().StringToStringVar(&opts.labels, "labels", opts.labels, "the labels to be propagated to the sub-namespace")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an example of the usage to the description. e.g.,

Suggested change
cmd.Flags().StringToStringVar(&opts.labels, "labels", opts.labels, "the labels to be propagated to the sub-namespace")
cmd.Flags().StringToStringVar(&opts.labels, "labels", opts.labels, "the labels to be propagated to the sub-namespace. Example: a=b,c=d")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

@@ -36,6 +38,8 @@ This effectively creates a namespace named NAME as a sub-namespace of NS.`,
},
}

cmd.Flags().StringToStringVar(&opts.labels, "labels", opts.labels, "the labels to be propagated to the sub-namespace")
cmd.Flags().StringToStringVar(&opts.annotations, "annotations", opts.annotations, "the annotations to be propagated to the sub-namespace")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

Copy link
Member

@ymmt2005 ymmt2005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please validate the labels and annotations in SubNamespace spec field.
If a SubNamespace is created with an invalid key or value, the current implementation would fall into an infinite error loop.

Use v1validation.ValidateLabels to validate labels and validatoin.ValidateAnnotations to validate annotations.

ref. https://github.com/kubernetes/apimachinery/blob/v0.22.4/pkg/api/validation/objectmeta.go#L194-L195

Also, could you update this part of the user manual for this new feature?
https://cybozu-go.github.io/accurate/subnamespaces.html#creating-a-sub-namespace

docs/config.md Outdated Show resolved Hide resolved
@@ -92,6 +100,27 @@ func (r *NamespaceReconciler) propagateMeta(ctx context.Context, ns, parent *cor
ns.Annotations[k] = v
}
}

subNS := &accuratev1.SubNamespace{}
err := r.Get(ctx, types.NamespacedName{Name: ns.Name, Namespace: parent.Name}, subNS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the line 347, this method is called for a template instance namespace, not a sub-namespace.

I think this should look up a SubNamespace only when ns is a sub-namespace (i.e., it has accurate.cybozu.com/parent label).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

Co-authored-by: Yamamoto, Hirotaka <ymmt2005@gmail.com>
@bells17
Copy link
Contributor Author

bells17 commented Dec 1, 2021

Also, could you update this part of the user manual for this new feature?
https://cybozu-go.github.io/accurate/subnamespaces.html#creating-a-sub-namespace

I've already updated the user manual, but should I need to make additional updates?
https://github.com/cybozu-go/accurate/pull/19/files#diff-b394f5d0e85c3cd9c802a88109196f87c4f5047d04b50c0c557476474460adc4R88-R118

@ymmt2005
Copy link
Member

ymmt2005 commented Dec 1, 2021

@bells17

I've already updated the user manual, but should I need to make additional updates?

My bad. I overlooked that. It's enough, thank you.

@bells17
Copy link
Contributor Author

bells17 commented Dec 1, 2021

Please validate the labels and annotations in SubNamespace spec field.

added.

@bells17
Copy link
Contributor Author

bells17 commented Dec 1, 2021

@ymmt2005 (cc: @zoetrope) Thank you. I fixed points that are pointed on.

err := r.Get(ctx, types.NamespacedName{Name: ns.Name, Namespace: parent.Name}, subNS)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to get sub namespace %s/%s: %w", ns.Name, parent.Name, err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if apierrors.IsNotFound(err) is true?
In that case, subNS is invalid. Although the following code would work for such invalid data,
I want to check them and skip code using subNS.

@bells17 bells17 requested a review from ymmt2005 December 5, 2021 16:48
Copy link
Member

@ymmt2005 ymmt2005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ymmt2005 ymmt2005 merged commit 6773c56 into main Dec 6, 2021
@ymmt2005 ymmt2005 deleted the add-labels-and-annotations-field2 branch December 6, 2021 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants