Skip to content

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

feat: removed optional part as fakenet can be required

=d76c7e222d28
diff --git a/tests/harness/harness.go b/tests/harness/harness.go
index 995d4f80f8..881b3241de 100644
--- a/tests/harness/harness.go
+++ b/tests/harness/harness.go
@@ -143,19 +143,7 @@ func bringUp(ctx context.Context, profile Profile, cfg *config, pkg string) (*St
return nil, nil, err
}
- // Generated once and kept, because Infisical is handed this certificate when its
- // container is created and adopted by later binaries. Editing a fake rebuilds
- // fakenet; a CA minted per boot would leave the running Infisical trusting an
- // authority that no longer exists.
- caFile := filepath.Join(root, "tests", fakenet.CAFile)
- // Serialized because every package binary starts here at once, and two that each
- // found no file would write different CAs.
- releaseCA, err := infra.Lock("fakenet-ca")
- if err != nil {
- return nil, nil, err
- }
- _, err = fakenet.LoadOrCreateCA(caFile)
- releaseCA()
+ caFile, err := fakenet.PrepareCA(root)
if err != nil {
return nil, nil, err
}
diff --git a/tests/harness/infisical/infisical.go b/tests/harness/infisical/infisical.go
index eb52f3d876..d27bc89294 100644
--- a/tests/harness/infisical/infisical.go
+++ b/tests/harness/infisical/infisical.go
@@ -67,13 +67,11 @@ func (m *module) Key() infra.ModuleKey { return Key }
// Requires is the hard floor. env.ts enforces Redis with a zod refine, and the
// database URI has a default that resolves to a literal "undefined" host, so both
-// fail at connect time rather than parse time if absent.
-func (m *module) Requires() []infra.ModuleKey { return []infra.ModuleKey{postgres.Key, redis.Key} }
-
-// Optional is consumed if declared. fakenet is what makes outbound calls, mail and
-// entitlements controllable, and mail is what makes user creation possible at all.
-func (m *module) Optional() []infra.ModuleKey {
- return []infra.ModuleKey{fakenet.Key}
+// fail at connect time rather than parse time if absent. fakenet is what makes
+// outbound calls, mail and entitlements controllable, and mail is what makes creating
+// a user possible at all.
+func (m *module) Requires() []infra.ModuleKey {
+ return []infra.ModuleKey{postgres.Key, redis.Key, fakenet.Key}
}
func (m *module) Name() infra.NameParts {
@@ -104,43 +102,36 @@ func (m *module) Start(ctx context.Context, d infra.Deps) (infra.Handle, error)
env["INFISICAL_RUN_MODES"] = m.runModes
}
- _, wantSMTP := fakenet.From(d)
- var files []infra.File
- var dns []string
-
// Outbound interception is DNS, not a proxy. Every hostname resolves to fakenet,
// which answers as the service or refuses by name, so no HTTP client has to
// honour HTTP_PROXY. Docker's embedded resolver still answers container names
// first, so Postgres and Redis are unaffected.
- if fn, ok := fakenet.From(d); ok {
- if err := fn.VerifyCA(ctx); err != nil {
- return nil, err
- }
- dns = append(dns, fn.Resolver())
- files = append(files, infra.File{Src: fn.CAPEMFile(), Dst: fakenet.CAPath})
- env["NODE_EXTRA_CA_CERTS"] = fakenet.CAPath
-
- // fakenet answers for public hostnames from a private address, which the
- // SSRF guard would otherwise refuse. Node also warns about a missing
- // NODE_EXTRA_CA_CERTS and carries on, so the file above is not optional:
- // without it HTTP keeps working and every real provider breaks.
- env["ALLOW_INTERNAL_IP_CONNECTIONS"] = "true"
-
- // The License Server is one more fake, reached the same way as any other
- // third party: by name, over TLS fakenet terminates. It is what makes
- // entitlements resolve per organization rather than per instance.
- for k, v := range license.Env() {
- env[k] = v
- }
-
- env["SMTP_HOST"] = fakenet.IP
- env["SMTP_PORT"] = fmt.Sprint(smtp.Port)
- env["SMTP_FROM_ADDRESS"] = "[email protected]"
- env["SMTP_FROM_NAME"] = "Infisical Harness"
- // Defaults to true in env.ts, and the fake offers no STARTTLS, so leaving it
- // on makes every send fail.
- env["SMTP_REQUIRE_TLS"] = "false"
+ fn := fakenet.MustFrom(d)
+ if err := fn.VerifyCA(ctx); err != nil {
+ return nil, err
}
+ env["NODE_EXTRA_CA_CERTS"] = fakenet.CAPath
+
+ // fakenet answers for public hostnames from a private address, which the SSRF
+ // guard would otherwise refuse. Node also warns about a missing
+ // NODE_EXTRA_CA_CERTS and carries on, so the mounted CA is not optional: without
+ // it HTTP keeps working and every real provider breaks.
+ env["ALLOW_INTERNAL_IP_CONNECTIONS"] = "true"
+
+ // The License Server is one more fake, reached the same way as any other third
+ // party: by name, over TLS fakenet terminates. It is what makes entitlements
+ // resolve per organization rather than per instance.
+ for k, v := range license.Env() {
+ env[k] = v
+ }
+
+ env["SMTP_HOST"] = fakenet.IP
+ env["SMTP_PORT"] = fmt.Sprint(smtp.Port)
+ env["SMTP_FROM_ADDRESS"] = "[email protected]"
+ env["SMTP_FROM_NAME"] = "Infisical Harness"
+ // Defaults to true in env.ts, and the fake offers no STARTTLS, so leaving it on
+ // makes every send fail.
+ env["SMTP_REQUIRE_TLS"] = "false"
for k, v := range m.env {
env[k] = v
@@ -150,14 +141,14 @@ func (m *module) Start(ctx context.Context, d infra.Deps) (infra.Handle, error)
Image: m.image.Ref,
Env: env,
Ports: []int{port},
- Files: files,
- DNS: dns,
+ Files: []infra.File{{Src: fn.CAPEMFile(), Dst: fakenet.CAPath}},
+ DNS: []string{fn.Resolver()},
// /api/status is registered before the run-mode guard, so it answers even on
// a pod that serves no product routes. It also reports emailConfigured and
// redisConfigured, which the harness asserts so a misconfigured SMTP is a
// boot failure rather than a mysterious timeout twenty tests later.
Ready: readyStrategy(),
- Check: checkStatus(wantSMTP),
+ Check: checkStatus,
})
if err != nil {
return nil, err
diff --git a/tests/harness/infisical/infisical_test.go b/tests/harness/infisical/infisical_test.go
index fef443bf79..f0c9dd1536 100644
--- a/tests/harness/infisical/infisical_test.go
+++ b/tests/harness/infisical/infisical_test.go
@@ -8,6 +8,7 @@ import (
"github.com/Infisical/infisical/tests/harness/infisical"
"github.com/Infisical/infisical/tests/infra"
+ "github.com/Infisical/infisical/tests/infra/fakenet"
"github.com/Infisical/infisical/tests/infra/postgres"
"github.com/Infisical/infisical/tests/infra/redis"
"github.com/Infisical/infisical/tests/internal/spec"
@@ -31,8 +32,13 @@ func bootStack(t *testing.T, opts ...infisical.Option) *infisical.Handle {
require.NoError(t, err)
img, err := infisical.ResolveImage(ctx, root, log)
require.NoError(t, err)
+ fnImg, err := fakenet.ResolveImage(ctx, root, log)
+ require.NoError(t, err)
+ caFile, err := fakenet.PrepareCA(root)
+ require.NoError(t, err)
mods := []infra.Module{postgres.Module(), redis.Module(),
+ fakenet.Module(fakenet.WithImage(fnImg), fakenet.WithCAFile(caFile)),
infisical.Module(append([]infisical.Option{infisical.WithImage(img)}, opts...)...)}
scopes := map[infra.ModuleKey]infra.Scope{}
for _, m := range mods {
@@ -46,15 +52,22 @@ func bootStack(t *testing.T, opts ...infisical.Option) *infisical.Handle {
handles := map[infra.ModuleKey]infra.Handle{}
for _, m := range plan.Modules() {
+ shared := m.Key() == fakenet.Key
name := m.Name()
- name.Scope = infra.Test
- name.ScopeID = "m2boot"
+ if shared {
+ name.Scope = infra.Shared
+ } else {
+ name.Scope = infra.Test
+ name.ScopeID = "m2boot"
+ }
began := time.Now()
h, err := m.Start(ctx, infra.NewDeps(handles, infra.NetworkName, infra.Workspace(), name, runner, log))
require.NoErrorf(t, err, "starting %s", m.Key())
handles[m.Key()] = h
- t.Cleanup(func() { _ = h.Stop(context.WithoutCancel(ctx)) })
+ if !shared {
+ t.Cleanup(func() { _ = h.Stop(context.WithoutCancel(ctx)) })
+ }
log.Decision("ready", infra.ContainerName(name), h.Endpoint(infra.External), time.Since(began), "")
}
return handles[infisical.Key].(*infisical.Handle)
diff --git a/tests/harness/infisical/ready.go b/tests/harness/infisical/ready.go
index 6d92cb9756..a68659a3c8 100644
--- a/tests/harness/infisical/ready.go
+++ b/tests/harness/infisical/ready.go
@@ -31,39 +31,37 @@ func readyStrategy() infra.Ready {
WithStatusCodeMatcher(func(code int) bool { return code == http.StatusOK })
}
-// checkStatus runs once the application is up and asserts that the optional modules
+// checkStatus runs once the application is up and asserts that Redis and SMTP
// actually took effect.
//
// Separate from the wait strategy because a response matcher returns a bool, and the
// whole value here is the message. A silently unconfigured SMTP would otherwise
// surface twenty tests later as an email that never arrives, with nothing pointing
// at the cause.
-func checkStatus(wantSMTP bool) func(context.Context, infra.Container) error {
- return func(ctx context.Context, c infra.Container) error {
- url := c.Endpoint(infra.External, port).URL("http") + "/api/status"
+func checkStatus(ctx context.Context, c infra.Container) error {
+ url := c.Endpoint(infra.External, port).URL("http") + "/api/status"
- req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
- if err != nil {
- return err
- }
- resp, err := (&http.Client{Timeout: 10 * time.Second}).Do(req)
- if err != nil {
- return fmt.Errorf("reading /api/status: %w", err)
- }
- defer func() { _ = resp.Body.Close() }()
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
+ if err != nil {
+ return err
+ }
+ resp, err := (&http.Client{Timeout: 10 * time.Second}).Do(req)
+ if err != nil {
+ return fmt.Errorf("reading /api/status: %w", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
- var s status
- if err := json.NewDecoder(resp.Body).Decode(&s); err != nil {
- return fmt.Errorf("decoding /api/status: %w", err)
- }
+ var s status
+ if err := json.NewDecoder(resp.Body).Decode(&s); err != nil {
+ return fmt.Errorf("decoding /api/status: %w", err)
+ }
- if !s.RedisConfigured {
- return fmt.Errorf("reports redisConfigured=false; REDIS_URL did not take effect")
- }
- if wantSMTP && !s.EmailConfigured {
- return fmt.Errorf("reports emailConfigured=false; SMTP_HOST did not take effect, " +
- "which would otherwise surface later as an email that never arrives")
- }
- return nil
+ if !s.RedisConfigured {
+ return fmt.Errorf("reports redisConfigured=false; REDIS_URL did not take effect")
+ }
+ if !s.EmailConfigured {
+ return fmt.Errorf("reports emailConfigured=false; SMTP_HOST did not take effect, " +
+ "which would otherwise surface later as an email that never arrives")
}
+ return nil
}
diff --git a/tests/infra/fakenet/module.go b/tests/infra/fakenet/module.go
index ac977abfa4..803170a966 100644
--- a/tests/infra/fakenet/module.go
+++ b/tests/infra/fakenet/module.go
@@ -146,7 +146,6 @@ func Module(opts ...Option) infra.Module {
func (m *module) Key() infra.ModuleKey { return Key }
func (m *module) Requires() []infra.ModuleKey { return nil }
-func (m *module) Optional() []infra.ModuleKey { return nil }
func (m *module) Name() infra.NameParts {
return infra.NameParts{Module: "fakenet", Fingerprint: m.image.ShortID()}
@@ -260,7 +259,35 @@ func (h *Handle) fetchCA(ctx context.Context) ([]byte, error) {
return io.ReadAll(res.Body)
}
-func From(d infra.Deps) (*Handle, bool) { return d.Get[*Handle](Key) }
+// MustFrom is for a module that requires fakenet. Unreachable when Resolve has run,
+// since it rejects a missing requirement before any container starts.
+func MustFrom(d infra.Deps) *Handle {
+ h, ok := d.Get[*Handle](Key)
+ if !ok {
+ panic("infra/fakenet: MustFrom called for a module that was not declared")
+ }
+ return h
+}
+
+// PrepareCA creates the CA under the repository root if absent and returns its path.
+//
+// Generated once and kept, because Infisical is handed this certificate when its
+// container is created and adopted by later binaries. Editing a fake rebuilds
+// fakenet; a CA minted per boot would leave the running Infisical trusting an
+// authority that no longer exists. Serialized because every package binary starts at
+// once, and two that each found no file would write different CAs.
+func PrepareCA(repoRoot string) (string, error) {
+ caFile := filepath.Join(repoRoot, "tests", CAFile)
+ release, err := infra.Lock("fakenet-ca")
+ if err != nil {
+ return "", err
+ }
+ defer release()
+ if _, err := LoadOrCreateCA(caFile); err != nil {
+ return "", err
+ }
+ return caFile, nil
+}
// Denied is every outbound call that reached no fake, newest last.
func (h *Handle) Denied(ctx context.Context) ([]Denied, error) {
diff --git a/tests/infra/module.go b/tests/infra/module.go
index 7063ec5f73..0e71c035cb 100644
--- a/tests/infra/module.go
+++ b/tests/infra/module.go
@@ -27,9 +27,6 @@ type Module interface {
// connection timeout ninety seconds later.
Requires() []ModuleKey
- // Optional lists modules this one consumes if present and ignores if not.
- Optional() []ModuleKey
-
Start(context.Context, Deps) (Handle, error)
}
@@ -88,7 +85,7 @@ func (d Deps) Log() Logger { return d.log }
// Get returns a typed handle, or false when the module was not declared.
//
// A generic method rather than a package function, which Go 1.27 allows. Modules
-// still wrap it (fakenet.From, postgres.MustFrom) so call sites read as prose.
+// still wrap it (fakenet.MustFrom, postgres.MustFrom) so call sites read as prose.
func (d Deps) Get[H Handle](k ModuleKey) (H, bool) {
h, ok := d.handles[k]
if !ok {
@@ -128,7 +125,7 @@ func Resolve(modules []Module, scopes map[ModuleKey]Scope) (Plan, error) {
return Plan{}, fmt.Errorf("infra: %s requires %s, which was not declared", m.Key(), dep)
}
}
- if err := checkLifetimes(m, scopes, byKey); err != nil {
+ if err := checkLifetimes(m, scopes); err != nil {
return Plan{}, err
}
}
@@ -144,15 +141,12 @@ func Resolve(modules []Module, scopes map[ModuleKey]Scope) (Plan, error) {
// itself. A Shared Infisical takes fakenet's address and CA when its container is
// created, so if fakenet were Package-scoped it would vanish when that package
// finished and break every other package still using the instance.
-func checkLifetimes(m Module, scopes map[ModuleKey]Scope, byKey map[ModuleKey]Module) error {
+func checkLifetimes(m Module, scopes map[ModuleKey]Scope) error {
mine, ok := scopes[m.Key()]
if !ok {
return fmt.Errorf("infra: no scope declared for %s", m.Key())
}
- for _, dep := range append(append([]ModuleKey{}, m.Requires()...), m.Optional()...) {
- if _, declared := byKey[dep]; !declared {
- continue // optional and absent
- }
+ for _, dep := range m.Requires() {
theirs := scopes[dep]
if theirs.LongerLivedThan(mine) || theirs == mine {
continue
@@ -187,7 +181,7 @@ func topoSort(modules []Module, byKey map[ModuleKey]Module) ([]Module, error) {
state[m.Key()] = inProgress
path = append(path, m.Key())
- deps := append(append([]ModuleKey{}, m.Requires()...), m.Optional()...)
+ deps := append([]ModuleKey{}, m.Requires()...)
sort.Slice(deps, func(i, j int) bool { return deps[i] < deps[j] })
for _, k := range deps {
if dep, ok := byKey[k]; ok {
diff --git a/tests/infra/module_test.go b/tests/infra/module_test.go
index 4899ea7a44..49c71918bc 100644
--- a/tests/infra/module_test.go
+++ b/tests/infra/module_test.go
@@ -11,7 +11,6 @@ import (
type fakeModule struct {
key ModuleKey
requires []ModuleKey
- optional []ModuleKey
}
func (f fakeModule) Key() ModuleKey { return f.key }
@@ -19,7 +18,6 @@ func (f fakeModule) Name() NameParts {
return NameParts{Module: string(f.key)}
}
func (f fakeModule) Requires() []ModuleKey { return f.requires }
-func (f fakeModule) Optional() []ModuleKey { return f.optional }
func (f fakeModule) Start(context.Context, Deps) (Handle, error) {
return nil, nil
}
@@ -48,19 +46,6 @@ func TestResolve(t *testing.T) {
require.Equal(t, ModuleKey("infisical"), got[len(got)-1], "infisical must start last")
})
- t.Run("should accept an optional dependency that is absent", func(t *testing.T) {
- t.Parallel()
-
- // Action
- _, err := Resolve(
- mods(fakeModule{key: "infisical", optional: []ModuleKey{"fakenet"}}),
- map[ModuleKey]Scope{"infisical": Shared},
- )
-
- // Assert
- require.NoError(t, err)
- })
-
t.Run("should fail before any container starts when a required dependency is missing", func(t *testing.T) {
t.Parallel()
@@ -82,7 +67,7 @@ func TestResolve(t *testing.T) {
// Action
_, err := Resolve(
mods(
- fakeModule{key: "infisical", optional: []ModuleKey{"fakenet"}},
+ fakeModule{key: "infisical", requires: []ModuleKey{"fakenet"}},
fakeModule{key: "fakenet"},
),
map[ModuleKey]Scope{"infisical": Shared, "fakenet": Package},
@@ -101,7 +86,7 @@ func TestResolve(t *testing.T) {
// Action
_, err := Resolve(
mods(
- fakeModule{key: "infisical", optional: []ModuleKey{"fakenet"}},
+ fakeModule{key: "infisical", requires: []ModuleKey{"fakenet"}},
fakeModule{key: "fakenet"},
),
map[ModuleKey]Scope{"infisical": Package, "fakenet": Shared},
diff --git a/tests/infra/postgres/postgres.go b/tests/infra/postgres/postgres.go
index 8e96fcbe97..707acf420b 100644
--- a/tests/infra/postgres/postgres.go
+++ b/tests/infra/postgres/postgres.go
@@ -57,7 +57,6 @@ func (m *module) Key() infra.ModuleKey {
}
func (m *module) Requires() []infra.ModuleKey { return nil }
-func (m *module) Optional() []infra.ModuleKey { return nil }
func (m *module) Name() infra.NameParts {
return infra.NameParts{Module: "postgres", Instance: m.instance}
@@ -100,15 +99,10 @@ func (h *Handle) DSN(m infra.Mode) string {
return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", user, password, e.HostPort(), h.database)
}
-// From is for other MODULES during Start: an optional dependency.
-func From(d infra.Deps, instance ...string) (*Handle, bool) {
- return d.Get[*Handle](key(instance...))
-}
-
// MustFrom is for a required dependency. Unreachable when Resolve has run, since it
// rejects a missing requirement before any container starts.
func MustFrom(d infra.Deps, instance ...string) *Handle {
- h, ok := From(d, instance...)
+ h, ok := d.Get[*Handle](key(instance...))
if !ok {
panic("infra/postgres: MustFrom called for a module that was not declared")
}
diff --git a/tests/infra/redis/redis.go b/tests/infra/redis/redis.go
index c87bca7046..ff8b12bd37 100644
--- a/tests/infra/redis/redis.go
+++ b/tests/infra/redis/redis.go
@@ -39,7 +39,6 @@ func (m *module) Key() infra.ModuleKey {
}
func (m *module) Requires() []infra.ModuleKey { return nil }
-func (m *module) Optional() []infra.ModuleKey { return nil }
func (m *module) Name() infra.NameParts {
return infra.NameParts{Module: "redis", Instance: m.instance}
}
@@ -69,10 +68,8 @@ func (h *Handle) URL(m infra.Mode) string {
return fmt.Sprintf("redis://%s", h.Endpoint(m).HostPort())
}
-func From(d infra.Deps) (*Handle, bool) { return d.Get[*Handle](Key) }
-
func MustFrom(d infra.Deps) *Handle {
- h, ok := From(d)
+ h, ok := d.Get[*Handle](Key)
if !ok {
panic("infra/redis: MustFrom called for a module that was not declared")
}