Skip to content

Commit

Permalink
Add secret key for cli_python
Browse files Browse the repository at this point in the history
  • Loading branch information
bailletced committed Dec 20, 2024
1 parent 0499715 commit 6c5e792
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
4 changes: 2 additions & 2 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ services:
HOST_ADMIN: admin.openchurch.local
ELASTIC_PASSWORD: admin
ELASTICSEARCH_IRI: https://elastic:admin@elasticsearch:9200

SYNCHRO_SECRET_KEY: "secret"
python:
container_name: openchurch_python
build:
Expand All @@ -80,8 +80,8 @@ services:
environment:
SQLITE_DATABASE: local.sqlite
OPENCHURCH_HOST: "https://api.openchurch.local/api"
OPENCHURCH_API_TOKEN: "fake_token"
PYTHONWARNINGS: "ignore:Unverified HTTPS request"
SYNCHRO_SECRET_KEY: "secret"

volumes:
openchurch_db_data: {}
Expand Down
6 changes: 6 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ services:
App\Core\Infrastructure\ElasticSearch\Helper\OfficialElasticSearchHelper:
arguments:
$elasticsearchHost: "%env(ELASTICSEARCH_IRI)%"
App\Field\Infrastructure\Doctrine\DoctrineFieldListener:
arguments:
$synchroSecretKey: "%env(SYNCHRO_SECRET_KEY)%"
App\Community\Infrastructure\Doctrine\DoctrineCommunityListener:
arguments:
$synchroSecretKey: "%env(SYNCHRO_SECRET_KEY)%"

App\Core\Infrastructure\Doctrine\FixDoctrineMigrationTableSchema:
autoconfigure: false
Expand Down
28 changes: 28 additions & 0 deletions migrations/Version20241220160327.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\Uid\Uuid;

final class Version20241220160327 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql(
'INSERT INTO agent (id, name, api_key) VALUES (:id, "CLI_SYNCHRO", :syncroSecretKey)',
[
'id' => Uuid::v7()->toBinary(),
'syncroSecretKey' => $_ENV['SYNCHRO_SECRET_KEY'],
],
);
}

public function down(Schema $schema): void
{
$this->addSql('DELETE FROM agent WHERE name = "CLI_SYNCHRO"');
}
}
2 changes: 1 addition & 1 deletion scripts/synchro.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ class OpenChurchClient(object):
urllib3.disable_warnings(category=urllib3.exceptions.InsecureRequestWarning)
hostname = os.getenv('OPENCHURCH_HOST')
headers = {
'Authorization': 'Bearer ' + os.getenv('OPENCHURCH_API_TOKEN')
'Authorization': 'Bearer ' + os.getenv('SYNCHRO_SECRET_KEY')
}

def create_session(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@
use App\Shared\Domain\Enum\SearchIndex;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Symfony\Bundle\SecurityBundle\Security;

#[AsEntityListener(event: Events::postPersist, method: 'postPersist', entity: Community::class)]
final class DoctrineCommunityListener
{
public function __construct(
private readonly string $synchroSecretKey,
private readonly Security $security,
private readonly SearchHelperInterface $searchHelper,
) {
}

public function postPersist(Community $community): void
{
$agent = $this->security->getUser();
if ($agent->apiKey === $this->synchroSecretKey) {

Check failure on line 29 in src/Community/Infrastructure/Doctrine/DoctrineCommunityListener.php

View workflow job for this annotation

GitHub Actions / tests

Access to an undefined property Symfony\Component\Security\Core\User\UserInterface::$apiKey.
return;
}

$type = $community->getMostTrustableFieldByName(FieldCommunity::TYPE)?->getValue();
if ($type === CommunityType::PARISH->value) {
// A new parish has been inserted
Expand Down
8 changes: 8 additions & 0 deletions src/Field/Infrastructure/Doctrine/DoctrineFieldListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@
use App\Shared\Domain\Enum\SearchIndex;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Symfony\Bundle\SecurityBundle\Security;

#[AsEntityListener(event: Events::postUpdate, method: 'postUpdate', entity: Field::class)]
final class DoctrineFieldListener
{
public function __construct(
private readonly string $synchroSecretKey,
private readonly Security $security,
private readonly SearchHelperInterface $searchHelper,
private readonly CommunityRepositoryInterface $communityRepo,
) {
}

public function postUpdate(Field $field): void
{
$agent = $this->security->getUser();
if ($agent->apiKey === $this->synchroSecretKey) {

Check failure on line 31 in src/Field/Infrastructure/Doctrine/DoctrineFieldListener.php

View workflow job for this annotation

GitHub Actions / tests

Access to an undefined property Symfony\Component\Security\Core\User\UserInterface::$apiKey.
return;
}

if ($field->name === FieldCommunity::NAME->value) {
$this->onFieldNameChange($field);
}
Expand Down

0 comments on commit 6c5e792

Please sign in to comment.