Skip to content

Commit

Permalink
Merge pull request #33 from iastewar/mock-token
Browse files Browse the repository at this point in the history
Added an env var to use a mock token instead of fetching the real one
  • Loading branch information
spowelljr authored Dec 7, 2022
2 parents e010061 + c2b2144 commit 3d82287
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ A server that includes:
* A mutating webhook that will patch any newly created service accounts in your Kubernetes cluster with an image pull secret.
* A thread that monitors namespaces to make sure all namespaces include a image pull secret to be able to pull from GCR and AR.

Setting the environment variable `MOCK_GOOGLE_TOKEN` to `true` will prevent using the google application credentials to fetch the token used for the image pull secret. Instead the token will be mocked.

## Deployment
Use the image `gcr.io/k8s-minikube/gcp-auth-webhook` as the image for a Deployment in your Kubernetes manifest and add that to a MutatingWebhookConfiguration. See [minikube](https://github.com/kubernetes/minikube/blob/master/deploy/addons/gcp-auth/gcp-auth-webhook.yaml.tmpl) for details.

Expand Down
15 changes: 12 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"
"time"

Expand All @@ -31,6 +32,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
Expand Down Expand Up @@ -116,9 +118,16 @@ func createPullSecret(clientset *kubernetes.Clientset, ns *corev1.Namespace, cre
}
}

token, err := creds.TokenSource.Token()
if err != nil {
return err
// The MOCK_GOOGLE_TOKEN env var prevents using credentials to fetch the token. Instead the token will be mocked.
mockToken, _ := strconv.ParseBool(os.Getenv("MOCK_GOOGLE_TOKEN"))
var token *oauth2.Token
if mockToken {
token = &oauth2.Token{AccessToken: "mock_access_token"}
} else {
token, err = creds.TokenSource.Token()
if err != nil {
return err
}
}
var dockercfg string
registries := append(gcr_config.DefaultGCRRegistries[:], gcr_config.DefaultARRegistries[:]...)
Expand Down

0 comments on commit 3d82287

Please sign in to comment.