Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions packages/agentvault/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,25 @@ func isProxyTokenRejected(err error) bool {
return errors.As(err, &apiErr) && apiErr.Name == proxyTokenRejectedName
}

// The name the server puts on a refusal because the org used its free Agent Vault requests for the month. Not a
// verdict on the session or the proxy: the proxy keeps running and brokers again once the org adds a payment method or the
// month resets.
const freeLimitReachedName = "AgentVaultFreeLimitReached"

func isFreeLimitReached(err error) bool {
var apiErr *api.APIError
return errors.As(err, &apiErr) && apiErr.Name == freeLimitReachedName
}

// The server's message names the limit and the day it resets, so the agent gets it when there is one.
func freeLimitReachedBody(err error) string {
var apiErr *api.APIError
if errors.As(err, &apiErr) && apiErr.ErrorMessage != "" {
return apiErr.ErrorMessage
}
return freeLimitReachedRefusal
}

// The names on Infisical's own 404s and 401s. A route miss, a middlebox or an auth proxy in front of Infisical
// answers under another name or none.
const (
Expand Down Expand Up @@ -307,7 +326,7 @@ func (c *sessionCache) lookup(sessionToken string, admit func() bool) (cacheLook
if err != nil {
// A rejected proxy token is remembered too: the poll loop exits after two such heartbeats, but
// until then every agent request would otherwise cost a resolve.
if isSessionGone(err) || isProxyTokenRejected(err) {
if isSessionGone(err) || isProxyTokenRejected(err) || isFreeLimitReached(err) {
c.mu.Lock()
c.rememberRefusalLocked(key, err)
c.forgetLocked(key, true)
Expand Down Expand Up @@ -508,7 +527,7 @@ func (c *sessionCache) handleRefreshFailure(key string, err error) {
entry, ok := c.entries[key]
if !ok {
// A pinned session whose entry is gone is still polled, and a verdict on it still ends its connections.
if c.pinnedLocked(key) && (isProxyTokenRejected(err) || isSessionGone(err)) {
if c.pinnedLocked(key) && (isProxyTokenRejected(err) || isSessionGone(err) || isFreeLimitReached(err)) {
c.forgetLocked(key, true)
}
return
Expand All @@ -526,6 +545,13 @@ func (c *sessionCache) handleRefreshFailure(key string, err error) {
c.forgetLocked(key, true)
return
}
// Dropped at once, not after the grace window: a credential must not outlive the org's free requests.
if isFreeLimitReached(err) {
log.Warn().Str("sessionId", entry.sessionID).Msg("agent-vault: this organization used its free Agent Vault requests for this month, dropping the session's credentials")
c.rememberRefusalLocked(key, err)
c.forgetLocked(key, true)
return
}

// Infisical being unreachable isn't a verdict on the session, so open connections stay and keep being polled
// until the grace window has passed since its last answer, when refresh closes them.
Expand Down Expand Up @@ -579,14 +605,14 @@ func (e *refreshNotRecoveredError) Error() string {
return fmt.Sprintf("the refresh did not produce a token the upstream accepts (status %q)", e.status)
}

// The refusals that end the session itself. A 404 from credential-refresh can mean the service is gone
// The refusals that drop the session's credentials. A 404 from credential-refresh can mean the service is gone
// rather than the session, so it stays a credential failure.
func refreshEndedSession(err error) bool {
var apiErr *api.APIError
if errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound {
return false
}
return isProxyTokenRejected(err) || isSessionGone(err)
return isProxyTokenRejected(err) || isSessionGone(err) || isFreeLimitReached(err)
}

func credentialHash(cred *credential) string {
Expand Down
29 changes: 29 additions & 0 deletions packages/agentvault/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,35 @@ func TestARejectedProxyTokenDropsTheSessionButIsNotASessionVerdict(t *testing.T)
}
}

func TestAFreeLimitRefusalDropsTheSessionAtOnceAndIsNotASessionVerdict(t *testing.T) {
err := &api.APIError{StatusCode: 400, Name: freeLimitReachedName, ErrorMessage: "This organization used its 5,000 free Agent Vault requests this month."}
if isSessionGone(err) || isProxyTokenRejected(err) {
t.Fatal("a free limit refusal was read as the session or the proxy being gone")
}

resolver := &stubResolver{result: &resolveResult{SessionID: "s1", Services: []*resolvedService{serviceWithSecret("v")}}}
cache := newTestCache(resolver)
if _, e := cache.get("tok"); e != nil {
t.Fatal(e)
}
resolver.err = err
cache.refresh()
if _, e := cache.get("tok"); !isFreeLimitReached(e) {
t.Fatalf("the credential kept flowing after the org used its free requests: %v", e)
}
}

func TestAFreeLimitRefusalIsNotReResolvedEveryRequest(t *testing.T) {
resolver := &stubResolver{err: &api.APIError{StatusCode: 400, Name: freeLimitReachedName}}
cache := newTestCache(resolver)
for i := 0; i < 20; i++ {
_, _ = cache.get("agv_tok")
}
if got := resolver.callCount(); got != 1 {
t.Fatalf("20 requests from an org past its free requests made %d resolves, want 1", got)
}
}

func TestAnUnnamed401StillEndsTheSession(t *testing.T) {
if !isSessionGone(&api.APIError{StatusCode: 401, Name: "UnauthorizedError", ErrorMessage: "Session revoked"}) {
t.Fatal("a plain 401 from an older server must still end the session")
Expand Down
2 changes: 2 additions & 0 deletions packages/agentvault/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (e *ProxyRefusedError) Error() string { return e.Message }

const sessionInvalidRefusal = "the session is no longer valid, or it belongs to a different Agent Vault project than this proxy"

const freeLimitReachedRefusal = "This organization used its free Agent Vault requests for this month"

// SessionInvalid is true when the proxy refused because the session itself can't be used any more. A service refusal
// is the same 403, so the code tells them apart.
func (e *ProxyRefusedError) SessionInvalid() bool {
Expand Down
4 changes: 4 additions & 0 deletions packages/agentvault/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,13 @@ const (
errorCodeTLSRequired = "tls_required"
errorCodeHookDenied = "hook_denied"
errorCodeHookFailed = "hook_failed"
errorCodeFreeLimitReached = "free_limit_reached"
)

var proxyErrorHints = map[string]string{
errorCodeFreeLimitReached: "This is the Agent Vault proxy, not the API: this organization used its free Agent Vault requests " +
"for this month, so every request is refused. Pass the message on to the user: an admin can add a payment method in " +
"Infisical under Usage & Billing, or the user can wait for the monthly reset; nothing in this environment can fix it.",
errorCodeSessionInvalid: "This is the Agent Vault proxy, not the API: the session this agent runs on has expired or been revoked, " +
"or it belongs to a different Agent Vault project than this proxy, so every request is refused. The user has to start a new " +
"session, or use a proxy in the session's project; nothing in this environment can fix it.",
Expand Down
13 changes: 13 additions & 0 deletions packages/agentvault/native_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unicode"
"unicode/utf8"
Expand Down Expand Up @@ -53,6 +54,10 @@ type nativeSessionLogger struct {
svc *resolvedService
now func() time.Time

// Billed once per connection, however many queries or commands follow, and only once one arrives: PAM logs
// nothing until it has logged in to the service, so a connection the service turned away costs nothing.
billed atomic.Bool

mu sync.Mutex
grant *sessionLogGrant
// The command's own row is written when it starts. What is piped into it and what it prints follow in rows
Expand All @@ -74,7 +79,14 @@ func newNativeSessionLogger(ps *proxyServer, key string, grant *sessionLogGrant,
return &nativeSessionLogger{ps: ps, key: key, grant: grant, svc: svc, now: time.Now}
}

func (l *nativeSessionLogger) bill() {
if l.billed.CompareAndSwap(false, true) {
l.ps.usage.add(1)
}
}

func (l *nativeSessionLogger) LogEntry(entry session.SessionLogEntry) error {
l.bill()
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
detail := &nativeDetail{}
appendCapped(&detail.Statement, entry.Input, nativeLogInputCap, &detail.Truncated)
appendCapped(&detail.Result, entry.Output, nativeLogOutputCap, &detail.Truncated)
Expand All @@ -91,6 +103,7 @@ func (l *nativeSessionLogger) LogEntry(entry session.SessionLogEntry) error {
}

func (l *nativeSessionLogger) LogSessionEvent(event session.SessionEvent) error {
l.bill()
data := string(event.Data)

l.mu.Lock()
Expand Down
23 changes: 23 additions & 0 deletions packages/agentvault/native_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,26 @@ func TestASilentClientIsClosedWhenConnectStops(t *testing.T) {
t.Fatalf("the silent client was left open after cancel: %v", err)
}
}

func TestTheLoggerBillsItsConnectionOnceOnTheFirstEntryOrEvent(t *testing.T) {
for _, first := range []func(*nativeSessionLogger){
func(l *nativeSessionLogger) { _ = l.LogEntry(session.SessionLogEntry{Input: "SELECT 1"}) },
func(l *nativeSessionLogger) {
_ = l.LogSessionEvent(session.SessionEvent{ChannelType: session.SessionChannelExec, EventType: session.SessionEventInput, Data: []byte("$ whoami")})
},
} {
logger, ps := newLoggerUnderTest(t)
ps.usage = newUsageReporter(nil, nil)
logger.recordOutcome(decisionError, "the service refused the login")
if n := ps.usage.brokered.Load(); n != 0 {
t.Fatalf("billed %d for an outcome row", n)
}
first(logger)
_ = logger.LogEntry(session.SessionLogEntry{Input: "SELECT 2"})
_ = logger.LogSessionEvent(session.SessionEvent{ChannelType: session.SessionChannelShell, EventType: session.SessionEventOutput, Data: []byte("ok")})
_ = logger.Close()
if n := ps.usage.brokered.Load(); n != 1 {
t.Fatalf("billed %d for one connection, want 1", n)
}
}
}
62 changes: 62 additions & 0 deletions packages/agentvault/native_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,3 +792,65 @@ func TestAHookRefusalOfAConnectionReachesConnectAndTheSessionLog(t *testing.T) {
})
}
}

func TestADatabaseConnectionIsBilledOnceOnItsFirstQuery(t *testing.T) {
upstream := startFakePostgres(t, "agent", "secret")
ps, client, _ := newNativeProxy(t, []*resolvedService{nativeServiceAt(serviceKindPostgres, "db", upstream)})
ps.usage = newUsageReporter(nil, nil)

tunnel, err := client.OpenTunnel(context.Background(), "db")
if err != nil {
t.Fatal(err)
}
defer tunnel.Close()
_ = tunnel.SetDeadline(time.Now().Add(5 * time.Second))

frontend := pgproto3.NewFrontend(tunnel, tunnel)
frontend.Send(&pgproto3.StartupMessage{ProtocolVersion: pgproto3.ProtocolVersionNumber, Parameters: map[string]string{"user": "whoever"}})
if err := frontend.Flush(); err != nil {
t.Fatal(err)
}
waitFor[*pgproto3.ReadyForQuery](t, frontend)
if n := ps.usage.brokered.Load(); n != 0 {
t.Fatalf("billed %d before any query", n)
}
for _, query := range []string{"SELECT 1", "SELECT 2"} {
frontend.Send(&pgproto3.Query{String: query})
if err := frontend.Flush(); err != nil {
t.Fatal(err)
}
waitFor[*pgproto3.ReadyForQuery](t, frontend)
}
frontend.Send(&pgproto3.Terminate{})
_ = frontend.Flush()
waitNativeDone(t, ps)

if n := ps.usage.brokered.Load(); n != 1 {
t.Fatalf("billed %d for one connection with two queries, want 1", n)
}
}

func TestADatabaseConnectionThatFailsToLogInIsNotBilled(t *testing.T) {
upstream := startFakePostgres(t, "agent", "the-real-password")
ps, client, _ := newNativeProxy(t, []*resolvedService{nativeServiceAt(serviceKindPostgres, "db", upstream)})
ps.usage = newUsageReporter(nil, nil)

tunnel, err := client.OpenTunnel(context.Background(), "db")
if err != nil {
t.Fatal(err)
}
defer tunnel.Close()
_ = tunnel.SetDeadline(time.Now().Add(5 * time.Second))
frontend := pgproto3.NewFrontend(tunnel, tunnel)
frontend.Send(&pgproto3.StartupMessage{ProtocolVersion: pgproto3.ProtocolVersionNumber, Parameters: map[string]string{"user": "whoever"}})
_ = frontend.Flush()

if records := nativeRecords(t, ps, 1); len(errorRows(records)) != 1 {
t.Fatalf("records = %+v, want the failed login's error row", records)
}
_ = tunnel.Close()
waitNativeDone(t, ps)
if n := ps.usage.brokered.Load(); n != 0 {
t.Fatalf("billed %d for a connection that never logged in", n)
}
}
Loading
Loading