104 lines
4.0 KiB
Go
104 lines
4.0 KiB
Go
package store
|
|
|
|
import (
|
|
"bytes"
|
|
"database/sql"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
func (s *Store) LoadUploadGrantRequest(uploadID, operationID, digest string) ([]byte, error) {
|
|
var previous string
|
|
var grant []byte
|
|
if err := s.db.QueryRow(`SELECT request_hash,grant FROM upload_grant_requests WHERE upload_id=? AND operation_id=?`, uploadID, operationID).Scan(&previous, &grant); err != nil {
|
|
return nil, err
|
|
}
|
|
if previous != digest {
|
|
return nil, ErrIdempotencyConflict
|
|
}
|
|
return grant, nil
|
|
}
|
|
|
|
// IssueUploadGrant atomically binds one explicit request identity to one grant.
|
|
// Re-delivery returns the original grant, including its original expiry.
|
|
func (s *Store) IssueUploadGrant(record UploadRecord, operationID, digest string) ([]byte, error) {
|
|
if operationID == "" || digest == "" || record.UploadID == "" || record.State != "granted" || len(record.Grant) == 0 || len(record.Binding) == 0 || len(record.Asset) == 0 || record.ObjectKey == "" || record.Bucket == "" {
|
|
return nil, errors.New("complete upload and grant request identity are required")
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
tx, err := s.db.Begin()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
// Grant replay and first issuance must both stop once the recording has
|
|
// an immutable outcome. Keep this check inside the same SQLite transaction
|
|
// as the grant write so a concurrent outcome cannot authorize a late PUT.
|
|
var recorded sql.NullString
|
|
err = tx.QueryRow(`SELECT recording_status FROM local_v01_call_terminals WHERE upload_id=?`, record.UploadID).Scan(&recorded)
|
|
if err == nil && recorded.Valid {
|
|
return nil, ErrUploadMismatch
|
|
}
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return nil, err
|
|
}
|
|
var previous string
|
|
var grant []byte
|
|
err = tx.QueryRow(`SELECT request_hash,grant FROM upload_grant_requests WHERE upload_id=? AND operation_id=?`, record.UploadID, operationID).Scan(&previous, &grant)
|
|
if err == nil {
|
|
if previous != digest {
|
|
return nil, ErrIdempotencyConflict
|
|
}
|
|
return grant, nil
|
|
}
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return nil, err
|
|
}
|
|
var binding, asset []byte
|
|
var objectKey, state, bucket string
|
|
err = tx.QueryRow(`SELECT binding,asset,object_key,state FROM uploads WHERE upload_id=?`, record.UploadID).Scan(&binding, &asset, &objectKey, &state)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
if record.CreatedAt.IsZero() {
|
|
record.CreatedAt = s.now()
|
|
}
|
|
if _, err := tx.Exec(`INSERT INTO uploads(upload_id,binding,asset,grant,object_key,state,created_at) VALUES(?,?,?,?,?,'granted',?)`, record.UploadID, record.Binding, record.Asset, record.Grant, record.ObjectKey, record.CreatedAt.UTC().Format(time.RFC3339Nano)); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := tx.Exec(`INSERT INTO upload_destinations(upload_id,bucket) VALUES(?,?)`, record.UploadID, record.Bucket); err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := tx.QueryRow(`SELECT bucket FROM upload_destinations WHERE upload_id=?`, record.UploadID).Scan(&bucket); err != nil {
|
|
return nil, err
|
|
}
|
|
if bucket != record.Bucket || state != "granted" || objectKey != record.ObjectKey || !bytes.Equal(binding, record.Binding) || !bytes.Equal(asset, record.Asset) {
|
|
return nil, ErrUploadMismatch
|
|
}
|
|
result, err := tx.Exec(`UPDATE uploads SET grant=? WHERE upload_id=?
|
|
AND NOT EXISTS(SELECT 1 FROM upload_notifications WHERE upload_id=?)
|
|
AND NOT EXISTS(SELECT 1 FROM local_v01_call_terminals f WHERE f.upload_id=? AND f.recording_status IS NOT NULL)`,
|
|
record.Grant, record.UploadID, record.UploadID, record.UploadID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
changed, err := result.RowsAffected()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if changed != 1 {
|
|
return nil, ErrUploadMismatch
|
|
}
|
|
}
|
|
if _, err := tx.Exec(`INSERT INTO upload_grant_requests(upload_id,operation_id,request_hash,grant) VALUES(?,?,?,?)`, record.UploadID, operationID, digest, record.Grant); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
return append([]byte(nil), record.Grant...), nil
|
|
}
|