Skip to content

Invalid url in GetServiceAccountIssuerOpenIDConfigurationAsync #1340

Closed

Description

@molszews

Describe the bug
Calling GetServiceAccountIssuerOpenIDConfigurationAsync results in url request to /.well-known/openid-configuration/ path which is rejected by k8s API (403). I've noticed that removing trailing / from path, i.e. using /.well-known/openid-configuration is accepted by k8s API and responds with correct data (tested with curl and manually issuing http request from container).

edit: I think the same applies to GetServiceAccountIssuerOpenIDKeysetAsync method
edit2: looks like return type of the method (string) is invalid as well as it causes deserialization issues:

'System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
 ---> System.InvalidOperationException: Cannot get the value of a token type 'StartObject' as a string.

Kubernetes C# SDK Client Version
11.0.36

Server Kubernetes Version
1.26.3

Dotnet Runtime Version
.net7

To Reproduce
Steps to reproduce the behavior:

var config = KubernetesClientConfiguration.InClusterConfig();
var client = new Kubernetes(config);
            
var openIdConnectConfigurationRaw = await client.GetServiceAccountIssuerOpenIDConfigurationAsync(default);

Expected behavior
openIdConnectConfigurationRaw should contain JSON document contents.

Where do you run your app with Kubernetes SDK (please complete the following information):

  • OS: Linux
  • Environment: container
  • Cloud: Azure, AKS

Additional context
Sample stack trace:

