Skip to content

Commit 3d82287

Browse files
authored
Merge pull request #33 from iastewar/mock-token
Added an env var to use a mock token instead of fetching the real one
2 parents e010061 + c2b2144 commit 3d82287

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ A server that includes:
55
* A mutating webhook that will patch any newly created service accounts in your Kubernetes cluster with an image pull secret.
66
* A thread that monitors namespaces to make sure all namespaces include a image pull secret to be able to pull from GCR and AR.
77

8+
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.
9+
810
## Deployment
911
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.
1012

server.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"log"
2323
"net/http"
2424
"os"
25+
"strconv"
2526
"strings"
2627
"time"
2728

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

35+
"golang.org/x/oauth2"
3436
"golang.org/x/oauth2/google"
3537
"k8s.io/apimachinery/pkg/runtime"
3638
"k8s.io/apimachinery/pkg/runtime/serializer"
@@ -116,9 +118,16 @@ func createPullSecret(clientset *kubernetes.Clientset, ns *corev1.Namespace, cre
116118
}
117119
}
118120

119-
token, err := creds.TokenSource.Token()
120-
if err != nil {
121-
return err
121+
// The MOCK_GOOGLE_TOKEN env var prevents using credentials to fetch the token. Instead the token will be mocked.
122+
mockToken, _ := strconv.ParseBool(os.Getenv("MOCK_GOOGLE_TOKEN"))
123+
var token *oauth2.Token
124+
if mockToken {
125+
token = &oauth2.Token{AccessToken: "mock_access_token"}
126+
} else {
127+
token, err = creds.TokenSource.Token()
128+
if err != nil {
129+
return err
130+
}
122131
}
123132
var dockercfg string
124133
registries := append(gcr_config.DefaultGCRRegistries[:], gcr_config.DefaultARRegistries[:]...)

0 commit comments

Comments
 (0)