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
feat: refactored the provider code
=0bdd3e2f0d36
diff --git a/tests/CLAUDE.md b/tests/CLAUDE.mdindex f5fb110390..e30b2c5c77 100644--- a/tests/CLAUDE.md+++ b/tests/CLAUDE.md@@ -71,7 +71,6 @@ harness/ Stack, Profile, Tenant, Principalfixture/ platform resources: projects, app connections (one package, a file each)fixture/<product>/ one package per product: secretmanager, pki, pam (a file per resource)fakes/ one package per faked third party-provider/ what creating an app connection needs, per serviceinternal/ wait, mail, id, apierr, specclients/api/ generated Infisical client, never hand-edit```@@ -294,7 +293,7 @@ secretmanager.CreateSecret(t, proj, "dev", "DB_URL", "value",secretmanager.GetSecret(t, proj, "dev", "DB_URL")secretmanager.DeleteSecret(t, proj, "dev", "DB_URL")-conn := fixture.NewAppConnection(t, tn, provider.GitHub)+conn := fixture.NewAppConnection(t, tn, fixture.GitHubPATAppConnection)```**Fixtures take required arguments positionally and everything else as options.** What@@ -320,7 +319,7 @@ A fake is a working implementation holding real state, so you assert on what thedestination ended up holding rather than on which requests were sent:```go-conn := fixture.NewAppConnection(t, tn, provider.GitHub)+conn := fixture.NewAppConnection(t, tn, fixture.GitHubPATAppConnection)gh := github.Open(t, conn.FakenetAdmin(t), conn.Nonce())gh.Seed(t, github.RepoSecret("acme/app", "UNMANAGED", "keep"))@@ -406,14 +405,22 @@ sync, rotation and scanning; they all talk to the same GitHub.**Model the service honestly.** Real pagination, real error shapes, real crypto. A fakethat always answers on one page hides the bug where we never fetch page two.-### Adding a provider+### Adding an App Connection kind-`provider/` carries only what *creating a connection* needs: the app slug, the hostname-the client hardcodes, and a `Create` closure. How the service behaves lives in its fake.+A fake is the external service; a kind is how one Infisical resource connects to it.+Add one file to `fixture`, `appconnection_<app>.go`, declaring an `AppConnectionKind`+named after the app, auth method and resource (`GitHubPATAppConnection`). It carries the+app slug, the host taken from the fake (`github.Service.Host()`, never a second copy),+and how to call the create route. A different resource on the same service, such as a+dynamic secret, gets its own kind type in its own product's fixture package and reuses+the same fake.-The hostname must be one the client hardcodes. If the provider lets you configure a base-URL and you point it at fakenet, the test proves the fake works and nothing about-interception.+The host must be one the client hardcodes. If the app lets you configure a base URL and+you point it at fakenet, the test proves the fake works and nothing about interception.++A test about creating the connection itself, such as a refused credential, calls the+route through the generated client rather than the fixture+(`suites/appconnections/github_test.go`).## 9. Entitlements and rate limitsdiff --git a/tests/fixture/appconnection.go b/tests/fixture/appconnection.goindex 560c02a36e..581be0d64f 100644--- a/tests/fixture/appconnection.go+++ b/tests/fixture/appconnection.go@@ -4,74 +4,54 @@ import ("context""testing"+ "github.com/Infisical/infisical/tests/clients/api""github.com/Infisical/infisical/tests/harness""github.com/Infisical/infisical/tests/infra/fakenet""github.com/Infisical/infisical/tests/internal/id"- "github.com/Infisical/infisical/tests/provider""github.com/google/uuid""github.com/stretchr/testify/require")-// AppConnection is one connection and the credential its fake state is keyed on.-type AppConnection struct {- ID uuid.UUID- Name string- Provider provider.Provider-- nonce string- tn *harness.Tenant-}+// AppConnectionKind is one app and auth method an App Connection can be created with.+type AppConnectionKind struct {+ // App is the name in errors and the /api/v1/app-connections/<app> segment.+ App string-type AppConnectionOption func(*appConnectionConfig)+ // Host is what the app's client has hardcoded, taken from its fake. It has to be+ // hardcoded for the test to mean anything: a configurable base URL could be+ // pointed straight at fakenet, which would prove the fake works and nothing about+ // interception.+ Host string-type appConnectionConfig struct {- rejectWith int+ create func(ctx context.Context, c *api.ClientWithResponses, name, credential string) (uuid.UUID, error)}-// RejectCredentials makes the provider refuse the credential check, so the create-// fails. Proves the check is load bearing rather than merely made.-func RejectCredentials(status int) AppConnectionOption {- return func(c *appConnectionConfig) { c.rejectWith = status }-}+// AppConnection is one connection and the credential its fake state is keyed on.+type AppConnection struct {+ ID uuid.UUID+ Name string-func NewAppConnection(tt *testing.T, tn *harness.Tenant, p provider.Provider, opts ...AppConnectionOption) *AppConnection {- tt.Helper()- conn, err := TryAppConnection(tt, tn, p, opts...)- require.NoErrorf(tt, err, "creating a %s connection\n%s", p.App, deniedHint(tt, tn))- return conn+ nonce string+ tn *harness.Tenant}-// TryAppConnection is NewAppConnection without failing, for an expected refusal.-func TryAppConnection(tt *testing.T, tn *harness.Tenant, p provider.Provider, opts ...AppConnectionOption) (*AppConnection, error) {+func NewAppConnection(tt *testing.T, tn *harness.Tenant, kind AppConnectionKind) *AppConnection {tt.Helper()- var cfg appConnectionConfig- for _, o := range opts {- o(&cfg)- }-conn := &AppConnection{- Name: p.App + "-" + id.Short(),- Provider: p,- nonce: id.Nonce(),- tn: tn,+ Name: kind.App + "-" + id.Short(),+ nonce: id.Nonce(),+ tn: tn,}// Before the credential reaches Infisical, so nothing published under it can// arrive before its buffer exists.- fakenet.Track(tt, conn.FakenetAdmin(tt), p.Host, conn.nonce)-- if cfg.rejectWith != 0 {- fakenet.Open[any](tt, conn.FakenetAdmin(tt), p.Host, conn.nonce).- Fail(tt, "", "*", cfg.rejectWith)- }+ fakenet.Track(tt, FakenetAdmin(tt, tn), kind.Host, conn.nonce)- connID, err := p.Create(tt.Context(), tn.Admin.API, conn.Name, conn.nonce)- if err != nil {- return nil, err- }+ connID, err := kind.create(tt.Context(), tn.Admin.API, conn.Name, conn.nonce)+ require.NoErrorf(tt, err, "creating a %s connection\n%s", kind.App, deniedHint(tt, tn))conn.ID = connID- return conn, nil+ return conn}// deniedHint names the outbound calls that reached no fake, which the application's@@ -94,8 +74,14 @@ func deniedHint(tt *testing.T, tn *harness.Tenant) string {func (c *AppConnection) Nonce() string { return c.nonce }-// FakenetAdmin is a URL rather than a handle so fake packages never import fixtures.func (c *AppConnection) FakenetAdmin(tt *testing.T) string {tt.Helper()- return c.tn.Module(tt, fakenet.Key, "harness.Shared").(*fakenet.Handle).AdminURL()+ return FakenetAdmin(tt, c.tn)+}++// FakenetAdmin is fakenet's control URL, for a test that drives a fake before any+// connection exists. A URL rather than a handle so fake packages never import fixtures.+func FakenetAdmin(tt *testing.T, tn *harness.Tenant) string {+ tt.Helper()+ return tn.Module(tt, fakenet.Key, "harness.Shared").(*fakenet.Handle).AdminURL()}diff --git a/tests/fixture/appconnection_github.go b/tests/fixture/appconnection_github.gonew file mode 100644index 0000000000..44c3ca5d83--- /dev/null+++ b/tests/fixture/appconnection_github.go@@ -0,0 +1,71 @@+package fixture++import (+ "bytes"+ "context"+ "encoding/json"+ "fmt"++ "github.com/Infisical/infisical/tests/clients/api"+ "github.com/Infisical/infisical/tests/fakes/github"+ "github.com/google/uuid"+)++// GitHubPATAppConnection is a GitHub App Connection over a personal access token.+//+// PAT rather than the GitHub App or OAuth methods because it is the one that needs+// no prior handshake: the App method requires a `code` from an install callback and+// a server-configured app, neither of which a blackbox test can produce. The+// outbound call is the same either way, which is what this is here to exercise.+var GitHubPATAppConnection = AppConnectionKind{+ App: "github",+ Host: github.Service.Host(),+ create: createGitHubPATAppConnection,+}++func createGitHubPATAppConnection(ctx context.Context, c *api.ClientWithResponses, name, credential string) (uuid.UUID, error) {+ raw, err := GitHubPATAppConnectionBody(name, credential)+ if err != nil {+ return uuid.Nil, err+ }+ res, err := c.CreateGitHubAppConnectionWithBodyWithResponse(ctx, "application/json", bytes.NewReader(raw))+ if err != nil {+ return uuid.Nil, err+ }+ if res.JSON200 == nil {+ return uuid.Nil, fmt.Errorf("creating the connection returned %d: %s", res.StatusCode(), res.Body)+ }+ conn, err := res.JSON200.AppConnection.AsCreateGitHubAppConnection200JSONResponseBodyAppConnection0()+ if err != nil {+ return uuid.Nil, err+ }+ return conn.Id, nil+}++// GitHubPATAppConnectionBody is the create request, for a test that calls the route+// itself.+//+// Host and instanceType are both left unset, which is what pins the call to+// api.github.com. Setting host would point the client straight at fakenet, and the+// test would prove nothing about interception.+//+// Returned as bytes because oapi-codegen declares the request-body type as a defined+// type over the union struct, and a defined type does not inherit MarshalJSON, so the+// typed body would serialise as {}.+func GitHubPATAppConnectionBody(name, token string) ([]byte, error) {+ var creds api.CreateGitHubAppConnectionJSONBody_2_Credentials+ if err := creds.FromCreateGitHubAppConnectionJSONBody2Credentials1(+ api.CreateGitHubAppConnectionJSONBody2Credentials1{PersonalAccessToken: token}); err != nil {+ return nil, err+ }++ var body api.CreateGitHubAppConnectionJSONBody+ if err := body.FromCreateGitHubAppConnectionJSONBody2(api.CreateGitHubAppConnectionJSONBody2{+ Method: api.CreateGitHubAppConnectionJSONBody2MethodPat,+ Credentials: creds,+ }); err != nil {+ return nil, err+ }+ body.Name = name+ return json.Marshal(body)+}diff --git a/tests/fixture/secretmanager/secret.go b/tests/fixture/secretmanager/secret.goindex 83f024ff3f..adabb76eb9 100644--- a/tests/fixture/secretmanager/secret.go+++ b/tests/fixture/secretmanager/secret.go@@ -2,7 +2,6 @@package secretmanagerimport (- "fmt""testing""github.com/Infisical/infisical/tests/clients/api"@@ -69,14 +68,6 @@ func CreateSecret(tt *testing.T, p *fixture.Project, env, name, value string, op}func GetSecret(tt *testing.T, p *fixture.Project, env, name string, opts ...SecretOption) Secret {- tt.Helper()- s, err := TryGetSecret(tt, p, env, name, opts...)- require.NoError(tt, err)- return s-}--// TryGetSecret is GetSecret without failing, for asserting a secret is absent.-func TryGetSecret(tt *testing.T, p *fixture.Project, env, name string, opts ...SecretOption) (Secret, error) {tt.Helper()cfg, client := resolve(p, opts)@@ -86,18 +77,14 @@ func TryGetSecret(tt *testing.T, p *fixture.Project, env, name string, opts ...SSecretPath: &cfg.path,ViewSecretValue: new(api.GetSecretByNameV4ParamsViewSecretValue("true")),})- if err != nil {- return Secret{}, err- }- if res.JSON200 == nil {- return Secret{}, fmt.Errorf("reading secret %s returned %d: %s", name, res.StatusCode(), apierr.Body(res.Body))- }+ require.NoError(tt, err)+ require.NotNilf(tt, res.JSON200, "reading secret %s returned %d: %s", name, res.StatusCode(), apierr.Body(res.Body))return Secret{Value: res.JSON200.Secret.SecretValue,ValueHidden: res.JSON200.Secret.SecretValueHidden,Path: res.JSON200.Secret.SecretPath,Version: res.JSON200.Secret.Version,- }, nil+ }}func DeleteSecret(tt *testing.T, p *fixture.Project, env, name string, opts ...SecretOption) {diff --git a/tests/harnesstest/isolated/profile_test.go b/tests/harnesstest/isolated/profile_test.goindex 8f805dcdc9..c65a56f904 100644--- a/tests/harnesstest/isolated/profile_test.go+++ b/tests/harnesstest/isolated/profile_test.go@@ -7,7 +7,6 @@ import ("github.com/Infisical/infisical/tests/fixture""github.com/Infisical/infisical/tests/harness""github.com/Infisical/infisical/tests/internal/spec"- "github.com/Infisical/infisical/tests/provider""github.com/stretchr/testify/require")@@ -19,7 +18,7 @@ func TestIsolated_ReachesTheSharedFakenet(t *testing.T) {tn := harness.From(t).NewTenant(t)// Action- conn := fixture.NewAppConnection(t, tn, provider.GitHub)+ conn := fixture.NewAppConnection(t, tn, fixture.GitHubPATAppConnection)// Assertgh := github.Open(t, conn.FakenetAdmin(t), conn.Nonce())diff --git a/tests/harnesstest/shared/stubbing_test.go b/tests/harnesstest/shared/stubbing_test.goindex a8996d0333..79ec9d620e 100644--- a/tests/harnesstest/shared/stubbing_test.go+++ b/tests/harnesstest/shared/stubbing_test.go@@ -1,14 +1,12 @@package harnesstest_testimport (- "net/http""testing""github.com/Infisical/infisical/tests/fakes/github""github.com/Infisical/infisical/tests/fixture""github.com/Infisical/infisical/tests/harness""github.com/Infisical/infisical/tests/internal/spec"- "github.com/Infisical/infisical/tests/provider""github.com/stretchr/testify/require")@@ -25,7 +23,7 @@ func TestOutbound_IsIntercepted(t *testing.T) {tn := h.NewTenant(t)// Action- conn := fixture.NewAppConnection(t, tn, provider.GitHub)+ conn := fixture.NewAppConnection(t, tn, fixture.GitHubPATAppConnection)// Assertgh := github.Open(t, conn.FakenetAdmin(t), conn.Nonce())@@ -40,8 +38,8 @@ func TestOutbound_IsIntercepted(t *testing.T) {tenantA, tenantB := h.NewTenant(t), h.NewTenant(t)// Action- a := fixture.NewAppConnection(t, tenantA, provider.GitHub)- b := fixture.NewAppConnection(t, tenantB, provider.GitHub)+ a := fixture.NewAppConnection(t, tenantA, fixture.GitHubPATAppConnection)+ b := fixture.NewAppConnection(t, tenantB, fixture.GitHubPATAppConnection)// Assertfor _, conn := range []*fixture.AppConnection{a, b} {@@ -49,20 +47,4 @@ func TestOutbound_IsIntercepted(t *testing.T) {require.Equal(t, 1, gh.Received(t, "GET", "/user"), "a connection counted another's requests")}})-- t.Run("should refuse the connection when the fake rejects the credential", func(t *testing.T) {- t.Parallel()- spec.Why(t, `Proves the answer matters, not just that a request arrived. Also the- failure-injection path: a rule registered before the connection exists.`)-- // Setup- tn := h.NewTenant(t)-- // Action- _, err := fixture.TryAppConnection(t, tn, provider.GitHub,- fixture.RejectCredentials(http.StatusUnauthorized))-- // Assert- require.Error(t, err, "the connection was created despite a refused credential check")- })}diff --git a/tests/provider/provider.go b/tests/provider/provider.godeleted file mode 100644index fe8c9cf28a..0000000000--- a/tests/provider/provider.go+++ /dev/null@@ -1,85 +0,0 @@-// Package provider is what the harness knows about the third parties Infisical-// calls out to.-//-// One entry per service, added when a suite first needs it. It carries only what-// creating a connection needs: the app slug, the hostname the client hardcodes, and-// the call itself. How the service behaves once reached belongs to its fake under-// fakes/, not here.-package provider--import (- "bytes"- "context"- "encoding/json"- "fmt"-- "github.com/Infisical/infisical/tests/clients/api"- "github.com/google/uuid"-)--// Provider describes one third-party service.-type Provider struct {- // App is the name in errors and the /api/v1/app-connections/<app> segment.- App string-- // Host is what the provider's client has hardcoded, and is the key its fake is- // registered under. It has to be hardcoded for the test to mean anything: a- // configurable base URL could be pointed straight at fakenet, which would prove- // the fake works and nothing about interception.- Host string-- // Create makes the connection through the generated client.- Create func(ctx context.Context, c *api.ClientWithResponses, name, nonce string) (uuid.UUID, error)-}--// GitHub over a personal access token.-//-// PAT rather than the GitHub App or OAuth methods because it is the one that needs-// no prior handshake: the App method requires a `code` from an install callback and-// a server-configured app, neither of which a blackbox test can produce. The-// outbound call is the same either way, which is what this is here to exercise.-var GitHub = Provider{- App: "github",- Host: "api.github.com",-- Create: func(ctx context.Context, c *api.ClientWithResponses, name, nonce string) (uuid.UUID, error) {- // Host and instanceType are both left unset, which is what pins the call to- // api.github.com. Setting host would point the client straight at fakenet,- // and the test would prove nothing about interception.- var creds api.CreateGitHubAppConnectionJSONBody_2_Credentials- if err := creds.FromCreateGitHubAppConnectionJSONBody2Credentials1(- api.CreateGitHubAppConnectionJSONBody2Credentials1{PersonalAccessToken: nonce}); err != nil {- return uuid.Nil, err- }-- var body api.CreateGitHubAppConnectionJSONBody- if err := body.FromCreateGitHubAppConnectionJSONBody2(api.CreateGitHubAppConnectionJSONBody2{- Method: api.CreateGitHubAppConnectionJSONBody2MethodPat,- Credentials: creds,- }); err != nil {- return uuid.Nil, err- }- body.Name = name-- // Marshalled by hand for the same reason as the signup body: oapi-codegen- // declares the request-body type as a defined type over the union struct,- // and a defined type does not inherit MarshalJSON, so the union would- // serialise as {}.- raw, err := json.Marshal(body)- if err != nil {- return uuid.Nil, err- }- res, err := c.CreateGitHubAppConnectionWithBodyWithResponse(ctx, "application/json", bytes.NewReader(raw))- if err != nil {- return uuid.Nil, err- }- if res.JSON200 == nil {- return uuid.Nil, fmt.Errorf("creating the connection returned %d: %s", res.StatusCode(), res.Body)- }- conn, err := res.JSON200.AppConnection.AsCreateGitHubAppConnection200JSONResponseBodyAppConnection0()- if err != nil {- return uuid.Nil, err- }- return conn.Id, nil- },-}diff --git a/tests/suites/appconnections/github_test.go b/tests/suites/appconnections/github_test.gonew file mode 100644index 0000000000..cad26b2b82--- /dev/null+++ b/tests/suites/appconnections/github_test.go@@ -0,0 +1,38 @@+package appconnections_test++import (+ "bytes"+ "net/http"+ "testing"++ "github.com/Infisical/infisical/tests/fakes/github"+ "github.com/Infisical/infisical/tests/fixture"+ "github.com/Infisical/infisical/tests/harness"+ "github.com/Infisical/infisical/tests/internal/id"+ "github.com/stretchr/testify/require"+)++func TestGitHubAppConnection_Create(t *testing.T) {+ t.Parallel()+ h := harness.From(t)++ t.Run("should refuse the connection when GitHub rejects the token", func(t *testing.T) {+ t.Parallel()++ // Setup+ tn := h.NewTenant(t)+ token := id.Nonce()+ github.Open(t, fixture.FakenetAdmin(t, tn), token).Fail(t, "", "*", http.StatusUnauthorized)+ body, err := fixture.GitHubPATAppConnectionBody("refused-"+id.Short(), token)+ require.NoError(t, err)++ // Action+ res, err := tn.Admin.API.CreateGitHubAppConnectionWithBodyWithResponse(t.Context(),+ "application/json", bytes.NewReader(body))++ // Assert+ require.NoError(t, err)+ require.NotEqual(t, http.StatusOK, res.StatusCode(),+ "the connection was created despite a refused token")+ })+}diff --git a/tests/suites/appconnections/main_test.go b/tests/suites/appconnections/main_test.gonew file mode 100644index 0000000000..5ccaa8aeca--- /dev/null+++ b/tests/suites/appconnections/main_test.go@@ -0,0 +1,9 @@+package appconnections_test++import (+ "testing"++ "github.com/Infisical/infisical/tests/harness"+)++func TestMain(m *testing.M) { harness.Main(m, harness.Shared) }diff --git a/tests/suites/secretmanager/syncs/github_test.go b/tests/suites/secretmanager/syncs/github_test.goindex b06b735755..382d530580 100644--- a/tests/suites/secretmanager/syncs/github_test.go+++ b/tests/suites/secretmanager/syncs/github_test.go@@ -10,7 +10,6 @@ import ("github.com/Infisical/infisical/tests/harness""github.com/Infisical/infisical/tests/infra/fakenet""github.com/Infisical/infisical/tests/internal/spec"- "github.com/Infisical/infisical/tests/provider""github.com/stretchr/testify/require")@@ -19,7 +18,7 @@ func setup(t *testing.T) (*fixture.Project, *fixture.AppConnection, *github.Contt.Helper()tn := harness.From(t).NewTenant(t)proj := fixture.NewProject(t, tn, fixture.WithProjectType("secret-manager"))- conn := fixture.NewAppConnection(t, tn, provider.GitHub)+ conn := fixture.NewAppConnection(t, tn, fixture.GitHubPATAppConnection)return proj, conn, github.Open(t, conn.FakenetAdmin(t), conn.Nonce())}