Files
c28383ed14 Worktree findings batch 2 (#17)
* Fix issues F-003, 006, 001, 022

* Fix issues F-025, 026, 010, 008

- Login tokens name their identity provider in the `provider` claim;
  SelectOrg and GetUserToken read it with pb.IDPFromString, which also
  takes the number older tokens carry, and AuthenticateAPIKey answers
  Provider Undefined.
- A login that fails after the callback decodes its state lands on the
  redirect URI of the client, in its response mode, with `error` and
  `error_description` (RFC 6749 section 4.1.2.1); a server-side failure
  is described by its status text only. The calls to an IdP take the
  request context and a client with a timeout, and the answer of the IdP
  stays in the server log.
- The platform bootstrap reads its ca-config with ca.LoadBootstrapConfig,
  which refuses an issuer URL template with `${ISSUER_ID}` or
  `$ISSUER_ID`: the loader expands them to an empty string. The proto,
  README and CLI help name `:ISSUER_ID`.
- CaDb.RegisterIssuer takes the trust anchor of a root issuer and writes
  both in one transaction; the backend service and the bootstrap pass it,
  and an anchor registered in another scope is AlreadyExists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Address PR review comments

- Invite acceptance deletes the invite in its transaction
  (query.TakeInvite, DELETE ... RETURNING) and grants from the deleted
  row: a withdrawn or already accepted invite grants nothing, a re-issued
  one grants its current role, and an email mismatch, an expired invite or
  a failed grant rolls the delete back.
- CaDb.RegisterIssuer requires the anchor of a root issuer and refuses one
  for any other issuer; the service always builds it for a root.
- The OAuth response is merged into the query of a redirect URI that has
  one, and replaces its fragment in fragment mode.
- The codemap describes the transactions of RegisterOrg and the invite
  acceptance.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-23 19:59:28 -05:00

76 lines
2.6 KiB
Go

package pgsql_test
import (
"crypto/x509"
"fmt"
"testing"
"github.com/effective-security/trustyca/api/pb"
"github.com/effective-security/trustyca/internal/db/model"
"github.com/effective-security/trustyca/tests/testutils"
"github.com/effective-security/xdb"
"github.com/stretchr/testify/require"
)
// testKeyProvider is the crypto provider of the issuers the tests register
const testKeyProvider = "inmem"
// newTestIssuer builds an Active issuer row of the scope for the CA
func newTestIssuer(orgID, projectID xdb.ID, label string, ca *testutils.TestCA, typ pb.AuthorityType_Enum, parentID xdb.ID) *model.Issuer {
m := &model.Issuer{
OrgID: orgID,
ProjectID: projectID,
Label: label,
Type: typ,
Status: pb.IssuerStatus_Active,
ParentID: parentID,
KeyProvider: testKeyProvider,
KeyID: fmt.Sprintf("trustyca/%d/%d/%s", orgID.UInt64(), projectID.UInt64(), label),
}
m.SetCertificate(ca.Cert, testutils.CertPEM(ca.Cert), ca.ChainPEM(), testutils.CertPEM(ca.Root().Cert))
return m
}
// registerIssuer registers the issuer and deletes it after the test
func registerIssuer(t *testing.T, m *model.Issuer) *model.Issuer {
t.Helper()
res, err := provider.RegisterIssuer(ctx, m, nil)
require.NoError(t, err)
t.Cleanup(func() {
_, _ = provider.DeleteIssuer(ctx, res.OrgID.UInt64(), res.ID.UInt64())
})
return res
}
// registerRootIssuer registers the root issuer with its certificate as the
// anchor of its org, and deletes both after the test
func registerRootIssuer(t *testing.T, m *model.Issuer, crt *x509.Certificate) *model.Issuer {
t.Helper()
anchor := model.NewRootCertificate(crt, m.OrgID, pb.Trust_Private, m.Pem)
// a new anchor keeps the ID it is given
anchor.ID = provider.NextID()
res, err := provider.RegisterIssuer(ctx, m, anchor)
require.NoError(t, err)
t.Cleanup(func() {
_, _ = provider.DeleteIssuer(ctx, res.OrgID.UInt64(), res.ID.UInt64())
_, _ = provider.DeleteRoot(ctx, res.OrgID.UInt64(), anchor.ID.UInt64())
})
return res
}
// registerCertificate registers the certificate issued by the CA and deletes
// it after the test
func registerCertificate(t *testing.T, crt *x509.Certificate, ca *testutils.TestCA, orgID, projectID xdb.ID, profile string, mod func(m *model.Certificate)) *model.Certificate {
t.Helper()
m := model.NewCertificate(crt, orgID, projectID, profile, testutils.CertPEM(crt), testutils.CertPEM(ca.Cert)+ca.ChainPEM(), "edge-1", nil, nil)
if mod != nil {
mod(m)
}
res, err := provider.RegisterCertificate(ctx, m, nil)
require.NoError(t, err)
t.Cleanup(func() {
_, _ = provider.DeleteCertificate(ctx, res.OrgID.UInt64(), res.ID.UInt64())
})
return res
}