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

KEP-4633: Only allow anonymous auth for configured endpoints. #124917

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vinayakankugoyal
Copy link
Contributor

@vinayakankugoyal vinayakankugoyal commented May 16, 2024

What type of PR is this?

/kind feature

What this PR does / why we need it:

This PR adds the functionality of kubernetes/enhancements#4633 behind a feature gate. When the feature gate is enabled this PR allows a restricted anonymous auth mode that only allows a set of configured endpoints to be reached anonymously.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?

When the featuregate AnonymousAuthConfigurableEndpoints is enabled users can update the AuthenticationConfig file with endpoints for with anonymous requests are alllowed.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

- [KEP]: https://github.com/kubernetes/enhancements/issues/4633

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/apiserver area/cloudprovider area/kubelet sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cloud-provider Categorizes an issue or PR as relevant to SIG Cloud Provider. sig/node Categorizes an issue or PR as relevant to SIG Node. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels May 16, 2024
@vinayakankugoyal vinayakankugoyal marked this pull request as draft May 17, 2024 00:41
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 17, 2024
@vinayakankugoyal
Copy link
Contributor Author

Testing Notes:

  1. Created an image with the changes from my branch using
$ kind build node-image .
  1. Created a kind cluster with this node image
kind create cluster --image kindest/node:latest
  1. Get the IP for the API server
kubectl cluster-info                                                                                
Kubernetes control plane is running at https://127.0.0.1:33559
  1. Anonymous request to readyz should succeed
curl -k https://127.0.0.1:33559/readyz                                                                                   
ok%                                                                                                                                                                    
  1. But listing all pods should fail
curl -k https://127.0.0.1:33559/api/v1/pods                                                                               ok 
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "pods is forbidden: User \"system:anonymous\" cannot list resource \"pods\" in API group \"\" at the cluster scope",
  "reason": "Forbidden",
  "details": {
    "kind": "pods"
  },
  "code": 403
}%                                                   
  1. create a cluster role and cluster role binding that allows system:anonymous to view pods
kubectl create clusterrole --resource pods --verb get --verb list podview                                  
clusterrole.rbac.authorization.k8s.io/podview created

kubectl create clusterrolebinding --clusterrole=podview --user system:anonymous anon                      
clusterrolebinding.rbac.authorization.k8s.io/anon created
  1. now try to list pods again
curl -k https://127.0.0.1:33559/api/v1/pods | jq ".items[] | .metadata.name"                                              ok 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  134k    0  134k    0     0  10.3M      0 --:--:-- --:--:-- --:--:-- 10.9M
"coredns-7db6d8ff4d-c5272"
"coredns-7db6d8ff4d-qv8j2"
"etcd-kind-control-plane"
"kindnet-q7mfw"
"kube-apiserver-kind-control-plane"
"kube-controller-manager-kind-control-plane"
"kube-proxy-jjlnd"
"kube-scheduler-kind-control-plane"
"local-path-provisioner-c9cbdf4f9-5hlt8"
  1. now lets set --anonymous-auth-health-only=true in API server without setting the feature gate. This should fail.
docker exec -it $(docker container ls | grep kind-control-plane | awk '{print $1}') /bin/bash 

root@kind-control-plane:/# apt-get update

root@kind-control-plane:/# apt-get install vim

root@kind-control-plane:/# vim /etc/kubernetes/manifests/kube-apiserver.yaml 

I updated the kube-apiserver container with the argument --anonymous-auth-health-only=true

crictl ps -a --name  kube-apiserver
CONTAINER           IMAGE               CREATED             STATE               NAME                ATTEMPT             POD ID              POD
9de3341a21eb8       d9ccda0175e15       11 seconds ago      Exited              kube-apiserver      2                   e3d2f027fab3a       kube-apiserver-kind-control-plane
root@kind-control-plane:/# crictl logs 9de3341a21eb8
I0517 01:06:38.409000       1 options.go:225] external host was not specified, using 192.168.8.2
E0517 01:06:38.409476       1 run.go:72] "command failed" err="--anonymous-auth-health-only cannot be set to true unless HealthOnlyAnonymousAuth feature gate is enabled"

as expected it fails.

  1. now I updated the manifest to include the --feature-gates flag and enabled HealthOnlyAnonymousAuth feature. kube-apiserver is now healthy.
root@kind-control-plane:/# crictl ps -a --name  kube-apiserver
CONTAINER           IMAGE               CREATED             STATE               NAME                ATTEMPT             POD ID              POD
881b6d0919900       d9ccda0175e15       7 seconds ago       Running             kube-apiserver      0                   232a52b6522dd       kube-apiserver-kind-control-plane
  1. now lets check the readyz endpoint
curl -k https://127.0.0.1:33559/readyz                                                                           ok  10m 44s 
ok%                                        

anonymous request to readyz succeeded (public-info-viewer clusterrolebinding works)

  1. now lets check what bindings are there which have system:anonymous
