kaiwalyakoparkar / pod-logger

A simple dashboard to view and simplify logs from your pod
MIT License
1 stars 1 forks source link

[Bug] K8s API authentication failing for pod #1

Open kaiwalyakoparkar opened 3 months ago

kaiwalyakoparkar commented 3 months ago

Project Idea: “Pod-logger” - Simple application which uses k8s api to fetch the logs from the pods in the cluster and show it on a simple ui dashboard

What I am trying to do:

Deploying podlogger as a container in a pod and trying to access k8s api through this container/pod. (So that I can get logs of any pod in cluster)

Things I tried:

Creating a sh file with all the environments and simple curl command

Contexts to understand the code:

https://github.com/kaiwalyakoparkar/pod-logger/blob/11f0f27c3804758596a6a3fe229c3b39ad358bdb/api/controllers/controller.go#L33 - Here I am trying to invoke the shell script using the api endpoint, still fails https://github.com/kaiwalyakoparkar/pod-logger/blob/11f0f27c3804758596a6a3fe229c3b39ad358bdb/api/controllers/controller.go#L53 - This endpoint was to see if there is an error with env loading but that doesn’t seem to be an issue, last I checked all envs I needed were loaded https://github.com/kaiwalyakoparkar/pod-logger/blob/11f0f27c3804758596a6a3fe229c3b39ad358bdb/api/controllers/controller.go#L13 - This is the curl command I am trying to run through this endpoint

Revolyssup commented 3 months ago
  1. Dont expand env variables in exec.Command like {TOKEN}. instead:
  2. This will still cause error because your TOKEN env variable is just the path and not the actual token. Read that file and pass it
    cacert := os.Getenv("CACERT")
    tokenPath := os.Getenv("TOKEN")
    apiserver := os.Getenv("APISERVER")
    tokenFile, err := os.Open(tokenPath)
    if err != nil {
        fmt.Println("could not open token file: ", err)
        return ""
    }
    tokenData, err := io.ReadAll(tokenFile)
    if err != nil {
        fmt.Println("could not read token file: ", err)
        return ""
    }
    output := string(out)
    cmd = exec.Command("curl", "--cacert", cacert, "--header", "Authorization: Bearer "+string(tokenData), apiserver+"/api")
kaiwalyakoparkar commented 3 months ago
  1. Dont expand env variables in exec.Command like {TOKEN}. instead:
  2. This will still cause error because your TOKEN env variable is just the path and not the actual token. Read that file and pass it
  cacert := os.Getenv("CACERT")
  tokenPath := os.Getenv("TOKEN")
  apiserver := os.Getenv("APISERVER")
  tokenFile, err := os.Open(tokenPath)
  if err != nil {
      fmt.Println("could not open token file: ", err)
      return ""
  }
  tokenData, err := io.ReadAll(tokenFile)
  if err != nil {
      fmt.Println("could not read token file: ", err)
      return ""
  }
  output := string(out)
  cmd = exec.Command("curl", "--cacert", cacert, "--header", "Authorization: Bearer "+string(tokenData), apiserver+"/api")

Thank you so much @Revolyssup I will try this :D