Disconnect PKI workers from upstreams when they are no longer authorized (#2515)
This commit is contained in:
19 files changed
+966
-150
No files matched your search
@@ -30,4 +30,12 @@ const (
|
||||
)
|
||||
select * from worker_auth_authorized where worker_id in (select * from key_id_to_worker_id)
|
||||
`
|
||||
|
||||
authorizedWorkerQuery = `
|
||||
select distinct w.worker_key_identifier
|
||||
from
|
||||
worker_auth_certificate_bundle as w
|
||||
where
|
||||
w.worker_key_identifier in (?)
|
||||
`
|
||||
)
|
||||
@@ -520,6 +520,33 @@ func (r *WorkerAuthRepositoryStorage) findCertBundles(ctx context.Context, worke
|
||||
return certBundle, nil
|
||||
}
|
||||
|
||||
// FilterToAuthorizedWorkerKeyIds returns all the worker key identifiers that
|
||||
// are authorizable from the slice of key identifiers provided to the function.
|
||||
func (r *WorkerAuthRepositoryStorage) FilterToAuthorizedWorkerKeyIds(ctx context.Context, workerKeyIds []string) ([]string, error) {
|
||||
const op = "server.(WorkerAuthRepositoryStorage).FilterToAuthorizedWorkerKeyIds"
|
||||
if len(workerKeyIds) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
rows, err := r.reader.Query(ctx, authorizedWorkerQuery, []interface{}{workerKeyIds})
|
||||
if err != nil {
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
type rowsResult struct {
|
||||
WorkerKeyIdentifier string
|
||||
}
|
||||
var ret []string
|
||||
for rows.Next() {
|
||||
var result rowsResult
|
||||
err = r.reader.ScanRows(ctx, rows, &result)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
ret = append(ret, result.WorkerKeyIdentifier)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (r *WorkerAuthRepositoryStorage) loadRootCertificates(ctx context.Context, cert *types.RootCertificates) error {
|
||||
const op = "server.(WorkerAuthRepositoryStorage).loadRootCertificates"
|
||||
if cert == nil {
|
||||
|
||||
@@ -450,6 +450,49 @@ func TestStoreNodeInformationTx(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterToAuthorizedWorkerKeyIds(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
rootWrapper := db.TestWrapper(t)
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
kmsCache := kms.TestKms(t, conn, rootWrapper)
|
||||
|
||||
// Ensures the global scope contains a valid root key
|
||||
require.NoError(t, kmsCache.CreateKeys(context.Background(), scope.Global.String(), kms.WithRandomReader(rand.Reader)))
|
||||
|
||||
rw := db.New(conn)
|
||||
repo, err := NewRepositoryStorage(ctx, rw, rw, kmsCache)
|
||||
require.NoError(t, err)
|
||||
got, err := repo.FilterToAuthorizedWorkerKeyIds(ctx, []string{})
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, got)
|
||||
|
||||
var keyId1 string
|
||||
w1 := TestPkiWorker(t, conn, rootWrapper, WithTestPkiWorkerAuthorizedKeyId(&keyId1))
|
||||
var keyId2 string
|
||||
_ = TestPkiWorker(t, conn, rootWrapper, WithTestPkiWorkerAuthorizedKeyId(&keyId2))
|
||||
|
||||
got, err = repo.FilterToAuthorizedWorkerKeyIds(ctx, []string{"not-found-key-id", keyId1})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []string{keyId1}, got)
|
||||
|
||||
got, err = repo.FilterToAuthorizedWorkerKeyIds(ctx, []string{keyId2, "not-found-key-id"})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []string{keyId2}, got)
|
||||
|
||||
got, err = repo.FilterToAuthorizedWorkerKeyIds(ctx, []string{keyId1, keyId2, "unfound-key"})
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, []string{keyId1, keyId2}, got)
|
||||
|
||||
workerRepo, err := NewRepository(rw, rw, kmsCache)
|
||||
require.NoError(t, err)
|
||||
_, err = workerRepo.DeleteWorker(ctx, w1.GetPublicId())
|
||||
require.NoError(t, err)
|
||||
|
||||
got, err = repo.FilterToAuthorizedWorkerKeyIds(ctx, []string{keyId1, keyId2, "unfound-key"})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []string{keyId2}, got)
|
||||
}
|
||||
|
||||
type mockTestWrapper struct {
|
||||
wrapping.Wrapper
|
||||
decryptError bool
|
||||
|
||||
Reference in new issue
Block a user