2023-07-13 09:00:38.689Z k8s.Autorest.HttpOperationException: Operation returned an invalid status code 'Forbidden', response body {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"forbidden: User \"system:serviceaccount:portalpr10490:portal-users-service-sa\" cannot get path \"/.well-known/openid-configuration/\"","reason":"Forbidden","details":{},"code":403}
2023-07-13 09:00:38.689Z 
2023-07-13 09:00:38.689Z    at k8s.Kubernetes.SendRequestRaw(String requestContent, HttpRequestMessage httpRequest, CancellationToken cancellationToken)
2023-07-13 09:00:38.689Z    at k8s.AbstractKubernetes.k8s.IWellKnownOperations.GetServiceAccountIssuerOpenIDConfigurationWithHttpMessagesAsync(IReadOnlyDictionary`2 customHeaders, CancellationToken cancellationToken)
2023-07-13 09:00:38.689Z    at k8s.WellKnownOperationsExtensions.GetServiceAccountIssuerOpenIDConfigurationAsync(IWellKnownOperations operations, CancellationToken cancellationToken)
2023-07-13 09:00:38.689Z    at Portal.Users.Api.KubernetesJwksExtension.KubernetesConfigurationRetriever.GetConfigurationAsync(String address, IDocumentRetriever retriever, CancellationToken cancel)

Activity

  1. tg123 commented on Jul 13, 2023

    @tg123
    Member

    i double checked swagger.json
    it is /.well-known/openid-configuration/

    and you can test with

    kubectl get --raw /.well-known/openid-configuration/ with admin account

  2. molszews commented on Jul 13, 2023

    @molszews
    Author

    The command indeed works fine in both cases:

    ❯ k get --raw /.well-known/openid-configuration/
    {"issuer":"https://northeurope.oic.prod-aks.azure.com/db960ba0-2742-4c76-8c5f-xxxxxx/d6625b5b-c159-4583-bbba-xxxxxx/","jwks_uri":"https://\"lightkubedev-dns-xxxxxx.hcp.northeurope.azmk8s.io\":443/openid/v1/jwks","response_types_supported":["id_token"],"subject_types_supported":["public"],"id_token_signing_alg_values_supported":["RS256"]}%
    ❯ k get --raw /.well-known/openid-configuration
    {"issuer":"https://northeurope.oic.prod-aks.azure.com/db960ba0-2742-4c76-8c5f-xxxxxx/d6625b5b-c159-4583-bbba-xxxxxx/","jwks_uri":"https://\"lightkubedev-dns-xxxxxx.hcp.northeurope.azmk8s.io\":443/openid/v1/jwks","response_types_supported":["id_token"],"subject_types_supported":["public"],"id_token_signing_alg_values_supported":["RS256"]}%
    

    yet, when I spawn a container and run following commands I receive the same error as within dotnet app:

    export APISERVER=https://kubernetes.default.svc
    export SERVICEACCOUNT=/var/run/secrets/kubernetes.io/serviceaccount
    export CACERT=${SERVICEACCOUNT}/ca.crt
    export TOKEN=$(cat ${SERVICEACCOUNT}/token)
    

    with slash:

    curl --cacert ${CACERT} --header "Authorization: Bearer ${TOKEN}" -X GET ${APISERVER}/.well-known/openid-configuration/
    {
      "kind": "Status",
      "apiVersion": "v1",
      "metadata": {},
      "status": "Failure",
      "message": "forbidden: User \"system:serviceaccount:kube-system:default\" cannot get path \"/.well-known/openid-configuration/\"",
      "reason": "Forbidden",
      "details": {},
      "code": 403
    }
    

    without slash:

    curl --cacert ${CACERT} --header "Authorization: Bearer ${TOKEN}" -X GET ${APISERVER}/.well-known/openid-configuration
    {"issuer":"https://northeurope.oic.prod-aks.azure.com/db960ba0-2742-4c76-8c5f-xxxxxx/d6625b5b-c159-4583-bbba-xxxxxx/","jwks_uri":"https://\"lightkubedev-dns-xxxxxx.hcp.northeurope.azmk8s.io\":443/openid/v1/jwks","response_types_supported":["id_token"],"subject_types_supported":["public"],"id_token_signing_alg_values_supported":["RS256"]}
    
  3. tg123 commented on Jul 13, 2023

    @tg123
    Member

    link #117455

  4. tg123 commented on Jul 13, 2023

    @tg123
    Member

    still wonder how kubectl works, bc the server received all /

    kubectl get --raw /.well-known/openid-configuration////// -v8
    I0713 20:45:59.715966  166907 loader.go:373] Config loaded from file:  /home/bolian/.kube/config
    I0713 20:45:59.716800  166907 round_trippers.go:463] GET https://192.168.49.2:8443/.well-known/openid-configuration//////
    I0713 20:45:59.716822  166907 round_trippers.go:469] Request Headers:
    I0713 20:45:59.716829  166907 round_trippers.go:473]     Accept: application/json, */*
    I0713 20:45:59.716834  166907 round_trippers.go:473]     User-Agent: kubectl/v1.27.3 (linux/amd64) kubernetes/25b4e43
    I0713 20:45:59.716864  166907 cert_rotation.go:137] Starting client certificate rotation controller
    I0713 20:45:59.721364  166907 round_trippers.go:574] Response Status: 200 OK in 4 milliseconds
    I0713 20:45:59.721389  166907 round_trippers.go:577] Response Headers:
    I0713 20:45:59.721395  166907 round_trippers.go:580]     X-Kubernetes-Pf-Prioritylevel-Uid: 4cbe4927-656b-4e68-bff1-1c16f2f861f2
    I0713 20:45:59.721411  166907 round_trippers.go:580]     Content-Length: 238
    I0713 20:45:59.721415  166907 round_trippers.go:580]     Date: Thu, 13 Jul 2023 20:45:59 GMT
    I0713 20:45:59.721420  166907 round_trippers.go:580]     Audit-Id: e8953e2c-3e8a-46b9-bc1f-b698df6f1032
    I0713 20:45:59.721424  166907 round_trippers.go:580]     Cache-Control: public, max-age=3600
    I0713 20:45:59.721427  166907 round_trippers.go:580]     Content-Type: application/json
    I0713 20:45:59.721453  166907 round_trippers.go:580]     X-Kubernetes-Pf-Flowschema-Uid: b2739749-3b2e-4edf-9e23-5ecdab6bb67d
    {"issuer":"https://kubernetes.default.svc.cluster.local","jwks_uri":"https://192.168.49.2:8443/openid/v1/jwks","response_types_supported":["id_token"],"subject_types_supported":["public"],"id_token_signing_alg_values_supported":["RS256"]}
    
  5. molszews commented on Jul 14, 2023

    @molszews
    Author

    either way, can it be fixed (workarounded) in the client library itself or do we need to wait for #117455?

  6. tg123 commented on Jul 14, 2023

    @tg123
    Member

    please sent pr to swagger.json
    code will be generated automatically

  7. tg123 commented on Aug 31, 2023

    @tg123
    Member

    close as 1.28 merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions