-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #565 from RoboJackets/ross/grouper
Add Grouper Support
- Loading branch information
Showing
10 changed files
with
276 additions
and
5 deletions.
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
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,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Services\Grouper; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class SyncGrouper extends SyncJob | ||
{ | ||
/** | ||
* The queue this job will run on. | ||
* | ||
* @var string | ||
*/ | ||
public $queue = 'grouper'; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param string $username The user's GT username | ||
* @param bool $is_access_active Whether the user should have access to systems | ||
* @param array<string> $teams The names of the teams the user is in | ||
*/ | ||
protected function __construct(string $username, bool $is_access_active, array $teams) | ||
{ | ||
parent::__construct($username, '', '', $is_access_active, $teams); | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$membershipResponse = Grouper::getGroupMembershipsForUser($this->username); | ||
$memberships = property_exists($membershipResponse->WsGetMembershipsResults, 'wsMemberships') | ||
? collect($membershipResponse->WsGetMembershipsResults->wsMemberships) : | ||
Collection::empty(); | ||
$filteredMemberships = $memberships->where('membershipType', 'immediate'); | ||
$userGroupFullNames = $filteredMemberships->pluck('groupName'); | ||
$this->debug('User is a direct member of Grouper groups: '.implode(', ', $userGroupFullNames->toArray())); | ||
|
||
$allGroupsResponse = Cache::remember('grouper_groups', 900, static fn (): object => Grouper::getGroups()); | ||
$allGroups = collect($allGroupsResponse->WsFindGroupsResults->groupResults)->filter( | ||
static fn (object $group): bool => ! in_array( | ||
$group->displayExtension, | ||
(array) config('grouper.manual_groups'), | ||
true | ||
) | ||
); | ||
|
||
$userTeams = array_map('strtolower', $this->teams); | ||
$desiredGroups = $allGroups->filter( | ||
static fn (object $group): bool => in_array(strtolower($group->displayExtension), $userTeams, true) | ||
); | ||
|
||
foreach ($allGroups as $group) { | ||
$this->debug('Processing '.$group->name); | ||
$shouldBeGroupMember = $desiredGroups->contains($group); | ||
$currentGroupMember = $userGroupFullNames->contains($group->name); | ||
|
||
if ($this->is_access_active && $shouldBeGroupMember && ! $currentGroupMember) { | ||
// User should be, but is not currently, in the group | ||
$this->debug('Adding user to group '.$group->name); | ||
Grouper::addUserToGroup($group->displayExtension, $this->username); | ||
$this->info('Added user to group '.$group->name); | ||
} elseif ($this->is_access_active && $shouldBeGroupMember) { | ||
// User should be, and is currently, in the group. No action required. | ||
$this->debug('User is already a direct member of group '.$group->name); | ||
} elseif ( | ||
(! $this->is_access_active && $currentGroupMember) || | ||
($this->is_access_active && ! $shouldBeGroupMember && $currentGroupMember) | ||
) { | ||
// Remove the user, either because their access is inactive, or they otherwise shouldn't be a member | ||
$this->debug('Removing user from group '.$group->name); | ||
Grouper::removeUserFromGroup($group->displayExtension, $this->username); | ||
$this->info('Removed user from group '.$group->name); | ||
} elseif ($this->is_access_active && ! $shouldBeGroupMember && ! $currentGroupMember) { | ||
// User is access active, but is not and should not be a member of the group | ||
$this->debug('User is not a direct member of group '.$group->name); | ||
} | ||
} | ||
} | ||
|
||
private function debug(string $message): void | ||
{ | ||
Log::debug($this->jobDetails().$message); | ||
} | ||
|
||
private function info(string $message): void | ||
{ | ||
Log::info($this->jobDetails().$message); | ||
} | ||
|
||
private function jobDetails(): string | ||
{ | ||
return self::class.' GT='.$this->username.' '; | ||
} | ||
} |
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,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
// phpcs:disable SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification | ||
// phpcs:disable Generic.NamingConventions.CamelCapsFunctionName.ScopeNotCamelCaps | ||
|
||
namespace App\Services; | ||
|
||
use GuzzleHttp\Client; | ||
|
||
class Grouper extends Service | ||
{ | ||
/** | ||
* A Guzzle client configured for Grouper. | ||
*/ | ||
private static ?Client $client = null; | ||
|
||
public static function addUserToGroup(string $group_name, string $username): void | ||
{ | ||
$response = self::client()->post( | ||
'groups/'.config('grouper.folder_base_path').':'.$group_name.'/members', | ||
[ | ||
'json' => [ | ||
'WsRestAddMemberLiteRequest' => [ | ||
'subjectId' => $username, | ||
'groupName' => config('grouper.folder_base_path').':'.$group_name, | ||
], | ||
], | ||
] | ||
); | ||
self::expectStatusCodes($response, 200, 201); | ||
} | ||
|
||
public static function removeUserFromGroup(string $group_name, string $username): void | ||
{ | ||
$response = self::client()->post( | ||
'groups/'.config( | ||
'grouper.folder_base_path' | ||
).':'.$group_name.'/members/sources/gted-accounts/subjectId/'.$username, | ||
[ | ||
'json' => ['WsRestDeleteMemberLiteRequest' => new \stdClass()], | ||
] | ||
); | ||
self::expectStatusCodes($response, 200); | ||
} | ||
|
||
public static function getGroupMembershipsForUser(string $username): object | ||
{ | ||
$response = self::client()->get('subjects/'.$username.'/memberships'); | ||
|
||
self::expectStatusCodes($response, 200); | ||
|
||
return self::decodeToObject($response); | ||
} | ||
|
||
/** | ||
* Returns all groups in the RoboJackets Grouper hierarchy. | ||
*/ | ||
public static function getGroups(): object | ||
{ | ||
$response = self::client()->post( | ||
'groups', | ||
[ | ||
'json' => [ | ||
'WsRestFindGroupsLiteRequest' => [ | ||
'stemName' => config('grouper.folder_base_path'), | ||
'queryFilterType' => 'FIND_BY_STEM_NAME', | ||
], | ||
], | ||
] | ||
); | ||
|
||
self::expectStatusCodes($response, 200); | ||
|
||
return self::decodeToObject($response); | ||
} | ||
|
||
/** | ||
* Return a client configured for Grouper. | ||
* | ||
* The load balancer in front of Grouper isn't sending a complete (or correct) certificate chain | ||
* as of 2024/09/15, so we have to manually specify the intermediate certificate from InCommon. | ||
*/ | ||
public static function client(): Client | ||
{ | ||
self::$client = new Client( | ||
[ | ||
'base_uri' => 'https://'.config('grouper.server').'/grouper-ws/servicesRest/v4_0_000/', | ||
'headers' => [ | ||
'User-Agent' => 'RoboJacketsJEDI/'.config('sentry.release'), | ||
], | ||
'auth' => [config('grouper.username'), config('grouper.password')], | ||
'allow_redirects' => false, | ||
'http_errors' => true, | ||
'verify' => '/app/storage/certs/InCommon_RSA_Server_CA_2.cer', | ||
] | ||
); | ||
|
||
return self::$client; | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
return [ | ||
'enabled' => env('GROUPER_ENABLED', false), | ||
'server' => env('GROUPER_SERVER'), | ||
'username' => env('GROUPER_USERNAME'), | ||
'password' => env('GROUPER_PASSWORD'), | ||
'folder_base_path' => env('GROUPER_FOLDER_BASE_PATH'), | ||
'manual_groups' => explode(',', env('GROUPER_MANUAL_GROUPS', '')), | ||
]; |
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 |
---|---|---|
|
@@ -182,6 +182,7 @@ | |
'sums', | ||
'wordpress', | ||
'keycloak', | ||
'grouper', | ||
], | ||
'balance' => 'simple', | ||
'processes' => 1, | ||
|
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
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,36 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIGSjCCBDKgAwIBAgIRAINbdhUgbS1uCX4LbkCf78AwDQYJKoZIhvcNAQEMBQAw | ||
gYgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpOZXcgSmVyc2V5MRQwEgYDVQQHEwtK | ||
ZXJzZXkgQ2l0eTEeMBwGA1UEChMVVGhlIFVTRVJUUlVTVCBOZXR3b3JrMS4wLAYD | ||
VQQDEyVVU0VSVHJ1c3QgUlNBIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTIy | ||
MTExNjAwMDAwMFoXDTMyMTExNTIzNTk1OVowRDELMAkGA1UEBhMCVVMxEjAQBgNV | ||
BAoTCUludGVybmV0MjEhMB8GA1UEAxMYSW5Db21tb24gUlNBIFNlcnZlciBDQSAy | ||
MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAifBcxDi60DRXr5dVoPQi | ||
Q/w+GBE62216UiEGMdbUt7eSiIaFj/iZ/xiFop0rWuH4BCFJ3kSvQF+aIhEsOnuX | ||
R6mViSpUx53HM5ApIzFIVbd4GqY6tgwaPzu/XRI/4Dmz+hoLW/i/zD19iXvS95qf | ||
NU8qP7/3/USf2/VNSUNmuMKlaRgwkouue0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