Skip to content

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

add smoke test to validate that a state other than accepted or rejected closes a request

adilson7120964c071a
diff --git a/tests/fakes/servicenow/controlplane.go b/tests/fakes/servicenow/controlplane.go
index d8c7b88b66..1012850c8e 100644
--- a/tests/fakes/servicenow/controlplane.go
+++ b/tests/fakes/servicenow/controlplane.go
@@ -38,3 +38,9 @@ func Approved(sysID string) any {
func Rejected(sysID, notes string) any {
return Instance{AccessRequests: map[string]*AccessRequest{sysID: {Decision: "rejected", DecisionNotes: notes}}}
}
+
+// Expired records the customer's flow closing a record before anyone decided it, which
+// the application does not stamp a decision for.
+func Expired(sysID string) any {
+ return Instance{AccessRequests: map[string]*AccessRequest{sysID: {State: "expired"}}}
+}
diff --git a/tests/fakes/servicenow/fake.go b/tests/fakes/servicenow/fake.go
index caf6407955..a33a2cf831 100644
--- a/tests/fakes/servicenow/fake.go
+++ b/tests/fakes/servicenow/fake.go
@@ -121,9 +121,13 @@ func (in *Instance) readAccessRequest(w http.ResponseWriter, r *http.Request) {
return
}
- state := "awaiting_approval"
- if rec.Decision != "" {
+ state := rec.State
+ switch {
+ case state != "":
+ case rec.Decision != "":
state = "callback_pending"
+ default:
+ state = "pending"
}
in.events.Publish(AccessRequestRead{rec.detail()})
writeJSON(w, http.StatusOK, map[string]any{"result": map[string]any{
@@ -146,7 +150,7 @@ func (in *Instance) Snapshot() ([]byte, error) {
}
// Seed merges a partial instance in, so a test names only what it cares about. A
-// record is only ever decided here, never created: Infisical is what creates them.
+// record is only ever changed here, never created: Infisical is what creates them.
func (in *Instance) Seed(raw []byte) error {
var seed Instance
if err := json.Unmarshal(raw, &seed); err != nil {
@@ -163,7 +167,7 @@ func (in *Instance) Seed(raw []byte) error {
if !ok {
return fmt.Errorf("servicenow fake: no access request %q to decide", sysID)
}
- rec.Decision, rec.DecisionNotes = decided.Decision, decided.DecisionNotes
+ rec.Decision, rec.DecisionNotes, rec.State = decided.Decision, decided.DecisionNotes, decided.State
}
return nil
}
diff --git a/tests/fakes/servicenow/fake_test.go b/tests/fakes/servicenow/fake_test.go
index e191bdd22f..eda4fa8dfb 100644
--- a/tests/fakes/servicenow/fake_test.go
+++ b/tests/fakes/servicenow/fake_test.go
@@ -106,6 +106,34 @@ func TestAccessRequests(t *testing.T) {
require.Equal(t, new("approved"), reply.Result.Data.Decision)
})
+ t.Run("should report no decision when the customer's flow expired the record", func(t *testing.T) {
+ t.Parallel()
+
+ // Setup
+ fake := servicenow.Service.New(fakenet.Emitter{})
+ _, sysID := create(t, fake, "request-1")
+ raw, err := json.Marshal(servicenow.Expired(sysID))
+ require.NoError(t, err)
+ require.NoError(t, fake.Seed(raw))
+
+ // Action
+ code, body := send(t, fake, http.MethodGet, "/api/x_infis_approvals/v1/infisical/access_request/"+sysID, nil)
+
+ // Assert
+ require.Equalf(t, http.StatusOK, code, "read back: %s", body)
+ var reply struct {
+ Result struct {
+ Data struct {
+ State string `json:"state"`
+ Decision *string `json:"decision"`
+ } `json:"data"`
+ } `json:"result"`
+ }
+ require.NoError(t, json.Unmarshal(body, &reply))
+ require.Equal(t, "expired", reply.Result.Data.State)
+ require.Nil(t, reply.Result.Data.Decision)
+ })
+
t.Run("should return the record it already holds when the same request is sent twice", func(t *testing.T) {
t.Parallel()
diff --git a/tests/fakes/servicenow/state.go b/tests/fakes/servicenow/state.go
index ac588ab720..7661c75ca3 100644
--- a/tests/fakes/servicenow/state.go
+++ b/tests/fakes/servicenow/state.go
@@ -25,7 +25,8 @@ type Instance struct {
// AccessRequest is a record of the Infisical Approvals application, keyed by its sys_id.
// Everything above Decision is what Infisical sent; Decision and DecisionNotes are what
-// an approver records in ServiceNow, and stay empty until a test seeds them.
+// an approver records in ServiceNow, and stay empty until a test seeds them. State is
+// what the customer's flow sets; left empty, it follows from Decision.
type AccessRequest struct {
SysID string `json:"sysId"`
RequestID string `json:"requestId"`
@@ -38,6 +39,7 @@ type AccessRequest struct {
Decision string `json:"decision,omitempty"`
DecisionNotes string `json:"decisionNotes,omitempty"`
+ State string `json:"state,omitempty"`
}
func (rec *AccessRequest) detail() AccessRequestDetail {
diff --git a/tests/suites/secretmanager/approvals/delegated_access_request_test.go b/tests/suites/secretmanager/approvals/delegated_access_request_test.go
index d9a30e36b8..88b2e42c9d 100644
--- a/tests/suites/secretmanager/approvals/delegated_access_request_test.go
+++ b/tests/suites/secretmanager/approvals/delegated_access_request_test.go
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
+ "strings"
"sync"
"testing"
"time"
@@ -67,6 +68,49 @@ func TestDelegatedAccessRequest_Decision(t *testing.T) {
require.Equal(t, http.StatusForbidden, req.readSecret(t))
})
+ t.Run("should close the request without a grant when ServiceNow records a decision Infisical does not recognise", func(t *testing.T) {
+ t.Parallel()
+
+ // Setup
+ req := newWaitingDelegatedRequest(t)
+ req.sn.Seed(t, servicenow.Instance{AccessRequests: map[string]*servicenow.AccessRequest{
+ req.record.SysID: {Decision: "cancelled"},
+ }})
+
+ // Action
+ code := req.callback(t, req.record.CallbackToken)
+
+ // Assert
+ require.Equal(t, http.StatusOK, code)
+ closed := req.await(t, "failed")
+ require.Equal(t, delegatedState{status: "rejected", delegated: "failed", reason: closed.reason}, closed)
+ require.Contains(t, closed.reason, "does not recognise ('cancelled')")
+ require.Equal(t, http.StatusForbidden, req.readSecret(t))
+ })
+
+ t.Run("should keep waiting for a decision when ServiceNow expires the record without one", func(t *testing.T) {
+ t.Parallel()
+ spec.Why(t, "Infisical finalizes from the record's decision alone, so a record the customer's flow closed "+
+ "without one reads as undecided, and the request stays open for a later callback, its expiry, or a cancel.")
+
+ // Setup
+ req := newWaitingDelegatedRequest(t)
+ req.sn.Seed(t, servicenow.Expired(req.record.SysID))
+
+ // Action
+ code := req.callback(t, req.record.CallbackToken)
+
+ // Assert
+ require.Equal(t, http.StatusOK, code)
+ open, err := wait.For(t.Context(), func(ctx context.Context) (delegatedState, bool, error) {
+ st, err := req.state(ctx)
+ return st, err == nil && strings.Contains(st.reason, "(state: expired)"), err
+ }, wait.Timeout(outboxWait))
+ require.NoError(t, err, "waiting for the read-back to record the expired record")
+ require.Equal(t, delegatedState{status: "pending", delegated: "waiting_approval", reason: open.reason}, open)
+ require.Equal(t, http.StatusForbidden, req.readSecret(t))
+ })
+
t.Run("should cancel the request when the requester was deleted before ServiceNow approved", func(t *testing.T) {
t.Parallel()
spec.Why(t, "The access request list drops a request whose requester is gone, so the cancellation "+