Arnavion / k8s-openapi

Rust definitions of the resource types in the Kubernetes client API
Apache License 2.0
386 stars 41 forks source link

Struggling with TokenReview::create_token_review() #111

Closed crusty-dave closed 3 years ago

crusty-dave commented 3 years ago

I have looked for an example of usage, but I cannot find one anywhere.

I just have a very simple task for which I need this API, I would like to verify the service token received from a different service. I believe that I need to provide my service token to make the request.

I am struggling with how to convert AuthLayer into a form that pp.field_manager.with_header() will take:

        let mut body = api::TokenReview::default();
        body.spec.token.insert(token.to_string());

        loop {
            let config_result = Config::infer().await;
            let config = if let Ok(config) = config_result {
                config.clone()
            } else {
                break;
            };

            let client = match Client::try_default().await {
                Ok(client) => client,
                Err(e) => {
                    error!("{} failed to create default client {:?}", FN_NAME, e);
                    break;
                }
            };
            let mut pp = PostParams::default();
            let layer_opt = match config.auth_layer() {
                Ok(layer) => layer,
                Err(e) => {
                    error!("{} failed to get auth layer {:?}", FN_NAME, e);
                    break;
                }
            };
            if let Some(layer) = layer_opt {
                let value: HeaderValue = layer.into();
                pp.field_manager.with_header(header::AUTHORIZATION, value);
            }

...
        break;
    }
error[E0277]: the trait bound `HeaderValue: From<AuthLayer>` is not satisfied
  --> validator\src\k8s_service.rs:52:48
   |
52 |                 let value: HeaderValue = layer.into();
   |                                                ^^^^ the trait `From<AuthLayer>` is not implemented for `HeaderValue`
   |
   = help: the following implementations were found:
             <HeaderValue as From<&'a HeaderValue>>
             <HeaderValue as From<HeaderName>>
             <HeaderValue as From<i16>>
             <HeaderValue as From<i32>>
           and 6 others
   = note: required because of the requirements on the impl of `Into<HeaderValue>` for `AuthLayer`

Perhaps this is the wrong approach?

Thanks in advance, -Dave

crusty-dave commented 3 years ago

Actually, I believe that I just figured it out, haven't tested yet, but it compiles:

            let layer_opt = match config.auth_layer() {
                Ok(layer) => layer,
                Err(e) => {
                    error!("{} failed to get auth layer {:?}", FN_NAME, e);
                    break;
                }
            };
            let service = ServiceBuilder::new()
                .layer(config.base_uri_layer())
                .option_layer(layer_opt)
                .service(hyper::Client::new());
            let client = Client::new(service, config.default_namespace);
Arnavion commented 3 years ago

I think you meant to file this at https://github.com/kube-rs/kube-rs/issues ?

crusty-dave commented 3 years ago

@Arnavion - sorry, I wasn't sure which one. Even though the above compiled, I had tokio runtime issue when I tried to run it. So I started backing off versions.

I am trying this set, but the above code no longer compiles... sigh... kube = {registry = "crates-io-mirror", version="0.45.0", optional = true} k8s-openapi = {registry = "crates-io-mirror", version="0.10.0", default-features = false, features = ["api", "v1_19"], optional = true} tower-http = {registry = "crates-io-mirror", version="0.1", optional = true} tower = {registry = "crates-io-mirror", version="0.4", optional = true} hyper = {registry = "crates-io-mirror", version="0.14", features = ["client"], optional = true} hyper-tls = {registry = "crates-io-mirror", version="0.5", optional = true}