-
Notifications
You must be signed in to change notification settings - Fork 3
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
job: auto-update queryservice allowlist #870
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Wiki; | ||
use Maclof\Kubernetes\Client; | ||
use Maclof\Kubernetes\Models\ConfigMap; | ||
|
||
class UpdateQueryserviceAllowList extends Job | ||
{ | ||
public function handle(Client $k8s) | ||
{ | ||
$allowList = implode( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. took me a moment to parse this on a friday afternoon but this is very elegant! 💖 |
||
PHP_EOL, | ||
array_map( | ||
fn($domain) => "https://{$domain}/query/sparql", | ||
Wiki::all()->pluck('domain')->toArray() | ||
) | ||
); | ||
|
||
$k8s->setNamespace('default'); | ||
$configName = 'queryservice-allowlist'; | ||
$config = $k8s->configMaps()->setFieldSelector([ | ||
'metadata.name' => $configName | ||
])->first(); | ||
|
||
if ($config === null) { | ||
$this->fail( | ||
new \RuntimeException( | ||
"Queryservice config map '{$configName}' does not exist." | ||
) | ||
); | ||
return; | ||
} | ||
|
||
$allowListKey = 'allowlist.txt'; | ||
$allowListStaticKey = 'allowlist-static.txt'; | ||
|
||
$config = $config->toArray(); | ||
if (array_key_exists($allowListStaticKey, $config['data'])) { | ||
$allowList .= PHP_EOL . trim($config['data'][$allowListStaticKey]); | ||
} | ||
$config['data'][$allowListKey] = trim($allowList); | ||
$k8s->configMaps()->update(new ConfigMap($config)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace Tests\Jobs; | ||
|
||
use Tests\TestCase; | ||
use Illuminate\Contracts\Queue\Job; | ||
use App\Jobs\UpdateQueryserviceAllowList; | ||
use App\Wiki; | ||
use Maclof\Kubernetes\Client; | ||
use Http\Adapter\Guzzle7\Client as GuzzleClient; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\Middleware; | ||
use GuzzleHttp\Psr7\Response; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
class UpdateQueryserviceAllowListTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
Wiki::factory()->create(['domain' => 'somesite-5.localhost']); | ||
Wiki::factory()->create(['domain' => 'somesite-6.localhost']); | ||
} | ||
|
||
public function testMissingConfigMapFailure(): void | ||
{ | ||
$mockJob = $this->createMock(Job::class); | ||
$mockJob->expects($this->once()) | ||
->method('fail') | ||
->with(new \RuntimeException( | ||
"Queryservice config map 'queryservice-allowlist' does not exist." | ||
)); | ||
|
||
$job = new UpdateQueryserviceAllowList(); | ||
$job->setJob($mockJob); | ||
|
||
$mock = new MockHandler([ | ||
new Response(200, [], json_encode(['items' => []])), | ||
new Response(200, [], json_encode(['items' => []])) | ||
]); | ||
|
||
$handlerStack = HandlerStack::create($mock); | ||
$mockGuzzle = GuzzleClient::createWithConfig([ | ||
'handler' => $handlerStack, | ||
'verify' => '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt' | ||
]); | ||
|
||
$job->handle(new Client([ | ||
'master' => 'https://kubernetes.default.svc', | ||
'token' => '/var/run/secrets/kubernetes.io/serviceaccount/token' | ||
], null, $mockGuzzle)); | ||
} | ||
|
||
public function testSuccess(): void | ||
{ | ||
$mockJob = $this->createMock(Job::class); | ||
$mockJob->expects($this->never())->method('fail'); | ||
|
||
$job = new UpdateQueryserviceAllowList(); | ||
$job->setJob($mockJob); | ||
|
||
$mock = new MockHandler([ | ||
new Response(200, [], json_encode(['items' => [[ | ||
'kind' => 'ConfigMap', | ||
'apiVersion' => 'v1', | ||
'data' => [ | ||
'allowlist-static.txt' => implode(PHP_EOL, [ | ||
'https://somesite-1.localhost/query/sparql', | ||
'https://somesite-2.localhost/query/sparql' | ||
]), | ||
'allowlist.txt' => implode(PHP_EOL, [ | ||
'https://somesite-3.localhost/query/sparql', | ||
'https://somesite-4.localhost/query/sparql' | ||
]) | ||
] | ||
]]])), | ||
new Response(200, [], json_encode(['items' => []])) | ||
]); | ||
|
||
$requests = []; | ||
$handlerStack = HandlerStack::create($mock); | ||
$handlerStack->push(Middleware::history($requests)); | ||
$mockGuzzle = GuzzleClient::createWithConfig([ | ||
'handler' => $handlerStack, | ||
'verify' => '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt' | ||
]); | ||
|
||
$job->handle(new Client([ | ||
'master' => 'https://kubernetes.default.svc', | ||
'token' => '/var/run/secrets/kubernetes.io/serviceaccount/token' | ||
], null, $mockGuzzle)); | ||
|
||
$this->assertCount(2, $requests); | ||
$this->assertEquals( | ||
[ | ||
'kind' => 'ConfigMap', | ||
'apiVersion' => 'v1', | ||
'data' => [ | ||
'allowlist-static.txt' => implode(PHP_EOL, [ | ||
'https://somesite-1.localhost/query/sparql', | ||
'https://somesite-2.localhost/query/sparql' | ||
]), | ||
'allowlist.txt' => implode(PHP_EOL, [ | ||
'https://somesite-5.localhost/query/sparql', | ||
'https://somesite-6.localhost/query/sparql', | ||
'https://somesite-1.localhost/query/sparql', | ||
'https://somesite-2.localhost/query/sparql' | ||
]) | ||
] | ||
], | ||
json_decode($requests[1]['request']->getBody(), true) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can run this even nightly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but I don't think we plan on restarting the query service more than once a week.