lab7: исправления по независимому ревью

This commit is contained in:
Vladislav Kunetskiy
2026-11-10 14:22:47 +03:00
parent d1dc6d6b78
commit 30b6672058
18 changed files with 20572 additions and 20366 deletions
+42 -8
View File
@@ -274,6 +274,24 @@ func (l *Loader) verifyOwnChain(cert *tls.Certificate) ([][]*x509.Certificate, e
return chains, nil
}
func (l *Loader) mountedRoots(files Files) (map[string]struct{}, error) {
trust, err := secretfile.Read(files.CA)
if err != nil {
return nil, fmt.Errorf("read tls trust: %w", err)
}
issuers, err := parseCertificates(trust)
if err != nil {
return nil, err
}
current := map[string]struct{}{}
for _, cert := range issuers {
if bytes.Equal(cert.RawIssuer, cert.RawSubject) {
current[string(cert.Raw)] = struct{}{}
}
}
return current, nil
}
func (l *Loader) checkTrust(files Files) error {
trust, err := secretfile.Read(files.CA)
if err != nil {
@@ -282,16 +300,10 @@ func (l *Loader) checkTrust(files Files) error {
if bytes.Equal(trust, l.trust) {
return nil
}
issuers, err := parseCertificates(trust)
current, err := l.mountedRoots(files)
if err != nil {
return err
}
current := map[string]struct{}{}
for _, cert := range issuers {
if bytes.Equal(cert.RawIssuer, cert.RawSubject) {
current[string(cert.Raw)] = struct{}{}
}
}
// Adding roots (prepare overlap) is safe before a restart; removing a
// startup root requires a restart and fails closed.
for raw := range l.trustRoots {
@@ -302,8 +314,30 @@ func (l *Loader) checkTrust(files Files) error {
return nil
}
// VerifyTrustExact closes readiness unless the mounted roots equal the startup
// roots. A superset is safe for handshakes but not for the frozen TLS pools.
func (l *Loader) VerifyTrustExact() error {
files, err := l.files.snapshot()
if err != nil {
return err
}
current, err := l.mountedRoots(files)
if err != nil {
return err
}
if len(current) != len(l.trustRoots) {
return errors.New("tls trust differs from startup; restart required")
}
for raw := range l.trustRoots {
if _, ok := current[raw]; !ok {
return errors.New("tls trust differs from startup; restart required")
}
}
return nil
}
// VerifyTrust reports whether the mounted trust bundle still contains every
// root loaded at startup. Readiness uses it to retire un-restarted replicas.
// root loaded at startup. Kept for handshake-level subset checks.
func (l *Loader) VerifyTrust() error {
return l.verifyTrust()
}
+12 -13
View File
@@ -56,24 +56,23 @@ func (s *Service) Authenticate(ctx context.Context, username, plain string) (dom
return domain.User{}, fmt.Errorf("authenticate: %w", domain.ErrUnauthenticated)
}
if verified.NeedsRehash {
hash, err := password.Hash(plain)
if errors.Is(err, password.ErrBusy) {
// The credential is valid; a contended optional upgrade must not
// turn a successful login into a failure.
return credentials.User, nil
}
if err != nil {
return domain.User{}, fmt.Errorf("rehash password: %w", err)
}
// A concurrent upgrade wins the CAS; it must not invalidate this login.
if _, err := s.sessions.UpdatePasswordHash(ctx, credentials.User.ID, credentials.PasswordHash, hash); err != nil {
return domain.User{}, fmt.Errorf("upgrade password: %w", err)
}
// Optional upgrade: a busy derivation or transient write error must not
// turn an already verified login into a failure.
s.rehash(ctx, credentials.User.ID, credentials.PasswordHash, plain)
}
return credentials.User, nil
}
func (s *Service) rehash(ctx context.Context, userID uuid.UUID, oldHash, plain string) {
hash, err := password.Hash(plain)
if err != nil {
return
}
// A concurrent upgrade wins the CAS; both outcomes are acceptable.
_, _ = s.sessions.UpdatePasswordHash(ctx, userID, oldHash, hash)
}
func (s *Service) Resolve(ctx context.Context, raw string) (domain.User, error) {
hash, err := s.tokens.Hash(raw)
if err != nil {
+4 -4
View File
@@ -177,16 +177,16 @@ func (s *ServiceSuite) TestWrongLegacyPasswordDoesNotUpgrade() {
s.sessions.AssertNotCalled(s.T(), "UpdatePasswordHash", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
}
func (s *ServiceSuite) TestPasswordUpgradeFailureIsNotAuthenticationFailure() {
func (s *ServiceSuite) TestPasswordUpgradeFailureDoesNotBlockLogin() {
oldHash := obsoleteHash("secret-password-123", false)
s.sessions.EXPECT().Credentials(mock.Anything, "alice").
Return(domain.UserCredentials{User: s.user, PasswordHash: oldHash}, nil).Once()
s.sessions.On("UpdatePasswordHash", mock.Anything, s.user.ID, oldHash, mock.Anything).
Return(false, domain.ErrUnavailable).Once()
_, err := s.service.Authenticate(s.T().Context(), "alice", "secret-password-123")
s.Require().ErrorIs(err, domain.ErrUnavailable)
s.Require().NotErrorIs(err, domain.ErrUnauthenticated)
got, err := s.service.Authenticate(s.T().Context(), "alice", "secret-password-123")
s.Require().NoError(err)
s.Equal(s.user, got)
}
func obsoleteHash(plain string, legacy bool) string {