Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: encryption #2667

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ func New(ctx context.Context, conn *sql.DB, config Config, devel bool) (*Service
config.ControllerTimeout = time.Second * 5
}

encryptionSrv, err := encryption.New(ctx, conn, ftlencryption.NewBuilder().WithKMSURI(optional.Ptr(config.KMSURI)))
encryption, err := encryption.New(ctx, conn, ftlencryption.NewBuilder().WithKMSURI(optional.Ptr(config.KMSURI)))
if err != nil {
return nil, fmt.Errorf("failed to create encryption dal: %w", err)
}

db := dal.New(ctx, conn, encryptionSrv)
db := dal.New(ctx, conn, encryption)
ldb := leasesdal.New(conn)
svc := &Service{
tasks: scheduledtask.New(ctx, key, ldb),
Expand All @@ -253,7 +253,7 @@ func New(ctx context.Context, conn *sql.DB, config Config, devel bool) (*Service
svc.routes.Store(map[string][]dal.Route{})
svc.schema.Store(&schema.Schema{})

cronSvc := cronjobs.New(ctx, key, svc.config.Advertise.Host, conn)
cronSvc := cronjobs.New(ctx, key, svc.config.Advertise.Host, conn, encryption)
svc.cronJobs = cronSvc

pubSub := pubsub.New(ctx, db, svc.tasks, svc)
Expand Down
18 changes: 14 additions & 4 deletions backend/controller/cronjobs/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

"github.com/TBD54566975/ftl/backend/controller/cronjobs/dal"
parentdal "github.com/TBD54566975/ftl/backend/controller/dal"
"github.com/TBD54566975/ftl/backend/controller/encryption"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/cron"
ftlencryption "github.com/TBD54566975/ftl/internal/encryption"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/model"
)
Expand All @@ -21,19 +23,21 @@
key model.ControllerKey
requestSource string
dal dal.DAL
encryption *encryption.Service
clock clock.Clock
}

