Skip to content

Commit

Permalink
DDPB-3703 move to graviton based single cache resource per account (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrwarren authored Nov 15, 2023
1 parent 63e5a25 commit 178161a
Show file tree
Hide file tree
Showing 40 changed files with 309 additions and 93 deletions.
1 change: 1 addition & 0 deletions api/app/api.env
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ SSM_LOCALSTACK=true
SECRETS_MANAGER_LOCALSTACK=true

JWT_HOST=http://frontend-webserver
WORKSPACE=local
2 changes: 2 additions & 0 deletions api/app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ parameters:
version: 'latest'
region: 'eu-west-1'
validate: true

workspace: local
1 change: 1 addition & 0 deletions api/app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ services:
$projectDir: "%kernel.project_dir%"
$redis: "@snc_redis.default"
$frontendHost: "%env(JWT_HOST)%"
$workspace: "%workspace%"

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
Expand Down
1 change: 1 addition & 0 deletions api/app/config/services_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
bind:
$fixtureParams: "%fixtures%"
$symfonyEnvironment: "%kernel.environment%"
$workspace: "%workspace%"

App\Service\BruteForce\AttemptsInTimeChecker:
public: true
Expand Down
5 changes: 3 additions & 2 deletions api/app/src/Controller/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function __construct(
private RestFormatter $restFormatter,
private JWTService $JWTService,
private LoggerInterface $logger,
private TokenStorageInterface $tokenStorage
private TokenStorageInterface $tokenStorage,
private string $workspace
) {
}

Expand Down Expand Up @@ -53,7 +54,7 @@ public function login(
$em->flush();

// Now doing this inline rather than injecting RedisUserProvider
$authToken = $user->getId().'_'.sha1(microtime().spl_object_hash($user).rand(1, 999));
$authToken = $this->workspace.'_'.$user->getId().'_'.sha1(microtime().spl_object_hash($user).rand(1, 999));
$redis->set($authToken, serialize($this->tokenStorage->getToken()));

// add token into response
Expand Down
7 changes: 4 additions & 3 deletions api/app/src/Security/RedisUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public function __construct(
private Client $redis,
private LoggerInterface $logger,
private array $options,
private UserRepository $userRepository)
{
private UserRepository $userRepository,
private string $workspace
) {
$this->timeoutSeconds = $options['timeout_seconds'];
}

Expand All @@ -34,7 +35,7 @@ public function __construct(
*/
public function generateRandomTokenAndStore(User $user)
{
$token = $user->getId().'_'.sha1(microtime().spl_object_hash($user).rand(1, 999));
$token = $this->workspace.'_'.$user->getId().'_'.sha1(microtime().spl_object_hash($user).rand(1, 999));

$this->redis->set($token, $user->getId());
$this->redis->expire($token, $this->timeoutSeconds);
Expand Down
10 changes: 8 additions & 2 deletions api/app/src/Service/BruteForce/AttemptsInTimeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@ class AttemptsInTimeChecker
*/
private $redisPrefix;

public function __construct(PredisClient $redis, $prefix = null)
/**
* @var string
*/
private $workspace;

public function __construct(PredisClient $redis, $workspace, $prefix = null)
{
$this->redis = $redis;
$this->redisPrefix = $prefix;
$this->triggers = [];
$this->workspace = $workspace;
}

public function setRedisPrefix($redisPrefix)
{
$this->redisPrefix = $redisPrefix;
$this->redisPrefix = $this->workspace.'_'.$redisPrefix;

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@ class AttemptsIncrementalWaitingChecker

private array $secondsBeforeNextAttempt;

public function __construct(PredisClient $redis, $prefix = null)
/**
* @var string
*/
private $workspace;

public function __construct(PredisClient $redis, $workspace, $prefix = null)
{
$this->redis = $redis;
$this->redisPrefix = $prefix;
$this->freezeRules = [];
$this->secondsBeforeNextAttempt = [];
$this->workspace = $workspace;
}

public function setRedisPrefix($redisPrefix)
{
$this->redisPrefix = $redisPrefix;
$this->redisPrefix = $this->workspace.'_'.$redisPrefix;

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions api/app/tests/Unit/Service/Auth/UserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void
$this->logger = m::stub('Symfony\Bridge\Monolog\Logger');
$options = ['timeout_seconds' => 7];

$this->userProvider = new RedisUserProvider($this->em, $this->redis, $this->logger, $options, $this->repo);
$this->userProvider = new RedisUserProvider($this->em, $this->redis, $this->logger, $options, $this->repo, 'testing');
}

public function testloadUserByUsernameRedisNotFound()
Expand Down Expand Up @@ -75,7 +75,7 @@ public function testgenerateRandomTokenAndStore()
'getId' => 123,
]);

$tokenMatchPattern = '/^123_[0-9a-f]{5,40}[\d]{1,}/';
$tokenMatchPattern = '/^testing_123_[0-9a-f]{5,40}[\d]{1,}/';

$this->redis->shouldReceive('set')->with(\Mockery::pattern($tokenMatchPattern), 123)->atLeast(1);
$this->redis->shouldReceive('expire')->with(\Mockery::pattern($tokenMatchPattern), 7)->atLeast(1);
Expand Down
1 change: 1 addition & 0 deletions api/docker/app/confd/conf.d/parameters.hml.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ keys = [
"/smtp",
"/ssm/localstack",
"/url",
"/workspace"
]
3 changes: 2 additions & 1 deletion api/docker/app/confd/templates/parameters.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ parameters:
database_ssl_mode: {{ getv "/database/ssl" }}
locale: en
secret: {{ getv "/secret" }}
redis_dsn: '{{getv "/redis/dsn" }}'
redis_dsn: '{{ getv "/redis/dsn" }}'
log_level: warning
verbose_log_level: notice
log_path: /var/log/app/application.log
workspace: {{ getv "/workspace" }}
2 changes: 1 addition & 1 deletion client/app/admin.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
API_CLIENT_SECRET=api-admin-key
ROLE=admin
SESSION_REDIS_DSN=redis://redis-frontend
SESSION_PREFIX=dd_session_admin
SESSION_PREFIX=dd_admin

NGINX_APP_NAME=admin

Expand Down
2 changes: 1 addition & 1 deletion client/app/config/packages/snc_redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ snc_redis:
dsn: "%redis_dsn%"
session:
client: default
prefix: "%session_prefix%"
prefix: "%workspace%_%session_prefix%"
1 change: 1 addition & 0 deletions client/app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ parameters:
htmltopdf_address: "http://htmltopdf:80"
pa_pro_report_csv_filename: pa_pro_report.csv
lay_report_csv_filename: layDeputyReport.csv
workspace: local
1 change: 1 addition & 0 deletions client/app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ services:
$hostedEnv: "%env(ENVIRONMENT)%"
$projectDir: "%kernel.project_dir%"
$sessionPrefix: "%session_prefix%"
$workspace: "%workspace%"

App\:
resource: "../src/"
Expand Down
4 changes: 3 additions & 1 deletion client/app/frontend.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SESSION_COOKIE_SECURE=false

ROLE=front
SESSION_REDIS_DSN=redis://redis-frontend
SESSION_PREFIX=dd_session_front
SESSION_PREFIX=dd_front

GA_DEFAULT=UA-57312200-1
GA_GDS=
Expand Down Expand Up @@ -63,3 +63,5 @@ ENVIRONMENT=local

SSM_LOCALSTACK=true
SECRETS_MANAGER_LOCALSTACK=true

WORKSPACE=local
31 changes: 18 additions & 13 deletions client/app/src/Command/ProcessLayCSVCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Throwable;

class ProcessLayCSVCommand extends Command {
class ProcessLayCSVCommand extends Command
{
protected static $defaultName = 'digideps:process-lay-csv';

private const CHUNK_SIZE = 50;
Expand All @@ -38,11 +38,13 @@ public function __construct(
private Mailer $mailer,
private LoggerInterface $logger,
private ClientInterface $redis,
private string $workspace
) {
parent::__construct();
}

protected function configure(): void {
protected function configure(): void
{
$this
->setDescription('Process the Lay Deputies CSV from the S3 bucket')
->addArgument('email', InputArgument::REQUIRED, 'Email address to send results to');
Expand All @@ -57,7 +59,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->s3->getObject([
'Bucket' => $bucket,
'Key' => $layReportFile,
'SaveAs' => "/tmp/layReport.csv"
'SaveAs' => '/tmp/layReport.csv',
]);
} catch (S3Exception $e) {
if (in_array($e->getAwsErrorCode(), S3Storage::MISSING_FILE_AWS_ERROR_CODES)) {
Expand All @@ -67,17 +69,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$data = $this->csvToArray("/tmp/layReport.csv");
$data = $this->csvToArray('/tmp/layReport.csv');
$this->process($data, $input->getArgument('email'));

if (!unlink("/tmp/layReport.csv")) {
if (!unlink('/tmp/layReport.csv')) {
$this->logger->log('error', 'Unable to delete file /tmp/layReport.csv.');
}

return 0;
}

private function csvToArray(string $fileName) {
private function csvToArray(string $fileName)
{
try {
return (new CsvToArray($fileName, false, false))
->setOptionalColumns([
Expand All @@ -100,17 +103,18 @@ private function csvToArray(string $fileName) {
])
->setUnexpectedColumns(['LastReportDay', 'DeputyOrganisation'])
->getData();
} catch (Throwable $e) {
} catch (\Throwable $e) {
$this->logger->log('error', sprintf('Error processing CSV file: %s', $e->getMessage()));
}
}

private function process(mixed $data, string $email) {
private function process(mixed $data, string $email)
{
$this->restClient->delete('/pre-registration/delete');

$chunks = array_chunk($data, self::CHUNK_SIZE);

$this->redis->set('lay-csv-processing', 'processing');
$this->redis->set($this->workspace.'-lay-csv-processing', 'processing');

foreach ($chunks as $index => $chunk) {
$compressedChunk = CsvUploader::compressData($chunk);
Expand All @@ -121,13 +125,14 @@ private function process(mixed $data, string $email) {
$this->storeOutput($upload);
}

$this->redis->set('lay-csv-processing', 'completed');
$this->redis->set('lay-csv-completed-date', date('Y-m-d H:i:s'));
$this->redis->set($this->workspace.'-lay-csv-processing', 'completed');
$this->redis->set($this->workspace.'-lay-csv-completed-date', date('Y-m-d H:i:s'));

$this->mailer->sendProcessLayCSVEmail($email, $this->output);
}

private function storeOutput(array $output) {
private function storeOutput(array $output)
{
if (!empty($output['errors'])) {
$this->output['errors'] = array_merge($this->output['errors'], $output['errors']);
}
Expand Down
7 changes: 4 additions & 3 deletions client/app/src/Command/ProcessOrgCSVCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function __construct(
private Mailer $mailer,
private LoggerInterface $logger,
private ClientInterface $redis,
private string $workspace
) {
parent::__construct();
}
Expand Down Expand Up @@ -137,7 +138,7 @@ private function process(mixed $data, string $email)
{
$chunks = array_chunk($data, self::CHUNK_SIZE);

$this->redis->set('org-csv-processing', 'processing');
$this->redis->set($this->workspace.'-org-csv-processing', 'processing');

foreach ($chunks as $index => $chunk) {
try {
Expand All @@ -156,8 +157,8 @@ private function process(mixed $data, string $email)

$this->logger->log('notice', 'Successfully processed all chunks');

$this->redis->set('org-csv-processing', 'completed');
$this->redis->set('org-csv-completed-date', date('Y-m-d H:i:s'));
$this->redis->set($this->workspace.'-org-csv-processing', 'completed');
$this->redis->set($this->workspace.'-org-csv-completed-date', date('Y-m-d H:i:s'));

$this->mailer->sendProcessOrgCSVEmail($email, $this->output);
}
Expand Down
Loading

0 comments on commit 178161a

Please sign in to comment.