Skip to content

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

tests: instance relay tests on an isolated instance with RELAY_AUTH_SECRET

Claude9a89e0d4075f
diff --git a/tests/harness/harness.go b/tests/harness/harness.go
index 881b3241de..a4458e8011 100644
--- a/tests/harness/harness.go
+++ b/tests/harness/harness.go
@@ -54,6 +54,7 @@ type Option func(*config)
type config struct {
extra []infra.Module
plan *license.Plan
+ env map[string]string
}
// WithInfra adds a container the harness does not know about, for a suite that needs
@@ -62,6 +63,19 @@ func WithInfra(mods ...infra.Module) Option {
return func(c *config) { c.extra = append(c.extra, mods...) }
}
+// WithInfisicalEnv sets an environment variable on the package's own Infisical, such as
+// RELAY_AUTH_SECRET. Isolated only: a shared instance is adopted by name and keeps the
+// environment it was created with, so a Shared package asking for one would silently
+// not get it.
+func WithInfisicalEnv(key, value string) Option {
+ return func(c *config) {
+ if c.env == nil {
+ c.env = map[string]string{}
+ }
+ c.env[key] = value
+ }
+}
+
// WithDefaultPlan sets the entitlements a tenant gets unless it asks for others.
func WithDefaultPlan(p license.Plan) Option {
return func(c *config) { c.plan = &p }
@@ -148,7 +162,10 @@ func bringUp(ctx context.Context, profile Profile, cfg *config, pkg string) (*St
return nil, nil, err
}
- mods, scopes := profile.modules(img, fnImg, caFile, cfg.extra...)
+ if len(cfg.env) > 0 && profile != Isolated {
+ return nil, nil, fmt.Errorf("harness: WithInfisicalEnv needs harness.Isolated; a shared instance keeps the environment it was created with")
+ }
+ mods, scopes := profile.modules(img, fnImg, caFile, cfg.env, cfg.extra...)
plan, err := infra.Resolve(mods, scopes)
if err != nil {
return nil, nil, err
diff --git a/tests/harness/profile.go b/tests/harness/profile.go
index c68c31b838..8f0d7a709d 100644
--- a/tests/harness/profile.go
+++ b/tests/harness/profile.go
@@ -38,7 +38,7 @@ func (p Profile) String() string {
// shared container is adopted by name and takes its environment at creation, so if
// one package declared fakenet and another did not, whichever started the instance
// first would silently decide whether it runs in cloud mode for everyone.
-func (p Profile) modules(img infisical.Image, fnImg fakenet.Image, caFile string, extra ...infra.Module) ([]infra.Module, map[infra.ModuleKey]infra.Scope) {
+func (p Profile) modules(img infisical.Image, fnImg fakenet.Image, caFile string, env map[string]string, extra ...infra.Module) ([]infra.Module, map[infra.ModuleKey]infra.Scope) {
own := infra.Shared
if p == Isolated {
own = infra.Package
@@ -48,7 +48,7 @@ func (p Profile) modules(img infisical.Image, fnImg fakenet.Image, caFile string
postgres.Module(),
redis.Module(),
fakenet.Module(fakenet.WithImage(fnImg), fakenet.WithCAFile(caFile)),
- infisical.Module(infisical.WithImage(img)),
+ infisical.Module(append([]infisical.Option{infisical.WithImage(img)}, envOptions(env)...)...),
}
scopes := map[infra.ModuleKey]infra.Scope{
postgres.Key: own,
@@ -66,3 +66,11 @@ func (p Profile) modules(img infisical.Image, fnImg fakenet.Image, caFile string
}
return mods, scopes
}
+
+func envOptions(env map[string]string) []infisical.Option {
+ opts := make([]infisical.Option, 0, len(env))
+ for k, v := range env {
+ opts = append(opts, infisical.WithEnv(k, v))
+ }
+ return opts
+}
diff --git a/tests/suites/gateway/instancerelay/instance_relay_test.go b/tests/suites/gateway/instancerelay/instance_relay_test.go
new file mode 100644
index 0000000000..ed8885960d
--- /dev/null
+++ b/tests/suites/gateway/instancerelay/instance_relay_test.go
@@ -0,0 +1,74 @@
+package instancerelay_test
+
+import (
+ "net/http"
+ "testing"
+
+ "github.com/Infisical/infisical/tests/clients/api"
+ "github.com/Infisical/infisical/tests/harness"
+ "github.com/Infisical/infisical/tests/internal/apierr"
+ "github.com/Infisical/infisical/tests/internal/id"
+ "github.com/google/uuid"
+ "github.com/stretchr/testify/require"
+)
+
+func TestInstanceRelay(t *testing.T) {
+ h := harness.From(t)
+
+ t.Run("should list an instance relay registered with the auth secret to every organization", func(t *testing.T) {
+ // Setup
+ first, second := h.NewTenant(t), h.NewTenant(t)
+ name := "instance-" + id.Short()
+
+ // Action
+ res, err := first.Client(t, relayAuthSecret).RegisterInstanceRelayWithResponse(t.Context(),
+ api.RegisterInstanceRelayJSONRequestBody{Name: name, Host: name + ".relay.test"})
+
+ // Assert
+ require.NoError(t, err)
+ require.Equalf(t, http.StatusOK, res.StatusCode(), "registering: %s", apierr.Body(res.Body))
+ for _, tn := range []*harness.Tenant{first, second} {
+ relay := relayNamed(t, tn, name)
+ require.NotNilf(t, relay, "%s does not list the instance relay", tn.OrgSlug)
+ require.Nil(t, relay.OrgId)
+ }
+ })
+
+ t.Run("should refuse an organization admin deleting an instance relay and keep it", func(t *testing.T) {
+ // Setup
+ tn := h.NewTenant(t)
+ name := "instance-" + id.Short()
+ registered, err := tn.Client(t, relayAuthSecret).RegisterInstanceRelayWithResponse(t.Context(),
+ api.RegisterInstanceRelayJSONRequestBody{Name: name, Host: name + ".relay.test"})
+ require.NoError(t, err)
+ require.Equalf(t, http.StatusOK, registered.StatusCode(), "registering: %s", apierr.Body(registered.Body))
+ relay := relayNamed(t, tn, name)
+ require.NotNil(t, relay)
+
+ // Action
+ res, err := tn.Admin.API.DeleteRelayWithResponse(t.Context(), relay.Id.String())
+
+ // Assert
+ require.NoError(t, err)
+ require.Equalf(t, http.StatusNotFound, res.StatusCode(), "deleting: %s", apierr.Body(res.Body))
+ require.NotNil(t, relayNamed(t, tn, name))
+ })
+}
+
+type relay struct {
+ Id uuid.UUID
+ OrgId *uuid.UUID
+}
+
+func relayNamed(t *testing.T, tn *harness.Tenant, name string) *relay {
+ t.Helper()
+ res, err := tn.Admin.API.GetRelaysWithResponse(t.Context())
+ require.NoError(t, err)
+ require.NotNilf(t, res.JSON200, "listing relays returned %d: %s", res.StatusCode(), apierr.Body(res.Body))
+ for _, r := range *res.JSON200 {
+ if r.Name == name {
+ return &relay{Id: r.Id, OrgId: r.OrgId}
+ }
+ }
+ return nil
+}
diff --git a/tests/suites/gateway/instancerelay/main_test.go b/tests/suites/gateway/instancerelay/main_test.go
new file mode 100644
index 0000000000..a7abe570ed
--- /dev/null
+++ b/tests/suites/gateway/instancerelay/main_test.go
@@ -0,0 +1,15 @@
+package instancerelay_test
+
+import (
+ "testing"
+
+ "github.com/Infisical/infisical/tests/harness"
+)
+
+// relayAuthSecret is what an instance relay presents to register. It is instance-wide,
+// so the package gets an instance of its own.
+const relayAuthSecret = "harness-instance-relay-secret"
+
+func TestMain(m *testing.M) {
+ harness.Main(m, harness.Isolated, harness.WithInfisicalEnv("RELAY_AUTH_SECRET", relayAuthSecret))
+}