Skip to content

Commit ea30fce

Browse files
committed
fix: replace SHA1 with SHA256 in ServiceInstanceName; fix preflight E2E tests
- Use crypto/sha256 instead of crypto/sha1 to satisfy static analysis - Preflight E2E tests now create the DB first, then update with the bad config — asserting the update task fails rather than expecting NewDatabaseFixture itself to fail
1 parent 97dd4dc commit ea30fce

2 files changed

Lines changed: 19 additions & 23 deletions

File tree

e2e/postgrest_test.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ func TestPostgRESTPreflight_MissingSchema(t *testing.T) {
167167
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
168168
defer cancel()
169169

170+
// Create the database without any services first (this must succeed).
170171
db := fixture.NewDatabaseFixture(ctx, t, &controlplane.CreateDatabaseRequest{
172+
Spec: postgrestBaseSpec("test_postgrest_preflight_schema", []string{host1}, nil),
173+
})
174+
175+
// Adding PostgREST with a nonexistent schema must cause the task to fail.
176+
err := db.Update(ctx, UpdateOptions{
171177
Spec: postgrestBaseSpec(
172178
"test_postgrest_preflight_schema",
173179
[]string{host1},
@@ -178,17 +184,8 @@ func TestPostgRESTPreflight_MissingSchema(t *testing.T) {
178184
},
179185
),
180186
})
181-
182-
// The database itself provisions; only the PostgREST task fails.
183-
require.NoError(t, db.Refresh(ctx))
184-
185-
// Service instance should not reach running.
186-
for _, si := range db.ServiceInstances {
187-
if si.ServiceID == "postgrest-api" {
188-
assert.NotEqual(t, "running", si.State,
189-
"service should not reach running when schema is missing")
190-
}
191-
}
187+
require.Error(t, err, "expected update task to fail due to missing schema")
188+
assert.Contains(t, err.Error(), "nonexistent_schema", "error should mention the missing schema")
192189
}
193190

194191
// TestPostgRESTPreflight_MissingAnonRole verifies the preflight check rejects
@@ -201,7 +198,13 @@ func TestPostgRESTPreflight_MissingAnonRole(t *testing.T) {
201198
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
202199
defer cancel()
203200

201+
// Create the database without any services first (this must succeed).
204202
db := fixture.NewDatabaseFixture(ctx, t, &controlplane.CreateDatabaseRequest{
203+
Spec: postgrestBaseSpec("test_postgrest_preflight_role", []string{host1}, nil),
204+
})
205+
206+
// Adding PostgREST with a nonexistent anon role must cause the task to fail.
207+
err := db.Update(ctx, UpdateOptions{
205208
Spec: postgrestBaseSpec(
206209
"test_postgrest_preflight_role",
207210
[]string{host1},
@@ -212,15 +215,8 @@ func TestPostgRESTPreflight_MissingAnonRole(t *testing.T) {
212215
},
213216
),
214217
})
215-
216-
require.NoError(t, db.Refresh(ctx))
217-
218-
for _, si := range db.ServiceInstances {
219-
if si.ServiceID == "postgrest-api" {
220-
assert.NotEqual(t, "running", si.State,
221-
"service should not reach running when anon role is missing")
222-
}
223-
}
218+
require.Error(t, err, "expected update task to fail due to missing anon role")
219+
assert.Contains(t, err.Error(), "nonexistent_role", "error should mention the missing role")
224220
}
225221

226222
// TestPostgRESTHealthCheck verifies the service responds to HTTP requests once running.

server/internal/orchestrator/swarm/orchestrator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package swarm
33
import (
44
"bytes"
55
"context"
6-
"crypto/sha1"
6+
"crypto/sha256"
77
"errors"
88
"fmt"
99
"io"
@@ -162,9 +162,9 @@ func (o *Orchestrator) GenerateInstanceResources(spec *database.InstanceSpec) (*
162162
// are hashed together into a single 16-char base-36 suffix. The serviceType
163163
// prefix is kept for readability and is truncated to 10 chars to fit comfortably.
164164
func ServiceInstanceName(serviceType, databaseID, serviceID, hostID string) string {
165-
hash := sha1.Sum([]byte(databaseID + ":" + serviceID + ":" + hostID))
165+
hash := sha256.Sum256([]byte(databaseID + ":" + serviceID + ":" + hostID))
166166
base36 := new(big.Int).SetBytes(hash[:]).Text(36)
167-
// Pad to 16 chars (sha1 produces enough entropy; left-pad with zeros if short).
167+
// Pad to 16 chars (sha256 produces enough entropy; left-pad with zeros if short).
168168
suffix := fmt.Sprintf("%016s", base36)[:16]
169169
prefix := serviceType
170170
if len(prefix) > 10 {

0 commit comments

Comments
 (0)