Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
tests: GCP gce machine identity auth
Claude9374fa9c0f40
diff --git a/tests/fakes/oauth/google_signon.go b/tests/fakes/oauth/google_signon.gonew file mode 100644index 0000000000..5f8dfc1830--- /dev/null+++ b/tests/fakes/oauth/google_signon.go@@ -0,0 +1,76 @@+package oauth++import (+ "crypto/rsa"+ "crypto/x509"+ "encoding/pem"+)++// Google's federated sign-on certificates (GET /oauth2/v1/certs) verify the ID tokens+// GCE instances present for machine identity GCP auth. The request carries no+// credential, so it cannot be scoped per test: every test signs with this one fixed,+// test-only key, and is kept apart by the token's audience, its own identity id.+const (+ GoogleSignOnKeyID = "harness-google-federated-signon"++ googleSignOnScope = "federated-signon"++ googleSignOnKeyPEM = `-----BEGIN RSA PRIVATE KEY-----+MIIEogIBAAKCAQEAsmIsEL62xNJxVY6ch893QuYsi5LFquiz2y9mQvMdtXfjBAJx+SUvy8/hmXBd6ADbG34NhUc8CP8AkMvAxt/G7Plzs2O7GGYd5Rq+RhNJaqz/ApGA4+qspD1Vzozf+Q5D6KrfnQXCcZka5D5sCxNxoXdGGC9TfZNxtpcOQTZutSdvPdRJb5+MiDnmHmgvZGhlywrSkslshioOQGbJZseZZcRFVgSyubttM3+npmiupO4JRHtEbUu+cEdyB+GE3NUs7V/bbQC//fQnk9rjgHntKQnAq7UILtG6WlXZSImU5ppGZ2vSOnIH+NSq9+GdOwcQ7pvAbuqdhsInyRd4Lz6RMgpYDTwIDAQABAoIBAC0hSj24Slw0U6kO+KgrwEZ+yxR1QtkK9mdc/wDL6+A6ztJMMnSWqmd5gf2EYLW0Fbr4YqpP8d/ix/1aF+z0ZW2YT3FJLzodnOnP+lpn7Ai2DdAbt0fpGzF4NQ/LM1T653JjXKyIyv9beWK6aj+JJKovrtaejv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6QI8V1F7+x/rbOWYCPTJFAIeK5b9cdz0sZlpj97YKRS5INHMZh+3qe5HAofnM/YWs8tu4+jyf+2x5YbQXZic2XQD4=+-----END CERTIFICATE-----`+)++// GoogleSignOnKey is the private half of the published federated sign-on certificate.+func GoogleSignOnKey() *rsa.PrivateKey {+ block, _ := pem.Decode([]byte(googleSignOnKeyPEM))+ key, err := x509.ParsePKCS1PrivateKey(block.Bytes)+ if err != nil {+ panic(err)+ }+ return key+}diff --git a/tests/fakes/oauth/oauth.go b/tests/fakes/oauth/oauth.goindex 3beccc6096..e3a4b2858b 100644--- a/tests/fakes/oauth/oauth.go+++ b/tests/fakes/oauth/oauth.go@@ -60,6 +60,9 @@ func (s service) Host() string { return s.host }// the same code) on profile calls. GCP certificate lookups are scoped by the service// account they name.func (s service) Scope(r *http.Request) (string, bool) {+ if r.URL.Path == "/oauth2/v1/certs" {+ return googleSignOnScope, true+ }if sa, ok := strings.CutPrefix(r.URL.Path, "/service_accounts/v1/metadata/x509/"); ok {return sa, sa != ""}@@ -135,6 +138,9 @@ func (g *Grant) routes() *http.ServeMux {mux.HandleFunc("POST /token", g.token(GoogleClientID, GoogleClientSecret))mux.HandleFunc("GET /oauth2/v3/userinfo", g.googleUserinfo)mux.HandleFunc("GET /service_accounts/v1/metadata/x509/{sa}", g.certs)+ mux.HandleFunc("GET /oauth2/v1/certs", func(w http.ResponseWriter, _ *http.Request) {+ writeJSON(w, http.StatusOK, map[string]string{GoogleSignOnKeyID: googleSignOnCertPEM})+ })case providerGitHub:mux.HandleFunc("POST /login/oauth/access_token", g.token(GitHubClientID, GitHubClientSecret))case providerGitLab:diff --git a/tests/suites/machineidentity/gcpauth_test.go b/tests/suites/machineidentity/gcpauth_test.goindex f1a15aa199..aee68f5d4c 100644--- a/tests/suites/machineidentity/gcpauth_test.go+++ b/tests/suites/machineidentity/gcpauth_test.go@@ -16,8 +16,6 @@ import ("github.com/stretchr/testify/require")-// GCE identity tokens are verified against Google's global oauth2/v1/certs, which carry-// no credential fakenet could scope per test, so only the iam type is covered here.func TestGcpAuth_IamLogin(t *testing.T) {t.Parallel()h := harness.From(t)@@ -149,3 +147,112 @@ func fedLoginGcp(t *testing.T, tn *harness.Tenant, identityID uuid.UUID, jwt strrequire.NoError(t, err)return res}++func TestGcpAuth_GceLogin(t *testing.T) {+ t.Parallel()+ h := harness.From(t)++ t.Run("should issue a token for an instance in an allowed project and zone", func(t *testing.T) {+ t.Parallel()++ // Setup+ tn := h.NewTenant(t)+ identityID := fedNewIdentity(t, tn)+ fedAttachGcpGce(t, tn, identityID, "fed-proj", "us-central1-a")++ // Action+ res := fedLoginGcp(t, tn, identityID, fedGceToken(t, identityID.String(), "fed-proj", "us-central1-a"))++ // Assert+ require.NotNilf(t, res.JSON200, "login returned %d: %s", res.StatusCode(), apierr.Body(res.Body))+ fedRequireTokenWorks(t, tn, res.JSON200.AccessToken)+ })++ refusals := []struct {+ name, project, zone, message string+ }{+ {"should refuse an instance in a project outside the allowlist and issue no token", "other-proj", "us-central1-a", "Access denied: GCP project not allowed."},+ {"should refuse an instance in a zone outside the allowlist and issue no token", "fed-proj", "europe-west1-b", "Access denied: GCP zone not allowed."},+ }+ for _, c := range refusals {+ t.Run(c.name, func(t *testing.T) {+ t.Parallel()++ // Setup+ tn := h.NewTenant(t)+ identityID := fedNewIdentity(t, tn)+ fedAttachGcpGce(t, tn, identityID, "fed-proj", "us-central1-a")++ // Action+ res := fedLoginGcp(t, tn, identityID, fedGceToken(t, identityID.String(), c.project, c.zone))++ // Assert+ fedRequireStatus(t, http.StatusUnauthorized, res.StatusCode(), res.Body)+ require.Equal(t, c.message, apierr.Message(res.Body))+ fedRequireNeverLoggedIn(t, tn, identityID)+ })+ }++ t.Run("should refuse an ID token Google did not sign and issue no token", func(t *testing.T) {+ t.Parallel()++ // Setup+ tn := h.NewTenant(t)+ identityID := fedNewIdentity(t, tn)+ fedAttachGcpGce(t, tn, identityID, "fed-proj", "us-central1-a")+ forged := fedSignJWT(t, fedRSAKey(t), oauth.GoogleSignOnKeyID, fedGceClaims(identityID.String(), "fed-proj", "us-central1-a"))++ // Action+ res := fedLoginGcp(t, tn, identityID, forged)++ // Assert+ fedRequireStatus(t, http.StatusUnauthorized, res.StatusCode(), res.Body)+ require.Equal(t, "Invalid GCP ID token", apierr.Message(res.Body))+ fedRequireNeverLoggedIn(t, tn, identityID)+ })++ t.Run("should refuse an ID token minted for another audience and issue no token", func(t *testing.T) {+ t.Parallel()++ // Setup+ tn := h.NewTenant(t)+ identityID := fedNewIdentity(t, tn)+ fedAttachGcpGce(t, tn, identityID, "fed-proj", "us-central1-a")++ // Action+ res := fedLoginGcp(t, tn, identityID, fedGceToken(t, uuid.NewString(), "fed-proj", "us-central1-a"))++ // Assert+ fedRequireStatus(t, http.StatusUnauthorized, res.StatusCode(), res.Body)+ require.Equal(t, "Invalid GCP ID token", apierr.Message(res.Body))+ fedRequireNeverLoggedIn(t, tn, identityID)+ })+}++func fedAttachGcpGce(t *testing.T, tn *harness.Tenant, identityID uuid.UUID, project, zone string) {+ t.Helper()+ res, err := tn.Admin.API.AttachGcpAuthWithResponse(t.Context(), identityID.String(), api.AttachGcpAuthJSONRequestBody{+ Type: api.AttachGcpAuthJSONBodyType("gce"), AllowedProjects: new(project), AllowedZones: new(zone),+ })+ require.NoError(t, err)+ require.NotNilf(t, res.JSON200, "attaching GCP auth returned %d: %s", res.StatusCode(), apierr.Body(res.Body))+}++// fedGceClaims are what a GCE instance's metadata server puts in an ID token requested+// with format=full.+func fedGceClaims(audience, project, zone string) map[string]any {+ email := "vm-" + id.Short() + "@" + project + ".iam.gserviceaccount.com"+ return fedClaims("https://accounts.google.com", email, audience, map[string]any{+ "email": email,+ "google": map[string]any{"compute_engine": map[string]any{+ "project_id": project, "zone": zone, "instance_id": id.Short(), "instance_name": "vm",+ }},+ })+}++// fedGceToken is an ID token signed with the key the Google fake publishes as a+// federated sign-on certificate.+func fedGceToken(t *testing.T, audience, project, zone string) string {+ t.Helper()+ return fedSignJWT(t, oauth.GoogleSignOnKey(), oauth.GoogleSignOnKeyID, fedGceClaims(audience, project, zone))+}