func New(ctx context.Context, key model.ControllerKey, requestSource string, conn *sql.DB) *Service {
return NewForTesting(ctx, key, requestSource, *dal.New(conn), clock.New())
func New(ctx context.Context, key model.ControllerKey, requestSource string, conn *sql.DB, encryption *encryption.Service) *Service {
return NewForTesting(ctx, key, requestSource, *dal.New(conn, encryption), encryption, clock.New())
}

func NewForTesting(ctx context.Context, key model.ControllerKey, requestSource string, dal dal.DAL, clock clock.Clock) *Service {
func NewForTesting(ctx context.Context, key model.ControllerKey, requestSource string, dal dal.DAL, encryption *encryption.Service, clock clock.Clock) *Service {
svc := &Service{
key: key,
requestSource: requestSource,
dal: dal,
clock: clock,
encryption: encryption,
}
return svc
}
Expand Down Expand Up @@ -172,13 +176,19 @@
nextAttemptForJob = now
}

var encryptedResult ftlencryption.EncryptedAsyncColumn
type Empty struct{}
err = s.encryption.EncryptJSON(Empty{}, &encryptedResult)

Check failure on line 181 in backend/controller/cronjobs/cronjobs.go

View workflow job for this annotation

GitHub Actions / Lint

ineffectual assignment to err (ineffassign)
fmt.Println("TOOOOOOOOOOOODOOOOOOOO")
fmt.Printf("encryptedResult: %v", encryptedResult)

logger.Tracef("Scheduling cron job %q async_call execution at %s", job.Key, nextAttemptForJob)
origin := &parentdal.AsyncOriginCron{CronJobKey: job.Key}
id, err := tx.CreateAsyncCall(ctx, dal.CreateAsyncCallParams{
ScheduledAt: nextAttemptForJob,
Verb: schema.RefKey{Module: job.Verb.Module, Name: job.Verb.Name},
Origin: origin.String(),
Request: []byte(`{}`),
Request: encryptedResult,
})
if err != nil {
return fmt.Errorf("failed to create async call for job %q: %w", job.Key, err)
Expand Down
11 changes: 8 additions & 3 deletions backend/controller/cronjobs/cronjobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/alecthomas/assert/v2"
"github.com/alecthomas/types/either"
"github.com/alecthomas/types/optional"
"github.com/benbjohnson/clock"

"github.com/TBD54566975/ftl/backend/controller/cronjobs/dal"
Expand All @@ -34,10 +35,11 @@ func TestNewCronJobsForModule(t *testing.T) {

key := model.NewControllerKey("localhost", strconv.Itoa(8080+1))
conn := sqltest.OpenForTesting(ctx, t)
dal := dal.New(conn)

encryption, err := encryption.New(ctx, conn, ftlencryption.NewBuilder())
uri := "fake-kms://CK6YwYkBElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEJy4TIQgfCuwxA3ZZgChp_wYARABGK6YwYkBIAE"
encryption, err := encryption.New(ctx, conn, ftlencryption.NewBuilder().WithKMSURI(optional.Some(uri)))
assert.NoError(t, err)
dal := dal.New(conn, encryption)

parentDAL := parentdal.New(ctx, conn, encryption)
moduleName := "initial"
Expand All @@ -52,7 +54,7 @@ func TestNewCronJobsForModule(t *testing.T) {

// Progress so that start_time is valid
clk.Add(time.Second)
cjs := NewForTesting(ctx, key, "test.com", *dal, clk)
cjs := NewForTesting(ctx, key, "test.com", *dal, encryption, clk)
// All jobs need to be scheduled
expectUnscheduledJobs(t, dal, clk, 2)
unscheduledJobs, err := dal.GetUnscheduledCronJobs(ctx, clk.Now())
Expand Down Expand Up @@ -84,6 +86,9 @@ func TestNewCronJobsForModule(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, call.Verb, job.Verb.ToRefKey())
assert.Equal(t, call.Origin.String(), fmt.Sprintf("cron:%s", job.Key))

// err = encryption.DecryptJSON(&call.Request, &schema.CronJobRequest{})
// assert.NoError(t, err)
assert.Equal(t, call.Request, []byte("{}"))
assert.Equal(t, call.QueueDepth, int64(len(jobsToCreate)-i)) // widdling down queue

Expand Down
18 changes: 14 additions & 4 deletions backend/controller/cronjobs/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/alecthomas/types/optional"

"github.com/TBD54566975/ftl/backend/controller/cronjobs/dal/internal/sql"
"github.com/TBD54566975/ftl/backend/controller/encryption"
"github.com/TBD54566975/ftl/backend/controller/observability"
"github.com/TBD54566975/ftl/backend/libdal"
"github.com/TBD54566975/ftl/backend/schema"
Expand All @@ -17,16 +18,25 @@ import (

type DAL struct {
*libdal.Handle[DAL]
db sql.Querier
db sql.Querier
encryption *encryption.Service
}

func New(conn libdal.Connection) *DAL {
return &DAL{
func New(conn libdal.Connection, encryption *encryption.Service) *DAL {
var d *DAL
d = &DAL{
db: sql.New(conn),
Handle: libdal.New(conn, func(h *libdal.Handle[DAL]) *DAL {
return &DAL{Handle: h, db: sql.New(h.Connection)}
return &DAL{
Handle: h,
db: sql.New(h.Connection),
encryption: d.encryption,
}
}),
encryption: encryption,
}

return d
}

func cronJobFromRow(c sql.CronJob, d sql.Deployment) model.CronJob {
Expand Down
2 changes: 2 additions & 0 deletions backend/controller/dal/async_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func (d *DAL) AcquireAsyncCall(ctx context.Context) (call *AsyncCall, leaseCtx c
return nil, ctx, fmt.Errorf("failed to parse origin key %q: %w", row.Origin, err)
}

fmt.Println("row.Request", row.Request)

decryptedRequest, err := d.encryption.Decrypt(&row.Request)
if err != nil {
return nil, ctx, fmt.Errorf("failed to decrypt async call request: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func NewNoOpEncryptor() NoOpEncryptor {
var _ DataEncryptor = NoOpEncryptor{}

func (n NoOpEncryptor) Encrypt(cleartext []byte, dest Encrypted) error {
fmt.Println("no op encryptor")
dest.Set(cleartext)
return nil
}
Expand Down
Loading