Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

3.9 KB
Raw Blame
package access_test

import (
	"net/http"
	"testing"

	"github.com/Infisical/infisical/tests/fixture"
	"github.com/Infisical/infisical/tests/fixture/secretmanager"
	"github.com/Infisical/infisical/tests/harness"
	"github.com/stretchr/testify/require"
)

func TestPrincipal_ProjectAccess(t *testing.T) {
	t.Parallel()
	h := harness.From(t)

	t.Run("should refuse an org admin who is not a project member", func(t *testing.T) {
		t.Parallel()

		// Setup
		tn := h.NewTenant(t)
		proj := fixture.NewProject(t, tn, fixture.WithProjectType("secret-manager"))
		orgAdmin := tn.NewUser(t, harness.OrgRole("admin"))

		// Action
		res := listSecrets(t, orgAdmin.API, proj, "dev", "/")

		// Assert
		require.Equal(t, http.StatusForbidden, res.StatusCode())
		require.Contains(t, string(res.Body), "You are not a member of this project")
		require.Nil(t, userRoles(t, proj, orgAdmin))
	})

	t.Run("should make an org admin a project admin through grant-admin-access and record it", func(t *testing.T) {
		t.Parallel()

		// Setup
		tn := h.NewTenant(t)
		proj := fixture.NewProject(t, tn, fixture.WithProjectType("secret-manager"))
		orgAdmin := tn.NewUser(t, harness.OrgRole("admin"))

		// Action
		res, err := orgAdmin.API.GrantOrganizationAdminProjectAccessWithResponse(t.Context(), proj.ID)

		// Assert
		require.NoError(t, err)
		requireStatus(t, http.StatusOK, res.StatusCode(), res.Body)
		require.Equal(t, []string{"admin"}, slugs(userRoles(t, proj, orgAdmin)))
		created := createSecret(t, orgAdmin.API, proj, "dev", "/", "NEW", "value")
		requireStatus(t, http.StatusOK, created.StatusCode(), created.Body)
		event := auditEvent(t, tn, "org-admin-accessed-project", func(m map[string]any) bool {
			return m["projectId"] == proj.ID
		})
		require.Equal(t, orgAdmin.ID.String(), event["userId"])
	})

	t.Run("should refuse grant-admin-access to an org member and add no membership", func(t *testing.T) {
		t.Parallel()

		// Setup
		tn := h.NewTenant(t)
		proj := fixture.NewProject(t, tn, fixture.WithProjectType("secret-manager"))
		member := tn.NewUser(t, harness.OrgRole("member"))

		// Action
		res, err := member.API.GrantOrganizationAdminProjectAccessWithResponse(t.Context(), proj.ID)

		// Assert
		require.NoError(t, err)
		require.Equal(t, http.StatusForbidden, res.StatusCode())
		require.Nil(t, userRoles(t, proj, member))
	})

	t.Run("should refuse an identity once it is removed from the project", func(t *testing.T) {
		t.Parallel()

		// Setup
		tn := h.NewTenant(t)
		proj := fixture.NewProject(t, tn, fixture.WithProjectType("secret-manager"))
		secretmanager.CreateSecret(t, proj, "dev", "EXISTING", "value")
		identity := tn.NewMachineIdentity(t)
		proj.Grant(t, identity, "viewer")
		before := listSecrets(t, identity.API, proj, "dev", "/")
		requireStatus(t, http.StatusOK, before.StatusCode(), before.Body)

		// Action
		res, err := tn.Admin.API.DeleteProjectIdentityMembershipWithResponse(t.Context(), proj.ID, identity.ID.String())

		// Assert
		require.NoError(t, err)
		requireStatus(t, http.StatusOK, res.StatusCode(), res.Body)
		body := waitForStatus(t, http.StatusForbidden, func() (int, []byte) {
			r := listSecrets(t, identity.API, proj, "dev", "/")
			return r.StatusCode(), r.Body
		})
		require.Contains(t, string(body), "You are not a member of this project")
	})

	t.Run("should refuse a deleted identity's token as unauthenticated", func(t *testing.T) {
		t.Parallel()

		// Setup
		tn := h.NewTenant(t)
		proj := fixture.NewProject(t, tn, fixture.WithProjectType("secret-manager"))
		identity := tn.NewMachineIdentity(t)
		proj.Grant(t, identity, "viewer")

		// Action
		res, err := tn.Admin.API.DeleteMachineIdentityWithResponse(t.Context(), identity.ID.String())

		// Assert
		require.NoError(t, err)
		requireStatus(t, http.StatusOK, res.StatusCode(), res.Body)
		waitForStatus(t, http.StatusUnauthorized, func() (int, []byte) {
			r := listSecrets(t, identity.API, proj, "dev", "/")
			return r.StatusCode(), r.Body
		})
	})
}