func (a AuthService) AuthHandler(w http.ResponseWriter, r *http.Request) {
	authRequestsCounter.Inc()
	if r.Method == "GET" {
		vars := mux.Vars(r)
		pod := podData{
			name:      vars["pod"],
			namespace: vars["namespace"],
		}
		if pod.name == "" || pod.namespace == "" {
			klog.InfoS("failed to parse url parameters", "pod", pod.name, "namespace", pod.namespace)
			http.Error(w, "", http.StatusBadRequest)
			authRequestsFailures.Inc()
			return
		}
		err := authorize(a.kubeclient, pod)
		if err != nil {
			klog.ErrorS(err, "failed to authorize request", "pod", pod.name, "namespace", pod.namespace)
			http.Error(w, "", http.StatusForbidden)
			authRequestsFailures.Inc()
			return
		}
		w.Header().Set("Content-Type", "application/json; charset=UTF-8")
		w.WriteHeader(http.StatusOK)
		if err := json.NewEncoder(w).Encode(a.credentials); err != nil {
			klog.ErrorS(err, "failed to json encode token", "pod", pod.name, "namespace", pod.namespace)
			http.Error(w, err.Error(), http.StatusInternalServerError)
		} else {
			klog.InfoS("served oauth token", "pod", pod.name, "namespace", pod.namespace)
		}
	} else {
		authRequestsFailures.Inc()
		klog.InfoS("invalid request method")
		http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
	}
}
AuthHandler handles authentiction requests to the Auth Service
package auth
import (
	"context"
	"crypto/tls"
	"crypto/x509"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"strings"
	"time"
	"github.com/SparebankenVest/azure-key-vault-to-kubernetes/pkg/azure/credentialprovider"
	"github.com/gorilla/mux"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
	"github.com/spf13/viper"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/client-go/kubernetes"
	"k8s.io/klog/v2"
)
type AuthService struct {
	kubeclient  kubernetes.Interface
	credentials credentialprovider.Credentials
	caCert      []byte
	caKey       []byte
}
func fileExists(filename string) bool {
	info, err := os.Stat(filename)
	if os.IsNotExist(err) {
		return false
	}
	return !info.IsDir()
}
func NewAuthService(kubeclient kubernetes.Interface, credentials credentialprovider.Credentials) (*AuthService, error) {
	caCertDir := viper.GetString("ca_cert_dir")
	if caCertDir == "" {
		klog.InfoS("missing env var - must exist to use auth service", "env", "CA_CERT_DIR")
		return nil, fmt.Errorf("no ca cert directory found")
	}
	caCertFile := filepath.Join(caCertDir, "tls.crt")
	caKeyFile := filepath.Join(caCertDir, "tls.key")
	klog.V(4).InfoS("auth service ca cert", "file", caCertFile)
	klog.V(4).InfoS("auth service ca key", "file", caKeyFile)
	if !fileExists(caCertFile) {
		klog.InfoS("file does not exist", "file", caCertFile)
		return nil, fmt.Errorf("file does not exist")
	}
	if !fileExists(caKeyFile) {
		klog.InfoS("file does not exist", "file", caKeyFile)
		return nil, fmt.Errorf("file does not exist")
	}
	var err error
	caCert, err := ioutil.ReadFile(caCertFile)
	if err != nil {
		klog.ErrorS(err, "failed to read pem file for ca cert", "file", caCertFile)
		return nil, err
	}
	if len(caCert) == 0 {
		klog.InfoS("file is empty ", "file", caCertFile)
		return nil, fmt.Errorf("file %s is empty", caCertFile)
	}
	caKey, err := ioutil.ReadFile(caKeyFile)
	if err != nil {
		klog.ErrorS(err, "failed to read pem file for ca key", "file", caKeyFile)
		return nil, err
	}
	if len(caKey) == 0 {
		klog.InfoS("file is empty ", "file", caKeyFile)
		return nil, fmt.Errorf("file %s is empty", caKeyFile)
	}
	return &AuthService{
		kubeclient:  kubeclient,
		credentials: credentials,
		caCert:      caCert,
		caKey:       caKey,
	}, nil
}
var (
	authRequestsCounter = promauto.NewCounter(prometheus.CounterOpts{
		Name: "akv2k8s_auth_requests_total",
		Help: "The total number of successful auth requests",
	})
	authRequestsFailures = promauto.NewCounter(prometheus.CounterOpts{
		Name: "akv2k8s_auth_requests_failed_total",
		Help: "The total number failed auth requests",
	})
	authValidationCounter = promauto.NewCounter(prometheus.CounterOpts{
		Name: "akv2k8s_auth_validations_total",
		Help: "The total number of successful auth validations",
	})
	authValidationFailures = promauto.NewCounter(prometheus.CounterOpts{
		Name: "akv2k8s_auth_validations_failed_total",
		Help: "The total number of failed auth validations",
	})
)