kubectl get clusterrolebindings,rolebindings -o json | jq '.items[] | select(.subjects[]? | .name == "system:anonymous") | {kind: .kind, name:.metadata.name, namespace:.metadata.namespace, role:.roleRef.name, subjects:.subjects}'

{
  "kind": "ClusterRoleBinding",
  "name": "anon",
  "namespace": null,
  "role": "podview",
  "subjects": [
    {
      "apiGroup": "rbac.authorization.k8s.io",
      "kind": "User",
      "name": "system:anonymous"
    }
  ]
}
  1. according to rbac system:anonymous has role podview, lets check the permissions
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  creationTimestamp: "2024-05-17T00:57:23Z"
  name: podview
  resourceVersion: "679"
  uid: 5478cffe-f78f-48a3-b299-a6cc32f349f0
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list
  1. system:anonymous should be able to view pods according to rbac, but we have --anonymous-auth-health-only enabled so this request shouldn't authenticate
curl -k https://127.0.0.1:33559/api/v1/pods                                                                               
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "Unauthorized",
  "reason": "Unauthorized",
  "code": 401
}%                                            

@vinayakankugoyal vinayakankugoyal marked this pull request as ready for review May 17, 2024 01:21
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 17, 2024
@k8s-ci-robot k8s-ci-robot requested a review from aramase May 17, 2024 01:22
@vinayakankugoyal
Copy link
Contributor Author

/cc @liggitt

@k8s-ci-robot k8s-ci-robot requested a review from liggitt May 17, 2024 17:35
@vinayakankugoyal
Copy link
Contributor Author

Testing Notes 2:

Instead of docker execing and changing the kube-apiserver manifest I can just create a new kind cluster and use the kind cluster config to set extra args and feature-gates on the cluster. I used the following kind cluster config.

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
featureGates:
  "HealthOnlyAnonymousAuth": true
nodes:
- role: control-plane
  kubeadmConfigPatches:
  - |
    kind: ClusterConfiguration
    apiServer:
        extraArgs:
          anonymous-auth-health-only: "true"

@seans3
Copy link
Contributor

seans3 commented May 21, 2024

/triage accepted
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels May 21, 2024
@k8s-ci-robot k8s-ci-robot added the kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API label May 24, 2024
@k8s-triage-robot
Copy link

This PR may require API review.

If so, when the changes are ready, complete the pre-review checklist and request an API review.

Status of requested reviews is tracked in the API Review project.

@k8s-ci-robot k8s-ci-robot added area/test sig/testing Categorizes an issue or PR as relevant to SIG Testing. labels May 31, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: vinayakankugoyal
Once this PR has been reviewed and has the lgtm label, please assign andrewsykim, deads2k, mrunalp for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@vinayakankugoyal vinayakankugoyal changed the title KEP-4633: Allow health-only anonymous auth mode. KEP-4633: Only allow anonymous auth for configured endpoints. May 31, 2024
Signed-off-by: Vinayak Goyal <vinaygo@google.com>
@vinayakankugoyal
Copy link
Contributor Author

vinayakankugoyal commented Jun 1, 2024

Testing note 3:

After reworking the KEP a bit here is the new kubeadm config required to use this feature.

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
featureGates:
  # any feature gate can be enabled here with "Name": true
  # or disabled here with "Name": false
  # not all feature gates are tested, however
  "AnonymousAuthConfigurableEndpoints": true
  "StructuredAuthenticationConfiguration": true
nodes:
- role: control-plane
  extraMounts:
  - hostPath: /usr/local/google/home/vinaygo/go/src/k8s.io/kubernetes/authconfig.yaml
    containerPath: /etc/kubernetes/pki/authconfig.yaml
  kubeadmConfigPatches:
  - |
    kind: ClusterConfiguration
    apiServer:
        extraArgs:
          authentication-config: /etc/kubernetes/pki/authconfig.yaml

and here is how you need to setup the auth config file

apiVersion: apiserver.config.k8s.io/v1alpha1
kind: AuthenticationConfiguration
anonymous:
  enabled: true
  restrictToPaths:
  - /livez
  - /readyz
  - /healthz

@k8s-ci-robot
Copy link
Contributor

k8s-ci-robot commented Jun 1, 2024

@vinayakankugoyal: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-unit b0213b7 link true /test pull-kubernetes-unit
pull-kubernetes-verify b0213b7 link true /test pull-kubernetes-verify

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/apiserver area/cloudprovider area/kubelet area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/api-change Categorizes issue or PR as related to adding, removing, or otherwise changing an API kind/feature Categorizes issue or PR as related to a new feature. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/auth Categorizes an issue or PR as relevant to SIG Auth. sig/cloud-provider Categorizes an issue or PR as relevant to SIG Cloud Provider. sig/node Categorizes an issue or PR as relevant to SIG Node. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Status: In Review
Status: not-only-sig-node
Development

Successfully merging this pull request may close these issues.

None yet

4 participants